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