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