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
;
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 fprintf(fp
, "%s%s: ", indent
->str
, g_quark_to_string(name
));
81 fprintf(fp
, "%s%lu: ", indent
->str
, name
);
85 fprintf(fp
, "%d\n", *value
.v_int
);
88 fprintf(fp
, "%u\n", *value
.v_uint
);
91 fprintf(fp
, "%ld\n", *value
.v_long
);
94 fprintf(fp
, "%lu\n", *value
.v_ulong
);
97 fprintf(fp
, "%f\n", (double)*value
.v_float
);
100 fprintf(fp
, "%f\n", *value
.v_double
);
103 fprintf(fp
, "%10lu.%09lu\n", value
.v_time
->tv_sec
,
104 value
.v_time
->tv_nsec
);
107 fprintf(fp
, "POINTER\n");
110 fprintf(fp
, "%s\n", *value
.v_string
);
113 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
115 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
116 saved_length
= indent
->len
;
117 indent
= g_string_append(indent
, " ");
118 print_tree(fp
, indent
, subtree
);
119 g_string_truncate(indent
, saved_length
);
121 else fprintf(fp
, "GOBJECT\n");
131 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
133 int i
, nb
, saved_length
;
141 LttSystemDescription
*desc
;
143 if(tscs
->stats
== NULL
) return;
144 indent
= g_string_new("");
145 fprintf(fp
, "Traceset statistics:\n\n");
146 print_tree(fp
, indent
, tscs
->stats
);
148 ts
= tscs
->parent
.parent
.ts
;
149 nb
= lttv_traceset_number(ts
);
151 for(i
= 0 ; i
< nb
; i
++) {
152 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
154 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
155 LttTime start_time
= ltt_trace_system_description_trace_start_time(desc
);
156 fprintf(fp
, "Trace on system %s at time %lu.%09lu :\n",
157 ltt_trace_system_description_node_name(desc
),
161 saved_length
= indent
->len
;
162 indent
= g_string_append(indent
, " ");
163 print_tree(fp
, indent
, tcs
->stats
);
164 g_string_truncate(indent
, saved_length
);
166 g_string_free(indent
, TRUE
);
170 /* Insert the hooks before and after each trace and tracefile, and for each
171 event. Print a global header. */
175 static GString
*a_string
;
177 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
179 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
181 g_info("TextDump traceset header");
183 if(a_file_name
== NULL
) a_file
= stdout
;
184 else a_file
= fopen(a_file_name
, "w");
186 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
188 /* Print the trace set header */
189 fprintf(a_file
,"Trace set contains %d traces\n\n",
190 lttv_traceset_number(tc
->ts
));
196 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
198 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
200 g_info("TextDump traceset footer");
202 fprintf(a_file
,"End trace set\n\n");
204 if(LTTV_IS_TRACESET_STATS(tc
)) {
205 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
);
206 print_stats(a_file
, (LttvTracesetStats
*)tc
);
209 if(a_file_name
!= NULL
) fclose(a_file
);
215 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
217 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
219 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
221 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
222 ltt_trace_system_description_node_name(system
),
223 ltt_trace_system_description_domain_name(system
),
224 ltt_trace_system_description_description(system
));
230 static int write_event_content(void *hook_data
, void *call_data
)
232 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
234 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
236 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
240 LttvAttributeValue value_filter
;
244 guint cpu
= tfs
->cpu
;
245 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
246 LttvProcessState
*process
= ts
->running_process
[cpu
];
248 e
= ltt_tracefile_get_event(tfc
->tf
);
250 g_assert(lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
251 LTTV_POINTER
, &value_filter
));
252 filter
= (LttvFilter
*)*(value_filter
.v_pointer
);
255 * call to the filter if available
257 if(filter
->head
!= NULL
)
258 if(!lttv_filter_tree_parse(filter
->head
,e
,tfc
->tf
,
259 tfc
->t_context
->t
,tfc
))
262 lttv_event_to_string(e
, a_string
, TRUE
, a_field_names
, tfs
);
263 g_string_append_printf(a_string
,"\n");
266 g_string_append_printf(a_string
, " %s ",
267 g_quark_to_string(process
->state
->s
));
270 fputs(a_string
->str
, a_file
);
277 LttvAttributeValue value
;
279 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
281 g_info("Init textDump.c");
283 a_string
= g_string_new("");
286 lttv_option_add("output", 'o',
287 "output file where the text is written",
289 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
291 a_field_names
= FALSE
;
292 lttv_option_add("field_names", 'l',
293 "write the field names for each event",
295 LTTV_OPT_NONE
, &a_field_names
, NULL
, NULL
);
298 lttv_option_add("process_state", 's',
299 "write the pid and state for each event",
301 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
304 lttv_option_add("cpu_stats", 'c',
305 "write the per cpu statistics",
307 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
309 a_process_stats
= FALSE
;
310 lttv_option_add("process_stats", 'p',
311 "write the per process statistics",
313 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
315 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event",
316 LTTV_POINTER
, &value
));
317 g_assert((event_hook
= *(value
.v_pointer
)) != NULL
);
318 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
320 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
321 LTTV_POINTER
, &value
));
322 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
323 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
325 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
326 LTTV_POINTER
, &value
));
327 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
328 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
331 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
332 LTTV_POINTER
, &value
));
333 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
334 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
338 static void destroy()
340 g_info("Destroy textDump");
342 lttv_option_remove("output");
344 lttv_option_remove("field_names");
346 lttv_option_remove("process_state");
348 lttv_option_remove("cpu_stats");
350 lttv_option_remove("process_stats");
352 g_string_free(a_string
, TRUE
);
354 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
356 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
358 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
360 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
364 LTTV_MODULE("textDump", "Print events in a file", \
365 "Produce a detailed text printout of a trace", \
366 init
, destroy
, "stats", "batchAnalysis", "option", "print")