9c312311 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Michel Dagenais |
8e680509 |
3 | * 2005 Mathieu Desnoyers |
9c312311 |
4 | * |
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; |
8 | * |
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. |
13 | * |
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, |
17 | * MA 02111-1307, USA. |
18 | */ |
19 | |
48f6f3c2 |
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 |
22 | after each trace. */ |
23 | |
4e4d11b3 |
24 | #ifdef HAVE_CONFIG_H |
25 | #include <config.h> |
26 | #endif |
27 | |
996acd92 |
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> |
b445142a |
34 | #include <lttv/stats.h> |
cec3d7b0 |
35 | #include <lttv/filter.h> |
8e680509 |
36 | #include <lttv/print.h> |
ffd54a90 |
37 | #include <ltt/ltt.h> |
38 | #include <ltt/event.h> |
39 | #include <ltt/type.h> |
40 | #include <ltt/trace.h> |
ba6f11f1 |
41 | #include <ltt/facility.h> |
ffd54a90 |
42 | #include <stdio.h> |
996acd92 |
43 | |
dc877563 |
44 | static gboolean |
45 | a_field_names, |
b445142a |
46 | a_state, |
47 | a_cpu_stats, |
48 | a_process_stats; |
dc877563 |
49 | |
50 | static char |
b445142a |
51 | *a_file_name = NULL; |
dc877563 |
52 | |
53 | static LttvHooks |
54 | *before_traceset, |
55 | *after_traceset, |
56 | *before_trace, |
12c59c3d |
57 | *event_hook; |
dc877563 |
58 | |
48f6f3c2 |
59 | |
b445142a |
60 | static void |
61 | print_tree(FILE *fp, GString *indent, LttvAttribute *tree) |
62 | { |
63 | int i, nb, saved_length; |
64 | |
65 | LttvAttribute *subtree; |
66 | |
67 | LttvAttributeName name; |
68 | |
69 | LttvAttributeValue value; |
70 | |
71 | LttvAttributeType type; |
72 | |
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)); |
77 | |
78 | switch(type) { |
79 | case LTTV_INT: |
80 | fprintf(fp, "%d\n", *value.v_int); |
81 | break; |
82 | case LTTV_UINT: |
83 | fprintf(fp, "%u\n", *value.v_uint); |
84 | break; |
85 | case LTTV_LONG: |
86 | fprintf(fp, "%ld\n", *value.v_long); |
87 | break; |
88 | case LTTV_ULONG: |
89 | fprintf(fp, "%lu\n", *value.v_ulong); |
90 | break; |
91 | case LTTV_FLOAT: |
92 | fprintf(fp, "%f\n", (double)*value.v_float); |
93 | break; |
94 | case LTTV_DOUBLE: |
95 | fprintf(fp, "%f\n", *value.v_double); |
96 | break; |
97 | case LTTV_TIME: |
ba6f11f1 |
98 | fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec, |
b445142a |
99 | value.v_time->tv_nsec); |
100 | break; |
101 | case LTTV_POINTER: |
102 | fprintf(fp, "POINTER\n"); |
103 | break; |
104 | case LTTV_STRING: |
105 | fprintf(fp, "%s\n", *value.v_string); |
106 | break; |
107 | case LTTV_GOBJECT: |
108 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
109 | fprintf(fp, "\n"); |
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); |
115 | } |
116 | else fprintf(fp, "GOBJECT\n"); |
117 | break; |
118 | case LTTV_NONE: |
119 | break; |
120 | } |
121 | } |
122 | } |
123 | |
124 | |
125 | static void |
126 | print_stats(FILE *fp, LttvTracesetStats *tscs) |
127 | { |
128 | int i, nb, saved_length; |
129 | |
130 | LttvTraceset *ts; |
131 | |
132 | LttvTraceStats *tcs; |
133 | |
134 | GString *indent; |
135 | |
136 | LttSystemDescription *desc; |
137 | |
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); |
142 | |
143 | ts = tscs->parent.parent.ts; |
144 | nb = lttv_traceset_number(ts); |
145 | |
146 | for(i = 0 ; i < nb ; i++) { |
147 | tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]); |
9eb6aa7a |
148 | #if 0 //FIXME |
b445142a |
149 | desc = ltt_trace_system_description(tcs->parent.parent.t); |
ba6f11f1 |
150 | LttTime start_time = ltt_trace_system_description_trace_start_time(desc); |
151 | fprintf(fp, "Trace on system %s at time %lu.%09lu :\n", |
a5dcde2f |
152 | ltt_trace_system_description_node_name(desc), |
ba6f11f1 |
153 | start_time.tv_sec, |
154 | start_time.tv_nsec); |
9eb6aa7a |
155 | #endif //FIXME |
b445142a |
156 | saved_length = indent->len; |
157 | g_string_append(indent, " "); |
158 | print_tree(fp, indent, tcs->stats); |
159 | g_string_truncate(indent, saved_length); |
160 | } |
161 | g_string_free(indent, TRUE); |
162 | } |
163 | |
164 | |
dc877563 |
165 | /* Insert the hooks before and after each trace and tracefile, and for each |
166 | event. Print a global header. */ |
167 | |
168 | static FILE *a_file; |
48f6f3c2 |
169 | |
dc877563 |
170 | static GString *a_string; |
171 | |
ffd54a90 |
172 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
48f6f3c2 |
173 | { |
dc877563 |
174 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
48f6f3c2 |
175 | |
b445142a |
176 | g_info("TextDump traceset header"); |
177 | |
dc877563 |
178 | if(a_file_name == NULL) a_file = stdout; |
179 | else a_file = fopen(a_file_name, "w"); |
48f6f3c2 |
180 | |
dc877563 |
181 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
48f6f3c2 |
182 | |
dc877563 |
183 | /* Print the trace set header */ |
184 | fprintf(a_file,"Trace set contains %d traces\n\n", |
ffd54a90 |
185 | lttv_traceset_number(tc->ts)); |
48f6f3c2 |
186 | |
dc877563 |
187 | return FALSE; |
48f6f3c2 |
188 | } |
189 | |
190 | |
ffd54a90 |
191 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
48f6f3c2 |
192 | { |
dc877563 |
193 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
48f6f3c2 |
194 | |
b445142a |
195 | g_info("TextDump traceset footer"); |
196 | |
dc877563 |
197 | fprintf(a_file,"End trace set\n\n"); |
48f6f3c2 |
198 | |
b445142a |
199 | if(LTTV_IS_TRACESET_STATS(tc)) { |
f95bc830 |
200 | lttv_stats_sum_traceset((LttvTracesetStats *)tc); |
b445142a |
201 | print_stats(a_file, (LttvTracesetStats *)tc); |
202 | } |
203 | |
ffd54a90 |
204 | if(a_file_name != NULL) fclose(a_file); |
205 | |
dc877563 |
206 | return FALSE; |
48f6f3c2 |
207 | } |
208 | |
209 | |
dc877563 |
210 | static gboolean write_trace_header(void *hook_data, void *call_data) |
48f6f3c2 |
211 | { |
dc877563 |
212 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
9eb6aa7a |
213 | #if 0 //FIXME |
dc877563 |
214 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
48f6f3c2 |
215 | |
a5dcde2f |
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)); |
9eb6aa7a |
220 | #endif //0 |
dc877563 |
221 | return FALSE; |
48f6f3c2 |
222 | } |
223 | |
224 | |
dc877563 |
225 | static int write_event_content(void *hook_data, void *call_data) |
48f6f3c2 |
226 | { |
8bf54995 |
227 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
228 | |
dc877563 |
229 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
48f6f3c2 |
230 | |
dc877563 |
231 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
48f6f3c2 |
232 | |
dc877563 |
233 | LttEvent *e; |
48f6f3c2 |
234 | |
8bf54995 |
235 | LttvAttributeValue value_filter; |
236 | |
237 | LttvFilter *filter; |
238 | |
d730b5c8 |
239 | guint cpu = ltt_tracefile_num(tfs->parent.tf); |
240 | LttvTraceState *ts = (LttvTraceState*)tfc->t_context; |
241 | LttvProcessState *process = ts->running_process[cpu]; |
48f6f3c2 |
242 | |
d730b5c8 |
243 | e = ltt_tracefile_get_event(tfc->tf); |
8bf54995 |
244 | |
245 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/lttv_filter", |
246 | LTTV_POINTER, &value_filter)); |
247 | filter = (LttvFilter*)*(value_filter.v_pointer); |
248 | |
cec3d7b0 |
249 | /* |
250 | * call to the filter if available |
251 | */ |
0bc23ba9 |
252 | if(filter->head != NULL) |
d730b5c8 |
253 | if(!lttv_filter_tree_parse(filter->head,e,tfc->tf, |
8e680509 |
254 | tfc->t_context->t,tfc)) |
8ff6243c |
255 | return FALSE; |
cec3d7b0 |
256 | |
ba6f11f1 |
257 | lttv_event_to_string(e, a_string, TRUE, a_field_names, tfs); |
c6bc9cb9 |
258 | g_string_append_printf(a_string,"\n"); |
dc877563 |
259 | |
260 | if(a_state) { |
9d0bb8d1 |
261 | g_string_append_printf(a_string, " %s ", |
d730b5c8 |
262 | g_quark_to_string(process->state->s)); |
dc877563 |
263 | } |
48f6f3c2 |
264 | |
ffd54a90 |
265 | fputs(a_string->str, a_file); |
dc877563 |
266 | return FALSE; |
48f6f3c2 |
267 | } |
268 | |
269 | |
08b1c66e |
270 | static void init() |
48f6f3c2 |
271 | { |
ffd54a90 |
272 | LttvAttributeValue value; |
48f6f3c2 |
273 | |
ffd54a90 |
274 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
48f6f3c2 |
275 | |
b445142a |
276 | g_info("Init textDump.c"); |
9402662d |
277 | |
c6bc9cb9 |
278 | a_string = g_string_new(""); |
279 | |
ffd54a90 |
280 | a_file_name = NULL; |
281 | lttv_option_add("output", 'o', |
282 | "output file where the text is written", |
283 | "file name", |
284 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
48f6f3c2 |
285 | |
ffd54a90 |
286 | a_field_names = FALSE; |
287 | lttv_option_add("field_names", 'l', |
288 | "write the field names for each event", |
289 | "", |
290 | LTTV_OPT_NONE, &a_field_names, NULL, NULL); |
48f6f3c2 |
291 | |
ffd54a90 |
292 | a_state = FALSE; |
293 | lttv_option_add("process_state", 's', |
294 | "write the pid and state for each event", |
295 | "", |
296 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
dc877563 |
297 | |
b445142a |
298 | a_cpu_stats = FALSE; |
9d0bb8d1 |
299 | lttv_option_add("cpu_stats", 'c', |
b445142a |
300 | "write the per cpu statistics", |
301 | "", |
302 | LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL); |
303 | |
304 | a_process_stats = FALSE; |
9d0bb8d1 |
305 | lttv_option_add("process_stats", 'p', |
b445142a |
306 | "write the per process statistics", |
307 | "", |
308 | LTTV_OPT_NONE, &a_process_stats, NULL, NULL); |
309 | |
12c59c3d |
310 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event", |
ffd54a90 |
311 | LTTV_POINTER, &value)); |
12c59c3d |
312 | g_assert((event_hook = *(value.v_pointer)) != NULL); |
313 | lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT); |
dc877563 |
314 | |
ffd54a90 |
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); |
12c59c3d |
318 | lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT); |
dc877563 |
319 | |
ffd54a90 |
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); |
12c59c3d |
323 | lttv_hooks_add(before_traceset, write_traceset_header, NULL, |
324 | LTTV_PRIO_DEFAULT); |
dc877563 |
325 | |
ffd54a90 |
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); |
12c59c3d |
329 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL, |
330 | LTTV_PRIO_DEFAULT); |
ffd54a90 |
331 | } |
48f6f3c2 |
332 | |
08b1c66e |
333 | static void destroy() |
ffd54a90 |
334 | { |
b445142a |
335 | g_info("Destroy textDump"); |
336 | |
ffd54a90 |
337 | lttv_option_remove("output"); |
48f6f3c2 |
338 | |
ffd54a90 |
339 | lttv_option_remove("field_names"); |
48f6f3c2 |
340 | |
ffd54a90 |
341 | lttv_option_remove("process_state"); |
48f6f3c2 |
342 | |
b445142a |
343 | lttv_option_remove("cpu_stats"); |
344 | |
345 | lttv_option_remove("process_stats"); |
346 | |
c6bc9cb9 |
347 | g_string_free(a_string, TRUE); |
348 | |
12c59c3d |
349 | lttv_hooks_remove_data(event_hook, write_event_content, NULL); |
48f6f3c2 |
350 | |
ffd54a90 |
351 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
48f6f3c2 |
352 | |
ffd54a90 |
353 | lttv_hooks_remove_data(before_trace, write_traceset_header, NULL); |
48f6f3c2 |
354 | |
ffd54a90 |
355 | lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL); |
48f6f3c2 |
356 | } |
357 | |
358 | |
08b1c66e |
359 | LTTV_MODULE("textDump", "Print events in a file", \ |
360 | "Produce a detailed text printout of a trace", \ |
8e680509 |
361 | init, destroy, "stats", "batchAnalysis", "option", "print") |
48f6f3c2 |
362 | |