13ab9fcb |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Michel Dagenais |
3 | * 2005 Mathieu Desnoyers |
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 | |
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 | |
24 | #ifdef HAVE_CONFIG_H |
25 | #include <config.h> |
26 | #endif |
27 | |
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> |
37 | #include <ltt/ltt.h> |
38 | #include <ltt/event.h> |
39 | #include <ltt/trace.h> |
40 | #include <stdio.h> |
41 | |
42 | static gboolean |
43 | a_no_field_names, |
44 | a_state, |
45 | a_cpu_stats, |
46 | a_process_stats, |
47 | a_path_output; |
48 | |
49 | static char |
50 | *a_file_name = NULL; |
51 | |
52 | static LttvHooks |
53 | *before_traceset, |
54 | *after_traceset, |
55 | *before_trace, |
56 | *event_hook; |
57 | |
58 | |
59 | static void |
60 | print_path_tree(FILE *fp, GString *indent, LttvAttribute *tree) |
61 | { |
62 | int i, nb, saved_length; |
63 | |
64 | LttvAttribute *subtree; |
65 | |
66 | LttvAttributeName name; |
67 | |
68 | LttvAttributeValue value; |
69 | |
70 | LttvAttributeType type; |
71 | |
72 | gboolean is_named; |
73 | |
74 | saved_length = indent->len; |
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); |
78 | if(is_named) { |
79 | g_string_sprintfa(indent, "/%s", g_quark_to_string(name)); |
80 | } else { |
81 | g_string_sprintfa(indent, "/%s", name); |
82 | } |
83 | |
84 | switch(type) { |
85 | case LTTV_INT: |
86 | fprintf(fp, "%s: %d\n", indent->str, *value.v_int); |
87 | break; |
88 | case LTTV_UINT: |
89 | fprintf(fp, "%s: %d\n", indent->str, *value.v_int); |
90 | break; |
91 | case LTTV_LONG: |
92 | fprintf(fp, "%s: %ld\n", indent->str, *value.v_ulong); |
93 | break; |
94 | case LTTV_ULONG: |
95 | fprintf(fp, "%s: %lu\n", indent->str, *value.v_ulong); |
96 | break; |
97 | case LTTV_FLOAT: |
98 | fprintf(fp, "%s: %f\n", indent->str, (double) *value.v_float); |
99 | break; |
100 | case LTTV_DOUBLE: |
101 | fprintf(fp, "%s: %f\n", indent->str, *value.v_double); |
102 | break; |
103 | case LTTV_TIME: |
104 | fprintf(fp, "%s: %lu.%09lu\n", indent->str, value.v_time->tv_sec, value.v_time->tv_nsec); |
105 | break; |
106 | case LTTV_POINTER: |
107 | fprintf(fp, "%s: POINTER\n", indent->str); |
108 | break; |
109 | case LTTV_STRING: |
110 | fprintf(fp, "%s: %s\n", indent->str, *value.v_string); |
111 | break; |
112 | case LTTV_GOBJECT: |
113 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
114 | subtree = (LttvAttribute*) *(value.v_gobject); |
115 | print_path_tree(fp, indent, subtree); |
116 | } else { |
117 | fprintf(fp, "%s: GOBJECT\n", indent->str); |
118 | } |
119 | break; |
120 | case LTTV_NONE: |
121 | break; |
122 | } |
123 | g_string_truncate(indent, saved_length); |
124 | } |
125 | } |
126 | |
127 | static void |
128 | print_tree(FILE *fp, GString *indent, LttvAttribute *tree) |
129 | { |
130 | int i, nb, saved_length; |
131 | |
132 | LttvAttribute *subtree; |
133 | |
134 | LttvAttributeName name; |
135 | |
136 | LttvAttributeValue value; |
137 | |
138 | LttvAttributeType type; |
139 | |
140 | gboolean is_named; |
141 | |
142 | nb = lttv_attribute_get_number(tree); |
143 | for(i = 0 ; i < nb ; i++) { |
144 | type = lttv_attribute_get(tree, i, &name, &value, &is_named); |
145 | if(is_named) |
146 | fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name)); |
147 | else |
148 | fprintf(fp, "%s%lu: ", indent->str, name); |
149 | |
150 | switch(type) { |
151 | case LTTV_INT: |
152 | fprintf(fp, "%d\n", *value.v_int); |
153 | break; |
154 | case LTTV_UINT: |
155 | fprintf(fp, "%u\n", *value.v_uint); |
156 | break; |
157 | case LTTV_LONG: |
158 | fprintf(fp, "%ld\n", *value.v_long); |
159 | break; |
160 | case LTTV_ULONG: |
161 | fprintf(fp, "%lu\n", *value.v_ulong); |
162 | break; |
163 | case LTTV_FLOAT: |
164 | fprintf(fp, "%f\n", (double)*value.v_float); |
165 | break; |
166 | case LTTV_DOUBLE: |
167 | fprintf(fp, "%f\n", *value.v_double); |
168 | break; |
169 | case LTTV_TIME: |
170 | fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec, |
171 | value.v_time->tv_nsec); |
172 | break; |
173 | case LTTV_POINTER: |
174 | fprintf(fp, "POINTER\n"); |
175 | break; |
176 | case LTTV_STRING: |
177 | fprintf(fp, "%s\n", *value.v_string); |
178 | break; |
179 | case LTTV_GOBJECT: |
180 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
181 | fprintf(fp, "\n"); |
182 | subtree = (LttvAttribute *)*(value.v_gobject); |
183 | saved_length = indent->len; |
184 | indent = g_string_append(indent, " "); |
185 | print_tree(fp, indent, subtree); |
186 | g_string_truncate(indent, saved_length); |
187 | } |
188 | else fprintf(fp, "GOBJECT\n"); |
189 | break; |
190 | case LTTV_NONE: |
191 | break; |
192 | } |
193 | } |
194 | } |
195 | |
196 | static void |
197 | print_stats(FILE *fp, LttvTracesetStats *tscs) |
198 | { |
199 | int i, nb, saved_length; |
200 | |
201 | LttvTraceset *ts; |
202 | |
203 | LttvTraceStats *tcs; |
204 | |
205 | GString *indent; |
206 | |
207 | LttSystemDescription *desc; |
208 | |
209 | if(tscs->stats == NULL) return; |
210 | indent = g_string_new(""); |
211 | fprintf(fp, "Traceset statistics:\n\n"); |
212 | if(a_path_output) { |
213 | print_path_tree(fp, indent, tscs->stats); |
214 | } else { |
215 | print_tree(fp, indent, tscs->stats); |
216 | } |
217 | |
218 | ts = tscs->parent.parent.ts; |
219 | nb = lttv_traceset_number(ts); |
220 | |
221 | for(i = 0 ; i < nb ; i++) { |
222 | tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]); |
223 | #if 0 //FIXME |
224 | desc = ltt_trace_system_description(tcs->parent.parent.t); |
225 | LttTime start_time = ltt_trace_system_description_trace_start_time(desc); |
226 | fprintf(fp, "Trace on system %s at time %lu.%09lu :\n", |
227 | ltt_trace_system_description_node_name(desc), |
228 | start_time.tv_sec, |
229 | start_time.tv_nsec); |
230 | #endif //FIXME |
231 | saved_length = indent->len; |
232 | if(a_path_output) { |
233 | g_string_sprintfa(indent, "/trace%i", i); |
234 | print_path_tree(fp, indent, tcs->stats); |
235 | } else { |
236 | g_string_append(indent, " "); |
237 | print_tree(fp, indent, tcs->stats); |
238 | } |
239 | g_string_truncate(indent, saved_length); |
240 | } |
241 | g_string_free(indent, TRUE); |
242 | } |
243 | |
244 | /* Insert the hooks before and after each trace and tracefile, and for each |
245 | event. Print a global header. */ |
246 | |
247 | static FILE *a_file; |
248 | |
249 | static GString *a_string; |
250 | |
251 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
252 | { |
253 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
254 | |
255 | g_info("TextDump traceset header"); |
256 | |
257 | if(a_file_name == NULL) a_file = stdout; |
258 | else a_file = fopen(a_file_name, "w"); |
259 | |
260 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
261 | |
262 | /* Print the trace set header */ |
263 | fprintf(a_file,"Trace set contains %d traces\n\n", |
264 | lttv_traceset_number(tc->ts)); |
265 | |
266 | return FALSE; |
267 | } |
268 | |
269 | |
270 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
271 | { |
272 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
273 | |
274 | g_info("TextDump traceset footer"); |
275 | |
276 | fprintf(a_file,"End trace set\n\n"); |
277 | |
278 | if(LTTV_IS_TRACESET_STATS(tc)) { |
279 | lttv_stats_sum_traceset((LttvTracesetStats *)tc, ltt_time_infinite); |
280 | print_stats(a_file, (LttvTracesetStats *)tc); |
281 | } |
282 | |
283 | if(a_file_name != NULL) fclose(a_file); |
284 | |
285 | return FALSE; |
286 | } |
287 | |
288 | |
289 | static gboolean write_trace_header(void *hook_data, void *call_data) |
290 | { |
291 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
292 | #if 0 //FIXME |
293 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
294 | |
295 | fprintf(a_file," Trace from %s in %s\n%s\n\n", |
296 | ltt_trace_system_description_node_name(system), |
297 | ltt_trace_system_description_domain_name(system), |
298 | ltt_trace_system_description_description(system)); |
299 | #endif //0 |
300 | return FALSE; |
301 | } |
302 | |
303 | |
304 | static int write_event_content(void *hook_data, void *call_data) |
305 | { |
306 | gboolean result; |
307 | |
308 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
309 | |
310 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
311 | |
312 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
313 | |
314 | LttEvent *e; |
315 | |
316 | LttvAttributeValue value_filter; |
317 | |
318 | LttvFilter *filter; |
319 | |
320 | guint cpu = tfs->cpu; |
321 | LttvTraceState *ts = (LttvTraceState*)tfc->t_context; |
322 | LttvProcessState *process = ts->running_process[cpu]; |
323 | |
324 | e = ltt_tracefile_get_event(tfc->tf); |
325 | |
326 | result = lttv_iattribute_find_by_path(attributes, "filter/lttv_filter", |
327 | LTTV_POINTER, &value_filter); |
328 | g_assert(result); |
329 | filter = (LttvFilter*)*(value_filter.v_pointer); |
330 | |
331 | /* |
332 | * call to the filter if available |
333 | */ |
334 | if(filter->head != NULL) |
335 | if(!lttv_filter_tree_parse(filter->head,e,tfc->tf, |
336 | tfc->t_context->t,tfc,NULL,NULL)) |
337 | return FALSE; |
338 | |
339 | lttv_event_to_string(e, a_string, TRUE, !a_no_field_names, tfs); |
340 | |
341 | if(a_state) { |
342 | g_string_append_printf(a_string, " %s ", |
343 | g_quark_to_string(process->state->s)); |
344 | } |
345 | |
346 | g_string_append_printf(a_string,"\n"); |
347 | |
348 | fputs(a_string->str, a_file); |
349 | return FALSE; |
350 | } |
351 | |
352 | |
353 | static void init() |
354 | { |
355 | gboolean result; |
356 | |
357 | LttvAttributeValue value; |
358 | |
359 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
360 | |
361 | g_info("Init textDump.c"); |
362 | |
363 | a_string = g_string_new(""); |
364 | |
365 | a_file_name = NULL; |
366 | lttv_option_add("output", 'o', |
367 | "output file where the text is written", |
368 | "file name", |
369 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
370 | |
371 | a_no_field_names = FALSE; |
372 | lttv_option_add("field_names", 's', |
373 | "do not write the field names for each event", |
374 | "", |
375 | LTTV_OPT_NONE, &a_no_field_names, NULL, NULL); |
376 | |
377 | a_state = FALSE; |
378 | lttv_option_add("process_state", 'r', |
379 | "write the pid and state for each event", |
380 | "", |
381 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
382 | |
383 | a_cpu_stats = FALSE; |
384 | lttv_option_add("cpu_stats", 'c', |
385 | "write the per cpu statistics", |
386 | "", |
387 | LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL); |
388 | |
389 | a_process_stats = FALSE; |
390 | lttv_option_add("process_stats", 'p', |
391 | "write the per process statistics", |
392 | "", |
393 | LTTV_OPT_NONE, &a_process_stats, NULL, NULL); |
394 | |
395 | a_path_output = FALSE; |
396 | lttv_option_add("path_output", 'a', |
397 | "print the process stats in path format, for easy grepping", |
398 | "", |
399 | LTTV_OPT_NONE, &a_path_output, NULL, NULL); |
400 | |
401 | result = lttv_iattribute_find_by_path(attributes, "hooks/event", |
402 | LTTV_POINTER, &value); |
403 | g_assert(result); |
404 | event_hook = *(value.v_pointer); |
405 | g_assert(event_hook); |
406 | lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT); |
407 | |
408 | result = lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
409 | LTTV_POINTER, &value); |
410 | g_assert(result); |
411 | before_trace = *(value.v_pointer); |
412 | g_assert(before_trace); |
413 | lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT); |
414 | |
415 | result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
416 | LTTV_POINTER, &value); |
417 | g_assert(result); |
418 | before_traceset = *(value.v_pointer); |
419 | g_assert(before_traceset); |
420 | lttv_hooks_add(before_traceset, write_traceset_header, NULL, |
421 | LTTV_PRIO_DEFAULT); |
422 | |
423 | result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
424 | LTTV_POINTER, &value); |
425 | g_assert(result); |
426 | after_traceset = *(value.v_pointer); |
427 | g_assert(after_traceset); |
428 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL, |
429 | LTTV_PRIO_DEFAULT); |
430 | } |
431 | |
432 | static void destroy() |
433 | { |
434 | g_info("Destroy textDump"); |
435 | |
436 | lttv_option_remove("output"); |
437 | |
438 | lttv_option_remove("field_names"); |
439 | |
440 | lttv_option_remove("process_state"); |
441 | |
442 | lttv_option_remove("cpu_stats"); |
443 | |
444 | lttv_option_remove("process_stats"); |
445 | |
446 | lttv_option_remove("path_output"); |
447 | |
448 | g_string_free(a_string, TRUE); |
449 | |
450 | lttv_hooks_remove_data(event_hook, write_event_content, NULL); |
451 | |
452 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
453 | |
454 | lttv_hooks_remove_data(before_traceset, write_traceset_header, NULL); |
455 | |
456 | lttv_hooks_remove_data(after_traceset, write_traceset_footer, NULL); |
457 | } |
458 | |
459 | |
460 | LTTV_MODULE("textDump", "Print events in a file", \ |
461 | "Produce a detailed text printout of a trace", \ |
462 | init, destroy, "stats", "batchAnalysis", "option", "print") |
463 | |