sanitizer_stacktrace_printer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===-- sanitizer_stacktrace_printer.h --------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between sanitizers' run-time libraries.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_STACKTRACE_PRINTER_H
  13. #define SANITIZER_STACKTRACE_PRINTER_H
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_internal_defs.h"
  16. #include "sanitizer_symbolizer.h"
  17. namespace __sanitizer {
  18. // StacktracePrinter is an interface that is implemented by
  19. // classes that can perform rendering of the different parts
  20. // of a stacktrace.
  21. class StackTracePrinter {
  22. public:
  23. static StackTracePrinter *GetOrInit();
  24. // Strip interceptor prefixes from function name.
  25. const char *StripFunctionName(const char *function);
  26. virtual void RenderFrame(InternalScopedString *buffer, const char *format,
  27. int frame_no, uptr address, const AddressInfo *info,
  28. bool vs_style,
  29. const char *strip_path_prefix = "") = 0;
  30. virtual bool RenderNeedsSymbolization(const char *format) = 0;
  31. void RenderSourceLocation(InternalScopedString *buffer, const char *file,
  32. int line, int column, bool vs_style,
  33. const char *strip_path_prefix);
  34. void RenderModuleLocation(InternalScopedString *buffer, const char *module,
  35. uptr offset, ModuleArch arch,
  36. const char *strip_path_prefix);
  37. virtual void RenderData(InternalScopedString *buffer, const char *format,
  38. const DataInfo *DI,
  39. const char *strip_path_prefix = "") = 0;
  40. private:
  41. // To be called from StackTracePrinter::GetOrInit
  42. static StackTracePrinter *NewStackTracePrinter();
  43. protected:
  44. ~StackTracePrinter() {}
  45. };
  46. class FormattedStackTracePrinter : public StackTracePrinter {
  47. public:
  48. // Render the contents of "info" structure, which represents the contents of
  49. // stack frame "frame_no" and appends it to the "buffer". "format" is a
  50. // string with placeholders, which is copied to the output with
  51. // placeholders substituted with the contents of "info". For example,
  52. // format string
  53. // " frame %n: function %F at %S"
  54. // will be turned into
  55. // " frame 10: function foo::bar() at my/file.cc:10"
  56. // You may additionally pass "strip_path_prefix" to strip prefixes of paths to
  57. // source files and modules.
  58. // Here's the full list of available placeholders:
  59. // %% - represents a '%' character;
  60. // %n - frame number (copy of frame_no);
  61. // %p - PC in hex format;
  62. // %m - path to module (binary or shared object);
  63. // %o - offset in the module in hex format;
  64. // %f - function name;
  65. // %q - offset in the function in hex format (*if available*);
  66. // %s - path to source file;
  67. // %l - line in the source file;
  68. // %c - column in the source file;
  69. // %F - if function is known to be <foo>, prints "in <foo>", possibly
  70. // followed by the offset in this function, but only if source file
  71. // is unknown;
  72. // %S - prints file/line/column information;
  73. // %L - prints location information: file/line/column, if it is known, or
  74. // module+offset if it is known, or (<unknown module>) string.
  75. // %M - prints module basename and offset, if it is known, or PC.
  76. void RenderFrame(InternalScopedString *buffer, const char *format,
  77. int frame_no, uptr address, const AddressInfo *info,
  78. bool vs_style, const char *strip_path_prefix = "") override;
  79. bool RenderNeedsSymbolization(const char *format) override;
  80. // Same as RenderFrame, but for data section (global variables).
  81. // Accepts %s, %l from above.
  82. // Also accepts:
  83. // %g - name of the global variable.
  84. void RenderData(InternalScopedString *buffer, const char *format,
  85. const DataInfo *DI,
  86. const char *strip_path_prefix = "") override;
  87. protected:
  88. ~FormattedStackTracePrinter() {}
  89. };
  90. } // namespace __sanitizer
  91. #endif // SANITIZER_STACKTRACE_PRINTER_H