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