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