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