[Sanitizer] Add internal_isatty to sanitizer_libc and PrintsToTty to determine whether error reports are printed to terminal

llvm-svn: 167298
This commit is contained in:
Alexey Samsonov 2012-11-02 15:18:34 +00:00
parent f7a24c4e2b
commit 58358897a3
5 changed files with 28 additions and 9 deletions

View File

@ -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();

View File

@ -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 *));

View File

@ -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);

View File

@ -184,6 +184,10 @@ int Atexit(void (*function)(void)) {
#endif
}
int internal_isatty(fd_t fd) {
return isatty(fd);
}
} // namespace __sanitizer
#endif // __linux__ || __APPLE_

View File

@ -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();
}