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