Add internal BT_ASSERT() and BT_ASSERT_PRE() helpers
[babeltrace.git] / lib / plugin / plugin.c
CommitLineData
33b34c43
PP
1/*
2 * plugin.c
3 *
55bb57e0 4 * Babeltrace Plugin (generic)
33b34c43
PP
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
8c1a3187
PP
30#define BT_LOG_TAG "PLUGIN"
31#include <babeltrace/lib-logging-internal.h>
32
6fbd4105 33#include <babeltrace/babeltrace-internal.h>
3d9990ac 34#include <babeltrace/compiler-internal.h>
33b34c43 35#include <babeltrace/ref.h>
1670bffd 36#include <babeltrace/common-internal.h>
33b34c43 37#include <babeltrace/plugin/plugin-internal.h>
55bb57e0 38#include <babeltrace/plugin/plugin-so-internal.h>
8c1a3187
PP
39#include <babeltrace/graph/component-class.h>
40#include <babeltrace/graph/component-class-internal.h>
c55a9f58 41#include <babeltrace/types.h>
33b34c43 42#include <glib.h>
33b34c43
PP
43#include <unistd.h>
44#include <stdlib.h>
9ac68eb1 45#include <stdint.h>
8c1a3187 46#include <inttypes.h>
33b34c43 47#include <sys/stat.h>
e1f4c4f7
MJ
48#include <ftw.h>
49#include <pthread.h>
33b34c43 50
6fbd4105
PP
51#define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
52#define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
53#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
33b34c43 54
e1f4c4f7
MJ
55#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
56
6fbd4105
PP
57#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
58#include <babeltrace/plugin/python-plugin-provider-internal.h>
e1f4c4f7 59
33b34c43 60static
a8ff38ef 61struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
6fbd4105 62 bt_plugin_python_create_all_from_file;
95ef44ce
MJ
63
64static
65void init_python_plugin_provider(void) {}
6fbd4105
PP
66#else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
67static GModule *python_plugin_provider_module;
68static
a8ff38ef 69struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path);
33b34c43 70
95ef44ce 71static
6fbd4105 72void init_python_plugin_provider(void) {
95ef44ce
MJ
73 if (bt_plugin_python_create_all_from_file_sym != NULL) {
74 return;
75 }
76
8c1a3187 77 BT_LOGD_STR("Loading Python plugin provider module.");
6fbd4105 78 python_plugin_provider_module =
c92fb666 79 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0);
6fbd4105 80 if (!python_plugin_provider_module) {
a77aed14
PP
81 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
82 PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error());
6fbd4105 83 return;
33b34c43
PP
84 }
85
6fbd4105
PP
86 if (!g_module_symbol(python_plugin_provider_module,
87 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
88 (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
8c1a3187
PP
89 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
90 "file=\"%s\", symbol=\"%s\"",
91 PYTHON_PLUGIN_PROVIDER_FILENAME,
92 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
93 return;
33b34c43 94 }
8c1a3187 95
9e0bf9b0 96 BT_LOGI("Loaded Python plugin provider module: addr=%p",
8c1a3187 97 python_plugin_provider_module);
33b34c43
PP
98}
99
6fbd4105
PP
100__attribute__((destructor)) static
101void fini_python_plugin_provider(void) {
102 if (python_plugin_provider_module) {
8c1a3187
PP
103 BT_LOGD("Unloading Python plugin provider module.");
104
105 if (!g_module_close(python_plugin_provider_module)) {
106 BT_LOGE("Failed to close the Python plugin provider module: %s.",
107 g_module_error());
108 }
109
6fbd4105 110 python_plugin_provider_module = NULL;
6ba0b073 111 }
6ba0b073 112}
6fbd4105 113#endif
6ba0b073 114
a8ff38ef 115extern
544d0515 116int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
a8ff38ef 117{
544d0515 118 int64_t count = -1;
a8ff38ef
PP
119
120 if (!plugin_set) {
8c1a3187 121 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
a8ff38ef
PP
122 goto end;
123 }
124
9ac68eb1 125 count = (int64_t) plugin_set->plugins->len;
a8ff38ef
PP
126
127end:
128 return count;
129}
130
131extern
132struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set,
9ac68eb1 133 uint64_t index)
a8ff38ef
PP
134{
135 struct bt_plugin *plugin = NULL;
136
8c1a3187
PP
137 if (!plugin_set) {
138 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
139 goto end;
140 }
141
142 if (index >= plugin_set->plugins->len) {
143 BT_LOGW("Invalid parameter: index is out of bounds: "
144 "addr=%p, index=%" PRIu64 ", count=%u",
145 plugin_set, index, plugin_set->plugins->len);
a8ff38ef
PP
146 goto end;
147 }
148
149 plugin = bt_get(g_ptr_array_index(plugin_set->plugins, index));
150
151end:
152 return plugin;
153}
154
155struct bt_plugin_set *bt_plugin_create_all_from_static(void)
6ba0b073 156{
8c1a3187 157 /* bt_plugin_so_create_all_from_static() logs errors */
55bb57e0 158 return bt_plugin_so_create_all_from_static();
6ba0b073
PP
159}
160
a8ff38ef 161struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
33b34c43 162{
a8ff38ef 163 struct bt_plugin_set *plugin_set = NULL;
33b34c43
PP
164
165 if (!path) {
8c1a3187 166 BT_LOGW_STR("Invalid parameter: path is NULL.");
6ba0b073 167 goto end;
33b34c43
PP
168 }
169
8c1a3187 170 BT_LOGD("Creating plugins from file: path=\"%s\"", path);
6ba0b073 171
55bb57e0 172 /* Try shared object plugins */
a8ff38ef
PP
173 plugin_set = bt_plugin_so_create_all_from_file(path);
174 if (plugin_set) {
6ba0b073
PP
175 goto end;
176 }
177
6fbd4105 178 /* Try Python plugins if support is available */
95ef44ce 179 init_python_plugin_provider();
6fbd4105 180 if (bt_plugin_python_create_all_from_file_sym) {
a8ff38ef
PP
181 plugin_set = bt_plugin_python_create_all_from_file_sym(path);
182 if (plugin_set) {
6fbd4105
PP
183 goto end;
184 }
6ba0b073
PP
185 }
186
33b34c43 187end:
8c1a3187
PP
188 if (plugin_set) {
189 BT_LOGD("Created %u plugins from file: "
190 "path=\"%s\", count=%u, plugin-set-addr=%p",
191 plugin_set->plugins->len, path,
192 plugin_set->plugins->len, plugin_set);
193 } else {
194 BT_LOGD("Found no plugins in file: path=\"%s\"", path);
195 }
196
a8ff38ef 197 return plugin_set;
33b34c43
PP
198}
199
1670bffd
PP
200static void destroy_gstring(void *data)
201{
202 g_string_free(data, TRUE);
203}
204
2b43acf9 205struct bt_plugin *bt_plugin_find(const char *plugin_name)
1670bffd
PP
206{
207 const char *system_plugin_dir;
208 char *home_plugin_dir = NULL;
209 const char *envvar;
210 struct bt_plugin *plugin = NULL;
a8ff38ef 211 struct bt_plugin_set *plugin_set = NULL;
1670bffd
PP
212 GPtrArray *dirs = NULL;
213 int ret;
a8ff38ef 214 size_t i, j;
1670bffd
PP
215
216 if (!plugin_name) {
8c1a3187 217 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
1670bffd
PP
218 goto end;
219 }
220
8c1a3187
PP
221 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
222 "name=\"%s\"", plugin_name);
1670bffd
PP
223 dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
224 if (!dirs) {
8c1a3187 225 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1670bffd
PP
226 goto end;
227 }
228
229 /*
230 * Search order is:
231 *
232 * 1. BABELTRACE_PLUGIN_PATH environment variable
233 * (colon-separated list of directories)
234 * 2. ~/.local/lib/babeltrace/plugins
235 * 3. Default system directory for Babeltrace plugins, usually
236 * /usr/lib/babeltrace/plugins or
237 * /usr/local/lib/babeltrace/plugins if installed
238 * locally
239 * 4. Built-in plugins (static)
240 *
241 * Directories are searched non-recursively.
242 */
243 envvar = getenv("BABELTRACE_PLUGIN_PATH");
244 if (envvar) {
245 ret = bt_common_append_plugin_path_dirs(envvar, dirs);
246 if (ret) {
8c1a3187 247 BT_LOGE_STR("Failed to append plugin path to array of directories.");
1670bffd
PP
248 goto end;
249 }
250 }
251
252 home_plugin_dir = bt_common_get_home_plugin_path();
253 if (home_plugin_dir) {
254 GString *home_plugin_dir_str =
255 g_string_new(home_plugin_dir);
256
257 if (!home_plugin_dir_str) {
8c1a3187 258 BT_LOGE_STR("Failed to allocate a GString.");
1670bffd
PP
259 goto end;
260 }
261
262 g_ptr_array_add(dirs, home_plugin_dir_str);
263 }
264
265 system_plugin_dir = bt_common_get_system_plugin_path();
266 if (system_plugin_dir) {
267 GString *system_plugin_dir_str =
268 g_string_new(system_plugin_dir);
269
270 if (!system_plugin_dir_str) {
8c1a3187 271 BT_LOGE_STR("Failed to allocate a GString.");
1670bffd
PP
272 goto end;
273 }
274
275 g_ptr_array_add(dirs, system_plugin_dir_str);
276 }
277
278 for (i = 0; i < dirs->len; i++) {
279 GString *dir = g_ptr_array_index(dirs, i);
280
a8ff38ef 281 BT_PUT(plugin_set);
8c1a3187 282
50ad9320
PP
283 /*
284 * Skip this if the directory does not exist because
285 * bt_plugin_create_all_from_dir() would log a warning.
286 */
287 if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) {
288 BT_LOGV("Skipping nonexistent directory path: "
289 "path=\"%s\"", dir->str);
290 continue;
291 }
292
8c1a3187 293 /* bt_plugin_create_all_from_dir() logs details/errors */
c55a9f58 294 plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
a8ff38ef 295 if (!plugin_set) {
8c1a3187
PP
296 BT_LOGD("No plugins found in directory: path=\"%s\"",
297 dir->str);
1670bffd
PP
298 continue;
299 }
300
a8ff38ef
PP
301 for (j = 0; j < plugin_set->plugins->len; j++) {
302 struct bt_plugin *candidate_plugin =
303 g_ptr_array_index(plugin_set->plugins, j);
1670bffd 304
a8ff38ef
PP
305 if (strcmp(bt_plugin_get_name(candidate_plugin),
306 plugin_name) == 0) {
8c1a3187
PP
307 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
308 plugin_name, dir->str);
a8ff38ef 309 plugin = bt_get(candidate_plugin);
1670bffd
PP
310 goto end;
311 }
1670bffd 312 }
8c1a3187
PP
313
314 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
315 plugin_name, dir->str);
1670bffd
PP
316 }
317
a8ff38ef
PP
318 bt_put(plugin_set);
319 plugin_set = bt_plugin_create_all_from_static();
8c1a3187
PP
320 if (plugin_set) {
321 for (j = 0; j < plugin_set->plugins->len; j++) {
322 struct bt_plugin *candidate_plugin =
323 g_ptr_array_index(plugin_set->plugins, j);
a8ff38ef 324
8c1a3187
PP
325 if (strcmp(bt_plugin_get_name(candidate_plugin),
326 plugin_name) == 0) {
327 BT_LOGD("Plugin found in built-in plugins: "
328 "name=\"%s\"", plugin_name);
329 plugin = bt_get(candidate_plugin);
330 goto end;
331 }
1670bffd 332 }
1670bffd
PP
333 }
334
335end:
336 free(home_plugin_dir);
a8ff38ef 337 bt_put(plugin_set);
1670bffd
PP
338
339 if (dirs) {
340 g_ptr_array_free(dirs, TRUE);
341 }
342
8c1a3187
PP
343 if (plugin) {
344 BT_LOGD("Found plugin in standard directories and built-in plugins: "
345 "addr=%p, name=\"%s\", path=\"%s\"",
346 plugin, plugin_name, bt_plugin_get_path(plugin));
347 } else {
348 BT_LOGD("No plugin found in standard directories and built-in plugins: "
349 "name=\"%s\"", plugin_name);
350 }
351
1670bffd
PP
352 return plugin;
353}
354
2b43acf9
PP
355struct bt_component_class *bt_plugin_find_component_class(
356 const char *plugin_name, const char *comp_cls_name,
357 enum bt_component_class_type comp_cls_type)
358{
359 struct bt_plugin *plugin = NULL;
360 struct bt_component_class *comp_cls = NULL;
361
8c1a3187
PP
362 if (!plugin_name) {
363 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
2b43acf9
PP
364 goto end;
365 }
366
8c1a3187
PP
367 if (!comp_cls_name) {
368 BT_LOGW_STR("Invalid parameter: component class name is NULL.");
369 goto end;
370 }
371
372 BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
373 "plugin-name=\"%s\", comp-class-name=\"%s\"",
374 plugin_name, comp_cls_name);
2b43acf9
PP
375 plugin = bt_plugin_find(plugin_name);
376 if (!plugin) {
8c1a3187 377 BT_LOGD_STR("Plugin not found.");
2b43acf9
PP
378 goto end;
379 }
380
381 comp_cls = bt_plugin_get_component_class_by_name_and_type(
382 plugin, comp_cls_name, comp_cls_type);
8c1a3187
PP
383 if (!comp_cls) {
384 BT_LOGD("Component class not found in plugin: "
385 "plugin-addr=%p, plugin-path=\"%s\"",
386 plugin, bt_plugin_get_path(plugin));
387 }
2b43acf9
PP
388
389end:
390 bt_put(plugin);
391 return comp_cls;
392}
393
e1f4c4f7
MJ
394static struct {
395 pthread_mutex_t lock;
396 struct bt_plugin_set *plugin_set;
397 bt_bool recurse;
398} append_all_from_dir_info = {
399 .lock = PTHREAD_MUTEX_INITIALIZER
400};
401
33b34c43 402static
e1f4c4f7
MJ
403int nftw_append_all_from_dir(const char *file, const struct stat *sb, int flag,
404 struct FTW *s)
33b34c43 405{
e1f4c4f7
MJ
406 int ret = 0;
407 const char *name = file + s->base;
408
409 /* Check for recursion */
410 if (!append_all_from_dir_info.recurse && s->level > 1) {
411 goto end;
412 }
413
414 switch (flag) {
415 case FTW_F:
52238017
MJ
416 {
417 struct bt_plugin_set *plugins_from_file;
418
e1f4c4f7
MJ
419 if (name[0] == '.') {
420 /* Skip hidden files */
421 BT_LOGV("Skipping hidden file: path=\"%s\"", file);
422 goto end;
423 }
52238017
MJ
424
425 plugins_from_file = bt_plugin_create_all_from_file(file);
e1f4c4f7
MJ
426
427 if (plugins_from_file) {
428 size_t j;
429
430 for (j = 0; j < plugins_from_file->plugins->len; j++) {
431 struct bt_plugin *plugin =
432 g_ptr_array_index(plugins_from_file->plugins, j);
433
434 BT_LOGD("Adding plugin to plugin set: "
435 "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
436 file, plugin, bt_plugin_get_name(plugin));
437 bt_plugin_set_add_plugin(append_all_from_dir_info.plugin_set, plugin);
438 }
33b34c43 439
e1f4c4f7
MJ
440 bt_put(plugins_from_file);
441 }
442 break;
52238017 443 }
e1f4c4f7
MJ
444 case FTW_DNR:
445 /* Continue to next file / directory. */
446 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file);
447 break;
448 case FTW_NS:
449 /* Continue to next file / directory. */
450 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file);
451 break;
33b34c43 452 }
e1f4c4f7
MJ
453
454end:
455 return ret;
33b34c43
PP
456}
457
458static
459enum bt_plugin_status bt_plugin_create_append_all_from_dir(
a8ff38ef 460 struct bt_plugin_set *plugin_set, const char *path,
c55a9f58 461 bt_bool recurse)
33b34c43 462{
e1f4c4f7 463 int nftw_flags = FTW_PHYS;
6ba0b073 464 size_t path_len;
33b34c43
PP
465 enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
466
6ba0b073 467 if (!path) {
8c1a3187 468 BT_LOGW_STR("Invalid parameter: path is NULL.");
6ba0b073
PP
469 ret = BT_PLUGIN_STATUS_ERROR;
470 goto end;
471 }
472
473 path_len = strlen(path);
33b34c43 474 if (path_len >= PATH_MAX) {
8c1a3187
PP
475 BT_LOGW("Invalid parameter: path length is too large: "
476 "path-length=%zu", path_len);
33b34c43
PP
477 ret = BT_PLUGIN_STATUS_ERROR;
478 goto end;
479 }
480
e1f4c4f7 481 pthread_mutex_lock(&append_all_from_dir_info.lock);
33b34c43 482
e1f4c4f7
MJ
483 append_all_from_dir_info.plugin_set = plugin_set;
484 append_all_from_dir_info.recurse = recurse;
485 ret = nftw(path, nftw_append_all_from_dir,
486 APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
33b34c43 487
e1f4c4f7 488 pthread_mutex_unlock(&append_all_from_dir_info.lock);
33b34c43 489
e1f4c4f7
MJ
490 if (ret != 0) {
491 BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d",
492 strerror(errno), path, errno);
33b34c43 493 ret = BT_PLUGIN_STATUS_ERROR;
33b34c43 494 }
8c1a3187 495
33b34c43 496end:
33b34c43
PP
497 return ret;
498}
499
a8ff38ef 500struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
c55a9f58 501 bt_bool recurse)
33b34c43 502{
a8ff38ef 503 struct bt_plugin_set *plugin_set;
33b34c43
PP
504 enum bt_plugin_status status;
505
8c1a3187
PP
506 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
507 path, recurse);
a8ff38ef
PP
508 plugin_set = bt_plugin_set_create();
509 if (!plugin_set) {
8c1a3187 510 BT_LOGE_STR("Cannot create empty plugin set.");
a8ff38ef
PP
511 goto error;
512 }
513
33b34c43 514 /* Append found plugins to array */
a8ff38ef 515 status = bt_plugin_create_append_all_from_dir(plugin_set, path,
33b34c43
PP
516 recurse);
517 if (status < 0) {
8c1a3187 518 BT_LOGW("Cannot append plugins found in directory: "
dbd7f7e9
PP
519 "path=\"%s\", status=%s",
520 path, bt_plugin_status_string(status));
33b34c43
PP
521 goto error;
522 }
523
8c1a3187
PP
524 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
525 plugin_set->plugins->len, plugin_set->plugins->len, path);
33b34c43
PP
526 goto end;
527
528error:
a8ff38ef 529 BT_PUT(plugin_set);
33b34c43
PP
530
531end:
a8ff38ef 532 return plugin_set;
33b34c43
PP
533}
534
33b34c43
PP
535const char *bt_plugin_get_name(struct bt_plugin *plugin)
536{
8c1a3187
PP
537 const char *val = NULL;
538
539 if (!plugin) {
540 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
541 goto end;
542 }
543
544 if (plugin->info.name_set) {
545 val = plugin->info.name->str;
546 }
547
548end:
549 return val;
33b34c43
PP
550}
551
552const char *bt_plugin_get_author(struct bt_plugin *plugin)
553{
8c1a3187
PP
554 const char *val = NULL;
555
556 if (!plugin) {
557 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
558 goto end;
559 }
560
561 if (plugin->info.author_set) {
562 val = plugin->info.author->str;
563 }
564
565end:
566 return val;
33b34c43
PP
567}
568
569const char *bt_plugin_get_license(struct bt_plugin *plugin)
570{
8c1a3187
PP
571 const char *val = NULL;
572
573 if (!plugin) {
574 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
575 goto end;
576 }
577
578 if (plugin->info.license_set) {
579 val = plugin->info.license->str;
580 }
581
582end:
583 return val;
33b34c43
PP
584}
585
586const char *bt_plugin_get_path(struct bt_plugin *plugin)
587{
8c1a3187
PP
588 const char *val = NULL;
589
590 if (!plugin) {
591 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
592 goto end;
593 }
594
595 if (plugin->info.path_set) {
596 val = plugin->info.path->str;
597 }
598
599end:
600 return val;
33b34c43
PP
601}
602
603const char *bt_plugin_get_description(struct bt_plugin *plugin)
604{
8c1a3187
PP
605 const char *val = NULL;
606
607 if (!plugin) {
608 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
609 goto end;
610 }
611
612 if (plugin->info.description_set) {
613 val = plugin->info.description->str;
614 }
615
616end:
617 return val;
33b34c43
PP
618}
619
b6de043b
PP
620enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin,
621 unsigned int *major, unsigned int *minor, unsigned int *patch,
622 const char **extra)
623{
624 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
625
8c1a3187
PP
626 if (!plugin) {
627 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
628 status = BT_PLUGIN_STATUS_ERROR;
629 goto end;
630 }
631
632 if (!plugin->info.version_set) {
633 BT_LOGV("Plugin's version is not set: addr=%p", plugin);
b6de043b
PP
634 status = BT_PLUGIN_STATUS_ERROR;
635 goto end;
636 }
637
638 if (major) {
55bb57e0 639 *major = plugin->info.version.major;
b6de043b
PP
640 }
641
642 if (minor) {
55bb57e0 643 *minor = plugin->info.version.minor;
b6de043b
PP
644 }
645
646 if (patch) {
55bb57e0 647 *patch = plugin->info.version.patch;
b6de043b
PP
648 }
649
650 if (extra) {
55bb57e0 651 *extra = plugin->info.version.extra->str;
b6de043b
PP
652 }
653
654end:
655 return status;
656}
657
544d0515 658int64_t bt_plugin_get_component_class_count(struct bt_plugin *plugin)
33b34c43 659{
9ac68eb1 660 return plugin ? plugin->comp_classes->len : (int64_t) -1;
33b34c43
PP
661}
662
9ac68eb1
PP
663struct bt_component_class *bt_plugin_get_component_class_by_index(
664 struct bt_plugin *plugin, uint64_t index)
33b34c43
PP
665{
666 struct bt_component_class *comp_class = NULL;
667
8c1a3187
PP
668 if (!plugin) {
669 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
670 goto error;
671 }
672
673 if (index >= plugin->comp_classes->len) {
674 BT_LOGW("Invalid parameter: index is out of bounds: "
675 "addr=%p, index=%" PRIu64 ", count=%u",
676 plugin, index, plugin->comp_classes->len);
33b34c43
PP
677 goto error;
678 }
679
680 comp_class = g_ptr_array_index(plugin->comp_classes, index);
681 bt_get(comp_class);
682 goto end;
683
684error:
685 BT_PUT(comp_class);
686
687end:
688 return comp_class;
689}
690
691struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
692 struct bt_plugin *plugin, const char *name,
d3e4dcd8 693 enum bt_component_class_type type)
33b34c43
PP
694{
695 struct bt_component_class *comp_class = NULL;
696 size_t i;
697
8c1a3187
PP
698 if (!plugin) {
699 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
700 goto error;
701 }
702
703 if (!name) {
704 BT_LOGW_STR("Invalid parameter: name is NULL.");
33b34c43
PP
705 goto error;
706 }
707
708 for (i = 0; i < plugin->comp_classes->len; i++) {
709 struct bt_component_class *comp_class_candidate =
710 g_ptr_array_index(plugin->comp_classes, i);
711 const char *comp_class_cand_name =
712 bt_component_class_get_name(comp_class_candidate);
d3e4dcd8 713 enum bt_component_class_type comp_class_cand_type =
33b34c43
PP
714 bt_component_class_get_type(comp_class_candidate);
715
716 assert(comp_class_cand_name);
717 assert(comp_class_cand_type >= 0);
718
719 if (strcmp(name, comp_class_cand_name) == 0 &&
720 comp_class_cand_type == type) {
721 comp_class = bt_get(comp_class_candidate);
722 break;
723 }
724 }
725
726 goto end;
727
728error:
729 BT_PUT(comp_class);
730
731end:
732 return comp_class;
733}
734
33b34c43
PP
735enum bt_plugin_status bt_plugin_add_component_class(
736 struct bt_plugin *plugin, struct bt_component_class *comp_class)
737{
738 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
739 struct bt_component_class *comp_class_dup = NULL;
33b34c43
PP
740 int comp_class_index = -1;
741
8c1a3187
PP
742 if (!plugin) {
743 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
744 goto error;
745 }
746
747 if (!comp_class) {
748 BT_LOGW_STR("Invalid parameter: component class is NULL.");
749 goto error;
750 }
751
752 if (plugin->frozen) {
753 BT_LOGW("Invalid parameter: plugin is frozen: "
754 "addr=%p, name=\"%s\"", plugin,
755 bt_plugin_get_name(plugin));
33b34c43
PP
756 goto error;
757 }
758
759 /* Check for duplicate */
760 comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin,
761 bt_component_class_get_name(comp_class),
762 bt_component_class_get_type(comp_class));
763 if (comp_class_dup) {
8c1a3187
PP
764 BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
765 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
766 "comp-class-name=\"%s\", comp-class-type=%s",
767 plugin, bt_plugin_get_name(plugin),
768 bt_plugin_get_path(plugin),
33b34c43 769 bt_component_class_get_name(comp_class),
8c1a3187
PP
770 bt_component_class_type_string(
771 bt_component_class_get_type(comp_class)));
33b34c43
PP
772 goto error;
773 }
774
775 /* Add new component class */
776 comp_class_index = plugin->comp_classes->len;
777 g_ptr_array_add(plugin->comp_classes, bt_get(comp_class));
778
55bb57e0
PP
779 /* Special case for a shared object plugin */
780 if (plugin->type == BT_PLUGIN_TYPE_SO) {
3230ee6b 781 bt_plugin_so_on_add_component_class(plugin, comp_class);
33b34c43
PP
782 }
783
8c1a3187
PP
784 BT_LOGD("Added component class to plugin: "
785 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
786 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
787 plugin, bt_plugin_get_name(plugin),
788 bt_plugin_get_path(plugin),
789 comp_class,
790 bt_component_class_get_name(comp_class),
791 bt_component_class_type_string(
792 bt_component_class_get_type(comp_class)));
33b34c43
PP
793 goto end;
794
795error:
33b34c43
PP
796 /* Remove entry from plugin's component classes (if added) */
797 if (comp_class_index >= 0) {
798 g_ptr_array_remove_index(plugin->comp_classes,
799 comp_class_index);
800 }
801
802 status = BT_PLUGIN_STATUS_ERROR;
803
804end:
805 bt_put(comp_class_dup);
806 return status;
807}
This page took 0.068366 seconds and 4 git commands to generate.