1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include <string>
8 #include <sstream>
9 #include <stdlib.h>
10 #include <stdio.h>
11 
12 #ifdef XP_WIN
13 #include <process.h>
14 #define getpid _getpid
15 #else
16 #include <unistd.h>
17 #endif
18 
19 #ifndef mozilla_IntentionalCrash_h
20 #define mozilla_IntentionalCrash_h
21 
22 namespace mozilla {
23 
24 inline void
NoteIntentionalCrash(const char * aProcessType)25 NoteIntentionalCrash(const char* aProcessType)
26 {
27   char* f = getenv("XPCOM_MEM_BLOAT_LOG");
28   if (!f) {
29     return;
30   }
31 
32   fprintf(stderr, "XPCOM_MEM_BLOAT_LOG: %s\n", f);
33 
34   std::string bloatLog(f);
35 
36   bool hasExt = false;
37   if (bloatLog.size() >= 4 &&
38       bloatLog.compare(bloatLog.size() - 4, 4, ".log", 4) == 0) {
39     hasExt = true;
40     bloatLog.erase(bloatLog.size() - 4, 4);
41   }
42 
43   std::ostringstream bloatName;
44   bloatName << bloatLog << "_" << aProcessType << "_pid" << getpid();
45   if (hasExt) {
46     bloatName << ".log";
47   }
48 
49   fprintf(stderr, "Writing to log: %s\n", bloatName.str().c_str());
50 
51   FILE* processfd = fopen(bloatName.str().c_str(), "a");
52   fprintf(processfd, "==> process %d will purposefully crash\n", getpid());
53   fclose(processfd);
54 }
55 
56 } // namespace mozilla
57 
58 #endif // mozilla_IntentionalCrash_h
59