2 * Babeltrace - Debug Information State Tracker
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
6 * Copyright (c) 2015 Antoine Busque <abusque@efficios.com>
7 * Copyright (c) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-UTILS-DEBUG-INFO-FLT"
33 #include "debug-info.h"
38 struct proc_debug_info_sources
{
40 * Hash table: base address (pointer to uint64_t) to bin info; owned by
41 * proc_debug_info_sources.
43 GHashTable
*baddr_to_bin_info
;
46 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
47 * owned by proc_debug_info_sources.
49 GHashTable
*ip_to_debug_info_src
;
53 struct debug_info_component
*comp
;
56 * Hash table of VPIDs (pointer to int64_t) to
57 * (struct ctf_proc_debug_infos*); owned by debug_info.
59 GHashTable
*vpid_to_proc_dbg_info_src
;
60 GQuark q_statedump_bin_info
;
61 GQuark q_statedump_debug_link
;
62 GQuark q_statedump_build_id
;
63 GQuark q_statedump_start
;
70 int debug_info_init(struct debug_info
*info
)
72 info
->q_statedump_bin_info
= g_quark_from_string(
73 "lttng_ust_statedump:bin_info");
74 info
->q_statedump_debug_link
= g_quark_from_string(
75 "lttng_ust_statedump:debug_link)");
76 info
->q_statedump_build_id
= g_quark_from_string(
77 "lttng_ust_statedump:build_id");
78 info
->q_statedump_start
= g_quark_from_string(
79 "lttng_ust_statedump:start");
80 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
81 info
->q_lib_load
= g_quark_from_string("lttng_ust_lib:load");
82 info
->q_lib_unload
= g_quark_from_string("lttng_ust_lib:unload");
84 return bin_info_init();
88 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
90 if (!debug_info_src
) {
94 free(debug_info_src
->func
);
95 free(debug_info_src
->src_path
);
96 free(debug_info_src
->bin_path
);
97 free(debug_info_src
->bin_loc
);
98 g_free(debug_info_src
);
102 struct debug_info_source
*debug_info_source_create_from_bin(struct bin_info
*bin
,
106 struct debug_info_source
*debug_info_src
= NULL
;
107 struct source_location
*src_loc
= NULL
;
109 debug_info_src
= g_new0(struct debug_info_source
, 1);
111 if (!debug_info_src
) {
115 /* Lookup function name */
116 ret
= bin_info_lookup_function_name(bin
, ip
, &debug_info_src
->func
);
121 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
122 if (!bin
->is_elf_only
|| !debug_info_src
->func
) {
123 /* Lookup source location */
124 ret
= bin_info_lookup_source_location(bin
, ip
, &src_loc
);
125 BT_LOGD("Failed to lookup source location: ret=%d", ret
);
129 debug_info_src
->line_no
= src_loc
->line_no
;
131 if (src_loc
->filename
) {
132 debug_info_src
->src_path
= strdup(src_loc
->filename
);
133 if (!debug_info_src
->src_path
) {
137 debug_info_src
->short_src_path
= get_filename_from_path(
138 debug_info_src
->src_path
);
141 source_location_destroy(src_loc
);
145 debug_info_src
->bin_path
= strdup(bin
->elf_path
);
146 if (!debug_info_src
->bin_path
) {
150 debug_info_src
->short_bin_path
= get_filename_from_path(
151 debug_info_src
->bin_path
);
153 ret
= bin_info_get_bin_loc(bin
, ip
, &(debug_info_src
->bin_loc
));
160 return debug_info_src
;
163 debug_info_source_destroy(debug_info_src
);
168 void proc_debug_info_sources_destroy(
169 struct proc_debug_info_sources
*proc_dbg_info_src
)
171 if (!proc_dbg_info_src
) {
175 if (proc_dbg_info_src
->baddr_to_bin_info
) {
176 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_bin_info
);
179 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
180 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
183 g_free(proc_dbg_info_src
);
187 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
189 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
191 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
192 if (!proc_dbg_info_src
) {
196 proc_dbg_info_src
->baddr_to_bin_info
= g_hash_table_new_full(
197 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
198 (GDestroyNotify
) bin_info_destroy
);
199 if (!proc_dbg_info_src
->baddr_to_bin_info
) {
203 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
204 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
205 (GDestroyNotify
) debug_info_source_destroy
);
206 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
211 return proc_dbg_info_src
;
214 proc_debug_info_sources_destroy(proc_dbg_info_src
);
219 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
220 GHashTable
*ht
, int64_t vpid
)
222 gpointer key
= g_new0(int64_t, 1);
223 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
229 *((int64_t *) key
) = vpid
;
231 /* Exists? Return it */
232 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
233 if (proc_dbg_info_src
) {
237 /* Otherwise, create and return it */
238 proc_dbg_info_src
= proc_debug_info_sources_create();
239 if (!proc_dbg_info_src
) {
243 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
244 /* Ownership passed to ht */
248 return proc_dbg_info_src
;
252 struct debug_info_source
*proc_debug_info_sources_get_entry(
253 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
255 struct debug_info_source
*debug_info_src
= NULL
;
256 gpointer key
= g_new0(uint64_t, 1);
258 gpointer baddr
, value
;
264 *((uint64_t *) key
) = ip
;
266 /* Look in IP to debug infos hash table first. */
267 debug_info_src
= g_hash_table_lookup(
268 proc_dbg_info_src
->ip_to_debug_info_src
,
270 if (debug_info_src
) {
274 /* Check in all bin_infos. */
275 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_bin_info
);
277 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
279 struct bin_info
*bin
= value
;
281 if (!bin_info_has_address(value
, ip
)) {
286 * Found; add it to cache.
288 * FIXME: this should be bounded in size (and implement
289 * a caching policy), and entries should be prunned when
290 * libraries are unmapped.
292 debug_info_src
= debug_info_source_create_from_bin(bin
, ip
);
293 if (debug_info_src
) {
295 proc_dbg_info_src
->ip_to_debug_info_src
,
296 key
, debug_info_src
);
297 /* Ownership passed to ht. */
305 return debug_info_src
;
309 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
310 int64_t vpid
, uint64_t ip
)
312 struct debug_info_source
*dbg_info_src
= NULL
;
313 struct proc_debug_info_sources
*proc_dbg_info_src
;
315 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
316 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
317 if (!proc_dbg_info_src
) {
321 dbg_info_src
= proc_debug_info_sources_get_entry(proc_dbg_info_src
, ip
);
328 struct debug_info
*debug_info_create(struct debug_info_component
*comp
)
331 struct debug_info
*debug_info
;
333 debug_info
= g_new0(struct debug_info
, 1);
338 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
339 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
340 (GDestroyNotify
) proc_debug_info_sources_destroy
);
341 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
345 debug_info
->comp
= comp
;
346 ret
= debug_info_init(debug_info
);
359 void debug_info_destroy(struct debug_info
*debug_info
)
365 if (debug_info
->vpid_to_proc_dbg_info_src
) {
366 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
375 void handle_statedump_build_id_event(FILE *err
, struct debug_info
*debug_info
,
376 struct bt_ctf_event
*event
)
378 struct proc_debug_info_sources
*proc_dbg_info_src
;
379 struct bin_info
*bin
= NULL
;
384 ret
= get_stream_event_context_int_field_value(err
,
385 event
, "_vpid", &vpid
);
390 ret
= get_payload_unsigned_int_field_value(err
,
391 event
, "_baddr", &baddr
);
393 BT_LOGE_STR("Failed to get unsigned int value for _vpid field.");
397 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
398 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
399 if (!proc_dbg_info_src
) {
403 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
407 * The build_id event comes after the bin has been
408 * created. If it isn't found, just ignore this event.
413 ret
= get_payload_build_id_field_value(err
, event
, "_build_id",
414 &bin
->build_id
, &bin
->build_id_len
);
416 BT_LOGE_STR("Failed to get _build_id field value.");
421 * Reset the is_elf_only flag in case it had been set
422 * previously, because we might find separate debug info using
423 * the new build id information.
425 bin
->is_elf_only
= false;
428 // bin_info_set_build_id(bin, build_id, build_id_len);
435 void handle_statedump_debug_link_event(FILE *err
, struct debug_info
*debug_info
,
436 struct bt_ctf_event
*event
)
438 struct proc_debug_info_sources
*proc_dbg_info_src
;
439 struct bin_info
*bin
= NULL
;
442 const char *filename
= NULL
;
447 ret
= get_stream_event_context_int_field_value(err
, event
,
453 ret
= get_payload_unsigned_int_field_value(err
,
454 event
, "_baddr", &baddr
);
456 BT_LOGE_STR("Failed to get unsigned int value for _baddr field.");
461 ret
= get_payload_unsigned_int_field_value(err
, event
, "_crc32", &tmp
);
463 BT_LOGE_STR("Failed to get unsigned int value for _crc32 field.");
467 crc32
= (uint32_t) tmp
;
469 ret
= get_payload_string_field_value(err
,
470 event
, "_filename", &filename
);
472 BT_LOGE_STR("Failed to get string value for _filename field.");
477 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
478 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
479 if (!proc_dbg_info_src
) {
483 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
487 * The debug_link event comes after the bin has been
488 * created. If it isn't found, just ignore this event.
493 bin_info_set_debug_link(bin
, filename
, crc32
);
500 void handle_bin_info_event(FILE *err
, struct debug_info
*debug_info
,
501 struct bt_ctf_event
*event
, bool has_pic_field
)
503 struct proc_debug_info_sources
*proc_dbg_info_src
;
504 struct bin_info
*bin
;
505 uint64_t baddr
, memsz
;
512 ret
= get_payload_unsigned_int_field_value(err
,
513 event
, "_baddr", &baddr
);
515 BT_LOGE_STR("Failed to get unsigned int value for _baddr field.");
519 ret
= get_payload_unsigned_int_field_value(err
,
520 event
, "_memsz", &memsz
);
522 BT_LOGE_STR("Failed to get unsigned int value for _memsz field.");
527 * This field is not produced by the dlopen event emitted before
530 ret
= get_payload_string_field_value(err
,
531 event
, "_path", &path
);
539 ret
= get_payload_unsigned_int_field_value(err
,
540 event
, "_is_pic", &tmp
);
542 BT_LOGE_STR("Failed to get unsigned int value for _is_pic field.");
549 * dlopen has no is_pic field, because the shared
550 * object is always PIC.
555 ret
= get_stream_event_context_int_field_value(err
, event
, "_vpid",
566 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
567 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
568 if (!proc_dbg_info_src
) {
572 key
= g_new0(uint64_t, 1);
577 *((uint64_t *) key
) = baddr
;
579 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
585 bin
= bin_info_create(path
, baddr
, memsz
, is_pic
,
586 debug_info
->comp
->arg_debug_dir
,
587 debug_info
->comp
->arg_target_prefix
);
592 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
,
594 /* Ownership passed to ht. */
603 void handle_statedump_bin_info_event(FILE *err
, struct debug_info
*debug_info
,
604 struct bt_ctf_event
*event
)
606 handle_bin_info_event(err
, debug_info
, event
, true);
610 void handle_lib_load_event(FILE *err
, struct debug_info
*debug_info
,
611 struct bt_ctf_event
*event
)
613 handle_bin_info_event(err
, debug_info
, event
, false);
617 void handle_lib_unload_event(FILE *err
, struct debug_info
*debug_info
,
618 struct bt_ctf_event
*event
)
620 struct proc_debug_info_sources
*proc_dbg_info_src
;
623 gpointer key_ptr
= NULL
;
626 ret
= get_payload_unsigned_int_field_value(err
,
627 event
, "_baddr", &baddr
);
629 BT_LOGE_STR("Failed to get unsigned int value for _baddr field.");
634 ret
= get_stream_event_context_int_field_value(err
, event
, "_vpid",
640 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
641 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
642 if (!proc_dbg_info_src
) {
646 key_ptr
= (gpointer
) &baddr
;
647 (void) g_hash_table_remove(proc_dbg_info_src
->baddr_to_bin_info
,
654 void handle_statedump_start(FILE *err
, struct debug_info
*debug_info
,
655 struct bt_ctf_event
*event
)
657 struct proc_debug_info_sources
*proc_dbg_info_src
;
661 ret
= get_stream_event_context_int_field_value(err
, event
,
667 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
668 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
669 if (!proc_dbg_info_src
) {
673 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
674 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
681 void debug_info_handle_event(FILE *err
, struct bt_ctf_event
*event
,
682 struct debug_info
*debug_info
)
684 struct bt_ctf_event_class
*event_class
;
685 const char *event_name
;
688 if (!debug_info
|| !event
) {
691 event_class
= bt_ctf_event_get_class(event
);
695 event_name
= bt_ctf_event_class_get_name(event_class
);
699 q_event_name
= g_quark_try_string(event_name
);
701 if (q_event_name
== debug_info
->q_statedump_bin_info
) {
703 handle_statedump_bin_info_event(err
, debug_info
, event
);
704 } else if (q_event_name
== debug_info
->q_dl_open
||
705 q_event_name
== debug_info
->q_lib_load
) {
707 * dl_open and lib_load events are both checked for since
708 * only dl_open was produced as of lttng-ust 2.8.
710 * lib_load, which is produced from lttng-ust 2.9+, is a lot
711 * more reliable since it will be emitted when other functions
712 * of the dlopen family are called (e.g. dlmopen) and when
713 * library are transitively loaded.
715 handle_lib_load_event(err
, debug_info
, event
);
716 } else if (q_event_name
== debug_info
->q_statedump_start
) {
717 /* Start state dump */
718 handle_statedump_start(err
, debug_info
, event
);
719 } else if (q_event_name
== debug_info
->q_statedump_debug_link
) {
720 /* Debug link info */
721 handle_statedump_debug_link_event(err
, debug_info
, event
);
722 } else if (q_event_name
== debug_info
->q_statedump_build_id
) {
724 handle_statedump_build_id_event(err
, debug_info
, event
);
725 } else if (q_event_name
== debug_info
-> q_lib_unload
) {
726 handle_lib_unload_event(err
, debug_info
, event
);