sanitizer_symbolizer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Symbolizer is used by sanitizers to map instruction address to a location in
  9. // source code at run-time. Symbolizer either uses __sanitizer_symbolize_*
  10. // defined in the program, or (if they are missing) tries to find and
  11. // launch "llvm-symbolizer" commandline tool in a separate process and
  12. // communicate with it.
  13. //
  14. // Generally we should try to avoid calling system library functions during
  15. // symbolization (and use their replacements from sanitizer_libc.h instead).
  16. //===----------------------------------------------------------------------===//
  17. #ifndef SANITIZER_SYMBOLIZER_H
  18. #define SANITIZER_SYMBOLIZER_H
  19. #include "sanitizer_allocator_internal.h"
  20. #include "sanitizer_internal_defs.h"
  21. #include "sanitizer_libc.h"
  22. namespace __sanitizer {
  23. struct AddressInfo {
  24. uptr address;
  25. char *module;
  26. uptr module_offset;
  27. static const uptr kUnknown = ~(uptr)0;
  28. char *function;
  29. uptr function_offset;
  30. char *file;
  31. int line;
  32. int column;
  33. AddressInfo() {
  34. internal_memset(this, 0, sizeof(AddressInfo));
  35. function_offset = kUnknown;
  36. }
  37. // Deletes all strings and resets all fields.
  38. void Clear() {
  39. InternalFree(module);
  40. InternalFree(function);
  41. InternalFree(file);
  42. internal_memset(this, 0, sizeof(AddressInfo));
  43. function_offset = kUnknown;
  44. }
  45. void FillAddressAndModuleInfo(uptr addr, const char *mod_name,
  46. uptr mod_offset) {
  47. address = addr;
  48. module = internal_strdup(mod_name);
  49. module_offset = mod_offset;
  50. }
  51. };
  52. // For now, DataInfo is used to describe global variable.
  53. struct DataInfo {
  54. char *module;
  55. uptr module_offset;
  56. char *name;
  57. uptr start;
  58. uptr size;
  59. DataInfo() {
  60. internal_memset(this, 0, sizeof(DataInfo));
  61. }
  62. void Clear() {
  63. InternalFree(module);
  64. InternalFree(name);
  65. internal_memset(this, 0, sizeof(DataInfo));
  66. }
  67. };
  68. class Symbolizer {
  69. public:
  70. /// Initialize and return platform-specific implementation of symbolizer
  71. /// (if it wasn't already initialized).
  72. static Symbolizer *GetOrInit();
  73. // Fills at most "max_frames" elements of "frames" with descriptions
  74. // for a given address (in all inlined functions). Returns the number
  75. // of descriptions actually filled.
  76. virtual uptr SymbolizePC(uptr address, AddressInfo *frames, uptr max_frames) {
  77. return 0;
  78. }
  79. virtual bool SymbolizeData(uptr address, DataInfo *info) {
  80. return false;
  81. }
  82. virtual bool GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
  83. uptr *module_address) {
  84. return false;
  85. }
  86. virtual bool CanReturnFileLineInfo() {
  87. return false;
  88. }
  89. // Release internal caches (if any).
  90. virtual void Flush() {}
  91. // Attempts to demangle the provided C++ mangled name.
  92. virtual const char *Demangle(const char *name) {
  93. return name;
  94. }
  95. virtual void PrepareForSandboxing() {}
  96. // Allow user to install hooks that would be called before/after Symbolizer
  97. // does the actual file/line info fetching. Specific sanitizers may need this
  98. // to distinguish system library calls made in user code from calls made
  99. // during in-process symbolization.
  100. typedef void (*StartSymbolizationHook)();
  101. typedef void (*EndSymbolizationHook)();
  102. // May be called at most once.
  103. void AddHooks(StartSymbolizationHook start_hook,
  104. EndSymbolizationHook end_hook);
  105. private:
  106. /// Platform-specific function for creating a Symbolizer object.
  107. static Symbolizer *PlatformInit();
  108. /// Initialize the symbolizer in a disabled state. Not thread safe.
  109. static Symbolizer *Disable();
  110. static Symbolizer *symbolizer_;
  111. static StaticSpinMutex init_mu_;
  112. protected:
  113. Symbolizer();
  114. static LowLevelAllocator symbolizer_allocator_;
  115. StartSymbolizationHook start_hook_;
  116. EndSymbolizationHook end_hook_;
  117. class SymbolizerScope {
  118. public:
  119. explicit SymbolizerScope(const Symbolizer *sym);
  120. ~SymbolizerScope();
  121. private:
  122. const Symbolizer *sym_;
  123. };
  124. };
  125. } // namespace __sanitizer
  126. #endif // SANITIZER_SYMBOLIZER_H