Home | History | Annotate | Line # | Download | only in sanitizer_common
sanitizer_stacktrace_printer.h revision 1.1.1.9
      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   virtual const char *StripFunctionName(const char *function) = 0;
     29 
     30   virtual void RenderFrame(InternalScopedString *buffer, const char *format,
     31                            int frame_no, uptr address, const AddressInfo *info,
     32                            bool vs_style,
     33                            const char *strip_path_prefix = "") = 0;
     34 
     35   virtual bool RenderNeedsSymbolization(const char *format) = 0;
     36 
     37   virtual void RenderSourceLocation(InternalScopedString *buffer,
     38                                     const char *file, int line, int column,
     39                                     bool vs_style,
     40                                     const char *strip_path_prefix) = 0;
     41 
     42   virtual void RenderModuleLocation(InternalScopedString *buffer,
     43                                     const char *module, uptr offset,
     44                                     ModuleArch arch,
     45                                     const char *strip_path_prefix) = 0;
     46   virtual void RenderData(InternalScopedString *buffer, const char *format,
     47                           const DataInfo *DI,
     48                           const char *strip_path_prefix = "") = 0;
     49 
     50  protected:
     51   ~StackTracePrinter() {}
     52 };
     53 
     54 class FormattedStackTracePrinter : public StackTracePrinter {
     55  public:
     56   // Strip interceptor prefixes from function name.
     57   const char *StripFunctionName(const char *function) override;
     58 
     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   void RenderSourceLocation(InternalScopedString *buffer, const char *file,
     94                             int line, int column, bool vs_style,
     95                             const char *strip_path_prefix) override;
     96 
     97   void RenderModuleLocation(InternalScopedString *buffer, const char *module,
     98                             uptr offset, ModuleArch arch,
     99                             const char *strip_path_prefix) 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:
    110   ~FormattedStackTracePrinter() {}
    111 };
    112 
    113 }  // namespace __sanitizer
    114 
    115 #endif  // SANITIZER_STACKTRACE_PRINTER_H
    116