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