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>
36 struct proc_debug_info_sources
{
38 * Hash table: base address (pointer to uint64_t) to so info; owned by
39 * proc_debug_info_sources.
41 GHashTable
*baddr_to_so_info
;
44 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
45 * owned by proc_debug_info_sources.
47 GHashTable
*ip_to_debug_info_src
;
52 * Hash table of VPIDs (pointer to int64_t) to
53 * (struct ctf_proc_debug_infos*); owned by debug_info.
55 GHashTable
*vpid_to_proc_dbg_info_src
;
56 GQuark q_statedump_soinfo
;
57 GQuark q_statedump_debug_link
;
58 GQuark q_statedump_build_id
;
59 GQuark q_statedump_start
;
64 int debug_info_init(struct debug_info
*info
)
66 info
->q_statedump_soinfo
= g_quark_from_string(
67 "lttng_ust_statedump:soinfo");
68 info
->q_statedump_debug_link
= g_quark_from_string(
69 "lttng_ust_statedump:debug_link)");
70 info
->q_statedump_build_id
= g_quark_from_string(
71 "lttng_ust_statedump:build_id");
72 info
->q_statedump_start
= g_quark_from_string(
73 "lttng_ust_statedump:start");
74 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
76 return so_info_init();
80 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
82 if (!debug_info_src
) {
86 free(debug_info_src
->func
);
87 free(debug_info_src
->src_path
);
88 free(debug_info_src
->bin_path
);
89 g_free(debug_info_src
);
93 * Returns the location of a path's file (the last element of the path).
94 * Returns the original path on error.
97 const char *get_filename_from_path(const char *path
)
99 size_t i
= strlen(path
);
105 if (path
[i
- 1] == '/') {
107 * Path ends with a trailing slash, no filename to return.
108 * Return the original path.
114 if (path
[i
] == '/') {
124 struct debug_info_source
*debug_info_source_create_from_so(struct so_info
*so
,
128 struct debug_info_source
*debug_info_src
= NULL
;
129 struct source_location
*src_loc
= NULL
;
131 debug_info_src
= g_new0(struct debug_info_source
, 1);
133 if (!debug_info_src
) {
137 /* Lookup function name */
138 ret
= so_info_lookup_function_name(so
, ip
, &debug_info_src
->func
);
143 /* Can't retrieve src_loc from ELF only, skip it */
144 if (so
->is_elf_only
) {
148 /* Lookup source location */
149 ret
= so_info_lookup_source_location(so
, ip
, &src_loc
);
155 debug_info_src
->line_no
= src_loc
->line_no
;
157 if (src_loc
->filename
) {
158 debug_info_src
->src_path
= strdup(src_loc
->filename
);
159 if (!debug_info_src
->src_path
) {
163 debug_info_src
->short_src_path
= get_filename_from_path(
168 source_location_destroy(src_loc
);
172 debug_info_src
->bin_path
= strdup(so
->elf_path
);
173 if (!debug_info_src
->bin_path
) {
177 debug_info_src
->short_bin_path
= get_filename_from_path(
178 debug_info_src
->bin_path
);
182 return debug_info_src
;
185 debug_info_source_destroy(debug_info_src
);
190 void proc_debug_info_sources_destroy(
191 struct proc_debug_info_sources
*proc_dbg_info_src
)
193 if (!proc_dbg_info_src
) {
197 if (proc_dbg_info_src
->baddr_to_so_info
) {
198 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_so_info
);
201 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
202 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
205 g_free(proc_dbg_info_src
);
209 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
211 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
213 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
214 if (!proc_dbg_info_src
) {
218 proc_dbg_info_src
->baddr_to_so_info
= g_hash_table_new_full(
219 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
220 (GDestroyNotify
) so_info_destroy
);
221 if (!proc_dbg_info_src
->baddr_to_so_info
) {
225 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
226 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
227 (GDestroyNotify
) debug_info_source_destroy
);
228 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
233 return proc_dbg_info_src
;
236 proc_debug_info_sources_destroy(proc_dbg_info_src
);
241 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
242 GHashTable
*ht
, int64_t vpid
)
244 gpointer key
= g_new0(int64_t, 1);
245 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
251 *((int64_t *) key
) = vpid
;
253 /* Exists? Return it */
254 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
255 if (proc_dbg_info_src
) {
259 /* Otherwise, create and return it */
260 proc_dbg_info_src
= proc_debug_info_sources_create();
261 if (!proc_dbg_info_src
) {
265 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
266 /* Ownership passed to ht */
270 return proc_dbg_info_src
;
274 struct debug_info_source
*proc_debug_info_sources_get_entry(
275 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
277 struct debug_info_source
*debug_info_src
= NULL
;
278 gpointer key
= g_new0(uint64_t, 1);
280 gpointer baddr
, value
;
286 *((uint64_t *) key
) = ip
;
288 /* Look in IP to debug infos hash table first. */
289 debug_info_src
= g_hash_table_lookup(
290 proc_dbg_info_src
->ip_to_debug_info_src
,
292 if (debug_info_src
) {
296 /* Check in all so_infos. */
297 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_so_info
);
299 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
301 struct so_info
*so
= value
;
303 if (!so_info_has_address(value
, ip
)) {
308 * Found; add it to cache.
310 * FIXME: this should be bounded in size (and implement
311 * a caching policy), and entries should be prunned when
312 * libraries are unmapped.
314 debug_info_src
= debug_info_source_create_from_so(so
, ip
);
315 if (debug_info_src
) {
317 proc_dbg_info_src
->ip_to_debug_info_src
,
318 key
, debug_info_src
);
319 /* Ownership passed to ht. */
327 return debug_info_src
;
331 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
332 int64_t vpid
, uint64_t ip
)
334 struct debug_info_source
*dbg_info_src
= NULL
;
335 struct proc_debug_info_sources
*proc_dbg_info_src
;
337 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
338 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
339 if (!proc_dbg_info_src
) {
343 dbg_info_src
= proc_debug_info_sources_get_entry(
344 proc_dbg_info_src
, ip
);
354 struct debug_info
*debug_info_create(void)
357 struct debug_info
*debug_info
;
359 debug_info
= g_new0(struct debug_info
, 1);
364 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
365 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
366 (GDestroyNotify
) proc_debug_info_sources_destroy
);
367 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
371 ret
= debug_info_init(debug_info
);
384 void debug_info_destroy(struct debug_info
*debug_info
)
390 if (debug_info
->vpid_to_proc_dbg_info_src
) {
391 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
400 void handle_statedump_build_id_event(struct debug_info
*debug_info
,
401 struct ctf_event_definition
*event_def
)
403 struct proc_debug_info_sources
*proc_dbg_info_src
;
404 struct bt_definition
*event_fields_def
= NULL
;
405 struct bt_definition
*sec_def
= NULL
;
406 struct bt_definition
*baddr_def
= NULL
;
407 struct bt_definition
*vpid_def
= NULL
;
408 struct bt_definition
*build_id_def
= NULL
;
409 struct definition_sequence
*build_id_seq
;
410 struct so_info
*so
= NULL
;
414 uint8_t *build_id
= NULL
;
415 uint64_t build_id_len
;
417 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
418 sec_def
= (struct bt_definition
*)
419 event_def
->stream
->stream_event_context
;
421 if (!event_fields_def
|| !sec_def
) {
425 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
430 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
435 build_id_def
= bt_lookup_definition(event_fields_def
, "_build_id");
440 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
444 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
448 if (build_id_def
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
) {
452 baddr
= bt_get_unsigned_int(baddr_def
);
453 vpid
= bt_get_signed_int(vpid_def
);
454 build_id_seq
= container_of(build_id_def
,
455 struct definition_sequence
, p
);
456 build_id_len
= build_id_seq
->length
->value
._unsigned
;
458 build_id
= g_malloc(build_id_len
);
463 for (i
= 0; i
< build_id_len
; ++i
) {
464 struct bt_definition
**field
;
466 field
= (struct bt_definition
**) &g_ptr_array_index(
467 build_id_seq
->elems
, i
);
468 build_id
[i
] = bt_get_unsigned_int(*field
);
471 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
472 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
473 if (!proc_dbg_info_src
) {
477 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
481 * The build_id event comes after the so has been
482 * created. If it isn't found, just ignore this event.
487 so_info_set_build_id(so
, build_id
, build_id_len
);
495 void handle_statedump_debug_link_event(struct debug_info
*debug_info
,
496 struct ctf_event_definition
*event_def
)
498 struct proc_debug_info_sources
*proc_dbg_info_src
;
499 struct bt_definition
*event_fields_def
= NULL
;
500 struct bt_definition
*sec_def
= NULL
;
501 struct bt_definition
*baddr_def
= NULL
;
502 struct bt_definition
*vpid_def
= NULL
;
503 struct bt_definition
*filename_def
= NULL
;
504 struct bt_definition
*crc32_def
= NULL
;
505 struct so_info
*so
= NULL
;
508 char *filename
= NULL
;
511 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
512 sec_def
= (struct bt_definition
*)
513 event_def
->stream
->stream_event_context
;
515 if (!event_fields_def
|| !sec_def
) {
519 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
524 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
529 filename_def
= bt_lookup_definition(event_fields_def
, "_filename");
534 crc32_def
= bt_lookup_definition(event_fields_def
, "_crc32");
539 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
543 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
547 if (filename_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
551 if (crc32_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
555 baddr
= bt_get_unsigned_int(baddr_def
);
556 vpid
= bt_get_signed_int(vpid_def
);
558 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
559 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
560 if (!proc_dbg_info_src
) {
564 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
568 * The debug_link event comes after the so has been
569 * created. If it isn't found, just ignore this event.
574 filename
= bt_get_string(filename_def
);
575 crc32
= bt_get_unsigned_int(crc32_def
);
577 so_info_set_debug_link(so
, filename
, crc32
);
584 void handle_statedump_soinfo_event(struct debug_info
*debug_info
,
585 struct ctf_event_definition
*event_def
)
587 struct bt_definition
*baddr_def
= NULL
;
588 struct bt_definition
*memsz_def
= NULL
;
589 struct bt_definition
*sopath_def
= NULL
;
590 struct bt_definition
*vpid_def
= NULL
;
591 struct bt_definition
*event_fields_def
= NULL
;
592 struct bt_definition
*sec_def
= NULL
;
593 struct proc_debug_info_sources
*proc_dbg_info_src
;
595 uint64_t baddr
, memsz
;
600 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
601 sec_def
= (struct bt_definition
*)
602 event_def
->stream
->stream_event_context
;
604 if (!event_fields_def
|| !sec_def
) {
608 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
613 memsz_def
= bt_lookup_definition(event_fields_def
, "_memsz");
618 sopath_def
= bt_lookup_definition(event_fields_def
, "_sopath");
623 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
628 if (baddr_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
632 if (memsz_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
636 if (sopath_def
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
) {
640 if (vpid_def
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
644 baddr
= bt_get_unsigned_int(baddr_def
);
645 memsz
= bt_get_unsigned_int(memsz_def
);
646 sopath
= bt_get_string(sopath_def
);
647 vpid
= bt_get_signed_int(vpid_def
);
658 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
659 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
660 if (!proc_dbg_info_src
) {
664 key
= g_new0(uint64_t, 1);
669 *((uint64_t *) key
) = baddr
;
671 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
677 so
= so_info_create(sopath
, baddr
, memsz
);
682 g_hash_table_insert(proc_dbg_info_src
->baddr_to_so_info
,
684 /* Ownership passed to ht. */
693 void handle_statedump_start(struct debug_info
*debug_info
,
694 struct ctf_event_definition
*event_def
)
696 struct bt_definition
*vpid_def
= NULL
;
697 struct bt_definition
*sec_def
= NULL
;
698 struct proc_debug_info_sources
*proc_dbg_info_src
;
701 sec_def
= (struct bt_definition
*)
702 event_def
->stream
->stream_event_context
;
707 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
712 vpid
= bt_get_signed_int(vpid_def
);
714 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
715 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
716 if (!proc_dbg_info_src
) {
720 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_so_info
);
721 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
728 void register_event_debug_infos(struct debug_info
*debug_info
,
729 struct ctf_event_definition
*event
)
731 struct bt_definition
*ip_def
, *vpid_def
;
734 struct bt_definition
*sec_def
;
736 /* Get stream event context definition. */
737 sec_def
= (struct bt_definition
*) event
->stream
->stream_event_context
;
742 /* Get "ip" and "vpid" definitions. */
743 vpid_def
= bt_lookup_definition((struct bt_definition
*) sec_def
,
745 ip_def
= bt_lookup_definition((struct bt_definition
*) sec_def
, "_ip");
747 if (!vpid_def
|| !ip_def
) {
751 vpid
= bt_get_signed_int(vpid_def
);
752 ip
= bt_get_unsigned_int(ip_def
);
754 /* Get debug info for this context. */
755 ((struct definition_integer
*) ip_def
)->debug_info_src
=
756 debug_info_query(debug_info
, vpid
, ip
);
763 void debug_info_handle_event(struct debug_info
*debug_info
,
764 struct ctf_event_definition
*event
)
766 struct ctf_event_declaration
*event_class
;
767 struct ctf_stream_declaration
*stream_class
;
769 if (!debug_info
|| !event
) {
773 stream_class
= event
->stream
->stream_class
;
774 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
775 event
->stream
->event_id
);
777 if (event_class
->name
== debug_info
->q_statedump_soinfo
||
778 event_class
->name
== debug_info
->q_dl_open
) {
779 /* State dump/dlopen() */
780 handle_statedump_soinfo_event(debug_info
, event
);
781 } else if (event_class
->name
== debug_info
->q_statedump_start
) {
782 /* Start state dump */
783 handle_statedump_start(debug_info
, event
);
784 } else if (event_class
->name
== debug_info
->q_statedump_debug_link
) {
785 /* Debug link info */
786 handle_statedump_debug_link_event(debug_info
, event
);
787 } else if (event_class
->name
== debug_info
->q_statedump_build_id
) {
789 handle_statedump_build_id_event(debug_info
, event
);
791 /* Other events: register debug infos */
792 register_event_debug_infos(debug_info
, event
);