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;
33 static FILE *debug_file;
35 void debug_set_file(FILE *file)
40 int veprintf(int level, int var, const char *fmt, va_list args)
45 if (use_browser >= 1 && !redirect_to_stderr)
46 ui_helpline__vshow(fmt, args);
48 ret = vfprintf(debug_file, fmt, args);
54 int eprintf(int level, int var, const char *fmt, ...)
60 ret = veprintf(level, var, fmt, args);
66 static int veprintf_time(u64 t, const char *fmt, va_list args)
69 u64 secs, usecs, nsecs = t;
71 secs = nsecs / NSEC_PER_SEC;
72 nsecs -= secs * NSEC_PER_SEC;
73 usecs = nsecs / NSEC_PER_USEC;
75 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
77 ret += vfprintf(stderr, fmt, args);
81 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
88 ret = veprintf_time(t, fmt, args);
96 * Overloading libtraceevent standard info print
97 * function, display with -v in perf.
99 void pr_stat(const char *fmt, ...)
104 veprintf(1, verbose, fmt, args);
106 eprintf(1, verbose, "\n");
109 int dump_printf(const char *fmt, ...)
116 ret = vprintf(fmt, args);
123 static int trace_event_printer(enum binary_printer_ops op,
124 unsigned int val, void *extra, FILE *fp)
126 const char *color = PERF_COLOR_BLUE;
127 union perf_event *event = (union perf_event *)extra;
128 unsigned char ch = (unsigned char)val;
132 case BINARY_PRINT_DATA_BEGIN:
133 printed += fprintf(fp, ".");
134 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
137 case BINARY_PRINT_LINE_BEGIN:
138 printed += fprintf(fp, ".");
140 case BINARY_PRINT_ADDR:
141 printed += color_fprintf(fp, color, " %04x: ", val);
143 case BINARY_PRINT_NUM_DATA:
144 printed += color_fprintf(fp, color, " %02x", val);
146 case BINARY_PRINT_NUM_PAD:
147 printed += color_fprintf(fp, color, " ");
149 case BINARY_PRINT_SEP:
150 printed += color_fprintf(fp, color, " ");
152 case BINARY_PRINT_CHAR_DATA:
153 printed += color_fprintf(fp, color, "%c",
154 isprint(ch) ? ch : '.');
156 case BINARY_PRINT_CHAR_PAD:
157 printed += color_fprintf(fp, color, " ");
159 case BINARY_PRINT_LINE_END:
160 printed += color_fprintf(fp, color, "\n");
162 case BINARY_PRINT_DATA_END:
163 printed += fprintf(fp, "\n");
172 void trace_event(union perf_event *event)
174 unsigned char *raw_event = (void *)event;
179 print_binary(raw_event, event->header.size, 16,
180 trace_event_printer, event);
183 static struct sublevel_option debug_opts[] = {
184 { .name = "verbose", .value_ptr = &verbose },
185 { .name = "ordered-events", .value_ptr = &debug_ordered_events},
186 { .name = "stderr", .value_ptr = &redirect_to_stderr},
187 { .name = "data-convert", .value_ptr = &debug_data_convert },
188 { .name = "perf-event-open", .value_ptr = &debug_peo_args },
192 int perf_debug_option(const char *str)
196 ret = perf_parse_sublevel_options(str, debug_opts);
200 /* Allow only verbose value in range (0, 10), otherwise set 0. */
201 verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
206 int perf_quiet_option(void)
208 struct sublevel_option *opt = &debug_opts[0];
210 /* disable all debug messages */
212 *opt->value_ptr = -1;
219 #define DEBUG_WRAPPER(__n, __l) \
220 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
225 va_start(args, fmt); \
226 ret = veprintf(__l, verbose, fmt, args); \
231 DEBUG_WRAPPER(warning, 0);
232 DEBUG_WRAPPER(debug, 1);
234 void perf_debug_setup(void)
236 debug_set_file(stderr);
237 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
240 /* Obtain a backtrace and print it to stdout. */
241 #ifdef HAVE_BACKTRACE_SUPPORT
242 void dump_stack(void)
245 size_t size = backtrace(array, ARRAY_SIZE(array));
246 char **strings = backtrace_symbols(array, size);
249 printf("Obtained %zd stack frames.\n", size);
251 for (i = 0; i < size; i++)
252 printf("%s\n", strings[i]);
257 void dump_stack(void) {}
260 void sighandler_dump_stack(int sig)
262 psignal(sig, "perf");
264 signal(sig, SIG_DFL);