1 // SPDX-License-Identifier: GPL-2.0
2 /* For general debugging purposes */
11 #include <api/debug.h>
12 #include <linux/time64.h>
13 #ifdef HAVE_BACKTRACE_SUPPORT
20 #include "print_binary.h"
24 #include "sane_ctype.h"
27 bool dump_trace = false, quiet = false;
28 int debug_ordered_events;
29 static int redirect_to_stderr;
30 int debug_data_convert;
32 int veprintf(int level, int var, const char *fmt, va_list args)
37 if (use_browser >= 1 && !redirect_to_stderr)
38 ui_helpline__vshow(fmt, args);
40 ret = vfprintf(stderr, fmt, args);
46 int eprintf(int level, int var, const char *fmt, ...)
52 ret = veprintf(level, var, fmt, args);
58 static int veprintf_time(u64 t, const char *fmt, va_list args)
61 u64 secs, usecs, nsecs = t;
63 secs = nsecs / NSEC_PER_SEC;
64 nsecs -= secs * NSEC_PER_SEC;
65 usecs = nsecs / NSEC_PER_USEC;
67 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
69 ret += vfprintf(stderr, fmt, args);
73 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
80 ret = veprintf_time(t, fmt, args);
88 * Overloading libtraceevent standard info print
89 * function, display with -v in perf.
91 void pr_stat(const char *fmt, ...)
96 veprintf(1, verbose, fmt, args);
98 eprintf(1, verbose, "\n");
101 int dump_printf(const char *fmt, ...)
108 ret = vprintf(fmt, args);
115 static int trace_event_printer(enum binary_printer_ops op,
116 unsigned int val, void *extra, FILE *fp)
118 const char *color = PERF_COLOR_BLUE;
119 union perf_event *event = (union perf_event *)extra;
120 unsigned char ch = (unsigned char)val;
124 case BINARY_PRINT_DATA_BEGIN:
125 printed += fprintf(fp, ".");
126 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
129 case BINARY_PRINT_LINE_BEGIN:
130 printed += fprintf(fp, ".");
132 case BINARY_PRINT_ADDR:
133 printed += color_fprintf(fp, color, " %04x: ", val);
135 case BINARY_PRINT_NUM_DATA:
136 printed += color_fprintf(fp, color, " %02x", val);
138 case BINARY_PRINT_NUM_PAD:
139 printed += color_fprintf(fp, color, " ");
141 case BINARY_PRINT_SEP:
142 printed += color_fprintf(fp, color, " ");
144 case BINARY_PRINT_CHAR_DATA:
145 printed += color_fprintf(fp, color, "%c",
146 isprint(ch) ? ch : '.');
148 case BINARY_PRINT_CHAR_PAD:
149 printed += color_fprintf(fp, color, " ");
151 case BINARY_PRINT_LINE_END:
152 printed += color_fprintf(fp, color, "\n");
154 case BINARY_PRINT_DATA_END:
155 printed += fprintf(fp, "\n");
164 void trace_event(union perf_event *event)
166 unsigned char *raw_event = (void *)event;
171 print_binary(raw_event, event->header.size, 16,
172 trace_event_printer, event);
175 static struct debug_variable {
178 } debug_variables[] = {
179 { .name = "verbose", .ptr = &verbose },
180 { .name = "ordered-events", .ptr = &debug_ordered_events},
181 { .name = "stderr", .ptr = &redirect_to_stderr},
182 { .name = "data-convert", .ptr = &debug_data_convert },
186 int perf_debug_option(const char *str)
188 struct debug_variable *var = &debug_variables[0];
189 char *vstr, *s = strdup(str);
192 vstr = strchr(s, '=');
197 if (!strcmp(s, var->name))
203 pr_err("Unknown debug variable name '%s'\n", s);
211 * Allow only values in range (0, 10),
214 v = (v < 0) || (v > 10) ? 0 : v;
225 int perf_quiet_option(void)
227 struct debug_variable *var = &debug_variables[0];
229 /* disable all debug messages */
238 #define DEBUG_WRAPPER(__n, __l) \
239 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
244 va_start(args, fmt); \
245 ret = veprintf(__l, verbose, fmt, args); \
250 DEBUG_WRAPPER(warning, 0);
251 DEBUG_WRAPPER(debug, 1);
253 void perf_debug_setup(void)
255 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
258 /* Obtain a backtrace and print it to stdout. */
259 #ifdef HAVE_BACKTRACE_SUPPORT
260 void dump_stack(void)
263 size_t size = backtrace(array, ARRAY_SIZE(array));
264 char **strings = backtrace_symbols(array, size);
267 printf("Obtained %zd stack frames.\n", size);
269 for (i = 0; i < size; i++)
270 printf("%s\n", strings[i]);
275 void dump_stack(void) {}
278 void sighandler_dump_stack(int sig)
280 psignal(sig, "perf");
282 signal(sig, SIG_DFL);