Commit | Line | Data |
---|---|---|
f3c9a159 SM |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include <autodisc/autodisc.h> | |
26 | #include <common/common.h> | |
27 | ||
28 | /* | |
29 | * Interface between the Python bindings and the auto source discovery system: | |
30 | * input strings go in, specs for components to instantiate go out. | |
31 | * | |
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. | |
34 | * | |
35 | * Returns a map with the following entries: | |
36 | * | |
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): | |
41 | * | |
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 | |
46 | * | |
47 | * This function can also return None, if it failed to allocate memory | |
48 | * for the return value and status code. | |
49 | */ | |
50 | bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs, | |
51 | const bt_plugin_set *plugin_set) | |
52 | { | |
53 | uint64_t i; | |
54 | int status = 0; | |
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; | |
63 | ||
64 | BT_ASSERT(bt_value_get_type(inputs) == BT_VALUE_TYPE_ARRAY); | |
393729a6 | 65 | for (i = 0; i < bt_value_array_get_length(inputs); i++) { |
f3c9a159 SM |
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); | |
68 | } | |
69 | ||
70 | result = bt_value_map_create(); | |
71 | if (!result) { | |
72 | static const char * const err = "Failed to create a map value."; | |
73 | BT_LOGE_STR(err); | |
74 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, err); | |
75 | PyErr_NoMemory(); | |
76 | goto end; | |
77 | } | |
78 | ||
79 | status = auto_source_discovery_init(&auto_disc); | |
80 | if (status != 0) { | |
81 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
82 | "Failed to initialize auto source discovery structure."); | |
83 | goto error; | |
84 | } | |
85 | ||
86 | /* Create array of bt_plugin pointers from plugin set. */ | |
87 | plugins = g_new(const bt_plugin *, plugin_count); | |
88 | if (!plugins) { | |
89 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
90 | "Failed to allocate plugins array."); | |
91 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
92 | goto error; | |
93 | } | |
94 | ||
95 | for (i = 0; i < plugin_count; i++) { | |
96 | plugins[i] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); | |
97 | } | |
98 | ||
99 | status = auto_discover_source_components( | |
100 | inputs, | |
101 | plugins, | |
102 | plugin_count, | |
103 | NULL, | |
104 | bt_python_bindings_bt2_log_level, | |
3dae1685 SM |
105 | &auto_disc, |
106 | NULL); | |
f3c9a159 SM |
107 | if (status != 0) { |
108 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
109 | "Failed to auto discover sources."); | |
110 | goto error; | |
111 | } | |
112 | ||
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; | |
118 | goto error; | |
119 | } | |
120 | ||
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; | |
126 | goto error; | |
127 | } | |
128 | ||
129 | for (i = 0; i < auto_disc.results->len; i++) { | |
50233236 | 130 | struct auto_source_discovery_result *autodisc_result = |
f3c9a159 SM |
131 | g_ptr_array_index(auto_disc.results, i); |
132 | bt_value_array_append_element_status append_element_status; | |
133 | ||
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; | |
139 | goto error; | |
140 | } | |
141 | ||
142 | append_element_status = bt_value_array_append_string_element( | |
50233236 | 143 | component_info, autodisc_result->plugin_name); |
f3c9a159 SM |
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; | |
148 | goto error; | |
149 | } | |
150 | ||
151 | append_element_status = bt_value_array_append_string_element( | |
50233236 | 152 | component_info, autodisc_result->source_cc_name); |
f3c9a159 SM |
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; | |
157 | goto error; | |
158 | } | |
159 | ||
160 | append_element_status = bt_value_array_append_element( | |
50233236 | 161 | component_info, autodisc_result->inputs); |
f3c9a159 SM |
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; | |
166 | goto error; | |
167 | } | |
168 | ||
169 | append_element_status = bt_value_array_append_element( | |
50233236 | 170 | component_info, autodisc_result->original_input_indices); |
f3c9a159 SM |
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; | |
175 | goto error; | |
176 | } | |
177 | ||
178 | append_element_status = bt_value_array_append_element(components_list, | |
179 | component_info); | |
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; | |
184 | goto error; | |
185 | } | |
186 | ||
187 | BT_VALUE_PUT_REF_AND_RESET(component_info); | |
188 | } | |
189 | ||
190 | status = 0; | |
191 | goto end; | |
192 | error: | |
193 | BT_ASSERT(status != 0); | |
194 | ||
195 | end: | |
196 | if (result) { | |
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); | |
200 | PyErr_NoMemory(); | |
201 | } | |
202 | } | |
203 | ||
204 | auto_source_discovery_fini(&auto_disc); | |
205 | g_free(plugins); | |
206 | bt_value_put_ref(components_list); | |
207 | bt_value_put_ref(component_info); | |
208 | ||
209 | return result; | |
210 | } |