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>
60 print_path_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
62 int i
, nb
, saved_length
;
64 LttvAttribute
*subtree
;
66 LttvAttributeName name
;
68 LttvAttributeValue value
;
70 LttvAttributeType type
;
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
);
79 g_string_sprintfa(indent
, "/%s", g_quark_to_string(name
));
81 g_string_sprintfa(indent
, "/%s", name
);
86 fprintf(fp
, "%s: %d\n", indent
->str
, *value
.v_int
);
89 fprintf(fp
, "%s: %d\n", indent
->str
, *value
.v_int
);
92 fprintf(fp
, "%s: %ld\n", indent
->str
, *value
.v_ulong
);
95 fprintf(fp
, "%s: %lu\n", indent
->str
, *value
.v_ulong
);
98 fprintf(fp
, "%s: %f\n", indent
->str
, (double) *value
.v_float
);
101 fprintf(fp
, "%s: %f\n", indent
->str
, *value
.v_double
);
104 fprintf(fp
, "%s: %lu.%09lu\n", indent
->str
, value
.v_time
->tv_sec
, value
.v_time
->tv_nsec
);
107 fprintf(fp
, "%s: POINTER\n", indent
->str
);
110 fprintf(fp
, "%s: %s\n", indent
->str
, *value
.v_string
);
113 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
114 subtree
= (LttvAttribute
*) *(value
.v_gobject
);
115 print_path_tree(fp
, indent
, subtree
);
117 fprintf(fp
, "%s: GOBJECT\n", indent
->str
);
123 g_string_truncate(indent
, saved_length
);
128 print_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
130 int i
, nb
, saved_length
;
132 LttvAttribute
*subtree
;
134 LttvAttributeName name
;
136 LttvAttributeValue value
;
138 LttvAttributeType type
;
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
);
146 fprintf(fp
, "%s%s: ", indent
->str
, g_quark_to_string(name
));
148 fprintf(fp
, "%s%lu: ", indent
->str
, name
);
152 fprintf(fp
, "%d\n", *value
.v_int
);
155 fprintf(fp
, "%u\n", *value
.v_uint
);
158 fprintf(fp
, "%ld\n", *value
.v_long
);
161 fprintf(fp
, "%lu\n", *value
.v_ulong
);
164 fprintf(fp
, "%f\n", (double)*value
.v_float
);
167 fprintf(fp
, "%f\n", *value
.v_double
);
170 fprintf(fp
, "%10lu.%09lu\n", value
.v_time
->tv_sec
,
171 value
.v_time
->tv_nsec
);
174 fprintf(fp
, "POINTER\n");
177 fprintf(fp
, "%s\n", *value
.v_string
);
180 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
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
);
188 else fprintf(fp
, "GOBJECT\n");
197 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
199 int i
, nb
, saved_length
;
207 LttSystemDescription
*desc
;
209 if(tscs
->stats
== NULL
) return;
210 indent
= g_string_new("");
211 fprintf(fp
, "Traceset statistics:\n\n");
213 print_path_tree(fp
, indent
, tscs
->stats
);
215 print_tree(fp
, indent
, tscs
->stats
);
218 ts
= tscs
->parent
.parent
.ts
;
219 nb
= lttv_traceset_number(ts
);
221 for(i
= 0 ; i
< nb
; i
++) {
222 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
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
),
231 saved_length
= indent
->len
;
233 g_string_sprintfa(indent
, "/trace%i", i
);
234 print_path_tree(fp
, indent
, tcs
->stats
);
236 g_string_append(indent
, " ");
237 print_tree(fp
, indent
, tcs
->stats
);
239 g_string_truncate(indent
, saved_length
);
241 g_string_free(indent
, TRUE
);
244 /* Insert the hooks before and after each trace and tracefile, and for each
245 event. Print a global header. */
249 static GString
*a_string
;
251 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
253 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
255 g_info("TextDump traceset header");
257 if(a_file_name
== NULL
) a_file
= stdout
;
258 else a_file
= fopen(a_file_name
, "w");
260 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
262 /* Print the trace set header */
263 fprintf(a_file
,"Trace set contains %d traces\n\n",
264 lttv_traceset_number(tc
->ts
));
270 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
272 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
274 g_info("TextDump traceset footer");
276 fprintf(a_file
,"End trace set\n\n");
278 if(LTTV_IS_TRACESET_STATS(tc
)) {
279 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
, ltt_time_infinite
);
280 print_stats(a_file
, (LttvTracesetStats
*)tc
);
283 if(a_file_name
!= NULL
) fclose(a_file
);
289 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
291 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
293 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
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
));
304 static int write_event_content(void *hook_data
, void *call_data
)
308 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
310 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
312 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
316 LttvAttributeValue value_filter
;
320 guint cpu
= tfs
->cpu
;
321 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
322 LttvProcessState
*process
= ts
->running_process
[cpu
];
324 e
= ltt_tracefile_get_event(tfc
->tf
);
326 result
= lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
327 LTTV_POINTER
, &value_filter
);
329 filter
= (LttvFilter
*)*(value_filter
.v_pointer
);
332 * call to the filter if available
334 if(filter
->head
!= NULL
)
335 if(!lttv_filter_tree_parse(filter
->head
,e
,tfc
->tf
,
336 tfc
->t_context
->t
,tfc
,NULL
,NULL
))
339 lttv_event_to_string(e
, a_string
, TRUE
, !a_no_field_names
, tfs
);
342 g_string_append_printf(a_string
, " %s ",
343 g_quark_to_string(process
->state
->s
));
346 g_string_append_printf(a_string
,"\n");
348 fputs(a_string
->str
, a_file
);
357 LttvAttributeValue value
;
359 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
361 g_info("Init textDump.c");
363 a_string
= g_string_new("");
366 lttv_option_add("output", 'o',
367 "output file where the text is written",
369 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
371 a_no_field_names
= FALSE
;
372 lttv_option_add("field_names", 's',
373 "do not write the field names for each event",
375 LTTV_OPT_NONE
, &a_no_field_names
, NULL
, NULL
);
378 lttv_option_add("process_state", 'r',
379 "write the pid and state for each event",
381 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
384 lttv_option_add("cpu_stats", 'c',
385 "write the per cpu statistics",
387 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
389 a_process_stats
= FALSE
;
390 lttv_option_add("process_stats", 'p',
391 "write the per process statistics",
393 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
395 a_path_output
= FALSE
;
396 lttv_option_add("path_output", 'a',
397 "print the process stats in path format, for easy grepping",
399 LTTV_OPT_NONE
, &a_path_output
, NULL
, NULL
);
401 result
= lttv_iattribute_find_by_path(attributes
, "hooks/event",
402 LTTV_POINTER
, &value
);
404 event_hook
= *(value
.v_pointer
);
405 g_assert(event_hook
);
406 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
408 result
= lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
409 LTTV_POINTER
, &value
);
411 before_trace
= *(value
.v_pointer
);
412 g_assert(before_trace
);
413 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
415 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
416 LTTV_POINTER
, &value
);
418 before_traceset
= *(value
.v_pointer
);
419 g_assert(before_traceset
);
420 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
423 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
424 LTTV_POINTER
, &value
);
426 after_traceset
= *(value
.v_pointer
);
427 g_assert(after_traceset
);
428 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
432 static void destroy()
434 g_info("Destroy textDump");
436 lttv_option_remove("output");
438 lttv_option_remove("field_names");
440 lttv_option_remove("process_state");
442 lttv_option_remove("cpu_stats");
444 lttv_option_remove("process_stats");
446 lttv_option_remove("path_output");
448 g_string_free(a_string
, TRUE
);
450 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
452 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
454 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
456 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
460 LTTV_MODULE("textDump", "Print events in a file", \
461 "Produce a detailed text printout of a trace", \
462 init
, destroy
, "stats", "batchAnalysis", "option", "print")