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