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 temporary experimentation on Windows. | |
| 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 #include "components/version_info/version_info.h" | |
| 21 #include "third_party/crashpad/crashpad/minidump/minidump_extensions.h" | |
| 22 | |
| 23 namespace browser_watcher { | |
| 24 | |
| 25 // The stream type assigned to the minidump stream that holds the serialized | |
| 26 // stability report. | |
| 27 // Note: the value was obtained by adding 1 to the stream type used for holding | |
| 28 // the SyzyAsan proto. | |
| 29 // TODO(manzagop): centralize the stream type definitions to avoid issues. | |
| 30 const uint32_t kStabilityReportStreamType = 0x4B6B0002; | |
| 31 | |
| 32 PostmortemMinidumpWriter::PostmortemMinidumpWriter(FILE* minidump_file) | |
| 33 : cursor_(0U), minidump_file_(minidump_file) {} | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:14
DCHECK or even CHECK minidump_file here?
manzagop (departed)
2016/08/12 21:23:22
Done.
| |
| 34 | |
| 35 bool PostmortemMinidumpWriter::WriteDump(const StabilityReport& report, | |
| 36 const crashpad::UUID& client_id, | |
| 37 const crashpad::UUID& report_id) { | |
| 38 DCHECK_NE(nullptr, minidump_file_); | |
| 39 DCHECK_EQ(0U, cursor_); | |
| 40 DCHECK(directory_.empty()); | |
| 41 | |
| 42 // Allocate space for the header. | |
| 43 Position pos = 0U; | |
| 44 if (!Allocate(sizeof(MINIDUMP_HEADER), &pos)) | |
| 45 return false; | |
| 46 DCHECK_EQ(kHeaderPos, pos); | |
| 47 | |
| 48 // Write the proto to the file. | |
| 49 std::string serialized_report; | |
| 50 report.SerializeToString(&serialized_report); | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:14
Looks like this can fail - check the return value.
manzagop (departed)
2016/08/12 21:23:22
Huh. Looks like it's when messages are too large.
| |
| 51 Position report_pos = 0U; | |
| 52 if (!AppendBytes(serialized_report, &report_pos)) | |
| 53 return false; | |
| 54 | |
| 55 // The directory entry for the stability report's stream. | |
| 56 { | |
| 57 MINIDUMP_DIRECTORY report_directory = {0}; | |
| 58 report_directory.StreamType = kStabilityReportStreamType; | |
| 59 report_directory.Location.Rva = report_pos; | |
| 60 report_directory.Location.DataSize = serialized_report.length(); | |
| 61 directory_.push_back(report_directory); | |
| 62 } | |
| 63 | |
| 64 // Write mandatory crash keys. These will be read by crashpad and used as | |
| 65 // http request parameters for the upload. Keys and values should match | |
| 66 // server side configuration. | |
| 67 // TODO(manzagop): use product and version from the stability report. The | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:14
I think you want to pass the product and version i
manzagop (departed)
2016/08/12 21:23:22
Now passing in product and version.
Is the versio
| |
| 68 // current executable's values are an (imperfect) proxy. | |
| 69 std::map<std::string, std::string> crash_keys = { | |
| 70 {"product", version_info::GetProductName() + "_Postmortem"}, | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:14
Is this crash team's recommended way to disambigua
manzagop (departed)
2016/08/12 21:23:22
Yes, this is what we discussed.
| |
| 71 {"version", version_info::GetVersionNumber()}}; | |
| 72 if (!AppendCrashpadInfo(client_id, report_id, crash_keys)) | |
| 73 return false; | |
| 74 | |
| 75 // Write the directory. | |
| 76 Position directory_pos = 0U; | |
| 77 if (!AppendVec(directory_, &directory_pos)) | |
| 78 return false; | |
| 79 | |
| 80 // Write the header. | |
| 81 MINIDUMP_HEADER header; | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:15
so the module information is solely in the protobu
manzagop (departed)
2016/08/12 21:23:22
I've placed a TODO in the .h to revisit where this
| |
| 82 header.Signature = MINIDUMP_SIGNATURE; | |
| 83 header.Version = MINIDUMP_VERSION; | |
| 84 header.NumberOfStreams = directory_.size(); | |
| 85 header.StreamDirectoryRva = directory_pos; | |
| 86 return Write(kHeaderPos, header); | |
| 87 } | |
| 88 | |
| 89 bool PostmortemMinidumpWriter::AppendCrashpadInfo( | |
| 90 const crashpad::UUID& client_id, | |
| 91 const crashpad::UUID& report_id, | |
| 92 const std::map<std::string, std::string>& crash_keys) { | |
| 93 // Write the crash keys as the contents of a crashpad dictionary. | |
| 94 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry> entries; | |
| 95 for (const auto& crash_key : crash_keys) { | |
| 96 if (!AppendCrashpadDictionaryEntry(crash_key.first, crash_key.second, | |
| 97 &entries)) { | |
| 98 return false; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // Write the dictionary's index. | |
| 103 Position dict_pos = 0U; | |
| 104 uint32_t entry_count = entries.size(); | |
| 105 if (entry_count > 0) { | |
| 106 if (!Append(entry_count, &dict_pos)) | |
| 107 return false; | |
| 108 Position unused_pos = 0U; | |
| 109 if (!AppendVec(entries, &unused_pos)) | |
| 110 return false; | |
| 111 } | |
| 112 | |
| 113 MINIDUMP_LOCATION_DESCRIPTOR simple_annotations; | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:15
a simpler way to do this would be to subtract dict
manzagop (departed)
2016/08/12 21:23:22
Totally! Done.
| |
| 114 simple_annotations.DataSize = 0U; | |
| 115 if (entry_count > 0) { | |
| 116 simple_annotations.DataSize = | |
| 117 sizeof(crashpad::MinidumpSimpleStringDictionary) + | |
| 118 entry_count * sizeof(crashpad::MinidumpSimpleStringDictionaryEntry); | |
| 119 } | |
| 120 simple_annotations.Rva = dict_pos; | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:14
do you want a non-null RVA for an empty list?
manzagop (departed)
2016/08/12 21:23:22
An RVA of 0 signals the absence of a dictionary. A
| |
| 121 | |
| 122 // Write the crashpad info. | |
| 123 crashpad::MinidumpCrashpadInfo crashpad_info; | |
| 124 crashpad_info.version = crashpad::MinidumpCrashpadInfo::kVersion; | |
| 125 crashpad_info.report_id = report_id; | |
| 126 crashpad_info.client_id = client_id; | |
| 127 crashpad_info.simple_annotations = simple_annotations; | |
| 128 // Note: module_list is left at 0, which means none. | |
| 129 | |
| 130 Position crashpad_pos = 0U; | |
| 131 if (!Append(crashpad_info, &crashpad_pos)) | |
| 132 return false; | |
| 133 | |
| 134 // Append a directory entry for the crashpad info stream. | |
| 135 MINIDUMP_DIRECTORY crashpad_directory = {0}; | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:14
maybe pull the directory extension code out to a u
manzagop (departed)
2016/08/12 21:23:22
Done.
| |
| 136 crashpad_directory.StreamType = crashpad::kMinidumpStreamTypeCrashpadInfo; | |
| 137 crashpad_directory.Location.Rva = crashpad_pos; | |
| 138 crashpad_directory.Location.DataSize = sizeof(crashpad::MinidumpCrashpadInfo); | |
| 139 directory_.push_back(crashpad_directory); | |
| 140 | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 bool PostmortemMinidumpWriter::AppendCrashpadDictionaryEntry( | |
| 145 const std::string& key, | |
| 146 const std::string& value, | |
| 147 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry>* entries) { | |
| 148 DCHECK_NE(nullptr, entries); | |
| 149 | |
| 150 Position key_pos = 0U; | |
| 151 if (!AppendUtf8String(key, &key_pos)) | |
| 152 return false; | |
| 153 Position value_pos = 0U; | |
| 154 if (!AppendUtf8String(value, &value_pos)) | |
| 155 return false; | |
| 156 | |
| 157 crashpad::MinidumpSimpleStringDictionaryEntry entry = {0}; | |
| 158 entry.key = key_pos; | |
| 159 entry.value = value_pos; | |
| 160 entries->push_back(entry); | |
| 161 | |
| 162 return true; | |
| 163 } | |
| 164 | |
| 165 bool PostmortemMinidumpWriter::Allocate(size_t size_bytes, Position* pos) { | |
| 166 DCHECK(pos); | |
| 167 *pos = cursor_; | |
| 168 return IncrementCursor(size_bytes); | |
| 169 } | |
| 170 | |
| 171 bool PostmortemMinidumpWriter::WriteBytes(Position pos, | |
| 172 size_t size_bytes, | |
| 173 const void* data) { | |
| 174 DCHECK(data); | |
| 175 DCHECK_NE(base::kInvalidPlatformFile, minidump_file_); | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:15
I don't think this is right.
manzagop (departed)
2016/08/12 21:23:22
Done.
| |
| 176 | |
| 177 // Validate the write does not extend past the cursor. | |
| 178 base::CheckedNumeric<Position> pos_end = pos; | |
| 179 pos_end += size_bytes; | |
| 180 if (!pos_end.IsValid() || pos_end.ValueOrDie() > cursor_) | |
| 181 return false; | |
| 182 | |
| 183 // Seek and write. | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:14
ISTR there's a very non-trivial performance hit to
manzagop (departed)
2016/08/12 21:23:22
Good to know. I've made seeking explicit. Done.
| |
| 184 if (fseek(minidump_file_, pos, SEEK_SET) != 0) | |
| 185 return false; | |
| 186 return fwrite(data, sizeof(char), size_bytes, minidump_file_) == size_bytes; | |
| 187 } | |
| 188 | |
| 189 bool PostmortemMinidumpWriter::AppendUtf8String(const std::string data, | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:15
string&?
manzagop (departed)
2016/08/12 21:23:22
Changed to StringPiece.
| |
| 190 Position* pos) { | |
| 191 DCHECK(pos); | |
| 192 uint32_t string_size = data.size(); | |
| 193 if (!Append(string_size, pos)) | |
| 194 return false; | |
| 195 | |
| 196 Position unused_pos = 0U; | |
| 197 return AppendBytes(data, &unused_pos); | |
| 198 } | |
| 199 | |
| 200 bool PostmortemMinidumpWriter::AppendBytes(base::StringPiece data, | |
| 201 Position* pos) { | |
| 202 DCHECK(pos); | |
| 203 if (!Allocate(data.length(), pos)) | |
| 204 return false; | |
| 205 return WriteBytes(*pos, data.length(), data.data()); | |
| 206 } | |
| 207 | |
| 208 bool PostmortemMinidumpWriter::IncrementCursor(size_t size_bytes) { | |
| 209 base::CheckedNumeric<Position> cur = cursor_; | |
| 210 cur += size_bytes; | |
| 211 if (!cur.IsValid()) | |
| 212 return false; | |
| 213 | |
| 214 cursor_ += size_bytes; | |
| 215 return true; | |
| 216 } | |
| 217 | |
| 218 } // namespace browser_watcher | |
| OLD | NEW |