| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2003-2004 Michel Dagenais |
| 3 | * 2005 Mathieu Desnoyers |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License Version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 17 | * MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | /* The text dump facility needs to print headers before the trace set and |
| 21 | before each trace, to print each event, and to print statistics |
| 22 | after each trace. */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <lttv/lttv.h> |
| 29 | #include <lttv/option.h> |
| 30 | #include <lttv/module.h> |
| 31 | #include <lttv/hook.h> |
| 32 | #include <lttv/attribute.h> |
| 33 | #include <lttv/iattribute.h> |
| 34 | #include <lttv/stats.h> |
| 35 | #include <lttv/filter.h> |
| 36 | #include <lttv/print.h> |
| 37 | #include <ltt/ltt.h> |
| 38 | #include <ltt/event.h> |
| 39 | #include <ltt/type.h> |
| 40 | #include <ltt/trace.h> |
| 41 | #include <ltt/facility.h> |
| 42 | #include <stdio.h> |
| 43 | |
| 44 | static gboolean |
| 45 | a_field_names, |
| 46 | a_state, |
| 47 | a_cpu_stats, |
| 48 | a_process_stats; |
| 49 | |
| 50 | static char |
| 51 | *a_file_name = NULL; |
| 52 | |
| 53 | static LttvHooks |
| 54 | *before_traceset, |
| 55 | *after_traceset, |
| 56 | *before_trace, |
| 57 | *event_hook; |
| 58 | |
| 59 | |
| 60 | static void |
| 61 | print_tree(FILE *fp, GString *indent, LttvAttribute *tree) |
| 62 | { |
| 63 | int i, nb, saved_length; |
| 64 | |
| 65 | LttvAttribute *subtree; |
| 66 | |
| 67 | LttvAttributeName name; |
| 68 | |
| 69 | LttvAttributeValue value; |
| 70 | |
| 71 | LttvAttributeType type; |
| 72 | |
| 73 | gboolean is_named; |
| 74 | |
| 75 | nb = lttv_attribute_get_number(tree); |
| 76 | for(i = 0 ; i < nb ; i++) { |
| 77 | type = lttv_attribute_get(tree, i, &name, &value, &is_named); |
| 78 | if(is_named) |
| 79 | fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name)); |
| 80 | else |
| 81 | fprintf(fp, "%s%lu: ", indent->str, name); |
| 82 | |
| 83 | switch(type) { |
| 84 | case LTTV_INT: |
| 85 | fprintf(fp, "%d\n", *value.v_int); |
| 86 | break; |
| 87 | case LTTV_UINT: |
| 88 | fprintf(fp, "%u\n", *value.v_uint); |
| 89 | break; |
| 90 | case LTTV_LONG: |
| 91 | fprintf(fp, "%ld\n", *value.v_long); |
| 92 | break; |
| 93 | case LTTV_ULONG: |
| 94 | fprintf(fp, "%lu\n", *value.v_ulong); |
| 95 | break; |
| 96 | case LTTV_FLOAT: |
| 97 | fprintf(fp, "%f\n", (double)*value.v_float); |
| 98 | break; |
| 99 | case LTTV_DOUBLE: |
| 100 | fprintf(fp, "%f\n", *value.v_double); |
| 101 | break; |
| 102 | case LTTV_TIME: |
| 103 | fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec, |
| 104 | value.v_time->tv_nsec); |
| 105 | break; |
| 106 | case LTTV_POINTER: |
| 107 | fprintf(fp, "POINTER\n"); |
| 108 | break; |
| 109 | case LTTV_STRING: |
| 110 | fprintf(fp, "%s\n", *value.v_string); |
| 111 | break; |
| 112 | case LTTV_GOBJECT: |
| 113 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
| 114 | fprintf(fp, "\n"); |
| 115 | subtree = (LttvAttribute *)*(value.v_gobject); |
| 116 | saved_length = indent->len; |
| 117 | indent = g_string_append(indent, " "); |
| 118 | print_tree(fp, indent, subtree); |
| 119 | g_string_truncate(indent, saved_length); |
| 120 | } |
| 121 | else fprintf(fp, "GOBJECT\n"); |
| 122 | break; |
| 123 | case LTTV_NONE: |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static void |
| 131 | print_stats(FILE *fp, LttvTracesetStats *tscs) |
| 132 | { |
| 133 | int i, nb, saved_length; |
| 134 | |
| 135 | LttvTraceset *ts; |
| 136 | |
| 137 | LttvTraceStats *tcs; |
| 138 | |
| 139 | GString *indent; |
| 140 | |
| 141 | LttSystemDescription *desc; |
| 142 | |
| 143 | if(tscs->stats == NULL) return; |
| 144 | indent = g_string_new(""); |
| 145 | fprintf(fp, "Traceset statistics:\n\n"); |
| 146 | print_tree(fp, indent, tscs->stats); |
| 147 | |
| 148 | ts = tscs->parent.parent.ts; |
| 149 | nb = lttv_traceset_number(ts); |
| 150 | |
| 151 | for(i = 0 ; i < nb ; i++) { |
| 152 | tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]); |
| 153 | #if 0 //FIXME |
| 154 | desc = ltt_trace_system_description(tcs->parent.parent.t); |
| 155 | LttTime start_time = ltt_trace_system_description_trace_start_time(desc); |
| 156 | fprintf(fp, "Trace on system %s at time %lu.%09lu :\n", |
| 157 | ltt_trace_system_description_node_name(desc), |
| 158 | start_time.tv_sec, |
| 159 | start_time.tv_nsec); |
| 160 | #endif //FIXME |
| 161 | saved_length = indent->len; |
| 162 | indent = g_string_append(indent, " "); |
| 163 | print_tree(fp, indent, tcs->stats); |
| 164 | g_string_truncate(indent, saved_length); |
| 165 | } |
| 166 | g_string_free(indent, TRUE); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /* Insert the hooks before and after each trace and tracefile, and for each |
| 171 | event. Print a global header. */ |
| 172 | |
| 173 | static FILE *a_file; |
| 174 | |
| 175 | static GString *a_string; |
| 176 | |
| 177 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
| 178 | { |
| 179 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 180 | |
| 181 | g_info("TextDump traceset header"); |
| 182 | |
| 183 | if(a_file_name == NULL) a_file = stdout; |
| 184 | else a_file = fopen(a_file_name, "w"); |
| 185 | |
| 186 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
| 187 | |
| 188 | /* Print the trace set header */ |
| 189 | fprintf(a_file,"Trace set contains %d traces\n\n", |
| 190 | lttv_traceset_number(tc->ts)); |
| 191 | |
| 192 | return FALSE; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
| 197 | { |
| 198 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 199 | |
| 200 | g_info("TextDump traceset footer"); |
| 201 | |
| 202 | fprintf(a_file,"End trace set\n\n"); |
| 203 | |
| 204 | if(LTTV_IS_TRACESET_STATS(tc)) { |
| 205 | lttv_stats_sum_traceset((LttvTracesetStats *)tc); |
| 206 | print_stats(a_file, (LttvTracesetStats *)tc); |
| 207 | } |
| 208 | |
| 209 | if(a_file_name != NULL) fclose(a_file); |
| 210 | |
| 211 | return FALSE; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | static gboolean write_trace_header(void *hook_data, void *call_data) |
| 216 | { |
| 217 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
| 218 | #if 0 //FIXME |
| 219 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
| 220 | |
| 221 | fprintf(a_file," Trace from %s in %s\n%s\n\n", |
| 222 | ltt_trace_system_description_node_name(system), |
| 223 | ltt_trace_system_description_domain_name(system), |
| 224 | ltt_trace_system_description_description(system)); |
| 225 | #endif //0 |
| 226 | return FALSE; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | static int write_event_content(void *hook_data, void *call_data) |
| 231 | { |
| 232 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 233 | |
| 234 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
| 235 | |
| 236 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
| 237 | |
| 238 | LttEvent *e; |
| 239 | |
| 240 | LttvAttributeValue value_filter; |
| 241 | |
| 242 | LttvFilter *filter; |
| 243 | |
| 244 | guint cpu = tfs->cpu; |
| 245 | LttvTraceState *ts = (LttvTraceState*)tfc->t_context; |
| 246 | LttvProcessState *process = ts->running_process[cpu]; |
| 247 | |
| 248 | e = ltt_tracefile_get_event(tfc->tf); |
| 249 | |
| 250 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/lttv_filter", |
| 251 | LTTV_POINTER, &value_filter)); |
| 252 | filter = (LttvFilter*)*(value_filter.v_pointer); |
| 253 | |
| 254 | /* |
| 255 | * call to the filter if available |
| 256 | */ |
| 257 | if(filter->head != NULL) |
| 258 | if(!lttv_filter_tree_parse(filter->head,e,tfc->tf, |
| 259 | tfc->t_context->t,tfc)) |
| 260 | return FALSE; |
| 261 | |
| 262 | lttv_event_to_string(e, a_string, TRUE, a_field_names, tfs); |
| 263 | g_string_append_printf(a_string,"\n"); |
| 264 | |
| 265 | if(a_state) { |
| 266 | g_string_append_printf(a_string, " %s ", |
| 267 | g_quark_to_string(process->state->s)); |
| 268 | } |
| 269 | |
| 270 | fputs(a_string->str, a_file); |
| 271 | return FALSE; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static void init() |
| 276 | { |
| 277 | LttvAttributeValue value; |
| 278 | |
| 279 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 280 | |
| 281 | g_info("Init textDump.c"); |
| 282 | |
| 283 | a_string = g_string_new(""); |
| 284 | |
| 285 | a_file_name = NULL; |
| 286 | lttv_option_add("output", 'o', |
| 287 | "output file where the text is written", |
| 288 | "file name", |
| 289 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
| 290 | |
| 291 | a_field_names = FALSE; |
| 292 | lttv_option_add("field_names", 'l', |
| 293 | "write the field names for each event", |
| 294 | "", |
| 295 | LTTV_OPT_NONE, &a_field_names, NULL, NULL); |
| 296 | |
| 297 | a_state = FALSE; |
| 298 | lttv_option_add("process_state", 's', |
| 299 | "write the pid and state for each event", |
| 300 | "", |
| 301 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
| 302 | |
| 303 | a_cpu_stats = FALSE; |
| 304 | lttv_option_add("cpu_stats", 'c', |
| 305 | "write the per cpu statistics", |
| 306 | "", |
| 307 | LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL); |
| 308 | |
| 309 | a_process_stats = FALSE; |
| 310 | lttv_option_add("process_stats", 'p', |
| 311 | "write the per process statistics", |
| 312 | "", |
| 313 | LTTV_OPT_NONE, &a_process_stats, NULL, NULL); |
| 314 | |
| 315 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event", |
| 316 | LTTV_POINTER, &value)); |
| 317 | g_assert((event_hook = *(value.v_pointer)) != NULL); |
| 318 | lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT); |
| 319 | |
| 320 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
| 321 | LTTV_POINTER, &value)); |
| 322 | g_assert((before_trace = *(value.v_pointer)) != NULL); |
| 323 | lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT); |
| 324 | |
| 325 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
| 326 | LTTV_POINTER, &value)); |
| 327 | g_assert((before_traceset = *(value.v_pointer)) != NULL); |
| 328 | lttv_hooks_add(before_traceset, write_traceset_header, NULL, |
| 329 | LTTV_PRIO_DEFAULT); |
| 330 | |
| 331 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
| 332 | LTTV_POINTER, &value)); |
| 333 | g_assert((after_traceset = *(value.v_pointer)) != NULL); |
| 334 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL, |
| 335 | LTTV_PRIO_DEFAULT); |
| 336 | } |
| 337 | |
| 338 | static void destroy() |
| 339 | { |
| 340 | g_info("Destroy textDump"); |
| 341 | |
| 342 | lttv_option_remove("output"); |
| 343 | |
| 344 | lttv_option_remove("field_names"); |
| 345 | |
| 346 | lttv_option_remove("process_state"); |
| 347 | |
| 348 | lttv_option_remove("cpu_stats"); |
| 349 | |
| 350 | lttv_option_remove("process_stats"); |
| 351 | |
| 352 | g_string_free(a_string, TRUE); |
| 353 | |
| 354 | lttv_hooks_remove_data(event_hook, write_event_content, NULL); |
| 355 | |
| 356 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
| 357 | |
| 358 | lttv_hooks_remove_data(before_trace, write_traceset_header, NULL); |
| 359 | |
| 360 | lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | LTTV_MODULE("textDump", "Print events in a file", \ |
| 365 | "Produce a detailed text printout of a trace", \ |
| 366 | init, destroy, "stats", "batchAnalysis", "option", "print") |
| 367 | |