You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

symbolize.h 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // This file contains internal parts of the Abseil symbolizer.
  15. // Do not depend on the anything in this file, it may change at anytime.
  16. #ifndef ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_
  17. #define ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_
  18. #ifdef __cplusplus
  19. #include <cstddef>
  20. #include <cstdint>
  21. #include "absl/base/config.h"
  22. #include "absl/strings/string_view.h"
  23. #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
  24. #error ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE cannot be directly set
  25. #elif defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) && !defined(__asmjs__) && !defined(__wasm__)
  26. #define ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 1
  27. #include <elf.h>
  28. #include <link.h> // For ElfW() macro.
  29. #include <functional>
  30. #include <string>
  31. namespace absl
  32. {
  33. ABSL_NAMESPACE_BEGIN
  34. namespace debugging_internal
  35. {
  36. // Iterates over all sections, invoking callback on each with the section name
  37. // and the section header.
  38. //
  39. // Returns true on success; otherwise returns false in case of errors.
  40. //
  41. // This is not async-signal-safe.
  42. bool ForEachSection(int fd, const std::function<bool(absl::string_view name, const ElfW(Shdr) &)>& callback);
  43. // Gets the section header for the given name, if it exists. Returns true on
  44. // success. Otherwise, returns false.
  45. bool GetSectionHeaderByName(int fd, const char* name, size_t name_len, ElfW(Shdr) * out);
  46. } // namespace debugging_internal
  47. ABSL_NAMESPACE_END
  48. } // namespace absl
  49. #endif // ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
  50. #ifdef ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE
  51. #error ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE cannot be directly set
  52. #elif defined(__APPLE__)
  53. #define ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE 1
  54. #endif
  55. #ifdef ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE
  56. #error ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE cannot be directly set
  57. #elif defined(__EMSCRIPTEN__)
  58. #define ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE 1
  59. #endif
  60. namespace absl
  61. {
  62. ABSL_NAMESPACE_BEGIN
  63. namespace debugging_internal
  64. {
  65. struct SymbolDecoratorArgs
  66. {
  67. // The program counter we are getting symbolic name for.
  68. const void* pc;
  69. // 0 for main executable, load address for shared libraries.
  70. ptrdiff_t relocation;
  71. // Read-only file descriptor for ELF image covering "pc",
  72. // or -1 if no such ELF image exists in /proc/self/maps.
  73. int fd;
  74. // Output buffer, size.
  75. // Note: the buffer may not be empty -- default symbolizer may have already
  76. // produced some output, and earlier decorators may have adorned it in
  77. // some way. You are free to replace or augment the contents (within the
  78. // symbol_buf_size limit).
  79. char* const symbol_buf;
  80. size_t symbol_buf_size;
  81. // Temporary scratch space, size.
  82. // Use that space in preference to allocating your own stack buffer to
  83. // conserve stack.
  84. char* const tmp_buf;
  85. size_t tmp_buf_size;
  86. // User-provided argument
  87. void* arg;
  88. };
  89. using SymbolDecorator = void (*)(const SymbolDecoratorArgs*);
  90. // Installs a function-pointer as a decorator. Returns a value less than zero
  91. // if the system cannot install the decorator. Otherwise, returns a unique
  92. // identifier corresponding to the decorator. This identifier can be used to
  93. // uninstall the decorator - See RemoveSymbolDecorator() below.
  94. int InstallSymbolDecorator(SymbolDecorator decorator, void* arg);
  95. // Removes a previously installed function-pointer decorator. Parameter "ticket"
  96. // is the return-value from calling InstallSymbolDecorator().
  97. bool RemoveSymbolDecorator(int ticket);
  98. // Remove all installed decorators. Returns true if successful, false if
  99. // symbolization is currently in progress.
  100. bool RemoveAllSymbolDecorators(void);
  101. // Registers an address range to a file mapping.
  102. //
  103. // Preconditions:
  104. // start <= end
  105. // filename != nullptr
  106. //
  107. // Returns true if the file was successfully registered.
  108. bool RegisterFileMappingHint(const void* start, const void* end, uint64_t offset, const char* filename);
  109. // Looks up the file mapping registered by RegisterFileMappingHint for an
  110. // address range. If there is one, the file name is stored in *filename and
  111. // *start and *end are modified to reflect the registered mapping. Returns
  112. // whether any hint was found.
  113. bool GetFileMappingHint(const void** start, const void** end, uint64_t* offset, const char** filename);
  114. } // namespace debugging_internal
  115. ABSL_NAMESPACE_END
  116. } // namespace absl
  117. #endif // __cplusplus
  118. #include <stdbool.h>
  119. #ifdef __cplusplus
  120. extern "C"
  121. #endif // __cplusplus
  122. bool
  123. AbslInternalGetFileMappingHint(const void** start, const void** end, uint64_t* offset, const char** filename);
  124. #endif // ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_