Commit | Line | Data |
---|---|---|
5d7e8359 PP |
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.h" | |
16 | ||
17 | typedef void (* run_in_comp_cls_init_func)( | |
18 | bt_self_component *self_comp, void *user_data); | |
19 | ||
20 | struct comp_cls_init_method_data { | |
21 | run_in_comp_cls_init_func func; | |
22 | void *user_data; | |
23 | }; | |
24 | ||
25 | static | |
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) | |
30 | { | |
31 | struct comp_cls_init_method_data *data = init_method_data; | |
32 | ||
33 | /* Call user function which is expected to abort */ | |
34 | data->func(bt_self_component_source_as_self_component(self_comp), | |
35 | data->user_data); | |
36 | ||
37 | /* Never reached! */ | |
38 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
39 | } | |
40 | ||
41 | static | |
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, | |
45 | uint64_t *count) | |
46 | { | |
47 | /* Not used */ | |
48 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
49 | } | |
50 | ||
51 | static | |
52 | void run_in_comp_cls_init(run_in_comp_cls_init_func func, | |
53 | void *user_data) | |
54 | { | |
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; | |
58 | bt_graph *graph; | |
59 | struct comp_cls_init_method_data init_method_data = { | |
60 | .func = func, | |
61 | .user_data = user_data, | |
62 | }; | |
63 | ||
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); | |
68 | BT_ASSERT(comp_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); | |
72 | ||
73 | /* Create graph */ | |
74 | graph = bt_graph_create(0); | |
75 | BT_ASSERT(graph); | |
76 | ||
77 | /* | |
78 | * Add source component: this calls the initialization method, | |
79 | * calling `func`. | |
80 | */ | |
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); | |
84 | ||
85 | /* | |
86 | * This point is not expected to be reached as func() is | |
87 | * expected to abort. | |
88 | */ | |
89 | } | |
90 | ||
91 | static | |
92 | void run_in_comp_cls_init_defer(bt_self_component *self_comp, | |
93 | void *user_data) | |
94 | { | |
95 | cond_trigger_run_in_comp_cls_init_func user_func = user_data; | |
96 | ||
97 | user_func(self_comp); | |
98 | } | |
99 | ||
100 | static | |
101 | void run_trigger(const struct cond_trigger *trigger) | |
102 | { | |
103 | switch (trigger->func_type) { | |
104 | case COND_TRIGGER_FUNC_TYPE_BASIC: | |
105 | trigger->func.basic(); | |
106 | break; | |
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); | |
110 | break; | |
111 | default: | |
112 | abort(); | |
113 | } | |
114 | } | |
115 | ||
116 | static | |
117 | void escape_json_string(const char *str, GString *escaped_str) | |
118 | { | |
119 | g_string_assign(escaped_str, ""); | |
120 | ||
121 | for (const char *ch = str; *ch; ch++) { | |
122 | if (*ch == '\\' || *ch == '"') { | |
123 | g_string_append_c(escaped_str, '\\'); | |
124 | } | |
125 | ||
126 | g_string_append_c(escaped_str, *ch); | |
127 | } | |
128 | } | |
129 | ||
130 | static | |
131 | void list_triggers(const struct cond_trigger triggers[], size_t trigger_count) | |
132 | { | |
133 | GString *escaped_str = g_string_new(NULL); | |
134 | size_t i; | |
135 | ||
136 | BT_ASSERT(escaped_str); | |
137 | printf("["); | |
138 | ||
139 | for (i = 0; i < trigger_count; i++) { | |
140 | const struct cond_trigger *trigger = &triggers[i]; | |
141 | ||
142 | /* Condition ID */ | |
143 | escape_json_string(trigger->cond_id, escaped_str); | |
144 | printf("{\"cond-id\":\"%s\",", escaped_str->str); | |
145 | ||
146 | /* Name starts with condition ID */ | |
147 | printf("\"name\":\"%s", escaped_str->str); | |
148 | ||
149 | if (trigger->suffix) { | |
150 | escape_json_string(trigger->suffix, escaped_str); | |
151 | printf("-%s", escaped_str->str); | |
152 | } | |
153 | ||
154 | printf("\"}"); | |
155 | ||
156 | if (i < trigger_count - 1) { | |
157 | /* Comma between objects */ | |
158 | printf(","); | |
159 | } | |
160 | } | |
161 | ||
162 | printf("]"); | |
163 | g_string_free(escaped_str, TRUE); | |
164 | fflush(stdout); | |
165 | } | |
166 | ||
167 | void cond_main(int argc, const char *argv[], | |
168 | const struct cond_trigger triggers[], size_t trigger_count) | |
169 | { | |
170 | BT_ASSERT(argc >= 2); | |
171 | ||
172 | if (strcmp(argv[1], "list") == 0) { | |
173 | list_triggers(triggers, trigger_count); | |
174 | } else if (strcmp(argv[1], "run") == 0) { | |
175 | int index; | |
176 | ||
177 | BT_ASSERT(argc >= 3); | |
178 | index = atoi(argv[2]); | |
179 | BT_ASSERT(index >= 0 && index < trigger_count); | |
180 | run_trigger(&triggers[index]); | |
181 | } | |
182 | } |