xref: /aosp_15_r20/external/bcc/src/cc/bcc_syms.h (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1*387f9dfdSAndroid Build Coastguard Worker /*
2*387f9dfdSAndroid Build Coastguard Worker  * Copyright (c) 2016 GitHub, Inc.
3*387f9dfdSAndroid Build Coastguard Worker  *
4*387f9dfdSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*387f9dfdSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*387f9dfdSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*387f9dfdSAndroid Build Coastguard Worker  *
8*387f9dfdSAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
9*387f9dfdSAndroid Build Coastguard Worker  *
10*387f9dfdSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*387f9dfdSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*387f9dfdSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*387f9dfdSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*387f9dfdSAndroid Build Coastguard Worker  * limitations under the License.
15*387f9dfdSAndroid Build Coastguard Worker  */
16*387f9dfdSAndroid Build Coastguard Worker #ifndef LIBBCC_SYMS_H
17*387f9dfdSAndroid Build Coastguard Worker #define LIBBCC_SYMS_H
18*387f9dfdSAndroid Build Coastguard Worker 
19*387f9dfdSAndroid Build Coastguard Worker #ifdef __cplusplus
20*387f9dfdSAndroid Build Coastguard Worker extern "C" {
21*387f9dfdSAndroid Build Coastguard Worker #endif
22*387f9dfdSAndroid Build Coastguard Worker 
23*387f9dfdSAndroid Build Coastguard Worker #include <stdint.h>
24*387f9dfdSAndroid Build Coastguard Worker #include "linux/bpf.h"
25*387f9dfdSAndroid Build Coastguard Worker #include "bcc_proc.h"
26*387f9dfdSAndroid Build Coastguard Worker 
27*387f9dfdSAndroid Build Coastguard Worker struct bcc_symbol {
28*387f9dfdSAndroid Build Coastguard Worker   const char *name;
29*387f9dfdSAndroid Build Coastguard Worker   const char *demangle_name;
30*387f9dfdSAndroid Build Coastguard Worker   const char *module;
31*387f9dfdSAndroid Build Coastguard Worker   uint64_t offset;
32*387f9dfdSAndroid Build Coastguard Worker };
33*387f9dfdSAndroid Build Coastguard Worker 
34*387f9dfdSAndroid Build Coastguard Worker typedef int (*SYM_CB)(const char *symname, uint64_t addr);
35*387f9dfdSAndroid Build Coastguard Worker struct mod_info;
36*387f9dfdSAndroid Build Coastguard Worker 
37*387f9dfdSAndroid Build Coastguard Worker #ifndef STT_GNU_IFUNC
38*387f9dfdSAndroid Build Coastguard Worker #define STT_GNU_IFUNC 10
39*387f9dfdSAndroid Build Coastguard Worker #endif
40*387f9dfdSAndroid Build Coastguard Worker 
41*387f9dfdSAndroid Build Coastguard Worker #if defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2
42*387f9dfdSAndroid Build Coastguard Worker // Indicate if the Local Entry Point (LEP) should be used as a symbol's
43*387f9dfdSAndroid Build Coastguard Worker // start address
44*387f9dfdSAndroid Build Coastguard Worker #define STT_PPC64_ELFV2_SYM_LEP 31
45*387f9dfdSAndroid Build Coastguard Worker #endif
46*387f9dfdSAndroid Build Coastguard Worker 
47*387f9dfdSAndroid Build Coastguard Worker static const uint32_t BCC_SYM_ALL_TYPES = 65535;
48*387f9dfdSAndroid Build Coastguard Worker struct bcc_symbol_option {
49*387f9dfdSAndroid Build Coastguard Worker   int use_debug_file;
50*387f9dfdSAndroid Build Coastguard Worker   int check_debug_file_crc;
51*387f9dfdSAndroid Build Coastguard Worker   // Symbolize on-demand or symbolize everything ahead of time
52*387f9dfdSAndroid Build Coastguard Worker   int lazy_symbolize;
53*387f9dfdSAndroid Build Coastguard Worker   // Bitmask flags indicating what types of ELF symbols to use
54*387f9dfdSAndroid Build Coastguard Worker   uint32_t use_symbol_type;
55*387f9dfdSAndroid Build Coastguard Worker };
56*387f9dfdSAndroid Build Coastguard Worker 
57*387f9dfdSAndroid Build Coastguard Worker void *bcc_symcache_new(int pid, struct bcc_symbol_option *option);
58*387f9dfdSAndroid Build Coastguard Worker void bcc_free_symcache(void *symcache, int pid);
59*387f9dfdSAndroid Build Coastguard Worker 
60*387f9dfdSAndroid Build Coastguard Worker // The demangle_name pointer in bcc_symbol struct is returned from the
61*387f9dfdSAndroid Build Coastguard Worker // __cxa_demangle function call, which is supposed to be freed by caller. Call
62*387f9dfdSAndroid Build Coastguard Worker // this function after done using returned result of bcc_symcache_resolve.
63*387f9dfdSAndroid Build Coastguard Worker void bcc_symbol_free_demangle_name(struct bcc_symbol *sym);
64*387f9dfdSAndroid Build Coastguard Worker int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
65*387f9dfdSAndroid Build Coastguard Worker int bcc_symcache_resolve_no_demangle(void *symcache, uint64_t addr,
66*387f9dfdSAndroid Build Coastguard Worker                                      struct bcc_symbol *sym);
67*387f9dfdSAndroid Build Coastguard Worker 
68*387f9dfdSAndroid Build Coastguard Worker int bcc_symcache_resolve_name(void *resolver, const char *module,
69*387f9dfdSAndroid Build Coastguard Worker                               const char *name, uint64_t *addr);
70*387f9dfdSAndroid Build Coastguard Worker void bcc_symcache_refresh(void *resolver);
71*387f9dfdSAndroid Build Coastguard Worker 
72*387f9dfdSAndroid Build Coastguard Worker int _bcc_syms_find_module(struct mod_info *info, int enter_ns, void *p);
73*387f9dfdSAndroid Build Coastguard Worker int bcc_resolve_global_addr(int pid, const char *module, const uint64_t address,
74*387f9dfdSAndroid Build Coastguard Worker                             uint8_t inode_match_only, uint64_t *global);
75*387f9dfdSAndroid Build Coastguard Worker 
76*387f9dfdSAndroid Build Coastguard Worker /*bcc APIs for build_id stackmap support*/
77*387f9dfdSAndroid Build Coastguard Worker void *bcc_buildsymcache_new(void);
78*387f9dfdSAndroid Build Coastguard Worker void bcc_free_buildsymcache(void *symcache);
79*387f9dfdSAndroid Build Coastguard Worker int  bcc_buildsymcache_add_module(void *resolver, const char *module_name);
80*387f9dfdSAndroid Build Coastguard Worker int bcc_buildsymcache_resolve(void *resolver,
81*387f9dfdSAndroid Build Coastguard Worker                               struct bpf_stack_build_id *trace,
82*387f9dfdSAndroid Build Coastguard Worker                               struct bcc_symbol *sym);
83*387f9dfdSAndroid Build Coastguard Worker // Call cb on every function symbol in the specified module. Uses simpler
84*387f9dfdSAndroid Build Coastguard Worker // SYM_CB callback mainly for easier to use in Python API.
85*387f9dfdSAndroid Build Coastguard Worker // Will prefer use debug file and check debug file CRC when reading the module.
86*387f9dfdSAndroid Build Coastguard Worker int bcc_foreach_function_symbol(const char *module, SYM_CB cb);
87*387f9dfdSAndroid Build Coastguard Worker 
88*387f9dfdSAndroid Build Coastguard Worker // Find the offset of a symbol in a module binary. If addr is not zero, will
89*387f9dfdSAndroid Build Coastguard Worker // calculate the offset using the provided addr and the module's load address.
90*387f9dfdSAndroid Build Coastguard Worker //
91*387f9dfdSAndroid Build Coastguard Worker // If pid is provided, will use it to help lookup the module in the Process and
92*387f9dfdSAndroid Build Coastguard Worker // enter the Process's mount Namespace.
93*387f9dfdSAndroid Build Coastguard Worker //
94*387f9dfdSAndroid Build Coastguard Worker // If option is not NULL, will respect the specified options for lookup.
95*387f9dfdSAndroid Build Coastguard Worker // Otherwise default option will apply, which is to use debug file, verify
96*387f9dfdSAndroid Build Coastguard Worker // checksum, and try all types of symbols.
97*387f9dfdSAndroid Build Coastguard Worker //
98*387f9dfdSAndroid Build Coastguard Worker // Return 0 on success and -1 on failure. Output will be write to sym. After
99*387f9dfdSAndroid Build Coastguard Worker // use, sym->module need to be freed if it's not empty.
100*387f9dfdSAndroid Build Coastguard Worker int bcc_resolve_symname(const char *module, const char *symname,
101*387f9dfdSAndroid Build Coastguard Worker                         const uint64_t addr, int pid,
102*387f9dfdSAndroid Build Coastguard Worker                         struct bcc_symbol_option* option,
103*387f9dfdSAndroid Build Coastguard Worker                         struct bcc_symbol *sym);
104*387f9dfdSAndroid Build Coastguard Worker 
105*387f9dfdSAndroid Build Coastguard Worker /* Calculate the global address for 'offset' in a shared object loaded into
106*387f9dfdSAndroid Build Coastguard Worker  * a process
107*387f9dfdSAndroid Build Coastguard Worker  *
108*387f9dfdSAndroid Build Coastguard Worker  * Need to know (start_addr, file_offset) pairs for the /proc/PID/maps module
109*387f9dfdSAndroid Build Coastguard Worker  * entry containing the offset and the elf section containing the module's
110*387f9dfdSAndroid Build Coastguard Worker  * .text
111*387f9dfdSAndroid Build Coastguard Worker  */
112*387f9dfdSAndroid Build Coastguard Worker uint64_t __so_calc_global_addr(uint64_t mod_start_addr,
113*387f9dfdSAndroid Build Coastguard Worker                                uint64_t mod_file_offset,
114*387f9dfdSAndroid Build Coastguard Worker                                uint64_t elf_sec_start_addr,
115*387f9dfdSAndroid Build Coastguard Worker                                uint64_t elf_sec_file_offset, uint64_t offset);
116*387f9dfdSAndroid Build Coastguard Worker 
117*387f9dfdSAndroid Build Coastguard Worker /* Given a global address which falls within a shared object's mapping in a
118*387f9dfdSAndroid Build Coastguard Worker  * process, calculate the corresponding 'offset' in the .so
119*387f9dfdSAndroid Build Coastguard Worker  *
120*387f9dfdSAndroid Build Coastguard Worker  * Need to know (start_addr, file_offset) pairs for the /proc/PID/maps module
121*387f9dfdSAndroid Build Coastguard Worker  * entry containing the offset and the elf section containing the module's
122*387f9dfdSAndroid Build Coastguard Worker  * .text
123*387f9dfdSAndroid Build Coastguard Worker  */
124*387f9dfdSAndroid Build Coastguard Worker uint64_t __so_calc_mod_offset(uint64_t mod_start_addr, uint64_t mod_file_offset,
125*387f9dfdSAndroid Build Coastguard Worker                               uint64_t elf_sec_start_addr,
126*387f9dfdSAndroid Build Coastguard Worker                               uint64_t elf_sec_file_offset,
127*387f9dfdSAndroid Build Coastguard Worker                               uint64_t global_addr);
128*387f9dfdSAndroid Build Coastguard Worker 
129*387f9dfdSAndroid Build Coastguard Worker #ifdef __cplusplus
130*387f9dfdSAndroid Build Coastguard Worker }
131*387f9dfdSAndroid Build Coastguard Worker #endif
132*387f9dfdSAndroid Build Coastguard Worker #endif
133