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
37 bool dump_trace = false, quiet = false;
38 int debug_ordered_events;
39 static int redirect_to_stderr;
40 int debug_data_convert;
41 static FILE *debug_file;
42 bool debug_display_time;
44 void debug_set_file(FILE *file)
49 void debug_set_display_time(bool set)
51 debug_display_time = set;
54 static int fprintf_time(FILE *file)
60 if (!debug_display_time)
63 if (gettimeofday(&tod, NULL) != 0)
66 if (localtime_r(&tod.tv_sec, <ime) == NULL)
69 strftime(date, sizeof(date), "%F %H:%M:%S", <ime);
70 return fprintf(file, "[%s.%06lu] ", date, (long)tod.tv_usec);
73 int veprintf(int level, int var, const char *fmt, va_list args)
78 if (use_browser >= 1 && !redirect_to_stderr) {
79 ui_helpline__vshow(fmt, args);
81 ret = fprintf_time(debug_file);
82 ret += vfprintf(debug_file, fmt, args);
89 int eprintf(int level, int var, const char *fmt, ...)
95 ret = veprintf(level, var, fmt, args);
101 static int veprintf_time(u64 t, const char *fmt, va_list args)
104 u64 secs, usecs, nsecs = t;
106 secs = nsecs / NSEC_PER_SEC;
107 nsecs -= secs * NSEC_PER_SEC;
108 usecs = nsecs / NSEC_PER_USEC;
110 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
112 ret += vfprintf(stderr, fmt, args);
116 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
123 ret = veprintf_time(t, fmt, args);
131 * Overloading libtraceevent standard info print
132 * function, display with -v in perf.
134 void pr_stat(const char *fmt, ...)
139 veprintf(1, verbose, fmt, args);
141 eprintf(1, verbose, "\n");
144 int dump_printf(const char *fmt, ...)
151 ret = vprintf(fmt, args);
158 static int trace_event_printer(enum binary_printer_ops op,
159 unsigned int val, void *extra, FILE *fp)
161 const char *color = PERF_COLOR_BLUE;
162 union perf_event *event = (union perf_event *)extra;
163 unsigned char ch = (unsigned char)val;
167 case BINARY_PRINT_DATA_BEGIN:
168 printed += fprintf(fp, ".");
169 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
172 case BINARY_PRINT_LINE_BEGIN:
173 printed += fprintf(fp, ".");
175 case BINARY_PRINT_ADDR:
176 printed += color_fprintf(fp, color, " %04x: ", val);
178 case BINARY_PRINT_NUM_DATA:
179 printed += color_fprintf(fp, color, " %02x", val);
181 case BINARY_PRINT_NUM_PAD:
182 printed += color_fprintf(fp, color, " ");
184 case BINARY_PRINT_SEP:
185 printed += color_fprintf(fp, color, " ");
187 case BINARY_PRINT_CHAR_DATA:
188 printed += color_fprintf(fp, color, "%c",
189 isprint(ch) && isascii(ch) ? ch : '.');
191 case BINARY_PRINT_CHAR_PAD:
192 printed += color_fprintf(fp, color, " ");
194 case BINARY_PRINT_LINE_END:
195 printed += color_fprintf(fp, color, "\n");
197 case BINARY_PRINT_DATA_END:
198 printed += fprintf(fp, "\n");
207 void trace_event(union perf_event *event)
209 unsigned char *raw_event = (void *)event;
214 print_binary(raw_event, event->header.size, 16,
215 trace_event_printer, event);
218 static struct sublevel_option debug_opts[] = {
219 { .name = "verbose", .value_ptr = &verbose },
220 { .name = "ordered-events", .value_ptr = &debug_ordered_events},
221 { .name = "stderr", .value_ptr = &redirect_to_stderr},
222 { .name = "data-convert", .value_ptr = &debug_data_convert },
223 { .name = "perf-event-open", .value_ptr = &debug_peo_args },
227 int perf_debug_option(const char *str)
231 ret = perf_parse_sublevel_options(str, debug_opts);
235 /* Allow only verbose value in range (0, 10), otherwise set 0. */
236 verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
238 #if LIBTRACEEVENT_VERSION >= MAKE_LIBTRACEEVENT_VERSION(1, 3, 0)
240 tep_set_loglevel(TEP_LOG_INFO);
241 else if (verbose == 2)
242 tep_set_loglevel(TEP_LOG_DEBUG);
243 else if (verbose >= 3)
244 tep_set_loglevel(TEP_LOG_ALL);
249 int perf_quiet_option(void)
251 struct sublevel_option *opt = &debug_opts[0];
253 /* disable all debug messages */
255 *opt->value_ptr = -1;
259 /* For debug variables that are used as bool types, set to 0. */
260 redirect_to_stderr = 0;
266 #define DEBUG_WRAPPER(__n, __l) \
267 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
272 va_start(args, fmt); \
273 ret = veprintf(__l, verbose, fmt, args); \
278 DEBUG_WRAPPER(warning, 0);
279 DEBUG_WRAPPER(debug, 1);
281 void perf_debug_setup(void)
283 debug_set_file(stderr);
284 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
287 /* Obtain a backtrace and print it to stdout. */
288 #ifdef HAVE_BACKTRACE_SUPPORT
289 void dump_stack(void)
292 size_t size = backtrace(array, ARRAY_SIZE(array));
293 char **strings = backtrace_symbols(array, size);
296 printf("Obtained %zd stack frames.\n", size);
298 for (i = 0; i < size; i++)
299 printf("%s\n", strings[i]);
304 void dump_stack(void) {}
307 void sighandler_dump_stack(int sig)
309 psignal(sig, "perf");
311 signal(sig, SIG_DFL);