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