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
)
306 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
308 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
310 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
314 LttvAttributeValue value_filter
;
318 guint cpu
= tfs
->cpu
;
319 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
320 LttvProcessState
*process
= ts
->running_process
[cpu
];
322 e
= ltt_tracefile_get_event(tfc
->tf
);
324 g_assert(lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
325 LTTV_POINTER
, &value_filter
));
326 filter
= (LttvFilter
*)*(value_filter
.v_pointer
);
329 * call to the filter if available
331 if(filter
->head
!= NULL
)
332 if(!lttv_filter_tree_parse(filter
->head
,e
,tfc
->tf
,
333 tfc
->t_context
->t
,tfc
,NULL
,NULL
))
336 lttv_event_to_string(e
, a_string
, TRUE
, a_field_names
, tfs
);
339 g_string_append_printf(a_string
, " %s ",
340 g_quark_to_string(process
->state
->s
));
343 g_string_append_printf(a_string
,"\n");
345 fputs(a_string
->str
, a_file
);
352 LttvAttributeValue value
;
354 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
356 g_info("Init textDump.c");
358 a_string
= g_string_new("");
361 lttv_option_add("output", 'o',
362 "output file where the text is written",
364 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
366 a_field_names
= FALSE
;
367 lttv_option_add("field_names", 'l',
368 "write the field names for each event",
370 LTTV_OPT_NONE
, &a_field_names
, NULL
, NULL
);
373 lttv_option_add("process_state", 'r',
374 "write the pid and state for each event",
376 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
379 lttv_option_add("cpu_stats", 'c',
380 "write the per cpu statistics",
382 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
384 a_process_stats
= FALSE
;
385 lttv_option_add("process_stats", 'p',
386 "write the per process statistics",
388 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
390 a_path_output
= FALSE
;
391 lttv_option_add("path_output", 'a',
392 "print the process stats in path format, for easy grepping",
394 LTTV_OPT_NONE
, &a_path_output
, NULL
, NULL
);
396 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event",
397 LTTV_POINTER
, &value
));
398 g_assert((event_hook
= *(value
.v_pointer
)) != NULL
);
399 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
401 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
402 LTTV_POINTER
, &value
));
403 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
404 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
406 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
407 LTTV_POINTER
, &value
));
408 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
409 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
412 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
413 LTTV_POINTER
, &value
));
414 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
415 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
419 static void destroy()
421 g_info("Destroy textDump");
423 lttv_option_remove("output");
425 lttv_option_remove("field_names");
427 lttv_option_remove("process_state");
429 lttv_option_remove("cpu_stats");
431 lttv_option_remove("process_stats");
433 lttv_option_remove("path_output");
435 g_string_free(a_string
, TRUE
);
437 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
439 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
441 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
443 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
447 LTTV_MODULE("textDump", "Print events in a file", \
448 "Produce a detailed text printout of a trace", \
449 init
, destroy
, "stats", "batchAnalysis", "option", "print")