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