| 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); |
| 65 | for (i = 0; i < bt_value_array_get_size(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); |
| 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, |
| 105 | &auto_disc); |
| 106 | if (status != 0) { |
| 107 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 108 | "Failed to auto discover sources."); |
| 109 | goto error; |
| 110 | } |
| 111 | |
| 112 | components_list = bt_value_array_create(); |
| 113 | if (!components_list) { |
| 114 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 115 | "Failed to allocate one array value."); |
| 116 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | insert_entry_status = bt_value_map_insert_entry(result, "results", components_list); |
| 121 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 122 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 123 | "Failed to insert a map entry."); |
| 124 | status = (int) insert_entry_status; |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | for (i = 0; i < auto_disc.results->len; i++) { |
| 129 | struct auto_source_discovery_result *result = |
| 130 | g_ptr_array_index(auto_disc.results, i); |
| 131 | bt_value_array_append_element_status append_element_status; |
| 132 | |
| 133 | component_info = bt_value_array_create(); |
| 134 | if (!component_info) { |
| 135 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 136 | "Failed to allocate one array value."); |
| 137 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 138 | goto error; |
| 139 | } |
| 140 | |
| 141 | append_element_status = bt_value_array_append_string_element( |
| 142 | component_info, result->plugin_name); |
| 143 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 144 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 145 | "Failed to append one array element."); |
| 146 | status = (int) append_element_status; |
| 147 | goto error; |
| 148 | } |
| 149 | |
| 150 | append_element_status = bt_value_array_append_string_element( |
| 151 | component_info, result->source_cc_name); |
| 152 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 153 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 154 | "Failed to append one array element."); |
| 155 | status = (int) append_element_status; |
| 156 | goto error; |
| 157 | } |
| 158 | |
| 159 | append_element_status = bt_value_array_append_element( |
| 160 | component_info, result->inputs); |
| 161 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 162 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 163 | "Failed to append one array element."); |
| 164 | status = (int) append_element_status; |
| 165 | goto error; |
| 166 | } |
| 167 | |
| 168 | append_element_status = bt_value_array_append_element( |
| 169 | component_info, result->original_input_indices); |
| 170 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 171 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 172 | "Failed to append one array element."); |
| 173 | status = (int) append_element_status; |
| 174 | goto error; |
| 175 | } |
| 176 | |
| 177 | append_element_status = bt_value_array_append_element(components_list, |
| 178 | component_info); |
| 179 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 180 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 181 | "Failed to append one array element."); |
| 182 | status = (int) append_element_status; |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | BT_VALUE_PUT_REF_AND_RESET(component_info); |
| 187 | } |
| 188 | |
| 189 | status = 0; |
| 190 | goto end; |
| 191 | error: |
| 192 | BT_ASSERT(status != 0); |
| 193 | |
| 194 | end: |
| 195 | if (result) { |
| 196 | insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status); |
| 197 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 198 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 199 | PyErr_NoMemory(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | auto_source_discovery_fini(&auto_disc); |
| 204 | g_free(plugins); |
| 205 | bt_value_put_ref(components_list); |
| 206 | bt_value_put_ref(component_info); |
| 207 | |
| 208 | return result; |
| 209 | } |