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