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"
24 #define BT_LOG_OUTPUT_LEVEL log_level
25 #include "logging/log.h"
28 #include "common/common.h"
30 #define BT_AUTODISC_LOG_AND_APPEND(_lvl, _fmt, ...) \
32 BT_LOG_WRITE(_lvl, BT_LOG_TAG, _fmt, ##__VA_ARGS__); \
33 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( \
34 "Source auto-discovery", _fmt, ##__VA_ARGS__); \
37 #define BT_AUTODISC_LOGE_APPEND_CAUSE(_fmt, ...) \
38 BT_AUTODISC_LOG_AND_APPEND(BT_LOG_ERROR, _fmt, ##__VA_ARGS__)
40 /* Finalize and free a `struct auto_source_discovery_result`. */
43 void auto_source_discovery_result_destroy(struct auto_source_discovery_result
*res
)
47 bt_value_put_ref(res
->inputs
);
48 bt_value_put_ref(res
->original_input_indices
);
53 /* Allocate and initialize a `struct auto_source_discovery_result`. */
56 struct auto_source_discovery_result
*auto_source_discovery_result_create(
57 const char *plugin_name
, const char *source_cc_name
,
58 const char *group
, bt_logging_level log_level
)
60 struct auto_source_discovery_result
*res
;
62 res
= g_new0(struct auto_source_discovery_result
, 1);
64 BT_AUTODISC_LOGE_APPEND_CAUSE(
65 "Failed to allocate a auto_source_discovery_result structure.");
69 res
->plugin_name
= plugin_name
;
70 res
->source_cc_name
= source_cc_name
;
71 res
->group
= g_strdup(group
);
72 if (group
&& !res
->group
) {
73 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to allocate a string.");
77 res
->inputs
= bt_value_array_create();
79 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to allocate an array value.");
83 res
->original_input_indices
= bt_value_array_create();
84 if (!res
->original_input_indices
) {
85 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to allocate an array value.");
91 auto_source_discovery_result_destroy(res
);
97 /* Finalize a `struct auto_source_discovery`. */
99 void auto_source_discovery_fini(struct auto_source_discovery
*auto_disc
)
101 if (auto_disc
->results
) {
102 g_ptr_array_free(auto_disc
->results
, TRUE
);
106 /* Initialize an already allocated `struct auto_source_discovery`. */
108 int auto_source_discovery_init(struct auto_source_discovery
*auto_disc
)
112 auto_disc
->results
= g_ptr_array_new_with_free_func(
113 (GDestroyNotify
) auto_source_discovery_result_destroy
);
115 if (!auto_disc
->results
) {
123 auto_source_discovery_fini(auto_disc
);
132 const bt_value
*borrow_array_value_last_element_const(const bt_value
*array
)
134 uint64_t last_index
= bt_value_array_get_length(array
) - 1;
136 return bt_value_array_borrow_element_by_index_const(array
, last_index
);
140 * Assign `input` to source component class `source_cc_name` of plugin
141 * `plugin_name`, in the group with key `group`.
145 int auto_source_discovery_add(struct auto_source_discovery
*auto_disc
,
146 const char *plugin_name
,
147 const char *source_cc_name
,
150 uint64_t original_input_index
,
151 bt_logging_level log_level
)
154 bt_value_array_append_element_status append_status
;
157 struct auto_source_discovery_result
*res
= NULL
;
160 len
= auto_disc
->results
->len
;
164 for (i
= 0; i
< len
; i
++) {
165 res
= g_ptr_array_index(auto_disc
->results
, i
);
167 if (strcmp(res
->plugin_name
, plugin_name
) != 0) {
171 if (strcmp(res
->source_cc_name
, source_cc_name
) != 0) {
175 if (g_strcmp0(res
->group
, group
) != 0) {
184 /* Add a new result entry. */
185 res
= auto_source_discovery_result_create(plugin_name
,
186 source_cc_name
, group
, log_level
);
191 g_ptr_array_add(auto_disc
->results
, res
);
194 append_status
= bt_value_array_append_string_element(res
->inputs
, input
);
195 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
196 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to append a string value.");
201 * Append `original_input_index` to `original_input_indices` if not
202 * there already. We process the `inputs` array in order, so if it is
203 * present, it has to be the last element.
205 if (bt_value_array_is_empty(res
->original_input_indices
)) {
208 const bt_value
*last_index_value
;
212 borrow_array_value_last_element_const(res
->original_input_indices
);
213 last_index
= bt_value_integer_unsigned_get(last_index_value
);
215 BT_ASSERT(last_index
<= original_input_index
);
217 append_index
= (last_index
!= original_input_index
);
221 append_status
= bt_value_array_append_unsigned_integer_element(
222 res
->original_input_indices
, original_input_index
);
224 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
225 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to append an unsigned integer value.");
241 int convert_weight_value(const bt_value
*weight_value
, double *weight
,
242 const char *plugin_name
, const char *source_cc_name
,
243 const char *input
, const char *input_type
,
244 bt_logging_level log_level
)
246 enum bt_value_type weight_value_type
;
249 weight_value_type
= bt_value_get_type(weight_value
);
251 if (weight_value_type
== BT_VALUE_TYPE_REAL
) {
252 *weight
= bt_value_real_get(weight_value
);
253 } else if (weight_value_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
254 /* Accept signed integer as a convenience for "return 0" or "return 1" in Python. */
255 *weight
= bt_value_integer_signed_get(weight_value
);
257 BT_LOGW("babeltrace.support-info query: unexpected type for weight: "
258 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
259 "expected-entry-type=%s, actual-entry-type=%s",
260 plugin_name
, source_cc_name
, input
, input_type
,
261 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
262 bt_common_value_type_string(bt_value_get_type(weight_value
)));
266 if (*weight
< 0.0 || *weight
> 1.0) {
267 BT_LOGW("babeltrace.support-info query: weight value is out of range [0.0, 1.0]: "
268 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
270 plugin_name
, source_cc_name
, input
, input_type
, *weight
);
285 bt_query_executor_query_status
simple_query(const bt_component_class
*comp_cls
,
286 const char *obj
, const bt_value
*params
,
287 bt_logging_level log_level
, const bt_value
**result
)
289 bt_query_executor_query_status status
;
290 bt_query_executor_set_logging_level_status set_logging_level_status
;
291 bt_query_executor
*query_exec
;
293 query_exec
= bt_query_executor_create(comp_cls
, obj
, params
);
295 BT_AUTODISC_LOGE_APPEND_CAUSE("Cannot create a query executor.");
296 status
= BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
;
300 set_logging_level_status
= bt_query_executor_set_logging_level(query_exec
, log_level
);
301 if (set_logging_level_status
!= BT_QUERY_EXECUTOR_SET_LOGGING_LEVEL_STATUS_OK
) {
302 BT_AUTODISC_LOGE_APPEND_CAUSE(
303 "Cannot set query executor's logging level: "
305 bt_common_logging_level_string(log_level
));
306 status
= (int) set_logging_level_status
;
310 status
= bt_query_executor_query(query_exec
, result
);
313 bt_query_executor_put_ref(query_exec
);
320 * Query all known source components to see if any of them can handle `input`
321 * as the given `type`(arbitrary string, directory or file).
323 * If `plugin_restrict` is non-NULL, only query source component classes provided
324 * by the plugin with that name.
326 * If `component_class_restrict` is non-NULL, only query source component classes
331 * - > 0 on success, if no source component class has reported that it handles `input`
332 * - 0 on success, if a source component class has reported that it handles `input`
333 * - < 0 on failure (e.g. memory error)
336 int support_info_query_all_sources(const char *input
,
337 const char *input_type
,
338 uint64_t original_input_index
,
339 const bt_plugin
**plugins
,
341 const char *component_class_restrict
,
342 enum bt_logging_level log_level
,
343 struct auto_source_discovery
*auto_disc
)
345 bt_value_map_insert_entry_status insert_status
;
346 bt_value
*query_params
= NULL
;
349 const struct bt_value
*query_result
= NULL
;
351 const bt_component_class_source
*source
;
352 const bt_plugin
*plugin
;
353 const bt_value
*group
;
355 } winner
= { NULL
, NULL
, NULL
, 0 };
357 query_params
= bt_value_map_create();
359 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to allocate a map value.");
363 insert_status
= bt_value_map_insert_string_entry(query_params
, "input", input
);
364 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
365 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
369 insert_status
= bt_value_map_insert_string_entry(query_params
, "type", input_type
);
370 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
371 BT_AUTODISC_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
375 for (i_plugins
= 0; i_plugins
< plugin_count
; i_plugins
++) {
376 const bt_plugin
*plugin
;
377 const char *plugin_name
;
378 uint64_t source_count
;
381 plugin
= plugins
[i_plugins
];
382 plugin_name
= bt_plugin_get_name(plugin
);
384 source_count
= bt_plugin_get_source_component_class_count(plugin
);
386 for (i_sources
= 0; i_sources
< source_count
; i_sources
++) {
387 const bt_component_class_source
*source_cc
;
388 const bt_component_class
*cc
;
389 const char *source_cc_name
;
390 bt_query_executor_query_status query_status
;
392 source_cc
= bt_plugin_borrow_source_component_class_by_index_const(plugin
, i_sources
);
393 cc
= bt_component_class_source_as_component_class_const(source_cc
);
394 source_cc_name
= bt_component_class_get_name(cc
);
397 * If the search is restricted to a specific component class, only consider the
398 * component classes with that name.
400 if (component_class_restrict
&& strcmp(component_class_restrict
, source_cc_name
) != 0) {
404 BT_LOGD("babeltrace.support-info query: before: component-class-name=source.%s.%s, input=%s, "
405 "type=%s", plugin_name
, source_cc_name
, input
, input_type
);
407 BT_VALUE_PUT_REF_AND_RESET(query_result
);
408 query_status
= simple_query(cc
, "babeltrace.support-info",
409 query_params
, log_level
, &query_result
);
411 if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_OK
) {
413 const bt_value
*group_value
= NULL
;
414 enum bt_value_type query_result_type
;
416 BT_ASSERT(query_result
);
418 query_result_type
= bt_value_get_type(query_result
);
420 if (query_result_type
== BT_VALUE_TYPE_REAL
|| query_result_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
421 if (convert_weight_value(query_result
, &weight
, plugin_name
, source_cc_name
, input
, input_type
, log_level
) != 0) {
422 /* convert_weight_value has already warned. */
425 } else if (query_result_type
== BT_VALUE_TYPE_MAP
) {
426 const bt_value
*weight_value
;
428 if (!bt_value_map_has_entry(query_result
, "weight")) {
429 BT_LOGW("babeltrace.support-info query: result is missing `weight` entry: "
430 "component-class-name=source.%s.%s, input=%s, input-type=%s",
431 bt_plugin_get_name(plugin
),
432 bt_component_class_get_name(cc
), input
,
437 weight_value
= bt_value_map_borrow_entry_value_const(query_result
, "weight");
438 BT_ASSERT(weight_value
);
440 if (convert_weight_value(weight_value
, &weight
, plugin_name
, source_cc_name
, input
, input_type
, log_level
) != 0) {
441 /* convert_weight_value has already warned. */
445 if (bt_value_map_has_entry(query_result
, "group")) {
446 enum bt_value_type group_value_type
;
448 group_value
= bt_value_map_borrow_entry_value_const(query_result
, "group");
449 BT_ASSERT(group_value
);
451 group_value_type
= bt_value_get_type(group_value
);
453 if (group_value_type
== BT_VALUE_TYPE_NULL
) {
454 /* Do as if no value was passed. */
456 } else if (bt_value_get_type(group_value
) != BT_VALUE_TYPE_STRING
) {
457 BT_LOGW("babeltrace.support-info query: unexpected type for entry `group`: "
458 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
459 "expected-entry-type=%s,%s, actual-entry-type=%s",
460 bt_plugin_get_name(plugin
),
461 bt_component_class_get_name(cc
), input
,
463 bt_common_value_type_string(BT_VALUE_TYPE_NULL
),
464 bt_common_value_type_string(BT_VALUE_TYPE_STRING
),
465 bt_common_value_type_string(bt_value_get_type(group_value
)));
470 BT_LOGW("babeltrace.support-info query: unexpected result type: "
471 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
472 "expected-types=%s,%s,%s, actual-type=%s",
473 bt_plugin_get_name(plugin
),
474 bt_component_class_get_name(cc
), input
,
476 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
477 bt_common_value_type_string(BT_VALUE_TYPE_MAP
),
478 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER
),
479 bt_common_value_type_string(bt_value_get_type(query_result
)));
483 BT_LOGD("babeltrace.support-info query: success: component-class-name=source.%s.%s, input=%s, "
484 "type=%s, weight=%f\n",
485 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
488 if (weight
> winner
.weigth
) {
489 winner
.source
= source_cc
;
490 winner
.plugin
= plugin
;
492 bt_value_put_ref(winner
.group
);
493 winner
.group
= group_value
;
494 bt_value_get_ref(winner
.group
);
496 winner
.weigth
= weight
;
498 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
) {
499 BT_AUTODISC_LOGE_APPEND_CAUSE("babeltrace.support-info query failed.");
501 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
) {
502 BT_AUTODISC_LOGE_APPEND_CAUSE("Memory error.");
505 BT_LOGD("babeltrace.support-info query: failure: component-class-name=source.%s.%s, input=%s, "
506 "type=%s, status=%s\n",
507 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
509 bt_common_func_status_string(query_status
));
515 const char *source_name
;
516 const char *plugin_name
;
519 source_name
= bt_component_class_get_name(
520 bt_component_class_source_as_component_class_const(winner
.source
));
521 plugin_name
= bt_plugin_get_name(winner
.plugin
);
522 group
= winner
.group
? bt_value_string_get(winner
.group
) : NULL
;
524 BT_LOGI("Input %s is awarded to component class source.%s.%s with weight %f",
525 input
, plugin_name
, source_name
, winner
.weigth
);
527 status
= auto_source_discovery_add(auto_disc
, plugin_name
,
528 source_name
, group
, input
, original_input_index
, log_level
);
533 BT_LOGI("Input %s (%s) was not recognized by any source component class.",
544 bt_value_put_ref(query_result
);
545 bt_value_put_ref(query_params
);
546 bt_value_put_ref(winner
.group
);
552 * Look for a source component class that recognizes `input` as an arbitrary
555 * Same return value semantic as `support_info_query_all_sources`.
559 int auto_discover_source_for_input_as_string(const char *input
,
560 uint64_t original_input_index
,
561 const bt_plugin
**plugins
,
563 const char *component_class_restrict
,
564 enum bt_logging_level log_level
,
565 struct auto_source_discovery
*auto_disc
)
567 return support_info_query_all_sources(input
, "string",
568 original_input_index
, plugins
, plugin_count
,
569 component_class_restrict
, log_level
, auto_disc
);
573 int auto_discover_source_for_input_as_dir_or_file_rec(GString
*input
,
574 uint64_t original_input_index
,
575 const bt_plugin
**plugins
,
577 const char *component_class_restrict
,
578 enum bt_logging_level log_level
,
579 struct auto_source_discovery
*auto_disc
)
582 GError
*error
= NULL
;
584 if (g_file_test(input
->str
, G_FILE_TEST_IS_REGULAR
)) {
586 status
= support_info_query_all_sources(input
->str
,
587 "file", original_input_index
, plugins
, plugin_count
,
588 component_class_restrict
, log_level
, auto_disc
);
589 } else if (g_file_test(input
->str
, G_FILE_TEST_IS_DIR
)) {
592 gsize saved_input_len
;
595 /* It's a directory. */
596 status
= support_info_query_all_sources(input
->str
,
597 "directory", original_input_index
, plugins
,
598 plugin_count
, component_class_restrict
, log_level
,
604 } else if (status
== 0) {
606 * A component class claimed this input as a directory,
612 dir
= g_dir_open(input
->str
, 0, &error
);
614 const char *fmt
= "Failed to open directory %s: %s";
615 BT_LOGW(fmt
, input
->str
, error
->message
);
617 if (error
->code
== G_FILE_ERROR_ACCES
) {
618 /* This is not a fatal error, we just skip it. */
622 BT_AUTODISC_LOGE_APPEND_CAUSE(fmt
, input
->str
,
628 saved_input_len
= input
->len
;
632 dirent
= g_dir_read_name(dir
);
634 g_string_append_c_inline(input
, G_DIR_SEPARATOR
);
635 g_string_append(input
, dirent
);
637 status
= auto_discover_source_for_input_as_dir_or_file_rec(
638 input
, original_input_index
, plugins
, plugin_count
,
639 component_class_restrict
, log_level
, auto_disc
);
641 g_string_truncate(input
, saved_input_len
);
646 } else if (status
== 0) {
649 } else if (errno
!= 0) {
650 BT_LOGW_ERRNO("Failed to read directory entry", ": dir=%s", input
->str
);
659 BT_LOGD("Skipping %s, not a file or directory", input
->str
);
678 * Look for a source component class that recognizes `input` as a directory or
679 * file. If `input` is a directory and is not directly recognized, recurse and
680 * apply the same logic to children nodes.
682 * Same return value semantic as `support_info_query_all_sources`.
686 int auto_discover_source_for_input_as_dir_or_file(const char *input
,
687 uint64_t original_input_index
,
688 const bt_plugin
**plugins
,
690 const char *component_class_restrict
,
691 enum bt_logging_level log_level
,
692 struct auto_source_discovery
*auto_disc
)
694 GString
*mutable_input
;
697 mutable_input
= g_string_new(input
);
698 if (!mutable_input
) {
703 status
= auto_discover_source_for_input_as_dir_or_file_rec(
704 mutable_input
, original_input_index
, plugins
, plugin_count
,
705 component_class_restrict
, log_level
, auto_disc
);
707 g_string_free(mutable_input
, TRUE
);
712 int auto_discover_source_components(
713 const bt_value
*inputs
,
714 const bt_plugin
**plugins
,
716 const char *component_class_restrict
,
717 enum bt_logging_level log_level
,
718 struct auto_source_discovery
*auto_disc
)
720 uint64_t i_inputs
, input_count
;
723 input_count
= bt_value_array_get_length(inputs
);
725 for (i_inputs
= 0; i_inputs
< input_count
; i_inputs
++) {
726 const bt_value
*input_value
;
729 input_value
= bt_value_array_borrow_element_by_index_const(inputs
, i_inputs
);
730 input
= bt_value_string_get(input_value
);
731 status
= auto_discover_source_for_input_as_string(input
, i_inputs
,
732 plugins
, plugin_count
, component_class_restrict
,
733 log_level
, auto_disc
);
737 } else if (status
== 0) {
738 /* A component class has claimed this input as an arbitrary string. */
742 status
= auto_discover_source_for_input_as_dir_or_file(input
,
743 i_inputs
, plugins
, plugin_count
,
744 component_class_restrict
, log_level
, auto_disc
);
748 } else if (status
== 0) {
750 * This input (or something under it) was recognized.
755 BT_LOGW("No trace was found based on input `%s`.", input
);