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