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