Standard logging for libctfcopytrace
[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",
33ec5dfa 76 "BABELTRACE_PLUGIN_TEXT_DMESG_SRC_LOG_LEVEL",
fed72692 77 "BABELTRACE_PLUGIN_UTILS_MUXER_FLT_LOG_LEVEL",
b4565e8b 78 "BABELTRACE_PLUGIN_UTILS_TRIMMER_FLT_LOG_LEVEL",
3e4f69d3 79 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
fc11b6a6
PP
80 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
81 NULL,
82};
83
5401f780
PP
84/* Application's processing graph (weak) */
85static struct bt_graph *the_graph;
86static bool canceled = false;
87
33b34c43
PP
88GPtrArray *loaded_plugins;
89
5401f780
PP
90static
91void sigint_handler(int signum)
92{
93 if (signum != SIGINT) {
94 return;
95 }
96
97 if (the_graph) {
98 bt_graph_cancel(the_graph);
99 }
100
101 canceled = true;
102}
103
33b34c43 104static
9009cc24 105void init_static_data(void)
33b34c43 106{
9009cc24 107 loaded_plugins = g_ptr_array_new_with_free_func(bt_put);
33b34c43
PP
108}
109
110static
9009cc24 111void fini_static_data(void)
33b34c43
PP
112{
113 g_ptr_array_free(loaded_plugins, TRUE);
114}
115
116static
117struct bt_plugin *find_plugin(const char *name)
118{
119 int i;
120 struct bt_plugin *plugin = NULL;
121
7213a328
PP
122 assert(name);
123 BT_LOGD("Finding plugin: name=\"%s\"", name);
124
33b34c43
PP
125 for (i = 0; i < loaded_plugins->len; i++) {
126 plugin = g_ptr_array_index(loaded_plugins, i);
127
128 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
129 break;
130 }
131
132 plugin = NULL;
133 }
134
7213a328
PP
135 if (BT_LOG_ON_DEBUG) {
136 if (plugin) {
137 BT_LOGD("Found plugin: plugin-addr=%p", plugin);
138 } else {
139 BT_LOGD("Cannot find plugin.");
140 }
141 }
142
33b34c43
PP
143 return bt_get(plugin);
144}
145
146static
147struct bt_component_class *find_component_class(const char *plugin_name,
148 const char *comp_class_name,
d3e4dcd8 149 enum bt_component_class_type comp_class_type)
33b34c43
PP
150{
151 struct bt_component_class *comp_class = NULL;
7213a328
PP
152 struct bt_plugin *plugin;
153
154 BT_LOGD("Finding component class: plugin-name=\"%s\", "
155 "comp-cls-name=\"%s\", comp-cls-type=%d",
156 plugin_name, comp_class_name, comp_class_type);
157
158 plugin = find_plugin(plugin_name);
33b34c43
PP
159
160 if (!plugin) {
161 goto end;
162 }
163
164 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
165 comp_class_name, comp_class_type);
166 BT_PUT(plugin);
7213a328 167
33b34c43 168end:
7213a328
PP
169 if (BT_LOG_ON_DEBUG) {
170 if (comp_class) {
171 BT_LOGD("Found component class: comp-cls-addr=%p",
172 comp_class);
173 } else {
174 BT_LOGD("Cannot find component class.");
175 }
176 }
177
33b34c43
PP
178 return comp_class;
179}
6c2f3ee5 180
c42c79ea 181static
7213a328 182void print_indent(FILE *fp, size_t indent)
c42c79ea
PP
183{
184 size_t i;
185
186 for (i = 0; i < indent; i++) {
7213a328 187 fprintf(fp, " ");
c42c79ea
PP
188 }
189}
190
87796884
PP
191static
192const char *component_type_str(enum bt_component_class_type type)
193{
194 switch (type) {
195 case BT_COMPONENT_CLASS_TYPE_SOURCE:
196 return "source";
197 case BT_COMPONENT_CLASS_TYPE_SINK:
198 return "sink";
199 case BT_COMPONENT_CLASS_TYPE_FILTER:
200 return "filter";
201 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
202 default:
fd5f8053 203 return "(unknown)";
87796884
PP
204 }
205}
206
9009cc24
PP
207static
208void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,
87796884
PP
209 const char *comp_cls_name, enum bt_component_class_type type)
210{
9009cc24
PP
211 GString *shell_plugin_name = NULL;
212 GString *shell_comp_cls_name = NULL;
87796884 213
9009cc24 214 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
87796884
PP
215 if (!shell_plugin_name) {
216 goto end;
217 }
218
9009cc24 219 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
87796884
PP
220 if (!shell_comp_cls_name) {
221 goto end;
222 }
223
fd5f8053 224 fprintf(fh, "'%s%s%s%s.%s%s%s.%s%s%s'",
87796884
PP
225 bt_common_color_bold(),
226 bt_common_color_fg_cyan(),
227 component_type_str(type),
87796884 228 bt_common_color_fg_default(),
87796884 229 bt_common_color_fg_blue(),
9009cc24 230 shell_plugin_name->str,
87796884
PP
231 bt_common_color_fg_default(),
232 bt_common_color_fg_yellow(),
9009cc24 233 shell_comp_cls_name->str,
87796884
PP
234 bt_common_color_reset());
235
236end:
9009cc24
PP
237 if (shell_plugin_name) {
238 g_string_free(shell_plugin_name, TRUE);
239 }
240
241 if (shell_comp_cls_name) {
242 g_string_free(shell_comp_cls_name, TRUE);
243 }
87796884
PP
244}
245
c42c79ea 246static
7213a328 247void print_value(FILE *, struct bt_value *, size_t);
c42c79ea 248
c1081aa6 249static
7213a328
PP
250void print_value_rec(FILE *, struct bt_value *, size_t);
251
252struct print_map_value_data {
253 size_t indent;
254 FILE *fp;
255};
c1081aa6 256
c42c79ea 257static
c55a9f58 258bt_bool print_map_value(const char *key, struct bt_value *object, void *data)
c42c79ea 259{
7213a328 260 struct print_map_value_data *print_map_value_data = data;
290725f7 261
7213a328
PP
262 print_indent(print_map_value_data->fp, print_map_value_data->indent);
263 fprintf(print_map_value_data->fp, "%s: ", key);
290725f7
PP
264
265 if (bt_value_is_array(object) &&
266 bt_value_array_is_empty(object)) {
7213a328 267 fprintf(print_map_value_data->fp, "[ ]\n");
290725f7
PP
268 return true;
269 }
270
271 if (bt_value_is_map(object) &&
272 bt_value_map_is_empty(object)) {
7213a328 273 fprintf(print_map_value_data->fp, "{ }\n");
290725f7
PP
274 return true;
275 }
c42c79ea 276
290725f7
PP
277 if (bt_value_is_array(object) ||
278 bt_value_is_map(object)) {
7213a328 279 fprintf(print_map_value_data->fp, "\n");
290725f7 280 }
c42c79ea 281
7213a328
PP
282 print_value_rec(print_map_value_data->fp, object,
283 print_map_value_data->indent + 2);
c55a9f58 284 return BT_TRUE;
c42c79ea
PP
285}
286
287static
7213a328 288void print_value_rec(FILE *fp, struct bt_value *value, size_t indent)
c42c79ea 289{
c55a9f58 290 bt_bool bool_val;
c42c79ea
PP
291 int64_t int_val;
292 double dbl_val;
293 const char *str_val;
294 int size;
295 int i;
296
297 if (!value) {
298 return;
299 }
300
c42c79ea
PP
301 switch (bt_value_get_type(value)) {
302 case BT_VALUE_TYPE_NULL:
7213a328 303 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
c1081aa6 304 bt_common_color_reset());
c42c79ea
PP
305 break;
306 case BT_VALUE_TYPE_BOOL:
307 bt_value_bool_get(value, &bool_val);
7213a328 308 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
309 bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
310 bt_common_color_reset());
c42c79ea
PP
311 break;
312 case BT_VALUE_TYPE_INTEGER:
313 bt_value_integer_get(value, &int_val);
7213a328 314 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
c1081aa6
PP
315 bt_common_color_fg_red(), int_val,
316 bt_common_color_reset());
c42c79ea
PP
317 break;
318 case BT_VALUE_TYPE_FLOAT:
319 bt_value_float_get(value, &dbl_val);
7213a328 320 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
c1081aa6
PP
321 bt_common_color_fg_red(), dbl_val,
322 bt_common_color_reset());
c42c79ea
PP
323 break;
324 case BT_VALUE_TYPE_STRING:
325 bt_value_string_get(value, &str_val);
7213a328 326 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
327 bt_common_color_fg_green(), str_val,
328 bt_common_color_reset());
c42c79ea
PP
329 break;
330 case BT_VALUE_TYPE_ARRAY:
331 size = bt_value_array_size(value);
290725f7
PP
332 assert(size >= 0);
333
334 if (size == 0) {
7213a328
PP
335 print_indent(fp, indent);
336 fprintf(fp, "[ ]\n");
290725f7
PP
337 break;
338 }
c42c79ea
PP
339
340 for (i = 0; i < size; i++) {
341 struct bt_value *element =
342 bt_value_array_get(value, i);
343
290725f7 344 assert(element);
7213a328
PP
345 print_indent(fp, indent);
346 fprintf(fp, "- ");
290725f7
PP
347
348 if (bt_value_is_array(element) &&
349 bt_value_array_is_empty(element)) {
7213a328 350 fprintf(fp, "[ ]\n");
290725f7
PP
351 continue;
352 }
353
354 if (bt_value_is_map(element) &&
355 bt_value_map_is_empty(element)) {
7213a328 356 fprintf(fp, "{ }\n");
290725f7
PP
357 continue;
358 }
359
360 if (bt_value_is_array(element) ||
361 bt_value_is_map(element)) {
7213a328 362 fprintf(fp, "\n");
290725f7
PP
363 }
364
7213a328 365 print_value_rec(fp, element, indent + 2);
c42c79ea
PP
366 BT_PUT(element);
367 }
c42c79ea
PP
368 break;
369 case BT_VALUE_TYPE_MAP:
7213a328
PP
370 {
371 struct print_map_value_data data = {
372 .indent = indent,
373 .fp = fp,
374 };
375
c42c79ea 376 if (bt_value_map_is_empty(value)) {
7213a328
PP
377 print_indent(fp, indent);
378 fprintf(fp, "{ }\n");
290725f7 379 break;
c42c79ea
PP
380 }
381
7213a328 382 bt_value_map_foreach(value, print_map_value, &data);
c42c79ea 383 break;
7213a328 384 }
c42c79ea 385 default:
0fbb9a9f 386 abort();
c42c79ea
PP
387 }
388}
389
c1081aa6 390static
7213a328 391void print_value(FILE *fp, struct bt_value *value, size_t indent)
c1081aa6
PP
392{
393 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
7213a328 394 print_indent(fp, indent);
c1081aa6
PP
395 }
396
7213a328 397 print_value_rec(fp, value, indent);
c1081aa6
PP
398}
399
c42c79ea
PP
400static
401void print_bt_config_component(struct bt_config_component *bt_config_component)
402{
7213a328
PP
403 fprintf(stderr, " ");
404 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
db0f160a 405 bt_config_component->comp_cls_name->str,
87796884 406 bt_config_component->type);
7213a328 407 fprintf(stderr, ":\n");
3b6cfcc5
PP
408
409 if (bt_config_component->instance_name->len > 0) {
7213a328 410 fprintf(stderr, " Name: %s\n",
3b6cfcc5
PP
411 bt_config_component->instance_name->str);
412 }
413
7213a328
PP
414 fprintf(stderr, " Parameters:\n");
415 print_value(stderr, bt_config_component->params, 8);
c42c79ea
PP
416}
417
418static
419void print_bt_config_components(GPtrArray *array)
420{
421 size_t i;
422
423 for (i = 0; i < array->len; i++) {
424 struct bt_config_component *cfg_component =
e5bc7f81 425 bt_config_get_component(array, i);
c42c79ea
PP
426 print_bt_config_component(cfg_component);
427 BT_PUT(cfg_component);
428 }
429}
430
290725f7
PP
431static
432void print_plugin_paths(struct bt_value *plugin_paths)
433{
7213a328
PP
434 fprintf(stderr, " Plugin paths:\n");
435 print_value(stderr, plugin_paths, 4);
290725f7
PP
436}
437
438static
db0f160a 439void print_cfg_run(struct bt_config *cfg)
290725f7 440{
ebba3338
PP
441 size_t i;
442
db0f160a 443 print_plugin_paths(cfg->plugin_paths);
7213a328 444 fprintf(stderr, " Source component instances:\n");
db0f160a 445 print_bt_config_components(cfg->cmd_data.run.sources);
ebba3338 446
db0f160a 447 if (cfg->cmd_data.run.filters->len > 0) {
7213a328 448 fprintf(stderr, " Filter component instances:\n");
db0f160a 449 print_bt_config_components(cfg->cmd_data.run.filters);
ebba3338
PP
450 }
451
7213a328 452 fprintf(stderr, " Sink component instances:\n");
db0f160a 453 print_bt_config_components(cfg->cmd_data.run.sinks);
7213a328 454 fprintf(stderr, " Connections:\n");
ebba3338 455
db0f160a 456 for (i = 0; i < cfg->cmd_data.run.connections->len; i++) {
ebba3338 457 struct bt_config_connection *cfg_connection =
db0f160a 458 g_ptr_array_index(cfg->cmd_data.run.connections,
ebba3338
PP
459 i);
460
7213a328 461 fprintf(stderr, " %s%s%s -> %s%s%s\n",
9009cc24
PP
462 cfg_connection->upstream_comp_name->str,
463 cfg_connection->upstream_port_glob->len > 0 ? "." : "",
464 cfg_connection->upstream_port_glob->str,
465 cfg_connection->downstream_comp_name->str,
466 cfg_connection->downstream_port_glob->len > 0 ? "." : "",
467 cfg_connection->downstream_port_glob->str);
ebba3338 468 }
290725f7
PP
469}
470
471static
472void print_cfg_list_plugins(struct bt_config *cfg)
473{
db0f160a 474 print_plugin_paths(cfg->plugin_paths);
290725f7
PP
475}
476
c1081aa6
PP
477static
478void print_cfg_help(struct bt_config *cfg)
479{
db0f160a
PP
480 print_plugin_paths(cfg->plugin_paths);
481}
482
483static
484void print_cfg_print_ctf_metadata(struct bt_config *cfg)
485{
486 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
487 fprintf(stderr, " Path: %s\n",
488 cfg->cmd_data.print_ctf_metadata.path->str);
db0f160a
PP
489}
490
491static
492void print_cfg_print_lttng_live_sessions(struct bt_config *cfg)
493{
494 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
495 fprintf(stderr, " URL: %s\n",
496 cfg->cmd_data.print_lttng_live_sessions.url->str);
c1081aa6
PP
497}
498
499static
a67681c1 500void print_cfg_query(struct bt_config *cfg)
c1081aa6 501{
db0f160a 502 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
503 fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str);
504 fprintf(stderr, " Component class:\n");
a67681c1 505 print_bt_config_component(cfg->cmd_data.query.cfg_component);
c1081aa6
PP
506}
507
c42c79ea
PP
508static
509void print_cfg(struct bt_config *cfg)
510{
7213a328 511 if (!BT_LOG_ON_INFO) {
00447e45
PP
512 return;
513 }
514
7213a328
PP
515 BT_LOGI_STR("Configuration:");
516 fprintf(stderr, " Debug mode: %s\n", cfg->debug ? "yes" : "no");
517 fprintf(stderr, " Verbose mode: %s\n", cfg->verbose ? "yes" : "no");
290725f7
PP
518
519 switch (cfg->command) {
db0f160a
PP
520 case BT_CONFIG_COMMAND_RUN:
521 print_cfg_run(cfg);
290725f7
PP
522 break;
523 case BT_CONFIG_COMMAND_LIST_PLUGINS:
524 print_cfg_list_plugins(cfg);
c1081aa6
PP
525 break;
526 case BT_CONFIG_COMMAND_HELP:
527 print_cfg_help(cfg);
528 break;
a67681c1
PP
529 case BT_CONFIG_COMMAND_QUERY:
530 print_cfg_query(cfg);
290725f7 531 break;
db0f160a
PP
532 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
533 print_cfg_print_ctf_metadata(cfg);
534 break;
535 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
536 print_cfg_print_lttng_live_sessions(cfg);
537 break;
290725f7 538 default:
0fbb9a9f 539 abort();
290725f7 540 }
c42c79ea
PP
541}
542
33b34c43 543static
a8ff38ef 544void add_to_loaded_plugins(struct bt_plugin_set *plugin_set)
98ecef32 545{
544d0515
PP
546 int64_t i;
547 int64_t count;
a8ff38ef
PP
548
549 count = bt_plugin_set_get_plugin_count(plugin_set);
550 assert(count >= 0);
551
552 for (i = 0; i < count; i++) {
553 struct bt_plugin *plugin =
554 bt_plugin_set_get_plugin(plugin_set, i);
33b34c43
PP
555 struct bt_plugin *loaded_plugin =
556 find_plugin(bt_plugin_get_name(plugin));
557
a8ff38ef
PP
558 assert(plugin);
559
33b34c43 560 if (loaded_plugin) {
7213a328
PP
561 BT_LOGI("Not using plugin: another one already exists with the same name: "
562 "plugin-name=\"%s\", plugin-path=\"%s\", "
563 "existing-plugin-path=\"%s\"",
564 bt_plugin_get_name(plugin),
565 bt_plugin_get_path(plugin),
566 bt_plugin_get_path(loaded_plugin));
a8ff38ef 567 bt_put(loaded_plugin);
33b34c43 568 } else {
a8ff38ef 569 /* Add to global array. */
7213a328
PP
570 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
571 bt_plugin_get_name(plugin));
a8ff38ef 572 g_ptr_array_add(loaded_plugins, bt_get(plugin));
33b34c43 573 }
a8ff38ef
PP
574
575 bt_put(plugin);
33b34c43
PP
576 }
577}
578
579static
290725f7 580int load_dynamic_plugins(struct bt_value *plugin_paths)
33b34c43
PP
581{
582 int nr_paths, i, ret = 0;
98ecef32 583
290725f7 584 nr_paths = bt_value_array_size(plugin_paths);
98ecef32 585 if (nr_paths < 0) {
7213a328 586 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
33b34c43
PP
587 ret = -1;
588 goto end;
98ecef32 589 }
33b34c43 590
7213a328
PP
591 BT_LOGI("Loading dynamic plugins.");
592
98ecef32
MD
593 for (i = 0; i < nr_paths; i++) {
594 struct bt_value *plugin_path_value = NULL;
595 const char *plugin_path;
a8ff38ef 596 struct bt_plugin_set *plugin_set;
98ecef32 597
290725f7 598 plugin_path_value = bt_value_array_get(plugin_paths, i);
7213a328
PP
599 bt_value_string_get(plugin_path_value, &plugin_path);
600 assert(plugin_path);
50ad9320
PP
601
602 /*
603 * Skip this if the directory does not exist because
604 * bt_plugin_create_all_from_dir() expects an existing
605 * directory.
606 */
607 if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) {
608 BT_LOGV("Skipping nonexistent directory path: "
609 "path=\"%s\"", plugin_path);
610 BT_PUT(plugin_path_value);
611 continue;
612 }
613
a8ff38ef
PP
614 plugin_set = bt_plugin_create_all_from_dir(plugin_path, false);
615 if (!plugin_set) {
7213a328 616 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
98ecef32 617 plugin_path);
33b34c43
PP
618 BT_PUT(plugin_path_value);
619 continue;
98ecef32 620 }
33b34c43 621
a8ff38ef
PP
622 add_to_loaded_plugins(plugin_set);
623 bt_put(plugin_set);
98ecef32
MD
624 BT_PUT(plugin_path_value);
625 }
33b34c43
PP
626end:
627 return ret;
628}
629
630static
631int load_static_plugins(void)
632{
633 int ret = 0;
a8ff38ef 634 struct bt_plugin_set *plugin_set;
33b34c43 635
7213a328 636 BT_LOGI("Loading static plugins.");
a8ff38ef
PP
637 plugin_set = bt_plugin_create_all_from_static();
638 if (!plugin_set) {
7213a328 639 BT_LOGE("Unable to load static plugins.");
33b34c43
PP
640 ret = -1;
641 goto end;
642 }
643
a8ff38ef
PP
644 add_to_loaded_plugins(plugin_set);
645 bt_put(plugin_set);
33b34c43
PP
646end:
647 return ret;
98ecef32
MD
648}
649
9009cc24
PP
650static
651int load_all_plugins(struct bt_value *plugin_paths)
290725f7
PP
652{
653 int ret = 0;
33b34c43 654
290725f7 655 if (load_dynamic_plugins(plugin_paths)) {
290725f7 656 ret = -1;
c1870f57
JG
657 goto end;
658 }
659
290725f7 660 if (load_static_plugins()) {
290725f7 661 ret = -1;
c1870f57
JG
662 goto end;
663 }
664
7213a328
PP
665 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len);
666
290725f7
PP
667end:
668 return ret;
669}
670
9009cc24
PP
671static
672void print_plugin_info(struct bt_plugin *plugin)
22e22462
PP
673{
674 unsigned int major, minor, patch;
675 const char *extra;
676 enum bt_plugin_status version_status;
677 const char *plugin_name;
678 const char *path;
679 const char *author;
680 const char *license;
681 const char *plugin_description;
682
683 plugin_name = bt_plugin_get_name(plugin);
684 path = bt_plugin_get_path(plugin);
685 author = bt_plugin_get_author(plugin);
686 license = bt_plugin_get_license(plugin);
687 plugin_description = bt_plugin_get_description(plugin);
688 version_status = bt_plugin_get_version(plugin, &major, &minor,
689 &patch, &extra);
690 printf("%s%s%s%s:\n", bt_common_color_bold(),
691 bt_common_color_fg_blue(), plugin_name,
692 bt_common_color_reset());
693 printf(" %sPath%s: %s\n", bt_common_color_bold(),
694 bt_common_color_reset(), path ? path : "(None)");
695
696 if (version_status == BT_PLUGIN_STATUS_OK) {
697 printf(" %sVersion%s: %u.%u.%u",
698 bt_common_color_bold(), bt_common_color_reset(),
699 major, minor, patch);
700
701 if (extra) {
702 printf("%s", extra);
703 }
704
705 printf("\n");
706 }
707
708 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
709 bt_common_color_reset(),
710 plugin_description ? plugin_description : "(None)");
711 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
712 bt_common_color_reset(), author ? author : "(Unknown)");
713 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
714 bt_common_color_reset(),
715 license ? license : "(Unknown)");
716}
717
9009cc24
PP
718static
719int cmd_query(struct bt_config *cfg)
63ce0e1d 720{
db95fa29 721 int ret = 0;
63ce0e1d
PP
722 struct bt_component_class *comp_cls = NULL;
723 struct bt_value *results = NULL;
724
a67681c1 725 comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 726 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 727 cfg->cmd_data.query.cfg_component->type);
63ce0e1d 728 if (!comp_cls) {
7213a328
PP
729 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
730 "comp-cls-name=\"%s\", comp-cls-type=%d",
731 cfg->cmd_data.query.cfg_component->plugin_name->str,
732 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
733 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
734 fprintf(stderr, "%s%sCannot find component class %s",
735 bt_common_color_bold(),
736 bt_common_color_fg_red(),
737 bt_common_color_reset());
738 print_plugin_comp_cls_opt(stderr,
a67681c1 739 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 740 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 741 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
742 fprintf(stderr, "\n");
743 ret = -1;
744 goto end;
745 }
746
a67681c1
PP
747 results = bt_component_class_query(comp_cls,
748 cfg->cmd_data.query.object->str,
749 cfg->cmd_data.query.cfg_component->params);
63ce0e1d 750 if (!results) {
7213a328
PP
751 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
752 "comp-cls-name=\"%s\", comp-cls-type=%d "
753 "object=\"%s\"",
754 cfg->cmd_data.query.cfg_component->plugin_name->str,
755 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
756 cfg->cmd_data.query.cfg_component->type,
757 cfg->cmd_data.query.object->str);
63ce0e1d
PP
758 fprintf(stderr, "%s%sFailed to query info to %s",
759 bt_common_color_bold(),
760 bt_common_color_fg_red(),
761 bt_common_color_reset());
762 print_plugin_comp_cls_opt(stderr,
a67681c1 763 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 764 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1
PP
765 cfg->cmd_data.query.cfg_component->type);
766 fprintf(stderr, "%s%s with object `%s`%s\n",
63ce0e1d
PP
767 bt_common_color_bold(),
768 bt_common_color_fg_red(),
a67681c1 769 cfg->cmd_data.query.object->str,
63ce0e1d
PP
770 bt_common_color_reset());
771 ret = -1;
772 goto end;
773 }
774
7213a328 775 print_value(stdout, results, 0);
63ce0e1d
PP
776
777end:
778 bt_put(comp_cls);
779 bt_put(results);
780 return ret;
781}
782
9009cc24
PP
783static
784int cmd_help(struct bt_config *cfg)
22e22462 785{
db95fa29 786 int ret = 0;
22e22462
PP
787 struct bt_plugin *plugin = NULL;
788 size_t i;
789
90de159b 790 plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462 791 if (!plugin) {
7213a328
PP
792 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
793 cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462
PP
794 fprintf(stderr, "%s%sCannot find plugin %s%s%s\n",
795 bt_common_color_bold(), bt_common_color_fg_red(),
796 bt_common_color_fg_blue(),
90de159b 797 cfg->cmd_data.help.cfg_component->plugin_name->str,
22e22462
PP
798 bt_common_color_reset());
799 ret = -1;
800 goto end;
801 }
802
803 print_plugin_info(plugin);
804 printf(" %sComponent classes%s: %d\n",
805 bt_common_color_bold(),
806 bt_common_color_reset(),
544d0515 807 (int) bt_plugin_get_component_class_count(plugin));
22e22462
PP
808
809
90de159b 810 if (cfg->cmd_data.help.cfg_component->type !=
22e22462
PP
811 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
812 struct bt_component_class *needed_comp_cls =
813 find_component_class(
90de159b 814 cfg->cmd_data.help.cfg_component->plugin_name->str,
db0f160a 815 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
90de159b 816 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
817
818 if (!needed_comp_cls) {
7213a328
PP
819 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
820 "comp-cls-name=\"%s\", comp-cls-type=%d",
821 cfg->cmd_data.help.cfg_component->plugin_name->str,
822 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
823 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
824 fprintf(stderr, "\n%s%sCannot find component class %s",
825 bt_common_color_bold(),
826 bt_common_color_fg_red(),
827 bt_common_color_reset());
828 print_plugin_comp_cls_opt(stderr,
90de159b 829 cfg->cmd_data.help.cfg_component->plugin_name->str,
db0f160a 830 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
90de159b 831 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
832 fprintf(stderr, "\n");
833 ret = -1;
834 goto end;
835 }
836
837 bt_put(needed_comp_cls);
838 }
839
840 for (i = 0; i < bt_plugin_get_component_class_count(plugin); i++) {
841 struct bt_component_class *comp_cls =
9ac68eb1 842 bt_plugin_get_component_class_by_index(plugin, i);
22e22462
PP
843 const char *comp_class_name =
844 bt_component_class_get_name(comp_cls);
845 const char *comp_class_description =
846 bt_component_class_get_description(comp_cls);
847 const char *comp_class_help =
848 bt_component_class_get_help(comp_cls);
849 enum bt_component_class_type type =
850 bt_component_class_get_type(comp_cls);
851
852 assert(comp_cls);
853
90de159b 854 if (cfg->cmd_data.help.cfg_component->type !=
22e22462 855 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
db0f160a 856 if (strcmp(cfg->cmd_data.help.cfg_component->comp_cls_name->str,
22e22462
PP
857 comp_class_name) != 0 &&
858 type ==
90de159b 859 cfg->cmd_data.help.cfg_component->type) {
22e22462
PP
860 bt_put(comp_cls);
861 continue;
862 }
863 }
864
865 printf("\n");
866 print_plugin_comp_cls_opt(stdout,
90de159b 867 cfg->cmd_data.help.cfg_component->plugin_name->str,
22e22462
PP
868 comp_class_name,
869 type);
870 printf("\n");
871 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
872 bt_common_color_reset(),
873 comp_class_description ? comp_class_description : "(None)");
874
875 if (comp_class_help) {
876 printf("\n%s\n", comp_class_help);
877 }
878
879 bt_put(comp_cls);
880 }
881
882end:
883 bt_put(plugin);
884 return ret;
885}
886
9009cc24
PP
887static
888int cmd_list_plugins(struct bt_config *cfg)
290725f7 889{
7213a328 890 int ret = 0;
290725f7
PP
891 int plugins_count, component_classes_count = 0, i;
892
22e22462 893 printf("From the following plugin paths:\n\n");
7213a328 894 print_value(stdout, cfg->plugin_paths, 2);
22e22462 895 printf("\n");
290725f7
PP
896 plugins_count = loaded_plugins->len;
897 if (plugins_count == 0) {
7213a328 898 printf("No plugins found.\n");
56a1cced
JG
899 goto end;
900 }
901
290725f7
PP
902 for (i = 0; i < plugins_count; i++) {
903 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
904
905 component_classes_count += bt_plugin_get_component_class_count(plugin);
906 }
33bceaf8 907
290725f7
PP
908 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
909 bt_common_color_bold(),
910 component_classes_count,
911 bt_common_color_reset(),
912 bt_common_color_bold(),
913 plugins_count,
914 bt_common_color_reset());
915
916 for (i = 0; i < plugins_count; i++) {
917 int j;
918 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
290725f7
PP
919
920 component_classes_count =
921 bt_plugin_get_component_class_count(plugin);
22e22462
PP
922 printf("\n");
923 print_plugin_info(plugin);
290725f7
PP
924
925 if (component_classes_count == 0) {
9009cc24 926 printf(" %sComponent classes%s: (none)\n",
290725f7
PP
927 bt_common_color_bold(),
928 bt_common_color_reset());
929 } else {
930 printf(" %sComponent classes%s:\n",
931 bt_common_color_bold(),
932 bt_common_color_reset());
933 }
934
935 for (j = 0; j < component_classes_count; j++) {
936 struct bt_component_class *comp_class =
9ac68eb1
PP
937 bt_plugin_get_component_class_by_index(
938 plugin, j);
290725f7
PP
939 const char *comp_class_name =
940 bt_component_class_get_name(comp_class);
941 const char *comp_class_description =
942 bt_component_class_get_description(comp_class);
943 enum bt_component_class_type type =
944 bt_component_class_get_type(comp_class);
945
22e22462
PP
946 printf(" ");
947 print_plugin_comp_cls_opt(stdout,
948 bt_plugin_get_name(plugin), comp_class_name,
949 type);
290725f7
PP
950
951 if (comp_class_description) {
952 printf(": %s", comp_class_description);
953 }
954
955 printf("\n");
956 bt_put(comp_class);
957 }
958 }
959
960end:
961 return ret;
962}
963
9009cc24
PP
964static
965int cmd_print_lttng_live_sessions(struct bt_config *cfg)
db0f160a 966{
96e8c7e1
MD
967 int ret = 0;
968 struct bt_component_class *comp_cls = NULL;
969 struct bt_value *results = NULL;
970 struct bt_value *params = NULL;
971 struct bt_value *map = NULL;
972 struct bt_value *v = NULL;
973 static const char * const plugin_name = "ctf";
974 static const char * const comp_cls_name = "lttng-live";
975 static const enum bt_component_class_type comp_cls_type =
976 BT_COMPONENT_CLASS_TYPE_SOURCE;
977 int64_t array_size, i;
978
979 assert(cfg->cmd_data.print_lttng_live_sessions.url);
980 comp_cls = find_component_class(plugin_name, comp_cls_name,
981 comp_cls_type);
982 if (!comp_cls) {
983 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
984 "comp-cls-name=\"%s\", comp-cls-type=%d",
985 plugin_name, comp_cls_name,
986 BT_COMPONENT_CLASS_TYPE_SOURCE);
987 fprintf(stderr, "%s%sCannot find component class %s",
988 bt_common_color_bold(),
989 bt_common_color_fg_red(),
990 bt_common_color_reset());
991 print_plugin_comp_cls_opt(stderr, plugin_name,
992 comp_cls_name, comp_cls_type);
993 fprintf(stderr, "\n");
994 goto error;
995 }
996
997 params = bt_value_map_create();
998 if (!params) {
999 goto error;
1000 }
1001
1002 ret = bt_value_map_insert_string(params, "url",
1003 cfg->cmd_data.print_lttng_live_sessions.url->str);
1004 if (ret) {
1005 goto error;
1006 }
1007
1008 results = bt_component_class_query(comp_cls, "sessions",
1009 params);
1010 if (!results) {
1011 BT_LOGE_STR("Failed to query for sessions.");
1012 fprintf(stderr, "%s%sFailed to request sessions%s\n",
1013 bt_common_color_bold(),
1014 bt_common_color_fg_red(),
1015 bt_common_color_reset());
1016 goto error;
1017 }
1018
1019 if (!bt_value_is_array(results)) {
1020 BT_LOGE_STR("Expecting an array for sessions query.");
1021 fprintf(stderr, "%s%sUnexpected type returned by session query%s\n",
1022 bt_common_color_bold(),
1023 bt_common_color_fg_red(),
1024 bt_common_color_reset());
1025 goto error;
1026 }
1027
1028 array_size = bt_value_array_size(results);
1029 for (i = 0; i < array_size; i++) {
1030 const char *url_text;
1031 int64_t timer_us, streams, clients;
1032
1033 map = bt_value_array_get(results, i);
1034 if (!map) {
1035 BT_LOGE_STR("Unexpected empty array entry.");
1036 goto error;
1037 }
1038 if (!bt_value_is_map(map)) {
1039 BT_LOGE_STR("Unexpected entry type.");
1040 goto error;
1041 }
1042
1043 v = bt_value_map_get(map, "url");
1044 if (!v) {
1045 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1046 goto error;
1047 }
1048 ret = bt_value_string_get(v, &url_text);
1049 assert(ret == 0);
1050 printf("%s", url_text);
1051 BT_PUT(v);
1052
1053 v = bt_value_map_get(map, "timer-us");
1054 if (!v) {
1055 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1056 goto error;
1057 }
1058 ret = bt_value_integer_get(v, &timer_us);
1059 assert(ret == 0);
1060 printf(" (timer = %" PRIu64 ", ", timer_us);
1061 BT_PUT(v);
1062
1063 v = bt_value_map_get(map, "stream-count");
1064 if (!v) {
1065 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1066 goto error;
1067 }
1068 ret = bt_value_integer_get(v, &streams);
1069 assert(ret == 0);
1070 printf("%" PRIu64 " stream(s), ", streams);
1071 BT_PUT(v);
1072
1073 v = bt_value_map_get(map, "client-count");
1074 if (!v) {
1075 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1076 goto error;
1077 }
1078 ret = bt_value_integer_get(v, &clients);
1079 assert(ret == 0);
1080 printf("%" PRIu64 " client(s) connected)\n", clients);
1081 BT_PUT(v);
1082
1083 BT_PUT(map);
1084 }
1085end:
1086 bt_put(v);
1087 bt_put(map);
1088 bt_put(results);
1089 bt_put(params);
1090 bt_put(comp_cls);
1091 return 0;
1092
1093error:
1094 ret = -1;
1095 goto end;
db0f160a
PP
1096}
1097
9009cc24
PP
1098static
1099int cmd_print_ctf_metadata(struct bt_config *cfg)
05a67631
PP
1100{
1101 int ret = 0;
1102 struct bt_component_class *comp_cls = NULL;
05a67631 1103 struct bt_value *results = NULL;
05a67631
PP
1104 struct bt_value *params = NULL;
1105 struct bt_value *metadata_text_value = NULL;
1106 const char *metadata_text = NULL;
db0f160a
PP
1107 static const char * const plugin_name = "ctf";
1108 static const char * const comp_cls_name = "fs";
1109 static const enum bt_component_class_type comp_cls_type =
1110 BT_COMPONENT_CLASS_TYPE_SOURCE;
1111
1112 assert(cfg->cmd_data.print_ctf_metadata.path);
1113 comp_cls = find_component_class(plugin_name, comp_cls_name,
1114 comp_cls_type);
05a67631 1115 if (!comp_cls) {
7213a328
PP
1116 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1117 "comp-cls-name=\"%s\", comp-cls-type=%d",
1118 plugin_name, comp_cls_name,
1119 BT_COMPONENT_CLASS_TYPE_SOURCE);
05a67631
PP
1120 fprintf(stderr, "%s%sCannot find component class %s",
1121 bt_common_color_bold(),
1122 bt_common_color_fg_red(),
1123 bt_common_color_reset());
db0f160a
PP
1124 print_plugin_comp_cls_opt(stderr, plugin_name,
1125 comp_cls_name, comp_cls_type);
05a67631
PP
1126 fprintf(stderr, "\n");
1127 ret = -1;
1128 goto end;
1129 }
1130
05a67631
PP
1131 params = bt_value_map_create();
1132 if (!params) {
1133 ret = -1;
1134 goto end;
1135 }
1136
db0f160a
PP
1137 ret = bt_value_map_insert_string(params, "path",
1138 cfg->cmd_data.print_ctf_metadata.path->str);
05a67631
PP
1139 if (ret) {
1140 ret = -1;
1141 goto end;
1142 }
1143
a67681c1 1144 results = bt_component_class_query(comp_cls, "metadata-info",
05a67631
PP
1145 params);
1146 if (!results) {
1147 ret = -1;
7213a328 1148 BT_LOGE_STR("Failed to query for metadata info.");
a67681c1 1149 fprintf(stderr, "%s%sFailed to request metadata info%s\n",
05a67631
PP
1150 bt_common_color_bold(),
1151 bt_common_color_fg_red(),
1152 bt_common_color_reset());
1153 goto end;
1154 }
1155
1156 metadata_text_value = bt_value_map_get(results, "text");
1157 if (!metadata_text_value) {
7213a328 1158 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
05a67631
PP
1159 ret = -1;
1160 goto end;
1161 }
1162
1163 ret = bt_value_string_get(metadata_text_value, &metadata_text);
1164 assert(ret == 0);
1165 printf("%s\n", metadata_text);
1166
1167end:
1168 bt_put(results);
05a67631
PP
1169 bt_put(params);
1170 bt_put(metadata_text_value);
1171 bt_put(comp_cls);
05a67631
PP
1172 return 0;
1173}
1174
75a2cb9b
JG
1175struct port_id {
1176 char *instance_name;
1177 char *port_name;
1178};
1179
1180struct trace_range {
1181 uint64_t intersection_range_begin_ns;
1182 uint64_t intersection_range_end_ns;
1183};
1184
1185static
1186guint port_id_hash(gconstpointer v)
1187{
1188 const struct port_id *id = v;
1189
1190 assert(id->instance_name);
1191 assert(id->port_name);
1192
1193 return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name);
1194}
1195
1196static
1197gboolean port_id_equal(gconstpointer v1, gconstpointer v2)
1198{
1199 const struct port_id *id1 = v1;
1200 const struct port_id *id2 = v2;
1201
1202 return !strcmp(id1->instance_name, id2->instance_name) &&
1203 !strcmp(id1->port_name, id2->port_name);
1204}
1205
1206static
1207void port_id_destroy(gpointer data)
1208{
1209 struct port_id *id = data;
1210
1211 free(id->instance_name);
1212 free(id->port_name);
1213 free(id);
1214}
1215
1216static
1217void trace_range_destroy(gpointer data)
1218{
1219 free(data);
1220}
1221
9009cc24
PP
1222struct cmd_run_ctx {
1223 /* Owned by this */
1224 GHashTable *components;
1225
1226 /* Owned by this */
1227 struct bt_graph *graph;
1228
1229 /* Weak */
1230 struct bt_config *cfg;
1231
1232 bool connect_ports;
75a2cb9b
JG
1233
1234 bool stream_intersection_mode;
1235
1236 /*
1237 * Association of struct port_id -> struct trace_range.
1238 */
1239 GHashTable *intersections;
9009cc24
PP
1240};
1241
75a2cb9b
JG
1242/* Returns a timestamp of the form "(-)s.ns" */
1243static
1244char *s_from_ns(int64_t ns)
1245{
1246 int ret;
1247 char *s_ret = NULL;
1248 bool is_negative;
1249 int64_t ts_sec_abs, ts_nsec_abs;
1250 int64_t ts_sec = ns / NSEC_PER_SEC;
1251 int64_t ts_nsec = ns % NSEC_PER_SEC;
1252
1253 if (ts_sec >= 0 && ts_nsec >= 0) {
1254 is_negative = false;
1255 ts_sec_abs = ts_sec;
1256 ts_nsec_abs = ts_nsec;
1257 } else if (ts_sec > 0 && ts_nsec < 0) {
1258 is_negative = false;
1259 ts_sec_abs = ts_sec - 1;
1260 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
1261 } else if (ts_sec == 0 && ts_nsec < 0) {
1262 is_negative = true;
1263 ts_sec_abs = ts_sec;
1264 ts_nsec_abs = -ts_nsec;
1265 } else if (ts_sec < 0 && ts_nsec > 0) {
1266 is_negative = true;
1267 ts_sec_abs = -(ts_sec + 1);
1268 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
1269 } else if (ts_sec < 0 && ts_nsec == 0) {
1270 is_negative = true;
1271 ts_sec_abs = -ts_sec;
1272 ts_nsec_abs = ts_nsec;
1273 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1274 is_negative = true;
1275 ts_sec_abs = -ts_sec;
1276 ts_nsec_abs = -ts_nsec;
1277 }
1278
1279 ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64,
1280 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
1281 if (ret < 0) {
1282 s_ret = NULL;
1283 }
1284 return s_ret;
1285}
1286
9009cc24
PP
1287static
1288int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1289 struct cmd_run_ctx *ctx, struct bt_component *upstream_comp,
1290 struct bt_port *upstream_port,
1291 struct bt_config_connection *cfg_conn)
290725f7
PP
1292{
1293 int ret = 0;
9009cc24
PP
1294 GQuark downstreamp_comp_name_quark;
1295 struct bt_component *downstream_comp;
1296 int64_t downstream_port_count;
1297 uint64_t i;
1298 int64_t (*port_count_fn)(struct bt_component *);
1299 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
a256a42d 1300 enum bt_graph_status status = BT_GRAPH_STATUS_ERROR;
75a2cb9b
JG
1301 bool insert_trimmer = false;
1302 struct bt_value *trimmer_params = NULL;
1303 char *intersection_begin = NULL;
1304 char *intersection_end = NULL;
1305 struct bt_component *trimmer = NULL;
1306 struct bt_component_class *trimmer_class = NULL;
1307 struct bt_port *trimmer_input = NULL;
1308 struct bt_port *trimmer_output = NULL;
1309
1310 if (ctx->intersections &&
1311 bt_component_get_class_type(upstream_comp) ==
1312 BT_COMPONENT_CLASS_TYPE_SOURCE) {
1313 struct trace_range *range;
1314 struct port_id port_id = {
1315 .instance_name = (char *) bt_component_get_name(upstream_comp),
1316 .port_name = (char *) bt_port_get_name(upstream_port)
1317 };
1318
1319 if (!port_id.instance_name || !port_id.port_name) {
1320 goto error;
1321 }
1322
1323 range = (struct trace_range *) g_hash_table_lookup(
1324 ctx->intersections, &port_id);
1325 if (range) {
1326 enum bt_value_status status;
1327
1328 intersection_begin = s_from_ns(
1329 range->intersection_range_begin_ns);
1330 intersection_end = s_from_ns(
1331 range->intersection_range_end_ns);
1332 if (!intersection_begin || !intersection_end) {
1333 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1334 goto error;
1335 }
1336
1337 insert_trimmer = true;
1338 trimmer_params = bt_value_map_create();
1339 if (!trimmer_params) {
1340 goto error;
1341 }
1342
1343 status = bt_value_map_insert_string(trimmer_params,
1344 "begin", intersection_begin);
1345 if (status != BT_VALUE_STATUS_OK) {
1346 goto error;
1347 }
1348 status = bt_value_map_insert_string(trimmer_params,
1349 "end", intersection_end);
1350 if (status != BT_VALUE_STATUS_OK) {
1351 goto error;
1352 }
1353 }
1354
1355 trimmer_class = find_component_class("utils", "trimmer",
1356 BT_COMPONENT_CLASS_TYPE_FILTER);
1357 if (!trimmer_class) {
1358 goto error;
1359 }
1360 }
9009cc24 1361
7213a328
PP
1362 BT_LOGI("Connecting upstream port to the next available downstream port: "
1363 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1364 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1365 upstream_port, bt_port_get_name(upstream_port),
1366 cfg_conn->downstream_comp_name->str,
1367 cfg_conn->arg->str);
9009cc24
PP
1368 downstreamp_comp_name_quark = g_quark_from_string(
1369 cfg_conn->downstream_comp_name->str);
1370 assert(downstreamp_comp_name_quark > 0);
1371 downstream_comp = g_hash_table_lookup(ctx->components,
71c7c95f 1372 GUINT_TO_POINTER(downstreamp_comp_name_quark));
9009cc24 1373 if (!downstream_comp) {
7213a328
PP
1374 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1375 "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
1376 cfg_conn->arg->str);
9009cc24
PP
1377 fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n",
1378 cfg_conn->arg->str);
1379 goto error;
1380 }
1381
1382 if (bt_component_is_filter(downstream_comp)) {
1383 port_count_fn = bt_component_filter_get_input_port_count;
1384 port_by_index_fn = bt_component_filter_get_input_port_by_index;
1385 } else if (bt_component_is_sink(downstream_comp)) {
1386 port_count_fn = bt_component_sink_get_input_port_count;
1387 port_by_index_fn = bt_component_sink_get_input_port_by_index;
1388 } else {
1389 /*
1390 * Should never happen because the connections are
1391 * validated before we get here.
1392 */
7213a328
PP
1393 BT_LOGF("Invalid connection: downstream component is a source: "
1394 "conn-arg=\"%s\"", cfg_conn->arg->str);
0fbb9a9f 1395 abort();
9009cc24 1396 }
290725f7 1397
9009cc24
PP
1398 downstream_port_count = port_count_fn(downstream_comp);
1399 assert(downstream_port_count >= 0);
1400
1401 for (i = 0; i < downstream_port_count; i++) {
1402 struct bt_port *downstream_port =
1403 port_by_index_fn(downstream_comp, i);
75a2cb9b 1404 const char *upstream_port_name;
9009cc24
PP
1405 const char *downstream_port_name;
1406
1407 assert(downstream_port);
1408
75a2cb9b 1409 /* Skip port if it's already connected. */
9009cc24
PP
1410 if (bt_port_is_connected(downstream_port)) {
1411 bt_put(downstream_port);
7213a328
PP
1412 BT_LOGD("Skipping downstream port: already connected: "
1413 "port-addr=%p, port-name=\"%s\"",
1414 downstream_port,
1415 bt_port_get_name(downstream_port));
9009cc24
PP
1416 continue;
1417 }
1418
1419 downstream_port_name = bt_port_get_name(downstream_port);
1420 assert(downstream_port_name);
75a2cb9b
JG
1421 upstream_port_name = bt_port_get_name(upstream_port);
1422 assert(upstream_port_name);
9009cc24 1423
75a2cb9b 1424 if (!bt_common_star_glob_match(
1974687e
MJ
1425 cfg_conn->downstream_port_glob->str, SIZE_MAX,
1426 downstream_port_name, SIZE_MAX)) {
9009cc24 1427 bt_put(downstream_port);
75a2cb9b
JG
1428 continue;
1429 }
1430
1431 if (insert_trimmer) {
1432 /*
1433 * In order to insert the trimmer between the two
1434 * components that were being connected, we create
1435 * a connection configuration entry which describes
1436 * a connection from the trimmer's output to the
1437 * original input that was being connected.
1438 *
1439 * Hence, the creation of the trimmer will cause the
1440 * graph "new port" listener to establish all downstream
1441 * connections as its output port is connected. We will
1442 * then establish the connection between the original
1443 * upstream source and the trimmer.
1444 */
1445 char *trimmer_name = NULL;
1446 enum bt_graph_status graph_status;
1447
1448 ret = asprintf(&trimmer_name, "%s-%s",
1449 "stream-intersection-trimmer",
1450 upstream_port_name);
1451 if (ret < 0) {
1452 goto error;
1453 }
1454 ret = 0;
1455
1456 ctx->connect_ports = false;
1457 graph_status = bt_graph_add_component(ctx->graph,
1458 trimmer_class, trimmer_name, trimmer_params,
1459 &trimmer);
1460 free(trimmer_name);
1461 if (graph_status != BT_GRAPH_STATUS_OK) {
1462 goto error;
1463 }
1464 assert(trimmer);
1465
1466 trimmer_input =
1467 bt_component_filter_get_input_port_by_index(
1468 trimmer, 0);
1469 if (!trimmer_input) {
1470 goto error;
1471 }
1472 trimmer_output =
1473 bt_component_filter_get_output_port_by_index(
1474 trimmer, 0);
1475 if (!trimmer_output) {
9009cc24
PP
1476 goto error;
1477 }
1478
75a2cb9b
JG
1479 /*
1480 * Replace the current downstream port by the trimmer's
1481 * upstream port.
1482 */
1483 BT_MOVE(downstream_port, trimmer_input);
1484 downstream_port_name = bt_port_get_name(
1485 downstream_port);
1486 if (!downstream_port_name) {
1487 goto error;
1488 }
1489 }
1490
1491 /* We have a winner! */
1492 status = bt_graph_connect_ports(ctx->graph,
1493 upstream_port, downstream_port, NULL);
1494 BT_PUT(downstream_port);
1495 switch (status) {
1496 case BT_GRAPH_STATUS_OK:
1497 break;
1498 case BT_GRAPH_STATUS_CANCELED:
1499 BT_LOGI_STR("Graph was canceled by user.");
1500 status = BT_GRAPH_STATUS_OK;
1501 break;
1502 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
1503 BT_LOGE("A component refused a connection to one of its ports: "
7213a328
PP
1504 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1505 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1506 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1507 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1508 "conn-arg=\"%s\"",
1509 upstream_comp, bt_component_get_name(upstream_comp),
1510 upstream_port, bt_port_get_name(upstream_port),
1511 downstream_comp, cfg_conn->downstream_comp_name->str,
1512 downstream_port, downstream_port_name,
1513 cfg_conn->arg->str);
75a2cb9b
JG
1514 fprintf(stderr,
1515 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1516 bt_port_get_name(upstream_port),
1517 downstream_port_name,
1518 cfg_conn->arg->str);
1519 break;
1520 default:
1521 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1522 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1523 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1524 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1525 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1526 "conn-arg=\"%s\"",
1527 upstream_comp, bt_component_get_name(upstream_comp),
1528 upstream_port, bt_port_get_name(upstream_port),
1529 downstream_comp, cfg_conn->downstream_comp_name->str,
1530 downstream_port, downstream_port_name,
1531 cfg_conn->arg->str);
1532 fprintf(stderr,
1533 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1534 bt_port_get_name(upstream_port),
1535 downstream_port_name,
1536 cfg_conn->arg->str);
1537 goto error;
9009cc24
PP
1538 }
1539
75a2cb9b
JG
1540 BT_LOGI("Connected component ports: "
1541 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1542 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1543 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1544 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1545 "conn-arg=\"%s\"",
1546 upstream_comp, bt_component_get_name(upstream_comp),
1547 upstream_port, bt_port_get_name(upstream_port),
1548 downstream_comp, cfg_conn->downstream_comp_name->str,
1549 downstream_port, downstream_port_name,
1550 cfg_conn->arg->str);
1551
1552 if (insert_trimmer) {
1553 /*
1554 * The first connection, from the source to the trimmer,
1555 * has been done. We now connect the trimmer to the
1556 * original downstream port.
1557 */
1558 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1559 ctx, trimmer, trimmer_output, cfg_conn);
1560 if (ret) {
1561 goto error;
1562 }
1563 ctx->connect_ports = true;
1564 }
1565 goto end;
9009cc24
PP
1566 }
1567
a256a42d 1568 if (status != BT_GRAPH_STATUS_OK) {
7213a328
PP
1569 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1570 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1571 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1572 upstream_port, bt_port_get_name(upstream_port),
1573 cfg_conn->downstream_comp_name->str,
1574 cfg_conn->arg->str);
9009cc24
PP
1575 fprintf(stderr,
1576 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1577 bt_port_get_name(upstream_port), cfg_conn->arg->str);
1578 goto error;
05a67631
PP
1579 }
1580
9009cc24
PP
1581 goto end;
1582
1583error:
1584 ret = -1;
1585
1586end:
75a2cb9b
JG
1587 free(intersection_begin);
1588 free(intersection_end);
1589 BT_PUT(trimmer_params);
1590 BT_PUT(trimmer_class);
1591 BT_PUT(trimmer);
1592 BT_PUT(trimmer_input);
1593 BT_PUT(trimmer_output);
9009cc24
PP
1594 return ret;
1595}
1596
1597static
1598int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
1599 struct bt_port *upstream_port)
1600{
1601 int ret = 0;
1602 const char *upstream_port_name;
1603 const char *upstream_comp_name;
1604 struct bt_component *upstream_comp = NULL;
1605 size_t i;
1606
1607 assert(ctx);
1608 assert(upstream_port);
1609 upstream_port_name = bt_port_get_name(upstream_port);
1610 assert(upstream_port_name);
1611 upstream_comp = bt_port_get_component(upstream_port);
1612 if (!upstream_comp) {
7213a328
PP
1613 BT_LOGW("Upstream port to connect is not part of a component: "
1614 "port-addr=%p, port-name=\"%s\"",
1615 upstream_port, upstream_port_name);
98ecef32
MD
1616 ret = -1;
1617 goto end;
33bceaf8
JG
1618 }
1619
9009cc24
PP
1620 upstream_comp_name = bt_component_get_name(upstream_comp);
1621 assert(upstream_comp_name);
7213a328
PP
1622 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1623 "port-addr=%p, port-name=\"%s\"",
1624 upstream_comp, upstream_comp_name,
1625 upstream_port, upstream_port_name);
9009cc24
PP
1626
1627 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1628 struct bt_config_connection *cfg_conn =
1629 g_ptr_array_index(
1630 ctx->cfg->cmd_data.run.connections, i);
1631
1632 if (strcmp(cfg_conn->upstream_comp_name->str,
75a2cb9b
JG
1633 upstream_comp_name)) {
1634 continue;
1635 }
1636
1637 if (!bt_common_star_glob_match(
1638 cfg_conn->upstream_port_glob->str,
1974687e 1639 SIZE_MAX, upstream_port_name, SIZE_MAX)) {
75a2cb9b
JG
1640 continue;
1641 }
1642
1643 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1644 ctx, upstream_comp, upstream_port, cfg_conn);
1645 if (ret) {
1646 BT_LOGE("Cannot connect upstream port: "
1647 "port-addr=%p, port-name=\"%s\"",
1648 upstream_port,
1649 upstream_port_name);
1650 fprintf(stderr,
1651 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1652 upstream_port_name,
1653 upstream_comp_name,
1654 cfg_conn->arg->str);
1655 goto error;
9009cc24 1656 }
75a2cb9b 1657 goto end;
9009cc24
PP
1658 }
1659
7213a328
PP
1660 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1661 "port-addr=%p, port-name=\"%s\"", upstream_port,
1662 upstream_port_name);
9009cc24
PP
1663 fprintf(stderr,
1664 "Cannot create connection: upstream port `%s` does not match any connection\n",
7213a328 1665 upstream_port_name);
9009cc24
PP
1666
1667error:
1668 ret = -1;
1669
1670end:
1671 bt_put(upstream_comp);
1672 return ret;
1673}
1674
1675static
1676void graph_port_added_listener(struct bt_port *port, void *data)
1677{
1678 struct bt_component *comp = NULL;
1679 struct cmd_run_ctx *ctx = data;
1680
e12720c0
PP
1681 comp = bt_port_get_component(port);
1682 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1683 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1684 comp, comp ? bt_component_get_name(comp) : "",
7213a328 1685 port, bt_port_get_name(port));
36712f1d
PP
1686
1687 if (!ctx->connect_ports) {
1688 goto end;
1689 }
1690
e12720c0
PP
1691 if (!comp) {
1692 BT_LOGW_STR("Port has no component.");
56a1cced
JG
1693 goto end;
1694 }
7c7c0433 1695
e12720c0
PP
1696 if (bt_port_is_connected(port)) {
1697 BT_LOGW_STR("Port is already connected.");
7c7c0433
JG
1698 goto end;
1699 }
1700
9009cc24 1701 if (!bt_port_is_output(port)) {
7213a328 1702 BT_LOGI_STR("Skipping input port.");
61ddbc8a
JG
1703 goto end;
1704 }
1705
9009cc24 1706 if (cmd_run_ctx_connect_upstream_port(ctx, port)) {
7213a328 1707 BT_LOGF_STR("Cannot connect upstream port.");
9009cc24
PP
1708 fprintf(stderr, "Added port could not be connected: aborting\n");
1709 abort();
1710 }
1711
1712end:
1713 bt_put(comp);
1714 return;
1715}
1716
1717static
1718void graph_port_removed_listener(struct bt_component *component,
1719 struct bt_port *port, void *data)
1720{
7213a328
PP
1721 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1722 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1723 component, bt_component_get_name(component),
1724 port, bt_port_get_name(port));
9009cc24
PP
1725}
1726
1727static
1728void graph_ports_connected_listener(struct bt_port *upstream_port,
1729 struct bt_port *downstream_port, void *data)
1730{
e12720c0
PP
1731 struct bt_component *upstream_comp = bt_port_get_component(upstream_port);
1732 struct bt_component *downstream_comp = bt_port_get_component(downstream_port);
1733
1734 assert(upstream_comp);
1735 assert(downstream_comp);
7213a328 1736 BT_LOGI("Graph's component ports connected: "
e12720c0 1737 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
7213a328 1738 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
e12720c0 1739 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
7213a328 1740 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
e12720c0 1741 upstream_comp, bt_component_get_name(upstream_comp),
7213a328 1742 upstream_port, bt_port_get_name(upstream_port),
e12720c0 1743 downstream_comp, bt_component_get_name(downstream_comp),
7213a328 1744 downstream_port, bt_port_get_name(downstream_port));
e12720c0
PP
1745 bt_put(upstream_comp);
1746 bt_put(downstream_comp);
9009cc24
PP
1747}
1748
1749static
1750void graph_ports_disconnected_listener(
1751 struct bt_component *upstream_component,
1752 struct bt_component *downstream_component,
1753 struct bt_port *upstream_port, struct bt_port *downstream_port,
1754 void *data)
1755{
7213a328
PP
1756 BT_LOGI("Graph's component ports disconnected: "
1757 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1758 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1759 upstream_port, bt_port_get_name(upstream_port),
1760 downstream_port, bt_port_get_name(downstream_port));
9009cc24
PP
1761}
1762
1763static
1764void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
1765{
1766 if (!ctx) {
1767 return;
1768 }
1769
1770 if (ctx->components) {
1771 g_hash_table_destroy(ctx->components);
1772 ctx->components = NULL;
1773 }
1774
75a2cb9b
JG
1775 if (ctx->intersections) {
1776 g_hash_table_destroy(ctx->intersections);
a5302548 1777 ctx->intersections = NULL;
75a2cb9b
JG
1778 }
1779
9009cc24 1780 BT_PUT(ctx->graph);
5401f780 1781 the_graph = NULL;
9009cc24
PP
1782 ctx->cfg = NULL;
1783}
1784
1785static
1786int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
1787{
1788 int ret = 0;
1789
1790 ctx->cfg = cfg;
1791 ctx->connect_ports = false;
1792 ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1793 NULL, bt_put);
1794 if (!ctx->components) {
1795 goto error;
1796 }
1797
75a2cb9b
JG
1798 if (cfg->cmd_data.run.stream_intersection_mode) {
1799 ctx->stream_intersection_mode = true;
1800 ctx->intersections = g_hash_table_new_full(port_id_hash,
1801 port_id_equal, port_id_destroy, trace_range_destroy);
1802 if (!ctx->intersections) {
1803 goto error;
1804 }
1805 }
1806
9009cc24
PP
1807 ctx->graph = bt_graph_create();
1808 if (!ctx->graph) {
1809 goto error;
1810 }
1811
5401f780 1812 the_graph = ctx->graph;
9009cc24
PP
1813 ret = bt_graph_add_port_added_listener(ctx->graph,
1814 graph_port_added_listener, ctx);
0d107cdd
PP
1815 if (ret < 0) {
1816 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
9009cc24
PP
1817 goto error;
1818 }
1819
1820 ret = bt_graph_add_port_removed_listener(ctx->graph,
1821 graph_port_removed_listener, ctx);
0d107cdd
PP
1822 if (ret < 0) {
1823 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
9009cc24
PP
1824 goto error;
1825 }
1826
1827 ret = bt_graph_add_ports_connected_listener(ctx->graph,
1828 graph_ports_connected_listener, ctx);
0d107cdd
PP
1829 if (ret < 0) {
1830 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
9009cc24
PP
1831 goto error;
1832 }
1833
1834 ret = bt_graph_add_ports_disconnected_listener(ctx->graph,
1835 graph_ports_disconnected_listener, ctx);
0d107cdd
PP
1836 if (ret < 0) {
1837 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
9009cc24
PP
1838 goto error;
1839 }
1840
1841 goto end;
1842
1843error:
1844 cmd_run_ctx_destroy(ctx);
1845 ret = -1;
1846
1847end:
1848 return ret;
1849}
1850
75a2cb9b
JG
1851static
1852int set_stream_intersections(struct cmd_run_ctx *ctx,
1853 struct bt_config_component *cfg_comp,
1854 struct bt_component_class *comp_cls)
1855{
1856 int ret = 0;
1857 uint64_t trace_idx;
1858 int64_t trace_count;
1859 enum bt_value_status value_status;
1860 const char *path = NULL;
1861 struct bt_value *component_path_value = NULL;
1862 struct bt_value *query_params = NULL;
1863 struct bt_value *query_result = NULL;
1864 struct bt_value *trace_info = NULL;
1865 struct bt_value *intersection_range = NULL;
1866 struct bt_value *intersection_begin = NULL;
1867 struct bt_value *intersection_end = NULL;
1868 struct bt_value *stream_path_value = NULL;
1869 struct bt_value *stream_paths = NULL;
1870 struct bt_value *stream_infos = NULL;
1871 struct bt_value *stream_info = NULL;
1872 struct port_id *port_id = NULL;
1873 struct trace_range *trace_range = NULL;
1874
1875 component_path_value = bt_value_map_get(cfg_comp->params, "path");
1876 if (!bt_value_is_string(component_path_value)) {
1877 BT_LOGD("Cannot get path parameter: component-name=%s",
1878 cfg_comp->instance_name->str);
1879 ret = -1;
1880 goto error;
1881 }
1882
1883 value_status = bt_value_string_get(component_path_value, &path);
1884 if (value_status != BT_VALUE_STATUS_OK) {
1885 BT_LOGD("Cannot get path string value: component-name=%s",
1886 cfg_comp->instance_name->str);
1887 ret = -1;
1888 goto error;
1889 }
1890
1891 query_params = bt_value_map_create();
1892 if (!query_params) {
1893 BT_LOGE_STR("Cannot create query parameters.");
1894 ret = -1;
1895 goto error;
1896 }
1897
1898 value_status = bt_value_map_insert(query_params, "path", component_path_value);
1899 if (value_status != BT_VALUE_STATUS_OK) {
1900 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
1901 ret = -1;
1902 goto error;
1903 }
1904
1905 query_result = bt_component_class_query(comp_cls, "trace-info",
1906 query_params);
1907 if (!query_result) {
1908 BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
1909 bt_component_class_get_name(comp_cls));
1910 ret = -1;
1911 goto error;
1912 }
1913
1914 if (!bt_value_is_array(query_result)) {
1915 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1916 "component-class-name=%s",
1917 bt_component_class_get_name(comp_cls));
1918 ret = -1;
1919 goto error;
1920 }
1921
1922 trace_count = bt_value_array_size(query_result);
1923 if (trace_count < 0) {
1924 ret = -1;
1925 goto error;
1926 }
1927
1928 for (trace_idx = 0; trace_idx < trace_count; trace_idx++) {
1929 int64_t begin, end;
1930 uint64_t stream_idx;
1931 int64_t stream_count;
1932
1933 trace_info = bt_value_array_get(query_result, trace_idx);
1934 if (!trace_info || !bt_value_is_map(trace_info)) {
1935 ret = -1;
1936 BT_LOGD_STR("Cannot retrieve trace from query result.");
1937 goto error;
1938 }
1939
1940 intersection_range = bt_value_map_get(trace_info,
1941 "intersection-range-ns");
1942 if (!intersection_range) {
1943 ret = -1;
1944 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1945 goto error;
1946 }
1947
1948 intersection_begin = bt_value_map_get(intersection_range,
1949 "begin");
1950 if (!intersection_begin) {
1951 ret = -1;
1952 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1953 goto error;
1954 }
1955
1956 intersection_end = bt_value_map_get(intersection_range,
1957 "end");
1958 if (!intersection_end) {
1959 ret = -1;
1960 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1961 goto error;
1962 }
1963
1964 value_status = bt_value_integer_get(intersection_begin, &begin);
1965 if (value_status != BT_VALUE_STATUS_OK) {
1966 ret = -1;
1967 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
1968 goto error;
1969 }
1970
1971 value_status = bt_value_integer_get(intersection_end, &end);
1972 if (value_status != BT_VALUE_STATUS_OK) {
1973 ret = -1;
1974 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
1975 goto error;
1976 }
1977
1978 if (begin < 0 || end < 0 || end < begin) {
1979 BT_LOGW("Invalid trace stream intersection values: "
1980 "intersection-range-ns:begin=%" PRId64
1981 ", intersection-range-ns:end=%" PRId64,
1982 begin, end);
1983 ret = -1;
1984 goto error;
1985 }
1986
1987 stream_infos = bt_value_map_get(trace_info, "streams");
1988 if (!stream_infos || !bt_value_is_array(stream_infos)) {
1989 ret = -1;
1990 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
1991 goto error;
1992 }
1993
1994 stream_count = bt_value_array_size(stream_infos);
1995 if (stream_count < 0) {
1996 ret = -1;
1997 goto error;
1998 }
1999
2000 /*
2001 * FIXME
2002 *
2003 * The first path of a stream's "paths" is currently used to
2004 * associate streams/ports to a given trace intersection.
2005 *
2006 * This is a fragile hack as it relies on the port names
2007 * being set to the various streams path.
2008 *
2009 * A stream name should be introduced as part of the trace-info
2010 * query result.
2011 */
2012 for (stream_idx = 0; stream_idx < stream_count; stream_idx++) {
2013 const char *stream_path;
75a2cb9b
JG
2014
2015 port_id = g_new0(struct port_id, 1);
2016 if (!port_id) {
2017 ret = -1;
2018 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2019 goto error;
2020 }
2021 port_id->instance_name = strdup(cfg_comp->instance_name->str);
2022 if (!port_id->instance_name) {
2023 ret = -1;
2024 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2025 goto error;
2026 }
2027
2028 trace_range = g_new0(struct trace_range, 1);
2029 if (!trace_range) {
2030 ret = -1;
2031 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2032 goto error;
2033 }
2034 trace_range->intersection_range_begin_ns = begin;
2035 trace_range->intersection_range_end_ns = end;
2036
2037 stream_info = bt_value_array_get(stream_infos,
2038 stream_idx);
2039 if (!stream_info || !bt_value_is_map(stream_info)) {
2040 ret = -1;
2041 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2042 goto error;
2043 }
2044
2045 stream_paths = bt_value_map_get(stream_info, "paths");
2046 if (!stream_paths || !bt_value_is_array(stream_paths)) {
2047 ret = -1;
2048 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2049 goto error;
2050 }
2051
2052 stream_path_value = bt_value_array_get(stream_paths, 0);
2053 if (!stream_path_value ||
2054 !bt_value_is_string(stream_path_value)) {
2055 ret = -1;
2056 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2057 goto error;
2058 }
2059
2060 value_status = bt_value_string_get(stream_path_value,
2061 &stream_path);
2062 if (value_status != BT_VALUE_STATUS_OK) {
2063 ret = -1;
2064 goto error;
2065 }
2066
2067 port_id->port_name = strdup(stream_path);
2068 if (!port_id->port_name) {
2069 ret = -1;
2070 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2071 goto error;
2072 }
2073
2074 BT_LOGD("Inserting stream intersection ");
2075
1ddeea34 2076 g_hash_table_insert(ctx->intersections, port_id, trace_range);
75a2cb9b
JG
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
75a2cb9b
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)) {
75a2cb9b
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)) {
75a2cb9b
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)) {
75a2cb9b
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
75a2cb9b
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
b4565e8b
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 */
3efa3052
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 */
3efa3052
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) {
fbb2f0da 2577 g_setenv(*env_var_name, "I", 1);
c6d4d1ae 2578 } else if (cfg->debug) {
fbb2f0da 2579 g_setenv(*env_var_name, "V", 1);
c6d4d1ae 2580 } else {
3efa3052
PP
2581 char val[2] = { 0 };
2582
c6d4d1ae
PP
2583 /*
2584 * Set module's default log level if not
2585 * explicitly specified.
2586 */
3efa3052 2587 val[0] = cfg->log_level;
fbb2f0da 2588 g_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.178973 seconds and 4 git commands to generate.