| 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 | const char *procname; |
| 268 | |
| 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. %s, %s", pid, tid, procname, ppid, g_quark_to_string(process->state->t), g_quark_to_string(process->state->s)); |
| 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 bt_definition const *fields, GString* fieldsStr) |
| 349 | { |
| 350 | enum ctf_type_id fieldType = bt_ctf_field_type(bt_ctf_get_decl_from_def(fields)); |
| 351 | int ret = 0, isSigned = -1, len = 0, i = 0; |
| 352 | const struct bt_definition *index_def; |
| 353 | switch (fieldType) { |
| 354 | case CTF_TYPE_INTEGER: |
| 355 | isSigned = bt_ctf_get_int_signedness(bt_ctf_get_decl_from_def(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(bt_ctf_get_decl_from_def(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] = ",i); |
| 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 | g_string_append_printf(fieldsStr, "TYPE UNKNOWN"); |
| 389 | default: |
| 390 | g_string_append_printf(fieldsStr, "TYPE UNIMP %i",fieldType ); |
| 391 | break; |
| 392 | } |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | int getFieldsFromEvent(struct bt_ctf_event *ctf_event, GString* fields, gboolean field_names) |
| 397 | { |
| 398 | struct bt_definition const * const *list = NULL; |
| 399 | unsigned int count; |
| 400 | int i = 0, ret = 0; |
| 401 | gboolean noError = TRUE; |
| 402 | const struct bt_definition *scope; |
| 403 | scope = bt_ctf_get_top_level_scope(ctf_event, BT_EVENT_FIELDS); |
| 404 | |
| 405 | if (!scope) { |
| 406 | noError = FALSE; |
| 407 | } |
| 408 | if (noError) { |
| 409 | ret = bt_ctf_get_field_list(ctf_event, scope, &list, &count); |
| 410 | if (ret < 0) { |
| 411 | noError = TRUE; |
| 412 | } |
| 413 | else { |
| 414 | for (i = 0; i < count; i++) { |
| 415 | if (i > 0) { |
| 416 | g_string_append_printf(fields, ", "); |
| 417 | } |
| 418 | const char *name = bt_ctf_field_name(list[i]); |
| 419 | if (field_names) { |
| 420 | g_string_append_printf(fields, "%s = ", name); |
| 421 | } |
| 422 | getFields(ctf_event, list[i] ,fields); |
| 423 | if (bt_ctf_field_get_error()) { |
| 424 | continue; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | if (!noError) { |
| 430 | ret = -1; |
| 431 | } |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | void lttv_event_to_string(LttvEvent *event, GString *a_string, |
| 436 | gboolean field_names, gboolean long_version) |
| 437 | { |
| 438 | GString* processInfos = g_string_new(""); |
| 439 | GString* fields = g_string_new(""); |
| 440 | GString* cpuId_str = g_string_new(""); |
| 441 | |
| 442 | getProcessInfosFromEvent(event, processInfos); |
| 443 | getFieldsFromEvent(event->bt_event, fields, field_names); |
| 444 | getCPUIdFromEvent(event, cpuId_str); |
| 445 | |
| 446 | g_string_set_size(a_string,0); |
| 447 | if(long_version){ |
| 448 | g_string_append_printf(a_string, "%llu %s: ", |
| 449 | bt_ctf_get_timestamp(event->bt_event), |
| 450 | bt_ctf_event_name(event->bt_event)); |
| 451 | } |
| 452 | g_string_append_printf(a_string, "{ %s }", cpuId_str->str); |
| 453 | |
| 454 | if (strcmp("", processInfos->str) < 0) { |
| 455 | g_string_append_printf(a_string, ", { %s }", processInfos->str); |
| 456 | } |
| 457 | if (strcmp("", fields->str) < 0) { |
| 458 | g_string_append_printf(a_string, ", { %s }", fields->str); |
| 459 | } |
| 460 | |
| 461 | g_string_free(fields, TRUE); |
| 462 | g_string_free(processInfos, TRUE); |
| 463 | g_string_free(cpuId_str, TRUE); |
| 464 | } |
| 465 | void lttv_event_get_name(LttvEvent *event,GString *a_string) |
| 466 | { |
| 467 | g_string_set_size(a_string,0); |
| 468 | g_string_append_printf(a_string, " %s", bt_ctf_event_name(event->bt_event)); |
| 469 | } |
| 470 | #ifdef BABEL_CLEANUP |
| 471 | void lttv_event_to_string(LttEvent *e, GString *s, gboolean mandatory_fields, |
| 472 | gboolean field_names, LttvTracefileState *tfs) |
| 473 | { |
| 474 | struct marker_field *field; |
| 475 | struct marker_info *info; |
| 476 | |
| 477 | LttTime time; |
| 478 | |
| 479 | guint cpu = tfs->cpu; |
| 480 | LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context; |
| 481 | LttvProcessState *process = ts->running_process[cpu]; |
| 482 | |
| 483 | s = g_string_set_size(s,0); |
| 484 | |
| 485 | info = marker_get_info_from_id(tfs->parent.tf->mdata, e->event_id); |
| 486 | |
| 487 | if(mandatory_fields) { |
| 488 | time = ltt_event_time(e); |
| 489 | g_string_append_printf(s,"%s.%s: %ld.%09ld (%s/%s_%u)", |
| 490 | g_quark_to_string(ltt_tracefile_name(tfs->parent.tf)), |
| 491 | g_quark_to_string(info->name), (long)time.tv_sec, time.tv_nsec, |
| 492 | g_quark_to_string( |
| 493 | ltt_trace_name(ltt_tracefile_get_trace(tfs->parent.tf))), |
| 494 | g_quark_to_string(ltt_tracefile_name(tfs->parent.tf)), cpu); |
| 495 | /* Print the process id and the state/interrupt type of the process */ |
| 496 | g_string_append_printf(s,", %u, %u, %s, %s, %u, 0x%" PRIx64", %s", |
| 497 | process->pid, |
| 498 | process->tgid, |
| 499 | g_quark_to_string(process->name), |
| 500 | g_quark_to_string(process->brand), |
| 501 | process->ppid, |
| 502 | process->current_function, |
| 503 | g_quark_to_string(process->state->t)); |
| 504 | } |
| 505 | |
| 506 | if(marker_get_num_fields(info) == 0) return; |
| 507 | g_string_append_printf(s, " "); |
| 508 | g_string_append_printf(s, "{ "); |
| 509 | for (field = marker_get_field(info, 0); |
| 510 | field != marker_get_field(info, marker_get_num_fields(info)); |
| 511 | field++) { |
| 512 | if(field != marker_get_field(info, 0)) |
| 513 | g_string_append_printf(s, ", "); |
| 514 | lttv_print_field(e, field, s, field_names, tfs); |
| 515 | } |
| 516 | g_string_append_printf(s, " }"); |
| 517 | } |
| 518 | #endif /* BABEL_CLEANUP */ |
| 519 | |
| 520 | static void init() |
| 521 | { |
| 522 | } |
| 523 | |
| 524 | static void destroy() |
| 525 | { |
| 526 | } |
| 527 | |
| 528 | LTTV_MODULE("print", "Print events", \ |
| 529 | "Produce a detailed text printout of events", \ |
| 530 | init, destroy) |
| 531 | |