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