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