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