sanitizer_common_libcdep.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "sanitizer_common.h"
  12. #include "sanitizer_flags.h"
  13. #include "sanitizer_stacktrace.h"
  14. #include "sanitizer_symbolizer.h"
  15. namespace __sanitizer {
  16. bool PrintsToTty() {
  17. MaybeOpenReportFile();
  18. return internal_isatty(report_fd) != 0;
  19. }
  20. bool PrintsToTtyCached() {
  21. // FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color
  22. // printing on Windows.
  23. if (SANITIZER_WINDOWS)
  24. return 0;
  25. static int cached = 0;
  26. static bool prints_to_tty;
  27. if (!cached) { // Not thread-safe.
  28. prints_to_tty = PrintsToTty();
  29. cached = 1;
  30. }
  31. return prints_to_tty;
  32. }
  33. bool ColorizeReports() {
  34. const char *flag = common_flags()->color;
  35. return internal_strcmp(flag, "always") == 0 ||
  36. (internal_strcmp(flag, "auto") == 0 && PrintsToTtyCached());
  37. }
  38. static void (*sandboxing_callback)();
  39. void SetSandboxingCallback(void (*f)()) {
  40. sandboxing_callback = f;
  41. }
  42. void ReportErrorSummary(const char *error_type, StackTrace *stack) {
  43. if (!common_flags()->print_summary)
  44. return;
  45. AddressInfo ai;
  46. #if !SANITIZER_GO
  47. if (stack->size > 0 && Symbolizer::GetOrInit()->CanReturnFileLineInfo()) {
  48. // Currently, we include the first stack frame into the report summary.
  49. // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
  50. uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
  51. Symbolizer::GetOrInit()->SymbolizePC(pc, &ai, 1);
  52. }
  53. #endif
  54. ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
  55. }
  56. } // namespace __sanitizer
  57. void NOINLINE
  58. __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args) {
  59. PrepareForSandboxing(args);
  60. if (sandboxing_callback)
  61. sandboxing_callback();
  62. }