2 * SPDX-License-Identifier: MIT
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>
8 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
10 * Babeltrace - Debug Information State Tracker
13 #define BT_COMP_LOG_SELF_COMP self_comp
14 #define BT_LOG_OUTPUT_LEVEL log_level
15 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO"
16 #include "logging/comp-logging.h"
22 #include "common/assert.h"
23 #include "common/common.h"
24 #include "fd-cache/fd-cache.h"
27 #include "debug-info.h"
28 #include "trace-ir-data-copy.h"
29 #include "trace-ir-mapping.h"
30 #include "trace-ir-metadata-copy.h"
32 #include "plugins/common/param-validation/param-validation.h"
34 #define DEFAULT_DEBUG_INFO_FIELD_NAME "debug_info"
35 #define LTTNG_UST_STATEDUMP_PREFIX "lttng_ust"
36 #define VPID_FIELD_NAME "vpid"
37 #define IP_FIELD_NAME "ip"
38 #define BADDR_FIELD_NAME "baddr"
39 #define CRC32_FIELD_NAME "crc"
40 #define BUILD_ID_FIELD_NAME "build_id"
41 #define FILENAME_FIELD_NAME "filename"
42 #define IS_PIC_FIELD_NAME "is_pic"
43 #define MEMSZ_FIELD_NAME "memsz"
44 #define PATH_FIELD_NAME "path"
46 struct debug_info_component
{
47 bt_logging_level log_level
;
48 bt_self_component
*self_comp
;
49 bt_self_component_filter
*self_comp_filter
;
51 gchar
*arg_debug_info_field_name
;
52 gchar
*arg_target_prefix
;
53 bt_bool arg_full_path
;
56 struct debug_info_msg_iter
{
57 bt_logging_level log_level
;
58 struct debug_info_component
*debug_info_component
;
59 bt_self_message_iterator
*input_iterator
;
60 bt_self_component
*self_comp
;
61 bt_message_iterator
*msg_iter
;
63 struct trace_ir_maps
*ir_maps
;
64 /* in_trace -> debug_info_mapping. */
65 GHashTable
*debug_info_map
;
67 struct bt_fd_cache fd_cache
;
70 struct debug_info_source
{
71 /* Strings are owned by debug_info_source. */
74 * Store the line number as a string so that the allocation and
75 * conversion to string is only done once.
79 /* short_src_path points inside src_path, no need to free. */
80 const gchar
*short_src_path
;
82 /* short_bin_path points inside bin_path, no need to free. */
83 const gchar
*short_bin_path
;
85 * Location within the binary. Either absolute (@0x1234) or
91 struct proc_debug_info_sources
{
93 * Hash table: base address (pointer to uint64_t) to bin info; owned by
94 * proc_debug_info_sources.
96 GHashTable
*baddr_to_bin_info
;
99 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
100 * owned by proc_debug_info_sources.
102 GHashTable
*ip_to_debug_info_src
;
106 bt_logging_level log_level
;
107 bt_self_component
*self_comp
;
108 struct debug_info_component
*comp
;
109 const bt_trace
*input_trace
;
110 bt_listener_id destruction_listener_id
;
113 * Hash table of VPIDs (pointer to int64_t) to
114 * (struct proc_debug_info_sources*); owned by debug_info.
116 GHashTable
*vpid_to_proc_dbg_info_src
;
117 GQuark q_statedump_bin_info
;
118 GQuark q_statedump_debug_link
;
119 GQuark q_statedump_build_id
;
120 GQuark q_statedump_start
;
124 struct bt_fd_cache
*fd_cache
; /* Weak ref. Owned by the iterator. */
128 int debug_info_init(struct debug_info
*info
)
130 info
->q_statedump_bin_info
= g_quark_from_string(
131 "lttng_ust_statedump:bin_info");
132 info
->q_statedump_debug_link
= g_quark_from_string(
133 "lttng_ust_statedump:debug_link");
134 info
->q_statedump_build_id
= g_quark_from_string(
135 "lttng_ust_statedump:build_id");
136 info
->q_statedump_start
= g_quark_from_string(
137 "lttng_ust_statedump:start");
138 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
139 info
->q_lib_load
= g_quark_from_string("lttng_ust_lib:load");
140 info
->q_lib_unload
= g_quark_from_string("lttng_ust_lib:unload");
142 return bin_info_init(info
->log_level
, info
->self_comp
);
146 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
148 if (!debug_info_src
) {
152 g_free(debug_info_src
->func
);
153 g_free(debug_info_src
->line_no
);
154 g_free(debug_info_src
->src_path
);
155 g_free(debug_info_src
->bin_path
);
156 g_free(debug_info_src
->bin_loc
);
157 g_free(debug_info_src
);
161 struct debug_info_source
*debug_info_source_create_from_bin(
162 struct bin_info
*bin
, uint64_t ip
,
163 bt_self_component
*self_comp
)
166 struct debug_info_source
*debug_info_src
= NULL
;
167 struct source_location
*src_loc
= NULL
;
168 bt_logging_level log_level
;
172 log_level
= bin
->log_level
;
174 debug_info_src
= g_new0(struct debug_info_source
, 1);
176 if (!debug_info_src
) {
180 /* Lookup function name */
181 ret
= bin_info_lookup_function_name(bin
, ip
, &debug_info_src
->func
);
186 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
187 if (!bin
->is_elf_only
|| !debug_info_src
->func
) {
188 /* Lookup source location */
189 ret
= bin_info_lookup_source_location(bin
, ip
, &src_loc
);
191 BT_COMP_LOGI("Failed to lookup source location: ret=%d", ret
);
196 debug_info_src
->line_no
=
197 g_strdup_printf("%"PRId64
, src_loc
->line_no
);
198 if (!debug_info_src
->line_no
) {
199 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
200 "Error occurred when setting `line_no` field.");
204 if (src_loc
->filename
) {
205 debug_info_src
->src_path
= g_strdup(src_loc
->filename
);
206 if (!debug_info_src
->src_path
) {
210 debug_info_src
->short_src_path
= get_filename_from_path(
211 debug_info_src
->src_path
);
213 source_location_destroy(src_loc
);
217 debug_info_src
->bin_path
= g_strdup(bin
->elf_path
);
218 if (!debug_info_src
->bin_path
) {
222 debug_info_src
->short_bin_path
= get_filename_from_path(
223 debug_info_src
->bin_path
);
225 ret
= bin_info_get_bin_loc(bin
, ip
, &(debug_info_src
->bin_loc
));
232 return debug_info_src
;
235 debug_info_source_destroy(debug_info_src
);
240 void proc_debug_info_sources_destroy(
241 struct proc_debug_info_sources
*proc_dbg_info_src
)
243 if (!proc_dbg_info_src
) {
247 if (proc_dbg_info_src
->baddr_to_bin_info
) {
248 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_bin_info
);
251 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
252 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
255 g_free(proc_dbg_info_src
);
259 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
261 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
263 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
264 if (!proc_dbg_info_src
) {
268 proc_dbg_info_src
->baddr_to_bin_info
= g_hash_table_new_full(
269 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
270 (GDestroyNotify
) bin_info_destroy
);
271 if (!proc_dbg_info_src
->baddr_to_bin_info
) {
275 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
276 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
277 (GDestroyNotify
) debug_info_source_destroy
);
278 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
283 return proc_dbg_info_src
;
286 proc_debug_info_sources_destroy(proc_dbg_info_src
);
291 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
292 GHashTable
*ht
, int64_t vpid
)
294 gpointer key
= g_new0(int64_t, 1);
295 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
301 *((int64_t *) key
) = vpid
;
303 /* Exists? Return it */
304 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
305 if (proc_dbg_info_src
) {
309 /* Otherwise, create and return it */
310 proc_dbg_info_src
= proc_debug_info_sources_create();
311 if (!proc_dbg_info_src
) {
315 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
316 /* Ownership passed to ht */
320 return proc_dbg_info_src
;
324 const bt_field
*event_borrow_payload_field(const bt_event
*event
,
325 const char *field_name
)
327 const bt_field
*event_payload
, *field
;
329 event_payload
= bt_event_borrow_payload_field_const(event
);
330 BT_ASSERT_DBG(event_payload
);
332 field
= bt_field_structure_borrow_member_field_by_name_const(
333 event_payload
, field_name
);
338 const bt_field
*event_borrow_common_context_field(const bt_event
*event
,
339 const char *field_name
)
341 const bt_field
*event_common_ctx
, *field
= NULL
;
343 event_common_ctx
= bt_event_borrow_common_context_field_const(event
);
344 if (!event_common_ctx
) {
348 field
= bt_field_structure_borrow_member_field_by_name_const(
349 event_common_ctx
, field_name
);
356 void event_get_common_context_signed_integer_field_value(
357 const bt_event
*event
, const char *field_name
, int64_t *value
)
359 *value
= bt_field_integer_signed_get_value(
360 event_borrow_common_context_field(event
, field_name
));
364 void event_get_payload_build_id_length(const bt_event
*event
,
365 const char *field_name
, uint64_t *build_id_len
)
367 const bt_field
*build_id_field
;
368 const bt_field_class
*build_id_field_class
;
370 build_id_field
= event_borrow_payload_field(event
, field_name
);
371 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
373 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
374 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
||
375 bt_field_class_get_type(build_id_field_class
) ==
376 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
);
377 BT_ASSERT(bt_field_class_get_type(
378 bt_field_class_array_borrow_element_field_class_const(
379 build_id_field_class
)) ==
380 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
382 *build_id_len
= bt_field_array_get_length(build_id_field
);
386 void event_get_payload_build_id_value(const bt_event
*event
,
387 const char *field_name
, uint8_t *build_id
)
389 const bt_field
*curr_field
, *build_id_field
;
390 const bt_field_class
*build_id_field_class
;
391 uint64_t i
, build_id_len
;
393 build_id_field
= event_borrow_payload_field(event
, field_name
);
394 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
396 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
397 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
||
398 bt_field_class_get_type(build_id_field_class
) ==
399 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
);
400 BT_ASSERT(bt_field_class_get_type(
401 bt_field_class_array_borrow_element_field_class_const(
402 build_id_field_class
)) ==
403 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
405 build_id_len
= bt_field_array_get_length(build_id_field
);
407 for (i
= 0; i
< build_id_len
; i
++) {
408 curr_field
= bt_field_array_borrow_element_field_by_index_const(
411 build_id
[i
] = bt_field_integer_unsigned_get_value(curr_field
);
416 void event_get_payload_unsigned_integer_field_value(const bt_event
*event
,
417 const char *field_name
, uint64_t *value
)
419 *value
= bt_field_integer_unsigned_get_value(
420 event_borrow_payload_field(event
, field_name
));
424 void event_get_payload_string_field_value(const bt_event
*event
,
425 const char *field_name
, const char **value
)
427 *value
= bt_field_string_get_value(
428 event_borrow_payload_field(event
, field_name
));
432 bool event_has_payload_field(const bt_event
*event
,
433 const char *field_name
)
435 return event_borrow_payload_field(event
, field_name
);
439 struct debug_info_source
*proc_debug_info_sources_get_entry(
440 struct debug_info
*debug_info
,
441 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
443 struct debug_info_source
*debug_info_src
= NULL
;
444 gpointer key
= g_new0(uint64_t, 1);
446 gpointer baddr
, value
;
452 *((uint64_t *) key
) = ip
;
454 /* Look in IP to debug infos hash table first. */
455 debug_info_src
= g_hash_table_lookup(
456 proc_dbg_info_src
->ip_to_debug_info_src
, key
);
457 if (debug_info_src
) {
461 /* Check in all bin_infos. */
462 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_bin_info
);
464 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
466 struct bin_info
*bin
= value
;
468 if (!bin_info_has_address(value
, ip
)) {
473 * Found; add it to cache.
475 * FIXME: this should be bounded in size (and implement
476 * a caching policy), and entries should be prunned when
477 * libraries are unmapped.
479 debug_info_src
= debug_info_source_create_from_bin(bin
, ip
,
480 debug_info
->self_comp
);
481 if (debug_info_src
) {
483 proc_dbg_info_src
->ip_to_debug_info_src
, key
,
485 /* Ownership passed to ht. */
493 return debug_info_src
;
497 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
498 int64_t vpid
, uint64_t ip
)
500 struct debug_info_source
*dbg_info_src
= NULL
;
501 struct proc_debug_info_sources
*proc_dbg_info_src
;
503 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
504 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
505 if (!proc_dbg_info_src
) {
509 dbg_info_src
= proc_debug_info_sources_get_entry(debug_info
,
510 proc_dbg_info_src
, ip
);
517 struct debug_info
*debug_info_create(struct debug_info_component
*comp
,
518 const bt_trace
*trace
, struct bt_fd_cache
*fdc
)
521 struct debug_info
*debug_info
;
527 debug_info
= g_new0(struct debug_info
, 1);
532 debug_info
->log_level
= comp
->log_level
;
533 debug_info
->self_comp
= comp
->self_comp
;
534 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
535 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
536 (GDestroyNotify
) proc_debug_info_sources_destroy
);
537 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
541 debug_info
->comp
= comp
;
542 ret
= debug_info_init(debug_info
);
547 debug_info
->input_trace
= trace
;
548 debug_info
->fd_cache
= fdc
;
558 void debug_info_destroy(struct debug_info
*debug_info
)
560 bt_logging_level log_level
;
561 bt_self_component
*self_comp
;
562 bt_trace_remove_listener_status remove_listener_status
;
567 log_level
= debug_info
->log_level
;
568 self_comp
= debug_info
->self_comp
;
570 if (debug_info
->vpid_to_proc_dbg_info_src
) {
571 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
574 remove_listener_status
= bt_trace_remove_destruction_listener(
575 debug_info
->input_trace
,
576 debug_info
->destruction_listener_id
);
577 if (remove_listener_status
!= BT_TRACE_REMOVE_LISTENER_STATUS_OK
) {
578 BT_COMP_LOGE("Trace destruction listener removal failed.");
579 bt_current_thread_clear_error();
588 void handle_event_statedump_build_id(struct debug_info
*debug_info
,
589 const bt_event
*event
)
591 struct proc_debug_info_sources
*proc_dbg_info_src
;
592 uint64_t build_id_len
, baddr
;
593 uint8_t *build_id
= NULL
;
594 struct bin_info
*bin
;
598 event_get_common_context_signed_integer_field_value(event
,
599 VPID_FIELD_NAME
, &vpid
);
600 event_get_payload_unsigned_integer_field_value(event
,
601 BADDR_FIELD_NAME
, &baddr
);
603 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
604 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
605 if (!proc_dbg_info_src
) {
609 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
613 * The build_id event comes after the bin has been
614 * created. If it isn't found, just ignore this event.
619 event_get_payload_build_id_length(event
, BUILD_ID_FIELD_NAME
,
622 build_id
= g_new0(uint8_t, build_id_len
);
627 event_get_payload_build_id_value(event
, BUILD_ID_FIELD_NAME
, build_id
);
629 ret
= bin_info_set_build_id(bin
, build_id
, build_id_len
);
635 * Reset the is_elf_only flag in case it had been set
636 * previously, because we might find separate debug info using
637 * the new build id information.
639 bin
->is_elf_only
= false;
647 void handle_event_statedump_debug_link(struct debug_info
*debug_info
,
648 const bt_event
*event
)
650 struct proc_debug_info_sources
*proc_dbg_info_src
;
651 struct bin_info
*bin
= NULL
;
654 const char *filename
= NULL
;
656 uint64_t crc_field_value
;
658 event_get_common_context_signed_integer_field_value(event
,
659 VPID_FIELD_NAME
, &vpid
);
661 event_get_payload_unsigned_integer_field_value(event
,
662 BADDR_FIELD_NAME
, &baddr
);
664 event_get_payload_unsigned_integer_field_value(event
,
665 CRC32_FIELD_NAME
, &crc_field_value
);
667 crc32
= (uint32_t) crc_field_value
;
669 event_get_payload_string_field_value(event
,
670 FILENAME_FIELD_NAME
, &filename
);
672 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
673 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
674 if (!proc_dbg_info_src
) {
678 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
682 * The debug_link event comes after the bin has been
683 * created. If it isn't found, just ignore this event.
688 bin_info_set_debug_link(bin
, filename
, crc32
);
695 void handle_bin_info_event(struct debug_info
*debug_info
,
696 const bt_event
*event
, bool has_pic_field
)
698 struct proc_debug_info_sources
*proc_dbg_info_src
;
699 struct bin_info
*bin
;
700 uint64_t baddr
, memsz
;
706 event_get_payload_unsigned_integer_field_value(event
,
707 MEMSZ_FIELD_NAME
, &memsz
);
713 event_get_payload_unsigned_integer_field_value(event
,
714 BADDR_FIELD_NAME
, &baddr
);
717 * This field is not produced by the dlopen event emitted before
720 if (!event_has_payload_field(event
, PATH_FIELD_NAME
)) {
723 event_get_payload_string_field_value(event
, PATH_FIELD_NAME
, &path
);
726 uint64_t is_pic_field_value
;
728 event_get_payload_unsigned_integer_field_value(event
,
729 IS_PIC_FIELD_NAME
, &is_pic_field_value
);
730 is_pic
= is_pic_field_value
== 1;
733 * dlopen has no is_pic field, because the shared
734 * object is always PIC.
739 event_get_common_context_signed_integer_field_value(event
,
740 VPID_FIELD_NAME
, &vpid
);
742 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
743 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
744 if (!proc_dbg_info_src
) {
748 key
= g_new0(uint64_t, 1);
753 *((uint64_t *) key
) = baddr
;
755 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
, key
);
760 bin
= bin_info_create(debug_info
->fd_cache
, path
, baddr
, memsz
, is_pic
,
761 debug_info
->comp
->arg_debug_dir
,
762 debug_info
->comp
->arg_target_prefix
,
763 debug_info
->log_level
, debug_info
->self_comp
);
768 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
, key
, bin
);
769 /* Ownership passed to ht. */
778 void handle_event_statedump_bin_info(struct debug_info
*debug_info
,
779 const bt_event
*event
)
781 handle_bin_info_event(debug_info
, event
, true);
785 void handle_event_lib_load(struct debug_info
*debug_info
,
786 const bt_event
*event
)
788 handle_bin_info_event(debug_info
, event
, false);
792 void handle_event_lib_unload(struct debug_info
*debug_info
,
793 const bt_event
*event
)
796 struct proc_debug_info_sources
*proc_dbg_info_src
;
800 event_get_payload_unsigned_integer_field_value(event
, BADDR_FIELD_NAME
,
803 event_get_common_context_signed_integer_field_value(event
,
804 VPID_FIELD_NAME
, &vpid
);
806 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
807 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
808 if (!proc_dbg_info_src
) {
810 * It's an unload event for a library for which no load event
811 * was previously received.
816 ret
= g_hash_table_remove(proc_dbg_info_src
->baddr_to_bin_info
,
824 void handle_event_statedump_start(struct debug_info
*debug_info
,
825 const bt_event
*event
)
827 struct proc_debug_info_sources
*proc_dbg_info_src
;
830 event_get_common_context_signed_integer_field_value(
831 event
, VPID_FIELD_NAME
, &vpid
);
833 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
834 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
835 if (!proc_dbg_info_src
) {
839 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
840 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
847 void trace_debug_info_remove_func(const bt_trace
*in_trace
, void *data
)
849 struct debug_info_msg_iter
*debug_it
= data
;
850 if (debug_it
->debug_info_map
) {
852 ret
= g_hash_table_remove(debug_it
->debug_info_map
,
853 (gpointer
) in_trace
);
859 void handle_event_statedump(struct debug_info_msg_iter
*debug_it
,
860 const bt_event
*event
)
862 const bt_event_class
*event_class
;
863 const char *event_name
;
865 const bt_trace
*trace
;
866 struct debug_info
*debug_info
;
871 event_class
= bt_event_borrow_class_const(event
);
873 event_name
= bt_event_class_get_name(event_class
);
875 trace
= bt_stream_borrow_trace_const(
876 bt_event_borrow_stream_const(event
));
878 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
, trace
);
880 bt_trace_add_listener_status add_listener_status
;
882 debug_info
= debug_info_create(debug_it
->debug_info_component
,
883 trace
, &debug_it
->fd_cache
);
884 g_hash_table_insert(debug_it
->debug_info_map
, (gpointer
) trace
,
886 add_listener_status
= bt_trace_add_destruction_listener(
887 trace
, trace_debug_info_remove_func
, debug_it
,
888 &debug_info
->destruction_listener_id
);
889 BT_ASSERT(add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
892 q_event_name
= g_quark_try_string(event_name
);
894 if (q_event_name
== debug_info
->q_statedump_bin_info
) {
896 handle_event_statedump_bin_info(debug_info
, event
);
897 } else if (q_event_name
== debug_info
->q_dl_open
||
898 q_event_name
== debug_info
->q_lib_load
) {
900 * dl_open and lib_load events are both checked for since
901 * only dl_open was produced as of lttng-ust 2.8.
903 * lib_load, which is produced from lttng-ust 2.9+, is a lot
904 * more reliable since it will be emitted when other functions
905 * of the dlopen family are called (e.g. dlmopen) and when
906 * library are transitively loaded.
908 handle_event_lib_load(debug_info
, event
);
909 } else if (q_event_name
== debug_info
->q_statedump_start
) {
910 /* Start state dump */
911 handle_event_statedump_start(debug_info
, event
);
912 } else if (q_event_name
== debug_info
->q_statedump_debug_link
) {
913 /* Debug link info */
914 handle_event_statedump_debug_link(debug_info
, event
);
915 } else if (q_event_name
== debug_info
->q_statedump_build_id
) {
917 handle_event_statedump_build_id(debug_info
, event
);
918 } else if (q_event_name
== debug_info
-> q_lib_unload
) {
919 handle_event_lib_unload(debug_info
, event
);
926 void destroy_debug_info_comp(struct debug_info_component
*debug_info
)
932 g_free(debug_info
->arg_debug_dir
);
933 g_free(debug_info
->arg_debug_info_field_name
);
934 g_free(debug_info
->arg_target_prefix
);
939 void fill_debug_info_bin_field(struct debug_info_source
*dbg_info_src
,
940 bool full_path
, bt_field
*curr_field
,
941 bt_logging_level log_level
, bt_self_component
*self_comp
)
943 bt_field_string_set_value_status set_status
;
944 bt_field_string_append_status append_status
;
946 BT_ASSERT_DBG(bt_field_get_class_type(curr_field
) ==
947 BT_FIELD_CLASS_TYPE_STRING
);
951 set_status
= bt_field_string_set_value(curr_field
,
952 dbg_info_src
->bin_path
);
954 set_status
= bt_field_string_set_value(curr_field
,
955 dbg_info_src
->short_bin_path
);
957 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
958 BT_COMP_LOGE("Cannot set path component of `bin` "
959 "curr_field field's value: str-fc-addr=%p",
961 bt_current_thread_clear_error();
964 append_status
= bt_field_string_append(curr_field
,
965 dbg_info_src
->bin_loc
);
966 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
967 BT_COMP_LOGE("Cannot set bin location component of `bin` "
968 "curr_field field's value: str-fc-addr=%p",
970 bt_current_thread_clear_error();
973 set_status
= bt_field_string_set_value(curr_field
, "");
974 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
975 BT_COMP_LOGE("Cannot set `bin` curr_field field's value: "
976 "str-fc-addr=%p", curr_field
);
977 bt_current_thread_clear_error();
983 void fill_debug_info_func_field(struct debug_info_source
*dbg_info_src
,
984 bt_field
*curr_field
, bt_logging_level log_level
,
985 bt_self_component
*self_comp
)
987 bt_field_string_set_value_status status
;
989 BT_ASSERT_DBG(bt_field_get_class_type(curr_field
) ==
990 BT_FIELD_CLASS_TYPE_STRING
);
991 if (dbg_info_src
&& dbg_info_src
->func
) {
992 status
= bt_field_string_set_value(curr_field
,
995 status
= bt_field_string_set_value(curr_field
, "");
997 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
998 BT_COMP_LOGE("Cannot set `func` curr_field field's value: "
999 "str-fc-addr=%p", curr_field
);
1000 bt_current_thread_clear_error();
1005 void fill_debug_info_src_field(struct debug_info_source
*dbg_info_src
,
1006 bool full_path
, bt_field
*curr_field
,
1007 bt_logging_level log_level
,
1008 bt_self_component
*self_comp
)
1010 bt_field_string_set_value_status set_status
;
1011 bt_field_string_append_status append_status
;
1013 BT_ASSERT_DBG(bt_field_get_class_type(curr_field
) ==
1014 BT_FIELD_CLASS_TYPE_STRING
);
1016 if (dbg_info_src
&& dbg_info_src
->src_path
) {
1018 set_status
= bt_field_string_set_value(curr_field
,
1019 dbg_info_src
->src_path
);
1021 set_status
= bt_field_string_set_value(curr_field
,
1022 dbg_info_src
->short_src_path
);
1024 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1025 BT_COMP_LOGE("Cannot set path component of `src` "
1026 "curr_field field's value: str-fc-addr=%p",
1028 bt_current_thread_clear_error();
1031 append_status
= bt_field_string_append(curr_field
, ":");
1032 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
1033 BT_COMP_LOGE("Cannot set colon component of `src` "
1034 "curr_field field's value: str-fc-addr=%p",
1036 bt_current_thread_clear_error();
1039 append_status
= bt_field_string_append(curr_field
,
1040 dbg_info_src
->line_no
);
1041 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
1042 BT_COMP_LOGE("Cannot set line number component of `src` "
1043 "curr_field field's value: str-fc-addr=%p",
1045 bt_current_thread_clear_error();
1048 set_status
= bt_field_string_set_value(curr_field
, "");
1049 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1050 BT_COMP_LOGE("Cannot set `src` curr_field field's value: "
1051 "str-fc-addr=%p", curr_field
);
1052 bt_current_thread_clear_error();
1058 void fill_debug_info_field_empty(bt_field
*debug_info_field
,
1059 bt_logging_level log_level
, bt_self_component
*self_comp
)
1061 bt_field_string_set_value_status status
;
1062 bt_field
*bin_field
, *func_field
, *src_field
;
1064 BT_ASSERT_DBG(bt_field_get_class_type(debug_info_field
) ==
1065 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1067 bin_field
= bt_field_structure_borrow_member_field_by_name(
1068 debug_info_field
, "bin");
1069 func_field
= bt_field_structure_borrow_member_field_by_name(
1070 debug_info_field
, "func");
1071 src_field
= bt_field_structure_borrow_member_field_by_name(
1072 debug_info_field
, "src");
1074 BT_ASSERT_DBG(bt_field_get_class_type(bin_field
) ==
1075 BT_FIELD_CLASS_TYPE_STRING
);
1076 BT_ASSERT_DBG(bt_field_get_class_type(func_field
) ==
1077 BT_FIELD_CLASS_TYPE_STRING
);
1078 BT_ASSERT_DBG(bt_field_get_class_type(src_field
) ==
1079 BT_FIELD_CLASS_TYPE_STRING
);
1081 status
= bt_field_string_set_value(bin_field
, "");
1082 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1083 BT_COMP_LOGE("Cannot set `bin` field's value: "
1084 "str-fc-addr=%p", bin_field
);
1085 bt_current_thread_clear_error();
1088 status
= bt_field_string_set_value(func_field
, "");
1089 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1090 BT_COMP_LOGE("Cannot set `func` field's value: "
1091 "str-fc-addr=%p", func_field
);
1092 bt_current_thread_clear_error();
1095 status
= bt_field_string_set_value(src_field
, "");
1096 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1097 BT_COMP_LOGE("Cannot set `src` field's value: "
1098 "str-fc-addr=%p", src_field
);
1099 bt_current_thread_clear_error();
1103 void fill_debug_info_field(struct debug_info
*debug_info
, int64_t vpid
,
1104 uint64_t ip
, bt_field
*debug_info_field
)
1106 struct debug_info_source
*dbg_info_src
;
1107 const bt_field_class
*debug_info_fc
;
1109 BT_ASSERT_DBG(bt_field_get_class_type(debug_info_field
) ==
1110 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1112 debug_info_fc
= bt_field_borrow_class_const(debug_info_field
);
1114 BT_ASSERT_DBG(bt_field_class_structure_get_member_count(
1115 debug_info_fc
) == 3);
1117 dbg_info_src
= debug_info_query(debug_info
, vpid
, ip
);
1119 fill_debug_info_bin_field(dbg_info_src
,
1120 debug_info
->comp
->arg_full_path
,
1121 bt_field_structure_borrow_member_field_by_name(
1122 debug_info_field
, "bin"),
1123 debug_info
->log_level
, debug_info
->self_comp
);
1124 fill_debug_info_func_field(dbg_info_src
,
1125 bt_field_structure_borrow_member_field_by_name(
1126 debug_info_field
, "func"),
1127 debug_info
->log_level
, debug_info
->self_comp
);
1128 fill_debug_info_src_field(dbg_info_src
,
1129 debug_info
->comp
->arg_full_path
,
1130 bt_field_structure_borrow_member_field_by_name(
1131 debug_info_field
, "src"),
1132 debug_info
->log_level
, debug_info
->self_comp
);
1136 void fill_debug_info_event_if_needed(struct debug_info_msg_iter
*debug_it
,
1137 const bt_event
*in_event
, bt_event
*out_event
)
1139 bt_field
*out_common_ctx_field
, *out_debug_info_field
;
1140 const bt_field
*vpid_field
, *ip_field
, *in_common_ctx_field
;
1141 const bt_field_class
*in_common_ctx_fc
;
1142 struct debug_info
*debug_info
;
1145 gchar
*debug_info_field_name
=
1146 debug_it
->debug_info_component
->arg_debug_info_field_name
;
1147 bt_logging_level log_level
= debug_it
->log_level
;
1148 bt_self_component
*self_comp
= debug_it
->self_comp
;
1150 in_common_ctx_field
= bt_event_borrow_common_context_field_const(
1152 if (!in_common_ctx_field
) {
1154 * There is no event common context so no need to add debug
1160 in_common_ctx_fc
= bt_field_borrow_class_const(in_common_ctx_field
);
1161 if (!is_event_common_ctx_dbg_info_compatible(in_common_ctx_fc
,
1162 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1164 * The input event common context does not have the necessary
1165 * fields to resolve debug information.
1170 /* Borrow the debug-info field. */
1171 out_common_ctx_field
= bt_event_borrow_common_context_field(out_event
);
1172 if (!out_common_ctx_field
) {
1176 out_debug_info_field
= bt_field_structure_borrow_member_field_by_name(
1177 out_common_ctx_field
, debug_info_field_name
);
1179 vpid_field
= bt_field_structure_borrow_member_field_by_name_const(
1180 out_common_ctx_field
, VPID_FIELD_NAME
);
1181 ip_field
= bt_field_structure_borrow_member_field_by_name_const(
1182 out_common_ctx_field
, IP_FIELD_NAME
);
1184 vpid
= bt_field_integer_signed_get_value(vpid_field
);
1185 ip
= bt_field_integer_unsigned_get_value(ip_field
);
1188 * Borrow the debug_info structure needed for the source
1191 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
,
1192 bt_stream_borrow_trace_const(
1193 bt_event_borrow_stream_const(in_event
)));
1197 * Perform the debug-info resolving and set the event fields
1200 fill_debug_info_field(debug_info
, vpid
, ip
, out_debug_info_field
);
1202 BT_COMP_LOGD("No debug information for this trace. Setting debug "
1203 "info fields to empty strings.");
1204 fill_debug_info_field_empty(out_debug_info_field
,
1205 log_level
, self_comp
);
1212 void update_event_statedump_if_needed(struct debug_info_msg_iter
*debug_it
,
1213 const bt_event
*in_event
)
1215 const bt_field
*event_common_ctx
;
1216 const bt_field_class
*event_common_ctx_fc
;
1217 const bt_event_class
*in_event_class
= bt_event_borrow_class_const(in_event
);
1220 * If the event is an lttng_ust_statedump event AND has the right event
1221 * common context fields update the debug-info view for this process.
1223 event_common_ctx
= bt_event_borrow_common_context_field_const(in_event
);
1224 if (!event_common_ctx
) {
1228 event_common_ctx_fc
= bt_field_borrow_class_const(event_common_ctx
);
1229 if (is_event_common_ctx_dbg_info_compatible(event_common_ctx_fc
,
1230 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1231 /* Checkout if it might be a one of lttng ust statedump events. */
1232 const char *in_event_name
= bt_event_class_get_name(in_event_class
);
1233 if (strncmp(in_event_name
, LTTNG_UST_STATEDUMP_PREFIX
,
1234 strlen(LTTNG_UST_STATEDUMP_PREFIX
)) == 0) {
1235 /* Handle statedump events. */
1236 handle_event_statedump(debug_it
, in_event
);
1244 bt_message
*handle_event_message(struct debug_info_msg_iter
*debug_it
,
1245 const bt_message
*in_message
)
1247 const bt_clock_snapshot
*cs
;
1248 const bt_clock_class
*default_cc
;
1249 const bt_packet
*in_packet
;
1250 const bt_stream
*in_stream
;
1251 const bt_stream
*out_stream
;
1252 bt_event_class
*out_event_class
;
1253 bt_packet
*out_packet
= NULL
;
1254 bt_event
*out_event
;
1255 bt_logging_level log_level
= debug_it
->log_level
;
1256 bt_self_component
*self_comp
= debug_it
->self_comp
;
1258 bt_message
*out_message
= NULL
;
1260 /* Borrow the input event and its event class. */
1261 const bt_event
*in_event
=
1262 bt_message_event_borrow_event_const(in_message
);
1263 const bt_event_class
*in_event_class
=
1264 bt_event_borrow_class_const(in_event
);
1266 update_event_statedump_if_needed(debug_it
, in_event
);
1268 out_event_class
= trace_ir_mapping_borrow_mapped_event_class(
1269 debug_it
->ir_maps
, in_event_class
);
1270 if (!out_event_class
) {
1271 out_event_class
= trace_ir_mapping_create_new_mapped_event_class(
1272 debug_it
->ir_maps
, in_event_class
);
1274 BT_ASSERT_DBG(out_event_class
);
1276 /* Borrow the input stream. */
1277 in_stream
= bt_event_borrow_stream_const(in_event
);
1278 BT_ASSERT_DBG(in_stream
);
1279 out_stream
= trace_ir_mapping_borrow_mapped_stream(debug_it
->ir_maps
,
1281 BT_ASSERT_DBG(in_stream
);
1283 /* Borrow the input and output packets. */
1284 in_packet
= bt_event_borrow_packet_const(in_event
);
1286 out_packet
= trace_ir_mapping_borrow_mapped_packet(
1287 debug_it
->ir_maps
, in_packet
);
1290 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1291 bt_event_class_borrow_stream_class_const(in_event_class
));
1293 /* Borrow event clock snapshot. */
1294 cs
= bt_message_event_borrow_default_clock_snapshot_const(
1297 /* Create an output event message. */
1300 bt_message_event_create_with_packet_and_default_clock_snapshot(
1301 debug_it
->input_iterator
,
1302 out_event_class
, out_packet
,
1303 bt_clock_snapshot_get_value(cs
));
1306 bt_message_event_create_with_default_clock_snapshot(
1307 debug_it
->input_iterator
,
1308 out_event_class
, out_stream
,
1309 bt_clock_snapshot_get_value(cs
));
1313 out_message
= bt_message_event_create_with_packet(
1314 debug_it
->input_iterator
, out_event_class
,
1317 out_message
= bt_message_event_create(
1318 debug_it
->input_iterator
, out_event_class
,
1324 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1325 "Error creating output event message.");
1329 out_event
= bt_message_event_borrow_event(out_message
);
1331 /* Copy the original fields to the output event. */
1332 if (copy_event_content(in_event
, out_event
, log_level
, self_comp
) !=
1333 DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK
) {
1334 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1335 "Error copying event message content output event message: "
1336 "in-ev-addr=%p, out-ev-addr=%p", in_event
, out_event
);
1341 * Try to set the debug-info fields based on debug information that is
1344 fill_debug_info_event_if_needed(debug_it
, in_event
, out_event
);
1349 BT_MESSAGE_PUT_REF_AND_RESET(out_message
);
1355 bt_message
*handle_stream_begin_message(struct debug_info_msg_iter
*debug_it
,
1356 const bt_message
*in_message
)
1358 const bt_stream
*in_stream
;
1359 bt_message
*out_message
;
1360 bt_stream
*out_stream
;
1361 bt_logging_level log_level
= debug_it
->log_level
;
1362 bt_self_component
*self_comp
= debug_it
->self_comp
;
1364 in_stream
= bt_message_stream_beginning_borrow_stream_const(in_message
);
1365 BT_ASSERT(in_stream
);
1367 /* Create a duplicated output stream. */
1368 out_stream
= trace_ir_mapping_create_new_mapped_stream(
1369 debug_it
->ir_maps
, in_stream
);
1375 /* Create an output stream beginning message. */
1376 out_message
= bt_message_stream_beginning_create(
1377 debug_it
->input_iterator
, out_stream
);
1379 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1380 "Error creating output stream beginning message: "
1381 "out-s-addr=%p", out_stream
);
1388 bt_message
*handle_stream_end_message(struct debug_info_msg_iter
*debug_it
,
1389 const bt_message
*in_message
)
1391 const bt_stream
*in_stream
;
1392 bt_message
*out_message
= NULL
;
1393 bt_stream
*out_stream
;
1394 bt_logging_level log_level
= debug_it
->log_level
;
1395 bt_self_component
*self_comp
= debug_it
->self_comp
;
1397 in_stream
= bt_message_stream_end_borrow_stream_const(in_message
);
1398 BT_ASSERT(in_stream
);
1400 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1401 debug_it
->ir_maps
, in_stream
);
1402 BT_ASSERT(out_stream
);
1404 /* Create an output stream end message. */
1405 out_message
= bt_message_stream_end_create(debug_it
->input_iterator
,
1408 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1409 "Error creating output stream end message: "
1410 "out-s-addr=%p", out_stream
);
1414 /* Remove stream from trace mapping hashtable. */
1415 trace_ir_mapping_remove_mapped_stream(debug_it
->ir_maps
, in_stream
);
1422 bt_message
*handle_packet_begin_message(struct debug_info_msg_iter
*debug_it
,
1423 const bt_message
*in_message
)
1425 bool has_default_clock_snapshot
;
1426 const bt_clock_snapshot
*cs
;
1427 bt_message
*out_message
= NULL
;
1428 bt_packet
*out_packet
;
1429 bt_logging_level log_level
= debug_it
->log_level
;
1430 bt_self_component
*self_comp
= debug_it
->self_comp
;
1432 const bt_packet
*in_packet
=
1433 bt_message_packet_beginning_borrow_packet_const(in_message
);
1434 BT_ASSERT(in_packet
);
1436 /* This packet should not be already mapped. */
1437 BT_ASSERT(!trace_ir_mapping_borrow_mapped_packet(
1438 debug_it
->ir_maps
, in_packet
));
1440 out_packet
= trace_ir_mapping_create_new_mapped_packet(debug_it
->ir_maps
,
1443 BT_ASSERT(out_packet
);
1445 has_default_clock_snapshot
=
1446 bt_stream_class_packets_have_beginning_default_clock_snapshot(
1447 bt_stream_borrow_class_const(
1448 bt_packet_borrow_stream_const(in_packet
)));
1449 if (has_default_clock_snapshot
) {
1450 /* Borrow clock snapshot. */
1451 cs
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
1454 /* Create an output packet beginning message. */
1455 out_message
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1456 debug_it
->input_iterator
, out_packet
,
1457 bt_clock_snapshot_get_value(cs
));
1459 out_message
= bt_message_packet_beginning_create(
1460 debug_it
->input_iterator
, out_packet
);
1463 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1464 "Error creating output packet beginning message: "
1465 "out-p-addr=%p", out_packet
);
1472 bt_message
*handle_packet_end_message(struct debug_info_msg_iter
*debug_it
,
1473 const bt_message
*in_message
)
1475 bool has_default_clock_snapshot
;
1476 const bt_clock_snapshot
*cs
;
1477 const bt_packet
*in_packet
;
1478 bt_message
*out_message
= NULL
;
1479 bt_packet
*out_packet
;
1480 bt_logging_level log_level
= debug_it
->log_level
;
1481 bt_self_component
*self_comp
= debug_it
->self_comp
;
1483 in_packet
= bt_message_packet_end_borrow_packet_const(in_message
);
1484 BT_ASSERT(in_packet
);
1486 out_packet
= trace_ir_mapping_borrow_mapped_packet(debug_it
->ir_maps
, in_packet
);
1487 BT_ASSERT(out_packet
);
1489 has_default_clock_snapshot
=
1490 bt_stream_class_packets_have_end_default_clock_snapshot(
1491 bt_stream_borrow_class_const(
1492 bt_packet_borrow_stream_const(in_packet
)));
1493 if (has_default_clock_snapshot
) {
1494 /* Borrow clock snapshot. */
1495 cs
= bt_message_packet_end_borrow_default_clock_snapshot_const(
1498 /* Create an outpute packet end message. */
1499 out_message
= bt_message_packet_end_create_with_default_clock_snapshot(
1500 debug_it
->input_iterator
, out_packet
,
1501 bt_clock_snapshot_get_value(cs
));
1503 out_message
= bt_message_packet_end_create(
1504 debug_it
->input_iterator
, out_packet
);
1508 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1509 "Error creating output packet end message: "
1510 "out-p-addr=%p", out_packet
);
1514 /* Remove packet from data mapping hashtable. */
1515 trace_ir_mapping_remove_mapped_packet(debug_it
->ir_maps
, in_packet
);
1522 bt_message
*handle_msg_iterator_inactivity(struct debug_info_msg_iter
*debug_it
,
1523 const bt_message
*in_message
)
1526 * This message type can be forwarded directly because it does
1527 * not refer to any objects in the trace class.
1529 bt_message_get_ref(in_message
);
1530 return (bt_message
*) in_message
;
1534 bt_message
*handle_discarded_events_message(struct debug_info_msg_iter
*debug_it
,
1535 const bt_message
*in_message
)
1537 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1538 const bt_stream
*in_stream
;
1539 bool has_default_clock_snapshots
;
1540 uint64_t discarded_events
, begin_cs_value
, end_cs_value
;
1541 bt_property_availability prop_avail
;
1542 bt_message
*out_message
= NULL
;
1543 bt_stream
*out_stream
;
1544 bt_logging_level log_level
= debug_it
->log_level
;
1545 bt_self_component
*self_comp
= debug_it
->self_comp
;
1547 in_stream
= bt_message_discarded_events_borrow_stream_const(
1549 BT_ASSERT(in_stream
);
1551 out_stream
= trace_ir_mapping_borrow_mapped_stream( debug_it
->ir_maps
,
1553 BT_ASSERT(out_stream
);
1555 has_default_clock_snapshots
=
1556 bt_stream_class_discarded_events_have_default_clock_snapshots(
1557 bt_stream_borrow_class_const(in_stream
));
1558 if (has_default_clock_snapshots
) {
1559 begin_cs
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1561 end_cs
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1564 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1565 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1567 out_message
= bt_message_discarded_events_create_with_default_clock_snapshots(
1568 debug_it
->input_iterator
, out_stream
,
1569 begin_cs_value
, end_cs_value
);
1571 out_message
= bt_message_discarded_events_create(
1572 debug_it
->input_iterator
, out_stream
);
1575 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1576 "Error creating output discarded events message: "
1577 "out-s-addr=%p", out_stream
);
1581 prop_avail
= bt_message_discarded_events_get_count(in_message
,
1584 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1585 bt_message_discarded_events_set_count(out_message
,
1594 bt_message
*handle_discarded_packets_message(struct debug_info_msg_iter
*debug_it
,
1595 const bt_message
*in_message
)
1597 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1598 bool has_default_clock_snapshots
;
1599 const bt_stream
*in_stream
;
1600 uint64_t discarded_packets
, begin_cs_value
, end_cs_value
;
1601 bt_property_availability prop_avail
;
1602 bt_message
*out_message
= NULL
;
1603 bt_stream
*out_stream
;
1604 bt_logging_level log_level
= debug_it
->log_level
;
1605 bt_self_component
*self_comp
= debug_it
->self_comp
;
1607 in_stream
= bt_message_discarded_packets_borrow_stream_const(
1609 BT_ASSERT(in_stream
);
1611 out_stream
= trace_ir_mapping_borrow_mapped_stream( debug_it
->ir_maps
,
1613 BT_ASSERT(out_stream
);
1615 has_default_clock_snapshots
=
1616 bt_stream_class_discarded_packets_have_default_clock_snapshots(
1617 bt_stream_borrow_class_const(in_stream
));
1618 if (has_default_clock_snapshots
) {
1619 begin_cs
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1622 end_cs
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1625 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1626 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1628 out_message
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1629 debug_it
->input_iterator
, out_stream
,
1630 begin_cs_value
, end_cs_value
);
1632 out_message
= bt_message_discarded_packets_create(
1633 debug_it
->input_iterator
, out_stream
);
1636 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1637 "Error creating output discarded packet message: "
1638 "out-s-addr=%p", out_stream
);
1642 prop_avail
= bt_message_discarded_packets_get_count(in_message
,
1643 &discarded_packets
);
1644 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1645 bt_message_discarded_packets_set_count(out_message
,
1654 const bt_message
*handle_message(struct debug_info_msg_iter
*debug_it
,
1655 const bt_message
*in_message
)
1657 bt_message
*out_message
= NULL
;
1659 switch (bt_message_get_type(in_message
)) {
1660 case BT_MESSAGE_TYPE_EVENT
:
1661 out_message
= handle_event_message(debug_it
, in_message
);
1663 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1664 out_message
= handle_packet_begin_message(debug_it
, in_message
);
1666 case BT_MESSAGE_TYPE_PACKET_END
:
1667 out_message
= handle_packet_end_message(debug_it
, in_message
);
1669 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1670 out_message
= handle_stream_begin_message(debug_it
, in_message
);
1672 case BT_MESSAGE_TYPE_STREAM_END
:
1673 out_message
= handle_stream_end_message(debug_it
, in_message
);
1675 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1676 out_message
= handle_msg_iterator_inactivity(debug_it
, in_message
);
1678 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1679 out_message
= handle_discarded_events_message(debug_it
, in_message
);
1681 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1682 out_message
= handle_discarded_packets_message(debug_it
, in_message
);
1693 struct bt_param_validation_map_value_entry_descr debug_info_params
[] = {
1694 { "debug-info-field-name", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
1695 { "debug-info-dir", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
1696 { "target-prefix", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
1697 { "full-path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
1698 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1702 bt_component_class_initialize_method_status
init_from_params(
1703 struct debug_info_component
*debug_info_component
,
1704 const bt_value
*params
)
1706 const bt_value
*value
;
1707 bt_component_class_initialize_method_status status
;
1708 bt_logging_level log_level
= debug_info_component
->log_level
;
1709 enum bt_param_validation_status validation_status
;
1710 gchar
*validate_error
= NULL
;
1712 validation_status
= bt_param_validation_validate(params
,
1713 debug_info_params
, &validate_error
);
1714 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1715 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1717 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1718 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1719 BT_COMP_LOGE_APPEND_CAUSE(debug_info_component
->self_comp
,
1720 "%s", validate_error
);
1726 value
= bt_value_map_borrow_entry_value_const(params
,
1727 "debug-info-field-name");
1729 debug_info_component
->arg_debug_info_field_name
=
1730 g_strdup(bt_value_string_get(value
));
1732 debug_info_component
->arg_debug_info_field_name
=
1733 g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME
);
1736 value
= bt_value_map_borrow_entry_value_const(params
, "debug-info-dir");
1738 debug_info_component
->arg_debug_dir
=
1739 g_strdup(bt_value_string_get(value
));
1741 debug_info_component
->arg_debug_dir
= NULL
;
1745 value
= bt_value_map_borrow_entry_value_const(params
, "target-prefix");
1747 debug_info_component
->arg_target_prefix
=
1748 g_strdup(bt_value_string_get(value
));
1750 debug_info_component
->arg_target_prefix
= NULL
;
1753 value
= bt_value_map_borrow_entry_value_const(params
, "full-path");
1755 debug_info_component
->arg_full_path
= bt_value_bool_get(value
);
1757 debug_info_component
->arg_full_path
= BT_FALSE
;
1760 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1763 g_free(validate_error
);
1769 bt_component_class_initialize_method_status
debug_info_comp_init(
1770 bt_self_component_filter
*self_comp_flt
,
1771 bt_self_component_filter_configuration
*config
,
1772 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
1774 struct debug_info_component
*debug_info_comp
;
1775 bt_component_class_initialize_method_status status
=
1776 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1777 bt_self_component_add_port_status add_port_status
;
1778 bt_self_component
*self_comp
=
1779 bt_self_component_filter_as_self_component(self_comp_flt
);
1780 bt_logging_level log_level
= bt_component_get_logging_level(
1781 bt_self_component_as_component(self_comp
));
1783 BT_COMP_LOGI("Initializing debug_info component: "
1784 "comp-addr=%p, params-addr=%p", self_comp
, params
);
1786 debug_info_comp
= g_new0(struct debug_info_component
, 1);
1787 if (!debug_info_comp
) {
1788 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1789 "Failed to allocate one debug_info component.");
1793 debug_info_comp
->log_level
= log_level
;
1794 debug_info_comp
->self_comp
= self_comp
;
1795 debug_info_comp
->self_comp_filter
= self_comp_flt
;
1796 bt_self_component_set_data(self_comp
, debug_info_comp
);
1798 add_port_status
= bt_self_component_filter_add_input_port(
1799 self_comp_flt
, "in", NULL
, NULL
);
1800 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1801 status
= (int) add_port_status
;
1805 add_port_status
= bt_self_component_filter_add_output_port(
1806 self_comp_flt
, "out", NULL
, NULL
);
1807 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1808 status
= (int) add_port_status
;
1812 status
= init_from_params(debug_info_comp
, params
);
1813 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1814 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1815 "Cannot configure debug_info component: "
1816 "debug_info-comp-addr=%p, params-addr=%p",
1817 debug_info_comp
, params
);
1824 destroy_debug_info_comp(debug_info_comp
);
1825 bt_self_component_set_data(self_comp
, NULL
);
1827 if (status
== BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1828 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1835 void debug_info_comp_finalize(bt_self_component_filter
*self_comp_flt
)
1837 struct debug_info_component
*debug_info
=
1838 bt_self_component_get_data(
1839 bt_self_component_filter_as_self_component(
1841 bt_logging_level log_level
= debug_info
->log_level
;
1842 bt_self_component
*self_comp
= debug_info
->self_comp
;
1844 BT_COMP_LOGI("Finalizing debug_info self_component: comp-addr=%p",
1847 destroy_debug_info_comp(debug_info
);
1851 bt_message_iterator_class_next_method_status
debug_info_msg_iter_next(
1852 bt_self_message_iterator
*self_msg_iter
,
1853 const bt_message_array_const msgs
, uint64_t capacity
,
1856 bt_message_iterator
*upstream_iterator
= NULL
;
1857 bt_message_iterator_next_status upstream_iterator_ret_status
;
1858 struct debug_info_msg_iter
*debug_info_msg_iter
;
1859 struct debug_info_component
*debug_info
= NULL
;
1860 bt_message_iterator_class_next_method_status status
;
1861 bt_self_component
*self_comp
= NULL
;
1862 bt_message_array_const input_msgs
;
1863 const bt_message
*out_message
;
1864 uint64_t curr_msg_idx
, i
;
1866 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1868 self_comp
= bt_self_message_iterator_borrow_component(self_msg_iter
);
1869 BT_ASSERT_DBG(self_comp
);
1871 debug_info
= bt_self_component_get_data(self_comp
);
1872 BT_ASSERT_DBG(debug_info
);
1874 debug_info_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
1875 BT_ASSERT_DBG(debug_info_msg_iter
);
1877 upstream_iterator
= debug_info_msg_iter
->msg_iter
;
1878 BT_ASSERT_DBG(upstream_iterator
);
1880 upstream_iterator_ret_status
=
1881 bt_message_iterator_next(
1882 upstream_iterator
, &input_msgs
, count
);
1883 if (upstream_iterator_ret_status
!=
1884 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
1886 * No messages were returned. Not necessarily an error.
1887 * Convert the upstream message iterator status to a
1890 status
= (int) upstream_iterator_ret_status
;
1895 * There should never be more received messages than the capacity we
1898 BT_ASSERT_DBG(*count
<= capacity
);
1900 for (curr_msg_idx
= 0; curr_msg_idx
< *count
; curr_msg_idx
++) {
1901 out_message
= handle_message(debug_info_msg_iter
,
1902 input_msgs
[curr_msg_idx
]);
1904 goto handle_msg_error
;
1907 msgs
[curr_msg_idx
] = out_message
;
1909 * Drop our reference of the input message as we are done with
1910 * it and created a output copy.
1912 bt_message_put_ref(input_msgs
[curr_msg_idx
]);
1919 * Drop references of all the output messages created before the
1922 for (i
= 0; i
< curr_msg_idx
; i
++) {
1923 bt_message_put_ref(msgs
[i
]);
1927 * Drop references of all the input messages not dropped before the
1930 for (i
= curr_msg_idx
; i
< *count
; i
++) {
1931 bt_message_put_ref(input_msgs
[i
]);
1934 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1941 void debug_info_msg_iter_destroy(struct debug_info_msg_iter
*debug_info_msg_iter
)
1943 if (!debug_info_msg_iter
) {
1947 if (debug_info_msg_iter
->msg_iter
) {
1948 bt_message_iterator_put_ref(
1949 debug_info_msg_iter
->msg_iter
);
1952 if (debug_info_msg_iter
->ir_maps
) {
1953 trace_ir_maps_destroy(debug_info_msg_iter
->ir_maps
);
1956 if (debug_info_msg_iter
->debug_info_map
) {
1957 g_hash_table_destroy(debug_info_msg_iter
->debug_info_map
);
1960 bt_fd_cache_fini(&debug_info_msg_iter
->fd_cache
);
1961 g_free(debug_info_msg_iter
);
1968 bt_message_iterator_class_initialize_method_status
debug_info_msg_iter_init(
1969 bt_self_message_iterator
*self_msg_iter
,
1970 bt_self_message_iterator_configuration
*config
,
1971 bt_self_component_port_output
*self_port
)
1973 bt_message_iterator_class_initialize_method_status status
;
1974 bt_message_iterator_create_from_message_iterator_status
1976 struct bt_self_component_port_input
*input_port
= NULL
;
1977 bt_message_iterator
*upstream_iterator
= NULL
;
1978 struct debug_info_msg_iter
*debug_info_msg_iter
= NULL
;
1979 gchar
*debug_info_field_name
;
1981 bt_self_component
*self_comp
=
1982 bt_self_message_iterator_borrow_component(self_msg_iter
);
1983 bt_logging_level log_level
= bt_component_get_logging_level(
1984 bt_self_component_as_component(self_comp
));
1986 debug_info_msg_iter
= g_new0(struct debug_info_msg_iter
, 1);
1987 if (!debug_info_msg_iter
) {
1988 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1992 debug_info_msg_iter
->log_level
= log_level
;
1993 debug_info_msg_iter
->self_comp
= self_comp
;
1995 debug_info_msg_iter
->debug_info_component
=
1996 bt_self_component_get_data(self_comp
);
1998 /* Borrow the upstream input port. */
1999 input_port
= bt_self_component_filter_borrow_input_port_by_name(
2000 debug_info_msg_iter
->debug_info_component
->self_comp_filter
,
2003 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2007 /* Create an iterator on the upstream component. */
2008 msg_iter_status
= bt_message_iterator_create_from_message_iterator(
2009 self_msg_iter
, input_port
, &upstream_iterator
);
2010 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
2011 status
= (int) msg_iter_status
;
2015 BT_MESSAGE_ITERATOR_MOVE_REF(
2016 debug_info_msg_iter
->msg_iter
, upstream_iterator
);
2018 /* Create hashtable that will contain debug info mapping. */
2019 debug_info_msg_iter
->debug_info_map
= g_hash_table_new_full(
2020 g_direct_hash
, g_direct_equal
, (GDestroyNotify
) NULL
,
2021 (GDestroyNotify
) debug_info_destroy
);
2022 if (!debug_info_msg_iter
->debug_info_map
) {
2023 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2027 debug_info_field_name
=
2028 debug_info_msg_iter
->debug_info_component
->arg_debug_info_field_name
;
2030 debug_info_msg_iter
->ir_maps
= trace_ir_maps_create(self_comp
,
2031 debug_info_field_name
, log_level
);
2032 if (!debug_info_msg_iter
->ir_maps
) {
2033 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2037 ret
= bt_fd_cache_init(&debug_info_msg_iter
->fd_cache
, log_level
);
2039 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2043 bt_self_message_iterator_configuration_set_can_seek_forward(config
,
2044 bt_message_iterator_can_seek_forward(
2045 debug_info_msg_iter
->msg_iter
));
2047 bt_self_message_iterator_set_data(self_msg_iter
, debug_info_msg_iter
);
2048 debug_info_msg_iter
->input_iterator
= self_msg_iter
;
2050 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2054 debug_info_msg_iter_destroy(debug_info_msg_iter
);
2061 bt_message_iterator_class_can_seek_beginning_method_status
2062 debug_info_msg_iter_can_seek_beginning(bt_self_message_iterator
*self_msg_iter
,
2065 struct debug_info_msg_iter
*debug_info_msg_iter
=
2066 bt_self_message_iterator_get_data(self_msg_iter
);
2067 BT_ASSERT(debug_info_msg_iter
);
2069 return (int) bt_message_iterator_can_seek_beginning(
2070 debug_info_msg_iter
->msg_iter
, can_seek
);
2074 bt_message_iterator_class_seek_beginning_method_status
2075 debug_info_msg_iter_seek_beginning(bt_self_message_iterator
*self_msg_iter
)
2077 struct debug_info_msg_iter
*debug_info_msg_iter
=
2078 bt_self_message_iterator_get_data(self_msg_iter
);
2079 bt_message_iterator_class_seek_beginning_method_status status
=
2080 BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK
;
2081 bt_message_iterator_seek_beginning_status seek_beg_status
;
2083 BT_ASSERT(debug_info_msg_iter
);
2085 /* Ask the upstream component to seek to the beginning. */
2086 seek_beg_status
= bt_message_iterator_seek_beginning(
2087 debug_info_msg_iter
->msg_iter
);
2088 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
2089 status
= (int) seek_beg_status
;
2093 /* Clear this iterator data. */
2094 trace_ir_maps_clear(debug_info_msg_iter
->ir_maps
);
2095 g_hash_table_remove_all(debug_info_msg_iter
->debug_info_map
);
2102 void debug_info_msg_iter_finalize(bt_self_message_iterator
*it
)
2104 struct debug_info_msg_iter
*debug_info_msg_iter
;
2106 debug_info_msg_iter
= bt_self_message_iterator_get_data(it
);
2107 BT_ASSERT(debug_info_msg_iter
);
2109 debug_info_msg_iter_destroy(debug_info_msg_iter
);