Update FSF address
[lttv.git] / lttv / lttv / print.c
CommitLineData
8e680509 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * 2005 Mathieu Desnoyers
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
b9ce0bad
YB
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301, USA.
8e680509 18 */
19
20/* print.c
21 *
5290ec02 22 * Event printing routines.
23 */
8e680509 24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
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>
7a4bdb54 35#ifdef BABEL_CLEANUP
8e680509 36#include <lttv/stats.h>
37#include <lttv/filter.h>
7a4bdb54 38#endif
8e680509 39#include <lttv/print.h>
8e680509 40#include <stdio.h>
73e6c609 41#include <ctype.h>
73ccabb3 42#include <babeltrace/ctf/events.h>
e8548639 43#include <string.h>
43ed82b5 44#include <inttypes.h>
556b540a 45#include <lttv/event.h>
7a4bdb54
YB
46#include <lttv/traceset.h>
47#ifdef BABEL_CLEANUP
cf453ac7 48static inline void print_enum_events(LttEvent *e, struct marker_field *f,
90e19f82 49 guint64 value, GString *s, LttvTracefileState *tfs)
cf453ac7 50{
90e19f82
AM
51 LttvTraceState *ts = (LttvTraceState*)(tfs->parent.t_context);
52 LttvNameTables *nt = ts->name_tables;
53
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) {
e9a5b514 67#if (__WORDSIZE == 32)
90e19f82
AM
68 GQuark symbol = (GQuark)g_hash_table_lookup(nt->kprobe_hash,
69 (gconstpointer)&value);
9ee1ff6a 70#else
90e19f82
AM
71 GQuark symbol = (GQuark)(unsigned long)g_hash_table_lookup(nt->kprobe_hash,
72 (gconstpointer)value);
9ee1ff6a 73#endif
90e19f82
AM
74 if (symbol)
75 g_string_append_printf(s, " [%s]", g_quark_to_string(symbol));
76 }
77 }
cf453ac7 78}
8e680509 79
95868736 80void lttv_print_field(LttEvent *e, struct marker_field *f, GString *s,
90e19f82 81 gboolean field_names, LttvTracefileState *tfs)
95868736 82{
90e19f82
AM
83 GQuark name;
84 guint64 value;
85
86 //int nb, i;
8e680509 87
90e19f82
AM
88 switch(f->type) {
89 case LTT_TYPE_SIGNED_INT:
90 if(field_names) {
91 name = f->name;
92 if(name)
93 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
94 }
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);
100 break;
8e680509 101
90e19f82
AM
102 case LTT_TYPE_UNSIGNED_INT:
103 if(field_names) {
104 name = f->name;
105 if(name)
106 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
107 }
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));
113 break;
8e680509 114
95868736 115#if 0
90e19f82
AM
116 case LTT_CHAR:
117 case LTT_UCHAR:
118 {
119 unsigned car = ltt_event_get_unsigned(e,f);
120 if(field_names) {
121 name = ltt_field_name(f);
122 if(name)
123 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
124 }
125 if(isprint(car)) {
126 if(field_names) {
127 name = ltt_field_name(f);
128 if(name)
129 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
130 }
131 //g_string_append_printf(s, "%c", car);
132 g_string_append_printf(s, type->fmt, car);
133 } else {
134 g_string_append_printf(s, "\\%x", car);
135 }
136 }
137 break;
138 case LTT_FLOAT:
139 if(field_names) {
140 name = ltt_field_name(f);
141 if(name)
142 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
143 }
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));
146 break;
91346f59 147#endif
8e680509 148
90e19f82
AM
149 case LTT_TYPE_POINTER:
150 if(field_names) {
151 name = f->name;
152 if(name)
153 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
154 }
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));
157 break;
8e680509 158
90e19f82
AM
159 case LTT_TYPE_STRING:
160 if(field_names) {
161 name = f->name;
162 if(name)
163 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
164 }
165 g_string_append_printf(s, "\"%s\"", ltt_event_get_string(e,f));
166 break;
8e680509 167
95868736 168#if 0
90e19f82
AM
169 case LTT_ENUM:
170 {
171 GQuark value = ltt_enum_string_get(type, ltt_event_get_unsigned(e,f));
172 if(field_names) {
173 name = ltt_field_name(f);
174 if(name)
175 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
176 }
177 if(value)
178 g_string_append_printf(s, "%s", g_quark_to_string(value));
179 else
180 g_string_append_printf(s, "%lld", ltt_event_get_long_int(e,f));
181 }
182 break;
8e680509 183
90e19f82
AM
184 case LTT_ARRAY:
185 case LTT_SEQUENCE:
186 if(field_names) {
187 name = ltt_field_name(f);
188 if(name)
189 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
190 }
191 // g_string_append_printf(s, "{ ");
192 //Insert header
193 g_string_append_printf(s, type->header);//tested, works fine.
e8548639 194
195
90e19f82
AM
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);
e8548639 200 if(i<nb-1)
90e19f82
AM
201 g_string_append_printf(s,type->separator);
202 }
203 //g_string_append_printf(s, " }");
204 //Insert footer
205 g_string_append_printf(s, type->footer);//tested, works fine.
206 break;
8e680509 207
90e19f82
AM
208 case LTT_STRUCT:
209 if(field_names) {
210 name = ltt_field_name(f);
211 if(name)
212 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
213 }
214 // g_string_append_printf(s, "{ ");
215 //Insert header
216 g_string_append_printf(s, type->header);
e8548639 217
90e19f82
AM
218 nb = ltt_type_member_number(type);
219 for(i = 0 ; i < nb ; i++) {
220 LttField *element;
221 element = ltt_field_member(f,i);
222 lttv_print_field(e, element, s, field_names, i);
e8548639 223 if(i < nb-1)
90e19f82
AM
224 g_string_append_printf(s,type->separator);
225 }
226 //g_string_append_printf(s, " }");
227 //Insert footer
228 g_string_append_printf(s, type->footer);
229 break;
8e680509 230
90e19f82
AM
231 case LTT_UNION:
232 if(field_names) {
233 name = ltt_field_name(f);
234 if(name)
235 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
236 }
237 // g_string_append_printf(s, "{ ");
238 g_string_append_printf(s, type->header);
e8548639 239
90e19f82
AM
240 nb = ltt_type_member_number(type);
241 for(i = 0 ; i < nb ; i++) {
242 LttField *element;
243 element = ltt_field_member(f,i);
244 lttv_print_field(e, element, s, field_names, i);
e8548639 245 if(i<nb-1)
90e19f82
AM
246 g_string_append_printf(s, type->separator);
247 }
248 // g_string_append_printf(s, " }");
249 g_string_append_printf(s, type->footer);
250 break;
95868736 251#endif
90e19f82
AM
252 case LTT_TYPE_COMPACT:
253 g_error("compact type printing not implemented");
254 break;
255 case LTT_TYPE_NONE:
256 break;
257 }
8e680509 258}
7a4bdb54
YB
259#endif
260int getProcessInfosFromEvent(LttvEvent *event, GString* processInfos)
73ccabb3 261{
7a4bdb54 262 int pid=0, tid=0, ppid=0;
1f3686cd
YB
263 const char *procname;
264
73ccabb3
HM
265 unsigned long timestamp;
266
267 int ret = 0;
268
269 gboolean noError = TRUE;
270
7a4bdb54
YB
271 guint cpu;
272 LttvTraceState *state = event->state;
273 LttvProcessState *process;
274 struct bt_ctf_event *ctf_event = event->bt_event;
275
276 cpu = lttv_traceset_get_cpuid_from_event(event);
277
278 process = state->running_process[cpu];
279
73ccabb3 280 timestamp = bt_ctf_get_timestamp(ctf_event);
7a4bdb54
YB
281
282 pid = process->pid;
283 tid = process->tgid;
284 ppid = process->ppid;
285 procname = g_quark_to_string(process->name);
73ccabb3
HM
286 if (timestamp == -1ULL) {
287 noError = FALSE;
288 }
7a4bdb54 289#if 0
73ccabb3
HM
290 if (noError) {
291 scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_EVENT_CONTEXT);
292 if (bt_ctf_field_get_error()) {
293 noError = FALSE;
294 }
295 }
296 if (noError) {
297 pid = bt_ctf_get_int64(bt_ctf_get_field(ctf_event, scope, "_pid"));
298 if (bt_ctf_field_get_error()) {
299 noError = FALSE;
300 }
301 }
302 if (noError) {
303 tid = bt_ctf_get_int64(bt_ctf_get_field(ctf_event, scope, "_tid"));
304 if (bt_ctf_field_get_error()) {
305 noError = FALSE;
306 }
307 }
308 if (noError) {
309 ppid = bt_ctf_get_int64(bt_ctf_get_field(ctf_event, scope, "_ppid"));
310 if (bt_ctf_field_get_error()) {
311 noError = FALSE;
312 }
313 }
314 if (noError) {
315 procname = bt_ctf_get_char_array(bt_ctf_get_field(ctf_event, scope, "_procname"));
316 if (bt_ctf_field_get_error()) {
317 noError = FALSE;
318 }
319 }
7a4bdb54
YB
320#endif
321 if (noError||1) {
88bf15f0 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));
73ccabb3
HM
323 }
324 else {
325 ret = -1;
326 }
327
328 return ret;
329}
330
7a4bdb54
YB
331static
332int getCPUIdFromEvent(LttvEvent *event, GString* cpuId_str)
73ccabb3 333{
7a4bdb54 334 gint cpuid;
73ccabb3 335
7a4bdb54
YB
336 cpuid = lttv_traceset_get_cpuid_from_event(event);
337 if (cpuid < 0) {
338 return -1;
73ccabb3 339 }
7a4bdb54
YB
340 g_string_append_printf(cpuId_str, "%u", cpuid);
341 return 0;
73ccabb3
HM
342}
343
7f092317 344int getFields(struct bt_ctf_event *ctf_event, struct bt_definition const *fields, GString* fieldsStr)
73ccabb3 345{
1f3686cd 346 enum ctf_type_id fieldType = bt_ctf_field_type(bt_ctf_get_decl_from_def(fields));
73ccabb3 347 int ret = 0, isSigned = -1, len = 0, i = 0;
7f092317 348 const struct bt_definition *index_def;
73ccabb3
HM
349 switch (fieldType) {
350 case CTF_TYPE_INTEGER:
1f3686cd 351 isSigned = bt_ctf_get_int_signedness(bt_ctf_get_decl_from_def(fields));
73ccabb3
HM
352 if (isSigned == 1) {
353 g_string_append_printf(fieldsStr, "%lu", bt_ctf_get_int64(fields));
354 }
355 else if (isSigned == 0) {
d04fa838 356 g_string_append_printf(fieldsStr, "%" PRIu64 , bt_ctf_get_uint64(fields));
73ccabb3
HM
357 }
358 break;
359 case CTF_TYPE_STRING:
360 g_string_append_printf(fieldsStr, "%s", bt_ctf_get_string(fields));
361 break;
362
363 case CTF_TYPE_ARRAY:
364 g_string_append_printf(fieldsStr, "[ ");
1f3686cd
YB
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))) {
73ccabb3
HM
367 for (i = 0; i < len; i++) {
368 if (i > 0) {
369 g_string_append_printf(fieldsStr, ", ");
370 }
1f3686cd 371 //bt_ctf_field_type( bt_ctf_get_index(ctf_event, fields, i));
73ccabb3 372 g_string_append_printf(fieldsStr, " ");
1f3686cd 373 g_string_append_printf(fieldsStr, "[%d] = ",i);
73ccabb3
HM
374 getFields(ctf_event, bt_ctf_get_index(ctf_event, fields, i), fieldsStr);
375 }
376 }
377 else {
378 g_string_append_printf(fieldsStr, "%s", bt_ctf_get_char_array(fields));
379 }
380 g_string_append_printf(fieldsStr, " ]");
381
382 break;
383 case CTF_TYPE_UNKNOWN:
1f3686cd 384 g_string_append_printf(fieldsStr, "TYPE UNKNOWN");
73ccabb3 385 default:
1f3686cd 386 g_string_append_printf(fieldsStr, "TYPE UNIMP %i",fieldType );
73ccabb3
HM
387 break;
388 }
389 return ret;
390}
391
392int getFieldsFromEvent(struct bt_ctf_event *ctf_event, GString* fields, gboolean field_names)
393{
7f092317 394 struct bt_definition const * const *list = NULL;
73ccabb3 395 unsigned int count;
1f3686cd 396 int i = 0, ret = 0;
73ccabb3 397 gboolean noError = TRUE;
7f092317 398 const struct bt_definition *scope;
73ccabb3
HM
399 scope = bt_ctf_get_top_level_scope(ctf_event, BT_EVENT_FIELDS);
400
401 if (!scope) {
402 noError = FALSE;
403 }
404 if (noError) {
405 ret = bt_ctf_get_field_list(ctf_event, scope, &list, &count);
406 if (ret < 0) {
407 noError = TRUE;
408 }
409 else {
410 for (i = 0; i < count; i++) {
411 if (i > 0) {
412 g_string_append_printf(fields, ", ");
413 }
414 const char *name = bt_ctf_field_name(list[i]);
415 if (field_names) {
416 g_string_append_printf(fields, "%s = ", name);
417 }
418 getFields(ctf_event, list[i] ,fields);
419 if (bt_ctf_field_get_error()) {
420 continue;
421 }
422 }
423 }
424 }
425 if (!noError) {
426 ret = -1;
427 }
428 return ret;
429}
430
6db3427f
FD
431void lttv_event_to_string(LttvEvent *event, GString *a_string,
432 gboolean field_names, gboolean long_version)
73ccabb3
HM
433{
434 GString* processInfos = g_string_new("");
435 GString* fields = g_string_new("");
436 GString* cpuId_str = g_string_new("");
437
7a4bdb54 438 getProcessInfosFromEvent(event, processInfos);
556b540a 439 getFieldsFromEvent(event->bt_event, fields, field_names);
7a4bdb54 440 getCPUIdFromEvent(event, cpuId_str);
73ccabb3
HM
441
442 g_string_set_size(a_string,0);
6db3427f 443 if(long_version){
d04fa838 444 g_string_append_printf(a_string,"%" PRIu64 " %s: ",
762e15b0 445 bt_ctf_get_timestamp(event->bt_event),
6db3427f
FD
446 bt_ctf_event_name(event->bt_event));
447 }
448 g_string_append_printf(a_string, "{ %s }", cpuId_str->str);
449
73ccabb3
HM
450 if (strcmp("", processInfos->str) < 0) {
451 g_string_append_printf(a_string, ", { %s }", processInfos->str);
452 }
453 if (strcmp("", fields->str) < 0) {
454 g_string_append_printf(a_string, ", { %s }", fields->str);
455 }
456
457 g_string_free(fields, TRUE);
458 g_string_free(processInfos, TRUE);
459 g_string_free(cpuId_str, TRUE);
460}
6db3427f
FD
461void lttv_event_get_name(LttvEvent *event,GString *a_string)
462{
463 g_string_set_size(a_string,0);
464 g_string_append_printf(a_string, " %s", bt_ctf_event_name(event->bt_event));
465}
73ccabb3 466#ifdef BABEL_CLEANUP
90e19f82
AM
467void lttv_event_to_string(LttEvent *e, GString *s, gboolean mandatory_fields,
468 gboolean field_names, LttvTracefileState *tfs)
8e680509 469{
90e19f82
AM
470 struct marker_field *field;
471 struct marker_info *info;
472
473 LttTime time;
8e680509 474
90e19f82
AM
475 guint cpu = tfs->cpu;
476 LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
477 LttvProcessState *process = ts->running_process[cpu];
8e680509 478
90e19f82 479 s = g_string_set_size(s,0);
76373d36 480
90e19f82 481 info = marker_get_info_from_id(tfs->parent.tf->mdata, e->event_id);
8e680509 482
90e19f82
AM
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,
488 g_quark_to_string(
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 */
beed0826 492 g_string_append_printf(s,", %u, %u, %s, %u, 0x%" PRIx64", %s",
90e19f82
AM
493 process->pid,
494 process->tgid,
495 g_quark_to_string(process->name),
90e19f82
AM
496 process->ppid,
497 process->current_function,
498 g_quark_to_string(process->state->t));
499 }
8e680509 500
90e19f82
AM
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));
506 field++) {
507 if(field != marker_get_field(info, 0))
508 g_string_append_printf(s, ", ");
509 lttv_print_field(e, field, s, field_names, tfs);
510 }
511 g_string_append_printf(s, " }");
8e680509 512}
73ccabb3 513#endif /* BABEL_CLEANUP */
8e680509 514
515static void init()
516{
517}
518
519static void destroy()
520{
521}
522
523LTTV_MODULE("print", "Print events", \
90e19f82
AM
524 "Produce a detailed text printout of events", \
525 init, destroy)
8e680509 526
This page took 0.082932 seconds and 4 git commands to generate.