blob: b6471db6a687d56f1376586aa227163fec873d9c [file] [log] [blame]
Alexey Samsonov4e6c6c72012-06-04 09:33:06 +00001//===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===//
Alexey Samsonov2f7d8262012-06-01 06:11:13 +00002//
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 Samsonov94b50362012-06-05 14:25:27 +000027#include "sanitizer_internal_defs.h"
Alexey Samsonov2f7d8262012-06-01 06:11:13 +000028#include "sanitizer_libc.h"
29// WARNING: Do not include system headers here. See details above.
30
31namespace __sanitizer {
32
33struct 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 Samsonovfa82b082012-06-15 14:00:25 +000042 AddressInfo() {
43 internal_memset(this, 0, sizeof(AddressInfo));
44 }
45 // Deletes all strings and sets all fields to zero.
Peter Collingbournec09c9012013-05-27 21:00:36 +000046 void Clear() SANITIZER_WEAK_ATTRIBUTE;
Alexey Samsonov38e853d2012-09-04 15:34:43 +000047
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 Samsonov2f7d8262012-06-01 06:11:13 +000054};
55
Dmitry Vyukov5a1f2332013-01-11 07:23:51 +000056struct DataInfo {
57 uptr address;
58 char *module;
59 uptr module_offset;
60 char *name;
61 uptr start;
62 uptr size;
63};
64
Alexey Samsonovfa82b082012-06-15 14:00:25 +000065// 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 Collingbourne8b3af3a2013-05-21 12:08:37 +000069uptr SymbolizeCode(uptr address, AddressInfo *frames, uptr max_frames)
70 SANITIZER_WEAK_ATTRIBUTE;
Dmitry Vyukov5a1f2332013-01-11 07:23:51 +000071bool SymbolizeData(uptr address, DataInfo *info);
Alexey Samsonov2f7d8262012-06-01 06:11:13 +000072
Dmitry Vyukov90a24672013-01-29 09:35:14 +000073bool IsSymbolizerAvailable();
Dmitry Vyukov7fac2842013-03-19 10:23:17 +000074void FlushSymbolizer(); // releases internal caches (if any)
Dmitry Vyukov90a24672013-01-29 09:35:14 +000075
Richard Smithab637432012-12-20 05:00:13 +000076// Attempts to demangle the provided C++ mangled name.
77const char *Demangle(const char *Name);
78
Alexey Samsonov9c6e5302012-08-23 07:32:06 +000079// Starts external symbolizer program in a subprocess. Sanitizer communicates
80// with external symbolizer via pipes.
81bool InitializeExternalSymbolizer(const char *path_to_symbolizer);
Alexey Samsonova68633f2012-07-03 08:24:14 +000082
Alexey Samsonov9c6e5302012-08-23 07:32:06 +000083class LoadedModule {
Alexey Samsonova68633f2012-07-03 08:24:14 +000084 public:
Alexey Samsonov9c6e5302012-08-23 07:32:06 +000085 LoadedModule(const char *module_name, uptr base_address);
Alexey Samsonova68633f2012-07-03 08:24:14 +000086 void addAddressRange(uptr beg, uptr end);
87 bool containsAddress(uptr address) const;
Alexey Samsonova68633f2012-07-03 08:24:14 +000088
89 const char *full_name() const { return full_name_; }
Alexey Samsonov9c6e5302012-08-23 07:32:06 +000090 uptr base_address() const { return base_address_; }
Alexey Samsonova68633f2012-07-03 08:24:14 +000091
92 private:
Alexey Samsonova68633f2012-07-03 08:24:14 +000093 struct AddressRange {
94 uptr beg;
95 uptr end;
96 };
97 char *full_name_;
Alexey Samsonova68633f2012-07-03 08:24:14 +000098 uptr base_address_;
Alexey Samsonove98723f2012-10-17 13:12:23 +000099 static const uptr kMaxNumberOfAddressRanges = 6;
Alexey Samsonova68633f2012-07-03 08:24:14 +0000100 AddressRange ranges_[kMaxNumberOfAddressRanges];
101 uptr n_ranges_;
Alexey Samsonova68633f2012-07-03 08:24:14 +0000102};
103
Alexey Samsonov9c6e5302012-08-23 07:32:06 +0000104// Creates external symbolizer connected via pipe, user should write
105// to output_fd and read from input_fd.
106bool StartSymbolizerSubprocess(const char *path_to_symbolizer,
107 int *input_fd, int *output_fd);
108
109// OS-dependent function that fills array with descriptions of at most
110// "max_modules" currently loaded modules. Returns the number of
Sergey Matveevd1470cb2013-05-14 14:04:06 +0000111// initialized modules. If filter is nonzero, ignores modules for which
Kostya Serebryanyf931da82013-05-15 12:36:29 +0000112// filter(full_name) is false.
Sergey Matveevd1470cb2013-05-14 14:04:06 +0000113typedef bool (*string_predicate_t)(const char *);
114uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
115 string_predicate_t filter);
Alexey Samsonova68633f2012-07-03 08:24:14 +0000116
Alexander Potapenko5ce93fc2013-05-23 11:53:36 +0000117void SymbolizerPrepareForSandboxing();
118
Alexey Samsonov2f7d8262012-06-01 06:11:13 +0000119} // namespace __sanitizer
120
121#endif // SANITIZER_SYMBOLIZER_H