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