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