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