Commit | Line | Data |
---|---|---|
34ac0e6c | 1 | /* |
64fa3fec MD |
2 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
3 | * | |
4 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
34ac0e6c MD |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
c462e188 MD |
15 | * |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
34ac0e6c | 23 | */ |
4c8bfb7e | 24 | |
ea9f8b29 PP |
25 | #define BT_LOG_TAG "CLI" |
26 | #include "logging.h" | |
27 | ||
3fadfbc0 | 28 | #include <babeltrace2/babeltrace.h> |
578e048b | 29 | #include "common/common.h" |
a8ff38ef | 30 | #include <unistd.h> |
34ac0e6c | 31 | #include <stdlib.h> |
7f26a816 PP |
32 | #include <string.h> |
33 | #include <stdio.h> | |
33b34c43 | 34 | #include <glib.h> |
dc3fffef | 35 | #include <inttypes.h> |
7cdc2bab | 36 | #include <unistd.h> |
5401f780 | 37 | #include <signal.h> |
ec2c5e50 MJ |
38 | #include "babeltrace2-cfg.h" |
39 | #include "babeltrace2-cfg-cli-args.h" | |
40 | #include "babeltrace2-cfg-cli-args-default.h" | |
ebddaeb8 | 41 | #include "babeltrace2-log-level.h" |
743138a3 | 42 | #include "babeltrace2-plugins.h" |
3c729b9a | 43 | #include "babeltrace2-query.h" |
9009cc24 PP |
44 | |
45 | #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH" | |
75a2cb9b | 46 | #define NSEC_PER_SEC 1000000000LL |
34ac0e6c | 47 | |
9b4f9b42 PP |
48 | /* Application's interrupter (owned by this) */ |
49 | static bt_interrupter *the_interrupter; | |
5401f780 | 50 | |
65d3198f | 51 | #ifdef __MINGW32__ |
a2d06fd5 | 52 | |
65d3198f MJ |
53 | #include <windows.h> |
54 | ||
55 | static | |
56 | BOOL WINAPI signal_handler(DWORD signal) { | |
9b4f9b42 PP |
57 | if (the_interrupter) { |
58 | bt_interrupter_set(the_interrupter); | |
65d3198f MJ |
59 | } |
60 | ||
65d3198f MJ |
61 | return TRUE; |
62 | } | |
63 | ||
5401f780 | 64 | static |
65d3198f MJ |
65 | void set_signal_handler(void) |
66 | { | |
67 | if (!SetConsoleCtrlHandler(signal_handler, TRUE)) { | |
3f7d4d90 | 68 | BT_LOGE("Failed to set the Ctrl+C handler."); |
65d3198f MJ |
69 | } |
70 | } | |
a2d06fd5 | 71 | |
65d3198f | 72 | #else /* __MINGW32__ */ |
a2d06fd5 | 73 | |
65d3198f MJ |
74 | static |
75 | void signal_handler(int signum) | |
5401f780 PP |
76 | { |
77 | if (signum != SIGINT) { | |
78 | return; | |
79 | } | |
80 | ||
9b4f9b42 PP |
81 | if (the_interrupter) { |
82 | bt_interrupter_set(the_interrupter); | |
c7eee084 | 83 | } |
5401f780 PP |
84 | } |
85 | ||
65d3198f MJ |
86 | static |
87 | void set_signal_handler(void) | |
88 | { | |
89 | struct sigaction new_action, old_action; | |
90 | ||
91 | new_action.sa_handler = signal_handler; | |
92 | sigemptyset(&new_action.sa_mask); | |
93 | new_action.sa_flags = 0; | |
94 | sigaction(SIGINT, NULL, &old_action); | |
95 | ||
96 | if (old_action.sa_handler != SIG_IGN) { | |
97 | sigaction(SIGINT, &new_action, NULL); | |
98 | } | |
99 | } | |
a2d06fd5 | 100 | |
65d3198f MJ |
101 | #endif /* __MINGW32__ */ |
102 | ||
c7eee084 | 103 | static |
f4e38e70 PP |
104 | int query(struct bt_config *cfg, const bt_component_class *comp_cls, |
105 | const char *obj, const bt_value *params, | |
106 | const bt_value **user_result, const char **fail_reason) | |
c7eee084 | 107 | { |
3c729b9a PP |
108 | return cli_query(comp_cls, obj, params, cfg->log_level, |
109 | the_interrupter, user_result, fail_reason); | |
c7eee084 PP |
110 | } |
111 | ||
0d72b8c3 | 112 | typedef const void *(*plugin_borrow_comp_cls_func_t)( |
b19ff26f | 113 | const bt_plugin *, const char *); |
d94d92ac | 114 | |
33b34c43 | 115 | static |
0d72b8c3 | 116 | const void *find_component_class_from_plugin(const char *plugin_name, |
33b34c43 | 117 | const char *comp_class_name, |
d94d92ac | 118 | plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func) |
33b34c43 | 119 | { |
0d72b8c3 | 120 | const void *comp_class = NULL; |
b19ff26f | 121 | const bt_plugin *plugin; |
7213a328 | 122 | |
3f7d4d90 | 123 | BT_LOGI("Finding component class: plugin-name=\"%s\", " |
d94d92ac | 124 | "comp-cls-name=\"%s\"", plugin_name, comp_class_name); |
7213a328 | 125 | |
743138a3 | 126 | plugin = find_loaded_plugin(plugin_name); |
33b34c43 PP |
127 | if (!plugin) { |
128 | goto end; | |
129 | } | |
130 | ||
398454ed PP |
131 | comp_class = plugin_borrow_comp_cls_func(plugin, comp_class_name); |
132 | bt_object_get_ref(comp_class); | |
c5b9b441 | 133 | BT_PLUGIN_PUT_REF_AND_RESET(plugin); |
7213a328 | 134 | |
33b34c43 | 135 | end: |
3f7d4d90 PP |
136 | if (comp_class) { |
137 | BT_LOGI("Found component class: plugin-name=\"%s\", " | |
138 | "comp-cls-name=\"%s\"", plugin_name, comp_class_name); | |
139 | } else { | |
140 | BT_LOGI("Cannot find source component class: " | |
141 | "plugin-name=\"%s\", comp-cls-name=\"%s\"", | |
142 | plugin_name, comp_class_name); | |
7213a328 PP |
143 | } |
144 | ||
33b34c43 PP |
145 | return comp_class; |
146 | } | |
6c2f3ee5 | 147 | |
d94d92ac | 148 | static |
b19ff26f | 149 | const bt_component_class_source *find_source_component_class( |
d94d92ac PP |
150 | const char *plugin_name, const char *comp_class_name) |
151 | { | |
0d72b8c3 PP |
152 | return (const void *) find_component_class_from_plugin( |
153 | plugin_name, comp_class_name, | |
d94d92ac | 154 | (plugin_borrow_comp_cls_func_t) |
92fed4e1 | 155 | bt_plugin_borrow_source_component_class_by_name_const); |
d94d92ac PP |
156 | } |
157 | ||
158 | static | |
b19ff26f | 159 | const bt_component_class_filter *find_filter_component_class( |
d94d92ac PP |
160 | const char *plugin_name, const char *comp_class_name) |
161 | { | |
0d72b8c3 PP |
162 | return (const void *) find_component_class_from_plugin( |
163 | plugin_name, comp_class_name, | |
d94d92ac | 164 | (plugin_borrow_comp_cls_func_t) |
92fed4e1 | 165 | bt_plugin_borrow_filter_component_class_by_name_const); |
d94d92ac PP |
166 | } |
167 | ||
168 | static | |
b19ff26f | 169 | const bt_component_class_sink *find_sink_component_class( |
d94d92ac PP |
170 | const char *plugin_name, const char *comp_class_name) |
171 | { | |
0d72b8c3 | 172 | return (const void *) find_component_class_from_plugin(plugin_name, |
d94d92ac PP |
173 | comp_class_name, |
174 | (plugin_borrow_comp_cls_func_t) | |
92fed4e1 | 175 | bt_plugin_borrow_sink_component_class_by_name_const); |
d94d92ac PP |
176 | } |
177 | ||
178 | static | |
b19ff26f | 179 | const bt_component_class *find_component_class(const char *plugin_name, |
d94d92ac | 180 | const char *comp_class_name, |
4cdfc5e8 | 181 | bt_component_class_type comp_class_type) |
d94d92ac | 182 | { |
b19ff26f | 183 | const bt_component_class *comp_cls = NULL; |
d94d92ac PP |
184 | |
185 | switch (comp_class_type) { | |
186 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
0d72b8c3 | 187 | comp_cls = bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name, comp_class_name)); |
d94d92ac PP |
188 | break; |
189 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
0d72b8c3 | 190 | comp_cls = bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name, comp_class_name)); |
d94d92ac PP |
191 | break; |
192 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
0d72b8c3 | 193 | comp_cls = bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name, comp_class_name)); |
d94d92ac PP |
194 | break; |
195 | default: | |
196 | abort(); | |
197 | } | |
198 | ||
199 | return comp_cls; | |
200 | } | |
201 | ||
c42c79ea | 202 | static |
7213a328 | 203 | void print_indent(FILE *fp, size_t indent) |
c42c79ea PP |
204 | { |
205 | size_t i; | |
206 | ||
207 | for (i = 0; i < indent; i++) { | |
7213a328 | 208 | fprintf(fp, " "); |
c42c79ea PP |
209 | } |
210 | } | |
211 | ||
87796884 | 212 | static |
4cdfc5e8 | 213 | const char *component_type_str(bt_component_class_type type) |
87796884 PP |
214 | { |
215 | switch (type) { | |
216 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
217 | return "source"; | |
218 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
219 | return "sink"; | |
220 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
221 | return "filter"; | |
87796884 | 222 | default: |
fd5f8053 | 223 | return "(unknown)"; |
87796884 PP |
224 | } |
225 | } | |
226 | ||
9009cc24 PP |
227 | static |
228 | void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name, | |
4cdfc5e8 | 229 | const char *comp_cls_name, bt_component_class_type type) |
87796884 | 230 | { |
9009cc24 PP |
231 | GString *shell_plugin_name = NULL; |
232 | GString *shell_comp_cls_name = NULL; | |
87796884 | 233 | |
f393c19b PP |
234 | if (plugin_name) { |
235 | shell_plugin_name = bt_common_shell_quote(plugin_name, false); | |
236 | if (!shell_plugin_name) { | |
237 | goto end; | |
238 | } | |
87796884 PP |
239 | } |
240 | ||
9009cc24 | 241 | shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false); |
87796884 PP |
242 | if (!shell_comp_cls_name) { |
243 | goto end; | |
244 | } | |
245 | ||
f393c19b | 246 | fprintf(fh, "'%s%s%s%s", |
87796884 PP |
247 | bt_common_color_bold(), |
248 | bt_common_color_fg_cyan(), | |
249 | component_type_str(type), | |
f393c19b PP |
250 | bt_common_color_fg_default()); |
251 | ||
252 | if (shell_plugin_name) { | |
253 | fprintf(fh, ".%s%s%s", | |
254 | bt_common_color_fg_blue(), | |
255 | shell_plugin_name->str, | |
256 | bt_common_color_fg_default()); | |
257 | } | |
258 | ||
259 | fprintf(fh, ".%s%s%s'", | |
87796884 | 260 | bt_common_color_fg_yellow(), |
9009cc24 | 261 | shell_comp_cls_name->str, |
87796884 PP |
262 | bt_common_color_reset()); |
263 | ||
264 | end: | |
9009cc24 PP |
265 | if (shell_plugin_name) { |
266 | g_string_free(shell_plugin_name, TRUE); | |
267 | } | |
268 | ||
269 | if (shell_comp_cls_name) { | |
270 | g_string_free(shell_comp_cls_name, TRUE); | |
271 | } | |
87796884 PP |
272 | } |
273 | ||
c42c79ea | 274 | static |
b19ff26f | 275 | void print_value(FILE *, const bt_value *, size_t); |
c42c79ea | 276 | |
c1081aa6 | 277 | static |
b19ff26f | 278 | void print_value_rec(FILE *, const bt_value *, size_t); |
7213a328 | 279 | |
c42c79ea | 280 | static |
13763113 SM |
281 | void print_map_value(const char *key, const bt_value *object, FILE *fp, |
282 | size_t indent) | |
c42c79ea | 283 | { |
13763113 SM |
284 | print_indent(fp, indent); |
285 | fprintf(fp, "%s: ", key); | |
f6ccaed9 | 286 | BT_ASSERT(object); |
290725f7 PP |
287 | |
288 | if (bt_value_is_array(object) && | |
289 | bt_value_array_is_empty(object)) { | |
13763113 SM |
290 | fprintf(fp, "[ ]\n"); |
291 | goto end; | |
290725f7 PP |
292 | } |
293 | ||
294 | if (bt_value_is_map(object) && | |
295 | bt_value_map_is_empty(object)) { | |
13763113 SM |
296 | fprintf(fp, "{ }\n"); |
297 | goto end; | |
290725f7 | 298 | } |
c42c79ea | 299 | |
290725f7 PP |
300 | if (bt_value_is_array(object) || |
301 | bt_value_is_map(object)) { | |
13763113 | 302 | fprintf(fp, "\n"); |
290725f7 | 303 | } |
c42c79ea | 304 | |
13763113 SM |
305 | print_value_rec(fp, object, indent + 2); |
306 | ||
307 | end: | |
308 | return; | |
309 | } | |
310 | ||
311 | static | |
312 | bt_bool collect_map_keys(const char *key, const bt_value *object, void *data) | |
313 | { | |
314 | GPtrArray *map_keys = data; | |
315 | ||
316 | g_ptr_array_add(map_keys, (gpointer *) key); | |
317 | ||
c55a9f58 | 318 | return BT_TRUE; |
c42c79ea PP |
319 | } |
320 | ||
13763113 SM |
321 | static |
322 | gint g_ptr_array_sort_strings(gconstpointer a, gconstpointer b) { | |
323 | const char *s1 = *((const char **) a); | |
324 | const char *s2 = *((const char **) b); | |
325 | ||
326 | return g_strcmp0(s1, s2); | |
327 | } | |
328 | ||
c42c79ea | 329 | static |
b19ff26f | 330 | void print_value_rec(FILE *fp, const bt_value *value, size_t indent) |
c42c79ea | 331 | { |
c55a9f58 | 332 | bt_bool bool_val; |
c42c79ea | 333 | int64_t int_val; |
fdd3a2da | 334 | uint64_t uint_val; |
c42c79ea PP |
335 | double dbl_val; |
336 | const char *str_val; | |
13763113 | 337 | GPtrArray *map_keys = NULL; |
c42c79ea | 338 | |
3eb16918 | 339 | BT_ASSERT(value); |
c42c79ea | 340 | |
c42c79ea PP |
341 | switch (bt_value_get_type(value)) { |
342 | case BT_VALUE_TYPE_NULL: | |
7213a328 | 343 | fprintf(fp, "%snull%s\n", bt_common_color_bold(), |
c1081aa6 | 344 | bt_common_color_reset()); |
c42c79ea PP |
345 | break; |
346 | case BT_VALUE_TYPE_BOOL: | |
601b0d3c | 347 | bool_val = bt_value_bool_get(value); |
7213a328 | 348 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
c1081aa6 PP |
349 | bt_common_color_fg_cyan(), bool_val ? "yes" : "no", |
350 | bt_common_color_reset()); | |
c42c79ea | 351 | break; |
fdd3a2da | 352 | case BT_VALUE_TYPE_UNSIGNED_INTEGER: |
9c08c816 | 353 | uint_val = bt_value_integer_unsigned_get(value); |
fdd3a2da PP |
354 | fprintf(fp, "%s%s%" PRIu64 "%s\n", bt_common_color_bold(), |
355 | bt_common_color_fg_red(), uint_val, | |
356 | bt_common_color_reset()); | |
357 | break; | |
358 | case BT_VALUE_TYPE_SIGNED_INTEGER: | |
9c08c816 | 359 | int_val = bt_value_integer_signed_get(value); |
7213a328 | 360 | fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(), |
c1081aa6 PP |
361 | bt_common_color_fg_red(), int_val, |
362 | bt_common_color_reset()); | |
c42c79ea | 363 | break; |
a373bf69 | 364 | case BT_VALUE_TYPE_REAL: |
601b0d3c | 365 | dbl_val = bt_value_real_get(value); |
7213a328 | 366 | fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(), |
c1081aa6 PP |
367 | bt_common_color_fg_red(), dbl_val, |
368 | bt_common_color_reset()); | |
c42c79ea PP |
369 | break; |
370 | case BT_VALUE_TYPE_STRING: | |
601b0d3c | 371 | str_val = bt_value_string_get(value); |
7213a328 | 372 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
c1081aa6 PP |
373 | bt_common_color_fg_green(), str_val, |
374 | bt_common_color_reset()); | |
c42c79ea PP |
375 | break; |
376 | case BT_VALUE_TYPE_ARRAY: | |
50233236 | 377 | { |
f80e9ec1 | 378 | uint64_t i, size; |
393729a6 | 379 | size = bt_value_array_get_length(value); |
290725f7 | 380 | if (size == 0) { |
7213a328 PP |
381 | print_indent(fp, indent); |
382 | fprintf(fp, "[ ]\n"); | |
290725f7 PP |
383 | break; |
384 | } | |
c42c79ea PP |
385 | |
386 | for (i = 0; i < size; i++) { | |
b19ff26f | 387 | const bt_value *element = |
05e21286 | 388 | bt_value_array_borrow_element_by_index_const( |
07208d85 | 389 | value, i); |
c42c79ea | 390 | |
7213a328 PP |
391 | print_indent(fp, indent); |
392 | fprintf(fp, "- "); | |
290725f7 PP |
393 | |
394 | if (bt_value_is_array(element) && | |
395 | bt_value_array_is_empty(element)) { | |
7213a328 | 396 | fprintf(fp, "[ ]\n"); |
290725f7 PP |
397 | continue; |
398 | } | |
399 | ||
400 | if (bt_value_is_map(element) && | |
401 | bt_value_map_is_empty(element)) { | |
7213a328 | 402 | fprintf(fp, "{ }\n"); |
290725f7 PP |
403 | continue; |
404 | } | |
405 | ||
406 | if (bt_value_is_array(element) || | |
407 | bt_value_is_map(element)) { | |
7213a328 | 408 | fprintf(fp, "\n"); |
290725f7 PP |
409 | } |
410 | ||
7213a328 | 411 | print_value_rec(fp, element, indent + 2); |
c42c79ea | 412 | } |
c42c79ea | 413 | break; |
50233236 | 414 | } |
c42c79ea | 415 | case BT_VALUE_TYPE_MAP: |
7213a328 | 416 | { |
13763113 SM |
417 | guint i; |
418 | bt_value_map_foreach_entry_const_status foreach_status; | |
7213a328 | 419 | |
c42c79ea | 420 | if (bt_value_map_is_empty(value)) { |
7213a328 PP |
421 | print_indent(fp, indent); |
422 | fprintf(fp, "{ }\n"); | |
290725f7 | 423 | break; |
c42c79ea PP |
424 | } |
425 | ||
13763113 SM |
426 | map_keys = g_ptr_array_new(); |
427 | if (!map_keys) { | |
428 | BT_CLI_LOGE_APPEND_CAUSE("Failed to allocated on GPtrArray."); | |
429 | goto end; | |
430 | } | |
431 | ||
432 | /* | |
433 | * We want to print the map entries in a stable order. Collect | |
434 | * all the map's keys in a GPtrArray, sort it, then print the | |
435 | * entries in that order. | |
436 | */ | |
437 | foreach_status = bt_value_map_foreach_entry_const(value, | |
438 | collect_map_keys, map_keys); | |
439 | if (foreach_status != BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK) { | |
440 | BT_CLI_LOGE_APPEND_CAUSE("Failed to iterator on map value."); | |
441 | goto end; | |
442 | } | |
443 | ||
444 | g_ptr_array_sort(map_keys, g_ptr_array_sort_strings); | |
445 | ||
446 | for (i = 0; i < map_keys->len; i++) { | |
447 | const char *map_key = g_ptr_array_index(map_keys, i); | |
448 | const bt_value *map_value; | |
449 | ||
450 | map_value = bt_value_map_borrow_entry_value_const(value, map_key); | |
451 | BT_ASSERT(map_value); | |
452 | ||
453 | print_map_value(map_key, map_value, fp, indent); | |
454 | } | |
455 | ||
c42c79ea | 456 | break; |
7213a328 | 457 | } |
c42c79ea | 458 | default: |
0fbb9a9f | 459 | abort(); |
c42c79ea | 460 | } |
13763113 SM |
461 | |
462 | goto end; | |
60a97734 | 463 | |
13763113 SM |
464 | end: |
465 | if (map_keys) { | |
466 | g_ptr_array_free(map_keys, TRUE); | |
467 | } | |
c42c79ea PP |
468 | } |
469 | ||
c1081aa6 | 470 | static |
b19ff26f | 471 | void print_value(FILE *fp, const bt_value *value, size_t indent) |
c1081aa6 PP |
472 | { |
473 | if (!bt_value_is_array(value) && !bt_value_is_map(value)) { | |
7213a328 | 474 | print_indent(fp, indent); |
c1081aa6 PP |
475 | } |
476 | ||
7213a328 | 477 | print_value_rec(fp, value, indent); |
c1081aa6 PP |
478 | } |
479 | ||
c42c79ea PP |
480 | static |
481 | void print_bt_config_component(struct bt_config_component *bt_config_component) | |
482 | { | |
7213a328 PP |
483 | fprintf(stderr, " "); |
484 | print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str, | |
db0f160a | 485 | bt_config_component->comp_cls_name->str, |
87796884 | 486 | bt_config_component->type); |
7213a328 | 487 | fprintf(stderr, ":\n"); |
3b6cfcc5 PP |
488 | |
489 | if (bt_config_component->instance_name->len > 0) { | |
7213a328 | 490 | fprintf(stderr, " Name: %s\n", |
3b6cfcc5 PP |
491 | bt_config_component->instance_name->str); |
492 | } | |
493 | ||
7213a328 | 494 | fprintf(stderr, " Parameters:\n"); |
05e21286 | 495 | print_value(stderr, bt_config_component->params, 8); |
c42c79ea PP |
496 | } |
497 | ||
498 | static | |
499 | void print_bt_config_components(GPtrArray *array) | |
500 | { | |
501 | size_t i; | |
502 | ||
503 | for (i = 0; i < array->len; i++) { | |
504 | struct bt_config_component *cfg_component = | |
e5bc7f81 | 505 | bt_config_get_component(array, i); |
c42c79ea | 506 | print_bt_config_component(cfg_component); |
65300d60 | 507 | BT_OBJECT_PUT_REF_AND_RESET(cfg_component); |
c42c79ea PP |
508 | } |
509 | } | |
510 | ||
290725f7 | 511 | static |
b19ff26f | 512 | void print_plugin_paths(const bt_value *plugin_paths) |
290725f7 | 513 | { |
7213a328 PP |
514 | fprintf(stderr, " Plugin paths:\n"); |
515 | print_value(stderr, plugin_paths, 4); | |
290725f7 PP |
516 | } |
517 | ||
518 | static | |
db0f160a | 519 | void print_cfg_run(struct bt_config *cfg) |
290725f7 | 520 | { |
ebba3338 PP |
521 | size_t i; |
522 | ||
05e21286 | 523 | print_plugin_paths(cfg->plugin_paths); |
7213a328 | 524 | fprintf(stderr, " Source component instances:\n"); |
db0f160a | 525 | print_bt_config_components(cfg->cmd_data.run.sources); |
ebba3338 | 526 | |
db0f160a | 527 | if (cfg->cmd_data.run.filters->len > 0) { |
7213a328 | 528 | fprintf(stderr, " Filter component instances:\n"); |
db0f160a | 529 | print_bt_config_components(cfg->cmd_data.run.filters); |
ebba3338 PP |
530 | } |
531 | ||
7213a328 | 532 | fprintf(stderr, " Sink component instances:\n"); |
db0f160a | 533 | print_bt_config_components(cfg->cmd_data.run.sinks); |
7213a328 | 534 | fprintf(stderr, " Connections:\n"); |
ebba3338 | 535 | |
db0f160a | 536 | for (i = 0; i < cfg->cmd_data.run.connections->len; i++) { |
ebba3338 | 537 | struct bt_config_connection *cfg_connection = |
db0f160a | 538 | g_ptr_array_index(cfg->cmd_data.run.connections, |
ebba3338 PP |
539 | i); |
540 | ||
7213a328 | 541 | fprintf(stderr, " %s%s%s -> %s%s%s\n", |
9009cc24 PP |
542 | cfg_connection->upstream_comp_name->str, |
543 | cfg_connection->upstream_port_glob->len > 0 ? "." : "", | |
544 | cfg_connection->upstream_port_glob->str, | |
545 | cfg_connection->downstream_comp_name->str, | |
546 | cfg_connection->downstream_port_glob->len > 0 ? "." : "", | |
547 | cfg_connection->downstream_port_glob->str); | |
ebba3338 | 548 | } |
290725f7 PP |
549 | } |
550 | ||
551 | static | |
552 | void print_cfg_list_plugins(struct bt_config *cfg) | |
553 | { | |
05e21286 | 554 | print_plugin_paths(cfg->plugin_paths); |
290725f7 PP |
555 | } |
556 | ||
c1081aa6 PP |
557 | static |
558 | void print_cfg_help(struct bt_config *cfg) | |
559 | { | |
05e21286 | 560 | print_plugin_paths(cfg->plugin_paths); |
db0f160a PP |
561 | } |
562 | ||
563 | static | |
564 | void print_cfg_print_ctf_metadata(struct bt_config *cfg) | |
565 | { | |
05e21286 | 566 | print_plugin_paths(cfg->plugin_paths); |
7213a328 PP |
567 | fprintf(stderr, " Path: %s\n", |
568 | cfg->cmd_data.print_ctf_metadata.path->str); | |
db0f160a PP |
569 | } |
570 | ||
571 | static | |
572 | void print_cfg_print_lttng_live_sessions(struct bt_config *cfg) | |
573 | { | |
05e21286 | 574 | print_plugin_paths(cfg->plugin_paths); |
7213a328 PP |
575 | fprintf(stderr, " URL: %s\n", |
576 | cfg->cmd_data.print_lttng_live_sessions.url->str); | |
c1081aa6 PP |
577 | } |
578 | ||
579 | static | |
a67681c1 | 580 | void print_cfg_query(struct bt_config *cfg) |
c1081aa6 | 581 | { |
05e21286 | 582 | print_plugin_paths(cfg->plugin_paths); |
7213a328 PP |
583 | fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str); |
584 | fprintf(stderr, " Component class:\n"); | |
a67681c1 | 585 | print_bt_config_component(cfg->cmd_data.query.cfg_component); |
c1081aa6 PP |
586 | } |
587 | ||
c42c79ea PP |
588 | static |
589 | void print_cfg(struct bt_config *cfg) | |
590 | { | |
7213a328 | 591 | if (!BT_LOG_ON_INFO) { |
00447e45 PP |
592 | return; |
593 | } | |
594 | ||
3f7d4d90 | 595 | BT_LOGI_STR("CLI configuration:"); |
290725f7 PP |
596 | |
597 | switch (cfg->command) { | |
db0f160a PP |
598 | case BT_CONFIG_COMMAND_RUN: |
599 | print_cfg_run(cfg); | |
290725f7 PP |
600 | break; |
601 | case BT_CONFIG_COMMAND_LIST_PLUGINS: | |
602 | print_cfg_list_plugins(cfg); | |
c1081aa6 PP |
603 | break; |
604 | case BT_CONFIG_COMMAND_HELP: | |
605 | print_cfg_help(cfg); | |
606 | break; | |
a67681c1 PP |
607 | case BT_CONFIG_COMMAND_QUERY: |
608 | print_cfg_query(cfg); | |
290725f7 | 609 | break; |
db0f160a PP |
610 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
611 | print_cfg_print_ctf_metadata(cfg); | |
612 | break; | |
613 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: | |
614 | print_cfg_print_lttng_live_sessions(cfg); | |
615 | break; | |
290725f7 | 616 | default: |
0fbb9a9f | 617 | abort(); |
290725f7 | 618 | } |
c42c79ea PP |
619 | } |
620 | ||
9009cc24 | 621 | static |
b19ff26f | 622 | void print_plugin_info(const bt_plugin *plugin) |
22e22462 PP |
623 | { |
624 | unsigned int major, minor, patch; | |
625 | const char *extra; | |
4cdfc5e8 | 626 | bt_property_availability version_avail; |
22e22462 PP |
627 | const char *plugin_name; |
628 | const char *path; | |
629 | const char *author; | |
630 | const char *license; | |
631 | const char *plugin_description; | |
632 | ||
633 | plugin_name = bt_plugin_get_name(plugin); | |
634 | path = bt_plugin_get_path(plugin); | |
635 | author = bt_plugin_get_author(plugin); | |
636 | license = bt_plugin_get_license(plugin); | |
637 | plugin_description = bt_plugin_get_description(plugin); | |
9724cce9 | 638 | version_avail = bt_plugin_get_version(plugin, &major, &minor, |
22e22462 PP |
639 | &patch, &extra); |
640 | printf("%s%s%s%s:\n", bt_common_color_bold(), | |
641 | bt_common_color_fg_blue(), plugin_name, | |
642 | bt_common_color_reset()); | |
a157fea9 MJ |
643 | if (path) { |
644 | printf(" %sPath%s: %s\n", bt_common_color_bold(), | |
645 | bt_common_color_reset(), path); | |
646 | } else { | |
647 | puts(" Built-in"); | |
648 | } | |
22e22462 | 649 | |
9724cce9 | 650 | if (version_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
22e22462 PP |
651 | printf(" %sVersion%s: %u.%u.%u", |
652 | bt_common_color_bold(), bt_common_color_reset(), | |
653 | major, minor, patch); | |
654 | ||
655 | if (extra) { | |
656 | printf("%s", extra); | |
657 | } | |
658 | ||
659 | printf("\n"); | |
660 | } | |
661 | ||
662 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), | |
663 | bt_common_color_reset(), | |
664 | plugin_description ? plugin_description : "(None)"); | |
665 | printf(" %sAuthor%s: %s\n", bt_common_color_bold(), | |
666 | bt_common_color_reset(), author ? author : "(Unknown)"); | |
667 | printf(" %sLicense%s: %s\n", bt_common_color_bold(), | |
668 | bt_common_color_reset(), | |
669 | license ? license : "(Unknown)"); | |
670 | } | |
671 | ||
9009cc24 PP |
672 | static |
673 | int cmd_query(struct bt_config *cfg) | |
63ce0e1d | 674 | { |
db95fa29 | 675 | int ret = 0; |
b19ff26f PP |
676 | const bt_component_class *comp_cls = NULL; |
677 | const bt_value *results = NULL; | |
c7eee084 | 678 | const char *fail_reason = NULL; |
63ce0e1d | 679 | |
d94d92ac PP |
680 | comp_cls = find_component_class( |
681 | cfg->cmd_data.query.cfg_component->plugin_name->str, | |
db0f160a | 682 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
a67681c1 | 683 | cfg->cmd_data.query.cfg_component->type); |
63ce0e1d | 684 | if (!comp_cls) { |
cee05105 PP |
685 | BT_CLI_LOGE_APPEND_CAUSE( |
686 | "Cannot find component class: plugin-name=\"%s\", " | |
7213a328 PP |
687 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
688 | cfg->cmd_data.query.cfg_component->plugin_name->str, | |
689 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, | |
690 | cfg->cmd_data.query.cfg_component->type); | |
63ce0e1d PP |
691 | ret = -1; |
692 | goto end; | |
693 | } | |
694 | ||
f4e38e70 | 695 | ret = query(cfg, comp_cls, cfg->cmd_data.query.object->str, |
05e21286 | 696 | cfg->cmd_data.query.cfg_component->params, |
da91b29a | 697 | &results, &fail_reason); |
c7eee084 PP |
698 | if (ret) { |
699 | goto failed; | |
63ce0e1d PP |
700 | } |
701 | ||
7213a328 | 702 | print_value(stdout, results, 0); |
c7eee084 PP |
703 | goto end; |
704 | ||
705 | failed: | |
cee05105 PP |
706 | BT_CLI_LOGE_APPEND_CAUSE( |
707 | "Failed to query component class: %s: plugin-name=\"%s\", " | |
c7eee084 PP |
708 | "comp-cls-name=\"%s\", comp-cls-type=%d " |
709 | "object=\"%s\"", fail_reason, | |
710 | cfg->cmd_data.query.cfg_component->plugin_name->str, | |
711 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, | |
712 | cfg->cmd_data.query.cfg_component->type, | |
713 | cfg->cmd_data.query.object->str); | |
c7eee084 | 714 | ret = -1; |
63ce0e1d PP |
715 | |
716 | end: | |
c5b9b441 PP |
717 | bt_component_class_put_ref(comp_cls); |
718 | bt_value_put_ref(results); | |
63ce0e1d PP |
719 | return ret; |
720 | } | |
721 | ||
d94d92ac PP |
722 | static |
723 | void print_component_class_help(const char *plugin_name, | |
b19ff26f | 724 | const bt_component_class *comp_cls) |
d94d92ac PP |
725 | { |
726 | const char *comp_class_name = | |
727 | bt_component_class_get_name(comp_cls); | |
728 | const char *comp_class_description = | |
729 | bt_component_class_get_description(comp_cls); | |
730 | const char *comp_class_help = | |
731 | bt_component_class_get_help(comp_cls); | |
4cdfc5e8 | 732 | bt_component_class_type type = |
d94d92ac PP |
733 | bt_component_class_get_type(comp_cls); |
734 | ||
735 | print_plugin_comp_cls_opt(stdout, plugin_name, comp_class_name, type); | |
736 | printf("\n"); | |
737 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), | |
738 | bt_common_color_reset(), | |
739 | comp_class_description ? comp_class_description : "(None)"); | |
740 | ||
741 | if (comp_class_help) { | |
742 | printf("\n%s\n", comp_class_help); | |
743 | } | |
744 | } | |
745 | ||
9009cc24 PP |
746 | static |
747 | int cmd_help(struct bt_config *cfg) | |
22e22462 | 748 | { |
db95fa29 | 749 | int ret = 0; |
b19ff26f PP |
750 | const bt_plugin *plugin = NULL; |
751 | const bt_component_class *needed_comp_cls = NULL; | |
22e22462 | 752 | |
743138a3 | 753 | plugin = find_loaded_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str); |
22e22462 | 754 | if (!plugin) { |
cee05105 PP |
755 | BT_CLI_LOGE_APPEND_CAUSE( |
756 | "Cannot find plugin: plugin-name=\"%s\"", | |
7213a328 | 757 | cfg->cmd_data.help.cfg_component->plugin_name->str); |
22e22462 PP |
758 | ret = -1; |
759 | goto end; | |
760 | } | |
761 | ||
762 | print_plugin_info(plugin); | |
d94d92ac PP |
763 | printf(" %sSource component classes%s: %d\n", |
764 | bt_common_color_bold(), | |
765 | bt_common_color_reset(), | |
766 | (int) bt_plugin_get_source_component_class_count(plugin)); | |
767 | printf(" %sFilter component classes%s: %d\n", | |
22e22462 PP |
768 | bt_common_color_bold(), |
769 | bt_common_color_reset(), | |
d94d92ac PP |
770 | (int) bt_plugin_get_filter_component_class_count(plugin)); |
771 | printf(" %sSink component classes%s: %d\n", | |
772 | bt_common_color_bold(), | |
773 | bt_common_color_reset(), | |
774 | (int) bt_plugin_get_sink_component_class_count(plugin)); | |
22e22462 | 775 | |
d94d92ac PP |
776 | if (strlen(cfg->cmd_data.help.cfg_component->comp_cls_name->str) == 0) { |
777 | /* Plugin help only */ | |
778 | goto end; | |
779 | } | |
22e22462 | 780 | |
d94d92ac PP |
781 | needed_comp_cls = find_component_class( |
782 | cfg->cmd_data.help.cfg_component->plugin_name->str, | |
783 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, | |
784 | cfg->cmd_data.help.cfg_component->type); | |
785 | if (!needed_comp_cls) { | |
cee05105 PP |
786 | BT_CLI_LOGE_APPEND_CAUSE( |
787 | "Cannot find component class: plugin-name=\"%s\", " | |
d94d92ac PP |
788 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
789 | cfg->cmd_data.help.cfg_component->plugin_name->str, | |
790 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, | |
791 | cfg->cmd_data.help.cfg_component->type); | |
d94d92ac PP |
792 | ret = -1; |
793 | goto end; | |
794 | } | |
22e22462 | 795 | |
d94d92ac PP |
796 | printf("\n"); |
797 | print_component_class_help( | |
798 | cfg->cmd_data.help.cfg_component->plugin_name->str, | |
799 | needed_comp_cls); | |
800 | ||
801 | end: | |
c5b9b441 PP |
802 | bt_component_class_put_ref(needed_comp_cls); |
803 | bt_plugin_put_ref(plugin); | |
d94d92ac PP |
804 | return ret; |
805 | } | |
806 | ||
b19ff26f | 807 | typedef void *(* plugin_borrow_comp_cls_by_index_func_t)(const bt_plugin *, |
d94d92ac | 808 | uint64_t); |
b19ff26f | 809 | typedef const bt_component_class *(* spec_comp_cls_borrow_comp_cls_func_t)( |
d94d92ac PP |
810 | void *); |
811 | ||
7c7301d5 | 812 | static |
b19ff26f | 813 | void cmd_list_plugins_print_component_classes(const bt_plugin *plugin, |
d94d92ac PP |
814 | const char *cc_type_name, uint64_t count, |
815 | plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func, | |
816 | spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func) | |
817 | { | |
818 | uint64_t i; | |
22e22462 | 819 | |
d94d92ac | 820 | if (count == 0) { |
8a7c9d06 | 821 | printf(" %s%s component classes%s: (none)\n", |
d94d92ac | 822 | bt_common_color_bold(), |
8a7c9d06 | 823 | cc_type_name, |
d94d92ac PP |
824 | bt_common_color_reset()); |
825 | goto end; | |
826 | } else { | |
8a7c9d06 | 827 | printf(" %s%s component classes%s:\n", |
d94d92ac | 828 | bt_common_color_bold(), |
8a7c9d06 | 829 | cc_type_name, |
d94d92ac | 830 | bt_common_color_reset()); |
22e22462 PP |
831 | } |
832 | ||
d94d92ac | 833 | for (i = 0; i < count; i++) { |
b19ff26f | 834 | const bt_component_class *comp_class = |
d94d92ac PP |
835 | spec_comp_cls_borrow_comp_cls_func( |
836 | borrow_comp_cls_by_index_func(plugin, i)); | |
22e22462 | 837 | const char *comp_class_name = |
d94d92ac | 838 | bt_component_class_get_name(comp_class); |
22e22462 | 839 | const char *comp_class_description = |
d94d92ac | 840 | bt_component_class_get_description(comp_class); |
4cdfc5e8 | 841 | bt_component_class_type type = |
d94d92ac | 842 | bt_component_class_get_type(comp_class); |
22e22462 | 843 | |
d94d92ac | 844 | printf(" "); |
22e22462 | 845 | print_plugin_comp_cls_opt(stdout, |
d94d92ac | 846 | bt_plugin_get_name(plugin), comp_class_name, |
22e22462 | 847 | type); |
22e22462 | 848 | |
d94d92ac PP |
849 | if (comp_class_description) { |
850 | printf(": %s", comp_class_description); | |
22e22462 PP |
851 | } |
852 | ||
d94d92ac | 853 | printf("\n"); |
22e22462 PP |
854 | } |
855 | ||
856 | end: | |
d94d92ac | 857 | return; |
22e22462 PP |
858 | } |
859 | ||
9009cc24 PP |
860 | static |
861 | int cmd_list_plugins(struct bt_config *cfg) | |
290725f7 | 862 | { |
7213a328 | 863 | int ret = 0; |
290725f7 PP |
864 | int plugins_count, component_classes_count = 0, i; |
865 | ||
22e22462 | 866 | printf("From the following plugin paths:\n\n"); |
05e21286 | 867 | print_value(stdout, cfg->plugin_paths, 2); |
22e22462 | 868 | printf("\n"); |
743138a3 | 869 | plugins_count = get_loaded_plugins_count(); |
290725f7 | 870 | if (plugins_count == 0) { |
7213a328 | 871 | printf("No plugins found.\n"); |
56a1cced JG |
872 | goto end; |
873 | } | |
874 | ||
290725f7 | 875 | for (i = 0; i < plugins_count; i++) { |
743138a3 | 876 | const bt_plugin *plugin = borrow_loaded_plugin(i); |
290725f7 | 877 | |
d94d92ac PP |
878 | component_classes_count += |
879 | bt_plugin_get_source_component_class_count(plugin) + | |
880 | bt_plugin_get_filter_component_class_count(plugin) + | |
881 | bt_plugin_get_sink_component_class_count(plugin); | |
290725f7 | 882 | } |
33bceaf8 | 883 | |
290725f7 PP |
884 | printf("Found %s%d%s component classes in %s%d%s plugins.\n", |
885 | bt_common_color_bold(), | |
886 | component_classes_count, | |
887 | bt_common_color_reset(), | |
888 | bt_common_color_bold(), | |
889 | plugins_count, | |
890 | bt_common_color_reset()); | |
891 | ||
892 | for (i = 0; i < plugins_count; i++) { | |
743138a3 | 893 | const bt_plugin *plugin = borrow_loaded_plugin(i); |
290725f7 | 894 | |
22e22462 PP |
895 | printf("\n"); |
896 | print_plugin_info(plugin); | |
d94d92ac PP |
897 | cmd_list_plugins_print_component_classes(plugin, "Source", |
898 | bt_plugin_get_source_component_class_count(plugin), | |
899 | (plugin_borrow_comp_cls_by_index_func_t) | |
c3dd43ec | 900 | bt_plugin_borrow_source_component_class_by_index_const, |
d94d92ac | 901 | (spec_comp_cls_borrow_comp_cls_func_t) |
707b7d35 | 902 | bt_component_class_source_as_component_class); |
d94d92ac PP |
903 | cmd_list_plugins_print_component_classes(plugin, "Filter", |
904 | bt_plugin_get_filter_component_class_count(plugin), | |
905 | (plugin_borrow_comp_cls_by_index_func_t) | |
c3dd43ec | 906 | bt_plugin_borrow_filter_component_class_by_index_const, |
d94d92ac | 907 | (spec_comp_cls_borrow_comp_cls_func_t) |
707b7d35 | 908 | bt_component_class_filter_as_component_class); |
d94d92ac PP |
909 | cmd_list_plugins_print_component_classes(plugin, "Sink", |
910 | bt_plugin_get_sink_component_class_count(plugin), | |
911 | (plugin_borrow_comp_cls_by_index_func_t) | |
c3dd43ec | 912 | bt_plugin_borrow_sink_component_class_by_index_const, |
d94d92ac | 913 | (spec_comp_cls_borrow_comp_cls_func_t) |
707b7d35 | 914 | bt_component_class_sink_as_component_class); |
290725f7 PP |
915 | } |
916 | ||
917 | end: | |
918 | return ret; | |
919 | } | |
920 | ||
9009cc24 PP |
921 | static |
922 | int cmd_print_lttng_live_sessions(struct bt_config *cfg) | |
db0f160a | 923 | { |
96e8c7e1 | 924 | int ret = 0; |
b19ff26f PP |
925 | const bt_component_class *comp_cls = NULL; |
926 | const bt_value *results = NULL; | |
927 | bt_value *params = NULL; | |
928 | const bt_value *map = NULL; | |
929 | const bt_value *v = NULL; | |
96e8c7e1 MD |
930 | static const char * const plugin_name = "ctf"; |
931 | static const char * const comp_cls_name = "lttng-live"; | |
4cdfc5e8 | 932 | static const bt_component_class_type comp_cls_type = |
96e8c7e1 | 933 | BT_COMPONENT_CLASS_TYPE_SOURCE; |
f80e9ec1 | 934 | uint64_t array_size, i; |
c7eee084 | 935 | const char *fail_reason = NULL; |
c327e427 | 936 | FILE *out_stream = stdout; |
96e8c7e1 | 937 | |
f6ccaed9 | 938 | BT_ASSERT(cfg->cmd_data.print_lttng_live_sessions.url); |
96e8c7e1 MD |
939 | comp_cls = find_component_class(plugin_name, comp_cls_name, |
940 | comp_cls_type); | |
941 | if (!comp_cls) { | |
cee05105 PP |
942 | BT_CLI_LOGE_APPEND_CAUSE( |
943 | "Cannot find component class: plugin-name=\"%s\", " | |
96e8c7e1 MD |
944 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
945 | plugin_name, comp_cls_name, | |
946 | BT_COMPONENT_CLASS_TYPE_SOURCE); | |
96e8c7e1 MD |
947 | goto error; |
948 | } | |
949 | ||
05e21286 | 950 | params = bt_value_map_create(); |
96e8c7e1 MD |
951 | if (!params) { |
952 | goto error; | |
953 | } | |
954 | ||
05e21286 | 955 | ret = bt_value_map_insert_string_entry(params, "url", |
96e8c7e1 MD |
956 | cfg->cmd_data.print_lttng_live_sessions.url->str); |
957 | if (ret) { | |
958 | goto error; | |
959 | } | |
960 | ||
f4e38e70 | 961 | ret = query(cfg, comp_cls, "sessions", params, |
05e21286 | 962 | &results, &fail_reason); |
c7eee084 PP |
963 | if (ret) { |
964 | goto failed; | |
96e8c7e1 MD |
965 | } |
966 | ||
f6ccaed9 PP |
967 | BT_ASSERT(results); |
968 | ||
96e8c7e1 | 969 | if (!bt_value_is_array(results)) { |
cee05105 PP |
970 | BT_CLI_LOGE_APPEND_CAUSE( |
971 | "Expecting an array for LTTng live `sessions` query."); | |
96e8c7e1 MD |
972 | goto error; |
973 | } | |
974 | ||
c327e427 PP |
975 | if (cfg->cmd_data.print_lttng_live_sessions.output_path->len > 0) { |
976 | out_stream = | |
977 | fopen(cfg->cmd_data.print_lttng_live_sessions.output_path->str, | |
978 | "w"); | |
979 | if (!out_stream) { | |
980 | ret = -1; | |
981 | BT_LOGE_ERRNO("Cannot open file for writing", | |
982 | ": path=\"%s\"", | |
983 | cfg->cmd_data.print_lttng_live_sessions.output_path->str); | |
cee05105 PP |
984 | (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( |
985 | "Babeltrace CLI", | |
986 | "Cannot open file for writing: path=\"%s\"", | |
987 | cfg->cmd_data.print_lttng_live_sessions.output_path->str); | |
c327e427 PP |
988 | goto end; |
989 | } | |
990 | } | |
991 | ||
393729a6 | 992 | array_size = bt_value_array_get_length(results); |
96e8c7e1 MD |
993 | for (i = 0; i < array_size; i++) { |
994 | const char *url_text; | |
995 | int64_t timer_us, streams, clients; | |
996 | ||
05e21286 | 997 | map = bt_value_array_borrow_element_by_index_const(results, i); |
96e8c7e1 | 998 | if (!bt_value_is_map(map)) { |
cee05105 | 999 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type."); |
96e8c7e1 MD |
1000 | goto error; |
1001 | } | |
1002 | ||
05e21286 | 1003 | v = bt_value_map_borrow_entry_value_const(map, "url"); |
96e8c7e1 | 1004 | if (!v) { |
cee05105 | 1005 | BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry."); |
96e8c7e1 MD |
1006 | goto error; |
1007 | } | |
601b0d3c | 1008 | url_text = bt_value_string_get(v); |
c327e427 | 1009 | fprintf(out_stream, "%s", url_text); |
05e21286 | 1010 | v = bt_value_map_borrow_entry_value_const(map, "timer-us"); |
96e8c7e1 | 1011 | if (!v) { |
cee05105 | 1012 | BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry."); |
96e8c7e1 MD |
1013 | goto error; |
1014 | } | |
a91cb83e | 1015 | timer_us = bt_value_integer_unsigned_get(v); |
c327e427 | 1016 | fprintf(out_stream, " (timer = %" PRIu64 ", ", timer_us); |
05e21286 | 1017 | v = bt_value_map_borrow_entry_value_const(map, "stream-count"); |
96e8c7e1 | 1018 | if (!v) { |
cee05105 PP |
1019 | BT_CLI_LOGE_APPEND_CAUSE( |
1020 | "Missing `stream-count` entry."); | |
96e8c7e1 MD |
1021 | goto error; |
1022 | } | |
a91cb83e | 1023 | streams = bt_value_integer_unsigned_get(v); |
c327e427 | 1024 | fprintf(out_stream, "%" PRIu64 " stream(s), ", streams); |
05e21286 | 1025 | v = bt_value_map_borrow_entry_value_const(map, "client-count"); |
96e8c7e1 | 1026 | if (!v) { |
cee05105 PP |
1027 | BT_CLI_LOGE_APPEND_CAUSE( |
1028 | "Missing `client-count` entry."); | |
96e8c7e1 MD |
1029 | goto error; |
1030 | } | |
a91cb83e | 1031 | clients = bt_value_integer_unsigned_get(v); |
c327e427 | 1032 | fprintf(out_stream, "%" PRIu64 " client(s) connected)\n", clients); |
96e8c7e1 | 1033 | } |
c7eee084 PP |
1034 | |
1035 | goto end; | |
1036 | ||
1037 | failed: | |
cee05105 PP |
1038 | BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s", |
1039 | fail_reason); | |
c7eee084 PP |
1040 | |
1041 | error: | |
1042 | ret = -1; | |
1043 | ||
96e8c7e1 | 1044 | end: |
c5b9b441 PP |
1045 | bt_value_put_ref(results); |
1046 | bt_value_put_ref(params); | |
1047 | bt_component_class_put_ref(comp_cls); | |
c327e427 PP |
1048 | |
1049 | if (out_stream && out_stream != stdout) { | |
1050 | int fclose_ret = fclose(out_stream); | |
1051 | ||
1052 | if (fclose_ret) { | |
1053 | BT_LOGE_ERRNO("Cannot close file stream", | |
1054 | ": path=\"%s\"", | |
1055 | cfg->cmd_data.print_lttng_live_sessions.output_path->str); | |
1056 | } | |
1057 | } | |
1058 | ||
bb345f58 | 1059 | return ret; |
db0f160a PP |
1060 | } |
1061 | ||
9009cc24 PP |
1062 | static |
1063 | int cmd_print_ctf_metadata(struct bt_config *cfg) | |
05a67631 PP |
1064 | { |
1065 | int ret = 0; | |
b19ff26f PP |
1066 | const bt_component_class *comp_cls = NULL; |
1067 | const bt_value *results = NULL; | |
1068 | bt_value *params = NULL; | |
1069 | const bt_value *metadata_text_value = NULL; | |
05a67631 | 1070 | const char *metadata_text = NULL; |
db0f160a PP |
1071 | static const char * const plugin_name = "ctf"; |
1072 | static const char * const comp_cls_name = "fs"; | |
4cdfc5e8 | 1073 | static const bt_component_class_type comp_cls_type = |
db0f160a | 1074 | BT_COMPONENT_CLASS_TYPE_SOURCE; |
c7eee084 | 1075 | const char *fail_reason = NULL; |
c327e427 | 1076 | FILE *out_stream = stdout; |
db0f160a | 1077 | |
f6ccaed9 | 1078 | BT_ASSERT(cfg->cmd_data.print_ctf_metadata.path); |
db0f160a PP |
1079 | comp_cls = find_component_class(plugin_name, comp_cls_name, |
1080 | comp_cls_type); | |
05a67631 | 1081 | if (!comp_cls) { |
cee05105 PP |
1082 | BT_CLI_LOGE_APPEND_CAUSE( |
1083 | "Cannot find component class: plugin-name=\"%s\", " | |
7213a328 PP |
1084 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
1085 | plugin_name, comp_cls_name, | |
1086 | BT_COMPONENT_CLASS_TYPE_SOURCE); | |
05a67631 PP |
1087 | ret = -1; |
1088 | goto end; | |
1089 | } | |
1090 | ||
05e21286 | 1091 | params = bt_value_map_create(); |
05a67631 PP |
1092 | if (!params) { |
1093 | ret = -1; | |
1094 | goto end; | |
1095 | } | |
1096 | ||
05e21286 | 1097 | ret = bt_value_map_insert_string_entry(params, "path", |
db0f160a | 1098 | cfg->cmd_data.print_ctf_metadata.path->str); |
05a67631 PP |
1099 | if (ret) { |
1100 | ret = -1; | |
1101 | goto end; | |
1102 | } | |
1103 | ||
f4e38e70 | 1104 | ret = query(cfg, comp_cls, "metadata-info", |
05e21286 | 1105 | params, &results, &fail_reason); |
c7eee084 PP |
1106 | if (ret) { |
1107 | goto failed; | |
05a67631 PP |
1108 | } |
1109 | ||
05e21286 | 1110 | metadata_text_value = bt_value_map_borrow_entry_value_const(results, |
cee05105 | 1111 | "text"); |
05a67631 | 1112 | if (!metadata_text_value) { |
cee05105 PP |
1113 | BT_CLI_LOGE_APPEND_CAUSE( |
1114 | "Cannot find `text` string value in the resulting metadata info object."); | |
05a67631 PP |
1115 | ret = -1; |
1116 | goto end; | |
1117 | } | |
1118 | ||
601b0d3c | 1119 | metadata_text = bt_value_string_get(metadata_text_value); |
c327e427 PP |
1120 | |
1121 | if (cfg->cmd_data.print_ctf_metadata.output_path->len > 0) { | |
1122 | out_stream = | |
1123 | fopen(cfg->cmd_data.print_ctf_metadata.output_path->str, | |
1124 | "w"); | |
1125 | if (!out_stream) { | |
c327e427 PP |
1126 | BT_LOGE_ERRNO("Cannot open file for writing", |
1127 | ": path=\"%s\"", | |
1128 | cfg->cmd_data.print_ctf_metadata.output_path->str); | |
cee05105 PP |
1129 | (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( |
1130 | "Babeltrace CLI", | |
1131 | "Cannot open file for writing: path=\"%s\"", | |
1132 | cfg->cmd_data.print_ctf_metadata.output_path->str); | |
1133 | ret = -1; | |
c327e427 PP |
1134 | goto end; |
1135 | } | |
1136 | } | |
1137 | ||
1138 | ret = fprintf(out_stream, "%s\n", metadata_text); | |
1139 | if (ret < 0) { | |
cee05105 PP |
1140 | BT_CLI_LOGE_APPEND_CAUSE( |
1141 | "Cannot write whole metadata text to output stream: " | |
c327e427 | 1142 | "ret=%d", ret); |
b4c499ed | 1143 | goto end; |
c327e427 PP |
1144 | } |
1145 | ||
b4c499ed | 1146 | ret = 0; |
c7eee084 PP |
1147 | goto end; |
1148 | ||
1149 | failed: | |
1150 | ret = -1; | |
cee05105 PP |
1151 | BT_CLI_LOGE_APPEND_CAUSE( |
1152 | "Failed to query `metadata-info` object: %s", fail_reason); | |
05a67631 PP |
1153 | |
1154 | end: | |
c5b9b441 PP |
1155 | bt_value_put_ref(results); |
1156 | bt_value_put_ref(params); | |
1157 | bt_component_class_put_ref(comp_cls); | |
c327e427 PP |
1158 | |
1159 | if (out_stream && out_stream != stdout) { | |
1160 | int fclose_ret = fclose(out_stream); | |
1161 | ||
1162 | if (fclose_ret) { | |
1163 | BT_LOGE_ERRNO("Cannot close file stream", | |
1164 | ": path=\"%s\"", | |
1165 | cfg->cmd_data.print_ctf_metadata.output_path->str); | |
1166 | } | |
1167 | } | |
1168 | ||
bb345f58 | 1169 | return ret; |
05a67631 PP |
1170 | } |
1171 | ||
75a2cb9b JG |
1172 | struct port_id { |
1173 | char *instance_name; | |
1174 | char *port_name; | |
1175 | }; | |
1176 | ||
1177 | struct trace_range { | |
1178 | uint64_t intersection_range_begin_ns; | |
1179 | uint64_t intersection_range_end_ns; | |
1180 | }; | |
1181 | ||
1182 | static | |
1183 | guint port_id_hash(gconstpointer v) | |
1184 | { | |
1185 | const struct port_id *id = v; | |
1186 | ||
f6ccaed9 PP |
1187 | BT_ASSERT(id->instance_name); |
1188 | BT_ASSERT(id->port_name); | |
75a2cb9b JG |
1189 | |
1190 | return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name); | |
1191 | } | |
1192 | ||
1193 | static | |
1194 | gboolean port_id_equal(gconstpointer v1, gconstpointer v2) | |
1195 | { | |
1196 | const struct port_id *id1 = v1; | |
1197 | const struct port_id *id2 = v2; | |
1198 | ||
2242b43d PP |
1199 | return strcmp(id1->instance_name, id2->instance_name) == 0 && |
1200 | strcmp(id1->port_name, id2->port_name) == 0; | |
75a2cb9b JG |
1201 | } |
1202 | ||
1203 | static | |
1204 | void port_id_destroy(gpointer data) | |
1205 | { | |
1206 | struct port_id *id = data; | |
1207 | ||
1208 | free(id->instance_name); | |
1209 | free(id->port_name); | |
1210 | free(id); | |
1211 | } | |
1212 | ||
1213 | static | |
1214 | void trace_range_destroy(gpointer data) | |
1215 | { | |
1216 | free(data); | |
1217 | } | |
1218 | ||
9009cc24 PP |
1219 | struct cmd_run_ctx { |
1220 | /* Owned by this */ | |
d94d92ac PP |
1221 | GHashTable *src_components; |
1222 | ||
1223 | /* Owned by this */ | |
1224 | GHashTable *flt_components; | |
1225 | ||
1226 | /* Owned by this */ | |
1227 | GHashTable *sink_components; | |
9009cc24 PP |
1228 | |
1229 | /* Owned by this */ | |
b19ff26f | 1230 | bt_graph *graph; |
9009cc24 PP |
1231 | |
1232 | /* Weak */ | |
1233 | struct bt_config *cfg; | |
1234 | ||
1235 | bool connect_ports; | |
75a2cb9b JG |
1236 | |
1237 | bool stream_intersection_mode; | |
1238 | ||
1239 | /* | |
1240 | * Association of struct port_id -> struct trace_range. | |
1241 | */ | |
1242 | GHashTable *intersections; | |
9009cc24 PP |
1243 | }; |
1244 | ||
75a2cb9b JG |
1245 | /* Returns a timestamp of the form "(-)s.ns" */ |
1246 | static | |
1247 | char *s_from_ns(int64_t ns) | |
1248 | { | |
1249 | int ret; | |
1250 | char *s_ret = NULL; | |
1251 | bool is_negative; | |
1252 | int64_t ts_sec_abs, ts_nsec_abs; | |
1253 | int64_t ts_sec = ns / NSEC_PER_SEC; | |
1254 | int64_t ts_nsec = ns % NSEC_PER_SEC; | |
1255 | ||
1256 | if (ts_sec >= 0 && ts_nsec >= 0) { | |
1257 | is_negative = false; | |
1258 | ts_sec_abs = ts_sec; | |
1259 | ts_nsec_abs = ts_nsec; | |
1260 | } else if (ts_sec > 0 && ts_nsec < 0) { | |
1261 | is_negative = false; | |
1262 | ts_sec_abs = ts_sec - 1; | |
1263 | ts_nsec_abs = NSEC_PER_SEC + ts_nsec; | |
1264 | } else if (ts_sec == 0 && ts_nsec < 0) { | |
1265 | is_negative = true; | |
1266 | ts_sec_abs = ts_sec; | |
1267 | ts_nsec_abs = -ts_nsec; | |
1268 | } else if (ts_sec < 0 && ts_nsec > 0) { | |
1269 | is_negative = true; | |
1270 | ts_sec_abs = -(ts_sec + 1); | |
1271 | ts_nsec_abs = NSEC_PER_SEC - ts_nsec; | |
1272 | } else if (ts_sec < 0 && ts_nsec == 0) { | |
1273 | is_negative = true; | |
1274 | ts_sec_abs = -ts_sec; | |
1275 | ts_nsec_abs = ts_nsec; | |
1276 | } else { /* (ts_sec < 0 && ts_nsec < 0) */ | |
1277 | is_negative = true; | |
1278 | ts_sec_abs = -ts_sec; | |
1279 | ts_nsec_abs = -ts_nsec; | |
1280 | } | |
1281 | ||
1282 | ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64, | |
1283 | is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs); | |
1284 | if (ret < 0) { | |
1285 | s_ret = NULL; | |
1286 | } | |
1287 | return s_ret; | |
1288 | } | |
1289 | ||
9009cc24 PP |
1290 | static |
1291 | int cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
d94d92ac | 1292 | struct cmd_run_ctx *ctx, |
b19ff26f PP |
1293 | const bt_component *upstream_comp, |
1294 | const bt_port_output *out_upstream_port, | |
9009cc24 | 1295 | struct bt_config_connection *cfg_conn) |
290725f7 | 1296 | { |
d94d92ac | 1297 | typedef uint64_t (*input_port_count_func_t)(void *); |
b19ff26f | 1298 | typedef const bt_port_input *(*borrow_input_port_by_index_func_t)( |
0d72b8c3 | 1299 | const void *, uint64_t); |
b19ff26f | 1300 | const bt_port *upstream_port = |
0d72b8c3 | 1301 | bt_port_output_as_port_const(out_upstream_port); |
d94d92ac | 1302 | |
290725f7 | 1303 | int ret = 0; |
9009cc24 | 1304 | GQuark downstreamp_comp_name_quark; |
d94d92ac PP |
1305 | void *downstream_comp; |
1306 | uint64_t downstream_port_count; | |
9009cc24 | 1307 | uint64_t i; |
d94d92ac PP |
1308 | input_port_count_func_t port_count_fn; |
1309 | borrow_input_port_by_index_func_t port_by_index_fn; | |
d24d5663 PP |
1310 | bt_graph_connect_ports_status connect_ports_status = |
1311 | BT_GRAPH_CONNECT_PORTS_STATUS_OK; | |
75a2cb9b | 1312 | bool insert_trimmer = false; |
b19ff26f | 1313 | bt_value *trimmer_params = NULL; |
75a2cb9b JG |
1314 | char *intersection_begin = NULL; |
1315 | char *intersection_end = NULL; | |
b19ff26f PP |
1316 | const bt_component_filter *trimmer = NULL; |
1317 | const bt_component_class_filter *trimmer_class = NULL; | |
1318 | const bt_port_input *trimmer_input = NULL; | |
1319 | const bt_port_output *trimmer_output = NULL; | |
75a2cb9b JG |
1320 | |
1321 | if (ctx->intersections && | |
1322 | bt_component_get_class_type(upstream_comp) == | |
1323 | BT_COMPONENT_CLASS_TYPE_SOURCE) { | |
1324 | struct trace_range *range; | |
1325 | struct port_id port_id = { | |
1326 | .instance_name = (char *) bt_component_get_name(upstream_comp), | |
1327 | .port_name = (char *) bt_port_get_name(upstream_port) | |
1328 | }; | |
1329 | ||
1330 | if (!port_id.instance_name || !port_id.port_name) { | |
1331 | goto error; | |
1332 | } | |
1333 | ||
1334 | range = (struct trace_range *) g_hash_table_lookup( | |
1335 | ctx->intersections, &port_id); | |
1336 | if (range) { | |
d24d5663 | 1337 | bt_value_map_insert_entry_status insert_status; |
75a2cb9b JG |
1338 | |
1339 | intersection_begin = s_from_ns( | |
1340 | range->intersection_range_begin_ns); | |
1341 | intersection_end = s_from_ns( | |
1342 | range->intersection_range_end_ns); | |
1343 | if (!intersection_begin || !intersection_end) { | |
cee05105 PP |
1344 | BT_CLI_LOGE_APPEND_CAUSE( |
1345 | "Cannot create trimmer argument timestamp string."); | |
75a2cb9b JG |
1346 | goto error; |
1347 | } | |
1348 | ||
1349 | insert_trimmer = true; | |
05e21286 | 1350 | trimmer_params = bt_value_map_create(); |
75a2cb9b JG |
1351 | if (!trimmer_params) { |
1352 | goto error; | |
1353 | } | |
1354 | ||
d24d5663 | 1355 | insert_status = bt_value_map_insert_string_entry( |
07208d85 | 1356 | trimmer_params, "begin", intersection_begin); |
d24d5663 | 1357 | if (insert_status < 0) { |
75a2cb9b JG |
1358 | goto error; |
1359 | } | |
d24d5663 | 1360 | insert_status = bt_value_map_insert_string_entry( |
07208d85 | 1361 | trimmer_params, |
75a2cb9b | 1362 | "end", intersection_end); |
d24d5663 | 1363 | if (insert_status < 0) { |
75a2cb9b JG |
1364 | goto error; |
1365 | } | |
1366 | } | |
1367 | ||
d94d92ac | 1368 | trimmer_class = find_filter_component_class("utils", "trimmer"); |
75a2cb9b JG |
1369 | if (!trimmer_class) { |
1370 | goto error; | |
1371 | } | |
1372 | } | |
9009cc24 | 1373 | |
7213a328 PP |
1374 | BT_LOGI("Connecting upstream port to the next available downstream port: " |
1375 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1376 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1377 | upstream_port, bt_port_get_name(upstream_port), | |
1378 | cfg_conn->downstream_comp_name->str, | |
1379 | cfg_conn->arg->str); | |
9009cc24 PP |
1380 | downstreamp_comp_name_quark = g_quark_from_string( |
1381 | cfg_conn->downstream_comp_name->str); | |
f6ccaed9 | 1382 | BT_ASSERT(downstreamp_comp_name_quark > 0); |
d94d92ac | 1383 | downstream_comp = g_hash_table_lookup(ctx->flt_components, |
71c7c95f | 1384 | GUINT_TO_POINTER(downstreamp_comp_name_quark)); |
d94d92ac PP |
1385 | port_count_fn = (input_port_count_func_t) |
1386 | bt_component_filter_get_input_port_count; | |
1387 | port_by_index_fn = (borrow_input_port_by_index_func_t) | |
0d72b8c3 | 1388 | bt_component_filter_borrow_input_port_by_index_const; |
d94d92ac PP |
1389 | |
1390 | if (!downstream_comp) { | |
1391 | downstream_comp = g_hash_table_lookup(ctx->sink_components, | |
1392 | GUINT_TO_POINTER(downstreamp_comp_name_quark)); | |
1393 | port_count_fn = (input_port_count_func_t) | |
1394 | bt_component_sink_get_input_port_count; | |
1395 | port_by_index_fn = (borrow_input_port_by_index_func_t) | |
0d72b8c3 | 1396 | bt_component_sink_borrow_input_port_by_index_const; |
d94d92ac PP |
1397 | } |
1398 | ||
9009cc24 | 1399 | if (!downstream_comp) { |
cee05105 PP |
1400 | BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: " |
1401 | "comp-name=\"%s\", conn-arg=\"%s\"", | |
1402 | cfg_conn->downstream_comp_name->str, | |
9009cc24 PP |
1403 | cfg_conn->arg->str); |
1404 | goto error; | |
1405 | } | |
1406 | ||
9009cc24 | 1407 | downstream_port_count = port_count_fn(downstream_comp); |
9009cc24 PP |
1408 | |
1409 | for (i = 0; i < downstream_port_count; i++) { | |
b19ff26f | 1410 | const bt_port_input *in_downstream_port = |
9009cc24 | 1411 | port_by_index_fn(downstream_comp, i); |
b19ff26f | 1412 | const bt_port *downstream_port = |
0d72b8c3 | 1413 | bt_port_input_as_port_const(in_downstream_port); |
75a2cb9b | 1414 | const char *upstream_port_name; |
9009cc24 PP |
1415 | const char *downstream_port_name; |
1416 | ||
f6ccaed9 | 1417 | BT_ASSERT(downstream_port); |
9009cc24 | 1418 | |
75a2cb9b | 1419 | /* Skip port if it's already connected. */ |
9009cc24 | 1420 | if (bt_port_is_connected(downstream_port)) { |
3f7d4d90 | 1421 | BT_LOGI("Skipping downstream port: already connected: " |
7213a328 PP |
1422 | "port-addr=%p, port-name=\"%s\"", |
1423 | downstream_port, | |
1424 | bt_port_get_name(downstream_port)); | |
9009cc24 PP |
1425 | continue; |
1426 | } | |
1427 | ||
1428 | downstream_port_name = bt_port_get_name(downstream_port); | |
f6ccaed9 | 1429 | BT_ASSERT(downstream_port_name); |
75a2cb9b | 1430 | upstream_port_name = bt_port_get_name(upstream_port); |
f6ccaed9 | 1431 | BT_ASSERT(upstream_port_name); |
9009cc24 | 1432 | |
75a2cb9b | 1433 | if (!bt_common_star_glob_match( |
1974687e MJ |
1434 | cfg_conn->downstream_port_glob->str, SIZE_MAX, |
1435 | downstream_port_name, SIZE_MAX)) { | |
75a2cb9b JG |
1436 | continue; |
1437 | } | |
1438 | ||
1439 | if (insert_trimmer) { | |
1440 | /* | |
d94d92ac PP |
1441 | * In order to insert the trimmer between the |
1442 | * two components that were being connected, we | |
1443 | * create a connection configuration entry which | |
1444 | * describes a connection from the trimmer's | |
1445 | * output to the original input that was being | |
1446 | * connected. | |
75a2cb9b | 1447 | * |
d94d92ac PP |
1448 | * Hence, the creation of the trimmer will cause |
1449 | * the graph "new port" listener to establish | |
1450 | * all downstream connections as its output port | |
1451 | * is connected. We will then establish the | |
1452 | * connection between the original upstream | |
1453 | * source and the trimmer. | |
75a2cb9b JG |
1454 | */ |
1455 | char *trimmer_name = NULL; | |
d24d5663 | 1456 | bt_graph_add_component_status add_comp_status; |
75a2cb9b | 1457 | |
d94d92ac PP |
1458 | ret = asprintf(&trimmer_name, |
1459 | "stream-intersection-trimmer-%s", | |
75a2cb9b JG |
1460 | upstream_port_name); |
1461 | if (ret < 0) { | |
1462 | goto error; | |
1463 | } | |
1464 | ret = 0; | |
1465 | ||
1466 | ctx->connect_ports = false; | |
d24d5663 | 1467 | add_comp_status = bt_graph_add_filter_component( |
d94d92ac | 1468 | ctx->graph, trimmer_class, trimmer_name, |
e874da19 PP |
1469 | trimmer_params, ctx->cfg->log_level, |
1470 | &trimmer); | |
75a2cb9b | 1471 | free(trimmer_name); |
d24d5663 PP |
1472 | if (add_comp_status != |
1473 | BT_GRAPH_ADD_COMPONENT_STATUS_OK) { | |
75a2cb9b JG |
1474 | goto error; |
1475 | } | |
f6ccaed9 | 1476 | BT_ASSERT(trimmer); |
75a2cb9b JG |
1477 | |
1478 | trimmer_input = | |
0d72b8c3 | 1479 | bt_component_filter_borrow_input_port_by_index_const( |
75a2cb9b JG |
1480 | trimmer, 0); |
1481 | if (!trimmer_input) { | |
1482 | goto error; | |
1483 | } | |
1484 | trimmer_output = | |
0d72b8c3 | 1485 | bt_component_filter_borrow_output_port_by_index_const( |
75a2cb9b JG |
1486 | trimmer, 0); |
1487 | if (!trimmer_output) { | |
9009cc24 PP |
1488 | goto error; |
1489 | } | |
1490 | ||
75a2cb9b JG |
1491 | /* |
1492 | * Replace the current downstream port by the trimmer's | |
1493 | * upstream port. | |
1494 | */ | |
d94d92ac PP |
1495 | in_downstream_port = trimmer_input; |
1496 | downstream_port = | |
0d72b8c3 | 1497 | bt_port_input_as_port_const(in_downstream_port); |
75a2cb9b JG |
1498 | downstream_port_name = bt_port_get_name( |
1499 | downstream_port); | |
d94d92ac | 1500 | BT_ASSERT(downstream_port_name); |
75a2cb9b JG |
1501 | } |
1502 | ||
1503 | /* We have a winner! */ | |
d24d5663 | 1504 | connect_ports_status = bt_graph_connect_ports(ctx->graph, |
d94d92ac PP |
1505 | out_upstream_port, in_downstream_port, NULL); |
1506 | downstream_port = NULL; | |
d24d5663 PP |
1507 | switch (connect_ports_status) { |
1508 | case BT_GRAPH_CONNECT_PORTS_STATUS_OK: | |
75a2cb9b | 1509 | break; |
75a2cb9b | 1510 | default: |
cee05105 PP |
1511 | BT_CLI_LOGE_APPEND_CAUSE( |
1512 | "Cannot create connection: graph refuses to connect ports: " | |
75a2cb9b JG |
1513 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
1514 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1515 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1516 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1517 | "conn-arg=\"%s\"", | |
1518 | upstream_comp, bt_component_get_name(upstream_comp), | |
1519 | upstream_port, bt_port_get_name(upstream_port), | |
1520 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1521 | downstream_port, downstream_port_name, | |
1522 | cfg_conn->arg->str); | |
75a2cb9b | 1523 | goto error; |
9009cc24 PP |
1524 | } |
1525 | ||
75a2cb9b JG |
1526 | BT_LOGI("Connected component ports: " |
1527 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
1528 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1529 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1530 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1531 | "conn-arg=\"%s\"", | |
1532 | upstream_comp, bt_component_get_name(upstream_comp), | |
1533 | upstream_port, bt_port_get_name(upstream_port), | |
1534 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1535 | downstream_port, downstream_port_name, | |
1536 | cfg_conn->arg->str); | |
1537 | ||
1538 | if (insert_trimmer) { | |
1539 | /* | |
1540 | * The first connection, from the source to the trimmer, | |
1541 | * has been done. We now connect the trimmer to the | |
1542 | * original downstream port. | |
1543 | */ | |
1544 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
d94d92ac | 1545 | ctx, |
0d72b8c3 | 1546 | bt_component_filter_as_component_const(trimmer), |
d94d92ac | 1547 | trimmer_output, cfg_conn); |
75a2cb9b JG |
1548 | if (ret) { |
1549 | goto error; | |
1550 | } | |
1551 | ctx->connect_ports = true; | |
1552 | } | |
9009cc24 | 1553 | |
53bc54cd PP |
1554 | /* |
1555 | * We found a matching downstream port: the search is | |
1556 | * over. | |
1557 | */ | |
1558 | goto end; | |
05a67631 PP |
1559 | } |
1560 | ||
53bc54cd | 1561 | /* No downstream port found */ |
cee05105 PP |
1562 | BT_CLI_LOGE_APPEND_CAUSE( |
1563 | "Cannot create connection: cannot find a matching downstream port for upstream port: " | |
53bc54cd PP |
1564 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
1565 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1566 | upstream_port, bt_port_get_name(upstream_port), | |
1567 | cfg_conn->downstream_comp_name->str, | |
1568 | cfg_conn->arg->str); | |
9009cc24 PP |
1569 | |
1570 | error: | |
1571 | ret = -1; | |
1572 | ||
1573 | end: | |
75a2cb9b JG |
1574 | free(intersection_begin); |
1575 | free(intersection_end); | |
c5b9b441 PP |
1576 | BT_VALUE_PUT_REF_AND_RESET(trimmer_params); |
1577 | BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class); | |
1578 | BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer); | |
9009cc24 PP |
1579 | return ret; |
1580 | } | |
1581 | ||
1582 | static | |
1583 | int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx, | |
b19ff26f | 1584 | const bt_port_output *upstream_port) |
9009cc24 PP |
1585 | { |
1586 | int ret = 0; | |
1587 | const char *upstream_port_name; | |
1588 | const char *upstream_comp_name; | |
b19ff26f | 1589 | const bt_component *upstream_comp = NULL; |
9009cc24 PP |
1590 | size_t i; |
1591 | ||
f6ccaed9 PP |
1592 | BT_ASSERT(ctx); |
1593 | BT_ASSERT(upstream_port); | |
d94d92ac | 1594 | upstream_port_name = bt_port_get_name( |
0d72b8c3 | 1595 | bt_port_output_as_port_const(upstream_port)); |
f6ccaed9 | 1596 | BT_ASSERT(upstream_port_name); |
0d72b8c3 PP |
1597 | upstream_comp = bt_port_borrow_component_const( |
1598 | bt_port_output_as_port_const(upstream_port)); | |
cee05105 | 1599 | BT_ASSERT(upstream_comp); |
9009cc24 | 1600 | upstream_comp_name = bt_component_get_name(upstream_comp); |
f6ccaed9 | 1601 | BT_ASSERT(upstream_comp_name); |
7213a328 PP |
1602 | BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", " |
1603 | "port-addr=%p, port-name=\"%s\"", | |
1604 | upstream_comp, upstream_comp_name, | |
1605 | upstream_port, upstream_port_name); | |
9009cc24 PP |
1606 | |
1607 | for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) { | |
1608 | struct bt_config_connection *cfg_conn = | |
1609 | g_ptr_array_index( | |
1610 | ctx->cfg->cmd_data.run.connections, i); | |
1611 | ||
1612 | if (strcmp(cfg_conn->upstream_comp_name->str, | |
75a2cb9b JG |
1613 | upstream_comp_name)) { |
1614 | continue; | |
1615 | } | |
1616 | ||
1617 | if (!bt_common_star_glob_match( | |
1618 | cfg_conn->upstream_port_glob->str, | |
1974687e | 1619 | SIZE_MAX, upstream_port_name, SIZE_MAX)) { |
75a2cb9b JG |
1620 | continue; |
1621 | } | |
1622 | ||
1623 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1624 | ctx, upstream_comp, upstream_port, cfg_conn); | |
1625 | if (ret) { | |
cee05105 PP |
1626 | BT_CLI_LOGE_APPEND_CAUSE( |
1627 | "Cannot connect upstream port: " | |
75a2cb9b JG |
1628 | "port-addr=%p, port-name=\"%s\"", |
1629 | upstream_port, | |
1630 | upstream_port_name); | |
75a2cb9b | 1631 | goto error; |
9009cc24 | 1632 | } |
75a2cb9b | 1633 | goto end; |
9009cc24 PP |
1634 | } |
1635 | ||
cee05105 PP |
1636 | BT_CLI_LOGE_APPEND_CAUSE( |
1637 | "Cannot connect upstream port: port does not match any connection argument: " | |
7213a328 PP |
1638 | "port-addr=%p, port-name=\"%s\"", upstream_port, |
1639 | upstream_port_name); | |
9009cc24 PP |
1640 | |
1641 | error: | |
1642 | ret = -1; | |
1643 | ||
1644 | end: | |
9009cc24 PP |
1645 | return ret; |
1646 | } | |
1647 | ||
1648 | static | |
d24d5663 | 1649 | bt_graph_listener_func_status |
8cc56726 | 1650 | graph_output_port_added_listener(struct cmd_run_ctx *ctx, |
b19ff26f | 1651 | const bt_port_output *out_port) |
9009cc24 | 1652 | { |
b19ff26f PP |
1653 | const bt_component *comp; |
1654 | const bt_port *port = bt_port_output_as_port_const(out_port); | |
d24d5663 PP |
1655 | bt_graph_listener_func_status ret = |
1656 | BT_GRAPH_LISTENER_FUNC_STATUS_OK; | |
9009cc24 | 1657 | |
0d72b8c3 | 1658 | comp = bt_port_borrow_component_const(port); |
e12720c0 PP |
1659 | BT_LOGI("Port added to a graph's component: comp-addr=%p, " |
1660 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
1661 | comp, comp ? bt_component_get_name(comp) : "", | |
7213a328 | 1662 | port, bt_port_get_name(port)); |
36712f1d PP |
1663 | |
1664 | if (!ctx->connect_ports) { | |
1665 | goto end; | |
1666 | } | |
1667 | ||
cee05105 | 1668 | BT_ASSERT(comp); |
7c7c0433 | 1669 | |
e12720c0 PP |
1670 | if (bt_port_is_connected(port)) { |
1671 | BT_LOGW_STR("Port is already connected."); | |
7c7c0433 JG |
1672 | goto end; |
1673 | } | |
1674 | ||
d94d92ac | 1675 | if (cmd_run_ctx_connect_upstream_port(ctx, out_port)) { |
cee05105 | 1676 | BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port."); |
d24d5663 | 1677 | ret = BT_GRAPH_LISTENER_FUNC_STATUS_ERROR; |
8cc56726 | 1678 | goto end; |
9009cc24 PP |
1679 | } |
1680 | ||
1681 | end: | |
8cc56726 | 1682 | return ret; |
9009cc24 PP |
1683 | } |
1684 | ||
1685 | static | |
d24d5663 | 1686 | bt_graph_listener_func_status graph_source_output_port_added_listener( |
b19ff26f PP |
1687 | const bt_component_source *component, |
1688 | const bt_port_output *port, void *data) | |
9009cc24 | 1689 | { |
8cc56726 | 1690 | return graph_output_port_added_listener(data, port); |
9009cc24 PP |
1691 | } |
1692 | ||
1693 | static | |
d24d5663 | 1694 | bt_graph_listener_func_status graph_filter_output_port_added_listener( |
b19ff26f PP |
1695 | const bt_component_filter *component, |
1696 | const bt_port_output *port, void *data) | |
9009cc24 | 1697 | { |
8cc56726 | 1698 | return graph_output_port_added_listener(data, port); |
9009cc24 PP |
1699 | } |
1700 | ||
1701 | static | |
1702 | void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx) | |
1703 | { | |
1704 | if (!ctx) { | |
1705 | return; | |
1706 | } | |
1707 | ||
d94d92ac PP |
1708 | if (ctx->src_components) { |
1709 | g_hash_table_destroy(ctx->src_components); | |
1710 | ctx->src_components = NULL; | |
1711 | } | |
1712 | ||
1713 | if (ctx->flt_components) { | |
1714 | g_hash_table_destroy(ctx->flt_components); | |
1715 | ctx->flt_components = NULL; | |
1716 | } | |
1717 | ||
1718 | if (ctx->sink_components) { | |
1719 | g_hash_table_destroy(ctx->sink_components); | |
1720 | ctx->sink_components = NULL; | |
9009cc24 PP |
1721 | } |
1722 | ||
75a2cb9b JG |
1723 | if (ctx->intersections) { |
1724 | g_hash_table_destroy(ctx->intersections); | |
a5302548 | 1725 | ctx->intersections = NULL; |
75a2cb9b JG |
1726 | } |
1727 | ||
c5b9b441 | 1728 | BT_GRAPH_PUT_REF_AND_RESET(ctx->graph); |
9009cc24 PP |
1729 | ctx->cfg = NULL; |
1730 | } | |
1731 | ||
5fea82b8 PP |
1732 | static |
1733 | int add_descriptor_to_component_descriptor_set( | |
1734 | bt_component_descriptor_set *comp_descr_set, | |
1735 | const char *plugin_name, const char *comp_cls_name, | |
1736 | bt_component_class_type comp_cls_type, | |
1737 | const bt_value *params) | |
1738 | { | |
1739 | const bt_component_class *comp_cls; | |
1740 | int status = 0; | |
1741 | ||
1742 | comp_cls = find_component_class(plugin_name, comp_cls_name, | |
1743 | comp_cls_type); | |
1744 | if (!comp_cls) { | |
1745 | BT_CLI_LOGE_APPEND_CAUSE( | |
1746 | "Cannot find component class: plugin-name=\"%s\", " | |
1747 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
1748 | plugin_name, comp_cls_name, comp_cls_type); | |
1749 | status = -1; | |
1750 | goto end; | |
1751 | } | |
1752 | ||
1753 | status = bt_component_descriptor_set_add_descriptor( | |
1754 | comp_descr_set, comp_cls, params); | |
1755 | if (status != BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK) { | |
1756 | BT_CLI_LOGE_APPEND_CAUSE( | |
1757 | "Cannot append descriptor to component descriptor set: " | |
1758 | "status=%s", bt_common_func_status_string(status)); | |
1759 | goto end; | |
1760 | } | |
1761 | ||
1762 | end: | |
1763 | bt_component_class_put_ref(comp_cls); | |
1764 | return status; | |
1765 | } | |
1766 | ||
1767 | static | |
1768 | int append_descriptors_from_bt_config_component_array( | |
1769 | bt_component_descriptor_set *comp_descr_set, | |
1770 | GPtrArray *component_configs) | |
1771 | { | |
1772 | int ret = 0; | |
1773 | uint64_t i; | |
1774 | ||
1775 | for (i = 0; i < component_configs->len; i++) { | |
1776 | struct bt_config_component *cfg_comp = | |
1777 | component_configs->pdata[i]; | |
1778 | ||
1779 | ret = add_descriptor_to_component_descriptor_set( | |
1780 | comp_descr_set, | |
1781 | cfg_comp->plugin_name->str, | |
1782 | cfg_comp->comp_cls_name->str, | |
1783 | cfg_comp->type, cfg_comp->params); | |
1784 | if (ret) { | |
1785 | goto end; | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | end: | |
1790 | return ret; | |
1791 | } | |
1792 | ||
1793 | static | |
1794 | bt_get_greatest_operative_mip_version_status get_greatest_operative_mip_version( | |
1795 | struct bt_config *cfg, uint64_t *mip_version) | |
1796 | { | |
1797 | bt_get_greatest_operative_mip_version_status status = | |
1798 | BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK; | |
1799 | bt_component_descriptor_set *comp_descr_set = NULL; | |
1800 | int ret; | |
1801 | ||
1802 | BT_ASSERT(cfg); | |
1803 | BT_ASSERT(mip_version); | |
1804 | comp_descr_set = bt_component_descriptor_set_create(); | |
1805 | if (!comp_descr_set) { | |
1806 | BT_CLI_LOGE_APPEND_CAUSE( | |
1807 | "Failed to create a component descriptor set object."); | |
1808 | status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR; | |
1809 | goto end; | |
1810 | } | |
1811 | ||
1812 | ret = append_descriptors_from_bt_config_component_array( | |
1813 | comp_descr_set, cfg->cmd_data.run.sources); | |
1814 | if (ret) { | |
1815 | status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR; | |
1816 | goto end; | |
1817 | } | |
1818 | ||
1819 | ret = append_descriptors_from_bt_config_component_array( | |
1820 | comp_descr_set, cfg->cmd_data.run.filters); | |
1821 | if (ret) { | |
1822 | status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR; | |
1823 | goto end; | |
1824 | } | |
1825 | ||
1826 | ret = append_descriptors_from_bt_config_component_array( | |
1827 | comp_descr_set, cfg->cmd_data.run.sinks); | |
1828 | if (ret) { | |
1829 | status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR; | |
1830 | goto end; | |
1831 | } | |
1832 | ||
1833 | if (cfg->cmd_data.run.stream_intersection_mode) { | |
1834 | /* | |
1835 | * Stream intersection mode adds `flt.utils.trimmer` | |
1836 | * components; we need to include this type of component | |
1837 | * in the component descriptor set to get the real | |
1838 | * greatest operative MIP version. | |
1839 | */ | |
1840 | ret = add_descriptor_to_component_descriptor_set( | |
1841 | comp_descr_set, "utils", "trimmer", | |
1842 | BT_COMPONENT_CLASS_TYPE_FILTER, NULL); | |
1843 | if (ret) { | |
1844 | status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR; | |
1845 | goto end; | |
1846 | } | |
1847 | } | |
1848 | ||
1849 | status = bt_get_greatest_operative_mip_version(comp_descr_set, | |
1850 | bt_cli_log_level, mip_version); | |
1851 | ||
1852 | end: | |
1853 | bt_component_descriptor_set_put_ref(comp_descr_set); | |
1854 | return status; | |
1855 | } | |
1856 | ||
9009cc24 PP |
1857 | static |
1858 | int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg) | |
1859 | { | |
1860 | int ret = 0; | |
9275bef4 | 1861 | bt_graph_add_listener_status add_listener_status; |
5fea82b8 PP |
1862 | bt_get_greatest_operative_mip_version_status mip_version_status; |
1863 | uint64_t mip_version = UINT64_C(-1); | |
9009cc24 PP |
1864 | |
1865 | ctx->cfg = cfg; | |
1866 | ctx->connect_ports = false; | |
d94d92ac | 1867 | ctx->src_components = g_hash_table_new_full(g_direct_hash, |
398454ed | 1868 | g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref); |
d94d92ac PP |
1869 | if (!ctx->src_components) { |
1870 | goto error; | |
1871 | } | |
1872 | ||
1873 | ctx->flt_components = g_hash_table_new_full(g_direct_hash, | |
398454ed | 1874 | g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref); |
d94d92ac PP |
1875 | if (!ctx->flt_components) { |
1876 | goto error; | |
1877 | } | |
1878 | ||
1879 | ctx->sink_components = g_hash_table_new_full(g_direct_hash, | |
398454ed | 1880 | g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref); |
d94d92ac | 1881 | if (!ctx->sink_components) { |
9009cc24 PP |
1882 | goto error; |
1883 | } | |
1884 | ||
75a2cb9b JG |
1885 | if (cfg->cmd_data.run.stream_intersection_mode) { |
1886 | ctx->stream_intersection_mode = true; | |
1887 | ctx->intersections = g_hash_table_new_full(port_id_hash, | |
1888 | port_id_equal, port_id_destroy, trace_range_destroy); | |
1889 | if (!ctx->intersections) { | |
1890 | goto error; | |
1891 | } | |
1892 | } | |
1893 | ||
5fea82b8 PP |
1894 | /* |
1895 | * Get the greatest operative MIP version to use to configure | |
1896 | * the graph to create. | |
1897 | */ | |
1898 | mip_version_status = get_greatest_operative_mip_version( | |
1899 | cfg, &mip_version); | |
1900 | if (mip_version_status == BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH) { | |
1901 | BT_CLI_LOGE_APPEND_CAUSE( | |
1902 | "Failed to find an operative message interchange " | |
1903 | "protocol version to use to create the `run` command's " | |
1904 | "graph (components are not interoperable)."); | |
1905 | goto error; | |
1906 | } else if (mip_version_status < 0) { | |
1907 | BT_CLI_LOGE_APPEND_CAUSE( | |
1908 | "Cannot find an operative message interchange " | |
1909 | "protocol version to use to create the `run` command's " | |
1910 | "graph: status=%s", | |
1911 | bt_common_func_status_string(mip_version_status)); | |
1912 | goto error; | |
1913 | } | |
1914 | ||
1915 | BT_ASSERT(mip_version_status == BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK); | |
1916 | BT_LOGI("Found operative message interchange protocol version to " | |
1917 | "configure the `run` command's graph: mip-version=%" PRIu64, | |
1918 | mip_version); | |
1919 | ctx->graph = bt_graph_create(mip_version); | |
9009cc24 PP |
1920 | if (!ctx->graph) { |
1921 | goto error; | |
1922 | } | |
1923 | ||
9b4f9b42 | 1924 | bt_graph_add_interrupter(ctx->graph, the_interrupter); |
9275bef4 | 1925 | add_listener_status = bt_graph_add_source_component_output_port_added_listener( |
d94d92ac PP |
1926 | ctx->graph, graph_source_output_port_added_listener, NULL, ctx, |
1927 | NULL); | |
9275bef4 | 1928 | if (add_listener_status != BT_GRAPH_ADD_LISTENER_STATUS_OK) { |
cee05105 PP |
1929 | BT_CLI_LOGE_APPEND_CAUSE( |
1930 | "Cannot add \"port added\" listener to graph."); | |
9009cc24 PP |
1931 | goto error; |
1932 | } | |
1933 | ||
9275bef4 | 1934 | add_listener_status = bt_graph_add_filter_component_output_port_added_listener( |
d94d92ac PP |
1935 | ctx->graph, graph_filter_output_port_added_listener, NULL, ctx, |
1936 | NULL); | |
9275bef4 | 1937 | if (add_listener_status != BT_GRAPH_ADD_LISTENER_STATUS_OK) { |
cee05105 PP |
1938 | BT_CLI_LOGE_APPEND_CAUSE( |
1939 | "Cannot add \"port added\" listener to graph."); | |
9009cc24 PP |
1940 | goto error; |
1941 | } | |
1942 | ||
1943 | goto end; | |
1944 | ||
1945 | error: | |
1946 | cmd_run_ctx_destroy(ctx); | |
1947 | ret = -1; | |
1948 | ||
1949 | end: | |
1950 | return ret; | |
1951 | } | |
1952 | ||
30947af0 SM |
1953 | /* |
1954 | * Compute the intersection of all streams in the array `streams`, write it | |
1955 | * in `range`. | |
1956 | */ | |
1957 | ||
1958 | static | |
1959 | int compute_stream_intersection(const bt_value *streams, | |
1960 | struct trace_range *range) | |
1961 | { | |
f80e9ec1 | 1962 | uint64_t i, stream_count; |
30947af0 SM |
1963 | int ret; |
1964 | ||
1965 | BT_ASSERT(bt_value_get_type(streams) == BT_VALUE_TYPE_ARRAY); | |
1966 | ||
1967 | stream_count = bt_value_array_get_length(streams); | |
1968 | ||
1969 | BT_ASSERT(stream_count > 0); | |
1970 | ||
1971 | range->intersection_range_begin_ns = 0; | |
1972 | range->intersection_range_end_ns = UINT64_MAX; | |
1973 | ||
1974 | for (i = 0; i < stream_count; i++) { | |
1975 | int64_t begin_ns, end_ns; | |
1976 | uint64_t begin_ns_u, end_ns_u; | |
1977 | const bt_value *stream_value; | |
1978 | const bt_value *range_ns_value; | |
1979 | const bt_value *begin_value; | |
1980 | const bt_value *end_value; | |
1981 | ||
1982 | stream_value = bt_value_array_borrow_element_by_index_const(streams, i); | |
1983 | if (bt_value_get_type(stream_value) != BT_VALUE_TYPE_MAP) { | |
5f2a1585 | 1984 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: " |
30947af0 SM |
1985 | "expected streams array element to be a map, got %s.", |
1986 | bt_common_value_type_string(bt_value_get_type(stream_value))); | |
1987 | goto error; | |
1988 | } | |
1989 | ||
1990 | range_ns_value = bt_value_map_borrow_entry_value_const( | |
1991 | stream_value, "range-ns"); | |
1992 | if (!range_ns_value) { | |
5f2a1585 | 1993 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: " |
30947af0 SM |
1994 | "missing expected `range-ns` key in stream map."); |
1995 | goto error; | |
1996 | } | |
1997 | ||
1998 | if (bt_value_get_type(range_ns_value) != BT_VALUE_TYPE_MAP) { | |
5f2a1585 | 1999 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: " |
30947af0 SM |
2000 | "expected `range-ns` entry value of stream map to be a map, got %s.", |
2001 | bt_common_value_type_string(bt_value_get_type(range_ns_value))); | |
2002 | goto error; | |
2003 | } | |
2004 | ||
2005 | begin_value = bt_value_map_borrow_entry_value_const(range_ns_value, "begin"); | |
2006 | if (!begin_value) { | |
5f2a1585 | 2007 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: " |
30947af0 SM |
2008 | "missing expected `begin` key in range-ns map."); |
2009 | goto error; | |
2010 | } | |
2011 | ||
2012 | if (bt_value_get_type(begin_value) != BT_VALUE_TYPE_SIGNED_INTEGER) { | |
5f2a1585 | 2013 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: " |
30947af0 SM |
2014 | "expected `begin` entry value of range-ns map to be a signed integer, got %s.", |
2015 | bt_common_value_type_string(bt_value_get_type(range_ns_value))); | |
2016 | goto error; | |
2017 | } | |
2018 | ||
2019 | end_value = bt_value_map_borrow_entry_value_const(range_ns_value, "end"); | |
2020 | if (!end_value) { | |
5f2a1585 | 2021 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: " |
30947af0 SM |
2022 | "missing expected `end` key in range-ns map."); |
2023 | goto error; | |
2024 | } | |
2025 | ||
2026 | if (bt_value_get_type(end_value) != BT_VALUE_TYPE_SIGNED_INTEGER) { | |
5f2a1585 | 2027 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: " |
30947af0 SM |
2028 | "expected `end` entry value of range-ns map to be a signed integer, got %s.", |
2029 | bt_common_value_type_string(bt_value_get_type(range_ns_value))); | |
2030 | goto error; | |
2031 | } | |
2032 | ||
2033 | begin_ns = bt_value_integer_signed_get(begin_value); | |
2034 | end_ns = bt_value_integer_signed_get(end_value); | |
2035 | ||
2036 | if (begin_ns < 0 || end_ns < 0 || end_ns < begin_ns) { | |
2037 | BT_CLI_LOGE_APPEND_CAUSE( | |
2038 | "Invalid stream range values: " | |
2039 | "range-ns:begin=%" PRId64 ", " | |
2040 | "range-ns:end=%" PRId64, | |
2041 | begin_ns, end_ns); | |
30947af0 SM |
2042 | goto error; |
2043 | } | |
2044 | ||
2045 | begin_ns_u = begin_ns; | |
2046 | end_ns_u = end_ns; | |
2047 | ||
2048 | range->intersection_range_begin_ns = | |
2049 | MAX(range->intersection_range_begin_ns, begin_ns_u); | |
2050 | range->intersection_range_end_ns = | |
2051 | MIN(range->intersection_range_end_ns, end_ns_u); | |
2052 | } | |
2053 | ||
2054 | ret = 0; | |
2055 | goto end; | |
2056 | error: | |
2057 | ret = -1; | |
2058 | ||
2059 | end: | |
2060 | return ret; | |
2061 | } | |
2062 | ||
75a2cb9b JG |
2063 | static |
2064 | int set_stream_intersections(struct cmd_run_ctx *ctx, | |
2065 | struct bt_config_component *cfg_comp, | |
b19ff26f | 2066 | const bt_component_class_source *src_comp_cls) |
75a2cb9b JG |
2067 | { |
2068 | int ret = 0; | |
2069 | uint64_t trace_idx; | |
f80e9ec1 | 2070 | uint64_t trace_count; |
b19ff26f PP |
2071 | const bt_value *query_result = NULL; |
2072 | const bt_value *trace_info = NULL; | |
b19ff26f PP |
2073 | const bt_value *stream_infos = NULL; |
2074 | const bt_value *stream_info = NULL; | |
75a2cb9b | 2075 | struct port_id *port_id = NULL; |
30947af0 | 2076 | struct trace_range *stream_intersection = NULL; |
c7eee084 | 2077 | const char *fail_reason = NULL; |
b19ff26f | 2078 | const bt_component_class *comp_cls = |
0d72b8c3 | 2079 | bt_component_class_source_as_component_class_const(src_comp_cls); |
75a2cb9b | 2080 | |
5f2a1585 | 2081 | ret = query(ctx->cfg, comp_cls, "babeltrace.trace-infos", |
f280892e | 2082 | cfg_comp->params, &query_result, |
c7eee084 PP |
2083 | &fail_reason); |
2084 | if (ret) { | |
d4fb887f | 2085 | BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: " |
c7eee084 | 2086 | "comp-class-name=\"%s\"", fail_reason, |
75a2cb9b JG |
2087 | bt_component_class_get_name(comp_cls)); |
2088 | ret = -1; | |
8dfb232c | 2089 | goto end; |
75a2cb9b JG |
2090 | } |
2091 | ||
f6ccaed9 PP |
2092 | BT_ASSERT(query_result); |
2093 | ||
75a2cb9b | 2094 | if (!bt_value_is_array(query_result)) { |
5f2a1585 | 2095 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: " |
8dfb232c SM |
2096 | "component-class-name=%s, actual-type=%s", |
2097 | bt_component_class_get_name(comp_cls), | |
2098 | bt_common_value_type_string(bt_value_get_type(query_result))); | |
75a2cb9b | 2099 | ret = -1; |
8dfb232c | 2100 | goto end; |
75a2cb9b JG |
2101 | } |
2102 | ||
393729a6 | 2103 | trace_count = bt_value_array_get_length(query_result); |
f80e9ec1 | 2104 | if (trace_count == 0) { |
5f2a1585 | 2105 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: " |
8dfb232c | 2106 | "component-class-name=%s", bt_component_class_get_name(comp_cls)); |
75a2cb9b | 2107 | ret = -1; |
8dfb232c | 2108 | goto end; |
75a2cb9b JG |
2109 | } |
2110 | ||
2111 | for (trace_idx = 0; trace_idx < trace_count; trace_idx++) { | |
75a2cb9b JG |
2112 | uint64_t stream_idx; |
2113 | int64_t stream_count; | |
30947af0 | 2114 | struct trace_range trace_intersection; |
75a2cb9b | 2115 | |
05e21286 | 2116 | trace_info = bt_value_array_borrow_element_by_index_const( |
07208d85 | 2117 | query_result, trace_idx); |
8dfb232c | 2118 | if (!bt_value_is_map(trace_info)) { |
75a2cb9b | 2119 | ret = -1; |
5f2a1585 | 2120 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: " |
8dfb232c SM |
2121 | "component-class-name=%s, actual-type=%s", |
2122 | bt_component_class_get_name(comp_cls), | |
2123 | bt_common_value_type_string(bt_value_get_type(trace_info))); | |
2124 | goto end; | |
75a2cb9b JG |
2125 | } |
2126 | ||
8dfb232c | 2127 | stream_infos = bt_value_map_borrow_entry_value_const( |
5f2a1585 | 2128 | trace_info, "stream-infos"); |
8dfb232c | 2129 | if (!stream_infos) { |
75a2cb9b | 2130 | ret = -1; |
5f2a1585 | 2131 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: " |
8dfb232c SM |
2132 | "component-class-name=%s", |
2133 | bt_component_class_get_name(comp_cls)); | |
2134 | goto end; | |
2135 | } | |
2136 | ||
2137 | if (!bt_value_is_array(stream_infos)) { | |
2138 | ret = -1; | |
5f2a1585 | 2139 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: " |
8dfb232c SM |
2140 | "component-class-name=%s, actual-type=%s", |
2141 | bt_component_class_get_name(comp_cls), | |
2142 | bt_common_value_type_string(bt_value_get_type(stream_infos))); | |
2143 | goto end; | |
75a2cb9b JG |
2144 | } |
2145 | ||
393729a6 | 2146 | stream_count = bt_value_array_get_length(stream_infos); |
f80e9ec1 | 2147 | if (stream_count == 0) { |
75a2cb9b | 2148 | ret = -1; |
5f2a1585 | 2149 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: " |
8dfb232c SM |
2150 | "component-class-name=%s", |
2151 | bt_component_class_get_name(comp_cls)); | |
2152 | goto end; | |
75a2cb9b JG |
2153 | } |
2154 | ||
30947af0 SM |
2155 | ret = compute_stream_intersection(stream_infos, &trace_intersection); |
2156 | if (ret != 0) { | |
2157 | BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection."); | |
8dfb232c | 2158 | goto end; |
30947af0 SM |
2159 | } |
2160 | ||
75a2cb9b | 2161 | for (stream_idx = 0; stream_idx < stream_count; stream_idx++) { |
a38d7650 | 2162 | const bt_value *port_name; |
75a2cb9b JG |
2163 | |
2164 | port_id = g_new0(struct port_id, 1); | |
2165 | if (!port_id) { | |
2166 | ret = -1; | |
cee05105 PP |
2167 | BT_CLI_LOGE_APPEND_CAUSE( |
2168 | "Cannot allocate memory for port_id structure."); | |
8dfb232c | 2169 | goto end; |
75a2cb9b JG |
2170 | } |
2171 | port_id->instance_name = strdup(cfg_comp->instance_name->str); | |
2172 | if (!port_id->instance_name) { | |
2173 | ret = -1; | |
cee05105 PP |
2174 | BT_CLI_LOGE_APPEND_CAUSE( |
2175 | "Cannot allocate memory for port_id component instance name."); | |
8dfb232c | 2176 | goto end; |
75a2cb9b JG |
2177 | } |
2178 | ||
30947af0 SM |
2179 | stream_intersection = g_new0(struct trace_range, 1); |
2180 | if (!stream_intersection) { | |
75a2cb9b | 2181 | ret = -1; |
cee05105 PP |
2182 | BT_CLI_LOGE_APPEND_CAUSE( |
2183 | "Cannot allocate memory for trace_range structure."); | |
8dfb232c | 2184 | goto end; |
75a2cb9b | 2185 | } |
30947af0 SM |
2186 | |
2187 | *stream_intersection = trace_intersection; | |
75a2cb9b | 2188 | |
05e21286 | 2189 | stream_info = bt_value_array_borrow_element_by_index_const( |
07208d85 | 2190 | stream_infos, stream_idx); |
8dfb232c | 2191 | if (!bt_value_is_map(stream_info)) { |
75a2cb9b | 2192 | ret = -1; |
5f2a1585 | 2193 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: " |
8dfb232c SM |
2194 | "expecting element of stream list to be a map: " |
2195 | "component-class-name=%s, actual-type=%s", | |
2196 | bt_component_class_get_name(comp_cls), | |
2197 | bt_common_value_type_string(bt_value_get_type(stream_info))); | |
2198 | goto end; | |
75a2cb9b JG |
2199 | } |
2200 | ||
a38d7650 | 2201 | port_name = bt_value_map_borrow_entry_value_const(stream_info, "port-name"); |
8dfb232c | 2202 | if (!port_name) { |
75a2cb9b | 2203 | ret = -1; |
5f2a1585 | 2204 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: " |
8dfb232c SM |
2205 | "missing `port-name` key in stream info map: " |
2206 | "component-class-name=%s", | |
2207 | bt_component_class_get_name(comp_cls)); | |
2208 | goto end; | |
2209 | } | |
2210 | ||
2211 | if (!bt_value_is_string(port_name)) { | |
2212 | ret = -1; | |
5f2a1585 | 2213 | BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: " |
8dfb232c SM |
2214 | "expecting `port-name` entry of stream info map to be a string: " |
2215 | "component-class-name=%s, actual-type=%s", | |
2216 | bt_component_class_get_name(comp_cls), | |
2217 | bt_common_value_type_string(bt_value_get_type(port_name))); | |
2218 | goto end; | |
75a2cb9b JG |
2219 | } |
2220 | ||
a38d7650 | 2221 | port_id->port_name = g_strdup(bt_value_string_get(port_name)); |
75a2cb9b JG |
2222 | if (!port_id->port_name) { |
2223 | ret = -1; | |
cee05105 PP |
2224 | BT_CLI_LOGE_APPEND_CAUSE( |
2225 | "Cannot allocate memory for port_id port_name."); | |
8dfb232c | 2226 | goto end; |
75a2cb9b JG |
2227 | } |
2228 | ||
2229 | BT_LOGD("Inserting stream intersection "); | |
2230 | ||
30947af0 | 2231 | g_hash_table_insert(ctx->intersections, port_id, stream_intersection); |
75a2cb9b JG |
2232 | |
2233 | port_id = NULL; | |
30947af0 | 2234 | stream_intersection = NULL; |
75a2cb9b | 2235 | } |
75a2cb9b JG |
2236 | } |
2237 | ||
8dfb232c | 2238 | ret = 0; |
cee05105 | 2239 | |
75a2cb9b | 2240 | end: |
c5b9b441 | 2241 | bt_value_put_ref(query_result); |
75a2cb9b | 2242 | g_free(port_id); |
30947af0 | 2243 | g_free(stream_intersection); |
75a2cb9b JG |
2244 | return ret; |
2245 | } | |
2246 | ||
9009cc24 PP |
2247 | static |
2248 | int cmd_run_ctx_create_components_from_config_components( | |
2249 | struct cmd_run_ctx *ctx, GPtrArray *cfg_components) | |
2250 | { | |
2251 | size_t i; | |
0d72b8c3 PP |
2252 | const void *comp_cls = NULL; |
2253 | const void *comp = NULL; | |
9009cc24 PP |
2254 | int ret = 0; |
2255 | ||
2256 | for (i = 0; i < cfg_components->len; i++) { | |
2257 | struct bt_config_component *cfg_comp = | |
2258 | g_ptr_array_index(cfg_components, i); | |
2259 | GQuark quark; | |
2260 | ||
d94d92ac PP |
2261 | switch (cfg_comp->type) { |
2262 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
2263 | comp_cls = find_source_component_class( | |
2264 | cfg_comp->plugin_name->str, | |
2265 | cfg_comp->comp_cls_name->str); | |
2266 | break; | |
2267 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
2268 | comp_cls = find_filter_component_class( | |
2269 | cfg_comp->plugin_name->str, | |
2270 | cfg_comp->comp_cls_name->str); | |
2271 | break; | |
2272 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
2273 | comp_cls = find_sink_component_class( | |
2274 | cfg_comp->plugin_name->str, | |
2275 | cfg_comp->comp_cls_name->str); | |
2276 | break; | |
2277 | default: | |
2278 | abort(); | |
2279 | } | |
2280 | ||
9009cc24 | 2281 | if (!comp_cls) { |
cee05105 PP |
2282 | BT_CLI_LOGE_APPEND_CAUSE( |
2283 | "Cannot find component class: plugin-name=\"%s\", " | |
7213a328 PP |
2284 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
2285 | cfg_comp->plugin_name->str, | |
2286 | cfg_comp->comp_cls_name->str, | |
2287 | cfg_comp->type); | |
9009cc24 PP |
2288 | goto error; |
2289 | } | |
2290 | ||
ef267d12 | 2291 | BT_ASSERT(cfg_comp->log_level >= BT_LOG_TRACE); |
29da2ffc | 2292 | |
d94d92ac PP |
2293 | switch (cfg_comp->type) { |
2294 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
0d72b8c3 | 2295 | ret = bt_graph_add_source_component(ctx->graph, |
d94d92ac | 2296 | comp_cls, cfg_comp->instance_name->str, |
29da2ffc | 2297 | cfg_comp->params, cfg_comp->log_level, |
d94d92ac PP |
2298 | (void *) &comp); |
2299 | break; | |
2300 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
0d72b8c3 | 2301 | ret = bt_graph_add_filter_component(ctx->graph, |
d94d92ac | 2302 | comp_cls, cfg_comp->instance_name->str, |
29da2ffc | 2303 | cfg_comp->params, cfg_comp->log_level, |
d94d92ac PP |
2304 | (void *) &comp); |
2305 | break; | |
2306 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
0d72b8c3 | 2307 | ret = bt_graph_add_sink_component(ctx->graph, |
d94d92ac | 2308 | comp_cls, cfg_comp->instance_name->str, |
29da2ffc | 2309 | cfg_comp->params, cfg_comp->log_level, |
d94d92ac PP |
2310 | (void *) &comp); |
2311 | break; | |
2312 | default: | |
2313 | abort(); | |
2314 | } | |
2315 | ||
36712f1d | 2316 | if (ret) { |
cee05105 PP |
2317 | BT_CLI_LOGE_APPEND_CAUSE( |
2318 | "Cannot create component: plugin-name=\"%s\", " | |
32e87ceb | 2319 | "comp-cls-name=\"%s\", comp-cls-type=%d, " |
7213a328 PP |
2320 | "comp-name=\"%s\"", |
2321 | cfg_comp->plugin_name->str, | |
2322 | cfg_comp->comp_cls_name->str, | |
2323 | cfg_comp->type, cfg_comp->instance_name->str); | |
9009cc24 PP |
2324 | goto error; |
2325 | } | |
2326 | ||
75a2cb9b JG |
2327 | if (ctx->stream_intersection_mode && |
2328 | cfg_comp->type == BT_COMPONENT_CLASS_TYPE_SOURCE) { | |
2329 | ret = set_stream_intersections(ctx, cfg_comp, comp_cls); | |
2330 | if (ret) { | |
8dfb232c SM |
2331 | BT_CLI_LOGE_APPEND_CAUSE( |
2332 | "Cannot determine stream intersection of trace."); | |
75a2cb9b JG |
2333 | goto error; |
2334 | } | |
2335 | } | |
2336 | ||
7213a328 PP |
2337 | BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"", |
2338 | comp, cfg_comp->instance_name->str); | |
9009cc24 | 2339 | quark = g_quark_from_string(cfg_comp->instance_name->str); |
f6ccaed9 | 2340 | BT_ASSERT(quark > 0); |
d94d92ac PP |
2341 | |
2342 | switch (cfg_comp->type) { | |
2343 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
2344 | g_hash_table_insert(ctx->src_components, | |
0d72b8c3 | 2345 | GUINT_TO_POINTER(quark), (void *) comp); |
d94d92ac PP |
2346 | break; |
2347 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
2348 | g_hash_table_insert(ctx->flt_components, | |
0d72b8c3 | 2349 | GUINT_TO_POINTER(quark), (void *) comp); |
d94d92ac PP |
2350 | break; |
2351 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
2352 | g_hash_table_insert(ctx->sink_components, | |
0d72b8c3 | 2353 | GUINT_TO_POINTER(quark), (void *) comp); |
d94d92ac PP |
2354 | break; |
2355 | default: | |
2356 | abort(); | |
2357 | } | |
2358 | ||
9009cc24 | 2359 | comp = NULL; |
65300d60 | 2360 | BT_OBJECT_PUT_REF_AND_RESET(comp_cls); |
9009cc24 PP |
2361 | } |
2362 | ||
2363 | goto end; | |
2364 | ||
2365 | error: | |
2366 | ret = -1; | |
2367 | ||
2368 | end: | |
65300d60 PP |
2369 | bt_object_put_ref(comp); |
2370 | bt_object_put_ref(comp_cls); | |
9009cc24 PP |
2371 | return ret; |
2372 | } | |
56a1cced | 2373 | |
9009cc24 PP |
2374 | static |
2375 | int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx) | |
2376 | { | |
2377 | int ret = 0; | |
2378 | ||
2379 | /* | |
2380 | * Make sure that, during this phase, our graph's "port added" | |
2381 | * listener does not connect ports while we are creating the | |
2382 | * components because we have a special, initial phase for | |
2383 | * this. | |
2384 | */ | |
2385 | ctx->connect_ports = false; | |
2386 | ||
2387 | ret = cmd_run_ctx_create_components_from_config_components( | |
2388 | ctx, ctx->cfg->cmd_data.run.sources); | |
2389 | if (ret) { | |
7c7c0433 | 2390 | ret = -1; |
2e339de1 JG |
2391 | goto end; |
2392 | } | |
2393 | ||
9009cc24 PP |
2394 | ret = cmd_run_ctx_create_components_from_config_components( |
2395 | ctx, ctx->cfg->cmd_data.run.filters); | |
6c2f3ee5 | 2396 | if (ret) { |
290725f7 | 2397 | ret = -1; |
fec2a9f2 JG |
2398 | goto end; |
2399 | } | |
78586d8a | 2400 | |
9009cc24 PP |
2401 | ret = cmd_run_ctx_create_components_from_config_components( |
2402 | ctx, ctx->cfg->cmd_data.run.sinks); | |
2403 | if (ret) { | |
2404 | ret = -1; | |
2405 | goto end; | |
2406 | } | |
2407 | ||
2408 | end: | |
2409 | return ret; | |
2410 | } | |
2411 | ||
0d72b8c3 | 2412 | typedef uint64_t (*output_port_count_func_t)(const void *); |
b19ff26f | 2413 | typedef const bt_port_output *(*borrow_output_port_by_index_func_t)( |
0d72b8c3 | 2414 | const void *, uint64_t); |
d94d92ac | 2415 | |
9009cc24 PP |
2416 | static |
2417 | int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx, | |
d94d92ac PP |
2418 | void *comp, output_port_count_func_t port_count_fn, |
2419 | borrow_output_port_by_index_func_t port_by_index_fn) | |
9009cc24 PP |
2420 | { |
2421 | int ret = 0; | |
d94d92ac | 2422 | uint64_t count; |
9009cc24 PP |
2423 | uint64_t i; |
2424 | ||
2425 | count = port_count_fn(comp); | |
9009cc24 PP |
2426 | |
2427 | for (i = 0; i < count; i++) { | |
b19ff26f | 2428 | const bt_port_output *upstream_port = port_by_index_fn(comp, i); |
9009cc24 | 2429 | |
f6ccaed9 | 2430 | BT_ASSERT(upstream_port); |
9009cc24 | 2431 | ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port); |
9009cc24 PP |
2432 | if (ret) { |
2433 | goto end; | |
2434 | } | |
2435 | } | |
2436 | ||
2437 | end: | |
2438 | return ret; | |
2439 | } | |
2440 | ||
2441 | static | |
2442 | int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx) | |
2443 | { | |
2444 | int ret = 0; | |
2445 | GHashTableIter iter; | |
2446 | gpointer g_name_quark, g_comp; | |
2447 | ||
2448 | ctx->connect_ports = true; | |
d94d92ac | 2449 | g_hash_table_iter_init(&iter, ctx->src_components); |
9009cc24 PP |
2450 | |
2451 | while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) { | |
d94d92ac PP |
2452 | ret = cmd_run_ctx_connect_comp_ports(ctx, g_comp, |
2453 | (output_port_count_func_t) | |
2454 | bt_component_source_get_output_port_count, | |
2455 | (borrow_output_port_by_index_func_t) | |
0d72b8c3 | 2456 | bt_component_source_borrow_output_port_by_index_const); |
d94d92ac PP |
2457 | if (ret) { |
2458 | goto end; | |
9009cc24 | 2459 | } |
d94d92ac PP |
2460 | } |
2461 | ||
2462 | g_hash_table_iter_init(&iter, ctx->flt_components); | |
9009cc24 | 2463 | |
d94d92ac PP |
2464 | while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) { |
2465 | ret = cmd_run_ctx_connect_comp_ports(ctx, g_comp, | |
2466 | (output_port_count_func_t) | |
2467 | bt_component_filter_get_output_port_count, | |
2468 | (borrow_output_port_by_index_func_t) | |
0d72b8c3 | 2469 | bt_component_filter_borrow_output_port_by_index_const); |
9009cc24 PP |
2470 | if (ret) { |
2471 | goto end; | |
2472 | } | |
2473 | } | |
2474 | ||
2475 | end: | |
2476 | return ret; | |
2477 | } | |
2478 | ||
2479 | static | |
2480 | int cmd_run(struct bt_config *cfg) | |
2481 | { | |
2482 | int ret = 0; | |
2483 | struct cmd_run_ctx ctx = { 0 }; | |
2484 | ||
9009cc24 PP |
2485 | /* Initialize the command's context and the graph object */ |
2486 | if (cmd_run_ctx_init(&ctx, cfg)) { | |
cee05105 PP |
2487 | BT_CLI_LOGE_APPEND_CAUSE( |
2488 | "Cannot initialize the command's context."); | |
9009cc24 PP |
2489 | goto error; |
2490 | } | |
2491 | ||
aede8fc9 | 2492 | if (bt_interrupter_is_set(the_interrupter)) { |
63d355d2 | 2493 | BT_CLI_LOGW_APPEND_CAUSE( |
9b4f9b42 | 2494 | "Interrupted by user before creating components."); |
cc308374 PP |
2495 | goto error; |
2496 | } | |
2497 | ||
2498 | BT_LOGI_STR("Creating components."); | |
2499 | ||
9009cc24 PP |
2500 | /* Create the requested component instances */ |
2501 | if (cmd_run_ctx_create_components(&ctx)) { | |
cee05105 | 2502 | BT_CLI_LOGE_APPEND_CAUSE("Cannot create components."); |
9009cc24 PP |
2503 | goto error; |
2504 | } | |
2505 | ||
aede8fc9 | 2506 | if (bt_interrupter_is_set(the_interrupter)) { |
63d355d2 | 2507 | BT_CLI_LOGW_APPEND_CAUSE( |
9b4f9b42 | 2508 | "Interrupted by user before connecting components."); |
cc308374 PP |
2509 | goto error; |
2510 | } | |
2511 | ||
2512 | BT_LOGI_STR("Connecting components."); | |
2513 | ||
9009cc24 PP |
2514 | /* Connect the initially visible component ports */ |
2515 | if (cmd_run_ctx_connect_ports(&ctx)) { | |
cee05105 PP |
2516 | BT_CLI_LOGE_APPEND_CAUSE( |
2517 | "Cannot connect initial component ports."); | |
9009cc24 PP |
2518 | goto error; |
2519 | } | |
2520 | ||
7213a328 PP |
2521 | BT_LOGI_STR("Running the graph."); |
2522 | ||
9009cc24 | 2523 | /* Run the graph */ |
fec2a9f2 | 2524 | while (true) { |
d24d5663 | 2525 | bt_graph_run_status run_status = bt_graph_run(ctx.graph); |
61ddbc8a | 2526 | |
5669a3e7 PP |
2527 | /* |
2528 | * Reset console in case something messed with console | |
2529 | * codes during the graph's execution. | |
2530 | */ | |
2531 | printf("%s", bt_common_color_reset()); | |
2532 | fflush(stdout); | |
2533 | fprintf(stderr, "%s", bt_common_color_reset()); | |
ef267d12 | 2534 | BT_LOGT("bt_graph_run() returned: status=%s", |
d24d5663 | 2535 | bt_common_func_status_string(run_status)); |
fd948396 | 2536 | |
d24d5663 PP |
2537 | switch (run_status) { |
2538 | case BT_GRAPH_RUN_STATUS_OK: | |
9669d693 | 2539 | goto end; |
d24d5663 | 2540 | case BT_GRAPH_RUN_STATUS_AGAIN: |
9b4f9b42 | 2541 | if (bt_interrupter_is_set(the_interrupter)) { |
63d355d2 | 2542 | BT_CLI_LOGW_APPEND_CAUSE( |
9b4f9b42 | 2543 | "Graph was interrupted by user."); |
5401f780 PP |
2544 | goto error; |
2545 | } | |
2546 | ||
9009cc24 | 2547 | if (cfg->cmd_data.run.retry_duration_us > 0) { |
d24d5663 | 2548 | BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: " |
7213a328 PP |
2549 | "time-us=%" PRIu64, |
2550 | cfg->cmd_data.run.retry_duration_us); | |
2551 | ||
9009cc24 | 2552 | if (usleep(cfg->cmd_data.run.retry_duration_us)) { |
9b4f9b42 | 2553 | if (bt_interrupter_is_set(the_interrupter)) { |
63d355d2 | 2554 | BT_CLI_LOGW_APPEND_CAUSE( |
9b4f9b42 | 2555 | "Graph was interrupted by user."); |
cfa4637b PP |
2556 | goto error; |
2557 | } | |
9009cc24 PP |
2558 | } |
2559 | } | |
78586d8a | 2560 | break; |
fec2a9f2 | 2561 | default: |
9b4f9b42 PP |
2562 | if (bt_interrupter_is_set(the_interrupter)) { |
2563 | BT_CLI_LOGW_APPEND_CAUSE( | |
2564 | "Graph was interrupted by user and failed: " | |
2565 | "status=%s", | |
2566 | bt_common_func_status_string(run_status)); | |
2567 | goto error; | |
2568 | } | |
2569 | ||
cee05105 PP |
2570 | BT_CLI_LOGE_APPEND_CAUSE( |
2571 | "Graph failed to complete successfully"); | |
9009cc24 | 2572 | goto error; |
78586d8a | 2573 | } |
fec2a9f2 | 2574 | } |
290725f7 | 2575 | |
9009cc24 PP |
2576 | goto end; |
2577 | ||
2578 | error: | |
2579 | if (ret == 0) { | |
2580 | ret = -1; | |
2581 | } | |
2582 | ||
11e1d048 | 2583 | end: |
9009cc24 | 2584 | cmd_run_ctx_destroy(&ctx); |
290725f7 PP |
2585 | return ret; |
2586 | } | |
2587 | ||
9009cc24 PP |
2588 | static |
2589 | void warn_command_name_and_directory_clash(struct bt_config *cfg) | |
290725f7 | 2590 | { |
9009cc24 PP |
2591 | const char *env_clash; |
2592 | ||
290725f7 PP |
2593 | if (!cfg->command_name) { |
2594 | return; | |
2595 | } | |
2596 | ||
9009cc24 PP |
2597 | env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH); |
2598 | if (env_clash && strcmp(env_clash, "0") == 0) { | |
2599 | return; | |
2600 | } | |
2601 | ||
290725f7 PP |
2602 | if (g_file_test(cfg->command_name, |
2603 | G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { | |
cee05105 | 2604 | _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__, |
770538dd | 2605 | BT_LOG_WARNING, BT_LOG_TAG, |
cee05105 PP |
2606 | "The `%s` command was executed. " |
2607 | "If you meant to convert a trace located in " | |
2608 | "the local `%s` directory, please use:\n\n" | |
2609 | " babeltrace2 convert %s [OPTIONS]", | |
2610 | cfg->command_name, cfg->command_name, | |
2611 | cfg->command_name); | |
290725f7 PP |
2612 | } |
2613 | } | |
2614 | ||
7213a328 PP |
2615 | static |
2616 | void init_log_level(void) | |
2617 | { | |
c6d4d1ae | 2618 | bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL); |
7213a328 PP |
2619 | } |
2620 | ||
f393c19b PP |
2621 | static |
2622 | void print_error_causes(void) | |
2623 | { | |
2624 | const bt_error *error = bt_current_thread_take_error(); | |
2625 | int64_t i; | |
2626 | GString *folded = NULL; | |
2627 | unsigned int columns; | |
2628 | ||
2629 | if (!error || bt_error_get_cause_count(error) == 0) { | |
2630 | fprintf(stderr, "%s%sUnknown command-line error.%s\n", | |
2631 | bt_common_color_bold(), bt_common_color_fg_red(), | |
2632 | bt_common_color_reset()); | |
2633 | goto end; | |
2634 | } | |
2635 | ||
2636 | /* Try to get terminal width to fold the error cause messages */ | |
2637 | if (bt_common_get_term_size(&columns, NULL) < 0) { | |
2638 | /* Width not found: default to 80 */ | |
2639 | columns = 80; | |
2640 | } | |
2641 | ||
2642 | /* | |
2643 | * This helps visually separate the error causes from the last | |
2644 | * logging statement. | |
2645 | */ | |
2646 | fprintf(stderr, "\n"); | |
2647 | ||
2648 | /* Reverse order: deepest (root) cause printed at the end */ | |
2649 | for (i = bt_error_get_cause_count(error) - 1; i >= 0; i--) { | |
2650 | const bt_error_cause *cause = | |
2651 | bt_error_borrow_cause_by_index(error, (uint64_t) i); | |
2652 | const char *prefix_fmt = | |
2653 | i == bt_error_get_cause_count(error) - 1 ? | |
2654 | "%s%sERROR%s: " : "%s%sCAUSED BY%s "; | |
2655 | ||
2656 | /* Print prefix */ | |
2657 | fprintf(stderr, prefix_fmt, | |
2658 | bt_common_color_bold(), bt_common_color_fg_red(), | |
2659 | bt_common_color_reset()); | |
2660 | ||
2661 | /* Print actor name */ | |
2662 | fprintf(stderr, "["); | |
2663 | switch (bt_error_cause_get_actor_type(cause)) { | |
2664 | case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN: | |
2665 | fprintf(stderr, "%s%s%s", | |
2666 | bt_common_color_bold(), | |
2667 | bt_error_cause_get_module_name(cause), | |
2668 | bt_common_color_reset()); | |
2669 | break; | |
2670 | case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT: | |
2671 | fprintf(stderr, "%s%s%s: ", | |
2672 | bt_common_color_bold(), | |
2673 | bt_error_cause_component_actor_get_component_name(cause), | |
2674 | bt_common_color_reset()); | |
2675 | print_plugin_comp_cls_opt(stderr, | |
2676 | bt_error_cause_component_actor_get_plugin_name(cause), | |
2677 | bt_error_cause_component_actor_get_component_class_name(cause), | |
2678 | bt_error_cause_component_actor_get_component_class_type(cause)); | |
2679 | break; | |
2680 | case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS: | |
2681 | print_plugin_comp_cls_opt(stderr, | |
2682 | bt_error_cause_component_class_actor_get_plugin_name(cause), | |
2683 | bt_error_cause_component_class_actor_get_component_class_name(cause), | |
2684 | bt_error_cause_component_class_actor_get_component_class_type(cause)); | |
2685 | break; | |
2686 | case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR: | |
2687 | fprintf(stderr, "%s%s%s (%s%s%s): ", | |
2688 | bt_common_color_bold(), | |
2689 | bt_error_cause_message_iterator_actor_get_component_name(cause), | |
2690 | bt_common_color_reset(), | |
2691 | bt_common_color_bold(), | |
2692 | bt_error_cause_message_iterator_actor_get_component_output_port_name(cause), | |
2693 | bt_common_color_reset()); | |
2694 | print_plugin_comp_cls_opt(stderr, | |
2695 | bt_error_cause_message_iterator_actor_get_plugin_name(cause), | |
2696 | bt_error_cause_message_iterator_actor_get_component_class_name(cause), | |
2697 | bt_error_cause_message_iterator_actor_get_component_class_type(cause)); | |
2698 | break; | |
2699 | default: | |
2700 | abort(); | |
2701 | } | |
2702 | ||
2703 | /* Print file name and line number */ | |
2704 | fprintf(stderr, "] (%s%s%s%s:%s%" PRIu64 "%s)\n", | |
2705 | bt_common_color_bold(), | |
2706 | bt_common_color_fg_magenta(), | |
2707 | bt_error_cause_get_file_name(cause), | |
2708 | bt_common_color_reset(), | |
2709 | bt_common_color_fg_green(), | |
2710 | bt_error_cause_get_line_number(cause), | |
2711 | bt_common_color_reset()); | |
2712 | ||
2713 | /* Print message */ | |
2714 | folded = bt_common_fold(bt_error_cause_get_message(cause), | |
2715 | columns, 2); | |
2716 | if (!folded) { | |
2717 | BT_LOGE_STR("Could not fold string."); | |
2718 | fprintf(stderr, "%s\n", | |
2719 | bt_error_cause_get_message(cause)); | |
2720 | continue; | |
2721 | } | |
2722 | ||
2723 | fprintf(stderr, "%s\n", folded->str); | |
2724 | g_string_free(folded, TRUE); | |
2725 | folded = NULL; | |
2726 | } | |
2727 | ||
2728 | end: | |
6c2baa60 | 2729 | BT_ASSERT(!folded); |
f393c19b PP |
2730 | |
2731 | if (error) { | |
2732 | bt_error_release(error); | |
2733 | } | |
2734 | } | |
2735 | ||
290725f7 PP |
2736 | int main(int argc, const char **argv) |
2737 | { | |
2738 | int ret; | |
2739 | int retcode; | |
3dae1685 | 2740 | struct bt_config *cfg = NULL; |
290725f7 | 2741 | |
7213a328 | 2742 | init_log_level(); |
65d3198f | 2743 | set_signal_handler(); |
743138a3 | 2744 | init_loaded_plugins(); |
3dae1685 SM |
2745 | |
2746 | BT_ASSERT(!the_interrupter); | |
2747 | the_interrupter = bt_interrupter_create(); | |
2748 | if (!the_interrupter) { | |
2749 | BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object."); | |
2750 | retcode = 1; | |
2751 | goto end; | |
2752 | } | |
2753 | ||
2754 | cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode, | |
2755 | the_interrupter); | |
290725f7 PP |
2756 | |
2757 | if (retcode < 0) { | |
2758 | /* Quit without errors; typically usage/version */ | |
2759 | retcode = 0; | |
7213a328 | 2760 | BT_LOGI_STR("Quitting without errors."); |
290725f7 PP |
2761 | goto end; |
2762 | } | |
2763 | ||
2764 | if (retcode > 0) { | |
cee05105 PP |
2765 | BT_CLI_LOGE_APPEND_CAUSE( |
2766 | "Command-line error: retcode=%d", retcode); | |
290725f7 PP |
2767 | goto end; |
2768 | } | |
2769 | ||
2770 | if (!cfg) { | |
cee05105 | 2771 | BT_CLI_LOGE_APPEND_CAUSE( |
9442c9a2 | 2772 | "Failed to create a valid Babeltrace CLI configuration."); |
db0f160a | 2773 | retcode = 1; |
290725f7 PP |
2774 | goto end; |
2775 | } | |
2776 | ||
290725f7 PP |
2777 | print_cfg(cfg); |
2778 | ||
db0f160a | 2779 | if (cfg->command_needs_plugins) { |
73760435 | 2780 | ret = require_loaded_plugins(cfg->plugin_paths); |
db0f160a | 2781 | if (ret) { |
cee05105 PP |
2782 | BT_CLI_LOGE_APPEND_CAUSE( |
2783 | "Failed to load plugins: ret=%d", ret); | |
db0f160a PP |
2784 | retcode = 1; |
2785 | goto end; | |
2786 | } | |
2787 | } | |
2788 | ||
7213a328 PP |
2789 | BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"", |
2790 | cfg->command, cfg->command_name); | |
2791 | ||
290725f7 | 2792 | switch (cfg->command) { |
db0f160a PP |
2793 | case BT_CONFIG_COMMAND_RUN: |
2794 | ret = cmd_run(cfg); | |
290725f7 PP |
2795 | break; |
2796 | case BT_CONFIG_COMMAND_LIST_PLUGINS: | |
2797 | ret = cmd_list_plugins(cfg); | |
2798 | break; | |
22e22462 PP |
2799 | case BT_CONFIG_COMMAND_HELP: |
2800 | ret = cmd_help(cfg); | |
2801 | break; | |
a67681c1 PP |
2802 | case BT_CONFIG_COMMAND_QUERY: |
2803 | ret = cmd_query(cfg); | |
63ce0e1d | 2804 | break; |
db0f160a PP |
2805 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
2806 | ret = cmd_print_ctf_metadata(cfg); | |
2807 | break; | |
2808 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: | |
2809 | ret = cmd_print_lttng_live_sessions(cfg); | |
2810 | break; | |
290725f7 | 2811 | default: |
0fbb9a9f PP |
2812 | BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command); |
2813 | abort(); | |
290725f7 PP |
2814 | } |
2815 | ||
d26ef3e3 PP |
2816 | BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d", |
2817 | cfg->command, cfg->command_name, ret); | |
290725f7 PP |
2818 | warn_command_name_and_directory_clash(cfg); |
2819 | retcode = ret ? 1 : 0; | |
2820 | ||
2821 | end: | |
65300d60 | 2822 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
743138a3 | 2823 | fini_loaded_plugins(); |
9b4f9b42 | 2824 | bt_interrupter_put_ref(the_interrupter); |
0232a535 | 2825 | |
f393c19b PP |
2826 | if (retcode != 0) { |
2827 | print_error_causes(); | |
2828 | } | |
2829 | ||
0232a535 PP |
2830 | /* |
2831 | * Clear current thread's error in case there is one to avoid a | |
2832 | * memory leak. | |
2833 | */ | |
2834 | bt_current_thread_clear_error(); | |
290725f7 | 2835 | return retcode; |
4c8bfb7e | 2836 | } |