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