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