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