Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Side by Side Diff: components/browser_watcher/postmortem_minidump_writer_win.cc

Issue 2128683002: Collect unclean shutdown debug information (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@tracker
Patch Set: Add CrashpadInfo to minidump Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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()
33 : cursor_(0U), minidump_file_(nullptr) {}
34
35 bool PostmortemMinidumpWriter::WriteDump(const StabilityReport& report,
36 const crashpad::UUID& client_id,
37 const crashpad::UUID& report_id,
38 FILE* minidump_file) {
39 DCHECK_NE(nullptr, minidump_file);
40 DCHECK_EQ(0U, cursor_);
41 DCHECK(directory_.empty());
42
43 minidump_file_ = minidump_file;
bcwhite 2016/08/10 16:59:52 Does |minidump_file_| exist just to avoid passing
manzagop (departed) 2016/08/11 13:40:47 It could go either way at this point. Agreed it's
44
45 // Allocate space for the header.
46 Position pos = 0U;
47 if (!Allocate(sizeof(MINIDUMP_HEADER), &pos))
48 return false;
49 DCHECK_EQ(kHeaderPos, pos);
50
51 // Write the proto to the file.
52 std::string serialized_report;
53 report.SerializeToString(&serialized_report);
54 Position report_pos = 0U;
55 if (!AppendBytes(serialized_report, &report_pos))
56 return false;
57
58 // The directory entry for the stability report's stream.
59 {
60 MINIDUMP_DIRECTORY report_directory = {0};
61 report_directory.StreamType = kStabilityReportStreamType;
62 report_directory.Location.Rva = report_pos;
63 report_directory.Location.DataSize = serialized_report.length();
64 directory_.push_back(report_directory);
65 }
66
67 // Write mandatory crash keys. These will be read by crashpad and used as
68 // http request parameters for the upload. Keys and values should match
69 // server side configuration.
70 // TODO(manzagop): use product and version from the stability report. The
71 // current executable's values are an (imperfect) proxy.
72 std::map<std::string, std::string> crash_keys = {
73 {"product", version_info::GetProductName() + "_Postmortem"},
74 {"version", version_info::GetVersionNumber()}};
75 if (!AppendCrashpadInfo(client_id, report_id, crash_keys))
76 return false;
77
78 // Write the directory.
79 Position directory_pos = 0U;
80 if (!AppendVec(directory_, &directory_pos))
81 return false;
82
83 // Write the header.
84 MINIDUMP_HEADER header;
85 header.Signature = MINIDUMP_SIGNATURE;
86 header.Version = MINIDUMP_VERSION;
87 header.NumberOfStreams = directory_.size();
88 header.StreamDirectoryRva = directory_pos;
89 return Write(kHeaderPos, header);
90 }
91
92 bool PostmortemMinidumpWriter::AppendCrashpadInfo(
93 const crashpad::UUID& client_id,
94 const crashpad::UUID& report_id,
95 const std::map<std::string, std::string>& crash_keys) {
96 // Write the crash keys as the contents of a crashpad dictionary.
97 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry> entries;
98 for (const auto& crash_key : crash_keys) {
99 if (!AppendCrashpadDictionaryEntry(crash_key.first, crash_key.second,
100 &entries)) {
101 return false;
102 }
103 }
104
105 // Write the dictionary's index.
106 Position dict_pos = 0U;
107 uint32_t entry_count = entries.size();
108 if (entry_count > 0) {
109 if (!Append(entry_count, &dict_pos))
110 return false;
111 Position unused_pos = 0U;
112 if (!AppendVec(entries, &unused_pos))
113 return false;
114 }
115
116 MINIDUMP_LOCATION_DESCRIPTOR simple_annotations;
117 simple_annotations.DataSize = 0U;
118 if (entry_count > 0) {
119 simple_annotations.DataSize =
120 sizeof(crashpad::MinidumpSimpleStringDictionary) +
121 entry_count * sizeof(crashpad::MinidumpSimpleStringDictionaryEntry);
122 }
123 simple_annotations.Rva = dict_pos;
124
125 // Write the crashpad info.
126 crashpad::MinidumpCrashpadInfo crashpad_info;
127 crashpad_info.version = crashpad::MinidumpCrashpadInfo::kVersion;
128 crashpad_info.report_id = report_id;
129 crashpad_info.client_id = client_id;
130 crashpad_info.simple_annotations = simple_annotations;
131 // Note: module_list is left at 0, which means none.
132
133 Position crashpad_pos = 0U;
134 if (!Append(crashpad_info, &crashpad_pos))
135 return false;
136
137 // Append a directory entry for the crashpad info stream.
138 MINIDUMP_DIRECTORY crashpad_directory = {0};
139 crashpad_directory.StreamType = crashpad::kMinidumpStreamTypeCrashpadInfo;
140 crashpad_directory.Location.Rva = crashpad_pos;
141 crashpad_directory.Location.DataSize = sizeof(crashpad::MinidumpCrashpadInfo);
142 directory_.push_back(crashpad_directory);
143
144 return true;
145 }
146
147 bool PostmortemMinidumpWriter::AppendCrashpadDictionaryEntry(
148 const std::string& key,
149 const std::string& value,
150 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry>* entries) {
151 DCHECK_NE(nullptr, entries);
152
153 Position key_pos = 0U;
154 if (!AppendUtf8String(key, &key_pos))
155 return false;
156 Position value_pos = 0U;
157 if (!AppendUtf8String(value, &value_pos))
158 return false;
159
160 crashpad::MinidumpSimpleStringDictionaryEntry entry = {0};
161 entry.key = key_pos;
162 entry.value = value_pos;
163 entries->push_back(entry);
164
165 return true;
166 }
167
168 bool PostmortemMinidumpWriter::Allocate(size_t size_bytes, Position* pos) {
169 DCHECK(pos);
170 *pos = cursor_;
171 return IncrementCursor(size_bytes);
172 }
173
174 bool PostmortemMinidumpWriter::WriteBytes(Position pos,
175 size_t size_bytes,
176 const void* data) {
177 DCHECK(data);
178 DCHECK_NE(base::kInvalidPlatformFile, minidump_file_);
179
180 // Validate the write does not extend past the cursor.
181 base::CheckedNumeric<Position> pos_end = pos;
182 pos_end += size_bytes;
183 if (!pos_end.IsValid() || pos_end.ValueOrDie() > cursor_)
184 return false;
185
186 // Seek and write.
187 if (fseek(minidump_file_, pos, SEEK_SET) != 0)
188 return false;
189 return fwrite(data, sizeof(char), size_bytes, minidump_file_) == size_bytes;
190 }
191
192 bool PostmortemMinidumpWriter::AppendUtf8String(const std::string data,
193 Position* pos) {
194 DCHECK(pos);
195 uint32_t string_size = data.size();
196 if (!Append(string_size, pos))
197 return false;
198
199 Position unused_pos = 0U;
200 return AppendBytes(data, &unused_pos);
201 }
202
203 bool PostmortemMinidumpWriter::AppendBytes(base::StringPiece data,
204 Position* pos) {
205 DCHECK(pos);
206 if (!Allocate(data.length(), pos))
207 return false;
208 return WriteBytes(*pos, data.length(), data.data());
209 }
210
211 bool PostmortemMinidumpWriter::IncrementCursor(size_t size_bytes) {
212 base::CheckedNumeric<Position> cur = cursor_;
213 cur += size_bytes;
214 if (!cur.IsValid())
215 return false;
216
217 cursor_ += size_bytes;
218 return true;
219 }
220
221 } // namespace browser_watcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698