Home | History | Annotate | Download | only in includes
      1 /*
      2  * Copyright (c) 2016 GitHub, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 #ifndef LIBBCC_SYMS_H
     17 #define LIBBCC_SYMS_H
     18 
     19 #ifdef __cplusplus
     20 extern "C" {
     21 #endif
     22 
     23 #include <stdint.h>
     24 
     25 struct bcc_symbol {
     26   const char *name;
     27   const char *demangle_name;
     28   const char *module;
     29   uint64_t offset;
     30 };
     31 
     32 typedef int (*SYM_CB)(const char *symname, uint64_t addr);
     33 
     34 #ifndef STT_GNU_IFUNC
     35 #define STT_GNU_IFUNC 10
     36 #endif
     37 static const uint32_t BCC_SYM_ALL_TYPES = 65535;
     38 struct bcc_symbol_option {
     39   int use_debug_file;
     40   int check_debug_file_crc;
     41   // Bitmask flags indicating what types of ELF symbols to use
     42   uint32_t use_symbol_type;
     43 };
     44 
     45 void *bcc_symcache_new(int pid, struct bcc_symbol_option *option);
     46 void bcc_free_symcache(void *symcache, int pid);
     47 
     48 // The demangle_name pointer in bcc_symbol struct is returned from the
     49 // __cxa_demangle function call, which is supposed to be freed by caller. Call
     50 // this function after done using returned result of bcc_symcache_resolve.
     51 void bcc_symbol_free_demangle_name(struct bcc_symbol *sym);
     52 int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
     53 int bcc_symcache_resolve_no_demangle(void *symcache, uint64_t addr,
     54                                      struct bcc_symbol *sym);
     55 
     56 int bcc_symcache_resolve_name(void *resolver, const char *module,
     57                               const char *name, uint64_t *addr);
     58 void bcc_symcache_refresh(void *resolver);
     59 
     60 int bcc_resolve_global_addr(int pid, const char *module, const uint64_t address,
     61                             uint64_t *global);
     62 
     63 // Call cb on every function symbol in the specified module. Uses simpler
     64 // SYM_CB callback mainly for easier to use in Python API.
     65 // Will prefer use debug file and check debug file CRC when reading the module.
     66 int bcc_foreach_function_symbol(const char *module, SYM_CB cb);
     67 
     68 // Find the offset of a symbol in a module binary. If addr is not zero, will
     69 // calculate the offset using the provided addr and the module's load address.
     70 //
     71 // If pid is provided, will use it to help lookup the module in the Process and
     72 // enter the Process's mount Namespace.
     73 //
     74 // If option is not NULL, will respect the specified options for lookup.
     75 // Otherwise default option will apply, which is to use debug file, verify
     76 // checksum, and try all types of symbols.
     77 //
     78 // Return 0 on success and -1 on failure. Output will be write to sym. After
     79 // use, sym->module need to be freed if it's not empty.
     80 int bcc_resolve_symname(const char *module, const char *symname,
     81                         const uint64_t addr, int pid,
     82                         struct bcc_symbol_option* option,
     83                         struct bcc_symbol *sym);
     84 
     85 #ifdef __cplusplus
     86 }
     87 #endif
     88 #endif
     89