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