Bug 1257486 - Annotate global memory status in the crash reporter for child process; r=bsmedberg
authorCervantes Yu <cyu@mozilla.com>
Tue, 29 Mar 2016 19:16:09 +0800
changeset 348366 4f662b15f40b63818e4c0bf707c434f82321deb4
parent 348365 ab62a777c115ca638278771925f27921675b9efd
child 348367 4b76e05f7ecf45a4a6877517f27c3d4d067802cd
push id14826
push userbenj@benj.me
push dateThu, 07 Apr 2016 11:56:36 +0000
reviewersbsmedberg
bugs1257486
milestone48.0a1
Bug 1257486 - Annotate global memory status in the crash reporter for child process; r=bsmedberg MozReview-Commit-ID: ExVZS3aAp41
toolkit/crashreporter/nsExceptionHandler.cpp
toolkit/crashreporter/test/unit/test_oom_annotation_windows.js
toolkit/crashreporter/test/unit/xpcshell.ini
toolkit/crashreporter/test/unit_ipc/test_content_oom_annotation_windows.js
toolkit/crashreporter/test/unit_ipc/xpcshell.ini
--- a/toolkit/crashreporter/nsExceptionHandler.cpp
+++ b/toolkit/crashreporter/nsExceptionHandler.cpp
@@ -763,16 +763,49 @@ OpenAPIData(PlatformWriter& aWriter,
       p -= 4;
       size += 4;
     }
   }
   Concat(p, extraFileExtension, &size);
   aWriter.Open(extraDataPath);
 }
 
+#ifdef XP_WIN
+void
+WriteGlobalMemoryStatus(PlatformWriter* apiData, PlatformWriter* eventFile)
+{
+  char buffer[128];
+
+  // Try to get some information about memory.
+  MEMORYSTATUSEX statex;
+  statex.dwLength = sizeof(statex);
+  if (GlobalMemoryStatusEx(&statex)) {
+
+#define WRITE_STATEX_FIELD(field, name, conversionFunc)        \
+    conversionFunc(statex.field, buffer, 10);                  \
+    if (apiData) {                                             \
+      WriteAnnotation(*apiData, name, buffer);                  \
+    }                                                          \
+    if (eventFile) {                                           \
+      WriteAnnotation(*eventFile, name, buffer);                \
+    }
+
+    WRITE_STATEX_FIELD(dwMemoryLoad, "SystemMemoryUsePercentage", ltoa);
+    WRITE_STATEX_FIELD(ullTotalVirtual, "TotalVirtualMemory", _ui64toa);
+    WRITE_STATEX_FIELD(ullAvailVirtual, "AvailableVirtualMemory", _ui64toa);
+    WRITE_STATEX_FIELD(ullTotalPageFile, "TotalPageFile", _ui64toa);
+    WRITE_STATEX_FIELD(ullAvailPageFile, "AvailablePageFile", _ui64toa);
+    WRITE_STATEX_FIELD(ullTotalPhys, "TotalPhysicalMemory", _ui64toa);
+    WRITE_STATEX_FIELD(ullAvailPhys, "AvailablePhysicalMemory", _ui64toa);
+
+#undef WRITE_STATEX_FIELD
+  }
+}
+#endif
+
 bool MinidumpCallback(
 #ifdef XP_LINUX
                       const MinidumpDescriptor& descriptor,
 #else
                       const XP_CHAR* dump_path,
                       const XP_CHAR* minidump_id,
 #endif
                       void* context,
@@ -963,37 +996,17 @@ bool MinidumpCallback(
     }
 
 #ifdef HAS_DLL_BLOCKLIST
     if (apiData.Valid()) {
       DllBlocklist_WriteNotes(apiData.Handle());
       DllBlocklist_WriteNotes(eventFile.Handle());
     }
 #endif
-
-    // Try to get some information about memory.
-    MEMORYSTATUSEX statex;
-    statex.dwLength = sizeof(statex);
-    if (GlobalMemoryStatusEx(&statex)) {
-
-#define WRITE_STATEX_FIELD(field, name, conversionFunc)          \
-      conversionFunc(statex.field, buffer, 10);                  \
-      WriteAnnotation(apiData, name, buffer);                    \
-      WriteAnnotation(eventFile, name, buffer);
-
-      WRITE_STATEX_FIELD(dwMemoryLoad, "SystemMemoryUsePercentage", ltoa);
-      WRITE_STATEX_FIELD(ullTotalVirtual, "TotalVirtualMemory", _ui64toa);
-      WRITE_STATEX_FIELD(ullAvailVirtual, "AvailableVirtualMemory", _ui64toa);
-      WRITE_STATEX_FIELD(ullTotalPageFile, "TotalPageFile", _ui64toa);
-      WRITE_STATEX_FIELD(ullAvailPageFile, "AvailablePageFile", _ui64toa);
-      WRITE_STATEX_FIELD(ullTotalPhys, "TotalPhysicalMemory", _ui64toa);
-      WRITE_STATEX_FIELD(ullAvailPhys, "AvailablePhysicalMemory", _ui64toa);
-
-#undef WRITE_STATEX_FIELD
-    }
+    WriteGlobalMemoryStatus(&apiData, &eventFile);
 #endif // XP_WIN
 
     if (gMozCrashReason) {
       WriteAnnotation(apiData, "MozCrashReason", gMozCrashReason);
       WriteAnnotation(eventFile, "MozCrashReason", gMozCrashReason);
     }
 
     if (oomAllocationSizeBuffer[0]) {
@@ -1266,16 +1279,20 @@ PrepareChildExceptionTimeAnnotations()
   p = Concat(p, pidBuffer, &charsAvailable);
 
   // Now open the file...
   PlatformWriter apiData;
   OpenAPIData(apiData, tempPath);
 
   // ...and write out any annotations. These must be escaped if necessary
   // (but don't call EscapeAnnotation here, because it touches the heap).
+#ifdef XP_WIN
+  WriteGlobalMemoryStatus(&apiData, nullptr);
+#endif
+
   char oomAllocationSizeBuffer[32] = "";
   if (gOOMAllocationSize) {
     XP_STOA(gOOMAllocationSize, oomAllocationSizeBuffer, 10);
   }
 
   if (oomAllocationSizeBuffer[0]) {
     WriteAnnotation(apiData, "OOMAllocationSize", oomAllocationSizeBuffer);
   }
new file mode 100644
--- /dev/null
+++ b/toolkit/crashreporter/test/unit/test_oom_annotation_windows.js
@@ -0,0 +1,27 @@
+function run_test()
+{
+  if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+    dump("INFO | test_crash_oom.js | Can't test crashreporter in a non-libxul build.\n");
+    return;
+  }
+
+  do_crash(
+    function() {
+      crashType = CrashTestUtils.CRASH_OOM;
+      crashReporter.annotateCrashReport("TestingOOMCrash", "Yes");
+    },
+    function(mdump, extra) {
+      do_check_eq(extra.TestingOOMCrash, "Yes");
+      do_check_true("OOMAllocationSize" in extra);
+      do_check_true(Number(extra.OOMAllocationSize) > 0);
+      do_check_true("SystemMemoryUsePercentage" in extra);
+      do_check_true("TotalVirtualMemory" in extra);
+      do_check_true("AvailableVirtualMemory" in extra);
+      do_check_true("TotalPageFile" in extra);
+      do_check_true("AvailablePageFile" in extra);
+      do_check_true("TotalPhysicalMemory" in extra);
+      do_check_true("AvailablePhysicalMemory" in extra);
+
+    },
+    true);
+}
--- a/toolkit/crashreporter/test/unit/xpcshell.ini
+++ b/toolkit/crashreporter/test/unit/xpcshell.ini
@@ -10,16 +10,19 @@ support-files =
 [test_crash_purevirtual.js]
 [test_crash_runtimeabort.js]
 [test_crash_after_js_oom_reported.js]
 [test_crash_after_js_oom_recovered.js]
 [test_crash_after_js_oom_reported_2.js]
 [test_crash_after_js_large_allocation_failure.js]
 [test_crash_after_js_large_allocation_failure_reporting.js]
 [test_crash_oom.js]
+[test_oom_annotation_windows.js]
+skip-if = os != 'win'
+
 [test_crash_abort.js]
 skip-if = os == 'win'
 
 [test_crash_with_memory_report.js]
 [test_crashreporter.js]
 [test_crashreporter_crash.js]
 [test_crashreporter_crash_profile_lock.js]
 [test_override_exception_handler.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/crashreporter/test/unit_ipc/test_content_oom_annotation_windows.js
@@ -0,0 +1,23 @@
+load("../unit/head_crashreporter.js");
+
+function run_test()
+{
+  if (!("@mozilla.org/toolkit/crash-reporter;1" in Components.classes)) {
+    dump("INFO | test_content_annotation.js | Can't test crashreporter in a non-libxul build.\n");
+    return;
+  }
+
+  // Try crashing with an OOM
+  do_content_crash(function() {
+                     crashType = CrashTestUtils.CRASH_OOM;
+                   },
+                   function(mdump, extra) {
+                     do_check_true("SystemMemoryUsePercentage" in extra);
+                     do_check_true("TotalVirtualMemory" in extra);
+                     do_check_true("AvailableVirtualMemory" in extra);
+                     do_check_true("TotalPageFile" in extra);
+                     do_check_true("AvailablePageFile" in extra);
+                     do_check_true("TotalPhysicalMemory" in extra);
+                     do_check_true("AvailablePhysicalMemory" in extra);
+                   });
+}
--- a/toolkit/crashreporter/test/unit_ipc/xpcshell.ini
+++ b/toolkit/crashreporter/test/unit_ipc/xpcshell.ini
@@ -4,8 +4,10 @@ tail =
 skip-if = toolkit == 'android' || toolkit == 'gonk'
 support-files =
   !/toolkit/crashreporter/test/unit/crasher_subprocess_head.js
   !/toolkit/crashreporter/test/unit/crasher_subprocess_tail.js
   !/toolkit/crashreporter/test/unit/head_crashreporter.js
 
 [test_content_annotation.js]
 [test_content_exception_time_annotation.js]
+[test_content_oom_annotation_windows.js]
+skip-if = os != 'win'