symbolize.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright (c) 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: Satoru Takabayashi
  31. //
  32. // This library provides Symbolize() function that symbolizes program
  33. // counters to their corresponding symbol names on linux platforms.
  34. // This library has a minimal implementation of an ELF symbol table
  35. // reader (i.e. it doesn't depend on libelf, etc.).
  36. //
  37. // The algorithm used in Symbolize() is as follows.
  38. //
  39. // 1. Go through a list of maps in /proc/self/maps and find the map
  40. // containing the program counter.
  41. //
  42. // 2. Open the mapped file and find a regular symbol table inside.
  43. // Iterate over symbols in the symbol table and look for the symbol
  44. // containing the program counter. If such a symbol is found,
  45. // obtain the symbol name, and demangle the symbol if possible.
  46. // If the symbol isn't found in the regular symbol table (binary is
  47. // stripped), try the same thing with a dynamic symbol table.
  48. //
  49. // Note that Symbolize() is originally implemented to be used in
  50. // FailureSignalHandler() in base/google.cc. Hence it doesn't use
  51. // malloc() and other unsafe operations. It should be both
  52. // thread-safe and async-signal-safe.
  53. #ifndef BASE_SYMBOLIZE_H_
  54. #define BASE_SYMBOLIZE_H_
  55. #include "utilities.h"
  56. #include "config.h"
  57. #include "glog/logging.h"
  58. #ifdef HAVE_SYMBOLIZE
  59. #include <algorithm>
  60. #include <utility>
  61. #if defined(__ELF__) // defined by gcc
  62. #if defined(__OpenBSD__)
  63. #include <sys/exec_elf.h>
  64. #else
  65. #include <elf.h>
  66. #endif
  67. #if !defined(ANDROID)
  68. #include <link.h> // For ElfW() macro.
  69. #endif
  70. // For systems where SIZEOF_VOID_P is not defined, determine it
  71. // based on __LP64__ (defined by gcc on 64-bit systems)
  72. #if !defined(SIZEOF_VOID_P)
  73. # if defined(__LP64__)
  74. # define SIZEOF_VOID_P 8
  75. # else
  76. # define SIZEOF_VOID_P 4
  77. # endif
  78. #endif
  79. // If there is no ElfW macro, let's define it by ourself.
  80. #ifndef ElfW
  81. # if SIZEOF_VOID_P == 4
  82. # define ElfW(type) Elf32_##type
  83. # elif SIZEOF_VOID_P == 8
  84. # define ElfW(type) Elf64_##type
  85. # else
  86. # error "Unknown sizeof(void *)"
  87. # endif
  88. #endif
  89. _START_GOOGLE_NAMESPACE_
  90. // Gets the section header for the given name, if it exists. Returns true on
  91. // success. Otherwise, returns false.
  92. bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
  93. ElfW(Shdr) *out);
  94. int OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc,
  95. uint64_t& start_address,
  96. uint64_t& end_address,
  97. uint64_t& base_address,
  98. char* out_file_name,
  99. int out_file_name_size);
  100. ssize_t ReadFromOffset(const int fd,
  101. void* buf,
  102. const size_t count,
  103. const off_t offset);
  104. // Thin wrapper around a file descriptor so that the file descriptor
  105. // gets closed for sure.
  106. struct FileDescriptor {
  107. int fd_;
  108. explicit FileDescriptor(int fd);
  109. ~FileDescriptor();
  110. FileDescriptor(FileDescriptor&& other) : fd_(std::exchange(other.fd_, -1)) {}
  111. FileDescriptor& operator=(FileDescriptor&& rhs) {
  112. if (&rhs != this) {
  113. fd_ = std::exchange(rhs.fd_, -1);
  114. }
  115. return *this;
  116. }
  117. FileDescriptor(const FileDescriptor&) = delete;
  118. void operator=(const FileDescriptor&) = delete;
  119. int get() { return fd_; }
  120. };
  121. char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding);
  122. _END_GOOGLE_NAMESPACE_
  123. #endif /* __ELF__ */
  124. _START_GOOGLE_NAMESPACE_
  125. // Restrictions on the callbacks that follow:
  126. // - The callbacks must not use heaps but only use stacks.
  127. // - The callbacks must be async-signal-safe.
  128. // Installs a callback function, which will be called right before a symbol name
  129. // is printed. The callback is intended to be used for showing a file name and a
  130. // line number preceding a symbol name.
  131. // "fd" is a file descriptor of the object file containing the program
  132. // counter "pc". The callback function should write output to "out"
  133. // and return the size of the output written. On error, the callback
  134. // function should return -1.
  135. typedef int (*SymbolizeCallback)(int fd,
  136. void* pc,
  137. char* out,
  138. size_t out_size,
  139. uint64_t relocation);
  140. void InstallSymbolizeCallback(SymbolizeCallback callback);
  141. // Installs a callback function, which will be called instead of
  142. // OpenObjectFileContainingPcAndGetStartAddress. The callback is expected
  143. // to searches for the object file (from /proc/self/maps) that contains
  144. // the specified pc. If found, sets |start_address| to the start address
  145. // of where this object file is mapped in memory, sets the module base
  146. // address into |base_address|, copies the object file name into
  147. // |out_file_name|, and attempts to open the object file. If the object
  148. // file is opened successfully, returns the file descriptor. Otherwise,
  149. // returns -1. |out_file_name_size| is the size of the file name buffer
  150. // (including the null-terminator).
  151. typedef int (*SymbolizeOpenObjectFileCallback)(uint64_t pc,
  152. uint64_t& start_address,
  153. uint64_t& base_address,
  154. char* out_file_name,
  155. int out_file_name_size);
  156. void InstallSymbolizeOpenObjectFileCallback(
  157. SymbolizeOpenObjectFileCallback callback);
  158. _END_GOOGLE_NAMESPACE_
  159. #endif
  160. _START_GOOGLE_NAMESPACE_
  161. // Symbolizes a program counter. On success, returns true and write the
  162. // symbol name to "out". The symbol name is demangled if possible
  163. // (supports symbols generated by GCC 3.x or newer). Otherwise,
  164. // returns false.
  165. GOOGLE_GLOG_DLL_DECL bool Symbolize(void *pc, char *out, int out_size);
  166. _END_GOOGLE_NAMESPACE_
  167. #endif // BASE_SYMBOLIZE_H_