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