4 * Babeltrace Debug Info Plug-in
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@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 #include <babeltrace/plugin/plugin-dev.h>
30 #include <babeltrace/component/component.h>
31 #include <babeltrace/component/component-filter.h>
32 #include <babeltrace/component/port.h>
33 #include <babeltrace/component/connection.h>
34 #include <babeltrace/component/notification/notification.h>
35 #include <babeltrace/component/notification/iterator.h>
36 #include <babeltrace/component/notification/event.h>
37 #include <babeltrace/component/notification/packet.h>
38 #include <babeltrace/component/notification/stream.h>
39 #include <plugins-common.h>
41 #include "debug-info.h"
45 void destroy_debug_info_data(struct debug_info_component
*debug_info
)
47 free(debug_info
->arg_debug_info_field_name
);
52 void destroy_debug_info_component(struct bt_component
*component
)
54 void *data
= bt_component_get_private_data(component
);
55 destroy_debug_info_data(data
);
59 struct debug_info_component
*create_debug_info_component_data(void)
61 struct debug_info_component
*debug_info
;
63 debug_info
= g_new0(struct debug_info_component
, 1);
67 debug_info
->err
= stderr
;
74 void unref_debug_info(struct debug_info
*debug_info
)
76 debug_info_destroy(debug_info
);
80 void unref_trace(struct bt_ctf_trace
*trace
)
86 void unref_stream(struct bt_ctf_stream
*stream
)
92 void unref_packet(struct bt_ctf_packet
*packet
)
98 void unref_stream_class(struct bt_ctf_stream_class
*stream_class
)
100 bt_put(stream_class
);
104 void debug_info_iterator_destroy(struct bt_notification_iterator
*it
)
106 struct debug_info_iterator
*it_data
;
108 it_data
= bt_notification_iterator_get_private_data(it
);
111 if (it_data
->input_iterator_group
) {
112 g_ptr_array_free(it_data
->input_iterator_group
, TRUE
);
114 bt_put(it_data
->current_notification
);
115 bt_put(it_data
->input_iterator
);
116 g_hash_table_destroy(it_data
->trace_map
);
117 g_hash_table_destroy(it_data
->stream_map
);
118 g_hash_table_destroy(it_data
->stream_class_map
);
119 g_hash_table_destroy(it_data
->packet_map
);
120 g_hash_table_destroy(it_data
->trace_debug_map
);
125 struct bt_notification
*handle_notification(FILE *err
,
126 struct debug_info_iterator
*debug_it
,
127 struct bt_notification
*notification
)
129 struct bt_notification
*new_notification
= NULL
;
131 switch (bt_notification_get_type(notification
)) {
132 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
134 struct bt_ctf_packet
*packet
=
135 bt_notification_packet_begin_get_packet(notification
);
136 struct bt_ctf_packet
*writer_packet
;
142 writer_packet
= debug_info_new_packet(debug_it
, packet
);
143 assert(writer_packet
);
144 new_notification
= bt_notification_packet_begin_create(
146 assert(new_notification
);
150 case BT_NOTIFICATION_TYPE_PACKET_END
:
152 struct bt_ctf_packet
*packet
=
153 bt_notification_packet_end_get_packet(notification
);
154 struct bt_ctf_packet
*writer_packet
;
160 writer_packet
= debug_info_close_packet(debug_it
, packet
);
161 assert(writer_packet
);
162 new_notification
= bt_notification_packet_end_create(
164 assert(new_notification
);
166 bt_put(writer_packet
);
169 case BT_NOTIFICATION_TYPE_EVENT
:
171 struct bt_ctf_event
*event
= bt_notification_event_get_event(
173 struct bt_ctf_event
*writer_event
;
174 struct bt_clock_class_priority_map
*cc_prio_map
=
175 bt_notification_event_get_clock_class_priority_map(
181 writer_event
= debug_info_output_event(debug_it
, event
);
182 assert(writer_event
);
183 new_notification
= bt_notification_event_create(writer_event
,
186 assert(new_notification
);
188 bt_put(writer_event
);
191 case BT_NOTIFICATION_TYPE_STREAM_END
:
193 struct bt_ctf_stream
*stream
=
194 bt_notification_stream_end_get_stream(notification
);
195 struct bt_ctf_stream
*writer_stream
;
201 writer_stream
= debug_info_stream_end(debug_it
, stream
);
202 assert(writer_stream
);
203 new_notification
= bt_notification_stream_end_create(
205 assert(new_notification
);
207 bt_put(writer_stream
);
211 puts("Unhandled notification type");
215 return new_notification
;
219 enum bt_notification_iterator_status
debug_info_iterator_next(
220 struct bt_notification_iterator
*iterator
)
222 struct debug_info_iterator
*debug_it
= NULL
;
223 struct bt_component
*component
= NULL
;
224 struct debug_info_component
*debug_info
= NULL
;
225 struct bt_notification_iterator
*source_it
= NULL
;
226 enum bt_notification_iterator_status ret
=
227 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
228 struct bt_notification
*notification
, *new_notification
;
230 debug_it
= bt_notification_iterator_get_private_data(iterator
);
233 component
= bt_notification_iterator_get_component(iterator
);
235 debug_info
= bt_component_get_private_data(component
);
238 source_it
= debug_it
->input_iterator
;
240 ret
= bt_notification_iterator_next(source_it
);
241 if (ret
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
245 notification
= bt_notification_iterator_get_notification(
248 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
252 new_notification
= handle_notification(debug_info
->err
, debug_it
,
254 assert(new_notification
);
255 bt_put(notification
);
257 BT_MOVE(debug_it
->current_notification
, new_notification
);
265 struct bt_notification
*debug_info_iterator_get(
266 struct bt_notification_iterator
*iterator
)
268 struct debug_info_iterator
*debug_it
;
270 debug_it
= bt_notification_iterator_get_private_data(iterator
);
273 if (!debug_it
->current_notification
) {
274 enum bt_notification_iterator_status it_ret
;
276 it_ret
= debug_info_iterator_next(iterator
);
283 return bt_get(debug_it
->current_notification
);
287 enum bt_notification_iterator_status
debug_info_iterator_seek_time(
288 struct bt_notification_iterator
*iterator
, int64_t time
)
290 enum bt_notification_iterator_status ret
;
292 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
298 enum bt_notification_iterator_status
debug_info_iterator_init(struct bt_component
*component
,
299 struct bt_notification_iterator
*iterator
,
300 UNUSED_VAR
void *init_method_data
)
302 enum bt_notification_iterator_status ret
=
303 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
304 enum bt_notification_iterator_status it_ret
;
305 struct bt_port
*input_port
= NULL
;
306 struct bt_connection
*connection
= NULL
;
307 struct debug_info_iterator
*it_data
= g_new0(struct debug_info_iterator
, 1);
310 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
314 /* Create a new iterator on the upstream component. */
315 input_port
= bt_component_filter_get_default_input_port(component
);
317 connection
= bt_port_get_connection(input_port
, 0);
320 it_data
->input_iterator
= bt_connection_create_notification_iterator(
322 if (!it_data
->input_iterator
) {
323 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
326 it_data
->debug_info_component
= (struct debug_info_component
*)
327 bt_component_get_private_data(component
);
329 it_data
->err
= it_data
->debug_info_component
->err
;
330 it_data
->trace_map
= g_hash_table_new_full(g_direct_hash
,
331 g_direct_equal
, NULL
, (GDestroyNotify
) unref_trace
);
332 it_data
->stream_map
= g_hash_table_new_full(g_direct_hash
,
333 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream
);
334 it_data
->stream_class_map
= g_hash_table_new_full(g_direct_hash
,
335 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream_class
);
336 it_data
->packet_map
= g_hash_table_new_full(g_direct_hash
,
337 g_direct_equal
, NULL
, (GDestroyNotify
) unref_packet
);
338 it_data
->trace_debug_map
= g_hash_table_new_full(g_direct_hash
,
339 g_direct_equal
, NULL
, (GDestroyNotify
) unref_debug_info
);
341 it_ret
= bt_notification_iterator_set_private_data(iterator
, it_data
);
353 enum bt_component_status
init_from_params(
354 struct debug_info_component
*debug_info_component
,
355 struct bt_value
*params
)
357 struct bt_value
*value
= NULL
;
358 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
362 value
= bt_value_map_get(params
, "debug-info-field-name");
364 enum bt_value_status value_ret
;
367 value_ret
= bt_value_string_get(value
, &tmp
);
369 ret
= BT_COMPONENT_STATUS_INVALID
;
370 printf_error("Failed to retrieve debug-info-field-name value. "
371 "Expecting a string");
373 strcpy(debug_info_component
->arg_debug_info_field_name
, tmp
);
376 debug_info_component
->arg_debug_info_field_name
=
377 malloc(strlen("debug_info") + 1);
378 if (!debug_info_component
->arg_debug_info_field_name
) {
379 ret
= BT_COMPONENT_STATUS_NOMEM
;
382 sprintf(debug_info_component
->arg_debug_info_field_name
,
385 if (ret
!= BT_COMPONENT_STATUS_OK
) {
389 value
= bt_value_map_get(params
, "debug-dir");
391 enum bt_value_status value_ret
;
393 value_ret
= bt_value_string_get(value
,
394 &debug_info_component
->arg_debug_dir
);
396 ret
= BT_COMPONENT_STATUS_INVALID
;
397 printf_error("Failed to retrieve debug-dir value. "
398 "Expecting a string");
402 if (ret
!= BT_COMPONENT_STATUS_OK
) {
406 value
= bt_value_map_get(params
, "target-prefix");
408 enum bt_value_status value_ret
;
410 value_ret
= bt_value_string_get(value
,
411 &debug_info_component
->arg_target_prefix
);
413 ret
= BT_COMPONENT_STATUS_INVALID
;
414 printf_error("Failed to retrieve target-prefix value. "
415 "Expecting a string");
419 if (ret
!= BT_COMPONENT_STATUS_OK
) {
423 value
= bt_value_map_get(params
, "full-path");
425 enum bt_value_status value_ret
;
427 value_ret
= bt_value_bool_get(value
,
428 &debug_info_component
->arg_full_path
);
430 ret
= BT_COMPONENT_STATUS_INVALID
;
431 printf_error("Failed to retrieve full-path value. "
432 "Expecting a boolean");
436 if (ret
!= BT_COMPONENT_STATUS_OK
) {
444 enum bt_component_status
debug_info_component_init(
445 struct bt_component
*component
, struct bt_value
*params
,
446 UNUSED_VAR
void *init_method_data
)
448 enum bt_component_status ret
;
449 struct debug_info_component
*debug_info
= create_debug_info_component_data();
452 ret
= BT_COMPONENT_STATUS_NOMEM
;
456 ret
= bt_component_set_private_data(component
, debug_info
);
457 if (ret
!= BT_COMPONENT_STATUS_OK
) {
461 ret
= init_from_params(debug_info
, params
);
465 destroy_debug_info_data(debug_info
);
469 /* Initialize plug-in entry points. */
470 BT_PLUGIN(debug_info
);
471 BT_PLUGIN_DESCRIPTION("Babeltrace Debug Informations Plug-In.");
472 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
473 BT_PLUGIN_LICENSE("MIT");
475 BT_PLUGIN_FILTER_COMPONENT_CLASS(debug_info
, debug_info_iterator_get
,
476 debug_info_iterator_next
);
477 BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION(debug_info
,
478 "Add the debug information to events if possible.");
479 BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD(debug_info
, debug_info_component_init
);
480 BT_PLUGIN_FILTER_COMPONENT_CLASS_DESTROY_METHOD(debug_info
, destroy_debug_info_component
);
481 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_INIT_METHOD(debug_info
,
482 debug_info_iterator_init
);
483 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_DESTROY_METHOD(debug_info
,
484 debug_info_iterator_destroy
);
485 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_SEEK_TIME_METHOD(debug_info
,
486 debug_info_iterator_seek_time
);