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