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 15 #include "sanitizer_common.h" 16 #include "sanitizer_internal_defs.h" 17 #include "sanitizer_symbolizer.h" 18 19 namespace __sanitizer { 20 21 // StacktracePrinter is an interface that is implemented by 22 // classes that can perform rendering of the different parts 23 // of a stacktrace. 24 class StackTracePrinter { 25 public: 26 static StackTracePrinter *GetOrInit(); 27 28 // Strip interceptor prefixes from function name. 29 const char *StripFunctionName(const char *function); 30 31 virtual void RenderFrame(InternalScopedString *buffer, const char *format, 32 int frame_no, uptr address, const AddressInfo *info, 33 bool vs_style, const char *strip_path_prefix = "") { 34 // Should be pure virtual, but we can't depend on __cxa_pure_virtual. 35 UNIMPLEMENTED(); 36 } 37 RenderNeedsSymbolization(const char * format)38 virtual bool RenderNeedsSymbolization(const char *format) { 39 // Should be pure virtual, but we can't depend on __cxa_pure_virtual. 40 UNIMPLEMENTED(); 41 } 42 43 void RenderSourceLocation(InternalScopedString *buffer, const char *file, 44 int line, int column, bool vs_style, 45 const char *strip_path_prefix); 46 47 void RenderModuleLocation(InternalScopedString *buffer, const char *module, 48 uptr offset, ModuleArch arch, 49 const char *strip_path_prefix); 50 virtual void RenderData(InternalScopedString *buffer, const char *format, 51 const DataInfo *DI, 52 const char *strip_path_prefix = "") { 53 // Should be pure virtual, but we can't depend on __cxa_pure_virtual. 54 UNIMPLEMENTED(); 55 } 56 57 private: 58 // To be called from StackTracePrinter::GetOrInit 59 static StackTracePrinter *NewStackTracePrinter(); 60 61 protected: ~StackTracePrinter()62 ~StackTracePrinter() {} 63 }; 64 65 class FormattedStackTracePrinter : public StackTracePrinter { 66 public: 67 // Render the contents of "info" structure, which represents the contents of 68 // stack frame "frame_no" and appends it to the "buffer". "format" is a 69 // string with placeholders, which is copied to the output with 70 // placeholders substituted with the contents of "info". For example, 71 // format string 72 // " frame %n: function %F at %S" 73 // will be turned into 74 // " frame 10: function foo::bar() at my/file.cc:10" 75 // You may additionally pass "strip_path_prefix" to strip prefixes of paths to 76 // source files and modules. 77 // Here's the full list of available placeholders: 78 // %% - represents a '%' character; 79 // %n - frame number (copy of frame_no); 80 // %p - PC in hex format; 81 // %m - path to module (binary or shared object); 82 // %o - offset in the module in hex format; 83 // %f - function name; 84 // %q - offset in the function in hex format (*if available*); 85 // %s - path to source file; 86 // %l - line in the source file; 87 // %c - column in the source file; 88 // %F - if function is known to be <foo>, prints "in <foo>", possibly 89 // followed by the offset in this function, but only if source file 90 // is unknown; 91 // %S - prints file/line/column information; 92 // %L - prints location information: file/line/column, if it is known, or 93 // module+offset if it is known, or (<unknown module>) string. 94 // %M - prints module basename and offset, if it is known, or PC. 95 void RenderFrame(InternalScopedString *buffer, const char *format, 96 int frame_no, uptr address, const AddressInfo *info, 97 bool vs_style, const char *strip_path_prefix = "") override; 98 99 bool RenderNeedsSymbolization(const char *format) override; 100 101 // Same as RenderFrame, but for data section (global variables). 102 // Accepts %s, %l from above. 103 // Also accepts: 104 // %g - name of the global variable. 105 void RenderData(InternalScopedString *buffer, const char *format, 106 const DataInfo *DI, 107 const char *strip_path_prefix = "") override; 108 109 protected: ~FormattedStackTracePrinter()110 ~FormattedStackTracePrinter() {} 111 }; 112 113 } // namespace __sanitizer 114 115 #endif // SANITIZER_STACKTRACE_PRINTER_H 116