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