Commit | Line | Data |
---|---|---|
34ac0e6c MD |
1 | /* |
2 | * babeltrace.c | |
3 | * | |
4 | * Babeltrace Trace Converter | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
34ac0e6c MD |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
c462e188 MD |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
34ac0e6c | 27 | */ |
4c8bfb7e | 28 | |
95d36295 | 29 | #include <babeltrace/babeltrace.h> |
7c7c0433 | 30 | #include <babeltrace/plugin/plugin.h> |
290725f7 | 31 | #include <babeltrace/common-internal.h> |
b2e0c907 PP |
32 | #include <babeltrace/graph/component.h> |
33 | #include <babeltrace/graph/component-source.h> | |
34 | #include <babeltrace/graph/component-sink.h> | |
35 | #include <babeltrace/graph/component-filter.h> | |
36 | #include <babeltrace/graph/component-class.h> | |
37 | #include <babeltrace/graph/port.h> | |
38 | #include <babeltrace/graph/graph.h> | |
39 | #include <babeltrace/graph/connection.h> | |
40 | #include <babeltrace/graph/notification-iterator.h> | |
2e339de1 JG |
41 | #include <babeltrace/ref.h> |
42 | #include <babeltrace/values.h> | |
7213a328 | 43 | #include <babeltrace/logging.h> |
a8ff38ef | 44 | #include <unistd.h> |
34ac0e6c | 45 | #include <stdlib.h> |
7f26a816 PP |
46 | #include <popt.h> |
47 | #include <string.h> | |
48 | #include <stdio.h> | |
33b34c43 | 49 | #include <glib.h> |
dc3fffef | 50 | #include <inttypes.h> |
7cdc2bab | 51 | #include <unistd.h> |
5401f780 | 52 | #include <signal.h> |
c42c79ea | 53 | #include "babeltrace-cfg.h" |
9009cc24 PP |
54 | #include "babeltrace-cfg-cli-args.h" |
55 | #include "babeltrace-cfg-cli-args-default.h" | |
56 | ||
7213a328 PP |
57 | #define BT_LOG_TAG "CLI" |
58 | #include "logging.h" | |
59 | ||
9009cc24 | 60 | #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH" |
34ac0e6c | 61 | |
5401f780 PP |
62 | /* Application's processing graph (weak) */ |
63 | static struct bt_graph *the_graph; | |
64 | static bool canceled = false; | |
65 | ||
33b34c43 PP |
66 | GPtrArray *loaded_plugins; |
67 | ||
7213a328 PP |
68 | BT_HIDDEN |
69 | int bt_cli_log_level = BT_LOG_NONE; | |
70 | ||
5401f780 PP |
71 | static |
72 | void sigint_handler(int signum) | |
73 | { | |
74 | if (signum != SIGINT) { | |
75 | return; | |
76 | } | |
77 | ||
78 | if (the_graph) { | |
79 | bt_graph_cancel(the_graph); | |
80 | } | |
81 | ||
82 | canceled = true; | |
83 | } | |
84 | ||
33b34c43 | 85 | static |
9009cc24 | 86 | void init_static_data(void) |
33b34c43 | 87 | { |
9009cc24 | 88 | loaded_plugins = g_ptr_array_new_with_free_func(bt_put); |
33b34c43 PP |
89 | } |
90 | ||
91 | static | |
9009cc24 | 92 | void fini_static_data(void) |
33b34c43 PP |
93 | { |
94 | g_ptr_array_free(loaded_plugins, TRUE); | |
95 | } | |
96 | ||
97 | static | |
98 | struct bt_plugin *find_plugin(const char *name) | |
99 | { | |
100 | int i; | |
101 | struct bt_plugin *plugin = NULL; | |
102 | ||
7213a328 PP |
103 | assert(name); |
104 | BT_LOGD("Finding plugin: name=\"%s\"", name); | |
105 | ||
33b34c43 PP |
106 | for (i = 0; i < loaded_plugins->len; i++) { |
107 | plugin = g_ptr_array_index(loaded_plugins, i); | |
108 | ||
109 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { | |
110 | break; | |
111 | } | |
112 | ||
113 | plugin = NULL; | |
114 | } | |
115 | ||
7213a328 PP |
116 | if (BT_LOG_ON_DEBUG) { |
117 | if (plugin) { | |
118 | BT_LOGD("Found plugin: plugin-addr=%p", plugin); | |
119 | } else { | |
120 | BT_LOGD("Cannot find plugin."); | |
121 | } | |
122 | } | |
123 | ||
33b34c43 PP |
124 | return bt_get(plugin); |
125 | } | |
126 | ||
127 | static | |
128 | struct bt_component_class *find_component_class(const char *plugin_name, | |
129 | const char *comp_class_name, | |
d3e4dcd8 | 130 | enum bt_component_class_type comp_class_type) |
33b34c43 PP |
131 | { |
132 | struct bt_component_class *comp_class = NULL; | |
7213a328 PP |
133 | struct bt_plugin *plugin; |
134 | ||
135 | BT_LOGD("Finding component class: plugin-name=\"%s\", " | |
136 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
137 | plugin_name, comp_class_name, comp_class_type); | |
138 | ||
139 | plugin = find_plugin(plugin_name); | |
33b34c43 PP |
140 | |
141 | if (!plugin) { | |
142 | goto end; | |
143 | } | |
144 | ||
145 | comp_class = bt_plugin_get_component_class_by_name_and_type(plugin, | |
146 | comp_class_name, comp_class_type); | |
147 | BT_PUT(plugin); | |
7213a328 | 148 | |
33b34c43 | 149 | end: |
7213a328 PP |
150 | if (BT_LOG_ON_DEBUG) { |
151 | if (comp_class) { | |
152 | BT_LOGD("Found component class: comp-cls-addr=%p", | |
153 | comp_class); | |
154 | } else { | |
155 | BT_LOGD("Cannot find component class."); | |
156 | } | |
157 | } | |
158 | ||
33b34c43 PP |
159 | return comp_class; |
160 | } | |
6c2f3ee5 | 161 | |
c42c79ea | 162 | static |
7213a328 | 163 | void print_indent(FILE *fp, size_t indent) |
c42c79ea PP |
164 | { |
165 | size_t i; | |
166 | ||
167 | for (i = 0; i < indent; i++) { | |
7213a328 | 168 | fprintf(fp, " "); |
c42c79ea PP |
169 | } |
170 | } | |
171 | ||
87796884 PP |
172 | static |
173 | const char *component_type_str(enum bt_component_class_type type) | |
174 | { | |
175 | switch (type) { | |
176 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
177 | return "source"; | |
178 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
179 | return "sink"; | |
180 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
181 | return "filter"; | |
182 | case BT_COMPONENT_CLASS_TYPE_UNKNOWN: | |
183 | default: | |
184 | return "unknown"; | |
185 | } | |
186 | } | |
187 | ||
9009cc24 PP |
188 | static |
189 | void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name, | |
87796884 PP |
190 | const char *comp_cls_name, enum bt_component_class_type type) |
191 | { | |
9009cc24 PP |
192 | GString *shell_plugin_name = NULL; |
193 | GString *shell_comp_cls_name = NULL; | |
87796884 | 194 | |
9009cc24 | 195 | shell_plugin_name = bt_common_shell_quote(plugin_name, false); |
87796884 PP |
196 | if (!shell_plugin_name) { |
197 | goto end; | |
198 | } | |
199 | ||
9009cc24 | 200 | shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false); |
87796884 PP |
201 | if (!shell_comp_cls_name) { |
202 | goto end; | |
203 | } | |
204 | ||
205 | fprintf(fh, "%s%s--%s%s %s'%s%s%s%s.%s%s%s'", | |
206 | bt_common_color_bold(), | |
207 | bt_common_color_fg_cyan(), | |
208 | component_type_str(type), | |
209 | bt_common_color_reset(), | |
210 | bt_common_color_fg_default(), | |
211 | bt_common_color_bold(), | |
212 | bt_common_color_fg_blue(), | |
9009cc24 | 213 | shell_plugin_name->str, |
87796884 PP |
214 | bt_common_color_fg_default(), |
215 | bt_common_color_fg_yellow(), | |
9009cc24 | 216 | shell_comp_cls_name->str, |
87796884 PP |
217 | bt_common_color_reset()); |
218 | ||
219 | end: | |
9009cc24 PP |
220 | if (shell_plugin_name) { |
221 | g_string_free(shell_plugin_name, TRUE); | |
222 | } | |
223 | ||
224 | if (shell_comp_cls_name) { | |
225 | g_string_free(shell_comp_cls_name, TRUE); | |
226 | } | |
87796884 PP |
227 | } |
228 | ||
c42c79ea | 229 | static |
7213a328 | 230 | void print_value(FILE *, struct bt_value *, size_t); |
c42c79ea | 231 | |
c1081aa6 | 232 | static |
7213a328 PP |
233 | void print_value_rec(FILE *, struct bt_value *, size_t); |
234 | ||
235 | struct print_map_value_data { | |
236 | size_t indent; | |
237 | FILE *fp; | |
238 | }; | |
c1081aa6 | 239 | |
c42c79ea | 240 | static |
c55a9f58 | 241 | bt_bool print_map_value(const char *key, struct bt_value *object, void *data) |
c42c79ea | 242 | { |
7213a328 | 243 | struct print_map_value_data *print_map_value_data = data; |
290725f7 | 244 | |
7213a328 PP |
245 | print_indent(print_map_value_data->fp, print_map_value_data->indent); |
246 | fprintf(print_map_value_data->fp, "%s: ", key); | |
290725f7 PP |
247 | |
248 | if (bt_value_is_array(object) && | |
249 | bt_value_array_is_empty(object)) { | |
7213a328 | 250 | fprintf(print_map_value_data->fp, "[ ]\n"); |
290725f7 PP |
251 | return true; |
252 | } | |
253 | ||
254 | if (bt_value_is_map(object) && | |
255 | bt_value_map_is_empty(object)) { | |
7213a328 | 256 | fprintf(print_map_value_data->fp, "{ }\n"); |
290725f7 PP |
257 | return true; |
258 | } | |
c42c79ea | 259 | |
290725f7 PP |
260 | if (bt_value_is_array(object) || |
261 | bt_value_is_map(object)) { | |
7213a328 | 262 | fprintf(print_map_value_data->fp, "\n"); |
290725f7 | 263 | } |
c42c79ea | 264 | |
7213a328 PP |
265 | print_value_rec(print_map_value_data->fp, object, |
266 | print_map_value_data->indent + 2); | |
c55a9f58 | 267 | return BT_TRUE; |
c42c79ea PP |
268 | } |
269 | ||
270 | static | |
7213a328 | 271 | void print_value_rec(FILE *fp, struct bt_value *value, size_t indent) |
c42c79ea | 272 | { |
c55a9f58 | 273 | bt_bool bool_val; |
c42c79ea PP |
274 | int64_t int_val; |
275 | double dbl_val; | |
276 | const char *str_val; | |
277 | int size; | |
278 | int i; | |
279 | ||
280 | if (!value) { | |
281 | return; | |
282 | } | |
283 | ||
c42c79ea PP |
284 | switch (bt_value_get_type(value)) { |
285 | case BT_VALUE_TYPE_NULL: | |
7213a328 | 286 | fprintf(fp, "%snull%s\n", bt_common_color_bold(), |
c1081aa6 | 287 | bt_common_color_reset()); |
c42c79ea PP |
288 | break; |
289 | case BT_VALUE_TYPE_BOOL: | |
290 | bt_value_bool_get(value, &bool_val); | |
7213a328 | 291 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
c1081aa6 PP |
292 | bt_common_color_fg_cyan(), bool_val ? "yes" : "no", |
293 | bt_common_color_reset()); | |
c42c79ea PP |
294 | break; |
295 | case BT_VALUE_TYPE_INTEGER: | |
296 | bt_value_integer_get(value, &int_val); | |
7213a328 | 297 | fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(), |
c1081aa6 PP |
298 | bt_common_color_fg_red(), int_val, |
299 | bt_common_color_reset()); | |
c42c79ea PP |
300 | break; |
301 | case BT_VALUE_TYPE_FLOAT: | |
302 | bt_value_float_get(value, &dbl_val); | |
7213a328 | 303 | fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(), |
c1081aa6 PP |
304 | bt_common_color_fg_red(), dbl_val, |
305 | bt_common_color_reset()); | |
c42c79ea PP |
306 | break; |
307 | case BT_VALUE_TYPE_STRING: | |
308 | bt_value_string_get(value, &str_val); | |
7213a328 | 309 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
c1081aa6 PP |
310 | bt_common_color_fg_green(), str_val, |
311 | bt_common_color_reset()); | |
c42c79ea PP |
312 | break; |
313 | case BT_VALUE_TYPE_ARRAY: | |
314 | size = bt_value_array_size(value); | |
290725f7 PP |
315 | assert(size >= 0); |
316 | ||
317 | if (size == 0) { | |
7213a328 PP |
318 | print_indent(fp, indent); |
319 | fprintf(fp, "[ ]\n"); | |
290725f7 PP |
320 | break; |
321 | } | |
c42c79ea PP |
322 | |
323 | for (i = 0; i < size; i++) { | |
324 | struct bt_value *element = | |
325 | bt_value_array_get(value, i); | |
326 | ||
290725f7 | 327 | assert(element); |
7213a328 PP |
328 | print_indent(fp, indent); |
329 | fprintf(fp, "- "); | |
290725f7 PP |
330 | |
331 | if (bt_value_is_array(element) && | |
332 | bt_value_array_is_empty(element)) { | |
7213a328 | 333 | fprintf(fp, "[ ]\n"); |
290725f7 PP |
334 | continue; |
335 | } | |
336 | ||
337 | if (bt_value_is_map(element) && | |
338 | bt_value_map_is_empty(element)) { | |
7213a328 | 339 | fprintf(fp, "{ }\n"); |
290725f7 PP |
340 | continue; |
341 | } | |
342 | ||
343 | if (bt_value_is_array(element) || | |
344 | bt_value_is_map(element)) { | |
7213a328 | 345 | fprintf(fp, "\n"); |
290725f7 PP |
346 | } |
347 | ||
7213a328 | 348 | print_value_rec(fp, element, indent + 2); |
c42c79ea PP |
349 | BT_PUT(element); |
350 | } | |
c42c79ea PP |
351 | break; |
352 | case BT_VALUE_TYPE_MAP: | |
7213a328 PP |
353 | { |
354 | struct print_map_value_data data = { | |
355 | .indent = indent, | |
356 | .fp = fp, | |
357 | }; | |
358 | ||
c42c79ea | 359 | if (bt_value_map_is_empty(value)) { |
7213a328 PP |
360 | print_indent(fp, indent); |
361 | fprintf(fp, "{ }\n"); | |
290725f7 | 362 | break; |
c42c79ea PP |
363 | } |
364 | ||
7213a328 | 365 | bt_value_map_foreach(value, print_map_value, &data); |
c42c79ea | 366 | break; |
7213a328 | 367 | } |
c42c79ea | 368 | default: |
0fbb9a9f | 369 | abort(); |
c42c79ea PP |
370 | } |
371 | } | |
372 | ||
c1081aa6 | 373 | static |
7213a328 | 374 | void print_value(FILE *fp, struct bt_value *value, size_t indent) |
c1081aa6 PP |
375 | { |
376 | if (!bt_value_is_array(value) && !bt_value_is_map(value)) { | |
7213a328 | 377 | print_indent(fp, indent); |
c1081aa6 PP |
378 | } |
379 | ||
7213a328 | 380 | print_value_rec(fp, value, indent); |
c1081aa6 PP |
381 | } |
382 | ||
c42c79ea PP |
383 | static |
384 | void print_bt_config_component(struct bt_config_component *bt_config_component) | |
385 | { | |
7213a328 PP |
386 | fprintf(stderr, " "); |
387 | print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str, | |
db0f160a | 388 | bt_config_component->comp_cls_name->str, |
87796884 | 389 | bt_config_component->type); |
7213a328 | 390 | fprintf(stderr, ":\n"); |
3b6cfcc5 PP |
391 | |
392 | if (bt_config_component->instance_name->len > 0) { | |
7213a328 | 393 | fprintf(stderr, " Name: %s\n", |
3b6cfcc5 PP |
394 | bt_config_component->instance_name->str); |
395 | } | |
396 | ||
7213a328 PP |
397 | fprintf(stderr, " Parameters:\n"); |
398 | print_value(stderr, bt_config_component->params, 8); | |
c42c79ea PP |
399 | } |
400 | ||
401 | static | |
402 | void print_bt_config_components(GPtrArray *array) | |
403 | { | |
404 | size_t i; | |
405 | ||
406 | for (i = 0; i < array->len; i++) { | |
407 | struct bt_config_component *cfg_component = | |
e5bc7f81 | 408 | bt_config_get_component(array, i); |
c42c79ea PP |
409 | print_bt_config_component(cfg_component); |
410 | BT_PUT(cfg_component); | |
411 | } | |
412 | } | |
413 | ||
290725f7 PP |
414 | static |
415 | void print_plugin_paths(struct bt_value *plugin_paths) | |
416 | { | |
7213a328 PP |
417 | fprintf(stderr, " Plugin paths:\n"); |
418 | print_value(stderr, plugin_paths, 4); | |
290725f7 PP |
419 | } |
420 | ||
421 | static | |
db0f160a | 422 | void print_cfg_run(struct bt_config *cfg) |
290725f7 | 423 | { |
ebba3338 PP |
424 | size_t i; |
425 | ||
db0f160a | 426 | print_plugin_paths(cfg->plugin_paths); |
7213a328 | 427 | fprintf(stderr, " Source component instances:\n"); |
db0f160a | 428 | print_bt_config_components(cfg->cmd_data.run.sources); |
ebba3338 | 429 | |
db0f160a | 430 | if (cfg->cmd_data.run.filters->len > 0) { |
7213a328 | 431 | fprintf(stderr, " Filter component instances:\n"); |
db0f160a | 432 | print_bt_config_components(cfg->cmd_data.run.filters); |
ebba3338 PP |
433 | } |
434 | ||
7213a328 | 435 | fprintf(stderr, " Sink component instances:\n"); |
db0f160a | 436 | print_bt_config_components(cfg->cmd_data.run.sinks); |
7213a328 | 437 | fprintf(stderr, " Connections:\n"); |
ebba3338 | 438 | |
db0f160a | 439 | for (i = 0; i < cfg->cmd_data.run.connections->len; i++) { |
ebba3338 | 440 | struct bt_config_connection *cfg_connection = |
db0f160a | 441 | g_ptr_array_index(cfg->cmd_data.run.connections, |
ebba3338 PP |
442 | i); |
443 | ||
7213a328 | 444 | fprintf(stderr, " %s%s%s -> %s%s%s\n", |
9009cc24 PP |
445 | cfg_connection->upstream_comp_name->str, |
446 | cfg_connection->upstream_port_glob->len > 0 ? "." : "", | |
447 | cfg_connection->upstream_port_glob->str, | |
448 | cfg_connection->downstream_comp_name->str, | |
449 | cfg_connection->downstream_port_glob->len > 0 ? "." : "", | |
450 | cfg_connection->downstream_port_glob->str); | |
ebba3338 | 451 | } |
290725f7 PP |
452 | } |
453 | ||
454 | static | |
455 | void print_cfg_list_plugins(struct bt_config *cfg) | |
456 | { | |
db0f160a | 457 | print_plugin_paths(cfg->plugin_paths); |
290725f7 PP |
458 | } |
459 | ||
c1081aa6 PP |
460 | static |
461 | void print_cfg_help(struct bt_config *cfg) | |
462 | { | |
db0f160a PP |
463 | print_plugin_paths(cfg->plugin_paths); |
464 | } | |
465 | ||
466 | static | |
467 | void print_cfg_print_ctf_metadata(struct bt_config *cfg) | |
468 | { | |
469 | print_plugin_paths(cfg->plugin_paths); | |
7213a328 PP |
470 | fprintf(stderr, " Path: %s\n", |
471 | cfg->cmd_data.print_ctf_metadata.path->str); | |
db0f160a PP |
472 | } |
473 | ||
474 | static | |
475 | void print_cfg_print_lttng_live_sessions(struct bt_config *cfg) | |
476 | { | |
477 | print_plugin_paths(cfg->plugin_paths); | |
7213a328 PP |
478 | fprintf(stderr, " URL: %s\n", |
479 | cfg->cmd_data.print_lttng_live_sessions.url->str); | |
c1081aa6 PP |
480 | } |
481 | ||
482 | static | |
a67681c1 | 483 | void print_cfg_query(struct bt_config *cfg) |
c1081aa6 | 484 | { |
db0f160a | 485 | print_plugin_paths(cfg->plugin_paths); |
7213a328 PP |
486 | fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str); |
487 | fprintf(stderr, " Component class:\n"); | |
a67681c1 | 488 | print_bt_config_component(cfg->cmd_data.query.cfg_component); |
c1081aa6 PP |
489 | } |
490 | ||
c42c79ea PP |
491 | static |
492 | void print_cfg(struct bt_config *cfg) | |
493 | { | |
7213a328 | 494 | if (!BT_LOG_ON_INFO) { |
00447e45 PP |
495 | return; |
496 | } | |
497 | ||
7213a328 PP |
498 | BT_LOGI_STR("Configuration:"); |
499 | fprintf(stderr, " Debug mode: %s\n", cfg->debug ? "yes" : "no"); | |
500 | fprintf(stderr, " Verbose mode: %s\n", cfg->verbose ? "yes" : "no"); | |
290725f7 PP |
501 | |
502 | switch (cfg->command) { | |
db0f160a PP |
503 | case BT_CONFIG_COMMAND_RUN: |
504 | print_cfg_run(cfg); | |
290725f7 PP |
505 | break; |
506 | case BT_CONFIG_COMMAND_LIST_PLUGINS: | |
507 | print_cfg_list_plugins(cfg); | |
c1081aa6 PP |
508 | break; |
509 | case BT_CONFIG_COMMAND_HELP: | |
510 | print_cfg_help(cfg); | |
511 | break; | |
a67681c1 PP |
512 | case BT_CONFIG_COMMAND_QUERY: |
513 | print_cfg_query(cfg); | |
290725f7 | 514 | break; |
db0f160a PP |
515 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
516 | print_cfg_print_ctf_metadata(cfg); | |
517 | break; | |
518 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: | |
519 | print_cfg_print_lttng_live_sessions(cfg); | |
520 | break; | |
290725f7 | 521 | default: |
0fbb9a9f | 522 | abort(); |
290725f7 | 523 | } |
c42c79ea PP |
524 | } |
525 | ||
33b34c43 | 526 | static |
a8ff38ef | 527 | void add_to_loaded_plugins(struct bt_plugin_set *plugin_set) |
98ecef32 | 528 | { |
544d0515 PP |
529 | int64_t i; |
530 | int64_t count; | |
a8ff38ef PP |
531 | |
532 | count = bt_plugin_set_get_plugin_count(plugin_set); | |
533 | assert(count >= 0); | |
534 | ||
535 | for (i = 0; i < count; i++) { | |
536 | struct bt_plugin *plugin = | |
537 | bt_plugin_set_get_plugin(plugin_set, i); | |
33b34c43 PP |
538 | struct bt_plugin *loaded_plugin = |
539 | find_plugin(bt_plugin_get_name(plugin)); | |
540 | ||
a8ff38ef PP |
541 | assert(plugin); |
542 | ||
33b34c43 | 543 | if (loaded_plugin) { |
7213a328 PP |
544 | BT_LOGI("Not using plugin: another one already exists with the same name: " |
545 | "plugin-name=\"%s\", plugin-path=\"%s\", " | |
546 | "existing-plugin-path=\"%s\"", | |
547 | bt_plugin_get_name(plugin), | |
548 | bt_plugin_get_path(plugin), | |
549 | bt_plugin_get_path(loaded_plugin)); | |
a8ff38ef | 550 | bt_put(loaded_plugin); |
33b34c43 | 551 | } else { |
a8ff38ef | 552 | /* Add to global array. */ |
7213a328 PP |
553 | BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"", |
554 | bt_plugin_get_name(plugin)); | |
a8ff38ef | 555 | g_ptr_array_add(loaded_plugins, bt_get(plugin)); |
33b34c43 | 556 | } |
a8ff38ef PP |
557 | |
558 | bt_put(plugin); | |
33b34c43 PP |
559 | } |
560 | } | |
561 | ||
562 | static | |
290725f7 | 563 | int load_dynamic_plugins(struct bt_value *plugin_paths) |
33b34c43 PP |
564 | { |
565 | int nr_paths, i, ret = 0; | |
98ecef32 | 566 | |
290725f7 | 567 | nr_paths = bt_value_array_size(plugin_paths); |
98ecef32 | 568 | if (nr_paths < 0) { |
7213a328 | 569 | BT_LOGE_STR("Cannot load dynamic plugins: no plugin path."); |
33b34c43 PP |
570 | ret = -1; |
571 | goto end; | |
98ecef32 | 572 | } |
33b34c43 | 573 | |
7213a328 PP |
574 | BT_LOGI("Loading dynamic plugins."); |
575 | ||
98ecef32 MD |
576 | for (i = 0; i < nr_paths; i++) { |
577 | struct bt_value *plugin_path_value = NULL; | |
578 | const char *plugin_path; | |
a8ff38ef | 579 | struct bt_plugin_set *plugin_set; |
98ecef32 | 580 | |
290725f7 | 581 | plugin_path_value = bt_value_array_get(plugin_paths, i); |
7213a328 PP |
582 | bt_value_string_get(plugin_path_value, &plugin_path); |
583 | assert(plugin_path); | |
50ad9320 PP |
584 | |
585 | /* | |
586 | * Skip this if the directory does not exist because | |
587 | * bt_plugin_create_all_from_dir() expects an existing | |
588 | * directory. | |
589 | */ | |
590 | if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) { | |
591 | BT_LOGV("Skipping nonexistent directory path: " | |
592 | "path=\"%s\"", plugin_path); | |
593 | BT_PUT(plugin_path_value); | |
594 | continue; | |
595 | } | |
596 | ||
a8ff38ef PP |
597 | plugin_set = bt_plugin_create_all_from_dir(plugin_path, false); |
598 | if (!plugin_set) { | |
7213a328 | 599 | BT_LOGD("Unable to load dynamic plugins: path=\"%s\"", |
98ecef32 | 600 | plugin_path); |
33b34c43 PP |
601 | BT_PUT(plugin_path_value); |
602 | continue; | |
98ecef32 | 603 | } |
33b34c43 | 604 | |
a8ff38ef PP |
605 | add_to_loaded_plugins(plugin_set); |
606 | bt_put(plugin_set); | |
98ecef32 MD |
607 | BT_PUT(plugin_path_value); |
608 | } | |
33b34c43 PP |
609 | end: |
610 | return ret; | |
611 | } | |
612 | ||
613 | static | |
614 | int load_static_plugins(void) | |
615 | { | |
616 | int ret = 0; | |
a8ff38ef | 617 | struct bt_plugin_set *plugin_set; |
33b34c43 | 618 | |
7213a328 | 619 | BT_LOGI("Loading static plugins."); |
a8ff38ef PP |
620 | plugin_set = bt_plugin_create_all_from_static(); |
621 | if (!plugin_set) { | |
7213a328 | 622 | BT_LOGE("Unable to load static plugins."); |
33b34c43 PP |
623 | ret = -1; |
624 | goto end; | |
625 | } | |
626 | ||
a8ff38ef PP |
627 | add_to_loaded_plugins(plugin_set); |
628 | bt_put(plugin_set); | |
33b34c43 PP |
629 | end: |
630 | return ret; | |
98ecef32 MD |
631 | } |
632 | ||
9009cc24 PP |
633 | static |
634 | int load_all_plugins(struct bt_value *plugin_paths) | |
290725f7 PP |
635 | { |
636 | int ret = 0; | |
33b34c43 | 637 | |
290725f7 | 638 | if (load_dynamic_plugins(plugin_paths)) { |
290725f7 | 639 | ret = -1; |
c1870f57 JG |
640 | goto end; |
641 | } | |
642 | ||
290725f7 | 643 | if (load_static_plugins()) { |
290725f7 | 644 | ret = -1; |
c1870f57 JG |
645 | goto end; |
646 | } | |
647 | ||
7213a328 PP |
648 | BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len); |
649 | ||
290725f7 PP |
650 | end: |
651 | return ret; | |
652 | } | |
653 | ||
9009cc24 PP |
654 | static |
655 | void print_plugin_info(struct bt_plugin *plugin) | |
22e22462 PP |
656 | { |
657 | unsigned int major, minor, patch; | |
658 | const char *extra; | |
659 | enum bt_plugin_status version_status; | |
660 | const char *plugin_name; | |
661 | const char *path; | |
662 | const char *author; | |
663 | const char *license; | |
664 | const char *plugin_description; | |
665 | ||
666 | plugin_name = bt_plugin_get_name(plugin); | |
667 | path = bt_plugin_get_path(plugin); | |
668 | author = bt_plugin_get_author(plugin); | |
669 | license = bt_plugin_get_license(plugin); | |
670 | plugin_description = bt_plugin_get_description(plugin); | |
671 | version_status = bt_plugin_get_version(plugin, &major, &minor, | |
672 | &patch, &extra); | |
673 | printf("%s%s%s%s:\n", bt_common_color_bold(), | |
674 | bt_common_color_fg_blue(), plugin_name, | |
675 | bt_common_color_reset()); | |
676 | printf(" %sPath%s: %s\n", bt_common_color_bold(), | |
677 | bt_common_color_reset(), path ? path : "(None)"); | |
678 | ||
679 | if (version_status == BT_PLUGIN_STATUS_OK) { | |
680 | printf(" %sVersion%s: %u.%u.%u", | |
681 | bt_common_color_bold(), bt_common_color_reset(), | |
682 | major, minor, patch); | |
683 | ||
684 | if (extra) { | |
685 | printf("%s", extra); | |
686 | } | |
687 | ||
688 | printf("\n"); | |
689 | } | |
690 | ||
691 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), | |
692 | bt_common_color_reset(), | |
693 | plugin_description ? plugin_description : "(None)"); | |
694 | printf(" %sAuthor%s: %s\n", bt_common_color_bold(), | |
695 | bt_common_color_reset(), author ? author : "(Unknown)"); | |
696 | printf(" %sLicense%s: %s\n", bt_common_color_bold(), | |
697 | bt_common_color_reset(), | |
698 | license ? license : "(Unknown)"); | |
699 | } | |
700 | ||
9009cc24 PP |
701 | static |
702 | int cmd_query(struct bt_config *cfg) | |
63ce0e1d | 703 | { |
db95fa29 | 704 | int ret = 0; |
63ce0e1d PP |
705 | struct bt_component_class *comp_cls = NULL; |
706 | struct bt_value *results = NULL; | |
707 | ||
a67681c1 | 708 | comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str, |
db0f160a | 709 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
a67681c1 | 710 | cfg->cmd_data.query.cfg_component->type); |
63ce0e1d | 711 | if (!comp_cls) { |
7213a328 PP |
712 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
713 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
714 | cfg->cmd_data.query.cfg_component->plugin_name->str, | |
715 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, | |
716 | cfg->cmd_data.query.cfg_component->type); | |
63ce0e1d PP |
717 | fprintf(stderr, "%s%sCannot find component class %s", |
718 | bt_common_color_bold(), | |
719 | bt_common_color_fg_red(), | |
720 | bt_common_color_reset()); | |
721 | print_plugin_comp_cls_opt(stderr, | |
a67681c1 | 722 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
db0f160a | 723 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
a67681c1 | 724 | cfg->cmd_data.query.cfg_component->type); |
63ce0e1d PP |
725 | fprintf(stderr, "\n"); |
726 | ret = -1; | |
727 | goto end; | |
728 | } | |
729 | ||
a67681c1 PP |
730 | results = bt_component_class_query(comp_cls, |
731 | cfg->cmd_data.query.object->str, | |
732 | cfg->cmd_data.query.cfg_component->params); | |
63ce0e1d | 733 | if (!results) { |
7213a328 PP |
734 | BT_LOGE("Failed to query component class: plugin-name=\"%s\", " |
735 | "comp-cls-name=\"%s\", comp-cls-type=%d " | |
736 | "object=\"%s\"", | |
737 | cfg->cmd_data.query.cfg_component->plugin_name->str, | |
738 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, | |
739 | cfg->cmd_data.query.cfg_component->type, | |
740 | cfg->cmd_data.query.object->str); | |
63ce0e1d PP |
741 | fprintf(stderr, "%s%sFailed to query info to %s", |
742 | bt_common_color_bold(), | |
743 | bt_common_color_fg_red(), | |
744 | bt_common_color_reset()); | |
745 | print_plugin_comp_cls_opt(stderr, | |
a67681c1 | 746 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
db0f160a | 747 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
a67681c1 PP |
748 | cfg->cmd_data.query.cfg_component->type); |
749 | fprintf(stderr, "%s%s with object `%s`%s\n", | |
63ce0e1d PP |
750 | bt_common_color_bold(), |
751 | bt_common_color_fg_red(), | |
a67681c1 | 752 | cfg->cmd_data.query.object->str, |
63ce0e1d PP |
753 | bt_common_color_reset()); |
754 | ret = -1; | |
755 | goto end; | |
756 | } | |
757 | ||
7213a328 | 758 | print_value(stdout, results, 0); |
63ce0e1d PP |
759 | |
760 | end: | |
761 | bt_put(comp_cls); | |
762 | bt_put(results); | |
763 | return ret; | |
764 | } | |
765 | ||
9009cc24 PP |
766 | static |
767 | int cmd_help(struct bt_config *cfg) | |
22e22462 | 768 | { |
db95fa29 | 769 | int ret = 0; |
22e22462 PP |
770 | struct bt_plugin *plugin = NULL; |
771 | size_t i; | |
772 | ||
90de159b | 773 | plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str); |
22e22462 | 774 | if (!plugin) { |
7213a328 PP |
775 | BT_LOGE("Cannot find plugin: plugin-name=\"%s\"", |
776 | cfg->cmd_data.help.cfg_component->plugin_name->str); | |
22e22462 PP |
777 | fprintf(stderr, "%s%sCannot find plugin %s%s%s\n", |
778 | bt_common_color_bold(), bt_common_color_fg_red(), | |
779 | bt_common_color_fg_blue(), | |
90de159b | 780 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
22e22462 PP |
781 | bt_common_color_reset()); |
782 | ret = -1; | |
783 | goto end; | |
784 | } | |
785 | ||
786 | print_plugin_info(plugin); | |
787 | printf(" %sComponent classes%s: %d\n", | |
788 | bt_common_color_bold(), | |
789 | bt_common_color_reset(), | |
544d0515 | 790 | (int) bt_plugin_get_component_class_count(plugin)); |
22e22462 PP |
791 | |
792 | ||
90de159b | 793 | if (cfg->cmd_data.help.cfg_component->type != |
22e22462 PP |
794 | BT_COMPONENT_CLASS_TYPE_UNKNOWN) { |
795 | struct bt_component_class *needed_comp_cls = | |
796 | find_component_class( | |
90de159b | 797 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
db0f160a | 798 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
90de159b | 799 | cfg->cmd_data.help.cfg_component->type); |
22e22462 PP |
800 | |
801 | if (!needed_comp_cls) { | |
7213a328 PP |
802 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
803 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
804 | cfg->cmd_data.help.cfg_component->plugin_name->str, | |
805 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, | |
806 | cfg->cmd_data.help.cfg_component->type); | |
22e22462 PP |
807 | fprintf(stderr, "\n%s%sCannot find component class %s", |
808 | bt_common_color_bold(), | |
809 | bt_common_color_fg_red(), | |
810 | bt_common_color_reset()); | |
811 | print_plugin_comp_cls_opt(stderr, | |
90de159b | 812 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
db0f160a | 813 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
90de159b | 814 | cfg->cmd_data.help.cfg_component->type); |
22e22462 PP |
815 | fprintf(stderr, "\n"); |
816 | ret = -1; | |
817 | goto end; | |
818 | } | |
819 | ||
820 | bt_put(needed_comp_cls); | |
821 | } | |
822 | ||
823 | for (i = 0; i < bt_plugin_get_component_class_count(plugin); i++) { | |
824 | struct bt_component_class *comp_cls = | |
9ac68eb1 | 825 | bt_plugin_get_component_class_by_index(plugin, i); |
22e22462 PP |
826 | const char *comp_class_name = |
827 | bt_component_class_get_name(comp_cls); | |
828 | const char *comp_class_description = | |
829 | bt_component_class_get_description(comp_cls); | |
830 | const char *comp_class_help = | |
831 | bt_component_class_get_help(comp_cls); | |
832 | enum bt_component_class_type type = | |
833 | bt_component_class_get_type(comp_cls); | |
834 | ||
835 | assert(comp_cls); | |
836 | ||
90de159b | 837 | if (cfg->cmd_data.help.cfg_component->type != |
22e22462 | 838 | BT_COMPONENT_CLASS_TYPE_UNKNOWN) { |
db0f160a | 839 | if (strcmp(cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
22e22462 PP |
840 | comp_class_name) != 0 && |
841 | type == | |
90de159b | 842 | cfg->cmd_data.help.cfg_component->type) { |
22e22462 PP |
843 | bt_put(comp_cls); |
844 | continue; | |
845 | } | |
846 | } | |
847 | ||
848 | printf("\n"); | |
849 | print_plugin_comp_cls_opt(stdout, | |
90de159b | 850 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
22e22462 PP |
851 | comp_class_name, |
852 | type); | |
853 | printf("\n"); | |
854 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), | |
855 | bt_common_color_reset(), | |
856 | comp_class_description ? comp_class_description : "(None)"); | |
857 | ||
858 | if (comp_class_help) { | |
859 | printf("\n%s\n", comp_class_help); | |
860 | } | |
861 | ||
862 | bt_put(comp_cls); | |
863 | } | |
864 | ||
865 | end: | |
866 | bt_put(plugin); | |
867 | return ret; | |
868 | } | |
869 | ||
9009cc24 PP |
870 | static |
871 | int cmd_list_plugins(struct bt_config *cfg) | |
290725f7 | 872 | { |
7213a328 | 873 | int ret = 0; |
290725f7 PP |
874 | int plugins_count, component_classes_count = 0, i; |
875 | ||
22e22462 | 876 | printf("From the following plugin paths:\n\n"); |
7213a328 | 877 | print_value(stdout, cfg->plugin_paths, 2); |
22e22462 | 878 | printf("\n"); |
290725f7 PP |
879 | plugins_count = loaded_plugins->len; |
880 | if (plugins_count == 0) { | |
7213a328 | 881 | printf("No plugins found.\n"); |
56a1cced JG |
882 | goto end; |
883 | } | |
884 | ||
290725f7 PP |
885 | for (i = 0; i < plugins_count; i++) { |
886 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); | |
887 | ||
888 | component_classes_count += bt_plugin_get_component_class_count(plugin); | |
889 | } | |
33bceaf8 | 890 | |
290725f7 PP |
891 | printf("Found %s%d%s component classes in %s%d%s plugins.\n", |
892 | bt_common_color_bold(), | |
893 | component_classes_count, | |
894 | bt_common_color_reset(), | |
895 | bt_common_color_bold(), | |
896 | plugins_count, | |
897 | bt_common_color_reset()); | |
898 | ||
899 | for (i = 0; i < plugins_count; i++) { | |
900 | int j; | |
901 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); | |
290725f7 PP |
902 | |
903 | component_classes_count = | |
904 | bt_plugin_get_component_class_count(plugin); | |
22e22462 PP |
905 | printf("\n"); |
906 | print_plugin_info(plugin); | |
290725f7 PP |
907 | |
908 | if (component_classes_count == 0) { | |
9009cc24 | 909 | printf(" %sComponent classes%s: (none)\n", |
290725f7 PP |
910 | bt_common_color_bold(), |
911 | bt_common_color_reset()); | |
912 | } else { | |
913 | printf(" %sComponent classes%s:\n", | |
914 | bt_common_color_bold(), | |
915 | bt_common_color_reset()); | |
916 | } | |
917 | ||
918 | for (j = 0; j < component_classes_count; j++) { | |
919 | struct bt_component_class *comp_class = | |
9ac68eb1 PP |
920 | bt_plugin_get_component_class_by_index( |
921 | plugin, j); | |
290725f7 PP |
922 | const char *comp_class_name = |
923 | bt_component_class_get_name(comp_class); | |
924 | const char *comp_class_description = | |
925 | bt_component_class_get_description(comp_class); | |
926 | enum bt_component_class_type type = | |
927 | bt_component_class_get_type(comp_class); | |
928 | ||
22e22462 PP |
929 | printf(" "); |
930 | print_plugin_comp_cls_opt(stdout, | |
931 | bt_plugin_get_name(plugin), comp_class_name, | |
932 | type); | |
290725f7 PP |
933 | |
934 | if (comp_class_description) { | |
935 | printf(": %s", comp_class_description); | |
936 | } | |
937 | ||
938 | printf("\n"); | |
939 | bt_put(comp_class); | |
940 | } | |
941 | } | |
942 | ||
943 | end: | |
944 | return ret; | |
945 | } | |
946 | ||
9009cc24 PP |
947 | static |
948 | int cmd_print_lttng_live_sessions(struct bt_config *cfg) | |
db0f160a | 949 | { |
96e8c7e1 MD |
950 | int ret = 0; |
951 | struct bt_component_class *comp_cls = NULL; | |
952 | struct bt_value *results = NULL; | |
953 | struct bt_value *params = NULL; | |
954 | struct bt_value *map = NULL; | |
955 | struct bt_value *v = NULL; | |
956 | static const char * const plugin_name = "ctf"; | |
957 | static const char * const comp_cls_name = "lttng-live"; | |
958 | static const enum bt_component_class_type comp_cls_type = | |
959 | BT_COMPONENT_CLASS_TYPE_SOURCE; | |
960 | int64_t array_size, i; | |
961 | ||
962 | assert(cfg->cmd_data.print_lttng_live_sessions.url); | |
963 | comp_cls = find_component_class(plugin_name, comp_cls_name, | |
964 | comp_cls_type); | |
965 | if (!comp_cls) { | |
966 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " | |
967 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
968 | plugin_name, comp_cls_name, | |
969 | BT_COMPONENT_CLASS_TYPE_SOURCE); | |
970 | fprintf(stderr, "%s%sCannot find component class %s", | |
971 | bt_common_color_bold(), | |
972 | bt_common_color_fg_red(), | |
973 | bt_common_color_reset()); | |
974 | print_plugin_comp_cls_opt(stderr, plugin_name, | |
975 | comp_cls_name, comp_cls_type); | |
976 | fprintf(stderr, "\n"); | |
977 | goto error; | |
978 | } | |
979 | ||
980 | params = bt_value_map_create(); | |
981 | if (!params) { | |
982 | goto error; | |
983 | } | |
984 | ||
985 | ret = bt_value_map_insert_string(params, "url", | |
986 | cfg->cmd_data.print_lttng_live_sessions.url->str); | |
987 | if (ret) { | |
988 | goto error; | |
989 | } | |
990 | ||
991 | results = bt_component_class_query(comp_cls, "sessions", | |
992 | params); | |
993 | if (!results) { | |
994 | BT_LOGE_STR("Failed to query for sessions."); | |
995 | fprintf(stderr, "%s%sFailed to request sessions%s\n", | |
996 | bt_common_color_bold(), | |
997 | bt_common_color_fg_red(), | |
998 | bt_common_color_reset()); | |
999 | goto error; | |
1000 | } | |
1001 | ||
1002 | if (!bt_value_is_array(results)) { | |
1003 | BT_LOGE_STR("Expecting an array for sessions query."); | |
1004 | fprintf(stderr, "%s%sUnexpected type returned by session query%s\n", | |
1005 | bt_common_color_bold(), | |
1006 | bt_common_color_fg_red(), | |
1007 | bt_common_color_reset()); | |
1008 | goto error; | |
1009 | } | |
1010 | ||
1011 | array_size = bt_value_array_size(results); | |
1012 | for (i = 0; i < array_size; i++) { | |
1013 | const char *url_text; | |
1014 | int64_t timer_us, streams, clients; | |
1015 | ||
1016 | map = bt_value_array_get(results, i); | |
1017 | if (!map) { | |
1018 | BT_LOGE_STR("Unexpected empty array entry."); | |
1019 | goto error; | |
1020 | } | |
1021 | if (!bt_value_is_map(map)) { | |
1022 | BT_LOGE_STR("Unexpected entry type."); | |
1023 | goto error; | |
1024 | } | |
1025 | ||
1026 | v = bt_value_map_get(map, "url"); | |
1027 | if (!v) { | |
1028 | BT_LOGE_STR("Unexpected empty array \"url\" entry."); | |
1029 | goto error; | |
1030 | } | |
1031 | ret = bt_value_string_get(v, &url_text); | |
1032 | assert(ret == 0); | |
1033 | printf("%s", url_text); | |
1034 | BT_PUT(v); | |
1035 | ||
1036 | v = bt_value_map_get(map, "timer-us"); | |
1037 | if (!v) { | |
1038 | BT_LOGE_STR("Unexpected empty array \"timer-us\" entry."); | |
1039 | goto error; | |
1040 | } | |
1041 | ret = bt_value_integer_get(v, &timer_us); | |
1042 | assert(ret == 0); | |
1043 | printf(" (timer = %" PRIu64 ", ", timer_us); | |
1044 | BT_PUT(v); | |
1045 | ||
1046 | v = bt_value_map_get(map, "stream-count"); | |
1047 | if (!v) { | |
1048 | BT_LOGE_STR("Unexpected empty array \"stream-count\" entry."); | |
1049 | goto error; | |
1050 | } | |
1051 | ret = bt_value_integer_get(v, &streams); | |
1052 | assert(ret == 0); | |
1053 | printf("%" PRIu64 " stream(s), ", streams); | |
1054 | BT_PUT(v); | |
1055 | ||
1056 | v = bt_value_map_get(map, "client-count"); | |
1057 | if (!v) { | |
1058 | BT_LOGE_STR("Unexpected empty array \"client-count\" entry."); | |
1059 | goto error; | |
1060 | } | |
1061 | ret = bt_value_integer_get(v, &clients); | |
1062 | assert(ret == 0); | |
1063 | printf("%" PRIu64 " client(s) connected)\n", clients); | |
1064 | BT_PUT(v); | |
1065 | ||
1066 | BT_PUT(map); | |
1067 | } | |
1068 | end: | |
1069 | bt_put(v); | |
1070 | bt_put(map); | |
1071 | bt_put(results); | |
1072 | bt_put(params); | |
1073 | bt_put(comp_cls); | |
1074 | return 0; | |
1075 | ||
1076 | error: | |
1077 | ret = -1; | |
1078 | goto end; | |
db0f160a PP |
1079 | } |
1080 | ||
9009cc24 PP |
1081 | static |
1082 | int cmd_print_ctf_metadata(struct bt_config *cfg) | |
05a67631 PP |
1083 | { |
1084 | int ret = 0; | |
1085 | struct bt_component_class *comp_cls = NULL; | |
05a67631 | 1086 | struct bt_value *results = NULL; |
05a67631 PP |
1087 | struct bt_value *params = NULL; |
1088 | struct bt_value *metadata_text_value = NULL; | |
1089 | const char *metadata_text = NULL; | |
db0f160a PP |
1090 | static const char * const plugin_name = "ctf"; |
1091 | static const char * const comp_cls_name = "fs"; | |
1092 | static const enum bt_component_class_type comp_cls_type = | |
1093 | BT_COMPONENT_CLASS_TYPE_SOURCE; | |
1094 | ||
1095 | assert(cfg->cmd_data.print_ctf_metadata.path); | |
1096 | comp_cls = find_component_class(plugin_name, comp_cls_name, | |
1097 | comp_cls_type); | |
05a67631 | 1098 | if (!comp_cls) { |
7213a328 PP |
1099 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
1100 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
1101 | plugin_name, comp_cls_name, | |
1102 | BT_COMPONENT_CLASS_TYPE_SOURCE); | |
05a67631 PP |
1103 | fprintf(stderr, "%s%sCannot find component class %s", |
1104 | bt_common_color_bold(), | |
1105 | bt_common_color_fg_red(), | |
1106 | bt_common_color_reset()); | |
db0f160a PP |
1107 | print_plugin_comp_cls_opt(stderr, plugin_name, |
1108 | comp_cls_name, comp_cls_type); | |
05a67631 PP |
1109 | fprintf(stderr, "\n"); |
1110 | ret = -1; | |
1111 | goto end; | |
1112 | } | |
1113 | ||
05a67631 PP |
1114 | params = bt_value_map_create(); |
1115 | if (!params) { | |
1116 | ret = -1; | |
1117 | goto end; | |
1118 | } | |
1119 | ||
db0f160a PP |
1120 | ret = bt_value_map_insert_string(params, "path", |
1121 | cfg->cmd_data.print_ctf_metadata.path->str); | |
05a67631 PP |
1122 | if (ret) { |
1123 | ret = -1; | |
1124 | goto end; | |
1125 | } | |
1126 | ||
a67681c1 | 1127 | results = bt_component_class_query(comp_cls, "metadata-info", |
05a67631 PP |
1128 | params); |
1129 | if (!results) { | |
1130 | ret = -1; | |
7213a328 | 1131 | BT_LOGE_STR("Failed to query for metadata info."); |
a67681c1 | 1132 | fprintf(stderr, "%s%sFailed to request metadata info%s\n", |
05a67631 PP |
1133 | bt_common_color_bold(), |
1134 | bt_common_color_fg_red(), | |
1135 | bt_common_color_reset()); | |
1136 | goto end; | |
1137 | } | |
1138 | ||
1139 | metadata_text_value = bt_value_map_get(results, "text"); | |
1140 | if (!metadata_text_value) { | |
7213a328 | 1141 | BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object."); |
05a67631 PP |
1142 | ret = -1; |
1143 | goto end; | |
1144 | } | |
1145 | ||
1146 | ret = bt_value_string_get(metadata_text_value, &metadata_text); | |
1147 | assert(ret == 0); | |
1148 | printf("%s\n", metadata_text); | |
1149 | ||
1150 | end: | |
1151 | bt_put(results); | |
05a67631 PP |
1152 | bt_put(params); |
1153 | bt_put(metadata_text_value); | |
1154 | bt_put(comp_cls); | |
05a67631 PP |
1155 | return 0; |
1156 | } | |
1157 | ||
9009cc24 PP |
1158 | struct cmd_run_ctx { |
1159 | /* Owned by this */ | |
1160 | GHashTable *components; | |
1161 | ||
1162 | /* Owned by this */ | |
1163 | struct bt_graph *graph; | |
1164 | ||
1165 | /* Weak */ | |
1166 | struct bt_config *cfg; | |
1167 | ||
1168 | bool connect_ports; | |
1169 | }; | |
1170 | ||
1171 | static | |
1172 | int cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1173 | struct cmd_run_ctx *ctx, struct bt_component *upstream_comp, | |
1174 | struct bt_port *upstream_port, | |
1175 | struct bt_config_connection *cfg_conn) | |
290725f7 PP |
1176 | { |
1177 | int ret = 0; | |
9009cc24 PP |
1178 | GQuark downstreamp_comp_name_quark; |
1179 | struct bt_component *downstream_comp; | |
1180 | int64_t downstream_port_count; | |
1181 | uint64_t i; | |
1182 | int64_t (*port_count_fn)(struct bt_component *); | |
1183 | struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t); | |
1184 | void *conn = NULL; | |
1185 | ||
7213a328 PP |
1186 | BT_LOGI("Connecting upstream port to the next available downstream port: " |
1187 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1188 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1189 | upstream_port, bt_port_get_name(upstream_port), | |
1190 | cfg_conn->downstream_comp_name->str, | |
1191 | cfg_conn->arg->str); | |
9009cc24 PP |
1192 | downstreamp_comp_name_quark = g_quark_from_string( |
1193 | cfg_conn->downstream_comp_name->str); | |
1194 | assert(downstreamp_comp_name_quark > 0); | |
1195 | downstream_comp = g_hash_table_lookup(ctx->components, | |
1196 | (gpointer) (long) downstreamp_comp_name_quark); | |
1197 | if (!downstream_comp) { | |
7213a328 PP |
1198 | BT_LOGE("Cannot find downstream component: comp-name=\"%s\", " |
1199 | "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str, | |
1200 | cfg_conn->arg->str); | |
9009cc24 PP |
1201 | fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n", |
1202 | cfg_conn->arg->str); | |
1203 | goto error; | |
1204 | } | |
1205 | ||
1206 | if (bt_component_is_filter(downstream_comp)) { | |
1207 | port_count_fn = bt_component_filter_get_input_port_count; | |
1208 | port_by_index_fn = bt_component_filter_get_input_port_by_index; | |
1209 | } else if (bt_component_is_sink(downstream_comp)) { | |
1210 | port_count_fn = bt_component_sink_get_input_port_count; | |
1211 | port_by_index_fn = bt_component_sink_get_input_port_by_index; | |
1212 | } else { | |
1213 | /* | |
1214 | * Should never happen because the connections are | |
1215 | * validated before we get here. | |
1216 | */ | |
7213a328 PP |
1217 | BT_LOGF("Invalid connection: downstream component is a source: " |
1218 | "conn-arg=\"%s\"", cfg_conn->arg->str); | |
0fbb9a9f | 1219 | abort(); |
9009cc24 | 1220 | } |
290725f7 | 1221 | |
9009cc24 PP |
1222 | downstream_port_count = port_count_fn(downstream_comp); |
1223 | assert(downstream_port_count >= 0); | |
1224 | ||
1225 | for (i = 0; i < downstream_port_count; i++) { | |
1226 | struct bt_port *downstream_port = | |
1227 | port_by_index_fn(downstream_comp, i); | |
1228 | const char *downstream_port_name; | |
1229 | ||
1230 | assert(downstream_port); | |
1231 | ||
1232 | /* Skip port if it's already connected */ | |
1233 | if (bt_port_is_connected(downstream_port)) { | |
1234 | bt_put(downstream_port); | |
7213a328 PP |
1235 | BT_LOGD("Skipping downstream port: already connected: " |
1236 | "port-addr=%p, port-name=\"%s\"", | |
1237 | downstream_port, | |
1238 | bt_port_get_name(downstream_port)); | |
9009cc24 PP |
1239 | continue; |
1240 | } | |
1241 | ||
1242 | downstream_port_name = bt_port_get_name(downstream_port); | |
1243 | assert(downstream_port_name); | |
1244 | ||
1245 | if (bt_common_star_glob_match( | |
1246 | cfg_conn->downstream_port_glob->str, -1ULL, | |
1247 | downstream_port_name, -1ULL)) { | |
1248 | /* We have a winner! */ | |
1249 | conn = bt_graph_connect_ports(ctx->graph, | |
1250 | upstream_port, downstream_port); | |
1251 | bt_put(downstream_port); | |
1252 | if (!conn) { | |
7213a328 PP |
1253 | BT_LOGE("Cannot create connection: graph refuses to connect ports: " |
1254 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
1255 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1256 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1257 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1258 | "conn-arg=\"%s\"", | |
1259 | upstream_comp, bt_component_get_name(upstream_comp), | |
1260 | upstream_port, bt_port_get_name(upstream_port), | |
1261 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1262 | downstream_port, downstream_port_name, | |
1263 | cfg_conn->arg->str); | |
9009cc24 PP |
1264 | fprintf(stderr, |
1265 | "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n", | |
1266 | bt_port_get_name(upstream_port), | |
1267 | downstream_port_name, | |
1268 | cfg_conn->arg->str); | |
1269 | goto error; | |
1270 | } | |
1271 | ||
7213a328 PP |
1272 | BT_LOGI("Connected component ports: " |
1273 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
1274 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1275 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1276 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1277 | "conn-arg=\"%s\"", | |
1278 | upstream_comp, bt_component_get_name(upstream_comp), | |
1279 | upstream_port, bt_port_get_name(upstream_port), | |
1280 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1281 | downstream_port, downstream_port_name, | |
1282 | cfg_conn->arg->str); | |
1283 | ||
9009cc24 PP |
1284 | goto end; |
1285 | } | |
1286 | ||
1287 | bt_put(downstream_port); | |
1288 | } | |
1289 | ||
1290 | if (!conn) { | |
7213a328 PP |
1291 | BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: " |
1292 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1293 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1294 | upstream_port, bt_port_get_name(upstream_port), | |
1295 | cfg_conn->downstream_comp_name->str, | |
1296 | cfg_conn->arg->str); | |
9009cc24 PP |
1297 | fprintf(stderr, |
1298 | "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n", | |
1299 | bt_port_get_name(upstream_port), cfg_conn->arg->str); | |
1300 | goto error; | |
05a67631 PP |
1301 | } |
1302 | ||
9009cc24 PP |
1303 | goto end; |
1304 | ||
1305 | error: | |
1306 | ret = -1; | |
1307 | ||
1308 | end: | |
1309 | bt_put(conn); | |
1310 | return ret; | |
1311 | } | |
1312 | ||
1313 | static | |
1314 | int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx, | |
1315 | struct bt_port *upstream_port) | |
1316 | { | |
1317 | int ret = 0; | |
1318 | const char *upstream_port_name; | |
1319 | const char *upstream_comp_name; | |
1320 | struct bt_component *upstream_comp = NULL; | |
1321 | size_t i; | |
1322 | ||
1323 | assert(ctx); | |
1324 | assert(upstream_port); | |
1325 | upstream_port_name = bt_port_get_name(upstream_port); | |
1326 | assert(upstream_port_name); | |
1327 | upstream_comp = bt_port_get_component(upstream_port); | |
1328 | if (!upstream_comp) { | |
7213a328 PP |
1329 | BT_LOGW("Upstream port to connect is not part of a component: " |
1330 | "port-addr=%p, port-name=\"%s\"", | |
1331 | upstream_port, upstream_port_name); | |
98ecef32 MD |
1332 | ret = -1; |
1333 | goto end; | |
33bceaf8 JG |
1334 | } |
1335 | ||
9009cc24 PP |
1336 | upstream_comp_name = bt_component_get_name(upstream_comp); |
1337 | assert(upstream_comp_name); | |
7213a328 PP |
1338 | BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", " |
1339 | "port-addr=%p, port-name=\"%s\"", | |
1340 | upstream_comp, upstream_comp_name, | |
1341 | upstream_port, upstream_port_name); | |
9009cc24 PP |
1342 | |
1343 | for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) { | |
1344 | struct bt_config_connection *cfg_conn = | |
1345 | g_ptr_array_index( | |
1346 | ctx->cfg->cmd_data.run.connections, i); | |
1347 | ||
1348 | if (strcmp(cfg_conn->upstream_comp_name->str, | |
1349 | upstream_comp_name) == 0) { | |
1350 | if (bt_common_star_glob_match( | |
1351 | cfg_conn->upstream_port_glob->str, | |
1352 | -1ULL, upstream_port_name, -1ULL)) { | |
1353 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1354 | ctx, upstream_comp, upstream_port, | |
1355 | cfg_conn); | |
1356 | if (ret) { | |
7213a328 PP |
1357 | BT_LOGE("Cannot connect upstream port: " |
1358 | "port-addr=%p, port-name=\"%s\"", | |
1359 | upstream_port, | |
1360 | upstream_port_name); | |
9009cc24 PP |
1361 | fprintf(stderr, |
1362 | "Cannot connect port `%s` of component `%s` to a downstream port: %s\n", | |
1363 | upstream_port_name, | |
1364 | upstream_comp_name, | |
1365 | cfg_conn->arg->str); | |
1366 | goto error; | |
1367 | } | |
1368 | ||
1369 | goto end; | |
1370 | } | |
1371 | } | |
1372 | } | |
1373 | ||
7213a328 PP |
1374 | BT_LOGE("Cannot connect upstream port: port does not match any connection argument: " |
1375 | "port-addr=%p, port-name=\"%s\"", upstream_port, | |
1376 | upstream_port_name); | |
9009cc24 PP |
1377 | fprintf(stderr, |
1378 | "Cannot create connection: upstream port `%s` does not match any connection\n", | |
7213a328 | 1379 | upstream_port_name); |
9009cc24 PP |
1380 | |
1381 | error: | |
1382 | ret = -1; | |
1383 | ||
1384 | end: | |
1385 | bt_put(upstream_comp); | |
1386 | return ret; | |
1387 | } | |
1388 | ||
1389 | static | |
1390 | void graph_port_added_listener(struct bt_port *port, void *data) | |
1391 | { | |
1392 | struct bt_component *comp = NULL; | |
1393 | struct cmd_run_ctx *ctx = data; | |
1394 | ||
e12720c0 PP |
1395 | comp = bt_port_get_component(port); |
1396 | BT_LOGI("Port added to a graph's component: comp-addr=%p, " | |
1397 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
1398 | comp, comp ? bt_component_get_name(comp) : "", | |
7213a328 | 1399 | port, bt_port_get_name(port)); |
e12720c0 PP |
1400 | if (!comp) { |
1401 | BT_LOGW_STR("Port has no component."); | |
56a1cced JG |
1402 | goto end; |
1403 | } | |
7c7c0433 | 1404 | |
e12720c0 PP |
1405 | if (bt_port_is_connected(port)) { |
1406 | BT_LOGW_STR("Port is already connected."); | |
7c7c0433 JG |
1407 | goto end; |
1408 | } | |
1409 | ||
9009cc24 | 1410 | if (!bt_port_is_output(port)) { |
7213a328 | 1411 | BT_LOGI_STR("Skipping input port."); |
61ddbc8a JG |
1412 | goto end; |
1413 | } | |
1414 | ||
9009cc24 | 1415 | if (cmd_run_ctx_connect_upstream_port(ctx, port)) { |
7213a328 | 1416 | BT_LOGF_STR("Cannot connect upstream port."); |
9009cc24 PP |
1417 | fprintf(stderr, "Added port could not be connected: aborting\n"); |
1418 | abort(); | |
1419 | } | |
1420 | ||
1421 | end: | |
1422 | bt_put(comp); | |
1423 | return; | |
1424 | } | |
1425 | ||
1426 | static | |
1427 | void graph_port_removed_listener(struct bt_component *component, | |
1428 | struct bt_port *port, void *data) | |
1429 | { | |
7213a328 PP |
1430 | BT_LOGI("Port removed from a graph's component: comp-addr=%p, " |
1431 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
1432 | component, bt_component_get_name(component), | |
1433 | port, bt_port_get_name(port)); | |
9009cc24 PP |
1434 | } |
1435 | ||
1436 | static | |
1437 | void graph_ports_connected_listener(struct bt_port *upstream_port, | |
1438 | struct bt_port *downstream_port, void *data) | |
1439 | { | |
e12720c0 PP |
1440 | struct bt_component *upstream_comp = bt_port_get_component(upstream_port); |
1441 | struct bt_component *downstream_comp = bt_port_get_component(downstream_port); | |
1442 | ||
1443 | assert(upstream_comp); | |
1444 | assert(downstream_comp); | |
7213a328 | 1445 | BT_LOGI("Graph's component ports connected: " |
e12720c0 | 1446 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
7213a328 | 1447 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
e12720c0 | 1448 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
7213a328 | 1449 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
e12720c0 | 1450 | upstream_comp, bt_component_get_name(upstream_comp), |
7213a328 | 1451 | upstream_port, bt_port_get_name(upstream_port), |
e12720c0 | 1452 | downstream_comp, bt_component_get_name(downstream_comp), |
7213a328 | 1453 | downstream_port, bt_port_get_name(downstream_port)); |
e12720c0 PP |
1454 | bt_put(upstream_comp); |
1455 | bt_put(downstream_comp); | |
9009cc24 PP |
1456 | } |
1457 | ||
1458 | static | |
1459 | void graph_ports_disconnected_listener( | |
1460 | struct bt_component *upstream_component, | |
1461 | struct bt_component *downstream_component, | |
1462 | struct bt_port *upstream_port, struct bt_port *downstream_port, | |
1463 | void *data) | |
1464 | { | |
7213a328 PP |
1465 | BT_LOGI("Graph's component ports disconnected: " |
1466 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1467 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
1468 | upstream_port, bt_port_get_name(upstream_port), | |
1469 | downstream_port, bt_port_get_name(downstream_port)); | |
9009cc24 PP |
1470 | } |
1471 | ||
1472 | static | |
1473 | void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx) | |
1474 | { | |
1475 | if (!ctx) { | |
1476 | return; | |
1477 | } | |
1478 | ||
1479 | if (ctx->components) { | |
1480 | g_hash_table_destroy(ctx->components); | |
1481 | ctx->components = NULL; | |
1482 | } | |
1483 | ||
1484 | BT_PUT(ctx->graph); | |
5401f780 | 1485 | the_graph = NULL; |
9009cc24 PP |
1486 | ctx->cfg = NULL; |
1487 | } | |
1488 | ||
1489 | static | |
1490 | int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg) | |
1491 | { | |
1492 | int ret = 0; | |
1493 | ||
1494 | ctx->cfg = cfg; | |
1495 | ctx->connect_ports = false; | |
1496 | ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
1497 | NULL, bt_put); | |
1498 | if (!ctx->components) { | |
1499 | goto error; | |
1500 | } | |
1501 | ||
1502 | ctx->graph = bt_graph_create(); | |
1503 | if (!ctx->graph) { | |
1504 | goto error; | |
1505 | } | |
1506 | ||
5401f780 | 1507 | the_graph = ctx->graph; |
9009cc24 PP |
1508 | ret = bt_graph_add_port_added_listener(ctx->graph, |
1509 | graph_port_added_listener, ctx); | |
0d107cdd PP |
1510 | if (ret < 0) { |
1511 | BT_LOGE_STR("Cannot add \"port added\" listener to graph."); | |
9009cc24 PP |
1512 | goto error; |
1513 | } | |
1514 | ||
1515 | ret = bt_graph_add_port_removed_listener(ctx->graph, | |
1516 | graph_port_removed_listener, ctx); | |
0d107cdd PP |
1517 | if (ret < 0) { |
1518 | BT_LOGE_STR("Cannot add \"port removed\" listener to graph."); | |
9009cc24 PP |
1519 | goto error; |
1520 | } | |
1521 | ||
1522 | ret = bt_graph_add_ports_connected_listener(ctx->graph, | |
1523 | graph_ports_connected_listener, ctx); | |
0d107cdd PP |
1524 | if (ret < 0) { |
1525 | BT_LOGE_STR("Cannot add \"ports connected\" listener to graph."); | |
9009cc24 PP |
1526 | goto error; |
1527 | } | |
1528 | ||
1529 | ret = bt_graph_add_ports_disconnected_listener(ctx->graph, | |
1530 | graph_ports_disconnected_listener, ctx); | |
0d107cdd PP |
1531 | if (ret < 0) { |
1532 | BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph."); | |
9009cc24 PP |
1533 | goto error; |
1534 | } | |
1535 | ||
1536 | goto end; | |
1537 | ||
1538 | error: | |
1539 | cmd_run_ctx_destroy(ctx); | |
1540 | ret = -1; | |
1541 | ||
1542 | end: | |
1543 | return ret; | |
1544 | } | |
1545 | ||
1546 | static | |
1547 | int cmd_run_ctx_create_components_from_config_components( | |
1548 | struct cmd_run_ctx *ctx, GPtrArray *cfg_components) | |
1549 | { | |
1550 | size_t i; | |
1551 | struct bt_component_class *comp_cls = NULL; | |
1552 | struct bt_component *comp = NULL; | |
1553 | int ret = 0; | |
1554 | ||
1555 | for (i = 0; i < cfg_components->len; i++) { | |
1556 | struct bt_config_component *cfg_comp = | |
1557 | g_ptr_array_index(cfg_components, i); | |
1558 | GQuark quark; | |
1559 | ||
1560 | comp_cls = find_component_class(cfg_comp->plugin_name->str, | |
1561 | cfg_comp->comp_cls_name->str, cfg_comp->type); | |
1562 | if (!comp_cls) { | |
7213a328 PP |
1563 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
1564 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
1565 | cfg_comp->plugin_name->str, | |
1566 | cfg_comp->comp_cls_name->str, | |
1567 | cfg_comp->type); | |
9009cc24 PP |
1568 | fprintf(stderr, "%s%sCannot find component class %s", |
1569 | bt_common_color_bold(), | |
1570 | bt_common_color_fg_red(), | |
1571 | bt_common_color_reset()); | |
1572 | print_plugin_comp_cls_opt(stderr, | |
1573 | cfg_comp->plugin_name->str, | |
1574 | cfg_comp->comp_cls_name->str, | |
1575 | cfg_comp->type); | |
1576 | fprintf(stderr, "\n"); | |
1577 | goto error; | |
1578 | } | |
1579 | ||
1580 | comp = bt_component_create(comp_cls, | |
1581 | cfg_comp->instance_name->str, cfg_comp->params); | |
1582 | if (!comp) { | |
7213a328 | 1583 | BT_LOGE("Cannot create component: plugin-name=\"%s\", " |
32e87ceb | 1584 | "comp-cls-name=\"%s\", comp-cls-type=%d, " |
7213a328 PP |
1585 | "comp-name=\"%s\"", |
1586 | cfg_comp->plugin_name->str, | |
1587 | cfg_comp->comp_cls_name->str, | |
1588 | cfg_comp->type, cfg_comp->instance_name->str); | |
9009cc24 PP |
1589 | fprintf(stderr, "%s%sCannot create component `%s`%s\n", |
1590 | bt_common_color_bold(), | |
1591 | bt_common_color_fg_red(), | |
1592 | cfg_comp->instance_name->str, | |
1593 | bt_common_color_reset()); | |
1594 | goto error; | |
1595 | } | |
1596 | ||
7213a328 PP |
1597 | BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"", |
1598 | comp, cfg_comp->instance_name->str); | |
9009cc24 PP |
1599 | quark = g_quark_from_string(cfg_comp->instance_name->str); |
1600 | assert(quark > 0); | |
1601 | g_hash_table_insert(ctx->components, | |
1602 | (gpointer) (long) quark, comp); | |
1603 | comp = NULL; | |
1604 | BT_PUT(comp_cls); | |
1605 | } | |
1606 | ||
1607 | goto end; | |
1608 | ||
1609 | error: | |
1610 | ret = -1; | |
1611 | ||
1612 | end: | |
1613 | bt_put(comp); | |
1614 | bt_put(comp_cls); | |
1615 | return ret; | |
1616 | } | |
56a1cced | 1617 | |
9009cc24 PP |
1618 | static |
1619 | int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx) | |
1620 | { | |
1621 | int ret = 0; | |
1622 | ||
1623 | /* | |
1624 | * Make sure that, during this phase, our graph's "port added" | |
1625 | * listener does not connect ports while we are creating the | |
1626 | * components because we have a special, initial phase for | |
1627 | * this. | |
1628 | */ | |
1629 | ctx->connect_ports = false; | |
1630 | ||
1631 | ret = cmd_run_ctx_create_components_from_config_components( | |
1632 | ctx, ctx->cfg->cmd_data.run.sources); | |
1633 | if (ret) { | |
7c7c0433 | 1634 | ret = -1; |
2e339de1 JG |
1635 | goto end; |
1636 | } | |
1637 | ||
9009cc24 PP |
1638 | ret = cmd_run_ctx_create_components_from_config_components( |
1639 | ctx, ctx->cfg->cmd_data.run.filters); | |
6c2f3ee5 | 1640 | if (ret) { |
290725f7 | 1641 | ret = -1; |
fec2a9f2 JG |
1642 | goto end; |
1643 | } | |
78586d8a | 1644 | |
9009cc24 PP |
1645 | ret = cmd_run_ctx_create_components_from_config_components( |
1646 | ctx, ctx->cfg->cmd_data.run.sinks); | |
1647 | if (ret) { | |
1648 | ret = -1; | |
1649 | goto end; | |
1650 | } | |
1651 | ||
1652 | end: | |
1653 | return ret; | |
1654 | } | |
1655 | ||
1656 | static | |
1657 | int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx, | |
1658 | struct bt_component *comp, | |
1659 | int64_t (*port_count_fn)(struct bt_component *), | |
1660 | struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t)) | |
1661 | { | |
1662 | int ret = 0; | |
1663 | int64_t count; | |
1664 | uint64_t i; | |
1665 | ||
1666 | count = port_count_fn(comp); | |
1667 | assert(count >= 0); | |
1668 | ||
1669 | for (i = 0; i < count; i++) { | |
1670 | struct bt_port *upstream_port = port_by_index_fn(comp, i); | |
1671 | ||
1672 | assert(upstream_port); | |
1673 | ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port); | |
1674 | bt_put(upstream_port); | |
1675 | if (ret) { | |
1676 | goto end; | |
1677 | } | |
1678 | } | |
1679 | ||
1680 | end: | |
1681 | return ret; | |
1682 | } | |
1683 | ||
1684 | static | |
1685 | int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx) | |
1686 | { | |
1687 | int ret = 0; | |
1688 | GHashTableIter iter; | |
1689 | gpointer g_name_quark, g_comp; | |
1690 | ||
1691 | ctx->connect_ports = true; | |
1692 | g_hash_table_iter_init(&iter, ctx->components); | |
1693 | ||
1694 | while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) { | |
1695 | if (bt_component_is_source(g_comp)) { | |
1696 | ret = cmd_run_ctx_connect_comp_ports(ctx, | |
1697 | g_comp, bt_component_source_get_output_port_count, | |
1698 | bt_component_source_get_output_port_by_index); | |
1699 | } else if (bt_component_is_filter(g_comp)) { | |
1700 | ret = cmd_run_ctx_connect_comp_ports(ctx, | |
1701 | g_comp, bt_component_filter_get_output_port_count, | |
1702 | bt_component_filter_get_output_port_by_index); | |
1703 | } | |
1704 | ||
1705 | if (ret) { | |
1706 | goto end; | |
1707 | } | |
1708 | } | |
1709 | ||
1710 | end: | |
1711 | return ret; | |
1712 | } | |
1713 | ||
fd948396 PP |
1714 | static inline |
1715 | const char *bt_graph_status_str(enum bt_graph_status status) | |
1716 | { | |
1717 | switch (status) { | |
1718 | case BT_GRAPH_STATUS_CANCELED: | |
1719 | return "BT_GRAPH_STATUS_CANCELED"; | |
1720 | case BT_GRAPH_STATUS_AGAIN: | |
1721 | return "BT_GRAPH_STATUS_AGAIN"; | |
1722 | case BT_GRAPH_STATUS_END: | |
1723 | return "BT_GRAPH_STATUS_END"; | |
1724 | case BT_GRAPH_STATUS_OK: | |
1725 | return "BT_GRAPH_STATUS_OK"; | |
1726 | case BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH: | |
1727 | return "BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH"; | |
1728 | case BT_GRAPH_STATUS_INVALID: | |
1729 | return "BT_GRAPH_STATUS_INVALID"; | |
1730 | case BT_GRAPH_STATUS_NO_SINK: | |
1731 | return "BT_GRAPH_STATUS_NO_SINK"; | |
1732 | case BT_GRAPH_STATUS_ERROR: | |
1733 | return "BT_GRAPH_STATUS_ERROR"; | |
1734 | default: | |
1735 | return "(unknown)"; | |
1736 | } | |
1737 | } | |
1738 | ||
9009cc24 PP |
1739 | static |
1740 | int cmd_run(struct bt_config *cfg) | |
1741 | { | |
1742 | int ret = 0; | |
1743 | struct cmd_run_ctx ctx = { 0 }; | |
1744 | ||
9009cc24 PP |
1745 | /* Initialize the command's context and the graph object */ |
1746 | if (cmd_run_ctx_init(&ctx, cfg)) { | |
7213a328 | 1747 | BT_LOGE_STR("Cannot initialize the command's context."); |
9009cc24 PP |
1748 | fprintf(stderr, "Cannot initialize the command's context\n"); |
1749 | goto error; | |
1750 | } | |
1751 | ||
cc308374 PP |
1752 | if (canceled) { |
1753 | BT_LOGI_STR("Canceled by user before creating components."); | |
1754 | goto error; | |
1755 | } | |
1756 | ||
1757 | BT_LOGI_STR("Creating components."); | |
1758 | ||
9009cc24 PP |
1759 | /* Create the requested component instances */ |
1760 | if (cmd_run_ctx_create_components(&ctx)) { | |
7213a328 | 1761 | BT_LOGE_STR("Cannot create components."); |
9009cc24 PP |
1762 | fprintf(stderr, "Cannot create components\n"); |
1763 | goto error; | |
1764 | } | |
1765 | ||
cc308374 PP |
1766 | if (canceled) { |
1767 | BT_LOGI_STR("Canceled by user before connecting components."); | |
1768 | goto error; | |
1769 | } | |
1770 | ||
1771 | BT_LOGI_STR("Connecting components."); | |
1772 | ||
9009cc24 PP |
1773 | /* Connect the initially visible component ports */ |
1774 | if (cmd_run_ctx_connect_ports(&ctx)) { | |
7213a328 | 1775 | BT_LOGE_STR("Cannot connect initial component ports."); |
9009cc24 PP |
1776 | fprintf(stderr, "Cannot connect initial component ports\n"); |
1777 | goto error; | |
1778 | } | |
1779 | ||
5401f780 | 1780 | if (canceled) { |
cc308374 PP |
1781 | BT_LOGI_STR("Canceled by user before running the graph."); |
1782 | goto error; | |
5401f780 PP |
1783 | } |
1784 | ||
7213a328 PP |
1785 | BT_LOGI_STR("Running the graph."); |
1786 | ||
9009cc24 | 1787 | /* Run the graph */ |
fec2a9f2 | 1788 | while (true) { |
9009cc24 | 1789 | enum bt_graph_status graph_status = bt_graph_run(ctx.graph); |
61ddbc8a | 1790 | |
5669a3e7 PP |
1791 | /* |
1792 | * Reset console in case something messed with console | |
1793 | * codes during the graph's execution. | |
1794 | */ | |
1795 | printf("%s", bt_common_color_reset()); | |
1796 | fflush(stdout); | |
1797 | fprintf(stderr, "%s", bt_common_color_reset()); | |
fd948396 PP |
1798 | BT_LOGV("bt_graph_run() returned: status=%s", |
1799 | bt_graph_status_str(graph_status)); | |
1800 | ||
61ddbc8a | 1801 | switch (graph_status) { |
9009cc24 PP |
1802 | case BT_GRAPH_STATUS_OK: |
1803 | break; | |
5401f780 | 1804 | case BT_GRAPH_STATUS_CANCELED: |
fd948396 | 1805 | BT_LOGI_STR("Graph was canceled by user."); |
5401f780 | 1806 | goto error; |
61ddbc8a | 1807 | case BT_GRAPH_STATUS_AGAIN: |
5401f780 | 1808 | if (bt_graph_is_canceled(ctx.graph)) { |
fd948396 | 1809 | BT_LOGI_STR("Graph was canceled by user."); |
5401f780 PP |
1810 | goto error; |
1811 | } | |
1812 | ||
9009cc24 | 1813 | if (cfg->cmd_data.run.retry_duration_us > 0) { |
7213a328 PP |
1814 | BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: " |
1815 | "time-us=%" PRIu64, | |
1816 | cfg->cmd_data.run.retry_duration_us); | |
1817 | ||
9009cc24 | 1818 | if (usleep(cfg->cmd_data.run.retry_duration_us)) { |
cfa4637b PP |
1819 | if (bt_graph_is_canceled(ctx.graph)) { |
1820 | BT_LOGI_STR("Graph was canceled by user."); | |
1821 | goto error; | |
1822 | } | |
9009cc24 PP |
1823 | } |
1824 | } | |
78586d8a | 1825 | break; |
fec2a9f2 JG |
1826 | case BT_COMPONENT_STATUS_END: |
1827 | goto end; | |
1828 | default: | |
7213a328 PP |
1829 | BT_LOGE_STR("Graph failed to complete successfully"); |
1830 | fprintf(stderr, "Graph failed to complete successfully\n"); | |
9009cc24 | 1831 | goto error; |
78586d8a | 1832 | } |
fec2a9f2 | 1833 | } |
290725f7 | 1834 | |
9009cc24 PP |
1835 | goto end; |
1836 | ||
1837 | error: | |
1838 | if (ret == 0) { | |
1839 | ret = -1; | |
1840 | } | |
1841 | ||
11e1d048 | 1842 | end: |
9009cc24 | 1843 | cmd_run_ctx_destroy(&ctx); |
290725f7 PP |
1844 | return ret; |
1845 | } | |
1846 | ||
9009cc24 PP |
1847 | static |
1848 | void warn_command_name_and_directory_clash(struct bt_config *cfg) | |
290725f7 | 1849 | { |
9009cc24 PP |
1850 | const char *env_clash; |
1851 | ||
290725f7 PP |
1852 | if (!cfg->command_name) { |
1853 | return; | |
1854 | } | |
1855 | ||
9009cc24 PP |
1856 | env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH); |
1857 | if (env_clash && strcmp(env_clash, "0") == 0) { | |
1858 | return; | |
1859 | } | |
1860 | ||
290725f7 PP |
1861 | if (g_file_test(cfg->command_name, |
1862 | G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { | |
1863 | fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n", | |
1864 | cfg->command_name); | |
1865 | fprintf(stderr, "trace located in the local `%s` directory, please use:\n", | |
1866 | cfg->command_name); | |
1867 | fprintf(stderr, "\n"); | |
1868 | fprintf(stderr, " babeltrace convert %s [OPTIONS]\n", | |
1869 | cfg->command_name); | |
1870 | } | |
1871 | } | |
1872 | ||
7213a328 PP |
1873 | static |
1874 | void init_log_level(void) | |
1875 | { | |
373c938b | 1876 | bt_cli_log_level = bt_log_get_level_from_env("BABELTRACE_CLI_LOG_LEVEL"); |
7213a328 PP |
1877 | } |
1878 | ||
5401f780 PP |
1879 | void set_sigint_handler(void) |
1880 | { | |
1881 | struct sigaction new_action, old_action; | |
1882 | ||
1883 | new_action.sa_handler = sigint_handler; | |
1884 | sigemptyset(&new_action.sa_mask); | |
1885 | new_action.sa_flags = 0; | |
1886 | sigaction(SIGINT, NULL, &old_action); | |
1887 | ||
1888 | if (old_action.sa_handler != SIG_IGN) { | |
1889 | sigaction(SIGINT, &new_action, NULL); | |
1890 | } | |
1891 | } | |
1892 | ||
290725f7 PP |
1893 | int main(int argc, const char **argv) |
1894 | { | |
1895 | int ret; | |
1896 | int retcode; | |
1897 | struct bt_config *cfg; | |
1898 | ||
7213a328 | 1899 | init_log_level(); |
5401f780 | 1900 | set_sigint_handler(); |
9009cc24 PP |
1901 | init_static_data(); |
1902 | cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode); | |
290725f7 PP |
1903 | |
1904 | if (retcode < 0) { | |
1905 | /* Quit without errors; typically usage/version */ | |
1906 | retcode = 0; | |
7213a328 | 1907 | BT_LOGI_STR("Quitting without errors."); |
290725f7 PP |
1908 | goto end; |
1909 | } | |
1910 | ||
1911 | if (retcode > 0) { | |
7213a328 | 1912 | BT_LOGE("Command-line error: retcode=%d", retcode); |
290725f7 PP |
1913 | goto end; |
1914 | } | |
1915 | ||
1916 | if (!cfg) { | |
7213a328 | 1917 | BT_LOGE_STR("Failed to create a valid Babeltrace configuration."); |
290725f7 | 1918 | fprintf(stderr, "Failed to create Babeltrace configuration\n"); |
db0f160a | 1919 | retcode = 1; |
290725f7 PP |
1920 | goto end; |
1921 | } | |
1922 | ||
7213a328 PP |
1923 | if (cfg->verbose) { |
1924 | bt_cli_log_level = BT_LOGGING_LEVEL_VERBOSE; | |
1925 | bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); | |
1926 | // TODO: for backward compat., set the log level | |
1927 | // environment variables of the known plugins | |
1928 | // to VERBOSE | |
1929 | } else if (cfg->debug) { | |
1930 | bt_cli_log_level = BT_LOGGING_LEVEL_DEBUG; | |
1931 | bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG); | |
1932 | // TODO: for backward compat., set the log level | |
1933 | // environment variables of the known plugins | |
1934 | // to DEBUG | |
1935 | } | |
1936 | ||
290725f7 PP |
1937 | babeltrace_debug = cfg->debug; |
1938 | babeltrace_verbose = cfg->verbose; | |
1939 | print_cfg(cfg); | |
1940 | ||
db0f160a PP |
1941 | if (cfg->command_needs_plugins) { |
1942 | ret = load_all_plugins(cfg->plugin_paths); | |
1943 | if (ret) { | |
7213a328 | 1944 | BT_LOGE("Failed to load plugins: ret=%d", ret); |
db0f160a PP |
1945 | retcode = 1; |
1946 | goto end; | |
1947 | } | |
1948 | } | |
1949 | ||
7213a328 PP |
1950 | BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"", |
1951 | cfg->command, cfg->command_name); | |
1952 | ||
290725f7 | 1953 | switch (cfg->command) { |
db0f160a PP |
1954 | case BT_CONFIG_COMMAND_RUN: |
1955 | ret = cmd_run(cfg); | |
290725f7 PP |
1956 | break; |
1957 | case BT_CONFIG_COMMAND_LIST_PLUGINS: | |
1958 | ret = cmd_list_plugins(cfg); | |
1959 | break; | |
22e22462 PP |
1960 | case BT_CONFIG_COMMAND_HELP: |
1961 | ret = cmd_help(cfg); | |
1962 | break; | |
a67681c1 PP |
1963 | case BT_CONFIG_COMMAND_QUERY: |
1964 | ret = cmd_query(cfg); | |
63ce0e1d | 1965 | break; |
db0f160a PP |
1966 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
1967 | ret = cmd_print_ctf_metadata(cfg); | |
1968 | break; | |
1969 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: | |
1970 | ret = cmd_print_lttng_live_sessions(cfg); | |
1971 | break; | |
290725f7 | 1972 | default: |
0fbb9a9f PP |
1973 | BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command); |
1974 | abort(); | |
290725f7 PP |
1975 | } |
1976 | ||
d26ef3e3 PP |
1977 | BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d", |
1978 | cfg->command, cfg->command_name, ret); | |
290725f7 PP |
1979 | warn_command_name_and_directory_clash(cfg); |
1980 | retcode = ret ? 1 : 0; | |
1981 | ||
1982 | end: | |
1983 | BT_PUT(cfg); | |
9009cc24 | 1984 | fini_static_data(); |
290725f7 | 1985 | return retcode; |
4c8bfb7e | 1986 | } |