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