update quickstart and flight
[lttv.git] / trunk / lttv / 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>
ffd54a90 39#include <ltt/trace.h>
40#include <stdio.h>
996acd92 41
dc877563 42static gboolean
7e5fe4d0 43 a_noevent,
b5ad5a5d 44 a_no_field_names,
b445142a 45 a_state,
46 a_cpu_stats,
7a36589d 47 a_process_stats,
48 a_path_output;
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
7a36589d 61print_path_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 gboolean is_named;
74
75 saved_length = indent->len;
76 nb = lttv_attribute_get_number(tree);
77 for(i = 0 ; i < nb ; i++) {
78 type = lttv_attribute_get(tree, i, &name, &value, &is_named);
79 if(is_named) {
80 g_string_sprintfa(indent, "/%s", g_quark_to_string(name));
81 } else {
82 g_string_sprintfa(indent, "/%s", name);
83 }
84
85 switch(type) {
86 case LTTV_INT:
87 fprintf(fp, "%s: %d\n", indent->str, *value.v_int);
88 break;
89 case LTTV_UINT:
90 fprintf(fp, "%s: %d\n", indent->str, *value.v_int);
91 break;
92 case LTTV_LONG:
93 fprintf(fp, "%s: %ld\n", indent->str, *value.v_ulong);
94 break;
95 case LTTV_ULONG:
96 fprintf(fp, "%s: %lu\n", indent->str, *value.v_ulong);
97 break;
98 case LTTV_FLOAT:
99 fprintf(fp, "%s: %f\n", indent->str, (double) *value.v_float);
100 break;
101 case LTTV_DOUBLE:
102 fprintf(fp, "%s: %f\n", indent->str, *value.v_double);
103 break;
104 case LTTV_TIME:
105 fprintf(fp, "%s: %lu.%09lu\n", indent->str, value.v_time->tv_sec, value.v_time->tv_nsec);
106 break;
107 case LTTV_POINTER:
108 fprintf(fp, "%s: POINTER\n", indent->str);
109 break;
110 case LTTV_STRING:
111 fprintf(fp, "%s: %s\n", indent->str, *value.v_string);
112 break;
113 case LTTV_GOBJECT:
114 if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) {
115 subtree = (LttvAttribute*) *(value.v_gobject);
116 print_path_tree(fp, indent, subtree);
117 } else {
118 fprintf(fp, "%s: GOBJECT\n", indent->str);
119 }
120 break;
121 case LTTV_NONE:
122 break;
123 }
124 g_string_truncate(indent, saved_length);
125 }
126}
127
128static void
b445142a 129print_tree(FILE *fp, GString *indent, LttvAttribute *tree)
130{
131 int i, nb, saved_length;
132
133 LttvAttribute *subtree;
134
135 LttvAttributeName name;
136
137 LttvAttributeValue value;
138
139 LttvAttributeType type;
140
c0cb4d12 141 gboolean is_named;
142
b445142a 143 nb = lttv_attribute_get_number(tree);
144 for(i = 0 ; i < nb ; i++) {
c0cb4d12 145 type = lttv_attribute_get(tree, i, &name, &value, &is_named);
146 if(is_named)
147 fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name));
148 else
149 fprintf(fp, "%s%lu: ", indent->str, name);
b445142a 150
151 switch(type) {
152 case LTTV_INT:
153 fprintf(fp, "%d\n", *value.v_int);
154 break;
155 case LTTV_UINT:
156 fprintf(fp, "%u\n", *value.v_uint);
157 break;
158 case LTTV_LONG:
159 fprintf(fp, "%ld\n", *value.v_long);
160 break;
161 case LTTV_ULONG:
162 fprintf(fp, "%lu\n", *value.v_ulong);
163 break;
164 case LTTV_FLOAT:
165 fprintf(fp, "%f\n", (double)*value.v_float);
166 break;
167 case LTTV_DOUBLE:
168 fprintf(fp, "%f\n", *value.v_double);
169 break;
170 case LTTV_TIME:
ba6f11f1 171 fprintf(fp, "%10lu.%09lu\n", value.v_time->tv_sec,
b445142a 172 value.v_time->tv_nsec);
173 break;
174 case LTTV_POINTER:
175 fprintf(fp, "POINTER\n");
176 break;
177 case LTTV_STRING:
178 fprintf(fp, "%s\n", *value.v_string);
179 break;
180 case LTTV_GOBJECT:
181 if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) {
182 fprintf(fp, "\n");
183 subtree = (LttvAttribute *)*(value.v_gobject);
184 saved_length = indent->len;
4ec0c904 185 indent = g_string_append(indent, " ");
b445142a 186 print_tree(fp, indent, subtree);
187 g_string_truncate(indent, saved_length);
188 }
189 else fprintf(fp, "GOBJECT\n");
190 break;
191 case LTTV_NONE:
192 break;
193 }
194 }
195}
196
b445142a 197static void
198print_stats(FILE *fp, LttvTracesetStats *tscs)
199{
200 int i, nb, saved_length;
201
202 LttvTraceset *ts;
203
204 LttvTraceStats *tcs;
205
206 GString *indent;
207
208 LttSystemDescription *desc;
209
210 if(tscs->stats == NULL) return;
211 indent = g_string_new("");
212 fprintf(fp, "Traceset statistics:\n\n");
7a36589d 213 if(a_path_output) {
214 print_path_tree(fp, indent, tscs->stats);
215 } else {
216 print_tree(fp, indent, tscs->stats);
217 }
b445142a 218
219 ts = tscs->parent.parent.ts;
220 nb = lttv_traceset_number(ts);
221
222 for(i = 0 ; i < nb ; i++) {
223 tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]);
9eb6aa7a 224#if 0 //FIXME
b445142a 225 desc = ltt_trace_system_description(tcs->parent.parent.t);
ba6f11f1 226 LttTime start_time = ltt_trace_system_description_trace_start_time(desc);
227 fprintf(fp, "Trace on system %s at time %lu.%09lu :\n",
a5dcde2f 228 ltt_trace_system_description_node_name(desc),
ba6f11f1 229 start_time.tv_sec,
230 start_time.tv_nsec);
9eb6aa7a 231#endif //FIXME
b445142a 232 saved_length = indent->len;
7a36589d 233 if(a_path_output) {
234 g_string_sprintfa(indent, "/trace%i", i);
235 print_path_tree(fp, indent, tcs->stats);
236 } else {
237 g_string_append(indent, " ");
238 print_tree(fp, indent, tcs->stats);
239 }
b445142a 240 g_string_truncate(indent, saved_length);
241 }
242 g_string_free(indent, TRUE);
243}
244
dc877563 245/* Insert the hooks before and after each trace and tracefile, and for each
246 event. Print a global header. */
247
248static FILE *a_file;
48f6f3c2 249
dc877563 250static GString *a_string;
251
ffd54a90 252static gboolean write_traceset_header(void *hook_data, void *call_data)
48f6f3c2 253{
dc877563 254 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 255
b445142a 256 g_info("TextDump traceset header");
257
dc877563 258 if(a_file_name == NULL) a_file = stdout;
259 else a_file = fopen(a_file_name, "w");
48f6f3c2 260
dc877563 261 if(a_file == NULL) g_error("cannot open file %s", a_file_name);
48f6f3c2 262
dc877563 263 /* Print the trace set header */
264 fprintf(a_file,"Trace set contains %d traces\n\n",
ffd54a90 265 lttv_traceset_number(tc->ts));
48f6f3c2 266
dc877563 267 return FALSE;
48f6f3c2 268}
269
270
ffd54a90 271static gboolean write_traceset_footer(void *hook_data, void *call_data)
48f6f3c2 272{
dc877563 273 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 274
b445142a 275 g_info("TextDump traceset footer");
276
dc877563 277 fprintf(a_file,"End trace set\n\n");
48f6f3c2 278
b445142a 279 if(LTTV_IS_TRACESET_STATS(tc)) {
b91e751b 280 lttv_stats_sum_traceset((LttvTracesetStats *)tc, ltt_time_infinite);
b445142a 281 print_stats(a_file, (LttvTracesetStats *)tc);
282 }
283
ffd54a90 284 if(a_file_name != NULL) fclose(a_file);
285
dc877563 286 return FALSE;
48f6f3c2 287}
288
289
dc877563 290static gboolean write_trace_header(void *hook_data, void *call_data)
48f6f3c2 291{
dc877563 292 LttvTraceContext *tc = (LttvTraceContext *)call_data;
9eb6aa7a 293#if 0 //FIXME
dc877563 294 LttSystemDescription *system = ltt_trace_system_description(tc->t);
48f6f3c2 295
a5dcde2f 296 fprintf(a_file," Trace from %s in %s\n%s\n\n",
297 ltt_trace_system_description_node_name(system),
298 ltt_trace_system_description_domain_name(system),
299 ltt_trace_system_description_description(system));
9eb6aa7a 300#endif //0
dc877563 301 return FALSE;
48f6f3c2 302}
303
304
dc877563 305static int write_event_content(void *hook_data, void *call_data)
48f6f3c2 306{
d5160b2b 307 gboolean result;
308
8bf54995 309 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
310
dc877563 311 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
48f6f3c2 312
dc877563 313 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
48f6f3c2 314
dc877563 315 LttEvent *e;
48f6f3c2 316
8bf54995 317 LttvAttributeValue value_filter;
318
319 LttvFilter *filter;
320
ae3d0f50 321 guint cpu = tfs->cpu;
d730b5c8 322 LttvTraceState *ts = (LttvTraceState*)tfc->t_context;
323 LttvProcessState *process = ts->running_process[cpu];
48f6f3c2 324
9d128d8e 325 if (a_noevent)
8ad2f153 326 return FALSE;
327
d730b5c8 328 e = ltt_tracefile_get_event(tfc->tf);
8bf54995 329
d5160b2b 330 result = lttv_iattribute_find_by_path(attributes, "filter/lttv_filter",
331 LTTV_POINTER, &value_filter);
332 g_assert(result);
8bf54995 333 filter = (LttvFilter*)*(value_filter.v_pointer);
334
cec3d7b0 335 /*
336 * call to the filter if available
337 */
0bc23ba9 338 if(filter->head != NULL)
d730b5c8 339 if(!lttv_filter_tree_parse(filter->head,e,tfc->tf,
b6ef18af 340 tfc->t_context->t,tfc,NULL,NULL))
8ff6243c 341 return FALSE;
cec3d7b0 342
b5ad5a5d 343 lttv_event_to_string(e, a_string, TRUE, !a_no_field_names, tfs);
dc877563 344
345 if(a_state) {
9d0bb8d1 346 g_string_append_printf(a_string, " %s ",
d730b5c8 347 g_quark_to_string(process->state->s));
dc877563 348 }
48f6f3c2 349
54d067f5 350 g_string_append_printf(a_string,"\n");
a0305aae 351
ffd54a90 352 fputs(a_string->str, a_file);
dc877563 353 return FALSE;
48f6f3c2 354}
355
356
08b1c66e 357static void init()
48f6f3c2 358{
d5160b2b 359 gboolean result;
360
ffd54a90 361 LttvAttributeValue value;
48f6f3c2 362
ffd54a90 363 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
48f6f3c2 364
b445142a 365 g_info("Init textDump.c");
9402662d 366
c6bc9cb9 367 a_string = g_string_new("");
368
ffd54a90 369 a_file_name = NULL;
370 lttv_option_add("output", 'o',
371 "output file where the text is written",
372 "file name",
373 LTTV_OPT_STRING, &a_file_name, NULL, NULL);
48f6f3c2 374
7e5fe4d0 375 a_noevent = FALSE;
376 lttv_option_add("noevent", 'n',
377 "disable event printout",
378 "",
379 LTTV_OPT_NONE, &a_noevent, NULL, NULL);
380
b5ad5a5d 381 a_no_field_names = FALSE;
382 lttv_option_add("field_names", 's',
383 "do not write the field names for each event",
ffd54a90 384 "",
b5ad5a5d 385 LTTV_OPT_NONE, &a_no_field_names, NULL, NULL);
48f6f3c2 386
ffd54a90 387 a_state = FALSE;
7e63c6cd 388 lttv_option_add("process_state", 'r',
ffd54a90 389 "write the pid and state for each event",
390 "",
391 LTTV_OPT_NONE, &a_state, NULL, NULL);
dc877563 392
b445142a 393 a_cpu_stats = FALSE;
9d0bb8d1 394 lttv_option_add("cpu_stats", 'c',
b445142a 395 "write the per cpu statistics",
396 "",
397 LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL);
398
399 a_process_stats = FALSE;
9d0bb8d1 400 lttv_option_add("process_stats", 'p',
b445142a 401 "write the per process statistics",
402 "",
403 LTTV_OPT_NONE, &a_process_stats, NULL, NULL);
404
7a36589d 405 a_path_output = FALSE;
406 lttv_option_add("path_output", 'a',
407 "print the process stats in path format, for easy grepping",
408 "",
409 LTTV_OPT_NONE, &a_path_output, NULL, NULL);
410
8ad2f153 411 result = lttv_iattribute_find_by_path(attributes, "hooks/event",
412 LTTV_POINTER, &value);
413 g_assert(result);
414 event_hook = *(value.v_pointer);
415 g_assert(event_hook);
416 lttv_hooks_add(event_hook, write_event_content, NULL, LTTV_PRIO_DEFAULT);
dc877563 417
d5160b2b 418 result = lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
419 LTTV_POINTER, &value);
420 g_assert(result);
421 before_trace = *(value.v_pointer);
422 g_assert(before_trace);
12c59c3d 423 lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT);
dc877563 424
d5160b2b 425 result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
426 LTTV_POINTER, &value);
427 g_assert(result);
428 before_traceset = *(value.v_pointer);
429 g_assert(before_traceset);
12c59c3d 430 lttv_hooks_add(before_traceset, write_traceset_header, NULL,
431 LTTV_PRIO_DEFAULT);
dc877563 432
d5160b2b 433 result = lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
434 LTTV_POINTER, &value);
435 g_assert(result);
436 after_traceset = *(value.v_pointer);
437 g_assert(after_traceset);
12c59c3d 438 lttv_hooks_add(after_traceset, write_traceset_footer, NULL,
439 LTTV_PRIO_DEFAULT);
ffd54a90 440}
48f6f3c2 441
08b1c66e 442static void destroy()
ffd54a90 443{
b445142a 444 g_info("Destroy textDump");
445
7e5fe4d0 446 lttv_option_remove("noevent");
447
ffd54a90 448 lttv_option_remove("output");
48f6f3c2 449
ffd54a90 450 lttv_option_remove("field_names");
48f6f3c2 451
ffd54a90 452 lttv_option_remove("process_state");
48f6f3c2 453
b445142a 454 lttv_option_remove("cpu_stats");
455
456 lttv_option_remove("process_stats");
457
4f9ac095 458 lttv_option_remove("path_output");
459
c6bc9cb9 460 g_string_free(a_string, TRUE);
461
8ad2f153 462 lttv_hooks_remove_data(event_hook, write_event_content, NULL);
48f6f3c2 463
ffd54a90 464 lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
48f6f3c2 465
58ad2e34 466 lttv_hooks_remove_data(before_traceset, write_traceset_header, NULL);
48f6f3c2 467
cbf66716 468 lttv_hooks_remove_data(after_traceset, write_traceset_footer, NULL);
48f6f3c2 469}
470
471
08b1c66e 472LTTV_MODULE("textDump", "Print events in a file", \
473 "Produce a detailed text printout of a trace", \
8e680509 474 init, destroy, "stats", "batchAnalysis", "option", "print")
48f6f3c2 475
This page took 0.082793 seconds and 4 git commands to generate.