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"
24 #include <linux/ctype.h>
28 bool dump_trace
= false, quiet
= false;
29 int debug_ordered_events
;
30 static int redirect_to_stderr
;
31 int debug_data_convert
;
33 int veprintf(int level
, int var
, const char *fmt
, va_list args
)
38 if (use_browser
>= 1 && !redirect_to_stderr
)
39 ui_helpline__vshow(fmt
, args
);
41 ret
= vfprintf(stderr
, fmt
, args
);
47 int eprintf(int level
, int var
, const char *fmt
, ...)
53 ret
= veprintf(level
, var
, fmt
, args
);
59 static int veprintf_time(u64 t
, const char *fmt
, va_list args
)
62 u64 secs
, usecs
, nsecs
= t
;
64 secs
= nsecs
/ NSEC_PER_SEC
;
65 nsecs
-= secs
* NSEC_PER_SEC
;
66 usecs
= nsecs
/ NSEC_PER_USEC
;
68 ret
= fprintf(stderr
, "[%13" PRIu64
".%06" PRIu64
"] ",
70 ret
+= vfprintf(stderr
, fmt
, args
);
74 int eprintf_time(int level
, int var
, u64 t
, const char *fmt
, ...)
81 ret
= veprintf_time(t
, fmt
, args
);
89 * Overloading libtraceevent standard info print
90 * function, display with -v in perf.
92 void pr_stat(const char *fmt
, ...)
97 veprintf(1, verbose
, fmt
, args
);
99 eprintf(1, verbose
, "\n");
102 int dump_printf(const char *fmt
, ...)
109 ret
= vprintf(fmt
, args
);
116 static int trace_event_printer(enum binary_printer_ops op
,
117 unsigned int val
, void *extra
, FILE *fp
)
119 const char *color
= PERF_COLOR_BLUE
;
120 union perf_event
*event
= (union perf_event
*)extra
;
121 unsigned char ch
= (unsigned char)val
;
125 case BINARY_PRINT_DATA_BEGIN
:
126 printed
+= fprintf(fp
, ".");
127 printed
+= color_fprintf(fp
, color
, "\n. ... raw event: size %d bytes\n",
130 case BINARY_PRINT_LINE_BEGIN
:
131 printed
+= fprintf(fp
, ".");
133 case BINARY_PRINT_ADDR
:
134 printed
+= color_fprintf(fp
, color
, " %04x: ", val
);
136 case BINARY_PRINT_NUM_DATA
:
137 printed
+= color_fprintf(fp
, color
, " %02x", val
);
139 case BINARY_PRINT_NUM_PAD
:
140 printed
+= color_fprintf(fp
, color
, " ");
142 case BINARY_PRINT_SEP
:
143 printed
+= color_fprintf(fp
, color
, " ");
145 case BINARY_PRINT_CHAR_DATA
:
146 printed
+= color_fprintf(fp
, color
, "%c",
147 isprint(ch
) ? ch
: '.');
149 case BINARY_PRINT_CHAR_PAD
:
150 printed
+= color_fprintf(fp
, color
, " ");
152 case BINARY_PRINT_LINE_END
:
153 printed
+= color_fprintf(fp
, color
, "\n");
155 case BINARY_PRINT_DATA_END
:
156 printed
+= fprintf(fp
, "\n");
165 void trace_event(union perf_event
*event
)
167 unsigned char *raw_event
= (void *)event
;
172 print_binary(raw_event
, event
->header
.size
, 16,
173 trace_event_printer
, event
);
176 static struct debug_variable
{
179 } debug_variables
[] = {
180 { .name
= "verbose", .ptr
= &verbose
},
181 { .name
= "ordered-events", .ptr
= &debug_ordered_events
},
182 { .name
= "stderr", .ptr
= &redirect_to_stderr
},
183 { .name
= "data-convert", .ptr
= &debug_data_convert
},
184 { .name
= "perf-event-open", .ptr
= &debug_peo_args
},
188 int perf_debug_option(const char *str
)
190 struct debug_variable
*var
= &debug_variables
[0];
191 char *vstr
, *s
= strdup(str
);
194 vstr
= strchr(s
, '=');
199 if (!strcmp(s
, var
->name
))
205 pr_err("Unknown debug variable name '%s'\n", s
);
213 * Allow only values in range (0, 10),
216 v
= (v
< 0) || (v
> 10) ? 0 : v
;
227 int perf_quiet_option(void)
229 struct debug_variable
*var
= &debug_variables
[0];
231 /* disable all debug messages */
240 #define DEBUG_WRAPPER(__n, __l) \
241 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
246 va_start(args, fmt); \
247 ret = veprintf(__l, verbose, fmt, args); \
252 DEBUG_WRAPPER(warning
, 0);
253 DEBUG_WRAPPER(debug
, 1);
255 void perf_debug_setup(void)
257 libapi_set_print(pr_warning_wrapper
, pr_warning_wrapper
, pr_debug_wrapper
);
260 /* Obtain a backtrace and print it to stdout. */
261 #ifdef HAVE_BACKTRACE_SUPPORT
262 void dump_stack(void)
265 size_t size
= backtrace(array
, ARRAY_SIZE(array
));
266 char **strings
= backtrace_symbols(array
, size
);
269 printf("Obtained %zd stack frames.\n", size
);
271 for (i
= 0; i
< size
; i
++)
272 printf("%s\n", strings
[i
]);
277 void dump_stack(void) {}
280 void sighandler_dump_stack(int sig
)
282 psignal(sig
, "perf");
284 signal(sig
, SIG_DFL
);