disable schedchange test : not meaningful if there is missing information
[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
ffd54a90 63 char *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:
63c35f6c 70 g_string_append_printf(s, " %lld", ltt_event_get_long_int(e,f));
ffd54a90 71 break;
dc877563 72
ffd54a90 73 case LTT_UINT:
63c35f6c 74 g_string_append_printf(s, " %llu", ltt_event_get_long_unsigned(e,f));
ffd54a90 75 break;
dc877563 76
ffd54a90 77 case LTT_FLOAT:
78 g_string_append_printf(s, " %g", ltt_event_get_double(e,f));
79 break;
dc877563 80
ffd54a90 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,
c6bc9cb9 87 ltt_event_get_unsigned(e,f)-1));
ffd54a90 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);
c432246e 107 if(field_names) {
ffd54a90 108 ltt_type_member_type(type, i, &name);
c432246e 109 g_string_append_printf(s, " %s = ", name);
ffd54a90 110 }
111 print_field(e, element, s, field_names);
112 }
113 g_string_append_printf(s, " }");
114 break;
ba6f11f1 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
ffd54a90 130 }
48f6f3c2 131}
132
133
ba6f11f1 134void lttv_event_to_string(LttEvent *e, GString *s,
3d27549e 135 gboolean mandatory_fields, gboolean field_names, LttvTracefileState *tfs)
136{
ffd54a90 137 LttFacility *facility;
48f6f3c2 138
ffd54a90 139 LttEventType *event_type;
48f6f3c2 140
ffd54a90 141 LttType *type;
142
143 LttField *field;
48f6f3c2 144
ffd54a90 145 LttTime time;
48f6f3c2 146
ffd54a90 147 g_string_set_size(s,0);
dc877563 148
ffd54a90 149 facility = ltt_event_facility(e);
150 event_type = ltt_event_eventtype(e);
151 field = ltt_event_field(e);
48f6f3c2 152
ffd54a90 153 if(mandatory_fields) {
154 time = ltt_event_time(e);
b445142a 155 g_string_append_printf(s,"%s.%s: %ld.%09ld (%s)",
156 ltt_facility_name(facility),
ffd54a90 157 ltt_eventtype_name(event_type), (long)time.tv_sec, time.tv_nsec,
b445142a 158 g_quark_to_string(tfs->cpu_name));
c432246e 159 /* Print the process id and the state/interrupt type of the process */
07bb8c8f 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));
ffd54a90 163 }
164
c6bc9cb9 165 if(field)
166 print_field(e, field, s, field_names);
ffd54a90 167}
48f6f3c2 168
169
b445142a 170static void
171print_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:
ba6f11f1 208 fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec,
b445142a 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
235static void
236print_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);
ba6f11f1 259 LttTime start_time = ltt_trace_system_description_trace_start_time(desc);
260 fprintf(fp, "Trace on system %s at time %lu.%09lu :\n",
a5dcde2f 261 ltt_trace_system_description_node_name(desc),
ba6f11f1 262 start_time.tv_sec,
263 start_time.tv_nsec);
b445142a 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
dc877563 273/* Insert the hooks before and after each trace and tracefile, and for each
274 event. Print a global header. */
275
276static FILE *a_file;
48f6f3c2 277
dc877563 278static GString *a_string;
279
ffd54a90 280static gboolean write_traceset_header(void *hook_data, void *call_data)
48f6f3c2 281{
dc877563 282 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 283
b445142a 284 g_info("TextDump traceset header");
285
dc877563 286 if(a_file_name == NULL) a_file = stdout;
287 else a_file = fopen(a_file_name, "w");
48f6f3c2 288
dc877563 289 if(a_file == NULL) g_error("cannot open file %s", a_file_name);
48f6f3c2 290
dc877563 291 /* Print the trace set header */
292 fprintf(a_file,"Trace set contains %d traces\n\n",
ffd54a90 293 lttv_traceset_number(tc->ts));
48f6f3c2 294
dc877563 295 return FALSE;
48f6f3c2 296}
297
298
ffd54a90 299static gboolean write_traceset_footer(void *hook_data, void *call_data)
48f6f3c2 300{
dc877563 301 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 302
b445142a 303 g_info("TextDump traceset footer");
304
dc877563 305 fprintf(a_file,"End trace set\n\n");
48f6f3c2 306
b445142a 307 if(LTTV_IS_TRACESET_STATS(tc)) {
f95bc830 308 lttv_stats_sum_traceset((LttvTracesetStats *)tc);
b445142a 309 print_stats(a_file, (LttvTracesetStats *)tc);
310 }
311
ffd54a90 312 if(a_file_name != NULL) fclose(a_file);
313
dc877563 314 return FALSE;
48f6f3c2 315}
316
317
dc877563 318static gboolean write_trace_header(void *hook_data, void *call_data)
48f6f3c2 319{
dc877563 320 LttvTraceContext *tc = (LttvTraceContext *)call_data;
321
322 LttSystemDescription *system = ltt_trace_system_description(tc->t);
48f6f3c2 323
a5dcde2f 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));
dc877563 328 return FALSE;
48f6f3c2 329}
330
331
dc877563 332static int write_event_content(void *hook_data, void *call_data)
48f6f3c2 333{
8bf54995 334 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
335
dc877563 336 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
48f6f3c2 337
dc877563 338 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
48f6f3c2 339
dc877563 340 LttEvent *e;
48f6f3c2 341
8bf54995 342 LttvAttributeValue value_filter;
343
344 LttvFilter *filter;
345
dc877563 346 e = tfc->e;
48f6f3c2 347
8bf54995 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
cec3d7b0 353 /*
354 * call to the filter if available
355 */
8bf54995 356 if(!lttv_filter_tree_parse(filter->head,e,tfc->tf,tfc->t_context->t,tfs->process,tfc)) {
8ff6243c 357 return FALSE;
358 }
cec3d7b0 359
ba6f11f1 360 lttv_event_to_string(e, a_string, TRUE, a_field_names, tfs);
c6bc9cb9 361 g_string_append_printf(a_string,"\n");
dc877563 362
363 if(a_state) {
9d0bb8d1 364 g_string_append_printf(a_string, " %s ",
ffd54a90 365 g_quark_to_string(tfs->process->state->s));
dc877563 366 }
48f6f3c2 367
ffd54a90 368 fputs(a_string->str, a_file);
dc877563 369 return FALSE;
48f6f3c2 370}
371
372
08b1c66e 373static void init()
48f6f3c2 374{
ffd54a90 375 LttvAttributeValue value;
48f6f3c2 376
ffd54a90 377 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
48f6f3c2 378
b445142a 379 g_info("Init textDump.c");
9402662d 380
c6bc9cb9 381 a_string = g_string_new("");
382
ffd54a90 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);
48f6f3c2 388
ffd54a90 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);
48f6f3c2 394
ffd54a90 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);
dc877563 400
b445142a 401 a_cpu_stats = FALSE;
9d0bb8d1 402 lttv_option_add("cpu_stats", 'c',
b445142a 403 "write the per cpu statistics",
404 "",
405 LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL);
406
407 a_process_stats = FALSE;
9d0bb8d1 408 lttv_option_add("process_stats", 'p',
b445142a 409 "write the per process statistics",
410 "",
411 LTTV_OPT_NONE, &a_process_stats, NULL, NULL);
412
12c59c3d 413 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event",
ffd54a90 414 LTTV_POINTER, &value));
12c59c3d 415 g_assert((event_hook = *(value.v_pointer)) != NULL);
416 lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT);
dc877563 417
ffd54a90 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);
12c59c3d 421 lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT);
dc877563 422
ffd54a90 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);
12c59c3d 426 lttv_hooks_add(before_traceset, write_traceset_header, NULL,
427 LTTV_PRIO_DEFAULT);
dc877563 428
ffd54a90 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);
12c59c3d 432 lttv_hooks_add(after_traceset, write_traceset_footer, NULL,
433 LTTV_PRIO_DEFAULT);
ffd54a90 434}
48f6f3c2 435
48f6f3c2 436
08b1c66e 437static void destroy()
ffd54a90 438{
b445142a 439 g_info("Destroy textDump");
440
ffd54a90 441 lttv_option_remove("output");
48f6f3c2 442
ffd54a90 443 lttv_option_remove("field_names");
48f6f3c2 444
ffd54a90 445 lttv_option_remove("process_state");
48f6f3c2 446
b445142a 447 lttv_option_remove("cpu_stats");
448
449 lttv_option_remove("process_stats");
450
c6bc9cb9 451 g_string_free(a_string, TRUE);
452
12c59c3d 453 lttv_hooks_remove_data(event_hook, write_event_content, NULL);
48f6f3c2 454
ffd54a90 455 lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
48f6f3c2 456
ffd54a90 457 lttv_hooks_remove_data(before_trace, write_traceset_header, NULL);
48f6f3c2 458
ffd54a90 459 lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL);
48f6f3c2 460}
461
462
08b1c66e 463LTTV_MODULE("textDump", "Print events in a file", \
464 "Produce a detailed text printout of a trace", \
0c5dbe3b 465 init, destroy, "stats", "batchAnalysis", "option")
48f6f3c2 466
This page took 0.056005 seconds and 4 git commands to generate.