Commit | Line | Data |
---|---|---|
b5a8598f | 1 | /* |
dae52e76 | 2 | * bin-info.c |
b5a8598f AB |
3 | * |
4 | * Babeltrace - Executable and Shared Object Debug Info Reader | |
5 | * | |
6 | * Copyright 2015 Antoine Busque <abusque@efficios.com> | |
7 | * | |
8 | * Author: Antoine Busque <abusque@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <fcntl.h> | |
30 | #include <math.h> | |
31 | #include <libgen.h> | |
32 | #include <stdio.h> | |
33 | #include <inttypes.h> | |
34 | #include <stdlib.h> | |
35 | #include <string.h> | |
36 | #include <unistd.h> | |
37 | #include <dwarf.h> | |
38 | #include <glib.h> | |
39 | #include <babeltrace/dwarf.h> | |
dae52e76 | 40 | #include <babeltrace/bin-info.h> |
b5a8598f AB |
41 | #include <babeltrace/crc32.h> |
42 | #include <babeltrace/babeltrace-internal.h> | |
beef86dc | 43 | #include <babeltrace/utils.h> |
b5a8598f AB |
44 | |
45 | /* | |
46 | * An address printed in hex is at most 20 bytes (16 for 64-bits + | |
47 | * leading 0x + optional leading '+' if addr is an offset + null | |
48 | * character). | |
49 | */ | |
50 | #define ADDR_STR_LEN 20 | |
51 | ||
52 | BT_HIDDEN | |
dae52e76 | 53 | int bin_info_init(void) |
b5a8598f AB |
54 | { |
55 | int ret = 0; | |
56 | ||
57 | if (elf_version(EV_CURRENT) == EV_NONE) { | |
1036949d AB |
58 | printf_debug("ELF library initialization failed: %s\n", |
59 | elf_errmsg(-1)); | |
b5a8598f AB |
60 | ret = -1; |
61 | } | |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
66 | BT_HIDDEN | |
dae52e76 | 67 | struct bin_info *bin_info_create(const char *path, uint64_t low_addr, |
1a4a1345 | 68 | uint64_t memsz, bool is_pic) |
b5a8598f | 69 | { |
dae52e76 | 70 | struct bin_info *bin = NULL; |
b5a8598f AB |
71 | |
72 | if (!path) { | |
73 | goto error; | |
74 | } | |
75 | ||
dae52e76 AB |
76 | bin = g_new0(struct bin_info, 1); |
77 | if (!bin) { | |
b5a8598f AB |
78 | goto error; |
79 | } | |
80 | ||
b7b61ced | 81 | if (opt_debug_info_target_prefix) { |
dae52e76 | 82 | bin->elf_path = g_build_path("/", opt_debug_info_target_prefix, |
b7b61ced AB |
83 | path, NULL); |
84 | } else { | |
dae52e76 | 85 | bin->elf_path = strdup(path); |
b7b61ced AB |
86 | } |
87 | ||
dae52e76 | 88 | if (!bin->elf_path) { |
b5a8598f AB |
89 | goto error; |
90 | } | |
91 | ||
dae52e76 AB |
92 | bin->is_pic = is_pic; |
93 | bin->memsz = memsz; | |
94 | bin->low_addr = low_addr; | |
95 | bin->high_addr = bin->low_addr + bin->memsz; | |
b5a8598f | 96 | |
dae52e76 | 97 | return bin; |
b5a8598f AB |
98 | |
99 | error: | |
dae52e76 | 100 | bin_info_destroy(bin); |
b5a8598f AB |
101 | return NULL; |
102 | } | |
103 | ||
104 | BT_HIDDEN | |
dae52e76 | 105 | void bin_info_destroy(struct bin_info *bin) |
b5a8598f | 106 | { |
dae52e76 | 107 | if (!bin) { |
b5a8598f AB |
108 | return; |
109 | } | |
110 | ||
dae52e76 | 111 | dwarf_end(bin->dwarf_info); |
b5a8598f | 112 | |
dae52e76 AB |
113 | free(bin->elf_path); |
114 | free(bin->dwarf_path); | |
115 | free(bin->build_id); | |
116 | free(bin->dbg_link_filename); | |
b5a8598f | 117 | |
dae52e76 | 118 | elf_end(bin->elf_file); |
b5a8598f | 119 | |
dae52e76 AB |
120 | close(bin->elf_fd); |
121 | close(bin->dwarf_fd); | |
b5a8598f | 122 | |
dae52e76 | 123 | g_free(bin); |
b5a8598f AB |
124 | } |
125 | ||
b8652b58 | 126 | |
b5a8598f | 127 | BT_HIDDEN |
dae52e76 | 128 | int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, |
b5a8598f AB |
129 | size_t build_id_len) |
130 | { | |
dae52e76 | 131 | if (!bin || !build_id) { |
b5a8598f AB |
132 | goto error; |
133 | } | |
134 | ||
dae52e76 AB |
135 | bin->build_id = malloc(build_id_len); |
136 | if (!bin->build_id) { | |
b5a8598f AB |
137 | goto error; |
138 | } | |
139 | ||
dae52e76 AB |
140 | memcpy(bin->build_id, build_id, build_id_len); |
141 | bin->build_id_len = build_id_len; | |
b5a8598f AB |
142 | |
143 | /* | |
144 | * Reset the is_elf_only flag in case it had been set | |
145 | * previously, because we might find separate debug info using | |
146 | * the new build id information. | |
147 | */ | |
dae52e76 | 148 | bin->is_elf_only = false; |
b5a8598f AB |
149 | |
150 | return 0; | |
151 | ||
152 | error: | |
153 | ||
154 | return -1; | |
155 | } | |
156 | ||
157 | BT_HIDDEN | |
dae52e76 | 158 | int bin_info_set_debug_link(struct bin_info *bin, char *filename, uint32_t crc) |
b5a8598f | 159 | { |
dae52e76 | 160 | if (!bin || !filename) { |
b5a8598f AB |
161 | goto error; |
162 | } | |
163 | ||
dae52e76 AB |
164 | bin->dbg_link_filename = strdup(filename); |
165 | if (!bin->dbg_link_filename) { | |
b5a8598f AB |
166 | goto error; |
167 | } | |
168 | ||
dae52e76 | 169 | bin->dbg_link_crc = crc; |
b5a8598f AB |
170 | |
171 | /* | |
172 | * Reset the is_elf_only flag in case it had been set | |
173 | * previously, because we might find separate debug info using | |
174 | * the new build id information. | |
175 | */ | |
dae52e76 | 176 | bin->is_elf_only = false; |
b5a8598f AB |
177 | |
178 | return 0; | |
179 | ||
180 | error: | |
181 | ||
182 | return -1; | |
183 | } | |
184 | ||
185 | /** | |
186 | * Tries to read DWARF info from the location given by path, and | |
dae52e76 | 187 | * attach it to the given bin_info instance if it exists. |
b5a8598f | 188 | * |
dae52e76 | 189 | * @param bin bin_info instance for which to set DWARF info |
b5a8598f AB |
190 | * @param path Presumed location of the DWARF info |
191 | * @returns 0 on success, -1 on failure | |
192 | */ | |
193 | static | |
dae52e76 | 194 | int bin_info_set_dwarf_info_from_path(struct bin_info *bin, char *path) |
b5a8598f AB |
195 | { |
196 | int fd = -1, ret = 0; | |
197 | struct bt_dwarf_cu *cu = NULL; | |
198 | Dwarf *dwarf_info = NULL; | |
199 | ||
dae52e76 | 200 | if (!bin || !path) { |
b5a8598f AB |
201 | goto error; |
202 | } | |
203 | ||
204 | fd = open(path, O_RDONLY); | |
205 | if (fd < 0) { | |
206 | goto error; | |
207 | } | |
208 | ||
209 | dwarf_info = dwarf_begin(fd, DWARF_C_READ); | |
210 | if (!dwarf_info) { | |
211 | goto error; | |
212 | } | |
213 | ||
214 | /* | |
dae52e76 AB |
215 | * Check if the dwarf info has any CU. If not, the |
216 | * executable's object file contains no DWARF info. | |
b5a8598f AB |
217 | */ |
218 | cu = bt_dwarf_cu_create(dwarf_info); | |
219 | if (!cu) { | |
220 | goto error; | |
221 | } | |
222 | ||
223 | ret = bt_dwarf_cu_next(cu); | |
224 | if (ret) { | |
225 | goto error; | |
226 | } | |
227 | ||
dae52e76 AB |
228 | bin->dwarf_fd = fd; |
229 | bin->dwarf_path = strdup(path); | |
230 | if (!bin->dwarf_path) { | |
b5a8598f AB |
231 | goto error; |
232 | } | |
dae52e76 | 233 | bin->dwarf_info = dwarf_info; |
b5a8598f AB |
234 | free(cu); |
235 | ||
236 | return 0; | |
237 | ||
238 | error: | |
239 | close(fd); | |
240 | dwarf_end(dwarf_info); | |
241 | g_free(dwarf_info); | |
242 | free(cu); | |
243 | ||
244 | return -1; | |
245 | } | |
246 | ||
247 | /** | |
dae52e76 | 248 | * Try to set the dwarf_info for a given bin_info instance via the |
b5a8598f AB |
249 | * build ID method. |
250 | * | |
dae52e76 | 251 | * @param bin bin_info instance for which to retrieve the |
b5a8598f AB |
252 | * DWARF info via build ID |
253 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure | |
254 | */ | |
255 | static | |
dae52e76 | 256 | int bin_info_set_dwarf_info_build_id(struct bin_info *bin) |
b5a8598f | 257 | { |
1b3f6ee3 | 258 | int i = 0, ret = 0; |
b5a8598f AB |
259 | char *path = NULL, *build_id_file = NULL; |
260 | const char *dbg_dir = NULL; | |
1b3f6ee3 | 261 | size_t build_id_file_len; |
b5a8598f | 262 | |
dae52e76 | 263 | if (!bin || !bin->build_id) { |
b5a8598f AB |
264 | goto error; |
265 | } | |
266 | ||
3be1e3c9 | 267 | dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; |
b5a8598f | 268 | |
5f07b6ce | 269 | /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */ |
dae52e76 | 270 | build_id_file_len = (2 * bin->build_id_len) + 1 + |
1b3f6ee3 | 271 | strlen(BUILD_ID_SUFFIX) + 1; |
b5a8598f AB |
272 | build_id_file = malloc(build_id_file_len); |
273 | if (!build_id_file) { | |
274 | goto error; | |
275 | } | |
276 | ||
dae52e76 AB |
277 | snprintf(build_id_file, 4, "%02x/", bin->build_id[0]); |
278 | for (i = 1; i < bin->build_id_len; ++i) { | |
b5a8598f AB |
279 | int path_idx = 3 + 2 * (i - 1); |
280 | ||
dae52e76 | 281 | snprintf(&build_id_file[path_idx], 3, "%02x", bin->build_id[i]); |
b5a8598f | 282 | } |
1b3f6ee3 | 283 | strcat(build_id_file, BUILD_ID_SUFFIX); |
b5a8598f | 284 | |
1b3f6ee3 | 285 | path = g_build_path("/", dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL); |
b5a8598f AB |
286 | if (!path) { |
287 | goto error; | |
288 | } | |
289 | ||
dae52e76 | 290 | ret = bin_info_set_dwarf_info_from_path(bin, path); |
b5a8598f AB |
291 | if (ret) { |
292 | goto error; | |
293 | } | |
294 | ||
295 | goto end; | |
296 | ||
297 | error: | |
298 | ret = -1; | |
299 | end: | |
300 | free(build_id_file); | |
301 | free(path); | |
302 | ||
303 | return ret; | |
304 | } | |
305 | ||
306 | /** | |
307 | * Tests whether the file located at path exists and has the expected | |
308 | * checksum. | |
309 | * | |
310 | * This predicate is used when looking up separate debug info via the | |
311 | * GNU debuglink method. The expected crc can be found .gnu_debuglink | |
312 | * section in the original ELF file, along with the filename for the | |
313 | * file containing the debug info. | |
314 | * | |
315 | * @param path Full path at which to look for the debug file | |
316 | * @param crc Expected checksum for the debug file | |
317 | * @returns 1 if the file exists and has the correct checksum, | |
318 | * 0 otherwise | |
319 | */ | |
320 | static | |
321 | int is_valid_debug_file(char *path, uint32_t crc) | |
322 | { | |
323 | int ret = 0, fd = -1; | |
324 | uint32_t _crc = 0; | |
325 | ||
326 | if (!path) { | |
327 | goto end; | |
328 | } | |
329 | ||
330 | fd = open(path, O_RDONLY); | |
331 | if (fd < 0) { | |
332 | goto end; | |
333 | } | |
334 | ||
335 | ret = crc32(fd, &_crc); | |
336 | if (ret) { | |
337 | ret = 0; | |
338 | goto end; | |
339 | } | |
340 | ||
341 | ret = (crc == _crc); | |
342 | ||
343 | end: | |
344 | close(fd); | |
345 | return ret; | |
346 | } | |
347 | ||
348 | /** | |
dae52e76 | 349 | * Try to set the dwarf_info for a given bin_info instance via the |
b5a8598f AB |
350 | * build ID method. |
351 | * | |
dae52e76 | 352 | * @param bin bin_info instance for which to retrieve the |
b5a8598f AB |
353 | * DWARF info via debug link |
354 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure | |
355 | */ | |
356 | static | |
dae52e76 | 357 | int bin_info_set_dwarf_info_debug_link(struct bin_info *bin) |
b5a8598f AB |
358 | { |
359 | int ret = 0; | |
360 | const char *dbg_dir = NULL; | |
dae52e76 | 361 | char *dir_name = NULL, *bin_dir = NULL, *path = NULL; |
b5a8598f AB |
362 | size_t max_path_len = 0; |
363 | ||
dae52e76 | 364 | if (!bin || !bin->dbg_link_filename) { |
b5a8598f AB |
365 | goto error; |
366 | } | |
367 | ||
3be1e3c9 | 368 | dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; |
b5a8598f | 369 | |
dae52e76 | 370 | dir_name = dirname(bin->elf_path); |
b5a8598f AB |
371 | if (!dir_name) { |
372 | goto error; | |
373 | } | |
374 | ||
dae52e76 AB |
375 | /* bin_dir is just dir_name with a trailing slash */ |
376 | bin_dir = malloc(strlen(dir_name) + 2); | |
377 | if (!bin_dir) { | |
b5a8598f AB |
378 | goto error; |
379 | } | |
380 | ||
dae52e76 AB |
381 | strcpy(bin_dir, dir_name); |
382 | strcat(bin_dir, "/"); | |
b5a8598f | 383 | |
dae52e76 AB |
384 | max_path_len = strlen(dbg_dir) + strlen(bin_dir) + |
385 | strlen(DEBUG_SUBDIR) + strlen(bin->dbg_link_filename) | |
b5a8598f AB |
386 | + 1; |
387 | path = malloc(max_path_len); | |
388 | if (!path) { | |
389 | goto error; | |
390 | } | |
391 | ||
dae52e76 AB |
392 | /* First look in the executable's dir */ |
393 | strcpy(path, bin_dir); | |
394 | strcat(path, bin->dbg_link_filename); | |
b5a8598f | 395 | |
dae52e76 | 396 | if (is_valid_debug_file(path, bin->dbg_link_crc)) { |
b5a8598f AB |
397 | goto found; |
398 | } | |
399 | ||
400 | /* If not found, look in .debug subdir */ | |
dae52e76 | 401 | strcpy(path, bin_dir); |
b5a8598f | 402 | strcat(path, DEBUG_SUBDIR); |
dae52e76 | 403 | strcat(path, bin->dbg_link_filename); |
b5a8598f | 404 | |
dae52e76 | 405 | if (is_valid_debug_file(path, bin->dbg_link_crc)) { |
b5a8598f AB |
406 | goto found; |
407 | } | |
408 | ||
409 | /* Lastly, look under the global debug directory */ | |
410 | strcpy(path, dbg_dir); | |
dae52e76 AB |
411 | strcat(path, bin_dir); |
412 | strcat(path, bin->dbg_link_filename); | |
b5a8598f | 413 | |
dae52e76 | 414 | if (is_valid_debug_file(path, bin->dbg_link_crc)) { |
b5a8598f AB |
415 | goto found; |
416 | } | |
417 | ||
418 | error: | |
419 | ret = -1; | |
420 | end: | |
421 | free(path); | |
dae52e76 | 422 | free(bin_dir); |
b5a8598f AB |
423 | |
424 | return ret; | |
425 | ||
426 | found: | |
dae52e76 | 427 | ret = bin_info_set_dwarf_info_from_path(bin, path); |
b5a8598f AB |
428 | if (ret) { |
429 | goto error; | |
430 | } | |
431 | ||
432 | goto end; | |
433 | } | |
434 | ||
435 | /** | |
436 | * Initialize the DWARF info for a given executable. | |
437 | * | |
dae52e76 | 438 | * @param bin bin_info instance |
b5a8598f AB |
439 | * @returns 0 on success, -1 on failure |
440 | */ | |
441 | static | |
dae52e76 | 442 | int bin_info_set_dwarf_info(struct bin_info *bin) |
b5a8598f AB |
443 | { |
444 | int ret = 0; | |
445 | ||
dae52e76 | 446 | if (!bin) { |
b5a8598f AB |
447 | goto error; |
448 | } | |
449 | ||
450 | /* First try to set the DWARF info from the ELF file */ | |
dae52e76 | 451 | ret = bin_info_set_dwarf_info_from_path(bin, bin->elf_path); |
b5a8598f AB |
452 | if (!ret) { |
453 | goto end; | |
454 | } | |
455 | ||
456 | /* | |
457 | * If that fails, try to find separate debug info via build ID | |
458 | * and debug link. | |
459 | */ | |
dae52e76 | 460 | ret = bin_info_set_dwarf_info_build_id(bin); |
b5a8598f AB |
461 | if (!ret) { |
462 | goto end; | |
463 | } | |
464 | ||
dae52e76 | 465 | ret = bin_info_set_dwarf_info_debug_link(bin); |
b5a8598f AB |
466 | if (!ret) { |
467 | goto end; | |
468 | } | |
469 | ||
470 | error: | |
471 | ret = -1; | |
472 | end: | |
473 | return ret; | |
474 | } | |
475 | ||
b8652b58 AB |
476 | /** |
477 | * Initialize the ELF file for a given executable. | |
478 | * | |
dae52e76 | 479 | * @param bin bin_info instance |
b8652b58 AB |
480 | * @returns 0 on success, -1 on failure |
481 | */ | |
482 | static | |
dae52e76 | 483 | int bin_info_set_elf_file(struct bin_info *bin) |
b8652b58 | 484 | { |
bfadaf12 | 485 | int elf_fd = -1; |
e362dc93 | 486 | Elf *elf_file = NULL; |
b8652b58 | 487 | |
dae52e76 | 488 | if (!bin) { |
b8652b58 AB |
489 | goto error; |
490 | } | |
491 | ||
dae52e76 | 492 | elf_fd = open(bin->elf_path, O_RDONLY); |
b8652b58 | 493 | if (elf_fd < 0) { |
dae52e76 | 494 | printf_verbose("Failed to open %s\n", bin->elf_path); |
b8652b58 AB |
495 | goto error; |
496 | } | |
497 | ||
498 | elf_file = elf_begin(elf_fd, ELF_C_READ, NULL); | |
499 | if (!elf_file) { | |
1036949d | 500 | printf_debug("elf_begin failed: %s\n", elf_errmsg(-1)); |
b8652b58 AB |
501 | goto error; |
502 | } | |
503 | ||
504 | if (elf_kind(elf_file) != ELF_K_ELF) { | |
1036949d | 505 | printf_verbose("Error: %s is not an ELF object\n", |
dae52e76 | 506 | bin->elf_path); |
b8652b58 AB |
507 | goto error; |
508 | } | |
509 | ||
dae52e76 AB |
510 | bin->elf_fd = elf_fd; |
511 | bin->elf_file = elf_file; | |
b8652b58 AB |
512 | return 0; |
513 | ||
514 | error: | |
bfadaf12 JG |
515 | if (elf_fd >= 0) { |
516 | close(elf_fd); | |
517 | } | |
b8652b58 AB |
518 | elf_end(elf_file); |
519 | return -1; | |
520 | } | |
521 | ||
b5a8598f AB |
522 | BT_HIDDEN |
523 | void source_location_destroy(struct source_location *src_loc) | |
524 | { | |
525 | if (!src_loc) { | |
526 | return; | |
527 | } | |
528 | ||
529 | free(src_loc->filename); | |
530 | g_free(src_loc); | |
531 | } | |
483791a5 | 532 | |
1c47ec6c AB |
533 | /** |
534 | * Append a string representation of an address offset to an existing | |
535 | * string. | |
536 | * | |
537 | * On success, the out parameter `result` will contain the base string | |
538 | * followed by the offset string of the form "+0x1234". On failure, | |
539 | * `result` remains unchanged. | |
540 | * | |
541 | * @param base_str The string to which to append an offset string | |
542 | * @param low_addr The lower virtual memory address, the base from | |
543 | * which the offset is computed | |
544 | * @param high_addr The higher virtual memory address | |
545 | * @param result Out parameter, the base string followed by the | |
546 | * offset string | |
547 | * @returns 0 on success, -1 on failure | |
548 | */ | |
549 | static | |
dae52e76 | 550 | int bin_info_append_offset_str(const char *base_str, uint64_t low_addr, |
1c47ec6c AB |
551 | uint64_t high_addr, char **result) |
552 | { | |
553 | int ret; | |
554 | uint64_t offset; | |
555 | char *_result = NULL; | |
556 | char offset_str[ADDR_STR_LEN]; | |
557 | ||
558 | if (!base_str || !result) { | |
559 | goto error; | |
560 | } | |
561 | ||
562 | offset = high_addr - low_addr; | |
563 | ||
564 | _result = malloc(strlen(base_str) + ADDR_STR_LEN); | |
565 | if (!_result) { | |
566 | goto error; | |
567 | } | |
568 | ||
569 | ret = snprintf(offset_str, ADDR_STR_LEN, "+%#0" PRIx64, offset); | |
570 | if (ret < 0) { | |
571 | goto error; | |
572 | } | |
573 | strcpy(_result, base_str); | |
574 | strcat(_result, offset_str); | |
575 | *result = _result; | |
576 | ||
577 | return 0; | |
578 | ||
579 | error: | |
580 | free(_result); | |
581 | return -1; | |
582 | } | |
b5a8598f AB |
583 | |
584 | /** | |
585 | * Try to find the symbol closest to an address within a given ELF | |
586 | * section. | |
587 | * | |
588 | * Only function symbols are taken into account. The symbol's address | |
589 | * must precede `addr`. A symbol with a closer address might exist | |
590 | * after `addr` but is irrelevant because it cannot encompass `addr`. | |
591 | * | |
592 | * On success, if found, the out parameters `sym` and `shdr` are | |
593 | * set. On failure or if none are found, they remain unchanged. | |
594 | * | |
595 | * @param scn ELF section in which to look for the address | |
596 | * @param addr Virtual memory address for which to find the | |
597 | * nearest function symbol | |
598 | * @param sym Out parameter, the nearest function symbol | |
599 | * @param shdr Out parameter, the section header for scn | |
600 | * @returns 0 on success, -1 on failure | |
601 | */ | |
602 | static | |
dae52e76 | 603 | int bin_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr, |
b5a8598f AB |
604 | GElf_Sym **sym, GElf_Shdr **shdr) |
605 | { | |
606 | int i; | |
607 | size_t symbol_count; | |
608 | Elf_Data *data = NULL; | |
609 | GElf_Shdr *_shdr = NULL; | |
610 | GElf_Sym *nearest_sym = NULL; | |
611 | ||
612 | if (!scn || !sym || !shdr) { | |
613 | goto error; | |
614 | } | |
615 | ||
616 | _shdr = g_new0(GElf_Shdr, 1); | |
617 | if (!_shdr) { | |
618 | goto error; | |
619 | } | |
620 | ||
621 | _shdr = gelf_getshdr(scn, _shdr); | |
622 | if (!_shdr) { | |
623 | goto error; | |
624 | } | |
625 | ||
626 | if (_shdr->sh_type != SHT_SYMTAB) { | |
627 | /* | |
628 | * We are only interested in symbol table (symtab) | |
629 | * sections, skip this one. | |
630 | */ | |
631 | goto end; | |
632 | } | |
633 | ||
634 | data = elf_getdata(scn, NULL); | |
635 | if (!data) { | |
636 | goto error; | |
637 | } | |
638 | ||
639 | symbol_count = _shdr->sh_size / _shdr->sh_entsize; | |
640 | ||
641 | for (i = 0; i < symbol_count; ++i) { | |
642 | GElf_Sym *cur_sym = NULL; | |
643 | ||
644 | cur_sym = g_new0(GElf_Sym, 1); | |
645 | if (!cur_sym) { | |
646 | goto error; | |
647 | } | |
648 | cur_sym = gelf_getsym(data, i, cur_sym); | |
649 | if (!cur_sym) { | |
650 | goto error; | |
651 | } | |
652 | if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) { | |
653 | /* We're only interested in the functions. */ | |
654 | g_free(cur_sym); | |
655 | continue; | |
656 | } | |
657 | ||
658 | if (cur_sym->st_value <= addr && | |
659 | (!nearest_sym || | |
660 | cur_sym->st_value > nearest_sym->st_value)) { | |
661 | g_free(nearest_sym); | |
662 | nearest_sym = cur_sym; | |
663 | } else { | |
664 | g_free(cur_sym); | |
665 | } | |
666 | } | |
667 | ||
668 | end: | |
669 | if (nearest_sym) { | |
670 | *sym = nearest_sym; | |
671 | *shdr = _shdr; | |
672 | } else { | |
673 | g_free(_shdr); | |
674 | } | |
675 | ||
676 | return 0; | |
677 | ||
678 | error: | |
679 | g_free(nearest_sym); | |
680 | g_free(_shdr); | |
681 | return -1; | |
682 | } | |
683 | ||
684 | /** | |
685 | * Get the name of the function containing a given address within an | |
686 | * executable using ELF symbols. | |
687 | * | |
688 | * The function name is in fact the name of the nearest ELF symbol, | |
689 | * followed by the offset in bytes between the address and the symbol | |
690 | * (in hex), separated by a '+' character. | |
691 | * | |
692 | * If found, the out parameter `func_name` is set on success. On failure, | |
693 | * it remains unchanged. | |
694 | * | |
dae52e76 | 695 | * @param bin bin_info instance for the executable containing |
b5a8598f AB |
696 | * the address |
697 | * @param addr Virtual memory address for which to find the | |
698 | * function name | |
699 | * @param func_name Out parameter, the function name | |
700 | * @returns 0 on success, -1 on failure | |
701 | */ | |
702 | static | |
dae52e76 | 703 | int bin_info_lookup_elf_function_name(struct bin_info *bin, uint64_t addr, |
b5a8598f AB |
704 | char **func_name) |
705 | { | |
706 | /* | |
707 | * TODO (possible optimisation): if an ELF has no symtab | |
708 | * section, it has been stripped. Therefore, it would be wise | |
709 | * to store a flag indicating the stripped status after the | |
710 | * first iteration to prevent subsequent ones. | |
711 | */ | |
712 | int ret = 0; | |
713 | Elf_Scn *scn = NULL; | |
714 | GElf_Sym *sym = NULL; | |
715 | GElf_Shdr *shdr = NULL; | |
716 | char *sym_name = NULL; | |
b5a8598f | 717 | |
b8652b58 | 718 | /* Set ELF file if it hasn't been accessed yet. */ |
dae52e76 AB |
719 | if (!bin->elf_file) { |
720 | ret = bin_info_set_elf_file(bin); | |
b8652b58 AB |
721 | if (ret) { |
722 | /* Failed to set ELF file. */ | |
723 | goto error; | |
724 | } | |
725 | } | |
726 | ||
dae52e76 | 727 | scn = elf_nextscn(bin->elf_file, scn); |
b5a8598f AB |
728 | if (!scn) { |
729 | goto error; | |
730 | } | |
731 | ||
732 | while (scn && !sym) { | |
dae52e76 | 733 | ret = bin_info_get_nearest_symbol_from_section( |
b5a8598f AB |
734 | scn, addr, &sym, &shdr); |
735 | if (ret) { | |
736 | goto error; | |
737 | } | |
738 | ||
dae52e76 | 739 | scn = elf_nextscn(bin->elf_file, scn); |
b5a8598f AB |
740 | } |
741 | ||
742 | if (sym) { | |
dae52e76 | 743 | sym_name = elf_strptr(bin->elf_file, shdr->sh_link, |
b5a8598f AB |
744 | sym->st_name); |
745 | if (!sym_name) { | |
746 | goto error; | |
747 | } | |
748 | ||
dae52e76 | 749 | ret = bin_info_append_offset_str(sym_name, sym->st_value, addr, |
1c47ec6c AB |
750 | func_name); |
751 | if (ret) { | |
b5a8598f AB |
752 | goto error; |
753 | } | |
b5a8598f AB |
754 | } |
755 | ||
756 | g_free(shdr); | |
757 | g_free(sym); | |
758 | return 0; | |
759 | ||
760 | error: | |
761 | g_free(shdr); | |
762 | g_free(sym); | |
b5a8598f AB |
763 | return -1; |
764 | } | |
765 | ||
766 | /** | |
767 | * Get the name of the function containing a given address within a | |
768 | * given compile unit (CU). | |
769 | * | |
770 | * If found, the out parameter `func_name` is set on success. On | |
771 | * failure, it remains unchanged. | |
772 | * | |
773 | * @param cu bt_dwarf_cu instance which may contain the address | |
774 | * @param addr Virtual memory address for which to find the | |
775 | * function name | |
776 | * @param func_name Out parameter, the function name | |
777 | * @returns 0 on success, -1 on failure | |
778 | */ | |
779 | static | |
dae52e76 | 780 | int bin_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr, |
b5a8598f AB |
781 | char **func_name) |
782 | { | |
302314ec AB |
783 | int ret = 0; |
784 | bool found = false; | |
b5a8598f AB |
785 | struct bt_dwarf_die *die = NULL; |
786 | ||
787 | if (!cu || !func_name) { | |
788 | goto error; | |
789 | } | |
790 | ||
791 | die = bt_dwarf_die_create(cu); | |
792 | if (!die) { | |
793 | goto error; | |
794 | } | |
795 | ||
796 | while (bt_dwarf_die_next(die) == 0) { | |
797 | int tag; | |
798 | ||
799 | ret = bt_dwarf_die_get_tag(die, &tag); | |
800 | if (ret) { | |
801 | goto error; | |
802 | } | |
803 | ||
804 | if (tag == DW_TAG_subprogram) { | |
805 | ret = bt_dwarf_die_contains_addr(die, addr, &found); | |
806 | if (ret) { | |
807 | goto error; | |
808 | } | |
809 | ||
810 | if (found) { | |
811 | break; | |
812 | } | |
813 | } | |
814 | } | |
815 | ||
816 | if (found) { | |
1c47ec6c AB |
817 | uint64_t low_addr = 0; |
818 | char *die_name = NULL; | |
819 | ||
d3c33bc6 AB |
820 | ret = bt_dwarf_die_get_name(die, &die_name); |
821 | if (ret) { | |
822 | goto error; | |
823 | } | |
824 | ||
825 | ret = dwarf_lowpc(die->dwarf_die, &low_addr); | |
b5a8598f AB |
826 | if (ret) { |
827 | goto error; | |
828 | } | |
829 | ||
dae52e76 | 830 | ret = bin_info_append_offset_str(die_name, low_addr, addr, |
1c47ec6c AB |
831 | func_name); |
832 | if (ret) { | |
d3c33bc6 AB |
833 | goto error; |
834 | } | |
b5a8598f AB |
835 | } |
836 | ||
837 | bt_dwarf_die_destroy(die); | |
838 | return 0; | |
839 | ||
840 | error: | |
841 | bt_dwarf_die_destroy(die); | |
842 | return -1; | |
843 | } | |
844 | ||
845 | /** | |
846 | * Get the name of the function containing a given address within an | |
847 | * executable using DWARF debug info. | |
848 | * | |
849 | * If found, the out parameter `func_name` is set on success. On | |
850 | * failure, it remains unchanged. | |
851 | * | |
dae52e76 | 852 | * @param bin bin_info instance for the executable containing |
b5a8598f AB |
853 | * the address |
854 | * @param addr Virtual memory address for which to find the | |
855 | * function name | |
856 | * @param func_name Out parameter, the function name | |
857 | * @returns 0 on success, -1 on failure | |
858 | */ | |
859 | static | |
dae52e76 | 860 | int bin_info_lookup_dwarf_function_name(struct bin_info *bin, uint64_t addr, |
b5a8598f AB |
861 | char **func_name) |
862 | { | |
863 | int ret = 0; | |
864 | char *_func_name = NULL; | |
865 | struct bt_dwarf_cu *cu = NULL; | |
866 | ||
dae52e76 | 867 | if (!bin || !func_name) { |
b5a8598f AB |
868 | goto error; |
869 | } | |
870 | ||
dae52e76 | 871 | cu = bt_dwarf_cu_create(bin->dwarf_info); |
b5a8598f AB |
872 | if (!cu) { |
873 | goto error; | |
874 | } | |
875 | ||
876 | while (bt_dwarf_cu_next(cu) == 0) { | |
dae52e76 | 877 | ret = bin_info_lookup_cu_function_name(cu, addr, &_func_name); |
b5a8598f AB |
878 | if (ret) { |
879 | goto error; | |
880 | } | |
881 | ||
882 | if (_func_name) { | |
883 | break; | |
884 | } | |
885 | } | |
886 | ||
887 | if (_func_name) { | |
888 | *func_name = _func_name; | |
fcd94e37 JG |
889 | } else { |
890 | goto error; | |
b5a8598f AB |
891 | } |
892 | ||
893 | bt_dwarf_cu_destroy(cu); | |
894 | return 0; | |
895 | ||
896 | error: | |
897 | bt_dwarf_cu_destroy(cu); | |
898 | return -1; | |
899 | } | |
900 | ||
901 | BT_HIDDEN | |
dae52e76 | 902 | int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr, |
b5a8598f AB |
903 | char **func_name) |
904 | { | |
905 | int ret = 0; | |
906 | char *_func_name = NULL; | |
907 | ||
dae52e76 | 908 | if (!bin || !func_name) { |
b5a8598f AB |
909 | goto error; |
910 | } | |
911 | ||
912 | /* Set DWARF info if it hasn't been accessed yet. */ | |
dae52e76 AB |
913 | if (!bin->dwarf_info && !bin->is_elf_only) { |
914 | ret = bin_info_set_dwarf_info(bin); | |
b5a8598f AB |
915 | if (ret) { |
916 | /* Failed to set DWARF info, fallback to ELF. */ | |
dae52e76 | 917 | bin->is_elf_only = true; |
b5a8598f AB |
918 | } |
919 | } | |
920 | ||
dae52e76 | 921 | if (!bin_info_has_address(bin, addr)) { |
b5a8598f AB |
922 | goto error; |
923 | } | |
924 | ||
925 | /* | |
926 | * Addresses in ELF and DWARF are relative to base address for | |
927 | * PIC, so make the address argument relative too if needed. | |
928 | */ | |
dae52e76 AB |
929 | if (bin->is_pic) { |
930 | addr -= bin->low_addr; | |
dc419b6c AB |
931 | } |
932 | ||
dae52e76 AB |
933 | if (bin->is_elf_only) { |
934 | ret = bin_info_lookup_elf_function_name(bin, addr, &_func_name); | |
b5a8598f | 935 | } else { |
dae52e76 | 936 | ret = bin_info_lookup_dwarf_function_name(bin, addr, &_func_name); |
b5a8598f AB |
937 | } |
938 | ||
dc419b6c | 939 | if (ret || !_func_name) { |
b5a8598f AB |
940 | goto error; |
941 | } | |
942 | ||
dc419b6c AB |
943 | *func_name = _func_name; |
944 | return 0; | |
beef86dc | 945 | |
dc419b6c AB |
946 | error: |
947 | return -1; | |
948 | } | |
949 | ||
950 | BT_HIDDEN | |
dae52e76 | 951 | int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc) |
dc419b6c AB |
952 | { |
953 | int ret = 0; | |
954 | char *_bin_loc = NULL; | |
955 | ||
dae52e76 | 956 | if (!bin || !bin_loc) { |
dc419b6c | 957 | goto error; |
b5a8598f AB |
958 | } |
959 | ||
dae52e76 AB |
960 | if (bin->is_pic) { |
961 | addr -= bin->low_addr; | |
dc419b6c AB |
962 | ret = asprintf(&_bin_loc, "+%#0" PRIx64, addr); |
963 | } else { | |
964 | ret = asprintf(&_bin_loc, "@%#0" PRIx64, addr); | |
965 | } | |
966 | ||
967 | if (ret == -1 || !_bin_loc) { | |
968 | goto error; | |
969 | } | |
970 | ||
971 | *bin_loc = _bin_loc; | |
b5a8598f AB |
972 | return 0; |
973 | ||
974 | error: | |
975 | return -1; | |
976 | } | |
977 | ||
978 | /** | |
979 | * Predicate used to determine whether the children of a given DIE | |
980 | * contain a specific address. | |
981 | * | |
982 | * More specifically, the parameter `die` is expected to be a | |
983 | * subprogram (function) DIE, and this predicate tells whether any | |
984 | * subroutines are inlined within this function and would contain | |
985 | * `addr`. | |
986 | * | |
302314ec AB |
987 | * On success, the out parameter `contains` is set with the boolean |
988 | * value indicating whether the DIE's range covers `addr`. On failure, | |
989 | * it remains unchanged. | |
990 | * | |
b5a8598f AB |
991 | * Do note that this function advances the position of `die`. If the |
992 | * address is found within one of its children, `die` will be pointing | |
993 | * to that child upon returning from the function, allowing to extract | |
994 | * the information deemed necessary. | |
995 | * | |
302314ec AB |
996 | * @param die The parent DIE in whose children the address will be |
997 | * looked for | |
998 | * @param addr The address for which to look for in the DIEs | |
999 | * @param contains Out parameter, true if addr is contained, | |
1000 | * false if not | |
1001 | * @returns Returns 0 on success, -1 on failure | |
b5a8598f AB |
1002 | */ |
1003 | static | |
302314ec | 1004 | int bin_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr, bool *contains) |
b5a8598f | 1005 | { |
302314ec AB |
1006 | int ret = 0; |
1007 | bool _contains = false; | |
b5a8598f AB |
1008 | |
1009 | if (!die) { | |
1010 | goto error; | |
1011 | } | |
1012 | ||
1013 | ret = bt_dwarf_die_child(die); | |
1014 | if (ret) { | |
1015 | goto error; | |
1016 | } | |
1017 | ||
1018 | do { | |
1019 | int tag; | |
1020 | ||
1021 | ret = bt_dwarf_die_get_tag(die, &tag); | |
1022 | if (ret) { | |
1023 | goto error; | |
1024 | } | |
1025 | ||
1026 | if (tag == DW_TAG_inlined_subroutine) { | |
302314ec | 1027 | ret = bt_dwarf_die_contains_addr(die, addr, &_contains); |
b5a8598f AB |
1028 | if (ret) { |
1029 | goto error; | |
1030 | } | |
1031 | ||
1032 | if (contains) { | |
b5a8598f AB |
1033 | goto end; |
1034 | } | |
1035 | } | |
1036 | } while (bt_dwarf_die_next(die) == 0); | |
1037 | ||
1038 | end: | |
302314ec AB |
1039 | *contains = _contains; |
1040 | return 0; | |
b5a8598f AB |
1041 | |
1042 | error: | |
302314ec | 1043 | return -1; |
b5a8598f AB |
1044 | } |
1045 | ||
1046 | /** | |
1047 | * Lookup the source location for a given address within a CU, making | |
1048 | * the assumption that it is contained within an inline routine in a | |
1049 | * function. | |
1050 | * | |
1051 | * @param cu bt_dwarf_cu instance in which to look for the address | |
1052 | * @param addr The address for which to look for | |
1053 | * @param src_loc Out parameter, the source location (filename and | |
1054 | * line number) for the address | |
1055 | * @returns 0 on success, -1 on failure | |
1056 | */ | |
1057 | static | |
dae52e76 | 1058 | int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
b5a8598f AB |
1059 | struct source_location **src_loc) |
1060 | { | |
302314ec AB |
1061 | int ret = 0; |
1062 | bool found = false; | |
b5a8598f AB |
1063 | struct bt_dwarf_die *die = NULL; |
1064 | struct source_location *_src_loc = NULL; | |
1065 | ||
1066 | if (!cu || !src_loc) { | |
1067 | goto error; | |
1068 | } | |
1069 | ||
1070 | die = bt_dwarf_die_create(cu); | |
1071 | if (!die) { | |
1072 | goto error; | |
1073 | } | |
1074 | ||
1075 | while (bt_dwarf_die_next(die) == 0) { | |
1076 | int tag; | |
1077 | ||
1078 | ret = bt_dwarf_die_get_tag(die, &tag); | |
1079 | if (ret) { | |
1080 | goto error; | |
1081 | } | |
1082 | ||
1083 | if (tag == DW_TAG_subprogram) { | |
302314ec | 1084 | bool contains = false; |
b5a8598f AB |
1085 | |
1086 | ret = bt_dwarf_die_contains_addr(die, addr, &contains); | |
1087 | if (ret) { | |
1088 | goto error; | |
1089 | } | |
1090 | ||
1091 | if (contains) { | |
1092 | /* | |
1093 | * Try to find an inlined subroutine | |
1094 | * child of this DIE containing addr. | |
1095 | */ | |
302314ec AB |
1096 | ret = bin_info_child_die_has_address(die, addr, |
1097 | &found); | |
1098 | if(ret) { | |
1099 | goto error; | |
1100 | } | |
1101 | ||
b5a8598f AB |
1102 | goto end; |
1103 | } | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | end: | |
1108 | if (found) { | |
1109 | char *filename = NULL; | |
1110 | uint64_t line_no; | |
1111 | ||
1112 | _src_loc = g_new0(struct source_location, 1); | |
1113 | if (!_src_loc) { | |
1114 | goto error; | |
1115 | } | |
1116 | ||
1117 | ret = bt_dwarf_die_get_call_file(die, &filename); | |
1118 | if (ret) { | |
1119 | goto error; | |
1120 | } | |
1121 | ret = bt_dwarf_die_get_call_line(die, &line_no); | |
1122 | if (ret) { | |
1123 | free(filename); | |
1124 | goto error; | |
1125 | } | |
1126 | ||
1127 | _src_loc->filename = filename; | |
1128 | _src_loc->line_no = line_no; | |
1129 | *src_loc = _src_loc; | |
1130 | } | |
1131 | ||
1132 | bt_dwarf_die_destroy(die); | |
1133 | return 0; | |
1134 | ||
1135 | error: | |
1136 | source_location_destroy(_src_loc); | |
1137 | bt_dwarf_die_destroy(die); | |
1138 | return -1; | |
1139 | } | |
1140 | ||
1141 | /** | |
1142 | * Lookup the source location for a given address within a CU, | |
1143 | * assuming that it is contained within an inlined function. | |
1144 | * | |
1145 | * A source location can be found regardless of inlining status for | |
1146 | * this method, but in the case of an inlined function, the returned | |
1147 | * source location will point not to the callsite but rather to the | |
1148 | * definition site of the inline function. | |
1149 | * | |
1150 | * @param cu bt_dwarf_cu instance in which to look for the address | |
1151 | * @param addr The address for which to look for | |
1152 | * @param src_loc Out parameter, the source location (filename and | |
1153 | * line number) for the address | |
1154 | * @returns 0 on success, -1 on failure | |
1155 | */ | |
1156 | static | |
dae52e76 | 1157 | int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
b5a8598f AB |
1158 | struct source_location **src_loc) |
1159 | { | |
1160 | struct source_location *_src_loc = NULL; | |
1161 | struct bt_dwarf_die *die = NULL; | |
1162 | const char *filename = NULL; | |
1163 | Dwarf_Line *line = NULL; | |
1164 | Dwarf_Addr line_addr; | |
1165 | int ret, line_no; | |
1166 | ||
1167 | if (!cu || !src_loc) { | |
1168 | goto error; | |
1169 | } | |
1170 | ||
1171 | die = bt_dwarf_die_create(cu); | |
1172 | if (!die) { | |
1173 | goto error; | |
1174 | } | |
1175 | ||
1176 | line = dwarf_getsrc_die(die->dwarf_die, addr); | |
1177 | if (!line) { | |
1178 | goto error; | |
1179 | } | |
1180 | ||
1181 | ret = dwarf_lineaddr(line, &line_addr); | |
1182 | if (ret) { | |
1183 | goto error; | |
1184 | } | |
1185 | ||
1186 | filename = dwarf_linesrc(line, NULL, NULL); | |
1187 | if (!filename) { | |
1188 | goto error; | |
1189 | } | |
1190 | ||
1191 | if (addr == line_addr) { | |
1192 | _src_loc = g_new0(struct source_location, 1); | |
1193 | if (!_src_loc) { | |
1194 | goto error; | |
1195 | } | |
1196 | ||
1197 | ret = dwarf_lineno(line, &line_no); | |
1198 | if (ret) { | |
1199 | goto error; | |
1200 | } | |
1201 | ||
1202 | _src_loc->line_no = line_no; | |
1203 | _src_loc->filename = strdup(filename); | |
1204 | } | |
1205 | ||
1206 | bt_dwarf_die_destroy(die); | |
1207 | ||
1208 | if (_src_loc) { | |
1209 | *src_loc = _src_loc; | |
1210 | } | |
1211 | ||
1212 | return 0; | |
1213 | ||
1214 | error: | |
1215 | source_location_destroy(_src_loc); | |
1216 | bt_dwarf_die_destroy(die); | |
1217 | return -1; | |
1218 | } | |
1219 | ||
1220 | /** | |
1221 | * Get the source location (file name and line number) for a given | |
1222 | * address within a compile unit (CU). | |
1223 | * | |
1224 | * On success, the out parameter `src_loc` is set if found. On | |
1225 | * failure, it remains unchanged. | |
1226 | * | |
dae52e76 | 1227 | * @param cu bt_dwarf_cu instance for the compile unit which |
b5a8598f AB |
1228 | * may contain the address |
1229 | * @param addr Virtual memory address for which to find the | |
1230 | * source location | |
1231 | * @param src_loc Out parameter, the source location | |
1232 | * @returns 0 on success, -1 on failure | |
1233 | */ | |
1234 | static | |
dae52e76 | 1235 | int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu *cu, uint64_t addr, |
b5a8598f AB |
1236 | struct source_location **src_loc) |
1237 | { | |
1238 | int ret = 0; | |
1239 | struct source_location *_src_loc = NULL; | |
1240 | ||
1241 | if (!cu || !src_loc) { | |
1242 | goto error; | |
1243 | } | |
1244 | ||
dae52e76 | 1245 | ret = bin_info_lookup_cu_src_loc_inl(cu, addr, &_src_loc); |
b5a8598f AB |
1246 | if (ret) { |
1247 | goto error; | |
1248 | } | |
1249 | ||
1250 | if (_src_loc) { | |
1251 | goto end; | |
1252 | } | |
1253 | ||
dae52e76 | 1254 | ret = bin_info_lookup_cu_src_loc_no_inl(cu, addr, &_src_loc); |
b5a8598f AB |
1255 | if (ret) { |
1256 | goto error; | |
1257 | } | |
1258 | ||
1259 | if (_src_loc) { | |
1260 | goto end; | |
1261 | } | |
1262 | ||
1263 | end: | |
1264 | if (_src_loc) { | |
1265 | *src_loc = _src_loc; | |
1266 | } | |
1267 | ||
1268 | return 0; | |
1269 | ||
1270 | error: | |
1271 | source_location_destroy(_src_loc); | |
1272 | return -1; | |
1273 | } | |
1274 | ||
1275 | BT_HIDDEN | |
dae52e76 | 1276 | int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, |
b5a8598f AB |
1277 | struct source_location **src_loc) |
1278 | { | |
1279 | struct bt_dwarf_cu *cu = NULL; | |
1280 | struct source_location *_src_loc = NULL; | |
1281 | ||
dae52e76 | 1282 | if (!bin || !src_loc) { |
b5a8598f AB |
1283 | goto error; |
1284 | } | |
1285 | ||
1286 | /* Set DWARF info if it hasn't been accessed yet. */ | |
dae52e76 AB |
1287 | if (!bin->dwarf_info && !bin->is_elf_only) { |
1288 | if (bin_info_set_dwarf_info(bin)) { | |
b5a8598f | 1289 | /* Failed to set DWARF info. */ |
dae52e76 | 1290 | bin->is_elf_only = true; |
b5a8598f AB |
1291 | } |
1292 | } | |
1293 | ||
dae52e76 | 1294 | if (bin->is_elf_only) { |
b5a8598f AB |
1295 | /* We cannot lookup source location without DWARF info. */ |
1296 | goto error; | |
1297 | } | |
1298 | ||
dae52e76 | 1299 | if (!bin_info_has_address(bin, addr)) { |
b5a8598f AB |
1300 | goto error; |
1301 | } | |
1302 | ||
1303 | /* | |
1304 | * Addresses in ELF and DWARF are relative to base address for | |
1305 | * PIC, so make the address argument relative too if needed. | |
1306 | */ | |
dae52e76 AB |
1307 | if (bin->is_pic) { |
1308 | addr -= bin->low_addr; | |
b5a8598f AB |
1309 | } |
1310 | ||
dae52e76 | 1311 | cu = bt_dwarf_cu_create(bin->dwarf_info); |
b5a8598f AB |
1312 | if (!cu) { |
1313 | goto error; | |
1314 | } | |
1315 | ||
1316 | while (bt_dwarf_cu_next(cu) == 0) { | |
1317 | int ret; | |
1318 | ||
dae52e76 | 1319 | ret = bin_info_lookup_cu_src_loc(cu, addr, &_src_loc); |
b5a8598f AB |
1320 | if (ret) { |
1321 | goto error; | |
1322 | } | |
1323 | ||
1324 | if (_src_loc) { | |
1325 | break; | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | bt_dwarf_cu_destroy(cu); | |
1330 | if (_src_loc) { | |
1331 | *src_loc = _src_loc; | |
1332 | } | |
1333 | ||
1334 | return 0; | |
1335 | ||
1336 | error: | |
1337 | source_location_destroy(_src_loc); | |
1338 | bt_dwarf_cu_destroy(cu); | |
1339 | return -1; | |
1340 | } |