Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2 | /* For general debugging purposes */ |
| 3 | |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 4 | #include <inttypes.h> |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <stdio.h> |
Arnaldo Carvalho de Melo | 215a0d3 | 2019-07-04 11:21:24 -0300 | [diff] [blame] | 8 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 4208735 | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 9 | #include <sys/wait.h> |
Jiri Olsa | dd629cc | 2016-02-14 17:03:45 +0100 | [diff] [blame] | 10 | #include <api/debug.h> |
Arnaldo Carvalho de Melo | 4cb3c6d | 2019-08-29 17:19:02 -0300 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 12 | #include <linux/time64.h> |
Arnaldo Carvalho de Melo | 8c2b7ca | 2017-04-19 16:21:59 -0300 | [diff] [blame] | 13 | #ifdef HAVE_BACKTRACE_SUPPORT |
| 14 | #include <execinfo.h> |
| 15 | #endif |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 16 | #include "color.h" |
| 17 | #include "event.h" |
| 18 | #include "debug.h" |
Arnaldo Carvalho de Melo | fea0139 | 2017-04-17 16:23:22 -0300 | [diff] [blame] | 19 | #include "print_binary.h" |
Namhyung Kim | 16ad2ff | 2012-05-07 14:09:02 +0900 | [diff] [blame] | 20 | #include "target.h" |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 21 | #include "ui/helpline.h" |
Arnaldo Carvalho de Melo | fa0d984 | 2019-08-30 12:52:25 -0300 | [diff] [blame] | 22 | #include "ui/ui.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 24 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 25 | |
Arnaldo Carvalho de Melo | b44308f | 2010-10-26 15:20:09 -0200 | [diff] [blame] | 26 | int verbose; |
Ravi Bangoria | ccd2674 | 2019-11-08 15:11:28 +0530 | [diff] [blame] | 27 | int debug_peo_args; |
Arnaldo Carvalho de Melo | b44308f | 2010-10-26 15:20:09 -0200 | [diff] [blame] | 28 | bool dump_trace = false, quiet = false; |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 29 | int debug_ordered_events; |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 30 | static int redirect_to_stderr; |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 31 | int debug_data_convert; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 32 | |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 33 | int veprintf(int level, int var, const char *fmt, va_list args) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 34 | { |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 35 | int ret = 0; |
| 36 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 37 | if (var >= level) { |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 38 | if (use_browser >= 1 && !redirect_to_stderr) |
Namhyung Kim | b56e533 | 2012-11-15 01:47:41 +0900 | [diff] [blame] | 39 | ui_helpline__vshow(fmt, args); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 40 | else |
| 41 | ret = vfprintf(stderr, fmt, args); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return ret; |
| 45 | } |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 46 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 47 | int eprintf(int level, int var, const char *fmt, ...) |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 48 | { |
| 49 | va_list args; |
| 50 | int ret; |
| 51 | |
| 52 | va_start(args, fmt); |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 53 | ret = veprintf(level, var, fmt, args); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 54 | va_end(args); |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 59 | static int veprintf_time(u64 t, const char *fmt, va_list args) |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 60 | { |
| 61 | int ret = 0; |
| 62 | u64 secs, usecs, nsecs = t; |
| 63 | |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 64 | secs = nsecs / NSEC_PER_SEC; |
| 65 | nsecs -= secs * NSEC_PER_SEC; |
| 66 | usecs = nsecs / NSEC_PER_USEC; |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 67 | |
| 68 | ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ", |
| 69 | secs, usecs); |
| 70 | ret += vfprintf(stderr, fmt, args); |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | int eprintf_time(int level, int var, u64 t, const char *fmt, ...) |
| 75 | { |
| 76 | int ret = 0; |
| 77 | va_list args; |
| 78 | |
| 79 | if (var >= level) { |
| 80 | va_start(args, fmt); |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 81 | ret = veprintf_time(t, fmt, args); |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 82 | va_end(args); |
| 83 | } |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 88 | /* |
| 89 | * Overloading libtraceevent standard info print |
| 90 | * function, display with -v in perf. |
| 91 | */ |
| 92 | void pr_stat(const char *fmt, ...) |
| 93 | { |
| 94 | va_list args; |
| 95 | |
| 96 | va_start(args, fmt); |
Arnaldo Carvalho de Melo | bedbdd4 | 2016-02-16 11:48:38 -0300 | [diff] [blame] | 97 | veprintf(1, verbose, fmt, args); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 98 | va_end(args); |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 99 | eprintf(1, verbose, "\n"); |
Jiri Olsa | f772abc | 2013-12-03 14:09:25 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 102 | int dump_printf(const char *fmt, ...) |
| 103 | { |
| 104 | va_list args; |
| 105 | int ret = 0; |
| 106 | |
| 107 | if (dump_trace) { |
| 108 | va_start(args, fmt); |
| 109 | ret = vprintf(fmt, args); |
| 110 | va_end(args); |
| 111 | } |
| 112 | |
| 113 | return ret; |
| 114 | } |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 115 | |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 116 | static int trace_event_printer(enum binary_printer_ops op, |
| 117 | unsigned int val, void *extra, FILE *fp) |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 118 | { |
| 119 | const char *color = PERF_COLOR_BLUE; |
| 120 | union perf_event *event = (union perf_event *)extra; |
| 121 | unsigned char ch = (unsigned char)val; |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 122 | int printed = 0; |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 123 | |
| 124 | switch (op) { |
| 125 | case BINARY_PRINT_DATA_BEGIN: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 126 | printed += fprintf(fp, "."); |
| 127 | printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n", |
| 128 | event->header.size); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 129 | break; |
| 130 | case BINARY_PRINT_LINE_BEGIN: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 131 | printed += fprintf(fp, "."); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 132 | break; |
| 133 | case BINARY_PRINT_ADDR: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 134 | printed += color_fprintf(fp, color, " %04x: ", val); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 135 | break; |
| 136 | case BINARY_PRINT_NUM_DATA: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 137 | printed += color_fprintf(fp, color, " %02x", val); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 138 | break; |
| 139 | case BINARY_PRINT_NUM_PAD: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 140 | printed += color_fprintf(fp, color, " "); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 141 | break; |
| 142 | case BINARY_PRINT_SEP: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 143 | printed += color_fprintf(fp, color, " "); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 144 | break; |
| 145 | case BINARY_PRINT_CHAR_DATA: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 146 | printed += color_fprintf(fp, color, "%c", |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 147 | isprint(ch) ? ch : '.'); |
| 148 | break; |
| 149 | case BINARY_PRINT_CHAR_PAD: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 150 | printed += color_fprintf(fp, color, " "); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 151 | break; |
| 152 | case BINARY_PRINT_LINE_END: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 153 | printed += color_fprintf(fp, color, "\n"); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 154 | break; |
| 155 | case BINARY_PRINT_DATA_END: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 156 | printed += fprintf(fp, "\n"); |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | } |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 161 | |
| 162 | return printed; |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 165 | void trace_event(union perf_event *event) |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 166 | { |
| 167 | unsigned char *raw_event = (void *)event; |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 168 | |
| 169 | if (!dump_trace) |
| 170 | return; |
| 171 | |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 172 | print_binary(raw_event, event->header.size, 16, |
| 173 | trace_event_printer, event); |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 174 | } |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 175 | |
| 176 | static struct debug_variable { |
| 177 | const char *name; |
| 178 | int *ptr; |
| 179 | } debug_variables[] = { |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 180 | { .name = "verbose", .ptr = &verbose }, |
| 181 | { .name = "ordered-events", .ptr = &debug_ordered_events}, |
Andi Kleen | f78eaef | 2014-11-21 13:38:00 -0800 | [diff] [blame] | 182 | { .name = "stderr", .ptr = &redirect_to_stderr}, |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 183 | { .name = "data-convert", .ptr = &debug_data_convert }, |
Ravi Bangoria | ccd2674 | 2019-11-08 15:11:28 +0530 | [diff] [blame] | 184 | { .name = "perf-event-open", .ptr = &debug_peo_args }, |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 185 | { .name = NULL, } |
| 186 | }; |
| 187 | |
| 188 | int perf_debug_option(const char *str) |
| 189 | { |
| 190 | struct debug_variable *var = &debug_variables[0]; |
| 191 | char *vstr, *s = strdup(str); |
| 192 | int v = 1; |
| 193 | |
| 194 | vstr = strchr(s, '='); |
| 195 | if (vstr) |
| 196 | *vstr++ = 0; |
| 197 | |
| 198 | while (var->name) { |
| 199 | if (!strcmp(s, var->name)) |
| 200 | break; |
| 201 | var++; |
| 202 | } |
| 203 | |
| 204 | if (!var->name) { |
| 205 | pr_err("Unknown debug variable name '%s'\n", s); |
| 206 | free(s); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | if (vstr) { |
| 211 | v = atoi(vstr); |
| 212 | /* |
| 213 | * Allow only values in range (0, 10), |
| 214 | * otherwise set 0. |
| 215 | */ |
| 216 | v = (v < 0) || (v > 10) ? 0 : v; |
| 217 | } |
| 218 | |
Namhyung Kim | 80df198 | 2017-02-17 17:17:37 +0900 | [diff] [blame] | 219 | if (quiet) |
| 220 | v = -1; |
| 221 | |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 222 | *var->ptr = v; |
| 223 | free(s); |
| 224 | return 0; |
| 225 | } |
Jiri Olsa | dd629cc | 2016-02-14 17:03:45 +0100 | [diff] [blame] | 226 | |
Namhyung Kim | 80df198 | 2017-02-17 17:17:37 +0900 | [diff] [blame] | 227 | int perf_quiet_option(void) |
| 228 | { |
| 229 | struct debug_variable *var = &debug_variables[0]; |
| 230 | |
| 231 | /* disable all debug messages */ |
| 232 | while (var->name) { |
| 233 | *var->ptr = -1; |
| 234 | var++; |
| 235 | } |
| 236 | |
Namhyung Kim | 80df198 | 2017-02-17 17:17:37 +0900 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
Jiri Olsa | dd629cc | 2016-02-14 17:03:45 +0100 | [diff] [blame] | 240 | #define DEBUG_WRAPPER(__n, __l) \ |
| 241 | static int pr_ ## __n ## _wrapper(const char *fmt, ...) \ |
| 242 | { \ |
| 243 | va_list args; \ |
| 244 | int ret; \ |
| 245 | \ |
| 246 | va_start(args, fmt); \ |
| 247 | ret = veprintf(__l, verbose, fmt, args); \ |
| 248 | va_end(args); \ |
| 249 | return ret; \ |
| 250 | } |
| 251 | |
| 252 | DEBUG_WRAPPER(warning, 0); |
| 253 | DEBUG_WRAPPER(debug, 1); |
| 254 | |
| 255 | void perf_debug_setup(void) |
| 256 | { |
| 257 | libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper); |
| 258 | } |
Arnaldo Carvalho de Melo | 8c2b7ca | 2017-04-19 16:21:59 -0300 | [diff] [blame] | 259 | |
| 260 | /* Obtain a backtrace and print it to stdout. */ |
| 261 | #ifdef HAVE_BACKTRACE_SUPPORT |
| 262 | void dump_stack(void) |
| 263 | { |
| 264 | void *array[16]; |
| 265 | size_t size = backtrace(array, ARRAY_SIZE(array)); |
| 266 | char **strings = backtrace_symbols(array, size); |
| 267 | size_t i; |
| 268 | |
| 269 | printf("Obtained %zd stack frames.\n", size); |
| 270 | |
| 271 | for (i = 0; i < size; i++) |
| 272 | printf("%s\n", strings[i]); |
| 273 | |
| 274 | free(strings); |
| 275 | } |
| 276 | #else |
| 277 | void dump_stack(void) {} |
| 278 | #endif |
| 279 | |
| 280 | void sighandler_dump_stack(int sig) |
| 281 | { |
| 282 | psignal(sig, "perf"); |
| 283 | dump_stack(); |
| 284 | signal(sig, SIG_DFL); |
| 285 | raise(sig); |
| 286 | } |