Commit | Line | Data |
---|---|---|
b5a8598f AB |
1 | #ifndef _BABELTRACE_SO_INFO_H |
2 | #define _BABELTRACE_SO_INFO_H | |
3 | ||
4 | /* | |
5 | * Babeltrace - Executable and Shared Object Debug Info Reader | |
6 | * | |
7 | * Copyright 2015 Antoine Busque <abusque@efficios.com> | |
8 | * | |
9 | * Author: Antoine Busque <abusque@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
30 | #include <stdint.h> | |
31 | #include <stdbool.h> | |
32 | #include <gelf.h> | |
33 | #include <elfutils/libdw.h> | |
34 | #include <babeltrace/babeltrace-internal.h> | |
35 | ||
36 | #define DEFAULT_DEBUG_DIR "/usr/lib/debug" | |
37 | #define DEBUG_SUBDIR ".debug/" | |
38 | #define BUILD_ID_SUBDIR ".build-id/" | |
39 | #define BUILD_ID_SUFFIX ".debug" | |
40 | ||
41 | struct so_info { | |
42 | /* Base virtual memory address. */ | |
43 | uint64_t low_addr; | |
44 | /* Upper bound of exec address space. */ | |
45 | uint64_t high_addr; | |
46 | /* Size of exec address space. */ | |
47 | uint64_t memsz; | |
48 | /* Paths to ELF and DWARF files. */ | |
49 | char *elf_path; | |
50 | char *dwarf_path; | |
51 | /* libelf and libdw objects representing the files. */ | |
52 | Elf *elf_file; | |
53 | Dwarf *dwarf_info; | |
54 | /* Optional build ID info. */ | |
55 | uint8_t *build_id; | |
56 | size_t build_id_len; | |
57 | /* Optional debug link info. */ | |
58 | char *dbg_link_filename; | |
59 | uint32_t dbg_link_crc; | |
60 | /* FDs to ELF and DWARF files. */ | |
61 | int elf_fd; | |
62 | int dwarf_fd; | |
63 | /* Denotes whether the executable is position independent code. */ | |
64 | bool is_pic:1; | |
65 | /* Denotes whether the SO only has ELF symbols and no DWARF info. */ | |
66 | bool is_elf_only:1; | |
67 | }; | |
68 | ||
69 | struct source_location { | |
70 | uint64_t line_no; | |
71 | char *filename; | |
72 | }; | |
73 | ||
74 | /** | |
75 | * Initializes the so_info framework. Call this before calling | |
76 | * anything else. | |
77 | * | |
78 | * @returns 0 on success, -1 on failure | |
79 | */ | |
80 | BT_HIDDEN | |
81 | int so_info_init(void); | |
82 | ||
83 | /** | |
84 | * Instantiate a structure representing an ELF executable, possibly | |
85 | * with DWARF info, located at the given path. | |
86 | * | |
87 | * @param path Path to the ELF file | |
88 | * @param low_addr Base address of the executable | |
89 | * @param memsz In-memory size of the executable | |
90 | * @returns Pointer to the new so_info on success, | |
91 | * NULL on failure. | |
92 | */ | |
93 | BT_HIDDEN | |
94 | struct so_info *so_info_create(const char *path, uint64_t low_addr, | |
95 | uint64_t memsz); | |
96 | ||
97 | /** | |
98 | * Destroy the given so_info instance | |
99 | * | |
100 | * @param so so_info instance to destroy | |
101 | */ | |
102 | BT_HIDDEN | |
103 | void so_info_destroy(struct so_info *so); | |
104 | ||
105 | /** | |
106 | * Sets the build ID information for a given so_info instance. | |
107 | * | |
108 | * @param so The so_info instance for which to set | |
109 | * the build ID | |
110 | * @param build_id Array of bytes containing the actual ID | |
111 | * @param build_id_len Length in bytes of the build_id | |
112 | * @returns 0 on success, -1 on failure | |
113 | */ | |
114 | BT_HIDDEN | |
115 | int so_info_set_build_id(struct so_info *so, uint8_t *build_id, | |
116 | size_t build_id_len); | |
117 | ||
118 | /** | |
119 | * Sets the debug link information for a given so_info instance. | |
120 | * | |
121 | * @param so The so_info instance for which to set | |
122 | * the debug link | |
123 | * @param filename Name of the separate debug info file | |
124 | * @param crc Checksum for the debug info file | |
125 | * @returns 0 on success, -1 on failure | |
126 | */ | |
127 | BT_HIDDEN | |
128 | int so_info_set_debug_link(struct so_info *so, char *filename, uint32_t crc); | |
129 | ||
130 | /** | |
131 | * Returns whether or not the given SO info \p so contains the address | |
132 | * \p addr. | |
133 | * | |
134 | * @param so so_info instance | |
135 | * @param addr Address to lookup | |
136 | * @returns 1 if \p so contains \p addr, 0 if it does not, | |
137 | * -1 on failure | |
138 | */ | |
139 | static inline | |
140 | int so_info_has_address(struct so_info *so, uint64_t addr) | |
141 | { | |
142 | if (!so) { | |
143 | return -1; | |
144 | } | |
145 | ||
146 | return addr >= so->low_addr && addr < so->high_addr; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Get the name of the function containing a given address within an | |
151 | * executable. | |
152 | * | |
153 | * If no DWARF info is available, the function falls back to ELF | |
154 | * symbols and the "function name" is in fact the name of the closest | |
155 | * symbol, followed by the offset between the symbol and the address. | |
156 | * | |
157 | * On success, if found, the out parameter `func_name` is set. The ownership | |
158 | * of `func_name` is passed to the caller. On failure, `func_name` remains | |
159 | * unchanged. | |
160 | * | |
161 | * @param so so_info instance for the executable containing | |
162 | * the address | |
163 | * @param addr Virtual memory address for which to find the | |
164 | * function name | |
165 | * @param func_name Out parameter, the function name. | |
166 | * @returns 0 on success, -1 on failure | |
167 | */ | |
168 | BT_HIDDEN | |
169 | int so_info_lookup_function_name(struct so_info *so, uint64_t addr, | |
170 | char **func_name); | |
171 | ||
172 | /** | |
173 | * Get the source location (file name and line number) for a given | |
174 | * address within an executable. | |
175 | * | |
176 | * If no DWARF info is available, the source location cannot be found | |
177 | * and the function will return unsuccesfully. | |
178 | * | |
179 | * On success, if found, the out parameter `src_loc` is set. The ownership | |
180 | * of `src_loc` is passed to the caller. On failure, `src_loc` remains | |
181 | * unchanged. | |
182 | * | |
183 | * @param so so_info instance for the executable containing | |
184 | * the address | |
185 | * @param addr Virtual memory address for which to find the | |
186 | * source location | |
187 | * @param src_loc Out parameter, the source location | |
188 | * @returns 0 on success, -1 on failure | |
189 | */ | |
190 | BT_HIDDEN | |
191 | int so_info_lookup_source_location(struct so_info *so, uint64_t addr, | |
192 | struct source_location **src_loc); | |
193 | ||
194 | /** | |
195 | * Destroy the given source_location instance | |
196 | * | |
197 | * @param src_loc source_location instance to destroy | |
198 | */ | |
199 | BT_HIDDEN | |
200 | void source_location_destroy(struct source_location *src_loc); | |
201 | ||
202 | #endif /* _BABELTRACE_SO_INFO_H */ |