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.
51 bt_value
*bt_bt2_auto_discover_source_components(const bt_value
*inputs
,
52 const bt_plugin_set
*plugin_set
)
56 static const char * const module_name
= "Automatic source discovery";
57 const bt_plugin
**plugins
= NULL
;
58 const uint64_t plugin_count
= bt_plugin_set_get_plugin_count(plugin_set
);
59 struct auto_source_discovery auto_disc
= { 0 };
60 bt_value
*result
= NULL
;
61 bt_value
*components_list
= NULL
;
62 bt_value
*component_info
= NULL
;
63 bt_value_map_insert_entry_status insert_entry_status
;
65 BT_ASSERT(bt_value_get_type(inputs
) == BT_VALUE_TYPE_ARRAY
);
66 for (i
= 0; i
< bt_value_array_get_length(inputs
); i
++) {
67 const bt_value
*elem
= bt_value_array_borrow_element_by_index_const(inputs
, i
);
68 BT_ASSERT(bt_value_get_type(elem
) == BT_VALUE_TYPE_STRING
);
71 result
= bt_value_map_create();
73 static const char * const err
= "Failed to create a map value.";
75 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
, err
);
80 status
= auto_source_discovery_init(&auto_disc
);
82 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
83 "Failed to initialize auto source discovery structure.");
87 /* Create array of bt_plugin pointers from plugin set. */
88 plugins
= g_new(const bt_plugin
*, plugin_count
);
90 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
91 "Failed to allocate plugins array.");
92 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
96 for (i
= 0; i
< plugin_count
; i
++) {
97 plugins
[i
] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
100 status
= auto_discover_source_components(
105 bt_python_bindings_bt2_log_level
,
109 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
110 "Failed to auto discover sources.");
114 components_list
= bt_value_array_create();
115 if (!components_list
) {
116 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
117 "Failed to allocate one array value.");
118 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
122 insert_entry_status
= bt_value_map_insert_entry(result
, "results", components_list
);
123 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
124 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
125 "Failed to insert a map entry.");
126 status
= (int) insert_entry_status
;
130 for (i
= 0; i
< auto_disc
.results
->len
; i
++) {
131 struct auto_source_discovery_result
*autodisc_result
=
132 g_ptr_array_index(auto_disc
.results
, i
);
133 bt_value_array_append_element_status append_element_status
;
135 component_info
= bt_value_array_create();
136 if (!component_info
) {
137 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
138 "Failed to allocate one array value.");
139 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
143 append_element_status
= bt_value_array_append_string_element(
144 component_info
, autodisc_result
->plugin_name
);
145 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
146 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
147 "Failed to append one array element.");
148 status
= (int) append_element_status
;
152 append_element_status
= bt_value_array_append_string_element(
153 component_info
, autodisc_result
->source_cc_name
);
154 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
155 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
156 "Failed to append one array element.");
157 status
= (int) append_element_status
;
161 append_element_status
= bt_value_array_append_element(
162 component_info
, autodisc_result
->inputs
);
163 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
164 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
165 "Failed to append one array element.");
166 status
= (int) append_element_status
;
170 append_element_status
= bt_value_array_append_element(
171 component_info
, autodisc_result
->original_input_indices
);
172 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
173 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
174 "Failed to append one array element.");
175 status
= (int) append_element_status
;
179 append_element_status
= bt_value_array_append_element(components_list
,
181 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
182 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
183 "Failed to append one array element.");
184 status
= (int) append_element_status
;
188 BT_VALUE_PUT_REF_AND_RESET(component_info
);
194 BT_ASSERT(status
!= 0);
198 insert_entry_status
= bt_value_map_insert_signed_integer_entry(result
, "status", status
);
199 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
200 BT_VALUE_PUT_REF_AND_RESET(result
);
205 auto_source_discovery_fini(&auto_disc
);
207 bt_value_put_ref(components_list
);
208 bt_value_put_ref(component_info
);