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>
8 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_COMP_LOG_SELF_COMP self_comp
30 #define BT_LOG_OUTPUT_LEVEL log_level
31 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO"
32 #include "plugins/comp-logging.h"
36 #include "common/assert.h"
37 #include "common/common.h"
38 #include "fd-cache/fd-cache.h"
41 #include "debug-info.h"
42 #include "trace-ir-data-copy.h"
43 #include "trace-ir-mapping.h"
44 #include "trace-ir-metadata-copy.h"
47 #define DEFAULT_DEBUG_INFO_FIELD_NAME "debug_info"
48 #define LTTNG_UST_STATEDUMP_PREFIX "lttng_ust"
49 #define VPID_FIELD_NAME "vpid"
50 #define IP_FIELD_NAME "ip"
51 #define BADDR_FIELD_NAME "baddr"
52 #define CRC32_FIELD_NAME "crc"
53 #define BUILD_ID_FIELD_NAME "build_id"
54 #define FILENAME_FIELD_NAME "filename"
55 #define IS_PIC_FIELD_NAME "is_pic"
56 #define MEMSZ_FIELD_NAME "memsz"
57 #define PATH_FIELD_NAME "path"
59 struct debug_info_component
{
60 bt_logging_level log_level
;
61 bt_self_component
*self_comp
;
63 gchar
*arg_debug_info_field_name
;
64 gchar
*arg_target_prefix
;
65 bt_bool arg_full_path
;
68 struct debug_info_msg_iter
{
69 bt_logging_level log_level
;
70 struct debug_info_component
*debug_info_component
;
71 bt_self_message_iterator
*input_iterator
;
72 bt_self_component
*self_comp
;
73 bt_self_component_port_input_message_iterator
*msg_iter
;
75 struct trace_ir_maps
*ir_maps
;
76 /* in_trace -> debug_info_mapping. */
77 GHashTable
*debug_info_map
;
79 struct bt_fd_cache fd_cache
;
82 struct debug_info_source
{
83 /* Strings are owned by debug_info_source. */
86 * Store the line number as a string so that the allocation and
87 * conversion to string is only done once.
91 /* short_src_path points inside src_path, no need to free. */
92 const gchar
*short_src_path
;
94 /* short_bin_path points inside bin_path, no need to free. */
95 const gchar
*short_bin_path
;
97 * Location within the binary. Either absolute (@0x1234) or
103 struct proc_debug_info_sources
{
105 * Hash table: base address (pointer to uint64_t) to bin info; owned by
106 * proc_debug_info_sources.
108 GHashTable
*baddr_to_bin_info
;
111 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
112 * owned by proc_debug_info_sources.
114 GHashTable
*ip_to_debug_info_src
;
118 bt_logging_level log_level
;
119 bt_self_component
*self_comp
;
120 struct debug_info_component
*comp
;
121 const bt_trace
*input_trace
;
122 uint64_t destruction_listener_id
;
125 * Hash table of VPIDs (pointer to int64_t) to
126 * (struct proc_debug_info_sources*); owned by debug_info.
128 GHashTable
*vpid_to_proc_dbg_info_src
;
129 GQuark q_statedump_bin_info
;
130 GQuark q_statedump_debug_link
;
131 GQuark q_statedump_build_id
;
132 GQuark q_statedump_start
;
136 struct bt_fd_cache
*fd_cache
; /* Weak ref. Owned by the iterator. */
140 int debug_info_init(struct debug_info
*info
)
142 info
->q_statedump_bin_info
= g_quark_from_string(
143 "lttng_ust_statedump:bin_info");
144 info
->q_statedump_debug_link
= g_quark_from_string(
145 "lttng_ust_statedump:debug_link");
146 info
->q_statedump_build_id
= g_quark_from_string(
147 "lttng_ust_statedump:build_id");
148 info
->q_statedump_start
= g_quark_from_string(
149 "lttng_ust_statedump:start");
150 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
151 info
->q_lib_load
= g_quark_from_string("lttng_ust_lib:load");
152 info
->q_lib_unload
= g_quark_from_string("lttng_ust_lib:unload");
154 return bin_info_init(info
->log_level
, info
->self_comp
);
158 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
160 if (!debug_info_src
) {
164 g_free(debug_info_src
->func
);
165 g_free(debug_info_src
->line_no
);
166 g_free(debug_info_src
->src_path
);
167 g_free(debug_info_src
->bin_path
);
168 g_free(debug_info_src
->bin_loc
);
169 g_free(debug_info_src
);
173 struct debug_info_source
*debug_info_source_create_from_bin(
174 struct bin_info
*bin
, uint64_t ip
,
175 bt_self_component
*self_comp
)
178 struct debug_info_source
*debug_info_src
= NULL
;
179 struct source_location
*src_loc
= NULL
;
180 bt_logging_level log_level
= bin
->log_level
;
182 debug_info_src
= g_new0(struct debug_info_source
, 1);
184 if (!debug_info_src
) {
188 /* Lookup function name */
189 ret
= bin_info_lookup_function_name(bin
, ip
, &debug_info_src
->func
);
194 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
195 if (!bin
->is_elf_only
|| !debug_info_src
->func
) {
196 /* Lookup source location */
197 ret
= bin_info_lookup_source_location(bin
, ip
, &src_loc
);
199 BT_COMP_LOGI("Failed to lookup source location: ret=%d", ret
);
204 debug_info_src
->line_no
=
205 g_strdup_printf("%"PRId64
, src_loc
->line_no
);
206 if (!debug_info_src
->line_no
) {
207 BT_COMP_LOGE("Error occured when setting line_no field.");
211 if (src_loc
->filename
) {
212 debug_info_src
->src_path
= g_strdup(src_loc
->filename
);
213 if (!debug_info_src
->src_path
) {
217 debug_info_src
->short_src_path
= get_filename_from_path(
218 debug_info_src
->src_path
);
220 source_location_destroy(src_loc
);
224 debug_info_src
->bin_path
= g_strdup(bin
->elf_path
);
225 if (!debug_info_src
->bin_path
) {
229 debug_info_src
->short_bin_path
= get_filename_from_path(
230 debug_info_src
->bin_path
);
232 ret
= bin_info_get_bin_loc(bin
, ip
, &(debug_info_src
->bin_loc
));
239 return debug_info_src
;
242 debug_info_source_destroy(debug_info_src
);
247 void proc_debug_info_sources_destroy(
248 struct proc_debug_info_sources
*proc_dbg_info_src
)
250 if (!proc_dbg_info_src
) {
254 if (proc_dbg_info_src
->baddr_to_bin_info
) {
255 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_bin_info
);
258 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
259 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
262 g_free(proc_dbg_info_src
);
266 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
268 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
270 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
271 if (!proc_dbg_info_src
) {
275 proc_dbg_info_src
->baddr_to_bin_info
= g_hash_table_new_full(
276 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
277 (GDestroyNotify
) bin_info_destroy
);
278 if (!proc_dbg_info_src
->baddr_to_bin_info
) {
282 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
283 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
284 (GDestroyNotify
) debug_info_source_destroy
);
285 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
290 return proc_dbg_info_src
;
293 proc_debug_info_sources_destroy(proc_dbg_info_src
);
298 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
299 GHashTable
*ht
, int64_t vpid
)
301 gpointer key
= g_new0(int64_t, 1);
302 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
308 *((int64_t *) key
) = vpid
;
310 /* Exists? Return it */
311 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
312 if (proc_dbg_info_src
) {
316 /* Otherwise, create and return it */
317 proc_dbg_info_src
= proc_debug_info_sources_create();
318 if (!proc_dbg_info_src
) {
322 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
323 /* Ownership passed to ht */
327 return proc_dbg_info_src
;
331 const bt_field
*event_borrow_payload_field(const bt_event
*event
,
332 const char *field_name
)
334 const bt_field
*event_payload
, *field
;
336 event_payload
= bt_event_borrow_payload_field_const(event
);
337 BT_ASSERT(event_payload
);
339 field
= bt_field_structure_borrow_member_field_by_name_const(
340 event_payload
, field_name
);
345 const bt_field
*event_borrow_common_context_field(const bt_event
*event
,
346 const char *field_name
)
348 const bt_field
*event_common_ctx
, *field
= NULL
;
350 event_common_ctx
= bt_event_borrow_common_context_field_const(event
);
351 if (!event_common_ctx
) {
355 field
= bt_field_structure_borrow_member_field_by_name_const(
356 event_common_ctx
, field_name
);
363 void event_get_common_context_signed_integer_field_value(
364 const bt_event
*event
, const char *field_name
, int64_t *value
)
366 *value
= bt_field_signed_integer_get_value(
367 event_borrow_common_context_field(event
, field_name
));
371 int event_get_payload_build_id_length(const bt_event
*event
,
372 const char *field_name
, uint64_t *build_id_len
)
374 const bt_field
*build_id_field
;
375 const bt_field_class
*build_id_field_class
;
377 build_id_field
= event_borrow_payload_field(event
, field_name
);
378 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
380 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
381 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
);
382 BT_ASSERT(bt_field_class_get_type(
383 bt_field_class_array_borrow_element_field_class_const(
384 build_id_field_class
)) ==
385 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
387 *build_id_len
= bt_field_array_get_length(build_id_field
);
393 int event_get_payload_build_id_value(const bt_event
*event
,
394 const char *field_name
, uint8_t *build_id
)
396 const bt_field
*curr_field
, *build_id_field
;
397 const bt_field_class
*build_id_field_class
;
398 uint64_t i
, build_id_len
;
403 build_id_field
= event_borrow_payload_field(event
, field_name
);
404 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
406 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
407 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
);
408 BT_ASSERT(bt_field_class_get_type(
409 bt_field_class_array_borrow_element_field_class_const(
410 build_id_field_class
)) ==
411 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
413 build_id_len
= bt_field_array_get_length(build_id_field
);
415 for (i
= 0; i
< build_id_len
; i
++) {
417 bt_field_array_borrow_element_field_by_index_const(
420 build_id
[i
] = bt_field_unsigned_integer_get_value(curr_field
);
427 void event_get_payload_unsigned_integer_field_value(const bt_event
*event
,
428 const char *field_name
, uint64_t *value
)
430 *value
= bt_field_unsigned_integer_get_value(
431 event_borrow_payload_field(event
, field_name
));
435 void event_get_payload_string_field_value(const bt_event
*event
,
436 const char *field_name
, const char **value
)
438 *value
= bt_field_string_get_value(
439 event_borrow_payload_field(event
, field_name
));
443 bool event_has_payload_field(const bt_event
*event
,
444 const char *field_name
)
446 return event_borrow_payload_field(event
, field_name
) != NULL
;
450 struct debug_info_source
*proc_debug_info_sources_get_entry(
451 struct debug_info
*debug_info
,
452 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
454 struct debug_info_source
*debug_info_src
= NULL
;
455 gpointer key
= g_new0(uint64_t, 1);
457 gpointer baddr
, value
;
463 *((uint64_t *) key
) = ip
;
465 /* Look in IP to debug infos hash table first. */
466 debug_info_src
= g_hash_table_lookup(
467 proc_dbg_info_src
->ip_to_debug_info_src
,
469 if (debug_info_src
) {
473 /* Check in all bin_infos. */
474 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_bin_info
);
476 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
478 struct bin_info
*bin
= value
;
480 if (!bin_info_has_address(value
, ip
)) {
485 * Found; add it to cache.
487 * FIXME: this should be bounded in size (and implement
488 * a caching policy), and entries should be prunned when
489 * libraries are unmapped.
491 debug_info_src
= debug_info_source_create_from_bin(bin
, ip
,
492 debug_info
->self_comp
);
493 if (debug_info_src
) {
495 proc_dbg_info_src
->ip_to_debug_info_src
,
496 key
, debug_info_src
);
497 /* Ownership passed to ht. */
505 return debug_info_src
;
509 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
510 int64_t vpid
, uint64_t ip
)
512 struct debug_info_source
*dbg_info_src
= NULL
;
513 struct proc_debug_info_sources
*proc_dbg_info_src
;
515 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
516 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
517 if (!proc_dbg_info_src
) {
521 dbg_info_src
= proc_debug_info_sources_get_entry(debug_info
,
522 proc_dbg_info_src
, ip
);
529 struct debug_info
*debug_info_create(struct debug_info_component
*comp
,
530 const bt_trace
*trace
, struct bt_fd_cache
*fdc
)
533 struct debug_info
*debug_info
;
539 debug_info
= g_new0(struct debug_info
, 1);
544 debug_info
->log_level
= comp
->log_level
;
545 debug_info
->self_comp
= comp
->self_comp
;
546 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
547 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
548 (GDestroyNotify
) proc_debug_info_sources_destroy
);
549 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
553 debug_info
->comp
= comp
;
554 ret
= debug_info_init(debug_info
);
559 debug_info
->input_trace
= trace
;
560 debug_info
->fd_cache
= fdc
;
570 void debug_info_destroy(struct debug_info
*debug_info
)
572 bt_logging_level log_level
;
573 bt_self_component
*self_comp
;
574 bt_trace_remove_listener_status remove_listener_status
;
579 log_level
= debug_info
->log_level
;
580 self_comp
= debug_info
->self_comp
;
582 if (debug_info
->vpid_to_proc_dbg_info_src
) {
583 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
586 remove_listener_status
= bt_trace_remove_destruction_listener(
587 debug_info
->input_trace
,
588 debug_info
->destruction_listener_id
);
589 if (remove_listener_status
!= BT_TRACE_REMOVE_LISTENER_STATUS_OK
) {
590 BT_COMP_LOGE("Trace destruction listener removal failed.");
591 bt_current_thread_clear_error();
600 void handle_event_statedump_build_id(struct debug_info
*debug_info
,
601 const bt_event
*event
)
603 struct proc_debug_info_sources
*proc_dbg_info_src
;
604 uint64_t build_id_len
, baddr
;
605 uint8_t *build_id
= NULL
;
606 struct bin_info
*bin
;
610 event_get_common_context_signed_integer_field_value(event
,
611 VPID_FIELD_NAME
, &vpid
);
612 event_get_payload_unsigned_integer_field_value(event
,
613 BADDR_FIELD_NAME
, &baddr
);
615 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
616 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
617 if (!proc_dbg_info_src
) {
621 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
625 * The build_id event comes after the bin has been
626 * created. If it isn't found, just ignore this event.
630 ret
= event_get_payload_build_id_length(event
, BUILD_ID_FIELD_NAME
,
633 build_id
= g_new0(uint8_t, build_id_len
);
638 ret
= event_get_payload_build_id_value(event
, BUILD_ID_FIELD_NAME
,
644 ret
= bin_info_set_build_id(bin
, build_id
, build_id_len
);
650 * Reset the is_elf_only flag in case it had been set
651 * previously, because we might find separate debug info using
652 * the new build id information.
654 bin
->is_elf_only
= false;
662 void handle_event_statedump_debug_link(struct debug_info
*debug_info
,
663 const bt_event
*event
)
665 struct proc_debug_info_sources
*proc_dbg_info_src
;
666 struct bin_info
*bin
= NULL
;
669 const char *filename
= NULL
;
671 uint64_t crc_field_value
;
673 event_get_common_context_signed_integer_field_value(event
,
674 VPID_FIELD_NAME
, &vpid
);
676 event_get_payload_unsigned_integer_field_value(event
,
677 BADDR_FIELD_NAME
, &baddr
);
679 event_get_payload_unsigned_integer_field_value(event
,
680 CRC32_FIELD_NAME
, &crc_field_value
);
682 crc32
= (uint32_t) crc_field_value
;
684 event_get_payload_string_field_value(event
,
685 FILENAME_FIELD_NAME
, &filename
);
687 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
688 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
689 if (!proc_dbg_info_src
) {
693 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
697 * The debug_link event comes after the bin has been
698 * created. If it isn't found, just ignore this event.
703 bin_info_set_debug_link(bin
, filename
, crc32
);
710 void handle_bin_info_event(struct debug_info
*debug_info
,
711 const bt_event
*event
, bool has_pic_field
)
713 struct proc_debug_info_sources
*proc_dbg_info_src
;
714 struct bin_info
*bin
;
715 uint64_t baddr
, memsz
;
721 event_get_payload_unsigned_integer_field_value(event
,
722 MEMSZ_FIELD_NAME
, &memsz
);
728 event_get_payload_unsigned_integer_field_value(event
,
729 BADDR_FIELD_NAME
, &baddr
);
732 * This field is not produced by the dlopen event emitted before
735 if (!event_has_payload_field(event
, PATH_FIELD_NAME
)) {
738 event_get_payload_string_field_value(event
, PATH_FIELD_NAME
, &path
);
741 uint64_t is_pic_field_value
;
743 event_get_payload_unsigned_integer_field_value(event
,
744 IS_PIC_FIELD_NAME
, &is_pic_field_value
);
745 is_pic
= is_pic_field_value
== 1;
748 * dlopen has no is_pic field, because the shared
749 * object is always PIC.
754 event_get_common_context_signed_integer_field_value(event
,
755 VPID_FIELD_NAME
, &vpid
);
757 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
758 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
759 if (!proc_dbg_info_src
) {
763 key
= g_new0(uint64_t, 1);
768 *((uint64_t *) key
) = baddr
;
770 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
, key
);
775 bin
= bin_info_create(debug_info
->fd_cache
, path
, baddr
, memsz
, is_pic
,
776 debug_info
->comp
->arg_debug_dir
,
777 debug_info
->comp
->arg_target_prefix
,
778 debug_info
->log_level
, debug_info
->self_comp
);
783 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
, key
, bin
);
784 /* Ownership passed to ht. */
793 void handle_event_statedump_bin_info(struct debug_info
*debug_info
,
794 const bt_event
*event
)
796 handle_bin_info_event(debug_info
, event
, true);
800 void handle_event_lib_load(struct debug_info
*debug_info
,
801 const bt_event
*event
)
803 handle_bin_info_event(debug_info
, event
, false);
807 void handle_event_lib_unload(struct debug_info
*debug_info
,
808 const bt_event
*event
)
811 struct proc_debug_info_sources
*proc_dbg_info_src
;
815 event_get_payload_unsigned_integer_field_value(event
, BADDR_FIELD_NAME
,
818 event_get_common_context_signed_integer_field_value(event
,
819 VPID_FIELD_NAME
, &vpid
);
821 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
822 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
823 if (!proc_dbg_info_src
) {
825 * It's an unload event for a library for which no load event
826 * was previously received.
831 ret
= g_hash_table_remove(proc_dbg_info_src
->baddr_to_bin_info
,
839 void handle_event_statedump_start(struct debug_info
*debug_info
,
840 const bt_event
*event
)
842 struct proc_debug_info_sources
*proc_dbg_info_src
;
845 event_get_common_context_signed_integer_field_value(
846 event
, VPID_FIELD_NAME
, &vpid
);
848 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
849 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
850 if (!proc_dbg_info_src
) {
854 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
855 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
861 void trace_debug_info_remove_func(const bt_trace
*in_trace
, void *data
)
863 struct debug_info_msg_iter
*debug_it
= data
;
864 if (debug_it
->debug_info_map
) {
866 ret
= g_hash_table_remove(debug_it
->debug_info_map
,
867 (gpointer
) in_trace
);
873 void handle_event_statedump(struct debug_info_msg_iter
*debug_it
,
874 const bt_event
*event
)
876 const bt_event_class
*event_class
;
877 const char *event_name
;
879 const bt_trace
*trace
;
880 struct debug_info
*debug_info
;
885 event_class
= bt_event_borrow_class_const(event
);
887 event_name
= bt_event_class_get_name(event_class
);
889 trace
= bt_stream_borrow_trace_const(
890 bt_event_borrow_stream_const(event
));
892 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
, trace
);
894 bt_trace_add_listener_status add_listener_status
;
896 debug_info
= debug_info_create(debug_it
->debug_info_component
,
897 trace
, &debug_it
->fd_cache
);
898 g_hash_table_insert(debug_it
->debug_info_map
, (gpointer
) trace
,
900 add_listener_status
= bt_trace_add_destruction_listener(
901 trace
, trace_debug_info_remove_func
,
903 &debug_info
->destruction_listener_id
);
904 BT_ASSERT(add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
907 q_event_name
= g_quark_try_string(event_name
);
909 if (q_event_name
== debug_info
->q_statedump_bin_info
) {
911 handle_event_statedump_bin_info(debug_info
, event
);
912 } else if (q_event_name
== debug_info
->q_dl_open
||
913 q_event_name
== debug_info
->q_lib_load
) {
915 * dl_open and lib_load events are both checked for since
916 * only dl_open was produced as of lttng-ust 2.8.
918 * lib_load, which is produced from lttng-ust 2.9+, is a lot
919 * more reliable since it will be emitted when other functions
920 * of the dlopen family are called (e.g. dlmopen) and when
921 * library are transitively loaded.
923 handle_event_lib_load(debug_info
, event
);
924 } else if (q_event_name
== debug_info
->q_statedump_start
) {
925 /* Start state dump */
926 handle_event_statedump_start(debug_info
, event
);
927 } else if (q_event_name
== debug_info
->q_statedump_debug_link
) {
928 /* Debug link info */
929 handle_event_statedump_debug_link(debug_info
, event
);
930 } else if (q_event_name
== debug_info
->q_statedump_build_id
) {
932 handle_event_statedump_build_id(debug_info
, event
);
933 } else if (q_event_name
== debug_info
-> q_lib_unload
) {
934 handle_event_lib_unload(debug_info
, event
);
941 void destroy_debug_info_comp(struct debug_info_component
*debug_info
)
947 g_free(debug_info
->arg_debug_dir
);
948 g_free(debug_info
->arg_debug_info_field_name
);
949 g_free(debug_info
->arg_target_prefix
);
954 void fill_debug_info_bin_field(struct debug_info_source
*dbg_info_src
,
955 bool full_path
, bt_field
*curr_field
,
956 bt_logging_level log_level
, bt_self_component
*self_comp
)
958 bt_field_string_set_value_status set_status
;
959 bt_field_string_append_status append_status
;
961 BT_ASSERT(bt_field_get_class_type(curr_field
) ==
962 BT_FIELD_CLASS_TYPE_STRING
);
966 set_status
= bt_field_string_set_value(curr_field
,
967 dbg_info_src
->bin_path
);
969 set_status
= bt_field_string_set_value(curr_field
,
970 dbg_info_src
->short_bin_path
);
972 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
973 BT_COMP_LOGE("Cannot set path component of \"bin\" "
974 "curr_field field's value: str-fc-addr=%p",
976 bt_current_thread_clear_error();
979 append_status
= bt_field_string_append(curr_field
,
980 dbg_info_src
->bin_loc
);
981 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
982 BT_COMP_LOGE("Cannot set bin location component of \"bin\" "
983 "curr_field field's value: str-fc-addr=%p",
985 bt_current_thread_clear_error();
988 set_status
= bt_field_string_set_value(curr_field
, "");
989 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
990 BT_COMP_LOGE("Cannot set \"bin\" curr_field field's value: "
991 "str-fc-addr=%p", curr_field
);
992 bt_current_thread_clear_error();
998 void fill_debug_info_func_field(struct debug_info_source
*dbg_info_src
,
999 bt_field
*curr_field
, bt_logging_level log_level
,
1000 bt_self_component
*self_comp
)
1002 bt_field_string_set_value_status status
;
1004 BT_ASSERT(bt_field_get_class_type(curr_field
) ==
1005 BT_FIELD_CLASS_TYPE_STRING
);
1006 if (dbg_info_src
&& dbg_info_src
->func
) {
1007 status
= bt_field_string_set_value(curr_field
,
1008 dbg_info_src
->func
);
1010 status
= bt_field_string_set_value(curr_field
, "");
1012 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1013 BT_COMP_LOGE("Cannot set \"func\" curr_field field's value: "
1014 "str-fc-addr=%p", curr_field
);
1015 bt_current_thread_clear_error();
1020 void fill_debug_info_src_field(struct debug_info_source
*dbg_info_src
,
1021 bool full_path
, bt_field
*curr_field
,
1022 bt_logging_level log_level
,
1023 bt_self_component
*self_comp
)
1025 bt_field_string_set_value_status set_status
;
1026 bt_field_string_append_status append_status
;
1028 BT_ASSERT(bt_field_get_class_type(curr_field
) ==
1029 BT_FIELD_CLASS_TYPE_STRING
);
1031 if (dbg_info_src
&& dbg_info_src
->src_path
) {
1033 set_status
= bt_field_string_set_value(curr_field
,
1034 dbg_info_src
->src_path
);
1036 set_status
= bt_field_string_set_value(curr_field
,
1037 dbg_info_src
->short_src_path
);
1039 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1040 BT_COMP_LOGE("Cannot set path component of \"src\" "
1041 "curr_field field's value: str-fc-addr=%p",
1043 bt_current_thread_clear_error();
1046 append_status
= bt_field_string_append(curr_field
, ":");
1047 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
1048 BT_COMP_LOGE("Cannot set colon component of \"src\" "
1049 "curr_field field's value: str-fc-addr=%p",
1051 bt_current_thread_clear_error();
1054 append_status
= bt_field_string_append(curr_field
,
1055 dbg_info_src
->line_no
);
1056 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
1057 BT_COMP_LOGE("Cannot set line number component of \"src\" "
1058 "curr_field field's value: str-fc-addr=%p",
1060 bt_current_thread_clear_error();
1063 set_status
= bt_field_string_set_value(curr_field
, "");
1064 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1065 BT_COMP_LOGE("Cannot set \"src\" curr_field field's value: "
1066 "str-fc-addr=%p", curr_field
);
1067 bt_current_thread_clear_error();
1073 void fill_debug_info_field_empty(bt_field
*debug_info_field
,
1074 bt_logging_level log_level
, bt_self_component
*self_comp
)
1076 bt_field_string_set_value_status status
;
1077 bt_field
*bin_field
, *func_field
, *src_field
;
1079 BT_ASSERT(bt_field_get_class_type(debug_info_field
) ==
1080 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1082 bin_field
= bt_field_structure_borrow_member_field_by_name(
1083 debug_info_field
, "bin");
1084 func_field
= bt_field_structure_borrow_member_field_by_name(
1085 debug_info_field
, "func");
1086 src_field
= bt_field_structure_borrow_member_field_by_name(
1087 debug_info_field
, "src");
1089 BT_ASSERT(bt_field_get_class_type(bin_field
) ==
1090 BT_FIELD_CLASS_TYPE_STRING
);
1091 BT_ASSERT(bt_field_get_class_type(func_field
) ==
1092 BT_FIELD_CLASS_TYPE_STRING
);
1093 BT_ASSERT(bt_field_get_class_type(src_field
) ==
1094 BT_FIELD_CLASS_TYPE_STRING
);
1096 status
= bt_field_string_set_value(bin_field
, "");
1097 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1098 BT_COMP_LOGE("Cannot set \"bin\" bin_field field's value: "
1099 "str-fc-addr=%p", bin_field
);
1100 bt_current_thread_clear_error();
1103 status
= bt_field_string_set_value(func_field
, "");
1104 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1105 BT_COMP_LOGE("Cannot set \"func\" func_field field's value: "
1106 "str-fc-addr=%p", func_field
);
1107 bt_current_thread_clear_error();
1110 status
= bt_field_string_set_value(src_field
, "");
1111 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1112 BT_COMP_LOGE("Cannot set \"src\" src_field field's value: "
1113 "str-fc-addr=%p", src_field
);
1114 bt_current_thread_clear_error();
1118 void fill_debug_info_field(struct debug_info
*debug_info
, int64_t vpid
,
1119 uint64_t ip
, bt_field
*debug_info_field
)
1121 struct debug_info_source
*dbg_info_src
;
1122 const bt_field_class
*debug_info_fc
;
1124 BT_ASSERT(bt_field_get_class_type(debug_info_field
) ==
1125 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1127 debug_info_fc
= bt_field_borrow_class_const(debug_info_field
);
1129 BT_ASSERT(bt_field_class_structure_get_member_count(debug_info_fc
) == 3);
1131 dbg_info_src
= debug_info_query(debug_info
, vpid
, ip
);
1133 fill_debug_info_bin_field(dbg_info_src
,
1134 debug_info
->comp
->arg_full_path
,
1135 bt_field_structure_borrow_member_field_by_name(
1136 debug_info_field
, "bin"),
1137 debug_info
->log_level
, debug_info
->self_comp
);
1138 fill_debug_info_func_field(dbg_info_src
,
1139 bt_field_structure_borrow_member_field_by_name(
1140 debug_info_field
, "func"),
1141 debug_info
->log_level
, debug_info
->self_comp
);
1142 fill_debug_info_src_field(dbg_info_src
,
1143 debug_info
->comp
->arg_full_path
,
1144 bt_field_structure_borrow_member_field_by_name(
1145 debug_info_field
, "src"),
1146 debug_info
->log_level
, debug_info
->self_comp
);
1150 void fill_debug_info_event_if_needed(struct debug_info_msg_iter
*debug_it
,
1151 const bt_event
*in_event
, bt_event
*out_event
)
1153 bt_field
*out_common_ctx_field
, *out_debug_info_field
;
1154 const bt_field
*vpid_field
, *ip_field
, *in_common_ctx_field
;
1155 const bt_field_class
*in_common_ctx_fc
;
1156 struct debug_info
*debug_info
;
1159 gchar
*debug_info_field_name
=
1160 debug_it
->debug_info_component
->arg_debug_info_field_name
;
1161 bt_logging_level log_level
= debug_it
->log_level
;
1162 bt_self_component
*self_comp
= debug_it
->self_comp
;
1164 in_common_ctx_field
= bt_event_borrow_common_context_field_const(
1166 if (!in_common_ctx_field
) {
1168 * There is no event common context so no need to add debug
1174 in_common_ctx_fc
= bt_field_borrow_class_const(in_common_ctx_field
);
1175 if (!is_event_common_ctx_dbg_info_compatible(in_common_ctx_fc
,
1176 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1178 * The input event common context does not have the necessary
1179 * fields to resolve debug information.
1184 /* Borrow the debug-info field. */
1185 out_common_ctx_field
= bt_event_borrow_common_context_field(out_event
);
1186 if (!out_common_ctx_field
) {
1190 out_debug_info_field
= bt_field_structure_borrow_member_field_by_name(
1191 out_common_ctx_field
, debug_info_field_name
);
1193 vpid_field
= bt_field_structure_borrow_member_field_by_name_const(
1194 out_common_ctx_field
, VPID_FIELD_NAME
);
1195 ip_field
= bt_field_structure_borrow_member_field_by_name_const(
1196 out_common_ctx_field
, IP_FIELD_NAME
);
1198 vpid
= bt_field_signed_integer_get_value(vpid_field
);
1199 ip
= bt_field_unsigned_integer_get_value(ip_field
);
1202 * Borrow the debug_info structure needed for the source
1205 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
,
1206 bt_stream_borrow_trace_const(
1207 bt_event_borrow_stream_const(in_event
)));
1211 * Perform the debug-info resolving and set the event fields
1214 fill_debug_info_field(debug_info
, vpid
, ip
, out_debug_info_field
);
1216 BT_COMP_LOGD("No debug information for this trace. Setting debug "
1217 "info fields to empty strings.");
1218 fill_debug_info_field_empty(out_debug_info_field
,
1219 log_level
, self_comp
);
1226 void update_event_statedump_if_needed(struct debug_info_msg_iter
*debug_it
,
1227 const bt_event
*in_event
)
1229 const bt_field
*event_common_ctx
;
1230 const bt_field_class
*event_common_ctx_fc
;
1231 const bt_event_class
*in_event_class
= bt_event_borrow_class_const(in_event
);
1234 * If the event is an lttng_ust_statedump event AND has the right event
1235 * common context fields update the debug-info view for this process.
1237 event_common_ctx
= bt_event_borrow_common_context_field_const(in_event
);
1238 if (!event_common_ctx
) {
1242 event_common_ctx_fc
= bt_field_borrow_class_const(event_common_ctx
);
1243 if (is_event_common_ctx_dbg_info_compatible(event_common_ctx_fc
,
1244 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1245 /* Checkout if it might be a one of lttng ust statedump events. */
1246 const char *in_event_name
= bt_event_class_get_name(in_event_class
);
1247 if (strncmp(in_event_name
, LTTNG_UST_STATEDUMP_PREFIX
,
1248 strlen(LTTNG_UST_STATEDUMP_PREFIX
)) == 0) {
1249 /* Handle statedump events. */
1250 handle_event_statedump(debug_it
, in_event
);
1258 bt_message
*handle_event_message(struct debug_info_msg_iter
*debug_it
,
1259 const bt_message
*in_message
)
1261 const bt_clock_snapshot
*cs
;
1262 const bt_clock_class
*default_cc
;
1263 const bt_packet
*in_packet
;
1264 bt_event_class
*out_event_class
;
1265 bt_packet
*out_packet
;
1266 bt_event
*out_event
;
1267 bt_logging_level log_level
= debug_it
->log_level
;
1268 bt_self_component
*self_comp
= debug_it
->self_comp
;
1270 bt_message
*out_message
= NULL
;
1272 /* Borrow the input event and its event class. */
1273 const bt_event
*in_event
=
1274 bt_message_event_borrow_event_const(in_message
);
1275 const bt_event_class
*in_event_class
=
1276 bt_event_borrow_class_const(in_event
);
1278 update_event_statedump_if_needed(debug_it
, in_event
);
1280 out_event_class
= trace_ir_mapping_borrow_mapped_event_class(
1281 debug_it
->ir_maps
, in_event_class
);
1282 if (!out_event_class
) {
1283 out_event_class
= trace_ir_mapping_create_new_mapped_event_class(
1284 debug_it
->ir_maps
, in_event_class
);
1286 BT_ASSERT(out_event_class
);
1288 /* Borrow the input and output packets. */
1289 in_packet
= bt_event_borrow_packet_const(in_event
);
1290 out_packet
= trace_ir_mapping_borrow_mapped_packet(debug_it
->ir_maps
,
1293 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1294 bt_event_class_borrow_stream_class_const(in_event_class
));
1296 /* Borrow event clock snapshot. */
1297 cs
= bt_message_event_borrow_default_clock_snapshot_const(
1300 /* Create an output event message. */
1301 out_message
= bt_message_event_create_with_default_clock_snapshot(
1302 debug_it
->input_iterator
,
1303 out_event_class
, out_packet
,
1304 bt_clock_snapshot_get_value(cs
));
1306 out_message
= bt_message_event_create(debug_it
->input_iterator
,
1307 out_event_class
, out_packet
);
1311 BT_COMP_LOGE("Error creating output event message.");
1315 out_event
= bt_message_event_borrow_event(out_message
);
1317 /* Copy the original fields to the output event. */
1318 copy_event_content(in_event
, out_event
, log_level
, self_comp
);
1321 * Try to set the debug-info fields based on debug information that is
1324 fill_debug_info_event_if_needed(debug_it
, in_event
, out_event
);
1331 bt_message
*handle_stream_begin_message(struct debug_info_msg_iter
*debug_it
,
1332 const bt_message
*in_message
)
1334 const bt_stream
*in_stream
;
1335 bt_message
*out_message
;
1336 bt_stream
*out_stream
;
1337 bt_logging_level log_level
= debug_it
->log_level
;
1338 bt_self_component
*self_comp
= debug_it
->self_comp
;
1340 in_stream
= bt_message_stream_beginning_borrow_stream_const(in_message
);
1341 BT_ASSERT(in_stream
);
1343 /* Create a duplicated output stream. */
1344 out_stream
= trace_ir_mapping_create_new_mapped_stream(
1345 debug_it
->ir_maps
, in_stream
);
1351 /* Create an output stream beginning message. */
1352 out_message
= bt_message_stream_beginning_create(
1353 debug_it
->input_iterator
, out_stream
);
1355 BT_COMP_LOGE("Error creating output stream beginning message: "
1356 "out-s-addr=%p", out_stream
);
1363 bt_message
*handle_stream_end_message(struct debug_info_msg_iter
*debug_it
,
1364 const bt_message
*in_message
)
1366 const bt_stream
*in_stream
;
1367 bt_message
*out_message
= NULL
;
1368 bt_stream
*out_stream
;
1369 bt_logging_level log_level
= debug_it
->log_level
;
1370 bt_self_component
*self_comp
= debug_it
->self_comp
;
1372 in_stream
= bt_message_stream_end_borrow_stream_const(in_message
);
1373 BT_ASSERT(in_stream
);
1375 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1376 debug_it
->ir_maps
, in_stream
);
1377 BT_ASSERT(out_stream
);
1379 /* Create an output stream end message. */
1380 out_message
= bt_message_stream_end_create(debug_it
->input_iterator
,
1383 BT_COMP_LOGE("Error creating output stream end message: out-s-addr=%p",
1388 /* Remove stream from trace mapping hashtable. */
1389 trace_ir_mapping_remove_mapped_stream(debug_it
->ir_maps
, in_stream
);
1396 bt_message
*handle_packet_begin_message(struct debug_info_msg_iter
*debug_it
,
1397 const bt_message
*in_message
)
1399 bool has_default_clock_snapshot
;
1400 const bt_clock_snapshot
*cs
;
1401 bt_message
*out_message
= NULL
;
1402 bt_packet
*out_packet
;
1403 bt_logging_level log_level
= debug_it
->log_level
;
1404 bt_self_component
*self_comp
= debug_it
->self_comp
;
1406 const bt_packet
*in_packet
=
1407 bt_message_packet_beginning_borrow_packet_const(in_message
);
1408 BT_ASSERT(in_packet
);
1410 /* This packet should not be already mapped. */
1411 BT_ASSERT(!trace_ir_mapping_borrow_mapped_packet(
1412 debug_it
->ir_maps
, in_packet
));
1414 out_packet
= trace_ir_mapping_create_new_mapped_packet(debug_it
->ir_maps
,
1417 BT_ASSERT(out_packet
);
1419 has_default_clock_snapshot
=
1420 bt_stream_class_packets_have_beginning_default_clock_snapshot(
1421 bt_stream_borrow_class_const(
1422 bt_packet_borrow_stream_const(in_packet
)));
1423 if (has_default_clock_snapshot
) {
1424 /* Borrow clock snapshot. */
1425 cs
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
1428 /* Create an output packet beginning message. */
1429 out_message
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1430 debug_it
->input_iterator
, out_packet
,
1431 bt_clock_snapshot_get_value(cs
));
1433 out_message
= bt_message_packet_beginning_create(
1434 debug_it
->input_iterator
, out_packet
);
1437 BT_COMP_LOGE("Error creating output packet beginning message: "
1438 "out-p-addr=%p", out_packet
);
1445 bt_message
*handle_packet_end_message(struct debug_info_msg_iter
*debug_it
,
1446 const bt_message
*in_message
)
1448 bool has_default_clock_snapshot
;
1449 const bt_clock_snapshot
*cs
;
1450 const bt_packet
*in_packet
;
1451 bt_message
*out_message
= NULL
;
1452 bt_packet
*out_packet
;
1453 bt_logging_level log_level
= debug_it
->log_level
;
1454 bt_self_component
*self_comp
= debug_it
->self_comp
;
1456 in_packet
= bt_message_packet_end_borrow_packet_const(in_message
);
1457 BT_ASSERT(in_packet
);
1459 out_packet
= trace_ir_mapping_borrow_mapped_packet(debug_it
->ir_maps
, in_packet
);
1460 BT_ASSERT(out_packet
);
1462 has_default_clock_snapshot
=
1463 bt_stream_class_packets_have_end_default_clock_snapshot(
1464 bt_stream_borrow_class_const(
1465 bt_packet_borrow_stream_const(in_packet
)));
1466 if (has_default_clock_snapshot
) {
1467 /* Borrow clock snapshot. */
1468 cs
= bt_message_packet_end_borrow_default_clock_snapshot_const(
1471 /* Create an outpute packet end message. */
1472 out_message
= bt_message_packet_end_create_with_default_clock_snapshot(
1473 debug_it
->input_iterator
, out_packet
,
1474 bt_clock_snapshot_get_value(cs
));
1476 out_message
= bt_message_packet_end_create(
1477 debug_it
->input_iterator
, out_packet
);
1481 BT_COMP_LOGE("Error creating output packet end message: "
1482 "out-p-addr=%p", out_packet
);
1486 /* Remove packet from data mapping hashtable. */
1487 trace_ir_mapping_remove_mapped_packet(debug_it
->ir_maps
, in_packet
);
1494 bt_message
*handle_msg_iterator_inactivity(struct debug_info_msg_iter
*debug_it
,
1495 const bt_message
*in_message
)
1498 * This message type can be forwarded directly because it does
1499 * not refer to any objects in the trace class.
1501 bt_message_get_ref(in_message
);
1502 return (bt_message
*) in_message
;
1506 bt_message
*handle_stream_act_begin_message(struct debug_info_msg_iter
*debug_it
,
1507 const bt_message
*in_message
)
1509 const bt_clock_snapshot
*cs
;
1510 const bt_clock_class
*default_cc
;
1511 bt_message
*out_message
= NULL
;
1512 bt_stream
*out_stream
;
1514 bt_message_stream_activity_clock_snapshot_state cs_state
;
1515 bt_logging_level log_level
= debug_it
->log_level
;
1516 bt_self_component
*self_comp
= debug_it
->self_comp
;
1518 const bt_stream
*in_stream
=
1519 bt_message_stream_activity_beginning_borrow_stream_const(
1521 BT_ASSERT(in_stream
);
1523 out_stream
= trace_ir_mapping_borrow_mapped_stream(debug_it
->ir_maps
,
1525 BT_ASSERT(out_stream
);
1527 out_message
= bt_message_stream_activity_beginning_create(
1528 debug_it
->input_iterator
, out_stream
);
1530 BT_COMP_LOGE("Error creating output stream activity beginning "
1531 "message: out-s-addr=%p", out_stream
);
1535 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1536 bt_stream_borrow_class_const(in_stream
));
1538 /* Borrow clock snapshot. */
1540 bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
1543 if (cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1544 cs_value
= bt_clock_snapshot_get_value(cs
);
1545 bt_message_stream_activity_beginning_set_default_clock_snapshot(
1546 out_message
, cs_value
);
1548 bt_message_stream_activity_beginning_set_default_clock_snapshot_state(
1549 out_message
, cs_state
);
1558 bt_message
*handle_stream_act_end_message(struct debug_info_msg_iter
*debug_it
,
1559 const bt_message
*in_message
)
1561 const bt_clock_snapshot
*cs
;
1562 const bt_clock_class
*default_cc
;
1563 const bt_stream
*in_stream
;
1564 bt_message
*out_message
;
1565 bt_stream
*out_stream
;
1567 bt_message_stream_activity_clock_snapshot_state cs_state
;
1568 bt_logging_level log_level
= debug_it
->log_level
;
1569 bt_self_component
*self_comp
= debug_it
->self_comp
;
1571 in_stream
= bt_message_stream_activity_end_borrow_stream_const(
1573 BT_ASSERT(in_stream
);
1575 out_stream
= trace_ir_mapping_borrow_mapped_stream(debug_it
->ir_maps
,
1577 BT_ASSERT(out_stream
);
1579 out_message
= bt_message_stream_activity_end_create(
1580 debug_it
->input_iterator
, out_stream
);
1582 BT_COMP_LOGE("Error creating output stream activity end message: "
1583 "out-s-addr=%p", out_stream
);
1587 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1588 bt_stream_borrow_class_const(in_stream
));
1592 bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
1595 if (cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1596 cs_value
= bt_clock_snapshot_get_value(cs
);
1597 bt_message_stream_activity_end_set_default_clock_snapshot(
1598 out_message
, cs_value
);
1600 bt_message_stream_activity_end_set_default_clock_snapshot_state(
1601 out_message
, cs_state
);
1610 bt_message
*handle_discarded_events_message(struct debug_info_msg_iter
*debug_it
,
1611 const bt_message
*in_message
)
1613 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1614 const bt_stream
*in_stream
;
1615 bool has_default_clock_snapshots
;
1616 uint64_t discarded_events
, begin_cs_value
, end_cs_value
;
1617 bt_property_availability prop_avail
;
1618 bt_message
*out_message
= NULL
;
1619 bt_stream
*out_stream
;
1620 bt_logging_level log_level
= debug_it
->log_level
;
1621 bt_self_component
*self_comp
= debug_it
->self_comp
;
1623 in_stream
= bt_message_discarded_events_borrow_stream_const(
1625 BT_ASSERT(in_stream
);
1627 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1628 debug_it
->ir_maps
, in_stream
);
1629 BT_ASSERT(out_stream
);
1631 has_default_clock_snapshots
=
1632 bt_stream_class_discarded_events_have_default_clock_snapshots(
1633 bt_stream_borrow_class_const(in_stream
));
1634 if (has_default_clock_snapshots
) {
1636 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1639 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1642 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1643 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1646 bt_message_discarded_events_create_with_default_clock_snapshots(
1647 debug_it
->input_iterator
, out_stream
,
1648 begin_cs_value
, end_cs_value
);
1650 out_message
= bt_message_discarded_events_create(
1651 debug_it
->input_iterator
, out_stream
);
1654 BT_COMP_LOGE("Error creating output discarded events message: "
1655 "out-s-addr=%p", out_stream
);
1659 prop_avail
= bt_message_discarded_events_get_count(in_message
,
1662 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1663 bt_message_discarded_events_set_count(out_message
,
1672 bt_message
*handle_discarded_packets_message(struct debug_info_msg_iter
*debug_it
,
1673 const bt_message
*in_message
)
1675 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1676 bool has_default_clock_snapshots
;
1677 const bt_stream
*in_stream
;
1678 uint64_t discarded_packets
, begin_cs_value
, end_cs_value
;
1679 bt_property_availability prop_avail
;
1680 bt_message
*out_message
= NULL
;
1681 bt_stream
*out_stream
;
1682 bt_logging_level log_level
= debug_it
->log_level
;
1683 bt_self_component
*self_comp
= debug_it
->self_comp
;
1685 in_stream
= bt_message_discarded_packets_borrow_stream_const(
1687 BT_ASSERT(in_stream
);
1689 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1690 debug_it
->ir_maps
, in_stream
);
1691 BT_ASSERT(out_stream
);
1693 has_default_clock_snapshots
=
1694 bt_stream_class_discarded_packets_have_default_clock_snapshots(
1695 bt_stream_borrow_class_const(in_stream
));
1696 if (has_default_clock_snapshots
) {
1698 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1702 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1705 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1706 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1708 out_message
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1709 debug_it
->input_iterator
, out_stream
,
1710 begin_cs_value
, end_cs_value
);
1712 out_message
= bt_message_discarded_packets_create(
1713 debug_it
->input_iterator
, out_stream
);
1716 BT_COMP_LOGE("Error creating output discarded packet message: "
1717 "out-s-addr=%p", out_stream
);
1721 prop_avail
= bt_message_discarded_packets_get_count(in_message
,
1722 &discarded_packets
);
1723 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1724 bt_message_discarded_packets_set_count(out_message
,
1733 const bt_message
*handle_message(struct debug_info_msg_iter
*debug_it
,
1734 const bt_message
*in_message
)
1736 bt_message
*out_message
= NULL
;
1738 switch (bt_message_get_type(in_message
)) {
1739 case BT_MESSAGE_TYPE_EVENT
:
1740 out_message
= handle_event_message(debug_it
,
1743 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1744 out_message
= handle_packet_begin_message(debug_it
,
1747 case BT_MESSAGE_TYPE_PACKET_END
:
1748 out_message
= handle_packet_end_message(debug_it
,
1751 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1752 out_message
= handle_stream_begin_message(debug_it
,
1755 case BT_MESSAGE_TYPE_STREAM_END
:
1756 out_message
= handle_stream_end_message(debug_it
,
1759 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1760 out_message
= handle_msg_iterator_inactivity(debug_it
,
1763 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1764 out_message
= handle_stream_act_begin_message(debug_it
,
1767 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1768 out_message
= handle_stream_act_end_message(debug_it
,
1771 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1772 out_message
= handle_discarded_events_message(debug_it
,
1775 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1776 out_message
= handle_discarded_packets_message(debug_it
,
1788 int init_from_params(struct debug_info_component
*debug_info_component
,
1789 const bt_value
*params
)
1791 const bt_value
*value
= NULL
;
1796 value
= bt_value_map_borrow_entry_value_const(params
,
1797 "debug-info-field-name");
1799 debug_info_component
->arg_debug_info_field_name
=
1800 g_strdup(bt_value_string_get(value
));
1802 debug_info_component
->arg_debug_info_field_name
=
1803 g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME
);
1806 value
= bt_value_map_borrow_entry_value_const(params
, "debug-info-dir");
1808 debug_info_component
->arg_debug_dir
=
1809 g_strdup(bt_value_string_get(value
));
1811 debug_info_component
->arg_debug_dir
= NULL
;
1815 value
= bt_value_map_borrow_entry_value_const(params
, "target-prefix");
1817 debug_info_component
->arg_target_prefix
=
1818 g_strdup(bt_value_string_get(value
));
1820 debug_info_component
->arg_target_prefix
= NULL
;
1823 value
= bt_value_map_borrow_entry_value_const(params
, "full-path");
1825 debug_info_component
->arg_full_path
= bt_value_bool_get(value
);
1827 debug_info_component
->arg_full_path
= BT_FALSE
;
1834 bt_component_class_init_method_status
debug_info_comp_init(
1835 bt_self_component_filter
*self_comp_flt
,
1836 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
1839 struct debug_info_component
*debug_info_comp
;
1840 bt_component_class_init_method_status status
=
1841 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
1842 bt_self_component_add_port_status add_port_status
;
1843 bt_self_component
*self_comp
=
1844 bt_self_component_filter_as_self_component(self_comp_flt
);
1845 bt_logging_level log_level
= bt_component_get_logging_level(
1846 bt_self_component_as_component(self_comp
));
1849 BT_COMP_LOGI("Initializing debug_info component: "
1850 "comp-addr=%p, params-addr=%p", self_comp
, params
);
1852 debug_info_comp
= g_new0(struct debug_info_component
, 1);
1853 if (!debug_info_comp
) {
1854 BT_COMP_LOGE_STR("Failed to allocate one debug_info component.");
1858 debug_info_comp
->log_level
= log_level
;
1859 debug_info_comp
->self_comp
= self_comp
;
1860 bt_self_component_set_data(self_comp
, debug_info_comp
);
1862 add_port_status
= bt_self_component_filter_add_input_port(
1863 self_comp_flt
, "in", NULL
, NULL
);
1864 switch (add_port_status
) {
1865 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
1866 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1868 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
1869 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1875 add_port_status
= bt_self_component_filter_add_output_port(
1876 self_comp_flt
, "out", NULL
, NULL
);
1877 switch (add_port_status
) {
1878 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
1879 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1881 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
1882 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1888 ret
= init_from_params(debug_info_comp
, params
);
1890 BT_COMP_LOGE("Cannot configure debug_info component: "
1891 "debug_info-comp-addr=%p, params-addr=%p",
1892 debug_info_comp
, params
);
1899 destroy_debug_info_comp(debug_info_comp
);
1900 bt_self_component_set_data(self_comp
, NULL
);
1902 if (status
== BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
) {
1903 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1910 void debug_info_comp_finalize(bt_self_component_filter
*self_comp_flt
)
1912 struct debug_info_component
*debug_info
=
1913 bt_self_component_get_data(
1914 bt_self_component_filter_as_self_component(
1916 bt_logging_level log_level
= debug_info
->log_level
;
1917 bt_self_component
*self_comp
= debug_info
->self_comp
;
1919 BT_COMP_LOGI("Finalizing debug_info self_component: comp-addr=%p",
1922 destroy_debug_info_comp(debug_info
);
1926 bt_component_class_message_iterator_next_method_status
debug_info_msg_iter_next(
1927 bt_self_message_iterator
*self_msg_iter
,
1928 const bt_message_array_const msgs
, uint64_t capacity
,
1931 bt_self_component_port_input_message_iterator
*upstream_iterator
= NULL
;
1932 bt_message_iterator_next_status upstream_iterator_ret_status
;
1933 struct debug_info_msg_iter
*debug_info_msg_iter
;
1934 struct debug_info_component
*debug_info
= NULL
;
1935 bt_component_class_message_iterator_next_method_status status
;
1936 bt_self_component
*self_comp
= NULL
;
1937 bt_message_array_const input_msgs
;
1938 const bt_message
*out_message
;
1939 uint64_t curr_msg_idx
, i
;
1941 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1943 self_comp
= bt_self_message_iterator_borrow_component(self_msg_iter
);
1944 BT_ASSERT(self_comp
);
1946 debug_info
= bt_self_component_get_data(self_comp
);
1947 BT_ASSERT(debug_info
);
1949 debug_info_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
1950 BT_ASSERT(debug_info_msg_iter
);
1952 upstream_iterator
= debug_info_msg_iter
->msg_iter
;
1953 BT_ASSERT(upstream_iterator
);
1955 upstream_iterator_ret_status
=
1956 bt_self_component_port_input_message_iterator_next(
1957 upstream_iterator
, &input_msgs
, count
);
1958 if (upstream_iterator_ret_status
!=
1959 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
1961 * No messages were returned. Not necessarily an error.
1962 * Convert the upstream message iterator status to a
1965 status
= (int) upstream_iterator_ret_status
;
1970 * There should never be more received messages than the capacity we
1973 BT_ASSERT(*count
<= capacity
);
1975 for (curr_msg_idx
= 0; curr_msg_idx
< *count
; curr_msg_idx
++) {
1976 out_message
= handle_message(debug_info_msg_iter
,
1977 input_msgs
[curr_msg_idx
]);
1979 goto handle_msg_error
;
1982 msgs
[curr_msg_idx
] = out_message
;
1984 * Drop our reference of the input message as we are done with
1985 * it and created a output copy.
1987 bt_message_put_ref(input_msgs
[curr_msg_idx
]);
1994 * Drop references of all the output messages created before the
1997 for (i
= 0; i
< curr_msg_idx
; i
++) {
1998 bt_message_put_ref(msgs
[i
]);
2001 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
2008 void debug_info_msg_iter_destroy(struct debug_info_msg_iter
*debug_info_msg_iter
)
2010 if (!debug_info_msg_iter
) {
2014 if (debug_info_msg_iter
->msg_iter
) {
2015 bt_self_component_port_input_message_iterator_put_ref(
2016 debug_info_msg_iter
->msg_iter
);
2019 if (debug_info_msg_iter
->ir_maps
) {
2020 trace_ir_maps_destroy(debug_info_msg_iter
->ir_maps
);
2023 if (debug_info_msg_iter
->debug_info_map
) {
2024 g_hash_table_destroy(debug_info_msg_iter
->debug_info_map
);
2027 bt_fd_cache_fini(&debug_info_msg_iter
->fd_cache
);
2028 g_free(debug_info_msg_iter
);
2035 bt_component_class_message_iterator_init_method_status
debug_info_msg_iter_init(
2036 bt_self_message_iterator
*self_msg_iter
,
2037 bt_self_component_filter
*self_comp_flt
,
2038 bt_self_component_port_output
*self_port
)
2040 bt_component_class_message_iterator_init_method_status status
=
2041 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
2042 struct bt_self_component_port_input
*input_port
= NULL
;
2043 bt_self_component_port_input_message_iterator
*upstream_iterator
= NULL
;
2044 struct debug_info_msg_iter
*debug_info_msg_iter
= NULL
;
2045 gchar
*debug_info_field_name
;
2047 bt_self_component
*self_comp
=
2048 bt_self_component_filter_as_self_component(self_comp_flt
);
2049 bt_logging_level log_level
= bt_component_get_logging_level(
2050 bt_self_component_as_component(self_comp
));
2052 /* Borrow the upstream input port. */
2053 input_port
= bt_self_component_filter_borrow_input_port_by_name(
2054 self_comp_flt
, "in");
2056 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
2060 debug_info_msg_iter
= g_new0(struct debug_info_msg_iter
, 1);
2061 if (!debug_info_msg_iter
) {
2062 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
2066 debug_info_msg_iter
->log_level
= log_level
;
2067 debug_info_msg_iter
->self_comp
= self_comp
;
2069 /* Create an iterator on the upstream component. */
2070 upstream_iterator
= bt_self_component_port_input_message_iterator_create(
2072 if (!upstream_iterator
) {
2073 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
2077 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
2078 debug_info_msg_iter
->msg_iter
, upstream_iterator
);
2080 /* Create hashtable that will contain debug info mapping. */
2081 debug_info_msg_iter
->debug_info_map
= g_hash_table_new_full(
2082 g_direct_hash
, g_direct_equal
, (GDestroyNotify
) NULL
,
2083 (GDestroyNotify
) debug_info_destroy
);
2084 if (!debug_info_msg_iter
->debug_info_map
) {
2085 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
2089 debug_info_msg_iter
->debug_info_component
=
2090 bt_self_component_get_data(self_comp
);
2092 debug_info_field_name
=
2093 debug_info_msg_iter
->debug_info_component
->arg_debug_info_field_name
;
2095 debug_info_msg_iter
->ir_maps
= trace_ir_maps_create(self_comp
,
2096 debug_info_field_name
, log_level
);
2097 if (!debug_info_msg_iter
->ir_maps
) {
2098 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
2102 ret
= bt_fd_cache_init(&debug_info_msg_iter
->fd_cache
, log_level
);
2104 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
2108 bt_self_message_iterator_set_data(self_msg_iter
, debug_info_msg_iter
);
2109 debug_info_msg_iter
->input_iterator
= self_msg_iter
;
2114 debug_info_msg_iter_destroy(debug_info_msg_iter
);
2121 bt_bool
debug_info_msg_iter_can_seek_beginning(
2122 bt_self_message_iterator
*self_msg_iter
)
2124 struct debug_info_msg_iter
*debug_info_msg_iter
=
2125 bt_self_message_iterator_get_data(self_msg_iter
);
2126 BT_ASSERT(debug_info_msg_iter
);
2128 return bt_self_component_port_input_message_iterator_can_seek_beginning(
2129 debug_info_msg_iter
->msg_iter
);
2133 bt_component_class_message_iterator_seek_beginning_method_status
debug_info_msg_iter_seek_beginning(
2134 bt_self_message_iterator
*self_msg_iter
)
2136 struct debug_info_msg_iter
*debug_info_msg_iter
=
2137 bt_self_message_iterator_get_data(self_msg_iter
);
2138 bt_component_class_message_iterator_seek_beginning_method_status status
=
2139 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
2140 bt_message_iterator_seek_beginning_status seek_beg_status
;
2142 BT_ASSERT(debug_info_msg_iter
);
2144 /* Ask the upstream component to seek to the beginning. */
2145 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
2146 debug_info_msg_iter
->msg_iter
);
2147 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
2148 status
= (int) seek_beg_status
;
2152 /* Clear this iterator data. */
2153 trace_ir_maps_clear(debug_info_msg_iter
->ir_maps
);
2154 g_hash_table_remove_all(debug_info_msg_iter
->debug_info_map
);
2161 void debug_info_msg_iter_finalize(bt_self_message_iterator
*it
)
2163 struct debug_info_msg_iter
*debug_info_msg_iter
;
2165 debug_info_msg_iter
= bt_self_message_iterator_get_data(it
);
2166 BT_ASSERT(debug_info_msg_iter
);
2168 debug_info_msg_iter_destroy(debug_info_msg_iter
);