2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
11 #include <babeltrace2/babeltrace.h>
14 #include "common/assert.h"
17 typedef void (* run_in_comp_cls_init_func
)(
18 bt_self_component
*self_comp
, void *user_data
);
20 struct comp_cls_init_method_data
{
21 run_in_comp_cls_init_func func
;
26 bt_component_class_initialize_method_status
comp_cls_init(
27 bt_self_component_source
*self_comp
,
28 bt_self_component_source_configuration
*conf
,
29 const bt_value
*params
, void *init_method_data
)
31 struct comp_cls_init_method_data
*data
= init_method_data
;
33 /* Call user function which is expected to abort */
34 data
->func(bt_self_component_source_as_self_component(self_comp
),
38 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
42 bt_message_iterator_class_next_method_status
msg_iter_cls_next(
43 bt_self_message_iterator
*self_msg_iter
,
44 bt_message_array_const msgs
, uint64_t capacity
,
48 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
52 void run_in_comp_cls_init(run_in_comp_cls_init_func func
,
55 bt_message_iterator_class
*msg_iter_cls
;
56 bt_component_class_source
*comp_cls
;
57 bt_component_class_set_method_status set_method_status
;
59 struct comp_cls_init_method_data init_method_data
= {
61 .user_data
= user_data
,
64 /* Create component class */
65 msg_iter_cls
= bt_message_iterator_class_create(msg_iter_cls_next
);
66 BT_ASSERT(msg_iter_cls
);
67 comp_cls
= bt_component_class_source_create("yo", msg_iter_cls
);
69 set_method_status
= bt_component_class_source_set_initialize_method(
70 comp_cls
, comp_cls_init
);
71 BT_ASSERT(set_method_status
== BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK
);
74 graph
= bt_graph_create(0);
78 * Add source component: this calls the initialization method,
81 (void) bt_graph_add_source_component_with_initialize_method_data(graph
,
82 comp_cls
, "whatever", NULL
, &init_method_data
,
83 BT_LOGGING_LEVEL_NONE
, NULL
);
86 * This point is not expected to be reached as func() is
92 void run_in_comp_cls_init_defer(bt_self_component
*self_comp
,
95 cond_trigger_run_in_comp_cls_init_func user_func
= user_data
;
101 void run_trigger(const struct cond_trigger
*trigger
)
103 switch (trigger
->func_type
) {
104 case COND_TRIGGER_FUNC_TYPE_BASIC
:
105 trigger
->func
.basic();
107 case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT
:
108 run_in_comp_cls_init(run_in_comp_cls_init_defer
,
109 trigger
->func
.run_in_comp_cls_init
);
117 void escape_json_string(const char *str
, GString
*escaped_str
)
119 g_string_assign(escaped_str
, "");
121 for (const char *ch
= str
; *ch
; ch
++) {
122 if (*ch
== '\\' || *ch
== '"') {
123 g_string_append_c(escaped_str
, '\\');
126 g_string_append_c(escaped_str
, *ch
);
131 void list_triggers(const struct cond_trigger triggers
[], size_t trigger_count
)
133 GString
*escaped_str
= g_string_new(NULL
);
136 BT_ASSERT(escaped_str
);
139 for (i
= 0; i
< trigger_count
; i
++) {
140 const struct cond_trigger
*trigger
= &triggers
[i
];
143 escape_json_string(trigger
->cond_id
, escaped_str
);
144 printf("{\"cond-id\":\"%s\",", escaped_str
->str
);
146 /* Name starts with condition ID */
147 printf("\"name\":\"%s", escaped_str
->str
);
149 if (trigger
->suffix
) {
150 escape_json_string(trigger
->suffix
, escaped_str
);
151 printf("-%s", escaped_str
->str
);
156 if (i
< trigger_count
- 1) {
157 /* Comma between objects */
163 g_string_free(escaped_str
, TRUE
);
167 void cond_main(int argc
, const char *argv
[],
168 const struct cond_trigger triggers
[], size_t trigger_count
)
170 BT_ASSERT(argc
>= 2);
172 if (strcmp(argv
[1], "list") == 0) {
173 list_triggers(triggers
, trigger_count
);
174 } else if (strcmp(argv
[1], "run") == 0) {
177 BT_ASSERT(argc
>= 3);
178 index
= atoi(argv
[2]);
179 BT_ASSERT(index
>= 0 && index
< trigger_count
);
180 run_trigger(&triggers
[index
]);