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