Initialize gflags in signalhandler_unittest.
[platform/upstream/glog.git] / src / symbolize.h
1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: Satoru Takabayashi
3 //
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.).
8 //
9 // The algorithm used in Symbolize() is as follows.
10 //
11 //   1. Go through a list of maps in /proc/self/maps and find the map
12 //   containing the program counter.
13 //
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.
20 //
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.
25
26 #ifndef BASE_SYMBOLIZE_H_
27 #define BASE_SYMBOLIZE_H_
28
29 #include "utilities.h"
30 #include "config.h"
31 #include "glog/logging.h"
32
33 #ifdef HAVE_SYMBOLIZE
34
35 #if defined(__ELF__)  // defined by gcc on Linux
36 #include <elf.h>
37 #include <link.h>  // For ElfW() macro.
38
39 // If there is no ElfW macro, let's define it by ourself.
40 #ifndef ElfW
41 # if SIZEOF_VOID_P == 4
42 #  define ElfW(type) Elf32_##type
43 # elif SIZEOF_VOID_P == 8
44 #  define ElfW(type) Elf64_##type
45 # else
46 #  error "Unknown sizeof(void *)"
47 # endif
48 #endif
49
50 _START_GOOGLE_NAMESPACE_
51
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,
55                             ElfW(Shdr) *out);
56
57 _END_GOOGLE_NAMESPACE_
58
59 #endif  /* __ELF__ */
60
61 _START_GOOGLE_NAMESPACE_
62
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,
71                                  uint64 relocation);
72 void InstallSymbolizeCallback(SymbolizeCallback callback);
73
74 _END_GOOGLE_NAMESPACE_
75
76 #endif
77
78 _START_GOOGLE_NAMESPACE_
79
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,
83 // returns false.
84 bool Symbolize(void *pc, char *out, int out_size);
85
86 _END_GOOGLE_NAMESPACE_
87
88 #endif  // BASE_SYMBOLIZE_H_