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