[compiler-rt] r190030 - sanitizers: Make sure Visual Studio gets error reports

Kostya Serebryany kcc at google.com
Wed Sep 4 21:53:20 PDT 2013


On Thu, Sep 5, 2013 at 7:19 AM, Reid Kleckner <reid at kleckner.net> wrote:

> Author: rnk
> Date: Wed Sep  4 22:19:57 2013
> New Revision: 190030
>
> URL: http://llvm.org/viewvc/llvm-project?rev=190030&view=rev
> Log:
> sanitizers: Make sure Visual Studio gets error reports
>
> Visual Studio appears to close stderr before launching a non-console
> win32 program.  This means we don't see any sanitizer reports.  If
> stderr printing fails, call OutputDebugStringA to get the reports into
> the Visual Studio debugger console.
>
> Modified:
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=190030&r1=190029&r2=190030&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc Wed Sep  4
> 22:19:57 2013
> @@ -26,16 +26,19 @@ uptr GetPageSizeCached() {
>    return PageSize;
>  }
>
> -static bool log_to_file = false;  // Set to true by
> __sanitizer_set_report_path
>
>  // By default, dump to stderr. If |log_to_file| is true and
> |report_fd_pid|
>  // isn't equal to the current PID, try to obtain file descriptor by
> opening
>  // file "report_path_prefix.<PID>".
>  fd_t report_fd = kStderrFd;
> -static char report_path_prefix[4096];  // Set via
> __sanitizer_set_report_path.
> +
> +// Set via __sanitizer_set_report_path.
> +bool log_to_file = false;
> +char report_path_prefix[sizeof(report_path_prefix)];
>

hmm. What is
   char X[sizeof(X)]
supposed to mean?

also, I am not a great fan of non-static globals, even in a namespace.



> +
>  // PID of process that opened |report_fd|. If a fork() occurs, the PID of
> the
>  // child thread will be different from |report_fd_pid|.
> -static uptr report_fd_pid = 0;
> +uptr report_fd_pid = 0;
>
>  static DieCallbackType DieCallback;
>  void SetDieCallback(DieCallbackType callback) {
> @@ -68,36 +71,6 @@ void NORETURN CheckFailed(const char *fi
>    Die();
>  }
>
> -void MaybeOpenReportFile() {
> -  if (!log_to_file || (report_fd_pid == internal_getpid())) return;
> -  InternalScopedBuffer<char> report_path_full(4096);
> -  internal_snprintf(report_path_full.data(), report_path_full.size(),
> -                    "%s.%d", report_path_prefix, internal_getpid());
> -  uptr openrv = OpenFile(report_path_full.data(), true);
> -  if (internal_iserror(openrv)) {
> -    report_fd = kStderrFd;
> -    log_to_file = false;
> -    Report("ERROR: Can't open file: %s\n", report_path_full.data());
> -    Die();
> -  }
> -  if (report_fd != kInvalidFd) {
> -    // We're in the child. Close the parent's log.
> -    internal_close(report_fd);
> -  }
> -  report_fd = openrv;
> -  report_fd_pid = internal_getpid();
> -}
> -
> -void RawWrite(const char *buffer) {
> -  static const char *kRawWriteError = "RawWrite can't output requested
> buffer!";
> -  uptr length = (uptr)internal_strlen(buffer);
> -  MaybeOpenReportFile();
> -  if (length != internal_write(report_fd, buffer, length)) {
> -    internal_write(report_fd, kRawWriteError,
> internal_strlen(kRawWriteError));
> -    Die();
> -  }
> -}
> -
>  uptr ReadFileToBuffer(const char *file_name, char **buff,
>                        uptr *buff_size, uptr max_len) {
>    uptr PageSize = GetPageSizeCached();
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=190030&r1=190029&r2=190030&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Wed Sep  4
> 22:19:57 2013
> @@ -116,6 +116,9 @@ void SetPrintfAndReportCallback(void (*c
>  extern StaticSpinMutex CommonSanitizerReportMutex;
>  void MaybeOpenReportFile();
>  extern fd_t report_fd;
> +extern bool log_to_file;
> +extern char report_path_prefix[4096];
> +extern uptr report_fd_pid;
>
>  uptr OpenFile(const char *filename, bool write);
>  // Opens the file 'file_name" and reads up to 'max_len' bytes.
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=190030&r1=190029&r2=190030&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Wed Sep  4
> 22:19:57 2013
> @@ -197,6 +197,37 @@ char *FindPathToBinary(const char *name)
>    return 0;
>  }
>
> +void MaybeOpenReportFile() {
> +  if (!log_to_file || (report_fd_pid == internal_getpid())) return;
> +  InternalScopedBuffer<char> report_path_full(4096);
> +  internal_snprintf(report_path_full.data(), report_path_full.size(),
> +                    "%s.%d", report_path_prefix, internal_getpid());
> +  uptr openrv = OpenFile(report_path_full.data(), true);
> +  if (internal_iserror(openrv)) {
> +    report_fd = kStderrFd;
> +    log_to_file = false;
> +    Report("ERROR: Can't open file: %s\n", report_path_full.data());
> +    Die();
> +  }
> +  if (report_fd != kInvalidFd) {
> +    // We're in the child. Close the parent's log.
> +    internal_close(report_fd);
> +  }
> +  report_fd = openrv;
> +  report_fd_pid = internal_getpid();
> +}
> +
> +void RawWrite(const char *buffer) {
> +  static const char *kRawWriteError =
> +      "RawWrite can't output requested buffer!\n";
> +  uptr length = (uptr)internal_strlen(buffer);
> +  MaybeOpenReportFile();
> +  if (length != internal_write(report_fd, buffer, length)) {
> +    internal_write(report_fd, kRawWriteError,
> internal_strlen(kRawWriteError));
> +    Die();
> +  }
> +}
> +
>  }  // namespace __sanitizer
>
>  #endif  // SANITIZER_LINUX || SANITIZER_MAC
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=190030&r1=190029&r2=190030&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Wed Sep  4
> 22:19:57 2013
> @@ -398,6 +398,24 @@ void GetStackTrace(StackTrace *stack, up
>      stack->trace[i] = (uptr)tmp[i + offset];
>  }
>
> +void MaybeOpenReportFile() {
> +  // Windows doesn't have native fork, and we don't support Cygwin or
> other
> +  // environments that try to fake it, so the initial report_fd will
> always be
> +  // correct.
> +}
> +
> +void RawWrite(const char *buffer) {
> +  static const char *kRawWriteError =
> +      "RawWrite can't output requested buffer!\n";
> +  uptr length = (uptr)internal_strlen(buffer);
> +  if (length != internal_write(report_fd, buffer, length)) {
> +    // stderr may be closed, but we may be able to print to the debugger
> +    // instead.  This is the case when launching a program from Visual
> Studio,
> +    // and the following routine should write to its console.
> +    OutputDebugStringA(buffer);
> +  }
> +}
> +
>  }  // namespace __sanitizer
>
>  #endif  // _WIN32
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130905/08c9e9fb/attachment.html>


More information about the llvm-commits mailing list