3ea99597325b4007bc0de0f0dddcc14fc4f22318
[babeltrace.git] / tests / lib / conds / utils.cpp
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <babeltrace2/babeltrace.h>
12 #include <glib.h>
13
14 #include "common/assert.h"
15 #include "utils.hpp"
16
17 typedef void (*run_in_comp_cls_init_func)(bt_self_component *self_comp, void *user_data);
18
19 struct comp_cls_init_method_data
20 {
21 run_in_comp_cls_init_func func;
22 void *user_data;
23 };
24
25 static bt_component_class_initialize_method_status
26 comp_cls_init(bt_self_component_source *self_comp, bt_self_component_source_configuration *conf,
27 const bt_value *params, void *init_method_data)
28 {
29 comp_cls_init_method_data *data = static_cast<comp_cls_init_method_data *>(init_method_data);
30
31 /* Call user function which is expected to abort */
32 data->func(bt_self_component_source_as_self_component(self_comp), data->user_data);
33
34 /* Never reached! */
35 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
36 }
37
38 static bt_message_iterator_class_next_method_status
39 msg_iter_cls_next(bt_self_message_iterator *self_msg_iter, bt_message_array_const msgs,
40 uint64_t capacity, uint64_t *count)
41 {
42 /* Not used */
43 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
44 }
45
46 static void run_in_comp_cls_init(run_in_comp_cls_init_func func, void *user_data)
47 {
48 bt_message_iterator_class *msg_iter_cls;
49 bt_component_class_source *comp_cls;
50 bt_component_class_set_method_status set_method_status;
51 bt_graph *graph;
52 struct comp_cls_init_method_data init_method_data = {
53 .func = func,
54 .user_data = user_data,
55 };
56
57 /* Create component class */
58 msg_iter_cls = bt_message_iterator_class_create(msg_iter_cls_next);
59 BT_ASSERT(msg_iter_cls);
60 comp_cls = bt_component_class_source_create("yo", msg_iter_cls);
61 BT_ASSERT(comp_cls);
62 set_method_status = bt_component_class_source_set_initialize_method(comp_cls, comp_cls_init);
63 BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
64
65 /* Create graph */
66 graph = bt_graph_create(0);
67 BT_ASSERT(graph);
68
69 /*
70 * Add source component: this calls the initialization method,
71 * calling `func`.
72 */
73 (void) bt_graph_add_source_component_with_initialize_method_data(
74 graph, comp_cls, "whatever", NULL, &init_method_data, BT_LOGGING_LEVEL_NONE, NULL);
75
76 /*
77 * This point is not expected to be reached as func() is
78 * expected to abort.
79 */
80 }
81
82 static void run_in_comp_cls_init_defer(bt_self_component *self_comp, void *user_data)
83 {
84 cond_trigger_run_in_comp_cls_init_func user_func =
85 reinterpret_cast<cond_trigger_run_in_comp_cls_init_func>(user_data);
86
87 user_func(self_comp);
88 }
89
90 static void run_trigger(const struct cond_trigger *trigger)
91 {
92 switch (trigger->func_type) {
93 case COND_TRIGGER_FUNC_TYPE_BASIC:
94 trigger->func.basic();
95 break;
96 case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
97 run_in_comp_cls_init(run_in_comp_cls_init_defer,
98 reinterpret_cast<void *>(trigger->func.run_in_comp_cls_init));
99 break;
100 default:
101 abort();
102 }
103 }
104
105 static void escape_json_string(const char *str, GString *escaped_str)
106 {
107 g_string_assign(escaped_str, "");
108
109 for (const char *ch = str; *ch; ch++) {
110 if (*ch == '\\' || *ch == '"') {
111 g_string_append_c(escaped_str, '\\');
112 }
113
114 g_string_append_c(escaped_str, *ch);
115 }
116 }
117
118 static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
119 {
120 GString *escaped_str = g_string_new(NULL);
121 size_t i;
122
123 BT_ASSERT(escaped_str);
124 printf("[");
125
126 for (i = 0; i < trigger_count; i++) {
127 const struct cond_trigger *trigger = &triggers[i];
128
129 /* Condition ID */
130 escape_json_string(trigger->cond_id, escaped_str);
131 printf("{\"cond-id\":\"%s\",", escaped_str->str);
132
133 /* Name starts with condition ID */
134 printf("\"name\":\"%s", escaped_str->str);
135
136 if (trigger->suffix) {
137 escape_json_string(trigger->suffix, escaped_str);
138 printf("-%s", escaped_str->str);
139 }
140
141 printf("\"}");
142
143 if (i < trigger_count - 1) {
144 /* Comma between objects */
145 printf(",");
146 }
147 }
148
149 printf("]");
150 g_string_free(escaped_str, TRUE);
151 fflush(stdout);
152 }
153
154 void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
155 size_t trigger_count)
156 {
157 BT_ASSERT(argc >= 2);
158
159 if (strcmp(argv[1], "list") == 0) {
160 list_triggers(triggers, trigger_count);
161 } else if (strcmp(argv[1], "run") == 0) {
162 int index;
163
164 BT_ASSERT(argc >= 3);
165 index = atoi(argv[2]);
166 BT_ASSERT(index >= 0 && index < trigger_count);
167 run_trigger(&triggers[index]);
168 }
169 }
This page took 0.03233 seconds and 3 git commands to generate.