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