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