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