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