Added options to run different tests in module batchtest
[lttv.git] / ltt / branches / poly / lttv / main / module.c
CommitLineData
9c312311 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
eccb5352 19
20/* module.c : Implementation of the module loading/unloading mechanism.
21 *
22 */
23
3d218f2a 24#include <lttv/module.h>
dc877563 25
dc877563 26struct _LttvModule
27{
28 GModule *module;
29 guint ref_count;
30 guint load_count;
31 GPtrArray *dependents;
32};
33
eccb5352 34
35/* Table of loaded modules and paths where to search for modules */
36
37static GHashTable *modules = NULL;
38
eccb5352 39static GPtrArray *modulesPaths = NULL;
40
dc877563 41static void lttv_module_unload_all();
42
43
44void lttv_module_init(int argc, char **argv)
45{
2a2fa4f0 46 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Init module.c");
eccb5352 47 modules = g_hash_table_new(g_str_hash, g_str_equal);
eccb5352 48 modulesPaths = g_ptr_array_new();
49}
50
dc877563 51
52void lttv_module_destroy()
53{
eccb5352 54 int i;
55
2a2fa4f0 56 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Destroy module.c");
b445142a 57
eccb5352 58 /* Unload all modules */
59 lttv_module_unload_all();
60
61 /* Free the modules paths pointer array as well as the elements */
dc877563 62 for(i = 0; i < modulesPaths->len ; i++) {
eccb5352 63 g_free(modulesPaths->pdata[i]);
64 }
65 g_ptr_array_free(modulesPaths,TRUE) ;
eccb5352 66 modulesPaths = NULL;
eccb5352 67
68 /* destroy the hash table */
69 g_hash_table_destroy(modules) ;
70 modules = NULL;
71}
72
dc877563 73
eccb5352 74/* Add a new pathname to the modules loading search path */
75
dc877563 76void lttv_module_path_add(const char *name)
77{
2a2fa4f0 78 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Add module path %s", name);
eccb5352 79 g_ptr_array_add(modulesPaths,(char*)g_strdup(name));
80}
81
82
ffd54a90 83static LttvModule *
dc877563 84module_load(const char *name, int argc, char **argv)
85{
86 GModule *gm;
eccb5352 87
dc877563 88 LttvModule *m;
eccb5352 89
90 int i;
91
92 char *pathname;
ffd54a90 93
94 const char *module_name;
eccb5352 95
dc877563 96 LttvModuleInit init_function;
eccb5352 97
2a2fa4f0 98 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Load module %s", name);
b445142a 99
dc877563 100 /* Try to find the module along all the user specified paths */
eccb5352 101
102 for(i = 0 ; i < modulesPaths->len ; i++) {
103 pathname = g_module_build_path(modulesPaths->pdata[i],name);
2a2fa4f0 104 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Try path %s", pathname);
b445142a 105 gm = g_module_open(pathname,0);
dc877563 106 g_free(pathname);
eccb5352 107
dc877563 108 if(gm != NULL) break;
2a2fa4f0 109 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,"Trial failed, %s",g_module_error());
dc877563 110 }
111
112 /* Try the default system path */
113
114 if(gm == NULL) {
115 pathname = g_module_build_path(NULL,name);
2a2fa4f0 116 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Try default path");
b445142a 117 gm = g_module_open(pathname,0);
eccb5352 118 g_free(pathname);
119 }
eccb5352 120
dc877563 121 /* Module cannot be found */
b445142a 122 if(gm == NULL) {
2a2fa4f0 123 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,"Trial failed, %s",g_module_error());
124 g_warning("Failed to load module %s", name);
b445142a 125 return NULL;
126 }
dc877563 127
128 /* Check if the module was already opened using the hopefully canonical name
129 returned by g_module_name. */
130
ffd54a90 131 module_name = g_module_name(gm);
eccb5352 132
ffd54a90 133 m = g_hash_table_lookup(modules, module_name);
eccb5352 134
dc877563 135 if(m == NULL) {
2a2fa4f0 136 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
137 "Module %s (%s) loaded, call its init function", name, module_name);
eccb5352 138
dc877563 139 /* Module loaded for the first time. Insert it in the table and call the
140 init function if any. */
eccb5352 141
ffd54a90 142 m = g_new(LttvModule, 1);
dc877563 143 m->module = gm;
144 m->ref_count = 0;
145 m->load_count = 0;
146 m->dependents = g_ptr_array_new();
ffd54a90 147 g_hash_table_insert(modules, (gpointer)module_name, m);
dc877563 148
149 if(!g_module_symbol(gm, "init", (gpointer)&init_function)) {
150 g_warning("module %s (%s) has no init function", name, pathname);
151 }
c432246e 152 else init_function(m, argc, argv);
eccb5352 153 }
154 else {
dc877563 155
156 /* Module was already opened, check that it really is the same and
157 undo the extra g_module_open */
158
2a2fa4f0 159 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
160 "Module %s (%s) was already loaded, no need to call init function",
161 name, module_name);
dc877563 162 if(m->module != gm) g_error("Two gmodules with the same pathname");
163 g_module_close(gm);
eccb5352 164 }
165
dc877563 166 m->ref_count++;
167 return m;
eccb5352 168}
169
eccb5352 170
ffd54a90 171LttvModule *
dc877563 172lttv_module_load(const char *name, int argc, char **argv)
173{
2a2fa4f0 174 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Load module %s explicitly", name);
dc877563 175 LttvModule *m = module_load(name, argc, argv);
dc877563 176 if(m != NULL) m->load_count++;
177 return m;
178}
eccb5352 179
eccb5352 180
dc877563 181LttvModule *
182lttv_module_require(LttvModule *m, const char *name, int argc, char **argv)
183{
184 LttvModule *module;
eccb5352 185
2a2fa4f0 186 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO,
187 "Load module %s, as %s is a dependent requiring it", name,
b445142a 188 g_module_name(m->module));
dc877563 189 module = module_load(name, argc, argv);
dbb7bb09 190 if(module != NULL) g_ptr_array_add(m->dependents, module);
dc877563 191 return module;
eccb5352 192}
193
194
dc877563 195static void module_unload(LttvModule *m)
196{
197 LttvModuleDestroy destroy_function;
eccb5352 198
dc877563 199 char *pathname;
eccb5352 200
ffd54a90 201 guint i, len;
eccb5352 202
dc877563 203 /* Decrement the reference count */
eccb5352 204
2a2fa4f0 205 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload module %s",
206 g_module_name(m->module));
dc877563 207 m->ref_count--;
b445142a 208 if(m->ref_count > 0) {
2a2fa4f0 209 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
210 "Module usage count decremented to %d", m->ref_count);
b445142a 211 return;
212 }
dbb7bb09 213
214 /* We really have to unload the module. First destroy it. */
215
216 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
217 "Call the destroy function and unload the module");
218 if(!g_module_symbol(m->module, "destroy", (gpointer)&destroy_function)) {
219 g_warning("module (%s) has no destroy function", pathname);
220 }
221 else destroy_function();
222
223 /* Then release the modules required by this module */
eccb5352 224
dc877563 225 len = m->dependents->len;
2a2fa4f0 226 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload dependent modules");
eccb5352 227
dc877563 228 for(i = 0 ; i < len ; i++) {
229 module_unload(m->dependents->pdata[i]);
eccb5352 230 }
eccb5352 231
dc877563 232 if(len != m->dependents->len) g_error("dependents list modified");
233
dbb7bb09 234 /* Finally remove any trace of this module */
eccb5352 235
dc877563 236 g_hash_table_remove(modules, g_module_name(m->module));
237 g_ptr_array_free(m->dependents, TRUE);
238 g_module_close(m->module);
239 g_free(m);
240}
eccb5352 241
eccb5352 242
dc877563 243void lttv_module_unload(LttvModule *m)
244{
2a2fa4f0 245 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Explicitly unload module %s",
246 g_module_name(m->module));
dc877563 247 if(m->load_count <= 0) {
248 g_error("more unload than load (%s)", g_module_name(m->module));
249 return;
eccb5352 250 }
dc877563 251 m->load_count--;
252 module_unload(m);
253}
254
eccb5352 255
dc877563 256static void
257list_modules(gpointer key, gpointer value, gpointer user_data)
258{
259 g_ptr_array_add((GPtrArray *)user_data, value);
eccb5352 260}
261
dc877563 262
263LttvModule **
264lttv_module_list(guint *nb)
265{
266 GPtrArray *list = g_ptr_array_new();
267
268 LttvModule **array;
269
270 g_hash_table_foreach(modules, list_modules, list);
271 *nb = list->len;
272 array = (LttvModule **)list->pdata;
273 g_ptr_array_free(list, FALSE);
274 return array;
275}
276
277
278LttvModule **
279lttv_module_info(LttvModule *m, const char **name,
280 guint *ref_count, guint *load_count, guint *nb_dependents)
281{
282 guint i, len = m->dependents->len;
283
284 LttvModule **array = g_new(LttvModule *, len);
285
286 *name = g_module_name(m->module);
287 *ref_count = m->ref_count;
288 *load_count = m->load_count;
289 *nb_dependents = len;
290 for(i = 0 ; i < len ; i++) array[i] = m->dependents->pdata[i];
291 return array;
292}
293
36b3c068 294char *
295lttv_module_name(LttvModule *m)
296{
297 return g_module_name(m->module);
298}
dc877563 299
300static void
301list_independent(gpointer key, gpointer value, gpointer user_data)
302{
303 LttvModule *m = (LttvModule *)value;
304
305 if(m->load_count > 0) g_ptr_array_add((GPtrArray *)user_data, m);
306}
307
308
309void
310lttv_module_unload_all()
311{
312 guint i;
313
314 LttvModule *m;
315
dbb7bb09 316 GPtrArray *independent_modules;
dc877563 317
dbb7bb09 318 while(g_hash_table_size(modules) != 0) {
319 independent_modules = g_ptr_array_new();
320 g_hash_table_foreach(modules, list_independent, independent_modules);
dc877563 321
dbb7bb09 322 for(i = 0 ; i < independent_modules->len ; i++) {
323 m = (LttvModule *)independent_modules->pdata[i];
324 lttv_module_unload(m);
325 }
326 g_ptr_array_free(independent_modules, TRUE);
eccb5352 327 }
eccb5352 328}
This page took 0.041323 seconds and 4 git commands to generate.