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