Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Note: aside from using windows headers to obtain the definitions of minidump | |
| 6 // structures, nothing here is windows specific. This seems like the best | |
| 7 // approach given this code is for tempory experimentation on Windows. | |
|
scottmg
2016/08/03 22:47:06
temporary
manzagop (departed)
2016/08/10 15:59:52
Done.
| |
| 8 // Longer term, CrashPad will take over the minidump writing in this case as | |
| 9 // well. | |
| 10 | |
| 11 #include "components/browser_watcher/postmortem_minidump_writer.h" | |
| 12 | |
| 13 #include <windows.h> // NOLINT | |
| 14 #include <dbghelp.h> | |
| 15 | |
| 16 #include <string> | |
| 17 | |
| 18 #include "base/files/file_util.h" | |
| 19 #include "base/numerics/safe_math.h" | |
| 20 | |
| 21 namespace browser_watcher { | |
| 22 | |
| 23 // The stream type assigned to the minidump stream that holds the serialized | |
| 24 // stability report. | |
| 25 // Note: the value was obtained by adding 1 to the stream type used for holding | |
| 26 // the SyzyAsan proto. | |
| 27 // TODO(manzagop): centralize the stream type definitions to avoid issues. | |
| 28 const uint32_t kStabilityReportStreamType = 0x4B6B0002; | |
|
scottmg
2016/08/03 22:47:06
(Kk02... "K"as"k"o lives on, I guess?
manzagop (departed)
2016/08/10 15:59:52
Yeah, I really had no clue what value to use!
Is
| |
| 29 | |
| 30 PostmortemMinidumpWriter::PostmortemMinidumpWriter() : cursor_(0U) {} | |
| 31 | |
| 32 bool PostmortemMinidumpWriter::WriteDump(const base::FilePath& mindump_path, | |
| 33 const StabilityReport& report) { | |
| 34 // Open the minidump file. | |
| 35 minidump_.reset(base::OpenFile(mindump_path, "wb")); | |
| 36 if (minidump_.get() == nullptr) | |
| 37 return false; | |
|
scottmg
2016/08/03 22:47:06
Is it useful to log on any of these failures? Or i
manzagop (departed)
2016/08/10 15:59:52
Totally! It's a major TODO to sprinkle some UMA re
| |
| 38 | |
| 39 // Allocate space for the header. | |
| 40 Position pos = 0U; | |
| 41 if (!Allocate(sizeof(MINIDUMP_HEADER), &pos)) | |
| 42 return false; | |
| 43 DCHECK_EQ(kHeaderPos, pos); | |
| 44 | |
| 45 // Write the proto to the file. | |
| 46 std::string serialized_report; | |
| 47 report.SerializeToString(&serialized_report); | |
| 48 Position report_pos = 0U; | |
| 49 if (!AppendBytes(serialized_report, &report_pos)) | |
| 50 return false; | |
| 51 | |
| 52 // Write the directory entry for the stability report's stream. | |
|
scottmg
2016/08/03 22:47:06
As Mark described, you'll have to add keys for the
manzagop (departed)
2016/08/10 15:59:52
Done. I added product and version. Hopefully corre
| |
| 53 MINIDUMP_DIRECTORY report_directory = {0}; | |
| 54 report_directory.StreamType = kStabilityReportStreamType; | |
| 55 report_directory.Location.Rva = report_pos; | |
| 56 report_directory.Location.DataSize = serialized_report.length(); | |
| 57 Position directory_pos = 0U; | |
| 58 if (!Append(report_directory, &directory_pos)) | |
| 59 return false; | |
| 60 | |
| 61 // Write the header. | |
| 62 MINIDUMP_HEADER header; | |
| 63 header.Signature = MINIDUMP_SIGNATURE; | |
| 64 header.NumberOfStreams = 1; | |
| 65 header.StreamDirectoryRva = directory_pos; | |
| 66 return Write(kHeaderPos, header); | |
| 67 } | |
| 68 | |
| 69 bool PostmortemMinidumpWriter::Allocate(size_t size_bytes, Position* pos) { | |
| 70 DCHECK(pos); | |
| 71 *pos = cursor_; | |
| 72 return IncrementCursor(size_bytes); | |
| 73 } | |
| 74 | |
| 75 bool PostmortemMinidumpWriter::WriteBytes(Position pos, | |
| 76 size_t size_bytes, | |
| 77 const void* data) { | |
| 78 DCHECK(data); | |
| 79 DCHECK(minidump_.get()); | |
| 80 | |
| 81 // Validate the write does not extend past the cursor. | |
| 82 base::CheckedNumeric<Position> pos_end = pos; | |
| 83 pos_end += size_bytes; | |
| 84 if (!pos_end.IsValid() || pos_end.ValueOrDie() > cursor_) | |
| 85 return false; | |
| 86 | |
| 87 // Seek and write. | |
| 88 if (fseek(minidump_.get(), pos, SEEK_SET) != 0) | |
| 89 return false; | |
| 90 return fwrite(data, sizeof(char), size_bytes, minidump_.get()) == size_bytes; | |
| 91 } | |
| 92 | |
| 93 bool PostmortemMinidumpWriter::AppendBytes(base::StringPiece data, | |
| 94 Position* pos) { | |
| 95 DCHECK(pos); | |
| 96 if (!Allocate(data.length(), pos)) | |
| 97 return false; | |
| 98 return WriteBytes(*pos, data.length(), data.data()); | |
| 99 } | |
| 100 | |
| 101 bool PostmortemMinidumpWriter::IncrementCursor(size_t size_bytes) { | |
| 102 base::CheckedNumeric<Position> cur = cursor_; | |
| 103 cur += size_bytes; | |
| 104 if (!cur.IsValid()) | |
| 105 return false; | |
| 106 | |
| 107 cursor_ += size_bytes; | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 } // namespace browser_watcher | |
| OLD | NEW |