Added options to run different tests in module batchtest
[lttv.git] / ltt / branches / poly / lttv / main / module.c
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
19
20 /* module.c : Implementation of the module loading/unloading mechanism.
21 *
22 */
23
24 #include <lttv/module.h>
25
26 struct _LttvModule
27 {
28 GModule *module;
29 guint ref_count;
30 guint load_count;
31 GPtrArray *dependents;
32 };
33
34
35 /* Table of loaded modules and paths where to search for modules */
36
37 static GHashTable *modules = NULL;
38
39 static GPtrArray *modulesPaths = NULL;
40
41 static void lttv_module_unload_all();
42
43
44 void lttv_module_init(int argc, char **argv)
45 {
46 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Init module.c");
47 modules = g_hash_table_new(g_str_hash, g_str_equal);
48 modulesPaths = g_ptr_array_new();
49 }
50
51
52 void lttv_module_destroy()
53 {
54 int i;
55
56 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Destroy module.c");
57
58 /* Unload all modules */
59 lttv_module_unload_all();
60
61 /* Free the modules paths pointer array as well as the elements */
62 for(i = 0; i < modulesPaths->len ; i++) {
63 g_free(modulesPaths->pdata[i]);
64 }
65 g_ptr_array_free(modulesPaths,TRUE) ;
66 modulesPaths = NULL;
67
68 /* destroy the hash table */
69 g_hash_table_destroy(modules) ;
70 modules = NULL;
71 }
72
73
74 /* Add a new pathname to the modules loading search path */
75
76 void lttv_module_path_add(const char *name)
77 {
78 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Add module path %s", name);
79 g_ptr_array_add(modulesPaths,(char*)g_strdup(name));
80 }
81
82
83 static LttvModule *
84 module_load(const char *name, int argc, char **argv)
85 {
86 GModule *gm;
87
88 LttvModule *m;
89
90 int i;
91
92 char *pathname;
93
94 const char *module_name;
95
96 LttvModuleInit init_function;
97
98 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Load module %s", name);
99
100 /* Try to find the module along all the user specified paths */
101
102 for(i = 0 ; i < modulesPaths->len ; i++) {
103 pathname = g_module_build_path(modulesPaths->pdata[i],name);
104 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Try path %s", pathname);
105 gm = g_module_open(pathname,0);
106 g_free(pathname);
107
108 if(gm != NULL) break;
109 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,"Trial failed, %s",g_module_error());
110 }
111
112 /* Try the default system path */
113
114 if(gm == NULL) {
115 pathname = g_module_build_path(NULL,name);
116 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Try default path");
117 gm = g_module_open(pathname,0);
118 g_free(pathname);
119 }
120
121 /* Module cannot be found */
122 if(gm == NULL) {
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);
125 return NULL;
126 }
127
128 /* Check if the module was already opened using the hopefully canonical name
129 returned by g_module_name. */
130
131 module_name = g_module_name(gm);
132
133 m = g_hash_table_lookup(modules, module_name);
134
135 if(m == NULL) {
136 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
137 "Module %s (%s) loaded, call its init function", name, module_name);
138
139 /* Module loaded for the first time. Insert it in the table and call the
140 init function if any. */
141
142 m = g_new(LttvModule, 1);
143 m->module = gm;
144 m->ref_count = 0;
145 m->load_count = 0;
146 m->dependents = g_ptr_array_new();
147 g_hash_table_insert(modules, (gpointer)module_name, m);
148
149 if(!g_module_symbol(gm, "init", (gpointer)&init_function)) {
150 g_warning("module %s (%s) has no init function", name, pathname);
151 }
152 else init_function(m, argc, argv);
153 }
154 else {
155
156 /* Module was already opened, check that it really is the same and
157 undo the extra g_module_open */
158
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);
162 if(m->module != gm) g_error("Two gmodules with the same pathname");
163 g_module_close(gm);
164 }
165
166 m->ref_count++;
167 return m;
168 }
169
170
171 LttvModule *
172 lttv_module_load(const char *name, int argc, char **argv)
173 {
174 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Load module %s explicitly", name);
175 LttvModule *m = module_load(name, argc, argv);
176 if(m != NULL) m->load_count++;
177 return m;
178 }
179
180
181 LttvModule *
182 lttv_module_require(LttvModule *m, const char *name, int argc, char **argv)
183 {
184 LttvModule *module;
185
186 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO,
187 "Load module %s, as %s is a dependent requiring it", name,
188 g_module_name(m->module));
189 module = module_load(name, argc, argv);
190 if(module != NULL) g_ptr_array_add(m->dependents, module);
191 return module;
192 }
193
194
195 static void module_unload(LttvModule *m)
196 {
197 LttvModuleDestroy destroy_function;
198
199 char *pathname;
200
201 guint i, len;
202
203 /* Decrement the reference count */
204
205 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload module %s",
206 g_module_name(m->module));
207 m->ref_count--;
208 if(m->ref_count > 0) {
209 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
210 "Module usage count decremented to %d", m->ref_count);
211 return;
212 }
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 */
224
225 len = m->dependents->len;
226 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload dependent modules");
227
228 for(i = 0 ; i < len ; i++) {
229 module_unload(m->dependents->pdata[i]);
230 }
231
232 if(len != m->dependents->len) g_error("dependents list modified");
233
234 /* Finally remove any trace of this module */
235
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 }
241
242
243 void lttv_module_unload(LttvModule *m)
244 {
245 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Explicitly unload module %s",
246 g_module_name(m->module));
247 if(m->load_count <= 0) {
248 g_error("more unload than load (%s)", g_module_name(m->module));
249 return;
250 }
251 m->load_count--;
252 module_unload(m);
253 }
254
255
256 static void
257 list_modules(gpointer key, gpointer value, gpointer user_data)
258 {
259 g_ptr_array_add((GPtrArray *)user_data, value);
260 }
261
262
263 LttvModule **
264 lttv_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
278 LttvModule **
279 lttv_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
294 char *
295 lttv_module_name(LttvModule *m)
296 {
297 return g_module_name(m->module);
298 }
299
300 static void
301 list_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
309 void
310 lttv_module_unload_all()
311 {
312 guint i;
313
314 LttvModule *m;
315
316 GPtrArray *independent_modules;
317
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);
321
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);
327 }
328 }
This page took 0.039961 seconds and 4 git commands to generate.