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 "logging/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"
46 #include "plugins/common/param-validation/param-validation.h"
48 #define DEFAULT_DEBUG_INFO_FIELD_NAME "debug_info"
49 #define LTTNG_UST_STATEDUMP_PREFIX "lttng_ust"
50 #define VPID_FIELD_NAME "vpid"
51 #define IP_FIELD_NAME "ip"
52 #define BADDR_FIELD_NAME "baddr"
53 #define CRC32_FIELD_NAME "crc"
54 #define BUILD_ID_FIELD_NAME "build_id"
55 #define FILENAME_FIELD_NAME "filename"
56 #define IS_PIC_FIELD_NAME "is_pic"
57 #define MEMSZ_FIELD_NAME "memsz"
58 #define PATH_FIELD_NAME "path"
60 struct debug_info_component
{
61 bt_logging_level log_level
;
62 bt_self_component
*self_comp
;
64 gchar
*arg_debug_info_field_name
;
65 gchar
*arg_target_prefix
;
66 bt_bool arg_full_path
;
69 struct debug_info_msg_iter
{
70 bt_logging_level log_level
;
71 struct debug_info_component
*debug_info_component
;
72 bt_self_message_iterator
*input_iterator
;
73 bt_self_component
*self_comp
;
74 bt_self_component_port_input_message_iterator
*msg_iter
;
76 struct trace_ir_maps
*ir_maps
;
77 /* in_trace -> debug_info_mapping. */
78 GHashTable
*debug_info_map
;
80 struct bt_fd_cache fd_cache
;
83 struct debug_info_source
{
84 /* Strings are owned by debug_info_source. */
87 * Store the line number as a string so that the allocation and
88 * conversion to string is only done once.
92 /* short_src_path points inside src_path, no need to free. */
93 const gchar
*short_src_path
;
95 /* short_bin_path points inside bin_path, no need to free. */
96 const gchar
*short_bin_path
;
98 * Location within the binary. Either absolute (@0x1234) or
104 struct proc_debug_info_sources
{
106 * Hash table: base address (pointer to uint64_t) to bin info; owned by
107 * proc_debug_info_sources.
109 GHashTable
*baddr_to_bin_info
;
112 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
113 * owned by proc_debug_info_sources.
115 GHashTable
*ip_to_debug_info_src
;
119 bt_logging_level log_level
;
120 bt_self_component
*self_comp
;
121 struct debug_info_component
*comp
;
122 const bt_trace
*input_trace
;
123 bt_listener_id destruction_listener_id
;
126 * Hash table of VPIDs (pointer to int64_t) to
127 * (struct proc_debug_info_sources*); owned by debug_info.
129 GHashTable
*vpid_to_proc_dbg_info_src
;
130 GQuark q_statedump_bin_info
;
131 GQuark q_statedump_debug_link
;
132 GQuark q_statedump_build_id
;
133 GQuark q_statedump_start
;
137 struct bt_fd_cache
*fd_cache
; /* Weak ref. Owned by the iterator. */
141 int debug_info_init(struct debug_info
*info
)
143 info
->q_statedump_bin_info
= g_quark_from_string(
144 "lttng_ust_statedump:bin_info");
145 info
->q_statedump_debug_link
= g_quark_from_string(
146 "lttng_ust_statedump:debug_link");
147 info
->q_statedump_build_id
= g_quark_from_string(
148 "lttng_ust_statedump:build_id");
149 info
->q_statedump_start
= g_quark_from_string(
150 "lttng_ust_statedump:start");
151 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
152 info
->q_lib_load
= g_quark_from_string("lttng_ust_lib:load");
153 info
->q_lib_unload
= g_quark_from_string("lttng_ust_lib:unload");
155 return bin_info_init(info
->log_level
, info
->self_comp
);
159 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
161 if (!debug_info_src
) {
165 g_free(debug_info_src
->func
);
166 g_free(debug_info_src
->line_no
);
167 g_free(debug_info_src
->src_path
);
168 g_free(debug_info_src
->bin_path
);
169 g_free(debug_info_src
->bin_loc
);
170 g_free(debug_info_src
);
174 struct debug_info_source
*debug_info_source_create_from_bin(
175 struct bin_info
*bin
, uint64_t ip
,
176 bt_self_component
*self_comp
)
179 struct debug_info_source
*debug_info_src
= NULL
;
180 struct source_location
*src_loc
= NULL
;
181 bt_logging_level log_level
= bin
->log_level
;
183 debug_info_src
= g_new0(struct debug_info_source
, 1);
185 if (!debug_info_src
) {
189 /* Lookup function name */
190 ret
= bin_info_lookup_function_name(bin
, ip
, &debug_info_src
->func
);
195 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
196 if (!bin
->is_elf_only
|| !debug_info_src
->func
) {
197 /* Lookup source location */
198 ret
= bin_info_lookup_source_location(bin
, ip
, &src_loc
);
200 BT_COMP_LOGI("Failed to lookup source location: ret=%d", ret
);
205 debug_info_src
->line_no
=
206 g_strdup_printf("%"PRId64
, src_loc
->line_no
);
207 if (!debug_info_src
->line_no
) {
208 BT_COMP_LOGE("Error occured when setting line_no field.");
212 if (src_loc
->filename
) {
213 debug_info_src
->src_path
= g_strdup(src_loc
->filename
);
214 if (!debug_info_src
->src_path
) {
218 debug_info_src
->short_src_path
= get_filename_from_path(
219 debug_info_src
->src_path
);
221 source_location_destroy(src_loc
);
225 debug_info_src
->bin_path
= g_strdup(bin
->elf_path
);
226 if (!debug_info_src
->bin_path
) {
230 debug_info_src
->short_bin_path
= get_filename_from_path(
231 debug_info_src
->bin_path
);
233 ret
= bin_info_get_bin_loc(bin
, ip
, &(debug_info_src
->bin_loc
));
240 return debug_info_src
;
243 debug_info_source_destroy(debug_info_src
);
248 void proc_debug_info_sources_destroy(
249 struct proc_debug_info_sources
*proc_dbg_info_src
)
251 if (!proc_dbg_info_src
) {
255 if (proc_dbg_info_src
->baddr_to_bin_info
) {
256 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_bin_info
);
259 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
260 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
263 g_free(proc_dbg_info_src
);
267 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
269 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
271 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
272 if (!proc_dbg_info_src
) {
276 proc_dbg_info_src
->baddr_to_bin_info
= g_hash_table_new_full(
277 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
278 (GDestroyNotify
) bin_info_destroy
);
279 if (!proc_dbg_info_src
->baddr_to_bin_info
) {
283 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
284 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
285 (GDestroyNotify
) debug_info_source_destroy
);
286 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
291 return proc_dbg_info_src
;
294 proc_debug_info_sources_destroy(proc_dbg_info_src
);
299 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
300 GHashTable
*ht
, int64_t vpid
)
302 gpointer key
= g_new0(int64_t, 1);
303 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
309 *((int64_t *) key
) = vpid
;
311 /* Exists? Return it */
312 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
313 if (proc_dbg_info_src
) {
317 /* Otherwise, create and return it */
318 proc_dbg_info_src
= proc_debug_info_sources_create();
319 if (!proc_dbg_info_src
) {
323 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
324 /* Ownership passed to ht */
328 return proc_dbg_info_src
;
332 const bt_field
*event_borrow_payload_field(const bt_event
*event
,
333 const char *field_name
)
335 const bt_field
*event_payload
, *field
;
337 event_payload
= bt_event_borrow_payload_field_const(event
);
338 BT_ASSERT_DBG(event_payload
);
340 field
= bt_field_structure_borrow_member_field_by_name_const(
341 event_payload
, field_name
);
346 const bt_field
*event_borrow_common_context_field(const bt_event
*event
,
347 const char *field_name
)
349 const bt_field
*event_common_ctx
, *field
= NULL
;
351 event_common_ctx
= bt_event_borrow_common_context_field_const(event
);
352 if (!event_common_ctx
) {
356 field
= bt_field_structure_borrow_member_field_by_name_const(
357 event_common_ctx
, field_name
);
364 void event_get_common_context_signed_integer_field_value(
365 const bt_event
*event
, const char *field_name
, int64_t *value
)
367 *value
= bt_field_integer_signed_get_value(
368 event_borrow_common_context_field(event
, field_name
));
372 void event_get_payload_build_id_length(const bt_event
*event
,
373 const char *field_name
, uint64_t *build_id_len
)
375 const bt_field
*build_id_field
;
376 const bt_field_class
*build_id_field_class
;
378 build_id_field
= event_borrow_payload_field(event
, field_name
);
379 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
381 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
382 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
||
383 bt_field_class_get_type(build_id_field_class
) ==
384 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
);
385 BT_ASSERT(bt_field_class_get_type(
386 bt_field_class_array_borrow_element_field_class_const(
387 build_id_field_class
)) ==
388 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
390 *build_id_len
= bt_field_array_get_length(build_id_field
);
394 void event_get_payload_build_id_value(const bt_event
*event
,
395 const char *field_name
, uint8_t *build_id
)
397 const bt_field
*curr_field
, *build_id_field
;
398 const bt_field_class
*build_id_field_class
;
399 uint64_t i
, build_id_len
;
401 build_id_field
= event_borrow_payload_field(event
, field_name
);
402 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
404 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
405 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
||
406 bt_field_class_get_type(build_id_field_class
) ==
407 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
);
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
++) {
416 curr_field
= bt_field_array_borrow_element_field_by_index_const(
419 build_id
[i
] = bt_field_integer_unsigned_get_value(curr_field
);
424 void event_get_payload_unsigned_integer_field_value(const bt_event
*event
,
425 const char *field_name
, uint64_t *value
)
427 *value
= bt_field_integer_unsigned_get_value(
428 event_borrow_payload_field(event
, field_name
));
432 void event_get_payload_string_field_value(const bt_event
*event
,
433 const char *field_name
, const char **value
)
435 *value
= bt_field_string_get_value(
436 event_borrow_payload_field(event
, field_name
));
440 bool event_has_payload_field(const bt_event
*event
,
441 const char *field_name
)
443 return event_borrow_payload_field(event
, field_name
);
447 struct debug_info_source
*proc_debug_info_sources_get_entry(
448 struct debug_info
*debug_info
,
449 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
451 struct debug_info_source
*debug_info_src
= NULL
;
452 gpointer key
= g_new0(uint64_t, 1);
454 gpointer baddr
, value
;
460 *((uint64_t *) key
) = ip
;
462 /* Look in IP to debug infos hash table first. */
463 debug_info_src
= g_hash_table_lookup(
464 proc_dbg_info_src
->ip_to_debug_info_src
, key
);
465 if (debug_info_src
) {
469 /* Check in all bin_infos. */
470 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_bin_info
);
472 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
474 struct bin_info
*bin
= value
;
476 if (!bin_info_has_address(value
, ip
)) {
481 * Found; add it to cache.
483 * FIXME: this should be bounded in size (and implement
484 * a caching policy), and entries should be prunned when
485 * libraries are unmapped.
487 debug_info_src
= debug_info_source_create_from_bin(bin
, ip
,
488 debug_info
->self_comp
);
489 if (debug_info_src
) {
491 proc_dbg_info_src
->ip_to_debug_info_src
, key
,
493 /* Ownership passed to ht. */
501 return debug_info_src
;
505 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
506 int64_t vpid
, uint64_t ip
)
508 struct debug_info_source
*dbg_info_src
= NULL
;
509 struct proc_debug_info_sources
*proc_dbg_info_src
;
511 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
512 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
513 if (!proc_dbg_info_src
) {
517 dbg_info_src
= proc_debug_info_sources_get_entry(debug_info
,
518 proc_dbg_info_src
, ip
);
525 struct debug_info
*debug_info_create(struct debug_info_component
*comp
,
526 const bt_trace
*trace
, struct bt_fd_cache
*fdc
)
529 struct debug_info
*debug_info
;
535 debug_info
= g_new0(struct debug_info
, 1);
540 debug_info
->log_level
= comp
->log_level
;
541 debug_info
->self_comp
= comp
->self_comp
;
542 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
543 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
544 (GDestroyNotify
) proc_debug_info_sources_destroy
);
545 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
549 debug_info
->comp
= comp
;
550 ret
= debug_info_init(debug_info
);
555 debug_info
->input_trace
= trace
;
556 debug_info
->fd_cache
= fdc
;
566 void debug_info_destroy(struct debug_info
*debug_info
)
568 bt_logging_level log_level
;
569 bt_self_component
*self_comp
;
570 bt_trace_remove_listener_status remove_listener_status
;
575 log_level
= debug_info
->log_level
;
576 self_comp
= debug_info
->self_comp
;
578 if (debug_info
->vpid_to_proc_dbg_info_src
) {
579 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
582 remove_listener_status
= bt_trace_remove_destruction_listener(
583 debug_info
->input_trace
,
584 debug_info
->destruction_listener_id
);
585 if (remove_listener_status
!= BT_TRACE_REMOVE_LISTENER_STATUS_OK
) {
586 BT_COMP_LOGE("Trace destruction listener removal failed.");
587 bt_current_thread_clear_error();
596 void handle_event_statedump_build_id(struct debug_info
*debug_info
,
597 const bt_event
*event
)
599 struct proc_debug_info_sources
*proc_dbg_info_src
;
600 uint64_t build_id_len
, baddr
;
601 uint8_t *build_id
= NULL
;
602 struct bin_info
*bin
;
606 event_get_common_context_signed_integer_field_value(event
,
607 VPID_FIELD_NAME
, &vpid
);
608 event_get_payload_unsigned_integer_field_value(event
,
609 BADDR_FIELD_NAME
, &baddr
);
611 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
612 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
613 if (!proc_dbg_info_src
) {
617 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
621 * The build_id event comes after the bin has been
622 * created. If it isn't found, just ignore this event.
627 event_get_payload_build_id_length(event
, BUILD_ID_FIELD_NAME
,
630 build_id
= g_new0(uint8_t, build_id_len
);
635 event_get_payload_build_id_value(event
, BUILD_ID_FIELD_NAME
, build_id
);
637 ret
= bin_info_set_build_id(bin
, build_id
, build_id_len
);
643 * Reset the is_elf_only flag in case it had been set
644 * previously, because we might find separate debug info using
645 * the new build id information.
647 bin
->is_elf_only
= false;
655 void handle_event_statedump_debug_link(struct debug_info
*debug_info
,
656 const bt_event
*event
)
658 struct proc_debug_info_sources
*proc_dbg_info_src
;
659 struct bin_info
*bin
= NULL
;
662 const char *filename
= NULL
;
664 uint64_t crc_field_value
;
666 event_get_common_context_signed_integer_field_value(event
,
667 VPID_FIELD_NAME
, &vpid
);
669 event_get_payload_unsigned_integer_field_value(event
,
670 BADDR_FIELD_NAME
, &baddr
);
672 event_get_payload_unsigned_integer_field_value(event
,
673 CRC32_FIELD_NAME
, &crc_field_value
);
675 crc32
= (uint32_t) crc_field_value
;
677 event_get_payload_string_field_value(event
,
678 FILENAME_FIELD_NAME
, &filename
);
680 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
681 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
682 if (!proc_dbg_info_src
) {
686 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
690 * The debug_link event comes after the bin has been
691 * created. If it isn't found, just ignore this event.
696 bin_info_set_debug_link(bin
, filename
, crc32
);
703 void handle_bin_info_event(struct debug_info
*debug_info
,
704 const bt_event
*event
, bool has_pic_field
)
706 struct proc_debug_info_sources
*proc_dbg_info_src
;
707 struct bin_info
*bin
;
708 uint64_t baddr
, memsz
;
714 event_get_payload_unsigned_integer_field_value(event
,
715 MEMSZ_FIELD_NAME
, &memsz
);
721 event_get_payload_unsigned_integer_field_value(event
,
722 BADDR_FIELD_NAME
, &baddr
);
725 * This field is not produced by the dlopen event emitted before
728 if (!event_has_payload_field(event
, PATH_FIELD_NAME
)) {
731 event_get_payload_string_field_value(event
, PATH_FIELD_NAME
, &path
);
734 uint64_t is_pic_field_value
;
736 event_get_payload_unsigned_integer_field_value(event
,
737 IS_PIC_FIELD_NAME
, &is_pic_field_value
);
738 is_pic
= is_pic_field_value
== 1;
741 * dlopen has no is_pic field, because the shared
742 * object is always PIC.
747 event_get_common_context_signed_integer_field_value(event
,
748 VPID_FIELD_NAME
, &vpid
);
750 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
751 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
752 if (!proc_dbg_info_src
) {
756 key
= g_new0(uint64_t, 1);
761 *((uint64_t *) key
) = baddr
;
763 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
, key
);
768 bin
= bin_info_create(debug_info
->fd_cache
, path
, baddr
, memsz
, is_pic
,
769 debug_info
->comp
->arg_debug_dir
,
770 debug_info
->comp
->arg_target_prefix
,
771 debug_info
->log_level
, debug_info
->self_comp
);
776 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
, key
, bin
);
777 /* Ownership passed to ht. */
786 void handle_event_statedump_bin_info(struct debug_info
*debug_info
,
787 const bt_event
*event
)
789 handle_bin_info_event(debug_info
, event
, true);
793 void handle_event_lib_load(struct debug_info
*debug_info
,
794 const bt_event
*event
)
796 handle_bin_info_event(debug_info
, event
, false);
800 void handle_event_lib_unload(struct debug_info
*debug_info
,
801 const bt_event
*event
)
804 struct proc_debug_info_sources
*proc_dbg_info_src
;
808 event_get_payload_unsigned_integer_field_value(event
, BADDR_FIELD_NAME
,
811 event_get_common_context_signed_integer_field_value(event
,
812 VPID_FIELD_NAME
, &vpid
);
814 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
815 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
816 if (!proc_dbg_info_src
) {
818 * It's an unload event for a library for which no load event
819 * was previously received.
824 ret
= g_hash_table_remove(proc_dbg_info_src
->baddr_to_bin_info
,
832 void handle_event_statedump_start(struct debug_info
*debug_info
,
833 const bt_event
*event
)
835 struct proc_debug_info_sources
*proc_dbg_info_src
;
838 event_get_common_context_signed_integer_field_value(
839 event
, VPID_FIELD_NAME
, &vpid
);
841 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
842 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
843 if (!proc_dbg_info_src
) {
847 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
848 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
854 void trace_debug_info_remove_func(const bt_trace
*in_trace
, void *data
)
856 struct debug_info_msg_iter
*debug_it
= data
;
857 if (debug_it
->debug_info_map
) {
859 ret
= g_hash_table_remove(debug_it
->debug_info_map
,
860 (gpointer
) in_trace
);
866 void handle_event_statedump(struct debug_info_msg_iter
*debug_it
,
867 const bt_event
*event
)
869 const bt_event_class
*event_class
;
870 const char *event_name
;
872 const bt_trace
*trace
;
873 struct debug_info
*debug_info
;
878 event_class
= bt_event_borrow_class_const(event
);
880 event_name
= bt_event_class_get_name(event_class
);
882 trace
= bt_stream_borrow_trace_const(
883 bt_event_borrow_stream_const(event
));
885 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
, trace
);
887 bt_trace_add_listener_status add_listener_status
;
889 debug_info
= debug_info_create(debug_it
->debug_info_component
,
890 trace
, &debug_it
->fd_cache
);
891 g_hash_table_insert(debug_it
->debug_info_map
, (gpointer
) trace
,
893 add_listener_status
= bt_trace_add_destruction_listener(
894 trace
, trace_debug_info_remove_func
, debug_it
,
895 &debug_info
->destruction_listener_id
);
896 BT_ASSERT(add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
899 q_event_name
= g_quark_try_string(event_name
);
901 if (q_event_name
== debug_info
->q_statedump_bin_info
) {
903 handle_event_statedump_bin_info(debug_info
, event
);
904 } else if (q_event_name
== debug_info
->q_dl_open
||
905 q_event_name
== debug_info
->q_lib_load
) {
907 * dl_open and lib_load events are both checked for since
908 * only dl_open was produced as of lttng-ust 2.8.
910 * lib_load, which is produced from lttng-ust 2.9+, is a lot
911 * more reliable since it will be emitted when other functions
912 * of the dlopen family are called (e.g. dlmopen) and when
913 * library are transitively loaded.
915 handle_event_lib_load(debug_info
, event
);
916 } else if (q_event_name
== debug_info
->q_statedump_start
) {
917 /* Start state dump */
918 handle_event_statedump_start(debug_info
, event
);
919 } else if (q_event_name
== debug_info
->q_statedump_debug_link
) {
920 /* Debug link info */
921 handle_event_statedump_debug_link(debug_info
, event
);
922 } else if (q_event_name
== debug_info
->q_statedump_build_id
) {
924 handle_event_statedump_build_id(debug_info
, event
);
925 } else if (q_event_name
== debug_info
-> q_lib_unload
) {
926 handle_event_lib_unload(debug_info
, event
);
933 void destroy_debug_info_comp(struct debug_info_component
*debug_info
)
939 g_free(debug_info
->arg_debug_dir
);
940 g_free(debug_info
->arg_debug_info_field_name
);
941 g_free(debug_info
->arg_target_prefix
);
946 void fill_debug_info_bin_field(struct debug_info_source
*dbg_info_src
,
947 bool full_path
, bt_field
*curr_field
,
948 bt_logging_level log_level
, bt_self_component
*self_comp
)
950 bt_field_string_set_value_status set_status
;
951 bt_field_string_append_status append_status
;
953 BT_ASSERT_DBG(bt_field_get_class_type(curr_field
) ==
954 BT_FIELD_CLASS_TYPE_STRING
);
958 set_status
= bt_field_string_set_value(curr_field
,
959 dbg_info_src
->bin_path
);
961 set_status
= bt_field_string_set_value(curr_field
,
962 dbg_info_src
->short_bin_path
);
964 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
965 BT_COMP_LOGE("Cannot set path component of \"bin\" "
966 "curr_field field's value: str-fc-addr=%p",
968 bt_current_thread_clear_error();
971 append_status
= bt_field_string_append(curr_field
,
972 dbg_info_src
->bin_loc
);
973 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
974 BT_COMP_LOGE("Cannot set bin location component of \"bin\" "
975 "curr_field field's value: str-fc-addr=%p",
977 bt_current_thread_clear_error();
980 set_status
= bt_field_string_set_value(curr_field
, "");
981 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
982 BT_COMP_LOGE("Cannot set \"bin\" curr_field field's value: "
983 "str-fc-addr=%p", curr_field
);
984 bt_current_thread_clear_error();
990 void fill_debug_info_func_field(struct debug_info_source
*dbg_info_src
,
991 bt_field
*curr_field
, bt_logging_level log_level
,
992 bt_self_component
*self_comp
)
994 bt_field_string_set_value_status status
;
996 BT_ASSERT_DBG(bt_field_get_class_type(curr_field
) ==
997 BT_FIELD_CLASS_TYPE_STRING
);
998 if (dbg_info_src
&& dbg_info_src
->func
) {
999 status
= bt_field_string_set_value(curr_field
,
1000 dbg_info_src
->func
);
1002 status
= bt_field_string_set_value(curr_field
, "");
1004 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1005 BT_COMP_LOGE("Cannot set \"func\" curr_field field's value: "
1006 "str-fc-addr=%p", curr_field
);
1007 bt_current_thread_clear_error();
1012 void fill_debug_info_src_field(struct debug_info_source
*dbg_info_src
,
1013 bool full_path
, bt_field
*curr_field
,
1014 bt_logging_level log_level
,
1015 bt_self_component
*self_comp
)
1017 bt_field_string_set_value_status set_status
;
1018 bt_field_string_append_status append_status
;
1020 BT_ASSERT_DBG(bt_field_get_class_type(curr_field
) ==
1021 BT_FIELD_CLASS_TYPE_STRING
);
1023 if (dbg_info_src
&& dbg_info_src
->src_path
) {
1025 set_status
= bt_field_string_set_value(curr_field
,
1026 dbg_info_src
->src_path
);
1028 set_status
= bt_field_string_set_value(curr_field
,
1029 dbg_info_src
->short_src_path
);
1031 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1032 BT_COMP_LOGE("Cannot set path component of \"src\" "
1033 "curr_field field's value: str-fc-addr=%p",
1035 bt_current_thread_clear_error();
1038 append_status
= bt_field_string_append(curr_field
, ":");
1039 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
1040 BT_COMP_LOGE("Cannot set colon 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 dbg_info_src
->line_no
);
1048 if (append_status
!= BT_FIELD_STRING_APPEND_STATUS_OK
) {
1049 BT_COMP_LOGE("Cannot set line number component of \"src\" "
1050 "curr_field field's value: str-fc-addr=%p",
1052 bt_current_thread_clear_error();
1055 set_status
= bt_field_string_set_value(curr_field
, "");
1056 if (set_status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1057 BT_COMP_LOGE("Cannot set \"src\" curr_field field's value: "
1058 "str-fc-addr=%p", curr_field
);
1059 bt_current_thread_clear_error();
1065 void fill_debug_info_field_empty(bt_field
*debug_info_field
,
1066 bt_logging_level log_level
, bt_self_component
*self_comp
)
1068 bt_field_string_set_value_status status
;
1069 bt_field
*bin_field
, *func_field
, *src_field
;
1071 BT_ASSERT_DBG(bt_field_get_class_type(debug_info_field
) ==
1072 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1074 bin_field
= bt_field_structure_borrow_member_field_by_name(
1075 debug_info_field
, "bin");
1076 func_field
= bt_field_structure_borrow_member_field_by_name(
1077 debug_info_field
, "func");
1078 src_field
= bt_field_structure_borrow_member_field_by_name(
1079 debug_info_field
, "src");
1081 BT_ASSERT_DBG(bt_field_get_class_type(bin_field
) ==
1082 BT_FIELD_CLASS_TYPE_STRING
);
1083 BT_ASSERT_DBG(bt_field_get_class_type(func_field
) ==
1084 BT_FIELD_CLASS_TYPE_STRING
);
1085 BT_ASSERT_DBG(bt_field_get_class_type(src_field
) ==
1086 BT_FIELD_CLASS_TYPE_STRING
);
1088 status
= bt_field_string_set_value(bin_field
, "");
1089 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1090 BT_COMP_LOGE("Cannot set \"bin\" bin_field field's value: "
1091 "str-fc-addr=%p", bin_field
);
1092 bt_current_thread_clear_error();
1095 status
= bt_field_string_set_value(func_field
, "");
1096 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1097 BT_COMP_LOGE("Cannot set \"func\" func_field field's value: "
1098 "str-fc-addr=%p", func_field
);
1099 bt_current_thread_clear_error();
1102 status
= bt_field_string_set_value(src_field
, "");
1103 if (status
!= BT_FIELD_STRING_SET_VALUE_STATUS_OK
) {
1104 BT_COMP_LOGE("Cannot set \"src\" src_field field's value: "
1105 "str-fc-addr=%p", src_field
);
1106 bt_current_thread_clear_error();
1110 void fill_debug_info_field(struct debug_info
*debug_info
, int64_t vpid
,
1111 uint64_t ip
, bt_field
*debug_info_field
)
1113 struct debug_info_source
*dbg_info_src
;
1114 const bt_field_class
*debug_info_fc
;
1116 BT_ASSERT_DBG(bt_field_get_class_type(debug_info_field
) ==
1117 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1119 debug_info_fc
= bt_field_borrow_class_const(debug_info_field
);
1121 BT_ASSERT_DBG(bt_field_class_structure_get_member_count(
1122 debug_info_fc
) == 3);
1124 dbg_info_src
= debug_info_query(debug_info
, vpid
, ip
);
1126 fill_debug_info_bin_field(dbg_info_src
,
1127 debug_info
->comp
->arg_full_path
,
1128 bt_field_structure_borrow_member_field_by_name(
1129 debug_info_field
, "bin"),
1130 debug_info
->log_level
, debug_info
->self_comp
);
1131 fill_debug_info_func_field(dbg_info_src
,
1132 bt_field_structure_borrow_member_field_by_name(
1133 debug_info_field
, "func"),
1134 debug_info
->log_level
, debug_info
->self_comp
);
1135 fill_debug_info_src_field(dbg_info_src
,
1136 debug_info
->comp
->arg_full_path
,
1137 bt_field_structure_borrow_member_field_by_name(
1138 debug_info_field
, "src"),
1139 debug_info
->log_level
, debug_info
->self_comp
);
1143 void fill_debug_info_event_if_needed(struct debug_info_msg_iter
*debug_it
,
1144 const bt_event
*in_event
, bt_event
*out_event
)
1146 bt_field
*out_common_ctx_field
, *out_debug_info_field
;
1147 const bt_field
*vpid_field
, *ip_field
, *in_common_ctx_field
;
1148 const bt_field_class
*in_common_ctx_fc
;
1149 struct debug_info
*debug_info
;
1152 gchar
*debug_info_field_name
=
1153 debug_it
->debug_info_component
->arg_debug_info_field_name
;
1154 bt_logging_level log_level
= debug_it
->log_level
;
1155 bt_self_component
*self_comp
= debug_it
->self_comp
;
1157 in_common_ctx_field
= bt_event_borrow_common_context_field_const(
1159 if (!in_common_ctx_field
) {
1161 * There is no event common context so no need to add debug
1167 in_common_ctx_fc
= bt_field_borrow_class_const(in_common_ctx_field
);
1168 if (!is_event_common_ctx_dbg_info_compatible(in_common_ctx_fc
,
1169 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1171 * The input event common context does not have the necessary
1172 * fields to resolve debug information.
1177 /* Borrow the debug-info field. */
1178 out_common_ctx_field
= bt_event_borrow_common_context_field(out_event
);
1179 if (!out_common_ctx_field
) {
1183 out_debug_info_field
= bt_field_structure_borrow_member_field_by_name(
1184 out_common_ctx_field
, debug_info_field_name
);
1186 vpid_field
= bt_field_structure_borrow_member_field_by_name_const(
1187 out_common_ctx_field
, VPID_FIELD_NAME
);
1188 ip_field
= bt_field_structure_borrow_member_field_by_name_const(
1189 out_common_ctx_field
, IP_FIELD_NAME
);
1191 vpid
= bt_field_integer_signed_get_value(vpid_field
);
1192 ip
= bt_field_integer_unsigned_get_value(ip_field
);
1195 * Borrow the debug_info structure needed for the source
1198 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
,
1199 bt_stream_borrow_trace_const(
1200 bt_event_borrow_stream_const(in_event
)));
1204 * Perform the debug-info resolving and set the event fields
1207 fill_debug_info_field(debug_info
, vpid
, ip
, out_debug_info_field
);
1209 BT_COMP_LOGD("No debug information for this trace. Setting debug "
1210 "info fields to empty strings.");
1211 fill_debug_info_field_empty(out_debug_info_field
,
1212 log_level
, self_comp
);
1219 void update_event_statedump_if_needed(struct debug_info_msg_iter
*debug_it
,
1220 const bt_event
*in_event
)
1222 const bt_field
*event_common_ctx
;
1223 const bt_field_class
*event_common_ctx_fc
;
1224 const bt_event_class
*in_event_class
= bt_event_borrow_class_const(in_event
);
1227 * If the event is an lttng_ust_statedump event AND has the right event
1228 * common context fields update the debug-info view for this process.
1230 event_common_ctx
= bt_event_borrow_common_context_field_const(in_event
);
1231 if (!event_common_ctx
) {
1235 event_common_ctx_fc
= bt_field_borrow_class_const(event_common_ctx
);
1236 if (is_event_common_ctx_dbg_info_compatible(event_common_ctx_fc
,
1237 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1238 /* Checkout if it might be a one of lttng ust statedump events. */
1239 const char *in_event_name
= bt_event_class_get_name(in_event_class
);
1240 if (strncmp(in_event_name
, LTTNG_UST_STATEDUMP_PREFIX
,
1241 strlen(LTTNG_UST_STATEDUMP_PREFIX
)) == 0) {
1242 /* Handle statedump events. */
1243 handle_event_statedump(debug_it
, in_event
);
1251 bt_message
*handle_event_message(struct debug_info_msg_iter
*debug_it
,
1252 const bt_message
*in_message
)
1254 const bt_clock_snapshot
*cs
;
1255 const bt_clock_class
*default_cc
;
1256 const bt_packet
*in_packet
;
1257 const bt_stream
*in_stream
;
1258 const bt_stream
*out_stream
;
1259 bt_event_class
*out_event_class
;
1260 bt_packet
*out_packet
= NULL
;
1261 bt_event
*out_event
;
1262 bt_logging_level log_level
= debug_it
->log_level
;
1263 bt_self_component
*self_comp
= debug_it
->self_comp
;
1265 bt_message
*out_message
= NULL
;
1267 /* Borrow the input event and its event class. */
1268 const bt_event
*in_event
=
1269 bt_message_event_borrow_event_const(in_message
);
1270 const bt_event_class
*in_event_class
=
1271 bt_event_borrow_class_const(in_event
);
1273 update_event_statedump_if_needed(debug_it
, in_event
);
1275 out_event_class
= trace_ir_mapping_borrow_mapped_event_class(
1276 debug_it
->ir_maps
, in_event_class
);
1277 if (!out_event_class
) {
1278 out_event_class
= trace_ir_mapping_create_new_mapped_event_class(
1279 debug_it
->ir_maps
, in_event_class
);
1281 BT_ASSERT_DBG(out_event_class
);
1283 /* Borrow the input stream. */
1284 in_stream
= bt_event_borrow_stream_const(in_event
);
1285 BT_ASSERT_DBG(in_stream
);
1286 out_stream
= trace_ir_mapping_borrow_mapped_stream(debug_it
->ir_maps
,
1288 BT_ASSERT_DBG(in_stream
);
1290 /* Borrow the input and output packets. */
1291 in_packet
= bt_event_borrow_packet_const(in_event
);
1293 out_packet
= trace_ir_mapping_borrow_mapped_packet(
1294 debug_it
->ir_maps
, in_packet
);
1297 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1298 bt_event_class_borrow_stream_class_const(in_event_class
));
1300 /* Borrow event clock snapshot. */
1301 cs
= bt_message_event_borrow_default_clock_snapshot_const(
1304 /* Create an output event message. */
1307 bt_message_event_create_with_packet_and_default_clock_snapshot(
1308 debug_it
->input_iterator
,
1309 out_event_class
, out_packet
,
1310 bt_clock_snapshot_get_value(cs
));
1313 bt_message_event_create_with_default_clock_snapshot(
1314 debug_it
->input_iterator
,
1315 out_event_class
, out_stream
,
1316 bt_clock_snapshot_get_value(cs
));
1320 out_message
= bt_message_event_create_with_packet(
1321 debug_it
->input_iterator
, out_event_class
,
1324 out_message
= bt_message_event_create(
1325 debug_it
->input_iterator
, out_event_class
,
1331 BT_COMP_LOGE("Error creating output event message.");
1335 out_event
= bt_message_event_borrow_event(out_message
);
1337 /* Copy the original fields to the output event. */
1338 copy_event_content(in_event
, out_event
, log_level
, self_comp
);
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
);
1351 bt_message
*handle_stream_begin_message(struct debug_info_msg_iter
*debug_it
,
1352 const bt_message
*in_message
)
1354 const bt_stream
*in_stream
;
1355 bt_message
*out_message
;
1356 bt_stream
*out_stream
;
1357 bt_logging_level log_level
= debug_it
->log_level
;
1358 bt_self_component
*self_comp
= debug_it
->self_comp
;
1360 in_stream
= bt_message_stream_beginning_borrow_stream_const(in_message
);
1361 BT_ASSERT(in_stream
);
1363 /* Create a duplicated output stream. */
1364 out_stream
= trace_ir_mapping_create_new_mapped_stream(
1365 debug_it
->ir_maps
, in_stream
);
1371 /* Create an output stream beginning message. */
1372 out_message
= bt_message_stream_beginning_create(
1373 debug_it
->input_iterator
, out_stream
);
1375 BT_COMP_LOGE("Error creating output stream beginning message: "
1376 "out-s-addr=%p", out_stream
);
1383 bt_message
*handle_stream_end_message(struct debug_info_msg_iter
*debug_it
,
1384 const bt_message
*in_message
)
1386 const bt_stream
*in_stream
;
1387 bt_message
*out_message
= NULL
;
1388 bt_stream
*out_stream
;
1389 bt_logging_level log_level
= debug_it
->log_level
;
1390 bt_self_component
*self_comp
= debug_it
->self_comp
;
1392 in_stream
= bt_message_stream_end_borrow_stream_const(in_message
);
1393 BT_ASSERT(in_stream
);
1395 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1396 debug_it
->ir_maps
, in_stream
);
1397 BT_ASSERT(out_stream
);
1399 /* Create an output stream end message. */
1400 out_message
= bt_message_stream_end_create(debug_it
->input_iterator
,
1403 BT_COMP_LOGE("Error creating output stream end message: out-s-addr=%p",
1408 /* Remove stream from trace mapping hashtable. */
1409 trace_ir_mapping_remove_mapped_stream(debug_it
->ir_maps
, in_stream
);
1416 bt_message
*handle_packet_begin_message(struct debug_info_msg_iter
*debug_it
,
1417 const bt_message
*in_message
)
1419 bool has_default_clock_snapshot
;
1420 const bt_clock_snapshot
*cs
;
1421 bt_message
*out_message
= NULL
;
1422 bt_packet
*out_packet
;
1423 bt_logging_level log_level
= debug_it
->log_level
;
1424 bt_self_component
*self_comp
= debug_it
->self_comp
;
1426 const bt_packet
*in_packet
=
1427 bt_message_packet_beginning_borrow_packet_const(in_message
);
1428 BT_ASSERT(in_packet
);
1430 /* This packet should not be already mapped. */
1431 BT_ASSERT(!trace_ir_mapping_borrow_mapped_packet(
1432 debug_it
->ir_maps
, in_packet
));
1434 out_packet
= trace_ir_mapping_create_new_mapped_packet(debug_it
->ir_maps
,
1437 BT_ASSERT(out_packet
);
1439 has_default_clock_snapshot
=
1440 bt_stream_class_packets_have_beginning_default_clock_snapshot(
1441 bt_stream_borrow_class_const(
1442 bt_packet_borrow_stream_const(in_packet
)));
1443 if (has_default_clock_snapshot
) {
1444 /* Borrow clock snapshot. */
1445 cs
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
1448 /* Create an output packet beginning message. */
1449 out_message
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1450 debug_it
->input_iterator
, out_packet
,
1451 bt_clock_snapshot_get_value(cs
));
1453 out_message
= bt_message_packet_beginning_create(
1454 debug_it
->input_iterator
, out_packet
);
1457 BT_COMP_LOGE("Error creating output packet beginning message: "
1458 "out-p-addr=%p", out_packet
);
1465 bt_message
*handle_packet_end_message(struct debug_info_msg_iter
*debug_it
,
1466 const bt_message
*in_message
)
1468 bool has_default_clock_snapshot
;
1469 const bt_clock_snapshot
*cs
;
1470 const bt_packet
*in_packet
;
1471 bt_message
*out_message
= NULL
;
1472 bt_packet
*out_packet
;
1473 bt_logging_level log_level
= debug_it
->log_level
;
1474 bt_self_component
*self_comp
= debug_it
->self_comp
;
1476 in_packet
= bt_message_packet_end_borrow_packet_const(in_message
);
1477 BT_ASSERT(in_packet
);
1479 out_packet
= trace_ir_mapping_borrow_mapped_packet(debug_it
->ir_maps
, in_packet
);
1480 BT_ASSERT(out_packet
);
1482 has_default_clock_snapshot
=
1483 bt_stream_class_packets_have_end_default_clock_snapshot(
1484 bt_stream_borrow_class_const(
1485 bt_packet_borrow_stream_const(in_packet
)));
1486 if (has_default_clock_snapshot
) {
1487 /* Borrow clock snapshot. */
1488 cs
= bt_message_packet_end_borrow_default_clock_snapshot_const(
1491 /* Create an outpute packet end message. */
1492 out_message
= bt_message_packet_end_create_with_default_clock_snapshot(
1493 debug_it
->input_iterator
, out_packet
,
1494 bt_clock_snapshot_get_value(cs
));
1496 out_message
= bt_message_packet_end_create(
1497 debug_it
->input_iterator
, out_packet
);
1501 BT_COMP_LOGE("Error creating output packet end message: "
1502 "out-p-addr=%p", out_packet
);
1506 /* Remove packet from data mapping hashtable. */
1507 trace_ir_mapping_remove_mapped_packet(debug_it
->ir_maps
, in_packet
);
1514 bt_message
*handle_msg_iterator_inactivity(struct debug_info_msg_iter
*debug_it
,
1515 const bt_message
*in_message
)
1518 * This message type can be forwarded directly because it does
1519 * not refer to any objects in the trace class.
1521 bt_message_get_ref(in_message
);
1522 return (bt_message
*) in_message
;
1526 bt_message
*handle_discarded_events_message(struct debug_info_msg_iter
*debug_it
,
1527 const bt_message
*in_message
)
1529 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1530 const bt_stream
*in_stream
;
1531 bool has_default_clock_snapshots
;
1532 uint64_t discarded_events
, begin_cs_value
, end_cs_value
;
1533 bt_property_availability prop_avail
;
1534 bt_message
*out_message
= NULL
;
1535 bt_stream
*out_stream
;
1536 bt_logging_level log_level
= debug_it
->log_level
;
1537 bt_self_component
*self_comp
= debug_it
->self_comp
;
1539 in_stream
= bt_message_discarded_events_borrow_stream_const(
1541 BT_ASSERT(in_stream
);
1543 out_stream
= trace_ir_mapping_borrow_mapped_stream( debug_it
->ir_maps
,
1545 BT_ASSERT(out_stream
);
1547 has_default_clock_snapshots
=
1548 bt_stream_class_discarded_events_have_default_clock_snapshots(
1549 bt_stream_borrow_class_const(in_stream
));
1550 if (has_default_clock_snapshots
) {
1551 begin_cs
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1553 end_cs
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1556 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1557 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1559 out_message
= bt_message_discarded_events_create_with_default_clock_snapshots(
1560 debug_it
->input_iterator
, out_stream
,
1561 begin_cs_value
, end_cs_value
);
1563 out_message
= bt_message_discarded_events_create(
1564 debug_it
->input_iterator
, out_stream
);
1567 BT_COMP_LOGE("Error creating output discarded events message: "
1568 "out-s-addr=%p", out_stream
);
1572 prop_avail
= bt_message_discarded_events_get_count(in_message
,
1575 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1576 bt_message_discarded_events_set_count(out_message
,
1585 bt_message
*handle_discarded_packets_message(struct debug_info_msg_iter
*debug_it
,
1586 const bt_message
*in_message
)
1588 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1589 bool has_default_clock_snapshots
;
1590 const bt_stream
*in_stream
;
1591 uint64_t discarded_packets
, begin_cs_value
, end_cs_value
;
1592 bt_property_availability prop_avail
;
1593 bt_message
*out_message
= NULL
;
1594 bt_stream
*out_stream
;
1595 bt_logging_level log_level
= debug_it
->log_level
;
1596 bt_self_component
*self_comp
= debug_it
->self_comp
;
1598 in_stream
= bt_message_discarded_packets_borrow_stream_const(
1600 BT_ASSERT(in_stream
);
1602 out_stream
= trace_ir_mapping_borrow_mapped_stream( debug_it
->ir_maps
,
1604 BT_ASSERT(out_stream
);
1606 has_default_clock_snapshots
=
1607 bt_stream_class_discarded_packets_have_default_clock_snapshots(
1608 bt_stream_borrow_class_const(in_stream
));
1609 if (has_default_clock_snapshots
) {
1610 begin_cs
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1613 end_cs
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1616 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1617 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1619 out_message
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1620 debug_it
->input_iterator
, out_stream
,
1621 begin_cs_value
, end_cs_value
);
1623 out_message
= bt_message_discarded_packets_create(
1624 debug_it
->input_iterator
, out_stream
);
1627 BT_COMP_LOGE("Error creating output discarded packet message: "
1628 "out-s-addr=%p", out_stream
);
1632 prop_avail
= bt_message_discarded_packets_get_count(in_message
,
1633 &discarded_packets
);
1634 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1635 bt_message_discarded_packets_set_count(out_message
,
1644 const bt_message
*handle_message(struct debug_info_msg_iter
*debug_it
,
1645 const bt_message
*in_message
)
1647 bt_message
*out_message
= NULL
;
1649 switch (bt_message_get_type(in_message
)) {
1650 case BT_MESSAGE_TYPE_EVENT
:
1651 out_message
= handle_event_message(debug_it
, in_message
);
1653 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1654 out_message
= handle_packet_begin_message(debug_it
, in_message
);
1656 case BT_MESSAGE_TYPE_PACKET_END
:
1657 out_message
= handle_packet_end_message(debug_it
, in_message
);
1659 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1660 out_message
= handle_stream_begin_message(debug_it
, in_message
);
1662 case BT_MESSAGE_TYPE_STREAM_END
:
1663 out_message
= handle_stream_end_message(debug_it
, in_message
);
1665 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1666 out_message
= handle_msg_iterator_inactivity(debug_it
, in_message
);
1668 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1669 out_message
= handle_discarded_events_message(debug_it
, in_message
);
1671 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1672 out_message
= handle_discarded_packets_message(debug_it
, in_message
);
1682 struct bt_param_validation_map_value_entry_descr debug_info_params
[] = {
1683 { "debug-info-field-name", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
1684 { "debug-info-dir", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
1685 { "target-prefix", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
1686 { "full-path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
1687 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1691 bt_component_class_initialize_method_status
init_from_params(
1692 struct debug_info_component
*debug_info_component
,
1693 const bt_value
*params
)
1695 const bt_value
*value
;
1696 bt_component_class_initialize_method_status status
;
1697 bt_logging_level log_level
= debug_info_component
->log_level
;
1698 enum bt_param_validation_status validation_status
;
1699 gchar
*validate_error
= NULL
;
1701 validation_status
= bt_param_validation_validate(params
,
1702 debug_info_params
, &validate_error
);
1703 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1704 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1706 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1707 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1708 BT_COMP_LOGE_APPEND_CAUSE(debug_info_component
->self_comp
,
1709 "%s", validate_error
);
1715 value
= bt_value_map_borrow_entry_value_const(params
,
1716 "debug-info-field-name");
1718 debug_info_component
->arg_debug_info_field_name
=
1719 g_strdup(bt_value_string_get(value
));
1721 debug_info_component
->arg_debug_info_field_name
=
1722 g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME
);
1725 value
= bt_value_map_borrow_entry_value_const(params
, "debug-info-dir");
1727 debug_info_component
->arg_debug_dir
=
1728 g_strdup(bt_value_string_get(value
));
1730 debug_info_component
->arg_debug_dir
= NULL
;
1734 value
= bt_value_map_borrow_entry_value_const(params
, "target-prefix");
1736 debug_info_component
->arg_target_prefix
=
1737 g_strdup(bt_value_string_get(value
));
1739 debug_info_component
->arg_target_prefix
= NULL
;
1742 value
= bt_value_map_borrow_entry_value_const(params
, "full-path");
1744 debug_info_component
->arg_full_path
= bt_value_bool_get(value
);
1746 debug_info_component
->arg_full_path
= BT_FALSE
;
1749 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1752 g_free(validate_error
);
1758 bt_component_class_initialize_method_status
debug_info_comp_init(
1759 bt_self_component_filter
*self_comp_flt
,
1760 bt_self_component_filter_configuration
*config
,
1761 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
1763 struct debug_info_component
*debug_info_comp
;
1764 bt_component_class_initialize_method_status status
=
1765 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1766 bt_self_component_add_port_status add_port_status
;
1767 bt_self_component
*self_comp
=
1768 bt_self_component_filter_as_self_component(self_comp_flt
);
1769 bt_logging_level log_level
= bt_component_get_logging_level(
1770 bt_self_component_as_component(self_comp
));
1772 BT_COMP_LOGI("Initializing debug_info component: "
1773 "comp-addr=%p, params-addr=%p", self_comp
, params
);
1775 debug_info_comp
= g_new0(struct debug_info_component
, 1);
1776 if (!debug_info_comp
) {
1777 BT_COMP_LOGE_STR("Failed to allocate one debug_info component.");
1781 debug_info_comp
->log_level
= log_level
;
1782 debug_info_comp
->self_comp
= self_comp
;
1783 bt_self_component_set_data(self_comp
, debug_info_comp
);
1785 add_port_status
= bt_self_component_filter_add_input_port(
1786 self_comp_flt
, "in", NULL
, NULL
);
1787 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1788 status
= (int) add_port_status
;
1792 add_port_status
= bt_self_component_filter_add_output_port(
1793 self_comp_flt
, "out", NULL
, NULL
);
1794 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1795 status
= (int) add_port_status
;
1799 status
= init_from_params(debug_info_comp
, params
);
1800 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1801 BT_COMP_LOGE("Cannot configure debug_info component: "
1802 "debug_info-comp-addr=%p, params-addr=%p",
1803 debug_info_comp
, params
);
1810 destroy_debug_info_comp(debug_info_comp
);
1811 bt_self_component_set_data(self_comp
, NULL
);
1813 if (status
== BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1814 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1821 void debug_info_comp_finalize(bt_self_component_filter
*self_comp_flt
)
1823 struct debug_info_component
*debug_info
=
1824 bt_self_component_get_data(
1825 bt_self_component_filter_as_self_component(
1827 bt_logging_level log_level
= debug_info
->log_level
;
1828 bt_self_component
*self_comp
= debug_info
->self_comp
;
1830 BT_COMP_LOGI("Finalizing debug_info self_component: comp-addr=%p",
1833 destroy_debug_info_comp(debug_info
);
1837 bt_component_class_message_iterator_next_method_status
debug_info_msg_iter_next(
1838 bt_self_message_iterator
*self_msg_iter
,
1839 const bt_message_array_const msgs
, uint64_t capacity
,
1842 bt_self_component_port_input_message_iterator
*upstream_iterator
= NULL
;
1843 bt_message_iterator_next_status upstream_iterator_ret_status
;
1844 struct debug_info_msg_iter
*debug_info_msg_iter
;
1845 struct debug_info_component
*debug_info
= NULL
;
1846 bt_component_class_message_iterator_next_method_status status
;
1847 bt_self_component
*self_comp
= NULL
;
1848 bt_message_array_const input_msgs
;
1849 const bt_message
*out_message
;
1850 uint64_t curr_msg_idx
, i
;
1852 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1854 self_comp
= bt_self_message_iterator_borrow_component(self_msg_iter
);
1855 BT_ASSERT_DBG(self_comp
);
1857 debug_info
= bt_self_component_get_data(self_comp
);
1858 BT_ASSERT_DBG(debug_info
);
1860 debug_info_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
1861 BT_ASSERT_DBG(debug_info_msg_iter
);
1863 upstream_iterator
= debug_info_msg_iter
->msg_iter
;
1864 BT_ASSERT_DBG(upstream_iterator
);
1866 upstream_iterator_ret_status
=
1867 bt_self_component_port_input_message_iterator_next(
1868 upstream_iterator
, &input_msgs
, count
);
1869 if (upstream_iterator_ret_status
!=
1870 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
1872 * No messages were returned. Not necessarily an error.
1873 * Convert the upstream message iterator status to a
1876 status
= (int) upstream_iterator_ret_status
;
1881 * There should never be more received messages than the capacity we
1884 BT_ASSERT_DBG(*count
<= capacity
);
1886 for (curr_msg_idx
= 0; curr_msg_idx
< *count
; curr_msg_idx
++) {
1887 out_message
= handle_message(debug_info_msg_iter
,
1888 input_msgs
[curr_msg_idx
]);
1890 goto handle_msg_error
;
1893 msgs
[curr_msg_idx
] = out_message
;
1895 * Drop our reference of the input message as we are done with
1896 * it and created a output copy.
1898 bt_message_put_ref(input_msgs
[curr_msg_idx
]);
1905 * Drop references of all the output messages created before the
1908 for (i
= 0; i
< curr_msg_idx
; i
++) {
1909 bt_message_put_ref(msgs
[i
]);
1913 * Drop references of all the input messages not dropped before the
1916 for (i
= curr_msg_idx
; i
< *count
; i
++) {
1917 bt_message_put_ref(input_msgs
[i
]);
1920 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1927 void debug_info_msg_iter_destroy(struct debug_info_msg_iter
*debug_info_msg_iter
)
1929 if (!debug_info_msg_iter
) {
1933 if (debug_info_msg_iter
->msg_iter
) {
1934 bt_self_component_port_input_message_iterator_put_ref(
1935 debug_info_msg_iter
->msg_iter
);
1938 if (debug_info_msg_iter
->ir_maps
) {
1939 trace_ir_maps_destroy(debug_info_msg_iter
->ir_maps
);
1942 if (debug_info_msg_iter
->debug_info_map
) {
1943 g_hash_table_destroy(debug_info_msg_iter
->debug_info_map
);
1946 bt_fd_cache_fini(&debug_info_msg_iter
->fd_cache
);
1947 g_free(debug_info_msg_iter
);
1954 bt_component_class_message_iterator_initialize_method_status
debug_info_msg_iter_init(
1955 bt_self_message_iterator
*self_msg_iter
,
1956 bt_self_message_iterator_configuration
*config
,
1957 bt_self_component_filter
*self_comp_flt
,
1958 bt_self_component_port_output
*self_port
)
1960 bt_component_class_message_iterator_initialize_method_status status
;
1961 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
1963 struct bt_self_component_port_input
*input_port
= NULL
;
1964 bt_self_component_port_input_message_iterator
*upstream_iterator
= NULL
;
1965 struct debug_info_msg_iter
*debug_info_msg_iter
= NULL
;
1966 gchar
*debug_info_field_name
;
1968 bt_self_component
*self_comp
=
1969 bt_self_component_filter_as_self_component(self_comp_flt
);
1970 bt_logging_level log_level
= bt_component_get_logging_level(
1971 bt_self_component_as_component(self_comp
));
1973 /* Borrow the upstream input port. */
1974 input_port
= bt_self_component_filter_borrow_input_port_by_name(
1975 self_comp_flt
, "in");
1977 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1981 debug_info_msg_iter
= g_new0(struct debug_info_msg_iter
, 1);
1982 if (!debug_info_msg_iter
) {
1983 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1987 debug_info_msg_iter
->log_level
= log_level
;
1988 debug_info_msg_iter
->self_comp
= self_comp
;
1990 /* Create an iterator on the upstream component. */
1991 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_message_iterator(
1992 self_msg_iter
, input_port
, &upstream_iterator
);
1993 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
1994 status
= (int) msg_iter_status
;
1998 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
1999 debug_info_msg_iter
->msg_iter
, upstream_iterator
);
2001 /* Create hashtable that will contain debug info mapping. */
2002 debug_info_msg_iter
->debug_info_map
= g_hash_table_new_full(
2003 g_direct_hash
, g_direct_equal
, (GDestroyNotify
) NULL
,
2004 (GDestroyNotify
) debug_info_destroy
);
2005 if (!debug_info_msg_iter
->debug_info_map
) {
2006 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2010 debug_info_msg_iter
->debug_info_component
=
2011 bt_self_component_get_data(self_comp
);
2013 debug_info_field_name
=
2014 debug_info_msg_iter
->debug_info_component
->arg_debug_info_field_name
;
2016 debug_info_msg_iter
->ir_maps
= trace_ir_maps_create(self_comp
,
2017 debug_info_field_name
, log_level
);
2018 if (!debug_info_msg_iter
->ir_maps
) {
2019 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2023 ret
= bt_fd_cache_init(&debug_info_msg_iter
->fd_cache
, log_level
);
2025 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2029 bt_self_message_iterator_configuration_set_can_seek_forward(config
,
2030 bt_self_component_port_input_message_iterator_can_seek_forward(
2031 debug_info_msg_iter
->msg_iter
));
2033 bt_self_message_iterator_set_data(self_msg_iter
, debug_info_msg_iter
);
2034 debug_info_msg_iter
->input_iterator
= self_msg_iter
;
2036 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
2040 debug_info_msg_iter_destroy(debug_info_msg_iter
);
2047 bt_component_class_message_iterator_can_seek_beginning_method_status
2048 debug_info_msg_iter_can_seek_beginning(bt_self_message_iterator
*self_msg_iter
,
2051 struct debug_info_msg_iter
*debug_info_msg_iter
=
2052 bt_self_message_iterator_get_data(self_msg_iter
);
2053 BT_ASSERT(debug_info_msg_iter
);
2055 return (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
2056 debug_info_msg_iter
->msg_iter
, can_seek
);
2060 bt_component_class_message_iterator_seek_beginning_method_status
2061 debug_info_msg_iter_seek_beginning(bt_self_message_iterator
*self_msg_iter
)
2063 struct debug_info_msg_iter
*debug_info_msg_iter
=
2064 bt_self_message_iterator_get_data(self_msg_iter
);
2065 bt_component_class_message_iterator_seek_beginning_method_status status
=
2066 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
2067 bt_message_iterator_seek_beginning_status seek_beg_status
;
2069 BT_ASSERT(debug_info_msg_iter
);
2071 /* Ask the upstream component to seek to the beginning. */
2072 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
2073 debug_info_msg_iter
->msg_iter
);
2074 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
2075 status
= (int) seek_beg_status
;
2079 /* Clear this iterator data. */
2080 trace_ir_maps_clear(debug_info_msg_iter
->ir_maps
);
2081 g_hash_table_remove_all(debug_info_msg_iter
->debug_info_map
);
2088 void debug_info_msg_iter_finalize(bt_self_message_iterator
*it
)
2090 struct debug_info_msg_iter
*debug_info_msg_iter
;
2092 debug_info_msg_iter
= bt_self_message_iterator_get_data(it
);
2093 BT_ASSERT(debug_info_msg_iter
);
2095 debug_info_msg_iter_destroy(debug_info_msg_iter
);