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