Bug 1485224 - Make best efforts to write a stack frame atomically. r=froydnj, a=IanN CLOSED TREE DONTBUILD SEAMONKEY_2_49_ESR_RELBRANCH
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Thu, 13 Sep 2018 19:22:18 +0900
branchSEAMONKEY_2_49_ESR_RELBRANCH
changeset 357546 fade2a70ad157186a7fdc858c8f7910bc71ef688
parent 357545 d47e78bd1354d920e9ee8344fcad16046aa28fc4
child 357547 7eb0d834eba615dcb511c96f9efda78a09ca68cf
push id7834
push userfrgrahl@gmx.net
push dateSun, 13 Jan 2019 12:17:02 +0000
treeherdermozilla-esr52@6e4ad8a8f2e8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj, IanN
bugs1485224
milestone52.9.1
Bug 1485224 - Make best efforts to write a stack frame atomically. r=froydnj, a=IanN CLOSED TREE DONTBUILD mozilla-esr52 SEAMONKEY_2_49_ESR_RELBRANCH
xpcom/base/nsTraceRefcnt.cpp
--- a/xpcom/base/nsTraceRefcnt.cpp
+++ b/xpcom/base/nsTraceRefcnt.cpp
@@ -838,38 +838,67 @@ InitTraceLog()
     gLogging = FullLogging;
   }
 }
 
 
 extern "C" {
 
 static void
+EnsureWrite(FILE* aStream, const char* aBuf, size_t aLen)
+{
+#ifdef XP_WIN
+  int fd = _fileno(aStream);
+#else
+  int fd = fileno(aStream);
+#endif
+  while (aLen > 0) {
+#ifdef XP_WIN
+    auto written = _write(fd, aBuf, aLen);
+#else
+    auto written = write(fd, aBuf, aLen);
+#endif
+    if (written <= 0 || size_t(written) > aLen) {
+      break;
+    }
+    aBuf += written;
+    aLen -= written;
+  }
+}
+
+static void
 PrintStackFrame(uint32_t aFrameNumber, void* aPC, void* aSP, void* aClosure)
 {
   FILE* stream = (FILE*)aClosure;
   MozCodeAddressDetails details;
-  char buf[1024];
+  static const size_t buflen = 1024;
+  char buf[buflen + 1];  // 1 for trailing '\n'
 
   MozDescribeCodeAddress(aPC, &details);
-  MozFormatCodeAddressDetails(buf, sizeof(buf), aFrameNumber, aPC, &details);
-  fprintf(stream, "%s\n", buf);
+  MozFormatCodeAddressDetails(buf, buflen, aFrameNumber, aPC, &details);
+  size_t len = std::min(strlen(buf), buflen + 1 - 2);
+  buf[len++] = '\n';
+  buf[len] = '\0';
   fflush(stream);
+  EnsureWrite(stream, buf, len);
 }
 
 static void
 PrintStackFrameCached(uint32_t aFrameNumber, void* aPC, void* aSP,
                       void* aClosure)
 {
   auto stream = static_cast<FILE*>(aClosure);
   static const size_t buflen = 1024;
-  char buf[buflen];
-  gCodeAddressService->GetLocation(aFrameNumber, aPC, buf, buflen);
-  fprintf(stream, "    %s\n", buf);
+  char buf[buflen + 5] = "    ";  // 5 for leading "    " and trailing '\n'
+  gCodeAddressService->GetLocation(aFrameNumber, aPC, buf + 4, buflen);
+  size_t len = std::min(strlen(buf), buflen + 5 - 2);
+  buf[len++] = '\n';
+  buf[len] = '\0';
   fflush(stream);
+  EnsureWrite(stream, buf, len);
 }
 
 static void
 RecordStackFrame(uint32_t /*aFrameNumber*/, void* aPC, void* /*aSP*/,
                  void* aClosure)
 {
   auto locations = static_cast<std::vector<void*>*>(aClosure);
   locations->push_back(aPC);