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>
14 #ifdef HAVE_BACKTRACE_SUPPORT
20 #include "print_binary.h"
22 #include "ui/helpline.h"
24 #include "util/parse-sublevel-options.h"
26 #include <linux/ctype.h>
30 bool dump_trace = false, quiet = false;
31 int debug_ordered_events;
32 static int redirect_to_stderr;
33 int debug_data_convert;
34 static FILE *debug_file;
35 bool debug_display_time;
37 void debug_set_file(FILE *file)
42 void debug_set_display_time(bool set)
44 debug_display_time = set;
47 static int fprintf_time(FILE *file)
53 if (!debug_display_time)
56 if (gettimeofday(&tod, NULL) != 0)
59 if (localtime_r(&tod.tv_sec, <ime) == NULL)
62 strftime(date, sizeof(date), "%F %H:%M:%S", <ime);
63 return fprintf(file, "[%s.%06lu] ", date, (long)tod.tv_usec);
66 int veprintf(int level, int var, const char *fmt, va_list args)
71 if (use_browser >= 1 && !redirect_to_stderr) {
72 ui_helpline__vshow(fmt, args);
74 ret = fprintf_time(debug_file);
75 ret += vfprintf(debug_file, fmt, args);
82 int eprintf(int level, int var, const char *fmt, ...)
88 ret = veprintf(level, var, fmt, args);
94 static int veprintf_time(u64 t, const char *fmt, va_list args)
97 u64 secs, usecs, nsecs = t;
99 secs = nsecs / NSEC_PER_SEC;
100 nsecs -= secs * NSEC_PER_SEC;
101 usecs = nsecs / NSEC_PER_USEC;
103 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
105 ret += vfprintf(stderr, fmt, args);
109 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
116 ret = veprintf_time(t, fmt, args);
124 * Overloading libtraceevent standard info print
125 * function, display with -v in perf.
127 void pr_stat(const char *fmt, ...)
132 veprintf(1, verbose, fmt, args);
134 eprintf(1, verbose, "\n");
137 int dump_printf(const char *fmt, ...)
144 ret = vprintf(fmt, args);
151 static int trace_event_printer(enum binary_printer_ops op,
152 unsigned int val, void *extra, FILE *fp)
154 const char *color = PERF_COLOR_BLUE;
155 union perf_event *event = (union perf_event *)extra;
156 unsigned char ch = (unsigned char)val;
160 case BINARY_PRINT_DATA_BEGIN:
161 printed += fprintf(fp, ".");
162 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
165 case BINARY_PRINT_LINE_BEGIN:
166 printed += fprintf(fp, ".");
168 case BINARY_PRINT_ADDR:
169 printed += color_fprintf(fp, color, " %04x: ", val);
171 case BINARY_PRINT_NUM_DATA:
172 printed += color_fprintf(fp, color, " %02x", val);
174 case BINARY_PRINT_NUM_PAD:
175 printed += color_fprintf(fp, color, " ");
177 case BINARY_PRINT_SEP:
178 printed += color_fprintf(fp, color, " ");
180 case BINARY_PRINT_CHAR_DATA:
181 printed += color_fprintf(fp, color, "%c",
182 isprint(ch) ? ch : '.');
184 case BINARY_PRINT_CHAR_PAD:
185 printed += color_fprintf(fp, color, " ");
187 case BINARY_PRINT_LINE_END:
188 printed += color_fprintf(fp, color, "\n");
190 case BINARY_PRINT_DATA_END:
191 printed += fprintf(fp, "\n");
200 void trace_event(union perf_event *event)
202 unsigned char *raw_event = (void *)event;
207 print_binary(raw_event, event->header.size, 16,
208 trace_event_printer, event);
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 },
220 int perf_debug_option(const char *str)
224 ret = perf_parse_sublevel_options(str, debug_opts);
228 /* Allow only verbose value in range (0, 10), otherwise set 0. */
229 verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
234 int perf_quiet_option(void)
236 struct sublevel_option *opt = &debug_opts[0];
238 /* disable all debug messages */
240 *opt->value_ptr = -1;
247 #define DEBUG_WRAPPER(__n, __l) \
248 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
253 va_start(args, fmt); \
254 ret = veprintf(__l, verbose, fmt, args); \
259 DEBUG_WRAPPER(warning, 0);
260 DEBUG_WRAPPER(debug, 1);
262 void perf_debug_setup(void)
264 debug_set_file(stderr);
265 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
268 /* Obtain a backtrace and print it to stdout. */
269 #ifdef HAVE_BACKTRACE_SUPPORT
270 void dump_stack(void)
273 size_t size = backtrace(array, ARRAY_SIZE(array));
274 char **strings = backtrace_symbols(array, size);
277 printf("Obtained %zd stack frames.\n", size);
279 for (i = 0; i < size; i++)
280 printf("%s\n", strings[i]);
285 void dump_stack(void) {}
288 void sighandler_dump_stack(int sig)
290 psignal(sig, "perf");
292 signal(sig, SIG_DFL);