sanitizer_common_libcdep.cc revision 1.1.1.1
1//===-- sanitizer_common_libcdep.cc ---------------------------------------===//
2//
3// This file is distributed under the University of Illinois Open Source
4// License. See LICENSE.TXT for details.
5//
6//===----------------------------------------------------------------------===//
7//
8// This file is shared between AddressSanitizer and ThreadSanitizer
9// run-time libraries.
10//===----------------------------------------------------------------------===//
11
12#include "sanitizer_common.h"
13#include "sanitizer_flags.h"
14#include "sanitizer_stacktrace.h"
15#include "sanitizer_symbolizer.h"
16
17namespace __sanitizer {
18
19bool PrintsToTty() {
20  MaybeOpenReportFile();
21  return internal_isatty(report_fd) != 0;
22}
23
24bool PrintsToTtyCached() {
25  // FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color
26  // printing on Windows.
27  if (SANITIZER_WINDOWS)
28    return 0;
29
30  static int cached = 0;
31  static bool prints_to_tty;
32  if (!cached) {  // Not thread-safe.
33    prints_to_tty = PrintsToTty();
34    cached = 1;
35  }
36  return prints_to_tty;
37}
38
39bool ColorizeReports() {
40  const char *flag = common_flags()->color;
41  return internal_strcmp(flag, "always") == 0 ||
42         (internal_strcmp(flag, "auto") == 0 && PrintsToTtyCached());
43}
44
45static void (*sandboxing_callback)();
46void SetSandboxingCallback(void (*f)()) {
47  sandboxing_callback = f;
48}
49
50void ReportErrorSummary(const char *error_type, StackTrace *stack) {
51  if (!common_flags()->print_summary)
52    return;
53  AddressInfo ai;
54#if !SANITIZER_GO
55  if (stack->size > 0 && Symbolizer::GetOrInit()->CanReturnFileLineInfo()) {
56    // Currently, we include the first stack frame into the report summary.
57    // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
58    uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
59    Symbolizer::GetOrInit()->SymbolizePC(pc, &ai, 1);
60  }
61#endif
62  ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
63}
64
65}  // namespace __sanitizer
66
67void NOINLINE
68__sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args) {
69  PrepareForSandboxing(args);
70  if (sandboxing_callback)
71    sandboxing_callback();
72}
73