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