9c312311 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Michel Dagenais |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License Version 2 as |
6 | * published by the Free Software Foundation; |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
16 | * MA 02111-1307, USA. |
17 | */ |
18 | |
48f6f3c2 |
19 | /* The text dump facility needs to print headers before the trace set and |
20 | before each trace, to print each event, and to print statistics |
21 | after each trace. */ |
22 | |
996acd92 |
23 | #include <lttv/lttv.h> |
24 | #include <lttv/option.h> |
25 | #include <lttv/module.h> |
26 | #include <lttv/hook.h> |
27 | #include <lttv/attribute.h> |
28 | #include <lttv/iattribute.h> |
b445142a |
29 | #include <lttv/stats.h> |
ffd54a90 |
30 | #include <ltt/ltt.h> |
31 | #include <ltt/event.h> |
32 | #include <ltt/type.h> |
33 | #include <ltt/trace.h> |
ba6f11f1 |
34 | #include <ltt/facility.h> |
ffd54a90 |
35 | #include <stdio.h> |
996acd92 |
36 | |
dc877563 |
37 | static gboolean |
38 | a_field_names, |
b445142a |
39 | a_state, |
40 | a_cpu_stats, |
41 | a_process_stats; |
dc877563 |
42 | |
43 | static char |
b445142a |
44 | *a_file_name = NULL; |
dc877563 |
45 | |
46 | static LttvHooks |
47 | *before_traceset, |
48 | *after_traceset, |
49 | *before_trace, |
12c59c3d |
50 | *event_hook; |
dc877563 |
51 | |
f60d7a47 |
52 | |
ffd54a90 |
53 | void print_field(LttEvent *e, LttField *f, GString *s, gboolean field_names) { |
dc877563 |
54 | |
ffd54a90 |
55 | LttType *type; |
dc877563 |
56 | |
ffd54a90 |
57 | LttField *element; |
dc877563 |
58 | |
ffd54a90 |
59 | char *name; |
dc877563 |
60 | |
ffd54a90 |
61 | int nb, i; |
dc877563 |
62 | |
ffd54a90 |
63 | type = ltt_field_type(f); |
64 | switch(ltt_type_class(type)) { |
65 | case LTT_INT: |
66 | g_string_append_printf(s, " %ld", ltt_event_get_long_int(e,f)); |
67 | break; |
dc877563 |
68 | |
ffd54a90 |
69 | case LTT_UINT: |
70 | g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f)); |
71 | break; |
dc877563 |
72 | |
ffd54a90 |
73 | case LTT_FLOAT: |
74 | g_string_append_printf(s, " %g", ltt_event_get_double(e,f)); |
75 | break; |
dc877563 |
76 | |
ffd54a90 |
77 | case LTT_STRING: |
78 | g_string_append_printf(s, " \"%s\"", ltt_event_get_string(e,f)); |
79 | break; |
80 | |
81 | case LTT_ENUM: |
82 | g_string_append_printf(s, " %s", ltt_enum_string_get(type, |
c6bc9cb9 |
83 | ltt_event_get_unsigned(e,f)-1)); |
ffd54a90 |
84 | break; |
85 | |
86 | case LTT_ARRAY: |
87 | case LTT_SEQUENCE: |
88 | g_string_append_printf(s, " {"); |
89 | nb = ltt_event_field_element_number(e,f); |
90 | element = ltt_field_element(f); |
91 | for(i = 0 ; i < nb ; i++) { |
92 | ltt_event_field_element_select(e,f,i); |
93 | print_field(e, element, s, field_names); |
94 | } |
95 | g_string_append_printf(s, " }"); |
96 | break; |
97 | |
98 | case LTT_STRUCT: |
99 | g_string_append_printf(s, " {"); |
100 | nb = ltt_type_member_number(type); |
101 | for(i = 0 ; i < nb ; i++) { |
102 | element = ltt_field_member(f,i); |
c432246e |
103 | if(field_names) { |
ffd54a90 |
104 | ltt_type_member_type(type, i, &name); |
c432246e |
105 | g_string_append_printf(s, " %s = ", name); |
ffd54a90 |
106 | } |
107 | print_field(e, element, s, field_names); |
108 | } |
109 | g_string_append_printf(s, " }"); |
110 | break; |
ba6f11f1 |
111 | |
112 | case LTT_UNION: |
113 | g_string_append_printf(s, " {"); |
114 | nb = ltt_type_member_number(type); |
115 | for(i = 0 ; i < nb ; i++) { |
116 | element = ltt_field_member(f,i); |
117 | if(field_names) { |
118 | ltt_type_member_type(type, i, &name); |
119 | g_string_append_printf(s, " %s = ", name); |
120 | } |
121 | print_field(e, element, s, field_names); |
122 | } |
123 | g_string_append_printf(s, " }"); |
124 | break; |
125 | |
ffd54a90 |
126 | } |
48f6f3c2 |
127 | } |
128 | |
129 | |
ba6f11f1 |
130 | void lttv_event_to_string(LttEvent *e, GString *s, |
3d27549e |
131 | gboolean mandatory_fields, gboolean field_names, LttvTracefileState *tfs) |
132 | { |
ffd54a90 |
133 | LttFacility *facility; |
48f6f3c2 |
134 | |
ffd54a90 |
135 | LttEventType *event_type; |
48f6f3c2 |
136 | |
ffd54a90 |
137 | LttType *type; |
138 | |
139 | LttField *field; |
48f6f3c2 |
140 | |
ffd54a90 |
141 | LttTime time; |
48f6f3c2 |
142 | |
ffd54a90 |
143 | g_string_set_size(s,0); |
dc877563 |
144 | |
ffd54a90 |
145 | facility = ltt_event_facility(e); |
146 | event_type = ltt_event_eventtype(e); |
147 | field = ltt_event_field(e); |
48f6f3c2 |
148 | |
ffd54a90 |
149 | if(mandatory_fields) { |
150 | time = ltt_event_time(e); |
b445142a |
151 | g_string_append_printf(s,"%s.%s: %ld.%09ld (%s)", |
152 | ltt_facility_name(facility), |
ffd54a90 |
153 | ltt_eventtype_name(event_type), (long)time.tv_sec, time.tv_nsec, |
b445142a |
154 | g_quark_to_string(tfs->cpu_name)); |
c432246e |
155 | /* Print the process id and the state/interrupt type of the process */ |
07bb8c8f |
156 | g_string_append_printf(s,", %u, %u, %s", tfs->process->pid, |
157 | tfs->process->ppid, |
158 | g_quark_to_string(tfs->process->state->t)); |
ffd54a90 |
159 | } |
160 | |
c6bc9cb9 |
161 | if(field) |
162 | print_field(e, field, s, field_names); |
ffd54a90 |
163 | } |
48f6f3c2 |
164 | |
165 | |
b445142a |
166 | static void |
167 | print_tree(FILE *fp, GString *indent, LttvAttribute *tree) |
168 | { |
169 | int i, nb, saved_length; |
170 | |
171 | LttvAttribute *subtree; |
172 | |
173 | LttvAttributeName name; |
174 | |
175 | LttvAttributeValue value; |
176 | |
177 | LttvAttributeType type; |
178 | |
179 | nb = lttv_attribute_get_number(tree); |
180 | for(i = 0 ; i < nb ; i++) { |
181 | type = lttv_attribute_get(tree, i, &name, &value); |
182 | fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name)); |
183 | |
184 | switch(type) { |
185 | case LTTV_INT: |
186 | fprintf(fp, "%d\n", *value.v_int); |
187 | break; |
188 | case LTTV_UINT: |
189 | fprintf(fp, "%u\n", *value.v_uint); |
190 | break; |
191 | case LTTV_LONG: |
192 | fprintf(fp, "%ld\n", *value.v_long); |
193 | break; |
194 | case LTTV_ULONG: |
195 | fprintf(fp, "%lu\n", *value.v_ulong); |
196 | break; |
197 | case LTTV_FLOAT: |
198 | fprintf(fp, "%f\n", (double)*value.v_float); |
199 | break; |
200 | case LTTV_DOUBLE: |
201 | fprintf(fp, "%f\n", *value.v_double); |
202 | break; |
203 | case LTTV_TIME: |
ba6f11f1 |
204 | fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec, |
b445142a |
205 | value.v_time->tv_nsec); |
206 | break; |
207 | case LTTV_POINTER: |
208 | fprintf(fp, "POINTER\n"); |
209 | break; |
210 | case LTTV_STRING: |
211 | fprintf(fp, "%s\n", *value.v_string); |
212 | break; |
213 | case LTTV_GOBJECT: |
214 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
215 | fprintf(fp, "\n"); |
216 | subtree = (LttvAttribute *)*(value.v_gobject); |
217 | saved_length = indent->len; |
218 | g_string_append(indent, " "); |
219 | print_tree(fp, indent, subtree); |
220 | g_string_truncate(indent, saved_length); |
221 | } |
222 | else fprintf(fp, "GOBJECT\n"); |
223 | break; |
224 | case LTTV_NONE: |
225 | break; |
226 | } |
227 | } |
228 | } |
229 | |
230 | |
231 | static void |
232 | print_stats(FILE *fp, LttvTracesetStats *tscs) |
233 | { |
234 | int i, nb, saved_length; |
235 | |
236 | LttvTraceset *ts; |
237 | |
238 | LttvTraceStats *tcs; |
239 | |
240 | GString *indent; |
241 | |
242 | LttSystemDescription *desc; |
243 | |
244 | if(tscs->stats == NULL) return; |
245 | indent = g_string_new(""); |
246 | fprintf(fp, "Traceset statistics:\n\n"); |
247 | print_tree(fp, indent, tscs->stats); |
248 | |
249 | ts = tscs->parent.parent.ts; |
250 | nb = lttv_traceset_number(ts); |
251 | |
252 | for(i = 0 ; i < nb ; i++) { |
253 | tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]); |
254 | desc = ltt_trace_system_description(tcs->parent.parent.t); |
ba6f11f1 |
255 | LttTime start_time = ltt_trace_system_description_trace_start_time(desc); |
256 | fprintf(fp, "Trace on system %s at time %lu.%09lu :\n", |
a5dcde2f |
257 | ltt_trace_system_description_node_name(desc), |
ba6f11f1 |
258 | start_time.tv_sec, |
259 | start_time.tv_nsec); |
b445142a |
260 | saved_length = indent->len; |
261 | g_string_append(indent, " "); |
262 | print_tree(fp, indent, tcs->stats); |
263 | g_string_truncate(indent, saved_length); |
264 | } |
265 | g_string_free(indent, TRUE); |
266 | } |
267 | |
268 | |
dc877563 |
269 | /* Insert the hooks before and after each trace and tracefile, and for each |
270 | event. Print a global header. */ |
271 | |
272 | static FILE *a_file; |
48f6f3c2 |
273 | |
dc877563 |
274 | static GString *a_string; |
275 | |
ffd54a90 |
276 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
48f6f3c2 |
277 | { |
dc877563 |
278 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
48f6f3c2 |
279 | |
b445142a |
280 | g_info("TextDump traceset header"); |
281 | |
dc877563 |
282 | if(a_file_name == NULL) a_file = stdout; |
283 | else a_file = fopen(a_file_name, "w"); |
48f6f3c2 |
284 | |
dc877563 |
285 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
48f6f3c2 |
286 | |
dc877563 |
287 | /* Print the trace set header */ |
288 | fprintf(a_file,"Trace set contains %d traces\n\n", |
ffd54a90 |
289 | lttv_traceset_number(tc->ts)); |
48f6f3c2 |
290 | |
dc877563 |
291 | return FALSE; |
48f6f3c2 |
292 | } |
293 | |
294 | |
ffd54a90 |
295 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
48f6f3c2 |
296 | { |
dc877563 |
297 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
48f6f3c2 |
298 | |
b445142a |
299 | g_info("TextDump traceset footer"); |
300 | |
dc877563 |
301 | fprintf(a_file,"End trace set\n\n"); |
48f6f3c2 |
302 | |
b445142a |
303 | if(LTTV_IS_TRACESET_STATS(tc)) { |
f95bc830 |
304 | lttv_stats_sum_traceset((LttvTracesetStats *)tc); |
b445142a |
305 | print_stats(a_file, (LttvTracesetStats *)tc); |
306 | } |
307 | |
ffd54a90 |
308 | if(a_file_name != NULL) fclose(a_file); |
309 | |
dc877563 |
310 | return FALSE; |
48f6f3c2 |
311 | } |
312 | |
313 | |
dc877563 |
314 | static gboolean write_trace_header(void *hook_data, void *call_data) |
48f6f3c2 |
315 | { |
dc877563 |
316 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
317 | |
318 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
48f6f3c2 |
319 | |
a5dcde2f |
320 | fprintf(a_file," Trace from %s in %s\n%s\n\n", |
321 | ltt_trace_system_description_node_name(system), |
322 | ltt_trace_system_description_domain_name(system), |
323 | ltt_trace_system_description_description(system)); |
dc877563 |
324 | return FALSE; |
48f6f3c2 |
325 | } |
326 | |
327 | |
dc877563 |
328 | static int write_event_content(void *hook_data, void *call_data) |
48f6f3c2 |
329 | { |
dc877563 |
330 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
48f6f3c2 |
331 | |
dc877563 |
332 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
48f6f3c2 |
333 | |
dc877563 |
334 | LttEvent *e; |
48f6f3c2 |
335 | |
dc877563 |
336 | e = tfc->e; |
48f6f3c2 |
337 | |
ba6f11f1 |
338 | lttv_event_to_string(e, a_string, TRUE, a_field_names, tfs); |
c6bc9cb9 |
339 | g_string_append_printf(a_string,"\n"); |
dc877563 |
340 | |
341 | if(a_state) { |
9d0bb8d1 |
342 | g_string_append_printf(a_string, " %s ", |
ffd54a90 |
343 | g_quark_to_string(tfs->process->state->s)); |
dc877563 |
344 | } |
48f6f3c2 |
345 | |
ffd54a90 |
346 | fputs(a_string->str, a_file); |
dc877563 |
347 | return FALSE; |
48f6f3c2 |
348 | } |
349 | |
350 | |
08b1c66e |
351 | static void init() |
48f6f3c2 |
352 | { |
ffd54a90 |
353 | LttvAttributeValue value; |
48f6f3c2 |
354 | |
ffd54a90 |
355 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
48f6f3c2 |
356 | |
b445142a |
357 | g_info("Init textDump.c"); |
9402662d |
358 | |
c6bc9cb9 |
359 | a_string = g_string_new(""); |
360 | |
ffd54a90 |
361 | a_file_name = NULL; |
362 | lttv_option_add("output", 'o', |
363 | "output file where the text is written", |
364 | "file name", |
365 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
48f6f3c2 |
366 | |
ffd54a90 |
367 | a_field_names = FALSE; |
368 | lttv_option_add("field_names", 'l', |
369 | "write the field names for each event", |
370 | "", |
371 | LTTV_OPT_NONE, &a_field_names, NULL, NULL); |
48f6f3c2 |
372 | |
ffd54a90 |
373 | a_state = FALSE; |
374 | lttv_option_add("process_state", 's', |
375 | "write the pid and state for each event", |
376 | "", |
377 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
dc877563 |
378 | |
b445142a |
379 | a_cpu_stats = FALSE; |
9d0bb8d1 |
380 | lttv_option_add("cpu_stats", 'c', |
b445142a |
381 | "write the per cpu statistics", |
382 | "", |
383 | LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL); |
384 | |
385 | a_process_stats = FALSE; |
9d0bb8d1 |
386 | lttv_option_add("process_stats", 'p', |
b445142a |
387 | "write the per process statistics", |
388 | "", |
389 | LTTV_OPT_NONE, &a_process_stats, NULL, NULL); |
390 | |
12c59c3d |
391 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event", |
ffd54a90 |
392 | LTTV_POINTER, &value)); |
12c59c3d |
393 | g_assert((event_hook = *(value.v_pointer)) != NULL); |
394 | lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT); |
dc877563 |
395 | |
ffd54a90 |
396 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
397 | LTTV_POINTER, &value)); |
398 | g_assert((before_trace = *(value.v_pointer)) != NULL); |
12c59c3d |
399 | lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT); |
dc877563 |
400 | |
ffd54a90 |
401 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
402 | LTTV_POINTER, &value)); |
403 | g_assert((before_traceset = *(value.v_pointer)) != NULL); |
12c59c3d |
404 | lttv_hooks_add(before_traceset, write_traceset_header, NULL, |
405 | LTTV_PRIO_DEFAULT); |
dc877563 |
406 | |
ffd54a90 |
407 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
408 | LTTV_POINTER, &value)); |
409 | g_assert((after_traceset = *(value.v_pointer)) != NULL); |
12c59c3d |
410 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL, |
411 | LTTV_PRIO_DEFAULT); |
ffd54a90 |
412 | } |
48f6f3c2 |
413 | |
48f6f3c2 |
414 | |
08b1c66e |
415 | static void destroy() |
ffd54a90 |
416 | { |
b445142a |
417 | g_info("Destroy textDump"); |
418 | |
ffd54a90 |
419 | lttv_option_remove("output"); |
48f6f3c2 |
420 | |
ffd54a90 |
421 | lttv_option_remove("field_names"); |
48f6f3c2 |
422 | |
ffd54a90 |
423 | lttv_option_remove("process_state"); |
48f6f3c2 |
424 | |
b445142a |
425 | lttv_option_remove("cpu_stats"); |
426 | |
427 | lttv_option_remove("process_stats"); |
428 | |
c6bc9cb9 |
429 | g_string_free(a_string, TRUE); |
430 | |
12c59c3d |
431 | lttv_hooks_remove_data(event_hook, write_event_content, NULL); |
48f6f3c2 |
432 | |
ffd54a90 |
433 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
48f6f3c2 |
434 | |
ffd54a90 |
435 | lttv_hooks_remove_data(before_trace, write_traceset_header, NULL); |
48f6f3c2 |
436 | |
ffd54a90 |
437 | lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL); |
48f6f3c2 |
438 | } |
439 | |
440 | |
08b1c66e |
441 | LTTV_MODULE("textDump", "Print events in a file", \ |
442 | "Produce a detailed text printout of a trace", \ |
0c5dbe3b |
443 | init, destroy, "stats", "batchAnalysis", "option") |
48f6f3c2 |
444 | |