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