1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 /* The text dump facility needs to print headers before the trace set and
20 before each trace, to print each event, and to print statistics
27 #include <lttv/lttv.h>
28 #include <lttv/option.h>
29 #include <lttv/module.h>
30 #include <lttv/hook.h>
31 #include <lttv/attribute.h>
32 #include <lttv/iattribute.h>
33 #include <lttv/stats.h>
34 #include <lttv/filter.h>
36 #include <ltt/event.h>
38 #include <ltt/trace.h>
39 #include <ltt/facility.h>
57 void print_field(LttEvent
*e
, LttField
*f
, GString
*s
, gboolean field_names
) {
67 type
= ltt_field_type(f
);
68 switch(ltt_type_class(type
)) {
72 g_string_append_printf(s
, " %lld", ltt_event_get_long_int(e
,f
));
79 g_string_append_printf(s
, " %llu", ltt_event_get_long_unsigned(e
,f
));
83 g_string_append_printf(s
, " %g", ltt_event_get_double(e
,f
));
87 g_string_append_printf(s
, " 0x%llx", ltt_event_get_long_unsigned(e
,f
));
91 g_string_append_printf(s
, " \"%s\"", ltt_event_get_string(e
,f
));
95 g_string_append_printf(s
, " %s", ltt_enum_string_get(type
,
96 ltt_event_get_unsigned(e
,f
)-1));
101 g_string_append_printf(s
, " {");
102 nb
= ltt_event_field_element_number(e
,f
);
103 element
= ltt_field_element(f
);
104 for(i
= 0 ; i
< nb
; i
++) {
105 ltt_event_field_element_select(e
,f
,i
);
106 print_field(e
, element
, s
, field_names
);
108 g_string_append_printf(s
, " }");
112 g_string_append_printf(s
, " {");
113 nb
= ltt_type_member_number(type
);
114 for(i
= 0 ; i
< nb
; i
++) {
115 element
= ltt_field_member(f
,i
);
117 ltt_type_member_type(type
, i
, &name
);
118 g_string_append_printf(s
, " %s = ", g_quark_to_string(name
));
120 print_field(e
, element
, s
, field_names
);
122 g_string_append_printf(s
, " }");
126 g_string_append_printf(s
, " {");
127 nb
= ltt_type_member_number(type
);
128 for(i
= 0 ; i
< nb
; i
++) {
129 element
= ltt_field_member(f
,i
);
131 ltt_type_member_type(type
, i
, &name
);
132 g_string_append_printf(s
, " %s = ", g_quark_to_string(name
));
134 print_field(e
, element
, s
, field_names
);
136 g_string_append_printf(s
, " }");
143 void lttv_event_to_string(LttEvent
*e
, GString
*s
,
144 gboolean mandatory_fields
, gboolean field_names
, LttvTracefileState
*tfs
)
146 LttFacility
*facility
;
148 LttEventType
*event_type
;
156 g_string_set_size(s
,0);
158 facility
= ltt_event_facility(e
);
159 event_type
= ltt_event_eventtype(e
);
160 field
= ltt_event_field(e
);
162 if(mandatory_fields
) {
163 time
= ltt_event_time(e
);
164 g_string_append_printf(s
,"%s.%s: %ld.%09ld (%s)",
165 g_quark_to_string(ltt_facility_name(facility
)),
166 g_quark_to_string(ltt_eventtype_name(event_type
)),
167 (long)time
.tv_sec
, time
.tv_nsec
,
168 g_quark_to_string(tfs
->cpu_name
));
169 /* Print the process id and the state/interrupt type of the process */
170 g_string_append_printf(s
,", %u, %u, %s", tfs
->process
->pid
,
172 g_quark_to_string(tfs
->process
->state
->t
));
176 print_field(e
, field
, s
, field_names
);
181 print_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
183 int i
, nb
, saved_length
;
185 LttvAttribute
*subtree
;
187 LttvAttributeName name
;
189 LttvAttributeValue value
;
191 LttvAttributeType type
;
193 nb
= lttv_attribute_get_number(tree
);
194 for(i
= 0 ; i
< nb
; i
++) {
195 type
= lttv_attribute_get(tree
, i
, &name
, &value
);
196 fprintf(fp
, "%s%s: ", indent
->str
, g_quark_to_string(name
));
200 fprintf(fp
, "%d\n", *value
.v_int
);
203 fprintf(fp
, "%u\n", *value
.v_uint
);
206 fprintf(fp
, "%ld\n", *value
.v_long
);
209 fprintf(fp
, "%lu\n", *value
.v_ulong
);
212 fprintf(fp
, "%f\n", (double)*value
.v_float
);
215 fprintf(fp
, "%f\n", *value
.v_double
);
218 fprintf(fp
, "%10lu.%09lu\n", value
.v_time
->tv_sec
,
219 value
.v_time
->tv_nsec
);
222 fprintf(fp
, "POINTER\n");
225 fprintf(fp
, "%s\n", *value
.v_string
);
228 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
230 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
231 saved_length
= indent
->len
;
232 g_string_append(indent
, " ");
233 print_tree(fp
, indent
, subtree
);
234 g_string_truncate(indent
, saved_length
);
236 else fprintf(fp
, "GOBJECT\n");
246 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
248 int i
, nb
, saved_length
;
256 LttSystemDescription
*desc
;
258 if(tscs
->stats
== NULL
) return;
259 indent
= g_string_new("");
260 fprintf(fp
, "Traceset statistics:\n\n");
261 print_tree(fp
, indent
, tscs
->stats
);
263 ts
= tscs
->parent
.parent
.ts
;
264 nb
= lttv_traceset_number(ts
);
266 for(i
= 0 ; i
< nb
; i
++) {
267 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
269 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
270 LttTime start_time
= ltt_trace_system_description_trace_start_time(desc
);
271 fprintf(fp
, "Trace on system %s at time %lu.%09lu :\n",
272 ltt_trace_system_description_node_name(desc
),
276 saved_length
= indent
->len
;
277 g_string_append(indent
, " ");
278 print_tree(fp
, indent
, tcs
->stats
);
279 g_string_truncate(indent
, saved_length
);
281 g_string_free(indent
, TRUE
);
285 /* Insert the hooks before and after each trace and tracefile, and for each
286 event. Print a global header. */
290 static GString
*a_string
;
292 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
294 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
296 g_info("TextDump traceset header");
298 if(a_file_name
== NULL
) a_file
= stdout
;
299 else a_file
= fopen(a_file_name
, "w");
301 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
303 /* Print the trace set header */
304 fprintf(a_file
,"Trace set contains %d traces\n\n",
305 lttv_traceset_number(tc
->ts
));
311 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
313 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
315 g_info("TextDump traceset footer");
317 fprintf(a_file
,"End trace set\n\n");
319 if(LTTV_IS_TRACESET_STATS(tc
)) {
320 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
);
321 print_stats(a_file
, (LttvTracesetStats
*)tc
);
324 if(a_file_name
!= NULL
) fclose(a_file
);
330 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
332 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
334 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
336 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
337 ltt_trace_system_description_node_name(system
),
338 ltt_trace_system_description_domain_name(system
),
339 ltt_trace_system_description_description(system
));
345 static int write_event_content(void *hook_data
, void *call_data
)
347 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
349 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
351 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
355 LttvAttributeValue value_filter
;
359 e
= ltt_tracefile_get_event(tfc
->tf
);
362 g_assert(lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
363 LTTV_POINTER
, &value_filter
));
364 filter
= (LttvFilter
*)*(value_filter
.v_pointer
);
367 * call to the filter if available
369 if(filter
->head
!= NULL
)
370 if(!lttv_filter_tree_parse(filter
->head
,e
,tfc
->tf
,tfc
->t_context
->t
,tfs
->process
,tfc
))
373 lttv_event_to_string(e
, a_string
, TRUE
, a_field_names
, tfs
);
374 g_string_append_printf(a_string
,"\n");
377 g_string_append_printf(a_string
, " %s ",
378 g_quark_to_string(tfs
->process
->state
->s
));
381 fputs(a_string
->str
, a_file
);
388 LttvAttributeValue value
;
390 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
392 g_info("Init textDump.c");
394 a_string
= g_string_new("");
397 lttv_option_add("output", 'o',
398 "output file where the text is written",
400 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
402 a_field_names
= FALSE
;
403 lttv_option_add("field_names", 'l',
404 "write the field names for each event",
406 LTTV_OPT_NONE
, &a_field_names
, NULL
, NULL
);
409 lttv_option_add("process_state", 's',
410 "write the pid and state for each event",
412 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
415 lttv_option_add("cpu_stats", 'c',
416 "write the per cpu statistics",
418 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
420 a_process_stats
= FALSE
;
421 lttv_option_add("process_stats", 'p',
422 "write the per process statistics",
424 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
426 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event",
427 LTTV_POINTER
, &value
));
428 g_assert((event_hook
= *(value
.v_pointer
)) != NULL
);
429 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
431 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
432 LTTV_POINTER
, &value
));
433 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
434 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
436 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
437 LTTV_POINTER
, &value
));
438 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
439 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
442 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
443 LTTV_POINTER
, &value
));
444 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
445 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
450 static void destroy()
452 g_info("Destroy textDump");
454 lttv_option_remove("output");
456 lttv_option_remove("field_names");
458 lttv_option_remove("process_state");
460 lttv_option_remove("cpu_stats");
462 lttv_option_remove("process_stats");
464 g_string_free(a_string
, TRUE
);
466 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
468 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
470 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
472 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
476 LTTV_MODULE("textDump", "Print events in a file", \
477 "Produce a detailed text printout of a trace", \
478 init
, destroy
, "stats", "batchAnalysis", "option")