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