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