1 //===-- sanitizer_stacktrace_printer.h --------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between sanitizers' run-time libraries.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_STACKTRACE_PRINTER_H
13 #define SANITIZER_STACKTRACE_PRINTER_H
15 #include "sanitizer_common.h"
16 #include "sanitizer_internal_defs.h"
17 #include "sanitizer_symbolizer.h"
19 namespace __sanitizer
{
21 // StacktracePrinter is an interface that is implemented by
22 // classes that can perform rendering of the different parts
24 class StackTracePrinter
{
26 static StackTracePrinter
*GetOrInit();
28 virtual const char *StripFunctionName(const char *function
) = 0;
30 virtual void RenderFrame(InternalScopedString
*buffer
, const char *format
,
31 int frame_no
, uptr address
, const AddressInfo
*info
,
33 const char *strip_path_prefix
= "") = 0;
35 virtual bool RenderNeedsSymbolization(const char *format
) = 0;
37 virtual void RenderSourceLocation(InternalScopedString
*buffer
,
38 const char *file
, int line
, int column
,
40 const char *strip_path_prefix
) = 0;
42 virtual void RenderModuleLocation(InternalScopedString
*buffer
,
43 const char *module
, uptr offset
,
45 const char *strip_path_prefix
) = 0;
46 virtual void RenderData(InternalScopedString
*buffer
, const char *format
,
48 const char *strip_path_prefix
= "") = 0;
51 ~StackTracePrinter() {}
54 class FormattedStackTracePrinter
: public StackTracePrinter
{
56 // Strip interceptor prefixes from function name.
57 const char *StripFunctionName(const char *function
) override
;
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,
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
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
;
91 bool RenderNeedsSymbolization(const char *format
) override
;
93 void RenderSourceLocation(InternalScopedString
*buffer
, const char *file
,
94 int line
, int column
, bool vs_style
,
95 const char *strip_path_prefix
) override
;
97 void RenderModuleLocation(InternalScopedString
*buffer
, const char *module
,
98 uptr offset
, ModuleArch arch
,
99 const char *strip_path_prefix
) override
;
101 // Same as RenderFrame, but for data section (global variables).
102 // Accepts %s, %l from above.
104 // %g - name of the global variable.
105 void RenderData(InternalScopedString
*buffer
, const char *format
,
107 const char *strip_path_prefix
= "") override
;
110 ~FormattedStackTracePrinter() {}
113 } // namespace __sanitizer
115 #endif // SANITIZER_STACKTRACE_PRINTER_H