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