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>
60 /* Insert the hooks before and after each trace and tracefile, and for each
61 event. Print a global header. */
65 static GString
*a_string
;
67 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
69 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
71 g_info("TextDump traceset header");
73 if(a_file_name
== NULL
) a_file
= stdout
;
74 else a_file
= fopen(a_file_name
, "w");
76 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
78 /* Print the trace set header */
79 fprintf(a_file
,"Trace set contains %d traces\n\n",
80 lttv_traceset_number(tc
->ts
));
86 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
88 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
90 g_info("TextDump traceset footer");
92 fprintf(a_file
,"End trace set\n\n");
94 if(LTTV_IS_TRACESET_STATS(tc
)) {
95 lttv_stats_sum_traceset((LttvTracesetStats
*)tc
);
96 print_stats(a_file
, (LttvTracesetStats
*)tc
);
99 if(a_file_name
!= NULL
) fclose(a_file
);
105 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
107 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
109 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
111 fprintf(a_file
," Trace from %s in %s\n%s\n\n",
112 ltt_trace_system_description_node_name(system
),
113 ltt_trace_system_description_domain_name(system
),
114 ltt_trace_system_description_description(system
));
120 static int for_each_event(void *hook_data
, void *call_data
)
122 guint
*event_count
= (guint
*)hook_data
;
124 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
126 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
128 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
132 LttvAttributeValue value_filter
;
134 /* Only save at LTTV_STATE_SAVE_INTERVAL */
135 if(likely((*event_count
)++ < LTTV_STATE_SAVE_INTERVAL
))
140 guint cpu
= tfs
->cpu
;
141 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
142 LttvProcessState
*process
= ts
->running_process
[cpu
];
144 e
= ltt_tracefile_get_event(tfc
->tf
);
146 lttv_state_write(ts
, tfs
->parent
.timestamp
, a_file
);
154 LttvAttributeValue value
;
156 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
158 g_info("Init textDump.c");
160 a_string
= g_string_new("");
163 lttv_option_add("output", 'o',
164 "output file where the saved states are to be written",
166 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
168 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event",
169 LTTV_POINTER
, &value
));
170 g_assert((event_hook
= *(value
.v_pointer
)) != NULL
);
171 lttv_hooks_add(event_hook
, write_event_content
, NULL
, LTTV_PRIO_DEFAULT
);
173 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
174 LTTV_POINTER
, &value
));
175 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
176 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
178 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
179 LTTV_POINTER
, &value
));
180 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
181 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
184 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
185 LTTV_POINTER
, &value
));
186 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
187 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
191 static void destroy()
193 g_info("Destroy textDump");
195 lttv_option_remove("output");
197 g_string_free(a_string
, TRUE
);
199 lttv_hooks_remove_data(event_hook
, for_each_event
, NULL
);
201 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
203 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
205 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
209 LTTV_MODULE("textDump", "Print events in a file", \
210 "Produce a detailed text printout of a trace", \
211 init
, destroy
, "stats", "batchAnalysis", "option", "print")