Loading...
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
15int verbose;
16bool dump_trace = false, quiet = false;
17
18int eprintf(int level, const char *fmt, ...)
19{
20 va_list args;
21 int ret = 0;
22
23 if (verbose >= level) {
24 va_start(args, fmt);
25 if (use_browser > 0)
26 ret = ui_helpline__show_help(fmt, args);
27 else
28 ret = vfprintf(stderr, fmt, args);
29 va_end(args);
30 }
31
32 return ret;
33}
34
35int dump_printf(const char *fmt, ...)
36{
37 va_list args;
38 int ret = 0;
39
40 if (dump_trace) {
41 va_start(args, fmt);
42 ret = vprintf(fmt, args);
43 va_end(args);
44 }
45
46 return ret;
47}
48
49#ifdef NO_NEWT_SUPPORT
50void ui__warning(const char *format, ...)
51{
52 va_list args;
53
54 va_start(args, format);
55 vfprintf(stderr, format, args);
56 va_end(args);
57}
58#endif
59
60void ui__warning_paranoid(void)
61{
62 ui__warning("Permission error - are you root?\n"
63 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
64 " -1 - Not paranoid at all\n"
65 " 0 - Disallow raw tracepoint access for unpriv\n"
66 " 1 - Disallow cpu events for unpriv\n"
67 " 2 - Disallow kernel profiling for unpriv\n");
68}
69
70void trace_event(union perf_event *event)
71{
72 unsigned char *raw_event = (void *)event;
73 const char *color = PERF_COLOR_BLUE;
74 int i, j;
75
76 if (!dump_trace)
77 return;
78
79 printf(".");
80 color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n",
81 event->header.size);
82
83 for (i = 0; i < event->header.size; i++) {
84 if ((i & 15) == 0) {
85 printf(".");
86 color_fprintf(stdout, color, " %04x: ", i);
87 }
88
89 color_fprintf(stdout, color, " %02x", raw_event[i]);
90
91 if (((i & 15) == 15) || i == event->header.size-1) {
92 color_fprintf(stdout, color, " ");
93 for (j = 0; j < 15-(i & 15); j++)
94 color_fprintf(stdout, color, " ");
95 for (j = i & ~15; j <= i; j++) {
96 color_fprintf(stdout, color, "%c",
97 isprint(raw_event[j]) ?
98 raw_event[j] : '.');
99 }
100 color_fprintf(stdout, color, "\n");
101 }
102 }
103 printf(".\n");
104}
1/* For general debugging purposes */
2
3#include "../perf.h"
4
5#include <string.h>
6#include <stdarg.h>
7#include <stdio.h>
8#include <api/debug.h>
9#include <linux/time64.h>
10
11#include "cache.h"
12#include "color.h"
13#include "event.h"
14#include "debug.h"
15#include "util.h"
16#include "target.h"
17
18int verbose;
19bool dump_trace = false, quiet = false;
20int debug_ordered_events;
21static int redirect_to_stderr;
22int debug_data_convert;
23
24int veprintf(int level, int var, const char *fmt, va_list args)
25{
26 int ret = 0;
27
28 if (var >= level) {
29 if (use_browser >= 1 && !redirect_to_stderr)
30 ui_helpline__vshow(fmt, args);
31 else
32 ret = vfprintf(stderr, fmt, args);
33 }
34
35 return ret;
36}
37
38int eprintf(int level, int var, const char *fmt, ...)
39{
40 va_list args;
41 int ret;
42
43 va_start(args, fmt);
44 ret = veprintf(level, var, fmt, args);
45 va_end(args);
46
47 return ret;
48}
49
50static int veprintf_time(u64 t, const char *fmt, va_list args)
51{
52 int ret = 0;
53 u64 secs, usecs, nsecs = t;
54
55 secs = nsecs / NSEC_PER_SEC;
56 nsecs -= secs * NSEC_PER_SEC;
57 usecs = nsecs / NSEC_PER_USEC;
58
59 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
60 secs, usecs);
61 ret += vfprintf(stderr, fmt, args);
62 return ret;
63}
64
65int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
66{
67 int ret = 0;
68 va_list args;
69
70 if (var >= level) {
71 va_start(args, fmt);
72 ret = veprintf_time(t, fmt, args);
73 va_end(args);
74 }
75
76 return ret;
77}
78
79/*
80 * Overloading libtraceevent standard info print
81 * function, display with -v in perf.
82 */
83void pr_stat(const char *fmt, ...)
84{
85 va_list args;
86
87 va_start(args, fmt);
88 veprintf(1, verbose, fmt, args);
89 va_end(args);
90 eprintf(1, verbose, "\n");
91}
92
93int dump_printf(const char *fmt, ...)
94{
95 va_list args;
96 int ret = 0;
97
98 if (dump_trace) {
99 va_start(args, fmt);
100 ret = vprintf(fmt, args);
101 va_end(args);
102 }
103
104 return ret;
105}
106
107static void trace_event_printer(enum binary_printer_ops op,
108 unsigned int val, void *extra)
109{
110 const char *color = PERF_COLOR_BLUE;
111 union perf_event *event = (union perf_event *)extra;
112 unsigned char ch = (unsigned char)val;
113
114 switch (op) {
115 case BINARY_PRINT_DATA_BEGIN:
116 printf(".");
117 color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n",
118 event->header.size);
119 break;
120 case BINARY_PRINT_LINE_BEGIN:
121 printf(".");
122 break;
123 case BINARY_PRINT_ADDR:
124 color_fprintf(stdout, color, " %04x: ", val);
125 break;
126 case BINARY_PRINT_NUM_DATA:
127 color_fprintf(stdout, color, " %02x", val);
128 break;
129 case BINARY_PRINT_NUM_PAD:
130 color_fprintf(stdout, color, " ");
131 break;
132 case BINARY_PRINT_SEP:
133 color_fprintf(stdout, color, " ");
134 break;
135 case BINARY_PRINT_CHAR_DATA:
136 color_fprintf(stdout, color, "%c",
137 isprint(ch) ? ch : '.');
138 break;
139 case BINARY_PRINT_CHAR_PAD:
140 color_fprintf(stdout, color, " ");
141 break;
142 case BINARY_PRINT_LINE_END:
143 color_fprintf(stdout, color, "\n");
144 break;
145 case BINARY_PRINT_DATA_END:
146 printf("\n");
147 break;
148 default:
149 break;
150 }
151}
152
153void trace_event(union perf_event *event)
154{
155 unsigned char *raw_event = (void *)event;
156
157 if (!dump_trace)
158 return;
159
160 print_binary(raw_event, event->header.size, 16,
161 trace_event_printer, event);
162}
163
164static struct debug_variable {
165 const char *name;
166 int *ptr;
167} debug_variables[] = {
168 { .name = "verbose", .ptr = &verbose },
169 { .name = "ordered-events", .ptr = &debug_ordered_events},
170 { .name = "stderr", .ptr = &redirect_to_stderr},
171 { .name = "data-convert", .ptr = &debug_data_convert },
172 { .name = NULL, }
173};
174
175int perf_debug_option(const char *str)
176{
177 struct debug_variable *var = &debug_variables[0];
178 char *vstr, *s = strdup(str);
179 int v = 1;
180
181 vstr = strchr(s, '=');
182 if (vstr)
183 *vstr++ = 0;
184
185 while (var->name) {
186 if (!strcmp(s, var->name))
187 break;
188 var++;
189 }
190
191 if (!var->name) {
192 pr_err("Unknown debug variable name '%s'\n", s);
193 free(s);
194 return -1;
195 }
196
197 if (vstr) {
198 v = atoi(vstr);
199 /*
200 * Allow only values in range (0, 10),
201 * otherwise set 0.
202 */
203 v = (v < 0) || (v > 10) ? 0 : v;
204 }
205
206 *var->ptr = v;
207 free(s);
208 return 0;
209}
210
211#define DEBUG_WRAPPER(__n, __l) \
212static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
213{ \
214 va_list args; \
215 int ret; \
216 \
217 va_start(args, fmt); \
218 ret = veprintf(__l, verbose, fmt, args); \
219 va_end(args); \
220 return ret; \
221}
222
223DEBUG_WRAPPER(warning, 0);
224DEBUG_WRAPPER(debug, 1);
225
226void perf_debug_setup(void)
227{
228 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
229}