symbolize.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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__) && \
  26. !defined(__asmjs__) && !defined(__wasm__)
  27. #define ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 1
  28. #include <elf.h>
  29. #include <link.h> // For ElfW() macro.
  30. #include <functional>
  31. #include <string>
  32. namespace absl {
  33. ABSL_NAMESPACE_BEGIN
  34. namespace debugging_internal {
  35. // Iterates over all sections, invoking callback on each with the section name
  36. // and the section header.
  37. //
  38. // Returns true on success; otherwise returns false in case of errors.
  39. //
  40. // This is not async-signal-safe.
  41. bool ForEachSection(int fd,
  42. const std::function<bool(absl::string_view name,
  43. const ElfW(Shdr) &)>& callback);
  44. // Gets the section header for the given name, if it exists. Returns true on
  45. // success. Otherwise, returns false.
  46. bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
  47. ElfW(Shdr) *out);
  48. } // namespace debugging_internal
  49. ABSL_NAMESPACE_END
  50. } // namespace absl
  51. #endif // ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
  52. #ifdef ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE
  53. #error ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE cannot be directly set
  54. #elif defined(__APPLE__)
  55. #define ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE 1
  56. #endif
  57. namespace absl {
  58. ABSL_NAMESPACE_BEGIN
  59. namespace debugging_internal {
  60. struct SymbolDecoratorArgs {
  61. // The program counter we are getting symbolic name for.
  62. const void *pc;
  63. // 0 for main executable, load address for shared libraries.
  64. ptrdiff_t relocation;
  65. // Read-only file descriptor for ELF image covering "pc",
  66. // or -1 if no such ELF image exists in /proc/self/maps.
  67. int fd;
  68. // Output buffer, size.
  69. // Note: the buffer may not be empty -- default symbolizer may have already
  70. // produced some output, and earlier decorators may have adorned it in
  71. // some way. You are free to replace or augment the contents (within the
  72. // symbol_buf_size limit).
  73. char *const symbol_buf;
  74. size_t symbol_buf_size;
  75. // Temporary scratch space, size.
  76. // Use that space in preference to allocating your own stack buffer to
  77. // conserve stack.
  78. char *const tmp_buf;
  79. size_t tmp_buf_size;
  80. // User-provided argument
  81. void* arg;
  82. };
  83. using SymbolDecorator = void (*)(const SymbolDecoratorArgs *);
  84. // Installs a function-pointer as a decorator. Returns a value less than zero
  85. // if the system cannot install the decorator. Otherwise, returns a unique
  86. // identifier corresponding to the decorator. This identifier can be used to
  87. // uninstall the decorator - See RemoveSymbolDecorator() below.
  88. int InstallSymbolDecorator(SymbolDecorator decorator, void* arg);
  89. // Removes a previously installed function-pointer decorator. Parameter "ticket"
  90. // is the return-value from calling InstallSymbolDecorator().
  91. bool RemoveSymbolDecorator(int ticket);
  92. // Remove all installed decorators. Returns true if successful, false if
  93. // symbolization is currently in progress.
  94. bool RemoveAllSymbolDecorators(void);
  95. // Registers an address range to a file mapping.
  96. //
  97. // Preconditions:
  98. // start <= end
  99. // filename != nullptr
  100. //
  101. // Returns true if the file was successfully registered.
  102. bool RegisterFileMappingHint(
  103. const void* start, const void* end, uint64_t offset, const char* filename);
  104. // Looks up the file mapping registered by RegisterFileMappingHint for an
  105. // address range. If there is one, the file name is stored in *filename and
  106. // *start and *end are modified to reflect the registered mapping. Returns
  107. // whether any hint was found.
  108. bool GetFileMappingHint(const void** start,
  109. const void** end,
  110. uint64_t * offset,
  111. const char** filename);
  112. } // namespace debugging_internal
  113. ABSL_NAMESPACE_END
  114. } // namespace absl
  115. #endif // __cplusplus
  116. #include <stdbool.h>
  117. #ifdef __cplusplus
  118. extern "C"
  119. #endif // __cplusplus
  120. bool
  121. AbslInternalGetFileMappingHint(const void** start, const void** end,
  122. uint64_t* offset, const char** filename);
  123. #endif // ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_