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