Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1 | /* For general debugging purposes */ |
| 2 | |
| 3 | #include "../perf.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 4 | |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <stdio.h> |
| 8 | |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 9 | #include "color.h" |
| 10 | #include "event.h" |
| 11 | #include "debug.h" |
Arnaldo Carvalho de Melo | 4a58e61 | 2009-12-27 21:37:00 -0200 | [diff] [blame] | 12 | #include "util.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 13 | |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 14 | int verbose = 0; |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 15 | int dump_trace = 0; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 17 | int eprintf(int level, const char *fmt, ...) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 18 | { |
| 19 | va_list args; |
| 20 | int ret = 0; |
| 21 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 22 | if (verbose >= level) { |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 23 | va_start(args, fmt); |
| 24 | ret = vfprintf(stderr, fmt, args); |
| 25 | va_end(args); |
| 26 | } |
| 27 | |
| 28 | return ret; |
| 29 | } |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 30 | |
| 31 | int dump_printf(const char *fmt, ...) |
| 32 | { |
| 33 | va_list args; |
| 34 | int ret = 0; |
| 35 | |
| 36 | if (dump_trace) { |
| 37 | va_start(args, fmt); |
| 38 | ret = vprintf(fmt, args); |
| 39 | va_end(args); |
| 40 | } |
| 41 | |
| 42 | return ret; |
| 43 | } |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 44 | |
| 45 | static int dump_printf_color(const char *fmt, const char *color, ...) |
| 46 | { |
| 47 | va_list args; |
| 48 | int ret = 0; |
| 49 | |
| 50 | if (dump_trace) { |
| 51 | va_start(args, color); |
| 52 | ret = color_vfprintf(stdout, color, fmt, args); |
| 53 | va_end(args); |
| 54 | } |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void trace_event(event_t *event) |
| 61 | { |
| 62 | unsigned char *raw_event = (void *)event; |
| 63 | const char *color = PERF_COLOR_BLUE; |
| 64 | int i, j; |
| 65 | |
| 66 | if (!dump_trace) |
| 67 | return; |
| 68 | |
| 69 | dump_printf("."); |
| 70 | dump_printf_color("\n. ... raw event: size %d bytes\n", color, |
| 71 | event->header.size); |
| 72 | |
| 73 | for (i = 0; i < event->header.size; i++) { |
| 74 | if ((i & 15) == 0) { |
| 75 | dump_printf("."); |
| 76 | dump_printf_color(" %04x: ", color, i); |
| 77 | } |
| 78 | |
| 79 | dump_printf_color(" %02x", color, raw_event[i]); |
| 80 | |
| 81 | if (((i & 15) == 15) || i == event->header.size-1) { |
| 82 | dump_printf_color(" ", color); |
| 83 | for (j = 0; j < 15-(i & 15); j++) |
| 84 | dump_printf_color(" ", color); |
| 85 | for (j = 0; j < (i & 15); j++) { |
| 86 | if (isprint(raw_event[i-15+j])) |
| 87 | dump_printf_color("%c", color, |
| 88 | raw_event[i-15+j]); |
| 89 | else |
| 90 | dump_printf_color(".", color); |
| 91 | } |
| 92 | dump_printf_color("\n", color); |
| 93 | } |
| 94 | } |
| 95 | dump_printf(".\n"); |
| 96 | } |