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