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
;
66 int debug_info_init(struct debug_info
*info
)
68 info
->q_statedump_bin_info
= g_quark_from_static_string(
69 "lttng_ust_statedump:bin_info");
70 info
->q_statedump_debug_link
= g_quark_from_static_string(
71 "lttng_ust_statedump:debug_link)");
72 info
->q_statedump_build_id
= g_quark_from_static_string(
73 "lttng_ust_statedump:build_id");
74 info
->q_statedump_start
= g_quark_from_static_string(
75 "lttng_ust_statedump:start");
76 info
->q_dl_open
= g_quark_from_static_string("lttng_ust_dl:dlopen");
77 info
->q_lib_load
= g_quark_from_static_string("lttng_ust_lib:load");
79 return bin_info_init();
83 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
85 if (!debug_info_src
) {
89 free(debug_info_src
->func
);
90 free(debug_info_src
->src_path
);
91 free(debug_info_src
->bin_path
);
92 free(debug_info_src
->bin_loc
);
93 g_free(debug_info_src
);
97 struct debug_info_source
*debug_info_source_create_from_bin(struct bin_info
*bin
,
101 struct debug_info_source
*debug_info_src
= NULL
;
102 struct source_location
*src_loc
= NULL
;
104 debug_info_src
= g_new0(struct debug_info_source
, 1);
106 if (!debug_info_src
) {
110 /* Lookup function name */
111 ret
= bin_info_lookup_function_name(bin
, ip
, &debug_info_src
->func
);
116 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
117 if (!bin
->is_elf_only
|| !debug_info_src
->func
) {
118 /* Lookup source location */
119 ret
= bin_info_lookup_source_location(bin
, ip
, &src_loc
);
120 printf_verbose("Failed to lookup source location (err: %i)\n", ret
);
124 debug_info_src
->line_no
= src_loc
->line_no
;
126 if (src_loc
->filename
) {
127 debug_info_src
->src_path
= strdup(src_loc
->filename
);
128 if (!debug_info_src
->src_path
) {
132 debug_info_src
->short_src_path
= get_filename_from_path(
133 debug_info_src
->src_path
);
136 source_location_destroy(src_loc
);
140 debug_info_src
->bin_path
= strdup(bin
->elf_path
);
141 if (!debug_info_src
->bin_path
) {
145 debug_info_src
->short_bin_path
= get_filename_from_path(
146 debug_info_src
->bin_path
);
148 ret
= bin_info_get_bin_loc(bin
, ip
, &(debug_info_src
->bin_loc
));
155 return debug_info_src
;
158 debug_info_source_destroy(debug_info_src
);
163 void proc_debug_info_sources_destroy(
164 struct proc_debug_info_sources
*proc_dbg_info_src
)
166 if (!proc_dbg_info_src
) {
170 if (proc_dbg_info_src
->baddr_to_bin_info
) {
171 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_bin_info
);
174 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
175 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
178 g_free(proc_dbg_info_src
);
182 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
184 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
186 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
187 if (!proc_dbg_info_src
) {
191 proc_dbg_info_src
->baddr_to_bin_info
= g_hash_table_new_full(
192 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
193 (GDestroyNotify
) bin_info_destroy
);
194 if (!proc_dbg_info_src
->baddr_to_bin_info
) {
198 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
199 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
200 (GDestroyNotify
) debug_info_source_destroy
);
201 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
206 return proc_dbg_info_src
;
209 proc_debug_info_sources_destroy(proc_dbg_info_src
);
214 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
215 GHashTable
*ht
, int64_t vpid
)
217 gpointer key
= g_new0(int64_t, 1);
218 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
224 *((int64_t *) key
) = vpid
;
226 /* Exists? Return it */
227 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
228 if (proc_dbg_info_src
) {
232 /* Otherwise, create and return it */
233 proc_dbg_info_src
= proc_debug_info_sources_create();
234 if (!proc_dbg_info_src
) {
238 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
239 /* Ownership passed to ht */
243 return proc_dbg_info_src
;
247 struct debug_info_source
*proc_debug_info_sources_get_entry(
248 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
250 struct debug_info_source
*debug_info_src
= NULL
;
251 gpointer key
= g_new0(uint64_t, 1);
253 gpointer baddr
, value
;
259 *((uint64_t *) key
) = ip
;
261 /* Look in IP to debug infos hash table first. */
262 debug_info_src
= g_hash_table_lookup(
263 proc_dbg_info_src
->ip_to_debug_info_src
,
265 if (debug_info_src
) {
269 /* Check in all bin_infos. */
270 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_bin_info
);
272 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
274 struct bin_info
*bin
= value
;
276 if (!bin_info_has_address(value
, ip
)) {
281 * Found; add it to cache.
283 * FIXME: this should be bounded in size (and implement
284 * a caching policy), and entries should be prunned when
285 * libraries are unmapped.
287 debug_info_src
= debug_info_source_create_from_bin(bin
, ip
);
288 if (debug_info_src
) {
290 proc_dbg_info_src
->ip_to_debug_info_src
,
291 key
, debug_info_src
);
292 /* Ownership passed to ht. */
300 return debug_info_src
;
304 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
305 int64_t vpid
, uint64_t ip
)
307 struct debug_info_source
*dbg_info_src
= NULL
;
308 struct proc_debug_info_sources
*proc_dbg_info_src
;
310 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
311 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
312 if (!proc_dbg_info_src
) {
316 dbg_info_src
= proc_debug_info_sources_get_entry(
317 proc_dbg_info_src
, ip
);
327 struct debug_info
*debug_info_create(void)
330 struct debug_info
*debug_info
;
332 debug_info
= g_new0(struct debug_info
, 1);
337 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
338 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
339 (GDestroyNotify
) proc_debug_info_sources_destroy
);
340 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
344 ret
= debug_info_init(debug_info
);
357 void debug_info_destroy(struct debug_info
*debug_info
)
363 if (debug_info
->vpid_to_proc_dbg_info_src
) {
364 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
373 void handle_statedump_build_id_event(struct debug_info
*debug_info
,
374 struct ctf_event_definition
*event_def
)
376 struct proc_debug_info_sources
*proc_dbg_info_src
;
377 struct bt_definition
*event_fields_def
= NULL
;
378 struct bt_definition
*sec_def
= NULL
;
379 struct bt_definition
*baddr_def
= NULL
;
380 struct bt_definition
*vpid_def
= NULL
;
381 struct bt_definition
*build_id_def
= NULL
;
382 struct definition_sequence
*build_id_seq
;
383 struct bin_info
*bin
= NULL
;
387 uint8_t *build_id
= NULL
;
388 uint64_t build_id_len
;
390 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
391 sec_def
= (struct bt_definition
*)
392 event_def
->stream
->stream_event_context
;
394 if (!event_fields_def
|| !sec_def
) {
398 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
403 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
408 build_id_def
= bt_lookup_definition(event_fields_def
, "_build_id");
413 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
417 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
421 if (build_id_def
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
) {
425 baddr
= bt_get_unsigned_int(baddr_def
);
426 vpid
= bt_get_signed_int(vpid_def
);
427 build_id_seq
= container_of(build_id_def
,
428 struct definition_sequence
, p
);
429 build_id_len
= build_id_seq
->length
->value
._unsigned
;
431 build_id
= g_malloc(build_id_len
);
436 for (i
= 0; i
< build_id_len
; ++i
) {
437 struct bt_definition
**field
;
439 field
= (struct bt_definition
**) &g_ptr_array_index(
440 build_id_seq
->elems
, i
);
441 build_id
[i
] = bt_get_unsigned_int(*field
);
444 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
445 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
446 if (!proc_dbg_info_src
) {
450 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
454 * The build_id event comes after the bin has been
455 * created. If it isn't found, just ignore this event.
460 bin_info_set_build_id(bin
, build_id
, build_id_len
);
468 void handle_statedump_debug_link_event(struct debug_info
*debug_info
,
469 struct ctf_event_definition
*event_def
)
471 struct proc_debug_info_sources
*proc_dbg_info_src
;
472 struct bt_definition
*event_fields_def
= NULL
;
473 struct bt_definition
*sec_def
= NULL
;
474 struct bt_definition
*baddr_def
= NULL
;
475 struct bt_definition
*vpid_def
= NULL
;
476 struct bt_definition
*filename_def
= NULL
;
477 struct bt_definition
*crc32_def
= NULL
;
478 struct bin_info
*bin
= NULL
;
481 char *filename
= NULL
;
484 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
485 sec_def
= (struct bt_definition
*)
486 event_def
->stream
->stream_event_context
;
488 if (!event_fields_def
|| !sec_def
) {
492 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
497 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
502 filename_def
= bt_lookup_definition(event_fields_def
, "_filename");
507 crc32_def
= bt_lookup_definition(event_fields_def
, "_crc32");
512 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
516 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
520 if (filename_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
524 if (crc32_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
528 baddr
= bt_get_unsigned_int(baddr_def
);
529 vpid
= bt_get_signed_int(vpid_def
);
531 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
532 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
533 if (!proc_dbg_info_src
) {
537 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
541 * The debug_link event comes after the bin has been
542 * created. If it isn't found, just ignore this event.
547 filename
= bt_get_string(filename_def
);
548 crc32
= bt_get_unsigned_int(crc32_def
);
550 bin_info_set_debug_link(bin
, filename
, crc32
);
557 void handle_bin_info_event(struct debug_info
*debug_info
,
558 struct ctf_event_definition
*event_def
, bool has_pic_field
)
560 struct bt_definition
*baddr_def
= NULL
;
561 struct bt_definition
*memsz_def
= NULL
;
562 struct bt_definition
*path_def
= NULL
;
563 struct bt_definition
*is_pic_def
= NULL
;
564 struct bt_definition
*vpid_def
= NULL
;
565 struct bt_definition
*event_fields_def
= NULL
;
566 struct bt_definition
*sec_def
= NULL
;
567 struct proc_debug_info_sources
*proc_dbg_info_src
;
568 struct bin_info
*bin
;
569 uint64_t baddr
, memsz
;
575 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
576 sec_def
= (struct bt_definition
*)
577 event_def
->stream
->stream_event_context
;
579 if (!event_fields_def
|| !sec_def
) {
583 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
588 memsz_def
= bt_lookup_definition(event_fields_def
, "_memsz");
593 path_def
= bt_lookup_definition(event_fields_def
, "_path");
599 is_pic_def
= bt_lookup_definition(event_fields_def
, "_is_pic");
604 if (is_pic_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
608 is_pic
= (bt_get_unsigned_int(is_pic_def
) == 1);
611 * dlopen has no is_pic field, because the shared
612 * object is always PIC.
617 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
622 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
626 if (memsz_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
630 if (path_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
634 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
638 baddr
= bt_get_unsigned_int(baddr_def
);
639 memsz
= bt_get_unsigned_int(memsz_def
);
640 path
= bt_get_string(path_def
);
641 vpid
= bt_get_signed_int(vpid_def
);
652 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
653 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
654 if (!proc_dbg_info_src
) {
658 key
= g_new0(uint64_t, 1);
663 *((uint64_t *) key
) = baddr
;
665 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
671 bin
= bin_info_create(path
, baddr
, memsz
, is_pic
);
676 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
,
678 /* Ownership passed to ht. */
687 void handle_statedump_bin_info_event(struct debug_info
*debug_info
,
688 struct ctf_event_definition
*event_def
)
690 handle_bin_info_event(debug_info
, event_def
, true);
694 void handle_lib_load_event(struct debug_info
*debug_info
,
695 struct ctf_event_definition
*event_def
)
697 handle_bin_info_event(debug_info
, event_def
, false);
702 void handle_statedump_start(struct debug_info
*debug_info
,
703 struct ctf_event_definition
*event_def
)
705 struct bt_definition
*vpid_def
= NULL
;
706 struct bt_definition
*sec_def
= NULL
;
707 struct proc_debug_info_sources
*proc_dbg_info_src
;
710 sec_def
= (struct bt_definition
*)
711 event_def
->stream
->stream_event_context
;
716 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
721 vpid
= bt_get_signed_int(vpid_def
);
723 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
724 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
725 if (!proc_dbg_info_src
) {
729 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
730 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
737 void register_event_debug_infos(struct debug_info
*debug_info
,
738 struct ctf_event_definition
*event
)
740 struct bt_definition
*ip_def
, *vpid_def
;
743 struct bt_definition
*sec_def
;
745 /* Get stream event context definition. */
746 sec_def
= (struct bt_definition
*) event
->stream
->stream_event_context
;
751 /* Get "ip" and "vpid" definitions. */
752 vpid_def
= bt_lookup_definition((struct bt_definition
*) sec_def
,
754 ip_def
= bt_lookup_definition((struct bt_definition
*) sec_def
, "_ip");
756 if (!vpid_def
|| !ip_def
) {
760 vpid
= bt_get_signed_int(vpid_def
);
761 ip
= bt_get_unsigned_int(ip_def
);
763 /* Get debug info for this context. */
764 ((struct definition_integer
*) ip_def
)->debug_info_src
=
765 debug_info_query(debug_info
, vpid
, ip
);
772 void debug_info_handle_event(struct debug_info
*debug_info
,
773 struct ctf_event_definition
*event
)
775 struct ctf_event_declaration
*event_class
;
776 struct ctf_stream_declaration
*stream_class
;
778 if (!debug_info
|| !event
) {
782 stream_class
= event
->stream
->stream_class
;
783 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
784 event
->stream
->event_id
);
786 if (event_class
->name
== debug_info
->q_statedump_bin_info
) {
788 handle_statedump_bin_info_event(debug_info
, event
);
789 } else if (event_class
->name
== debug_info
->q_dl_open
||
790 event_class
->name
== debug_info
->q_lib_load
) {
792 * dl_open and lib_load events are both checked for since
793 * only dl_open was produced as of lttng-ust 2.8.
795 * lib_load, which is produced from lttng-ust 2.9+, is a lot
796 * more reliable since it will be emitted when other functions
797 * of the dlopen family are called (e.g. dlmopen) and when
798 * library are transitively loaded.
800 handle_lib_load_event(debug_info
, event
);
801 } else if (event_class
->name
== debug_info
->q_statedump_start
) {
802 /* Start state dump */
803 handle_statedump_start(debug_info
, event
);
804 } else if (event_class
->name
== debug_info
->q_statedump_debug_link
) {
805 /* Debug link info */
806 handle_statedump_debug_link_event(debug_info
, event
);
807 } else if (event_class
->name
== debug_info
->q_statedump_build_id
) {
809 handle_statedump_build_id_event(debug_info
, event
);
811 /* Other events: register debug infos */
812 register_event_debug_infos(debug_info
, event
);