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/graph/notification-iterator.h>
30 #include <babeltrace/graph/private-notification-iterator.h>
31 #include <babeltrace/graph/notification.h>
32 #include <babeltrace/graph/notification-event.h>
33 #include <babeltrace/graph/notification-stream.h>
34 #include <babeltrace/graph/notification-packet.h>
35 #include <babeltrace/graph/component-filter.h>
36 #include <babeltrace/graph/private-component-filter.h>
37 #include <babeltrace/graph/private-port.h>
38 #include <babeltrace/graph/private-connection.h>
39 #include <babeltrace/graph/private-component.h>
40 #include <babeltrace/plugin/plugin-dev.h>
41 #include <plugins-common.h>
43 #include "debug-info.h"
47 void destroy_debug_info_data(struct debug_info_component
*debug_info
)
49 free(debug_info
->arg_debug_info_field_name
);
54 void destroy_debug_info_component(struct bt_private_component
*component
)
56 void *data
= bt_private_component_get_user_data(component
);
57 destroy_debug_info_data(data
);
61 struct debug_info_component
*create_debug_info_component_data(void)
63 struct debug_info_component
*debug_info
;
65 debug_info
= g_new0(struct debug_info_component
, 1);
69 debug_info
->err
= stderr
;
76 void unref_debug_info(struct debug_info
*debug_info
)
78 debug_info_destroy(debug_info
);
82 void unref_trace(struct bt_ctf_trace
*trace
)
88 void unref_stream(struct bt_ctf_stream
*stream
)
94 void unref_packet(struct bt_ctf_packet
*packet
)
100 void unref_stream_class(struct bt_ctf_stream_class
*stream_class
)
102 bt_put(stream_class
);
106 void debug_info_iterator_destroy(struct bt_private_notification_iterator
*it
)
108 struct debug_info_iterator
*it_data
;
110 it_data
= bt_private_notification_iterator_get_user_data(it
);
113 if (it_data
->input_iterator_group
) {
114 g_ptr_array_free(it_data
->input_iterator_group
, TRUE
);
116 bt_put(it_data
->current_notification
);
117 bt_put(it_data
->input_iterator
);
118 g_hash_table_destroy(it_data
->trace_map
);
119 g_hash_table_destroy(it_data
->stream_map
);
120 g_hash_table_destroy(it_data
->stream_class_map
);
121 g_hash_table_destroy(it_data
->packet_map
);
122 g_hash_table_destroy(it_data
->trace_debug_map
);
127 struct bt_notification
*handle_notification(FILE *err
,
128 struct debug_info_iterator
*debug_it
,
129 struct bt_notification
*notification
)
131 struct bt_notification
*new_notification
= NULL
;
133 switch (bt_notification_get_type(notification
)) {
134 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
136 struct bt_ctf_packet
*packet
=
137 bt_notification_packet_begin_get_packet(notification
);
138 struct bt_ctf_packet
*writer_packet
;
144 writer_packet
= debug_info_new_packet(debug_it
, packet
);
145 assert(writer_packet
);
146 new_notification
= bt_notification_packet_begin_create(
148 assert(new_notification
);
152 case BT_NOTIFICATION_TYPE_PACKET_END
:
154 struct bt_ctf_packet
*packet
=
155 bt_notification_packet_end_get_packet(notification
);
156 struct bt_ctf_packet
*writer_packet
;
162 writer_packet
= debug_info_close_packet(debug_it
, packet
);
163 assert(writer_packet
);
164 new_notification
= bt_notification_packet_end_create(
166 assert(new_notification
);
170 case BT_NOTIFICATION_TYPE_EVENT
:
172 struct bt_ctf_event
*event
= bt_notification_event_get_event(
174 struct bt_ctf_event
*writer_event
;
175 struct bt_clock_class_priority_map
*cc_prio_map
=
176 bt_notification_event_get_clock_class_priority_map(
182 writer_event
= debug_info_output_event(debug_it
, event
);
183 assert(writer_event
);
184 new_notification
= bt_notification_event_create(writer_event
,
187 assert(new_notification
);
189 bt_put(writer_event
);
192 case BT_NOTIFICATION_TYPE_STREAM_END
:
194 struct bt_ctf_stream
*stream
=
195 bt_notification_stream_end_get_stream(notification
);
196 struct bt_ctf_stream
*writer_stream
;
202 writer_stream
= debug_info_stream_end(debug_it
, stream
);
203 assert(writer_stream
);
204 new_notification
= bt_notification_stream_end_create(
206 assert(new_notification
);
208 bt_put(writer_stream
);
211 case BT_NOTIFICATION_TYPE_INACTIVITY
:
213 new_notification
= bt_get(notification
);
217 puts("Unhandled notification type");
221 return new_notification
;
225 struct bt_notification_iterator_next_return
debug_info_iterator_next(
226 struct bt_private_notification_iterator
*iterator
)
228 struct debug_info_iterator
*debug_it
= NULL
;
229 struct bt_private_component
*component
= NULL
;
230 struct debug_info_component
*debug_info
= NULL
;
231 struct bt_notification_iterator
*source_it
= NULL
;
232 struct bt_notification
*notification
;
233 struct bt_notification_iterator_next_return ret
= {
234 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
235 .notification
= NULL
,
238 debug_it
= bt_private_notification_iterator_get_user_data(iterator
);
241 component
= bt_private_notification_iterator_get_private_component(iterator
);
243 debug_info
= bt_private_component_get_user_data(component
);
246 source_it
= debug_it
->input_iterator
;
248 ret
.status
= bt_notification_iterator_next(source_it
);
249 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
253 notification
= bt_notification_iterator_get_notification(
256 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
260 ret
.notification
= handle_notification(debug_info
->err
, debug_it
,
262 assert(ret
.notification
);
263 bt_put(notification
);
271 enum bt_notification_iterator_status
debug_info_iterator_init(
272 struct bt_private_notification_iterator
*iterator
,
273 struct bt_private_port
*port
)
275 enum bt_notification_iterator_status ret
=
276 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
277 enum bt_notification_iterator_status it_ret
;
278 struct bt_private_connection
*connection
= NULL
;
279 struct bt_private_component
*component
=
280 bt_private_notification_iterator_get_private_component(iterator
);
281 struct debug_info_iterator
*it_data
= g_new0(struct debug_info_iterator
, 1);
282 struct bt_private_port
*input_port
;
283 static const enum bt_notification_type notif_types
[] = {
284 BT_NOTIFICATION_TYPE_EVENT
,
285 BT_NOTIFICATION_TYPE_STREAM_END
,
286 BT_NOTIFICATION_TYPE_PACKET_BEGIN
,
287 BT_NOTIFICATION_TYPE_PACKET_END
,
288 BT_NOTIFICATION_TYPE_SENTINEL
,
292 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
296 input_port
= bt_private_component_filter_get_input_private_port_by_name(
299 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
303 connection
= bt_private_port_get_private_connection(input_port
);
306 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
310 it_data
->input_iterator
= bt_private_connection_create_notification_iterator(
311 connection
, notif_types
);
312 if (!it_data
->input_iterator
) {
313 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
317 it_data
->debug_info_component
= (struct debug_info_component
*)
318 bt_private_component_get_user_data(component
);
319 it_data
->err
= it_data
->debug_info_component
->err
;
320 it_data
->trace_map
= g_hash_table_new_full(g_direct_hash
,
321 g_direct_equal
, NULL
, (GDestroyNotify
) unref_trace
);
322 it_data
->stream_map
= g_hash_table_new_full(g_direct_hash
,
323 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream
);
324 it_data
->stream_class_map
= g_hash_table_new_full(g_direct_hash
,
325 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream_class
);
326 it_data
->packet_map
= g_hash_table_new_full(g_direct_hash
,
327 g_direct_equal
, NULL
, (GDestroyNotify
) unref_packet
);
328 it_data
->trace_debug_map
= g_hash_table_new_full(g_direct_hash
,
329 g_direct_equal
, NULL
, (GDestroyNotify
) unref_debug_info
);
331 it_ret
= bt_private_notification_iterator_set_user_data(iterator
, it_data
);
343 enum bt_component_status
init_from_params(
344 struct debug_info_component
*debug_info_component
,
345 struct bt_value
*params
)
347 struct bt_value
*value
= NULL
;
348 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
352 value
= bt_value_map_get(params
, "debug-info-field-name");
354 enum bt_value_status value_ret
;
357 value_ret
= bt_value_string_get(value
, &tmp
);
359 ret
= BT_COMPONENT_STATUS_INVALID
;
360 printf_error("Failed to retrieve debug-info-field-name value. "
361 "Expecting a string");
363 strcpy(debug_info_component
->arg_debug_info_field_name
, tmp
);
366 debug_info_component
->arg_debug_info_field_name
=
367 malloc(strlen("debug_info") + 1);
368 if (!debug_info_component
->arg_debug_info_field_name
) {
369 ret
= BT_COMPONENT_STATUS_NOMEM
;
372 sprintf(debug_info_component
->arg_debug_info_field_name
,
375 if (ret
!= BT_COMPONENT_STATUS_OK
) {
379 value
= bt_value_map_get(params
, "debug-dir");
381 enum bt_value_status value_ret
;
383 value_ret
= bt_value_string_get(value
,
384 &debug_info_component
->arg_debug_dir
);
386 ret
= BT_COMPONENT_STATUS_INVALID
;
387 printf_error("Failed to retrieve debug-dir value. "
388 "Expecting a string");
392 if (ret
!= BT_COMPONENT_STATUS_OK
) {
396 value
= bt_value_map_get(params
, "target-prefix");
398 enum bt_value_status value_ret
;
400 value_ret
= bt_value_string_get(value
,
401 &debug_info_component
->arg_target_prefix
);
403 ret
= BT_COMPONENT_STATUS_INVALID
;
404 printf_error("Failed to retrieve target-prefix value. "
405 "Expecting a string");
409 if (ret
!= BT_COMPONENT_STATUS_OK
) {
413 value
= bt_value_map_get(params
, "full-path");
415 enum bt_value_status value_ret
;
418 value_ret
= bt_value_bool_get(value
,
421 ret
= BT_COMPONENT_STATUS_INVALID
;
422 printf_error("Failed to retrieve full-path value. "
423 "Expecting a boolean");
426 debug_info_component
->arg_full_path
= bool_val
;
429 if (ret
!= BT_COMPONENT_STATUS_OK
) {
437 enum bt_component_status
debug_info_component_init(
438 struct bt_private_component
*component
, struct bt_value
*params
,
439 UNUSED_VAR
void *init_method_data
)
441 enum bt_component_status ret
;
442 struct debug_info_component
*debug_info
= create_debug_info_component_data();
445 ret
= BT_COMPONENT_STATUS_NOMEM
;
449 ret
= bt_private_component_set_user_data(component
, debug_info
);
450 if (ret
!= BT_COMPONENT_STATUS_OK
) {
454 ret
= bt_private_component_filter_add_input_private_port(
455 component
, "in", NULL
, NULL
);
456 if (ret
!= BT_COMPONENT_STATUS_OK
) {
460 ret
= bt_private_component_filter_add_output_private_port(
461 component
, "out", NULL
, NULL
);
462 if (ret
!= BT_COMPONENT_STATUS_OK
) {
466 ret
= init_from_params(debug_info
, params
);
470 destroy_debug_info_data(debug_info
);
474 /* Initialize plug-in entry points. */
475 BT_PLUGIN_WITH_ID(lttng_utils
, "lttng-utils");
476 BT_PLUGIN_DESCRIPTION_WITH_ID(lttng_utils
, "LTTng utilities");
477 BT_PLUGIN_AUTHOR_WITH_ID(lttng_utils
, "Julien Desfossez");
478 BT_PLUGIN_LICENSE_WITH_ID(lttng_utils
, "MIT");
480 BT_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID(lttng_utils
, debug_info
, "debug-info",
481 debug_info_iterator_next
);
482 BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION_WITH_ID(lttng_utils
, debug_info
,
483 "Augment compatible events with debugging information.");
484 BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD_WITH_ID(lttng_utils
,
485 debug_info
, debug_info_component_init
);
486 BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID(lttng_utils
,
487 debug_info
, destroy_debug_info_component
);
488 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_INIT_METHOD_WITH_ID(
489 lttng_utils
, debug_info
, debug_info_iterator_init
);
490 BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_FINALIZE_METHOD_WITH_ID(
491 lttng_utils
, debug_info
, debug_info_iterator_destroy
);