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>
35 #include <lttv/stats.h>
36 #include <lttv/filter.h>
38 #include <lttv/print.h>
40 #include <ltt/event.h>
41 #include <ltt/trace.h>
45 #include <lttv/event.h>
66 print_path_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
68 int i
, nb
, saved_length
;
70 LttvAttribute
*subtree
;
72 LttvAttributeName name
;
74 LttvAttributeValue value
;
76 LttvAttributeType type
;
80 saved_length
= indent
->len
;
81 nb
= lttv_attribute_get_number(tree
);
82 for(i
= 0 ; i
< nb
; i
++) {
83 type
= lttv_attribute_get(tree
, i
, &name
, &value
, &is_named
);
85 g_string_sprintfa(indent
, "/%s", g_quark_to_string(name
));
87 g_string_sprintfa(indent
, "/%" PRIu32
, (guint32
) name
);
92 fprintf(fp
, "%s: %d\n", indent
->str
, *value
.v_int
);
95 fprintf(fp
, "%s: %d\n", indent
->str
, *value
.v_int
);
98 fprintf(fp
, "%s: %ld\n", indent
->str
, *value
.v_ulong
);
101 fprintf(fp
, "%s: %lu\n", indent
->str
, *value
.v_ulong
);
104 fprintf(fp
, "%s: %f\n", indent
->str
, (double) *value
.v_float
);
107 fprintf(fp
, "%s: %f\n", indent
->str
, *value
.v_double
);
110 fprintf(fp
, "%s: %lu.%09lu\n", indent
->str
, value
.v_time
->tv_sec
, value
.v_time
->tv_nsec
);
113 fprintf(fp
, "%s: POINTER\n", indent
->str
);
116 fprintf(fp
, "%s: %s\n", indent
->str
, *value
.v_string
);
119 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
120 subtree
= (LttvAttribute
*) *(value
.v_gobject
);
121 print_path_tree(fp
, indent
, subtree
);
123 fprintf(fp
, "%s: GOBJECT\n", indent
->str
);
129 g_string_truncate(indent
, saved_length
);
134 print_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
136 int i
, nb
, saved_length
;
138 LttvAttribute
*subtree
;
140 LttvAttributeName name
;
142 LttvAttributeValue value
;
144 LttvAttributeType type
;
148 nb
= lttv_attribute_get_number(tree
);
149 for(i
= 0 ; i
< nb
; i
++) {
150 type
= lttv_attribute_get(tree
, i
, &name
, &value
, &is_named
);
152 fprintf(fp
, "%s%s: ", indent
->str
, g_quark_to_string(name
));
154 fprintf(fp
, "%s%" PRIu32
": ", indent
->str
,
159 fprintf(fp
, "%d\n", *value
.v_int
);
162 fprintf(fp
, "%u\n", *value
.v_uint
);
165 fprintf(fp
, "%ld\n", *value
.v_long
);
168 fprintf(fp
, "%lu\n", *value
.v_ulong
);
171 fprintf(fp
, "%f\n", (double)*value
.v_float
);
174 fprintf(fp
, "%f\n", *value
.v_double
);
177 fprintf(fp
, "%10lu.%09lu\n", value
.v_time
->tv_sec
,
178 value
.v_time
->tv_nsec
);
181 fprintf(fp
, "POINTER\n");
184 fprintf(fp
, "%s\n", *value
.v_string
);
187 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
189 subtree
= (LttvAttribute
*)*(value
.v_gobject
);
190 saved_length
= indent
->len
;
191 indent
= g_string_append(indent
, " ");
192 print_tree(fp
, indent
, subtree
);
193 g_string_truncate(indent
, saved_length
);
195 else fprintf(fp
, "GOBJECT\n");
204 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
206 int i
, nb
, saved_length
;
214 if(tscs
->stats
== NULL
) return;
215 indent
= g_string_new("");
216 fprintf(fp
, "Traceset statistics:\n\n");
218 print_path_tree(fp
, indent
, tscs
->stats
);
220 print_tree(fp
, indent
, tscs
->stats
);
223 ts
= tscs
->parent
.parent
.ts
;
224 nb
= lttv_traceset_number(ts
);
226 for(i
= 0 ; i
< nb
; i
++) {
227 tcs
= (LttvTraceStats
*)(LTTV_TRACESET_CONTEXT(tscs
)->traces
[i
]);
229 LttSystemDescription
*desc
;
230 desc
= ltt_trace_system_description(tcs
->parent
.parent
.t
);
231 LttTime start_time
= ltt_trace_system_description_trace_start_time(desc
);
232 fprintf(fp
, "Trace on system %s at time %lu.%09lu :\n",
233 ltt_trace_system_description_node_name(desc
),
237 saved_length
= indent
->len
;
239 g_string_sprintfa(indent
, "/trace%i", i
);
240 print_path_tree(fp
, indent
, tcs
->stats
);
242 g_string_append(indent
, " ");
243 print_tree(fp
, indent
, tcs
->stats
);
245 g_string_truncate(indent
, saved_length
);
247 g_string_free(indent
, TRUE
);
250 /* Insert the hooks before and after each trace and tracefile, and for each
251 event. Print a global header. */
255 static GString
*a_string
;
257 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
259 LttvTraceset
*traceset
= (LttvTraceset
*)call_data
;
261 g_info("TextDump traceset header");
263 if(a_file_name
== NULL
) a_file
= stdout
;
264 else a_file
= fopen(a_file_name
, "w");
266 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
268 /* Print the trace set header */
269 fprintf(a_file
,"Trace set contains %d traces\n\n",
270 lttv_traceset_number(traceset
));
276 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
278 LttvTraceset
*traceset
= (LttvTraceset
*)call_data
;
280 g_info("TextDump traceset footer");
282 fprintf(a_file
,"End trace set\n\n");
284 if(LTTV_IS_TRACESET_STATS(tc
)) {
285 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
, ltt_time_infinite
);
286 print_stats(a_file
, (LttvTracesetStats
*)tc
);
289 if(a_file_name
!= NULL
) fclose(a_file
);
295 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
298 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
299 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
301 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
302 ltt_trace_system_description_node_name(system
),
303 ltt_trace_system_description_domain_name(system
),
304 ltt_trace_system_description_description(system
));
309 static int write_event_content(void *hook_data
, void *call_data
)
313 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
315 LttvEvent
*event
= (LttvEvent
*)call_data
;
317 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
319 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
323 LttvAttributeValue value_filter
;
327 guint cpu
= tfs
->cpu
;
328 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
329 LttvProcessState
*process
= ts
->running_process
[cpu
];
334 e
= ltt_tracefile_get_event(tfc
->tf
);
336 result
= lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
337 LTTV_POINTER
, &value_filter
);
339 filter
= (LttvFilter
*)*(value_filter
.v_pointer
);
342 * call to the filter if available
344 if(filter
->head
!= NULL
)
345 if(!lttv_filter_tree_parse(filter
->head
,e
,tfc
->tf
,
346 tfc
->t_context
->t
,tfc
,NULL
,NULL
))
350 lttv_event_to_string(e
, a_string
, TRUE
, !a_no_field_names
, tfs
);
353 /* TODO 2012-03-12 Add state info */
354 lttv_event_to_string(event
, a_string
, !a_no_field_names
);
358 g_string_append_printf(a_string
, " %s ",
359 g_quark_to_string(process
->state
->s
));
363 g_string_append_printf(a_string
,"\n");
365 fputs(a_string
->str
, a_file
);
375 LttvAttributeValue value
;
377 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
379 g_info("Init textDump.c");
381 a_string
= g_string_new("");
384 lttv_option_add("output", 'o',
385 "output file where the text is written",
387 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
390 lttv_option_add("noevent", 'n',
391 "disable event printout",
393 LTTV_OPT_NONE
, &a_noevent
, NULL
, NULL
);
395 a_no_field_names
= FALSE
;
396 lttv_option_add("field_names", 's',
397 "do not write the field names for each event",
399 LTTV_OPT_NONE
, &a_no_field_names
, NULL
, NULL
);
402 lttv_option_add("process_state", 'r',
403 "write the pid and state for each event",
405 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
408 lttv_option_add("cpu_stats", 'c',
409 "write the per cpu statistics",
411 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
413 a_process_stats
= FALSE
;
414 lttv_option_add("process_stats", 'p',
415 "write the per process statistics",
417 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
419 a_path_output
= FALSE
;
420 lttv_option_add("path_output", 'a',
421 "print the process stats in path format, for easy grepping",
423 LTTV_OPT_NONE
, &a_path_output
, NULL
, NULL
);
425 result
= lttv_iattribute_find_by_path(attributes
, "hooks/event",
426 LTTV_POINTER
, &value
);
428 event_hook
= *(value
.v_pointer
);
429 g_assert(event_hook
);
430 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
432 result
= lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
433 LTTV_POINTER
, &value
);
435 before_trace
= *(value
.v_pointer
);
436 g_assert(before_trace
);
437 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
439 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
440 LTTV_POINTER
, &value
);
442 before_traceset
= *(value
.v_pointer
);
443 g_assert(before_traceset
);
444 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
447 result
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
448 LTTV_POINTER
, &value
);
450 after_traceset
= *(value
.v_pointer
);
451 g_assert(after_traceset
);
452 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
456 static void destroy()
458 g_info("Destroy textDump");
460 lttv_option_remove("noevent");
462 lttv_option_remove("output");
464 lttv_option_remove("field_names");
466 lttv_option_remove("process_state");
468 lttv_option_remove("cpu_stats");
470 lttv_option_remove("process_stats");
472 lttv_option_remove("path_output");
474 g_string_free(a_string
, TRUE
);
476 lttv_hooks_remove_data(event_hook
, write_event_content
, NULL
);
478 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
480 lttv_hooks_remove_data(before_traceset
, write_traceset_header
, NULL
);
482 lttv_hooks_remove_data(after_traceset
, write_traceset_footer
, NULL
);
486 LTTV_MODULE("textDump", "Print events in a file", \
487 "Produce a detailed text printout of a trace", \
488 init
, destroy
, "batchAnalysis", "option", "print")