IntentionalCrash.h 1.3 KB

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