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