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