<div dir="ltr">ah, thanks. </div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Sep 5, 2013 at 9:40 AM, Reid Kleckner <span dir="ltr"><<a href="mailto:rnk@google.com" target="_blank">rnk@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div class="h5">On Wed, Sep 4, 2013 at 9:53 PM, Kostya Serebryany <span dir="ltr"><<a href="mailto:kcc@google.com" target="_blank">kcc@google.com</a>></span> wrote:<br>
</div></div><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Thu, Sep 5, 2013 at 7:19 AM, Reid Kleckner <span dir="ltr"><<a href="mailto:reid@kleckner.net" target="_blank">reid@kleckner.net</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rnk<br>
Date: Wed Sep  4 22:19:57 2013<br>
New Revision: 190030<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=190030&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=190030&view=rev</a><br>
Log:<br>
sanitizers: Make sure Visual Studio gets error reports<br>
<br>
Visual Studio appears to close stderr before launching a non-console<br>
win32 program.  This means we don't see any sanitizer reports.  If<br>
stderr printing fails, call OutputDebugStringA to get the reports into<br>
the Visual Studio debugger console.<br>
<br>
Modified:<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=190030&r1=190029&r2=190030&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=190030&r1=190029&r2=190030&view=diff</a><br>



==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc Wed Sep  4 22:19:57 2013<br>
@@ -26,16 +26,19 @@ uptr GetPageSizeCached() {<br>
   return PageSize;<br>
 }<br>
<br>
-static bool log_to_file = false;  // Set to true by __sanitizer_set_report_path<br>
<br>
 // By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|<br>
 // isn't equal to the current PID, try to obtain file descriptor by opening<br>
 // file "report_path_prefix.<PID>".<br>
 fd_t report_fd = kStderrFd;<br>
-static char report_path_prefix[4096];  // Set via __sanitizer_set_report_path.<br>
+<br>
+// Set via __sanitizer_set_report_path.<br>
+bool log_to_file = false;<br>
+char report_path_prefix[sizeof(report_path_prefix)];<br></blockquote><div><br></div></div></div><div>hmm. What is </div><div>   char X[sizeof(X)] </div><div>supposed to mean? </div></div></div></div></blockquote><div><br>

</div></div></div><div>Trying to take the size of X from the header file.  It will fail if X is not available.</div><div><br>The alternative is:</div><div>.h:</div><div>enum { kBufferSize = 4096 };</div><div>extern char X[kBufferSize];</div>

<div>.cpp:</div><div>char X[kBufferSize];</div><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra">
<div class="gmail_quote">
<div>also, I am not a great fan of non-static globals, even in a namespace. </div><div><div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
 // PID of process that opened |report_fd|. If a fork() occurs, the PID of the<br>
 // child thread will be different from |report_fd_pid|.<br>
-static uptr report_fd_pid = 0;<br>
+uptr report_fd_pid = 0;<br>
<br>
 static DieCallbackType DieCallback;<br>
 void SetDieCallback(DieCallbackType callback) {<br>
@@ -68,36 +71,6 @@ void NORETURN CheckFailed(const char *fi<br>
   Die();<br>
 }<br>
<br>
-void MaybeOpenReportFile() {<br>
-  if (!log_to_file || (report_fd_pid == internal_getpid())) return;<br>
-  InternalScopedBuffer<char> report_path_full(4096);<br>
-  internal_snprintf(report_path_full.data(), report_path_full.size(),<br>
-                    "%s.%d", report_path_prefix, internal_getpid());<br>
-  uptr openrv = OpenFile(report_path_full.data(), true);<br>
-  if (internal_iserror(openrv)) {<br>
-    report_fd = kStderrFd;<br>
-    log_to_file = false;<br>
-    Report("ERROR: Can't open file: %s\n", report_path_full.data());<br>
-    Die();<br>
-  }<br>
-  if (report_fd != kInvalidFd) {<br>
-    // We're in the child. Close the parent's log.<br>
-    internal_close(report_fd);<br>
-  }<br>
-  report_fd = openrv;<br>
-  report_fd_pid = internal_getpid();<br>
-}<br>
-<br>
-void RawWrite(const char *buffer) {<br>
-  static const char *kRawWriteError = "RawWrite can't output requested buffer!";<br>
-  uptr length = (uptr)internal_strlen(buffer);<br>
-  MaybeOpenReportFile();<br>
-  if (length != internal_write(report_fd, buffer, length)) {<br>
-    internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));<br>
-    Die();<br>
-  }<br>
-}<br>
-<br>
 uptr ReadFileToBuffer(const char *file_name, char **buff,<br>
                       uptr *buff_size, uptr max_len) {<br>
   uptr PageSize = GetPageSizeCached();<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=190030&r1=190029&r2=190030&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=190030&r1=190029&r2=190030&view=diff</a><br>



==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Wed Sep  4 22:19:57 2013<br>
@@ -116,6 +116,9 @@ void SetPrintfAndReportCallback(void (*c<br>
 extern StaticSpinMutex CommonSanitizerReportMutex;<br>
 void MaybeOpenReportFile();<br>
 extern fd_t report_fd;<br>
+extern bool log_to_file;<br>
+extern char report_path_prefix[4096];<br>
+extern uptr report_fd_pid;<br>
<br>
 uptr OpenFile(const char *filename, bool write);<br>
 // Opens the file 'file_name" and reads up to 'max_len' bytes.<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=190030&r1=190029&r2=190030&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=190030&r1=190029&r2=190030&view=diff</a><br>



==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Wed Sep  4 22:19:57 2013<br>
@@ -197,6 +197,37 @@ char *FindPathToBinary(const char *name)<br>
   return 0;<br>
 }<br>
<br>
+void MaybeOpenReportFile() {<br>
+  if (!log_to_file || (report_fd_pid == internal_getpid())) return;<br>
+  InternalScopedBuffer<char> report_path_full(4096);<br>
+  internal_snprintf(report_path_full.data(), report_path_full.size(),<br>
+                    "%s.%d", report_path_prefix, internal_getpid());<br>
+  uptr openrv = OpenFile(report_path_full.data(), true);<br>
+  if (internal_iserror(openrv)) {<br>
+    report_fd = kStderrFd;<br>
+    log_to_file = false;<br>
+    Report("ERROR: Can't open file: %s\n", report_path_full.data());<br>
+    Die();<br>
+  }<br>
+  if (report_fd != kInvalidFd) {<br>
+    // We're in the child. Close the parent's log.<br>
+    internal_close(report_fd);<br>
+  }<br>
+  report_fd = openrv;<br>
+  report_fd_pid = internal_getpid();<br>
+}<br>
+<br>
+void RawWrite(const char *buffer) {<br>
+  static const char *kRawWriteError =<br>
+      "RawWrite can't output requested buffer!\n";<br>
+  uptr length = (uptr)internal_strlen(buffer);<br>
+  MaybeOpenReportFile();<br>
+  if (length != internal_write(report_fd, buffer, length)) {<br>
+    internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));<br>
+    Die();<br>
+  }<br>
+}<br>
+<br>
 }  // namespace __sanitizer<br>
<br>
 #endif  // SANITIZER_LINUX || SANITIZER_MAC<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=190030&r1=190029&r2=190030&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=190030&r1=190029&r2=190030&view=diff</a><br>



==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Wed Sep  4 22:19:57 2013<br>
@@ -398,6 +398,24 @@ void GetStackTrace(StackTrace *stack, up<br>
     stack->trace[i] = (uptr)tmp[i + offset];<br>
 }<br>
<br>
+void MaybeOpenReportFile() {<br>
+  // Windows doesn't have native fork, and we don't support Cygwin or other<br>
+  // environments that try to fake it, so the initial report_fd will always be<br>
+  // correct.<br>
+}<br>
+<br>
+void RawWrite(const char *buffer) {<br>
+  static const char *kRawWriteError =<br>
+      "RawWrite can't output requested buffer!\n";<br>
+  uptr length = (uptr)internal_strlen(buffer);<br>
+  if (length != internal_write(report_fd, buffer, length)) {<br>
+    // stderr may be closed, but we may be able to print to the debugger<br>
+    // instead.  This is the case when launching a program from Visual Studio,<br>
+    // and the following routine should write to its console.<br>
+    OutputDebugStringA(buffer);<br>
+  }<br>
+}<br>
+<br>
 }  // namespace __sanitizer<br>
<br>
 #endif  // _WIN32<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div></div><br></div></div>
<br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div></div></div><br></div></div>
</blockquote></div><br></div>