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