14e6c6c75a241644c52fd6e62ff3ad7aef7f5c523Alexey Samsonov//===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===//
22f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//
32f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//                     The LLVM Compiler Infrastructure
42f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//
52f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// This file is distributed under the University of Illinois Open Source
62f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// License. See LICENSE.TXT for details.
72f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//
82f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//===----------------------------------------------------------------------===//
92f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//
102f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// Symbolizer is intended to be used by both
112f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// AddressSanitizer and ThreadSanitizer to symbolize a given
122f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// address. It is an analogue of addr2line utility and allows to map
132f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// instruction address to a location in source code at run-time.
142f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//
152f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// Symbolizer is planned to use debug information (in DWARF format)
162f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// in a binary via interface defined in "llvm/DebugInfo/DIContext.h"
172f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//
182f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// Symbolizer code should be called from the run-time library of
192f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// dynamic tools, and generally should not call memory allocation
202f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// routines or other system library functions intercepted by those tools.
212f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// Instead, Symbolizer code should use their replacements, defined in
222f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// "compiler-rt/lib/sanitizer_common/sanitizer_libc.h".
232f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov//===----------------------------------------------------------------------===//
242f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov#ifndef SANITIZER_SYMBOLIZER_H
252f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov#define SANITIZER_SYMBOLIZER_H
262f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov
2794b5036ee6ba866e1702848855b6d687d1e70afaAlexey Samsonov#include "sanitizer_internal_defs.h"
282f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov#include "sanitizer_libc.h"
292f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov// WARNING: Do not include system headers here. See details above.
302f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov
312f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonovnamespace __sanitizer {
322f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov
332f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonovstruct AddressInfo {
342f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  uptr address;
352f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  char *module;
362f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  uptr module_offset;
372f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  char *function;
382f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  char *file;
392f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  int line;
402f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  int column;
412f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov
42fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov  AddressInfo() {
43fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov    internal_memset(this, 0, sizeof(AddressInfo));
44fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov  }
45fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov  // Deletes all strings and sets all fields to zero.
462f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov  void Clear();
4738e853db8353285da16416885da42219fee701c4Alexey Samsonov
4838e853db8353285da16416885da42219fee701c4Alexey Samsonov  void FillAddressAndModuleInfo(uptr addr, const char *mod_name,
4938e853db8353285da16416885da42219fee701c4Alexey Samsonov                                uptr mod_offset) {
5038e853db8353285da16416885da42219fee701c4Alexey Samsonov    address = addr;
5138e853db8353285da16416885da42219fee701c4Alexey Samsonov    module = internal_strdup(mod_name);
5238e853db8353285da16416885da42219fee701c4Alexey Samsonov    module_offset = mod_offset;
5338e853db8353285da16416885da42219fee701c4Alexey Samsonov  }
542f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov};
552f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov
56fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov// Fills at most "max_frames" elements of "frames" with descriptions
57fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov// for a given address (in all inlined functions). Returns the number
58fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov// of descriptions actually filled.
59fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonov// This function should NOT be called from two threads simultaneously.
60fa82b0887fd558b77a856730a0656460d5bf078dAlexey Samsonovuptr SymbolizeCode(uptr address, AddressInfo *frames, uptr max_frames);
612f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov
629c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov// Starts external symbolizer program in a subprocess. Sanitizer communicates
639c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov// with external symbolizer via pipes.
649c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonovbool InitializeExternalSymbolizer(const char *path_to_symbolizer);
6541df5652a12b45998111ca2aca09fc0c63674684Alexey Samsonov
669c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonovclass LoadedModule {
67a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov public:
689c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov  LoadedModule(const char *module_name, uptr base_address);
69a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  void addAddressRange(uptr beg, uptr end);
70a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  bool containsAddress(uptr address) const;
71a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov
72a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  const char *full_name() const { return full_name_; }
739c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov  uptr base_address() const { return base_address_; }
74a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov
75a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov private:
76a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  struct AddressRange {
77a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov    uptr beg;
78a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov    uptr end;
79a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  };
80a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  char *full_name_;
81a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  uptr base_address_;
8275983dd851e39e5ba70edeeda19cfaedbd114cb3Alexey Samsonov  static const uptr kMaxNumberOfAddressRanges = 8;
83a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  AddressRange ranges_[kMaxNumberOfAddressRanges];
84a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov  uptr n_ranges_;
85a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov};
86a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov
879c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov// Creates external symbolizer connected via pipe, user should write
889c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov// to output_fd and read from input_fd.
899c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonovbool StartSymbolizerSubprocess(const char *path_to_symbolizer,
909c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov                               int *input_fd, int *output_fd);
919c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov
929c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov// OS-dependent function that fills array with descriptions of at most
939c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov// "max_modules" currently loaded modules. Returns the number of
949c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonov// initialized modules.
959c6e5303fa025a73a09cee0766ae403909b1bb8bAlexey Samsonovuptr GetListOfModules(LoadedModule *modules, uptr max_modules);
96a68633fb76208137ccb807914df52758ee5ca6f0Alexey Samsonov
972f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov}  // namespace __sanitizer
982f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov
992f7d82687c99d468aa845ed68d88910710cc51ecAlexey Samsonov#endif  // SANITIZER_SYMBOLIZER_H
100