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> |
a8ff38ef | 34 | #include <unistd.h> |
34ac0e6c | 35 | #include <stdlib.h> |
7f26a816 PP |
36 | #include <popt.h> |
37 | #include <string.h> | |
38 | #include <stdio.h> | |
33b34c43 | 39 | #include <glib.h> |
dc3fffef | 40 | #include <inttypes.h> |
7cdc2bab | 41 | #include <unistd.h> |
5401f780 | 42 | #include <signal.h> |
c42c79ea | 43 | #include "babeltrace-cfg.h" |
9009cc24 PP |
44 | #include "babeltrace-cfg-cli-args.h" |
45 | #include "babeltrace-cfg-cli-args-default.h" | |
46 | ||
47 | #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH" | |
c6d4d1ae | 48 | #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL" |
75a2cb9b | 49 | #define NSEC_PER_SEC 1000000000LL |
34ac0e6c | 50 | |
fc11b6a6 PP |
51 | /* |
52 | * Known environment variable names for the log levels of the project's | |
53 | * modules. | |
54 | */ | |
55 | static const char* log_level_env_var_names[] = { | |
b4565e8b | 56 | "BABELTRACE_COMMON_LOG_LEVEL", |
2941181d | 57 | "BABELTRACE_COMPAT_LOG_LEVEL", |
fc11b6a6 | 58 | "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL", |
2fad3ae7 PP |
59 | "BABELTRACE_SINK_CTF_FS_LOG_LEVEL", |
60 | "BABELTRACE_SRC_CTF_FS_LOG_LEVEL", | |
61 | "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL", | |
fc11b6a6 PP |
62 | "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL", |
63 | "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL", | |
fdad611f | 64 | "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL", |
2fad3ae7 PP |
65 | "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL", |
66 | "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL", | |
67 | "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL", | |
68 | "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL", | |
69 | "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL", | |
fdad611f | 70 | "BABELTRACE_PYTHON_BT2_LOG_LEVEL", |
fc11b6a6 PP |
71 | "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL", |
72 | NULL, | |
73 | }; | |
74 | ||
5401f780 PP |
75 | /* Application's processing graph (weak) */ |
76 | static struct bt_graph *the_graph; | |
c7eee084 | 77 | static struct bt_query_executor *the_query_executor; |
5401f780 PP |
78 | static bool canceled = false; |
79 | ||
33b34c43 PP |
80 | GPtrArray *loaded_plugins; |
81 | ||
65d3198f MJ |
82 | #ifdef __MINGW32__ |
83 | #include <windows.h> | |
84 | ||
85 | static | |
86 | BOOL WINAPI signal_handler(DWORD signal) { | |
87 | if (the_graph) { | |
88 | bt_graph_cancel(the_graph); | |
89 | } | |
90 | ||
91 | canceled = true; | |
92 | ||
93 | return TRUE; | |
94 | } | |
95 | ||
5401f780 | 96 | static |
65d3198f MJ |
97 | void set_signal_handler(void) |
98 | { | |
99 | if (!SetConsoleCtrlHandler(signal_handler, TRUE)) { | |
100 | BT_LOGE("Failed to set the ctrl+c handler."); | |
101 | } | |
102 | } | |
103 | #else /* __MINGW32__ */ | |
104 | static | |
105 | void signal_handler(int signum) | |
5401f780 PP |
106 | { |
107 | if (signum != SIGINT) { | |
108 | return; | |
109 | } | |
110 | ||
111 | if (the_graph) { | |
112 | bt_graph_cancel(the_graph); | |
113 | } | |
114 | ||
c7eee084 PP |
115 | if (the_query_executor) { |
116 | bt_query_executor_cancel(the_query_executor); | |
117 | } | |
118 | ||
5401f780 PP |
119 | canceled = true; |
120 | } | |
121 | ||
65d3198f MJ |
122 | static |
123 | void set_signal_handler(void) | |
124 | { | |
125 | struct sigaction new_action, old_action; | |
126 | ||
127 | new_action.sa_handler = signal_handler; | |
128 | sigemptyset(&new_action.sa_mask); | |
129 | new_action.sa_flags = 0; | |
130 | sigaction(SIGINT, NULL, &old_action); | |
131 | ||
132 | if (old_action.sa_handler != SIG_IGN) { | |
133 | sigaction(SIGINT, &new_action, NULL); | |
134 | } | |
135 | } | |
136 | #endif /* __MINGW32__ */ | |
137 | ||
33b34c43 | 138 | static |
9009cc24 | 139 | void init_static_data(void) |
33b34c43 | 140 | { |
65300d60 | 141 | loaded_plugins = g_ptr_array_new_with_free_func(bt_object_put_ref); |
33b34c43 PP |
142 | } |
143 | ||
144 | static | |
9009cc24 | 145 | void fini_static_data(void) |
33b34c43 PP |
146 | { |
147 | g_ptr_array_free(loaded_plugins, TRUE); | |
148 | } | |
149 | ||
c7eee084 PP |
150 | static |
151 | int create_the_query_executor(void) | |
152 | { | |
153 | int ret = 0; | |
154 | ||
155 | the_query_executor = bt_query_executor_create(); | |
156 | if (!the_query_executor) { | |
157 | BT_LOGE_STR("Cannot create a query executor."); | |
158 | ret = -1; | |
159 | } | |
160 | ||
161 | return ret; | |
162 | } | |
163 | ||
164 | static | |
165 | void destroy_the_query_executor(void) | |
166 | { | |
65300d60 | 167 | BT_OBJECT_PUT_REF_AND_RESET(the_query_executor); |
c7eee084 PP |
168 | } |
169 | ||
170 | static | |
171 | int query(struct bt_component_class *comp_cls, const char *obj, | |
172 | struct bt_value *params, struct bt_value **user_result, | |
173 | const char **fail_reason) | |
174 | { | |
175 | struct bt_value *result = NULL; | |
176 | enum bt_query_status status; | |
177 | *fail_reason = "unknown error"; | |
178 | int ret = 0; | |
179 | ||
f6ccaed9 PP |
180 | BT_ASSERT(fail_reason); |
181 | BT_ASSERT(user_result); | |
c7eee084 PP |
182 | ret = create_the_query_executor(); |
183 | if (ret) { | |
184 | /* create_the_query_executor() logs errors */ | |
185 | goto end; | |
186 | } | |
187 | ||
188 | if (canceled) { | |
189 | BT_LOGI("Canceled by user before executing the query: " | |
190 | "comp-cls-addr=%p, comp-cls-name=\"%s\", " | |
191 | "query-obj=\"%s\"", comp_cls, | |
192 | bt_component_class_get_name(comp_cls), obj); | |
193 | *fail_reason = "canceled by user"; | |
194 | goto error; | |
195 | } | |
196 | ||
197 | while (true) { | |
198 | status = bt_query_executor_query(the_query_executor, comp_cls, | |
199 | obj, params, &result); | |
200 | switch (status) { | |
201 | case BT_QUERY_STATUS_OK: | |
202 | goto ok; | |
203 | case BT_QUERY_STATUS_AGAIN: | |
204 | { | |
205 | const uint64_t sleep_time_us = 100000; | |
206 | ||
207 | /* Wait 100 ms and retry */ | |
208 | BT_LOGV("Got BT_QUERY_STATUS_AGAIN: sleeping: " | |
209 | "time-us=%" PRIu64, sleep_time_us); | |
210 | ||
211 | if (usleep(sleep_time_us)) { | |
212 | if (bt_query_executor_is_canceled(the_query_executor)) { | |
213 | BT_LOGI("Query was canceled by user: " | |
214 | "comp-cls-addr=%p, comp-cls-name=\"%s\", " | |
215 | "query-obj=\"%s\"", comp_cls, | |
216 | bt_component_class_get_name(comp_cls), | |
217 | obj); | |
218 | *fail_reason = "canceled by user"; | |
219 | goto error; | |
220 | } | |
221 | } | |
222 | ||
223 | continue; | |
224 | } | |
225 | case BT_QUERY_STATUS_EXECUTOR_CANCELED: | |
226 | *fail_reason = "canceled by user"; | |
227 | goto error; | |
228 | case BT_QUERY_STATUS_ERROR: | |
229 | case BT_QUERY_STATUS_INVALID: | |
230 | goto error; | |
231 | case BT_QUERY_STATUS_INVALID_OBJECT: | |
232 | *fail_reason = "invalid or unknown query object"; | |
233 | goto error; | |
234 | case BT_QUERY_STATUS_INVALID_PARAMS: | |
235 | *fail_reason = "invalid query parameters"; | |
236 | goto error; | |
237 | case BT_QUERY_STATUS_NOMEM: | |
238 | *fail_reason = "not enough memory"; | |
239 | goto error; | |
240 | default: | |
241 | BT_LOGF("Unknown query status: status=%d", status); | |
242 | abort(); | |
243 | } | |
244 | } | |
245 | ||
246 | ok: | |
247 | *user_result = result; | |
248 | result = NULL; | |
249 | goto end; | |
250 | ||
251 | error: | |
252 | ret = -1; | |
253 | ||
254 | end: | |
255 | destroy_the_query_executor(); | |
65300d60 | 256 | bt_object_put_ref(result); |
c7eee084 PP |
257 | return ret; |
258 | } | |
259 | ||
33b34c43 PP |
260 | static |
261 | struct bt_plugin *find_plugin(const char *name) | |
262 | { | |
263 | int i; | |
264 | struct bt_plugin *plugin = NULL; | |
265 | ||
f6ccaed9 | 266 | BT_ASSERT(name); |
7213a328 PP |
267 | BT_LOGD("Finding plugin: name=\"%s\"", name); |
268 | ||
33b34c43 PP |
269 | for (i = 0; i < loaded_plugins->len; i++) { |
270 | plugin = g_ptr_array_index(loaded_plugins, i); | |
271 | ||
272 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { | |
273 | break; | |
274 | } | |
275 | ||
276 | plugin = NULL; | |
277 | } | |
278 | ||
7213a328 PP |
279 | if (BT_LOG_ON_DEBUG) { |
280 | if (plugin) { | |
281 | BT_LOGD("Found plugin: plugin-addr=%p", plugin); | |
282 | } else { | |
283 | BT_LOGD("Cannot find plugin."); | |
284 | } | |
285 | } | |
286 | ||
65300d60 | 287 | return bt_object_get_ref(plugin); |
33b34c43 PP |
288 | } |
289 | ||
290 | static | |
291 | struct bt_component_class *find_component_class(const char *plugin_name, | |
292 | const char *comp_class_name, | |
d3e4dcd8 | 293 | enum bt_component_class_type comp_class_type) |
33b34c43 PP |
294 | { |
295 | struct bt_component_class *comp_class = NULL; | |
7213a328 PP |
296 | struct bt_plugin *plugin; |
297 | ||
298 | BT_LOGD("Finding component class: plugin-name=\"%s\", " | |
299 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
300 | plugin_name, comp_class_name, comp_class_type); | |
301 | ||
302 | plugin = find_plugin(plugin_name); | |
33b34c43 PP |
303 | |
304 | if (!plugin) { | |
305 | goto end; | |
306 | } | |
307 | ||
308 | comp_class = bt_plugin_get_component_class_by_name_and_type(plugin, | |
309 | comp_class_name, comp_class_type); | |
65300d60 | 310 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
7213a328 | 311 | |
33b34c43 | 312 | end: |
7213a328 PP |
313 | if (BT_LOG_ON_DEBUG) { |
314 | if (comp_class) { | |
315 | BT_LOGD("Found component class: comp-cls-addr=%p", | |
316 | comp_class); | |
317 | } else { | |
318 | BT_LOGD("Cannot find component class."); | |
319 | } | |
320 | } | |
321 | ||
33b34c43 PP |
322 | return comp_class; |
323 | } | |
6c2f3ee5 | 324 | |
c42c79ea | 325 | static |
7213a328 | 326 | void print_indent(FILE *fp, size_t indent) |
c42c79ea PP |
327 | { |
328 | size_t i; | |
329 | ||
330 | for (i = 0; i < indent; i++) { | |
7213a328 | 331 | fprintf(fp, " "); |
c42c79ea PP |
332 | } |
333 | } | |
334 | ||
87796884 PP |
335 | static |
336 | const char *component_type_str(enum bt_component_class_type type) | |
337 | { | |
338 | switch (type) { | |
339 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
340 | return "source"; | |
341 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
342 | return "sink"; | |
343 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
344 | return "filter"; | |
345 | case BT_COMPONENT_CLASS_TYPE_UNKNOWN: | |
346 | default: | |
fd5f8053 | 347 | return "(unknown)"; |
87796884 PP |
348 | } |
349 | } | |
350 | ||
9009cc24 PP |
351 | static |
352 | void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name, | |
87796884 PP |
353 | const char *comp_cls_name, enum bt_component_class_type type) |
354 | { | |
9009cc24 PP |
355 | GString *shell_plugin_name = NULL; |
356 | GString *shell_comp_cls_name = NULL; | |
87796884 | 357 | |
9009cc24 | 358 | shell_plugin_name = bt_common_shell_quote(plugin_name, false); |
87796884 PP |
359 | if (!shell_plugin_name) { |
360 | goto end; | |
361 | } | |
362 | ||
9009cc24 | 363 | shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false); |
87796884 PP |
364 | if (!shell_comp_cls_name) { |
365 | goto end; | |
366 | } | |
367 | ||
fd5f8053 | 368 | fprintf(fh, "'%s%s%s%s.%s%s%s.%s%s%s'", |
87796884 PP |
369 | bt_common_color_bold(), |
370 | bt_common_color_fg_cyan(), | |
371 | component_type_str(type), | |
87796884 | 372 | bt_common_color_fg_default(), |
87796884 | 373 | bt_common_color_fg_blue(), |
9009cc24 | 374 | shell_plugin_name->str, |
87796884 PP |
375 | bt_common_color_fg_default(), |
376 | bt_common_color_fg_yellow(), | |
9009cc24 | 377 | shell_comp_cls_name->str, |
87796884 PP |
378 | bt_common_color_reset()); |
379 | ||
380 | end: | |
9009cc24 PP |
381 | if (shell_plugin_name) { |
382 | g_string_free(shell_plugin_name, TRUE); | |
383 | } | |
384 | ||
385 | if (shell_comp_cls_name) { | |
386 | g_string_free(shell_comp_cls_name, TRUE); | |
387 | } | |
87796884 PP |
388 | } |
389 | ||
c42c79ea | 390 | static |
7213a328 | 391 | void print_value(FILE *, struct bt_value *, size_t); |
c42c79ea | 392 | |
c1081aa6 | 393 | static |
7213a328 PP |
394 | void print_value_rec(FILE *, struct bt_value *, size_t); |
395 | ||
396 | struct print_map_value_data { | |
397 | size_t indent; | |
398 | FILE *fp; | |
399 | }; | |
c1081aa6 | 400 | |
c42c79ea | 401 | static |
c55a9f58 | 402 | bt_bool print_map_value(const char *key, struct bt_value *object, void *data) |
c42c79ea | 403 | { |
7213a328 | 404 | struct print_map_value_data *print_map_value_data = data; |
290725f7 | 405 | |
7213a328 PP |
406 | print_indent(print_map_value_data->fp, print_map_value_data->indent); |
407 | fprintf(print_map_value_data->fp, "%s: ", key); | |
f6ccaed9 | 408 | BT_ASSERT(object); |
290725f7 PP |
409 | |
410 | if (bt_value_is_array(object) && | |
411 | bt_value_array_is_empty(object)) { | |
7213a328 | 412 | fprintf(print_map_value_data->fp, "[ ]\n"); |
290725f7 PP |
413 | return true; |
414 | } | |
415 | ||
416 | if (bt_value_is_map(object) && | |
417 | bt_value_map_is_empty(object)) { | |
7213a328 | 418 | fprintf(print_map_value_data->fp, "{ }\n"); |
290725f7 PP |
419 | return true; |
420 | } | |
c42c79ea | 421 | |
290725f7 PP |
422 | if (bt_value_is_array(object) || |
423 | bt_value_is_map(object)) { | |
7213a328 | 424 | fprintf(print_map_value_data->fp, "\n"); |
290725f7 | 425 | } |
c42c79ea | 426 | |
7213a328 PP |
427 | print_value_rec(print_map_value_data->fp, object, |
428 | print_map_value_data->indent + 2); | |
c55a9f58 | 429 | return BT_TRUE; |
c42c79ea PP |
430 | } |
431 | ||
432 | static | |
7213a328 | 433 | void print_value_rec(FILE *fp, struct bt_value *value, size_t indent) |
c42c79ea | 434 | { |
c55a9f58 | 435 | bt_bool bool_val; |
c42c79ea PP |
436 | int64_t int_val; |
437 | double dbl_val; | |
438 | const char *str_val; | |
439 | int size; | |
440 | int i; | |
60a97734 | 441 | enum bt_value_status status; |
c42c79ea PP |
442 | |
443 | if (!value) { | |
444 | return; | |
445 | } | |
446 | ||
c42c79ea PP |
447 | switch (bt_value_get_type(value)) { |
448 | case BT_VALUE_TYPE_NULL: | |
7213a328 | 449 | fprintf(fp, "%snull%s\n", bt_common_color_bold(), |
c1081aa6 | 450 | bt_common_color_reset()); |
c42c79ea PP |
451 | break; |
452 | case BT_VALUE_TYPE_BOOL: | |
60a97734 MD |
453 | status = bt_value_bool_get(value, &bool_val); |
454 | if (status != BT_VALUE_STATUS_OK) { | |
455 | goto error; | |
456 | } | |
7213a328 | 457 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
c1081aa6 PP |
458 | bt_common_color_fg_cyan(), bool_val ? "yes" : "no", |
459 | bt_common_color_reset()); | |
c42c79ea PP |
460 | break; |
461 | case BT_VALUE_TYPE_INTEGER: | |
60a97734 MD |
462 | status = bt_value_integer_get(value, &int_val); |
463 | if (status != BT_VALUE_STATUS_OK) { | |
464 | goto error; | |
465 | } | |
7213a328 | 466 | fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(), |
c1081aa6 PP |
467 | bt_common_color_fg_red(), int_val, |
468 | bt_common_color_reset()); | |
c42c79ea | 469 | break; |
a373bf69 PP |
470 | case BT_VALUE_TYPE_REAL: |
471 | status = bt_value_real_get(value, &dbl_val); | |
60a97734 MD |
472 | if (status != BT_VALUE_STATUS_OK) { |
473 | goto error; | |
474 | } | |
7213a328 | 475 | fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(), |
c1081aa6 PP |
476 | bt_common_color_fg_red(), dbl_val, |
477 | bt_common_color_reset()); | |
c42c79ea PP |
478 | break; |
479 | case BT_VALUE_TYPE_STRING: | |
60a97734 MD |
480 | status = bt_value_string_get(value, &str_val); |
481 | if (status != BT_VALUE_STATUS_OK) { | |
482 | goto error; | |
483 | } | |
7213a328 | 484 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
c1081aa6 PP |
485 | bt_common_color_fg_green(), str_val, |
486 | bt_common_color_reset()); | |
c42c79ea PP |
487 | break; |
488 | case BT_VALUE_TYPE_ARRAY: | |
07208d85 | 489 | size = bt_value_array_get_size(value); |
60a97734 MD |
490 | if (size < 0) { |
491 | goto error; | |
492 | } | |
290725f7 PP |
493 | |
494 | if (size == 0) { | |
7213a328 PP |
495 | print_indent(fp, indent); |
496 | fprintf(fp, "[ ]\n"); | |
290725f7 PP |
497 | break; |
498 | } | |
c42c79ea PP |
499 | |
500 | for (i = 0; i < size; i++) { | |
501 | struct bt_value *element = | |
07208d85 PP |
502 | bt_value_array_borrow_element_by_index( |
503 | value, i); | |
c42c79ea | 504 | |
60a97734 MD |
505 | if (!element) { |
506 | goto error; | |
507 | } | |
7213a328 PP |
508 | print_indent(fp, indent); |
509 | fprintf(fp, "- "); | |
290725f7 PP |
510 | |
511 | if (bt_value_is_array(element) && | |
512 | bt_value_array_is_empty(element)) { | |
7213a328 | 513 | fprintf(fp, "[ ]\n"); |
290725f7 PP |
514 | continue; |
515 | } | |
516 | ||
517 | if (bt_value_is_map(element) && | |
518 | bt_value_map_is_empty(element)) { | |
7213a328 | 519 | fprintf(fp, "{ }\n"); |
290725f7 PP |
520 | continue; |
521 | } | |
522 | ||
523 | if (bt_value_is_array(element) || | |
524 | bt_value_is_map(element)) { | |
7213a328 | 525 | fprintf(fp, "\n"); |
290725f7 PP |
526 | } |
527 | ||
7213a328 | 528 | print_value_rec(fp, element, indent + 2); |
c42c79ea | 529 | } |
c42c79ea PP |
530 | break; |
531 | case BT_VALUE_TYPE_MAP: | |
7213a328 PP |
532 | { |
533 | struct print_map_value_data data = { | |
534 | .indent = indent, | |
535 | .fp = fp, | |
536 | }; | |
537 | ||
c42c79ea | 538 | if (bt_value_map_is_empty(value)) { |
7213a328 PP |
539 | print_indent(fp, indent); |
540 | fprintf(fp, "{ }\n"); | |
290725f7 | 541 | break; |
c42c79ea PP |
542 | } |
543 | ||
07208d85 | 544 | bt_value_map_foreach_entry(value, print_map_value, &data); |
c42c79ea | 545 | break; |
7213a328 | 546 | } |
c42c79ea | 547 | default: |
0fbb9a9f | 548 | abort(); |
c42c79ea | 549 | } |
60a97734 MD |
550 | return; |
551 | ||
552 | error: | |
553 | BT_LOGE("Error printing value of type %s.", | |
da91b29a | 554 | bt_common_value_type_string(bt_value_get_type(value))); |
c42c79ea PP |
555 | } |
556 | ||
c1081aa6 | 557 | static |
7213a328 | 558 | void print_value(FILE *fp, struct bt_value *value, size_t indent) |
c1081aa6 PP |
559 | { |
560 | if (!bt_value_is_array(value) && !bt_value_is_map(value)) { | |
7213a328 | 561 | print_indent(fp, indent); |
c1081aa6 PP |
562 | } |
563 | ||
7213a328 | 564 | print_value_rec(fp, value, indent); |
c1081aa6 PP |
565 | } |
566 | ||
c42c79ea PP |
567 | static |
568 | void print_bt_config_component(struct bt_config_component *bt_config_component) | |
569 | { | |
7213a328 PP |
570 | fprintf(stderr, " "); |
571 | print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str, | |
db0f160a | 572 | bt_config_component->comp_cls_name->str, |
87796884 | 573 | bt_config_component->type); |
7213a328 | 574 | fprintf(stderr, ":\n"); |
3b6cfcc5 PP |
575 | |
576 | if (bt_config_component->instance_name->len > 0) { | |
7213a328 | 577 | fprintf(stderr, " Name: %s\n", |
3b6cfcc5 PP |
578 | bt_config_component->instance_name->str); |
579 | } | |
580 | ||
7213a328 | 581 | fprintf(stderr, " Parameters:\n"); |
da91b29a PP |
582 | print_value(stderr, |
583 | bt_value_borrow_from_private(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); |
65300d60 | 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 | ||
da91b29a | 611 | print_plugin_paths(bt_value_borrow_from_private(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 | { | |
da91b29a | 642 | print_plugin_paths(bt_value_borrow_from_private(cfg->plugin_paths)); |
290725f7 PP |
643 | } |
644 | ||
c1081aa6 PP |
645 | static |
646 | void print_cfg_help(struct bt_config *cfg) | |
647 | { | |
da91b29a | 648 | print_plugin_paths(bt_value_borrow_from_private(cfg->plugin_paths)); |
db0f160a PP |
649 | } |
650 | ||
651 | static | |
652 | void print_cfg_print_ctf_metadata(struct bt_config *cfg) | |
653 | { | |
da91b29a | 654 | print_plugin_paths(bt_value_borrow_from_private(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 | { | |
da91b29a | 662 | print_plugin_paths(bt_value_borrow_from_private(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 | { |
da91b29a | 670 | print_plugin_paths(bt_value_borrow_from_private(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); | |
f6ccaed9 | 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 | ||
f6ccaed9 | 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)); | |
65300d60 | 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)); | |
65300d60 | 740 | g_ptr_array_add(loaded_plugins, bt_object_get_ref(plugin)); |
33b34c43 | 741 | } |
a8ff38ef | 742 | |
65300d60 | 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 | |
07208d85 | 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; |
1ba2329b | 765 | enum bt_value_status status; |
98ecef32 | 766 | |
07208d85 PP |
767 | plugin_path_value = bt_value_array_borrow_element_by_index( |
768 | plugin_paths, i); | |
1ba2329b 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."); | |
1ba2329b 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); |
65300d60 | 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); |
65300d60 | 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()); | |
a157fea9 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; | |
c7eee084 | 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 | ||
c7eee084 | 922 | ret = query(comp_cls, cfg->cmd_data.query.object->str, |
da91b29a PP |
923 | bt_value_borrow_from_private(cfg->cmd_data.query.cfg_component->params), |
924 | &results, &fail_reason); | |
c7eee084 PP |
925 | if (ret) { |
926 | goto failed; | |
63ce0e1d PP |
927 | } |
928 | ||
7213a328 | 929 | print_value(stdout, results, 0); |
c7eee084 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: | |
65300d60 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 | ||
65300d60 | 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 | ||
f6ccaed9 | 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, |
e5c7cd9b PP |
1036 | comp_class_name) != 0 || |
1037 | type != cfg->cmd_data.help.cfg_component->type) { | |
65300d60 | 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 | ||
65300d60 | 1057 | bt_object_put_ref(comp_cls); |
22e22462 PP |
1058 | } |
1059 | ||
1060 | end: | |
65300d60 | 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"); |
da91b29a | 1072 | print_value(stdout, bt_value_borrow_from_private(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"); | |
65300d60 | 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; | |
da91b29a | 1148 | struct bt_private_value *params = NULL; |
96e8c7e1 MD |
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; | |
c7eee084 | 1156 | const char *fail_reason = NULL; |
c327e427 | 1157 | FILE *out_stream = stdout; |
96e8c7e1 | 1158 | |
f6ccaed9 | 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 | ||
da91b29a | 1177 | params = bt_private_value_map_create(); |
96e8c7e1 MD |
1178 | if (!params) { |
1179 | goto error; | |
1180 | } | |
1181 | ||
da91b29a | 1182 | ret = bt_private_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 | ||
da91b29a PP |
1188 | ret = query(comp_cls, "sessions", bt_value_borrow_from_private(params), |
1189 | &results, &fail_reason); | |
c7eee084 PP |
1190 | if (ret) { |
1191 | goto failed; | |
96e8c7e1 MD |
1192 | } |
1193 | ||
f6ccaed9 PP |
1194 | BT_ASSERT(results); |
1195 | ||
96e8c7e1 MD |
1196 | if (!bt_value_is_array(results)) { |
1197 | BT_LOGE_STR("Expecting an array for sessions query."); | |
1198 | fprintf(stderr, "%s%sUnexpected type returned by session query%s\n", | |
1199 | bt_common_color_bold(), | |
1200 | bt_common_color_fg_red(), | |
1201 | bt_common_color_reset()); | |
1202 | goto error; | |
1203 | } | |
1204 | ||
c327e427 PP |
1205 | if (cfg->cmd_data.print_lttng_live_sessions.output_path->len > 0) { |
1206 | out_stream = | |
1207 | fopen(cfg->cmd_data.print_lttng_live_sessions.output_path->str, | |
1208 | "w"); | |
1209 | if (!out_stream) { | |
1210 | ret = -1; | |
1211 | BT_LOGE_ERRNO("Cannot open file for writing", | |
1212 | ": path=\"%s\"", | |
1213 | cfg->cmd_data.print_lttng_live_sessions.output_path->str); | |
1214 | goto end; | |
1215 | } | |
1216 | } | |
1217 | ||
07208d85 | 1218 | array_size = bt_value_array_get_size(results); |
96e8c7e1 MD |
1219 | for (i = 0; i < array_size; i++) { |
1220 | const char *url_text; | |
1221 | int64_t timer_us, streams, clients; | |
1222 | ||
07208d85 | 1223 | map = bt_value_array_borrow_element_by_index(results, i); |
96e8c7e1 MD |
1224 | if (!map) { |
1225 | BT_LOGE_STR("Unexpected empty array entry."); | |
1226 | goto error; | |
1227 | } | |
1228 | if (!bt_value_is_map(map)) { | |
1229 | BT_LOGE_STR("Unexpected entry type."); | |
1230 | goto error; | |
1231 | } | |
1232 | ||
07208d85 | 1233 | v = bt_value_map_borrow_entry_value(map, "url"); |
96e8c7e1 MD |
1234 | if (!v) { |
1235 | BT_LOGE_STR("Unexpected empty array \"url\" entry."); | |
1236 | goto error; | |
1237 | } | |
1238 | ret = bt_value_string_get(v, &url_text); | |
f6ccaed9 | 1239 | BT_ASSERT(ret == 0); |
c327e427 | 1240 | fprintf(out_stream, "%s", url_text); |
07208d85 | 1241 | v = bt_value_map_borrow_entry_value(map, "timer-us"); |
96e8c7e1 MD |
1242 | if (!v) { |
1243 | BT_LOGE_STR("Unexpected empty array \"timer-us\" entry."); | |
1244 | goto error; | |
1245 | } | |
1246 | ret = bt_value_integer_get(v, &timer_us); | |
f6ccaed9 | 1247 | BT_ASSERT(ret == 0); |
c327e427 | 1248 | fprintf(out_stream, " (timer = %" PRIu64 ", ", timer_us); |
07208d85 | 1249 | v = bt_value_map_borrow_entry_value(map, "stream-count"); |
96e8c7e1 MD |
1250 | if (!v) { |
1251 | BT_LOGE_STR("Unexpected empty array \"stream-count\" entry."); | |
1252 | goto error; | |
1253 | } | |
1254 | ret = bt_value_integer_get(v, &streams); | |
f6ccaed9 | 1255 | BT_ASSERT(ret == 0); |
c327e427 | 1256 | fprintf(out_stream, "%" PRIu64 " stream(s), ", streams); |
07208d85 | 1257 | v = bt_value_map_borrow_entry_value(map, "client-count"); |
96e8c7e1 MD |
1258 | if (!v) { |
1259 | BT_LOGE_STR("Unexpected empty array \"client-count\" entry."); | |
1260 | goto error; | |
1261 | } | |
1262 | ret = bt_value_integer_get(v, &clients); | |
f6ccaed9 | 1263 | BT_ASSERT(ret == 0); |
c327e427 | 1264 | fprintf(out_stream, "%" PRIu64 " client(s) connected)\n", clients); |
96e8c7e1 | 1265 | } |
c7eee084 PP |
1266 | |
1267 | goto end; | |
1268 | ||
1269 | failed: | |
1270 | BT_LOGE("Failed to query for sessions: %s", fail_reason); | |
1271 | fprintf(stderr, "%s%sFailed to request sessions: %s%s\n", | |
1272 | bt_common_color_bold(), | |
1273 | bt_common_color_fg_red(), | |
1274 | fail_reason, | |
1275 | bt_common_color_reset()); | |
1276 | ||
1277 | error: | |
1278 | ret = -1; | |
1279 | ||
96e8c7e1 | 1280 | end: |
65300d60 PP |
1281 | bt_object_put_ref(results); |
1282 | bt_object_put_ref(params); | |
1283 | bt_object_put_ref(comp_cls); | |
c327e427 PP |
1284 | |
1285 | if (out_stream && out_stream != stdout) { | |
1286 | int fclose_ret = fclose(out_stream); | |
1287 | ||
1288 | if (fclose_ret) { | |
1289 | BT_LOGE_ERRNO("Cannot close file stream", | |
1290 | ": path=\"%s\"", | |
1291 | cfg->cmd_data.print_lttng_live_sessions.output_path->str); | |
1292 | } | |
1293 | } | |
1294 | ||
96e8c7e1 | 1295 | return 0; |
db0f160a PP |
1296 | } |
1297 | ||
9009cc24 PP |
1298 | static |
1299 | int cmd_print_ctf_metadata(struct bt_config *cfg) | |
05a67631 PP |
1300 | { |
1301 | int ret = 0; | |
1302 | struct bt_component_class *comp_cls = NULL; | |
05a67631 | 1303 | struct bt_value *results = NULL; |
da91b29a | 1304 | struct bt_private_value *params = NULL; |
05a67631 PP |
1305 | struct bt_value *metadata_text_value = NULL; |
1306 | const char *metadata_text = NULL; | |
db0f160a PP |
1307 | static const char * const plugin_name = "ctf"; |
1308 | static const char * const comp_cls_name = "fs"; | |
1309 | static const enum bt_component_class_type comp_cls_type = | |
1310 | BT_COMPONENT_CLASS_TYPE_SOURCE; | |
c7eee084 | 1311 | const char *fail_reason = NULL; |
c327e427 | 1312 | FILE *out_stream = stdout; |
db0f160a | 1313 | |
f6ccaed9 | 1314 | BT_ASSERT(cfg->cmd_data.print_ctf_metadata.path); |
db0f160a PP |
1315 | comp_cls = find_component_class(plugin_name, comp_cls_name, |
1316 | comp_cls_type); | |
05a67631 | 1317 | if (!comp_cls) { |
7213a328 PP |
1318 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
1319 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
1320 | plugin_name, comp_cls_name, | |
1321 | BT_COMPONENT_CLASS_TYPE_SOURCE); | |
05a67631 PP |
1322 | fprintf(stderr, "%s%sCannot find component class %s", |
1323 | bt_common_color_bold(), | |
1324 | bt_common_color_fg_red(), | |
1325 | bt_common_color_reset()); | |
db0f160a PP |
1326 | print_plugin_comp_cls_opt(stderr, plugin_name, |
1327 | comp_cls_name, comp_cls_type); | |
05a67631 PP |
1328 | fprintf(stderr, "\n"); |
1329 | ret = -1; | |
1330 | goto end; | |
1331 | } | |
1332 | ||
da91b29a | 1333 | params = bt_private_value_map_create(); |
05a67631 PP |
1334 | if (!params) { |
1335 | ret = -1; | |
1336 | goto end; | |
1337 | } | |
1338 | ||
da91b29a | 1339 | ret = bt_private_value_map_insert_string_entry(params, "path", |
db0f160a | 1340 | cfg->cmd_data.print_ctf_metadata.path->str); |
05a67631 PP |
1341 | if (ret) { |
1342 | ret = -1; | |
1343 | goto end; | |
1344 | } | |
1345 | ||
da91b29a PP |
1346 | ret = query(comp_cls, "metadata-info", |
1347 | bt_value_borrow_from_private(params), &results, &fail_reason); | |
c7eee084 PP |
1348 | if (ret) { |
1349 | goto failed; | |
05a67631 PP |
1350 | } |
1351 | ||
07208d85 | 1352 | metadata_text_value = bt_value_map_borrow_entry_value(results, "text"); |
05a67631 | 1353 | if (!metadata_text_value) { |
7213a328 | 1354 | BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object."); |
05a67631 PP |
1355 | ret = -1; |
1356 | goto end; | |
1357 | } | |
1358 | ||
1359 | ret = bt_value_string_get(metadata_text_value, &metadata_text); | |
f6ccaed9 | 1360 | BT_ASSERT(ret == 0); |
c327e427 PP |
1361 | |
1362 | if (cfg->cmd_data.print_ctf_metadata.output_path->len > 0) { | |
1363 | out_stream = | |
1364 | fopen(cfg->cmd_data.print_ctf_metadata.output_path->str, | |
1365 | "w"); | |
1366 | if (!out_stream) { | |
1367 | ret = -1; | |
1368 | BT_LOGE_ERRNO("Cannot open file for writing", | |
1369 | ": path=\"%s\"", | |
1370 | cfg->cmd_data.print_ctf_metadata.output_path->str); | |
1371 | goto end; | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | ret = fprintf(out_stream, "%s\n", metadata_text); | |
1376 | if (ret < 0) { | |
1377 | BT_LOGE("Cannot write whole metadata text to output stream: " | |
1378 | "ret=%d", ret); | |
1379 | } | |
1380 | ||
c7eee084 PP |
1381 | goto end; |
1382 | ||
1383 | failed: | |
1384 | ret = -1; | |
1385 | BT_LOGE("Failed to query for metadata info: %s", fail_reason); | |
1386 | fprintf(stderr, "%s%sFailed to request metadata info: %s%s\n", | |
1387 | bt_common_color_bold(), | |
1388 | bt_common_color_fg_red(), | |
1389 | fail_reason, | |
1390 | bt_common_color_reset()); | |
05a67631 PP |
1391 | |
1392 | end: | |
c7eee084 | 1393 | destroy_the_query_executor(); |
65300d60 PP |
1394 | bt_object_put_ref(results); |
1395 | bt_object_put_ref(params); | |
1396 | bt_object_put_ref(comp_cls); | |
c327e427 PP |
1397 | |
1398 | if (out_stream && out_stream != stdout) { | |
1399 | int fclose_ret = fclose(out_stream); | |
1400 | ||
1401 | if (fclose_ret) { | |
1402 | BT_LOGE_ERRNO("Cannot close file stream", | |
1403 | ": path=\"%s\"", | |
1404 | cfg->cmd_data.print_ctf_metadata.output_path->str); | |
1405 | } | |
1406 | } | |
1407 | ||
05a67631 PP |
1408 | return 0; |
1409 | } | |
1410 | ||
75a2cb9b JG |
1411 | struct port_id { |
1412 | char *instance_name; | |
1413 | char *port_name; | |
1414 | }; | |
1415 | ||
1416 | struct trace_range { | |
1417 | uint64_t intersection_range_begin_ns; | |
1418 | uint64_t intersection_range_end_ns; | |
1419 | }; | |
1420 | ||
1421 | static | |
1422 | guint port_id_hash(gconstpointer v) | |
1423 | { | |
1424 | const struct port_id *id = v; | |
1425 | ||
f6ccaed9 PP |
1426 | BT_ASSERT(id->instance_name); |
1427 | BT_ASSERT(id->port_name); | |
75a2cb9b JG |
1428 | |
1429 | return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name); | |
1430 | } | |
1431 | ||
1432 | static | |
1433 | gboolean port_id_equal(gconstpointer v1, gconstpointer v2) | |
1434 | { | |
1435 | const struct port_id *id1 = v1; | |
1436 | const struct port_id *id2 = v2; | |
1437 | ||
1438 | return !strcmp(id1->instance_name, id2->instance_name) && | |
1439 | !strcmp(id1->port_name, id2->port_name); | |
1440 | } | |
1441 | ||
1442 | static | |
1443 | void port_id_destroy(gpointer data) | |
1444 | { | |
1445 | struct port_id *id = data; | |
1446 | ||
1447 | free(id->instance_name); | |
1448 | free(id->port_name); | |
1449 | free(id); | |
1450 | } | |
1451 | ||
1452 | static | |
1453 | void trace_range_destroy(gpointer data) | |
1454 | { | |
1455 | free(data); | |
1456 | } | |
1457 | ||
9009cc24 PP |
1458 | struct cmd_run_ctx { |
1459 | /* Owned by this */ | |
1460 | GHashTable *components; | |
1461 | ||
1462 | /* Owned by this */ | |
1463 | struct bt_graph *graph; | |
1464 | ||
1465 | /* Weak */ | |
1466 | struct bt_config *cfg; | |
1467 | ||
1468 | bool connect_ports; | |
75a2cb9b JG |
1469 | |
1470 | bool stream_intersection_mode; | |
1471 | ||
1472 | /* | |
1473 | * Association of struct port_id -> struct trace_range. | |
1474 | */ | |
1475 | GHashTable *intersections; | |
9009cc24 PP |
1476 | }; |
1477 | ||
75a2cb9b JG |
1478 | /* Returns a timestamp of the form "(-)s.ns" */ |
1479 | static | |
1480 | char *s_from_ns(int64_t ns) | |
1481 | { | |
1482 | int ret; | |
1483 | char *s_ret = NULL; | |
1484 | bool is_negative; | |
1485 | int64_t ts_sec_abs, ts_nsec_abs; | |
1486 | int64_t ts_sec = ns / NSEC_PER_SEC; | |
1487 | int64_t ts_nsec = ns % NSEC_PER_SEC; | |
1488 | ||
1489 | if (ts_sec >= 0 && ts_nsec >= 0) { | |
1490 | is_negative = false; | |
1491 | ts_sec_abs = ts_sec; | |
1492 | ts_nsec_abs = ts_nsec; | |
1493 | } else if (ts_sec > 0 && ts_nsec < 0) { | |
1494 | is_negative = false; | |
1495 | ts_sec_abs = ts_sec - 1; | |
1496 | ts_nsec_abs = NSEC_PER_SEC + ts_nsec; | |
1497 | } else if (ts_sec == 0 && ts_nsec < 0) { | |
1498 | is_negative = true; | |
1499 | ts_sec_abs = ts_sec; | |
1500 | ts_nsec_abs = -ts_nsec; | |
1501 | } else if (ts_sec < 0 && ts_nsec > 0) { | |
1502 | is_negative = true; | |
1503 | ts_sec_abs = -(ts_sec + 1); | |
1504 | ts_nsec_abs = NSEC_PER_SEC - ts_nsec; | |
1505 | } else if (ts_sec < 0 && ts_nsec == 0) { | |
1506 | is_negative = true; | |
1507 | ts_sec_abs = -ts_sec; | |
1508 | ts_nsec_abs = ts_nsec; | |
1509 | } else { /* (ts_sec < 0 && ts_nsec < 0) */ | |
1510 | is_negative = true; | |
1511 | ts_sec_abs = -ts_sec; | |
1512 | ts_nsec_abs = -ts_nsec; | |
1513 | } | |
1514 | ||
1515 | ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64, | |
1516 | is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs); | |
1517 | if (ret < 0) { | |
1518 | s_ret = NULL; | |
1519 | } | |
1520 | return s_ret; | |
1521 | } | |
1522 | ||
9009cc24 PP |
1523 | static |
1524 | int cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1525 | struct cmd_run_ctx *ctx, struct bt_component *upstream_comp, | |
1526 | struct bt_port *upstream_port, | |
1527 | struct bt_config_connection *cfg_conn) | |
290725f7 PP |
1528 | { |
1529 | int ret = 0; | |
9009cc24 PP |
1530 | GQuark downstreamp_comp_name_quark; |
1531 | struct bt_component *downstream_comp; | |
1532 | int64_t downstream_port_count; | |
1533 | uint64_t i; | |
1534 | int64_t (*port_count_fn)(struct bt_component *); | |
1535 | struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t); | |
a256a42d | 1536 | enum bt_graph_status status = BT_GRAPH_STATUS_ERROR; |
75a2cb9b | 1537 | bool insert_trimmer = false; |
da91b29a | 1538 | struct bt_private_value *trimmer_params = NULL; |
75a2cb9b JG |
1539 | char *intersection_begin = NULL; |
1540 | char *intersection_end = NULL; | |
1541 | struct bt_component *trimmer = NULL; | |
1542 | struct bt_component_class *trimmer_class = NULL; | |
1543 | struct bt_port *trimmer_input = NULL; | |
1544 | struct bt_port *trimmer_output = NULL; | |
1545 | ||
1546 | if (ctx->intersections && | |
1547 | bt_component_get_class_type(upstream_comp) == | |
1548 | BT_COMPONENT_CLASS_TYPE_SOURCE) { | |
1549 | struct trace_range *range; | |
1550 | struct port_id port_id = { | |
1551 | .instance_name = (char *) bt_component_get_name(upstream_comp), | |
1552 | .port_name = (char *) bt_port_get_name(upstream_port) | |
1553 | }; | |
1554 | ||
1555 | if (!port_id.instance_name || !port_id.port_name) { | |
1556 | goto error; | |
1557 | } | |
1558 | ||
1559 | range = (struct trace_range *) g_hash_table_lookup( | |
1560 | ctx->intersections, &port_id); | |
1561 | if (range) { | |
1562 | enum bt_value_status status; | |
1563 | ||
1564 | intersection_begin = s_from_ns( | |
1565 | range->intersection_range_begin_ns); | |
1566 | intersection_end = s_from_ns( | |
1567 | range->intersection_range_end_ns); | |
1568 | if (!intersection_begin || !intersection_end) { | |
1569 | BT_LOGE_STR("Cannot create trimmer argument timestamp string."); | |
1570 | goto error; | |
1571 | } | |
1572 | ||
1573 | insert_trimmer = true; | |
da91b29a | 1574 | trimmer_params = bt_private_value_map_create(); |
75a2cb9b JG |
1575 | if (!trimmer_params) { |
1576 | goto error; | |
1577 | } | |
1578 | ||
da91b29a | 1579 | status = bt_private_value_map_insert_string_entry( |
07208d85 | 1580 | trimmer_params, "begin", intersection_begin); |
75a2cb9b JG |
1581 | if (status != BT_VALUE_STATUS_OK) { |
1582 | goto error; | |
1583 | } | |
da91b29a | 1584 | status = bt_private_value_map_insert_string_entry( |
07208d85 | 1585 | trimmer_params, |
75a2cb9b JG |
1586 | "end", intersection_end); |
1587 | if (status != BT_VALUE_STATUS_OK) { | |
1588 | goto error; | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | trimmer_class = find_component_class("utils", "trimmer", | |
1593 | BT_COMPONENT_CLASS_TYPE_FILTER); | |
1594 | if (!trimmer_class) { | |
1595 | goto error; | |
1596 | } | |
1597 | } | |
9009cc24 | 1598 | |
7213a328 PP |
1599 | BT_LOGI("Connecting upstream port to the next available downstream port: " |
1600 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1601 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1602 | upstream_port, bt_port_get_name(upstream_port), | |
1603 | cfg_conn->downstream_comp_name->str, | |
1604 | cfg_conn->arg->str); | |
9009cc24 PP |
1605 | downstreamp_comp_name_quark = g_quark_from_string( |
1606 | cfg_conn->downstream_comp_name->str); | |
f6ccaed9 | 1607 | BT_ASSERT(downstreamp_comp_name_quark > 0); |
9009cc24 | 1608 | downstream_comp = g_hash_table_lookup(ctx->components, |
71c7c95f | 1609 | GUINT_TO_POINTER(downstreamp_comp_name_quark)); |
9009cc24 | 1610 | if (!downstream_comp) { |
7213a328 PP |
1611 | BT_LOGE("Cannot find downstream component: comp-name=\"%s\", " |
1612 | "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str, | |
1613 | cfg_conn->arg->str); | |
9009cc24 PP |
1614 | fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n", |
1615 | cfg_conn->arg->str); | |
1616 | goto error; | |
1617 | } | |
1618 | ||
1619 | if (bt_component_is_filter(downstream_comp)) { | |
1620 | port_count_fn = bt_component_filter_get_input_port_count; | |
1621 | port_by_index_fn = bt_component_filter_get_input_port_by_index; | |
1622 | } else if (bt_component_is_sink(downstream_comp)) { | |
1623 | port_count_fn = bt_component_sink_get_input_port_count; | |
1624 | port_by_index_fn = bt_component_sink_get_input_port_by_index; | |
1625 | } else { | |
1626 | /* | |
1627 | * Should never happen because the connections are | |
1628 | * validated before we get here. | |
1629 | */ | |
7213a328 PP |
1630 | BT_LOGF("Invalid connection: downstream component is a source: " |
1631 | "conn-arg=\"%s\"", cfg_conn->arg->str); | |
0fbb9a9f | 1632 | abort(); |
9009cc24 | 1633 | } |
290725f7 | 1634 | |
9009cc24 | 1635 | downstream_port_count = port_count_fn(downstream_comp); |
f6ccaed9 | 1636 | BT_ASSERT(downstream_port_count >= 0); |
9009cc24 PP |
1637 | |
1638 | for (i = 0; i < downstream_port_count; i++) { | |
1639 | struct bt_port *downstream_port = | |
1640 | port_by_index_fn(downstream_comp, i); | |
75a2cb9b | 1641 | const char *upstream_port_name; |
9009cc24 PP |
1642 | const char *downstream_port_name; |
1643 | ||
f6ccaed9 | 1644 | BT_ASSERT(downstream_port); |
9009cc24 | 1645 | |
75a2cb9b | 1646 | /* Skip port if it's already connected. */ |
9009cc24 | 1647 | if (bt_port_is_connected(downstream_port)) { |
65300d60 | 1648 | bt_object_put_ref(downstream_port); |
7213a328 PP |
1649 | BT_LOGD("Skipping downstream port: already connected: " |
1650 | "port-addr=%p, port-name=\"%s\"", | |
1651 | downstream_port, | |
1652 | bt_port_get_name(downstream_port)); | |
9009cc24 PP |
1653 | continue; |
1654 | } | |
1655 | ||
1656 | downstream_port_name = bt_port_get_name(downstream_port); | |
f6ccaed9 | 1657 | BT_ASSERT(downstream_port_name); |
75a2cb9b | 1658 | upstream_port_name = bt_port_get_name(upstream_port); |
f6ccaed9 | 1659 | BT_ASSERT(upstream_port_name); |
9009cc24 | 1660 | |
75a2cb9b | 1661 | if (!bt_common_star_glob_match( |
1974687e MJ |
1662 | cfg_conn->downstream_port_glob->str, SIZE_MAX, |
1663 | downstream_port_name, SIZE_MAX)) { | |
65300d60 | 1664 | bt_object_put_ref(downstream_port); |
75a2cb9b JG |
1665 | continue; |
1666 | } | |
1667 | ||
1668 | if (insert_trimmer) { | |
1669 | /* | |
1670 | * In order to insert the trimmer between the two | |
1671 | * components that were being connected, we create | |
1672 | * a connection configuration entry which describes | |
1673 | * a connection from the trimmer's output to the | |
1674 | * original input that was being connected. | |
1675 | * | |
1676 | * Hence, the creation of the trimmer will cause the | |
1677 | * graph "new port" listener to establish all downstream | |
1678 | * connections as its output port is connected. We will | |
1679 | * then establish the connection between the original | |
1680 | * upstream source and the trimmer. | |
1681 | */ | |
1682 | char *trimmer_name = NULL; | |
1683 | enum bt_graph_status graph_status; | |
1684 | ||
1685 | ret = asprintf(&trimmer_name, "%s-%s", | |
1686 | "stream-intersection-trimmer", | |
1687 | upstream_port_name); | |
1688 | if (ret < 0) { | |
1689 | goto error; | |
1690 | } | |
1691 | ret = 0; | |
1692 | ||
1693 | ctx->connect_ports = false; | |
1694 | graph_status = bt_graph_add_component(ctx->graph, | |
da91b29a PP |
1695 | trimmer_class, trimmer_name, |
1696 | bt_value_borrow_from_private(trimmer_params), | |
75a2cb9b JG |
1697 | &trimmer); |
1698 | free(trimmer_name); | |
1699 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
1700 | goto error; | |
1701 | } | |
f6ccaed9 | 1702 | BT_ASSERT(trimmer); |
75a2cb9b JG |
1703 | |
1704 | trimmer_input = | |
1705 | bt_component_filter_get_input_port_by_index( | |
1706 | trimmer, 0); | |
1707 | if (!trimmer_input) { | |
1708 | goto error; | |
1709 | } | |
1710 | trimmer_output = | |
1711 | bt_component_filter_get_output_port_by_index( | |
1712 | trimmer, 0); | |
1713 | if (!trimmer_output) { | |
9009cc24 PP |
1714 | goto error; |
1715 | } | |
1716 | ||
75a2cb9b JG |
1717 | /* |
1718 | * Replace the current downstream port by the trimmer's | |
1719 | * upstream port. | |
1720 | */ | |
65300d60 | 1721 | BT_OBJECT_MOVE_REF(downstream_port, trimmer_input); |
75a2cb9b JG |
1722 | downstream_port_name = bt_port_get_name( |
1723 | downstream_port); | |
1724 | if (!downstream_port_name) { | |
1725 | goto error; | |
1726 | } | |
1727 | } | |
1728 | ||
1729 | /* We have a winner! */ | |
1730 | status = bt_graph_connect_ports(ctx->graph, | |
1731 | upstream_port, downstream_port, NULL); | |
65300d60 | 1732 | BT_OBJECT_PUT_REF_AND_RESET(downstream_port); |
75a2cb9b JG |
1733 | switch (status) { |
1734 | case BT_GRAPH_STATUS_OK: | |
1735 | break; | |
1736 | case BT_GRAPH_STATUS_CANCELED: | |
1737 | BT_LOGI_STR("Graph was canceled by user."); | |
1738 | status = BT_GRAPH_STATUS_OK; | |
1739 | break; | |
1740 | case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION: | |
1741 | BT_LOGE("A component refused a connection to one of its ports: " | |
7213a328 PP |
1742 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
1743 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1744 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1745 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1746 | "conn-arg=\"%s\"", | |
1747 | upstream_comp, bt_component_get_name(upstream_comp), | |
1748 | upstream_port, bt_port_get_name(upstream_port), | |
1749 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1750 | downstream_port, downstream_port_name, | |
1751 | cfg_conn->arg->str); | |
75a2cb9b JG |
1752 | fprintf(stderr, |
1753 | "A component refused a connection to one of its ports (`%s` to `%s`): %s\n", | |
1754 | bt_port_get_name(upstream_port), | |
1755 | downstream_port_name, | |
1756 | cfg_conn->arg->str); | |
1757 | break; | |
1758 | default: | |
1759 | BT_LOGE("Cannot create connection: graph refuses to connect ports: " | |
1760 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
1761 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1762 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1763 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1764 | "conn-arg=\"%s\"", | |
1765 | upstream_comp, bt_component_get_name(upstream_comp), | |
1766 | upstream_port, bt_port_get_name(upstream_port), | |
1767 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1768 | downstream_port, downstream_port_name, | |
1769 | cfg_conn->arg->str); | |
1770 | fprintf(stderr, | |
1771 | "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n", | |
1772 | bt_port_get_name(upstream_port), | |
1773 | downstream_port_name, | |
1774 | cfg_conn->arg->str); | |
1775 | goto error; | |
9009cc24 PP |
1776 | } |
1777 | ||
75a2cb9b JG |
1778 | BT_LOGI("Connected component ports: " |
1779 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
1780 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1781 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1782 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1783 | "conn-arg=\"%s\"", | |
1784 | upstream_comp, bt_component_get_name(upstream_comp), | |
1785 | upstream_port, bt_port_get_name(upstream_port), | |
1786 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1787 | downstream_port, downstream_port_name, | |
1788 | cfg_conn->arg->str); | |
1789 | ||
1790 | if (insert_trimmer) { | |
1791 | /* | |
1792 | * The first connection, from the source to the trimmer, | |
1793 | * has been done. We now connect the trimmer to the | |
1794 | * original downstream port. | |
1795 | */ | |
1796 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1797 | ctx, trimmer, trimmer_output, cfg_conn); | |
1798 | if (ret) { | |
1799 | goto error; | |
1800 | } | |
1801 | ctx->connect_ports = true; | |
1802 | } | |
9009cc24 | 1803 | |
53bc54cd PP |
1804 | /* |
1805 | * We found a matching downstream port: the search is | |
1806 | * over. | |
1807 | */ | |
1808 | goto end; | |
05a67631 PP |
1809 | } |
1810 | ||
53bc54cd PP |
1811 | /* No downstream port found */ |
1812 | BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: " | |
1813 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1814 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1815 | upstream_port, bt_port_get_name(upstream_port), | |
1816 | cfg_conn->downstream_comp_name->str, | |
1817 | cfg_conn->arg->str); | |
1818 | fprintf(stderr, | |
1819 | "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n", | |
1820 | bt_port_get_name(upstream_port), cfg_conn->arg->str); | |
9009cc24 PP |
1821 | |
1822 | error: | |
1823 | ret = -1; | |
1824 | ||
1825 | end: | |
75a2cb9b JG |
1826 | free(intersection_begin); |
1827 | free(intersection_end); | |
65300d60 PP |
1828 | BT_OBJECT_PUT_REF_AND_RESET(trimmer_params); |
1829 | BT_OBJECT_PUT_REF_AND_RESET(trimmer_class); | |
1830 | BT_OBJECT_PUT_REF_AND_RESET(trimmer); | |
1831 | BT_OBJECT_PUT_REF_AND_RESET(trimmer_input); | |
1832 | BT_OBJECT_PUT_REF_AND_RESET(trimmer_output); | |
9009cc24 PP |
1833 | return ret; |
1834 | } | |
1835 | ||
1836 | static | |
1837 | int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx, | |
1838 | struct bt_port *upstream_port) | |
1839 | { | |
1840 | int ret = 0; | |
1841 | const char *upstream_port_name; | |
1842 | const char *upstream_comp_name; | |
1843 | struct bt_component *upstream_comp = NULL; | |
1844 | size_t i; | |
1845 | ||
f6ccaed9 PP |
1846 | BT_ASSERT(ctx); |
1847 | BT_ASSERT(upstream_port); | |
9009cc24 | 1848 | upstream_port_name = bt_port_get_name(upstream_port); |
f6ccaed9 | 1849 | BT_ASSERT(upstream_port_name); |
9009cc24 PP |
1850 | upstream_comp = bt_port_get_component(upstream_port); |
1851 | if (!upstream_comp) { | |
7213a328 PP |
1852 | BT_LOGW("Upstream port to connect is not part of a component: " |
1853 | "port-addr=%p, port-name=\"%s\"", | |
1854 | upstream_port, upstream_port_name); | |
98ecef32 MD |
1855 | ret = -1; |
1856 | goto end; | |
33bceaf8 JG |
1857 | } |
1858 | ||
9009cc24 | 1859 | upstream_comp_name = bt_component_get_name(upstream_comp); |
f6ccaed9 | 1860 | BT_ASSERT(upstream_comp_name); |
7213a328 PP |
1861 | BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", " |
1862 | "port-addr=%p, port-name=\"%s\"", | |
1863 | upstream_comp, upstream_comp_name, | |
1864 | upstream_port, upstream_port_name); | |
9009cc24 PP |
1865 | |
1866 | for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) { | |
1867 | struct bt_config_connection *cfg_conn = | |
1868 | g_ptr_array_index( | |
1869 | ctx->cfg->cmd_data.run.connections, i); | |
1870 | ||
1871 | if (strcmp(cfg_conn->upstream_comp_name->str, | |
75a2cb9b JG |
1872 | upstream_comp_name)) { |
1873 | continue; | |
1874 | } | |
1875 | ||
1876 | if (!bt_common_star_glob_match( | |
1877 | cfg_conn->upstream_port_glob->str, | |
1974687e | 1878 | SIZE_MAX, upstream_port_name, SIZE_MAX)) { |
75a2cb9b JG |
1879 | continue; |
1880 | } | |
1881 | ||
1882 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1883 | ctx, upstream_comp, upstream_port, cfg_conn); | |
1884 | if (ret) { | |
1885 | BT_LOGE("Cannot connect upstream port: " | |
1886 | "port-addr=%p, port-name=\"%s\"", | |
1887 | upstream_port, | |
1888 | upstream_port_name); | |
1889 | fprintf(stderr, | |
1890 | "Cannot connect port `%s` of component `%s` to a downstream port: %s\n", | |
1891 | upstream_port_name, | |
1892 | upstream_comp_name, | |
1893 | cfg_conn->arg->str); | |
1894 | goto error; | |
9009cc24 | 1895 | } |
75a2cb9b | 1896 | goto end; |
9009cc24 PP |
1897 | } |
1898 | ||
7213a328 PP |
1899 | BT_LOGE("Cannot connect upstream port: port does not match any connection argument: " |
1900 | "port-addr=%p, port-name=\"%s\"", upstream_port, | |
1901 | upstream_port_name); | |
9009cc24 PP |
1902 | fprintf(stderr, |
1903 | "Cannot create connection: upstream port `%s` does not match any connection\n", | |
7213a328 | 1904 | upstream_port_name); |
9009cc24 PP |
1905 | |
1906 | error: | |
1907 | ret = -1; | |
1908 | ||
1909 | end: | |
65300d60 | 1910 | bt_object_put_ref(upstream_comp); |
9009cc24 PP |
1911 | return ret; |
1912 | } | |
1913 | ||
1914 | static | |
1915 | void graph_port_added_listener(struct bt_port *port, void *data) | |
1916 | { | |
1917 | struct bt_component *comp = NULL; | |
1918 | struct cmd_run_ctx *ctx = data; | |
1919 | ||
e12720c0 PP |
1920 | comp = bt_port_get_component(port); |
1921 | BT_LOGI("Port added to a graph's component: comp-addr=%p, " | |
1922 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
1923 | comp, comp ? bt_component_get_name(comp) : "", | |
7213a328 | 1924 | port, bt_port_get_name(port)); |
36712f1d PP |
1925 | |
1926 | if (!ctx->connect_ports) { | |
1927 | goto end; | |
1928 | } | |
1929 | ||
e12720c0 PP |
1930 | if (!comp) { |
1931 | BT_LOGW_STR("Port has no component."); | |
56a1cced JG |
1932 | goto end; |
1933 | } | |
7c7c0433 | 1934 | |
e12720c0 PP |
1935 | if (bt_port_is_connected(port)) { |
1936 | BT_LOGW_STR("Port is already connected."); | |
7c7c0433 JG |
1937 | goto end; |
1938 | } | |
1939 | ||
9009cc24 | 1940 | if (!bt_port_is_output(port)) { |
7213a328 | 1941 | BT_LOGI_STR("Skipping input port."); |
61ddbc8a JG |
1942 | goto end; |
1943 | } | |
1944 | ||
9009cc24 | 1945 | if (cmd_run_ctx_connect_upstream_port(ctx, port)) { |
7213a328 | 1946 | BT_LOGF_STR("Cannot connect upstream port."); |
9009cc24 PP |
1947 | fprintf(stderr, "Added port could not be connected: aborting\n"); |
1948 | abort(); | |
1949 | } | |
1950 | ||
1951 | end: | |
65300d60 | 1952 | bt_object_put_ref(comp); |
9009cc24 PP |
1953 | return; |
1954 | } | |
1955 | ||
1956 | static | |
1957 | void graph_port_removed_listener(struct bt_component *component, | |
1958 | struct bt_port *port, void *data) | |
1959 | { | |
7213a328 PP |
1960 | BT_LOGI("Port removed from a graph's component: comp-addr=%p, " |
1961 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
1962 | component, bt_component_get_name(component), | |
1963 | port, bt_port_get_name(port)); | |
9009cc24 PP |
1964 | } |
1965 | ||
1966 | static | |
1967 | void graph_ports_connected_listener(struct bt_port *upstream_port, | |
1968 | struct bt_port *downstream_port, void *data) | |
1969 | { | |
e12720c0 PP |
1970 | struct bt_component *upstream_comp = bt_port_get_component(upstream_port); |
1971 | struct bt_component *downstream_comp = bt_port_get_component(downstream_port); | |
1972 | ||
f6ccaed9 PP |
1973 | BT_ASSERT(upstream_comp); |
1974 | BT_ASSERT(downstream_comp); | |
7213a328 | 1975 | BT_LOGI("Graph's component ports connected: " |
e12720c0 | 1976 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
7213a328 | 1977 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
e12720c0 | 1978 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
7213a328 | 1979 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
e12720c0 | 1980 | upstream_comp, bt_component_get_name(upstream_comp), |
7213a328 | 1981 | upstream_port, bt_port_get_name(upstream_port), |
e12720c0 | 1982 | downstream_comp, bt_component_get_name(downstream_comp), |
7213a328 | 1983 | downstream_port, bt_port_get_name(downstream_port)); |
65300d60 PP |
1984 | bt_object_put_ref(upstream_comp); |
1985 | bt_object_put_ref(downstream_comp); | |
9009cc24 PP |
1986 | } |
1987 | ||
1988 | static | |
1989 | void graph_ports_disconnected_listener( | |
1990 | struct bt_component *upstream_component, | |
1991 | struct bt_component *downstream_component, | |
1992 | struct bt_port *upstream_port, struct bt_port *downstream_port, | |
1993 | void *data) | |
1994 | { | |
7213a328 PP |
1995 | BT_LOGI("Graph's component ports disconnected: " |
1996 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1997 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
1998 | upstream_port, bt_port_get_name(upstream_port), | |
1999 | downstream_port, bt_port_get_name(downstream_port)); | |
9009cc24 PP |
2000 | } |
2001 | ||
2002 | static | |
2003 | void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx) | |
2004 | { | |
2005 | if (!ctx) { | |
2006 | return; | |
2007 | } | |
2008 | ||
2009 | if (ctx->components) { | |
2010 | g_hash_table_destroy(ctx->components); | |
2011 | ctx->components = NULL; | |
2012 | } | |
2013 | ||
75a2cb9b JG |
2014 | if (ctx->intersections) { |
2015 | g_hash_table_destroy(ctx->intersections); | |
a5302548 | 2016 | ctx->intersections = NULL; |
75a2cb9b JG |
2017 | } |
2018 | ||
65300d60 | 2019 | BT_OBJECT_PUT_REF_AND_RESET(ctx->graph); |
5401f780 | 2020 | the_graph = NULL; |
9009cc24 PP |
2021 | ctx->cfg = NULL; |
2022 | } | |
2023 | ||
2024 | static | |
2025 | int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg) | |
2026 | { | |
2027 | int ret = 0; | |
2028 | ||
2029 | ctx->cfg = cfg; | |
2030 | ctx->connect_ports = false; | |
2031 | ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
65300d60 | 2032 | NULL, bt_object_put_ref); |
9009cc24 PP |
2033 | if (!ctx->components) { |
2034 | goto error; | |
2035 | } | |
2036 | ||
75a2cb9b JG |
2037 | if (cfg->cmd_data.run.stream_intersection_mode) { |
2038 | ctx->stream_intersection_mode = true; | |
2039 | ctx->intersections = g_hash_table_new_full(port_id_hash, | |
2040 | port_id_equal, port_id_destroy, trace_range_destroy); | |
2041 | if (!ctx->intersections) { | |
2042 | goto error; | |
2043 | } | |
2044 | } | |
2045 | ||
9009cc24 PP |
2046 | ctx->graph = bt_graph_create(); |
2047 | if (!ctx->graph) { | |
2048 | goto error; | |
2049 | } | |
2050 | ||
5401f780 | 2051 | the_graph = ctx->graph; |
9009cc24 | 2052 | ret = bt_graph_add_port_added_listener(ctx->graph, |
8cc092c9 | 2053 | graph_port_added_listener, NULL, ctx); |
0d107cdd PP |
2054 | if (ret < 0) { |
2055 | BT_LOGE_STR("Cannot add \"port added\" listener to graph."); | |
9009cc24 PP |
2056 | goto error; |
2057 | } | |
2058 | ||
2059 | ret = bt_graph_add_port_removed_listener(ctx->graph, | |
8cc092c9 | 2060 | graph_port_removed_listener, NULL, ctx); |
0d107cdd PP |
2061 | if (ret < 0) { |
2062 | BT_LOGE_STR("Cannot add \"port removed\" listener to graph."); | |
9009cc24 PP |
2063 | goto error; |
2064 | } | |
2065 | ||
2066 | ret = bt_graph_add_ports_connected_listener(ctx->graph, | |
8cc092c9 | 2067 | graph_ports_connected_listener, NULL, ctx); |
0d107cdd PP |
2068 | if (ret < 0) { |
2069 | BT_LOGE_STR("Cannot add \"ports connected\" listener to graph."); | |
9009cc24 PP |
2070 | goto error; |
2071 | } | |
2072 | ||
2073 | ret = bt_graph_add_ports_disconnected_listener(ctx->graph, | |
8cc092c9 | 2074 | graph_ports_disconnected_listener, NULL, ctx); |
0d107cdd PP |
2075 | if (ret < 0) { |
2076 | BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph."); | |
9009cc24 PP |
2077 | goto error; |
2078 | } | |
2079 | ||
2080 | goto end; | |
2081 | ||
2082 | error: | |
2083 | cmd_run_ctx_destroy(ctx); | |
2084 | ret = -1; | |
2085 | ||
2086 | end: | |
2087 | return ret; | |
2088 | } | |
2089 | ||
75a2cb9b JG |
2090 | static |
2091 | int set_stream_intersections(struct cmd_run_ctx *ctx, | |
2092 | struct bt_config_component *cfg_comp, | |
2093 | struct bt_component_class *comp_cls) | |
2094 | { | |
2095 | int ret = 0; | |
2096 | uint64_t trace_idx; | |
2097 | int64_t trace_count; | |
2098 | enum bt_value_status value_status; | |
2099 | const char *path = NULL; | |
2100 | struct bt_value *component_path_value = NULL; | |
da91b29a | 2101 | struct bt_private_value *query_params = NULL; |
75a2cb9b JG |
2102 | struct bt_value *query_result = NULL; |
2103 | struct bt_value *trace_info = NULL; | |
2104 | struct bt_value *intersection_range = NULL; | |
2105 | struct bt_value *intersection_begin = NULL; | |
2106 | struct bt_value *intersection_end = NULL; | |
2107 | struct bt_value *stream_path_value = NULL; | |
2108 | struct bt_value *stream_paths = NULL; | |
2109 | struct bt_value *stream_infos = NULL; | |
2110 | struct bt_value *stream_info = NULL; | |
2111 | struct port_id *port_id = NULL; | |
2112 | struct trace_range *trace_range = NULL; | |
c7eee084 | 2113 | const char *fail_reason = NULL; |
75a2cb9b | 2114 | |
da91b29a PP |
2115 | component_path_value = bt_value_map_borrow_entry_value( |
2116 | bt_value_borrow_from_private(cfg_comp->params), | |
07208d85 | 2117 | "path"); |
f6ccaed9 | 2118 | if (component_path_value && !bt_value_is_string(component_path_value)) { |
75a2cb9b JG |
2119 | BT_LOGD("Cannot get path parameter: component-name=%s", |
2120 | cfg_comp->instance_name->str); | |
2121 | ret = -1; | |
2122 | goto error; | |
2123 | } | |
2124 | ||
2125 | value_status = bt_value_string_get(component_path_value, &path); | |
2126 | if (value_status != BT_VALUE_STATUS_OK) { | |
2127 | BT_LOGD("Cannot get path string value: component-name=%s", | |
2128 | cfg_comp->instance_name->str); | |
2129 | ret = -1; | |
2130 | goto error; | |
2131 | } | |
2132 | ||
da91b29a | 2133 | query_params = bt_private_value_map_create(); |
75a2cb9b JG |
2134 | if (!query_params) { |
2135 | BT_LOGE_STR("Cannot create query parameters."); | |
2136 | ret = -1; | |
2137 | goto error; | |
2138 | } | |
2139 | ||
da91b29a | 2140 | value_status = bt_private_value_map_insert_entry(query_params, "path", |
07208d85 | 2141 | component_path_value); |
75a2cb9b | 2142 | if (value_status != BT_VALUE_STATUS_OK) { |
54b8d024 | 2143 | BT_LOGE_STR("Cannot insert path parameter in query parameter map."); |
75a2cb9b JG |
2144 | ret = -1; |
2145 | goto error; | |
2146 | } | |
2147 | ||
da91b29a PP |
2148 | ret = query(comp_cls, "trace-info", |
2149 | bt_value_borrow_from_private(query_params), &query_result, | |
c7eee084 PP |
2150 | &fail_reason); |
2151 | if (ret) { | |
2152 | BT_LOGD("Component class does not support the `trace-info` query: %s: " | |
2153 | "comp-class-name=\"%s\"", fail_reason, | |
75a2cb9b JG |
2154 | bt_component_class_get_name(comp_cls)); |
2155 | ret = -1; | |
2156 | goto error; | |
2157 | } | |
2158 | ||
f6ccaed9 PP |
2159 | BT_ASSERT(query_result); |
2160 | ||
75a2cb9b JG |
2161 | if (!bt_value_is_array(query_result)) { |
2162 | BT_LOGD("Unexpected format of \'trace-info\' query result: " | |
2163 | "component-class-name=%s", | |
2164 | bt_component_class_get_name(comp_cls)); | |
2165 | ret = -1; | |
2166 | goto error; | |
2167 | } | |
2168 | ||
07208d85 | 2169 | trace_count = bt_value_array_get_size(query_result); |
75a2cb9b JG |
2170 | if (trace_count < 0) { |
2171 | ret = -1; | |
2172 | goto error; | |
2173 | } | |
2174 | ||
2175 | for (trace_idx = 0; trace_idx < trace_count; trace_idx++) { | |
2176 | int64_t begin, end; | |
2177 | uint64_t stream_idx; | |
2178 | int64_t stream_count; | |
2179 | ||
07208d85 PP |
2180 | trace_info = bt_value_array_borrow_element_by_index( |
2181 | query_result, trace_idx); | |
75a2cb9b JG |
2182 | if (!trace_info || !bt_value_is_map(trace_info)) { |
2183 | ret = -1; | |
2184 | BT_LOGD_STR("Cannot retrieve trace from query result."); | |
2185 | goto error; | |
2186 | } | |
2187 | ||
07208d85 | 2188 | intersection_range = bt_value_map_borrow_entry_value(trace_info, |
75a2cb9b JG |
2189 | "intersection-range-ns"); |
2190 | if (!intersection_range) { | |
2191 | ret = -1; | |
2192 | BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result."); | |
2193 | goto error; | |
2194 | } | |
2195 | ||
07208d85 PP |
2196 | intersection_begin = bt_value_map_borrow_entry_value( |
2197 | intersection_range, "begin"); | |
75a2cb9b JG |
2198 | if (!intersection_begin) { |
2199 | ret = -1; | |
2200 | BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result."); | |
2201 | goto error; | |
2202 | } | |
2203 | ||
07208d85 PP |
2204 | intersection_end = bt_value_map_borrow_entry_value( |
2205 | intersection_range, "end"); | |
75a2cb9b JG |
2206 | if (!intersection_end) { |
2207 | ret = -1; | |
2208 | BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result."); | |
2209 | goto error; | |
2210 | } | |
2211 | ||
2212 | value_status = bt_value_integer_get(intersection_begin, &begin); | |
2213 | if (value_status != BT_VALUE_STATUS_OK) { | |
2214 | ret = -1; | |
2215 | BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result."); | |
2216 | goto error; | |
2217 | } | |
2218 | ||
2219 | value_status = bt_value_integer_get(intersection_end, &end); | |
2220 | if (value_status != BT_VALUE_STATUS_OK) { | |
2221 | ret = -1; | |
2222 | BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result."); | |
2223 | goto error; | |
2224 | } | |
2225 | ||
2226 | if (begin < 0 || end < 0 || end < begin) { | |
2227 | BT_LOGW("Invalid trace stream intersection values: " | |
2228 | "intersection-range-ns:begin=%" PRId64 | |
2229 | ", intersection-range-ns:end=%" PRId64, | |
2230 | begin, end); | |
2231 | ret = -1; | |
2232 | goto error; | |
2233 | } | |
2234 | ||
07208d85 PP |
2235 | stream_infos = bt_value_map_borrow_entry_value(trace_info, |
2236 | "streams"); | |
75a2cb9b JG |
2237 | if (!stream_infos || !bt_value_is_array(stream_infos)) { |
2238 | ret = -1; | |
e07ab7c8 | 2239 | BT_LOGD_STR("Cannot retrieve stream information from trace in query result."); |
75a2cb9b JG |
2240 | goto error; |
2241 | } | |
2242 | ||
07208d85 | 2243 | stream_count = bt_value_array_get_size(stream_infos); |
75a2cb9b JG |
2244 | if (stream_count < 0) { |
2245 | ret = -1; | |
2246 | goto error; | |
2247 | } | |
2248 | ||
2249 | /* | |
2250 | * FIXME | |
2251 | * | |
2252 | * The first path of a stream's "paths" is currently used to | |
2253 | * associate streams/ports to a given trace intersection. | |
2254 | * | |
2255 | * This is a fragile hack as it relies on the port names | |
2256 | * being set to the various streams path. | |
2257 | * | |
2258 | * A stream name should be introduced as part of the trace-info | |
2259 | * query result. | |
2260 | */ | |
2261 | for (stream_idx = 0; stream_idx < stream_count; stream_idx++) { | |
2262 | const char *stream_path; | |
75a2cb9b JG |
2263 | |
2264 | port_id = g_new0(struct port_id, 1); | |
2265 | if (!port_id) { | |
2266 | ret = -1; | |
2267 | BT_LOGE_STR("Cannot allocate memory for port_id structure."); | |
2268 | goto error; | |
2269 | } | |
2270 | port_id->instance_name = strdup(cfg_comp->instance_name->str); | |
2271 | if (!port_id->instance_name) { | |
2272 | ret = -1; | |
2273 | BT_LOGE_STR("Cannot allocate memory for port_id component instance name."); | |
2274 | goto error; | |
2275 | } | |
2276 | ||
2277 | trace_range = g_new0(struct trace_range, 1); | |
2278 | if (!trace_range) { | |
2279 | ret = -1; | |
2280 | BT_LOGE_STR("Cannot allocate memory for trace_range structure."); | |
2281 | goto error; | |
2282 | } | |
2283 | trace_range->intersection_range_begin_ns = begin; | |
2284 | trace_range->intersection_range_end_ns = end; | |
2285 | ||
07208d85 PP |
2286 | stream_info = bt_value_array_borrow_element_by_index( |
2287 | stream_infos, stream_idx); | |
75a2cb9b JG |
2288 | if (!stream_info || !bt_value_is_map(stream_info)) { |
2289 | ret = -1; | |
2290 | BT_LOGD_STR("Cannot retrieve stream informations from trace in query result."); | |
2291 | goto error; | |
2292 | } | |
2293 | ||
07208d85 PP |
2294 | stream_paths = bt_value_map_borrow_entry_value( |
2295 | stream_info, "paths"); | |
75a2cb9b JG |
2296 | if (!stream_paths || !bt_value_is_array(stream_paths)) { |
2297 | ret = -1; | |
2298 | BT_LOGD_STR("Cannot retrieve stream paths from trace in query result."); | |
2299 | goto error; | |
2300 | } | |
2301 | ||
07208d85 PP |
2302 | stream_path_value = |
2303 | bt_value_array_borrow_element_by_index( | |
2304 | stream_paths, 0); | |
75a2cb9b JG |
2305 | if (!stream_path_value || |
2306 | !bt_value_is_string(stream_path_value)) { | |
2307 | ret = -1; | |
2308 | BT_LOGD_STR("Cannot retrieve stream path value from trace in query result."); | |
2309 | goto error; | |
2310 | } | |
2311 | ||
2312 | value_status = bt_value_string_get(stream_path_value, | |
2313 | &stream_path); | |
2314 | if (value_status != BT_VALUE_STATUS_OK) { | |
2315 | ret = -1; | |
2316 | goto error; | |
2317 | } | |
2318 | ||
2319 | port_id->port_name = strdup(stream_path); | |
2320 | if (!port_id->port_name) { | |
2321 | ret = -1; | |
2322 | BT_LOGE_STR("Cannot allocate memory for port_id port_name."); | |
2323 | goto error; | |
2324 | } | |
2325 | ||
2326 | BT_LOGD("Inserting stream intersection "); | |
2327 | ||
1ddeea34 | 2328 | g_hash_table_insert(ctx->intersections, port_id, trace_range); |
75a2cb9b JG |
2329 | |
2330 | port_id = NULL; | |
2331 | trace_range = NULL; | |
75a2cb9b | 2332 | } |
75a2cb9b JG |
2333 | } |
2334 | ||
2335 | goto end; | |
2336 | ||
2337 | error: | |
2338 | fprintf(stderr, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n", | |
2339 | bt_common_color_bold(), | |
2340 | bt_common_color_fg_yellow(), | |
2341 | path ? path : "(unknown)", | |
2342 | bt_common_color_reset()); | |
2343 | end: | |
65300d60 PP |
2344 | bt_object_put_ref(query_params); |
2345 | bt_object_put_ref(query_result); | |
75a2cb9b JG |
2346 | g_free(port_id); |
2347 | g_free(trace_range); | |
2348 | return ret; | |
2349 | } | |
2350 | ||
9009cc24 PP |
2351 | static |
2352 | int cmd_run_ctx_create_components_from_config_components( | |
2353 | struct cmd_run_ctx *ctx, GPtrArray *cfg_components) | |
2354 | { | |
2355 | size_t i; | |
2356 | struct bt_component_class *comp_cls = NULL; | |
2357 | struct bt_component *comp = NULL; | |
2358 | int ret = 0; | |
2359 | ||
2360 | for (i = 0; i < cfg_components->len; i++) { | |
2361 | struct bt_config_component *cfg_comp = | |
2362 | g_ptr_array_index(cfg_components, i); | |
2363 | GQuark quark; | |
2364 | ||
2365 | comp_cls = find_component_class(cfg_comp->plugin_name->str, | |
2366 | cfg_comp->comp_cls_name->str, cfg_comp->type); | |
2367 | if (!comp_cls) { | |
7213a328 PP |
2368 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
2369 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
2370 | cfg_comp->plugin_name->str, | |
2371 | cfg_comp->comp_cls_name->str, | |
2372 | cfg_comp->type); | |
9009cc24 PP |
2373 | fprintf(stderr, "%s%sCannot find component class %s", |
2374 | bt_common_color_bold(), | |
2375 | bt_common_color_fg_red(), | |
2376 | bt_common_color_reset()); | |
2377 | print_plugin_comp_cls_opt(stderr, | |
2378 | cfg_comp->plugin_name->str, | |
2379 | cfg_comp->comp_cls_name->str, | |
2380 | cfg_comp->type); | |
2381 | fprintf(stderr, "\n"); | |
2382 | goto error; | |
2383 | } | |
2384 | ||
36712f1d | 2385 | ret = bt_graph_add_component(ctx->graph, comp_cls, |
da91b29a PP |
2386 | cfg_comp->instance_name->str, |
2387 | bt_value_borrow_from_private(cfg_comp->params), &comp); | |
36712f1d | 2388 | if (ret) { |
7213a328 | 2389 | BT_LOGE("Cannot create component: plugin-name=\"%s\", " |
32e87ceb | 2390 | "comp-cls-name=\"%s\", comp-cls-type=%d, " |
7213a328 PP |
2391 | "comp-name=\"%s\"", |
2392 | cfg_comp->plugin_name->str, | |
2393 | cfg_comp->comp_cls_name->str, | |
2394 | cfg_comp->type, cfg_comp->instance_name->str); | |
9009cc24 PP |
2395 | fprintf(stderr, "%s%sCannot create component `%s`%s\n", |
2396 | bt_common_color_bold(), | |
2397 | bt_common_color_fg_red(), | |
2398 | cfg_comp->instance_name->str, | |
2399 | bt_common_color_reset()); | |
2400 | goto error; | |
2401 | } | |
2402 | ||
75a2cb9b JG |
2403 | if (ctx->stream_intersection_mode && |
2404 | cfg_comp->type == BT_COMPONENT_CLASS_TYPE_SOURCE) { | |
2405 | ret = set_stream_intersections(ctx, cfg_comp, comp_cls); | |
2406 | if (ret) { | |
2407 | goto error; | |
2408 | } | |
2409 | } | |
2410 | ||
7213a328 PP |
2411 | BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"", |
2412 | comp, cfg_comp->instance_name->str); | |
9009cc24 | 2413 | quark = g_quark_from_string(cfg_comp->instance_name->str); |
f6ccaed9 | 2414 | BT_ASSERT(quark > 0); |
9009cc24 | 2415 | g_hash_table_insert(ctx->components, |
71c7c95f | 2416 | GUINT_TO_POINTER(quark), comp); |
9009cc24 | 2417 | comp = NULL; |
65300d60 | 2418 | BT_OBJECT_PUT_REF_AND_RESET(comp_cls); |
9009cc24 PP |
2419 | } |
2420 | ||
2421 | goto end; | |
2422 | ||
2423 | error: | |
2424 | ret = -1; | |
2425 | ||
2426 | end: | |
65300d60 PP |
2427 | bt_object_put_ref(comp); |
2428 | bt_object_put_ref(comp_cls); | |
9009cc24 PP |
2429 | return ret; |
2430 | } | |
56a1cced | 2431 | |
9009cc24 PP |
2432 | static |
2433 | int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx) | |
2434 | { | |
2435 | int ret = 0; | |
2436 | ||
2437 | /* | |
2438 | * Make sure that, during this phase, our graph's "port added" | |
2439 | * listener does not connect ports while we are creating the | |
2440 | * components because we have a special, initial phase for | |
2441 | * this. | |
2442 | */ | |
2443 | ctx->connect_ports = false; | |
2444 | ||
2445 | ret = cmd_run_ctx_create_components_from_config_components( | |
2446 | ctx, ctx->cfg->cmd_data.run.sources); | |
2447 | if (ret) { | |
7c7c0433 | 2448 | ret = -1; |
2e339de1 JG |
2449 | goto end; |
2450 | } | |
2451 | ||
9009cc24 PP |
2452 | ret = cmd_run_ctx_create_components_from_config_components( |
2453 | ctx, ctx->cfg->cmd_data.run.filters); | |
6c2f3ee5 | 2454 | if (ret) { |
290725f7 | 2455 | ret = -1; |
fec2a9f2 JG |
2456 | goto end; |
2457 | } | |
78586d8a | 2458 | |
9009cc24 PP |
2459 | ret = cmd_run_ctx_create_components_from_config_components( |
2460 | ctx, ctx->cfg->cmd_data.run.sinks); | |
2461 | if (ret) { | |
2462 | ret = -1; | |
2463 | goto end; | |
2464 | } | |
2465 | ||
2466 | end: | |
2467 | return ret; | |
2468 | } | |
2469 | ||
2470 | static | |
2471 | int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx, | |
2472 | struct bt_component *comp, | |
2473 | int64_t (*port_count_fn)(struct bt_component *), | |
2474 | struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t)) | |
2475 | { | |
2476 | int ret = 0; | |
2477 | int64_t count; | |
2478 | uint64_t i; | |
2479 | ||
2480 | count = port_count_fn(comp); | |
f6ccaed9 | 2481 | BT_ASSERT(count >= 0); |
9009cc24 PP |
2482 | |
2483 | for (i = 0; i < count; i++) { | |
2484 | struct bt_port *upstream_port = port_by_index_fn(comp, i); | |
2485 | ||
f6ccaed9 | 2486 | BT_ASSERT(upstream_port); |
9009cc24 | 2487 | ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port); |
65300d60 | 2488 | bt_object_put_ref(upstream_port); |
9009cc24 PP |
2489 | if (ret) { |
2490 | goto end; | |
2491 | } | |
2492 | } | |
2493 | ||
2494 | end: | |
2495 | return ret; | |
2496 | } | |
2497 | ||
2498 | static | |
2499 | int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx) | |
2500 | { | |
2501 | int ret = 0; | |
2502 | GHashTableIter iter; | |
2503 | gpointer g_name_quark, g_comp; | |
2504 | ||
2505 | ctx->connect_ports = true; | |
2506 | g_hash_table_iter_init(&iter, ctx->components); | |
2507 | ||
2508 | while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) { | |
75a2cb9b JG |
2509 | int64_t (*port_count_fn)(struct bt_component *); |
2510 | struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t); | |
2511 | ||
9009cc24 | 2512 | if (bt_component_is_source(g_comp)) { |
75a2cb9b JG |
2513 | port_count_fn = |
2514 | bt_component_source_get_output_port_count; | |
2515 | port_by_index_fn = | |
2516 | bt_component_source_get_output_port_by_index; | |
9009cc24 | 2517 | } else if (bt_component_is_filter(g_comp)) { |
75a2cb9b JG |
2518 | port_count_fn = |
2519 | bt_component_filter_get_output_port_count; | |
2520 | port_by_index_fn = | |
2521 | bt_component_filter_get_output_port_by_index; | |
2522 | } else { | |
2523 | continue; | |
9009cc24 PP |
2524 | } |
2525 | ||
75a2cb9b JG |
2526 | ret = cmd_run_ctx_connect_comp_ports(ctx, |
2527 | g_comp, port_count_fn, port_by_index_fn); | |
9009cc24 PP |
2528 | if (ret) { |
2529 | goto end; | |
2530 | } | |
2531 | } | |
2532 | ||
2533 | end: | |
2534 | return ret; | |
2535 | } | |
2536 | ||
fd948396 PP |
2537 | static inline |
2538 | const char *bt_graph_status_str(enum bt_graph_status status) | |
2539 | { | |
2540 | switch (status) { | |
2541 | case BT_GRAPH_STATUS_CANCELED: | |
2542 | return "BT_GRAPH_STATUS_CANCELED"; | |
2543 | case BT_GRAPH_STATUS_AGAIN: | |
2544 | return "BT_GRAPH_STATUS_AGAIN"; | |
2545 | case BT_GRAPH_STATUS_END: | |
2546 | return "BT_GRAPH_STATUS_END"; | |
2547 | case BT_GRAPH_STATUS_OK: | |
2548 | return "BT_GRAPH_STATUS_OK"; | |
fd948396 PP |
2549 | case BT_GRAPH_STATUS_INVALID: |
2550 | return "BT_GRAPH_STATUS_INVALID"; | |
2551 | case BT_GRAPH_STATUS_NO_SINK: | |
2552 | return "BT_GRAPH_STATUS_NO_SINK"; | |
2553 | case BT_GRAPH_STATUS_ERROR: | |
2554 | return "BT_GRAPH_STATUS_ERROR"; | |
2555 | default: | |
2556 | return "(unknown)"; | |
2557 | } | |
2558 | } | |
2559 | ||
9009cc24 PP |
2560 | static |
2561 | int cmd_run(struct bt_config *cfg) | |
2562 | { | |
2563 | int ret = 0; | |
2564 | struct cmd_run_ctx ctx = { 0 }; | |
2565 | ||
9009cc24 PP |
2566 | /* Initialize the command's context and the graph object */ |
2567 | if (cmd_run_ctx_init(&ctx, cfg)) { | |
7213a328 | 2568 | BT_LOGE_STR("Cannot initialize the command's context."); |
9009cc24 PP |
2569 | fprintf(stderr, "Cannot initialize the command's context\n"); |
2570 | goto error; | |
2571 | } | |
2572 | ||
cc308374 PP |
2573 | if (canceled) { |
2574 | BT_LOGI_STR("Canceled by user before creating components."); | |
2575 | goto error; | |
2576 | } | |
2577 | ||
2578 | BT_LOGI_STR("Creating components."); | |
2579 | ||
9009cc24 PP |
2580 | /* Create the requested component instances */ |
2581 | if (cmd_run_ctx_create_components(&ctx)) { | |
7213a328 | 2582 | BT_LOGE_STR("Cannot create components."); |
9009cc24 PP |
2583 | fprintf(stderr, "Cannot create components\n"); |
2584 | goto error; | |
2585 | } | |
2586 | ||
cc308374 PP |
2587 | if (canceled) { |
2588 | BT_LOGI_STR("Canceled by user before connecting components."); | |
2589 | goto error; | |
2590 | } | |
2591 | ||
2592 | BT_LOGI_STR("Connecting components."); | |
2593 | ||
9009cc24 PP |
2594 | /* Connect the initially visible component ports */ |
2595 | if (cmd_run_ctx_connect_ports(&ctx)) { | |
7213a328 | 2596 | BT_LOGE_STR("Cannot connect initial component ports."); |
9009cc24 PP |
2597 | fprintf(stderr, "Cannot connect initial component ports\n"); |
2598 | goto error; | |
2599 | } | |
2600 | ||
5401f780 | 2601 | if (canceled) { |
cc308374 PP |
2602 | BT_LOGI_STR("Canceled by user before running the graph."); |
2603 | goto error; | |
5401f780 PP |
2604 | } |
2605 | ||
7213a328 PP |
2606 | BT_LOGI_STR("Running the graph."); |
2607 | ||
9009cc24 | 2608 | /* Run the graph */ |
fec2a9f2 | 2609 | while (true) { |
9009cc24 | 2610 | enum bt_graph_status graph_status = bt_graph_run(ctx.graph); |
61ddbc8a | 2611 | |
5669a3e7 PP |
2612 | /* |
2613 | * Reset console in case something messed with console | |
2614 | * codes during the graph's execution. | |
2615 | */ | |
2616 | printf("%s", bt_common_color_reset()); | |
2617 | fflush(stdout); | |
2618 | fprintf(stderr, "%s", bt_common_color_reset()); | |
fd948396 PP |
2619 | BT_LOGV("bt_graph_run() returned: status=%s", |
2620 | bt_graph_status_str(graph_status)); | |
2621 | ||
61ddbc8a | 2622 | switch (graph_status) { |
9009cc24 PP |
2623 | case BT_GRAPH_STATUS_OK: |
2624 | break; | |
5401f780 | 2625 | case BT_GRAPH_STATUS_CANCELED: |
fd948396 | 2626 | BT_LOGI_STR("Graph was canceled by user."); |
5401f780 | 2627 | goto error; |
61ddbc8a | 2628 | case BT_GRAPH_STATUS_AGAIN: |
5401f780 | 2629 | if (bt_graph_is_canceled(ctx.graph)) { |
fd948396 | 2630 | BT_LOGI_STR("Graph was canceled by user."); |
5401f780 PP |
2631 | goto error; |
2632 | } | |
2633 | ||
9009cc24 | 2634 | if (cfg->cmd_data.run.retry_duration_us > 0) { |
7213a328 PP |
2635 | BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: " |
2636 | "time-us=%" PRIu64, | |
2637 | cfg->cmd_data.run.retry_duration_us); | |
2638 | ||
9009cc24 | 2639 | if (usleep(cfg->cmd_data.run.retry_duration_us)) { |
cfa4637b PP |
2640 | if (bt_graph_is_canceled(ctx.graph)) { |
2641 | BT_LOGI_STR("Graph was canceled by user."); | |
2642 | goto error; | |
2643 | } | |
9009cc24 PP |
2644 | } |
2645 | } | |
78586d8a | 2646 | break; |
fec2a9f2 JG |
2647 | case BT_COMPONENT_STATUS_END: |
2648 | goto end; | |
2649 | default: | |
7213a328 PP |
2650 | BT_LOGE_STR("Graph failed to complete successfully"); |
2651 | fprintf(stderr, "Graph failed to complete successfully\n"); | |
9009cc24 | 2652 | goto error; |
78586d8a | 2653 | } |
fec2a9f2 | 2654 | } |
290725f7 | 2655 | |
9009cc24 PP |
2656 | goto end; |
2657 | ||
2658 | error: | |
2659 | if (ret == 0) { | |
2660 | ret = -1; | |
2661 | } | |
2662 | ||
11e1d048 | 2663 | end: |
9009cc24 | 2664 | cmd_run_ctx_destroy(&ctx); |
290725f7 PP |
2665 | return ret; |
2666 | } | |
2667 | ||
9009cc24 PP |
2668 | static |
2669 | void warn_command_name_and_directory_clash(struct bt_config *cfg) | |
290725f7 | 2670 | { |
9009cc24 PP |
2671 | const char *env_clash; |
2672 | ||
290725f7 PP |
2673 | if (!cfg->command_name) { |
2674 | return; | |
2675 | } | |
2676 | ||
9009cc24 PP |
2677 | env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH); |
2678 | if (env_clash && strcmp(env_clash, "0") == 0) { | |
2679 | return; | |
2680 | } | |
2681 | ||
290725f7 PP |
2682 | if (g_file_test(cfg->command_name, |
2683 | G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { | |
2684 | fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n", | |
2685 | cfg->command_name); | |
2686 | fprintf(stderr, "trace located in the local `%s` directory, please use:\n", | |
2687 | cfg->command_name); | |
2688 | fprintf(stderr, "\n"); | |
2689 | fprintf(stderr, " babeltrace convert %s [OPTIONS]\n", | |
2690 | cfg->command_name); | |
2691 | } | |
2692 | } | |
2693 | ||
7213a328 PP |
2694 | static |
2695 | void init_log_level(void) | |
2696 | { | |
c6d4d1ae | 2697 | bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL); |
7213a328 PP |
2698 | } |
2699 | ||
c6d4d1ae PP |
2700 | static |
2701 | void set_auto_log_levels(struct bt_config *cfg) | |
2702 | { | |
2703 | const char **env_var_name; | |
2704 | ||
b4565e8b PP |
2705 | /* |
2706 | * Override the configuration's default log level if | |
2707 | * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables | |
2708 | * are found for backward compatibility with legacy Babetrace 1. | |
2709 | */ | |
2710 | if (getenv("BABELTRACE_DEBUG") && | |
2711 | strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) { | |
2712 | cfg->log_level = 'V'; | |
2713 | } else if (getenv("BABELTRACE_VERBOSE") && | |
2714 | strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) { | |
2715 | cfg->log_level = 'I'; | |
2716 | } | |
2717 | ||
c6d4d1ae PP |
2718 | /* |
2719 | * Set log levels according to --debug or --verbose. For | |
2720 | * backward compatibility, --debug is more verbose than | |
2721 | * --verbose. So: | |
2722 | * | |
2723 | * --verbose: INFO log level | |
2724 | * --debug: VERBOSE log level (includes DEBUG, which is | |
2725 | * is less verbose than VERBOSE in the internal | |
2726 | * logging framework) | |
2727 | */ | |
2728 | if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) { | |
2729 | if (cfg->verbose) { | |
2730 | bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO); | |
2731 | } else if (cfg->debug) { | |
2732 | bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); | |
2733 | } else { | |
2734 | /* | |
2735 | * Set library's default log level if not | |
2736 | * explicitly specified. | |
2737 | */ | |
3efa3052 PP |
2738 | switch (cfg->log_level) { |
2739 | case 'N': | |
2740 | bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE); | |
2741 | break; | |
2742 | case 'V': | |
2743 | bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); | |
2744 | break; | |
2745 | case 'D': | |
2746 | bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG); | |
2747 | break; | |
2748 | case 'I': | |
2749 | bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO); | |
2750 | break; | |
2751 | case 'W': | |
2752 | bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN); | |
2753 | break; | |
2754 | case 'E': | |
2755 | bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR); | |
2756 | break; | |
2757 | case 'F': | |
2758 | bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL); | |
2759 | break; | |
2760 | default: | |
2761 | abort(); | |
2762 | } | |
c6d4d1ae PP |
2763 | } |
2764 | } | |
2765 | ||
2766 | if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL)) { | |
2767 | if (cfg->verbose) { | |
2768 | bt_cli_log_level = BT_LOG_INFO; | |
2769 | } else if (cfg->debug) { | |
2770 | bt_cli_log_level = BT_LOG_VERBOSE; | |
2771 | } else { | |
2772 | /* | |
2773 | * Set CLI's default log level if not explicitly | |
2774 | * specified. | |
2775 | */ | |
3efa3052 PP |
2776 | switch (cfg->log_level) { |
2777 | case 'N': | |
2778 | bt_cli_log_level = BT_LOG_NONE; | |
2779 | break; | |
2780 | case 'V': | |
2781 | bt_cli_log_level = BT_LOG_VERBOSE; | |
2782 | break; | |
2783 | case 'D': | |
2784 | bt_cli_log_level = BT_LOG_DEBUG; | |
2785 | break; | |
2786 | case 'I': | |
2787 | bt_cli_log_level = BT_LOG_INFO; | |
2788 | break; | |
2789 | case 'W': | |
2790 | bt_cli_log_level = BT_LOG_WARN; | |
2791 | break; | |
2792 | case 'E': | |
2793 | bt_cli_log_level = BT_LOG_ERROR; | |
2794 | break; | |
2795 | case 'F': | |
2796 | bt_cli_log_level = BT_LOG_FATAL; | |
2797 | break; | |
2798 | default: | |
2799 | abort(); | |
2800 | } | |
c6d4d1ae PP |
2801 | } |
2802 | } | |
2803 | ||
2804 | env_var_name = log_level_env_var_names; | |
2805 | ||
2806 | while (*env_var_name) { | |
2807 | if (!getenv(*env_var_name)) { | |
2808 | if (cfg->verbose) { | |
fbb2f0da | 2809 | g_setenv(*env_var_name, "I", 1); |
c6d4d1ae | 2810 | } else if (cfg->debug) { |
fbb2f0da | 2811 | g_setenv(*env_var_name, "V", 1); |
c6d4d1ae | 2812 | } else { |
3efa3052 PP |
2813 | char val[2] = { 0 }; |
2814 | ||
c6d4d1ae PP |
2815 | /* |
2816 | * Set module's default log level if not | |
2817 | * explicitly specified. | |
2818 | */ | |
3efa3052 | 2819 | val[0] = cfg->log_level; |
fbb2f0da | 2820 | g_setenv(*env_var_name, val, 1); |
c6d4d1ae PP |
2821 | } |
2822 | } | |
2823 | ||
2824 | env_var_name++; | |
2825 | } | |
c6d4d1ae PP |
2826 | } |
2827 | ||
290725f7 PP |
2828 | int main(int argc, const char **argv) |
2829 | { | |
2830 | int ret; | |
2831 | int retcode; | |
2832 | struct bt_config *cfg; | |
2833 | ||
7213a328 | 2834 | init_log_level(); |
65d3198f | 2835 | set_signal_handler(); |
9009cc24 PP |
2836 | init_static_data(); |
2837 | cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode); | |
290725f7 PP |
2838 | |
2839 | if (retcode < 0) { | |
2840 | /* Quit without errors; typically usage/version */ | |
2841 | retcode = 0; | |
7213a328 | 2842 | BT_LOGI_STR("Quitting without errors."); |
290725f7 PP |
2843 | goto end; |
2844 | } | |
2845 | ||
2846 | if (retcode > 0) { | |
7213a328 | 2847 | BT_LOGE("Command-line error: retcode=%d", retcode); |
290725f7 PP |
2848 | goto end; |
2849 | } | |
2850 | ||
2851 | if (!cfg) { | |
7213a328 | 2852 | BT_LOGE_STR("Failed to create a valid Babeltrace configuration."); |
290725f7 | 2853 | fprintf(stderr, "Failed to create Babeltrace configuration\n"); |
db0f160a | 2854 | retcode = 1; |
290725f7 PP |
2855 | goto end; |
2856 | } | |
2857 | ||
c6d4d1ae | 2858 | set_auto_log_levels(cfg); |
290725f7 PP |
2859 | print_cfg(cfg); |
2860 | ||
db0f160a | 2861 | if (cfg->command_needs_plugins) { |
da91b29a PP |
2862 | ret = load_all_plugins( |
2863 | bt_value_borrow_from_private(cfg->plugin_paths)); | |
db0f160a | 2864 | if (ret) { |
7213a328 | 2865 | BT_LOGE("Failed to load plugins: ret=%d", ret); |
db0f160a PP |
2866 | retcode = 1; |
2867 | goto end; | |
2868 | } | |
2869 | } | |
2870 | ||
7213a328 PP |
2871 | BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"", |
2872 | cfg->command, cfg->command_name); | |
2873 | ||
290725f7 | 2874 | switch (cfg->command) { |
db0f160a PP |
2875 | case BT_CONFIG_COMMAND_RUN: |
2876 | ret = cmd_run(cfg); | |
290725f7 PP |
2877 | break; |
2878 | case BT_CONFIG_COMMAND_LIST_PLUGINS: | |
2879 | ret = cmd_list_plugins(cfg); | |
2880 | break; | |
22e22462 PP |
2881 | case BT_CONFIG_COMMAND_HELP: |
2882 | ret = cmd_help(cfg); | |
2883 | break; | |
a67681c1 PP |
2884 | case BT_CONFIG_COMMAND_QUERY: |
2885 | ret = cmd_query(cfg); | |
63ce0e1d | 2886 | break; |
db0f160a PP |
2887 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
2888 | ret = cmd_print_ctf_metadata(cfg); | |
2889 | break; | |
2890 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: | |
2891 | ret = cmd_print_lttng_live_sessions(cfg); | |
2892 | break; | |
290725f7 | 2893 | default: |
0fbb9a9f PP |
2894 | BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command); |
2895 | abort(); | |
290725f7 PP |
2896 | } |
2897 | ||
d26ef3e3 PP |
2898 | BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d", |
2899 | cfg->command, cfg->command_name, ret); | |
290725f7 PP |
2900 | warn_command_name_and_directory_clash(cfg); |
2901 | retcode = ret ? 1 : 0; | |
2902 | ||
2903 | end: | |
65300d60 | 2904 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 | 2905 | fini_static_data(); |
290725f7 | 2906 | return retcode; |
4c8bfb7e | 2907 | } |