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