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 "trace-event.h"
23 #include "ui/helpline.h"
25 #include "util/parse-sublevel-options.h"
27 #include <linux/ctype.h>
29 #ifdef HAVE_LIBTRACEEVENT
30 #include <traceevent/event-parse.h>
32 #define LIBTRACEEVENT_VERSION 0
38 bool dump_trace = false, quiet = false;
39 int debug_ordered_events;
40 static int redirect_to_stderr;
41 int debug_data_convert;
42 static FILE *_debug_file;
43 bool debug_display_time;
45 FILE *debug_file(void)
48 pr_warning_once("debug_file not set");
49 debug_set_file(stderr);
54 void debug_set_file(FILE *file)
59 void debug_set_display_time(bool set)
61 debug_display_time = set;
64 static int fprintf_time(FILE *file)
70 if (!debug_display_time)
73 if (gettimeofday(&tod, NULL) != 0)
76 if (localtime_r(&tod.tv_sec, <ime) == NULL)
79 strftime(date, sizeof(date), "%F %H:%M:%S", <ime);
80 return fprintf(file, "[%s.%06lu] ", date, (long)tod.tv_usec);
83 int veprintf(int level, int var, const char *fmt, va_list args)
88 if (use_browser >= 1 && !redirect_to_stderr) {
89 ui_helpline__vshow(fmt, args);
91 ret = fprintf_time(debug_file());
92 ret += vfprintf(debug_file(), fmt, args);
99 int eprintf(int level, int var, const char *fmt, ...)
105 ret = veprintf(level, var, fmt, args);
111 static int veprintf_time(u64 t, const char *fmt, va_list args)
114 u64 secs, usecs, nsecs = t;
116 secs = nsecs / NSEC_PER_SEC;
117 nsecs -= secs * NSEC_PER_SEC;
118 usecs = nsecs / NSEC_PER_USEC;
120 ret = fprintf(debug_file(), "[%13" PRIu64 ".%06" PRIu64 "] ", secs, usecs);
121 ret += vfprintf(debug_file(), fmt, args);
125 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
132 ret = veprintf_time(t, fmt, args);
140 * Overloading libtraceevent standard info print
141 * function, display with -v in perf.
143 void pr_stat(const char *fmt, ...)
148 veprintf(1, verbose, fmt, args);
150 eprintf(1, verbose, "\n");
153 int dump_printf(const char *fmt, ...)
160 ret = vprintf(fmt, args);
167 static int trace_event_printer(enum binary_printer_ops op,
168 unsigned int val, void *extra, FILE *fp)
170 const char *color = PERF_COLOR_BLUE;
171 union perf_event *event = (union perf_event *)extra;
172 unsigned char ch = (unsigned char)val;
176 case BINARY_PRINT_DATA_BEGIN:
177 printed += fprintf(fp, ".");
178 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
181 case BINARY_PRINT_LINE_BEGIN:
182 printed += fprintf(fp, ".");
184 case BINARY_PRINT_ADDR:
185 printed += color_fprintf(fp, color, " %04x: ", val);
187 case BINARY_PRINT_NUM_DATA:
188 printed += color_fprintf(fp, color, " %02x", val);
190 case BINARY_PRINT_NUM_PAD:
191 printed += color_fprintf(fp, color, " ");
193 case BINARY_PRINT_SEP:
194 printed += color_fprintf(fp, color, " ");
196 case BINARY_PRINT_CHAR_DATA:
197 printed += color_fprintf(fp, color, "%c",
198 isprint(ch) && isascii(ch) ? ch : '.');
200 case BINARY_PRINT_CHAR_PAD:
201 printed += color_fprintf(fp, color, " ");
203 case BINARY_PRINT_LINE_END:
204 printed += color_fprintf(fp, color, "\n");
206 case BINARY_PRINT_DATA_END:
207 printed += fprintf(fp, "\n");
216 void trace_event(union perf_event *event)
218 unsigned char *raw_event = (void *)event;
223 print_binary(raw_event, event->header.size, 16,
224 trace_event_printer, event);
227 static struct sublevel_option debug_opts[] = {
228 { .name = "verbose", .value_ptr = &verbose },
229 { .name = "ordered-events", .value_ptr = &debug_ordered_events},
230 { .name = "stderr", .value_ptr = &redirect_to_stderr},
231 { .name = "data-convert", .value_ptr = &debug_data_convert },
232 { .name = "perf-event-open", .value_ptr = &debug_peo_args },
233 { .name = "kmaps", .value_ptr = &debug_kmaps },
237 int perf_debug_option(const char *str)
241 ret = perf_parse_sublevel_options(str, debug_opts);
245 /* Allow only verbose value in range (0, 10), otherwise set 0. */
246 verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
248 #if LIBTRACEEVENT_VERSION >= MAKE_LIBTRACEEVENT_VERSION(1, 3, 0)
250 tep_set_loglevel(TEP_LOG_INFO);
251 else if (verbose == 2)
252 tep_set_loglevel(TEP_LOG_DEBUG);
253 else if (verbose >= 3)
254 tep_set_loglevel(TEP_LOG_ALL);
259 int perf_quiet_option(void)
261 struct sublevel_option *opt = &debug_opts[0];
263 /* disable all debug messages */
265 *opt->value_ptr = -1;
269 /* For debug variables that are used as bool types, set to 0. */
270 redirect_to_stderr = 0;
277 #define DEBUG_WRAPPER(__n, __l) \
278 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
283 va_start(args, fmt); \
284 ret = veprintf(__l, verbose, fmt, args); \
289 DEBUG_WRAPPER(warning, 0);
290 DEBUG_WRAPPER(debug, 1);
292 void perf_debug_setup(void)
294 debug_set_file(stderr);
295 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
298 /* Obtain a backtrace and print it to stdout. */
299 #ifdef HAVE_BACKTRACE_SUPPORT
300 void dump_stack(void)
303 size_t size = backtrace(array, ARRAY_SIZE(array));
304 char **strings = backtrace_symbols(array, size);
307 printf("Obtained %zd stack frames.\n", size);
309 for (i = 0; i < size; i++)
310 printf("%s\n", strings[i]);
315 void dump_stack(void) {}
318 void sighandler_dump_stack(int sig)
320 psignal(sig, "perf");
322 signal(sig, SIG_DFL);