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