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