5707175c3ee92627c5bbe568f42a3bc58fb64cad
[lttv.git] / lttv / lttv / print.c
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
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 */
19
20 /* print.c
21 *
22 * Event printing routines.
23 */
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>
35 #ifdef BABEL_CLEANUP
36 #include <lttv/stats.h>
37 #include <lttv/filter.h>
38 #endif
39 #include <lttv/print.h>
40 #include <ltt/ltt.h>
41 #include <ltt/event.h>
42 #include <ltt/trace.h>
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <ltt/ltt-private.h>
46 #include <babeltrace/ctf/events.h>
47 #include <string.h>
48 #include <inttypes.h>
49 #include <lttv/event.h>
50 #include <lttv/traceset.h>
51 #ifdef BABEL_CLEANUP
52 static inline void print_enum_events(LttEvent *e, struct marker_field *f,
53 guint64 value, GString *s, LttvTracefileState *tfs)
54 {
55 LttvTraceState *ts = (LttvTraceState*)(tfs->parent.t_context);
56 LttvNameTables *nt = ts->name_tables;
57
58 if (tf->name == LTT_CHANNEL_KERNEL) {
59 if (info->name == LTT_EVENT_SYSCALL_ENTRY
60 && f->name == LTT_FIELD_SYSCALL_ID) {
61 g_string_append_printf(s, " [%s]",
62 g_quark_to_string(nt->syscall_names[value]));
63 } else if ((info->name == LTT_EVENT_SOFT_IRQ_ENTRY
64 || info->name == LTT_EVENT_SOFT_IRQ_EXIT
65 || info->name == LTT_EVENT_SOFT_IRQ_RAISE)
66 && f->name == LTT_FIELD_SOFT_IRQ_ID) {
67 g_string_append_printf(s, " [%s]",
68 g_quark_to_string(nt->soft_irq_names[value]));
69 } else if (info->name == LTT_EVENT_KPROBE
70 && f->name == LTT_FIELD_IP) {
71 #if (__WORDSIZE == 32)
72 GQuark symbol = (GQuark)g_hash_table_lookup(nt->kprobe_hash,
73 (gconstpointer)&value);
74 #else
75 GQuark symbol = (GQuark)(unsigned long)g_hash_table_lookup(nt->kprobe_hash,
76 (gconstpointer)value);
77 #endif
78 if (symbol)
79 g_string_append_printf(s, " [%s]", g_quark_to_string(symbol));
80 }
81 }
82 }
83
84 void lttv_print_field(LttEvent *e, struct marker_field *f, GString *s,
85 gboolean field_names, LttvTracefileState *tfs)
86 {
87 GQuark name;
88 guint64 value;
89
90 //int nb, i;
91
92 switch(f->type) {
93 case LTT_TYPE_SIGNED_INT:
94 if(field_names) {
95 name = f->name;
96 if(name)
97 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
98 }
99 value = ltt_event_get_long_int(e,f);
100 //g_string_append_printf(s, "%lld", value);
101 g_string_append_printf(s, f->fmt->str, value);
102 //g_string_append_printf(s, type->fmt, ltt_event_get_long_int(e,f));
103 print_enum_events(e, f, value, s, tfs);
104 break;
105
106 case LTT_TYPE_UNSIGNED_INT:
107 if(field_names) {
108 name = f->name;
109 if(name)
110 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
111 }
112 value = ltt_event_get_long_unsigned(e,f);
113 //g_string_append_printf(s, "%llu", value);
114 g_string_append_printf(s, f->fmt->str, value);
115 print_enum_events(e, f, value, s, tfs);
116 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
117 break;
118
119 #if 0
120 case LTT_CHAR:
121 case LTT_UCHAR:
122 {
123 unsigned car = ltt_event_get_unsigned(e,f);
124 if(field_names) {
125 name = ltt_field_name(f);
126 if(name)
127 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
128 }
129 if(isprint(car)) {
130 if(field_names) {
131 name = ltt_field_name(f);
132 if(name)
133 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
134 }
135 //g_string_append_printf(s, "%c", car);
136 g_string_append_printf(s, type->fmt, car);
137 } else {
138 g_string_append_printf(s, "\\%x", car);
139 }
140 }
141 break;
142 case LTT_FLOAT:
143 if(field_names) {
144 name = ltt_field_name(f);
145 if(name)
146 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
147 }
148 //g_string_append_printf(s, "%g", ltt_event_get_double(e,f));
149 g_string_append_printf(s, type->fmt, ltt_event_get_double(e,f));
150 break;
151 #endif
152
153 case LTT_TYPE_POINTER:
154 if(field_names) {
155 name = f->name;
156 if(name)
157 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
158 }
159 g_string_append_printf(s, "0x%" PRIx64, ltt_event_get_long_unsigned(e,f));
160 //g_string_append_printf(s, type->fmt, ltt_event_get_long_unsigned(e,f));
161 break;
162
163 case LTT_TYPE_STRING:
164 if(field_names) {
165 name = f->name;
166 if(name)
167 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
168 }
169 g_string_append_printf(s, "\"%s\"", ltt_event_get_string(e,f));
170 break;
171
172 #if 0
173 case LTT_ENUM:
174 {
175 GQuark value = ltt_enum_string_get(type, ltt_event_get_unsigned(e,f));
176 if(field_names) {
177 name = ltt_field_name(f);
178 if(name)
179 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
180 }
181 if(value)
182 g_string_append_printf(s, "%s", g_quark_to_string(value));
183 else
184 g_string_append_printf(s, "%lld", ltt_event_get_long_int(e,f));
185 }
186 break;
187
188 case LTT_ARRAY:
189 case LTT_SEQUENCE:
190 if(field_names) {
191 name = ltt_field_name(f);
192 if(name)
193 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
194 }
195 // g_string_append_printf(s, "{ ");
196 //Insert header
197 g_string_append_printf(s, type->header);//tested, works fine.
198
199
200 nb = ltt_event_field_element_number(e,f);
201 for(i = 0 ; i < nb ; i++) {
202 LttField *child = ltt_event_field_element_select(e,f,i);
203 lttv_print_field(e, child, s, field_names, i);
204 if(i<nb-1)
205 g_string_append_printf(s,type->separator);
206 }
207 //g_string_append_printf(s, " }");
208 //Insert footer
209 g_string_append_printf(s, type->footer);//tested, works fine.
210 break;
211
212 case LTT_STRUCT:
213 if(field_names) {
214 name = ltt_field_name(f);
215 if(name)
216 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
217 }
218 // g_string_append_printf(s, "{ ");
219 //Insert header
220 g_string_append_printf(s, type->header);
221
222 nb = ltt_type_member_number(type);
223 for(i = 0 ; i < nb ; i++) {
224 LttField *element;
225 element = ltt_field_member(f,i);
226 lttv_print_field(e, element, s, field_names, i);
227 if(i < nb-1)
228 g_string_append_printf(s,type->separator);
229 }
230 //g_string_append_printf(s, " }");
231 //Insert footer
232 g_string_append_printf(s, type->footer);
233 break;
234
235 case LTT_UNION:
236 if(field_names) {
237 name = ltt_field_name(f);
238 if(name)
239 g_string_append_printf(s, "%s = ", g_quark_to_string(name));
240 }
241 // g_string_append_printf(s, "{ ");
242 g_string_append_printf(s, type->header);
243
244 nb = ltt_type_member_number(type);
245 for(i = 0 ; i < nb ; i++) {
246 LttField *element;
247 element = ltt_field_member(f,i);
248 lttv_print_field(e, element, s, field_names, i);
249 if(i<nb-1)
250 g_string_append_printf(s, type->separator);
251 }
252 // g_string_append_printf(s, " }");
253 g_string_append_printf(s, type->footer);
254 break;
255 #endif
256 case LTT_TYPE_COMPACT:
257 g_error("compact type printing not implemented");
258 break;
259 case LTT_TYPE_NONE:
260 break;
261 }
262 }
263 #endif
264 int getProcessInfosFromEvent(LttvEvent *event, GString* processInfos)
265 {
266 int pid=0, tid=0, ppid=0;
267 char *procname;
268 struct definition *scope;
269 unsigned long timestamp;
270
271 int ret = 0;
272
273 gboolean noError = TRUE;
274
275 guint cpu;
276 LttvTraceState *state = event->state;
277 LttvProcessState *process;
278 struct bt_ctf_event *ctf_event = event->bt_event;
279
280 cpu = lttv_traceset_get_cpuid_from_event(event);
281
282 process = state->running_process[cpu];
283
284 timestamp = bt_ctf_get_timestamp(ctf_event);
285
286 pid = process->pid;
287 tid = process->tgid;
288 ppid = process->ppid;
289 procname = g_quark_to_string(process->name);
290 if (timestamp == -1ULL) {
291 noError = FALSE;
292 }
293 #if 0
294 if (noError) {
295 scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_EVENT_CONTEXT);
296 if (bt_ctf_field_get_error()) {
297 noError = FALSE;
298 }
299 }
300 if (noError) {
301 pid = bt_ctf_get_int64(bt_ctf_get_field(ctf_event, scope, "_pid"));
302 if (bt_ctf_field_get_error()) {
303 noError = FALSE;
304 }
305 }
306 if (noError) {
307 tid = bt_ctf_get_int64(bt_ctf_get_field(ctf_event, scope, "_tid"));
308 if (bt_ctf_field_get_error()) {
309 noError = FALSE;
310 }
311 }
312 if (noError) {
313 ppid = bt_ctf_get_int64(bt_ctf_get_field(ctf_event, scope, "_ppid"));
314 if (bt_ctf_field_get_error()) {
315 noError = FALSE;
316 }
317 }
318 if (noError) {
319 procname = bt_ctf_get_char_array(bt_ctf_get_field(ctf_event, scope, "_procname"));
320 if (bt_ctf_field_get_error()) {
321 noError = FALSE;
322 }
323 }
324 #endif
325 if (noError||1) {
326 g_string_append_printf(processInfos, "%u, %u, %s, %u", pid, tid, procname, ppid);
327 }
328 else {
329 ret = -1;
330 }
331
332 return ret;
333 }
334
335 static
336 int getCPUIdFromEvent(LttvEvent *event, GString* cpuId_str)
337 {
338 gint cpuid;
339
340 cpuid = lttv_traceset_get_cpuid_from_event(event);
341 if (cpuid < 0) {
342 return -1;
343 }
344 g_string_append_printf(cpuId_str, "%u", cpuid);
345 return 0;
346 }
347
348 int getFields(struct bt_ctf_event *ctf_event, struct definition const *fields, GString* fieldsStr)
349 {
350 enum ctf_type_id fieldType = bt_ctf_field_type(fields);
351 int ret = 0, isSigned = -1, len = 0, i = 0;
352 struct definition *index_def;
353 switch (fieldType) {
354 case CTF_TYPE_INTEGER:
355 isSigned = bt_ctf_get_int_signedness(fields);
356 if (isSigned == 1) {
357 g_string_append_printf(fieldsStr, "%lu", bt_ctf_get_int64(fields));
358 }
359 else if (isSigned == 0) {
360 g_string_append_printf(fieldsStr, "%llu", bt_ctf_get_uint64(fields));
361 }
362 break;
363 case CTF_TYPE_STRING:
364 g_string_append_printf(fieldsStr, "%s", bt_ctf_get_string(fields));
365 break;
366
367 case CTF_TYPE_ARRAY:
368 g_string_append_printf(fieldsStr, "[ ");
369 len = bt_ctf_get_array_len(fields);
370 if (index_def = bt_ctf_get_index(ctf_event, fields, i)) {
371 for (i = 0; i < len; i++) {
372 if (i > 0) {
373 g_string_append_printf(fieldsStr, ", ");
374 }
375 bt_ctf_field_type(bt_ctf_get_index(ctf_event, fields, i));
376 g_string_append_printf(fieldsStr, " ");
377 g_string_append_printf(fieldsStr, "[%d] = ");
378 getFields(ctf_event, bt_ctf_get_index(ctf_event, fields, i), fieldsStr);
379 }
380 }
381 else {
382 g_string_append_printf(fieldsStr, "%s", bt_ctf_get_char_array(fields));
383 }
384 g_string_append_printf(fieldsStr, " ]");
385
386 break;
387 case CTF_TYPE_UNKNOWN:
388 default:
389 break;
390 }
391 return ret;
392 }
393
394 int getFieldsFromEvent(struct bt_ctf_event *ctf_event, GString* fields, gboolean field_names)
395 {
396 struct definition const * const *list = NULL;
397 unsigned int count;
398 int i = 0, j = 0, ret = 0;
399 gboolean noError = TRUE;
400 struct definition *scope;
401 scope = bt_ctf_get_top_level_scope(ctf_event, BT_EVENT_FIELDS);
402
403 if (!scope) {
404 noError = FALSE;
405 }
406 if (noError) {
407 ret = bt_ctf_get_field_list(ctf_event, scope, &list, &count);
408 if (ret < 0) {
409 noError = TRUE;
410 }
411 else {
412 for (i = 0; i < count; i++) {
413 if (i > 0) {
414 g_string_append_printf(fields, ", ");
415 }
416 const char *name = bt_ctf_field_name(list[i]);
417 if (field_names) {
418 g_string_append_printf(fields, "%s = ", name);
419 }
420 getFields(ctf_event, list[i] ,fields);
421 if (bt_ctf_field_get_error()) {
422 continue;
423 }
424 }
425 }
426 }
427 if (!noError) {
428 ret = -1;
429 }
430 return ret;
431 }
432
433 void lttv_event_to_string(LttvEvent *event, GString *a_string, gboolean field_names)
434 {
435 GString* processInfos = g_string_new("");
436 GString* fields = g_string_new("");
437 GString* cpuId_str = g_string_new("");
438
439 getProcessInfosFromEvent(event, processInfos);
440 getFieldsFromEvent(event->bt_event, fields, field_names);
441 getCPUIdFromEvent(event, cpuId_str);
442
443 g_string_set_size(a_string,0);
444
445 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);
446 if (strcmp("", processInfos->str) < 0) {
447 g_string_append_printf(a_string, ", { %s }", processInfos->str);
448 }
449 if (strcmp("", fields->str) < 0) {
450 g_string_append_printf(a_string, ", { %s }", fields->str);
451 }
452
453 g_string_free(fields, TRUE);
454 g_string_free(processInfos, TRUE);
455 g_string_free(cpuId_str, TRUE);
456 }
457
458 #ifdef BABEL_CLEANUP
459 void lttv_event_to_string(LttEvent *e, GString *s, gboolean mandatory_fields,
460 gboolean field_names, LttvTracefileState *tfs)
461 {
462 struct marker_field *field;
463 struct marker_info *info;
464
465 LttTime time;
466
467 guint cpu = tfs->cpu;
468 LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
469 LttvProcessState *process = ts->running_process[cpu];
470
471 s = g_string_set_size(s,0);
472
473 info = marker_get_info_from_id(tfs->parent.tf->mdata, e->event_id);
474
475 if(mandatory_fields) {
476 time = ltt_event_time(e);
477 g_string_append_printf(s,"%s.%s: %ld.%09ld (%s/%s_%u)",
478 g_quark_to_string(ltt_tracefile_name(tfs->parent.tf)),
479 g_quark_to_string(info->name), (long)time.tv_sec, time.tv_nsec,
480 g_quark_to_string(
481 ltt_trace_name(ltt_tracefile_get_trace(tfs->parent.tf))),
482 g_quark_to_string(ltt_tracefile_name(tfs->parent.tf)), cpu);
483 /* Print the process id and the state/interrupt type of the process */
484 g_string_append_printf(s,", %u, %u, %s, %s, %u, 0x%" PRIx64", %s",
485 process->pid,
486 process->tgid,
487 g_quark_to_string(process->name),
488 g_quark_to_string(process->brand),
489 process->ppid,
490 process->current_function,
491 g_quark_to_string(process->state->t));
492 }
493
494 if(marker_get_num_fields(info) == 0) return;
495 g_string_append_printf(s, " ");
496 g_string_append_printf(s, "{ ");
497 for (field = marker_get_field(info, 0);
498 field != marker_get_field(info, marker_get_num_fields(info));
499 field++) {
500 if(field != marker_get_field(info, 0))
501 g_string_append_printf(s, ", ");
502 lttv_print_field(e, field, s, field_names, tfs);
503 }
504 g_string_append_printf(s, " }");
505 }
506 #endif /* BABEL_CLEANUP */
507
508 static void init()
509 {
510 }
511
512 static void destroy()
513 {
514 }
515
516 LTTV_MODULE("print", "Print events", \
517 "Produce a detailed text printout of events", \
518 init, destroy)
519
This page took 0.038565 seconds and 3 git commands to generate.