add --fatal, -f, option : makes glib critical messages abort the program
[lttv.git] / ltt / branches / poly / lttv / lttv / main.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 #include <lttv/hook.h>
21 #include <lttv/module.h>
22 #include <lttv/lttv.h>
23 #include <lttv/iattribute.h>
24 #include <lttv/attribute.h>
25 #include <lttv/option.h>
26 #include <lttv/traceset.h>
27 #include <ltt/trace.h>
28 #include <stdio.h>
29
30
31 /* The main program maintains a few central data structures and relies
32 on modules for the rest. These data structures may be accessed by modules
33 through an exported API */
34
35 static LttvIAttribute *attributes;
36
37 static LttvHooks
38 *before_options,
39 *after_options,
40 *before_main,
41 *after_main;
42
43 static char
44 *a_module,
45 *a_module_path;
46
47 static gboolean
48 a_verbose,
49 a_debug,
50 a_fatal;
51
52 gboolean lttv_profile_memory;
53
54 int lttv_argc;
55
56 char **lttv_argv;
57
58 static void lttv_module_option(void *hook_data);
59
60 static void lttv_module_path_option(void *hook_data);
61
62 static void lttv_verbose(void *hook_data);
63
64 static void lttv_debug(void *hook_data);
65
66 static void lttv_fatal(void *hook_data);
67
68 static void lttv_help(void *hook_data);
69
70 /* This is the handler to specify when we dont need all the debugging
71 messages. It receives the message and does nothing. */
72
73 void ignore_and_drop_message(const gchar *log_domain, GLogLevelFlags log_level,
74 const gchar *message, gpointer user_data) {
75 }
76
77
78 /* Since everything is done in modules, the main program only takes care
79 of the infrastructure. */
80
81 int main(int argc, char **argv) {
82
83 int i;
84
85 char
86 *profile_memory_short_option = "-M",
87 *profile_memory_long_option = "--memory";
88
89 gboolean profile_memory = FALSE;
90
91 LttvAttributeValue value;
92
93 lttv_argc = argc;
94 lttv_argv = argv;
95
96 /* Before anything else, check if memory profiling is requested */
97
98 for(i = 1 ; i < argc ; i++) {
99 if(*(argv[i]) != '-') break;
100 if(strcmp(argv[i], profile_memory_short_option) == 0 ||
101 strcmp(argv[i], profile_memory_long_option) == 0) {
102 g_mem_set_vtable(glib_mem_profiler_table);
103 g_message("Memory summary before main");
104 g_mem_profile();
105 profile_memory = TRUE;
106 break;
107 }
108 }
109
110
111 /* Initialize glib and by default ignore info and debug messages */
112
113 g_type_init();
114 //g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
115 g_log_set_handler(NULL, G_LOG_LEVEL_INFO, ignore_and_drop_message, NULL);
116 g_log_set_handler(NULL, G_LOG_LEVEL_DEBUG, ignore_and_drop_message, NULL);
117
118
119 /* Have an attributes subtree to store hooks to be registered by modules. */
120
121 attributes = LTTV_IATTRIBUTE(g_object_new(LTTV_ATTRIBUTE_TYPE, NULL));
122
123 before_options = lttv_hooks_new();
124 after_options = lttv_hooks_new();
125 before_main = lttv_hooks_new();
126 after_main = lttv_hooks_new();
127
128
129 /* Create a number of hooks lists */
130
131 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/options/before",
132 LTTV_POINTER, &value));
133 *(value.v_pointer) = before_options;
134 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/options/after",
135 LTTV_POINTER, &value));
136 *(value.v_pointer) = after_options;
137 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/main/before",
138 LTTV_POINTER, &value));
139 *(value.v_pointer) = before_main;
140 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/main/after",
141 LTTV_POINTER, &value));
142 *(value.v_pointer) = after_main;
143
144
145 /* Initialize the command line options processing */
146
147 GError *error = NULL;
148
149 LttvModule *module_module = lttv_module_require("module", &error);
150 if(error != NULL) g_error(error->message);
151 LttvModule *module_option = lttv_module_require("option", &error);
152 if(error != NULL) g_error(error->message);
153
154 /* Initialize the module loading */
155
156 lttv_library_path_add(PACKAGE_PLUGIN_DIR);
157
158
159 /* Add some built-in options */
160
161 lttv_option_add("module",'m', "load a module", "name of module to load",
162 LTTV_OPT_STRING, &a_module, lttv_module_option, NULL);
163
164 lttv_option_add("modules-path", 'L',
165 "add a directory to the module search path",
166 "directory to add to the path", LTTV_OPT_STRING, &a_module_path,
167 lttv_module_path_option, NULL);
168
169 lttv_option_add("help",'h', "basic help", "none",
170 LTTV_OPT_NONE, NULL, lttv_help, NULL);
171
172 a_verbose = FALSE;
173 lttv_option_add("verbose",'v', "print information messages", "none",
174 LTTV_OPT_NONE, NULL, lttv_verbose, NULL);
175
176 a_debug = FALSE;
177 lttv_option_add("debug",'d', "print debugging messages", "none",
178 LTTV_OPT_NONE, NULL, lttv_debug, NULL);
179
180 a_fatal = FALSE;
181 lttv_option_add("fatal",'f', "make critical messages fatal",
182 "none",
183 LTTV_OPT_NONE, NULL, lttv_fatal, NULL);
184
185 lttv_profile_memory = FALSE;
186 lttv_option_add(profile_memory_long_option + 2,
187 profile_memory_short_option[1], "print memory information", "none",
188 LTTV_OPT_NONE, &lttv_profile_memory, NULL, NULL);
189
190
191 /* Process the options */
192
193 lttv_hooks_call(before_options, NULL);
194 lttv_option_parse(argc, argv);
195 lttv_hooks_call(after_options, NULL);
196
197
198 /* Memory profiling to be useful must be activated as early as possible */
199
200 if(profile_memory != lttv_profile_memory)
201 g_error("Memory profiling options must appear before other options");
202
203
204 /* Do the main work */
205
206 lttv_hooks_call(before_main, NULL);
207 lttv_hooks_call(after_main, NULL);
208
209
210 /* Clean up everything */
211
212 lttv_module_release(module_option);
213 lttv_module_release(module_module);
214
215 lttv_hooks_destroy(before_options);
216 lttv_hooks_destroy(after_options);
217 lttv_hooks_destroy(before_main);
218 lttv_hooks_destroy(after_main);
219 g_object_unref(attributes);
220
221 if(profile_memory) {
222 g_message("Memory summary after main");
223 g_mem_profile();
224 }
225 return 0;
226 }
227
228
229 LttvAttribute *lttv_global_attributes()
230 {
231 return (LttvAttribute*)attributes;
232 }
233
234
235 void lttv_module_option(void *hook_data)
236 {
237 GError *error = NULL;
238
239 lttv_module_require(a_module, &error);
240 if(error != NULL) g_error(error->message);
241 }
242
243
244 void lttv_module_path_option(void *hook_data)
245 {
246 lttv_library_path_add(a_module_path);
247 }
248
249
250 void lttv_verbose(void *hook_data)
251 {
252 g_log_set_handler(NULL, G_LOG_LEVEL_INFO, g_log_default_handler, NULL);
253 g_info("Logging set to include INFO level messages");
254 }
255
256 void lttv_debug(void *hook_data)
257 {
258 g_log_set_handler(NULL, G_LOG_LEVEL_DEBUG, g_log_default_handler, NULL);
259 g_info("Logging set to include DEBUG level messages");
260 }
261
262 void lttv_fatal(void *hook_data)
263 {
264 //g_log_set_always_fatal(G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
265 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
266 g_info("Critical log from glib will abort execution");
267 }
268
269 void lttv_help(void *hook_data)
270 {
271 printf("Linux Trace Toolkit Visualizer\n");
272 printf("\n");
273 lttv_option_show_help();
274 printf("\n");
275 }
276
277 /*
278
279 - Define formally traceset/trace in the GUI for the user and decide how
280 trace/traceset sharing goes in the application.
281
282 - Use appropriately the new functions in time.h
283
284 - remove the separate tracefiles (control/per cpu) arrays/loops in context.
285
286 - split processTrace into context.c and processTrace.c
287
288 - check spelling conventions.
289
290 - get all the copyright notices.
291
292 - remove all the warnings.
293
294 - get all the .h files properly doxygen commented to produce useful documents.
295
296 - have an intro/architecture document.
297
298 - write a tutorial */
This page took 0.035402 seconds and 5 git commands to generate.