1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * 2005 Mathieu Desnoyers
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;
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.
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,
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
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>
38 #include <ltt/event.h>
39 #include <ltt/trace.h>
62 print_path_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
64 int i
, nb
, saved_length
;
66 LttvAttribute
*subtree
;
68 LttvAttributeName name
;
70 LttvAttributeValue value
;
72 LttvAttributeType type
;
76 saved_length
= indent
->len
;
77 nb
= lttv_attribute_get_number(tree
);
78 for(i
= 0 ; i
< nb
; i
++) {
79 type
= lttv_attribute_get(tree
, i
, &name
, &value
, &is_named
);
81 g_string_sprintfa(indent
, "/%s", g_quark_to_string(name
));
83 g_string_sprintfa(indent
, "/%" PRIu32
, (guint32
) name
);
88 fprintf(fp
, "%s: %d\n", indent
->str
, *value
.v_int
);
91 fprintf(fp
, "%s: %d\n", indent
->str
, *value
.v_int
);
94 fprintf(fp
, "%s: %ld\n", indent
->str
, *value
.v_ulong
);
97 fprintf(fp
, "%s: %lu\n", indent
->str
, *value
.v_ulong
);
100 fprintf(fp
, "%s: %f\n", indent
->str
, (double) *value
.v_float
);
103 fprintf(fp
, "%s: %f\n", indent
->str
, *value
.v_double
);
106 fprintf(fp
, "%s: %lu.%09lu\n", indent
->str
, value
.v_time
->tv_sec
, value
.v_time
->tv_nsec
);
109 fprintf(fp
, "%s: POINTER\n", indent
->str
);
112 fprintf(fp
, "%s: %s\n", indent
->str
, *value
.v_string
);
115 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
116 subtree
= (LttvAttribute
*) *(value
.v_gobject
);
117 print_path_tree(fp
, indent
, subtree
);
119 fprintf(fp
, "%s: GOBJECT\n", indent
->str
);
125 g_string_truncate(indent
, saved_length
);
130 print_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
132 int i
, nb
, saved_length
;
134 LttvAttribute
*subtree
;
136 LttvAttributeName name
;
138 LttvAttributeValue value
;
140 LttvAttributeType type
;
144 nb
= lttv_attribute_get_number(tree
);
145 for(i
= 0 ; i
< nb
; i
++) {
146 type
= lttv_attribute_get(tree
, i
, &name
, &value
, &is_named
);
148 fprintf(fp
, "%s%s: ", indent
->str
, g_quark_to_string(name
));
150 fprintf(fp
, "%s%" PRIu32
": ", indent
->str
,
155 fprintf(fp
, "%d\n", *value
.v_int
);
158 fprintf(fp
, "%u\n", *value
.v_uint
);
161 fprintf(fp
, "%ld\n", *value
.v_long
);
164 fprintf(fp
, "%lu\n", *value
.v_ulong
);
167 fprintf(fp
, "%f\n", (double)*value
.v_float
);
170 fprintf(fp
, "%f\n", *value
.v_double
);
173 fprintf(fp
, "%10lu.%09lu\n", value
.v_time
->tv_sec
,
174 value
.v_time
->tv_nsec
);
177 fprintf(fp
, "POINTER\n");
180 fprintf(fp
, "%s\n", *value
.v_string
);
183 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
185 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
186 saved_length
= indent
->len
;
187 indent
= g_string_append(indent
, " ");
188 print_tree(fp
, indent
, subtree
);
189 g_string_truncate(indent
, saved_length
);
191 else fprintf(fp
, "GOBJECT\n");
200 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
202 int i
, nb
, saved_length
;
210 if(tscs
->stats
== NULL
) return;
211 indent
= g_string_new("");
212 fprintf(fp
, "Traceset statistics:\n\n");
214 print_path_tree(fp
, indent
, tscs
->stats
);
216 print_tree(fp
, indent
, tscs
->stats
);
219 ts
= tscs
->parent
.parent
.ts
;
220 nb
= lttv_traceset_number(ts
);
222 for(i
= 0 ; i
< nb
; i
++) {
223 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
225 LttSystemDescription
*desc
;
226 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
227 LttTime start_time
= ltt_trace_system_description_trace_start_time(desc
);
228 fprintf(fp
, "Trace on system %s at time %lu.%09lu :\n",
229 ltt_trace_system_description_node_name(desc
),
233 saved_length
= indent
->len
;
235 g_string_sprintfa(indent
, "/trace%i", i
);
236 print_path_tree(fp
, indent
, tcs
->stats
);
238 g_string_append(indent
, " ");
239 print_tree(fp
, indent
, tcs
->stats
);
241 g_string_truncate(indent
, saved_length
);
243 g_string_free(indent
, TRUE
);
246 /* Insert the hooks before and after each trace and tracefile, and for each
247 event. Print a global header. */
251 static GString
*a_string
;
253 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
255 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
257 g_info("TextDump traceset header");
259 if(a_file_name
== NULL
) a_file
= stdout
;
260 else a_file
= fopen(a_file_name
, "w");
262 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
264 /* Print the trace set header */
265 fprintf(a_file
,"Trace set contains %d traces\n\n",
266 lttv_traceset_number(tc
->ts
));
272 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
274 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
276 g_info("TextDump traceset footer");
278 fprintf(a_file
,"End trace set\n\n");
280 if(LTTV_IS_TRACESET_STATS(tc
)) {
281 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
, ltt_time_infinite
);
282 print_stats(a_file
, (LttvTracesetStats
*)tc
);
285 if(a_file_name
!= NULL
) fclose(a_file
);
291 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
294 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
295 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
297 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
298 ltt_trace_system_description_node_name(system
),
299 ltt_trace_system_description_domain_name(system
),
300 ltt_trace_system_description_description(system
));
306 static int write_event_content(void *hook_data
, void *call_data
)
310 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
312 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
314 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
318 LttvAttributeValue value_filter
;
322 guint cpu
= tfs
->cpu
;
323 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
324 LttvProcessState
*process
= ts
->running_process
[cpu
];
329 e
= ltt_tracefile_get_event(tfc
->tf
);
331 result
= lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
332 LTTV_POINTER
, &value_filter
);
334 filter
= (LttvFilter
*)*(value_filter
.v_pointer
);
337 * call to the filter if available
339 if(filter
->head
!= NULL
)
340 if(!lttv_filter_tree_parse(filter
->head
,e
,tfc
->tf
,
341 tfc
->t_context
->t
,tfc
,NULL
,NULL
))
344 lttv_event_to_string(e
, a_string
, TRUE
, !a_no_field_names
, tfs
);
347 g_string_append_printf(a_string
, " %s ",
348 g_quark_to_string(process
->state
->s
));
351 g_string_append_printf(a_string
,"\n");
353 fputs(a_string
->str
, a_file
);
362 LttvAttributeValue value
;
364 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
366 g_info("Init textDump.c");
368 a_string
= g_string_new("");
371 lttv_option_add("output", 'o',
372 "output file where the text is written",
374 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
377 lttv_option_add("noevent", 'n',
378 "disable event printout",
380 LTTV_OPT_NONE
, &a_noevent
, NULL
, NULL
);
382 a_no_field_names
= FALSE
;
383 lttv_option_add("field_names", 's',
384 "do not write the field names for each event",
386 LTTV_OPT_NONE
, &a_no_field_names
, NULL
, NULL
);
389 lttv_option_add("process_state", 'r',
390 "write the pid and state for each event",
392 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
395 lttv_option_add("cpu_stats", 'c',
396 "write the per cpu statistics",
398 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
400 a_process_stats
= FALSE
;
401 lttv_option_add("process_stats", 'p',
402 "write the per process statistics",
404 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
406 a_path_output
= FALSE
;
407 lttv_option_add("path_output", 'a',
408 "print the process stats in path format, for easy grepping",
410 LTTV_OPT_NONE
, &a_path_output
, NULL
, NULL
);
412 result
= lttv_iattribute_find_by_path(attributes
, "hooks/event",
413 LTTV_POINTER
, &value
);
415 event_hook
= *(value
.v_pointer
);
416 g_assert(event_hook
);
417 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
419 result
= lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
420 LTTV_POINTER
, &value
);
422 before_trace
= *(value
.v_pointer
);
423 g_assert(before_trace
);
424 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
426 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
427 LTTV_POINTER
, &value
);
429 before_traceset
= *(value
.v_pointer
);
430 g_assert(before_traceset
);
431 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
434 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
435 LTTV_POINTER
, &value
);
437 after_traceset
= *(value
.v_pointer
);
438 g_assert(after_traceset
);
439 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
443 static void destroy()
445 g_info("Destroy textDump");
447 lttv_option_remove("noevent");
449 lttv_option_remove("output");
451 lttv_option_remove("field_names");
453 lttv_option_remove("process_state");
455 lttv_option_remove("cpu_stats");
457 lttv_option_remove("process_stats");
459 lttv_option_remove("path_output");
461 g_string_free(a_string
, TRUE
);
463 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
465 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
467 lttv_hooks_remove_data(before_traceset
, write_traceset_header
, NULL
);
469 lttv_hooks_remove_data(after_traceset
, write_traceset_footer
, NULL
);
473 LTTV_MODULE("textDump", "Print events in a file", \
474 "Produce a detailed text printout of a trace", \
475 init
, destroy
, "stats", "batchAnalysis", "option", "print")