put print fields in print.c
[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 * 2005 Mathieu Desnoyers
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
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
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
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>
34 #include <lttv/stats.h>
35 #include <lttv/filter.h>
36 #include <lttv/print.h>
37 #include <ltt/ltt.h>
38 #include <ltt/event.h>
39 #include <ltt/type.h>
40 #include <ltt/trace.h>
41 #include <ltt/facility.h>
42 #include <stdio.h>
43
44 static gboolean
45 a_field_names,
46 a_state,
47 a_cpu_stats,
48 a_process_stats;
49
50 static char
51 *a_file_name = NULL;
52
53 static LttvHooks
54 *before_traceset,
55 *after_traceset,
56 *before_trace,
57 *event_hook;
58
59
60 static void
61 print_tree(FILE *fp, GString *indent, LttvAttribute *tree)
62 {
63 int i, nb, saved_length;
64
65 LttvAttribute *subtree;
66
67 LttvAttributeName name;
68
69 LttvAttributeValue value;
70
71 LttvAttributeType type;
72
73 nb = lttv_attribute_get_number(tree);
74 for(i = 0 ; i < nb ; i++) {
75 type = lttv_attribute_get(tree, i, &name, &value);
76 fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name));
77
78 switch(type) {
79 case LTTV_INT:
80 fprintf(fp, "%d\n", *value.v_int);
81 break;
82 case LTTV_UINT:
83 fprintf(fp, "%u\n", *value.v_uint);
84 break;
85 case LTTV_LONG:
86 fprintf(fp, "%ld\n", *value.v_long);
87 break;
88 case LTTV_ULONG:
89 fprintf(fp, "%lu\n", *value.v_ulong);
90 break;
91 case LTTV_FLOAT:
92 fprintf(fp, "%f\n", (double)*value.v_float);
93 break;
94 case LTTV_DOUBLE:
95 fprintf(fp, "%f\n", *value.v_double);
96 break;
97 case LTTV_TIME:
98 fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec,
99 value.v_time->tv_nsec);
100 break;
101 case LTTV_POINTER:
102 fprintf(fp, "POINTER\n");
103 break;
104 case LTTV_STRING:
105 fprintf(fp, "%s\n", *value.v_string);
106 break;
107 case LTTV_GOBJECT:
108 if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) {
109 fprintf(fp, "\n");
110 subtree = (LttvAttribute *)*(value.v_gobject);
111 saved_length = indent->len;
112 g_string_append(indent, " ");
113 print_tree(fp, indent, subtree);
114 g_string_truncate(indent, saved_length);
115 }
116 else fprintf(fp, "GOBJECT\n");
117 break;
118 case LTTV_NONE:
119 break;
120 }
121 }
122 }
123
124
125 static void
126 print_stats(FILE *fp, LttvTracesetStats *tscs)
127 {
128 int i, nb, saved_length;
129
130 LttvTraceset *ts;
131
132 LttvTraceStats *tcs;
133
134 GString *indent;
135
136 LttSystemDescription *desc;
137
138 if(tscs->stats == NULL) return;
139 indent = g_string_new("");
140 fprintf(fp, "Traceset statistics:\n\n");
141 print_tree(fp, indent, tscs->stats);
142
143 ts = tscs->parent.parent.ts;
144 nb = lttv_traceset_number(ts);
145
146 for(i = 0 ; i < nb ; i++) {
147 tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]);
148 #if 0 //FIXME
149 desc = ltt_trace_system_description(tcs->parent.parent.t);
150 LttTime start_time = ltt_trace_system_description_trace_start_time(desc);
151 fprintf(fp, "Trace on system %s at time %lu.%09lu :\n",
152 ltt_trace_system_description_node_name(desc),
153 start_time.tv_sec,
154 start_time.tv_nsec);
155 #endif //FIXME
156 saved_length = indent->len;
157 g_string_append(indent, " ");
158 print_tree(fp, indent, tcs->stats);
159 g_string_truncate(indent, saved_length);
160 }
161 g_string_free(indent, TRUE);
162 }
163
164
165 /* Insert the hooks before and after each trace and tracefile, and for each
166 event. Print a global header. */
167
168 static FILE *a_file;
169
170 static GString *a_string;
171
172 static gboolean write_traceset_header(void *hook_data, void *call_data)
173 {
174 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
175
176 g_info("TextDump traceset header");
177
178 if(a_file_name == NULL) a_file = stdout;
179 else a_file = fopen(a_file_name, "w");
180
181 if(a_file == NULL) g_error("cannot open file %s", a_file_name);
182
183 /* Print the trace set header */
184 fprintf(a_file,"Trace set contains %d traces\n\n",
185 lttv_traceset_number(tc->ts));
186
187 return FALSE;
188 }
189
190
191 static gboolean write_traceset_footer(void *hook_data, void *call_data)
192 {
193 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
194
195 g_info("TextDump traceset footer");
196
197 fprintf(a_file,"End trace set\n\n");
198
199 if(LTTV_IS_TRACESET_STATS(tc)) {
200 lttv_stats_sum_traceset((LttvTracesetStats *)tc);
201 print_stats(a_file, (LttvTracesetStats *)tc);
202 }
203
204 if(a_file_name != NULL) fclose(a_file);
205
206 return FALSE;
207 }
208
209
210 static gboolean write_trace_header(void *hook_data, void *call_data)
211 {
212 LttvTraceContext *tc = (LttvTraceContext *)call_data;
213 #if 0 //FIXME
214 LttSystemDescription *system = ltt_trace_system_description(tc->t);
215
216 fprintf(a_file," Trace from %s in %s\n%s\n\n",
217 ltt_trace_system_description_node_name(system),
218 ltt_trace_system_description_domain_name(system),
219 ltt_trace_system_description_description(system));
220 #endif //0
221 return FALSE;
222 }
223
224
225 static int write_event_content(void *hook_data, void *call_data)
226 {
227 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
228
229 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
230
231 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
232
233 LttEvent *e;
234
235 LttvAttributeValue value_filter;
236
237 LttvFilter *filter;
238
239 guint cpu = ltt_tracefile_num(tfs->parent.tf);
240 LttvTraceState *ts = (LttvTraceState*)tfc->t_context;
241 LttvProcessState *process = ts->running_process[cpu];
242
243 e = ltt_tracefile_get_event(tfc->tf);
244
245 g_assert(lttv_iattribute_find_by_path(attributes, "filter/lttv_filter",
246 LTTV_POINTER, &value_filter));
247 filter = (LttvFilter*)*(value_filter.v_pointer);
248
249 /*
250 * call to the filter if available
251 */
252 if(filter->head != NULL)
253 if(!lttv_filter_tree_parse(filter->head,e,tfc->tf,
254 tfc->t_context->t,tfc))
255 return FALSE;
256
257 lttv_event_to_string(e, a_string, TRUE, a_field_names, tfs);
258 g_string_append_printf(a_string,"\n");
259
260 if(a_state) {
261 g_string_append_printf(a_string, " %s ",
262 g_quark_to_string(process->state->s));
263 }
264
265 fputs(a_string->str, a_file);
266 return FALSE;
267 }
268
269
270 static void init()
271 {
272 LttvAttributeValue value;
273
274 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
275
276 g_info("Init textDump.c");
277
278 a_string = g_string_new("");
279
280 a_file_name = NULL;
281 lttv_option_add("output", 'o',
282 "output file where the text is written",
283 "file name",
284 LTTV_OPT_STRING, &a_file_name, NULL, NULL);
285
286 a_field_names = FALSE;
287 lttv_option_add("field_names", 'l',
288 "write the field names for each event",
289 "",
290 LTTV_OPT_NONE, &a_field_names, NULL, NULL);
291
292 a_state = FALSE;
293 lttv_option_add("process_state", 's',
294 "write the pid and state for each event",
295 "",
296 LTTV_OPT_NONE, &a_state, NULL, NULL);
297
298 a_cpu_stats = FALSE;
299 lttv_option_add("cpu_stats", 'c',
300 "write the per cpu statistics",
301 "",
302 LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL);
303
304 a_process_stats = FALSE;
305 lttv_option_add("process_stats", 'p',
306 "write the per process statistics",
307 "",
308 LTTV_OPT_NONE, &a_process_stats, NULL, NULL);
309
310 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event",
311 LTTV_POINTER, &value));
312 g_assert((event_hook = *(value.v_pointer)) != NULL);
313 lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT);
314
315 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
316 LTTV_POINTER, &value));
317 g_assert((before_trace = *(value.v_pointer)) != NULL);
318 lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT);
319
320 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
321 LTTV_POINTER, &value));
322 g_assert((before_traceset = *(value.v_pointer)) != NULL);
323 lttv_hooks_add(before_traceset, write_traceset_header, NULL,
324 LTTV_PRIO_DEFAULT);
325
326 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
327 LTTV_POINTER, &value));
328 g_assert((after_traceset = *(value.v_pointer)) != NULL);
329 lttv_hooks_add(after_traceset, write_traceset_footer, NULL,
330 LTTV_PRIO_DEFAULT);
331 }
332
333 static void destroy()
334 {
335 g_info("Destroy textDump");
336
337 lttv_option_remove("output");
338
339 lttv_option_remove("field_names");
340
341 lttv_option_remove("process_state");
342
343 lttv_option_remove("cpu_stats");
344
345 lttv_option_remove("process_stats");
346
347 g_string_free(a_string, TRUE);
348
349 lttv_hooks_remove_data(event_hook, write_event_content, NULL);
350
351 lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
352
353 lttv_hooks_remove_data(before_trace, write_traceset_header, NULL);
354
355 lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL);
356 }
357
358
359 LTTV_MODULE("textDump", "Print events in a file", \
360 "Produce a detailed text printout of a trace", \
361 init, destroy, "stats", "batchAnalysis", "option", "print")
362
This page took 0.0358619999999999 seconds and 4 git commands to generate.