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