1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <cstdio>
#include <cstring>
#include <string>
#include <sstream>
#include "json/json.h"
#include "google_breakpad/processor/basic_source_line_resolver.h"
#include "google_breakpad/processor/call_stack.h"
#include "google_breakpad/processor/code_module.h"
#include "google_breakpad/processor/code_modules.h"
#include "google_breakpad/processor/minidump.h"
#include "google_breakpad/processor/minidump_processor.h"
#include "google_breakpad/processor/process_state.h"
#include "google_breakpad/processor/stack_frame.h"
#include "processor/pathname_stripper.h"
#include "mozilla/FStream.h"
#include "mozilla/Unused.h"
#if defined(XP_WIN)
# include <windows.h>
# include "mozilla/glue/WindowsDllServices.h"
#elif defined(XP_UNIX) || defined(XP_MACOSX)
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
#endif
#include "MinidumpAnalyzerUtils.h"
#if XP_WIN && HAVE_64BIT_BUILD && defined(_M_X64)
# include "MozStackFrameSymbolizer.h"
#endif
namespace CrashReporter {
#if defined(XP_WIN)
static mozilla::glue::BasicDllServices gDllServices;
#endif
using std::hex;
using std::ios;
using std::ios_base;
using std::map;
using std::showbase;
using std::string;
using std::stringstream;
using std::wstring;
using google_breakpad::BasicSourceLineResolver;
using google_breakpad::CallStack;
using google_breakpad::CodeModule;
using google_breakpad::CodeModules;
using google_breakpad::Minidump;
using google_breakpad::MinidumpProcessor;
using google_breakpad::PathnameStripper;
using google_breakpad::ProcessResult;
using google_breakpad::ProcessState;
using google_breakpad::StackFrame;
using mozilla::IFStream;
using mozilla::OFStream;
using mozilla::Unused;
MinidumpAnalyzerOptions gMinidumpAnalyzerOptions;
// Path of the minidump to be analyzed.
static string gMinidumpPath;
struct ModuleCompare {
bool operator()(const CodeModule* aLhs, const CodeModule* aRhs) const {
return aLhs->base_address() < aRhs->base_address();
}
};
typedef map<const CodeModule*, unsigned int, ModuleCompare> OrderedModulesMap;
static void AddModulesFromCallStack(OrderedModulesMap& aOrderedModules,
const CallStack* aStack) {
int frameCount = aStack->frames()->size();
for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
const StackFrame* frame = aStack->frames()->at(frameIndex);
if (frame->module) {
aOrderedModules.insert(
std::pair<const CodeModule*, unsigned int>(frame->module, 0));
}
}
}
static void PopulateModuleList(const ProcessState& aProcessState,
OrderedModulesMap& aOrderedModules,
bool aFullStacks) {
int threadCount = aProcessState.threads()->size();
int requestingThread = aProcessState.requesting_thread();
if (!aFullStacks && (requestingThread != -1)) {
AddModulesFromCallStack(aOrderedModules,
aProcessState.threads()->at(requestingThread));
} else {
for (int threadIndex = 0; threadIndex < threadCount; ++threadIndex) {
AddModulesFromCallStack(aOrderedModules,
aProcessState.threads()->at(threadIndex));
}
}
int moduleCount = 0;
for (auto& itr : aOrderedModules) {
itr.second = moduleCount++;
}
}
static const char kExtraDataExtension[] = ".extra";
static string ToHex(uint64_t aValue) {
stringstream output;
output << hex << showbase << aValue;
return output.str();
}
// Convert the stack frame trust value into a readable string.
static string FrameTrust(const StackFrame::FrameTrust aTrust) {
switch (aTrust) {
case StackFrame::FRAME_TRUST_NONE:
return "none";
case StackFrame::FRAME_TRUST_SCAN:
return "scan";
case StackFrame::FRAME_TRUST_CFI_SCAN:
return "cfi_scan";
case StackFrame::FRAME_TRUST_FP:
return "frame_pointer";
case StackFrame::FRAME_TRUST_CFI:
return "cfi";
case StackFrame::FRAME_TRUST_PREWALKED:
return "prewalked";
case StackFrame::FRAME_TRUST_CONTEXT:
return "context";
}
return "none";
}
// Convert the result value of the minidump processing step into a readable
// string.
static string ResultString(ProcessResult aResult) {
switch (aResult) {
case google_breakpad::PROCESS_OK:
return "OK";
case google_breakpad::PROCESS_ERROR_MINIDUMP_NOT_FOUND:
return "ERROR_MINIDUMP_NOT_FOUND";
case google_breakpad::PROCESS_ERROR_NO_MINIDUMP_HEADER:
return "ERROR_NO_MINIDUMP_HEADER";
case google_breakpad::PROCESS_ERROR_NO_THREAD_LIST:
return "ERROR_NO_THREAD_LIST";
case google_breakpad::PROCESS_ERROR_GETTING_THREAD:
return "ERROR_GETTING_THREAD";
case google_breakpad::PROCESS_ERROR_GETTING_THREAD_ID:
return "ERROR_GETTING_THREAD_ID";
case google_breakpad::PROCESS_ERROR_DUPLICATE_REQUESTING_THREADS:
return "ERROR_DUPLICATE_REQUESTING_THREADS";
case google_breakpad::PROCESS_SYMBOL_SUPPLIER_INTERRUPTED:
return "SYMBOL_SUPPLIER_INTERRUPTED";
default:
return "";
}
}
// Convert the list of stack frames to JSON and append them to the array
// specified in the |aNode| parameter.
static void ConvertStackToJSON(const ProcessState& aProcessState,
const OrderedModulesMap& aOrderedModules,
const CallStack* aStack, Json::Value& aNode) {
int frameCount = aStack->frames()->size();
for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
const StackFrame* frame = aStack->frames()->at(frameIndex);
Json::Value frameNode;
if (frame->module) {
const auto& itr = aOrderedModules.find(frame->module);
if (itr != aOrderedModules.end()) {
frameNode["module_index"] = (*itr).second;
}
}
frameNode["trust"] = FrameTrust(frame->trust);
// The 'ip' field is equivalent to socorro's 'offset' field
frameNode["ip"] = ToHex(frame->instruction);
aNode.append(frameNode);
}
}
// Extract the list of certifications subjects from the list of modules and
// store it in the |aCertSubjects| parameter
static void RetrieveCertSubjects(const CodeModules* modules,
Json::Value& aCertSubjects) {
#if defined(XP_WIN)
if (modules) {
for (size_t i = 0; i < modules->module_count(); i++) {
const CodeModule* module = modules->GetModuleAtIndex(i);
auto certSubject = gDllServices.GetBinaryOrgName(
UTF8ToWide(module->code_file()).c_str());
if (certSubject) {
string strSubject(WideToUTF8(certSubject.get()));
// Json::Value::operator[] creates and returns a null member if the key
// does not exist.
Json::Value& subjectNode = aCertSubjects[strSubject];
if (!subjectNode) {
// If the member is null, we want to convert that to an array.
subjectNode = Json::Value(Json::arrayValue);
}
// Now we're guaranteed that subjectNode is an array. Add the new entry.
subjectNode.append(PathnameStripper::File(module->code_file()));
}
}
}
#endif // defined(XP_WIN)
}
// Convert the list of modules to JSON and append them to the array specified
// in the |aNode| parameter.
static int ConvertModulesToJSON(const ProcessState& aProcessState,
const OrderedModulesMap& aOrderedModules,
Json::Value& aNode) {
const CodeModules* modules = aProcessState.modules();
if (!modules) {
return -1;
}
uint64_t mainAddress = 0;
const CodeModule* mainModule = modules->GetMainModule();
if (mainModule) {
mainAddress = mainModule->base_address();
}
int mainModuleIndex = -1;
for (const auto& itr : aOrderedModules) {
const CodeModule* module = itr.first;
if ((module->base_address() == mainAddress) && mainModule) {
mainModuleIndex = itr.second;
}
Json::Value moduleNode;
moduleNode["filename"] = PathnameStripper::File(module->code_file());
moduleNode["code_id"] = PathnameStripper::File(module->code_identifier());
moduleNode["version"] = module->version();
moduleNode["debug_file"] = PathnameStripper::File(module->debug_file());
moduleNode["debug_id"] = module->debug_identifier();
moduleNode["base_addr"] = ToHex(module->base_address());
moduleNode["end_addr"] = ToHex(module->base_address() + module->size());
aNode.append(moduleNode);
}
return mainModuleIndex;
}
// Convert the list of unloaded modules to JSON and append them to the array
// specified in the |aNode| parameter. Return the number of unloaded modules
// that were found.
static size_t ConvertUnloadedModulesToJSON(const ProcessState& aProcessState,
Json::Value& aNode) {
const CodeModules* unloadedModules = aProcessState.unloaded_modules();
if (!unloadedModules) {
return 0;
}
const size_t unloadedModulesLen = unloadedModules->module_count();
for (size_t i = 0; i < unloadedModulesLen; i++) {
const CodeModule* unloadedModule = unloadedModules->GetModuleAtIndex(i);
Json::Value unloadedModuleNode;
unloadedModuleNode["filename"] =
PathnameStripper::File(unloadedModule->code_file());
unloadedModuleNode["code_id"] =
PathnameStripper::File(unloadedModule->code_identifier());
unloadedModuleNode["base_addr"] = ToHex(unloadedModule->base_address());
unloadedModuleNode["end_addr"] =
ToHex(unloadedModule->base_address() + unloadedModule->size());
aNode.append(unloadedModuleNode);
}
return unloadedModulesLen;
}
// Convert the process state to JSON, this includes information about the
// crash, the module list and stack traces for every thread
static void ConvertProcessStateToJSON(const ProcessState& aProcessState,
Json::Value& aStackTraces,
const bool aFullStacks,
Json::Value& aCertSubjects) {
// Crash info
Json::Value crashInfo;
int requestingThread = aProcessState.requesting_thread();
if (aProcessState.crashed()) {
crashInfo["type"] = aProcessState.crash_reason();
crashInfo["address"] = ToHex(aProcessState.crash_address());
if (requestingThread != -1) {
// Record the crashing thread index only if this is a full minidump
// and all threads' stacks are present, otherwise only the crashing
// thread stack is written out and this field is set to 0.
crashInfo["crashing_thread"] = aFullStacks ? requestingThread : 0;
}
} else {
crashInfo["type"] = Json::Value(Json::nullValue);
// Add assertion info, if available
string assertion = aProcessState.assertion();
if (!assertion.empty()) {
crashInfo["assertion"] = assertion;
}
}
aStackTraces["crash_info"] = crashInfo;
// Modules
OrderedModulesMap orderedModules;
PopulateModuleList(aProcessState, orderedModules, aFullStacks);
Json::Value modules(Json::arrayValue);
int mainModule = ConvertModulesToJSON(aProcessState, orderedModules, modules);
if (mainModule != -1) {
aStackTraces["main_module"] = mainModule;
}
aStackTraces["modules"] = modules;
Json::Value unloadedModules(Json::arrayValue);
size_t unloadedModulesLen =
ConvertUnloadedModulesToJSON(aProcessState, unloadedModules);
if (unloadedModulesLen > 0) {
aStackTraces["unloaded_modules"] = unloadedModules;
}
RetrieveCertSubjects(aProcessState.modules(), aCertSubjects);
RetrieveCertSubjects(aProcessState.unloaded_modules(), aCertSubjects);
// Threads
Json::Value threads(Json::arrayValue);
int threadCount = aProcessState.threads()->size();
if (!aFullStacks && (requestingThread != -1)) {
// Only add the crashing thread
Json::Value thread;
Json::Value stack(Json::arrayValue);
const CallStack* rawStack = aProcessState.threads()->at(requestingThread);
ConvertStackToJSON(aProcessState, orderedModules, rawStack, stack);
thread["frames"] = stack;
threads.append(thread);
} else {
for (int threadIndex = 0; threadIndex < threadCount; ++threadIndex) {
Json::Value thread;
Json::Value stack(Json::arrayValue);
const CallStack* rawStack = aProcessState.threads()->at(threadIndex);
ConvertStackToJSON(aProcessState, orderedModules, rawStack, stack);
thread["frames"] = stack;
threads.append(thread);
}
}
aStackTraces["threads"] = threads;
}
// Process the minidump file and append the JSON-formatted stack traces to
// the node specified in |aStackTraces|. We also populate |aCertSubjects| with
// information about the certificates used to sign modules, when present and
// supported by the underlying OS.
static bool ProcessMinidump(Json::Value& aStackTraces,
Json::Value& aCertSubjects, const string& aDumpFile,
const bool aFullStacks) {
#if XP_WIN && HAVE_64BIT_BUILD && defined(_M_X64)
MozStackFrameSymbolizer symbolizer;
MinidumpProcessor minidumpProcessor(&symbolizer, false);
#else
BasicSourceLineResolver resolver;
// We don't have a valid symbol resolver so we pass nullptr instead.
MinidumpProcessor minidumpProcessor(nullptr, &resolver);
#endif
// Process the minidump.
#if defined(XP_WIN)
// Breakpad invokes std::ifstream directly, so this path needs to be ANSI
Minidump dump(UTF8ToMBCS(aDumpFile));
#else
Minidump dump(aDumpFile);
#endif // defined(XP_WIN)
if (!dump.Read()) {
return false;
}
ProcessResult rv;
ProcessState processState;
rv = minidumpProcessor.Process(&dump, &processState);
aStackTraces["status"] = ResultString(rv);
ConvertProcessStateToJSON(processState, aStackTraces, aFullStacks,
aCertSubjects);
return true;
}
static bool ReadExtraFile(const string& aExtraDataPath, Json::Value& aExtra) {
IFStream f(
#if defined(XP_WIN)
UTF8ToWide(aExtraDataPath).c_str(),
#else
aExtraDataPath.c_str(),
#endif // defined(XP_WIN)
ios::in);
if (!f.is_open()) {
return false;
}
Json::CharReaderBuilder builder;
return parseFromStream(builder, f, &aExtra, nullptr);
}
// Update the extra data file by adding the StackTraces and ModuleSignatureInfo
// fields that contain the JSON outputs of this program.
static bool UpdateExtraDataFile(const string& aDumpPath,
const Json::Value& aStackTraces,
const Json::Value& aCertSubjects) {
string extraDataPath(aDumpPath);
int dot = extraDataPath.rfind('.');
if (dot < 0) {
return false; // Not a valid dump path
}
extraDataPath.replace(dot, extraDataPath.length() - dot, kExtraDataExtension);
Json::Value extra;
if (!ReadExtraFile(extraDataPath, extra)) {
return false;
}
OFStream f(
#if defined(XP_WIN)
UTF8ToWide(extraDataPath).c_str(),
#else
extraDataPath.c_str(),
#endif // defined(XP_WIN)
ios::out | ios::trunc);
bool res = false;
if (f.is_open()) {
Json::StreamWriterBuilder builder;
builder["indentation"] = "";
// The StackTraces field is not stored as a string because it's not a
// crash annotation. It's only used by the crash reporter client which
// strips it before submitting the other annotations to Socorro.
extra["StackTraces"] = aStackTraces;
if (!!aCertSubjects) {
extra["ModuleSignatureInfo"] = Json::writeString(builder, aCertSubjects);
}
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(extra, &f);
f << "\n";
res = !f.fail();
f.close();
}
return res;
}
static bool GenerateStacks(const string& aDumpPath, const bool aFullStacks) {
Json::Value stackTraces;
Json::Value certSubjects;
if (!ProcessMinidump(stackTraces, certSubjects, aDumpPath, aFullStacks)) {
return false;
}
return UpdateExtraDataFile(aDumpPath, stackTraces, certSubjects);
}
} // namespace CrashReporter
using namespace CrashReporter;
#if defined(XP_WIN)
# define XP_LITERAL(s) L##s
#else
# define XP_LITERAL(s) s
#endif
template <typename CharT>
struct CharTraits;
template <>
struct CharTraits<char> {
static int compare(const char* left, const char* right) {
return strcmp(left, right);
}
static string& assign(string& left, const char* right) {
left = right;
return left;
}
};
#if defined(XP_WIN)
template <>
struct CharTraits<wchar_t> {
static int compare(const wchar_t* left, const wchar_t* right) {
return wcscmp(left, right);
}
static string& assign(string& left, const wchar_t* right) {
left = WideToUTF8(right);
return left;
}
};
#endif // defined(XP_WIN)
static void LowerPriority() {
#if defined(XP_WIN)
Unused << SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
#else // Linux, MacOS X, etc...
Unused << nice(20);
#endif
}
template <typename CharT, typename Traits = CharTraits<CharT>>
static void ParseArguments(int argc, CharT** argv) {
if (argc <= 1) {
exit(EXIT_FAILURE);
}
for (int i = 1; i < argc - 1; i++) {
if (!Traits::compare(argv[i], XP_LITERAL("--full"))) {
gMinidumpAnalyzerOptions.fullMinidump = true;
} else if (!Traits::compare(argv[i], XP_LITERAL("--force-use-module")) &&
(i < argc - 2)) {
Traits::assign(gMinidumpAnalyzerOptions.forceUseModule, argv[i + 1]);
++i;
} else {
exit(EXIT_FAILURE);
}
}
Traits::assign(gMinidumpPath, argv[argc - 1]);
}
#if defined(XP_WIN)
// WARNING: Windows does *NOT* use UTF8 for char strings off the command line!
// Using wmain here so that the CRT doesn't need to perform a wasteful and
// lossy UTF-16 to MBCS conversion; ParseArguments will convert to UTF8
// directly.
extern "C" int wmain(int argc, wchar_t** argv)
#else
int main(int argc, char** argv)
#endif
{
LowerPriority();
ParseArguments(argc, argv);
if (!GenerateStacks(gMinidumpPath, gMinidumpAnalyzerOptions.fullMinidump)) {
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
|