lib/ctf-ir/packet.c: add logging
[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
95d36295 29#include <babeltrace/babeltrace.h>
7c7c0433 30#include <babeltrace/plugin/plugin.h>
290725f7 31#include <babeltrace/common-internal.h>
b2e0c907
PP
32#include <babeltrace/graph/component.h>
33#include <babeltrace/graph/component-source.h>
34#include <babeltrace/graph/component-sink.h>
35#include <babeltrace/graph/component-filter.h>
36#include <babeltrace/graph/component-class.h>
37#include <babeltrace/graph/port.h>
38#include <babeltrace/graph/graph.h>
39#include <babeltrace/graph/connection.h>
40#include <babeltrace/graph/notification-iterator.h>
2e339de1
JG
41#include <babeltrace/ref.h>
42#include <babeltrace/values.h>
7213a328 43#include <babeltrace/logging.h>
a8ff38ef 44#include <unistd.h>
34ac0e6c 45#include <stdlib.h>
7f26a816
PP
46#include <popt.h>
47#include <string.h>
48#include <stdio.h>
33b34c43 49#include <glib.h>
dc3fffef 50#include <inttypes.h>
7cdc2bab 51#include <unistd.h>
5401f780 52#include <signal.h>
c42c79ea 53#include "babeltrace-cfg.h"
9009cc24
PP
54#include "babeltrace-cfg-cli-args.h"
55#include "babeltrace-cfg-cli-args-default.h"
56
7213a328
PP
57#define BT_LOG_TAG "CLI"
58#include "logging.h"
59
9009cc24 60#define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
34ac0e6c 61
5401f780
PP
62/* Application's processing graph (weak) */
63static struct bt_graph *the_graph;
64static bool canceled = false;
65
33b34c43
PP
66GPtrArray *loaded_plugins;
67
7213a328
PP
68BT_HIDDEN
69int bt_cli_log_level = BT_LOG_NONE;
70
5401f780
PP
71static
72void sigint_handler(int signum)
73{
74 if (signum != SIGINT) {
75 return;
76 }
77
78 if (the_graph) {
79 bt_graph_cancel(the_graph);
80 }
81
82 canceled = true;
83}
84
33b34c43 85static
9009cc24 86void init_static_data(void)
33b34c43 87{
9009cc24 88 loaded_plugins = g_ptr_array_new_with_free_func(bt_put);
33b34c43
PP
89}
90
91static
9009cc24 92void fini_static_data(void)
33b34c43
PP
93{
94 g_ptr_array_free(loaded_plugins, TRUE);
95}
96
97static
98struct bt_plugin *find_plugin(const char *name)
99{
100 int i;
101 struct bt_plugin *plugin = NULL;
102
7213a328
PP
103 assert(name);
104 BT_LOGD("Finding plugin: name=\"%s\"", name);
105
33b34c43
PP
106 for (i = 0; i < loaded_plugins->len; i++) {
107 plugin = g_ptr_array_index(loaded_plugins, i);
108
109 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
110 break;
111 }
112
113 plugin = NULL;
114 }
115
7213a328
PP
116 if (BT_LOG_ON_DEBUG) {
117 if (plugin) {
118 BT_LOGD("Found plugin: plugin-addr=%p", plugin);
119 } else {
120 BT_LOGD("Cannot find plugin.");
121 }
122 }
123
33b34c43
PP
124 return bt_get(plugin);
125}
126
127static
128struct bt_component_class *find_component_class(const char *plugin_name,
129 const char *comp_class_name,
d3e4dcd8 130 enum bt_component_class_type comp_class_type)
33b34c43
PP
131{
132 struct bt_component_class *comp_class = NULL;
7213a328
PP
133 struct bt_plugin *plugin;
134
135 BT_LOGD("Finding component class: plugin-name=\"%s\", "
136 "comp-cls-name=\"%s\", comp-cls-type=%d",
137 plugin_name, comp_class_name, comp_class_type);
138
139 plugin = find_plugin(plugin_name);
33b34c43
PP
140
141 if (!plugin) {
142 goto end;
143 }
144
145 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
146 comp_class_name, comp_class_type);
147 BT_PUT(plugin);
7213a328 148
33b34c43 149end:
7213a328
PP
150 if (BT_LOG_ON_DEBUG) {
151 if (comp_class) {
152 BT_LOGD("Found component class: comp-cls-addr=%p",
153 comp_class);
154 } else {
155 BT_LOGD("Cannot find component class.");
156 }
157 }
158
33b34c43
PP
159 return comp_class;
160}
6c2f3ee5 161
c42c79ea 162static
7213a328 163void print_indent(FILE *fp, size_t indent)
c42c79ea
PP
164{
165 size_t i;
166
167 for (i = 0; i < indent; i++) {
7213a328 168 fprintf(fp, " ");
c42c79ea
PP
169 }
170}
171
87796884
PP
172static
173const char *component_type_str(enum bt_component_class_type type)
174{
175 switch (type) {
176 case BT_COMPONENT_CLASS_TYPE_SOURCE:
177 return "source";
178 case BT_COMPONENT_CLASS_TYPE_SINK:
179 return "sink";
180 case BT_COMPONENT_CLASS_TYPE_FILTER:
181 return "filter";
182 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
183 default:
184 return "unknown";
185 }
186}
187
9009cc24
PP
188static
189void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,
87796884
PP
190 const char *comp_cls_name, enum bt_component_class_type type)
191{
9009cc24
PP
192 GString *shell_plugin_name = NULL;
193 GString *shell_comp_cls_name = NULL;
87796884 194
9009cc24 195 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
87796884
PP
196 if (!shell_plugin_name) {
197 goto end;
198 }
199
9009cc24 200 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
87796884
PP
201 if (!shell_comp_cls_name) {
202 goto end;
203 }
204
205 fprintf(fh, "%s%s--%s%s %s'%s%s%s%s.%s%s%s'",
206 bt_common_color_bold(),
207 bt_common_color_fg_cyan(),
208 component_type_str(type),
209 bt_common_color_reset(),
210 bt_common_color_fg_default(),
211 bt_common_color_bold(),
212 bt_common_color_fg_blue(),
9009cc24 213 shell_plugin_name->str,
87796884
PP
214 bt_common_color_fg_default(),
215 bt_common_color_fg_yellow(),
9009cc24 216 shell_comp_cls_name->str,
87796884
PP
217 bt_common_color_reset());
218
219end:
9009cc24
PP
220 if (shell_plugin_name) {
221 g_string_free(shell_plugin_name, TRUE);
222 }
223
224 if (shell_comp_cls_name) {
225 g_string_free(shell_comp_cls_name, TRUE);
226 }
87796884
PP
227}
228
c42c79ea 229static
7213a328 230void print_value(FILE *, struct bt_value *, size_t);
c42c79ea 231
c1081aa6 232static
7213a328
PP
233void print_value_rec(FILE *, struct bt_value *, size_t);
234
235struct print_map_value_data {
236 size_t indent;
237 FILE *fp;
238};
c1081aa6 239
c42c79ea 240static
c55a9f58 241bt_bool print_map_value(const char *key, struct bt_value *object, void *data)
c42c79ea 242{
7213a328 243 struct print_map_value_data *print_map_value_data = data;
290725f7 244
7213a328
PP
245 print_indent(print_map_value_data->fp, print_map_value_data->indent);
246 fprintf(print_map_value_data->fp, "%s: ", key);
290725f7
PP
247
248 if (bt_value_is_array(object) &&
249 bt_value_array_is_empty(object)) {
7213a328 250 fprintf(print_map_value_data->fp, "[ ]\n");
290725f7
PP
251 return true;
252 }
253
254 if (bt_value_is_map(object) &&
255 bt_value_map_is_empty(object)) {
7213a328 256 fprintf(print_map_value_data->fp, "{ }\n");
290725f7
PP
257 return true;
258 }
c42c79ea 259
290725f7
PP
260 if (bt_value_is_array(object) ||
261 bt_value_is_map(object)) {
7213a328 262 fprintf(print_map_value_data->fp, "\n");
290725f7 263 }
c42c79ea 264
7213a328
PP
265 print_value_rec(print_map_value_data->fp, object,
266 print_map_value_data->indent + 2);
c55a9f58 267 return BT_TRUE;
c42c79ea
PP
268}
269
270static
7213a328 271void print_value_rec(FILE *fp, struct bt_value *value, size_t indent)
c42c79ea 272{
c55a9f58 273 bt_bool bool_val;
c42c79ea
PP
274 int64_t int_val;
275 double dbl_val;
276 const char *str_val;
277 int size;
278 int i;
279
280 if (!value) {
281 return;
282 }
283
c42c79ea
PP
284 switch (bt_value_get_type(value)) {
285 case BT_VALUE_TYPE_NULL:
7213a328 286 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
c1081aa6 287 bt_common_color_reset());
c42c79ea
PP
288 break;
289 case BT_VALUE_TYPE_BOOL:
290 bt_value_bool_get(value, &bool_val);
7213a328 291 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
292 bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
293 bt_common_color_reset());
c42c79ea
PP
294 break;
295 case BT_VALUE_TYPE_INTEGER:
296 bt_value_integer_get(value, &int_val);
7213a328 297 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
c1081aa6
PP
298 bt_common_color_fg_red(), int_val,
299 bt_common_color_reset());
c42c79ea
PP
300 break;
301 case BT_VALUE_TYPE_FLOAT:
302 bt_value_float_get(value, &dbl_val);
7213a328 303 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
c1081aa6
PP
304 bt_common_color_fg_red(), dbl_val,
305 bt_common_color_reset());
c42c79ea
PP
306 break;
307 case BT_VALUE_TYPE_STRING:
308 bt_value_string_get(value, &str_val);
7213a328 309 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
310 bt_common_color_fg_green(), str_val,
311 bt_common_color_reset());
c42c79ea
PP
312 break;
313 case BT_VALUE_TYPE_ARRAY:
314 size = bt_value_array_size(value);
290725f7
PP
315 assert(size >= 0);
316
317 if (size == 0) {
7213a328
PP
318 print_indent(fp, indent);
319 fprintf(fp, "[ ]\n");
290725f7
PP
320 break;
321 }
c42c79ea
PP
322
323 for (i = 0; i < size; i++) {
324 struct bt_value *element =
325 bt_value_array_get(value, i);
326
290725f7 327 assert(element);
7213a328
PP
328 print_indent(fp, indent);
329 fprintf(fp, "- ");
290725f7
PP
330
331 if (bt_value_is_array(element) &&
332 bt_value_array_is_empty(element)) {
7213a328 333 fprintf(fp, "[ ]\n");
290725f7
PP
334 continue;
335 }
336
337 if (bt_value_is_map(element) &&
338 bt_value_map_is_empty(element)) {
7213a328 339 fprintf(fp, "{ }\n");
290725f7
PP
340 continue;
341 }
342
343 if (bt_value_is_array(element) ||
344 bt_value_is_map(element)) {
7213a328 345 fprintf(fp, "\n");
290725f7
PP
346 }
347
7213a328 348 print_value_rec(fp, element, indent + 2);
c42c79ea
PP
349 BT_PUT(element);
350 }
c42c79ea
PP
351 break;
352 case BT_VALUE_TYPE_MAP:
7213a328
PP
353 {
354 struct print_map_value_data data = {
355 .indent = indent,
356 .fp = fp,
357 };
358
c42c79ea 359 if (bt_value_map_is_empty(value)) {
7213a328
PP
360 print_indent(fp, indent);
361 fprintf(fp, "{ }\n");
290725f7 362 break;
c42c79ea
PP
363 }
364
7213a328 365 bt_value_map_foreach(value, print_map_value, &data);
c42c79ea 366 break;
7213a328 367 }
c42c79ea
PP
368 default:
369 assert(false);
370 }
371}
372
c1081aa6 373static
7213a328 374void print_value(FILE *fp, struct bt_value *value, size_t indent)
c1081aa6
PP
375{
376 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
7213a328 377 print_indent(fp, indent);
c1081aa6
PP
378 }
379
7213a328 380 print_value_rec(fp, value, indent);
c1081aa6
PP
381}
382
c42c79ea
PP
383static
384void print_bt_config_component(struct bt_config_component *bt_config_component)
385{
7213a328
PP
386 fprintf(stderr, " ");
387 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
db0f160a 388 bt_config_component->comp_cls_name->str,
87796884 389 bt_config_component->type);
7213a328 390 fprintf(stderr, ":\n");
3b6cfcc5
PP
391
392 if (bt_config_component->instance_name->len > 0) {
7213a328 393 fprintf(stderr, " Name: %s\n",
3b6cfcc5
PP
394 bt_config_component->instance_name->str);
395 }
396
7213a328
PP
397 fprintf(stderr, " Parameters:\n");
398 print_value(stderr, bt_config_component->params, 8);
c42c79ea
PP
399}
400
401static
402void print_bt_config_components(GPtrArray *array)
403{
404 size_t i;
405
406 for (i = 0; i < array->len; i++) {
407 struct bt_config_component *cfg_component =
e5bc7f81 408 bt_config_get_component(array, i);
c42c79ea
PP
409 print_bt_config_component(cfg_component);
410 BT_PUT(cfg_component);
411 }
412}
413
290725f7
PP
414static
415void print_plugin_paths(struct bt_value *plugin_paths)
416{
7213a328
PP
417 fprintf(stderr, " Plugin paths:\n");
418 print_value(stderr, plugin_paths, 4);
290725f7
PP
419}
420
421static
db0f160a 422void print_cfg_run(struct bt_config *cfg)
290725f7 423{
ebba3338
PP
424 size_t i;
425
db0f160a 426 print_plugin_paths(cfg->plugin_paths);
7213a328 427 fprintf(stderr, " Source component instances:\n");
db0f160a 428 print_bt_config_components(cfg->cmd_data.run.sources);
ebba3338 429
db0f160a 430 if (cfg->cmd_data.run.filters->len > 0) {
7213a328 431 fprintf(stderr, " Filter component instances:\n");
db0f160a 432 print_bt_config_components(cfg->cmd_data.run.filters);
ebba3338
PP
433 }
434
7213a328 435 fprintf(stderr, " Sink component instances:\n");
db0f160a 436 print_bt_config_components(cfg->cmd_data.run.sinks);
7213a328 437 fprintf(stderr, " Connections:\n");
ebba3338 438
db0f160a 439 for (i = 0; i < cfg->cmd_data.run.connections->len; i++) {
ebba3338 440 struct bt_config_connection *cfg_connection =
db0f160a 441 g_ptr_array_index(cfg->cmd_data.run.connections,
ebba3338
PP
442 i);
443
7213a328 444 fprintf(stderr, " %s%s%s -> %s%s%s\n",
9009cc24
PP
445 cfg_connection->upstream_comp_name->str,
446 cfg_connection->upstream_port_glob->len > 0 ? "." : "",
447 cfg_connection->upstream_port_glob->str,
448 cfg_connection->downstream_comp_name->str,
449 cfg_connection->downstream_port_glob->len > 0 ? "." : "",
450 cfg_connection->downstream_port_glob->str);
ebba3338 451 }
290725f7
PP
452}
453
454static
455void print_cfg_list_plugins(struct bt_config *cfg)
456{
db0f160a 457 print_plugin_paths(cfg->plugin_paths);
290725f7
PP
458}
459
c1081aa6
PP
460static
461void print_cfg_help(struct bt_config *cfg)
462{
db0f160a
PP
463 print_plugin_paths(cfg->plugin_paths);
464}
465
466static
467void print_cfg_print_ctf_metadata(struct bt_config *cfg)
468{
469 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
470 fprintf(stderr, " Path: %s\n",
471 cfg->cmd_data.print_ctf_metadata.path->str);
db0f160a
PP
472}
473
474static
475void print_cfg_print_lttng_live_sessions(struct bt_config *cfg)
476{
477 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
478 fprintf(stderr, " URL: %s\n",
479 cfg->cmd_data.print_lttng_live_sessions.url->str);
c1081aa6
PP
480}
481
482static
a67681c1 483void print_cfg_query(struct bt_config *cfg)
c1081aa6 484{
db0f160a 485 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
486 fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str);
487 fprintf(stderr, " Component class:\n");
a67681c1 488 print_bt_config_component(cfg->cmd_data.query.cfg_component);
c1081aa6
PP
489}
490
c42c79ea
PP
491static
492void print_cfg(struct bt_config *cfg)
493{
7213a328 494 if (!BT_LOG_ON_INFO) {
00447e45
PP
495 return;
496 }
497
7213a328
PP
498 BT_LOGI_STR("Configuration:");
499 fprintf(stderr, " Debug mode: %s\n", cfg->debug ? "yes" : "no");
500 fprintf(stderr, " Verbose mode: %s\n", cfg->verbose ? "yes" : "no");
290725f7
PP
501
502 switch (cfg->command) {
db0f160a
PP
503 case BT_CONFIG_COMMAND_RUN:
504 print_cfg_run(cfg);
290725f7
PP
505 break;
506 case BT_CONFIG_COMMAND_LIST_PLUGINS:
507 print_cfg_list_plugins(cfg);
c1081aa6
PP
508 break;
509 case BT_CONFIG_COMMAND_HELP:
510 print_cfg_help(cfg);
511 break;
a67681c1
PP
512 case BT_CONFIG_COMMAND_QUERY:
513 print_cfg_query(cfg);
290725f7 514 break;
db0f160a
PP
515 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
516 print_cfg_print_ctf_metadata(cfg);
517 break;
518 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
519 print_cfg_print_lttng_live_sessions(cfg);
520 break;
290725f7
PP
521 default:
522 assert(false);
523 }
c42c79ea
PP
524}
525
33b34c43 526static
a8ff38ef 527void add_to_loaded_plugins(struct bt_plugin_set *plugin_set)
98ecef32 528{
544d0515
PP
529 int64_t i;
530 int64_t count;
a8ff38ef
PP
531
532 count = bt_plugin_set_get_plugin_count(plugin_set);
533 assert(count >= 0);
534
535 for (i = 0; i < count; i++) {
536 struct bt_plugin *plugin =
537 bt_plugin_set_get_plugin(plugin_set, i);
33b34c43
PP
538 struct bt_plugin *loaded_plugin =
539 find_plugin(bt_plugin_get_name(plugin));
540
a8ff38ef
PP
541 assert(plugin);
542
33b34c43 543 if (loaded_plugin) {
7213a328
PP
544 BT_LOGI("Not using plugin: another one already exists with the same name: "
545 "plugin-name=\"%s\", plugin-path=\"%s\", "
546 "existing-plugin-path=\"%s\"",
547 bt_plugin_get_name(plugin),
548 bt_plugin_get_path(plugin),
549 bt_plugin_get_path(loaded_plugin));
a8ff38ef 550 bt_put(loaded_plugin);
33b34c43 551 } else {
a8ff38ef 552 /* Add to global array. */
7213a328
PP
553 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
554 bt_plugin_get_name(plugin));
a8ff38ef 555 g_ptr_array_add(loaded_plugins, bt_get(plugin));
33b34c43 556 }
a8ff38ef
PP
557
558 bt_put(plugin);
33b34c43
PP
559 }
560}
561
562static
290725f7 563int load_dynamic_plugins(struct bt_value *plugin_paths)
33b34c43
PP
564{
565 int nr_paths, i, ret = 0;
98ecef32 566
290725f7 567 nr_paths = bt_value_array_size(plugin_paths);
98ecef32 568 if (nr_paths < 0) {
7213a328 569 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
33b34c43
PP
570 ret = -1;
571 goto end;
98ecef32 572 }
33b34c43 573
7213a328
PP
574 BT_LOGI("Loading dynamic plugins.");
575
98ecef32
MD
576 for (i = 0; i < nr_paths; i++) {
577 struct bt_value *plugin_path_value = NULL;
578 const char *plugin_path;
a8ff38ef 579 struct bt_plugin_set *plugin_set;
98ecef32 580
290725f7 581 plugin_path_value = bt_value_array_get(plugin_paths, i);
7213a328
PP
582 bt_value_string_get(plugin_path_value, &plugin_path);
583 assert(plugin_path);
a8ff38ef
PP
584 plugin_set = bt_plugin_create_all_from_dir(plugin_path, false);
585 if (!plugin_set) {
7213a328 586 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
98ecef32 587 plugin_path);
33b34c43
PP
588 BT_PUT(plugin_path_value);
589 continue;
98ecef32 590 }
33b34c43 591
a8ff38ef
PP
592 add_to_loaded_plugins(plugin_set);
593 bt_put(plugin_set);
98ecef32
MD
594 BT_PUT(plugin_path_value);
595 }
33b34c43
PP
596end:
597 return ret;
598}
599
600static
601int load_static_plugins(void)
602{
603 int ret = 0;
a8ff38ef 604 struct bt_plugin_set *plugin_set;
33b34c43 605
7213a328 606 BT_LOGI("Loading static plugins.");
a8ff38ef
PP
607 plugin_set = bt_plugin_create_all_from_static();
608 if (!plugin_set) {
7213a328 609 BT_LOGE("Unable to load static plugins.");
33b34c43
PP
610 ret = -1;
611 goto end;
612 }
613
a8ff38ef
PP
614 add_to_loaded_plugins(plugin_set);
615 bt_put(plugin_set);
33b34c43
PP
616end:
617 return ret;
98ecef32
MD
618}
619
9009cc24
PP
620static
621int load_all_plugins(struct bt_value *plugin_paths)
290725f7
PP
622{
623 int ret = 0;
33b34c43 624
290725f7 625 if (load_dynamic_plugins(plugin_paths)) {
290725f7 626 ret = -1;
c1870f57
JG
627 goto end;
628 }
629
290725f7 630 if (load_static_plugins()) {
290725f7 631 ret = -1;
c1870f57
JG
632 goto end;
633 }
634
7213a328
PP
635 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len);
636
290725f7
PP
637end:
638 return ret;
639}
640
9009cc24
PP
641static
642void print_plugin_info(struct bt_plugin *plugin)
22e22462
PP
643{
644 unsigned int major, minor, patch;
645 const char *extra;
646 enum bt_plugin_status version_status;
647 const char *plugin_name;
648 const char *path;
649 const char *author;
650 const char *license;
651 const char *plugin_description;
652
653 plugin_name = bt_plugin_get_name(plugin);
654 path = bt_plugin_get_path(plugin);
655 author = bt_plugin_get_author(plugin);
656 license = bt_plugin_get_license(plugin);
657 plugin_description = bt_plugin_get_description(plugin);
658 version_status = bt_plugin_get_version(plugin, &major, &minor,
659 &patch, &extra);
660 printf("%s%s%s%s:\n", bt_common_color_bold(),
661 bt_common_color_fg_blue(), plugin_name,
662 bt_common_color_reset());
663 printf(" %sPath%s: %s\n", bt_common_color_bold(),
664 bt_common_color_reset(), path ? path : "(None)");
665
666 if (version_status == BT_PLUGIN_STATUS_OK) {
667 printf(" %sVersion%s: %u.%u.%u",
668 bt_common_color_bold(), bt_common_color_reset(),
669 major, minor, patch);
670
671 if (extra) {
672 printf("%s", extra);
673 }
674
675 printf("\n");
676 }
677
678 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
679 bt_common_color_reset(),
680 plugin_description ? plugin_description : "(None)");
681 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
682 bt_common_color_reset(), author ? author : "(Unknown)");
683 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
684 bt_common_color_reset(),
685 license ? license : "(Unknown)");
686}
687
9009cc24
PP
688static
689int cmd_query(struct bt_config *cfg)
63ce0e1d 690{
db95fa29 691 int ret = 0;
63ce0e1d
PP
692 struct bt_component_class *comp_cls = NULL;
693 struct bt_value *results = NULL;
694
a67681c1 695 comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 696 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 697 cfg->cmd_data.query.cfg_component->type);
63ce0e1d 698 if (!comp_cls) {
7213a328
PP
699 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
700 "comp-cls-name=\"%s\", comp-cls-type=%d",
701 cfg->cmd_data.query.cfg_component->plugin_name->str,
702 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
703 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
704 fprintf(stderr, "%s%sCannot find component class %s",
705 bt_common_color_bold(),
706 bt_common_color_fg_red(),
707 bt_common_color_reset());
708 print_plugin_comp_cls_opt(stderr,
a67681c1 709 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 710 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 711 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
712 fprintf(stderr, "\n");
713 ret = -1;
714 goto end;
715 }
716
a67681c1
PP
717 results = bt_component_class_query(comp_cls,
718 cfg->cmd_data.query.object->str,
719 cfg->cmd_data.query.cfg_component->params);
63ce0e1d 720 if (!results) {
7213a328
PP
721 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
722 "comp-cls-name=\"%s\", comp-cls-type=%d "
723 "object=\"%s\"",
724 cfg->cmd_data.query.cfg_component->plugin_name->str,
725 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
726 cfg->cmd_data.query.cfg_component->type,
727 cfg->cmd_data.query.object->str);
63ce0e1d
PP
728 fprintf(stderr, "%s%sFailed to query info to %s",
729 bt_common_color_bold(),
730 bt_common_color_fg_red(),
731 bt_common_color_reset());
732 print_plugin_comp_cls_opt(stderr,
a67681c1 733 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 734 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1
PP
735 cfg->cmd_data.query.cfg_component->type);
736 fprintf(stderr, "%s%s with object `%s`%s\n",
63ce0e1d
PP
737 bt_common_color_bold(),
738 bt_common_color_fg_red(),
a67681c1 739 cfg->cmd_data.query.object->str,
63ce0e1d
PP
740 bt_common_color_reset());
741 ret = -1;
742 goto end;
743 }
744
7213a328 745 print_value(stdout, results, 0);
63ce0e1d
PP
746
747end:
748 bt_put(comp_cls);
749 bt_put(results);
750 return ret;
751}
752
9009cc24
PP
753static
754int cmd_help(struct bt_config *cfg)
22e22462 755{
db95fa29 756 int ret = 0;
22e22462
PP
757 struct bt_plugin *plugin = NULL;
758 size_t i;
759
90de159b 760 plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462 761 if (!plugin) {
7213a328
PP
762 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
763 cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462
PP
764 fprintf(stderr, "%s%sCannot find plugin %s%s%s\n",
765 bt_common_color_bold(), bt_common_color_fg_red(),
766 bt_common_color_fg_blue(),
90de159b 767 cfg->cmd_data.help.cfg_component->plugin_name->str,
22e22462
PP
768 bt_common_color_reset());
769 ret = -1;
770 goto end;
771 }
772
773 print_plugin_info(plugin);
774 printf(" %sComponent classes%s: %d\n",
775 bt_common_color_bold(),
776 bt_common_color_reset(),
544d0515 777 (int) bt_plugin_get_component_class_count(plugin));
22e22462
PP
778
779
90de159b 780 if (cfg->cmd_data.help.cfg_component->type !=
22e22462
PP
781 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
782 struct bt_component_class *needed_comp_cls =
783 find_component_class(
90de159b 784 cfg->cmd_data.help.cfg_component->plugin_name->str,
db0f160a 785 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
90de159b 786 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
787
788 if (!needed_comp_cls) {
7213a328
PP
789 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
790 "comp-cls-name=\"%s\", comp-cls-type=%d",
791 cfg->cmd_data.help.cfg_component->plugin_name->str,
792 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
793 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
794 fprintf(stderr, "\n%s%sCannot find component class %s",
795 bt_common_color_bold(),
796 bt_common_color_fg_red(),
797 bt_common_color_reset());
798 print_plugin_comp_cls_opt(stderr,
90de159b 799 cfg->cmd_data.help.cfg_component->plugin_name->str,
db0f160a 800 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
90de159b 801 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
802 fprintf(stderr, "\n");
803 ret = -1;
804 goto end;
805 }
806
807 bt_put(needed_comp_cls);
808 }
809
810 for (i = 0; i < bt_plugin_get_component_class_count(plugin); i++) {
811 struct bt_component_class *comp_cls =
9ac68eb1 812 bt_plugin_get_component_class_by_index(plugin, i);
22e22462
PP
813 const char *comp_class_name =
814 bt_component_class_get_name(comp_cls);
815 const char *comp_class_description =
816 bt_component_class_get_description(comp_cls);
817 const char *comp_class_help =
818 bt_component_class_get_help(comp_cls);
819 enum bt_component_class_type type =
820 bt_component_class_get_type(comp_cls);
821
822 assert(comp_cls);
823
90de159b 824 if (cfg->cmd_data.help.cfg_component->type !=
22e22462 825 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
db0f160a 826 if (strcmp(cfg->cmd_data.help.cfg_component->comp_cls_name->str,
22e22462
PP
827 comp_class_name) != 0 &&
828 type ==
90de159b 829 cfg->cmd_data.help.cfg_component->type) {
22e22462
PP
830 bt_put(comp_cls);
831 continue;
832 }
833 }
834
835 printf("\n");
836 print_plugin_comp_cls_opt(stdout,
90de159b 837 cfg->cmd_data.help.cfg_component->plugin_name->str,
22e22462
PP
838 comp_class_name,
839 type);
840 printf("\n");
841 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
842 bt_common_color_reset(),
843 comp_class_description ? comp_class_description : "(None)");
844
845 if (comp_class_help) {
846 printf("\n%s\n", comp_class_help);
847 }
848
849 bt_put(comp_cls);
850 }
851
852end:
853 bt_put(plugin);
854 return ret;
855}
856
9009cc24
PP
857static
858int cmd_list_plugins(struct bt_config *cfg)
290725f7 859{
7213a328 860 int ret = 0;
290725f7
PP
861 int plugins_count, component_classes_count = 0, i;
862
22e22462 863 printf("From the following plugin paths:\n\n");
7213a328 864 print_value(stdout, cfg->plugin_paths, 2);
22e22462 865 printf("\n");
290725f7
PP
866 plugins_count = loaded_plugins->len;
867 if (plugins_count == 0) {
7213a328 868 printf("No plugins found.\n");
56a1cced
JG
869 goto end;
870 }
871
290725f7
PP
872 for (i = 0; i < plugins_count; i++) {
873 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
874
875 component_classes_count += bt_plugin_get_component_class_count(plugin);
876 }
33bceaf8 877
290725f7
PP
878 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
879 bt_common_color_bold(),
880 component_classes_count,
881 bt_common_color_reset(),
882 bt_common_color_bold(),
883 plugins_count,
884 bt_common_color_reset());
885
886 for (i = 0; i < plugins_count; i++) {
887 int j;
888 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
290725f7
PP
889
890 component_classes_count =
891 bt_plugin_get_component_class_count(plugin);
22e22462
PP
892 printf("\n");
893 print_plugin_info(plugin);
290725f7
PP
894
895 if (component_classes_count == 0) {
9009cc24 896 printf(" %sComponent classes%s: (none)\n",
290725f7
PP
897 bt_common_color_bold(),
898 bt_common_color_reset());
899 } else {
900 printf(" %sComponent classes%s:\n",
901 bt_common_color_bold(),
902 bt_common_color_reset());
903 }
904
905 for (j = 0; j < component_classes_count; j++) {
906 struct bt_component_class *comp_class =
9ac68eb1
PP
907 bt_plugin_get_component_class_by_index(
908 plugin, j);
290725f7
PP
909 const char *comp_class_name =
910 bt_component_class_get_name(comp_class);
911 const char *comp_class_description =
912 bt_component_class_get_description(comp_class);
913 enum bt_component_class_type type =
914 bt_component_class_get_type(comp_class);
915
22e22462
PP
916 printf(" ");
917 print_plugin_comp_cls_opt(stdout,
918 bt_plugin_get_name(plugin), comp_class_name,
919 type);
290725f7
PP
920
921 if (comp_class_description) {
922 printf(": %s", comp_class_description);
923 }
924
925 printf("\n");
926 bt_put(comp_class);
927 }
928 }
929
930end:
931 return ret;
932}
933
9009cc24
PP
934static
935int cmd_print_lttng_live_sessions(struct bt_config *cfg)
db0f160a
PP
936{
937 printf("TODO\n");
938 return -1;
939}
940
9009cc24
PP
941static
942int cmd_print_ctf_metadata(struct bt_config *cfg)
05a67631
PP
943{
944 int ret = 0;
945 struct bt_component_class *comp_cls = NULL;
05a67631 946 struct bt_value *results = NULL;
05a67631
PP
947 struct bt_value *params = NULL;
948 struct bt_value *metadata_text_value = NULL;
949 const char *metadata_text = NULL;
db0f160a
PP
950 static const char * const plugin_name = "ctf";
951 static const char * const comp_cls_name = "fs";
952 static const enum bt_component_class_type comp_cls_type =
953 BT_COMPONENT_CLASS_TYPE_SOURCE;
954
955 assert(cfg->cmd_data.print_ctf_metadata.path);
956 comp_cls = find_component_class(plugin_name, comp_cls_name,
957 comp_cls_type);
05a67631 958 if (!comp_cls) {
7213a328
PP
959 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
960 "comp-cls-name=\"%s\", comp-cls-type=%d",
961 plugin_name, comp_cls_name,
962 BT_COMPONENT_CLASS_TYPE_SOURCE);
05a67631
PP
963 fprintf(stderr, "%s%sCannot find component class %s",
964 bt_common_color_bold(),
965 bt_common_color_fg_red(),
966 bt_common_color_reset());
db0f160a
PP
967 print_plugin_comp_cls_opt(stderr, plugin_name,
968 comp_cls_name, comp_cls_type);
05a67631
PP
969 fprintf(stderr, "\n");
970 ret = -1;
971 goto end;
972 }
973
05a67631
PP
974 params = bt_value_map_create();
975 if (!params) {
976 ret = -1;
977 goto end;
978 }
979
db0f160a
PP
980 ret = bt_value_map_insert_string(params, "path",
981 cfg->cmd_data.print_ctf_metadata.path->str);
05a67631
PP
982 if (ret) {
983 ret = -1;
984 goto end;
985 }
986
a67681c1 987 results = bt_component_class_query(comp_cls, "metadata-info",
05a67631
PP
988 params);
989 if (!results) {
990 ret = -1;
7213a328 991 BT_LOGE_STR("Failed to query for metadata info.");
a67681c1 992 fprintf(stderr, "%s%sFailed to request metadata info%s\n",
05a67631
PP
993 bt_common_color_bold(),
994 bt_common_color_fg_red(),
995 bt_common_color_reset());
996 goto end;
997 }
998
999 metadata_text_value = bt_value_map_get(results, "text");
1000 if (!metadata_text_value) {
7213a328 1001 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
05a67631
PP
1002 ret = -1;
1003 goto end;
1004 }
1005
1006 ret = bt_value_string_get(metadata_text_value, &metadata_text);
1007 assert(ret == 0);
1008 printf("%s\n", metadata_text);
1009
1010end:
1011 bt_put(results);
05a67631
PP
1012 bt_put(params);
1013 bt_put(metadata_text_value);
1014 bt_put(comp_cls);
05a67631
PP
1015 return 0;
1016}
1017
9009cc24
PP
1018struct cmd_run_ctx {
1019 /* Owned by this */
1020 GHashTable *components;
1021
1022 /* Owned by this */
1023 struct bt_graph *graph;
1024
1025 /* Weak */
1026 struct bt_config *cfg;
1027
1028 bool connect_ports;
1029};
1030
1031static
1032int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1033 struct cmd_run_ctx *ctx, struct bt_component *upstream_comp,
1034 struct bt_port *upstream_port,
1035 struct bt_config_connection *cfg_conn)
290725f7
PP
1036{
1037 int ret = 0;
9009cc24
PP
1038 GQuark downstreamp_comp_name_quark;
1039 struct bt_component *downstream_comp;
1040 int64_t downstream_port_count;
1041 uint64_t i;
1042 int64_t (*port_count_fn)(struct bt_component *);
1043 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
1044 void *conn = NULL;
1045
7213a328
PP
1046 BT_LOGI("Connecting upstream port to the next available downstream port: "
1047 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1048 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1049 upstream_port, bt_port_get_name(upstream_port),
1050 cfg_conn->downstream_comp_name->str,
1051 cfg_conn->arg->str);
9009cc24
PP
1052 downstreamp_comp_name_quark = g_quark_from_string(
1053 cfg_conn->downstream_comp_name->str);
1054 assert(downstreamp_comp_name_quark > 0);
1055 downstream_comp = g_hash_table_lookup(ctx->components,
1056 (gpointer) (long) downstreamp_comp_name_quark);
1057 if (!downstream_comp) {
7213a328
PP
1058 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1059 "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
1060 cfg_conn->arg->str);
9009cc24
PP
1061 fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n",
1062 cfg_conn->arg->str);
1063 goto error;
1064 }
1065
1066 if (bt_component_is_filter(downstream_comp)) {
1067 port_count_fn = bt_component_filter_get_input_port_count;
1068 port_by_index_fn = bt_component_filter_get_input_port_by_index;
1069 } else if (bt_component_is_sink(downstream_comp)) {
1070 port_count_fn = bt_component_sink_get_input_port_count;
1071 port_by_index_fn = bt_component_sink_get_input_port_by_index;
1072 } else {
1073 /*
1074 * Should never happen because the connections are
1075 * validated before we get here.
1076 */
7213a328
PP
1077 BT_LOGF("Invalid connection: downstream component is a source: "
1078 "conn-arg=\"%s\"", cfg_conn->arg->str);
9009cc24
PP
1079 assert(false);
1080 }
290725f7 1081
9009cc24
PP
1082 downstream_port_count = port_count_fn(downstream_comp);
1083 assert(downstream_port_count >= 0);
1084
1085 for (i = 0; i < downstream_port_count; i++) {
1086 struct bt_port *downstream_port =
1087 port_by_index_fn(downstream_comp, i);
1088 const char *downstream_port_name;
1089
1090 assert(downstream_port);
1091
1092 /* Skip port if it's already connected */
1093 if (bt_port_is_connected(downstream_port)) {
1094 bt_put(downstream_port);
7213a328
PP
1095 BT_LOGD("Skipping downstream port: already connected: "
1096 "port-addr=%p, port-name=\"%s\"",
1097 downstream_port,
1098 bt_port_get_name(downstream_port));
9009cc24
PP
1099 continue;
1100 }
1101
1102 downstream_port_name = bt_port_get_name(downstream_port);
1103 assert(downstream_port_name);
1104
1105 if (bt_common_star_glob_match(
1106 cfg_conn->downstream_port_glob->str, -1ULL,
1107 downstream_port_name, -1ULL)) {
1108 /* We have a winner! */
1109 conn = bt_graph_connect_ports(ctx->graph,
1110 upstream_port, downstream_port);
1111 bt_put(downstream_port);
1112 if (!conn) {
7213a328
PP
1113 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1114 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1115 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1116 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1117 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1118 "conn-arg=\"%s\"",
1119 upstream_comp, bt_component_get_name(upstream_comp),
1120 upstream_port, bt_port_get_name(upstream_port),
1121 downstream_comp, cfg_conn->downstream_comp_name->str,
1122 downstream_port, downstream_port_name,
1123 cfg_conn->arg->str);
9009cc24
PP
1124 fprintf(stderr,
1125 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1126 bt_port_get_name(upstream_port),
1127 downstream_port_name,
1128 cfg_conn->arg->str);
1129 goto error;
1130 }
1131
7213a328
PP
1132 BT_LOGI("Connected component ports: "
1133 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1134 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1135 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1136 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1137 "conn-arg=\"%s\"",
1138 upstream_comp, bt_component_get_name(upstream_comp),
1139 upstream_port, bt_port_get_name(upstream_port),
1140 downstream_comp, cfg_conn->downstream_comp_name->str,
1141 downstream_port, downstream_port_name,
1142 cfg_conn->arg->str);
1143
9009cc24
PP
1144 goto end;
1145 }
1146
1147 bt_put(downstream_port);
1148 }
1149
1150 if (!conn) {
7213a328
PP
1151 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1152 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1153 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1154 upstream_port, bt_port_get_name(upstream_port),
1155 cfg_conn->downstream_comp_name->str,
1156 cfg_conn->arg->str);
9009cc24
PP
1157 fprintf(stderr,
1158 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1159 bt_port_get_name(upstream_port), cfg_conn->arg->str);
1160 goto error;
05a67631
PP
1161 }
1162
9009cc24
PP
1163 goto end;
1164
1165error:
1166 ret = -1;
1167
1168end:
1169 bt_put(conn);
1170 return ret;
1171}
1172
1173static
1174int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
1175 struct bt_port *upstream_port)
1176{
1177 int ret = 0;
1178 const char *upstream_port_name;
1179 const char *upstream_comp_name;
1180 struct bt_component *upstream_comp = NULL;
1181 size_t i;
1182
1183 assert(ctx);
1184 assert(upstream_port);
1185 upstream_port_name = bt_port_get_name(upstream_port);
1186 assert(upstream_port_name);
1187 upstream_comp = bt_port_get_component(upstream_port);
1188 if (!upstream_comp) {
7213a328
PP
1189 BT_LOGW("Upstream port to connect is not part of a component: "
1190 "port-addr=%p, port-name=\"%s\"",
1191 upstream_port, upstream_port_name);
98ecef32
MD
1192 ret = -1;
1193 goto end;
33bceaf8
JG
1194 }
1195
9009cc24
PP
1196 upstream_comp_name = bt_component_get_name(upstream_comp);
1197 assert(upstream_comp_name);
7213a328
PP
1198 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1199 "port-addr=%p, port-name=\"%s\"",
1200 upstream_comp, upstream_comp_name,
1201 upstream_port, upstream_port_name);
9009cc24
PP
1202
1203 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1204 struct bt_config_connection *cfg_conn =
1205 g_ptr_array_index(
1206 ctx->cfg->cmd_data.run.connections, i);
1207
1208 if (strcmp(cfg_conn->upstream_comp_name->str,
1209 upstream_comp_name) == 0) {
1210 if (bt_common_star_glob_match(
1211 cfg_conn->upstream_port_glob->str,
1212 -1ULL, upstream_port_name, -1ULL)) {
1213 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1214 ctx, upstream_comp, upstream_port,
1215 cfg_conn);
1216 if (ret) {
7213a328
PP
1217 BT_LOGE("Cannot connect upstream port: "
1218 "port-addr=%p, port-name=\"%s\"",
1219 upstream_port,
1220 upstream_port_name);
9009cc24
PP
1221 fprintf(stderr,
1222 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1223 upstream_port_name,
1224 upstream_comp_name,
1225 cfg_conn->arg->str);
1226 goto error;
1227 }
1228
1229 goto end;
1230 }
1231 }
1232 }
1233
7213a328
PP
1234 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1235 "port-addr=%p, port-name=\"%s\"", upstream_port,
1236 upstream_port_name);
9009cc24
PP
1237 fprintf(stderr,
1238 "Cannot create connection: upstream port `%s` does not match any connection\n",
7213a328 1239 upstream_port_name);
9009cc24
PP
1240
1241error:
1242 ret = -1;
1243
1244end:
1245 bt_put(upstream_comp);
1246 return ret;
1247}
1248
1249static
1250void graph_port_added_listener(struct bt_port *port, void *data)
1251{
1252 struct bt_component *comp = NULL;
1253 struct cmd_run_ctx *ctx = data;
1254
7213a328
PP
1255 BT_LOGI("Port added to a graph's component: port-addr=%p, port-name=\"%s\"",
1256 port, bt_port_get_name(port));
1257
9009cc24 1258 if (bt_port_is_connected(port)) {
7213a328 1259 BT_LOGW_STR("Port is already connected.");
56a1cced
JG
1260 goto end;
1261 }
7c7c0433 1262
9009cc24
PP
1263 comp = bt_port_get_component(port);
1264 if (!comp) {
7213a328 1265 BT_LOGW_STR("Port has no component.");
7c7c0433
JG
1266 goto end;
1267 }
1268
9009cc24 1269 if (!bt_port_is_output(port)) {
7213a328 1270 BT_LOGI_STR("Skipping input port.");
61ddbc8a
JG
1271 goto end;
1272 }
1273
9009cc24 1274 if (cmd_run_ctx_connect_upstream_port(ctx, port)) {
7213a328 1275 BT_LOGF_STR("Cannot connect upstream port.");
9009cc24
PP
1276 fprintf(stderr, "Added port could not be connected: aborting\n");
1277 abort();
1278 }
1279
1280end:
1281 bt_put(comp);
1282 return;
1283}
1284
1285static
1286void graph_port_removed_listener(struct bt_component *component,
1287 struct bt_port *port, void *data)
1288{
7213a328
PP
1289 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1290 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1291 component, bt_component_get_name(component),
1292 port, bt_port_get_name(port));
9009cc24
PP
1293}
1294
1295static
1296void graph_ports_connected_listener(struct bt_port *upstream_port,
1297 struct bt_port *downstream_port, void *data)
1298{
7213a328
PP
1299 BT_LOGI("Graph's component ports connected: "
1300 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1301 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1302 upstream_port, bt_port_get_name(upstream_port),
1303 downstream_port, bt_port_get_name(downstream_port));
9009cc24
PP
1304}
1305
1306static
1307void graph_ports_disconnected_listener(
1308 struct bt_component *upstream_component,
1309 struct bt_component *downstream_component,
1310 struct bt_port *upstream_port, struct bt_port *downstream_port,
1311 void *data)
1312{
7213a328
PP
1313 BT_LOGI("Graph's component ports disconnected: "
1314 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1315 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1316 upstream_port, bt_port_get_name(upstream_port),
1317 downstream_port, bt_port_get_name(downstream_port));
9009cc24
PP
1318}
1319
1320static
1321void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
1322{
1323 if (!ctx) {
1324 return;
1325 }
1326
1327 if (ctx->components) {
1328 g_hash_table_destroy(ctx->components);
1329 ctx->components = NULL;
1330 }
1331
1332 BT_PUT(ctx->graph);
5401f780 1333 the_graph = NULL;
9009cc24
PP
1334 ctx->cfg = NULL;
1335}
1336
1337static
1338int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
1339{
1340 int ret = 0;
1341
1342 ctx->cfg = cfg;
1343 ctx->connect_ports = false;
1344 ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1345 NULL, bt_put);
1346 if (!ctx->components) {
1347 goto error;
1348 }
1349
1350 ctx->graph = bt_graph_create();
1351 if (!ctx->graph) {
1352 goto error;
1353 }
1354
5401f780 1355 the_graph = ctx->graph;
9009cc24
PP
1356 ret = bt_graph_add_port_added_listener(ctx->graph,
1357 graph_port_added_listener, ctx);
1358 if (ret) {
1359 goto error;
1360 }
1361
1362 ret = bt_graph_add_port_removed_listener(ctx->graph,
1363 graph_port_removed_listener, ctx);
1364 if (ret) {
1365 goto error;
1366 }
1367
1368 ret = bt_graph_add_ports_connected_listener(ctx->graph,
1369 graph_ports_connected_listener, ctx);
1370 if (ret) {
1371 goto error;
1372 }
1373
1374 ret = bt_graph_add_ports_disconnected_listener(ctx->graph,
1375 graph_ports_disconnected_listener, ctx);
1376 if (ret) {
1377 goto error;
1378 }
1379
1380 goto end;
1381
1382error:
1383 cmd_run_ctx_destroy(ctx);
1384 ret = -1;
1385
1386end:
1387 return ret;
1388}
1389
1390static
1391int cmd_run_ctx_create_components_from_config_components(
1392 struct cmd_run_ctx *ctx, GPtrArray *cfg_components)
1393{
1394 size_t i;
1395 struct bt_component_class *comp_cls = NULL;
1396 struct bt_component *comp = NULL;
1397 int ret = 0;
1398
1399 for (i = 0; i < cfg_components->len; i++) {
1400 struct bt_config_component *cfg_comp =
1401 g_ptr_array_index(cfg_components, i);
1402 GQuark quark;
1403
1404 comp_cls = find_component_class(cfg_comp->plugin_name->str,
1405 cfg_comp->comp_cls_name->str, cfg_comp->type);
1406 if (!comp_cls) {
7213a328
PP
1407 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1408 "comp-cls-name=\"%s\", comp-cls-type=%d",
1409 cfg_comp->plugin_name->str,
1410 cfg_comp->comp_cls_name->str,
1411 cfg_comp->type);
9009cc24
PP
1412 fprintf(stderr, "%s%sCannot find component class %s",
1413 bt_common_color_bold(),
1414 bt_common_color_fg_red(),
1415 bt_common_color_reset());
1416 print_plugin_comp_cls_opt(stderr,
1417 cfg_comp->plugin_name->str,
1418 cfg_comp->comp_cls_name->str,
1419 cfg_comp->type);
1420 fprintf(stderr, "\n");
1421 goto error;
1422 }
1423
1424 comp = bt_component_create(comp_cls,
1425 cfg_comp->instance_name->str, cfg_comp->params);
1426 if (!comp) {
7213a328
PP
1427 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
1428 "comp-cls-name=\"%s\", comp-cls-type=%d",
1429 "comp-name=\"%s\"",
1430 cfg_comp->plugin_name->str,
1431 cfg_comp->comp_cls_name->str,
1432 cfg_comp->type, cfg_comp->instance_name->str);
9009cc24
PP
1433 fprintf(stderr, "%s%sCannot create component `%s`%s\n",
1434 bt_common_color_bold(),
1435 bt_common_color_fg_red(),
1436 cfg_comp->instance_name->str,
1437 bt_common_color_reset());
1438 goto error;
1439 }
1440
7213a328
PP
1441 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1442 comp, cfg_comp->instance_name->str);
9009cc24
PP
1443 quark = g_quark_from_string(cfg_comp->instance_name->str);
1444 assert(quark > 0);
1445 g_hash_table_insert(ctx->components,
1446 (gpointer) (long) quark, comp);
1447 comp = NULL;
1448 BT_PUT(comp_cls);
1449 }
1450
1451 goto end;
1452
1453error:
1454 ret = -1;
1455
1456end:
1457 bt_put(comp);
1458 bt_put(comp_cls);
1459 return ret;
1460}
56a1cced 1461
9009cc24
PP
1462static
1463int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx)
1464{
1465 int ret = 0;
1466
1467 /*
1468 * Make sure that, during this phase, our graph's "port added"
1469 * listener does not connect ports while we are creating the
1470 * components because we have a special, initial phase for
1471 * this.
1472 */
1473 ctx->connect_ports = false;
1474
1475 ret = cmd_run_ctx_create_components_from_config_components(
1476 ctx, ctx->cfg->cmd_data.run.sources);
1477 if (ret) {
7c7c0433 1478 ret = -1;
2e339de1
JG
1479 goto end;
1480 }
1481
9009cc24
PP
1482 ret = cmd_run_ctx_create_components_from_config_components(
1483 ctx, ctx->cfg->cmd_data.run.filters);
6c2f3ee5 1484 if (ret) {
290725f7 1485 ret = -1;
fec2a9f2
JG
1486 goto end;
1487 }
78586d8a 1488
9009cc24
PP
1489 ret = cmd_run_ctx_create_components_from_config_components(
1490 ctx, ctx->cfg->cmd_data.run.sinks);
1491 if (ret) {
1492 ret = -1;
1493 goto end;
1494 }
1495
1496end:
1497 return ret;
1498}
1499
1500static
1501int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx,
1502 struct bt_component *comp,
1503 int64_t (*port_count_fn)(struct bt_component *),
1504 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t))
1505{
1506 int ret = 0;
1507 int64_t count;
1508 uint64_t i;
1509
1510 count = port_count_fn(comp);
1511 assert(count >= 0);
1512
1513 for (i = 0; i < count; i++) {
1514 struct bt_port *upstream_port = port_by_index_fn(comp, i);
1515
1516 assert(upstream_port);
1517 ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port);
1518 bt_put(upstream_port);
1519 if (ret) {
1520 goto end;
1521 }
1522 }
1523
1524end:
1525 return ret;
1526}
1527
1528static
1529int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx)
1530{
1531 int ret = 0;
1532 GHashTableIter iter;
1533 gpointer g_name_quark, g_comp;
1534
1535 ctx->connect_ports = true;
1536 g_hash_table_iter_init(&iter, ctx->components);
1537
1538 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
1539 if (bt_component_is_source(g_comp)) {
1540 ret = cmd_run_ctx_connect_comp_ports(ctx,
1541 g_comp, bt_component_source_get_output_port_count,
1542 bt_component_source_get_output_port_by_index);
1543 } else if (bt_component_is_filter(g_comp)) {
1544 ret = cmd_run_ctx_connect_comp_ports(ctx,
1545 g_comp, bt_component_filter_get_output_port_count,
1546 bt_component_filter_get_output_port_by_index);
1547 }
1548
1549 if (ret) {
1550 goto end;
1551 }
1552 }
1553
1554end:
1555 return ret;
1556}
1557
1558static
1559int cmd_run(struct bt_config *cfg)
1560{
1561 int ret = 0;
1562 struct cmd_run_ctx ctx = { 0 };
1563
9009cc24
PP
1564 /* Initialize the command's context and the graph object */
1565 if (cmd_run_ctx_init(&ctx, cfg)) {
7213a328 1566 BT_LOGE_STR("Cannot initialize the command's context.");
9009cc24
PP
1567 fprintf(stderr, "Cannot initialize the command's context\n");
1568 goto error;
1569 }
1570
1571 /* Create the requested component instances */
1572 if (cmd_run_ctx_create_components(&ctx)) {
7213a328 1573 BT_LOGE_STR("Cannot create components.");
9009cc24
PP
1574 fprintf(stderr, "Cannot create components\n");
1575 goto error;
1576 }
1577
1578 /* Connect the initially visible component ports */
1579 if (cmd_run_ctx_connect_ports(&ctx)) {
7213a328 1580 BT_LOGE_STR("Cannot connect initial component ports.");
9009cc24
PP
1581 fprintf(stderr, "Cannot connect initial component ports\n");
1582 goto error;
1583 }
1584
5401f780
PP
1585 if (canceled) {
1586 goto end;
1587 }
1588
7213a328
PP
1589 BT_LOGI_STR("Running the graph.");
1590
9009cc24 1591 /* Run the graph */
fec2a9f2 1592 while (true) {
9009cc24 1593 enum bt_graph_status graph_status = bt_graph_run(ctx.graph);
61ddbc8a 1594
61ddbc8a 1595 switch (graph_status) {
9009cc24
PP
1596 case BT_GRAPH_STATUS_OK:
1597 break;
5401f780
PP
1598 case BT_GRAPH_STATUS_CANCELED:
1599 BT_LOGI("Graph was canceled by user: status=%d",
1600 graph_status);
1601 goto error;
61ddbc8a 1602 case BT_GRAPH_STATUS_AGAIN:
5401f780
PP
1603 if (bt_graph_is_canceled(ctx.graph)) {
1604 BT_LOGI("Graph was canceled by user: status=%d",
1605 graph_status);
1606 goto error;
1607 }
1608
9009cc24 1609 if (cfg->cmd_data.run.retry_duration_us > 0) {
7213a328
PP
1610 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1611 "time-us=%" PRIu64,
1612 cfg->cmd_data.run.retry_duration_us);
1613
9009cc24
PP
1614 if (usleep(cfg->cmd_data.run.retry_duration_us)) {
1615 // TODO: check EINTR and signal handler
1616 }
1617 }
78586d8a 1618 break;
fec2a9f2
JG
1619 case BT_COMPONENT_STATUS_END:
1620 goto end;
1621 default:
7213a328
PP
1622 BT_LOGE_STR("Graph failed to complete successfully");
1623 fprintf(stderr, "Graph failed to complete successfully\n");
9009cc24 1624 goto error;
78586d8a 1625 }
fec2a9f2 1626 }
290725f7 1627
9009cc24
PP
1628 goto end;
1629
1630error:
1631 if (ret == 0) {
1632 ret = -1;
1633 }
1634
11e1d048 1635end:
9009cc24 1636 cmd_run_ctx_destroy(&ctx);
290725f7
PP
1637 return ret;
1638}
1639
9009cc24
PP
1640static
1641void warn_command_name_and_directory_clash(struct bt_config *cfg)
290725f7 1642{
9009cc24
PP
1643 const char *env_clash;
1644
290725f7
PP
1645 if (!cfg->command_name) {
1646 return;
1647 }
1648
9009cc24
PP
1649 env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH);
1650 if (env_clash && strcmp(env_clash, "0") == 0) {
1651 return;
1652 }
1653
290725f7
PP
1654 if (g_file_test(cfg->command_name,
1655 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
1656 fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1657 cfg->command_name);
1658 fprintf(stderr, "trace located in the local `%s` directory, please use:\n",
1659 cfg->command_name);
1660 fprintf(stderr, "\n");
1661 fprintf(stderr, " babeltrace convert %s [OPTIONS]\n",
1662 cfg->command_name);
1663 }
1664}
1665
7213a328
PP
1666static
1667void init_log_level(void)
1668{
1669 enum bt_logging_level log_level = BT_LOG_NONE;
1670 const char *log_level_env = getenv("BABELTRACE_CLI_LOG_LEVEL");
1671
1672 if (!log_level_env) {
1673 goto set_level;
1674 }
1675
1676 if (strcmp(log_level_env, "VERBOSE") == 0) {
1677 log_level = BT_LOGGING_LEVEL_VERBOSE;
1678 } else if (strcmp(log_level_env, "DEBUG") == 0) {
1679 log_level = BT_LOGGING_LEVEL_DEBUG;
1680 } else if (strcmp(log_level_env, "INFO") == 0) {
1681 log_level = BT_LOGGING_LEVEL_INFO;
1682 } else if (strcmp(log_level_env, "WARN") == 0) {
1683 log_level = BT_LOGGING_LEVEL_WARN;
1684 } else if (strcmp(log_level_env, "ERROR") == 0) {
1685 log_level = BT_LOGGING_LEVEL_ERROR;
1686 } else if (strcmp(log_level_env, "FATAL") == 0) {
1687 log_level = BT_LOGGING_LEVEL_FATAL;
1688 } else if (strcmp(log_level_env, "NONE") == 0) {
1689 log_level = BT_LOGGING_LEVEL_NONE;
1690 }
1691
1692set_level:
1693 bt_cli_log_level = log_level;
1694}
1695
5401f780
PP
1696void set_sigint_handler(void)
1697{
1698 struct sigaction new_action, old_action;
1699
1700 new_action.sa_handler = sigint_handler;
1701 sigemptyset(&new_action.sa_mask);
1702 new_action.sa_flags = 0;
1703 sigaction(SIGINT, NULL, &old_action);
1704
1705 if (old_action.sa_handler != SIG_IGN) {
1706 sigaction(SIGINT, &new_action, NULL);
1707 }
1708}
1709
290725f7
PP
1710int main(int argc, const char **argv)
1711{
1712 int ret;
1713 int retcode;
1714 struct bt_config *cfg;
1715
7213a328 1716 init_log_level();
5401f780 1717 set_sigint_handler();
9009cc24
PP
1718 init_static_data();
1719 cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode);
290725f7
PP
1720
1721 if (retcode < 0) {
1722 /* Quit without errors; typically usage/version */
1723 retcode = 0;
7213a328 1724 BT_LOGI_STR("Quitting without errors.");
290725f7
PP
1725 goto end;
1726 }
1727
1728 if (retcode > 0) {
7213a328 1729 BT_LOGE("Command-line error: retcode=%d", retcode);
290725f7
PP
1730 goto end;
1731 }
1732
1733 if (!cfg) {
7213a328 1734 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
290725f7 1735 fprintf(stderr, "Failed to create Babeltrace configuration\n");
db0f160a 1736 retcode = 1;
290725f7
PP
1737 goto end;
1738 }
1739
7213a328
PP
1740 if (cfg->verbose) {
1741 bt_cli_log_level = BT_LOGGING_LEVEL_VERBOSE;
1742 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE);
1743 // TODO: for backward compat., set the log level
1744 // environment variables of the known plugins
1745 // to VERBOSE
1746 } else if (cfg->debug) {
1747 bt_cli_log_level = BT_LOGGING_LEVEL_DEBUG;
1748 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG);
1749 // TODO: for backward compat., set the log level
1750 // environment variables of the known plugins
1751 // to DEBUG
1752 }
1753
290725f7
PP
1754 babeltrace_debug = cfg->debug;
1755 babeltrace_verbose = cfg->verbose;
1756 print_cfg(cfg);
1757
db0f160a
PP
1758 if (cfg->command_needs_plugins) {
1759 ret = load_all_plugins(cfg->plugin_paths);
1760 if (ret) {
7213a328 1761 BT_LOGE("Failed to load plugins: ret=%d", ret);
db0f160a
PP
1762 retcode = 1;
1763 goto end;
1764 }
1765 }
1766
7213a328
PP
1767 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
1768 cfg->command, cfg->command_name);
1769
290725f7 1770 switch (cfg->command) {
db0f160a
PP
1771 case BT_CONFIG_COMMAND_RUN:
1772 ret = cmd_run(cfg);
290725f7
PP
1773 break;
1774 case BT_CONFIG_COMMAND_LIST_PLUGINS:
1775 ret = cmd_list_plugins(cfg);
1776 break;
22e22462
PP
1777 case BT_CONFIG_COMMAND_HELP:
1778 ret = cmd_help(cfg);
1779 break;
a67681c1
PP
1780 case BT_CONFIG_COMMAND_QUERY:
1781 ret = cmd_query(cfg);
63ce0e1d 1782 break;
db0f160a
PP
1783 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
1784 ret = cmd_print_ctf_metadata(cfg);
1785 break;
1786 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
1787 ret = cmd_print_lttng_live_sessions(cfg);
1788 break;
290725f7 1789 default:
7213a328 1790 BT_LOGF("Invalid command: cmd=%d", cfg->command);
290725f7
PP
1791 assert(false);
1792 }
1793
d26ef3e3
PP
1794 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
1795 cfg->command, cfg->command_name, ret);
290725f7
PP
1796 warn_command_name_and_directory_clash(cfg);
1797 retcode = ret ? 1 : 0;
1798
1799end:
1800 BT_PUT(cfg);
9009cc24 1801 fini_static_data();
290725f7 1802 return retcode;
4c8bfb7e 1803}
This page took 0.128135 seconds and 4 git commands to generate.