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