Index: components/browser_watcher/postmortem_minidump_writer.h |
diff --git a/components/browser_watcher/postmortem_minidump_writer.h b/components/browser_watcher/postmortem_minidump_writer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1c59c65d61c2a90f24d747e8f8a53acac60946ba |
--- /dev/null |
+++ b/components/browser_watcher/postmortem_minidump_writer.h |
@@ -0,0 +1,149 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_BROWSER_WATCHER_POSTMORTEM_MINIDUMP_WRITER_H_ |
+#define COMPONENTS_BROWSER_WATCHER_POSTMORTEM_MINIDUMP_WRITER_H_ |
+ |
+#include <stdint.h> |
+ |
+#include <map> |
+#include <memory> |
+#include <string> |
+#include <type_traits> |
+#include <vector> |
+ |
+#include "base/files/file.h" |
+#include "base/macros.h" |
+#include "base/strings/string_piece.h" |
+#include "components/browser_watcher/stability_report.pb.h" |
+#include "third_party/crashpad/crashpad/minidump/minidump_extensions.h" |
+#include "third_party/crashpad/crashpad/util/misc/uuid.h" |
+ |
+namespace browser_watcher { |
+ |
+// A class with functionality for writing minimal minidump containers to wrap |
+// postmortem stability reports. |
+// TODO(manzagop): remove this class once CrashPad takes over writing postmortem |
+// minidumps. |
+// TODO(manzagop): revisit where the module information should be transported, |
+// in the protocol buffer or in a module stream. |
+class PostmortemMinidumpWriter { |
scottmg
2016/09/13 18:07:43
This seems like it could just be a WriteDump() fun
manzagop (departed)
2016/09/13 21:19:41
Done.
|
+ public: |
+ struct ReportInfo; |
+ |
+ PostmortemMinidumpWriter() = default; |
+ ~PostmortemMinidumpWriter() = default; |
+ |
+ // Write to |minidump_file| a minimal minidump that wraps |report|. Returns |
+ // true on success, false otherwise. |
+ // Note: the caller owns |minidump_file| and is responsible for keeping it |
+ // valid for this object's lifetime. |minidump_file| is expected to be empty |
+ // and a binary stream. |
+ bool WriteDump(base::PlatformFile minidump_file, |
+ const StabilityReport& report, |
+ const ReportInfo& report_info); |
+ |
+ private: |
+ // An offset within a minidump file. Note: using this type to avoid including |
+ // windows.h and relying on the RVA type. |
+ using FilePosition = uint32_t; |
+ |
+ // The minidump header is always located at the head. |
+ static const FilePosition kHeaderPos = 0U; |
+ |
+ void Initialize(); |
+ |
+ bool AppendCrashpadInfo(const crashpad::UUID& client_id, |
+ const crashpad::UUID& report_id, |
+ const std::map<std::string, std::string>& crash_keys); |
+ |
+ bool AppendCrashpadDictionaryEntry( |
+ const std::string& key, |
+ const std::string& value, |
+ std::vector<crashpad::MinidumpSimpleStringDictionaryEntry>* entries); |
+ |
+ // Allocate |size_bytes| within the minidump. On success, |pos| contains the |
+ // location of the allocation. Returns true on success, false otherwise. |
+ bool Allocate(size_t size_bytes, FilePosition* pos); |
+ |
+ // Seeks |cursor_|. The seek operation is kept separate from the write in |
+ // order to make the call explicit. Seek operations can be costly and should |
+ // be avoided. |
+ bool SeekCursor(FilePosition destination); |
+ |
+ // Write to pre-allocated space. |
+ // Note: |pos| must match |cursor_|. |
+ template <class DataType> |
+ bool Write(FilePosition pos, const DataType& data); |
+ bool WriteBytes(FilePosition pos, size_t size_bytes, const char* data); |
+ |
+ // Allocate space for and write the contents of |data|. On success, |pos| |
+ // contains the location of the write. Returns true on success, false |
+ // otherwise. |
+ template <class DataType> |
+ bool Append(const DataType& data, FilePosition* pos); |
+ template <class DataType> |
+ bool AppendVec(const std::vector<DataType>& data, FilePosition* pos); |
+ bool AppendUtf8String(base::StringPiece data, FilePosition* pos); |
+ bool AppendBytes(base::StringPiece data, FilePosition* pos); |
+ |
+ void RegisterDirectoryEntry(uint32_t stream_type, |
+ FilePosition pos, |
+ uint32_t size); |
+ |
+ // The next allocatable FilePosition. |
+ FilePosition next_available_byte_; |
+ |
+ // Storage for the directory during writes. |
+ std::vector<MINIDUMP_DIRECTORY> directory_; |
+ |
+ // The file to write to. Only valid within the scope of a call to WriteDump. |
+ base::File* minidump_file_; |
Sigurður Ásgeirsson
2016/09/13 17:36:56
Do you need to null-initialize this in a construct
manzagop (departed)
2016/09/13 21:19:41
No you're right. =default is equivalent to {} exce
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(PostmortemMinidumpWriter); |
+}; |
+ |
+// Information pertaining to the report and required by CrashPad. |
+struct PostmortemMinidumpWriter::ReportInfo { |
+ crashpad::UUID client_id; // The client's identifier. |
+ crashpad::UUID report_id; // The report's identifier. |
+ std::string product_name; // The product name to be used by the reporter. |
+ std::string version_number; // The product's version number. |
+}; |
+ |
+template <class DataType> |
+bool PostmortemMinidumpWriter::Write(FilePosition pos, const DataType& data) { |
+ static_assert(std::is_trivially_copyable<DataType>::value, |
+ "restricted to trivially copyable"); |
+ return WriteBytes(pos, sizeof(data), reinterpret_cast<const char*>(&data)); |
+} |
+ |
+template <class DataType> |
+bool PostmortemMinidumpWriter::Append(const DataType& data, FilePosition* pos) { |
+ static_assert(std::is_trivially_copyable<DataType>::value, |
+ "restricted to trivially copyable"); |
+ DCHECK(pos); |
+ if (!Allocate(sizeof(data), pos)) |
+ return false; |
+ return Write(*pos, data); |
+} |
+ |
+template <class DataType> |
+bool PostmortemMinidumpWriter::AppendVec(const std::vector<DataType>& data, |
+ FilePosition* pos) { |
+ static_assert(std::is_trivially_copyable<DataType>::value, |
+ "restricted to trivially copyable"); |
+ DCHECK(!data.empty()); |
+ DCHECK(pos); |
+ |
+ size_t size_bytes = sizeof(DataType) * data.size(); |
+ if (!Allocate(size_bytes, pos)) |
+ return false; |
+ return WriteBytes(*pos, size_bytes, |
+ reinterpret_cast<const char*>(&data.at(0))); |
+} |
+ |
+} // namespace browser_watcher |
+ |
+#endif // COMPONENTS_BROWSER_WATCHER_POSTMORTEM_MINIDUMP_WRITER_H_ |