1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "linux/handler/exception_handler.h"
6 
7 using google_breakpad::ExceptionHandler;
8 
9 static ExceptionHandler* gExceptionHandler = nullptr;
10 
11 // Flag saying whether to generate a minidump. Can be (probably temporarily) set
12 // to false when a crash is expected.
13 bool __attribute__((visibility("default"))) gBreakpadInjectorEnabled = true;
14 
TestEnabled(void *)15 bool TestEnabled(void* /* context */) { return gBreakpadInjectorEnabled; }
16 
SetGlobalExceptionHandler(ExceptionHandler::FilterCallback filterCallback,ExceptionHandler::MinidumpCallback minidumpCallback)17 bool SetGlobalExceptionHandler(
18     ExceptionHandler::FilterCallback filterCallback,
19     ExceptionHandler::MinidumpCallback minidumpCallback) {
20   const char* tempPath = getenv("TMPDIR");
21   if (!tempPath) tempPath = "/tmp";
22 
23   google_breakpad::MinidumpDescriptor descriptor(tempPath);
24 
25   gExceptionHandler = new ExceptionHandler(descriptor, filterCallback,
26                                            minidumpCallback, nullptr, true, -1);
27   if (!gExceptionHandler) return false;
28 
29   return true;
30 }
31 
32 // Called when loading the DLL (eg via LD_PRELOAD, or the JS shell --dll
33 // option).
SetBreakpadExceptionHandler()34 void __attribute__((constructor)) SetBreakpadExceptionHandler() {
35   if (gExceptionHandler) abort();
36 
37   if (!SetGlobalExceptionHandler(TestEnabled, nullptr)) abort();
38 
39   if (!gExceptionHandler) abort();
40 }
41