1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: Satoru Takabayashi
4 // This library provides Symbolize() function that symbolizes program
5 // counters to their corresponding symbol names on linux platforms.
6 // This library has a minimal implementation of an ELF symbol table
7 // reader (i.e. it doesn't depend on libelf, etc.).
9 // The algorithm used in Symbolize() is as follows.
11 // 1. Go through a list of maps in /proc/self/maps and find the map
12 // containing the program counter.
14 // 2. Open the mapped file and find a regular symbol table inside.
15 // Iterate over symbols in the symbol table and look for the symbol
16 // containing the program counter. If such a symbol is found,
17 // obtain the symbol name, and demangle the symbol if possible.
18 // If the symbol isn't found in the regular symbol table (binary is
19 // stripped), try the same thing with a dynamic symbol table.
21 // Note that Symbolize() is originally implemented to be used in
22 // FailureSignalHandler() in base/google.cc. Hence it doesn't use
23 // malloc() and other unsafe operations. It should be both
24 // thread-safe and async-signal-safe.
26 #ifndef BASE_SYMBOLIZE_H_
27 #define BASE_SYMBOLIZE_H_
29 #include "utilities.h"
31 #include "glog/logging.h"
35 #if defined(__ELF__) // defined by gcc on Linux
37 #include <link.h> // For ElfW() macro.
39 // If there is no ElfW macro, let's define it by ourself.
41 # if SIZEOF_VOID_P == 4
42 # define ElfW(type) Elf32_##type
43 # elif SIZEOF_VOID_P == 8
44 # define ElfW(type) Elf64_##type
46 # error "Unknown sizeof(void *)"
50 _START_GOOGLE_NAMESPACE_
52 // Gets the section header for the given name, if it exists. Returns true on
53 // success. Otherwise, returns false.
54 bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
57 _END_GOOGLE_NAMESPACE_
61 _START_GOOGLE_NAMESPACE_
63 // Installs a callback function, which will be called right before a symbol name
64 // is printed. The callback is intended to be used for showing a file name and a
65 // line number preceding a symbol name.
66 // "fd" is a file descriptor of the object file containing the program
67 // counter "pc". The callback function should write output to "out"
68 // and return the size of the output written. On error, the callback
69 // function should return -1.
70 typedef int (*SymbolizeCallback)(int fd, void *pc, char *out, size_t out_size,
72 void InstallSymbolizeCallback(SymbolizeCallback callback);
74 _END_GOOGLE_NAMESPACE_
78 _START_GOOGLE_NAMESPACE_
80 // Symbolizes a program counter. On success, returns true and write the
81 // symbol name to "out". The symbol name is demangled if possible
82 // (supports symbols generated by GCC 3.x or newer). Otherwise,
84 bool Symbolize(void *pc, char *out, int out_size);
86 _END_GOOGLE_NAMESPACE_
88 #endif // BASE_SYMBOLIZE_H_