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_LOG_TAG "PLUGIN-CTF-LTTNG-UTILS-DEBUG-INFO-FLT"
33 #include <plugins-common.h>
35 #include <babeltrace/assert-internal.h>
36 #include <babeltrace/common-internal.h>
37 #include <babeltrace/fd-cache-internal.h>
40 #include "debug-info.h"
41 #include "trace-ir-data-copy.h"
42 #include "trace-ir-mapping.h"
43 #include "trace-ir-metadata-copy.h"
46 #define DEFAULT_DEBUG_INFO_FIELD_NAME "debug_info"
47 #define LTTNG_UST_STATEDUMP_PREFIX "lttng_ust"
48 #define VPID_FIELD_NAME "vpid"
49 #define IP_FIELD_NAME "ip"
50 #define BADDR_FIELD_NAME "baddr"
51 #define CRC32_FIELD_NAME "crc"
52 #define BUILD_ID_FIELD_NAME "build_id"
53 #define FILENAME_FIELD_NAME "filename"
54 #define IS_PIC_FIELD_NAME "is_pic"
55 #define MEMSZ_FIELD_NAME "memsz"
56 #define PATH_FIELD_NAME "path"
58 struct debug_info_component
{
60 gchar
*arg_debug_info_field_name
;
61 gchar
*arg_target_prefix
;
62 bt_bool arg_full_path
;
65 struct debug_info_msg_iter
{
66 struct debug_info_component
*debug_info_component
;
67 bt_self_message_iterator
*input_iterator
;
68 bt_self_component
*self_comp
;
69 bt_self_component_port_input_message_iterator
*msg_iter
;
71 struct trace_ir_maps
*ir_maps
;
72 /* in_trace -> debug_info_mapping. */
73 GHashTable
*debug_info_map
;
75 struct bt_fd_cache fd_cache
;
78 struct debug_info_source
{
79 /* Strings are owned by debug_info_source. */
82 * Store the line number as a string so that the allocation and
83 * conversion to string is only done once.
87 /* short_src_path points inside src_path, no need to free. */
88 const gchar
*short_src_path
;
90 /* short_bin_path points inside bin_path, no need to free. */
91 const gchar
*short_bin_path
;
93 * Location within the binary. Either absolute (@0x1234) or
99 struct proc_debug_info_sources
{
101 * Hash table: base address (pointer to uint64_t) to bin info; owned by
102 * proc_debug_info_sources.
104 GHashTable
*baddr_to_bin_info
;
107 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
108 * owned by proc_debug_info_sources.
110 GHashTable
*ip_to_debug_info_src
;
114 struct debug_info_component
*comp
;
115 const bt_trace
*input_trace
;
116 uint64_t destruction_listener_id
;
119 * Hash table of VPIDs (pointer to int64_t) to
120 * (struct proc_debug_info_sources*); owned by debug_info.
122 GHashTable
*vpid_to_proc_dbg_info_src
;
123 GQuark q_statedump_bin_info
;
124 GQuark q_statedump_debug_link
;
125 GQuark q_statedump_build_id
;
126 GQuark q_statedump_start
;
130 struct bt_fd_cache
*fd_cache
; /* Weak ref. Owned by the iterator. */
134 int debug_info_init(struct debug_info
*info
)
136 info
->q_statedump_bin_info
= g_quark_from_string(
137 "lttng_ust_statedump:bin_info");
138 info
->q_statedump_debug_link
= g_quark_from_string(
139 "lttng_ust_statedump:debug_link");
140 info
->q_statedump_build_id
= g_quark_from_string(
141 "lttng_ust_statedump:build_id");
142 info
->q_statedump_start
= g_quark_from_string(
143 "lttng_ust_statedump:start");
144 info
->q_dl_open
= g_quark_from_string("lttng_ust_dl:dlopen");
145 info
->q_lib_load
= g_quark_from_string("lttng_ust_lib:load");
146 info
->q_lib_unload
= g_quark_from_string("lttng_ust_lib:unload");
148 return bin_info_init();
152 void debug_info_source_destroy(struct debug_info_source
*debug_info_src
)
154 if (!debug_info_src
) {
158 g_free(debug_info_src
->func
);
159 g_free(debug_info_src
->line_no
);
160 g_free(debug_info_src
->src_path
);
161 g_free(debug_info_src
->bin_path
);
162 g_free(debug_info_src
->bin_loc
);
163 g_free(debug_info_src
);
167 struct debug_info_source
*debug_info_source_create_from_bin(
168 struct bin_info
*bin
, uint64_t ip
)
171 struct debug_info_source
*debug_info_src
= NULL
;
172 struct source_location
*src_loc
= NULL
;
174 debug_info_src
= g_new0(struct debug_info_source
, 1);
176 if (!debug_info_src
) {
180 /* Lookup function name */
181 ret
= bin_info_lookup_function_name(bin
, ip
, &debug_info_src
->func
);
186 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
187 if (!bin
->is_elf_only
|| !debug_info_src
->func
) {
188 /* Lookup source location */
189 ret
= bin_info_lookup_source_location(bin
, ip
, &src_loc
);
191 BT_LOGD("Failed to lookup source location: ret=%d", ret
);
196 debug_info_src
->line_no
=
197 g_strdup_printf("%"PRId64
, src_loc
->line_no
);
198 if (!debug_info_src
->line_no
) {
199 BT_LOGD("Error occured when setting line_no field.");
203 if (src_loc
->filename
) {
204 debug_info_src
->src_path
= g_strdup(src_loc
->filename
);
205 if (!debug_info_src
->src_path
) {
209 debug_info_src
->short_src_path
= get_filename_from_path(
210 debug_info_src
->src_path
);
212 source_location_destroy(src_loc
);
216 debug_info_src
->bin_path
= g_strdup(bin
->elf_path
);
217 if (!debug_info_src
->bin_path
) {
221 debug_info_src
->short_bin_path
= get_filename_from_path(
222 debug_info_src
->bin_path
);
224 ret
= bin_info_get_bin_loc(bin
, ip
, &(debug_info_src
->bin_loc
));
231 return debug_info_src
;
234 debug_info_source_destroy(debug_info_src
);
239 void proc_debug_info_sources_destroy(
240 struct proc_debug_info_sources
*proc_dbg_info_src
)
242 if (!proc_dbg_info_src
) {
246 if (proc_dbg_info_src
->baddr_to_bin_info
) {
247 g_hash_table_destroy(proc_dbg_info_src
->baddr_to_bin_info
);
250 if (proc_dbg_info_src
->ip_to_debug_info_src
) {
251 g_hash_table_destroy(proc_dbg_info_src
->ip_to_debug_info_src
);
254 g_free(proc_dbg_info_src
);
258 struct proc_debug_info_sources
*proc_debug_info_sources_create(void)
260 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
262 proc_dbg_info_src
= g_new0(struct proc_debug_info_sources
, 1);
263 if (!proc_dbg_info_src
) {
267 proc_dbg_info_src
->baddr_to_bin_info
= g_hash_table_new_full(
268 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
269 (GDestroyNotify
) bin_info_destroy
);
270 if (!proc_dbg_info_src
->baddr_to_bin_info
) {
274 proc_dbg_info_src
->ip_to_debug_info_src
= g_hash_table_new_full(
275 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
276 (GDestroyNotify
) debug_info_source_destroy
);
277 if (!proc_dbg_info_src
->ip_to_debug_info_src
) {
282 return proc_dbg_info_src
;
285 proc_debug_info_sources_destroy(proc_dbg_info_src
);
290 struct proc_debug_info_sources
*proc_debug_info_sources_ht_get_entry(
291 GHashTable
*ht
, int64_t vpid
)
293 gpointer key
= g_new0(int64_t, 1);
294 struct proc_debug_info_sources
*proc_dbg_info_src
= NULL
;
300 *((int64_t *) key
) = vpid
;
302 /* Exists? Return it */
303 proc_dbg_info_src
= g_hash_table_lookup(ht
, key
);
304 if (proc_dbg_info_src
) {
308 /* Otherwise, create and return it */
309 proc_dbg_info_src
= proc_debug_info_sources_create();
310 if (!proc_dbg_info_src
) {
314 g_hash_table_insert(ht
, key
, proc_dbg_info_src
);
315 /* Ownership passed to ht */
319 return proc_dbg_info_src
;
323 const bt_field
*event_borrow_payload_field(const bt_event
*event
,
324 const char *field_name
)
326 const bt_field
*event_payload
, *field
;
328 event_payload
= bt_event_borrow_payload_field_const(event
);
329 BT_ASSERT(event_payload
);
331 field
= bt_field_structure_borrow_member_field_by_name_const(
332 event_payload
, field_name
);
337 const bt_field
*event_borrow_common_context_field(const bt_event
*event
,
338 const char *field_name
)
340 const bt_field
*event_common_ctx
, *field
= NULL
;
342 event_common_ctx
= bt_event_borrow_common_context_field_const(event
);
343 if (!event_common_ctx
) {
347 field
= bt_field_structure_borrow_member_field_by_name_const(
348 event_common_ctx
, field_name
);
355 void event_get_common_context_signed_integer_field_value(
356 const bt_event
*event
, const char *field_name
, int64_t *value
)
358 *value
= bt_field_signed_integer_get_value(
359 event_borrow_common_context_field(event
, field_name
));
363 int event_get_payload_build_id_length(const bt_event
*event
,
364 const char *field_name
, uint64_t *build_id_len
)
366 const bt_field
*build_id_field
;
367 const bt_field_class
*build_id_field_class
;
369 build_id_field
= event_borrow_payload_field(event
, field_name
);
370 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
372 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
373 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
);
374 BT_ASSERT(bt_field_class_get_type(
375 bt_field_class_array_borrow_element_field_class_const(
376 build_id_field_class
)) ==
377 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
379 *build_id_len
= bt_field_array_get_length(build_id_field
);
385 int event_get_payload_build_id_value(const bt_event
*event
,
386 const char *field_name
, uint8_t *build_id
)
388 const bt_field
*curr_field
, *build_id_field
;
389 const bt_field_class
*build_id_field_class
;
390 uint64_t i
, build_id_len
;
395 build_id_field
= event_borrow_payload_field(event
, field_name
);
396 build_id_field_class
= bt_field_borrow_class_const(build_id_field
);
398 BT_ASSERT(bt_field_class_get_type(build_id_field_class
) ==
399 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
);
400 BT_ASSERT(bt_field_class_get_type(
401 bt_field_class_array_borrow_element_field_class_const(
402 build_id_field_class
)) ==
403 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
405 build_id_len
= bt_field_array_get_length(build_id_field
);
407 for (i
= 0; i
< build_id_len
; i
++) {
409 bt_field_array_borrow_element_field_by_index_const(
412 build_id
[i
] = bt_field_unsigned_integer_get_value(curr_field
);
419 void event_get_payload_unsigned_integer_field_value(const bt_event
*event
,
420 const char *field_name
, uint64_t *value
)
422 *value
= bt_field_unsigned_integer_get_value(
423 event_borrow_payload_field(event
, field_name
));
427 void event_get_payload_string_field_value(const bt_event
*event
,
428 const char *field_name
, const char **value
)
430 *value
= bt_field_string_get_value(
431 event_borrow_payload_field(event
, field_name
));
435 bool event_has_payload_field(const bt_event
*event
,
436 const char *field_name
)
438 return event_borrow_payload_field(event
, field_name
) != NULL
;
442 struct debug_info_source
*proc_debug_info_sources_get_entry(
443 struct proc_debug_info_sources
*proc_dbg_info_src
, uint64_t ip
)
445 struct debug_info_source
*debug_info_src
= NULL
;
446 gpointer key
= g_new0(uint64_t, 1);
448 gpointer baddr
, value
;
454 *((uint64_t *) key
) = ip
;
456 /* Look in IP to debug infos hash table first. */
457 debug_info_src
= g_hash_table_lookup(
458 proc_dbg_info_src
->ip_to_debug_info_src
,
460 if (debug_info_src
) {
464 /* Check in all bin_infos. */
465 g_hash_table_iter_init(&iter
, proc_dbg_info_src
->baddr_to_bin_info
);
467 while (g_hash_table_iter_next(&iter
, &baddr
, &value
))
469 struct bin_info
*bin
= value
;
471 if (!bin_info_has_address(value
, ip
)) {
476 * Found; add it to cache.
478 * FIXME: this should be bounded in size (and implement
479 * a caching policy), and entries should be prunned when
480 * libraries are unmapped.
482 debug_info_src
= debug_info_source_create_from_bin(bin
, ip
);
483 if (debug_info_src
) {
485 proc_dbg_info_src
->ip_to_debug_info_src
,
486 key
, debug_info_src
);
487 /* Ownership passed to ht. */
495 return debug_info_src
;
499 struct debug_info_source
*debug_info_query(struct debug_info
*debug_info
,
500 int64_t vpid
, uint64_t ip
)
502 struct debug_info_source
*dbg_info_src
= NULL
;
503 struct proc_debug_info_sources
*proc_dbg_info_src
;
505 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
506 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
507 if (!proc_dbg_info_src
) {
511 dbg_info_src
= proc_debug_info_sources_get_entry(proc_dbg_info_src
, ip
);
518 struct debug_info
*debug_info_create(struct debug_info_component
*comp
,
519 const bt_trace
*trace
, struct bt_fd_cache
*fdc
)
522 struct debug_info
*debug_info
;
528 debug_info
= g_new0(struct debug_info
, 1);
533 debug_info
->vpid_to_proc_dbg_info_src
= g_hash_table_new_full(
534 g_int64_hash
, g_int64_equal
, (GDestroyNotify
) g_free
,
535 (GDestroyNotify
) proc_debug_info_sources_destroy
);
536 if (!debug_info
->vpid_to_proc_dbg_info_src
) {
540 debug_info
->comp
= comp
;
541 ret
= debug_info_init(debug_info
);
546 debug_info
->input_trace
= trace
;
547 debug_info
->fd_cache
= fdc
;
557 void debug_info_destroy(struct debug_info
*debug_info
)
559 bt_trace_status status
;
564 if (debug_info
->vpid_to_proc_dbg_info_src
) {
565 g_hash_table_destroy(debug_info
->vpid_to_proc_dbg_info_src
);
568 status
= bt_trace_remove_destruction_listener(debug_info
->input_trace
,
569 debug_info
->destruction_listener_id
);
570 if (status
!= BT_TRACE_STATUS_OK
) {
571 BT_LOGD("Trace destruction listener removal failed.");
580 void handle_event_statedump_build_id(struct debug_info
*debug_info
,
581 const bt_event
*event
)
583 struct proc_debug_info_sources
*proc_dbg_info_src
;
584 uint64_t build_id_len
, baddr
;
585 uint8_t *build_id
= NULL
;
586 struct bin_info
*bin
;
590 event_get_common_context_signed_integer_field_value(event
,
591 VPID_FIELD_NAME
, &vpid
);
592 event_get_payload_unsigned_integer_field_value(event
,
593 BADDR_FIELD_NAME
, &baddr
);
595 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
596 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
597 if (!proc_dbg_info_src
) {
601 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
605 * The build_id event comes after the bin has been
606 * created. If it isn't found, just ignore this event.
610 ret
= event_get_payload_build_id_length(event
, BUILD_ID_FIELD_NAME
,
613 build_id
= g_new0(uint8_t, build_id_len
);
618 ret
= event_get_payload_build_id_value(event
, BUILD_ID_FIELD_NAME
,
624 ret
= bin_info_set_build_id(bin
, build_id
, build_id_len
);
630 * Reset the is_elf_only flag in case it had been set
631 * previously, because we might find separate debug info using
632 * the new build id information.
634 bin
->is_elf_only
= false;
642 void handle_event_statedump_debug_link(struct debug_info
*debug_info
,
643 const bt_event
*event
)
645 struct proc_debug_info_sources
*proc_dbg_info_src
;
646 struct bin_info
*bin
= NULL
;
649 const char *filename
= NULL
;
651 uint64_t crc_field_value
;
653 event_get_common_context_signed_integer_field_value(event
,
654 VPID_FIELD_NAME
, &vpid
);
656 event_get_payload_unsigned_integer_field_value(event
,
657 BADDR_FIELD_NAME
, &baddr
);
659 event_get_payload_unsigned_integer_field_value(event
,
660 CRC32_FIELD_NAME
, &crc_field_value
);
662 crc32
= (uint32_t) crc_field_value
;
664 event_get_payload_string_field_value(event
,
665 FILENAME_FIELD_NAME
, &filename
);
667 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
668 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
669 if (!proc_dbg_info_src
) {
673 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
,
677 * The debug_link event comes after the bin has been
678 * created. If it isn't found, just ignore this event.
683 bin_info_set_debug_link(bin
, filename
, crc32
);
690 void handle_bin_info_event(struct debug_info
*debug_info
,
691 const bt_event
*event
, bool has_pic_field
)
693 struct proc_debug_info_sources
*proc_dbg_info_src
;
694 struct bin_info
*bin
;
695 uint64_t baddr
, memsz
;
701 event_get_payload_unsigned_integer_field_value(event
,
702 MEMSZ_FIELD_NAME
, &memsz
);
708 event_get_payload_unsigned_integer_field_value(event
,
709 BADDR_FIELD_NAME
, &baddr
);
712 * This field is not produced by the dlopen event emitted before
715 if (!event_has_payload_field(event
, PATH_FIELD_NAME
)) {
718 event_get_payload_string_field_value(event
, PATH_FIELD_NAME
, &path
);
721 uint64_t is_pic_field_value
;
723 event_get_payload_unsigned_integer_field_value(event
,
724 IS_PIC_FIELD_NAME
, &is_pic_field_value
);
725 is_pic
= is_pic_field_value
== 1;
728 * dlopen has no is_pic field, because the shared
729 * object is always PIC.
734 event_get_common_context_signed_integer_field_value(event
,
735 VPID_FIELD_NAME
, &vpid
);
737 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
738 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
739 if (!proc_dbg_info_src
) {
743 key
= g_new0(uint64_t, 1);
748 *((uint64_t *) key
) = baddr
;
750 bin
= g_hash_table_lookup(proc_dbg_info_src
->baddr_to_bin_info
, key
);
755 bin
= bin_info_create(debug_info
->fd_cache
, path
, baddr
, memsz
, is_pic
,
756 debug_info
->comp
->arg_debug_dir
,
757 debug_info
->comp
->arg_target_prefix
);
762 g_hash_table_insert(proc_dbg_info_src
->baddr_to_bin_info
, key
, bin
);
763 /* Ownership passed to ht. */
772 void handle_event_statedump_bin_info(struct debug_info
*debug_info
,
773 const bt_event
*event
)
775 handle_bin_info_event(debug_info
, event
, true);
779 void handle_event_lib_load(struct debug_info
*debug_info
,
780 const bt_event
*event
)
782 handle_bin_info_event(debug_info
, event
, false);
786 void handle_event_lib_unload(struct debug_info
*debug_info
,
787 const bt_event
*event
)
790 struct proc_debug_info_sources
*proc_dbg_info_src
;
794 event_get_payload_unsigned_integer_field_value(event
, BADDR_FIELD_NAME
,
797 event_get_common_context_signed_integer_field_value(event
,
798 VPID_FIELD_NAME
, &vpid
);
800 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
801 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
802 if (!proc_dbg_info_src
) {
804 * It's an unload event for a library for which no load event
805 * was previously received.
810 ret
= g_hash_table_remove(proc_dbg_info_src
->baddr_to_bin_info
,
818 void handle_event_statedump_start(struct debug_info
*debug_info
,
819 const bt_event
*event
)
821 struct proc_debug_info_sources
*proc_dbg_info_src
;
824 event_get_common_context_signed_integer_field_value(
825 event
, VPID_FIELD_NAME
, &vpid
);
827 proc_dbg_info_src
= proc_debug_info_sources_ht_get_entry(
828 debug_info
->vpid_to_proc_dbg_info_src
, vpid
);
829 if (!proc_dbg_info_src
) {
833 g_hash_table_remove_all(proc_dbg_info_src
->baddr_to_bin_info
);
834 g_hash_table_remove_all(proc_dbg_info_src
->ip_to_debug_info_src
);
840 void trace_debug_info_remove_func(const bt_trace
*in_trace
, void *data
)
842 struct debug_info_msg_iter
*debug_it
= data
;
843 if (debug_it
->debug_info_map
) {
845 ret
= g_hash_table_remove(debug_it
->debug_info_map
,
846 (gpointer
) in_trace
);
852 void handle_event_statedump(struct debug_info_msg_iter
*debug_it
,
853 const bt_event
*event
)
855 const bt_event_class
*event_class
;
856 const char *event_name
;
858 const bt_trace
*trace
;
859 struct debug_info
*debug_info
;
864 event_class
= bt_event_borrow_class_const(event
);
866 event_name
= bt_event_class_get_name(event_class
);
868 trace
= bt_stream_borrow_trace_const(
869 bt_event_borrow_stream_const(event
));
871 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
, trace
);
873 debug_info
= debug_info_create(debug_it
->debug_info_component
,
874 trace
, &debug_it
->fd_cache
);
875 g_hash_table_insert(debug_it
->debug_info_map
, (gpointer
) trace
,
877 bt_trace_add_destruction_listener(trace
,
878 trace_debug_info_remove_func
, debug_it
,
879 &debug_info
->destruction_listener_id
);
882 q_event_name
= g_quark_try_string(event_name
);
884 if (q_event_name
== debug_info
->q_statedump_bin_info
) {
886 handle_event_statedump_bin_info(debug_info
, event
);
887 } else if (q_event_name
== debug_info
->q_dl_open
||
888 q_event_name
== debug_info
->q_lib_load
) {
890 * dl_open and lib_load events are both checked for since
891 * only dl_open was produced as of lttng-ust 2.8.
893 * lib_load, which is produced from lttng-ust 2.9+, is a lot
894 * more reliable since it will be emitted when other functions
895 * of the dlopen family are called (e.g. dlmopen) and when
896 * library are transitively loaded.
898 handle_event_lib_load(debug_info
, event
);
899 } else if (q_event_name
== debug_info
->q_statedump_start
) {
900 /* Start state dump */
901 handle_event_statedump_start(debug_info
, event
);
902 } else if (q_event_name
== debug_info
->q_statedump_debug_link
) {
903 /* Debug link info */
904 handle_event_statedump_debug_link(debug_info
, event
);
905 } else if (q_event_name
== debug_info
->q_statedump_build_id
) {
907 handle_event_statedump_build_id(debug_info
, event
);
908 } else if (q_event_name
== debug_info
-> q_lib_unload
) {
909 handle_event_lib_unload(debug_info
, event
);
916 void destroy_debug_info_comp(struct debug_info_component
*debug_info
)
922 g_free(debug_info
->arg_debug_dir
);
923 g_free(debug_info
->arg_debug_info_field_name
);
924 g_free(debug_info
->arg_target_prefix
);
929 void fill_debug_info_bin_field(struct debug_info_source
*dbg_info_src
,
930 bool full_path
, bt_field
*curr_field
)
932 bt_field_status status
;
934 BT_ASSERT(bt_field_get_class_type(curr_field
) ==
935 BT_FIELD_CLASS_TYPE_STRING
);
939 status
= bt_field_string_set_value(curr_field
,
940 dbg_info_src
->bin_path
);
942 status
= bt_field_string_set_value(curr_field
,
943 dbg_info_src
->short_bin_path
);
945 if (status
!= BT_FIELD_STATUS_OK
) {
946 BT_LOGE("Cannot set path component of \"bin\" "
947 "curr_field field's value: str-fc-addr=%p",
951 status
= bt_field_string_append(curr_field
, dbg_info_src
->bin_loc
);
952 if (status
!= BT_FIELD_STATUS_OK
) {
953 BT_LOGE("Cannot set bin location component of \"bin\" "
954 "curr_field field's value: str-fc-addr=%p",
958 status
= bt_field_string_set_value(curr_field
, "");
959 if (status
!= BT_FIELD_STATUS_OK
) {
960 BT_LOGE("Cannot set \"bin\" curr_field field's value: "
961 "str-fc-addr=%p", curr_field
);
967 void fill_debug_info_func_field(struct debug_info_source
*dbg_info_src
,
968 bt_field
*curr_field
)
970 bt_field_status status
;
972 BT_ASSERT(bt_field_get_class_type(curr_field
) ==
973 BT_FIELD_CLASS_TYPE_STRING
);
974 if (dbg_info_src
&& dbg_info_src
->func
) {
975 status
= bt_field_string_set_value(curr_field
,
978 status
= bt_field_string_set_value(curr_field
, "");
980 if (status
!= BT_FIELD_STATUS_OK
) {
981 BT_LOGE("Cannot set \"func\" curr_field field's value: "
982 "str-fc-addr=%p", curr_field
);
987 void fill_debug_info_src_field(struct debug_info_source
*dbg_info_src
,
988 bool full_path
, bt_field
*curr_field
)
990 bt_field_status status
;
992 BT_ASSERT(bt_field_get_class_type(curr_field
) ==
993 BT_FIELD_CLASS_TYPE_STRING
);
995 if (dbg_info_src
&& dbg_info_src
->src_path
) {
997 status
= bt_field_string_set_value(curr_field
,
998 dbg_info_src
->src_path
);
1000 status
= bt_field_string_set_value(curr_field
,
1001 dbg_info_src
->short_src_path
);
1003 if (status
!= BT_FIELD_STATUS_OK
) {
1004 BT_LOGE("Cannot set path component of \"src\" "
1005 "curr_field field's value: str-fc-addr=%p",
1009 status
= bt_field_string_append(curr_field
, ":");
1010 if (status
!= BT_FIELD_STATUS_OK
) {
1011 BT_LOGE("Cannot set colon component of \"src\" "
1012 "curr_field field's value: str-fc-addr=%p",
1016 status
= bt_field_string_append(curr_field
, dbg_info_src
->line_no
);
1017 if (status
!= BT_FIELD_STATUS_OK
) {
1018 BT_LOGE("Cannot set line number component of \"src\" "
1019 "curr_field field's value: str-fc-addr=%p",
1023 status
= bt_field_string_set_value(curr_field
, "");
1024 if (status
!= BT_FIELD_STATUS_OK
) {
1025 BT_LOGE("Cannot set \"src\" curr_field field's value: "
1026 "str-fc-addr=%p", curr_field
);
1031 void fill_debug_info_field_empty(bt_field
*debug_info_field
)
1033 bt_field_status status
;
1034 bt_field
*bin_field
, *func_field
, *src_field
;
1036 BT_ASSERT(bt_field_get_class_type(debug_info_field
) ==
1037 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1039 bin_field
= bt_field_structure_borrow_member_field_by_name(
1040 debug_info_field
, "bin");
1041 func_field
= bt_field_structure_borrow_member_field_by_name(
1042 debug_info_field
, "func");
1043 src_field
= bt_field_structure_borrow_member_field_by_name(
1044 debug_info_field
, "src");
1046 BT_ASSERT(bt_field_get_class_type(bin_field
) ==
1047 BT_FIELD_CLASS_TYPE_STRING
);
1048 BT_ASSERT(bt_field_get_class_type(func_field
) ==
1049 BT_FIELD_CLASS_TYPE_STRING
);
1050 BT_ASSERT(bt_field_get_class_type(src_field
) ==
1051 BT_FIELD_CLASS_TYPE_STRING
);
1053 status
= bt_field_string_set_value(bin_field
, "");
1054 if (status
!= BT_FIELD_STATUS_OK
) {
1055 BT_LOGE("Cannot set \"bin\" bin_field field's value: "
1056 "str-fc-addr=%p", bin_field
);
1059 status
= bt_field_string_set_value(func_field
, "");
1060 if (status
!= BT_FIELD_STATUS_OK
) {
1061 BT_LOGE("Cannot set \"func\" func_field field's value: "
1062 "str-fc-addr=%p", func_field
);
1065 status
= bt_field_string_set_value(src_field
, "");
1066 if (status
!= BT_FIELD_STATUS_OK
) {
1067 BT_LOGE("Cannot set \"src\" src_field field's value: "
1068 "str-fc-addr=%p", src_field
);
1072 void fill_debug_info_field(struct debug_info
*debug_info
, int64_t vpid
,
1073 uint64_t ip
, bt_field
*debug_info_field
)
1075 struct debug_info_source
*dbg_info_src
;
1076 const bt_field_class
*debug_info_fc
;
1078 BT_ASSERT(bt_field_get_class_type(debug_info_field
) ==
1079 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1081 debug_info_fc
= bt_field_borrow_class_const(debug_info_field
);
1083 BT_ASSERT(bt_field_class_structure_get_member_count(debug_info_fc
) == 3);
1085 dbg_info_src
= debug_info_query(debug_info
, vpid
, ip
);
1087 fill_debug_info_bin_field(dbg_info_src
, debug_info
->comp
->arg_full_path
,
1088 bt_field_structure_borrow_member_field_by_name(
1089 debug_info_field
, "bin"));
1090 fill_debug_info_func_field(dbg_info_src
,
1091 bt_field_structure_borrow_member_field_by_name(
1092 debug_info_field
, "func"));
1093 fill_debug_info_src_field(dbg_info_src
, debug_info
->comp
->arg_full_path
,
1094 bt_field_structure_borrow_member_field_by_name(
1095 debug_info_field
, "src"));
1099 void fill_debug_info_event_if_needed(struct debug_info_msg_iter
*debug_it
,
1100 const bt_event
*in_event
, bt_event
*out_event
)
1102 bt_field
*out_common_ctx_field
, *out_debug_info_field
;
1103 const bt_field
*vpid_field
, *ip_field
, *in_common_ctx_field
;
1104 const bt_field_class
*in_common_ctx_fc
;
1105 struct debug_info
*debug_info
;
1108 gchar
*debug_info_field_name
=
1109 debug_it
->debug_info_component
->arg_debug_info_field_name
;
1111 in_common_ctx_field
= bt_event_borrow_common_context_field_const(
1113 if (!in_common_ctx_field
) {
1115 * There is no event common context so no need to add debug
1121 in_common_ctx_fc
= bt_field_borrow_class_const(in_common_ctx_field
);
1122 if (!is_event_common_ctx_dbg_info_compatible(in_common_ctx_fc
,
1123 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1125 * The input event common context does not have the necessary
1126 * fields to resolve debug information.
1131 /* Borrow the debug-info field. */
1132 out_common_ctx_field
= bt_event_borrow_common_context_field(out_event
);
1133 if (!out_common_ctx_field
) {
1137 out_debug_info_field
= bt_field_structure_borrow_member_field_by_name(
1138 out_common_ctx_field
, debug_info_field_name
);
1140 vpid_field
= bt_field_structure_borrow_member_field_by_name_const(
1141 out_common_ctx_field
, VPID_FIELD_NAME
);
1142 ip_field
= bt_field_structure_borrow_member_field_by_name_const(
1143 out_common_ctx_field
, IP_FIELD_NAME
);
1145 vpid
= bt_field_signed_integer_get_value(vpid_field
);
1146 ip
= bt_field_unsigned_integer_get_value(ip_field
);
1149 * Borrow the debug_info structure needed for the source
1152 debug_info
= g_hash_table_lookup(debug_it
->debug_info_map
,
1153 bt_stream_borrow_trace_const(
1154 bt_event_borrow_stream_const(in_event
)));
1158 * Perform the debug-info resolving and set the event fields
1161 fill_debug_info_field(debug_info
, vpid
, ip
, out_debug_info_field
);
1163 BT_LOGD("No debug information for this trace. Setting debug "
1164 "info fields to empty strings.");
1165 fill_debug_info_field_empty(out_debug_info_field
);
1172 void update_event_statedump_if_needed(struct debug_info_msg_iter
*debug_it
,
1173 const bt_event
*in_event
)
1175 const bt_field
*event_common_ctx
;
1176 const bt_field_class
*event_common_ctx_fc
;
1177 const bt_event_class
*in_event_class
= bt_event_borrow_class_const(in_event
);
1180 * If the event is an lttng_ust_statedump event AND has the right event
1181 * common context fields update the debug-info view for this process.
1183 event_common_ctx
= bt_event_borrow_common_context_field_const(in_event
);
1184 if (!event_common_ctx
) {
1188 event_common_ctx_fc
= bt_field_borrow_class_const(event_common_ctx
);
1189 if (is_event_common_ctx_dbg_info_compatible(event_common_ctx_fc
,
1190 debug_it
->ir_maps
->debug_info_field_class_name
)) {
1191 /* Checkout if it might be a one of lttng ust statedump events. */
1192 const char *in_event_name
= bt_event_class_get_name(in_event_class
);
1193 if (strncmp(in_event_name
, LTTNG_UST_STATEDUMP_PREFIX
,
1194 strlen(LTTNG_UST_STATEDUMP_PREFIX
)) == 0) {
1195 /* Handle statedump events. */
1196 handle_event_statedump(debug_it
, in_event
);
1204 bt_message
*handle_event_message(struct debug_info_msg_iter
*debug_it
,
1205 const bt_message
*in_message
)
1207 const bt_clock_snapshot
*cs
;
1208 const bt_clock_class
*default_cc
;
1209 const bt_packet
*in_packet
;
1210 bt_clock_snapshot_state cs_state
;
1211 bt_event_class
*out_event_class
;
1212 bt_packet
*out_packet
;
1213 bt_event
*out_event
;
1215 bt_message
*out_message
= NULL
;
1217 /* Borrow the input event and its event class. */
1218 const bt_event
*in_event
=
1219 bt_message_event_borrow_event_const(in_message
);
1220 const bt_event_class
*in_event_class
=
1221 bt_event_borrow_class_const(in_event
);
1223 update_event_statedump_if_needed(debug_it
, in_event
);
1225 out_event_class
= trace_ir_mapping_borrow_mapped_event_class(
1226 debug_it
->ir_maps
, in_event_class
);
1227 if (!out_event_class
) {
1228 out_event_class
= trace_ir_mapping_create_new_mapped_event_class(
1229 debug_it
->ir_maps
, in_event_class
);
1231 BT_ASSERT(out_event_class
);
1233 /* Borrow the input and output packets. */
1234 in_packet
= bt_event_borrow_packet_const(in_event
);
1235 out_packet
= trace_ir_mapping_borrow_mapped_packet(debug_it
->ir_maps
,
1238 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1239 bt_event_class_borrow_stream_class_const(in_event_class
));
1241 /* Borrow event clock snapshot. */
1243 bt_message_event_borrow_default_clock_snapshot_const(
1246 /* Create an output event message. */
1247 BT_ASSERT (cs_state
== BT_CLOCK_SNAPSHOT_STATE_KNOWN
);
1248 out_message
= bt_message_event_create_with_default_clock_snapshot(
1249 debug_it
->input_iterator
,
1250 out_event_class
, out_packet
,
1251 bt_clock_snapshot_get_value(cs
));
1253 out_message
= bt_message_event_create(debug_it
->input_iterator
,
1254 out_event_class
, out_packet
);
1258 BT_LOGE("Error creating output event message.");
1262 out_event
= bt_message_event_borrow_event(out_message
);
1264 /* Copy the original fields to the output event. */
1265 copy_event_content(in_event
, out_event
);
1268 * Try to set the debug-info fields based on debug information that is
1271 fill_debug_info_event_if_needed(debug_it
, in_event
, out_event
);
1278 bt_message
*handle_stream_begin_message(struct debug_info_msg_iter
*debug_it
,
1279 const bt_message
*in_message
)
1281 const bt_stream
*in_stream
;
1282 bt_message
*out_message
;
1283 bt_stream
*out_stream
;
1285 in_stream
= bt_message_stream_beginning_borrow_stream_const(in_message
);
1286 BT_ASSERT(in_stream
);
1288 /* Create a duplicated output stream. */
1289 out_stream
= trace_ir_mapping_create_new_mapped_stream(
1290 debug_it
->ir_maps
, in_stream
);
1296 /* Create an output stream beginning message. */
1297 out_message
= bt_message_stream_beginning_create(
1298 debug_it
->input_iterator
, out_stream
);
1300 BT_LOGE("Error creating output stream beginning message: "
1301 "out-s-addr=%p", out_stream
);
1308 bt_message
*handle_stream_end_message(struct debug_info_msg_iter
*debug_it
,
1309 const bt_message
*in_message
)
1311 const bt_stream
*in_stream
;
1312 bt_message
*out_message
= NULL
;
1313 bt_stream
*out_stream
;
1315 in_stream
= bt_message_stream_end_borrow_stream_const(in_message
);
1316 BT_ASSERT(in_stream
);
1318 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1319 debug_it
->ir_maps
, in_stream
);
1320 BT_ASSERT(out_stream
);
1322 /* Create an output stream end message. */
1323 out_message
= bt_message_stream_end_create(debug_it
->input_iterator
,
1326 BT_LOGE("Error creating output stream end message: out-s-addr=%p",
1330 /* Remove stream from trace mapping hashtable. */
1331 trace_ir_mapping_remove_mapped_stream(debug_it
->ir_maps
, in_stream
);
1337 bt_message
*handle_packet_begin_message(struct debug_info_msg_iter
*debug_it
,
1338 const bt_message
*in_message
)
1340 const bt_clock_class
*default_cc
;
1341 bt_clock_snapshot_state cs_state
;
1342 const bt_clock_snapshot
*cs
;
1343 bt_message
*out_message
= NULL
;
1344 bt_packet
*out_packet
;
1346 const bt_packet
*in_packet
=
1347 bt_message_packet_beginning_borrow_packet_const(in_message
);
1348 BT_ASSERT(in_packet
);
1350 /* This packet should not be already mapped. */
1351 BT_ASSERT(!trace_ir_mapping_borrow_mapped_packet(
1352 debug_it
->ir_maps
, in_packet
));
1354 out_packet
= trace_ir_mapping_create_new_mapped_packet(debug_it
->ir_maps
,
1357 BT_ASSERT(out_packet
);
1359 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1360 bt_stream_borrow_class_const(
1361 bt_packet_borrow_stream_const(in_packet
)));
1363 /* Borrow clock snapshot. */
1365 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
1368 /* Create an output packet beginning message. */
1369 BT_ASSERT(cs_state
== BT_CLOCK_SNAPSHOT_STATE_KNOWN
);
1370 out_message
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1371 debug_it
->input_iterator
, out_packet
,
1372 bt_clock_snapshot_get_value(cs
));
1374 out_message
= bt_message_packet_beginning_create(
1375 debug_it
->input_iterator
, out_packet
);
1378 BT_LOGE("Error creating output packet beginning message: "
1379 "out-p-addr=%p", out_packet
);
1386 bt_message
*handle_packet_end_message(struct debug_info_msg_iter
*debug_it
,
1387 const bt_message
*in_message
)
1389 const bt_clock_snapshot
*cs
;
1390 const bt_packet
*in_packet
;
1391 const bt_clock_class
*default_cc
;
1392 bt_clock_snapshot_state cs_state
;
1393 bt_message
*out_message
= NULL
;
1394 bt_packet
*out_packet
;
1396 in_packet
= bt_message_packet_end_borrow_packet_const(in_message
);
1397 BT_ASSERT(in_packet
);
1399 out_packet
= trace_ir_mapping_borrow_mapped_packet(debug_it
->ir_maps
, in_packet
);
1400 BT_ASSERT(out_packet
);
1402 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1403 bt_stream_borrow_class_const(
1404 bt_packet_borrow_stream_const(in_packet
)));
1406 /* Borrow clock snapshot. */
1408 bt_message_packet_end_borrow_default_clock_snapshot_const(
1411 /* Create an outpute packet end message. */
1412 BT_ASSERT(cs_state
== BT_CLOCK_SNAPSHOT_STATE_KNOWN
);
1413 out_message
= bt_message_packet_end_create_with_default_clock_snapshot(
1414 debug_it
->input_iterator
, out_packet
,
1415 bt_clock_snapshot_get_value(cs
));
1417 out_message
= bt_message_packet_end_create(
1418 debug_it
->input_iterator
, out_packet
);
1422 BT_LOGE("Error creating output packet end message: "
1423 "out-p-addr=%p", out_packet
);
1426 /* Remove packet from data mapping hashtable. */
1427 trace_ir_mapping_remove_mapped_packet(debug_it
->ir_maps
, in_packet
);
1433 bt_message
*handle_msg_iterator_inactivity(struct debug_info_msg_iter
*debug_it
,
1434 const bt_message
*in_message
)
1437 * This message type can be forwarded directly because it does
1438 * not refer to any objects in the trace class.
1440 bt_message_get_ref(in_message
);
1441 return (bt_message
*) in_message
;
1445 bt_message
*handle_stream_act_begin_message(struct debug_info_msg_iter
*debug_it
,
1446 const bt_message
*in_message
)
1448 const bt_clock_snapshot
*cs
;
1449 const bt_clock_class
*default_cc
;
1450 bt_message
*out_message
= NULL
;
1451 bt_stream
*out_stream
;
1453 bt_message_stream_activity_clock_snapshot_state cs_state
;
1455 const bt_stream
*in_stream
=
1456 bt_message_stream_activity_beginning_borrow_stream_const(
1458 BT_ASSERT(in_stream
);
1460 out_stream
= trace_ir_mapping_borrow_mapped_stream(debug_it
->ir_maps
,
1462 BT_ASSERT(out_stream
);
1464 out_message
= bt_message_stream_activity_beginning_create(
1465 debug_it
->input_iterator
, out_stream
);
1467 BT_LOGE("Error creating output stream activity beginning "
1468 "message: out-s-addr=%p", out_stream
);
1472 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1473 bt_stream_borrow_class_const(in_stream
));
1475 /* Borrow clock snapshot. */
1477 bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
1480 if (cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1481 cs_value
= bt_clock_snapshot_get_value(cs
);
1482 bt_message_stream_activity_beginning_set_default_clock_snapshot(
1483 out_message
, cs_value
);
1485 bt_message_stream_activity_beginning_set_default_clock_snapshot_state(
1486 out_message
, cs_state
);
1495 bt_message
*handle_stream_act_end_message(struct debug_info_msg_iter
*debug_it
,
1496 const bt_message
*in_message
)
1498 const bt_clock_snapshot
*cs
;
1499 const bt_clock_class
*default_cc
;
1500 const bt_stream
*in_stream
;
1501 bt_message
*out_message
;
1502 bt_stream
*out_stream
;
1504 bt_message_stream_activity_clock_snapshot_state cs_state
;
1506 in_stream
= bt_message_stream_activity_end_borrow_stream_const(
1508 BT_ASSERT(in_stream
);
1510 out_stream
= trace_ir_mapping_borrow_mapped_stream(debug_it
->ir_maps
,
1512 BT_ASSERT(out_stream
);
1514 out_message
= bt_message_stream_activity_end_create(
1515 debug_it
->input_iterator
, out_stream
);
1517 BT_LOGE("Error creating output stream activity end message: "
1518 "out-s-addr=%p", out_stream
);
1522 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1523 bt_stream_borrow_class_const(in_stream
));
1527 bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
1530 if (cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1531 cs_value
= bt_clock_snapshot_get_value(cs
);
1532 bt_message_stream_activity_end_set_default_clock_snapshot(
1533 out_message
, cs_value
);
1535 bt_message_stream_activity_end_set_default_clock_snapshot_state(
1536 out_message
, cs_state
);
1545 bt_message
*handle_discarded_events_message(struct debug_info_msg_iter
*debug_it
,
1546 const bt_message
*in_message
)
1548 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1549 const bt_stream
*in_stream
;
1550 const bt_clock_class
*default_cc
;
1551 uint64_t discarded_events
, begin_cs_value
, end_cs_value
;
1552 bt_clock_snapshot_state begin_cs_state
, end_cs_state
;
1553 bt_property_availability prop_avail
;
1554 bt_message
*out_message
= NULL
;
1555 bt_stream
*out_stream
;
1557 in_stream
= bt_message_discarded_events_borrow_stream_const(
1559 BT_ASSERT(in_stream
);
1561 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1562 debug_it
->ir_maps
, in_stream
);
1563 BT_ASSERT(out_stream
);
1565 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1566 bt_stream_borrow_class_const(in_stream
));
1569 bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
1570 in_message
, &begin_cs
);
1572 bt_message_discarded_events_borrow_default_end_clock_snapshot_const(
1573 in_message
, &end_cs
);
1575 * Both clock snapshots should be known as we check that the
1576 * all input stream classes have an always known clock. Unknown
1577 * clock is not yet supported.
1579 BT_ASSERT(begin_cs_state
== BT_CLOCK_SNAPSHOT_STATE_KNOWN
&&
1580 end_cs_state
== BT_CLOCK_SNAPSHOT_STATE_KNOWN
);
1582 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1583 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1586 bt_message_discarded_events_create_with_default_clock_snapshots(
1587 debug_it
->input_iterator
, out_stream
,
1588 begin_cs_value
, end_cs_value
);
1590 out_message
= bt_message_discarded_events_create(
1591 debug_it
->input_iterator
, out_stream
);
1594 BT_LOGE("Error creating output discarded events message: "
1595 "out-s-addr=%p", out_stream
);
1599 prop_avail
= bt_message_discarded_events_get_count(in_message
,
1602 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1603 bt_message_discarded_events_set_count(out_message
,
1612 bt_message
*handle_discarded_packets_message(struct debug_info_msg_iter
*debug_it
,
1613 const bt_message
*in_message
)
1615 const bt_clock_snapshot
*begin_cs
, *end_cs
;
1616 const bt_clock_class
*default_cc
;
1617 const bt_stream
*in_stream
;
1618 uint64_t discarded_packets
, begin_cs_value
, end_cs_value
;
1619 bt_clock_snapshot_state begin_cs_state
, end_cs_state
;
1620 bt_property_availability prop_avail
;
1621 bt_message
*out_message
= NULL
;
1622 bt_stream
*out_stream
;
1624 in_stream
= bt_message_discarded_packets_borrow_stream_const(
1626 BT_ASSERT(in_stream
);
1628 out_stream
= trace_ir_mapping_borrow_mapped_stream(
1629 debug_it
->ir_maps
, in_stream
);
1630 BT_ASSERT(out_stream
);
1632 default_cc
= bt_stream_class_borrow_default_clock_class_const(
1633 bt_stream_borrow_class_const(in_stream
));
1636 bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
1637 in_message
, &begin_cs
);
1640 bt_message_discarded_packets_borrow_default_end_clock_snapshot_const(
1641 in_message
, &end_cs
);
1644 * Both clock snapshots should be known as we check that the
1645 * all input stream classes have an always known clock. Unknown
1646 * clock is not yet supported.
1648 BT_ASSERT(begin_cs_state
== BT_CLOCK_SNAPSHOT_STATE_KNOWN
&&
1649 end_cs_state
== BT_CLOCK_SNAPSHOT_STATE_KNOWN
);
1651 begin_cs_value
= bt_clock_snapshot_get_value(begin_cs
);
1652 end_cs_value
= bt_clock_snapshot_get_value(end_cs
);
1654 out_message
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1655 debug_it
->input_iterator
, out_stream
,
1656 begin_cs_value
, end_cs_value
);
1658 out_message
= bt_message_discarded_packets_create(
1659 debug_it
->input_iterator
, out_stream
);
1662 BT_LOGE("Error creating output discarded packet message: "
1663 "out-s-addr=%p", out_stream
);
1667 prop_avail
= bt_message_discarded_packets_get_count(in_message
,
1668 &discarded_packets
);
1669 if (prop_avail
== BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1670 bt_message_discarded_packets_set_count(out_message
,
1679 const bt_message
*handle_message(struct debug_info_msg_iter
*debug_it
,
1680 const bt_message
*in_message
)
1682 bt_message
*out_message
= NULL
;
1684 switch (bt_message_get_type(in_message
)) {
1685 case BT_MESSAGE_TYPE_EVENT
:
1686 out_message
= handle_event_message(debug_it
,
1689 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1690 out_message
= handle_packet_begin_message(debug_it
,
1693 case BT_MESSAGE_TYPE_PACKET_END
:
1694 out_message
= handle_packet_end_message(debug_it
,
1697 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1698 out_message
= handle_stream_begin_message(debug_it
,
1701 case BT_MESSAGE_TYPE_STREAM_END
:
1702 out_message
= handle_stream_end_message(debug_it
,
1705 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1706 out_message
= handle_msg_iterator_inactivity(debug_it
,
1709 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1710 out_message
= handle_stream_act_begin_message(debug_it
,
1713 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1714 out_message
= handle_stream_act_end_message(debug_it
,
1717 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1718 out_message
= handle_discarded_events_message(debug_it
,
1721 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1722 out_message
= handle_discarded_packets_message(debug_it
,
1734 int init_from_params(struct debug_info_component
*debug_info_component
,
1735 const bt_value
*params
)
1737 const bt_value
*value
= NULL
;
1742 value
= bt_value_map_borrow_entry_value_const(params
,
1743 "debug-info-field-name");
1745 debug_info_component
->arg_debug_info_field_name
=
1746 g_strdup(bt_value_string_get(value
));
1748 debug_info_component
->arg_debug_info_field_name
=
1749 g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME
);
1752 value
= bt_value_map_borrow_entry_value_const(params
, "debug-info-dir");
1754 debug_info_component
->arg_debug_dir
=
1755 g_strdup(bt_value_string_get(value
));
1757 debug_info_component
->arg_debug_dir
= NULL
;
1761 value
= bt_value_map_borrow_entry_value_const(params
, "target-prefix");
1763 debug_info_component
->arg_target_prefix
=
1764 g_strdup(bt_value_string_get(value
));
1766 debug_info_component
->arg_target_prefix
= NULL
;
1769 value
= bt_value_map_borrow_entry_value_const(params
, "full-path");
1771 debug_info_component
->arg_full_path
= bt_value_bool_get(value
);
1773 debug_info_component
->arg_full_path
= BT_FALSE
;
1780 bt_self_component_status
debug_info_comp_init(
1781 bt_self_component_filter
*self_comp
,
1782 const bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1785 struct debug_info_component
*debug_info_comp
;
1786 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
1788 BT_LOGD("Initializing debug_info component: "
1789 "comp-addr=%p, params-addr=%p", self_comp
, params
);
1791 debug_info_comp
= g_new0(struct debug_info_component
, 1);
1792 if (!debug_info_comp
) {
1793 BT_LOGE_STR("Failed to allocate one debug_info component.");
1797 bt_self_component_set_data(
1798 bt_self_component_filter_as_self_component(self_comp
),
1801 status
= bt_self_component_filter_add_input_port(self_comp
, "in",
1803 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
1807 status
= bt_self_component_filter_add_output_port(self_comp
, "out",
1809 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
1813 ret
= init_from_params(debug_info_comp
, params
);
1815 BT_LOGE("Cannot configure debug_info component: "
1816 "debug_info-comp-addr=%p, params-addr=%p",
1817 debug_info_comp
, params
);
1824 destroy_debug_info_comp(debug_info_comp
);
1825 bt_self_component_set_data(
1826 bt_self_component_filter_as_self_component(self_comp
),
1829 if (status
== BT_SELF_COMPONENT_STATUS_OK
) {
1830 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
1837 void debug_info_comp_finalize(bt_self_component_filter
*self_comp
)
1839 struct debug_info_component
*debug_info
=
1840 bt_self_component_get_data(
1841 bt_self_component_filter_as_self_component(
1843 BT_LOGD("Finalizing debug_info self_component: comp-addr=%p",
1846 destroy_debug_info_comp(debug_info
);
1850 bt_self_message_iterator_status
debug_info_msg_iter_next(
1851 bt_self_message_iterator
*self_msg_iter
,
1852 const bt_message_array_const msgs
, uint64_t capacity
,
1855 bt_self_component_port_input_message_iterator
*upstream_iterator
= NULL
;
1856 bt_message_iterator_status upstream_iterator_ret_status
;
1857 struct debug_info_msg_iter
*debug_info_msg_iter
;
1858 struct debug_info_component
*debug_info
= NULL
;
1859 bt_self_message_iterator_status status
;
1860 bt_self_component
*self_comp
= NULL
;
1861 bt_message_array_const input_msgs
;
1862 const bt_message
*out_message
;
1863 uint64_t curr_msg_idx
, i
;
1865 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1867 self_comp
= bt_self_message_iterator_borrow_component(self_msg_iter
);
1868 BT_ASSERT(self_comp
);
1870 debug_info
= bt_self_component_get_data(self_comp
);
1871 BT_ASSERT(debug_info
);
1873 debug_info_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
1874 BT_ASSERT(debug_info_msg_iter
);
1876 upstream_iterator
= debug_info_msg_iter
->msg_iter
;
1877 BT_ASSERT(upstream_iterator
);
1879 upstream_iterator_ret_status
=
1880 bt_self_component_port_input_message_iterator_next(
1881 upstream_iterator
, &input_msgs
, count
);
1882 if (upstream_iterator_ret_status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
1884 * No messages were returned. Not necessarily an error. Convert
1885 * the upstream message iterator status to a self status.
1887 status
= bt_common_message_iterator_status_to_self(
1888 upstream_iterator_ret_status
);
1893 * There should never be more received messages than the capacity we
1896 BT_ASSERT(*count
<= capacity
);
1898 for (curr_msg_idx
= 0; curr_msg_idx
< *count
; curr_msg_idx
++) {
1899 out_message
= handle_message(debug_info_msg_iter
,
1900 input_msgs
[curr_msg_idx
]);
1902 goto handle_msg_error
;
1905 msgs
[curr_msg_idx
] = out_message
;
1907 * Drop our reference of the input message as we are done with
1908 * it and created a output copy.
1910 bt_message_put_ref(input_msgs
[curr_msg_idx
]);
1917 * Drop references of all the output messages created before the
1920 for (i
= 0; i
< curr_msg_idx
; i
++) {
1921 bt_message_put_ref(msgs
[i
]);
1924 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1930 void debug_info_msg_iter_destroy(struct debug_info_msg_iter
*debug_info_msg_iter
)
1932 if (!debug_info_msg_iter
) {
1936 if (debug_info_msg_iter
->msg_iter
) {
1937 bt_self_component_port_input_message_iterator_put_ref(
1938 debug_info_msg_iter
->msg_iter
);
1941 if (debug_info_msg_iter
->ir_maps
) {
1942 trace_ir_maps_destroy(debug_info_msg_iter
->ir_maps
);
1945 if (debug_info_msg_iter
->debug_info_map
) {
1946 g_hash_table_destroy(debug_info_msg_iter
->debug_info_map
);
1949 bt_fd_cache_fini(&debug_info_msg_iter
->fd_cache
);
1950 g_free(debug_info_msg_iter
);
1957 bt_self_message_iterator_status
debug_info_msg_iter_init(
1958 bt_self_message_iterator
*self_msg_iter
,
1959 bt_self_component_filter
*self_comp
,
1960 bt_self_component_port_output
*self_port
)
1962 bt_self_message_iterator_status status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
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
;
1969 /* Borrow the upstream input port. */
1970 input_port
= bt_self_component_filter_borrow_input_port_by_name(
1973 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1977 debug_info_msg_iter
= g_new0(struct debug_info_msg_iter
, 1);
1978 if (!debug_info_msg_iter
) {
1979 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1983 /* Create an iterator on the upstream component. */
1984 upstream_iterator
= bt_self_component_port_input_message_iterator_create(
1986 if (!upstream_iterator
) {
1987 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1991 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
1992 debug_info_msg_iter
->msg_iter
, upstream_iterator
);
1994 /* Create hashtable that will contain debug info mapping. */
1995 debug_info_msg_iter
->debug_info_map
= g_hash_table_new_full(
1996 g_direct_hash
, g_direct_equal
, (GDestroyNotify
) NULL
,
1997 (GDestroyNotify
) debug_info_destroy
);
1998 if (!debug_info_msg_iter
->debug_info_map
) {
1999 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
2003 debug_info_msg_iter
->self_comp
=
2004 bt_self_component_filter_as_self_component(self_comp
);
2006 debug_info_msg_iter
->debug_info_component
= bt_self_component_get_data(
2007 bt_self_component_filter_as_self_component(
2010 debug_info_field_name
=
2011 debug_info_msg_iter
->debug_info_component
->arg_debug_info_field_name
;
2013 debug_info_msg_iter
->ir_maps
= trace_ir_maps_create(
2014 bt_self_component_filter_as_self_component(self_comp
),
2015 debug_info_field_name
);
2016 if (!debug_info_msg_iter
->ir_maps
) {
2017 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
2021 ret
= bt_fd_cache_init(&debug_info_msg_iter
->fd_cache
);
2023 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
2027 bt_self_message_iterator_set_data(self_msg_iter
, debug_info_msg_iter
);
2028 debug_info_msg_iter
->input_iterator
= self_msg_iter
;
2033 debug_info_msg_iter_destroy(debug_info_msg_iter
);
2039 bt_bool
debug_info_msg_iter_can_seek_beginning(
2040 bt_self_message_iterator
*self_msg_iter
)
2042 struct debug_info_msg_iter
*debug_info_msg_iter
=
2043 bt_self_message_iterator_get_data(self_msg_iter
);
2044 BT_ASSERT(debug_info_msg_iter
);
2046 return bt_self_component_port_input_message_iterator_can_seek_beginning(
2047 debug_info_msg_iter
->msg_iter
);
2051 bt_self_message_iterator_status
debug_info_msg_iter_seek_beginning(
2052 bt_self_message_iterator
*self_msg_iter
)
2054 struct debug_info_msg_iter
*debug_info_msg_iter
=
2055 bt_self_message_iterator_get_data(self_msg_iter
);
2056 bt_message_iterator_status status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
2058 BT_ASSERT(debug_info_msg_iter
);
2060 /* Ask the upstream component to seek to the beginning. */
2061 status
= bt_self_component_port_input_message_iterator_seek_beginning(
2062 debug_info_msg_iter
->msg_iter
);
2063 if (status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
2067 /* Clear this iterator data. */
2068 trace_ir_maps_clear(debug_info_msg_iter
->ir_maps
);
2069 g_hash_table_remove_all(debug_info_msg_iter
->debug_info_map
);
2071 return bt_common_message_iterator_status_to_self(status
);
2075 void debug_info_msg_iter_finalize(bt_self_message_iterator
*it
)
2077 struct debug_info_msg_iter
*debug_info_msg_iter
;
2079 debug_info_msg_iter
= bt_self_message_iterator_get_data(it
);
2080 BT_ASSERT(debug_info_msg_iter
);
2082 debug_info_msg_iter_destroy(debug_info_msg_iter
);