Use g_info and g_debug properly.
[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(module->dependents, m);
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 /* We really have to unload the module, first unload its dependents */
214
215 len = m->dependents->len;
216 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Unload dependent modules");
217
218 for(i = 0 ; i < len ; i++) {
219 module_unload(m->dependents->pdata[i]);
220 }
221
222 if(len != m->dependents->len) g_error("dependents list modified");
223
224 /* Unload the module itself */
225
226 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
227 "Call the destroy function and unload the module");
228 if(!g_module_symbol(m->module, "destroy", (gpointer)&destroy_function)) {
229 g_warning("module (%s) has no destroy function", pathname);
230 }
231 else destroy_function();
232
233 g_hash_table_remove(modules, g_module_name(m->module));
234 g_ptr_array_free(m->dependents, TRUE);
235 g_module_close(m->module);
236 g_free(m);
237 }
238
239
240 void lttv_module_unload(LttvModule *m)
241 {
242 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Explicitly unload module %s",
243 g_module_name(m->module));
244 if(m->load_count <= 0) {
245 g_error("more unload than load (%s)", g_module_name(m->module));
246 return;
247 }
248 m->load_count--;
249 module_unload(m);
250 }
251
252
253 static void
254 list_modules(gpointer key, gpointer value, gpointer user_data)
255 {
256 g_ptr_array_add((GPtrArray *)user_data, value);
257 }
258
259
260 LttvModule **
261 lttv_module_list(guint *nb)
262 {
263 GPtrArray *list = g_ptr_array_new();
264
265 LttvModule **array;
266
267 g_hash_table_foreach(modules, list_modules, list);
268 *nb = list->len;
269 array = (LttvModule **)list->pdata;
270 g_ptr_array_free(list, FALSE);
271 return array;
272 }
273
274
275 LttvModule **
276 lttv_module_info(LttvModule *m, const char **name,
277 guint *ref_count, guint *load_count, guint *nb_dependents)
278 {
279 guint i, len = m->dependents->len;
280
281 LttvModule **array = g_new(LttvModule *, len);
282
283 *name = g_module_name(m->module);
284 *ref_count = m->ref_count;
285 *load_count = m->load_count;
286 *nb_dependents = len;
287 for(i = 0 ; i < len ; i++) array[i] = m->dependents->pdata[i];
288 return array;
289 }
290
291 char *
292 lttv_module_name(LttvModule *m)
293 {
294 return g_module_name(m->module);
295 }
296
297 static void
298 list_independent(gpointer key, gpointer value, gpointer user_data)
299 {
300 LttvModule *m = (LttvModule *)value;
301
302 if(m->load_count > 0) g_ptr_array_add((GPtrArray *)user_data, m);
303 }
304
305
306 void
307 lttv_module_unload_all()
308 {
309 guint i;
310
311 LttvModule *m;
312
313 GPtrArray *independent_modules = g_ptr_array_new();
314
315 g_hash_table_foreach(modules, list_independent, independent_modules);
316
317 for(i = 0 ; i < independent_modules->len ; i++) {
318 m = (LttvModule *)independent_modules->pdata[i];
319 while(m->load_count > 0) lttv_module_unload(m);
320 }
321
322 g_ptr_array_free(independent_modules, TRUE);
323 if(g_hash_table_size(modules) != 0) g_warning("cannot unload all modules");
324 }
This page took 0.036317 seconds and 4 git commands to generate.