2 * Copyright (c) 2019 EfficiOS Inc. and Linux Foundation
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "CLI-CFG-SRC-AUTO-DISC"
26 #include "babeltrace2-cfg-src-auto-disc.h"
27 #include "babeltrace2-plugins.h"
28 #include "babeltrace2-query.h"
29 #include "common/common.h"
31 /* Finalize and free a `struct auto_source_discovery_result`. */
34 void auto_source_discovery_result_destroy(struct auto_source_discovery_result
*res
)
38 bt_value_put_ref(res
->inputs
);
43 /* Allocate and initialize a `struct auto_source_discovery_result`. */
46 struct auto_source_discovery_result
*auto_source_discovery_result_create(
47 const char *plugin_name
, const char *source_cc_name
,
50 struct auto_source_discovery_result
*res
;
52 res
= g_new0(struct auto_source_discovery_result
, 1);
54 BT_CLI_LOGE_APPEND_CAUSE(
55 "Failed to allocate a auto_source_discovery_result structure.");
59 res
->plugin_name
= plugin_name
;
60 res
->source_cc_name
= source_cc_name
;
61 res
->group
= g_strdup(group
);
62 if (group
&& !res
->group
) {
63 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a string.");
67 res
->inputs
= bt_value_array_create();
69 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate an array value.");
75 auto_source_discovery_result_destroy(res
);
81 /* Finalize a `struct auto_source_discovery`. */
83 void auto_source_discovery_fini(struct auto_source_discovery
*auto_disc
)
85 if (auto_disc
->results
) {
86 g_ptr_array_free(auto_disc
->results
, TRUE
);
90 /* Initialize an already allocated `struct auto_source_discovery`. */
92 int auto_source_discovery_init(struct auto_source_discovery
*auto_disc
)
96 auto_disc
->results
= g_ptr_array_new_with_free_func(
97 (GDestroyNotify
) auto_source_discovery_result_destroy
);
99 if (!auto_disc
->results
) {
107 auto_source_discovery_fini(auto_disc
);
116 * Assign `input` to source component class `source_cc_name` of plugin
117 * `plugin_name`, in the group with key `group`.
121 int auto_source_discovery_add(struct auto_source_discovery
*auto_disc
,
122 const char *plugin_name
,
123 const char *source_cc_name
,
128 bt_value_array_append_element_status append_status
;
131 struct auto_source_discovery_result
*res
= NULL
;
133 len
= auto_disc
->results
->len
;
137 for (i
= 0; i
< len
; i
++) {
138 res
= g_ptr_array_index(auto_disc
->results
, i
);
140 if (strcmp(res
->plugin_name
, plugin_name
) != 0) {
144 if (strcmp(res
->source_cc_name
, source_cc_name
) != 0) {
148 if (g_strcmp0(res
->group
, group
) != 0) {
157 /* Add a new result entry. */
158 res
= auto_source_discovery_result_create(plugin_name
,
159 source_cc_name
, group
);
164 g_ptr_array_add(auto_disc
->results
, res
);
167 append_status
= bt_value_array_append_string_element(res
->inputs
, input
);
168 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
169 BT_CLI_LOGE_APPEND_CAUSE("Failed to append a string value.");
185 int convert_weight_value(const bt_value
*weight_value
, double *weight
,
186 const char *plugin_name
, const char *source_cc_name
,
187 const char *input
, const char *input_type
)
189 enum bt_value_type weight_value_type
;
192 weight_value_type
= bt_value_get_type(weight_value
);
194 if (weight_value_type
== BT_VALUE_TYPE_REAL
) {
195 *weight
= bt_value_real_get(weight_value
);
196 } else if (weight_value_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
197 /* Accept signed integer as a convenience for "return 0" or "return 1" in Python. */
198 *weight
= bt_value_integer_signed_get(weight_value
);
200 BT_LOGW("babeltrace.support-info query: unexpected type for weight: "
201 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
202 "expected-entry-type=%s, actual-entry-type=%s",
203 plugin_name
, source_cc_name
, input
, input_type
,
204 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
205 bt_common_value_type_string(bt_value_get_type(weight_value
)));
209 if (*weight
< 0.0 || *weight
> 1.0) {
210 BT_LOGW("babeltrace.support-info query: weight value is out of range [0.0, 1.0]: "
211 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
213 plugin_name
, source_cc_name
, input
, input_type
, *weight
);
228 * Query all known source components to see if any of them can handle `input`
229 * as the given `type`(arbitrary string, directory or file).
231 * If `plugin_restrict` is non-NULL, only query source component classes provided
232 * by the plugin with that name.
234 * If `component_class_restrict` is non-NULL, only query source component classes
239 * - > 0 on success, if no source component class has reported that it handles `input`
240 * - 0 on success, if a source component class has reported that it handles `input`
241 * - < 0 on failure (e.g. memory error)
244 int support_info_query_all_sources(const char *input
,
245 const char *input_type
, size_t plugin_count
,
246 const char *plugin_restrict
,
247 const char *component_class_restrict
,
248 enum bt_logging_level log_level
,
249 struct auto_source_discovery
*auto_disc
)
251 bt_value_map_insert_entry_status insert_status
;
252 bt_value
*query_params
= NULL
;
255 const struct bt_value
*query_result
= NULL
;
257 const bt_component_class_source
*source
;
258 const bt_plugin
*plugin
;
259 const bt_value
*group
;
261 } winner
= { NULL
, NULL
, NULL
, 0 };
263 query_params
= bt_value_map_create();
265 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a map value.");
269 insert_status
= bt_value_map_insert_string_entry(query_params
, "input", input
);
270 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
271 BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
275 insert_status
= bt_value_map_insert_string_entry(query_params
, "type", input_type
);
276 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
277 BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
281 for (i_plugins
= 0; i_plugins
< plugin_count
; i_plugins
++) {
282 const bt_plugin
*plugin
;
283 const char *plugin_name
;
284 uint64_t source_count
;
287 plugin
= borrow_loaded_plugin(i_plugins
);
288 plugin_name
= bt_plugin_get_name(plugin
);
291 * If the search is restricted to a specific plugin, only consider
292 * the plugin with that name.
294 if (plugin_restrict
&& strcmp(plugin_restrict
, plugin_name
) != 0) {
298 source_count
= bt_plugin_get_source_component_class_count(plugin
);
300 for (i_sources
= 0; i_sources
< source_count
; i_sources
++) {
301 const bt_component_class_source
*source_cc
;
302 const bt_component_class
*cc
;
303 const char *source_cc_name
;
304 bt_query_executor_query_status query_status
;
306 source_cc
= bt_plugin_borrow_source_component_class_by_index_const(plugin
, i_sources
);
307 cc
= bt_component_class_source_as_component_class_const(source_cc
);
308 source_cc_name
= bt_component_class_get_name(cc
);
311 * If the search is restricted to a specific component class, only consider the
312 * component classes with that name.
314 if (component_class_restrict
&& strcmp(component_class_restrict
, source_cc_name
) != 0) {
318 BT_LOGD("babeltrace.support-info query: before: component-class-name=source.%s.%s, input=%s, "
319 "type=%s", plugin_name
, source_cc_name
, input
, input_type
);
321 BT_VALUE_PUT_REF_AND_RESET(query_result
);
322 query_status
= cli_query(cc
, "babeltrace.support-info",
323 query_params
, log_level
, NULL
, &query_result
,
326 if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_OK
) {
328 const bt_value
*group_value
= NULL
;
329 enum bt_value_type query_result_type
;
331 BT_ASSERT(query_result
);
333 query_result_type
= bt_value_get_type(query_result
);
335 if (query_result_type
== BT_VALUE_TYPE_REAL
|| query_result_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
336 if (convert_weight_value(query_result
, &weight
, plugin_name
, source_cc_name
, input
, input_type
) != 0) {
337 /* convert_weight_value has already warned. */
340 } else if (query_result_type
== BT_VALUE_TYPE_MAP
) {
341 const bt_value
*weight_value
;
343 if (!bt_value_map_has_entry(query_result
, "weight")) {
344 BT_LOGW("babeltrace.support-info query: result is missing `weight` entry: "
345 "component-class-name=source.%s.%s, input=%s, input-type=%s",
346 bt_plugin_get_name(plugin
),
347 bt_component_class_get_name(cc
), input
,
352 weight_value
= bt_value_map_borrow_entry_value_const(query_result
, "weight");
353 BT_ASSERT(weight_value
);
355 if (convert_weight_value(weight_value
, &weight
, plugin_name
, source_cc_name
, input
, input_type
) != 0) {
356 /* convert_weight_value has already warned. */
360 if (bt_value_map_has_entry(query_result
, "group")) {
361 enum bt_value_type group_value_type
;
363 group_value
= bt_value_map_borrow_entry_value_const(query_result
, "group");
364 BT_ASSERT(group_value
);
366 group_value_type
= bt_value_get_type(group_value
);
368 if (group_value_type
== BT_VALUE_TYPE_NULL
) {
369 /* Do as if no value was passed. */
371 } else if (bt_value_get_type(group_value
) != BT_VALUE_TYPE_STRING
) {
372 BT_LOGW("babeltrace.support-info query: unexpected type for entry `group`: "
373 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
374 "expected-entry-type=%s,%s, actual-entry-type=%s",
375 bt_plugin_get_name(plugin
),
376 bt_component_class_get_name(cc
), input
,
378 bt_common_value_type_string(BT_VALUE_TYPE_NULL
),
379 bt_common_value_type_string(BT_VALUE_TYPE_STRING
),
380 bt_common_value_type_string(bt_value_get_type(group_value
)));
385 BT_LOGW("babeltrace.support-info query: unexpected result type: "
386 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
387 "expected-types=%s,%s,%s, actual-type=%s",
388 bt_plugin_get_name(plugin
),
389 bt_component_class_get_name(cc
), input
,
391 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
392 bt_common_value_type_string(BT_VALUE_TYPE_MAP
),
393 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER
),
394 bt_common_value_type_string(bt_value_get_type(query_result
)));
398 BT_LOGD("babeltrace.support-info query: success: component-class-name=source.%s.%s, input=%s, "
399 "type=%s, weight=%f\n",
400 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
403 if (weight
> winner
.weigth
) {
404 winner
.source
= source_cc
;
405 winner
.plugin
= plugin
;
407 bt_value_put_ref(winner
.group
);
408 winner
.group
= group_value
;
409 bt_value_get_ref(winner
.group
);
411 winner
.weigth
= weight
;
413 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
) {
414 BT_CLI_LOGE_APPEND_CAUSE("babeltrace.support-info query failed.");
416 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
) {
417 BT_CLI_LOGE_APPEND_CAUSE("Memory error.");
420 BT_LOGD("babeltrace.support-info query: failure: component-class-name=source.%s.%s, input=%s, "
421 "type=%s, status=%s\n",
422 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
424 bt_common_func_status_string(query_status
));
430 const char *source_name
;
431 const char *plugin_name
;
434 source_name
= bt_component_class_get_name(
435 bt_component_class_source_as_component_class_const(winner
.source
));
436 plugin_name
= bt_plugin_get_name(winner
.plugin
);
437 group
= winner
.group
? bt_value_string_get(winner
.group
) : NULL
;
439 BT_LOGI("Input %s is awarded to component class source.%s.%s with weight %f",
440 input
, plugin_name
, source_name
, winner
.weigth
);
442 status
= auto_source_discovery_add(auto_disc
, plugin_name
, source_name
, group
, input
);
447 BT_LOGI("Input %s (%s) was not recognized by any source component class.",
458 bt_value_put_ref(query_result
);
459 bt_value_put_ref(query_params
);
460 bt_value_put_ref(winner
.group
);
466 * Look for a source component class that recognizes `input` as an arbitrary
469 * Same return value semantic as `support_info_query_all_sources`.
473 int auto_discover_source_for_input_as_string(const char *input
,
474 size_t plugin_count
, const char *plugin_restrict
,
475 const char *component_class_restrict
,
476 enum bt_logging_level log_level
,
477 struct auto_source_discovery
*auto_disc
)
479 return support_info_query_all_sources(input
, "string",
480 plugin_count
, plugin_restrict
, component_class_restrict
,
481 log_level
, auto_disc
);
485 int auto_discover_source_for_input_as_dir_or_file_rec(GString
*input
,
486 size_t plugin_count
, const char *plugin_restrict
,
487 const char *component_class_restrict
,
488 enum bt_logging_level log_level
,
489 struct auto_source_discovery
*auto_disc
)
492 GError
*error
= NULL
;
494 if (g_file_test(input
->str
, G_FILE_TEST_IS_REGULAR
)) {
496 status
= support_info_query_all_sources(input
->str
,
497 "file", plugin_count
,
498 plugin_restrict
, component_class_restrict
, log_level
, auto_disc
);
499 } else if (g_file_test(input
->str
, G_FILE_TEST_IS_DIR
)) {
502 gsize saved_input_len
;
505 /* It's a directory. */
506 status
= support_info_query_all_sources(input
->str
,
507 "directory", plugin_count
,
508 plugin_restrict
, component_class_restrict
, log_level
,
514 } else if (status
== 0) {
516 * A component class claimed this input as a directory,
522 dir
= g_dir_open(input
->str
, 0, &error
);
524 const char *fmt
= "Failed to open directory %s: %s";
525 BT_LOGW(fmt
, input
->str
, error
->message
);
527 if (error
->code
== G_FILE_ERROR_ACCES
) {
528 /* This is not a fatal error, we just skip it. */
532 BT_CLI_LOGE_APPEND_CAUSE(fmt
, input
->str
,
538 saved_input_len
= input
->len
;
542 dirent
= g_dir_read_name(dir
);
544 g_string_append_c_inline(input
, G_DIR_SEPARATOR
);
545 g_string_append(input
, dirent
);
547 status
= auto_discover_source_for_input_as_dir_or_file_rec(
549 plugin_restrict
, component_class_restrict
,
550 log_level
, auto_disc
);
552 g_string_truncate(input
, saved_input_len
);
557 } else if (status
== 0) {
560 } else if (errno
!= 0) {
561 BT_LOGW_ERRNO("Failed to read directory entry", ": dir=%s", input
->str
);
570 BT_LOGD("Skipping %s, not a file or directory", input
->str
);
589 * Look for a source component class that recognizes `input` as a directory or
590 * file. If `input` is a directory and is not directly recognized, recurse and
591 * apply the same logic to children nodes.
593 * Same return value semantic as `support_info_query_all_sources`.
597 int auto_discover_source_for_input_as_dir_or_file(const char *input
,
598 size_t plugin_count
, const char *plugin_restrict
,
599 const char *component_class_restrict
,
600 enum bt_logging_level log_level
,
601 struct auto_source_discovery
*auto_disc
)
603 GString
*mutable_input
;
606 mutable_input
= g_string_new(input
);
607 if (!mutable_input
) {
612 status
= auto_discover_source_for_input_as_dir_or_file_rec(
613 mutable_input
, plugin_count
, plugin_restrict
,
614 component_class_restrict
, log_level
, auto_disc
);
616 g_string_free(mutable_input
, TRUE
);
621 int auto_discover_source_components(
622 const bt_value
*plugin_paths
,
623 const bt_value
*inputs
,
624 const char *plugin_restrict
,
625 const char *component_class_restrict
,
626 enum bt_logging_level log_level
,
627 struct auto_source_discovery
*auto_disc
)
629 uint64_t i_inputs
, input_count
;
633 input_count
= bt_value_array_get_size(inputs
);
635 status
= require_loaded_plugins(plugin_paths
);
640 plugin_count
= get_loaded_plugins_count();
642 for (i_inputs
= 0; i_inputs
< input_count
; i_inputs
++) {
643 const bt_value
*input_value
;
646 input_value
= bt_value_array_borrow_element_by_index_const(inputs
, i_inputs
);
647 input
= bt_value_string_get(input_value
);
648 status
= auto_discover_source_for_input_as_string(input
,
649 plugin_count
, plugin_restrict
, component_class_restrict
,
650 log_level
, auto_disc
);
654 } else if (status
== 0) {
655 /* A component class has claimed this input as an arbitrary string. */
659 status
= auto_discover_source_for_input_as_dir_or_file(input
,
660 plugin_count
, plugin_restrict
, component_class_restrict
,
661 log_level
, auto_disc
);
665 } else if (status
== 0) {
667 * This input (or something under it) was recognized.
672 BT_LOGW("No trace was found based on input `%s`.", input
);