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
23 #include <lttv/lttv.h>
24 #include <lttv/option.h>
25 #include <lttv/module.h>
26 #include <lttv/hook.h>
27 #include <lttv/attribute.h>
28 #include <lttv/iattribute.h>
29 #include <lttv/stats.h>
31 #include <ltt/event.h>
33 #include <ltt/trace.h>
52 void print_field(LttEvent
*e
, LttField
*f
, GString
*s
, gboolean field_names
) {
62 type
= ltt_field_type(f
);
63 switch(ltt_type_class(type
)) {
65 g_string_append_printf(s
, " %ld", ltt_event_get_long_int(e
,f
));
69 g_string_append_printf(s
, " %lu", ltt_event_get_long_unsigned(e
,f
));
73 g_string_append_printf(s
, " %g", ltt_event_get_double(e
,f
));
77 g_string_append_printf(s
, " \"%s\"", ltt_event_get_string(e
,f
));
81 g_string_append_printf(s
, " %s", ltt_enum_string_get(type
,
82 ltt_event_get_unsigned(e
,f
)-1));
87 g_string_append_printf(s
, " {");
88 nb
= ltt_event_field_element_number(e
,f
);
89 element
= ltt_field_element(f
);
90 for(i
= 0 ; i
< nb
; i
++) {
91 ltt_event_field_element_select(e
,f
,i
);
92 print_field(e
, element
, s
, field_names
);
94 g_string_append_printf(s
, " }");
98 g_string_append_printf(s
, " {");
99 nb
= ltt_type_member_number(type
);
100 for(i
= 0 ; i
< nb
; i
++) {
101 element
= ltt_field_member(f
,i
);
103 ltt_type_member_type(type
, i
, &name
);
104 g_string_append_printf(s
, " %s = ", name
);
106 print_field(e
, element
, s
, field_names
);
108 g_string_append_printf(s
, " }");
114 void lttv_event_to_string(LttEvent
*e
, LttTracefile
*tf
, GString
*s
,
115 gboolean mandatory_fields
, gboolean field_names
, LttvTracefileState
*tfs
)
117 LttFacility
*facility
;
119 LttEventType
*event_type
;
127 g_string_set_size(s
,0);
129 facility
= ltt_event_facility(e
);
130 event_type
= ltt_event_eventtype(e
);
131 field
= ltt_event_field(e
);
133 if(mandatory_fields
) {
134 time
= ltt_event_time(e
);
135 g_string_append_printf(s
,"%s.%s: %ld.%09ld (%s)",
136 ltt_facility_name(facility
),
137 ltt_eventtype_name(event_type
), (long)time
.tv_sec
, time
.tv_nsec
,
138 g_quark_to_string(tfs
->cpu_name
));
139 /* Print the process id and the state/interrupt type of the process */
140 g_string_append_printf(s
,", %u, %u, %s", tfs
->process
->pid
,
142 g_quark_to_string(tfs
->process
->state
->t
));
146 print_field(e
, field
, s
, field_names
);
151 print_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
153 int i
, nb
, saved_length
;
155 LttvAttribute
*subtree
;
157 LttvAttributeName name
;
159 LttvAttributeValue value
;
161 LttvAttributeType type
;
163 nb
= lttv_attribute_get_number(tree
);
164 for(i
= 0 ; i
< nb
; i
++) {
165 type
= lttv_attribute_get(tree
, i
, &name
, &value
);
166 fprintf(fp
, "%s%s: ", indent
->str
, g_quark_to_string(name
));
170 fprintf(fp
, "%d\n", *value
.v_int
);
173 fprintf(fp
, "%u\n", *value
.v_uint
);
176 fprintf(fp
, "%ld\n", *value
.v_long
);
179 fprintf(fp
, "%lu\n", *value
.v_ulong
);
182 fprintf(fp
, "%f\n", (double)*value
.v_float
);
185 fprintf(fp
, "%f\n", *value
.v_double
);
188 fprintf(fp
, "%10u.%09u\n", value
.v_time
->tv_sec
,
189 value
.v_time
->tv_nsec
);
192 fprintf(fp
, "POINTER\n");
195 fprintf(fp
, "%s\n", *value
.v_string
);
198 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
200 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
201 saved_length
= indent
->len
;
202 g_string_append(indent
, " ");
203 print_tree(fp
, indent
, subtree
);
204 g_string_truncate(indent
, saved_length
);
206 else fprintf(fp
, "GOBJECT\n");
216 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
218 int i
, nb
, saved_length
;
226 LttSystemDescription
*desc
;
228 if(tscs
->stats
== NULL
) return;
229 indent
= g_string_new("");
230 fprintf(fp
, "Traceset statistics:\n\n");
231 print_tree(fp
, indent
, tscs
->stats
);
233 ts
= tscs
->parent
.parent
.ts
;
234 nb
= lttv_traceset_number(ts
);
236 for(i
= 0 ; i
< nb
; i
++) {
237 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
238 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
239 fprintf(fp
, "Trace on system %s at time %d secs:\n",
240 ltt_trace_system_description_node_name(desc
),
241 (ltt_trace_system_description_trace_start_time(desc
)).tv_sec
);
242 saved_length
= indent
->len
;
243 g_string_append(indent
, " ");
244 print_tree(fp
, indent
, tcs
->stats
);
245 g_string_truncate(indent
, saved_length
);
247 g_string_free(indent
, TRUE
);
251 /* Insert the hooks before and after each trace and tracefile, and for each
252 event. Print a global header. */
256 static GString
*a_string
;
258 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
260 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
262 g_info("TextDump traceset header");
264 if(a_file_name
== NULL
) a_file
= stdout
;
265 else a_file
= fopen(a_file_name
, "w");
267 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
269 /* Print the trace set header */
270 fprintf(a_file
,"Trace set contains %d traces\n\n",
271 lttv_traceset_number(tc
->ts
));
277 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
279 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
281 g_info("TextDump traceset footer");
283 fprintf(a_file
,"End trace set\n\n");
285 if(LTTV_IS_TRACESET_STATS(tc
)) {
286 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
);
287 print_stats(a_file
, (LttvTracesetStats
*)tc
);
290 if(a_file_name
!= NULL
) fclose(a_file
);
296 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
298 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
300 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
302 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
303 ltt_trace_system_description_node_name(system
),
304 ltt_trace_system_description_domain_name(system
),
305 ltt_trace_system_description_description(system
));
310 static int write_event_content(void *hook_data
, void *call_data
)
312 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
314 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
320 lttv_event_to_string(e
, tfc
->tf
, a_string
, TRUE
, a_field_names
, tfs
);
321 g_string_append_printf(a_string
,"\n");
324 g_string_append_printf(a_string
, " %s ",
325 g_quark_to_string(tfs
->process
->state
->s
));
328 fputs(a_string
->str
, a_file
);
335 LttvAttributeValue value
;
337 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
339 g_info("Init textDump.c");
341 a_string
= g_string_new("");
344 lttv_option_add("output", 'o',
345 "output file where the text is written",
347 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
349 a_field_names
= FALSE
;
350 lttv_option_add("field_names", 'l',
351 "write the field names for each event",
353 LTTV_OPT_NONE
, &a_field_names
, NULL
, NULL
);
356 lttv_option_add("process_state", 's',
357 "write the pid and state for each event",
359 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
362 lttv_option_add("cpu_stats", 'c',
363 "write the per cpu statistics",
365 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
367 a_process_stats
= FALSE
;
368 lttv_option_add("process_stats", 'p',
369 "write the per process statistics",
371 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
373 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event",
374 LTTV_POINTER
, &value
));
375 g_assert((event_hook
= *(value
.v_pointer
)) != NULL
);
376 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
378 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
379 LTTV_POINTER
, &value
));
380 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
381 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
383 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
384 LTTV_POINTER
, &value
));
385 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
386 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
389 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
390 LTTV_POINTER
, &value
));
391 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
392 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
397 static void destroy()
399 g_info("Destroy textDump");
401 lttv_option_remove("output");
403 lttv_option_remove("field_names");
405 lttv_option_remove("process_state");
407 lttv_option_remove("cpu_stats");
409 lttv_option_remove("process_stats");
411 g_string_free(a_string
, TRUE
);
413 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
415 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
417 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
419 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
423 LTTV_MODULE("textDump", "Print events in a file", \
424 "Produce a detailed text printout of a trace", \
425 init
, destroy
, "stats", "batchAnalysis", "option")