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/debuginfo.h>
33 #include <babeltrace/so-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 so info; owned by
40 * proc_debug_info_sources.
42 GHashTable
*baddr_to_so_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_soinfo
;
58 GQuark q_statedump_debug_link
;
59 GQuark q_statedump_build_id
;
60 GQuark q_statedump_start
;
65 int debug_info_init(struct debug_info
*info
)
67 info
->q_statedump_soinfo
= g_quark_from_string(
68 "lttng_ust_statedump:soinfo");
69 info
->q_statedump_debug_link
= g_quark_from_string(
70 "lttng_ust_statedump:debug_link)");
71 info
->q_statedump_build_id
= g_quark_from_string(
72 "lttng_ust_statedump:build_id");
73 info
->q_statedump_start
= g_quark_from_string(
74 "lttng_ust_statedump:start");
75 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
77 return so_info_init();
81 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
83 if (!debug_info_src
) {
87 free(debug_info_src
->func
);
88 free(debug_info_src
->src_path
);
89 free(debug_info_src
->bin_path
);
90 g_free(debug_info_src
);
94 struct debug_info_source
*debug_info_source_create_from_so(struct so_info
*so
,
98 struct debug_info_source
*debug_info_src
= NULL
;
99 struct source_location
*src_loc
= NULL
;
101 debug_info_src
= g_new0(struct debug_info_source
, 1);
103 if (!debug_info_src
) {
107 /* Lookup function name */
108 ret
= so_info_lookup_function_name(so
, ip
, &debug_info_src
->func
);
113 /* Can't retrieve src_loc from ELF only, skip it */
114 if (!so
->is_elf_only
) {
115 /* Lookup source location */
116 ret
= so_info_lookup_source_location(so
, ip
, &src_loc
);
123 debug_info_src
->line_no
= src_loc
->line_no
;
125 if (src_loc
->filename
) {
126 debug_info_src
->src_path
= strdup(src_loc
->filename
);
127 if (!debug_info_src
->src_path
) {
131 debug_info_src
->short_src_path
= get_filename_from_path(
136 source_location_destroy(src_loc
);
140 debug_info_src
->bin_path
= strdup(so
->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
);
150 return debug_info_src
;
153 debug_info_source_destroy(debug_info_src
);
158 void proc_debug_info_sources_destroy(
159 struct proc_debug_info_sources
*proc_dbg_info_src
)
161 if (!proc_dbg_info_src
) {
165 if (proc_dbg_info_src
->baddr_to_so_info
) {
166 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_so_info
);
169 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
170 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
173 g_free(proc_dbg_info_src
);
177 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
179 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
181 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
182 if (!proc_dbg_info_src
) {
186 proc_dbg_info_src
->baddr_to_so_info
= g_hash_table_new_full(
187 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
188 (GDestroyNotify
) so_info_destroy
);
189 if (!proc_dbg_info_src
->baddr_to_so_info
) {
193 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
194 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
195 (GDestroyNotify
) debug_info_source_destroy
);
196 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
201 return proc_dbg_info_src
;
204 proc_debug_info_sources_destroy(proc_dbg_info_src
);
209 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
210 GHashTable
*ht
, int64_t vpid
)
212 gpointer key
= g_new0(int64_t, 1);
213 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
219 *((int64_t *) key
) = vpid
;
221 /* Exists? Return it */
222 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
223 if (proc_dbg_info_src
) {
227 /* Otherwise, create and return it */
228 proc_dbg_info_src
= proc_debug_info_sources_create();
229 if (!proc_dbg_info_src
) {
233 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
234 /* Ownership passed to ht */
238 return proc_dbg_info_src
;
242 struct debug_info_source
*proc_debug_info_sources_get_entry(
243 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
245 struct debug_info_source
*debug_info_src
= NULL
;
246 gpointer key
= g_new0(uint64_t, 1);
248 gpointer baddr
, value
;
254 *((uint64_t *) key
) = ip
;
256 /* Look in IP to debug infos hash table first. */
257 debug_info_src
= g_hash_table_lookup(
258 proc_dbg_info_src
->ip_to_debug_info_src
,
260 if (debug_info_src
) {
264 /* Check in all so_infos. */
265 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_so_info
);
267 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
269 struct so_info
*so
= value
;
271 if (!so_info_has_address(value
, ip
)) {
276 * Found; add it to cache.
278 * FIXME: this should be bounded in size (and implement
279 * a caching policy), and entries should be prunned when
280 * libraries are unmapped.
282 debug_info_src
= debug_info_source_create_from_so(so
, ip
);
283 if (debug_info_src
) {
285 proc_dbg_info_src
->ip_to_debug_info_src
,
286 key
, debug_info_src
);
287 /* Ownership passed to ht. */
295 return debug_info_src
;
299 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
300 int64_t vpid
, uint64_t ip
)
302 struct debug_info_source
*dbg_info_src
= NULL
;
303 struct proc_debug_info_sources
*proc_dbg_info_src
;
305 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
306 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
307 if (!proc_dbg_info_src
) {
311 dbg_info_src
= proc_debug_info_sources_get_entry(
312 proc_dbg_info_src
, ip
);
322 struct debug_info
*debug_info_create(void)
325 struct debug_info
*debug_info
;
327 debug_info
= g_new0(struct debug_info
, 1);
332 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
333 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
334 (GDestroyNotify
) proc_debug_info_sources_destroy
);
335 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
339 ret
= debug_info_init(debug_info
);
352 void debug_info_destroy(struct debug_info
*debug_info
)
358 if (debug_info
->vpid_to_proc_dbg_info_src
) {
359 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
368 void handle_statedump_build_id_event(struct debug_info
*debug_info
,
369 struct ctf_event_definition
*event_def
)
371 struct proc_debug_info_sources
*proc_dbg_info_src
;
372 struct bt_definition
*event_fields_def
= NULL
;
373 struct bt_definition
*sec_def
= NULL
;
374 struct bt_definition
*baddr_def
= NULL
;
375 struct bt_definition
*vpid_def
= NULL
;
376 struct bt_definition
*build_id_def
= NULL
;
377 struct definition_sequence
*build_id_seq
;
378 struct so_info
*so
= NULL
;
382 uint8_t *build_id
= NULL
;
383 uint64_t build_id_len
;
385 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
386 sec_def
= (struct bt_definition
*)
387 event_def
->stream
->stream_event_context
;
389 if (!event_fields_def
|| !sec_def
) {
393 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
398 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
403 build_id_def
= bt_lookup_definition(event_fields_def
, "_build_id");
408 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
412 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
416 if (build_id_def
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
) {
420 baddr
= bt_get_unsigned_int(baddr_def
);
421 vpid
= bt_get_signed_int(vpid_def
);
422 build_id_seq
= container_of(build_id_def
,
423 struct definition_sequence
, p
);
424 build_id_len
= build_id_seq
->length
->value
._unsigned
;
426 build_id
= g_malloc(build_id_len
);
431 for (i
= 0; i
< build_id_len
; ++i
) {
432 struct bt_definition
**field
;
434 field
= (struct bt_definition
**) &g_ptr_array_index(
435 build_id_seq
->elems
, i
);
436 build_id
[i
] = bt_get_unsigned_int(*field
);
439 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
440 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
441 if (!proc_dbg_info_src
) {
445 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
449 * The build_id event comes after the so has been
450 * created. If it isn't found, just ignore this event.
455 so_info_set_build_id(so
, build_id
, build_id_len
);
463 void handle_statedump_debug_link_event(struct debug_info
*debug_info
,
464 struct ctf_event_definition
*event_def
)
466 struct proc_debug_info_sources
*proc_dbg_info_src
;
467 struct bt_definition
*event_fields_def
= NULL
;
468 struct bt_definition
*sec_def
= NULL
;
469 struct bt_definition
*baddr_def
= NULL
;
470 struct bt_definition
*vpid_def
= NULL
;
471 struct bt_definition
*filename_def
= NULL
;
472 struct bt_definition
*crc32_def
= NULL
;
473 struct so_info
*so
= NULL
;
476 char *filename
= NULL
;
479 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
480 sec_def
= (struct bt_definition
*)
481 event_def
->stream
->stream_event_context
;
483 if (!event_fields_def
|| !sec_def
) {
487 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
492 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
497 filename_def
= bt_lookup_definition(event_fields_def
, "_filename");
502 crc32_def
= bt_lookup_definition(event_fields_def
, "_crc32");
507 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
511 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
515 if (filename_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
519 if (crc32_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
523 baddr
= bt_get_unsigned_int(baddr_def
);
524 vpid
= bt_get_signed_int(vpid_def
);
526 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
527 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
528 if (!proc_dbg_info_src
) {
532 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
536 * The debug_link event comes after the so has been
537 * created. If it isn't found, just ignore this event.
542 filename
= bt_get_string(filename_def
);
543 crc32
= bt_get_unsigned_int(crc32_def
);
545 so_info_set_debug_link(so
, filename
, crc32
);
552 void handle_statedump_soinfo_event(struct debug_info
*debug_info
,
553 struct ctf_event_definition
*event_def
)
555 struct bt_definition
*baddr_def
= NULL
;
556 struct bt_definition
*memsz_def
= NULL
;
557 struct bt_definition
*sopath_def
= NULL
;
558 struct bt_definition
*vpid_def
= NULL
;
559 struct bt_definition
*event_fields_def
= NULL
;
560 struct bt_definition
*sec_def
= NULL
;
561 struct proc_debug_info_sources
*proc_dbg_info_src
;
563 uint64_t baddr
, memsz
;
568 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
569 sec_def
= (struct bt_definition
*)
570 event_def
->stream
->stream_event_context
;
572 if (!event_fields_def
|| !sec_def
) {
576 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
581 memsz_def
= bt_lookup_definition(event_fields_def
, "_memsz");
586 sopath_def
= bt_lookup_definition(event_fields_def
, "_sopath");
591 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
596 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
600 if (memsz_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
604 if (sopath_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
608 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
612 baddr
= bt_get_unsigned_int(baddr_def
);
613 memsz
= bt_get_unsigned_int(memsz_def
);
614 sopath
= bt_get_string(sopath_def
);
615 vpid
= bt_get_signed_int(vpid_def
);
626 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
627 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
628 if (!proc_dbg_info_src
) {
632 key
= g_new0(uint64_t, 1);
637 *((uint64_t *) key
) = baddr
;
639 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
645 so
= so_info_create(sopath
, baddr
, memsz
);
650 g_hash_table_insert(proc_dbg_info_src
->baddr_to_so_info
,
652 /* Ownership passed to ht. */
661 void handle_statedump_start(struct debug_info
*debug_info
,
662 struct ctf_event_definition
*event_def
)
664 struct bt_definition
*vpid_def
= NULL
;
665 struct bt_definition
*sec_def
= NULL
;
666 struct proc_debug_info_sources
*proc_dbg_info_src
;
669 sec_def
= (struct bt_definition
*)
670 event_def
->stream
->stream_event_context
;
675 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
680 vpid
= bt_get_signed_int(vpid_def
);
682 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
683 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
684 if (!proc_dbg_info_src
) {
688 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_so_info
);
689 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
696 void register_event_debug_infos(struct debug_info
*debug_info
,
697 struct ctf_event_definition
*event
)
699 struct bt_definition
*ip_def
, *vpid_def
;
702 struct bt_definition
*sec_def
;
704 /* Get stream event context definition. */
705 sec_def
= (struct bt_definition
*) event
->stream
->stream_event_context
;
710 /* Get "ip" and "vpid" definitions. */
711 vpid_def
= bt_lookup_definition((struct bt_definition
*) sec_def
,
713 ip_def
= bt_lookup_definition((struct bt_definition
*) sec_def
, "_ip");
715 if (!vpid_def
|| !ip_def
) {
719 vpid
= bt_get_signed_int(vpid_def
);
720 ip
= bt_get_unsigned_int(ip_def
);
722 /* Get debug info for this context. */
723 ((struct definition_integer
*) ip_def
)->debug_info_src
=
724 debug_info_query(debug_info
, vpid
, ip
);
731 void debug_info_handle_event(struct debug_info
*debug_info
,
732 struct ctf_event_definition
*event
)
734 struct ctf_event_declaration
*event_class
;
735 struct ctf_stream_declaration
*stream_class
;
737 if (!debug_info
|| !event
) {
741 stream_class
= event
->stream
->stream_class
;
742 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
743 event
->stream
->event_id
);
745 if (event_class
->name
== debug_info
->q_statedump_soinfo
||
746 event_class
->name
== debug_info
->q_dl_open
) {
747 /* State dump/dlopen() */
748 handle_statedump_soinfo_event(debug_info
, event
);
749 } else if (event_class
->name
== debug_info
->q_statedump_start
) {
750 /* Start state dump */
751 handle_statedump_start(debug_info
, event
);
752 } else if (event_class
->name
== debug_info
->q_statedump_debug_link
) {
753 /* Debug link info */
754 handle_statedump_debug_link_event(debug_info
, event
);
755 } else if (event_class
->name
== debug_info
->q_statedump_build_id
) {
757 handle_statedump_build_id_event(debug_info
, event
);
759 /* Other events: register debug infos */
760 register_event_debug_infos(debug_info
, event
);