1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * 2005 Mathieu Desnoyers
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;
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.
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,
22 * Event printing routines.
29 #include <lttv/lttv.h>
30 #include <lttv/option.h>
31 #include <lttv/module.h>
32 #include <lttv/hook.h>
33 #include <lttv/attribute.h>
34 #include <lttv/iattribute.h>
35 #include <lttv/stats.h>
36 #include <lttv/filter.h>
37 #include <lttv/print.h>
39 #include <ltt/event.h>
40 #include <ltt/trace.h>
43 #include <ltt/ltt-private.h>
44 #include <babeltrace/ctf/events.h>
47 #include <lttv/event.h>
49 static inline void print_enum_events(LttEvent
*e
, struct marker_field
*f
,
50 guint64 value
, GString
*s
, LttvTracefileState
*tfs
)
52 LttTracefile
*tf
= tfs
->parent
.tf
;
53 struct marker_info
*info
= marker_get_info_from_id(tf
->mdata
, e
->event_id
);
54 LttvTraceState
*ts
= (LttvTraceState
*)(tfs
->parent
.t_context
);
55 LttvNameTables
*nt
= ts
->name_tables
;
57 if (tf
->name
== LTT_CHANNEL_KERNEL
) {
58 if (info
->name
== LTT_EVENT_SYSCALL_ENTRY
59 && f
->name
== LTT_FIELD_SYSCALL_ID
) {
60 g_string_append_printf(s
, " [%s]",
61 g_quark_to_string(nt
->syscall_names
[value
]));
62 } else if ((info
->name
== LTT_EVENT_SOFT_IRQ_ENTRY
63 || info
->name
== LTT_EVENT_SOFT_IRQ_EXIT
64 || info
->name
== LTT_EVENT_SOFT_IRQ_RAISE
)
65 && f
->name
== LTT_FIELD_SOFT_IRQ_ID
) {
66 g_string_append_printf(s
, " [%s]",
67 g_quark_to_string(nt
->soft_irq_names
[value
]));
68 } else if (info
->name
== LTT_EVENT_KPROBE
69 && f
->name
== LTT_FIELD_IP
) {
70 #if (__WORDSIZE == 32)
71 GQuark symbol
= (GQuark
)g_hash_table_lookup(nt
->kprobe_hash
,
72 (gconstpointer
)&value
);
74 GQuark symbol
= (GQuark
)(unsigned long)g_hash_table_lookup(nt
->kprobe_hash
,
75 (gconstpointer
)value
);
78 g_string_append_printf(s
, " [%s]", g_quark_to_string(symbol
));
83 void lttv_print_field(LttEvent
*e
, struct marker_field
*f
, GString
*s
,
84 gboolean field_names
, LttvTracefileState
*tfs
)
92 case LTT_TYPE_SIGNED_INT
:
96 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
98 value
= ltt_event_get_long_int(e
,f
);
99 //g_string_append_printf(s, "%lld", value);
100 g_string_append_printf(s
, f
->fmt
->str
, value
);
101 //g_string_append_printf(s, type->fmt, ltt_event_get_long_int(e,f));
102 print_enum_events(e
, f
, value
, s
, tfs
);
105 case LTT_TYPE_UNSIGNED_INT
:
109 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
111 value
= ltt_event_get_long_unsigned(e
,f
);
112 //g_string_append_printf(s, "%llu", value);
113 g_string_append_printf(s
, f
->fmt
->str
, value
);
114 print_enum_events(e
, f
, value
, s
, tfs
);
115 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
122 unsigned car
= ltt_event_get_unsigned(e
,f
);
124 name
= ltt_field_name(f
);
126 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
130 name
= ltt_field_name(f
);
132 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
134 //g_string_append_printf(s, "%c", car);
135 g_string_append_printf(s
, type
->fmt
, car
);
137 g_string_append_printf(s
, "\\%x", car
);
143 name
= ltt_field_name(f
);
145 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
147 //g_string_append_printf(s, "%g", ltt_event_get_double(e,f));
148 g_string_append_printf(s
, type
->fmt
, ltt_event_get_double(e
,f
));
152 case LTT_TYPE_POINTER
:
156 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
158 g_string_append_printf(s
, "0x%" PRIx64
, ltt_event_get_long_unsigned(e
,f
));
159 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
162 case LTT_TYPE_STRING
:
166 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
168 g_string_append_printf(s
, "\"%s\"", ltt_event_get_string(e
,f
));
174 GQuark value
= ltt_enum_string_get(type
, ltt_event_get_unsigned(e
,f
));
176 name
= ltt_field_name(f
);
178 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
181 g_string_append_printf(s
, "%s", g_quark_to_string(value
));
183 g_string_append_printf(s
, "%lld", ltt_event_get_long_int(e
,f
));
190 name
= ltt_field_name(f
);
192 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
194 // g_string_append_printf(s, "{ ");
196 g_string_append_printf(s
, type
->header
);//tested, works fine.
199 nb
= ltt_event_field_element_number(e
,f
);
200 for(i
= 0 ; i
< nb
; i
++) {
201 LttField
*child
= ltt_event_field_element_select(e
,f
,i
);
202 lttv_print_field(e
, child
, s
, field_names
, i
);
204 g_string_append_printf(s
,type
->separator
);
206 //g_string_append_printf(s, " }");
208 g_string_append_printf(s
, type
->footer
);//tested, works fine.
213 name
= ltt_field_name(f
);
215 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
217 // g_string_append_printf(s, "{ ");
219 g_string_append_printf(s
, type
->header
);
221 nb
= ltt_type_member_number(type
);
222 for(i
= 0 ; i
< nb
; i
++) {
224 element
= ltt_field_member(f
,i
);
225 lttv_print_field(e
, element
, s
, field_names
, i
);
227 g_string_append_printf(s
,type
->separator
);
229 //g_string_append_printf(s, " }");
231 g_string_append_printf(s
, type
->footer
);
236 name
= ltt_field_name(f
);
238 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
240 // g_string_append_printf(s, "{ ");
241 g_string_append_printf(s
, type
->header
);
243 nb
= ltt_type_member_number(type
);
244 for(i
= 0 ; i
< nb
; i
++) {
246 element
= ltt_field_member(f
,i
);
247 lttv_print_field(e
, element
, s
, field_names
, i
);
249 g_string_append_printf(s
, type
->separator
);
251 // g_string_append_printf(s, " }");
252 g_string_append_printf(s
, type
->footer
);
255 case LTT_TYPE_COMPACT
:
256 g_error("compact type printing not implemented");
263 int getProcessInfosFromEvent(struct bt_ctf_event
*ctf_event
, GString
* processInfos
)
267 struct definition
*scope
;
268 unsigned long timestamp
;
272 gboolean noError
= TRUE
;
274 timestamp
= bt_ctf_get_timestamp(ctf_event
);
275 if (timestamp
== -1ULL) {
279 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_EVENT_CONTEXT
);
280 if (bt_ctf_field_get_error()) {
285 pid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_pid"));
286 if (bt_ctf_field_get_error()) {
291 tid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_tid"));
292 if (bt_ctf_field_get_error()) {
297 ppid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_ppid"));
298 if (bt_ctf_field_get_error()) {
303 procname
= bt_ctf_get_char_array(bt_ctf_get_field(ctf_event
, scope
, "_procname"));
304 if (bt_ctf_field_get_error()) {
310 g_string_append_printf(processInfos
, "%u, %u, %s, %u", pid
, tid
, procname
, ppid
);
320 int getCPUIdFromEvent(struct bt_ctf_event
*ctf_event
, GString
* cpuId_str
)
322 struct definition
*scope
;
323 unsigned long timestamp
;
327 gboolean noError
= TRUE
;
329 timestamp
= bt_ctf_get_timestamp(ctf_event
);
330 if (timestamp
== -1ULL) {
334 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_PACKET_CONTEXT
);
335 if (bt_ctf_field_get_error()) {
340 cpu_id
= bt_ctf_get_uint64(bt_ctf_get_field(ctf_event
, scope
, "cpu_id"));
341 if (bt_ctf_field_get_error()) {
345 g_string_append_printf(cpuId_str
, "%u", cpu_id
);
356 int getFields(struct bt_ctf_event
*ctf_event
, struct definition
const *fields
, GString
* fieldsStr
)
358 enum ctf_type_id fieldType
= bt_ctf_field_type(fields
);
359 int ret
= 0, isSigned
= -1, len
= 0, i
= 0;
360 struct definition
*index_def
;
362 case CTF_TYPE_INTEGER
:
363 isSigned
= bt_ctf_get_int_signedness(fields
);
365 g_string_append_printf(fieldsStr
, "%lu", bt_ctf_get_int64(fields
));
367 else if (isSigned
== 0) {
368 g_string_append_printf(fieldsStr
, "%llu", bt_ctf_get_uint64(fields
));
371 case CTF_TYPE_STRING
:
372 g_string_append_printf(fieldsStr
, "%s", bt_ctf_get_string(fields
));
376 g_string_append_printf(fieldsStr
, "[ ");
377 len
= bt_ctf_get_array_len(fields
);
378 if (index_def
= bt_ctf_get_index(ctf_event
, fields
, i
)) {
379 for (i
= 0; i
< len
; i
++) {
381 g_string_append_printf(fieldsStr
, ", ");
383 bt_ctf_field_type(bt_ctf_get_index(ctf_event
, fields
, i
));
384 g_string_append_printf(fieldsStr
, " ");
385 g_string_append_printf(fieldsStr
, "[%d] = ");
386 getFields(ctf_event
, bt_ctf_get_index(ctf_event
, fields
, i
), fieldsStr
);
390 g_string_append_printf(fieldsStr
, "%s", bt_ctf_get_char_array(fields
));
392 g_string_append_printf(fieldsStr
, " ]");
395 case CTF_TYPE_UNKNOWN
:
402 int getFieldsFromEvent(struct bt_ctf_event
*ctf_event
, GString
* fields
, gboolean field_names
)
404 struct definition
const * const *list
= NULL
;
406 int i
= 0, j
= 0, ret
= 0;
407 gboolean noError
= TRUE
;
408 struct definition
*scope
;
409 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_EVENT_FIELDS
);
415 ret
= bt_ctf_get_field_list(ctf_event
, scope
, &list
, &count
);
420 for (i
= 0; i
< count
; i
++) {
422 g_string_append_printf(fields
, ", ");
424 const char *name
= bt_ctf_field_name(list
[i
]);
426 g_string_append_printf(fields
, "%s = ", name
);
428 getFields(ctf_event
, list
[i
] ,fields
);
429 if (bt_ctf_field_get_error()) {
441 void lttv_event_to_string(LttvEvent
*event
, GString
*a_string
, gboolean field_names
)
443 GString
* processInfos
= g_string_new("");
444 GString
* fields
= g_string_new("");
445 GString
* cpuId_str
= g_string_new("");
447 getProcessInfosFromEvent(event
->bt_event
, processInfos
);
448 getFieldsFromEvent(event
->bt_event
, fields
, field_names
);
449 getCPUIdFromEvent(event
->bt_event
, cpuId_str
);
451 g_string_set_size(a_string
,0);
453 g_string_append_printf(a_string
, "%llu %s: { %s }", bt_ctf_get_timestamp(event
->bt_event
), bt_ctf_event_name(event
->bt_event
), cpuId_str
->str
);
454 if (strcmp("", processInfos
->str
) < 0) {
455 g_string_append_printf(a_string
, ", { %s }", processInfos
->str
);
457 if (strcmp("", fields
->str
) < 0) {
458 g_string_append_printf(a_string
, ", { %s }", fields
->str
);
461 g_string_free(fields
, TRUE
);
462 g_string_free(processInfos
, TRUE
);
463 g_string_free(cpuId_str
, TRUE
);
467 void lttv_event_to_string(LttEvent
*e
, GString
*s
, gboolean mandatory_fields
,
468 gboolean field_names
, LttvTracefileState
*tfs
)
470 struct marker_field
*field
;
471 struct marker_info
*info
;
475 guint cpu
= tfs
->cpu
;
476 LttvTraceState
*ts
= (LttvTraceState
*)tfs
->parent
.t_context
;
477 LttvProcessState
*process
= ts
->running_process
[cpu
];
479 s
= g_string_set_size(s
,0);
481 info
= marker_get_info_from_id(tfs
->parent
.tf
->mdata
, e
->event_id
);
483 if(mandatory_fields
) {
484 time
= ltt_event_time(e
);
485 g_string_append_printf(s
,"%s.%s: %ld.%09ld (%s/%s_%u)",
486 g_quark_to_string(ltt_tracefile_name(tfs
->parent
.tf
)),
487 g_quark_to_string(info
->name
), (long)time
.tv_sec
, time
.tv_nsec
,
489 ltt_trace_name(ltt_tracefile_get_trace(tfs
->parent
.tf
))),
490 g_quark_to_string(ltt_tracefile_name(tfs
->parent
.tf
)), cpu
);
491 /* Print the process id and the state/interrupt type of the process */
492 g_string_append_printf(s
,", %u, %u, %s, %s, %u, 0x%" PRIx64
", %s",
495 g_quark_to_string(process
->name
),
496 g_quark_to_string(process
->brand
),
498 process
->current_function
,
499 g_quark_to_string(process
->state
->t
));
502 if(marker_get_num_fields(info
) == 0) return;
503 g_string_append_printf(s
, " ");
504 g_string_append_printf(s
, "{ ");
505 for (field
= marker_get_field(info
, 0);
506 field
!= marker_get_field(info
, marker_get_num_fields(info
));
508 if(field
!= marker_get_field(info
, 0))
509 g_string_append_printf(s
, ", ");
510 lttv_print_field(e
, field
, s
, field_names
, tfs
);
512 g_string_append_printf(s
, " }");
514 #endif /* BABEL_CLEANUP */
520 static void destroy()
524 LTTV_MODULE("print", "Print events", \
525 "Produce a detailed text printout of events", \