/thirdparty/breakpad/third_party/glog/src/symbolize.h

http://github.com/tomahawk-player/tomahawk · C Header · 116 lines · 33 code · 17 blank · 66 comment · 3 complexity · 279bb9637647108f8869118827a43e9b MD5 · raw file

  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. #if defined(__ELF__) // defined by gcc on Linux
  60. #include <elf.h>
  61. #include <link.h> // For ElfW() macro.
  62. // If there is no ElfW macro, let's define it by ourself.
  63. #ifndef ElfW
  64. # if SIZEOF_VOID_P == 4
  65. # define ElfW(type) Elf32_##type
  66. # elif SIZEOF_VOID_P == 8
  67. # define ElfW(type) Elf64_##type
  68. # else
  69. # error "Unknown sizeof(void *)"
  70. # endif
  71. #endif
  72. _START_GOOGLE_NAMESPACE_
  73. // Gets the section header for the given name, if it exists. Returns true on
  74. // success. Otherwise, returns false.
  75. bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
  76. ElfW(Shdr) *out);
  77. _END_GOOGLE_NAMESPACE_
  78. #endif /* __ELF__ */
  79. _START_GOOGLE_NAMESPACE_
  80. // Installs a callback function, which will be called right before a symbol name
  81. // is printed. The callback is intended to be used for showing a file name and a
  82. // line number preceding a symbol name.
  83. // "fd" is a file descriptor of the object file containing the program
  84. // counter "pc". The callback function should write output to "out"
  85. // and return the size of the output written. On error, the callback
  86. // function should return -1.
  87. typedef int (*SymbolizeCallback)(int fd, void *pc, char *out, size_t out_size,
  88. uint64 relocation);
  89. void InstallSymbolizeCallback(SymbolizeCallback callback);
  90. _END_GOOGLE_NAMESPACE_
  91. #endif
  92. _START_GOOGLE_NAMESPACE_
  93. // Symbolizes a program counter. On success, returns true and write the
  94. // symbol name to "out". The symbol name is demangled if possible
  95. // (supports symbols generated by GCC 3.x or newer). Otherwise,
  96. // returns false.
  97. bool Symbolize(void *pc, char *out, int out_size);
  98. _END_GOOGLE_NAMESPACE_
  99. #endif // BASE_SYMBOLIZE_H_