Merge tag 'pwm/for-3.17-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / debug.c
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 static int _eprintf(int level, int var, const char *fmt, va_list args)
20 {
21         int ret = 0;
22
23         if (var >= level) {
24                 if (use_browser >= 1)
25                         ui_helpline__vshow(fmt, args);
26                 else
27                         ret = vfprintf(stderr, fmt, args);
28         }
29
30         return ret;
31 }
32
33 int eprintf(int level, int var, const char *fmt, ...)
34 {
35         va_list args;
36         int ret;
37
38         va_start(args, fmt);
39         ret = _eprintf(level, var, fmt, args);
40         va_end(args);
41
42         return ret;
43 }
44
45 /*
46  * Overloading libtraceevent standard info print
47  * function, display with -v in perf.
48  */
49 void pr_stat(const char *fmt, ...)
50 {
51         va_list args;
52
53         va_start(args, fmt);
54         _eprintf(1, verbose, fmt, args);
55         va_end(args);
56         eprintf(1, verbose, "\n");
57 }
58
59 int dump_printf(const char *fmt, ...)
60 {
61         va_list args;
62         int ret = 0;
63
64         if (dump_trace) {
65                 va_start(args, fmt);
66                 ret = vprintf(fmt, args);
67                 va_end(args);
68         }
69
70         return ret;
71 }
72
73 void trace_event(union perf_event *event)
74 {
75         unsigned char *raw_event = (void *)event;
76         const char *color = PERF_COLOR_BLUE;
77         int i, j;
78
79         if (!dump_trace)
80                 return;
81
82         printf(".");
83         color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n",
84                       event->header.size);
85
86         for (i = 0; i < event->header.size; i++) {
87                 if ((i & 15) == 0) {
88                         printf(".");
89                         color_fprintf(stdout, color, "  %04x: ", i);
90                 }
91
92                 color_fprintf(stdout, color, " %02x", raw_event[i]);
93
94                 if (((i & 15) == 15) || i == event->header.size-1) {
95                         color_fprintf(stdout, color, "  ");
96                         for (j = 0; j < 15-(i & 15); j++)
97                                 color_fprintf(stdout, color, "   ");
98                         for (j = i & ~15; j <= i; j++) {
99                                 color_fprintf(stdout, color, "%c",
100                                               isprint(raw_event[j]) ?
101                                               raw_event[j] : '.');
102                         }
103                         color_fprintf(stdout, color, "\n");
104                 }
105         }
106         printf(".\n");
107 }
108
109 static struct debug_variable {
110         const char *name;
111         int *ptr;
112 } debug_variables[] = {
113         { .name = "verbose", .ptr = &verbose },
114         { .name = NULL, }
115 };
116
117 int perf_debug_option(const char *str)
118 {
119         struct debug_variable *var = &debug_variables[0];
120         char *vstr, *s = strdup(str);
121         int v = 1;
122
123         vstr = strchr(s, '=');
124         if (vstr)
125                 *vstr++ = 0;
126
127         while (var->name) {
128                 if (!strcmp(s, var->name))
129                         break;
130                 var++;
131         }
132
133         if (!var->name) {
134                 pr_err("Unknown debug variable name '%s'\n", s);
135                 free(s);
136                 return -1;
137         }
138
139         if (vstr) {
140                 v = atoi(vstr);
141                 /*
142                  * Allow only values in range (0, 10),
143                  * otherwise set 0.
144                  */
145                 v = (v < 0) || (v > 10) ? 0 : v;
146         }
147
148         *var->ptr = v;
149         free(s);
150         return 0;
151 }