Alexey Samsonov | 4e6c6c7 | 2012-06-04 09:33:06 +0000 | [diff] [blame] | 1 | //===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===// |
Alexey Samsonov | 2f7d826 | 2012-06-01 06:11:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Symbolizer is intended to be used by both |
| 11 | // AddressSanitizer and ThreadSanitizer to symbolize a given |
| 12 | // address. It is an analogue of addr2line utility and allows to map |
| 13 | // instruction address to a location in source code at run-time. |
| 14 | // |
| 15 | // Symbolizer is planned to use debug information (in DWARF format) |
| 16 | // in a binary via interface defined in "llvm/DebugInfo/DIContext.h" |
| 17 | // |
| 18 | // Symbolizer code should be called from the run-time library of |
| 19 | // dynamic tools, and generally should not call memory allocation |
| 20 | // routines or other system library functions intercepted by those tools. |
| 21 | // Instead, Symbolizer code should use their replacements, defined in |
| 22 | // "compiler-rt/lib/sanitizer_common/sanitizer_libc.h". |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | #ifndef SANITIZER_SYMBOLIZER_H |
| 25 | #define SANITIZER_SYMBOLIZER_H |
| 26 | |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 27 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | 2f7d826 | 2012-06-01 06:11:13 +0000 | [diff] [blame] | 28 | #include "sanitizer_libc.h" |
| 29 | // WARNING: Do not include system headers here. See details above. |
| 30 | |
| 31 | namespace __sanitizer { |
| 32 | |
| 33 | struct AddressInfo { |
| 34 | uptr address; |
| 35 | char *module; |
| 36 | uptr module_offset; |
| 37 | char *function; |
| 38 | char *file; |
| 39 | int line; |
| 40 | int column; |
| 41 | |
Alexey Samsonov | fa82b08 | 2012-06-15 14:00:25 +0000 | [diff] [blame] | 42 | AddressInfo() { |
| 43 | internal_memset(this, 0, sizeof(AddressInfo)); |
| 44 | } |
| 45 | // Deletes all strings and sets all fields to zero. |
Peter Collingbourne | c09c901 | 2013-05-27 21:00:36 +0000 | [diff] [blame] | 46 | void Clear() SANITIZER_WEAK_ATTRIBUTE; |
Alexey Samsonov | 38e853d | 2012-09-04 15:34:43 +0000 | [diff] [blame] | 47 | |
| 48 | void FillAddressAndModuleInfo(uptr addr, const char *mod_name, |
| 49 | uptr mod_offset) { |
| 50 | address = addr; |
| 51 | module = internal_strdup(mod_name); |
| 52 | module_offset = mod_offset; |
| 53 | } |
Alexey Samsonov | 2f7d826 | 2012-06-01 06:11:13 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Dmitry Vyukov | 5a1f233 | 2013-01-11 07:23:51 +0000 | [diff] [blame] | 56 | struct DataInfo { |
| 57 | uptr address; |
| 58 | char *module; |
| 59 | uptr module_offset; |
| 60 | char *name; |
| 61 | uptr start; |
| 62 | uptr size; |
| 63 | }; |
| 64 | |
Alexey Samsonov | fa82b08 | 2012-06-15 14:00:25 +0000 | [diff] [blame] | 65 | // Fills at most "max_frames" elements of "frames" with descriptions |
| 66 | // for a given address (in all inlined functions). Returns the number |
| 67 | // of descriptions actually filled. |
| 68 | // This function should NOT be called from two threads simultaneously. |
Peter Collingbourne | 8b3af3a | 2013-05-21 12:08:37 +0000 | [diff] [blame] | 69 | uptr SymbolizeCode(uptr address, AddressInfo *frames, uptr max_frames) |
| 70 | SANITIZER_WEAK_ATTRIBUTE; |
Dmitry Vyukov | 5a1f233 | 2013-01-11 07:23:51 +0000 | [diff] [blame] | 71 | bool SymbolizeData(uptr address, DataInfo *info); |
Alexey Samsonov | 2f7d826 | 2012-06-01 06:11:13 +0000 | [diff] [blame] | 72 | |
Dmitry Vyukov | 90a2467 | 2013-01-29 09:35:14 +0000 | [diff] [blame] | 73 | bool IsSymbolizerAvailable(); |
Dmitry Vyukov | 7fac284 | 2013-03-19 10:23:17 +0000 | [diff] [blame] | 74 | void FlushSymbolizer(); // releases internal caches (if any) |
Dmitry Vyukov | 90a2467 | 2013-01-29 09:35:14 +0000 | [diff] [blame] | 75 | |
Richard Smith | ab63743 | 2012-12-20 05:00:13 +0000 | [diff] [blame] | 76 | // Attempts to demangle the provided C++ mangled name. |
Alexey Samsonov | 6b30cf0 | 2013-06-28 12:30:24 +0000 | [diff] [blame] | 77 | const char *Demangle(const char *name); |
| 78 | // Attempts to demangle the name via __cxa_demangle from __cxxabiv1. |
| 79 | const char *DemangleCXXABI(const char *name); |
Richard Smith | ab63743 | 2012-12-20 05:00:13 +0000 | [diff] [blame] | 80 | |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 81 | // Starts external symbolizer program in a subprocess. Sanitizer communicates |
| 82 | // with external symbolizer via pipes. |
| 83 | bool InitializeExternalSymbolizer(const char *path_to_symbolizer); |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 84 | |
Alexey Samsonov | d64bcf4 | 2013-06-11 08:13:36 +0000 | [diff] [blame] | 85 | const int kSymbolizerStartupTimeMillis = 10; |
| 86 | |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 87 | class LoadedModule { |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 88 | public: |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 89 | LoadedModule(const char *module_name, uptr base_address); |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 90 | void addAddressRange(uptr beg, uptr end); |
| 91 | bool containsAddress(uptr address) const; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 92 | |
| 93 | const char *full_name() const { return full_name_; } |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 94 | uptr base_address() const { return base_address_; } |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 95 | |
| 96 | private: |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 97 | struct AddressRange { |
| 98 | uptr beg; |
| 99 | uptr end; |
| 100 | }; |
| 101 | char *full_name_; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 102 | uptr base_address_; |
Alexey Samsonov | e98723f | 2012-10-17 13:12:23 +0000 | [diff] [blame] | 103 | static const uptr kMaxNumberOfAddressRanges = 6; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 104 | AddressRange ranges_[kMaxNumberOfAddressRanges]; |
| 105 | uptr n_ranges_; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Alexey Samsonov | 9c6e530 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 108 | // Creates external symbolizer connected via pipe, user should write |
| 109 | // to output_fd and read from input_fd. |
| 110 | bool StartSymbolizerSubprocess(const char *path_to_symbolizer, |
| 111 | int *input_fd, int *output_fd); |
| 112 | |
| 113 | // OS-dependent function that fills array with descriptions of at most |
| 114 | // "max_modules" currently loaded modules. Returns the number of |
Sergey Matveev | d1470cb | 2013-05-14 14:04:06 +0000 | [diff] [blame] | 115 | // initialized modules. If filter is nonzero, ignores modules for which |
Kostya Serebryany | f931da8 | 2013-05-15 12:36:29 +0000 | [diff] [blame] | 116 | // filter(full_name) is false. |
Sergey Matveev | d1470cb | 2013-05-14 14:04:06 +0000 | [diff] [blame] | 117 | typedef bool (*string_predicate_t)(const char *); |
| 118 | uptr GetListOfModules(LoadedModule *modules, uptr max_modules, |
| 119 | string_predicate_t filter); |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 120 | |
Peter Collingbourne | 51c963a | 2013-05-29 12:11:43 +0000 | [diff] [blame] | 121 | void SymbolizerPrepareForSandboxing(); |
Alexander Potapenko | 5ce93fc | 2013-05-23 11:53:36 +0000 | [diff] [blame] | 122 | |
Alexey Samsonov | 2f7d826 | 2012-06-01 06:11:13 +0000 | [diff] [blame] | 123 | } // namespace __sanitizer |
| 124 | |
| 125 | #endif // SANITIZER_SYMBOLIZER_H |