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