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
30 #include <babeltrace/types.h>
31 #include <babeltrace/ctf-ir/metadata.h>
32 #include <babeltrace/debug-info.h>
33 #include <babeltrace/bin-info.h>
34 #include <babeltrace/babeltrace-internal.h>
35 #include <babeltrace/utils.h>
37 struct proc_debug_info_sources
{
39 * Hash table: base address (pointer to uint64_t) to bin info; owned by
40 * proc_debug_info_sources.
42 GHashTable
*baddr_to_bin_info
;
45 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
46 * owned by proc_debug_info_sources.
48 GHashTable
*ip_to_debug_info_src
;
53 * Hash table of VPIDs (pointer to int64_t) to
54 * (struct ctf_proc_debug_infos*); owned by debug_info.
56 GHashTable
*vpid_to_proc_dbg_info_src
;
57 GQuark q_statedump_bin_info
;
58 GQuark q_statedump_debug_link
;
59 GQuark q_statedump_build_id
;
60 GQuark q_statedump_start
;
67 int debug_info_init(struct debug_info
*info
)
69 info
->q_statedump_bin_info
= g_quark_from_string(
70 "lttng_ust_statedump:bin_info");
71 info
->q_statedump_debug_link
= g_quark_from_string(
72 "lttng_ust_statedump:debug_link)");
73 info
->q_statedump_build_id
= g_quark_from_string(
74 "lttng_ust_statedump:build_id");
75 info
->q_statedump_start
= g_quark_from_string(
76 "lttng_ust_statedump:start");
77 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
78 info
->q_lib_load
= g_quark_from_string("lttng_ust_lib:load");
79 info
->q_lib_unload
= g_quark_from_string("lttng_ust_lib:unload");
81 return bin_info_init();
85 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
87 if (!debug_info_src
) {
91 free(debug_info_src
->func
);
92 free(debug_info_src
->src_path
);
93 free(debug_info_src
->bin_path
);
94 free(debug_info_src
->bin_loc
);
95 g_free(debug_info_src
);
99 struct debug_info_source
*debug_info_source_create_from_bin(struct bin_info
*bin
,
103 struct debug_info_source
*debug_info_src
= NULL
;
104 struct source_location
*src_loc
= NULL
;
106 debug_info_src
= g_new0(struct debug_info_source
, 1);
108 if (!debug_info_src
) {
112 /* Lookup function name */
113 ret
= bin_info_lookup_function_name(bin
, ip
, &debug_info_src
->func
);
118 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
119 if (!bin
->is_elf_only
|| !debug_info_src
->func
) {
120 /* Lookup source location */
121 ret
= bin_info_lookup_source_location(bin
, ip
, &src_loc
);
122 printf_verbose("Failed to lookup source location (err: %i)\n", ret
);
126 debug_info_src
->line_no
= src_loc
->line_no
;
128 if (src_loc
->filename
) {
129 debug_info_src
->src_path
= strdup(src_loc
->filename
);
130 if (!debug_info_src
->src_path
) {
134 debug_info_src
->short_src_path
= get_filename_from_path(
135 debug_info_src
->src_path
);
138 source_location_destroy(src_loc
);
142 debug_info_src
->bin_path
= strdup(bin
->elf_path
);
143 if (!debug_info_src
->bin_path
) {
147 debug_info_src
->short_bin_path
= get_filename_from_path(
148 debug_info_src
->bin_path
);
150 ret
= bin_info_get_bin_loc(bin
, ip
, &(debug_info_src
->bin_loc
));
157 return debug_info_src
;
160 debug_info_source_destroy(debug_info_src
);
165 void proc_debug_info_sources_destroy(
166 struct proc_debug_info_sources
*proc_dbg_info_src
)
168 if (!proc_dbg_info_src
) {
172 if (proc_dbg_info_src
->baddr_to_bin_info
) {
173 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_bin_info
);
176 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
177 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
180 g_free(proc_dbg_info_src
);
184 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
186 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
188 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
189 if (!proc_dbg_info_src
) {
193 proc_dbg_info_src
->baddr_to_bin_info
= g_hash_table_new_full(
194 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
195 (GDestroyNotify
) bin_info_destroy
);
196 if (!proc_dbg_info_src
->baddr_to_bin_info
) {
200 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
201 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
202 (GDestroyNotify
) debug_info_source_destroy
);
203 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
208 return proc_dbg_info_src
;
211 proc_debug_info_sources_destroy(proc_dbg_info_src
);
216 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
217 GHashTable
*ht
, int64_t vpid
)
219 gpointer key
= g_new0(int64_t, 1);
220 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
226 *((int64_t *) key
) = vpid
;
228 /* Exists? Return it */
229 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
230 if (proc_dbg_info_src
) {
234 /* Otherwise, create and return it */
235 proc_dbg_info_src
= proc_debug_info_sources_create();
236 if (!proc_dbg_info_src
) {
240 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
241 /* Ownership passed to ht */
245 return proc_dbg_info_src
;
249 struct debug_info_source
*proc_debug_info_sources_get_entry(
250 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
252 struct debug_info_source
*debug_info_src
= NULL
;
253 gpointer key
= g_new0(uint64_t, 1);
255 gpointer baddr
, value
;
261 *((uint64_t *) key
) = ip
;
263 /* Look in IP to debug infos hash table first. */
264 debug_info_src
= g_hash_table_lookup(
265 proc_dbg_info_src
->ip_to_debug_info_src
,
267 if (debug_info_src
) {
271 /* Check in all bin_infos. */
272 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_bin_info
);
274 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
276 struct bin_info
*bin
= value
;
278 if (!bin_info_has_address(value
, ip
)) {
283 * Found; add it to cache.
285 * FIXME: this should be bounded in size (and implement
286 * a caching policy), and entries should be prunned when
287 * libraries are unmapped.
289 debug_info_src
= debug_info_source_create_from_bin(bin
, ip
);
290 if (debug_info_src
) {
292 proc_dbg_info_src
->ip_to_debug_info_src
,
293 key
, debug_info_src
);
294 /* Ownership passed to ht. */
302 return debug_info_src
;
306 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
307 int64_t vpid
, uint64_t ip
)
309 struct debug_info_source
*dbg_info_src
= NULL
;
310 struct proc_debug_info_sources
*proc_dbg_info_src
;
312 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
313 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
314 if (!proc_dbg_info_src
) {
318 dbg_info_src
= proc_debug_info_sources_get_entry(
319 proc_dbg_info_src
, ip
);
329 struct debug_info
*debug_info_create(void)
332 struct debug_info
*debug_info
;
334 debug_info
= g_new0(struct debug_info
, 1);
339 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
340 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
341 (GDestroyNotify
) proc_debug_info_sources_destroy
);
342 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
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(struct debug_info
*debug_info
,
376 struct ctf_event_definition
*event_def
)
378 struct proc_debug_info_sources
*proc_dbg_info_src
;
379 struct bt_definition
*event_fields_def
= NULL
;
380 struct bt_definition
*sec_def
= NULL
;
381 struct bt_definition
*baddr_def
= NULL
;
382 struct bt_definition
*vpid_def
= NULL
;
383 struct bt_definition
*build_id_def
= NULL
;
384 struct definition_sequence
*build_id_seq
;
385 struct bin_info
*bin
= NULL
;
389 uint8_t *build_id
= NULL
;
390 uint64_t build_id_len
;
392 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
393 sec_def
= (struct bt_definition
*)
394 event_def
->stream
->stream_event_context
;
396 if (!event_fields_def
|| !sec_def
) {
400 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
405 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
410 build_id_def
= bt_lookup_definition(event_fields_def
, "_build_id");
415 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
419 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
423 if (build_id_def
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
) {
427 baddr
= bt_get_unsigned_int(baddr_def
);
428 vpid
= bt_get_signed_int(vpid_def
);
429 build_id_seq
= container_of(build_id_def
,
430 struct definition_sequence
, p
);
431 build_id_len
= build_id_seq
->length
->value
._unsigned
;
433 build_id
= g_malloc(build_id_len
);
438 for (i
= 0; i
< build_id_len
; ++i
) {
439 struct bt_definition
**field
;
441 field
= (struct bt_definition
**) &g_ptr_array_index(
442 build_id_seq
->elems
, i
);
443 build_id
[i
] = bt_get_unsigned_int(*field
);
446 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
447 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
448 if (!proc_dbg_info_src
) {
452 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
456 * The build_id event comes after the bin has been
457 * created. If it isn't found, just ignore this event.
462 bin_info_set_build_id(bin
, build_id
, build_id_len
);
470 void handle_statedump_debug_link_event(struct debug_info
*debug_info
,
471 struct ctf_event_definition
*event_def
)
473 struct proc_debug_info_sources
*proc_dbg_info_src
;
474 struct bt_definition
*event_fields_def
= NULL
;
475 struct bt_definition
*sec_def
= NULL
;
476 struct bt_definition
*baddr_def
= NULL
;
477 struct bt_definition
*vpid_def
= NULL
;
478 struct bt_definition
*filename_def
= NULL
;
479 struct bt_definition
*crc32_def
= NULL
;
480 struct bin_info
*bin
= NULL
;
483 char *filename
= NULL
;
486 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
487 sec_def
= (struct bt_definition
*)
488 event_def
->stream
->stream_event_context
;
490 if (!event_fields_def
|| !sec_def
) {
494 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
499 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
504 filename_def
= bt_lookup_definition(event_fields_def
, "_filename");
509 crc32_def
= bt_lookup_definition(event_fields_def
, "_crc32");
514 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
518 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
522 if (filename_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
526 if (crc32_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
530 baddr
= bt_get_unsigned_int(baddr_def
);
531 vpid
= bt_get_signed_int(vpid_def
);
533 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
534 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
535 if (!proc_dbg_info_src
) {
539 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
543 * The debug_link event comes after the bin has been
544 * created. If it isn't found, just ignore this event.
549 filename
= bt_get_string(filename_def
);
550 crc32
= bt_get_unsigned_int(crc32_def
);
552 bin_info_set_debug_link(bin
, filename
, crc32
);
559 void handle_bin_info_event(struct debug_info
*debug_info
,
560 struct ctf_event_definition
*event_def
, bool has_pic_field
)
562 struct bt_definition
*baddr_def
= NULL
;
563 struct bt_definition
*memsz_def
= NULL
;
564 struct bt_definition
*path_def
= NULL
;
565 struct bt_definition
*is_pic_def
= NULL
;
566 struct bt_definition
*vpid_def
= NULL
;
567 struct bt_definition
*event_fields_def
= NULL
;
568 struct bt_definition
*sec_def
= NULL
;
569 struct proc_debug_info_sources
*proc_dbg_info_src
;
570 struct bin_info
*bin
;
571 uint64_t baddr
, memsz
;
577 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
578 sec_def
= (struct bt_definition
*)
579 event_def
->stream
->stream_event_context
;
581 if (!event_fields_def
|| !sec_def
) {
585 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
590 memsz_def
= bt_lookup_definition(event_fields_def
, "_memsz");
595 path_def
= bt_lookup_definition(event_fields_def
, "_path");
601 is_pic_def
= bt_lookup_definition(event_fields_def
, "_is_pic");
606 if (is_pic_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
610 is_pic
= (bt_get_unsigned_int(is_pic_def
) == 1);
613 * dlopen has no is_pic field, because the shared
614 * object is always PIC.
619 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
624 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
628 if (memsz_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
632 if (path_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
636 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
640 baddr
= bt_get_unsigned_int(baddr_def
);
641 memsz
= bt_get_unsigned_int(memsz_def
);
642 path
= bt_get_string(path_def
);
643 vpid
= bt_get_signed_int(vpid_def
);
654 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
655 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
656 if (!proc_dbg_info_src
) {
660 key
= g_new0(uint64_t, 1);
665 *((uint64_t *) key
) = baddr
;
667 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
673 bin
= bin_info_create(path
, baddr
, memsz
, is_pic
);
678 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
,
680 /* Ownership passed to ht. */
689 void handle_statedump_bin_info_event(struct debug_info
*debug_info
,
690 struct ctf_event_definition
*event_def
)
692 handle_bin_info_event(debug_info
, event_def
, true);
696 void handle_lib_load_event(struct debug_info
*debug_info
,
697 struct ctf_event_definition
*event_def
)
699 handle_bin_info_event(debug_info
, event_def
, false);
703 void handle_lib_unload_event(struct debug_info
*debug_info
,
704 struct ctf_event_definition
*event_def
)
706 struct bt_definition
*baddr_def
= NULL
;
707 struct bt_definition
*event_fields_def
= NULL
;
708 struct bt_definition
*sec_def
= NULL
;
709 struct bt_definition
*vpid_def
= NULL
;
710 struct proc_debug_info_sources
*proc_dbg_info_src
;
713 gpointer key_ptr
= NULL
;
715 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
716 sec_def
= (struct bt_definition
*)
717 event_def
->stream
->stream_event_context
;
718 if (!event_fields_def
|| !sec_def
) {
722 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
727 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
732 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
735 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
739 baddr
= bt_get_unsigned_int(baddr_def
);
740 vpid
= bt_get_signed_int(vpid_def
);
742 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
743 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
744 if (!proc_dbg_info_src
) {
748 key_ptr
= (gpointer
) &baddr
;
749 (void) g_hash_table_remove(proc_dbg_info_src
->baddr_to_bin_info
,
756 void handle_statedump_start(struct debug_info
*debug_info
,
757 struct ctf_event_definition
*event_def
)
759 struct bt_definition
*vpid_def
= NULL
;
760 struct bt_definition
*sec_def
= NULL
;
761 struct proc_debug_info_sources
*proc_dbg_info_src
;
764 sec_def
= (struct bt_definition
*)
765 event_def
->stream
->stream_event_context
;
770 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
775 vpid
= bt_get_signed_int(vpid_def
);
777 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
778 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
779 if (!proc_dbg_info_src
) {
783 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
784 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
791 void register_event_debug_infos(struct debug_info
*debug_info
,
792 struct ctf_event_definition
*event
)
794 struct bt_definition
*ip_def
, *vpid_def
;
797 struct bt_definition
*sec_def
;
799 /* Get stream event context definition. */
800 sec_def
= (struct bt_definition
*) event
->stream
->stream_event_context
;
805 /* Get "ip" and "vpid" definitions. */
806 vpid_def
= bt_lookup_definition((struct bt_definition
*) sec_def
,
808 ip_def
= bt_lookup_definition((struct bt_definition
*) sec_def
, "_ip");
810 if (!vpid_def
|| !ip_def
) {
814 vpid
= bt_get_signed_int(vpid_def
);
815 ip
= bt_get_unsigned_int(ip_def
);
817 /* Get debug info for this context. */
818 ((struct definition_integer
*) ip_def
)->debug_info_src
=
819 debug_info_query(debug_info
, vpid
, ip
);
826 void debug_info_handle_event(struct debug_info
*debug_info
,
827 struct ctf_event_definition
*event
)
829 struct ctf_event_declaration
*event_class
;
830 struct ctf_stream_declaration
*stream_class
;
832 if (!debug_info
|| !event
) {
836 stream_class
= event
->stream
->stream_class
;
837 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
838 event
->stream
->event_id
);
840 if (event_class
->name
== debug_info
->q_statedump_bin_info
) {
842 handle_statedump_bin_info_event(debug_info
, event
);
843 } else if (event_class
->name
== debug_info
->q_dl_open
||
844 event_class
->name
== debug_info
->q_lib_load
) {
846 * dl_open and lib_load events are both checked for since
847 * only dl_open was produced as of lttng-ust 2.8.
849 * lib_load, which is produced from lttng-ust 2.9+, is a lot
850 * more reliable since it will be emitted when other functions
851 * of the dlopen family are called (e.g. dlmopen) and when
852 * library are transitively loaded.
854 handle_lib_load_event(debug_info
, event
);
855 } else if (event_class
->name
== debug_info
->q_statedump_start
) {
856 /* Start state dump */
857 handle_statedump_start(debug_info
, event
);
858 } else if (event_class
->name
== debug_info
->q_statedump_debug_link
) {
859 /* Debug link info */
860 handle_statedump_debug_link_event(debug_info
, event
);
861 } else if (event_class
->name
== debug_info
->q_statedump_build_id
) {
863 handle_statedump_build_id_event(debug_info
, event
);
864 } else if (event_class
->name
== debug_info
-> q_lib_unload
) {
865 handle_lib_unload_event(debug_info
, event
);
868 /* All events: register debug infos */
869 register_event_debug_infos(debug_info
, event
);