Linux Audio

Check our new training course

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