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