1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Breakpad integration
17 * The Initial Developer of the Original Code is
18 * Ted Mielczarek <ted.mielczarek@gmail.com>
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
23 * Josh Aas <josh@mozilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsExceptionHandler.h"
42 #ifdef WIN32_LEAN_AND_MEAN
43 #undef WIN32_LEAN_AND_MEAN
46 #include "client/windows/handler/exception_handler.h"
48 #elif defined(XP_MACOSX)
49 #include "client/mac/handler/exception_handler.h"
51 #include <Carbon/Carbon.h>
53 #include <sys/types.h>
55 #include "mac_utils.h"
56 #elif defined(XP_LINUX)
57 #include "client/linux/handler/exception_handler.h"
59 #include <sys/types.h>
61 #elif defined(XP_SOLARIS)
62 #include "client/solaris/handler/exception_handler.h"
64 #include <sys/types.h>
67 #error "Not yet implemented for this platform"
68 #endif // defined(XP_WIN32)
77 #include "nsILocalFile.h"
78 #include "nsDataHashtable.h"
80 namespace CrashReporter
{
83 typedef wchar_t XP_CHAR
;
84 #define CONVERT_UTF16_TO_XP_CHAR(x) x
85 #define XP_STRLEN(x) wcslen(x)
86 #define CRASH_REPORTER_FILENAME "crashreporter.exe"
87 #define PATH_SEPARATOR "\\"
88 #define XP_PATH_SEPARATOR L"\\"
89 // sort of arbitrary, but MAX_PATH is kinda small
90 #define XP_PATH_MAX 4096
91 // "<reporter path>" "<minidump path>"
92 #define CMDLINE_SIZE ((XP_PATH_MAX * 2) + 6)
93 #ifdef _USE_32BIT_TIME_T
94 #define XP_TTOA(time, buffer, base) ltoa(time, buffer, base)
96 #define XP_TTOA(time, buffer, base) _i64toa(time, buffer, base)
100 #define CONVERT_UTF16_TO_XP_CHAR(x) NS_ConvertUTF16toUTF8(x)
101 #define XP_STRLEN(x) strlen(x)
102 #define CRASH_REPORTER_FILENAME "crashreporter"
103 #define PATH_SEPARATOR "/"
104 #define XP_PATH_SEPARATOR "/"
105 #define XP_PATH_MAX PATH_MAX
106 #define XP_TTOA(time, buffer, base) sprintf(buffer, "%ld", time)
109 static const XP_CHAR dumpFileExtension
[] = {'.', 'd', 'm', 'p',
111 static const XP_CHAR extraFileExtension
[] = {'.', 'e', 'x', 't',
112 'r', 'a', '\0'}; // .extra
114 static google_breakpad::ExceptionHandler
* gExceptionHandler
= nsnull
;
116 static XP_CHAR
* crashReporterPath
;
118 // if this is false, we don't launch the crash reporter
119 static bool doReport
= true;
121 // if this is true, we pass the exception on to the OS crash reporter
122 static bool showOSCrashReporter
= false;
124 // The time of the last recorded crash, as a time_t value.
125 static time_t lastCrashTime
= 0;
126 // The pathname of a file to store the crash time in
127 static XP_CHAR lastCrashTimeFilename
[XP_PATH_MAX
] = {0};
129 // these are just here for readability
130 static const char kCrashTimeParameter
[] = "CrashTime=";
131 static const int kCrashTimeParameterLen
= sizeof(kCrashTimeParameter
)-1;
133 static const char kTimeSinceLastCrashParameter
[] = "SecondsSinceLastCrash=";
134 static const int kTimeSinceLastCrashParameterLen
=
135 sizeof(kTimeSinceLastCrashParameter
)-1;
137 // this holds additional data sent via the API
138 static nsDataHashtable
<nsCStringHashKey
,nsCString
>* crashReporterAPIData_Hash
;
139 static nsCString
* crashReporterAPIData
= nsnull
;
140 static nsCString
* notesField
= nsnull
;
143 Concat(XP_CHAR
* str
, const XP_CHAR
* toAppend
, int* size
)
145 int appendLen
= XP_STRLEN(toAppend
);
146 if (appendLen
>= *size
) appendLen
= *size
- 1;
148 memcpy(str
, toAppend
, appendLen
* sizeof(XP_CHAR
));
156 bool MinidumpCallback(const XP_CHAR
* dump_path
,
157 const XP_CHAR
* minidump_id
,
160 EXCEPTION_POINTERS
* exinfo
,
161 MDRawAssertionInfo
* assertion
,
165 bool returnValue
= showOSCrashReporter
? false : succeeded
;
167 XP_CHAR minidumpPath
[XP_PATH_MAX
];
168 int size
= XP_PATH_MAX
;
169 XP_CHAR
* p
= Concat(minidumpPath
, dump_path
, &size
);
170 p
= Concat(p
, XP_PATH_SEPARATOR
, &size
);
171 p
= Concat(p
, minidump_id
, &size
);
172 Concat(p
, dumpFileExtension
, &size
);
174 XP_CHAR extraDataPath
[XP_PATH_MAX
];
176 p
= Concat(extraDataPath
, dump_path
, &size
);
177 p
= Concat(p
, XP_PATH_SEPARATOR
, &size
);
178 p
= Concat(p
, minidump_id
, &size
);
179 Concat(p
, extraFileExtension
, &size
);
181 // calculate time since last crash (if possible), and store
182 // the time of this crash.
183 time_t crashTime
= time(NULL
);
184 time_t timeSinceLastCrash
= 0;
185 // stringified versions of the above
186 char crashTimeString
[32];
187 int crashTimeStringLen
= 0;
188 char timeSinceLastCrashString
[32];
189 int timeSinceLastCrashStringLen
= 0;
191 XP_TTOA(crashTime
, crashTimeString
, 10);
192 crashTimeStringLen
= strlen(crashTimeString
);
193 if (lastCrashTime
!= 0) {
194 timeSinceLastCrash
= crashTime
- lastCrashTime
;
195 XP_TTOA(timeSinceLastCrash
, timeSinceLastCrashString
, 10);
196 timeSinceLastCrashStringLen
= strlen(timeSinceLastCrashString
);
198 // write crash time to file
199 if (lastCrashTimeFilename
[0] != 0) {
200 #if defined(XP_WIN32)
201 HANDLE hFile
= CreateFile(lastCrashTimeFilename
, GENERIC_WRITE
, 0,
202 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
,
204 if(hFile
!= INVALID_HANDLE_VALUE
) {
206 WriteFile(hFile
, crashTimeString
, crashTimeStringLen
, &nBytes
, NULL
);
209 #elif defined(XP_UNIX)
210 int fd
= open(lastCrashTimeFilename
,
211 O_WRONLY
| O_CREAT
| O_TRUNC
,
214 write(fd
, crashTimeString
, crashTimeStringLen
);
220 #if defined(XP_WIN32)
221 XP_CHAR cmdLine
[CMDLINE_SIZE
];
223 p
= Concat(cmdLine
, L
"\"", &size
);
224 p
= Concat(p
, crashReporterPath
, &size
);
225 p
= Concat(p
, L
"\" \"", &size
);
226 p
= Concat(p
, minidumpPath
, &size
);
227 Concat(p
, L
"\"", &size
);
229 if (!crashReporterAPIData
->IsEmpty()) {
230 // write out API data
231 HANDLE hFile
= CreateFile(extraDataPath
, GENERIC_WRITE
, 0,
232 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
,
234 if(hFile
!= INVALID_HANDLE_VALUE
) {
236 WriteFile(hFile
, crashReporterAPIData
->get(),
237 crashReporterAPIData
->Length(), &nBytes
, NULL
);
238 WriteFile(hFile
, kCrashTimeParameter
, kCrashTimeParameterLen
,
240 WriteFile(hFile
, crashTimeString
, crashTimeStringLen
, &nBytes
, NULL
);
241 WriteFile(hFile
, "\n", 1, &nBytes
, NULL
);
242 if (timeSinceLastCrash
!= 0) {
243 WriteFile(hFile
, kTimeSinceLastCrashParameter
,
244 kTimeSinceLastCrashParameterLen
, &nBytes
, NULL
);
245 WriteFile(hFile
, timeSinceLastCrashString
, timeSinceLastCrashStringLen
,
247 WriteFile(hFile
, "\n", 1, &nBytes
, NULL
);
258 PROCESS_INFORMATION pi
;
260 ZeroMemory(&si
, sizeof(si
));
262 si
.dwFlags
= STARTF_USESHOWWINDOW
;
263 si
.wShowWindow
= SW_SHOWNORMAL
;
264 ZeroMemory(&pi
, sizeof(pi
));
266 if (CreateProcess(NULL
, (LPWSTR
)cmdLine
, NULL
, NULL
, FALSE
, 0,
267 NULL
, NULL
, &si
, &pi
)) {
268 CloseHandle( pi
.hProcess
);
269 CloseHandle( pi
.hThread
);
271 // we're not really in a position to do anything if the CreateProcess fails
272 TerminateProcess(GetCurrentProcess(), 1);
273 #elif defined(XP_UNIX)
274 if (!crashReporterAPIData
->IsEmpty()) {
275 // write out API data
276 int fd
= open(extraDataPath
,
277 O_WRONLY
| O_CREAT
| O_TRUNC
,
281 // not much we can do in case of error
282 write(fd
, crashReporterAPIData
->get(), crashReporterAPIData
->Length());
283 write(fd
, kCrashTimeParameter
, kCrashTimeParameterLen
);
284 write(fd
, crashTimeString
, crashTimeStringLen
);
286 if (timeSinceLastCrash
!= 0) {
287 write(fd
, kTimeSinceLastCrashParameter
,kTimeSinceLastCrashParameterLen
);
288 write(fd
, timeSinceLastCrashString
, timeSinceLastCrashStringLen
);
304 // need to clobber this, as libcurl might load NSS,
305 // and we want it to load the system NSS.
306 unsetenv("LD_LIBRARY_PATH");
307 (void) execl(crashReporterPath
,
308 crashReporterPath
, minidumpPath
, (char*)0);
316 nsresult
SetExceptionHandler(nsILocalFile
* aXREDirectory
,
317 const char* aServerURL
)
321 if (gExceptionHandler
)
322 return NS_ERROR_ALREADY_INITIALIZED
;
324 const char *envvar
= PR_GetEnv("MOZ_CRASHREPORTER_DISABLE");
325 if (envvar
&& *envvar
)
328 // this environment variable prevents us from launching
329 // the crash reporter client
330 envvar
= PR_GetEnv("MOZ_CRASHREPORTER_NO_REPORT");
331 if (envvar
&& *envvar
)
334 // allocate our strings
335 crashReporterAPIData
= new nsCString();
336 NS_ENSURE_TRUE(crashReporterAPIData
, NS_ERROR_OUT_OF_MEMORY
);
338 crashReporterAPIData_Hash
=
339 new nsDataHashtable
<nsCStringHashKey
,nsCString
>();
340 NS_ENSURE_TRUE(crashReporterAPIData_Hash
, NS_ERROR_OUT_OF_MEMORY
);
342 rv
= crashReporterAPIData_Hash
->Init();
343 NS_ENSURE_SUCCESS(rv
, rv
);
345 notesField
= new nsCString();
346 NS_ENSURE_TRUE(notesField
, NS_ERROR_OUT_OF_MEMORY
);
348 // locate crashreporter executable
349 nsCOMPtr
<nsIFile
> exePath
;
350 rv
= aXREDirectory
->Clone(getter_AddRefs(exePath
));
351 NS_ENSURE_SUCCESS(rv
, rv
);
353 #if defined(XP_MACOSX)
354 exePath
->Append(NS_LITERAL_STRING("crashreporter.app"));
355 exePath
->Append(NS_LITERAL_STRING("Contents"));
356 exePath
->Append(NS_LITERAL_STRING("MacOS"));
359 exePath
->AppendNative(NS_LITERAL_CSTRING(CRASH_REPORTER_FILENAME
));
362 nsString crashReporterPath_temp
;
363 exePath
->GetPath(crashReporterPath_temp
);
365 crashReporterPath
= ToNewUnicode(crashReporterPath_temp
);
367 nsCString crashReporterPath_temp
;
368 exePath
->GetNativePath(crashReporterPath_temp
);
370 crashReporterPath
= ToNewCString(crashReporterPath_temp
);
373 // get temp path to use for minidump path
374 #if defined(XP_WIN32)
377 // first figure out buffer size
378 int pathLen
= GetTempPath(0, NULL
);
380 return NS_ERROR_FAILURE
;
382 tempPath
.SetLength(pathLen
);
383 GetTempPath(pathLen
, (LPWSTR
)tempPath
.BeginWriting());
384 #elif defined(XP_MACOSX)
387 OSErr err
= FSFindFolder(kUserDomain
, kTemporaryFolderType
,
388 kCreateFolder
, &fsRef
);
390 return NS_ERROR_FAILURE
;
393 OSStatus status
= FSRefMakePath(&fsRef
, (UInt8
*)path
, PATH_MAX
);
395 return NS_ERROR_FAILURE
;
399 #elif defined(XP_UNIX)
400 // we assume it's always /tmp on unix systems
401 nsCString tempPath
= NS_LITERAL_CSTRING("/tmp/");
403 #error "Implement this for your platform"
406 // now set the exception handler
407 gExceptionHandler
= new google_breakpad::
408 ExceptionHandler(tempPath
.get(),
412 #if defined(XP_WIN32)
413 google_breakpad::ExceptionHandler::HANDLER_ALL
);
418 if (!gExceptionHandler
)
419 return NS_ERROR_OUT_OF_MEMORY
;
421 // store server URL with the API data
423 AnnotateCrashReport(NS_LITERAL_CSTRING("ServerURL"),
424 nsDependentCString(aServerURL
));
426 // store application start time
428 XP_TTOA(time(NULL
), timeString
, 10);
429 AnnotateCrashReport(NS_LITERAL_CSTRING("StartupTime"),
430 nsDependentCString(timeString
));
432 #if defined(XP_MACOSX)
433 // On OS X, many testers like to see the OS crash reporting dialog
434 // since it offers immediate stack traces. We allow them to set
435 // a default to pass exceptions to the OS handler.
436 showOSCrashReporter
= PassToOSCrashReporter();
442 nsresult
SetMinidumpPath(const nsAString
& aPath
)
444 if (!gExceptionHandler
)
445 return NS_ERROR_NOT_INITIALIZED
;
447 gExceptionHandler
->set_dump_path(CONVERT_UTF16_TO_XP_CHAR(aPath
).BeginReading());
453 WriteDataToFile(nsIFile
* aFile
, const nsACString
& data
)
455 nsCOMPtr
<nsILocalFile
> localFile
= do_QueryInterface(aFile
);
456 NS_ENSURE_TRUE(localFile
, NS_ERROR_FAILURE
);
459 nsresult rv
= localFile
->OpenNSPRFileDesc(PR_WRONLY
| PR_CREATE_FILE
, 00600,
461 NS_ENSURE_SUCCESS(rv
, rv
);
464 if (PR_Write(fd
, data
.Data(), data
.Length()) == -1) {
465 rv
= NS_ERROR_FAILURE
;
472 GetFileContents(nsIFile
* aFile
, nsACString
& data
)
474 nsCOMPtr
<nsILocalFile
> localFile
= do_QueryInterface(aFile
);
475 NS_ENSURE_TRUE(localFile
, NS_ERROR_FAILURE
);
478 nsresult rv
= localFile
->OpenNSPRFileDesc(PR_RDONLY
, 0, &fd
);
479 NS_ENSURE_SUCCESS(rv
, rv
);
482 PRInt32 filesize
= PR_Available(fd
);
484 rv
= NS_ERROR_FILE_NOT_FOUND
;
487 data
.SetLength(filesize
);
488 if (PR_Read(fd
, data
.BeginWriting(), filesize
) == -1) {
489 rv
= NS_ERROR_FAILURE
;
496 // Function typedef for initializing a piece of data that we
497 // don't already have.
498 typedef nsresult (*InitDataFunc
)(nsACString
&);
500 // Attempt to read aFile's contents into aContents, if aFile
501 // does not exist, create it and initialize its contents
502 // by calling aInitFunc for the data.
504 GetOrInit(nsIFile
* aDir
, const nsACString
& filename
,
505 nsACString
& aContents
, InitDataFunc aInitFunc
)
509 nsCOMPtr
<nsIFile
> dataFile
;
510 nsresult rv
= aDir
->Clone(getter_AddRefs(dataFile
));
511 NS_ENSURE_SUCCESS(rv
, rv
);
513 rv
= dataFile
->AppendNative(filename
);
514 NS_ENSURE_SUCCESS(rv
, rv
);
516 rv
= dataFile
->Exists(&exists
);
517 NS_ENSURE_SUCCESS(rv
, rv
);
521 // get the initial value and write it to the file
522 rv
= aInitFunc(aContents
);
523 NS_ENSURE_SUCCESS(rv
, rv
);
524 rv
= WriteDataToFile(dataFile
, aContents
);
527 // didn't pass in an init func
528 rv
= NS_ERROR_FAILURE
;
532 // just get the file's contents
533 rv
= GetFileContents(dataFile
, aContents
);
539 // Init the "install time" data. We're taking an easy way out here
540 // and just setting this to "the time when this version was first run".
542 InitInstallTime(nsACString
& aInstallTime
)
544 time_t t
= time(NULL
);
546 sprintf(buf
, "%ld", t
);
552 // Annotate the crash report with a Unique User ID and time
553 // since install. Also do some prep work for recording
554 // time since last crash, which must be calculated at
556 // If any piece of data doesn't exist, initialize it first.
557 nsresult
SetupExtraData(nsILocalFile
* aAppDataDirectory
,
558 const nsACString
& aBuildID
)
560 nsCOMPtr
<nsIFile
> dataDirectory
;
561 nsresult rv
= aAppDataDirectory
->Clone(getter_AddRefs(dataDirectory
));
562 NS_ENSURE_SUCCESS(rv
, rv
);
564 rv
= dataDirectory
->AppendNative(NS_LITERAL_CSTRING("Crash Reports"));
565 NS_ENSURE_SUCCESS(rv
, rv
);
568 rv
= dataDirectory
->Exists(&exists
);
569 NS_ENSURE_SUCCESS(rv
, rv
);
572 rv
= dataDirectory
->Create(nsIFile::DIRECTORY_TYPE
, 0700);
573 NS_ENSURE_SUCCESS(rv
, rv
);
576 #if defined(XP_WIN32)
577 nsAutoString
dataDirEnv(NS_LITERAL_STRING("MOZ_CRASHREPORTER_DATA_DIRECTORY="));
579 nsAutoString dataDirectoryPath
;
580 rv
= dataDirectory
->GetPath(dataDirectoryPath
);
581 NS_ENSURE_SUCCESS(rv
, rv
);
583 dataDirEnv
.Append(dataDirectoryPath
);
585 _wputenv(dataDirEnv
.get());
587 // Save this path in the environment for the crash reporter application.
588 nsCAutoString
dataDirEnv("MOZ_CRASHREPORTER_DATA_DIRECTORY=");
590 nsCAutoString dataDirectoryPath
;
591 rv
= dataDirectory
->GetNativePath(dataDirectoryPath
);
592 NS_ENSURE_SUCCESS(rv
, rv
);
594 dataDirEnv
.Append(dataDirectoryPath
);
596 char* env
= ToNewCString(dataDirEnv
);
597 NS_ENSURE_TRUE(env
, NS_ERROR_OUT_OF_MEMORY
);
603 if(NS_SUCCEEDED(GetOrInit(dataDirectory
,
604 NS_LITERAL_CSTRING("InstallTime") + aBuildID
,
605 data
, InitInstallTime
)))
606 AnnotateCrashReport(NS_LITERAL_CSTRING("InstallTime"), data
);
608 // this is a little different, since we can't init it with anything,
609 // since it's stored at crash time, and we can't annotate the
610 // crash report with the stored value, since we really want
611 // (now - LastCrash), so we just get a value if it exists,
612 // and store it in a time_t value.
613 if(NS_SUCCEEDED(GetOrInit(dataDirectory
, NS_LITERAL_CSTRING("LastCrash"),
615 lastCrashTime
= (time_t)atol(data
.get());
618 // not really the best place to init this, but I have the path I need here
619 nsCOMPtr
<nsIFile
> lastCrashFile
;
620 rv
= dataDirectory
->Clone(getter_AddRefs(lastCrashFile
));
621 NS_ENSURE_SUCCESS(rv
, rv
);
623 rv
= lastCrashFile
->AppendNative(NS_LITERAL_CSTRING("LastCrash"));
624 NS_ENSURE_SUCCESS(rv
, rv
);
625 memset(lastCrashTimeFilename
, 0, sizeof(lastCrashTimeFilename
));
627 #if defined(XP_WIN32)
628 nsAutoString filename
;
629 rv
= lastCrashFile
->GetPath(filename
);
630 NS_ENSURE_SUCCESS(rv
, rv
);
632 if (filename
.Length() < XP_PATH_MAX
)
633 wcsncpy(lastCrashTimeFilename
, filename
.get(), filename
.Length());
635 nsCAutoString filename
;
636 rv
= lastCrashFile
->GetNativePath(filename
);
637 NS_ENSURE_SUCCESS(rv
, rv
);
639 if (filename
.Length() < XP_PATH_MAX
)
640 strncpy(lastCrashTimeFilename
, filename
.get(), filename
.Length());
646 nsresult
UnsetExceptionHandler()
648 // do this here in the unlikely case that we succeeded in allocating
649 // our strings but failed to allocate gExceptionHandler.
650 if (crashReporterAPIData_Hash
) {
651 delete crashReporterAPIData_Hash
;
652 crashReporterAPIData_Hash
= nsnull
;
655 if (crashReporterAPIData
) {
656 delete crashReporterAPIData
;
657 crashReporterAPIData
= nsnull
;
665 if (crashReporterPath
) {
666 NS_Free(crashReporterPath
);
667 crashReporterPath
= nsnull
;
670 if (!gExceptionHandler
)
671 return NS_ERROR_NOT_INITIALIZED
;
673 delete gExceptionHandler
;
674 gExceptionHandler
= nsnull
;
679 static void ReplaceChar(nsCString
& str
, const nsACString
& character
,
680 const nsACString
& replacement
)
682 nsCString::const_iterator start
, end
;
684 str
.BeginReading(start
);
687 while (FindInReadable(character
, start
, end
)) {
688 PRInt32 pos
= end
.size_backward();
689 str
.Replace(pos
- 1, 1, replacement
);
691 str
.BeginReading(start
);
692 start
.advance(pos
+ replacement
.Length() - 1);
697 static PRBool
DoFindInReadable(const nsACString
& str
, const nsACString
& value
)
699 nsACString::const_iterator start
, end
;
700 str
.BeginReading(start
);
703 return FindInReadable(value
, start
, end
);
706 static PLDHashOperator PR_CALLBACK
EnumerateEntries(const nsACString
& key
,
710 crashReporterAPIData
->Append(key
+ NS_LITERAL_CSTRING("=") + entry
+
711 NS_LITERAL_CSTRING("\n"));
712 return PL_DHASH_NEXT
;
715 nsresult
AnnotateCrashReport(const nsACString
& key
, const nsACString
& data
)
717 if (!gExceptionHandler
)
718 return NS_ERROR_NOT_INITIALIZED
;
720 if (DoFindInReadable(key
, NS_LITERAL_CSTRING("=")) ||
721 DoFindInReadable(key
, NS_LITERAL_CSTRING("\n")))
722 return NS_ERROR_INVALID_ARG
;
724 if (DoFindInReadable(data
, NS_LITERAL_CSTRING("\0")))
725 return NS_ERROR_INVALID_ARG
;
727 nsCString
escapedData(data
);
729 // escape backslashes
730 ReplaceChar(escapedData
, NS_LITERAL_CSTRING("\\"),
731 NS_LITERAL_CSTRING("\\\\"));
733 ReplaceChar(escapedData
, NS_LITERAL_CSTRING("\n"),
734 NS_LITERAL_CSTRING("\\n"));
736 nsresult rv
= crashReporterAPIData_Hash
->Put(key
, escapedData
);
737 NS_ENSURE_SUCCESS(rv
, rv
);
739 // now rebuild the file contents
740 crashReporterAPIData
->Truncate(0);
741 crashReporterAPIData_Hash
->EnumerateRead(EnumerateEntries
,
742 crashReporterAPIData
);
747 nsresult
AppendAppNotesToCrashReport(const nsACString
& data
)
749 if (!gExceptionHandler
)
750 return NS_ERROR_NOT_INITIALIZED
;
752 if (DoFindInReadable(data
, NS_LITERAL_CSTRING("\0")))
753 return NS_ERROR_INVALID_ARG
;
755 notesField
->Append(data
);
756 return AnnotateCrashReport(NS_LITERAL_CSTRING("Notes"), *notesField
);
759 // Returns true if found, false if not found.
760 bool GetAnnotation(const nsACString
& key
, nsACString
& data
)
762 if (!gExceptionHandler
)
763 return NS_ERROR_NOT_INITIALIZED
;
766 if (!crashReporterAPIData_Hash
->Get(key
, &entry
))
774 SetRestartArgs(int argc
, char** argv
)
776 if (!gExceptionHandler
)
780 nsCAutoString envVar
;
782 for (i
= 0; i
< argc
; i
++) {
783 envVar
= "MOZ_CRASHREPORTER_RESTART_ARG_";
786 #if defined(XP_UNIX) && !defined(XP_MACOSX)
787 // we'd like to run the script around the binary
788 // instead of the binary itself, so remove the -bin
789 // if it exists on the first argument
792 (arg_len
= strlen(argv
[i
])) > 4 &&
793 strcmp(argv
[i
] + arg_len
- 4, "-bin") == 0) {
794 envVar
.Append(argv
[i
], arg_len
- 4);
801 // PR_SetEnv() wants the string to be available for the lifetime
802 // of the app, so dup it here
803 env
= ToNewCString(envVar
);
805 return NS_ERROR_OUT_OF_MEMORY
;
810 // make sure the arg list is terminated
811 envVar
= "MOZ_CRASHREPORTER_RESTART_ARG_";
815 // PR_SetEnv() wants the string to be available for the lifetime
816 // of the app, so dup it here
817 env
= ToNewCString(envVar
);
819 return NS_ERROR_OUT_OF_MEMORY
;
823 // make sure we save the info in XUL_APP_FILE for the reporter
824 const char *appfile
= PR_GetEnv("XUL_APP_FILE");
825 if (appfile
&& *appfile
) {
826 envVar
= "MOZ_CRASHREPORTER_RESTART_XUL_APP_FILE=";
828 env
= ToNewCString(envVar
);
836 nsresult
WriteMinidumpForException(EXCEPTION_POINTERS
* aExceptionInfo
)
838 if (!gExceptionHandler
)
839 return NS_ERROR_NOT_INITIALIZED
;
841 return gExceptionHandler
->WriteMinidumpForException(aExceptionInfo
) ? NS_OK
: NS_ERROR_FAILURE
;
846 nsresult
AppendObjCExceptionInfoToAppNotes(void *inException
)
848 nsCAutoString excString
;
849 GetObjCExceptionInfo(inException
, excString
);
850 AppendAppNotesToCrashReport(excString
);
855 } // namespace CrashReporter