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
->filename
);
88 g_free(debug_info_src
);
92 * Returns the location of a path's file (the last element of the path).
93 * Returns the original path on error.
96 const char *get_filename_from_path(const char *path
)
98 size_t i
= strlen(path
);
104 if (path
[i
- 1] == '/') {
106 * Path ends with a trailing slash, no filename to return.
107 * Return the original path.
113 if (path
[i
] == '/') {
123 struct debug_info_source
*debug_info_source_create_from_so(struct so_info
*so
,
127 struct debug_info_source
*debug_info_src
= NULL
;
128 struct source_location
*src_loc
= NULL
;
130 debug_info_src
= g_new0(struct debug_info_source
, 1);
132 if (!debug_info_src
) {
136 /* Lookup function name */
137 ret
= so_info_lookup_function_name(so
, ip
, &debug_info_src
->func
);
142 /* Can't retrieve src_loc from ELF only, skip it */
143 if (so
->is_elf_only
) {
147 /* Lookup source location */
148 ret
= so_info_lookup_source_location(so
, ip
, &src_loc
);
154 debug_info_src
->line_no
= src_loc
->line_no
;
156 if (src_loc
->filename
) {
157 debug_info_src
->filename
= strdup(src_loc
->filename
);
158 if (!debug_info_src
->filename
) {
163 * The short version of the filename does not include
164 * the full path, it will only point to the last element
165 * of the path (anything after the last '/').
167 debug_info_src
->short_filename
= get_filename_from_path(
172 source_location_destroy(src_loc
);
176 return debug_info_src
;
179 debug_info_source_destroy(debug_info_src
);
184 void proc_debug_info_sources_destroy(
185 struct proc_debug_info_sources
*proc_dbg_info_src
)
187 if (!proc_dbg_info_src
) {
191 if (proc_dbg_info_src
->baddr_to_so_info
) {
192 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_so_info
);
195 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
196 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
199 g_free(proc_dbg_info_src
);
203 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
205 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
207 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
208 if (!proc_dbg_info_src
) {
212 proc_dbg_info_src
->baddr_to_so_info
= g_hash_table_new_full(
213 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
214 (GDestroyNotify
) so_info_destroy
);
215 if (!proc_dbg_info_src
->baddr_to_so_info
) {
219 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
220 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
221 (GDestroyNotify
) debug_info_source_destroy
);
222 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
227 return proc_dbg_info_src
;
230 proc_debug_info_sources_destroy(proc_dbg_info_src
);
235 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
236 GHashTable
*ht
, int64_t vpid
)
238 gpointer key
= g_new0(int64_t, 1);
239 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
245 *((int64_t *) key
) = vpid
;
247 /* Exists? Return it */
248 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
249 if (proc_dbg_info_src
) {
253 /* Otherwise, create and return it */
254 proc_dbg_info_src
= proc_debug_info_sources_create();
255 if (!proc_dbg_info_src
) {
259 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
260 /* Ownership passed to ht */
264 return proc_dbg_info_src
;
268 struct debug_info_source
*proc_debug_info_sources_get_entry(
269 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
271 struct debug_info_source
*debug_info_src
= NULL
;
272 gpointer key
= g_new0(uint64_t, 1);
274 gpointer baddr
, value
;
280 *((uint64_t *) key
) = ip
;
282 /* Look in IP to debug infos hash table first. */
283 debug_info_src
= g_hash_table_lookup(
284 proc_dbg_info_src
->ip_to_debug_info_src
,
286 if (debug_info_src
) {
290 /* Check in all so_infos. */
291 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_so_info
);
293 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
295 struct so_info
*so
= value
;
297 if (!so_info_has_address(value
, ip
)) {
302 * Found; add it to cache.
304 * FIXME: this should be bounded in size (and implement
305 * a caching policy), and entries should be prunned when
306 * libraries are unmapped.
308 debug_info_src
= debug_info_source_create_from_so(so
, ip
);
309 if (debug_info_src
) {
311 proc_dbg_info_src
->ip_to_debug_info_src
,
312 key
, debug_info_src
);
313 /* Ownership passed to ht. */
321 return debug_info_src
;
325 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
326 int64_t vpid
, uint64_t ip
)
328 struct debug_info_source
*dbg_info_src
= NULL
;
329 struct proc_debug_info_sources
*proc_dbg_info_src
;
331 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
332 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
333 if (!proc_dbg_info_src
) {
337 dbg_info_src
= proc_debug_info_sources_get_entry(
338 proc_dbg_info_src
, ip
);
348 struct debug_info
*debug_info_create(void)
351 struct debug_info
*debug_info
;
353 debug_info
= g_new0(struct debug_info
, 1);
358 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
359 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
360 (GDestroyNotify
) proc_debug_info_sources_destroy
);
361 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
365 ret
= debug_info_init(debug_info
);
378 void debug_info_destroy(struct debug_info
*debug_info
)
384 if (debug_info
->vpid_to_proc_dbg_info_src
) {
385 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
394 void handle_statedump_build_id_event(struct debug_info
*debug_info
,
395 struct ctf_event_definition
*event_def
)
397 struct proc_debug_info_sources
*proc_dbg_info_src
;
398 struct bt_definition
*event_fields_def
= NULL
;
399 struct bt_definition
*sec_def
= NULL
;
400 struct bt_definition
*baddr_def
= NULL
;
401 struct bt_definition
*vpid_def
= NULL
;
402 struct bt_definition
*build_id_def
= NULL
;
403 struct definition_sequence
*build_id_seq
;
404 struct so_info
*so
= NULL
;
408 uint8_t *build_id
= NULL
;
409 uint64_t build_id_len
;
411 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
412 sec_def
= (struct bt_definition
*)
413 event_def
->stream
->stream_event_context
;
415 if (!event_fields_def
|| !sec_def
) {
419 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
424 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
429 build_id_def
= bt_lookup_definition(event_fields_def
, "_build_id");
434 if (baddr_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
438 if (vpid_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
442 if (build_id_def
->declaration
->id
!= CTF_TYPE_SEQUENCE
) {
446 baddr
= bt_get_unsigned_int(baddr_def
);
447 vpid
= bt_get_signed_int(vpid_def
);
448 build_id_seq
= container_of(build_id_def
,
449 struct definition_sequence
, p
);
450 build_id_len
= build_id_seq
->length
->value
._unsigned
;
452 build_id
= g_malloc(build_id_len
);
457 for (i
= 0; i
< build_id_len
; ++i
) {
458 struct bt_definition
**field
;
460 field
= (struct bt_definition
**) &g_ptr_array_index(
461 build_id_seq
->elems
, i
);
462 build_id
[i
] = bt_get_unsigned_int(*field
);
465 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
466 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
467 if (!proc_dbg_info_src
) {
471 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
475 * The build_id event comes after the so has been
476 * created. If it isn't found, just ignore this event.
481 so_info_set_build_id(so
, build_id
, build_id_len
);
489 void handle_statedump_debug_link_event(struct debug_info
*debug_info
,
490 struct ctf_event_definition
*event_def
)
492 struct proc_debug_info_sources
*proc_dbg_info_src
;
493 struct bt_definition
*event_fields_def
= NULL
;
494 struct bt_definition
*sec_def
= NULL
;
495 struct bt_definition
*baddr_def
= NULL
;
496 struct bt_definition
*vpid_def
= NULL
;
497 struct bt_definition
*filename_def
= NULL
;
498 struct bt_definition
*crc32_def
= NULL
;
499 struct so_info
*so
= NULL
;
502 char *filename
= NULL
;
505 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
506 sec_def
= (struct bt_definition
*)
507 event_def
->stream
->stream_event_context
;
509 if (!event_fields_def
|| !sec_def
) {
513 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
518 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
523 filename_def
= bt_lookup_definition(event_fields_def
, "_filename");
528 crc32_def
= bt_lookup_definition(event_fields_def
, "_crc32");
533 if (baddr_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
537 if (vpid_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
541 if (filename_def
->declaration
->id
!= CTF_TYPE_STRING
) {
545 if (crc32_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
549 baddr
= bt_get_unsigned_int(baddr_def
);
550 vpid
= bt_get_signed_int(vpid_def
);
552 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
553 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
554 if (!proc_dbg_info_src
) {
558 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
562 * The debug_link event comes after the so has been
563 * created. If it isn't found, just ignore this event.
568 filename
= bt_get_string(filename_def
);
569 crc32
= bt_get_unsigned_int(crc32_def
);
571 so_info_set_debug_link(so
, filename
, crc32
);
578 void handle_statedump_soinfo_event(struct debug_info
*debug_info
,
579 struct ctf_event_definition
*event_def
)
581 struct bt_definition
*baddr_def
= NULL
;
582 struct bt_definition
*memsz_def
= NULL
;
583 struct bt_definition
*sopath_def
= NULL
;
584 struct bt_definition
*vpid_def
= NULL
;
585 struct bt_definition
*event_fields_def
= NULL
;
586 struct bt_definition
*sec_def
= NULL
;
587 struct proc_debug_info_sources
*proc_dbg_info_src
;
589 uint64_t baddr
, memsz
;
594 event_fields_def
= (struct bt_definition
*) event_def
->event_fields
;
595 sec_def
= (struct bt_definition
*)
596 event_def
->stream
->stream_event_context
;
598 if (!event_fields_def
|| !sec_def
) {
602 baddr_def
= bt_lookup_definition(event_fields_def
, "_baddr");
607 memsz_def
= bt_lookup_definition(event_fields_def
, "_memsz");
612 sopath_def
= bt_lookup_definition(event_fields_def
, "_sopath");
617 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
622 if (baddr_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
626 if (memsz_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
630 if (sopath_def
->declaration
->id
!= CTF_TYPE_STRING
) {
634 if (vpid_def
->declaration
->id
!= CTF_TYPE_INTEGER
) {
638 baddr
= bt_get_unsigned_int(baddr_def
);
639 memsz
= bt_get_unsigned_int(memsz_def
);
640 sopath
= bt_get_string(sopath_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 so
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_so_info
,
671 so
= so_info_create(sopath
, baddr
, memsz
);
676 g_hash_table_insert(proc_dbg_info_src
->baddr_to_so_info
,
678 /* Ownership passed to ht. */
687 void handle_statedump_start(struct debug_info
*debug_info
,
688 struct ctf_event_definition
*event_def
)
690 struct bt_definition
*vpid_def
= NULL
;
691 struct bt_definition
*sec_def
= NULL
;
692 struct proc_debug_info_sources
*proc_dbg_info_src
;
695 sec_def
= (struct bt_definition
*)
696 event_def
->stream
->stream_event_context
;
701 vpid_def
= bt_lookup_definition(sec_def
, "_vpid");
706 vpid
= bt_get_signed_int(vpid_def
);
708 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
709 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
710 if (!proc_dbg_info_src
) {
714 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_so_info
);
715 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
722 void register_event_debug_infos(struct debug_info
*debug_info
,
723 struct ctf_event_definition
*event
)
725 struct bt_definition
*ip_def
, *vpid_def
;
728 struct bt_definition
*sec_def
;
730 /* Get stream event context definition. */
731 sec_def
= (struct bt_definition
*) event
->stream
->stream_event_context
;
736 /* Get "ip" and "vpid" definitions. */
737 vpid_def
= bt_lookup_definition((struct bt_definition
*) sec_def
,
739 ip_def
= bt_lookup_definition((struct bt_definition
*) sec_def
, "_ip");
741 if (!vpid_def
|| !ip_def
) {
745 vpid
= bt_get_signed_int(vpid_def
);
746 ip
= bt_get_unsigned_int(ip_def
);
748 /* Get debug info for this context. */
749 ((struct definition_integer
*) ip_def
)->debug_info_src
=
750 debug_info_query(debug_info
, vpid
, ip
);
757 void debug_info_handle_event(struct debug_info
*debug_info
,
758 struct ctf_event_definition
*event
)
760 struct ctf_event_declaration
*event_class
;
761 struct ctf_stream_declaration
*stream_class
;
763 if (!debug_info
|| !event
) {
767 stream_class
= event
->stream
->stream_class
;
768 event_class
= g_ptr_array_index(stream_class
->events_by_id
,
769 event
->stream
->event_id
);
771 if (event_class
->name
== debug_info
->q_statedump_soinfo
||
772 event_class
->name
== debug_info
->q_dl_open
) {
773 /* State dump/dlopen() */
774 handle_statedump_soinfo_event(debug_info
, event
);
775 } else if (event_class
->name
== debug_info
->q_statedump_start
) {
776 /* Start state dump */
777 handle_statedump_start(debug_info
, event
);
778 } else if (event_class
->name
== debug_info
->q_statedump_debug_link
) {
779 /* Debug link info */
780 handle_statedump_debug_link_event(debug_info
, event
);
781 } else if (event_class
->name
== debug_info
->q_statedump_build_id
) {
783 handle_statedump_build_id_event(debug_info
, event
);
785 /* Other events: register debug infos */
786 register_event_debug_infos(debug_info
, event
);