[PATCH] [sanitizer] Fix log_path behavior with StopTheWorld.

Sergey Matveev earthdok at google.com
Tue Dec 3 10:59:06 PST 2013


    - use the parent's FD

Hi samsonov,

http://llvm-reviews.chandlerc.com/D2306

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2306?vs=5849&id=5890#toc

Files:
  lib/sanitizer_common/sanitizer_common.cc
  lib/sanitizer_common/sanitizer_common.h
  lib/sanitizer_common/sanitizer_posix.cc
  lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc

Index: lib/sanitizer_common/sanitizer_common.cc
===================================================================
--- lib/sanitizer_common/sanitizer_common.cc
+++ lib/sanitizer_common/sanitizer_common.cc
@@ -42,6 +42,10 @@
 // child thread will be different from |report_fd_pid|.
 uptr report_fd_pid = 0;
 
+// PID of the tracer task in StopTheWorld. It shares the address space with the
+// main process, but has a different PID and thus requires special handling.
+uptr tracer_report_fd_pid = 0;
+
 static DieCallbackType DieCallback;
 void SetDieCallback(DieCallbackType callback) {
   DieCallback = callback;
Index: lib/sanitizer_common/sanitizer_common.h
===================================================================
--- lib/sanitizer_common/sanitizer_common.h
+++ lib/sanitizer_common/sanitizer_common.h
@@ -136,6 +136,7 @@
 extern bool log_to_file;
 extern char report_path_prefix[4096];
 extern uptr report_fd_pid;
+extern uptr tracer_report_fd_pid;
 
 uptr OpenFile(const char *filename, bool write);
 // Opens the file 'file_name" and reads up to 'max_len' bytes.
Index: lib/sanitizer_common/sanitizer_posix.cc
===================================================================
--- lib/sanitizer_common/sanitizer_posix.cc
+++ lib/sanitizer_common/sanitizer_posix.cc
@@ -198,10 +198,14 @@
 }
 
 void MaybeOpenReportFile() {
-  if (!log_to_file || (report_fd_pid == internal_getpid())) return;
+  uptr pid = internal_getpid();
+  // If in tracer, use the parent's file.
+  if (pid == tracer_report_fd_pid)
+    pid = internal_getppid();
+  if (!log_to_file || (report_fd_pid == pid)) return;
   InternalScopedBuffer<char> report_path_full(4096);
   internal_snprintf(report_path_full.data(), report_path_full.size(),
-                    "%s.%d", report_path_prefix, internal_getpid());
+                    "%s.%d", report_path_prefix, pid);
   uptr openrv = OpenFile(report_path_full.data(), true);
   if (internal_iserror(openrv)) {
     report_fd = kStderrFd;
@@ -214,7 +218,7 @@
     internal_close(report_fd);
   }
   report_fd = openrv;
-  report_fd_pid = internal_getpid();
+  report_fd_pid = pid;
 }
 
 void RawWrite(const char *buffer) {
Index: lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc
===================================================================
--- lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc
+++ lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc
@@ -356,6 +356,18 @@
   int process_was_dumpable_;
 };
 
+// When sanitizer output is being redirected to file (i.e. by using log_path),
+// the tracer should write to the parent's log instead of trying to open a new
+// file. Alert the logging code to the fact that we have a tracer.
+struct ScopedHandleReportFD {
+  explicit ScopedHandleReportFD(uptr tracer_pid) {
+    tracer_report_fd_pid = tracer_pid;
+  }
+  ~ScopedHandleReportFD() {
+    tracer_report_fd_pid = 0;
+  }
+};
+
 void StopTheWorld(StopTheWorldCallback callback, void *argument) {
   StopTheWorldScope in_stoptheworld;
   // Prepare the arguments for TracerThread.
@@ -379,6 +391,7 @@
       Report("Failed spawning a tracer thread (errno %d).\n", local_errno);
     tracer_thread_argument.mutex.Unlock();
   } else {
+    ScopedHandleReportFD scoped_handle_report_fd(tracer_pid);
     // On some systems we have to explicitly declare that we want to be traced
     // by the tracer thread.
 #ifdef PR_SET_PTRACER
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2306.2.patch
Type: text/x-patch
Size: 3416 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131203/083d1656/attachment.bin>


More information about the llvm-commits mailing list