symbolize.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Y_ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_
  17. #define Y_ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_
  18. #ifdef __cplusplus
  19. #include <cstddef>
  20. #include <cstdint>
  21. #include "y_absl/base/config.h"
  22. #include "y_absl/strings/string_view.h"
  23. #ifdef Y_ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
  24. #error Y_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 Y_ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 1
  28. #include <elf.h>
  29. #include <link.h> // For ElfW() macro.
  30. #include <functional>
  31. #include <util/generic/string.h>
  32. namespace y_absl {
  33. Y_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(y_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. Y_ABSL_NAMESPACE_END
  50. } // namespace y_absl
  51. #endif // Y_ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
  52. #ifdef Y_ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE
  53. #error Y_ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE cannot be directly set
  54. #elif defined(__APPLE__)
  55. #define Y_ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE 1
  56. #endif
  57. #ifdef Y_ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE
  58. #error Y_ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE cannot be directly set
  59. #elif defined(__EMSCRIPTEN__)
  60. #define Y_ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE 1
  61. #endif
  62. namespace y_absl {
  63. Y_ABSL_NAMESPACE_BEGIN
  64. namespace debugging_internal {
  65. struct SymbolDecoratorArgs {
  66. // The program counter we are getting symbolic name for.
  67. const void *pc;
  68. // 0 for main executable, load address for shared libraries.
  69. ptrdiff_t relocation;
  70. // Read-only file descriptor for ELF image covering "pc",
  71. // or -1 if no such ELF image exists in /proc/self/maps.
  72. int fd;
  73. // Output buffer, size.
  74. // Note: the buffer may not be empty -- default symbolizer may have already
  75. // produced some output, and earlier decorators may have adorned it in
  76. // some way. You are free to replace or augment the contents (within the
  77. // symbol_buf_size limit).
  78. char *const symbol_buf;
  79. size_t symbol_buf_size;
  80. // Temporary scratch space, size.
  81. // Use that space in preference to allocating your own stack buffer to
  82. // conserve stack.
  83. char *const tmp_buf;
  84. size_t tmp_buf_size;
  85. // User-provided argument
  86. void* arg;
  87. };
  88. using SymbolDecorator = void (*)(const SymbolDecoratorArgs *);
  89. // Installs a function-pointer as a decorator. Returns a value less than zero
  90. // if the system cannot install the decorator. Otherwise, returns a unique
  91. // identifier corresponding to the decorator. This identifier can be used to
  92. // uninstall the decorator - See RemoveSymbolDecorator() below.
  93. int InstallSymbolDecorator(SymbolDecorator decorator, void* arg);
  94. // Removes a previously installed function-pointer decorator. Parameter "ticket"
  95. // is the return-value from calling InstallSymbolDecorator().
  96. bool RemoveSymbolDecorator(int ticket);
  97. // Remove all installed decorators. Returns true if successful, false if
  98. // symbolization is currently in progress.
  99. bool RemoveAllSymbolDecorators();
  100. // Registers an address range to a file mapping.
  101. //
  102. // Preconditions:
  103. // start <= end
  104. // filename != nullptr
  105. //
  106. // Returns true if the file was successfully registered.
  107. bool RegisterFileMappingHint(const void* start, const void* end,
  108. 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,
  114. const char** filename);
  115. } // namespace debugging_internal
  116. Y_ABSL_NAMESPACE_END
  117. } // namespace y_absl
  118. #endif // __cplusplus
  119. #include <stdbool.h>
  120. #ifdef __cplusplus
  121. extern "C"
  122. #endif // __cplusplus
  123. bool
  124. YAbslInternalGetFileMappingHint(const void** start, const void** end,
  125. uint64_t* offset, const char** filename);
  126. #endif // Y_ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_