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" |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 14 | #include "target.h" |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 15 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 16 | #define NSECS_PER_SEC 1000000000ULL |
| 17 | #define NSECS_PER_USEC 1000ULL |
| 18 | |
Arnaldo Carvalho de Melo | b44308f | 2010-10-26 15:20:09 -0200 | [diff] [blame] | 19 | int verbose; |
| 20 | bool dump_trace = false, quiet = false; |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 21 | int debug_ordered_events; |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 22 | static int redirect_to_stderr; |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 23 | int debug_data_convert; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 24 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 25 | static int _eprintf(int level, int var, const char *fmt, va_list args) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 26 | { |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 27 | int ret = 0; |
| 28 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 29 | if (var >= level) { |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 30 | if (use_browser >= 1 && !redirect_to_stderr) |
Namhyung Kim | b56e533 | 2012-11-15 01:47:41 +0900 | [diff] [blame] | 31 | ui_helpline__vshow(fmt, args); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 32 | else |
| 33 | ret = vfprintf(stderr, fmt, args); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | return ret; |
| 37 | } |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 38 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 39 | int eprintf(int level, int var, const char *fmt, ...) |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 40 | { |
| 41 | va_list args; |
| 42 | int ret; |
| 43 | |
| 44 | va_start(args, fmt); |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 45 | ret = _eprintf(level, var, fmt, args); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 46 | va_end(args); |
| 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 51 | static int __eprintf_time(u64 t, const char *fmt, va_list args) |
| 52 | { |
| 53 | int ret = 0; |
| 54 | u64 secs, usecs, nsecs = t; |
| 55 | |
| 56 | secs = nsecs / NSECS_PER_SEC; |
| 57 | nsecs -= secs * NSECS_PER_SEC; |
| 58 | usecs = nsecs / NSECS_PER_USEC; |
| 59 | |
| 60 | ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ", |
| 61 | secs, usecs); |
| 62 | ret += vfprintf(stderr, fmt, args); |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | int eprintf_time(int level, int var, u64 t, const char *fmt, ...) |
| 67 | { |
| 68 | int ret = 0; |
| 69 | va_list args; |
| 70 | |
| 71 | if (var >= level) { |
| 72 | va_start(args, fmt); |
| 73 | ret = __eprintf_time(t, fmt, args); |
| 74 | va_end(args); |
| 75 | } |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 80 | /* |
| 81 | * Overloading libtraceevent standard info print |
| 82 | * function, display with -v in perf. |
| 83 | */ |
| 84 | void pr_stat(const char *fmt, ...) |
| 85 | { |
| 86 | va_list args; |
| 87 | |
| 88 | va_start(args, fmt); |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 89 | _eprintf(1, verbose, fmt, args); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 90 | va_end(args); |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 91 | eprintf(1, verbose, "\n"); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 94 | int dump_printf(const char *fmt, ...) |
| 95 | { |
| 96 | va_list args; |
| 97 | int ret = 0; |
| 98 | |
| 99 | if (dump_trace) { |
| 100 | va_start(args, fmt); |
| 101 | ret = vprintf(fmt, args); |
| 102 | va_end(args); |
| 103 | } |
| 104 | |
| 105 | return ret; |
| 106 | } |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 107 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 108 | void trace_event(union perf_event *event) |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 109 | { |
| 110 | unsigned char *raw_event = (void *)event; |
| 111 | const char *color = PERF_COLOR_BLUE; |
| 112 | int i, j; |
| 113 | |
| 114 | if (!dump_trace) |
| 115 | return; |
| 116 | |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 117 | printf("."); |
| 118 | color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n", |
| 119 | event->header.size); |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 120 | |
| 121 | for (i = 0; i < event->header.size; i++) { |
| 122 | if ((i & 15) == 0) { |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 123 | printf("."); |
| 124 | color_fprintf(stdout, color, " %04x: ", i); |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 127 | color_fprintf(stdout, color, " %02x", raw_event[i]); |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 128 | |
| 129 | if (((i & 15) == 15) || i == event->header.size-1) { |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 130 | color_fprintf(stdout, color, " "); |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 131 | for (j = 0; j < 15-(i & 15); j++) |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 132 | color_fprintf(stdout, color, " "); |
Andy Isaacson | 84c104a | 2010-06-11 19:44:04 -0700 | [diff] [blame] | 133 | for (j = i & ~15; j <= i; j++) { |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 134 | color_fprintf(stdout, color, "%c", |
| 135 | isprint(raw_event[j]) ? |
| 136 | raw_event[j] : '.'); |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 137 | } |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 138 | color_fprintf(stdout, color, "\n"); |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
Arnaldo Carvalho de Melo | 5b1c144 | 2010-11-30 17:48:53 -0200 | [diff] [blame] | 141 | printf(".\n"); |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 142 | } |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 143 | |
| 144 | static struct debug_variable { |
| 145 | const char *name; |
| 146 | int *ptr; |
| 147 | } debug_variables[] = { |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 148 | { .name = "verbose", .ptr = &verbose }, |
| 149 | { .name = "ordered-events", .ptr = &debug_ordered_events}, |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 150 | { .name = "stderr", .ptr = &redirect_to_stderr}, |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 151 | { .name = "data-convert", .ptr = &debug_data_convert }, |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 152 | { .name = NULL, } |
| 153 | }; |
| 154 | |
| 155 | int perf_debug_option(const char *str) |
| 156 | { |
| 157 | struct debug_variable *var = &debug_variables[0]; |
| 158 | char *vstr, *s = strdup(str); |
| 159 | int v = 1; |
| 160 | |
| 161 | vstr = strchr(s, '='); |
| 162 | if (vstr) |
| 163 | *vstr++ = 0; |
| 164 | |
| 165 | while (var->name) { |
| 166 | if (!strcmp(s, var->name)) |
| 167 | break; |
| 168 | var++; |
| 169 | } |
| 170 | |
| 171 | if (!var->name) { |
| 172 | pr_err("Unknown debug variable name '%s'\n", s); |
| 173 | free(s); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | if (vstr) { |
| 178 | v = atoi(vstr); |
| 179 | /* |
| 180 | * Allow only values in range (0, 10), |
| 181 | * otherwise set 0. |
| 182 | */ |
| 183 | v = (v < 0) || (v > 10) ? 0 : v; |
| 184 | } |
| 185 | |
| 186 | *var->ptr = v; |
| 187 | free(s); |
| 188 | return 0; |
| 189 | } |