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