xpcom/glue/IntentionalCrash.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/IntentionalCrash.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,55 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <string>
     1.9 +#include <sstream>
    1.10 +#include <stdlib.h>
    1.11 +#include <stdio.h>
    1.12 +
    1.13 +#ifdef XP_WIN
    1.14 +#include <process.h>
    1.15 +#define getpid _getpid
    1.16 +#else
    1.17 +#include <unistd.h>
    1.18 +#endif
    1.19 +
    1.20 +#ifndef mozilla_IntentionalCrash_h
    1.21 +#define mozilla_IntentionalCrash_h
    1.22 +
    1.23 +namespace mozilla {
    1.24 +
    1.25 +inline void
    1.26 +NoteIntentionalCrash(const char* processType)
    1.27 +{
    1.28 +  char* f = getenv("XPCOM_MEM_BLOAT_LOG");
    1.29 +
    1.30 +  if (!f) 
    1.31 +    return;
    1.32 +
    1.33 +  fprintf(stderr, "XPCOM_MEM_BLOAT_LOG: %s\n", f);
    1.34 +
    1.35 +  std::string bloatLog(f);
    1.36 +  
    1.37 +  bool hasExt = false;
    1.38 +  if (bloatLog.size() >= 4 &&
    1.39 +      0 == bloatLog.compare(bloatLog.size() - 4, 4, ".log", 4)) {
    1.40 +    hasExt = true;
    1.41 +    bloatLog.erase(bloatLog.size() - 4, 4);
    1.42 +  }
    1.43 +
    1.44 +  std::ostringstream bloatName;
    1.45 +  bloatName << bloatLog << "_" << processType << "_pid" << getpid();
    1.46 +  if (hasExt)
    1.47 +    bloatName << ".log";
    1.48 +
    1.49 +  fprintf(stderr, "Writing to log: %s\n", bloatName.str().c_str());
    1.50 +
    1.51 +  FILE* processfd = fopen(bloatName.str().c_str(), "a");
    1.52 +  fprintf(processfd, "==> process %d will purposefully crash\n", getpid());
    1.53 +  fclose(processfd);
    1.54 +}
    1.55 +
    1.56 +} // namespace mozilla
    1.57 +
    1.58 +#endif // mozilla_IntentionalCrash_h

mercurial