xref: /linux/tools/perf/util/debug.c (revision 95e9fd10f06cb5642028b6b851e32b8c8afb4571)
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 
16 int verbose;
17 bool dump_trace = false, quiet = false;
18 
19 int 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 
36 int 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 #if defined(NO_NEWT_SUPPORT) && defined(NO_GTK2_SUPPORT)
51 int 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 
62 int 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 
72 void 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 }
107