| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <autodisc/autodisc.h> |
| 8 | #include <common/common.h> |
| 9 | |
| 10 | /* |
| 11 | * Interface between the Python bindings and the auto source discovery system: |
| 12 | * input strings go in, specs for components to instantiate go out. |
| 13 | * |
| 14 | * `inputs` must be an array of strings, the list of inputs in which to look |
| 15 | * for traces. `plugin_set` is the set of plugins to query. |
| 16 | * |
| 17 | * Returns a map with the following entries: |
| 18 | * |
| 19 | * - status: signed integer, return status of this function |
| 20 | * - results: array, each element is an array describing one auto |
| 21 | * source discovery result (see `struct |
| 22 | * auto_source_discovery_result` for more details about these): |
| 23 | * |
| 24 | * - 0: plugin name, string |
| 25 | * - 1: class name, string |
| 26 | * - 2: inputs, array of strings |
| 27 | * - 3: original input indices, array of unsigned integers |
| 28 | * |
| 29 | * This function can also return None, if it failed to allocate memory |
| 30 | * for the return value and status code. |
| 31 | */ |
| 32 | static |
| 33 | bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs, |
| 34 | const bt_plugin_set *plugin_set) |
| 35 | { |
| 36 | uint64_t i; |
| 37 | int status = 0; |
| 38 | static const char * const module_name = "Automatic source discovery"; |
| 39 | const bt_plugin **plugins = NULL; |
| 40 | const uint64_t plugin_count = bt_plugin_set_get_plugin_count(plugin_set); |
| 41 | struct auto_source_discovery auto_disc = { 0 }; |
| 42 | bt_value *result = NULL; |
| 43 | bt_value *components_list = NULL; |
| 44 | bt_value *component_info = NULL; |
| 45 | bt_value_map_insert_entry_status insert_entry_status; |
| 46 | const bt_error *error = NULL; |
| 47 | |
| 48 | BT_ASSERT(bt_value_get_type(inputs) == BT_VALUE_TYPE_ARRAY); |
| 49 | for (i = 0; i < bt_value_array_get_length(inputs); i++) { |
| 50 | const bt_value *elem = bt_value_array_borrow_element_by_index_const(inputs, i); |
| 51 | BT_ASSERT(bt_value_get_type(elem) == BT_VALUE_TYPE_STRING); |
| 52 | } |
| 53 | |
| 54 | result = bt_value_map_create(); |
| 55 | if (!result) { |
| 56 | #define BT_FMT "Failed to create a map value." |
| 57 | BT_LOGE_STR(BT_FMT); |
| 58 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, BT_FMT); |
| 59 | #undef BT_FMT |
| 60 | PyErr_NoMemory(); |
| 61 | goto end; |
| 62 | } |
| 63 | |
| 64 | status = auto_source_discovery_init(&auto_disc); |
| 65 | if (status != 0) { |
| 66 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 67 | "Failed to initialize auto source discovery structure."); |
| 68 | goto error; |
| 69 | } |
| 70 | |
| 71 | /* Create array of bt_plugin pointers from plugin set. */ |
| 72 | plugins = g_new(const bt_plugin *, plugin_count); |
| 73 | if (!plugins) { |
| 74 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 75 | "Failed to allocate plugins array."); |
| 76 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | for (i = 0; i < plugin_count; i++) { |
| 81 | plugins[i] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); |
| 82 | } |
| 83 | |
| 84 | status = auto_discover_source_components( |
| 85 | inputs, |
| 86 | plugins, |
| 87 | plugin_count, |
| 88 | NULL, |
| 89 | (bt_logging_level) bt_python_bindings_bt2_log_level, |
| 90 | &auto_disc, |
| 91 | NULL); |
| 92 | if (status != 0) { |
| 93 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 94 | "Failed to auto discover sources."); |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | components_list = bt_value_array_create(); |
| 99 | if (!components_list) { |
| 100 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 101 | "Failed to allocate one array value."); |
| 102 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | insert_entry_status = bt_value_map_insert_entry(result, "results", components_list); |
| 107 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 108 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 109 | "Failed to insert a map entry."); |
| 110 | status = (int) insert_entry_status; |
| 111 | goto error; |
| 112 | } |
| 113 | |
| 114 | for (i = 0; i < auto_disc.results->len; i++) { |
| 115 | struct auto_source_discovery_result *autodisc_result = |
| 116 | g_ptr_array_index(auto_disc.results, i); |
| 117 | bt_value_array_append_element_status append_element_status; |
| 118 | |
| 119 | component_info = bt_value_array_create(); |
| 120 | if (!component_info) { |
| 121 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 122 | "Failed to allocate one array value."); |
| 123 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | append_element_status = bt_value_array_append_string_element( |
| 128 | component_info, autodisc_result->plugin_name); |
| 129 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 130 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 131 | "Failed to append one array element."); |
| 132 | status = (int) append_element_status; |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | append_element_status = bt_value_array_append_string_element( |
| 137 | component_info, autodisc_result->source_cc_name); |
| 138 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 139 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 140 | "Failed to append one array element."); |
| 141 | status = (int) append_element_status; |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | append_element_status = bt_value_array_append_element( |
| 146 | component_info, autodisc_result->inputs); |
| 147 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 148 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 149 | "Failed to append one array element."); |
| 150 | status = (int) append_element_status; |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | append_element_status = bt_value_array_append_element( |
| 155 | component_info, autodisc_result->original_input_indices); |
| 156 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 157 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 158 | "Failed to append one array element."); |
| 159 | status = (int) append_element_status; |
| 160 | goto error; |
| 161 | } |
| 162 | |
| 163 | append_element_status = bt_value_array_append_element(components_list, |
| 164 | component_info); |
| 165 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 166 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 167 | "Failed to append one array element."); |
| 168 | status = (int) append_element_status; |
| 169 | goto error; |
| 170 | } |
| 171 | |
| 172 | BT_VALUE_PUT_REF_AND_RESET(component_info); |
| 173 | } |
| 174 | |
| 175 | status = 0; |
| 176 | goto end; |
| 177 | error: |
| 178 | BT_ASSERT(status != 0); |
| 179 | |
| 180 | end: |
| 181 | if (result) { |
| 182 | /* |
| 183 | * If an error happened, we must clear the error temporarily |
| 184 | * while we insert the status in the map. |
| 185 | */ |
| 186 | error = bt_current_thread_take_error(); |
| 187 | insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status); |
| 188 | if (insert_entry_status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 189 | if (error) { |
| 190 | bt_current_thread_move_error(error); |
| 191 | error = NULL; |
| 192 | } |
| 193 | } else { |
| 194 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 195 | PyErr_NoMemory(); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | } |
| 200 | |
| 201 | auto_source_discovery_fini(&auto_disc); |
| 202 | g_free(plugins); |
| 203 | bt_value_put_ref(components_list); |
| 204 | bt_value_put_ref(component_info); |
| 205 | |
| 206 | if (error) { |
| 207 | bt_error_release(error); |
| 208 | } |
| 209 | |
| 210 | return result; |
| 211 | } |