X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fplugin%2Fplugin-so.c;h=44aa967ed603b18820456e263468004178c319c1;hb=bfa9a4be6ef22434c7ce0a5953e7daef7e39d2fb;hp=77c29c13b6e8104c8c30978b13dfe7a68ca107b0;hpb=0d8b4d8edb6b11ddd7f4e6adc77b9390a0db0b20;p=babeltrace.git diff --git a/lib/plugin/plugin-so.c b/lib/plugin/plugin-so.c index 77c29c13..44aa967e 100644 --- a/lib/plugin/plugin-so.c +++ b/lib/plugin/plugin-so.c @@ -27,6 +27,9 @@ * SOFTWARE. */ +#define BT_LOG_TAG "PLUGIN-SO" +#include + #include #include #include @@ -34,12 +37,14 @@ #include #include #include +#include +#include #include -#include +#include #include #include -#define NATIVE_PLUGIN_SUFFIX ".so" +#define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) #define LIBTOOL_PLUGIN_SUFFIX ".la" #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX) @@ -61,59 +66,49 @@ DECLARE_SECTION(struct __bt_plugin_component_class_descriptor const *, __bt_plug DECLARE_SECTION(struct __bt_plugin_component_class_descriptor_attribute const *, __bt_plugin_component_class_descriptor_attributes); /* - * This hash table, global to the library, maps component class pointers - * to shared library handles. - * - * The keys (component classes) are NOT owned by this hash table, whereas - * the values (shared library handles) are owned by this hash table. + * This list, global to the library, keeps all component classes that + * have a reference to their shared library handles. It allows iteration + * on all component classes still present when the destructor executes + * to release the shared library handle references they might still have. * - * The keys are the component classes created with + * The list items are the component classes created with * bt_plugin_add_component_class(). They keep the shared library handle * object created by their plugin alive so that the plugin's code is * not discarded when it could still be in use by living components * created from those component classes: * - * [component] --ref-> [component class] --through this HT-> [shlib handle] - * - * This hash table exists for two reasons: + * [component] --ref-> [component class]-> [shlib handle] * - * 1. To allow this application: + * It allows this use-case: * * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so"); * // instantiate components from a plugin's component classes * // put plugins and free my_plugins here * // user code of instantiated components still exists * - * 2. To decouple the plugin subsystem from the component subsystem: - * while plugins objects need to know component class objects, the - * opposite is not necessary, thus it makes no sense for a component - * class to keep a reference to the plugin object from which it was - * created. - * - * An entry is removed from this HT when a component class is destroyed - * thanks to a custom destroy listener. When the entry is removed, the - * GLib function calls the value destroy notifier of the HT, which is - * bt_put(). This decreases the reference count of the mapped shared - * library handle. Assuming the original plugin object which contained - * some component classes is put first, when the last component class is - * removed from this HT, the shared library handle object's reference - * count falls to zero and the shared library is finally closed. + * An entry is removed from this list when a component class is + * destroyed thanks to a custom destroy listener. When the entry is + * removed, the entry is removed from the list, and we release the + * reference on the shlib handle. Assuming the original plugin object + * which contained some component classes is put first, when the last + * component class is removed from this list, the shared library handle + * object's reference count falls to zero and the shared library is + * finally closed. */ -static -GHashTable *comp_classes_to_shlib_handles; -__attribute__((constructor)) static -void init_comp_classes_to_shlib_handles(void) { - comp_classes_to_shlib_handles = g_hash_table_new_full(g_direct_hash, - g_direct_equal, NULL, bt_put); - assert(comp_classes_to_shlib_handles); -} +static +BT_LIST_HEAD(component_class_list); __attribute__((destructor)) static -void fini_comp_classes_to_shlib_handles(void) { - if (comp_classes_to_shlib_handles) { - g_hash_table_destroy(comp_classes_to_shlib_handles); +void fini_comp_class_list(void) +{ + struct bt_component_class *comp_class, *tmp; + + bt_list_for_each_entry_safe(comp_class, tmp, &component_class_list, node) { + bt_list_del(&comp_class->node); + BT_PUT(comp_class->so_handle); } + BT_LOGD_STR("Released references from all component classes to shared library handles."); } static @@ -124,24 +119,50 @@ void bt_plugin_so_shared_lib_handle_destroy(struct bt_object *obj) assert(obj); shared_lib_handle = container_of(obj, struct bt_plugin_so_shared_lib_handle, base); + const char *path = shared_lib_handle->path ? + shared_lib_handle->path->str : NULL; + + BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"", + shared_lib_handle, path); if (shared_lib_handle->init_called && shared_lib_handle->exit) { - enum bt_plugin_status status = shared_lib_handle->exit(); + enum bt_plugin_status status; - if (status < 0) { - const char *path = shared_lib_handle->path ? - shared_lib_handle->path->str : "[built-in]"; + BT_LOGD_STR("Calling user's plugin exit function."); + status = shared_lib_handle->exit(); + BT_LOGD("User function returned: %s", + bt_plugin_status_string(status)); - printf_verbose("Plugin in module `%s` exited with error %d\n", - path, status); + if (status < 0) { + BT_LOGW("User's plugin exit function failed: " + "path=\"%s\"", path); } } if (shared_lib_handle->module) { - if (!g_module_close(shared_lib_handle->module)) { - printf_error("Module close error: %s\n", - g_module_error()); +#ifndef NDEBUG + /* + * Valgrind shows incomplete stack traces when + * dynamically loaded libraries are closed before it + * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug + * build to avoid this. + */ + const char *var = getenv("BABELTRACE_NO_DLCLOSE"); + + if (!var || strcmp(var, "1") != 0) { +#endif + BT_LOGD("Closing GModule: path=\"%s\"", path); + + if (!g_module_close(shared_lib_handle->module)) { + BT_LOGE("Cannot close GModule: %s: path=\"%s\"", + g_module_error(), path); + } +#ifndef NDEBUG + } else { + BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: " + "path=\"%s\"", path); } +#endif } if (shared_lib_handle->path) { @@ -157,8 +178,10 @@ struct bt_plugin_so_shared_lib_handle *bt_plugin_so_shared_lib_handle_create( { struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; + BT_LOGD("Creating shared library handle: path=\"%s\"", path); shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1); if (!shared_lib_handle) { + BT_LOGE_STR("Failed to allocate one shared library handle."); goto error; } @@ -170,12 +193,21 @@ struct bt_plugin_so_shared_lib_handle *bt_plugin_so_shared_lib_handle_create( shared_lib_handle->path = g_string_new(path); if (!shared_lib_handle->path) { + BT_LOGE_STR("Failed to allocate a GString."); goto error; } shared_lib_handle->module = g_module_open(path, 0); if (!shared_lib_handle->module) { - printf_verbose("Module open error: %s\n", g_module_error()); + /* + * DEBUG-level logging because we're only _trying_ to + * open this file as a Babeltrace plugin: if it's not, + * it's not an error. And because this can be tried + * during bt_plugin_create_all_from_dir(), it's not even + * a warning. + */ + BT_LOGD("Cannot open GModule: %s: path=\"%s\"", + g_module_error(), path); goto error; } @@ -185,6 +217,11 @@ error: BT_PUT(shared_lib_handle); end: + if (shared_lib_handle) { + BT_LOGD("Created shared library handle: path=\"%s\", addr=%p", + path, shared_lib_handle); + } + return shared_lib_handle; } @@ -268,9 +305,21 @@ enum bt_plugin_status bt_plugin_so_init( size_t i; int ret; + BT_LOGD("Initializing plugin object from descriptors found in sections: " + "plugin-addr=%p, plugin-path=\"%s\", " + "attrs-begin-addr=%p, attrs-end-addr=%p, " + "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, " + "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p", + plugin, + spec->shared_lib_handle->path ? + spec->shared_lib_handle->path->str : NULL, + attrs_begin, attrs_end, + cc_descriptors_begin, cc_descriptors_end, + cc_descr_attrs_begin, cc_descr_attrs_end); comp_class_full_descriptors = g_array_new(FALSE, TRUE, sizeof(struct comp_class_full_descriptor)); if (!comp_class_full_descriptors) { + BT_LOGE_STR("Failed to allocate a GArray."); status = BT_PLUGIN_STATUS_ERROR; goto end; } @@ -315,9 +364,21 @@ enum bt_plugin_status bt_plugin_so_init( cur_attr->value.version.extra); break; default: - printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for plugin %s\n", - cur_attr->type_name, cur_attr->type, - descriptor->name); + /* + * WARN-level logging because this should not + * happen with the appropriate ABI version. If + * we're here, we know that for the reported + * version of the ABI, this attribute is + * unknown. + */ + BT_LOGW("Ignoring unknown plugin descriptor attribute: " + "plugin-path=\"%s\", plugin-name=\"%s\", " + "attr-type-name=\"%s\", attr-type-id=%d", + spec->shared_lib_handle->path ? + spec->shared_lib_handle->path->str : + NULL, + descriptor->name, cur_attr->type_name, + cur_attr->type); break; } } @@ -409,12 +470,31 @@ enum bt_plugin_status bt_plugin_so_init( cur_cc_descr_attr->value.notif_iter_seek_time_method; break; default: - printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for component class %s (type %d) in plugin %s\n", - cur_cc_descr_attr->type_name, - cur_cc_descr_attr->type, + /* + * WARN-level logging because + * this should not happen with + * the appropriate ABI version. + * If we're here, we know that + * for the reported version of + * the ABI, this attribute is + * unknown. + */ + BT_LOGW("Ignoring unknown component class descriptor attribute: " + "plugin-path=\"%s\", " + "plugin-name=\"%s\", " + "comp-class-name=\"%s\", " + "comp-class-type=%s, " + "attr-type-name=\"%s\", " + "attr-type-id=%d", + spec->shared_lib_handle->path ? + spec->shared_lib_handle->path->str : + NULL, + descriptor->name, cur_cc_descr_attr->comp_class_descriptor->name, - cur_cc_descr_attr->comp_class_descriptor->type, - descriptor->name); + bt_component_class_type_string( + cur_cc_descr_attr->comp_class_descriptor->type), + cur_cc_descr_attr->type_name, + cur_cc_descr_attr->type); break; } } @@ -423,15 +503,18 @@ enum bt_plugin_status bt_plugin_so_init( /* Initialize plugin */ if (spec->init) { + BT_LOGD_STR("Calling user's plugin initialization function."); status = spec->init(plugin); + BT_LOGD("User function returned: %s", + bt_plugin_status_string(status)); + if (status < 0) { - printf_verbose("Plugin `%s` initialization error: %d\n", - bt_plugin_get_name(plugin), status); + BT_LOGW_STR("User's plugin initialization function failed."); goto end; } } - spec->shared_lib_handle->init_called = true; + spec->shared_lib_handle->init_called = BT_TRUE; /* Add described component classes to plugin */ for (i = 0; i < comp_class_full_descriptors->len; i++) { @@ -440,6 +523,17 @@ enum bt_plugin_status bt_plugin_so_init( struct comp_class_full_descriptor, i); struct bt_component_class *comp_class; + BT_LOGD("Creating and setting properties of plugin's component class: " + "plugin-path=\"%s\", plugin-name=\"%s\", " + "comp-class-name=\"%s\", comp-class-type=%s", + spec->shared_lib_handle->path ? + spec->shared_lib_handle->path->str : + NULL, + descriptor->name, + cc_full_descr->descriptor->name, + bt_component_class_type_string( + cc_full_descr->descriptor->type)); + switch (cc_full_descr->descriptor->type) { case BT_COMPONENT_CLASS_TYPE_SOURCE: comp_class = bt_component_class_source_create( @@ -457,14 +551,27 @@ enum bt_plugin_status bt_plugin_so_init( cc_full_descr->descriptor->methods.sink.consume); break; default: - printf_verbose("WARNING: Unknown component class type %d for component class %s in plugin %s\n", - cc_full_descr->descriptor->type, + /* + * WARN-level logging because this should not + * happen with the appropriate ABI version. If + * we're here, we know that for the reported + * version of the ABI, this component class type + * is unknown. + */ + BT_LOGW("Ignoring unknown component class type: " + "plugin-path=\"%s\", plugin-name=\"%s\", " + "comp-class-name=\"%s\", comp-class-type=%d", + spec->shared_lib_handle->path->str ? + spec->shared_lib_handle->path->str : + NULL, + descriptor->name, cc_full_descr->descriptor->name, - descriptor->name); + cc_full_descr->descriptor->type); continue; } if (!comp_class) { + BT_LOGE_STR("Cannot create component class."); status = BT_PLUGIN_STATUS_ERROR; goto end; } @@ -473,6 +580,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_description(comp_class, cc_full_descr->description); if (ret) { + BT_LOGE_STR("Cannot set component class's description."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -483,6 +591,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_help(comp_class, cc_full_descr->help); if (ret) { + BT_LOGE_STR("Cannot set component class's help string."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -493,6 +602,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_init_method(comp_class, cc_full_descr->init_method); if (ret) { + BT_LOGE_STR("Cannot set component class's initialization method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -503,6 +613,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_finalize_method(comp_class, cc_full_descr->finalize_method); if (ret) { + BT_LOGE_STR("Cannot set component class's finalization method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -513,6 +624,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_query_method( comp_class, cc_full_descr->query_method); if (ret) { + BT_LOGE_STR("Cannot set component class's query method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -523,6 +635,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_accept_port_connection_method( comp_class, cc_full_descr->accept_port_connection_method); if (ret) { + BT_LOGE_STR("Cannot set component class's \"accept port connection\" method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -533,6 +646,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_port_connected_method( comp_class, cc_full_descr->port_connected_method); if (ret) { + BT_LOGE_STR("Cannot set component class's \"port connected\" method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -543,6 +657,7 @@ enum bt_plugin_status bt_plugin_so_init( ret = bt_component_class_set_port_disconnected_method( comp_class, cc_full_descr->port_disconnected_method); if (ret) { + BT_LOGE_STR("Cannot set component class's \"port disconnected\" method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -556,6 +671,7 @@ enum bt_plugin_status bt_plugin_so_init( comp_class, cc_full_descr->iterator_methods.init); if (ret) { + BT_LOGE_STR("Cannot set component class's notification iterator initialization method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -567,6 +683,7 @@ enum bt_plugin_status bt_plugin_so_init( comp_class, cc_full_descr->iterator_methods.finalize); if (ret) { + BT_LOGE_STR("Cannot set source component class's notification iterator finalization method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -578,6 +695,7 @@ enum bt_plugin_status bt_plugin_so_init( comp_class, cc_full_descr->iterator_methods.seek_time); if (ret) { + BT_LOGE_STR("Cannot set source component class's notification iterator seek to time method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -590,6 +708,7 @@ enum bt_plugin_status bt_plugin_so_init( comp_class, cc_full_descr->iterator_methods.init); if (ret) { + BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -601,6 +720,7 @@ enum bt_plugin_status bt_plugin_so_init( comp_class, cc_full_descr->iterator_methods.finalize); if (ret) { + BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -612,6 +732,7 @@ enum bt_plugin_status bt_plugin_so_init( comp_class, cc_full_descr->iterator_methods.seek_time); if (ret) { + BT_LOGE_STR("Cannot set filter component class's notification iterator seek to time method."); status = BT_PLUGIN_STATUS_ERROR; BT_PUT(comp_class); goto end; @@ -621,8 +742,7 @@ enum bt_plugin_status bt_plugin_so_init( case BT_COMPONENT_CLASS_TYPE_SINK: break; default: - assert(false); - break; + abort(); } /* @@ -630,17 +750,14 @@ enum bt_plugin_status bt_plugin_so_init( * * This will call back * bt_plugin_so_on_add_component_class() so that we can - * add a mapping in comp_classes_to_shlib_handles when + * add a mapping in the component class list when * we know the component class is successfully added. */ status = bt_plugin_add_component_class(plugin, comp_class); BT_PUT(comp_class); if (status < 0) { - printf_verbose("Cannot add component class %s (type %d) to plugin `%s`: status = %d\n", - cc_full_descr->descriptor->name, - cc_full_descr->descriptor->type, - bt_plugin_get_name(plugin), status); + BT_LOGE("Cannot add component class to plugin."); goto end; } } @@ -673,6 +790,7 @@ struct bt_plugin *bt_plugin_so_create_empty( plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data; plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1); if (!plugin->spec_data) { + BT_LOGE_STR("Failed to allocate one SO plugin specific data structure."); goto error; } @@ -688,7 +806,7 @@ end: } static -struct bt_plugin **bt_plugin_so_create_all_from_sections( +struct bt_plugin_set *bt_plugin_so_create_all_from_sections( struct bt_plugin_so_shared_lib_handle *shared_lib_handle, struct __bt_plugin_descriptor const * const *descriptors_begin, struct __bt_plugin_descriptor const * const *descriptors_end, @@ -704,22 +822,31 @@ struct bt_plugin **bt_plugin_so_create_all_from_sections( size_t cc_descriptors_count; size_t cc_descr_attrs_count; size_t i; - struct bt_plugin **plugins = NULL; + struct bt_plugin_set *plugin_set = NULL; descriptor_count = descriptors_end - descriptors_begin; attrs_count = attrs_end - attrs_begin; cc_descriptors_count = cc_descriptors_end - cc_descriptors_begin; cc_descr_attrs_count = cc_descr_attrs_end - cc_descr_attrs_begin; - printf_verbose("Section: Plugin descriptors: [%p - %p], (%zu elements)\n", - descriptors_begin, descriptors_end, descriptor_count); - printf_verbose("Section: Plugin descriptor attributes: [%p - %p], (%zu elements)\n", - attrs_begin, attrs_end, attrs_count); - printf_verbose("Section: Plugin component class descriptors: [%p - %p], (%zu elements)\n", - cc_descriptors_begin, cc_descriptors_end, cc_descriptors_count); - printf_verbose("Section: Plugin component class descriptor attributes: [%p - %p], (%zu elements)\n", - cc_descr_attrs_begin, cc_descr_attrs_end, cc_descr_attrs_count); - plugins = calloc(descriptor_count + 1, sizeof(*plugins)); - if (!plugins) { + + BT_LOGD("Creating all SO plugins from sections: " + "plugin-path=\"%s\", " + "descr-begin-addr=%p, descr-end-addr=%p, " + "attrs-begin-addr=%p, attrs-end-addr=%p, " + "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, " + "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, " + "descr-count=%zu, attrs-count=%zu, " + "cc-descr-count=%zu, cc-descr-attrs-count=%zu", + shared_lib_handle->path ? shared_lib_handle->path->str : NULL, + descriptors_begin, descriptors_end, + attrs_begin, attrs_end, + cc_descriptors_begin, cc_descriptors_end, + cc_descr_attrs_begin, cc_descr_attrs_end, + descriptor_count, attrs_count, + cc_descriptors_count, cc_descr_attrs_count); + plugin_set = bt_plugin_set_create(); + if (!plugin_set) { + BT_LOGE_STR("Cannot create empty plugin set."); goto error; } @@ -729,19 +856,27 @@ struct bt_plugin **bt_plugin_so_create_all_from_sections( descriptors_begin[i]; struct bt_plugin *plugin; - printf_verbose("Loading plugin %s (ABI %d.%d)\n", descriptor->name, - descriptor->major, descriptor->minor); + BT_LOGD("Creating plugin object for plugin: " + "name=\"%s\", abi-major=%d, abi-minor=%d", + descriptor->name, descriptor->major, descriptor->minor); if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) { - printf_error("Unknown plugin's major version: %d\n", + /* + * DEBUG-level logging because we're only + * _trying_ to open this file as a compatible + * Babeltrace plugin: if it's not, it's not an + * error. And because this can be tried during + * bt_plugin_create_all_from_dir(), it's not + * even a warning. + */ + BT_LOGD("Unknown ABI major version: abi-major=%d", descriptor->major); goto error; } plugin = bt_plugin_so_create_empty(shared_lib_handle); if (!plugin) { - printf_error("Cannot allocate plugin object for plugin %s\n", - descriptor->name); + BT_LOGE_STR("Cannot create empty shared library handle."); goto error; } @@ -753,30 +888,37 @@ struct bt_plugin **bt_plugin_so_create_all_from_sections( attrs_end, cc_descriptors_begin, cc_descriptors_end, cc_descr_attrs_begin, cc_descr_attrs_end); if (status < 0) { - printf_error("Cannot initialize plugin object %s\n", - descriptor->name); + /* + * DEBUG-level logging because we're only + * _trying_ to open this file as a compatible + * Babeltrace plugin: if it's not, it's not an + * error. And because this can be tried during + * bt_plugin_create_all_from_dir(), it's not + * even a warning. + */ + BT_LOGD_STR("Cannot initialize SO plugin object from sections."); BT_PUT(plugin); goto error; } - /* Transfer ownership to the array */ - plugins[i] = plugin; + /* Add to plugin set */ + bt_plugin_set_add_plugin(plugin_set, plugin); + bt_put(plugin); } goto end; error: - g_free(plugins); - plugins = NULL; + BT_PUT(plugin_set); end: - return plugins; + return plugin_set; } BT_HIDDEN -struct bt_plugin **bt_plugin_so_create_all_from_static(void) +struct bt_plugin_set *bt_plugin_so_create_all_from_static(void) { - struct bt_plugin **plugins = NULL; + struct bt_plugin_set *plugin_set = NULL; struct bt_plugin_so_shared_lib_handle *shared_lib_handle = bt_plugin_so_shared_lib_handle_create(NULL); @@ -784,7 +926,8 @@ struct bt_plugin **bt_plugin_so_create_all_from_static(void) goto end; } - plugins = bt_plugin_so_create_all_from_sections(shared_lib_handle, + BT_LOGD_STR("Creating all SO plugins from built-in plugins."); + plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle, SECTION_BEGIN(__bt_plugin_descriptors), SECTION_END(__bt_plugin_descriptors), SECTION_BEGIN(__bt_plugin_descriptor_attributes), @@ -797,14 +940,14 @@ struct bt_plugin **bt_plugin_so_create_all_from_static(void) end: BT_PUT(shared_lib_handle); - return plugins; + return plugin_set; } BT_HIDDEN -struct bt_plugin **bt_plugin_so_create_all_from_file(const char *path) +struct bt_plugin_set *bt_plugin_so_create_all_from_file(const char *path) { size_t path_len; - struct bt_plugin **plugins = NULL; + struct bt_plugin_set *plugin_set = NULL; struct __bt_plugin_descriptor const * const *descriptors_begin = NULL; struct __bt_plugin_descriptor const * const *descriptors_end = NULL; struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL; @@ -813,15 +956,19 @@ struct bt_plugin **bt_plugin_so_create_all_from_file(const char *path) struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL; struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL; struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL; - bool is_libtool_wrapper = false, is_shared_object = false; + bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE; struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; if (!path) { + BT_LOGW_STR("Invalid parameter: path is NULL."); goto end; } + BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path); path_len = strlen(path); if (path_len <= PLUGIN_SUFFIX_LEN) { + BT_LOGW("Invalid parameter: path length is too short: " + "path-length=%zu", path_len); goto end; } @@ -837,138 +984,143 @@ struct bt_plugin **bt_plugin_so_create_all_from_file(const char *path) path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, NATIVE_PLUGIN_SUFFIX_LEN); if (!is_shared_object && !is_libtool_wrapper) { - /* Name indicates that this is not a plugin file. */ + /* Name indicates this is not a plugin file; not an error */ + BT_LOGV("File is not a SO plugin file: path=\"%s\"", path); goto end; } shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path); if (!shared_lib_handle) { + BT_LOGD_STR("Cannot create shared library handle."); goto end; } if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptors", (gpointer *) &descriptors_begin)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__start___bt_plugin_descriptors", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__start___bt_plugin_descriptors"); goto end; } if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptors", (gpointer *) &descriptors_end)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__stop___bt_plugin_descriptors", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__stop___bt_plugin_descriptors"); goto end; } if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptor_attributes", (gpointer *) &attrs_begin)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__start___bt_plugin_descriptor_attributes", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__start___bt_plugin_descriptor_attributes"); } if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptor_attributes", (gpointer *) &attrs_end)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__stop___bt_plugin_descriptor_attributes", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__stop___bt_plugin_descriptor_attributes"); } if ((!!attrs_begin - !!attrs_end) != 0) { - printf_verbose("Found __start___bt_plugin_descriptor_attributes or __stop___bt_plugin_descriptor_attributes symbol, but not both in %s\n", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Found section start or end symbol, but not both: " + "path=\"%s\", symbol-start=\"%s\", " + "symbol-end=\"%s\", symbol-start-addr=%p, " + "symbol-end-addr=%p", + path, "__start___bt_plugin_descriptor_attributes", + "__stop___bt_plugin_descriptor_attributes", + attrs_begin, attrs_end); goto end; } if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptors", (gpointer *) &cc_descriptors_begin)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__start___bt_plugin_component_class_descriptors", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__start___bt_plugin_component_class_descriptors"); } if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptors", (gpointer *) &cc_descriptors_end)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__stop___bt_plugin_component_class_descriptors", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__stop___bt_plugin_component_class_descriptors"); } if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) { - printf_verbose("Found __start___bt_plugin_component_class_descriptors or __stop___bt_plugin_component_class_descriptors symbol, but not both in %s\n", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Found section start or end symbol, but not both: " + "path=\"%s\", symbol-start=\"%s\", " + "symbol-end=\"%s\", symbol-start-addr=%p, " + "symbol-end-addr=%p", + path, "__start___bt_plugin_component_class_descriptors", + "__stop___bt_plugin_component_class_descriptors", + cc_descriptors_begin, cc_descriptors_end); goto end; } if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptor_attributes", (gpointer *) &cc_descr_attrs_begin)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__start___bt_plugin_component_class_descriptor_attributes", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__start___bt_plugin_component_class_descriptor_attributes"); } if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptor_attributes", (gpointer *) &cc_descr_attrs_end)) { - printf_verbose("Unable to resolve plugin symbol %s from %s\n", - "__stop___bt_plugin_component_class_descriptor_attributes", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " + "symbol=\"%s\"", path, + "__stop___bt_plugin_component_class_descriptor_attributes"); } if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) { - printf_verbose("Found __start___bt_plugin_component_class_descriptor_attributes or __stop___bt_plugin_component_class_descriptor_attributes symbol, but not both in %s\n", - g_module_name(shared_lib_handle->module)); + BT_LOGD("Found section start or end symbol, but not both: " + "path=\"%s\", symbol-start=\"%s\", " + "symbol-end=\"%s\", symbol-start-addr=%p, " + "symbol-end-addr=%p", + path, "__start___bt_plugin_component_class_descriptor_attributes", + "__stop___bt_plugin_component_class_descriptor_attributes", + cc_descr_attrs_begin, cc_descr_attrs_end); goto end; } /* Initialize plugin */ - plugins = bt_plugin_so_create_all_from_sections(shared_lib_handle, + BT_LOGD_STR("Initializing plugin object."); + plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle, descriptors_begin, descriptors_end, attrs_begin, attrs_end, cc_descriptors_begin, cc_descriptors_end, cc_descr_attrs_begin, cc_descr_attrs_end); end: BT_PUT(shared_lib_handle); - return plugins; + return plugin_set; } static void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, void *data) { - gboolean exists = g_hash_table_remove(comp_classes_to_shlib_handles, - comp_class); - assert(exists); + bt_list_del(&comp_class->node); + BT_PUT(comp_class->so_handle); + BT_LOGV("Component class destroyed: removed entry from list: " + "comp-cls-addr=%p", comp_class); } BT_HIDDEN -int bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, +void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, struct bt_component_class *comp_class) { - int ret; struct bt_plugin_so_spec_data *spec = plugin->spec_data; assert(plugin->spec_data); assert(plugin->type == BT_PLUGIN_TYPE_SO); - /* Map component class pointer to shared lib handle in global HT */ - g_hash_table_insert(comp_classes_to_shlib_handles, comp_class, - bt_get(spec->shared_lib_handle)); + bt_list_add(&comp_class->node, &component_class_list); + comp_class->so_handle = bt_get(spec->shared_lib_handle); /* Add our custom destroy listener */ - ret = bt_component_class_add_destroy_listener(comp_class, + bt_component_class_add_destroy_listener(comp_class, plugin_comp_class_destroy_listener, NULL); - if (ret) { - goto error; - } - goto end; - -error: - /* Remove entry from global hash table (if exists) */ - g_hash_table_remove(comp_classes_to_shlib_handles, - comp_class); - -end: - return ret; }