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 PP |
368 | default: |
369 | assert(false); | |
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 PP |
521 | default: |
522 | assert(false); | |
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); | |
a8ff38ef PP |
584 | plugin_set = bt_plugin_create_all_from_dir(plugin_path, false); |
585 | if (!plugin_set) { | |
7213a328 | 586 | BT_LOGD("Unable to load dynamic plugins: path=\"%s\"", |
98ecef32 | 587 | plugin_path); |
33b34c43 PP |
588 | BT_PUT(plugin_path_value); |
589 | continue; | |
98ecef32 | 590 | } |
33b34c43 | 591 | |
a8ff38ef PP |
592 | add_to_loaded_plugins(plugin_set); |
593 | bt_put(plugin_set); | |
98ecef32 MD |
594 | BT_PUT(plugin_path_value); |
595 | } | |
33b34c43 PP |
596 | end: |
597 | return ret; | |
598 | } | |
599 | ||
600 | static | |
601 | int load_static_plugins(void) | |
602 | { | |
603 | int ret = 0; | |
a8ff38ef | 604 | struct bt_plugin_set *plugin_set; |
33b34c43 | 605 | |
7213a328 | 606 | BT_LOGI("Loading static plugins."); |
a8ff38ef PP |
607 | plugin_set = bt_plugin_create_all_from_static(); |
608 | if (!plugin_set) { | |
7213a328 | 609 | BT_LOGE("Unable to load static plugins."); |
33b34c43 PP |
610 | ret = -1; |
611 | goto end; | |
612 | } | |
613 | ||
a8ff38ef PP |
614 | add_to_loaded_plugins(plugin_set); |
615 | bt_put(plugin_set); | |
33b34c43 PP |
616 | end: |
617 | return ret; | |
98ecef32 MD |
618 | } |
619 | ||
9009cc24 PP |
620 | static |
621 | int load_all_plugins(struct bt_value *plugin_paths) | |
290725f7 PP |
622 | { |
623 | int ret = 0; | |
33b34c43 | 624 | |
290725f7 | 625 | if (load_dynamic_plugins(plugin_paths)) { |
290725f7 | 626 | ret = -1; |
c1870f57 JG |
627 | goto end; |
628 | } | |
629 | ||
290725f7 | 630 | if (load_static_plugins()) { |
290725f7 | 631 | ret = -1; |
c1870f57 JG |
632 | goto end; |
633 | } | |
634 | ||
7213a328 PP |
635 | BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len); |
636 | ||
290725f7 PP |
637 | end: |
638 | return ret; | |
639 | } | |
640 | ||
9009cc24 PP |
641 | static |
642 | void print_plugin_info(struct bt_plugin *plugin) | |
22e22462 PP |
643 | { |
644 | unsigned int major, minor, patch; | |
645 | const char *extra; | |
646 | enum bt_plugin_status version_status; | |
647 | const char *plugin_name; | |
648 | const char *path; | |
649 | const char *author; | |
650 | const char *license; | |
651 | const char *plugin_description; | |
652 | ||
653 | plugin_name = bt_plugin_get_name(plugin); | |
654 | path = bt_plugin_get_path(plugin); | |
655 | author = bt_plugin_get_author(plugin); | |
656 | license = bt_plugin_get_license(plugin); | |
657 | plugin_description = bt_plugin_get_description(plugin); | |
658 | version_status = bt_plugin_get_version(plugin, &major, &minor, | |
659 | &patch, &extra); | |
660 | printf("%s%s%s%s:\n", bt_common_color_bold(), | |
661 | bt_common_color_fg_blue(), plugin_name, | |
662 | bt_common_color_reset()); | |
663 | printf(" %sPath%s: %s\n", bt_common_color_bold(), | |
664 | bt_common_color_reset(), path ? path : "(None)"); | |
665 | ||
666 | if (version_status == BT_PLUGIN_STATUS_OK) { | |
667 | printf(" %sVersion%s: %u.%u.%u", | |
668 | bt_common_color_bold(), bt_common_color_reset(), | |
669 | major, minor, patch); | |
670 | ||
671 | if (extra) { | |
672 | printf("%s", extra); | |
673 | } | |
674 | ||
675 | printf("\n"); | |
676 | } | |
677 | ||
678 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), | |
679 | bt_common_color_reset(), | |
680 | plugin_description ? plugin_description : "(None)"); | |
681 | printf(" %sAuthor%s: %s\n", bt_common_color_bold(), | |
682 | bt_common_color_reset(), author ? author : "(Unknown)"); | |
683 | printf(" %sLicense%s: %s\n", bt_common_color_bold(), | |
684 | bt_common_color_reset(), | |
685 | license ? license : "(Unknown)"); | |
686 | } | |
687 | ||
9009cc24 PP |
688 | static |
689 | int cmd_query(struct bt_config *cfg) | |
63ce0e1d PP |
690 | { |
691 | int ret; | |
692 | struct bt_component_class *comp_cls = NULL; | |
693 | struct bt_value *results = NULL; | |
694 | ||
db0f160a | 695 | ret = load_all_plugins(cfg->plugin_paths); |
63ce0e1d PP |
696 | if (ret) { |
697 | goto end; | |
698 | } | |
699 | ||
a67681c1 | 700 | comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str, |
db0f160a | 701 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
a67681c1 | 702 | cfg->cmd_data.query.cfg_component->type); |
63ce0e1d | 703 | if (!comp_cls) { |
7213a328 PP |
704 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
705 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
706 | cfg->cmd_data.query.cfg_component->plugin_name->str, | |
707 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, | |
708 | cfg->cmd_data.query.cfg_component->type); | |
63ce0e1d PP |
709 | fprintf(stderr, "%s%sCannot find component class %s", |
710 | bt_common_color_bold(), | |
711 | bt_common_color_fg_red(), | |
712 | bt_common_color_reset()); | |
713 | print_plugin_comp_cls_opt(stderr, | |
a67681c1 | 714 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
db0f160a | 715 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
a67681c1 | 716 | cfg->cmd_data.query.cfg_component->type); |
63ce0e1d PP |
717 | fprintf(stderr, "\n"); |
718 | ret = -1; | |
719 | goto end; | |
720 | } | |
721 | ||
a67681c1 PP |
722 | results = bt_component_class_query(comp_cls, |
723 | cfg->cmd_data.query.object->str, | |
724 | cfg->cmd_data.query.cfg_component->params); | |
63ce0e1d | 725 | if (!results) { |
7213a328 PP |
726 | BT_LOGE("Failed to query component class: plugin-name=\"%s\", " |
727 | "comp-cls-name=\"%s\", comp-cls-type=%d " | |
728 | "object=\"%s\"", | |
729 | cfg->cmd_data.query.cfg_component->plugin_name->str, | |
730 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, | |
731 | cfg->cmd_data.query.cfg_component->type, | |
732 | cfg->cmd_data.query.object->str); | |
63ce0e1d PP |
733 | fprintf(stderr, "%s%sFailed to query info to %s", |
734 | bt_common_color_bold(), | |
735 | bt_common_color_fg_red(), | |
736 | bt_common_color_reset()); | |
737 | print_plugin_comp_cls_opt(stderr, | |
a67681c1 | 738 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
db0f160a | 739 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
a67681c1 PP |
740 | cfg->cmd_data.query.cfg_component->type); |
741 | fprintf(stderr, "%s%s with object `%s`%s\n", | |
63ce0e1d PP |
742 | bt_common_color_bold(), |
743 | bt_common_color_fg_red(), | |
a67681c1 | 744 | cfg->cmd_data.query.object->str, |
63ce0e1d PP |
745 | bt_common_color_reset()); |
746 | ret = -1; | |
747 | goto end; | |
748 | } | |
749 | ||
7213a328 | 750 | print_value(stdout, results, 0); |
63ce0e1d PP |
751 | |
752 | end: | |
753 | bt_put(comp_cls); | |
754 | bt_put(results); | |
755 | return ret; | |
756 | } | |
757 | ||
9009cc24 PP |
758 | static |
759 | int cmd_help(struct bt_config *cfg) | |
22e22462 PP |
760 | { |
761 | int ret; | |
762 | struct bt_plugin *plugin = NULL; | |
763 | size_t i; | |
764 | ||
db0f160a | 765 | ret = load_all_plugins(cfg->plugin_paths); |
22e22462 PP |
766 | if (ret) { |
767 | goto end; | |
768 | } | |
769 | ||
90de159b | 770 | plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str); |
22e22462 | 771 | if (!plugin) { |
7213a328 PP |
772 | BT_LOGE("Cannot find plugin: plugin-name=\"%s\"", |
773 | cfg->cmd_data.help.cfg_component->plugin_name->str); | |
22e22462 PP |
774 | fprintf(stderr, "%s%sCannot find plugin %s%s%s\n", |
775 | bt_common_color_bold(), bt_common_color_fg_red(), | |
776 | bt_common_color_fg_blue(), | |
90de159b | 777 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
22e22462 PP |
778 | bt_common_color_reset()); |
779 | ret = -1; | |
780 | goto end; | |
781 | } | |
782 | ||
783 | print_plugin_info(plugin); | |
784 | printf(" %sComponent classes%s: %d\n", | |
785 | bt_common_color_bold(), | |
786 | bt_common_color_reset(), | |
544d0515 | 787 | (int) bt_plugin_get_component_class_count(plugin)); |
22e22462 PP |
788 | |
789 | ||
90de159b | 790 | if (cfg->cmd_data.help.cfg_component->type != |
22e22462 PP |
791 | BT_COMPONENT_CLASS_TYPE_UNKNOWN) { |
792 | struct bt_component_class *needed_comp_cls = | |
793 | find_component_class( | |
90de159b | 794 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
db0f160a | 795 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
90de159b | 796 | cfg->cmd_data.help.cfg_component->type); |
22e22462 PP |
797 | |
798 | if (!needed_comp_cls) { | |
7213a328 PP |
799 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
800 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
801 | cfg->cmd_data.help.cfg_component->plugin_name->str, | |
802 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, | |
803 | cfg->cmd_data.help.cfg_component->type); | |
22e22462 PP |
804 | fprintf(stderr, "\n%s%sCannot find component class %s", |
805 | bt_common_color_bold(), | |
806 | bt_common_color_fg_red(), | |
807 | bt_common_color_reset()); | |
808 | print_plugin_comp_cls_opt(stderr, | |
90de159b | 809 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
db0f160a | 810 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
90de159b | 811 | cfg->cmd_data.help.cfg_component->type); |
22e22462 PP |
812 | fprintf(stderr, "\n"); |
813 | ret = -1; | |
814 | goto end; | |
815 | } | |
816 | ||
817 | bt_put(needed_comp_cls); | |
818 | } | |
819 | ||
820 | for (i = 0; i < bt_plugin_get_component_class_count(plugin); i++) { | |
821 | struct bt_component_class *comp_cls = | |
9ac68eb1 | 822 | bt_plugin_get_component_class_by_index(plugin, i); |
22e22462 PP |
823 | const char *comp_class_name = |
824 | bt_component_class_get_name(comp_cls); | |
825 | const char *comp_class_description = | |
826 | bt_component_class_get_description(comp_cls); | |
827 | const char *comp_class_help = | |
828 | bt_component_class_get_help(comp_cls); | |
829 | enum bt_component_class_type type = | |
830 | bt_component_class_get_type(comp_cls); | |
831 | ||
832 | assert(comp_cls); | |
833 | ||
90de159b | 834 | if (cfg->cmd_data.help.cfg_component->type != |
22e22462 | 835 | BT_COMPONENT_CLASS_TYPE_UNKNOWN) { |
db0f160a | 836 | if (strcmp(cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
22e22462 PP |
837 | comp_class_name) != 0 && |
838 | type == | |
90de159b | 839 | cfg->cmd_data.help.cfg_component->type) { |
22e22462 PP |
840 | bt_put(comp_cls); |
841 | continue; | |
842 | } | |
843 | } | |
844 | ||
845 | printf("\n"); | |
846 | print_plugin_comp_cls_opt(stdout, | |
90de159b | 847 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
22e22462 PP |
848 | comp_class_name, |
849 | type); | |
850 | printf("\n"); | |
851 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), | |
852 | bt_common_color_reset(), | |
853 | comp_class_description ? comp_class_description : "(None)"); | |
854 | ||
855 | if (comp_class_help) { | |
856 | printf("\n%s\n", comp_class_help); | |
857 | } | |
858 | ||
859 | bt_put(comp_cls); | |
860 | } | |
861 | ||
862 | end: | |
863 | bt_put(plugin); | |
864 | return ret; | |
865 | } | |
866 | ||
9009cc24 PP |
867 | static |
868 | int cmd_list_plugins(struct bt_config *cfg) | |
290725f7 | 869 | { |
7213a328 | 870 | int ret = 0; |
290725f7 PP |
871 | int plugins_count, component_classes_count = 0, i; |
872 | ||
db0f160a | 873 | ret = load_all_plugins(cfg->plugin_paths); |
290725f7 | 874 | if (ret) { |
56a1cced JG |
875 | goto end; |
876 | } | |
877 | ||
22e22462 | 878 | printf("From the following plugin paths:\n\n"); |
7213a328 | 879 | print_value(stdout, cfg->plugin_paths, 2); |
22e22462 | 880 | printf("\n"); |
290725f7 PP |
881 | plugins_count = loaded_plugins->len; |
882 | if (plugins_count == 0) { | |
7213a328 | 883 | printf("No plugins found.\n"); |
56a1cced JG |
884 | goto end; |
885 | } | |
886 | ||
290725f7 PP |
887 | for (i = 0; i < plugins_count; i++) { |
888 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); | |
889 | ||
890 | component_classes_count += bt_plugin_get_component_class_count(plugin); | |
891 | } | |
33bceaf8 | 892 | |
290725f7 PP |
893 | printf("Found %s%d%s component classes in %s%d%s plugins.\n", |
894 | bt_common_color_bold(), | |
895 | component_classes_count, | |
896 | bt_common_color_reset(), | |
897 | bt_common_color_bold(), | |
898 | plugins_count, | |
899 | bt_common_color_reset()); | |
900 | ||
901 | for (i = 0; i < plugins_count; i++) { | |
902 | int j; | |
903 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); | |
290725f7 PP |
904 | |
905 | component_classes_count = | |
906 | bt_plugin_get_component_class_count(plugin); | |
22e22462 PP |
907 | printf("\n"); |
908 | print_plugin_info(plugin); | |
290725f7 PP |
909 | |
910 | if (component_classes_count == 0) { | |
9009cc24 | 911 | printf(" %sComponent classes%s: (none)\n", |
290725f7 PP |
912 | bt_common_color_bold(), |
913 | bt_common_color_reset()); | |
914 | } else { | |
915 | printf(" %sComponent classes%s:\n", | |
916 | bt_common_color_bold(), | |
917 | bt_common_color_reset()); | |
918 | } | |
919 | ||
920 | for (j = 0; j < component_classes_count; j++) { | |
921 | struct bt_component_class *comp_class = | |
9ac68eb1 PP |
922 | bt_plugin_get_component_class_by_index( |
923 | plugin, j); | |
290725f7 PP |
924 | const char *comp_class_name = |
925 | bt_component_class_get_name(comp_class); | |
926 | const char *comp_class_description = | |
927 | bt_component_class_get_description(comp_class); | |
928 | enum bt_component_class_type type = | |
929 | bt_component_class_get_type(comp_class); | |
930 | ||
22e22462 PP |
931 | printf(" "); |
932 | print_plugin_comp_cls_opt(stdout, | |
933 | bt_plugin_get_name(plugin), comp_class_name, | |
934 | type); | |
290725f7 PP |
935 | |
936 | if (comp_class_description) { | |
937 | printf(": %s", comp_class_description); | |
938 | } | |
939 | ||
940 | printf("\n"); | |
941 | bt_put(comp_class); | |
942 | } | |
943 | } | |
944 | ||
945 | end: | |
946 | return ret; | |
947 | } | |
948 | ||
9009cc24 PP |
949 | static |
950 | int cmd_print_lttng_live_sessions(struct bt_config *cfg) | |
db0f160a PP |
951 | { |
952 | printf("TODO\n"); | |
953 | return -1; | |
954 | } | |
955 | ||
9009cc24 PP |
956 | static |
957 | int cmd_print_ctf_metadata(struct bt_config *cfg) | |
05a67631 PP |
958 | { |
959 | int ret = 0; | |
960 | struct bt_component_class *comp_cls = NULL; | |
05a67631 | 961 | struct bt_value *results = NULL; |
05a67631 PP |
962 | struct bt_value *params = NULL; |
963 | struct bt_value *metadata_text_value = NULL; | |
964 | const char *metadata_text = NULL; | |
db0f160a PP |
965 | static const char * const plugin_name = "ctf"; |
966 | static const char * const comp_cls_name = "fs"; | |
967 | static const enum bt_component_class_type comp_cls_type = | |
968 | BT_COMPONENT_CLASS_TYPE_SOURCE; | |
969 | ||
970 | assert(cfg->cmd_data.print_ctf_metadata.path); | |
971 | comp_cls = find_component_class(plugin_name, comp_cls_name, | |
972 | comp_cls_type); | |
05a67631 | 973 | if (!comp_cls) { |
7213a328 PP |
974 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
975 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
976 | plugin_name, comp_cls_name, | |
977 | BT_COMPONENT_CLASS_TYPE_SOURCE); | |
05a67631 PP |
978 | fprintf(stderr, "%s%sCannot find component class %s", |
979 | bt_common_color_bold(), | |
980 | bt_common_color_fg_red(), | |
981 | bt_common_color_reset()); | |
db0f160a PP |
982 | print_plugin_comp_cls_opt(stderr, plugin_name, |
983 | comp_cls_name, comp_cls_type); | |
05a67631 PP |
984 | fprintf(stderr, "\n"); |
985 | ret = -1; | |
986 | goto end; | |
987 | } | |
988 | ||
05a67631 PP |
989 | params = bt_value_map_create(); |
990 | if (!params) { | |
991 | ret = -1; | |
992 | goto end; | |
993 | } | |
994 | ||
db0f160a PP |
995 | ret = bt_value_map_insert_string(params, "path", |
996 | cfg->cmd_data.print_ctf_metadata.path->str); | |
05a67631 PP |
997 | if (ret) { |
998 | ret = -1; | |
999 | goto end; | |
1000 | } | |
1001 | ||
a67681c1 | 1002 | results = bt_component_class_query(comp_cls, "metadata-info", |
05a67631 PP |
1003 | params); |
1004 | if (!results) { | |
1005 | ret = -1; | |
7213a328 | 1006 | BT_LOGE_STR("Failed to query for metadata info."); |
a67681c1 | 1007 | fprintf(stderr, "%s%sFailed to request metadata info%s\n", |
05a67631 PP |
1008 | bt_common_color_bold(), |
1009 | bt_common_color_fg_red(), | |
1010 | bt_common_color_reset()); | |
1011 | goto end; | |
1012 | } | |
1013 | ||
1014 | metadata_text_value = bt_value_map_get(results, "text"); | |
1015 | if (!metadata_text_value) { | |
7213a328 | 1016 | BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object."); |
05a67631 PP |
1017 | ret = -1; |
1018 | goto end; | |
1019 | } | |
1020 | ||
1021 | ret = bt_value_string_get(metadata_text_value, &metadata_text); | |
1022 | assert(ret == 0); | |
1023 | printf("%s\n", metadata_text); | |
1024 | ||
1025 | end: | |
1026 | bt_put(results); | |
05a67631 PP |
1027 | bt_put(params); |
1028 | bt_put(metadata_text_value); | |
1029 | bt_put(comp_cls); | |
05a67631 PP |
1030 | return 0; |
1031 | } | |
1032 | ||
9009cc24 PP |
1033 | struct cmd_run_ctx { |
1034 | /* Owned by this */ | |
1035 | GHashTable *components; | |
1036 | ||
1037 | /* Owned by this */ | |
1038 | struct bt_graph *graph; | |
1039 | ||
1040 | /* Weak */ | |
1041 | struct bt_config *cfg; | |
1042 | ||
1043 | bool connect_ports; | |
1044 | }; | |
1045 | ||
1046 | static | |
1047 | int cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1048 | struct cmd_run_ctx *ctx, struct bt_component *upstream_comp, | |
1049 | struct bt_port *upstream_port, | |
1050 | struct bt_config_connection *cfg_conn) | |
290725f7 PP |
1051 | { |
1052 | int ret = 0; | |
9009cc24 PP |
1053 | GQuark downstreamp_comp_name_quark; |
1054 | struct bt_component *downstream_comp; | |
1055 | int64_t downstream_port_count; | |
1056 | uint64_t i; | |
1057 | int64_t (*port_count_fn)(struct bt_component *); | |
1058 | struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t); | |
1059 | void *conn = NULL; | |
1060 | ||
7213a328 PP |
1061 | BT_LOGI("Connecting upstream port to the next available downstream port: " |
1062 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1063 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1064 | upstream_port, bt_port_get_name(upstream_port), | |
1065 | cfg_conn->downstream_comp_name->str, | |
1066 | cfg_conn->arg->str); | |
9009cc24 PP |
1067 | downstreamp_comp_name_quark = g_quark_from_string( |
1068 | cfg_conn->downstream_comp_name->str); | |
1069 | assert(downstreamp_comp_name_quark > 0); | |
1070 | downstream_comp = g_hash_table_lookup(ctx->components, | |
1071 | (gpointer) (long) downstreamp_comp_name_quark); | |
1072 | if (!downstream_comp) { | |
7213a328 PP |
1073 | BT_LOGE("Cannot find downstream component: comp-name=\"%s\", " |
1074 | "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str, | |
1075 | cfg_conn->arg->str); | |
9009cc24 PP |
1076 | fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n", |
1077 | cfg_conn->arg->str); | |
1078 | goto error; | |
1079 | } | |
1080 | ||
1081 | if (bt_component_is_filter(downstream_comp)) { | |
1082 | port_count_fn = bt_component_filter_get_input_port_count; | |
1083 | port_by_index_fn = bt_component_filter_get_input_port_by_index; | |
1084 | } else if (bt_component_is_sink(downstream_comp)) { | |
1085 | port_count_fn = bt_component_sink_get_input_port_count; | |
1086 | port_by_index_fn = bt_component_sink_get_input_port_by_index; | |
1087 | } else { | |
1088 | /* | |
1089 | * Should never happen because the connections are | |
1090 | * validated before we get here. | |
1091 | */ | |
7213a328 PP |
1092 | BT_LOGF("Invalid connection: downstream component is a source: " |
1093 | "conn-arg=\"%s\"", cfg_conn->arg->str); | |
9009cc24 PP |
1094 | assert(false); |
1095 | } | |
290725f7 | 1096 | |
9009cc24 PP |
1097 | downstream_port_count = port_count_fn(downstream_comp); |
1098 | assert(downstream_port_count >= 0); | |
1099 | ||
1100 | for (i = 0; i < downstream_port_count; i++) { | |
1101 | struct bt_port *downstream_port = | |
1102 | port_by_index_fn(downstream_comp, i); | |
1103 | const char *downstream_port_name; | |
1104 | ||
1105 | assert(downstream_port); | |
1106 | ||
1107 | /* Skip port if it's already connected */ | |
1108 | if (bt_port_is_connected(downstream_port)) { | |
1109 | bt_put(downstream_port); | |
7213a328 PP |
1110 | BT_LOGD("Skipping downstream port: already connected: " |
1111 | "port-addr=%p, port-name=\"%s\"", | |
1112 | downstream_port, | |
1113 | bt_port_get_name(downstream_port)); | |
9009cc24 PP |
1114 | continue; |
1115 | } | |
1116 | ||
1117 | downstream_port_name = bt_port_get_name(downstream_port); | |
1118 | assert(downstream_port_name); | |
1119 | ||
1120 | if (bt_common_star_glob_match( | |
1121 | cfg_conn->downstream_port_glob->str, -1ULL, | |
1122 | downstream_port_name, -1ULL)) { | |
1123 | /* We have a winner! */ | |
1124 | conn = bt_graph_connect_ports(ctx->graph, | |
1125 | upstream_port, downstream_port); | |
1126 | bt_put(downstream_port); | |
1127 | if (!conn) { | |
7213a328 PP |
1128 | BT_LOGE("Cannot create connection: graph refuses to connect ports: " |
1129 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
1130 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1131 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1132 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1133 | "conn-arg=\"%s\"", | |
1134 | upstream_comp, bt_component_get_name(upstream_comp), | |
1135 | upstream_port, bt_port_get_name(upstream_port), | |
1136 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1137 | downstream_port, downstream_port_name, | |
1138 | cfg_conn->arg->str); | |
9009cc24 PP |
1139 | fprintf(stderr, |
1140 | "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n", | |
1141 | bt_port_get_name(upstream_port), | |
1142 | downstream_port_name, | |
1143 | cfg_conn->arg->str); | |
1144 | goto error; | |
1145 | } | |
1146 | ||
7213a328 PP |
1147 | BT_LOGI("Connected component ports: " |
1148 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
1149 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1150 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
1151 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
1152 | "conn-arg=\"%s\"", | |
1153 | upstream_comp, bt_component_get_name(upstream_comp), | |
1154 | upstream_port, bt_port_get_name(upstream_port), | |
1155 | downstream_comp, cfg_conn->downstream_comp_name->str, | |
1156 | downstream_port, downstream_port_name, | |
1157 | cfg_conn->arg->str); | |
1158 | ||
9009cc24 PP |
1159 | goto end; |
1160 | } | |
1161 | ||
1162 | bt_put(downstream_port); | |
1163 | } | |
1164 | ||
1165 | if (!conn) { | |
7213a328 PP |
1166 | BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: " |
1167 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1168 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", | |
1169 | upstream_port, bt_port_get_name(upstream_port), | |
1170 | cfg_conn->downstream_comp_name->str, | |
1171 | cfg_conn->arg->str); | |
9009cc24 PP |
1172 | fprintf(stderr, |
1173 | "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n", | |
1174 | bt_port_get_name(upstream_port), cfg_conn->arg->str); | |
1175 | goto error; | |
05a67631 PP |
1176 | } |
1177 | ||
9009cc24 PP |
1178 | goto end; |
1179 | ||
1180 | error: | |
1181 | ret = -1; | |
1182 | ||
1183 | end: | |
1184 | bt_put(conn); | |
1185 | return ret; | |
1186 | } | |
1187 | ||
1188 | static | |
1189 | int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx, | |
1190 | struct bt_port *upstream_port) | |
1191 | { | |
1192 | int ret = 0; | |
1193 | const char *upstream_port_name; | |
1194 | const char *upstream_comp_name; | |
1195 | struct bt_component *upstream_comp = NULL; | |
1196 | size_t i; | |
1197 | ||
1198 | assert(ctx); | |
1199 | assert(upstream_port); | |
1200 | upstream_port_name = bt_port_get_name(upstream_port); | |
1201 | assert(upstream_port_name); | |
1202 | upstream_comp = bt_port_get_component(upstream_port); | |
1203 | if (!upstream_comp) { | |
7213a328 PP |
1204 | BT_LOGW("Upstream port to connect is not part of a component: " |
1205 | "port-addr=%p, port-name=\"%s\"", | |
1206 | upstream_port, upstream_port_name); | |
98ecef32 MD |
1207 | ret = -1; |
1208 | goto end; | |
33bceaf8 JG |
1209 | } |
1210 | ||
9009cc24 PP |
1211 | upstream_comp_name = bt_component_get_name(upstream_comp); |
1212 | assert(upstream_comp_name); | |
7213a328 PP |
1213 | BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", " |
1214 | "port-addr=%p, port-name=\"%s\"", | |
1215 | upstream_comp, upstream_comp_name, | |
1216 | upstream_port, upstream_port_name); | |
9009cc24 PP |
1217 | |
1218 | for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) { | |
1219 | struct bt_config_connection *cfg_conn = | |
1220 | g_ptr_array_index( | |
1221 | ctx->cfg->cmd_data.run.connections, i); | |
1222 | ||
1223 | if (strcmp(cfg_conn->upstream_comp_name->str, | |
1224 | upstream_comp_name) == 0) { | |
1225 | if (bt_common_star_glob_match( | |
1226 | cfg_conn->upstream_port_glob->str, | |
1227 | -1ULL, upstream_port_name, -1ULL)) { | |
1228 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( | |
1229 | ctx, upstream_comp, upstream_port, | |
1230 | cfg_conn); | |
1231 | if (ret) { | |
7213a328 PP |
1232 | BT_LOGE("Cannot connect upstream port: " |
1233 | "port-addr=%p, port-name=\"%s\"", | |
1234 | upstream_port, | |
1235 | upstream_port_name); | |
9009cc24 PP |
1236 | fprintf(stderr, |
1237 | "Cannot connect port `%s` of component `%s` to a downstream port: %s\n", | |
1238 | upstream_port_name, | |
1239 | upstream_comp_name, | |
1240 | cfg_conn->arg->str); | |
1241 | goto error; | |
1242 | } | |
1243 | ||
1244 | goto end; | |
1245 | } | |
1246 | } | |
1247 | } | |
1248 | ||
7213a328 PP |
1249 | BT_LOGE("Cannot connect upstream port: port does not match any connection argument: " |
1250 | "port-addr=%p, port-name=\"%s\"", upstream_port, | |
1251 | upstream_port_name); | |
9009cc24 PP |
1252 | fprintf(stderr, |
1253 | "Cannot create connection: upstream port `%s` does not match any connection\n", | |
7213a328 | 1254 | upstream_port_name); |
9009cc24 PP |
1255 | |
1256 | error: | |
1257 | ret = -1; | |
1258 | ||
1259 | end: | |
1260 | bt_put(upstream_comp); | |
1261 | return ret; | |
1262 | } | |
1263 | ||
1264 | static | |
1265 | void graph_port_added_listener(struct bt_port *port, void *data) | |
1266 | { | |
1267 | struct bt_component *comp = NULL; | |
1268 | struct cmd_run_ctx *ctx = data; | |
1269 | ||
7213a328 PP |
1270 | BT_LOGI("Port added to a graph's component: port-addr=%p, port-name=\"%s\"", |
1271 | port, bt_port_get_name(port)); | |
1272 | ||
9009cc24 | 1273 | if (bt_port_is_connected(port)) { |
7213a328 | 1274 | BT_LOGW_STR("Port is already connected."); |
56a1cced JG |
1275 | goto end; |
1276 | } | |
7c7c0433 | 1277 | |
9009cc24 PP |
1278 | comp = bt_port_get_component(port); |
1279 | if (!comp) { | |
7213a328 | 1280 | BT_LOGW_STR("Port has no component."); |
7c7c0433 JG |
1281 | goto end; |
1282 | } | |
1283 | ||
9009cc24 | 1284 | if (!bt_port_is_output(port)) { |
7213a328 | 1285 | BT_LOGI_STR("Skipping input port."); |
61ddbc8a JG |
1286 | goto end; |
1287 | } | |
1288 | ||
9009cc24 | 1289 | if (cmd_run_ctx_connect_upstream_port(ctx, port)) { |
7213a328 | 1290 | BT_LOGF_STR("Cannot connect upstream port."); |
9009cc24 PP |
1291 | fprintf(stderr, "Added port could not be connected: aborting\n"); |
1292 | abort(); | |
1293 | } | |
1294 | ||
1295 | end: | |
1296 | bt_put(comp); | |
1297 | return; | |
1298 | } | |
1299 | ||
1300 | static | |
1301 | void graph_port_removed_listener(struct bt_component *component, | |
1302 | struct bt_port *port, void *data) | |
1303 | { | |
7213a328 PP |
1304 | BT_LOGI("Port removed from a graph's component: comp-addr=%p, " |
1305 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
1306 | component, bt_component_get_name(component), | |
1307 | port, bt_port_get_name(port)); | |
9009cc24 PP |
1308 | } |
1309 | ||
1310 | static | |
1311 | void graph_ports_connected_listener(struct bt_port *upstream_port, | |
1312 | struct bt_port *downstream_port, void *data) | |
1313 | { | |
7213a328 PP |
1314 | BT_LOGI("Graph's component ports connected: " |
1315 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1316 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
1317 | upstream_port, bt_port_get_name(upstream_port), | |
1318 | downstream_port, bt_port_get_name(downstream_port)); | |
9009cc24 PP |
1319 | } |
1320 | ||
1321 | static | |
1322 | void graph_ports_disconnected_listener( | |
1323 | struct bt_component *upstream_component, | |
1324 | struct bt_component *downstream_component, | |
1325 | struct bt_port *upstream_port, struct bt_port *downstream_port, | |
1326 | void *data) | |
1327 | { | |
7213a328 PP |
1328 | BT_LOGI("Graph's component ports disconnected: " |
1329 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
1330 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
1331 | upstream_port, bt_port_get_name(upstream_port), | |
1332 | downstream_port, bt_port_get_name(downstream_port)); | |
9009cc24 PP |
1333 | } |
1334 | ||
1335 | static | |
1336 | void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx) | |
1337 | { | |
1338 | if (!ctx) { | |
1339 | return; | |
1340 | } | |
1341 | ||
1342 | if (ctx->components) { | |
1343 | g_hash_table_destroy(ctx->components); | |
1344 | ctx->components = NULL; | |
1345 | } | |
1346 | ||
1347 | BT_PUT(ctx->graph); | |
5401f780 | 1348 | the_graph = NULL; |
9009cc24 PP |
1349 | ctx->cfg = NULL; |
1350 | } | |
1351 | ||
1352 | static | |
1353 | int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg) | |
1354 | { | |
1355 | int ret = 0; | |
1356 | ||
1357 | ctx->cfg = cfg; | |
1358 | ctx->connect_ports = false; | |
1359 | ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
1360 | NULL, bt_put); | |
1361 | if (!ctx->components) { | |
1362 | goto error; | |
1363 | } | |
1364 | ||
1365 | ctx->graph = bt_graph_create(); | |
1366 | if (!ctx->graph) { | |
1367 | goto error; | |
1368 | } | |
1369 | ||
5401f780 | 1370 | the_graph = ctx->graph; |
9009cc24 PP |
1371 | ret = bt_graph_add_port_added_listener(ctx->graph, |
1372 | graph_port_added_listener, ctx); | |
1373 | if (ret) { | |
1374 | goto error; | |
1375 | } | |
1376 | ||
1377 | ret = bt_graph_add_port_removed_listener(ctx->graph, | |
1378 | graph_port_removed_listener, ctx); | |
1379 | if (ret) { | |
1380 | goto error; | |
1381 | } | |
1382 | ||
1383 | ret = bt_graph_add_ports_connected_listener(ctx->graph, | |
1384 | graph_ports_connected_listener, ctx); | |
1385 | if (ret) { | |
1386 | goto error; | |
1387 | } | |
1388 | ||
1389 | ret = bt_graph_add_ports_disconnected_listener(ctx->graph, | |
1390 | graph_ports_disconnected_listener, ctx); | |
1391 | if (ret) { | |
1392 | goto error; | |
1393 | } | |
1394 | ||
1395 | goto end; | |
1396 | ||
1397 | error: | |
1398 | cmd_run_ctx_destroy(ctx); | |
1399 | ret = -1; | |
1400 | ||
1401 | end: | |
1402 | return ret; | |
1403 | } | |
1404 | ||
1405 | static | |
1406 | int cmd_run_ctx_create_components_from_config_components( | |
1407 | struct cmd_run_ctx *ctx, GPtrArray *cfg_components) | |
1408 | { | |
1409 | size_t i; | |
1410 | struct bt_component_class *comp_cls = NULL; | |
1411 | struct bt_component *comp = NULL; | |
1412 | int ret = 0; | |
1413 | ||
1414 | for (i = 0; i < cfg_components->len; i++) { | |
1415 | struct bt_config_component *cfg_comp = | |
1416 | g_ptr_array_index(cfg_components, i); | |
1417 | GQuark quark; | |
1418 | ||
1419 | comp_cls = find_component_class(cfg_comp->plugin_name->str, | |
1420 | cfg_comp->comp_cls_name->str, cfg_comp->type); | |
1421 | if (!comp_cls) { | |
7213a328 PP |
1422 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
1423 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
1424 | cfg_comp->plugin_name->str, | |
1425 | cfg_comp->comp_cls_name->str, | |
1426 | cfg_comp->type); | |
9009cc24 PP |
1427 | fprintf(stderr, "%s%sCannot find component class %s", |
1428 | bt_common_color_bold(), | |
1429 | bt_common_color_fg_red(), | |
1430 | bt_common_color_reset()); | |
1431 | print_plugin_comp_cls_opt(stderr, | |
1432 | cfg_comp->plugin_name->str, | |
1433 | cfg_comp->comp_cls_name->str, | |
1434 | cfg_comp->type); | |
1435 | fprintf(stderr, "\n"); | |
1436 | goto error; | |
1437 | } | |
1438 | ||
1439 | comp = bt_component_create(comp_cls, | |
1440 | cfg_comp->instance_name->str, cfg_comp->params); | |
1441 | if (!comp) { | |
7213a328 PP |
1442 | BT_LOGE("Cannot create component: plugin-name=\"%s\", " |
1443 | "comp-cls-name=\"%s\", comp-cls-type=%d", | |
1444 | "comp-name=\"%s\"", | |
1445 | cfg_comp->plugin_name->str, | |
1446 | cfg_comp->comp_cls_name->str, | |
1447 | cfg_comp->type, cfg_comp->instance_name->str); | |
9009cc24 PP |
1448 | fprintf(stderr, "%s%sCannot create component `%s`%s\n", |
1449 | bt_common_color_bold(), | |
1450 | bt_common_color_fg_red(), | |
1451 | cfg_comp->instance_name->str, | |
1452 | bt_common_color_reset()); | |
1453 | goto error; | |
1454 | } | |
1455 | ||
7213a328 PP |
1456 | BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"", |
1457 | comp, cfg_comp->instance_name->str); | |
9009cc24 PP |
1458 | quark = g_quark_from_string(cfg_comp->instance_name->str); |
1459 | assert(quark > 0); | |
1460 | g_hash_table_insert(ctx->components, | |
1461 | (gpointer) (long) quark, comp); | |
1462 | comp = NULL; | |
1463 | BT_PUT(comp_cls); | |
1464 | } | |
1465 | ||
1466 | goto end; | |
1467 | ||
1468 | error: | |
1469 | ret = -1; | |
1470 | ||
1471 | end: | |
1472 | bt_put(comp); | |
1473 | bt_put(comp_cls); | |
1474 | return ret; | |
1475 | } | |
56a1cced | 1476 | |
9009cc24 PP |
1477 | static |
1478 | int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx) | |
1479 | { | |
1480 | int ret = 0; | |
1481 | ||
1482 | /* | |
1483 | * Make sure that, during this phase, our graph's "port added" | |
1484 | * listener does not connect ports while we are creating the | |
1485 | * components because we have a special, initial phase for | |
1486 | * this. | |
1487 | */ | |
1488 | ctx->connect_ports = false; | |
1489 | ||
1490 | ret = cmd_run_ctx_create_components_from_config_components( | |
1491 | ctx, ctx->cfg->cmd_data.run.sources); | |
1492 | if (ret) { | |
7c7c0433 | 1493 | ret = -1; |
2e339de1 JG |
1494 | goto end; |
1495 | } | |
1496 | ||
9009cc24 PP |
1497 | ret = cmd_run_ctx_create_components_from_config_components( |
1498 | ctx, ctx->cfg->cmd_data.run.filters); | |
6c2f3ee5 | 1499 | if (ret) { |
290725f7 | 1500 | ret = -1; |
fec2a9f2 JG |
1501 | goto end; |
1502 | } | |
78586d8a | 1503 | |
9009cc24 PP |
1504 | ret = cmd_run_ctx_create_components_from_config_components( |
1505 | ctx, ctx->cfg->cmd_data.run.sinks); | |
1506 | if (ret) { | |
1507 | ret = -1; | |
1508 | goto end; | |
1509 | } | |
1510 | ||
1511 | end: | |
1512 | return ret; | |
1513 | } | |
1514 | ||
1515 | static | |
1516 | int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx, | |
1517 | struct bt_component *comp, | |
1518 | int64_t (*port_count_fn)(struct bt_component *), | |
1519 | struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t)) | |
1520 | { | |
1521 | int ret = 0; | |
1522 | int64_t count; | |
1523 | uint64_t i; | |
1524 | ||
1525 | count = port_count_fn(comp); | |
1526 | assert(count >= 0); | |
1527 | ||
1528 | for (i = 0; i < count; i++) { | |
1529 | struct bt_port *upstream_port = port_by_index_fn(comp, i); | |
1530 | ||
1531 | assert(upstream_port); | |
1532 | ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port); | |
1533 | bt_put(upstream_port); | |
1534 | if (ret) { | |
1535 | goto end; | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | end: | |
1540 | return ret; | |
1541 | } | |
1542 | ||
1543 | static | |
1544 | int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx) | |
1545 | { | |
1546 | int ret = 0; | |
1547 | GHashTableIter iter; | |
1548 | gpointer g_name_quark, g_comp; | |
1549 | ||
1550 | ctx->connect_ports = true; | |
1551 | g_hash_table_iter_init(&iter, ctx->components); | |
1552 | ||
1553 | while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) { | |
1554 | if (bt_component_is_source(g_comp)) { | |
1555 | ret = cmd_run_ctx_connect_comp_ports(ctx, | |
1556 | g_comp, bt_component_source_get_output_port_count, | |
1557 | bt_component_source_get_output_port_by_index); | |
1558 | } else if (bt_component_is_filter(g_comp)) { | |
1559 | ret = cmd_run_ctx_connect_comp_ports(ctx, | |
1560 | g_comp, bt_component_filter_get_output_port_count, | |
1561 | bt_component_filter_get_output_port_by_index); | |
1562 | } | |
1563 | ||
1564 | if (ret) { | |
1565 | goto end; | |
1566 | } | |
1567 | } | |
1568 | ||
1569 | end: | |
1570 | return ret; | |
1571 | } | |
1572 | ||
1573 | static | |
1574 | int cmd_run(struct bt_config *cfg) | |
1575 | { | |
1576 | int ret = 0; | |
1577 | struct cmd_run_ctx ctx = { 0 }; | |
1578 | ||
1579 | ret = load_all_plugins(cfg->plugin_paths); | |
1580 | if (ret) { | |
1581 | goto error; | |
1582 | } | |
1583 | ||
1584 | /* Initialize the command's context and the graph object */ | |
1585 | if (cmd_run_ctx_init(&ctx, cfg)) { | |
7213a328 | 1586 | BT_LOGE_STR("Cannot initialize the command's context."); |
9009cc24 PP |
1587 | fprintf(stderr, "Cannot initialize the command's context\n"); |
1588 | goto error; | |
1589 | } | |
1590 | ||
1591 | /* Create the requested component instances */ | |
1592 | if (cmd_run_ctx_create_components(&ctx)) { | |
7213a328 | 1593 | BT_LOGE_STR("Cannot create components."); |
9009cc24 PP |
1594 | fprintf(stderr, "Cannot create components\n"); |
1595 | goto error; | |
1596 | } | |
1597 | ||
1598 | /* Connect the initially visible component ports */ | |
1599 | if (cmd_run_ctx_connect_ports(&ctx)) { | |
7213a328 | 1600 | BT_LOGE_STR("Cannot connect initial component ports."); |
9009cc24 PP |
1601 | fprintf(stderr, "Cannot connect initial component ports\n"); |
1602 | goto error; | |
1603 | } | |
1604 | ||
5401f780 PP |
1605 | if (canceled) { |
1606 | goto end; | |
1607 | } | |
1608 | ||
7213a328 PP |
1609 | BT_LOGI_STR("Running the graph."); |
1610 | ||
9009cc24 | 1611 | /* Run the graph */ |
fec2a9f2 | 1612 | while (true) { |
9009cc24 | 1613 | enum bt_graph_status graph_status = bt_graph_run(ctx.graph); |
61ddbc8a | 1614 | |
61ddbc8a | 1615 | switch (graph_status) { |
9009cc24 PP |
1616 | case BT_GRAPH_STATUS_OK: |
1617 | break; | |
5401f780 PP |
1618 | case BT_GRAPH_STATUS_CANCELED: |
1619 | BT_LOGI("Graph was canceled by user: status=%d", | |
1620 | graph_status); | |
1621 | goto error; | |
61ddbc8a | 1622 | case BT_GRAPH_STATUS_AGAIN: |
5401f780 PP |
1623 | if (bt_graph_is_canceled(ctx.graph)) { |
1624 | BT_LOGI("Graph was canceled by user: status=%d", | |
1625 | graph_status); | |
1626 | goto error; | |
1627 | } | |
1628 | ||
9009cc24 | 1629 | if (cfg->cmd_data.run.retry_duration_us > 0) { |
7213a328 PP |
1630 | BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: " |
1631 | "time-us=%" PRIu64, | |
1632 | cfg->cmd_data.run.retry_duration_us); | |
1633 | ||
9009cc24 PP |
1634 | if (usleep(cfg->cmd_data.run.retry_duration_us)) { |
1635 | // TODO: check EINTR and signal handler | |
1636 | } | |
1637 | } | |
78586d8a | 1638 | break; |
fec2a9f2 JG |
1639 | case BT_COMPONENT_STATUS_END: |
1640 | goto end; | |
1641 | default: | |
7213a328 PP |
1642 | BT_LOGE_STR("Graph failed to complete successfully"); |
1643 | fprintf(stderr, "Graph failed to complete successfully\n"); | |
9009cc24 | 1644 | goto error; |
78586d8a | 1645 | } |
fec2a9f2 | 1646 | } |
290725f7 | 1647 | |
9009cc24 PP |
1648 | goto end; |
1649 | ||
1650 | error: | |
1651 | if (ret == 0) { | |
1652 | ret = -1; | |
1653 | } | |
1654 | ||
11e1d048 | 1655 | end: |
9009cc24 | 1656 | cmd_run_ctx_destroy(&ctx); |
290725f7 PP |
1657 | return ret; |
1658 | } | |
1659 | ||
9009cc24 PP |
1660 | static |
1661 | void warn_command_name_and_directory_clash(struct bt_config *cfg) | |
290725f7 | 1662 | { |
9009cc24 PP |
1663 | const char *env_clash; |
1664 | ||
290725f7 PP |
1665 | if (!cfg->command_name) { |
1666 | return; | |
1667 | } | |
1668 | ||
9009cc24 PP |
1669 | env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH); |
1670 | if (env_clash && strcmp(env_clash, "0") == 0) { | |
1671 | return; | |
1672 | } | |
1673 | ||
290725f7 PP |
1674 | if (g_file_test(cfg->command_name, |
1675 | G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { | |
1676 | fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n", | |
1677 | cfg->command_name); | |
1678 | fprintf(stderr, "trace located in the local `%s` directory, please use:\n", | |
1679 | cfg->command_name); | |
1680 | fprintf(stderr, "\n"); | |
1681 | fprintf(stderr, " babeltrace convert %s [OPTIONS]\n", | |
1682 | cfg->command_name); | |
1683 | } | |
1684 | } | |
1685 | ||
7213a328 PP |
1686 | static |
1687 | void init_log_level(void) | |
1688 | { | |
1689 | enum bt_logging_level log_level = BT_LOG_NONE; | |
1690 | const char *log_level_env = getenv("BABELTRACE_CLI_LOG_LEVEL"); | |
1691 | ||
1692 | if (!log_level_env) { | |
1693 | goto set_level; | |
1694 | } | |
1695 | ||
1696 | if (strcmp(log_level_env, "VERBOSE") == 0) { | |
1697 | log_level = BT_LOGGING_LEVEL_VERBOSE; | |
1698 | } else if (strcmp(log_level_env, "DEBUG") == 0) { | |
1699 | log_level = BT_LOGGING_LEVEL_DEBUG; | |
1700 | } else if (strcmp(log_level_env, "INFO") == 0) { | |
1701 | log_level = BT_LOGGING_LEVEL_INFO; | |
1702 | } else if (strcmp(log_level_env, "WARN") == 0) { | |
1703 | log_level = BT_LOGGING_LEVEL_WARN; | |
1704 | } else if (strcmp(log_level_env, "ERROR") == 0) { | |
1705 | log_level = BT_LOGGING_LEVEL_ERROR; | |
1706 | } else if (strcmp(log_level_env, "FATAL") == 0) { | |
1707 | log_level = BT_LOGGING_LEVEL_FATAL; | |
1708 | } else if (strcmp(log_level_env, "NONE") == 0) { | |
1709 | log_level = BT_LOGGING_LEVEL_NONE; | |
1710 | } | |
1711 | ||
1712 | set_level: | |
1713 | bt_cli_log_level = log_level; | |
1714 | } | |
1715 | ||
5401f780 PP |
1716 | void set_sigint_handler(void) |
1717 | { | |
1718 | struct sigaction new_action, old_action; | |
1719 | ||
1720 | new_action.sa_handler = sigint_handler; | |
1721 | sigemptyset(&new_action.sa_mask); | |
1722 | new_action.sa_flags = 0; | |
1723 | sigaction(SIGINT, NULL, &old_action); | |
1724 | ||
1725 | if (old_action.sa_handler != SIG_IGN) { | |
1726 | sigaction(SIGINT, &new_action, NULL); | |
1727 | } | |
1728 | } | |
1729 | ||
290725f7 PP |
1730 | int main(int argc, const char **argv) |
1731 | { | |
1732 | int ret; | |
1733 | int retcode; | |
1734 | struct bt_config *cfg; | |
1735 | ||
7213a328 | 1736 | init_log_level(); |
5401f780 | 1737 | set_sigint_handler(); |
9009cc24 PP |
1738 | init_static_data(); |
1739 | cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode); | |
290725f7 PP |
1740 | |
1741 | if (retcode < 0) { | |
1742 | /* Quit without errors; typically usage/version */ | |
1743 | retcode = 0; | |
7213a328 | 1744 | BT_LOGI_STR("Quitting without errors."); |
290725f7 PP |
1745 | goto end; |
1746 | } | |
1747 | ||
1748 | if (retcode > 0) { | |
7213a328 | 1749 | BT_LOGE("Command-line error: retcode=%d", retcode); |
290725f7 PP |
1750 | goto end; |
1751 | } | |
1752 | ||
1753 | if (!cfg) { | |
7213a328 | 1754 | BT_LOGE_STR("Failed to create a valid Babeltrace configuration."); |
290725f7 | 1755 | fprintf(stderr, "Failed to create Babeltrace configuration\n"); |
db0f160a | 1756 | retcode = 1; |
290725f7 PP |
1757 | goto end; |
1758 | } | |
1759 | ||
7213a328 PP |
1760 | if (cfg->verbose) { |
1761 | bt_cli_log_level = BT_LOGGING_LEVEL_VERBOSE; | |
1762 | bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); | |
1763 | // TODO: for backward compat., set the log level | |
1764 | // environment variables of the known plugins | |
1765 | // to VERBOSE | |
1766 | } else if (cfg->debug) { | |
1767 | bt_cli_log_level = BT_LOGGING_LEVEL_DEBUG; | |
1768 | bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG); | |
1769 | // TODO: for backward compat., set the log level | |
1770 | // environment variables of the known plugins | |
1771 | // to DEBUG | |
1772 | } | |
1773 | ||
290725f7 PP |
1774 | babeltrace_debug = cfg->debug; |
1775 | babeltrace_verbose = cfg->verbose; | |
1776 | print_cfg(cfg); | |
1777 | ||
db0f160a PP |
1778 | if (cfg->command_needs_plugins) { |
1779 | ret = load_all_plugins(cfg->plugin_paths); | |
1780 | if (ret) { | |
7213a328 | 1781 | BT_LOGE("Failed to load plugins: ret=%d", ret); |
db0f160a PP |
1782 | retcode = 1; |
1783 | goto end; | |
1784 | } | |
1785 | } | |
1786 | ||
7213a328 PP |
1787 | BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"", |
1788 | cfg->command, cfg->command_name); | |
1789 | ||
290725f7 | 1790 | switch (cfg->command) { |
db0f160a PP |
1791 | case BT_CONFIG_COMMAND_RUN: |
1792 | ret = cmd_run(cfg); | |
290725f7 PP |
1793 | break; |
1794 | case BT_CONFIG_COMMAND_LIST_PLUGINS: | |
1795 | ret = cmd_list_plugins(cfg); | |
1796 | break; | |
22e22462 PP |
1797 | case BT_CONFIG_COMMAND_HELP: |
1798 | ret = cmd_help(cfg); | |
1799 | break; | |
a67681c1 PP |
1800 | case BT_CONFIG_COMMAND_QUERY: |
1801 | ret = cmd_query(cfg); | |
63ce0e1d | 1802 | break; |
db0f160a PP |
1803 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
1804 | ret = cmd_print_ctf_metadata(cfg); | |
1805 | break; | |
1806 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: | |
1807 | ret = cmd_print_lttng_live_sessions(cfg); | |
1808 | break; | |
290725f7 | 1809 | default: |
7213a328 | 1810 | BT_LOGF("Invalid command: cmd=%d", cfg->command); |
290725f7 PP |
1811 | assert(false); |
1812 | } | |
1813 | ||
1814 | warn_command_name_and_directory_clash(cfg); | |
1815 | retcode = ret ? 1 : 0; | |
1816 | ||
1817 | end: | |
1818 | BT_PUT(cfg); | |
9009cc24 | 1819 | fini_static_data(); |
290725f7 | 1820 | return retcode; |
4c8bfb7e | 1821 | } |