xref: /openbmc/linux/tools/perf/util/debug.c (revision b4e18b29)
1 // SPDX-License-Identifier: GPL-2.0
2 /* For general debugging purposes */
3 
4 #include <inttypes.h>
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/wait.h>
10 #include <api/debug.h>
11 #include <linux/kernel.h>
12 #include <linux/time64.h>
13 #ifdef HAVE_BACKTRACE_SUPPORT
14 #include <execinfo.h>
15 #endif
16 #include "color.h"
17 #include "event.h"
18 #include "debug.h"
19 #include "print_binary.h"
20 #include "target.h"
21 #include "ui/helpline.h"
22 #include "ui/ui.h"
23 #include "util/parse-sublevel-options.h"
24 
25 #include <linux/ctype.h>
26 
27 int verbose;
28 int debug_peo_args;
29 bool dump_trace = false, quiet = false;
30 int debug_ordered_events;
31 static int redirect_to_stderr;
32 int debug_data_convert;
33 static FILE *debug_file;
34 
35 void debug_set_file(FILE *file)
36 {
37 	debug_file = file;
38 }
39 
40 int veprintf(int level, int var, const char *fmt, va_list args)
41 {
42 	int ret = 0;
43 
44 	if (var >= level) {
45 		if (use_browser >= 1 && !redirect_to_stderr)
46 			ui_helpline__vshow(fmt, args);
47 		else
48 			ret = vfprintf(debug_file, fmt, args);
49 	}
50 
51 	return ret;
52 }
53 
54 int eprintf(int level, int var, const char *fmt, ...)
55 {
56 	va_list args;
57 	int ret;
58 
59 	va_start(args, fmt);
60 	ret = veprintf(level, var, fmt, args);
61 	va_end(args);
62 
63 	return ret;
64 }
65 
66 static int veprintf_time(u64 t, const char *fmt, va_list args)
67 {
68 	int ret = 0;
69 	u64 secs, usecs, nsecs = t;
70 
71 	secs   = nsecs / NSEC_PER_SEC;
72 	nsecs -= secs  * NSEC_PER_SEC;
73 	usecs  = nsecs / NSEC_PER_USEC;
74 
75 	ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
76 		      secs, usecs);
77 	ret += vfprintf(stderr, fmt, args);
78 	return ret;
79 }
80 
81 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
82 {
83 	int ret = 0;
84 	va_list args;
85 
86 	if (var >= level) {
87 		va_start(args, fmt);
88 		ret = veprintf_time(t, fmt, args);
89 		va_end(args);
90 	}
91 
92 	return ret;
93 }
94 
95 /*
96  * Overloading libtraceevent standard info print
97  * function, display with -v in perf.
98  */
99 void pr_stat(const char *fmt, ...)
100 {
101 	va_list args;
102 
103 	va_start(args, fmt);
104 	veprintf(1, verbose, fmt, args);
105 	va_end(args);
106 	eprintf(1, verbose, "\n");
107 }
108 
109 int dump_printf(const char *fmt, ...)
110 {
111 	va_list args;
112 	int ret = 0;
113 
114 	if (dump_trace) {
115 		va_start(args, fmt);
116 		ret = vprintf(fmt, args);
117 		va_end(args);
118 	}
119 
120 	return ret;
121 }
122 
123 static int trace_event_printer(enum binary_printer_ops op,
124 			       unsigned int val, void *extra, FILE *fp)
125 {
126 	const char *color = PERF_COLOR_BLUE;
127 	union perf_event *event = (union perf_event *)extra;
128 	unsigned char ch = (unsigned char)val;
129 	int printed = 0;
130 
131 	switch (op) {
132 	case BINARY_PRINT_DATA_BEGIN:
133 		printed += fprintf(fp, ".");
134 		printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
135 					 event->header.size);
136 		break;
137 	case BINARY_PRINT_LINE_BEGIN:
138 		printed += fprintf(fp, ".");
139 		break;
140 	case BINARY_PRINT_ADDR:
141 		printed += color_fprintf(fp, color, "  %04x: ", val);
142 		break;
143 	case BINARY_PRINT_NUM_DATA:
144 		printed += color_fprintf(fp, color, " %02x", val);
145 		break;
146 	case BINARY_PRINT_NUM_PAD:
147 		printed += color_fprintf(fp, color, "   ");
148 		break;
149 	case BINARY_PRINT_SEP:
150 		printed += color_fprintf(fp, color, "  ");
151 		break;
152 	case BINARY_PRINT_CHAR_DATA:
153 		printed += color_fprintf(fp, color, "%c",
154 			      isprint(ch) ? ch : '.');
155 		break;
156 	case BINARY_PRINT_CHAR_PAD:
157 		printed += color_fprintf(fp, color, " ");
158 		break;
159 	case BINARY_PRINT_LINE_END:
160 		printed += color_fprintf(fp, color, "\n");
161 		break;
162 	case BINARY_PRINT_DATA_END:
163 		printed += fprintf(fp, "\n");
164 		break;
165 	default:
166 		break;
167 	}
168 
169 	return printed;
170 }
171 
172 void trace_event(union perf_event *event)
173 {
174 	unsigned char *raw_event = (void *)event;
175 
176 	if (!dump_trace)
177 		return;
178 
179 	print_binary(raw_event, event->header.size, 16,
180 		     trace_event_printer, event);
181 }
182 
183 static struct sublevel_option debug_opts[] = {
184 	{ .name = "verbose",		.value_ptr = &verbose },
185 	{ .name = "ordered-events",	.value_ptr = &debug_ordered_events},
186 	{ .name = "stderr",		.value_ptr = &redirect_to_stderr},
187 	{ .name = "data-convert",	.value_ptr = &debug_data_convert },
188 	{ .name = "perf-event-open",	.value_ptr = &debug_peo_args },
189 	{ .name = NULL, }
190 };
191 
192 int perf_debug_option(const char *str)
193 {
194 	int ret;
195 
196 	ret = perf_parse_sublevel_options(str, debug_opts);
197 	if (ret)
198 		return ret;
199 
200 	/* Allow only verbose value in range (0, 10), otherwise set 0. */
201 	verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
202 
203 	return 0;
204 }
205 
206 int perf_quiet_option(void)
207 {
208 	struct sublevel_option *opt = &debug_opts[0];
209 
210 	/* disable all debug messages */
211 	while (opt->name) {
212 		*opt->value_ptr = -1;
213 		opt++;
214 	}
215 
216 	return 0;
217 }
218 
219 #define DEBUG_WRAPPER(__n, __l)				\
220 static int pr_ ## __n ## _wrapper(const char *fmt, ...)	\
221 {							\
222 	va_list args;					\
223 	int ret;					\
224 							\
225 	va_start(args, fmt);				\
226 	ret = veprintf(__l, verbose, fmt, args);	\
227 	va_end(args);					\
228 	return ret;					\
229 }
230 
231 DEBUG_WRAPPER(warning, 0);
232 DEBUG_WRAPPER(debug, 1);
233 
234 void perf_debug_setup(void)
235 {
236 	debug_set_file(stderr);
237 	libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
238 }
239 
240 /* Obtain a backtrace and print it to stdout. */
241 #ifdef HAVE_BACKTRACE_SUPPORT
242 void dump_stack(void)
243 {
244 	void *array[16];
245 	size_t size = backtrace(array, ARRAY_SIZE(array));
246 	char **strings = backtrace_symbols(array, size);
247 	size_t i;
248 
249 	printf("Obtained %zd stack frames.\n", size);
250 
251 	for (i = 0; i < size; i++)
252 		printf("%s\n", strings[i]);
253 
254 	free(strings);
255 }
256 #else
257 void dump_stack(void) {}
258 #endif
259 
260 void sighandler_dump_stack(int sig)
261 {
262 	psignal(sig, "perf");
263 	dump_stack();
264 	signal(sig, SIG_DFL);
265 	raise(sig);
266 }
267