add noevent option to text dump
[lttv.git] / trunk / lttv / 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
8e680509 3 * 2005 Mathieu Desnoyers
9c312311 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
48f6f3c2 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
4e4d11b3 24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
996acd92 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>
b445142a 34#include <lttv/stats.h>
cec3d7b0 35#include <lttv/filter.h>
8e680509 36#include <lttv/print.h>
ffd54a90 37#include <ltt/ltt.h>
38#include <ltt/event.h>
ffd54a90 39#include <ltt/trace.h>
40#include <stdio.h>
996acd92 41
dc877563 42static gboolean
7e5fe4d0 43 a_noevent,
b5ad5a5d 44 a_no_field_names,
b445142a 45 a_state,
46 a_cpu_stats,
7a36589d 47 a_process_stats,
48 a_path_output;
dc877563 49
50static char
b445142a 51 *a_file_name = NULL;
dc877563 52
53static LttvHooks
54 *before_traceset,
55 *after_traceset,
56 *before_trace,
12c59c3d 57 *event_hook;
dc877563 58
48f6f3c2 59
b445142a 60static void
7a36589d 61print_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
128static void
b445142a 129print_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
c0cb4d12 141 gboolean is_named;
142
b445142a 143 nb = lttv_attribute_get_number(tree);
144 for(i = 0 ; i < nb ; i++) {
c0cb4d12 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);
b445142a 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:
ba6f11f1 171 fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec,
b445142a 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;
4ec0c904 185 indent = g_string_append(indent, " ");
b445142a 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
b445142a 197static void
198print_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");
7a36589d 213 if(a_path_output) {
214 print_path_tree(fp, indent, tscs->stats);
215 } else {
216 print_tree(fp, indent, tscs->stats);
217 }
b445142a 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]);
9eb6aa7a 224#if 0 //FIXME
b445142a 225 desc = ltt_trace_system_description(tcs->parent.parent.t);
ba6f11f1 226 LttTime start_time = ltt_trace_system_description_trace_start_time(desc);
227 fprintf(fp, "Trace on system %s at time %lu.%09lu :\n",
a5dcde2f 228 ltt_trace_system_description_node_name(desc),
ba6f11f1 229 start_time.tv_sec,
230 start_time.tv_nsec);
9eb6aa7a 231#endif //FIXME
b445142a 232 saved_length = indent->len;
7a36589d 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 }
b445142a 240 g_string_truncate(indent, saved_length);
241 }
242 g_string_free(indent, TRUE);
243}
244
dc877563 245/* Insert the hooks before and after each trace and tracefile, and for each
246 event. Print a global header. */
247
248static FILE *a_file;
48f6f3c2 249
dc877563 250static GString *a_string;
251
ffd54a90 252static gboolean write_traceset_header(void *hook_data, void *call_data)
48f6f3c2 253{
dc877563 254 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 255
b445142a 256 g_info("TextDump traceset header");
257
dc877563 258 if(a_file_name == NULL) a_file = stdout;
259 else a_file = fopen(a_file_name, "w");
48f6f3c2 260
dc877563 261 if(a_file == NULL) g_error("cannot open file %s", a_file_name);
48f6f3c2 262
dc877563 263 /* Print the trace set header */
264 fprintf(a_file,"Trace set contains %d traces\n\n",
ffd54a90 265 lttv_traceset_number(tc->ts));
48f6f3c2 266
dc877563 267 return FALSE;
48f6f3c2 268}
269
270
ffd54a90 271static gboolean write_traceset_footer(void *hook_data, void *call_data)
48f6f3c2 272{
dc877563 273 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 274
b445142a 275 g_info("TextDump traceset footer");
276
dc877563 277 fprintf(a_file,"End trace set\n\n");
48f6f3c2 278
b445142a 279 if(LTTV_IS_TRACESET_STATS(tc)) {
b91e751b 280 lttv_stats_sum_traceset((LttvTracesetStats *)tc, ltt_time_infinite);
b445142a 281 print_stats(a_file, (LttvTracesetStats *)tc);
282 }
283
ffd54a90 284 if(a_file_name != NULL) fclose(a_file);
285
dc877563 286 return FALSE;
48f6f3c2 287}
288
289
dc877563 290static gboolean write_trace_header(void *hook_data, void *call_data)
48f6f3c2 291{
dc877563 292 LttvTraceContext *tc = (LttvTraceContext *)call_data;
9eb6aa7a 293#if 0 //FIXME
dc877563 294 LttSystemDescription *system = ltt_trace_system_description(tc->t);
48f6f3c2 295
a5dcde2f 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));
9eb6aa7a 300#endif //0
dc877563 301 return FALSE;
48f6f3c2 302}
303
304
dc877563 305static int write_event_content(void *hook_data, void *call_data)
48f6f3c2 306{
d5160b2b 307 gboolean result;
308
8bf54995 309 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
310
dc877563 311 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
48f6f3c2 312
dc877563 313 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
48f6f3c2 314
dc877563 315 LttEvent *e;
48f6f3c2 316
8bf54995 317 LttvAttributeValue value_filter;
318
319 LttvFilter *filter;
320
ae3d0f50 321 guint cpu = tfs->cpu;
d730b5c8 322 LttvTraceState *ts = (LttvTraceState*)tfc->t_context;
323 LttvProcessState *process = ts->running_process[cpu];
48f6f3c2 324
d730b5c8 325 e = ltt_tracefile_get_event(tfc->tf);
8bf54995 326
d5160b2b 327 result = lttv_iattribute_find_by_path(attributes, "filter/lttv_filter",
328 LTTV_POINTER, &value_filter);
329 g_assert(result);
8bf54995 330 filter = (LttvFilter*)*(value_filter.v_pointer);
331
cec3d7b0 332 /*
333 * call to the filter if available
334 */
0bc23ba9 335 if(filter->head != NULL)
d730b5c8 336 if(!lttv_filter_tree_parse(filter->head,e,tfc->tf,
b6ef18af 337 tfc->t_context->t,tfc,NULL,NULL))
8ff6243c 338 return FALSE;
cec3d7b0 339
b5ad5a5d 340 lttv_event_to_string(e, a_string, TRUE, !a_no_field_names, tfs);
dc877563 341
342 if(a_state) {
9d0bb8d1 343 g_string_append_printf(a_string, " %s ",
d730b5c8 344 g_quark_to_string(process->state->s));
dc877563 345 }
48f6f3c2 346
54d067f5 347 g_string_append_printf(a_string,"\n");
a0305aae 348
ffd54a90 349 fputs(a_string->str, a_file);
dc877563 350 return FALSE;
48f6f3c2 351}
352
353
08b1c66e 354static void init()
48f6f3c2 355{
d5160b2b 356 gboolean result;
357
ffd54a90 358 LttvAttributeValue value;
48f6f3c2 359
ffd54a90 360 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
48f6f3c2 361
b445142a 362 g_info("Init textDump.c");
9402662d 363
c6bc9cb9 364 a_string = g_string_new("");
365
ffd54a90 366 a_file_name = NULL;
367 lttv_option_add("output", 'o',
368 "output file where the text is written",
369 "file name",
370 LTTV_OPT_STRING, &a_file_name, NULL, NULL);
48f6f3c2 371
7e5fe4d0 372 a_noevent = FALSE;
373 lttv_option_add("noevent", 'n',
374 "disable event printout",
375 "",
376 LTTV_OPT_NONE, &a_noevent, NULL, NULL);
377
b5ad5a5d 378 a_no_field_names = FALSE;
379 lttv_option_add("field_names", 's',
380 "do not write the field names for each event",
ffd54a90 381 "",
b5ad5a5d 382 LTTV_OPT_NONE, &a_no_field_names, NULL, NULL);
48f6f3c2 383
ffd54a90 384 a_state = FALSE;
7e63c6cd 385 lttv_option_add("process_state", 'r',
ffd54a90 386 "write the pid and state for each event",
387 "",
388 LTTV_OPT_NONE, &a_state, NULL, NULL);
dc877563 389
b445142a 390 a_cpu_stats = FALSE;
9d0bb8d1 391 lttv_option_add("cpu_stats", 'c',
b445142a 392 "write the per cpu statistics",
393 "",
394 LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL);
395
396 a_process_stats = FALSE;
9d0bb8d1 397 lttv_option_add("process_stats", 'p',
b445142a 398 "write the per process statistics",
399 "",
400 LTTV_OPT_NONE, &a_process_stats, NULL, NULL);
401
7a36589d 402 a_path_output = FALSE;
403 lttv_option_add("path_output", 'a',
404 "print the process stats in path format, for easy grepping",
405 "",
406 LTTV_OPT_NONE, &a_path_output, NULL, NULL);
407
7e5fe4d0 408 if (!a_noevent) {
409 result = lttv_iattribute_find_by_path(attributes, "hooks/event",
410 LTTV_POINTER, &value);
411 g_assert(result);
412 event_hook = *(value.v_pointer);
413 g_assert(event_hook);
414 lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT);
415 }
dc877563 416
d5160b2b 417 result = lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
418 LTTV_POINTER, &value);
419 g_assert(result);
420 before_trace = *(value.v_pointer);
421 g_assert(before_trace);
12c59c3d 422 lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT);
dc877563 423
d5160b2b 424 result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
425 LTTV_POINTER, &value);
426 g_assert(result);
427 before_traceset = *(value.v_pointer);
428 g_assert(before_traceset);
12c59c3d 429 lttv_hooks_add(before_traceset, write_traceset_header, NULL,
430 LTTV_PRIO_DEFAULT);
dc877563 431
d5160b2b 432 result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
433 LTTV_POINTER, &value);
434 g_assert(result);
435 after_traceset = *(value.v_pointer);
436 g_assert(after_traceset);
12c59c3d 437 lttv_hooks_add(after_traceset, write_traceset_footer, NULL,
438 LTTV_PRIO_DEFAULT);
ffd54a90 439}
48f6f3c2 440
08b1c66e 441static void destroy()
ffd54a90 442{
b445142a 443 g_info("Destroy textDump");
444
7e5fe4d0 445 lttv_option_remove("noevent");
446
ffd54a90 447 lttv_option_remove("output");
48f6f3c2 448
ffd54a90 449 lttv_option_remove("field_names");
48f6f3c2 450
ffd54a90 451 lttv_option_remove("process_state");
48f6f3c2 452
b445142a 453 lttv_option_remove("cpu_stats");
454
455 lttv_option_remove("process_stats");
456
4f9ac095 457 lttv_option_remove("path_output");
458
c6bc9cb9 459 g_string_free(a_string, TRUE);
460
7e5fe4d0 461 if (!a_noevent)
462 lttv_hooks_remove_data(event_hook, write_event_content, NULL);
48f6f3c2 463
ffd54a90 464 lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
48f6f3c2 465
58ad2e34 466 lttv_hooks_remove_data(before_traceset, write_traceset_header, NULL);
48f6f3c2 467
cbf66716 468 lttv_hooks_remove_data(after_traceset, write_traceset_footer, NULL);
48f6f3c2 469}
470
471
08b1c66e 472LTTV_MODULE("textDump", "Print events in a file", \
473 "Produce a detailed text printout of a trace", \
8e680509 474 init, destroy, "stats", "batchAnalysis", "option", "print")
48f6f3c2 475
This page took 0.078451 seconds and 4 git commands to generate.