1 // SPDX-License-Identifier: GPL-2.0
2 /* For general debugging purposes */
10 #include <api/debug.h>
11 #include <linux/kernel.h>
12 #include <linux/time64.h>
13 #ifdef HAVE_BACKTRACE_SUPPORT
19 #include "print_binary.h"
21 #include "ui/helpline.h"
23 #include "util/parse-sublevel-options.h"
25 #include <linux/ctype.h>
29 bool dump_trace = false, quiet = false;
30 int debug_ordered_events;
31 static int redirect_to_stderr;
32 int debug_data_convert;
34 int veprintf(int level, int var, const char *fmt, va_list args)
39 if (use_browser >= 1 && !redirect_to_stderr)
40 ui_helpline__vshow(fmt, args);
42 ret = vfprintf(stderr, fmt, args);
48 int eprintf(int level, int var, const char *fmt, ...)
54 ret = veprintf(level, var, fmt, args);
60 static int veprintf_time(u64 t, const char *fmt, va_list args)
63 u64 secs, usecs, nsecs = t;
65 secs = nsecs / NSEC_PER_SEC;
66 nsecs -= secs * NSEC_PER_SEC;
67 usecs = nsecs / NSEC_PER_USEC;
69 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
71 ret += vfprintf(stderr, fmt, args);
75 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
82 ret = veprintf_time(t, fmt, args);
90 * Overloading libtraceevent standard info print
91 * function, display with -v in perf.
93 void pr_stat(const char *fmt, ...)
98 veprintf(1, verbose, fmt, args);
100 eprintf(1, verbose, "\n");
103 int dump_printf(const char *fmt, ...)
110 ret = vprintf(fmt, args);
117 static int trace_event_printer(enum binary_printer_ops op,
118 unsigned int val, void *extra, FILE *fp)
120 const char *color = PERF_COLOR_BLUE;
121 union perf_event *event = (union perf_event *)extra;
122 unsigned char ch = (unsigned char)val;
126 case BINARY_PRINT_DATA_BEGIN:
127 printed += fprintf(fp, ".");
128 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
131 case BINARY_PRINT_LINE_BEGIN:
132 printed += fprintf(fp, ".");
134 case BINARY_PRINT_ADDR:
135 printed += color_fprintf(fp, color, " %04x: ", val);
137 case BINARY_PRINT_NUM_DATA:
138 printed += color_fprintf(fp, color, " %02x", val);
140 case BINARY_PRINT_NUM_PAD:
141 printed += color_fprintf(fp, color, " ");
143 case BINARY_PRINT_SEP:
144 printed += color_fprintf(fp, color, " ");
146 case BINARY_PRINT_CHAR_DATA:
147 printed += color_fprintf(fp, color, "%c",
148 isprint(ch) && isascii(ch) ? ch : '.');
150 case BINARY_PRINT_CHAR_PAD:
151 printed += color_fprintf(fp, color, " ");
153 case BINARY_PRINT_LINE_END:
154 printed += color_fprintf(fp, color, "\n");
156 case BINARY_PRINT_DATA_END:
157 printed += fprintf(fp, "\n");
166 void trace_event(union perf_event *event)
168 unsigned char *raw_event = (void *)event;
173 print_binary(raw_event, event->header.size, 16,
174 trace_event_printer, event);
177 static struct sublevel_option debug_opts[] = {
178 { .name = "verbose", .value_ptr = &verbose },
179 { .name = "ordered-events", .value_ptr = &debug_ordered_events},
180 { .name = "stderr", .value_ptr = &redirect_to_stderr},
181 { .name = "data-convert", .value_ptr = &debug_data_convert },
182 { .name = "perf-event-open", .value_ptr = &debug_peo_args },
186 int perf_debug_option(const char *str)
190 ret = perf_parse_sublevel_options(str, debug_opts);
194 /* Allow only verbose value in range (0, 10), otherwise set 0. */
195 verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
200 int perf_quiet_option(void)
202 struct sublevel_option *opt = &debug_opts[0];
204 /* disable all debug messages */
206 *opt->value_ptr = -1;
210 /* For debug variables that are used as bool types, set to 0. */
211 redirect_to_stderr = 0;
217 #define DEBUG_WRAPPER(__n, __l) \
218 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
223 va_start(args, fmt); \
224 ret = veprintf(__l, verbose, fmt, args); \
229 DEBUG_WRAPPER(warning, 0);
230 DEBUG_WRAPPER(debug, 1);
232 void perf_debug_setup(void)
234 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
237 /* Obtain a backtrace and print it to stdout. */
238 #ifdef HAVE_BACKTRACE_SUPPORT
239 void dump_stack(void)
242 size_t size = backtrace(array, ARRAY_SIZE(array));
243 char **strings = backtrace_symbols(array, size);
246 printf("Obtained %zd stack frames.\n", size);
248 for (i = 0; i < size; i++)
249 printf("%s\n", strings[i]);
254 void dump_stack(void) {}
257 void sighandler_dump_stack(int sig)
259 psignal(sig, "perf");
261 signal(sig, SIG_DFL);