00001 // Copyright 2018 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 00015 // This file contains internal parts of the Abseil symbolizer. 00016 // Do not depend on the anything in this file, it may change at anytime. 00017 00018 #ifndef ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_ 00019 #define ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_ 00020 00021 #include <cstddef> 00022 #include <cstdint> 00023 00024 #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 00025 #error ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE cannot be directly set 00026 #elif defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) && \ 00027 !defined(__asmjs__) && !defined(__wasm__) 00028 #define ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 1 00029 00030 #include <elf.h> 00031 #include <link.h> // For ElfW() macro. 00032 #include <functional> 00033 #include <string> 00034 00035 namespace absl { 00036 namespace debugging_internal { 00037 00038 // Iterates over all sections, invoking callback on each with the section name 00039 // and the section header. 00040 // 00041 // Returns true on success; otherwise returns false in case of errors. 00042 // 00043 // This is not async-signal-safe. 00044 bool ForEachSection(int fd, 00045 const std::function<bool(const std::string& name, 00046 const ElfW(Shdr) &)>& callback); 00047 00048 // Gets the section header for the given name, if it exists. Returns true on 00049 // success. Otherwise, returns false. 00050 bool GetSectionHeaderByName(int fd, const char *name, size_t name_len, 00051 ElfW(Shdr) *out); 00052 00053 } // namespace debugging_internal 00054 } // namespace absl 00055 00056 #endif // ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 00057 00058 namespace absl { 00059 namespace debugging_internal { 00060 00061 struct SymbolDecoratorArgs { 00062 // The program counter we are getting symbolic name for. 00063 const void *pc; 00064 // 0 for main executable, load address for shared libraries. 00065 ptrdiff_t relocation; 00066 // Read-only file descriptor for ELF image covering "pc", 00067 // or -1 if no such ELF image exists in /proc/self/maps. 00068 int fd; 00069 // Output buffer, size. 00070 // Note: the buffer may not be empty -- default symbolizer may have already 00071 // produced some output, and earlier decorators may have adorned it in 00072 // some way. You are free to replace or augment the contents (within the 00073 // symbol_buf_size limit). 00074 char *const symbol_buf; 00075 size_t symbol_buf_size; 00076 // Temporary scratch space, size. 00077 // Use that space in preference to allocating your own stack buffer to 00078 // conserve stack. 00079 char *const tmp_buf; 00080 size_t tmp_buf_size; 00081 // User-provided argument 00082 void* arg; 00083 }; 00084 using SymbolDecorator = void (*)(const SymbolDecoratorArgs *); 00085 00086 // Installs a function-pointer as a decorator. Returns a value less than zero 00087 // if the system cannot install the decorator. Otherwise, returns a unique 00088 // identifier corresponding to the decorator. This identifier can be used to 00089 // uninstall the decorator - See RemoveSymbolDecorator() below. 00090 int InstallSymbolDecorator(SymbolDecorator decorator, void* arg); 00091 00092 // Removes a previously installed function-pointer decorator. Parameter "ticket" 00093 // is the return-value from calling InstallSymbolDecorator(). 00094 bool RemoveSymbolDecorator(int ticket); 00095 00096 // Remove all installed decorators. Returns true if successful, false if 00097 // symbolization is currently in progress. 00098 bool RemoveAllSymbolDecorators(void); 00099 00100 // Registers an address range to a file mapping. 00101 // 00102 // Preconditions: 00103 // start <= end 00104 // filename != nullptr 00105 // 00106 // Returns true if the file was successfully registered. 00107 bool RegisterFileMappingHint( 00108 const void* start, const void* end, uint64_t offset, const char* filename); 00109 00110 // Looks up the file mapping registered by RegisterFileMappingHint for an 00111 // address range. If there is one, the file name is stored in *filename and 00112 // *start and *end are modified to reflect the registered mapping. Returns 00113 // whether any hint was found. 00114 bool GetFileMappingHint(const void** start, 00115 const void** end, 00116 uint64_t * offset, 00117 const char** filename); 00118 00119 } // namespace debugging_internal 00120 } // namespace absl 00121 00122 #endif // ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_