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