| 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/trace.h> |
| 40 | #include <stdio.h> |
| 41 | |
| 42 | static gboolean |
| 43 | a_noevent, |
| 44 | a_no_field_names, |
| 45 | a_state, |
| 46 | a_cpu_stats, |
| 47 | a_process_stats, |
| 48 | a_path_output; |
| 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_path_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 | saved_length = indent->len; |
| 76 | nb = lttv_attribute_get_number(tree); |
| 77 | for(i = 0 ; i < nb ; i++) { |
| 78 | type = lttv_attribute_get(tree, i, &name, &value, &is_named); |
| 79 | if(is_named) { |
| 80 | g_string_sprintfa(indent, "/%s", g_quark_to_string(name)); |
| 81 | } else { |
| 82 | g_string_sprintfa(indent, "/%s", name); |
| 83 | } |
| 84 | |
| 85 | switch(type) { |
| 86 | case LTTV_INT: |
| 87 | fprintf(fp, "%s: %d\n", indent->str, *value.v_int); |
| 88 | break; |
| 89 | case LTTV_UINT: |
| 90 | fprintf(fp, "%s: %d\n", indent->str, *value.v_int); |
| 91 | break; |
| 92 | case LTTV_LONG: |
| 93 | fprintf(fp, "%s: %ld\n", indent->str, *value.v_ulong); |
| 94 | break; |
| 95 | case LTTV_ULONG: |
| 96 | fprintf(fp, "%s: %lu\n", indent->str, *value.v_ulong); |
| 97 | break; |
| 98 | case LTTV_FLOAT: |
| 99 | fprintf(fp, "%s: %f\n", indent->str, (double) *value.v_float); |
| 100 | break; |
| 101 | case LTTV_DOUBLE: |
| 102 | fprintf(fp, "%s: %f\n", indent->str, *value.v_double); |
| 103 | break; |
| 104 | case LTTV_TIME: |
| 105 | fprintf(fp, "%s: %lu.%09lu\n", indent->str, value.v_time->tv_sec, value.v_time->tv_nsec); |
| 106 | break; |
| 107 | case LTTV_POINTER: |
| 108 | fprintf(fp, "%s: POINTER\n", indent->str); |
| 109 | break; |
| 110 | case LTTV_STRING: |
| 111 | fprintf(fp, "%s: %s\n", indent->str, *value.v_string); |
| 112 | break; |
| 113 | case LTTV_GOBJECT: |
| 114 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
| 115 | subtree = (LttvAttribute*) *(value.v_gobject); |
| 116 | print_path_tree(fp, indent, subtree); |
| 117 | } else { |
| 118 | fprintf(fp, "%s: GOBJECT\n", indent->str); |
| 119 | } |
| 120 | break; |
| 121 | case LTTV_NONE: |
| 122 | break; |
| 123 | } |
| 124 | g_string_truncate(indent, saved_length); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | print_tree(FILE *fp, GString *indent, LttvAttribute *tree) |
| 130 | { |
| 131 | int i, nb, saved_length; |
| 132 | |
| 133 | LttvAttribute *subtree; |
| 134 | |
| 135 | LttvAttributeName name; |
| 136 | |
| 137 | LttvAttributeValue value; |
| 138 | |
| 139 | LttvAttributeType type; |
| 140 | |
| 141 | gboolean is_named; |
| 142 | |
| 143 | nb = lttv_attribute_get_number(tree); |
| 144 | for(i = 0 ; i < nb ; i++) { |
| 145 | type = lttv_attribute_get(tree, i, &name, &value, &is_named); |
| 146 | if(is_named) |
| 147 | fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name)); |
| 148 | else |
| 149 | fprintf(fp, "%s%lu: ", indent->str, name); |
| 150 | |
| 151 | switch(type) { |
| 152 | case LTTV_INT: |
| 153 | fprintf(fp, "%d\n", *value.v_int); |
| 154 | break; |
| 155 | case LTTV_UINT: |
| 156 | fprintf(fp, "%u\n", *value.v_uint); |
| 157 | break; |
| 158 | case LTTV_LONG: |
| 159 | fprintf(fp, "%ld\n", *value.v_long); |
| 160 | break; |
| 161 | case LTTV_ULONG: |
| 162 | fprintf(fp, "%lu\n", *value.v_ulong); |
| 163 | break; |
| 164 | case LTTV_FLOAT: |
| 165 | fprintf(fp, "%f\n", (double)*value.v_float); |
| 166 | break; |
| 167 | case LTTV_DOUBLE: |
| 168 | fprintf(fp, "%f\n", *value.v_double); |
| 169 | break; |
| 170 | case LTTV_TIME: |
| 171 | fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec, |
| 172 | value.v_time->tv_nsec); |
| 173 | break; |
| 174 | case LTTV_POINTER: |
| 175 | fprintf(fp, "POINTER\n"); |
| 176 | break; |
| 177 | case LTTV_STRING: |
| 178 | fprintf(fp, "%s\n", *value.v_string); |
| 179 | break; |
| 180 | case LTTV_GOBJECT: |
| 181 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
| 182 | fprintf(fp, "\n"); |
| 183 | subtree = (LttvAttribute *)*(value.v_gobject); |
| 184 | saved_length = indent->len; |
| 185 | indent = g_string_append(indent, " "); |
| 186 | print_tree(fp, indent, subtree); |
| 187 | g_string_truncate(indent, saved_length); |
| 188 | } |
| 189 | else fprintf(fp, "GOBJECT\n"); |
| 190 | break; |
| 191 | case LTTV_NONE: |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | print_stats(FILE *fp, LttvTracesetStats *tscs) |
| 199 | { |
| 200 | int i, nb, saved_length; |
| 201 | |
| 202 | LttvTraceset *ts; |
| 203 | |
| 204 | LttvTraceStats *tcs; |
| 205 | |
| 206 | GString *indent; |
| 207 | |
| 208 | LttSystemDescription *desc; |
| 209 | |
| 210 | if(tscs->stats == NULL) return; |
| 211 | indent = g_string_new(""); |
| 212 | fprintf(fp, "Traceset statistics:\n\n"); |
| 213 | if(a_path_output) { |
| 214 | print_path_tree(fp, indent, tscs->stats); |
| 215 | } else { |
| 216 | print_tree(fp, indent, tscs->stats); |
| 217 | } |
| 218 | |
| 219 | ts = tscs->parent.parent.ts; |
| 220 | nb = lttv_traceset_number(ts); |
| 221 | |
| 222 | for(i = 0 ; i < nb ; i++) { |
| 223 | tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]); |
| 224 | #if 0 //FIXME |
| 225 | desc = ltt_trace_system_description(tcs->parent.parent.t); |
| 226 | LttTime start_time = ltt_trace_system_description_trace_start_time(desc); |
| 227 | fprintf(fp, "Trace on system %s at time %lu.%09lu :\n", |
| 228 | ltt_trace_system_description_node_name(desc), |
| 229 | start_time.tv_sec, |
| 230 | start_time.tv_nsec); |
| 231 | #endif //FIXME |
| 232 | saved_length = indent->len; |
| 233 | if(a_path_output) { |
| 234 | g_string_sprintfa(indent, "/trace%i", i); |
| 235 | print_path_tree(fp, indent, tcs->stats); |
| 236 | } else { |
| 237 | g_string_append(indent, " "); |
| 238 | print_tree(fp, indent, tcs->stats); |
| 239 | } |
| 240 | g_string_truncate(indent, saved_length); |
| 241 | } |
| 242 | g_string_free(indent, TRUE); |
| 243 | } |
| 244 | |
| 245 | /* Insert the hooks before and after each trace and tracefile, and for each |
| 246 | event. Print a global header. */ |
| 247 | |
| 248 | static FILE *a_file; |
| 249 | |
| 250 | static GString *a_string; |
| 251 | |
| 252 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
| 253 | { |
| 254 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 255 | |
| 256 | g_info("TextDump traceset header"); |
| 257 | |
| 258 | if(a_file_name == NULL) a_file = stdout; |
| 259 | else a_file = fopen(a_file_name, "w"); |
| 260 | |
| 261 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
| 262 | |
| 263 | /* Print the trace set header */ |
| 264 | fprintf(a_file,"Trace set contains %d traces\n\n", |
| 265 | lttv_traceset_number(tc->ts)); |
| 266 | |
| 267 | return FALSE; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
| 272 | { |
| 273 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 274 | |
| 275 | g_info("TextDump traceset footer"); |
| 276 | |
| 277 | fprintf(a_file,"End trace set\n\n"); |
| 278 | |
| 279 | if(LTTV_IS_TRACESET_STATS(tc)) { |
| 280 | lttv_stats_sum_traceset((LttvTracesetStats *)tc, ltt_time_infinite); |
| 281 | print_stats(a_file, (LttvTracesetStats *)tc); |
| 282 | } |
| 283 | |
| 284 | if(a_file_name != NULL) fclose(a_file); |
| 285 | |
| 286 | return FALSE; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | static gboolean write_trace_header(void *hook_data, void *call_data) |
| 291 | { |
| 292 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
| 293 | #if 0 //FIXME |
| 294 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
| 295 | |
| 296 | fprintf(a_file," Trace from %s in %s\n%s\n\n", |
| 297 | ltt_trace_system_description_node_name(system), |
| 298 | ltt_trace_system_description_domain_name(system), |
| 299 | ltt_trace_system_description_description(system)); |
| 300 | #endif //0 |
| 301 | return FALSE; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | static int write_event_content(void *hook_data, void *call_data) |
| 306 | { |
| 307 | gboolean result; |
| 308 | |
| 309 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 310 | |
| 311 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
| 312 | |
| 313 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
| 314 | |
| 315 | LttEvent *e; |
| 316 | |
| 317 | LttvAttributeValue value_filter; |
| 318 | |
| 319 | LttvFilter *filter; |
| 320 | |
| 321 | guint cpu = tfs->cpu; |
| 322 | LttvTraceState *ts = (LttvTraceState*)tfc->t_context; |
| 323 | LttvProcessState *process = ts->running_process[cpu]; |
| 324 | |
| 325 | if (a_noevent) |
| 326 | return FALSE; |
| 327 | |
| 328 | e = ltt_tracefile_get_event(tfc->tf); |
| 329 | |
| 330 | result = lttv_iattribute_find_by_path(attributes, "filter/lttv_filter", |
| 331 | LTTV_POINTER, &value_filter); |
| 332 | g_assert(result); |
| 333 | filter = (LttvFilter*)*(value_filter.v_pointer); |
| 334 | |
| 335 | /* |
| 336 | * call to the filter if available |
| 337 | */ |
| 338 | if(filter->head != NULL) |
| 339 | if(!lttv_filter_tree_parse(filter->head,e,tfc->tf, |
| 340 | tfc->t_context->t,tfc,NULL,NULL)) |
| 341 | return FALSE; |
| 342 | |
| 343 | lttv_event_to_string(e, a_string, TRUE, !a_no_field_names, tfs); |
| 344 | |
| 345 | if(a_state) { |
| 346 | g_string_append_printf(a_string, " %s ", |
| 347 | g_quark_to_string(process->state->s)); |
| 348 | } |
| 349 | |
| 350 | g_string_append_printf(a_string,"\n"); |
| 351 | |
| 352 | fputs(a_string->str, a_file); |
| 353 | return FALSE; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | static void init() |
| 358 | { |
| 359 | gboolean result; |
| 360 | |
| 361 | LttvAttributeValue value; |
| 362 | |
| 363 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 364 | |
| 365 | g_info("Init textDump.c"); |
| 366 | |
| 367 | a_string = g_string_new(""); |
| 368 | |
| 369 | a_file_name = NULL; |
| 370 | lttv_option_add("output", 'o', |
| 371 | "output file where the text is written", |
| 372 | "file name", |
| 373 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
| 374 | |
| 375 | a_noevent = FALSE; |
| 376 | lttv_option_add("noevent", 'n', |
| 377 | "disable event printout", |
| 378 | "", |
| 379 | LTTV_OPT_NONE, &a_noevent, NULL, NULL); |
| 380 | |
| 381 | a_no_field_names = FALSE; |
| 382 | lttv_option_add("field_names", 's', |
| 383 | "do not write the field names for each event", |
| 384 | "", |
| 385 | LTTV_OPT_NONE, &a_no_field_names, NULL, NULL); |
| 386 | |
| 387 | a_state = FALSE; |
| 388 | lttv_option_add("process_state", 'r', |
| 389 | "write the pid and state for each event", |
| 390 | "", |
| 391 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
| 392 | |
| 393 | a_cpu_stats = FALSE; |
| 394 | lttv_option_add("cpu_stats", 'c', |
| 395 | "write the per cpu statistics", |
| 396 | "", |
| 397 | LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL); |
| 398 | |
| 399 | a_process_stats = FALSE; |
| 400 | lttv_option_add("process_stats", 'p', |
| 401 | "write the per process statistics", |
| 402 | "", |
| 403 | LTTV_OPT_NONE, &a_process_stats, NULL, NULL); |
| 404 | |
| 405 | a_path_output = FALSE; |
| 406 | lttv_option_add("path_output", 'a', |
| 407 | "print the process stats in path format, for easy grepping", |
| 408 | "", |
| 409 | LTTV_OPT_NONE, &a_path_output, NULL, NULL); |
| 410 | |
| 411 | result = lttv_iattribute_find_by_path(attributes, "hooks/event", |
| 412 | LTTV_POINTER, &value); |
| 413 | g_assert(result); |
| 414 | event_hook = *(value.v_pointer); |
| 415 | g_assert(event_hook); |
| 416 | lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT); |
| 417 | |
| 418 | result = lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
| 419 | LTTV_POINTER, &value); |
| 420 | g_assert(result); |
| 421 | before_trace = *(value.v_pointer); |
| 422 | g_assert(before_trace); |
| 423 | lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT); |
| 424 | |
| 425 | result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
| 426 | LTTV_POINTER, &value); |
| 427 | g_assert(result); |
| 428 | before_traceset = *(value.v_pointer); |
| 429 | g_assert(before_traceset); |
| 430 | lttv_hooks_add(before_traceset, write_traceset_header, NULL, |
| 431 | LTTV_PRIO_DEFAULT); |
| 432 | |
| 433 | result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
| 434 | LTTV_POINTER, &value); |
| 435 | g_assert(result); |
| 436 | after_traceset = *(value.v_pointer); |
| 437 | g_assert(after_traceset); |
| 438 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL, |
| 439 | LTTV_PRIO_DEFAULT); |
| 440 | } |
| 441 | |
| 442 | static void destroy() |
| 443 | { |
| 444 | g_info("Destroy textDump"); |
| 445 | |
| 446 | lttv_option_remove("noevent"); |
| 447 | |
| 448 | lttv_option_remove("output"); |
| 449 | |
| 450 | lttv_option_remove("field_names"); |
| 451 | |
| 452 | lttv_option_remove("process_state"); |
| 453 | |
| 454 | lttv_option_remove("cpu_stats"); |
| 455 | |
| 456 | lttv_option_remove("process_stats"); |
| 457 | |
| 458 | lttv_option_remove("path_output"); |
| 459 | |
| 460 | g_string_free(a_string, TRUE); |
| 461 | |
| 462 | lttv_hooks_remove_data(event_hook, write_event_content, NULL); |
| 463 | |
| 464 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
| 465 | |
| 466 | lttv_hooks_remove_data(before_traceset, write_traceset_header, NULL); |
| 467 | |
| 468 | lttv_hooks_remove_data(after_traceset, write_traceset_footer, NULL); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | LTTV_MODULE("textDump", "Print events in a file", \ |
| 473 | "Produce a detailed text printout of a trace", \ |
| 474 | init, destroy, "stats", "batchAnalysis", "option", "print") |
| 475 | |