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