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