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