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