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