From 58358897a3cff212d6dd618286175ac2e82841a0 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Fri, 2 Nov 2012 15:18:34 +0000 Subject: [PATCH] [Sanitizer] Add internal_isatty to sanitizer_libc and PrintsToTty to determine whether error reports are printed to terminal llvm-svn: 167298 --- .../lib/sanitizer_common/sanitizer_common.cc | 27 ++++++++++++------- .../lib/sanitizer_common/sanitizer_common.h | 1 + .../lib/sanitizer_common/sanitizer_libc.h | 1 + .../lib/sanitizer_common/sanitizer_posix.cc | 4 +++ .../lib/sanitizer_common/sanitizer_win.cc | 4 +++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc index d56cfdafd201..e0f9997e49d2 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc @@ -48,18 +48,27 @@ void NORETURN CheckFailed(const char *file, int line, const char *cond, Die(); } +static void MaybeOpenReportFile() { + if (report_fd != kInvalidFd) + return; + fd_t fd = internal_open(report_path, true); + if (fd == kInvalidFd) { + report_fd = kStderrFd; + Report("ERROR: Can't open file: %s\n", report_path); + Die(); + } + report_fd = fd; +} + +bool PrintsToTty() { + MaybeOpenReportFile(); + return internal_isatty(report_fd); +} + void RawWrite(const char *buffer) { static const char *kRawWriteError = "RawWrite can't output requested buffer!"; uptr length = (uptr)internal_strlen(buffer); - if (report_fd == kInvalidFd) { - fd_t fd = internal_open(report_path, true); - if (fd == kInvalidFd) { - report_fd = kStderrFd; - Report("ERROR: Can't open file: %s\n", report_path); - Die(); - } - report_fd = fd; - } + MaybeOpenReportFile(); if (length != internal_write(report_fd, buffer, length)) { internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError)); Die(); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 323b0667e2ff..b8c697d871ca 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -98,6 +98,7 @@ void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback); // IO void RawWrite(const char *buffer); +bool PrintsToTty(); void Printf(const char *format, ...); void Report(const char *format, ...); void SetPrintfAndReportCallback(void (*callback)(const char *)); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h index af16cff5d828..79794839d28b 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h @@ -59,6 +59,7 @@ const fd_t kStdinFd = 0; const fd_t kStdoutFd = 1; const fd_t kStderrFd = 2; int internal_close(fd_t fd); +int internal_isatty(fd_t fd); fd_t internal_open(const char *filename, bool write); uptr internal_read(fd_t fd, void *buf, uptr count); uptr internal_write(fd_t fd, const void *buf, uptr count); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc index 30325db9e8e9..035ddbcdb0d6 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc @@ -184,6 +184,10 @@ int Atexit(void (*function)(void)) { #endif } +int internal_isatty(fd_t fd) { + return isatty(fd); +} + } // namespace __sanitizer #endif // __linux__ || __APPLE_ diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc index 8919341f0be2..64b220255a8b 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc @@ -154,6 +154,10 @@ int internal_close(fd_t fd) { UNIMPLEMENTED(); } +int internal_isatty(fd_t fd) { + UNIMPLEMENTED(); +} + fd_t internal_open(const char *filename, bool write) { UNIMPLEMENTED(); }