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