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