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 "debug-info.h"
35 struct proc_debug_info_sources
{
37 * Hash table: base address (pointer to uint64_t) to bin info; owned by
38 * proc_debug_info_sources.
40 GHashTable
*baddr_to_bin_info
;
43 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
44 * owned by proc_debug_info_sources.
46 GHashTable
*ip_to_debug_info_src
;
51 * Hash table of VPIDs (pointer to int64_t) to
52 * (struct ctf_proc_debug_infos*); owned by debug_info.
54 GHashTable
*vpid_to_proc_dbg_info_src
;
55 GQuark q_statedump_bin_info
;
56 GQuark q_statedump_debug_link
;
57 GQuark q_statedump_build_id
;
58 GQuark q_statedump_start
;
65 int debug_info_init(struct debug_info
*info
)
67 info
->q_statedump_bin_info
= g_quark_from_string(
68 "lttng_ust_statedump:bin_info");
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");
76 info
->q_lib_load
= g_quark_from_string("lttng_ust_lib:load");
77 info
->q_lib_unload
= g_quark_from_string("lttng_ust_lib:unload");
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(proc_dbg_info_src
, ip
);
323 struct debug_info
*debug_info_create(void)
326 struct debug_info
*debug_info
;
328 debug_info
= g_new0(struct debug_info
, 1);
333 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
334 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
335 (GDestroyNotify
) proc_debug_info_sources_destroy
);
336 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
340 ret
= debug_info_init(debug_info
);
353 void debug_info_destroy(struct debug_info
*debug_info
)
359 if (debug_info
->vpid_to_proc_dbg_info_src
) {
360 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
369 void handle_statedump_build_id_event(FILE *err
, struct debug_info
*debug_info
,
370 struct bt_ctf_event
*event
)
372 struct proc_debug_info_sources
*proc_dbg_info_src
;
373 struct bin_info
*bin
= NULL
;
378 ret
= get_stream_event_context_int_field_value(err
,
379 event
, "_vpid", &vpid
);
381 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
387 ret
= get_payload_unsigned_int_field_value(err
,
388 event
, "_baddr", &baddr
);
390 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
396 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
397 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
398 if (!proc_dbg_info_src
) {
402 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
406 * The build_id event comes after the bin has been
407 * created. If it isn't found, just ignore this event.
412 ret
= get_payload_build_id_field_value(err
, event
, "_build_id",
413 &bin
->build_id
, &bin
->build_id_len
);
415 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
422 * Reset the is_elf_only flag in case it had been set
423 * previously, because we might find separate debug info using
424 * the new build id information.
426 bin
->is_elf_only
= false;
429 // bin_info_set_build_id(bin, build_id, build_id_len);
436 void handle_statedump_debug_link_event(FILE *err
, struct debug_info
*debug_info
,
437 struct bt_ctf_event
*event
)
439 struct proc_debug_info_sources
*proc_dbg_info_src
;
440 struct bin_info
*bin
= NULL
;
443 const char *filename
= NULL
;
448 ret
= get_stream_event_context_int_field_value(err
, event
,
451 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
457 ret
= get_payload_unsigned_int_field_value(err
,
458 event
, "_baddr", &baddr
);
460 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
466 ret
= get_payload_unsigned_int_field_value(err
, event
, "_crc32", &tmp
);
468 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
473 crc32
= (uint32_t) tmp
;
475 ret
= get_payload_string_field_value(err
,
476 event
, "_filename", &filename
);
478 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
484 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
485 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
486 if (!proc_dbg_info_src
) {
490 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
494 * The debug_link event comes after the bin has been
495 * created. If it isn't found, just ignore this event.
500 bin_info_set_debug_link(bin
, filename
, crc32
);
507 void handle_bin_info_event(FILE *err
, struct debug_info
*debug_info
,
508 struct bt_ctf_event
*event
, bool has_pic_field
)
510 struct proc_debug_info_sources
*proc_dbg_info_src
;
511 struct bin_info
*bin
;
512 uint64_t baddr
, memsz
;
519 ret
= get_payload_unsigned_int_field_value(err
,
520 event
, "_baddr", &baddr
);
522 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
528 ret
= get_payload_unsigned_int_field_value(err
,
529 event
, "_memsz", &memsz
);
531 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
537 ret
= get_payload_string_field_value(err
,
538 event
, "_path", &path
);
540 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
549 ret
= get_payload_unsigned_int_field_value(err
,
550 event
, "_is_pic", &tmp
);
552 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
560 * dlopen has no is_pic field, because the shared
561 * object is always PIC.
566 ret
= get_stream_event_context_int_field_value(err
, event
, "_vpid",
569 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
584 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
585 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
586 if (!proc_dbg_info_src
) {
590 key
= g_new0(uint64_t, 1);
595 *((uint64_t *) key
) = baddr
;
597 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
603 bin
= bin_info_create(path
, baddr
, memsz
, is_pic
);
608 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
,
610 /* Ownership passed to ht. */
619 void handle_statedump_bin_info_event(FILE *err
, struct debug_info
*debug_info
,
620 struct bt_ctf_event
*event
)
622 handle_bin_info_event(err
, debug_info
, event
, true);
626 void handle_lib_load_event(FILE *err
, struct debug_info
*debug_info
,
627 struct bt_ctf_event
*event
)
629 handle_bin_info_event(err
, debug_info
, event
, false);
633 void handle_lib_unload_event(FILE *err
, struct debug_info
*debug_info
,
634 struct bt_ctf_event
*event
)
636 struct proc_debug_info_sources
*proc_dbg_info_src
;
639 gpointer key_ptr
= NULL
;
642 ret
= get_payload_unsigned_int_field_value(err
,
643 event
, "_baddr", &baddr
);
645 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
651 ret
= get_stream_event_context_int_field_value(err
, event
, "_vpid",
654 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
660 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
661 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
662 if (!proc_dbg_info_src
) {
666 key_ptr
= (gpointer
) &baddr
;
667 (void) g_hash_table_remove(proc_dbg_info_src
->baddr_to_bin_info
,
674 void handle_statedump_start(FILE *err
, struct debug_info
*debug_info
,
675 struct bt_ctf_event
*event
)
677 struct proc_debug_info_sources
*proc_dbg_info_src
;
681 ret
= get_stream_event_context_int_field_value(err
, event
,
684 fprintf(err
, "[error] %s in %s:%d\n", __func__
,
690 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
691 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
692 if (!proc_dbg_info_src
) {
696 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
697 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
704 void debug_info_handle_event(FILE *err
, struct bt_ctf_event
*event
,
705 struct debug_info
*debug_info
)
707 struct bt_ctf_event_class
*event_class
;
708 const char *event_name
;
711 if (!debug_info
|| !event
) {
714 event_class
= bt_ctf_event_get_class(event
);
718 event_name
= bt_ctf_event_class_get_name(event_class
);
722 q_event_name
= g_quark_try_string(event_name
);
724 if (q_event_name
== debug_info
->q_statedump_bin_info
) {
726 handle_statedump_bin_info_event(err
, debug_info
, event
);
727 } else if (q_event_name
== debug_info
->q_dl_open
||
728 q_event_name
== debug_info
->q_lib_load
) {
730 * dl_open and lib_load events are both checked for since
731 * only dl_open was produced as of lttng-ust 2.8.
733 * lib_load, which is produced from lttng-ust 2.9+, is a lot
734 * more reliable since it will be emitted when other functions
735 * of the dlopen family are called (e.g. dlmopen) and when
736 * library are transitively loaded.
738 handle_lib_load_event(err
, debug_info
, event
);
739 } else if (q_event_name
== debug_info
->q_statedump_start
) {
740 /* Start state dump */
741 handle_statedump_start(err
, debug_info
, event
);
742 } else if (q_event_name
== debug_info
->q_statedump_debug_link
) {
743 /* Debug link info */
744 handle_statedump_debug_link_event(err
, debug_info
, event
);
745 } else if (q_event_name
== debug_info
->q_statedump_build_id
) {
747 handle_statedump_build_id_event(err
, debug_info
, event
);
748 } else if (q_event_name
== debug_info
-> q_lib_unload
) {
749 handle_lib_unload_event(err
, debug_info
, event
);