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 #ifndef mozilla_IntentionalCrash_h
8 #define mozilla_IntentionalCrash_h
9 
10 #include <string>
11 #include <sstream>
12 #include <stdlib.h>
13 #include <stdio.h>
14 
15 #ifdef XP_WIN
16 #  include <process.h>
17 #  define getpid _getpid
18 #else
19 #  include <unistd.h>
20 #endif
21 
22 namespace mozilla {
23 
NoteIntentionalCrash(const char * aProcessType)24 inline void NoteIntentionalCrash(const char* aProcessType) {
25 // In opt builds we don't actually have the leak checking enabled, and the
26 // sandbox doesn't allow writing to this path, so we just disable this
27 // function's behaviour.
28 #ifdef MOZ_DEBUG
29   char* f = getenv("XPCOM_MEM_BLOAT_LOG");
30   if (!f) {
31     return;
32   }
33 
34   fprintf(stderr, "XPCOM_MEM_BLOAT_LOG: %s\n", f);
35 
36   std::ostringstream bloatName;
37   std::string processType(aProcessType);
38   if (!processType.compare("default")) {
39     bloatName << f;
40   } else {
41     std::string bloatLog(f);
42 
43     bool hasExt = false;
44     if (bloatLog.size() >= 4 &&
45         bloatLog.compare(bloatLog.size() - 4, 4, ".log", 4) == 0) {
46       hasExt = true;
47       bloatLog.erase(bloatLog.size() - 4, 4);
48     }
49 
50     bloatName << bloatLog << "_" << processType << "_pid" << getpid();
51     if (hasExt) {
52       bloatName << ".log";
53     }
54   }
55 
56   fprintf(stderr, "Writing to log: %s\n", bloatName.str().c_str());
57 
58   FILE* processfd = fopen(bloatName.str().c_str(), "a");
59   if (processfd) {
60     fprintf(processfd, "==> process %d will purposefully crash\n", getpid());
61     fclose(processfd);
62   }
63 #endif
64 }
65 
66 }  // namespace mozilla
67 
68 #endif  // mozilla_IntentionalCrash_h
69