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