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