Make modules more flexible (builtin or loaded are identical). Add a test module
[lttv.git] / ltt / branches / poly / lttv / modules / text / textDump.c
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
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
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>
29 #include <lttv/stats.h>
30 #include <ltt/ltt.h>
31 #include <ltt/event.h>
32 #include <ltt/type.h>
33 #include <ltt/trace.h>
34 #include <stdio.h>
35
36 static gboolean
37 a_field_names,
38 a_state,
39 a_cpu_stats,
40 a_process_stats;
41
42 static char
43 *a_file_name = NULL;
44
45 static LttvHooks
46 *before_traceset,
47 *after_traceset,
48 *before_trace,
49 *before_event;
50
51
52 void print_field(LttEvent *e, LttField *f, GString *s, gboolean field_names) {
53
54 LttType *type;
55
56 LttField *element;
57
58 char *name;
59
60 int nb, i;
61
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;
67
68 case LTT_UINT:
69 g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f));
70 break;
71
72 case LTT_FLOAT:
73 g_string_append_printf(s, " %g", ltt_event_get_double(e,f));
74 break;
75
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,
82 ltt_event_get_unsigned(e,f)-1));
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);
102 if(field_names) {
103 ltt_type_member_type(type, i, &name);
104 g_string_append_printf(s, " %s = ", name);
105 }
106 print_field(e, element, s, field_names);
107 }
108 g_string_append_printf(s, " }");
109 break;
110 }
111 }
112
113
114 void lttv_event_to_string(LttEvent *e, LttTracefile *tf, GString *s,
115 gboolean mandatory_fields, gboolean field_names, LttvTracefileState *tfs)
116 {
117 LttFacility *facility;
118
119 LttEventType *event_type;
120
121 LttType *type;
122
123 LttField *field;
124
125 LttTime time;
126
127 g_string_set_size(s,0);
128
129 facility = ltt_event_facility(e);
130 event_type = ltt_event_eventtype(e);
131 field = ltt_event_field(e);
132
133 if(mandatory_fields) {
134 time = ltt_event_time(e);
135 g_string_append_printf(s,"%s.%s: %ld.%09ld (%s)",
136 ltt_facility_name(facility),
137 ltt_eventtype_name(event_type), (long)time.tv_sec, time.tv_nsec,
138 g_quark_to_string(tfs->cpu_name));
139 /* Print the process id and the state/interrupt type of the process */
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));
143 }
144
145 if(field)
146 print_field(e, field, s, field_names);
147 }
148
149
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);
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);
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
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;
255
256 static GString *a_string;
257
258 static gboolean write_traceset_header(void *hook_data, void *call_data)
259 {
260 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
261
262 g_info("TextDump traceset header");
263
264 if(a_file_name == NULL) a_file = stdout;
265 else a_file = fopen(a_file_name, "w");
266
267 if(a_file == NULL) g_error("cannot open file %s", a_file_name);
268
269 /* Print the trace set header */
270 fprintf(a_file,"Trace set contains %d traces\n\n",
271 lttv_traceset_number(tc->ts));
272
273 return FALSE;
274 }
275
276
277 static gboolean write_traceset_footer(void *hook_data, void *call_data)
278 {
279 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
280
281 g_info("TextDump traceset footer");
282
283 fprintf(a_file,"End trace set\n\n");
284
285 if(LTTV_IS_TRACESET_STATS(tc)) {
286 print_stats(a_file, (LttvTracesetStats *)tc);
287 }
288
289 if(a_file_name != NULL) fclose(a_file);
290
291 return FALSE;
292 }
293
294
295 static gboolean write_trace_header(void *hook_data, void *call_data)
296 {
297 LttvTraceContext *tc = (LttvTraceContext *)call_data;
298
299 LttSystemDescription *system = ltt_trace_system_description(tc->t);
300
301 fprintf(a_file," Trace from %s in %s\n%s\n\n",
302 ltt_trace_system_description_node_name(system),
303 ltt_trace_system_description_domain_name(system),
304 ltt_trace_system_description_description(system));
305 return FALSE;
306 }
307
308
309 static int write_event_content(void *hook_data, void *call_data)
310 {
311 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
312
313 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
314
315 LttEvent *e;
316
317 e = tfc->e;
318
319 lttv_event_to_string(e, tfc->tf, a_string, TRUE, a_field_names, tfs);
320 g_string_append_printf(a_string,"\n");
321
322 if(a_state) {
323 g_string_append_printf(a_string, " %s ",
324 g_quark_to_string(tfs->process->state->s));
325 }
326
327 fputs(a_string->str, a_file);
328 return FALSE;
329 }
330
331
332 static void init()
333 {
334 LttvAttributeValue value;
335
336 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
337
338 g_info("Init textDump.c");
339
340 a_string = g_string_new("");
341
342 a_file_name = NULL;
343 lttv_option_add("output", 'o',
344 "output file where the text is written",
345 "file name",
346 LTTV_OPT_STRING, &a_file_name, NULL, NULL);
347
348 a_field_names = FALSE;
349 lttv_option_add("field_names", 'l',
350 "write the field names for each event",
351 "",
352 LTTV_OPT_NONE, &a_field_names, NULL, NULL);
353
354 a_state = FALSE;
355 lttv_option_add("process_state", 's',
356 "write the pid and state for each event",
357 "",
358 LTTV_OPT_NONE, &a_state, NULL, NULL);
359
360 a_cpu_stats = FALSE;
361 lttv_option_add("cpu_stats", 'c',
362 "write the per cpu statistics",
363 "",
364 LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL);
365
366 a_process_stats = FALSE;
367 lttv_option_add("process_stats", 'p',
368 "write the per process statistics",
369 "",
370 LTTV_OPT_NONE, &a_process_stats, NULL, NULL);
371
372 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before",
373 LTTV_POINTER, &value));
374 g_assert((before_event = *(value.v_pointer)) != NULL);
375 lttv_hooks_add(before_event, write_event_content, NULL);
376
377 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
378 LTTV_POINTER, &value));
379 g_assert((before_trace = *(value.v_pointer)) != NULL);
380 lttv_hooks_add(before_trace, write_trace_header, NULL);
381
382 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
383 LTTV_POINTER, &value));
384 g_assert((before_traceset = *(value.v_pointer)) != NULL);
385 lttv_hooks_add(before_traceset, write_traceset_header, NULL);
386
387 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
388 LTTV_POINTER, &value));
389 g_assert((after_traceset = *(value.v_pointer)) != NULL);
390 lttv_hooks_add(after_traceset, write_traceset_footer, NULL);
391 }
392
393
394 static void destroy()
395 {
396 g_info("Destroy textDump");
397
398 lttv_option_remove("output");
399
400 lttv_option_remove("field_names");
401
402 lttv_option_remove("process_state");
403
404 lttv_option_remove("cpu_stats");
405
406 lttv_option_remove("process_stats");
407
408 g_string_free(a_string, TRUE);
409
410 lttv_hooks_remove_data(before_event, write_event_content, NULL);
411
412 lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
413
414 lttv_hooks_remove_data(before_trace, write_traceset_header, NULL);
415
416 lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL);
417 }
418
419
420 LTTV_MODULE("textDump", "Print events in a file", \
421 "Produce a detailed text printout of a trace", \
422 init, destroy, "stats", "batchAnalysis")
423
424
425
426
This page took 0.038184 seconds and 4 git commands to generate.