fix temporarily current time selection by disabling looking into loaded buffer :...
[lttv.git] / ltt / branches / poly / lttv / lttv / module.c
CommitLineData
08b1c66e 1
9c312311 2/* This file is part of the Linux Trace Toolkit viewer
3 * Copyright (C) 2003-2004 Michel Dagenais
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 */
19
eccb5352 20
08b1c66e 21/* module.c : Implementation of the module loading/unloading mechanism. */
eccb5352 22
3d218f2a 23#include <lttv/module.h>
08b1c66e 24#include <gmodule.h>
25
26
27struct _LttvLibrary
28{
29 LttvLibraryInfo info;
30 GPtrArray *modules;
31 GModule *gm;
32 guint locked_loaded;
33};
34
dc877563 35
dc877563 36struct _LttvModule
37{
08b1c66e 38 LttvModuleInfo info;
39 char **prerequisites_names;
40 GPtrArray *prerequisites;
dc877563 41};
42
eccb5352 43
08b1c66e 44/* Modules are searched by name. However, a library may be loaded which
45 provides a module with the same name as an existing one. A stack of
46 modules is thus maintained for each name.
47
48 Libraries correspond to glib modules. The g_module function is
49 responsible for loading each library only once. */
50
51static GHashTable *modules_by_name = NULL;
eccb5352 52
08b1c66e 53static GPtrArray *libraries = NULL;
eccb5352 54
08b1c66e 55static GHashTable *libraries_by_g_module = NULL;
eccb5352 56
08b1c66e 57static GPtrArray *library_paths = NULL;
dc877563 58
08b1c66e 59static gboolean initialized = FALSE;
dc877563 60
08b1c66e 61static gboolean destroyed = TRUE;
62
63static struct _LttvModuleDescription *builtin_chain = NULL;
64
65static struct _LttvModuleDescription *module_chain = NULL;
66
67static struct _LttvModuleDescription **module_next = &module_chain;
68
69static GQuark lttv_module_error;
70
71static void init();
72
00e74b69 73static void finish_destroy();
08b1c66e 74
75static void module_release(LttvModule *m);
76
77
78static LttvLibrary *library_add(char *name, char *path, GModule *gm)
dc877563 79{
08b1c66e 80 LttvLibrary *l;
eccb5352 81
08b1c66e 82 LttvModule *m;
dc877563 83
08b1c66e 84 struct _LttvModuleDescription *link;
eccb5352 85
08b1c66e 86 GPtrArray *modules;
87
88 l = g_new(LttvLibrary, 1);
89 l->modules = g_ptr_array_new();
90 l->gm = gm;
91 l->locked_loaded = 0;
92 l->info.name = g_strdup(name);
93 l->info.path = g_strdup(path);
94 l->info.load_count = 0;
b445142a 95
08b1c66e 96 g_ptr_array_add(libraries, l);
97 g_hash_table_insert(libraries_by_g_module, gm, l);
eccb5352 98
08b1c66e 99 *module_next = NULL;
100 for(link = module_chain; link != NULL; link = link->next) {
101 m = g_new(LttvModule, 1);
102 g_ptr_array_add(l->modules, m);
103
104 modules = g_hash_table_lookup(modules_by_name, link->name);
105 if(modules == NULL) {
106 modules = g_ptr_array_new();
107 g_hash_table_insert(modules_by_name, g_strdup(link->name), modules);
108 }
109 g_ptr_array_add(modules, m);
110
111 m->prerequisites_names = link->prerequisites;
112 m->prerequisites = g_ptr_array_new();
113 m->info.name = link->name;
114 m->info.short_description = link->short_description;
115 m->info.description = link->description;
116 m->info.init = link->init;
117 m->info.destroy = link->destroy;
118 m->info.library = l;
119 m->info.require_count = 0;
120 m->info.use_count = 0;
121 m->info.prerequisites_number = link->prerequisites_number;
eccb5352 122 }
08b1c66e 123 return l;
eccb5352 124}
125
dc877563 126
08b1c66e 127static void library_remove(LttvLibrary *l)
dc877563 128{
08b1c66e 129 LttvModule *m;
130
131 GPtrArray *modules;
00e74b69 132 GPtrArray **modules_ptr = &modules; /* for strict aliasing */
08b1c66e 133 guint i;
134
135 char *key;
00e74b69 136 char **key_ptr = &key; /* for strict aliasing */
08b1c66e 137
138 for(i = 0 ; i < l->modules->len ; i++) {
139 m = (LttvModule *)(l->modules->pdata[i]);
140
141 g_hash_table_lookup_extended(modules_by_name, m->info.name,
00e74b69 142 (gpointer *)key_ptr, (gpointer *)modules_ptr);
08b1c66e 143 g_assert(modules != NULL);
144 g_ptr_array_remove(modules, m);
145 if(modules->len == 0) {
146 g_hash_table_remove(modules_by_name, m->info.name);
147 g_ptr_array_free(modules, TRUE);
148 g_free(key);
149 }
150
151 g_ptr_array_free(m->prerequisites, TRUE);
152 g_free(m);
153 }
154
155 g_ptr_array_remove(libraries, l);
156 g_hash_table_remove(libraries_by_g_module, l->gm);
157 g_ptr_array_free(l->modules, TRUE);
158 g_free(l->info.name);
159 g_free(l->info.path);
160 g_free(l);
eccb5352 161}
162
163
08b1c66e 164static LttvLibrary *library_load(char *name, GError **error)
dc877563 165{
00e74b69 166 GModule *gm = NULL;
eccb5352 167
08b1c66e 168 int i, nb;
169
00e74b69 170 /* path is always initialized, checked */
171 char *path = NULL, *pathname;
08b1c66e 172
173 LttvLibrary *l;
eccb5352 174
08b1c66e 175 GString *messages = g_string_new("");
eccb5352 176
08b1c66e 177 /* insure that module.c is initialized */
ffd54a90 178
08b1c66e 179 init();
eccb5352 180
08b1c66e 181 /* Try to find the library along all the user specified paths */
b445142a 182
08b1c66e 183 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Load library %s", name);
184 nb = lttv_library_path_number();
185 for(i = 0 ; i <= nb ; i++) {
186 if(i < nb) path = lttv_library_path_get(i);
187 else path = NULL;
eccb5352 188
08b1c66e 189 pathname = g_module_build_path(path ,name);
190 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Try path %s", pathname);
191 module_chain = NULL;
192 module_next = &module_chain;
b445142a 193 gm = g_module_open(pathname,0);
8b0bbe19 194 g_free(pathname);
eccb5352 195
dc877563 196 if(gm != NULL) break;
dc877563 197
08b1c66e 198 g_string_append(messages, g_module_error());
199 g_string_append(messages, "\n");
200 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_INFO,"Trial failed, %s", g_module_error());
eccb5352 201 }
eccb5352 202
dc877563 203 /* Module cannot be found */
08b1c66e 204
b445142a 205 if(gm == NULL) {
08b1c66e 206 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Failed to load %s", name);
207 g_set_error(error, lttv_module_error, LTTV_MODULE_NOT_FOUND,
208 "Cannot load library %s: %s", name, messages->str);
209 g_string_free(messages, TRUE);
b445142a 210 return NULL;
211 }
08b1c66e 212 g_string_free(messages, TRUE);
213
214 /* Check if the library was already loaded */
215
216 l = g_hash_table_lookup(libraries_by_g_module, gm);
dc877563 217
08b1c66e 218 /* This library was not already loaded */
dc877563 219
08b1c66e 220 if(l == NULL) {
221 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Library %s (%s) loaded", name,
222 g_module_name(gm));
223 l = library_add(name, path, gm);
224 }
225 return l;
226}
eccb5352 227
eccb5352 228
08b1c66e 229LttvLibrary *lttv_library_load(char *name, GError **error)
230{
8b0bbe19 231 LttvLibrary *l = library_load(name, error);
232 if(l != NULL) l->info.load_count++;
08b1c66e 233 return l;
234}
eccb5352 235
eccb5352 236
08b1c66e 237static void library_unload(LttvLibrary *l)
238{
00e74b69 239 guint i;
08b1c66e 240
241 GModule *gm;
242
243 LttvModule *m;
244
245 if(l->locked_loaded > 0) {
246 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload library %s: locked loaded",
247 l->info.name);
248 return;
249 }
250
251 if(l->info.load_count > 0) {
252 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload library %s: load count %d",
253 l->info.name, l->info.load_count);
254 return;
eccb5352 255 }
dc877563 256
08b1c66e 257 /* Check if all its modules have been released */
dc877563 258
08b1c66e 259 for(i = 0 ; i < l->modules->len ; i++) {
260 m = (LttvModule *)(l->modules->pdata[i]);
261 if(m->info.use_count > 0) {
262 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO,"Unload library %s: module %s used",
263 l->info.name, m->info.name);
264 return;
265 }
eccb5352 266 }
08b1c66e 267
268 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload library %s: close the GModule",
269 l->info.name);
270 gm = l->gm;
271 library_remove(l);
272 if(gm != NULL) g_module_close(gm);
273
274 /* insure that module.c will be finalized */
275
276 finish_destroy();
eccb5352 277}
278
eccb5352 279
08b1c66e 280void lttv_library_unload(LttvLibrary *l)
dc877563 281{
145c1588 282 /* In the case where we wait for a module to release, the load count is 0
283 * and should not be decremented. */
284 if(l->info.load_count != 0) l->info.load_count--;
08b1c66e 285 library_unload(l);
dc877563 286}
eccb5352 287
eccb5352 288
08b1c66e 289static void library_lock_loaded(LttvLibrary *l)
dc877563 290{
08b1c66e 291 l->locked_loaded++;
eccb5352 292}
293
294
08b1c66e 295static void library_unlock_loaded(LttvLibrary *l)
dc877563 296{
08b1c66e 297 l->locked_loaded--;
298 library_unload(l);
299}
eccb5352 300
eccb5352 301
08b1c66e 302static LttvModule *module_require(char *name, GError **error)
303{
304 GError *tmp_error = NULL;
eccb5352 305
08b1c66e 306 guint i, j;
eccb5352 307
08b1c66e 308 LttvModule *m, *required;
309
310 LttvLibrary *l = NULL;
311
312 GPtrArray *modules;
313
314 /* Insure that module.c is initialized */
315
316 init();
dbb7bb09 317
08b1c66e 318 /* Check if the module is already loaded */
dbb7bb09 319
08b1c66e 320 modules = g_hash_table_lookup(modules_by_name, name);
321
322 /* Try to load a library having the module name */
323
324 if(modules == NULL) {
325 l = library_load(name, error);
326 if(l == NULL) return NULL;
327 else library_lock_loaded(l);
328
329 /* A library was found, does it contain the named module */
330
331 modules = g_hash_table_lookup(modules_by_name, name);
332 if(modules == NULL) {
333 g_set_error(error, lttv_module_error, LTTV_MODULE_NOT_FOUND,
334 "Module %s not found in library %s", name, l->info.name);
335 library_unlock_loaded(l);
336 return NULL;
337 }
dbb7bb09 338 }
08b1c66e 339 m = (LttvModule *)(modules->pdata[modules->len - 1]);
dbb7bb09 340
08b1c66e 341 /* We have the module */
eccb5352 342
08b1c66e 343 m->info.use_count++;
eccb5352 344
08b1c66e 345 /* First use of the module. Initialize after getting the prerequisites */
346
347 if(m->info.use_count == 1) {
348 for(i = 0 ; i < m->info.prerequisites_number ; i++) {
349 required = module_require(m->prerequisites_names[i], &tmp_error);
350
351 /* A prerequisite could not be found, undo everything and fail */
352
353 if(required == NULL) {
354 for(j = 0 ; j < m->prerequisites->len ; j++) {
355 module_release((LttvModule *)(m->prerequisites->pdata[j]));
356 }
357 g_ptr_array_set_size(m->prerequisites, 0);
358 if(l != NULL) library_unlock_loaded(l);
359 g_set_error(error, lttv_module_error, LTTV_MODULE_NOT_FOUND,
360 "Cannot find prerequisite for module %s: %s", name,
361 tmp_error->message);
362 g_clear_error(&tmp_error);
363 return NULL;
364 }
365 g_ptr_array_add(m->prerequisites, required);
366 }
367 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Module %s: init()", m->info.name);
368 m->info.init();
eccb5352 369 }
eccb5352 370
08b1c66e 371 /* Decrement the load count of the library. It will not really be
372 unloaded since it contains a currently used module. */
373
374 if(l != NULL) library_unlock_loaded(l);
dc877563 375
08b1c66e 376 return(m);
377}
378
379
380/* The require_count for a module is the number of explicit calls to
381 lttv_module_require, while the use_count also counts the number of times
382 a module is needed as a prerequisite. */
eccb5352 383
08b1c66e 384LttvModule *lttv_module_require(char *name, GError **error)
385{
386 LttvModule *m = module_require(name, error);
387 if(m != NULL) m->info.require_count++;
388 return(m);
dc877563 389}
eccb5352 390
eccb5352 391
08b1c66e 392static void module_release(LttvModule *m)
dc877563 393{
08b1c66e 394 guint i;
395
396 library_lock_loaded(m->info.library);
397
398 m->info.use_count--;
399 if(m->info.use_count == 0) {
400 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Module %s: destroy()",m->info.name);
401 m->info.destroy();
402 for(i = 0 ; i < m->prerequisites->len ; i++) {
403 module_release((LttvModule *)(m->prerequisites->pdata[i]));
404 }
405 g_ptr_array_set_size(m->prerequisites, 0);
eccb5352 406 }
08b1c66e 407 library_unlock_loaded(m->info.library);
408}
409
410
411void lttv_module_release(LttvModule *m)
412{
413 m->info.require_count--;
414 module_release(m);
415}
416
417
418void lttv_module_info(LttvModule *m, LttvModuleInfo *info)
419{
420 *info = m->info;
421}
422
423
424unsigned lttv_module_prerequisite_number(LttvModule *m)
425{
426 return m->prerequisites->len;
427}
428
429
430LttvModule *lttv_module_prerequisite_get(LttvModule *m, unsigned i)
431{
432 return (LttvModule *)(m->prerequisites->pdata[i]);
433}
434
435
436void lttv_library_info(LttvLibrary *l, LttvLibraryInfo *info)
437{
438 *info = l->info;
439}
440
441
442unsigned lttv_library_module_number(LttvLibrary *l)
443{
444 return l->modules->len;
445}
446
447
448LttvModule *lttv_library_module_get(LttvLibrary *l, unsigned i)
449{
450 return (LttvModule *)(l->modules->pdata[i]);
451}
452
453
454unsigned lttv_library_number()
455{
456 return libraries->len;
dc877563 457}
458
eccb5352 459
08b1c66e 460LttvLibrary *lttv_library_get(unsigned i)
dc877563 461{
08b1c66e 462 return (LttvLibrary *)(libraries->pdata[i]);
eccb5352 463}
464
dc877563 465
224446ce 466void lttv_library_path_add(const char *name)
dc877563 467{
08b1c66e 468 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Add library path %s", name);
469 g_ptr_array_add(library_paths,(char*)g_strdup(name));
470}
471
dc877563 472
224446ce 473void lttv_library_path_remove(const char *name)
08b1c66e 474{
475 guint i;
dc877563 476
08b1c66e 477 for(i = 0 ; i < library_paths->len ; i++) {
478 if(g_str_equal(name, library_paths->pdata[i])) {
479 g_free(library_paths->pdata[i]);
480 g_ptr_array_remove_index(library_paths,i);
481 return;
482 }
483 }
dc877563 484}
485
486
08b1c66e 487unsigned lttv_library_path_number()
dc877563 488{
08b1c66e 489 return library_paths->len;
490}
dc877563 491
dc877563 492
08b1c66e 493char *lttv_library_path_get(unsigned i)
494{
495 return (char *)(library_paths->pdata[library_paths->len - i - 1]);
dc877563 496}
497
08b1c66e 498
499void lttv_module_register(struct _LttvModuleDescription *d)
36b3c068 500{
08b1c66e 501 *module_next = d;
502 module_next = &(d->next);
36b3c068 503}
dc877563 504
08b1c66e 505
506static void init()
dc877563 507{
08b1c66e 508 if(initialized) return;
509 g_assert(destroyed);
510
511 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Init module.c");
dc877563 512
08b1c66e 513 initialized = TRUE;
514 destroyed = FALSE;
515 lttv_module_error = g_quark_from_string("LTTV_MODULE_ERROR");
516 modules_by_name = g_hash_table_new(g_str_hash, g_str_equal);
517 libraries = g_ptr_array_new();
518 libraries_by_g_module = g_hash_table_new(g_direct_hash, g_direct_equal);
519 library_paths = g_ptr_array_new();
520
521 if(builtin_chain == NULL) builtin_chain = module_chain;
522 module_chain = builtin_chain;
523 library_add("builtin", NULL, NULL);
dc877563 524}
525
526
00e74b69 527static void finish_destroy()
dc877563 528{
529 guint i;
530
08b1c66e 531 if(initialized) return;
532 g_assert(!destroyed);
533
534 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Finish destroy module.c");
535 g_hash_table_destroy(modules_by_name);
536 g_ptr_array_free(libraries, TRUE);
537 g_hash_table_destroy(libraries_by_g_module);
538 for(i = 0 ; i < library_paths->len ; i++) {
539 g_free(library_paths->pdata[i]);
540 }
541 g_ptr_array_free(library_paths, TRUE);
542 destroyed = TRUE;
543}
544
545
546static void destroy()
547{
548 guint i, j, nb;
549
550 LttvLibrary *l, **locked_libraries;
551
dc877563 552 LttvModule *m;
553
08b1c66e 554 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Destroy module.c");
555
556 /* Unload all libraries */
dc877563 557
08b1c66e 558 nb = libraries->len;
559 locked_libraries = g_new(LttvLibrary *, nb);
dc877563 560
08b1c66e 561 for(i = 0 ; i < nb ; i++) {
562 l = (LttvLibrary *)(libraries->pdata[i]);
563 locked_libraries[i] = l;
564 library_lock_loaded(l);
565 for(j = 0 ; j < l->modules->len ; j++) {
566 m = (LttvModule *)(l->modules->pdata[j]);
567 while(m->info.require_count > 0) lttv_module_release(m);
dbb7bb09 568 }
08b1c66e 569 while(l->info.load_count > 0) lttv_library_unload(l);
570 }
571
572 for(i = 0 ; i < nb ; i++) {
573 l = locked_libraries[i];
574 library_unlock_loaded(l);
eccb5352 575 }
08b1c66e 576 g_free(locked_libraries);
577
578 /* The library containing module.c may be locked by our caller */
579
580 g_assert(libraries->len <= 1);
581
582 initialized = FALSE;
eccb5352 583}
08b1c66e 584
585LTTV_MODULE("module", "Modules in libraries", \
586 "Load libraries, list, require and initialize contained modules", \
587 init, destroy)
588
This page took 0.057256 seconds and 4 git commands to generate.