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