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>
36 #include <lttv/stats.h>
37 #include <lttv/filter.h>
39 #include <lttv/print.h>
42 #include <babeltrace/ctf/events.h>
45 #include <lttv/event.h>
46 #include <lttv/traceset.h>
48 static inline void print_enum_events(LttEvent
*e
, struct marker_field
*f
,
49 guint64 value
, GString
*s
, LttvTracefileState
*tfs
)
51 LttvTraceState
*ts
= (LttvTraceState
*)(tfs
->parent
.t_context
);
52 LttvNameTables
*nt
= ts
->name_tables
;
54 if (tf
->name
== LTT_CHANNEL_KERNEL
) {
55 if (info
->name
== LTT_EVENT_SYSCALL_ENTRY
56 && f
->name
== LTT_FIELD_SYSCALL_ID
) {
57 g_string_append_printf(s
, " [%s]",
58 g_quark_to_string(nt
->syscall_names
[value
]));
59 } else if ((info
->name
== LTT_EVENT_SOFT_IRQ_ENTRY
60 || info
->name
== LTT_EVENT_SOFT_IRQ_EXIT
61 || info
->name
== LTT_EVENT_SOFT_IRQ_RAISE
)
62 && f
->name
== LTT_FIELD_SOFT_IRQ_ID
) {
63 g_string_append_printf(s
, " [%s]",
64 g_quark_to_string(nt
->soft_irq_names
[value
]));
65 } else if (info
->name
== LTT_EVENT_KPROBE
66 && f
->name
== LTT_FIELD_IP
) {
67 #if (__WORDSIZE == 32)
68 GQuark symbol
= (GQuark
)g_hash_table_lookup(nt
->kprobe_hash
,
69 (gconstpointer
)&value
);
71 GQuark symbol
= (GQuark
)(unsigned long)g_hash_table_lookup(nt
->kprobe_hash
,
72 (gconstpointer
)value
);
75 g_string_append_printf(s
, " [%s]", g_quark_to_string(symbol
));
80 void lttv_print_field(LttEvent
*e
, struct marker_field
*f
, GString
*s
,
81 gboolean field_names
, LttvTracefileState
*tfs
)
89 case LTT_TYPE_SIGNED_INT
:
93 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
95 value
= ltt_event_get_long_int(e
,f
);
96 //g_string_append_printf(s, "%lld", value);
97 g_string_append_printf(s
, f
->fmt
->str
, value
);
98 //g_string_append_printf(s, type->fmt, ltt_event_get_long_int(e,f));
99 print_enum_events(e
, f
, value
, s
, tfs
);
102 case LTT_TYPE_UNSIGNED_INT
:
106 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
108 value
= ltt_event_get_long_unsigned(e
,f
);
109 //g_string_append_printf(s, "%llu", value);
110 g_string_append_printf(s
, f
->fmt
->str
, value
);
111 print_enum_events(e
, f
, value
, s
, tfs
);
112 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
119 unsigned car
= ltt_event_get_unsigned(e
,f
);
121 name
= ltt_field_name(f
);
123 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
127 name
= ltt_field_name(f
);
129 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
131 //g_string_append_printf(s, "%c", car);
132 g_string_append_printf(s
, type
->fmt
, car
);
134 g_string_append_printf(s
, "\\%x", car
);
140 name
= ltt_field_name(f
);
142 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
144 //g_string_append_printf(s, "%g", ltt_event_get_double(e,f));
145 g_string_append_printf(s
, type
->fmt
, ltt_event_get_double(e
,f
));
149 case LTT_TYPE_POINTER
:
153 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
155 g_string_append_printf(s
, "0x%" PRIx64
, ltt_event_get_long_unsigned(e
,f
));
156 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
159 case LTT_TYPE_STRING
:
163 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
165 g_string_append_printf(s
, "\"%s\"", ltt_event_get_string(e
,f
));
171 GQuark value
= ltt_enum_string_get(type
, ltt_event_get_unsigned(e
,f
));
173 name
= ltt_field_name(f
);
175 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
178 g_string_append_printf(s
, "%s", g_quark_to_string(value
));
180 g_string_append_printf(s
, "%lld", ltt_event_get_long_int(e
,f
));
187 name
= ltt_field_name(f
);
189 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
191 // g_string_append_printf(s, "{ ");
193 g_string_append_printf(s
, type
->header
);//tested, works fine.
196 nb
= ltt_event_field_element_number(e
,f
);
197 for(i
= 0 ; i
< nb
; i
++) {
198 LttField
*child
= ltt_event_field_element_select(e
,f
,i
);
199 lttv_print_field(e
, child
, s
, field_names
, i
);
201 g_string_append_printf(s
,type
->separator
);
203 //g_string_append_printf(s, " }");
205 g_string_append_printf(s
, type
->footer
);//tested, works fine.
210 name
= ltt_field_name(f
);
212 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
214 // g_string_append_printf(s, "{ ");
216 g_string_append_printf(s
, type
->header
);
218 nb
= ltt_type_member_number(type
);
219 for(i
= 0 ; i
< nb
; i
++) {
221 element
= ltt_field_member(f
,i
);
222 lttv_print_field(e
, element
, s
, field_names
, i
);
224 g_string_append_printf(s
,type
->separator
);
226 //g_string_append_printf(s, " }");
228 g_string_append_printf(s
, type
->footer
);
233 name
= ltt_field_name(f
);
235 g_string_append_printf(s
, "%s = ", g_quark_to_string(name
));
237 // g_string_append_printf(s, "{ ");
238 g_string_append_printf(s
, type
->header
);
240 nb
= ltt_type_member_number(type
);
241 for(i
= 0 ; i
< nb
; i
++) {
243 element
= ltt_field_member(f
,i
);
244 lttv_print_field(e
, element
, s
, field_names
, i
);
246 g_string_append_printf(s
, type
->separator
);
248 // g_string_append_printf(s, " }");
249 g_string_append_printf(s
, type
->footer
);
252 case LTT_TYPE_COMPACT
:
253 g_error("compact type printing not implemented");
260 int getProcessInfosFromEvent(LttvEvent
*event
, GString
* processInfos
)
262 int pid
=0, tid
=0, ppid
=0;
263 const char *procname
;
265 unsigned long timestamp
;
269 gboolean noError
= TRUE
;
272 LttvTraceState
*state
= event
->state
;
273 LttvProcessState
*process
;
274 struct bt_ctf_event
*ctf_event
= event
->bt_event
;
276 cpu
= lttv_traceset_get_cpuid_from_event(event
);
278 process
= state
->running_process
[cpu
];
280 timestamp
= bt_ctf_get_timestamp(ctf_event
);
284 ppid
= process
->ppid
;
285 procname
= g_quark_to_string(process
->name
);
286 if (timestamp
== -1ULL) {
291 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_EVENT_CONTEXT
);
292 if (bt_ctf_field_get_error()) {
297 pid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_pid"));
298 if (bt_ctf_field_get_error()) {
303 tid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_tid"));
304 if (bt_ctf_field_get_error()) {
309 ppid
= bt_ctf_get_int64(bt_ctf_get_field(ctf_event
, scope
, "_ppid"));
310 if (bt_ctf_field_get_error()) {
315 procname
= bt_ctf_get_char_array(bt_ctf_get_field(ctf_event
, scope
, "_procname"));
316 if (bt_ctf_field_get_error()) {
322 g_string_append_printf(processInfos
, "%u, %u, %s, %u. %s, %s", pid
, tid
, procname
, ppid
, g_quark_to_string(process
->state
->t
), g_quark_to_string(process
->state
->s
));
332 int getCPUIdFromEvent(LttvEvent
*event
, GString
* cpuId_str
)
336 cpuid
= lttv_traceset_get_cpuid_from_event(event
);
340 g_string_append_printf(cpuId_str
, "%u", cpuid
);
344 int getFields(struct bt_ctf_event
*ctf_event
, struct bt_definition
const *fields
, GString
* fieldsStr
)
346 enum ctf_type_id fieldType
= bt_ctf_field_type(bt_ctf_get_decl_from_def(fields
));
347 int ret
= 0, isSigned
= -1, len
= 0, i
= 0;
348 const struct bt_definition
*index_def
;
350 case CTF_TYPE_INTEGER
:
351 isSigned
= bt_ctf_get_int_signedness(bt_ctf_get_decl_from_def(fields
));
353 g_string_append_printf(fieldsStr
, "%lu", bt_ctf_get_int64(fields
));
355 else if (isSigned
== 0) {
356 g_string_append_printf(fieldsStr
, "%llu", bt_ctf_get_uint64(fields
));
359 case CTF_TYPE_STRING
:
360 g_string_append_printf(fieldsStr
, "%s", bt_ctf_get_string(fields
));
364 g_string_append_printf(fieldsStr
, "[ ");
365 len
= bt_ctf_get_array_len(bt_ctf_get_decl_from_def(fields
));
366 if ((index_def
= bt_ctf_get_index(ctf_event
, fields
, i
))) {
367 for (i
= 0; i
< len
; i
++) {
369 g_string_append_printf(fieldsStr
, ", ");
371 //bt_ctf_field_type( bt_ctf_get_index(ctf_event, fields, i));
372 g_string_append_printf(fieldsStr
, " ");
373 g_string_append_printf(fieldsStr
, "[%d] = ",i
);
374 getFields(ctf_event
, bt_ctf_get_index(ctf_event
, fields
, i
), fieldsStr
);
378 g_string_append_printf(fieldsStr
, "%s", bt_ctf_get_char_array(fields
));
380 g_string_append_printf(fieldsStr
, " ]");
383 case CTF_TYPE_UNKNOWN
:
384 g_string_append_printf(fieldsStr
, "TYPE UNKNOWN");
386 g_string_append_printf(fieldsStr
, "TYPE UNIMP %i",fieldType
);
392 int getFieldsFromEvent(struct bt_ctf_event
*ctf_event
, GString
* fields
, gboolean field_names
)
394 struct bt_definition
const * const *list
= NULL
;
397 gboolean noError
= TRUE
;
398 const struct bt_definition
*scope
;
399 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_EVENT_FIELDS
);
405 ret
= bt_ctf_get_field_list(ctf_event
, scope
, &list
, &count
);
410 for (i
= 0; i
< count
; i
++) {
412 g_string_append_printf(fields
, ", ");
414 const char *name
= bt_ctf_field_name(list
[i
]);
416 g_string_append_printf(fields
, "%s = ", name
);
418 getFields(ctf_event
, list
[i
] ,fields
);
419 if (bt_ctf_field_get_error()) {
431 void lttv_event_to_string(LttvEvent
*event
, GString
*a_string
,
432 gboolean field_names
, gboolean long_version
)
434 GString
* processInfos
= g_string_new("");
435 GString
* fields
= g_string_new("");
436 GString
* cpuId_str
= g_string_new("");
438 getProcessInfosFromEvent(event
, processInfos
);
439 getFieldsFromEvent(event
->bt_event
, fields
, field_names
);
440 getCPUIdFromEvent(event
, cpuId_str
);
442 g_string_set_size(a_string
,0);
444 g_string_append_printf(a_string
, "%llu %s: ",
445 bt_ctf_get_timestamp(event
->bt_event
),
446 bt_ctf_event_name(event
->bt_event
));
448 g_string_append_printf(a_string
, "{ %s }", cpuId_str
->str
);
450 if (strcmp("", processInfos
->str
) < 0) {
451 g_string_append_printf(a_string
, ", { %s }", processInfos
->str
);
453 if (strcmp("", fields
->str
) < 0) {
454 g_string_append_printf(a_string
, ", { %s }", fields
->str
);
457 g_string_free(fields
, TRUE
);
458 g_string_free(processInfos
, TRUE
);
459 g_string_free(cpuId_str
, TRUE
);
461 void lttv_event_get_name(LttvEvent
*event
,GString
*a_string
)
463 g_string_set_size(a_string
,0);
464 g_string_append_printf(a_string
, " %s", bt_ctf_event_name(event
->bt_event
));
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, %u, 0x%" PRIx64
", %s",
495 g_quark_to_string(process
->name
),
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", \