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>
40 #include <ltt/trace.h>
41 #include <ltt/facility.h>
61 print_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
63 int i
, nb
, saved_length
;
65 LttvAttribute
*subtree
;
67 LttvAttributeName name
;
69 LttvAttributeValue value
;
71 LttvAttributeType type
;
73 nb
= lttv_attribute_get_number(tree
);
74 for(i
= 0 ; i
< nb
; i
++) {
75 type
= lttv_attribute_get(tree
, i
, &name
, &value
);
76 fprintf(fp
, "%s%s: ", indent
->str
, g_quark_to_string(name
));
80 fprintf(fp
, "%d\n", *value
.v_int
);
83 fprintf(fp
, "%u\n", *value
.v_uint
);
86 fprintf(fp
, "%ld\n", *value
.v_long
);
89 fprintf(fp
, "%lu\n", *value
.v_ulong
);
92 fprintf(fp
, "%f\n", (double)*value
.v_float
);
95 fprintf(fp
, "%f\n", *value
.v_double
);
98 fprintf(fp
, "%10lu.%09lu\n", value
.v_time
->tv_sec
,
99 value
.v_time
->tv_nsec
);
102 fprintf(fp
, "POINTER\n");
105 fprintf(fp
, "%s\n", *value
.v_string
);
108 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
110 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
111 saved_length
= indent
->len
;
112 g_string_append(indent
, " ");
113 print_tree(fp
, indent
, subtree
);
114 g_string_truncate(indent
, saved_length
);
116 else fprintf(fp
, "GOBJECT\n");
126 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
128 int i
, nb
, saved_length
;
136 LttSystemDescription
*desc
;
138 if(tscs
->stats
== NULL
) return;
139 indent
= g_string_new("");
140 fprintf(fp
, "Traceset statistics:\n\n");
141 print_tree(fp
, indent
, tscs
->stats
);
143 ts
= tscs
->parent
.parent
.ts
;
144 nb
= lttv_traceset_number(ts
);
146 for(i
= 0 ; i
< nb
; i
++) {
147 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
149 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
150 LttTime start_time
= ltt_trace_system_description_trace_start_time(desc
);
151 fprintf(fp
, "Trace on system %s at time %lu.%09lu :\n",
152 ltt_trace_system_description_node_name(desc
),
156 saved_length
= indent
->len
;
157 g_string_append(indent
, " ");
158 print_tree(fp
, indent
, tcs
->stats
);
159 g_string_truncate(indent
, saved_length
);
161 g_string_free(indent
, TRUE
);
165 /* Insert the hooks before and after each trace and tracefile, and for each
166 event. Print a global header. */
170 static GString
*a_string
;
172 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
174 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
176 g_info("TextDump traceset header");
178 if(a_file_name
== NULL
) a_file
= stdout
;
179 else a_file
= fopen(a_file_name
, "w");
181 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
183 /* Print the trace set header */
184 fprintf(a_file
,"Trace set contains %d traces\n\n",
185 lttv_traceset_number(tc
->ts
));
191 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
193 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
195 g_info("TextDump traceset footer");
197 fprintf(a_file
,"End trace set\n\n");
199 if(LTTV_IS_TRACESET_STATS(tc
)) {
200 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
);
201 print_stats(a_file
, (LttvTracesetStats
*)tc
);
204 if(a_file_name
!= NULL
) fclose(a_file
);
210 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
212 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
214 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
216 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
217 ltt_trace_system_description_node_name(system
),
218 ltt_trace_system_description_domain_name(system
),
219 ltt_trace_system_description_description(system
));
225 static int write_event_content(void *hook_data
, void *call_data
)
227 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
229 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
231 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
235 LttvAttributeValue value_filter
;
239 guint cpu
= ltt_tracefile_num(tfs
->parent
.tf
);
240 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
241 LttvProcessState
*process
= ts
->running_process
[cpu
];
243 e
= ltt_tracefile_get_event(tfc
->tf
);
245 g_assert(lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
246 LTTV_POINTER
, &value_filter
));
247 filter
= (LttvFilter
*)*(value_filter
.v_pointer
);
250 * call to the filter if available
252 if(filter
->head
!= NULL
)
253 if(!lttv_filter_tree_parse(filter
->head
,e
,tfc
->tf
,
254 tfc
->t_context
->t
,tfc
))
257 lttv_event_to_string(e
, a_string
, TRUE
, a_field_names
, tfs
);
258 g_string_append_printf(a_string
,"\n");
261 g_string_append_printf(a_string
, " %s ",
262 g_quark_to_string(process
->state
->s
));
265 fputs(a_string
->str
, a_file
);
272 LttvAttributeValue value
;
274 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
276 g_info("Init textDump.c");
278 a_string
= g_string_new("");
281 lttv_option_add("output", 'o',
282 "output file where the text is written",
284 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
286 a_field_names
= FALSE
;
287 lttv_option_add("field_names", 'l',
288 "write the field names for each event",
290 LTTV_OPT_NONE
, &a_field_names
, NULL
, NULL
);
293 lttv_option_add("process_state", 's',
294 "write the pid and state for each event",
296 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
299 lttv_option_add("cpu_stats", 'c',
300 "write the per cpu statistics",
302 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
304 a_process_stats
= FALSE
;
305 lttv_option_add("process_stats", 'p',
306 "write the per process statistics",
308 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
310 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event",
311 LTTV_POINTER
, &value
));
312 g_assert((event_hook
= *(value
.v_pointer
)) != NULL
);
313 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
315 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
316 LTTV_POINTER
, &value
));
317 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
318 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
320 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
321 LTTV_POINTER
, &value
));
322 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
323 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
326 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
327 LTTV_POINTER
, &value
));
328 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
329 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
333 static void destroy()
335 g_info("Destroy textDump");
337 lttv_option_remove("output");
339 lttv_option_remove("field_names");
341 lttv_option_remove("process_state");
343 lttv_option_remove("cpu_stats");
345 lttv_option_remove("process_stats");
347 g_string_free(a_string
, TRUE
);
349 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
351 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
353 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
355 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
359 LTTV_MODULE("textDump", "Print events in a file", \
360 "Produce a detailed text printout of a trace", \
361 init
, destroy
, "stats", "batchAnalysis", "option", "print")