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>
48 static inline void print_enum_events(LttEvent
*e
, struct marker_field
*f
,
49 guint64 value
, GString
*s
, LttvTracefileState
*tfs
)
51 LttTracefile
*tf
= tfs
->parent
.tf
;
52 struct marker_info
*info
= marker_get_info_from_id(tf
->mdata
, e
->event_id
);
53 LttvTraceState
*ts
= (LttvTraceState
*)(tfs
->parent
.t_context
);
54 LttvNameTables
*nt
= ts
->name_tables
;
56 if (tf
->name
== LTT_CHANNEL_KERNEL
) {
57 if (info
->name
== LTT_EVENT_SYSCALL_ENTRY
58 && f
->name
== LTT_FIELD_SYSCALL_ID
) {
59 g_string_append_printf(s
, " [%s]",
60 g_quark_to_string(nt
->syscall_names
[value
]));
61 } else if ((info
->name
== LTT_EVENT_SOFT_IRQ_ENTRY
62 || info
->name
== LTT_EVENT_SOFT_IRQ_EXIT
63 || info
->name
== LTT_EVENT_SOFT_IRQ_RAISE
)
64 && f
->name
== LTT_FIELD_SOFT_IRQ_ID
) {
65 g_string_append_printf(s
, " [%s]",
66 g_quark_to_string(nt
->soft_irq_names
[value
]));
67 } else if (info
->name
== LTT_EVENT_KPROBE
68 && f
->name
== LTT_FIELD_IP
) {
69 #if (__WORDSIZE == 32)
70 GQuark symbol
= (GQuark
)g_hash_table_lookup(nt
->kprobe_hash
,
71 (gconstpointer
)&value
);
73 GQuark symbol
= (GQuark
)(unsigned long)g_hash_table_lookup(nt
->kprobe_hash
,
74 (gconstpointer
)value
);
77 g_string_append_printf(s
, " [%s]", g_quark_to_string(symbol
));
82 void lttv_print_field(LttEvent
*e
, struct marker_field
*f
, GString
*s
,
83 gboolean field_names
, LttvTracefileState
*tfs
)
91 case LTT_TYPE_SIGNED_INT
:
95 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
97 value
= ltt_event_get_long_int(e
,f
);
98 //g_string_append_printf(s, "%lld", value);
99 g_string_append_printf(s
, f
->fmt
->str
, value
);
100 //g_string_append_printf(s, type->fmt, ltt_event_get_long_int(e,f));
101 print_enum_events(e
, f
, value
, s
, tfs
);
104 case LTT_TYPE_UNSIGNED_INT
:
108 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
110 value
= ltt_event_get_long_unsigned(e
,f
);
111 //g_string_append_printf(s, "%llu", value);
112 g_string_append_printf(s
, f
->fmt
->str
, value
);
113 print_enum_events(e
, f
, value
, s
, tfs
);
114 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
121 unsigned car
= ltt_event_get_unsigned(e
,f
);
123 name
= ltt_field_name(f
);
125 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
129 name
= ltt_field_name(f
);
131 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
133 //g_string_append_printf(s, "%c", car);
134 g_string_append_printf(s
, type
->fmt
, car
);
136 g_string_append_printf(s
, "\\%x", car
);
142 name
= ltt_field_name(f
);
144 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
146 //g_string_append_printf(s, "%g", ltt_event_get_double(e,f));
147 g_string_append_printf(s
, type
->fmt
, ltt_event_get_double(e
,f
));
151 case LTT_TYPE_POINTER
:
155 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
157 g_string_append_printf(s
, "0x%" PRIx64
, ltt_event_get_long_unsigned(e
,f
));
158 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
161 case LTT_TYPE_STRING
:
165 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
167 g_string_append_printf(s
, "\"%s\"", ltt_event_get_string(e
,f
));
173 GQuark value
= ltt_enum_string_get(type
, ltt_event_get_unsigned(e
,f
));
175 name
= ltt_field_name(f
);
177 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
180 g_string_append_printf(s
, "%s", g_quark_to_string(value
));
182 g_string_append_printf(s
, "%lld", ltt_event_get_long_int(e
,f
));
189 name
= ltt_field_name(f
);
191 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
193 // g_string_append_printf(s, "{ ");
195 g_string_append_printf(s
, type
->header
);//tested, works fine.
198 nb
= ltt_event_field_element_number(e
,f
);
199 for(i
= 0 ; i
< nb
; i
++) {
200 LttField
*child
= ltt_event_field_element_select(e
,f
,i
);
201 lttv_print_field(e
, child
, s
, field_names
, i
);
203 g_string_append_printf(s
,type
->separator
);
205 //g_string_append_printf(s, " }");
207 g_string_append_printf(s
, type
->footer
);//tested, works fine.
212 name
= ltt_field_name(f
);
214 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
216 // g_string_append_printf(s, "{ ");
218 g_string_append_printf(s
, type
->header
);
220 nb
= ltt_type_member_number(type
);
221 for(i
= 0 ; i
< nb
; i
++) {
223 element
= ltt_field_member(f
,i
);
224 lttv_print_field(e
, element
, s
, field_names
, i
);
226 g_string_append_printf(s
,type
->separator
);
228 //g_string_append_printf(s, " }");
230 g_string_append_printf(s
, type
->footer
);
235 name
= ltt_field_name(f
);
237 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
239 // g_string_append_printf(s, "{ ");
240 g_string_append_printf(s
, type
->header
);
242 nb
= ltt_type_member_number(type
);
243 for(i
= 0 ; i
< nb
; i
++) {
245 element
= ltt_field_member(f
,i
);
246 lttv_print_field(e
, element
, s
, field_names
, i
);
248 g_string_append_printf(s
, type
->separator
);
250 // g_string_append_printf(s, " }");
251 g_string_append_printf(s
, type
->footer
);
254 case LTT_TYPE_COMPACT
:
255 g_error("compact type printing not implemented");
262 int getProcessInfosFromEvent(struct bt_ctf_event
*ctf_event
, GString
* processInfos
)
266 struct definition
*scope
;
267 unsigned long timestamp
;
271 gboolean noError
= TRUE
;
273 timestamp
= bt_ctf_get_timestamp(ctf_event
);
274 if (timestamp
== -1ULL) {
278 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_EVENT_CONTEXT
);
279 if (bt_ctf_field_get_error()) {
284 pid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_pid"));
285 if (bt_ctf_field_get_error()) {
290 tid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_tid"));
291 if (bt_ctf_field_get_error()) {
296 ppid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_ppid"));
297 if (bt_ctf_field_get_error()) {
302 procname
= bt_ctf_get_char_array(bt_ctf_get_field(ctf_event
, scope
, "_procname"));
303 if (bt_ctf_field_get_error()) {
309 g_string_append_printf(processInfos
, "%u, %u, %s, %u", pid
, tid
, procname
, ppid
);
319 int getCPUIdFromEvent(struct bt_ctf_event
*ctf_event
, GString
* cpuId_str
)
321 struct definition
*scope
;
322 unsigned long timestamp
;
326 gboolean noError
= TRUE
;
328 timestamp
= bt_ctf_get_timestamp(ctf_event
);
329 if (timestamp
== -1ULL) {
333 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_PACKET_CONTEXT
);
334 if (bt_ctf_field_get_error()) {
339 cpu_id
= bt_ctf_get_uint64(bt_ctf_get_field(ctf_event
, scope
, "cpu_id"));
340 if (bt_ctf_field_get_error()) {
344 g_string_append_printf(cpuId_str
, "%u", cpu_id
);
355 int getFields(struct bt_ctf_event
*ctf_event
, struct definition
const *fields
, GString
* fieldsStr
)
357 enum ctf_type_id fieldType
= bt_ctf_field_type(fields
);
358 int ret
= 0, isSigned
= -1, len
= 0, i
= 0;
359 struct definition
*index_def
;
361 case CTF_TYPE_INTEGER
:
362 isSigned
= bt_ctf_get_int_signedness(fields
);
364 g_string_append_printf(fieldsStr
, "%lu", bt_ctf_get_int64(fields
));
366 else if (isSigned
== 0) {
367 g_string_append_printf(fieldsStr
, "%llu", bt_ctf_get_uint64(fields
));
370 case CTF_TYPE_STRING
:
371 g_string_append_printf(fieldsStr
, "%s", bt_ctf_get_string(fields
));
375 g_string_append_printf(fieldsStr
, "[ ");
376 len
= bt_ctf_get_array_len(fields
);
377 if (index_def
= bt_ctf_get_index(ctf_event
, fields
, i
)) {
378 for (i
= 0; i
< len
; i
++) {
380 g_string_append_printf(fieldsStr
, ", ");
382 bt_ctf_field_type(bt_ctf_get_index(ctf_event
, fields
, i
));
383 g_string_append_printf(fieldsStr
, " ");
384 g_string_append_printf(fieldsStr
, "[%d] = ");
385 getFields(ctf_event
, bt_ctf_get_index(ctf_event
, fields
, i
), fieldsStr
);
389 g_string_append_printf(fieldsStr
, "%s", bt_ctf_get_char_array(fields
));
391 g_string_append_printf(fieldsStr
, " ]");
394 case CTF_TYPE_UNKNOWN
:
401 int getFieldsFromEvent(struct bt_ctf_event
*ctf_event
, GString
* fields
, gboolean field_names
)
403 struct definition
const * const *list
= NULL
;
405 int i
= 0, j
= 0, ret
= 0;
406 gboolean noError
= TRUE
;
407 struct definition
*scope
;
408 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_EVENT_FIELDS
);
414 ret
= bt_ctf_get_field_list(ctf_event
, scope
, &list
, &count
);
419 for (i
= 0; i
< count
; i
++) {
421 g_string_append_printf(fields
, ", ");
423 const char *name
= bt_ctf_field_name(list
[i
]);
425 g_string_append_printf(fields
, "%s = ", name
);
427 getFields(ctf_event
, list
[i
] ,fields
);
428 if (bt_ctf_field_get_error()) {
440 void lttv_event_to_string(struct bt_ctf_event
*event
, GString
*a_string
, gboolean field_names
)
442 GString
* processInfos
= g_string_new("");
443 GString
* fields
= g_string_new("");
444 GString
* cpuId_str
= g_string_new("");
446 getProcessInfosFromEvent(event
, processInfos
);
447 getFieldsFromEvent(event
, fields
, field_names
);
448 getCPUIdFromEvent(event
, cpuId_str
);
450 g_string_set_size(a_string
,0);
452 g_string_append_printf(a_string
, "%llu %s: { %s }", bt_ctf_get_timestamp(event
), bt_ctf_event_name(event
), cpuId_str
->str
);
453 if (strcmp("", processInfos
->str
) < 0) {
454 g_string_append_printf(a_string
, ", { %s }", processInfos
->str
);
456 if (strcmp("", fields
->str
) < 0) {
457 g_string_append_printf(a_string
, ", { %s }", fields
->str
);
460 g_string_free(fields
, TRUE
);
461 g_string_free(processInfos
, TRUE
);
462 g_string_free(cpuId_str
, TRUE
);
466 void lttv_event_to_string(LttEvent
*e
, GString
*s
, gboolean mandatory_fields
,
467 gboolean field_names
, LttvTracefileState
*tfs
)
469 struct marker_field
*field
;
470 struct marker_info
*info
;
474 guint cpu
= tfs
->cpu
;
475 LttvTraceState
*ts
= (LttvTraceState
*)tfs
->parent
.t_context
;
476 LttvProcessState
*process
= ts
->running_process
[cpu
];
478 s
= g_string_set_size(s
,0);
480 info
= marker_get_info_from_id(tfs
->parent
.tf
->mdata
, e
->event_id
);
482 if(mandatory_fields
) {
483 time
= ltt_event_time(e
);
484 g_string_append_printf(s
,"%s.%s: %ld.%09ld (%s/%s_%u)",
485 g_quark_to_string(ltt_tracefile_name(tfs
->parent
.tf
)),
486 g_quark_to_string(info
->name
), (long)time
.tv_sec
, time
.tv_nsec
,
488 ltt_trace_name(ltt_tracefile_get_trace(tfs
->parent
.tf
))),
489 g_quark_to_string(ltt_tracefile_name(tfs
->parent
.tf
)), cpu
);
490 /* Print the process id and the state/interrupt type of the process */
491 g_string_append_printf(s
,", %u, %u, %s, %s, %u, 0x%" PRIx64
", %s",
494 g_quark_to_string(process
->name
),
495 g_quark_to_string(process
->brand
),
497 process
->current_function
,
498 g_quark_to_string(process
->state
->t
));
501 if(marker_get_num_fields(info
) == 0) return;
502 g_string_append_printf(s
, " ");
503 g_string_append_printf(s
, "{ ");
504 for (field
= marker_get_field(info
, 0);
505 field
!= marker_get_field(info
, marker_get_num_fields(info
));
507 if(field
!= marker_get_field(info
, 0))
508 g_string_append_printf(s
, ", ");
509 lttv_print_field(e
, field
, s
, field_names
, tfs
);
511 g_string_append_printf(s
, " }");
513 #endif /* BABEL_CLEANUP */
519 static void destroy()
523 LTTV_MODULE("print", "Print events", \
524 "Produce a detailed text printout of events", \