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