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