2 * The MIT License (MIT)
4 * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include <autodisc/autodisc.h>
26 #include <common/common.h>
29 * Interface between the Python bindings and the auto source discovery system:
30 * input strings go in, specs for components to instantiate go out.
32 * `inputs` must be an array of strings, the list of inputs in which to look
33 * for traces. `plugin_set` is the set of plugins to query.
35 * Returns a map with the following entries:
37 * - status: signed integer, return status of this function
38 * - results: array, each element is an array describing one auto
39 * source discovery result (see `struct
40 * auto_source_discovery_result` for more details about these):
42 * - 0: plugin name, string
43 * - 1: class name, string
44 * - 2: inputs, array of strings
45 * - 3: original input indices, array of unsigned integers
47 * This function can also return None, if it failed to allocate memory
48 * for the return value and status code.
50 bt_value
*bt_bt2_auto_discover_source_components(const bt_value
*inputs
,
51 const bt_plugin_set
*plugin_set
)
55 static const char * const module_name
= "Automatic source discovery";
56 const bt_plugin
**plugins
= NULL
;
57 const uint64_t plugin_count
= bt_plugin_set_get_plugin_count(plugin_set
);
58 struct auto_source_discovery auto_disc
= { 0 };
59 bt_value
*result
= NULL
;
60 bt_value
*components_list
= NULL
;
61 bt_value
*component_info
= NULL
;
62 bt_value_map_insert_entry_status insert_entry_status
;
64 BT_ASSERT(bt_value_get_type(inputs
) == BT_VALUE_TYPE_ARRAY
);
65 for (i
= 0; i
< bt_value_array_get_length(inputs
); i
++) {
66 const bt_value
*elem
= bt_value_array_borrow_element_by_index_const(inputs
, i
);
67 BT_ASSERT(bt_value_get_type(elem
) == BT_VALUE_TYPE_STRING
);
70 result
= bt_value_map_create();
72 static const char * const err
= "Failed to create a map value.";
74 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
, err
);
79 status
= auto_source_discovery_init(&auto_disc
);
81 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
82 "Failed to initialize auto source discovery structure.");
86 /* Create array of bt_plugin pointers from plugin set. */
87 plugins
= g_new(const bt_plugin
*, plugin_count
);
89 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
90 "Failed to allocate plugins array.");
91 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
95 for (i
= 0; i
< plugin_count
; i
++) {
96 plugins
[i
] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
99 status
= auto_discover_source_components(
104 bt_python_bindings_bt2_log_level
,
108 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
109 "Failed to auto discover sources.");
113 components_list
= bt_value_array_create();
114 if (!components_list
) {
115 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
116 "Failed to allocate one array value.");
117 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
121 insert_entry_status
= bt_value_map_insert_entry(result
, "results", components_list
);
122 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
123 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
124 "Failed to insert a map entry.");
125 status
= (int) insert_entry_status
;
129 for (i
= 0; i
< auto_disc
.results
->len
; i
++) {
130 struct auto_source_discovery_result
*result
=
131 g_ptr_array_index(auto_disc
.results
, i
);
132 bt_value_array_append_element_status append_element_status
;
134 component_info
= bt_value_array_create();
135 if (!component_info
) {
136 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
137 "Failed to allocate one array value.");
138 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
142 append_element_status
= bt_value_array_append_string_element(
143 component_info
, result
->plugin_name
);
144 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
145 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
146 "Failed to append one array element.");
147 status
= (int) append_element_status
;
151 append_element_status
= bt_value_array_append_string_element(
152 component_info
, result
->source_cc_name
);
153 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
154 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
155 "Failed to append one array element.");
156 status
= (int) append_element_status
;
160 append_element_status
= bt_value_array_append_element(
161 component_info
, result
->inputs
);
162 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
163 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
164 "Failed to append one array element.");
165 status
= (int) append_element_status
;
169 append_element_status
= bt_value_array_append_element(
170 component_info
, result
->original_input_indices
);
171 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
172 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
173 "Failed to append one array element.");
174 status
= (int) append_element_status
;
178 append_element_status
= bt_value_array_append_element(components_list
,
180 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
181 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
182 "Failed to append one array element.");
183 status
= (int) append_element_status
;
187 BT_VALUE_PUT_REF_AND_RESET(component_info
);
193 BT_ASSERT(status
!= 0);
197 insert_entry_status
= bt_value_map_insert_signed_integer_entry(result
, "status", status
);
198 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
199 BT_VALUE_PUT_REF_AND_RESET(result
);
204 auto_source_discovery_fini(&auto_disc
);
206 bt_value_put_ref(components_list
);
207 bt_value_put_ref(component_info
);