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