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> |
Jiri Olsa | dd629cc | 2016-02-14 17:03:45 +0100 | [diff] [blame] | 8 | #include <api/debug.h> |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 10 | #include "cache.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 11 | #include "color.h" |
| 12 | #include "event.h" |
| 13 | #include "debug.h" |
Arnaldo Carvalho de Melo | 4a58e61 | 2009-12-27 21:37:00 -0200 | [diff] [blame] | 14 | #include "util.h" |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 15 | #include "target.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 16 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 17 | #define NSECS_PER_SEC 1000000000ULL |
| 18 | #define NSECS_PER_USEC 1000ULL |
| 19 | |
Arnaldo Carvalho de Melo | b44308f | 2010-10-26 15:20:09 -0200 | [diff] [blame] | 20 | int verbose; |
| 21 | bool dump_trace = false, quiet = false; |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 22 | int debug_ordered_events; |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 23 | static int redirect_to_stderr; |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 24 | int debug_data_convert; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 25 | |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 26 | int veprintf(int level, int var, const char *fmt, va_list args) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 27 | { |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 28 | int ret = 0; |
| 29 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 30 | if (var >= level) { |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 31 | if (use_browser >= 1 && !redirect_to_stderr) |
Namhyung Kim | b56e533 | 2012-11-15 01:47:41 +0900 | [diff] [blame] | 32 | ui_helpline__vshow(fmt, args); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 33 | else |
| 34 | ret = vfprintf(stderr, fmt, args); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | return ret; |
| 38 | } |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 39 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 40 | int eprintf(int level, int var, const char *fmt, ...) |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 41 | { |
| 42 | va_list args; |
| 43 | int ret; |
| 44 | |
| 45 | va_start(args, fmt); |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 46 | ret = veprintf(level, var, fmt, args); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 47 | va_end(args); |
| 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 52 | static int veprintf_time(u64 t, const char *fmt, va_list args) |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 53 | { |
| 54 | int ret = 0; |
| 55 | u64 secs, usecs, nsecs = t; |
| 56 | |
| 57 | secs = nsecs / NSECS_PER_SEC; |
| 58 | nsecs -= secs * NSECS_PER_SEC; |
| 59 | usecs = nsecs / NSECS_PER_USEC; |
| 60 | |
| 61 | ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ", |
| 62 | secs, usecs); |
| 63 | ret += vfprintf(stderr, fmt, args); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | int eprintf_time(int level, int var, u64 t, const char *fmt, ...) |
| 68 | { |
| 69 | int ret = 0; |
| 70 | va_list args; |
| 71 | |
| 72 | if (var >= level) { |
| 73 | va_start(args, fmt); |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 74 | ret = veprintf_time(t, fmt, args); |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 75 | va_end(args); |
| 76 | } |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 81 | /* |
| 82 | * Overloading libtraceevent standard info print |
| 83 | * function, display with -v in perf. |
| 84 | */ |
| 85 | void pr_stat(const char *fmt, ...) |
| 86 | { |
| 87 | va_list args; |
| 88 | |
| 89 | va_start(args, fmt); |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 90 | veprintf(1, verbose, fmt, args); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 91 | va_end(args); |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 92 | eprintf(1, verbose, "\n"); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 95 | int dump_printf(const char *fmt, ...) |
| 96 | { |
| 97 | va_list args; |
| 98 | int ret = 0; |
| 99 | |
| 100 | if (dump_trace) { |
| 101 | va_start(args, fmt); |
| 102 | ret = vprintf(fmt, args); |
| 103 | va_end(args); |
| 104 | } |
| 105 | |
| 106 | return ret; |
| 107 | } |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 108 | |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 109 | static void trace_event_printer(enum binary_printer_ops op, |
| 110 | unsigned int val, void *extra) |
| 111 | { |
| 112 | const char *color = PERF_COLOR_BLUE; |
| 113 | union perf_event *event = (union perf_event *)extra; |
| 114 | unsigned char ch = (unsigned char)val; |
| 115 | |
| 116 | switch (op) { |
| 117 | case BINARY_PRINT_DATA_BEGIN: |
| 118 | printf("."); |
| 119 | color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n", |
| 120 | event->header.size); |
| 121 | break; |
| 122 | case BINARY_PRINT_LINE_BEGIN: |
| 123 | printf("."); |
| 124 | break; |
| 125 | case BINARY_PRINT_ADDR: |
| 126 | color_fprintf(stdout, color, " %04x: ", val); |
| 127 | break; |
| 128 | case BINARY_PRINT_NUM_DATA: |
| 129 | color_fprintf(stdout, color, " %02x", val); |
| 130 | break; |
| 131 | case BINARY_PRINT_NUM_PAD: |
| 132 | color_fprintf(stdout, color, " "); |
| 133 | break; |
| 134 | case BINARY_PRINT_SEP: |
| 135 | color_fprintf(stdout, color, " "); |
| 136 | break; |
| 137 | case BINARY_PRINT_CHAR_DATA: |
| 138 | color_fprintf(stdout, color, "%c", |
| 139 | isprint(ch) ? ch : '.'); |
| 140 | break; |
| 141 | case BINARY_PRINT_CHAR_PAD: |
| 142 | color_fprintf(stdout, color, " "); |
| 143 | break; |
| 144 | case BINARY_PRINT_LINE_END: |
| 145 | color_fprintf(stdout, color, "\n"); |
| 146 | break; |
| 147 | case BINARY_PRINT_DATA_END: |
| 148 | printf("\n"); |
| 149 | break; |
| 150 | default: |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 155 | void trace_event(union perf_event *event) |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 156 | { |
| 157 | unsigned char *raw_event = (void *)event; |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 158 | |
| 159 | if (!dump_trace) |
| 160 | return; |
| 161 | |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 162 | print_binary(raw_event, event->header.size, 16, |
| 163 | trace_event_printer, event); |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 164 | } |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 165 | |
| 166 | static struct debug_variable { |
| 167 | const char *name; |
| 168 | int *ptr; |
| 169 | } debug_variables[] = { |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 170 | { .name = "verbose", .ptr = &verbose }, |
| 171 | { .name = "ordered-events", .ptr = &debug_ordered_events}, |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 172 | { .name = "stderr", .ptr = &redirect_to_stderr}, |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 173 | { .name = "data-convert", .ptr = &debug_data_convert }, |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 174 | { .name = NULL, } |
| 175 | }; |
| 176 | |
| 177 | int perf_debug_option(const char *str) |
| 178 | { |
| 179 | struct debug_variable *var = &debug_variables[0]; |
| 180 | char *vstr, *s = strdup(str); |
| 181 | int v = 1; |
| 182 | |
| 183 | vstr = strchr(s, '='); |
| 184 | if (vstr) |
| 185 | *vstr++ = 0; |
| 186 | |
| 187 | while (var->name) { |
| 188 | if (!strcmp(s, var->name)) |
| 189 | break; |
| 190 | var++; |
| 191 | } |
| 192 | |
| 193 | if (!var->name) { |
| 194 | pr_err("Unknown debug variable name '%s'\n", s); |
| 195 | free(s); |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | if (vstr) { |
| 200 | v = atoi(vstr); |
| 201 | /* |
| 202 | * Allow only values in range (0, 10), |
| 203 | * otherwise set 0. |
| 204 | */ |
| 205 | v = (v < 0) || (v > 10) ? 0 : v; |
| 206 | } |
| 207 | |
| 208 | *var->ptr = v; |
| 209 | free(s); |
| 210 | return 0; |
| 211 | } |
Jiri Olsa | dd629cc | 2016-02-14 17:03:45 +0100 | [diff] [blame] | 212 | |
| 213 | #define DEBUG_WRAPPER(__n, __l) \ |
| 214 | static int pr_ ## __n ## _wrapper(const char *fmt, ...) \ |
| 215 | { \ |
| 216 | va_list args; \ |
| 217 | int ret; \ |
| 218 | \ |
| 219 | va_start(args, fmt); \ |
| 220 | ret = veprintf(__l, verbose, fmt, args); \ |
| 221 | va_end(args); \ |
| 222 | return ret; \ |
| 223 | } |
| 224 | |
| 225 | DEBUG_WRAPPER(warning, 0); |
| 226 | DEBUG_WRAPPER(debug, 1); |
| 227 | |
| 228 | void perf_debug_setup(void) |
| 229 | { |
| 230 | libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper); |
| 231 | } |