blob: 3cc578343f48894ef0da8c7f640796ff622a6c51 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002/* For general debugging purposes */
3
4#include "../perf.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +02005
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03006#include <inttypes.h>
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02007#include <string.h>
8#include <stdarg.h>
9#include <stdio.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030010#include <sys/wait.h>
Jiri Olsadd629cc2016-02-14 17:03:45 +010011#include <api/debug.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030012#include <linux/time64.h>
Arnaldo Carvalho de Melo8c2b7ca2017-04-19 16:21:59 -030013#ifdef HAVE_BACKTRACE_SUPPORT
14#include <execinfo.h>
15#endif
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030016#include "cache.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020017#include "color.h"
18#include "event.h"
19#include "debug.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030020#include "print_binary.h"
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020021#include "util.h"
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090022#include "target.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020023
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030024#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030025
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -020026int verbose;
27bool dump_trace = false, quiet = false;
Jiri Olsacee3ab92014-07-11 14:49:54 +020028int debug_ordered_events;
Andi Kleenf78eaef2014-11-21 13:38:00 -080029static int redirect_to_stderr;
Jiri Olsaedbe9812015-02-20 23:17:00 +010030int debug_data_convert;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020031
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030032int veprintf(int level, int var, const char *fmt, va_list args)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020033{
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020034 int ret = 0;
35
Jiri Olsac95688a2014-07-14 23:46:49 +020036 if (var >= level) {
Andi Kleenf78eaef2014-11-21 13:38:00 -080037 if (use_browser >= 1 && !redirect_to_stderr)
Namhyung Kimb56e5332012-11-15 01:47:41 +090038 ui_helpline__vshow(fmt, args);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030039 else
40 ret = vfprintf(stderr, fmt, args);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020041 }
42
43 return ret;
44}
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +020045
Jiri Olsac95688a2014-07-14 23:46:49 +020046int eprintf(int level, int var, const char *fmt, ...)
Jiri Olsaf772abc2013-12-03 14:09:25 +010047{
48 va_list args;
49 int ret;
50
51 va_start(args, fmt);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030052 ret = veprintf(level, var, fmt, args);
Jiri Olsaf772abc2013-12-03 14:09:25 +010053 va_end(args);
54
55 return ret;
56}
57
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030058static int veprintf_time(u64 t, const char *fmt, va_list args)
Jiri Olsacee3ab92014-07-11 14:49:54 +020059{
60 int ret = 0;
61 u64 secs, usecs, nsecs = t;
62
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030063 secs = nsecs / NSEC_PER_SEC;
64 nsecs -= secs * NSEC_PER_SEC;
65 usecs = nsecs / NSEC_PER_USEC;
Jiri Olsacee3ab92014-07-11 14:49:54 +020066
67 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
68 secs, usecs);
69 ret += vfprintf(stderr, fmt, args);
70 return ret;
71}
72
73int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
74{
75 int ret = 0;
76 va_list args;
77
78 if (var >= level) {
79 va_start(args, fmt);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030080 ret = veprintf_time(t, fmt, args);
Jiri Olsacee3ab92014-07-11 14:49:54 +020081 va_end(args);
82 }
83
84 return ret;
85}
86
Jiri Olsaf772abc2013-12-03 14:09:25 +010087/*
88 * Overloading libtraceevent standard info print
89 * function, display with -v in perf.
90 */
91void pr_stat(const char *fmt, ...)
92{
93 va_list args;
94
95 va_start(args, fmt);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030096 veprintf(1, verbose, fmt, args);
Jiri Olsaf772abc2013-12-03 14:09:25 +010097 va_end(args);
Jiri Olsac95688a2014-07-14 23:46:49 +020098 eprintf(1, verbose, "\n");
Jiri Olsaf772abc2013-12-03 14:09:25 +010099}
100
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200101int dump_printf(const char *fmt, ...)
102{
103 va_list args;
104 int ret = 0;
105
106 if (dump_trace) {
107 va_start(args, fmt);
108 ret = vprintf(fmt, args);
109 va_end(args);
110 }
111
112 return ret;
113}
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200114
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300115static int trace_event_printer(enum binary_printer_ops op,
116 unsigned int val, void *extra, FILE *fp)
Wang Nanc339b1a2016-02-24 11:20:44 +0000117{
118 const char *color = PERF_COLOR_BLUE;
119 union perf_event *event = (union perf_event *)extra;
120 unsigned char ch = (unsigned char)val;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300121 int printed = 0;
Wang Nanc339b1a2016-02-24 11:20:44 +0000122
123 switch (op) {
124 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300125 printed += fprintf(fp, ".");
126 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
127 event->header.size);
Wang Nanc339b1a2016-02-24 11:20:44 +0000128 break;
129 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300130 printed += fprintf(fp, ".");
Wang Nanc339b1a2016-02-24 11:20:44 +0000131 break;
132 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300133 printed += color_fprintf(fp, color, " %04x: ", val);
Wang Nanc339b1a2016-02-24 11:20:44 +0000134 break;
135 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300136 printed += color_fprintf(fp, color, " %02x", val);
Wang Nanc339b1a2016-02-24 11:20:44 +0000137 break;
138 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300139 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000140 break;
141 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300142 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000143 break;
144 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300145 printed += color_fprintf(fp, color, "%c",
Wang Nanc339b1a2016-02-24 11:20:44 +0000146 isprint(ch) ? ch : '.');
147 break;
148 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300149 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000150 break;
151 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300152 printed += color_fprintf(fp, color, "\n");
Wang Nanc339b1a2016-02-24 11:20:44 +0000153 break;
154 case BINARY_PRINT_DATA_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300155 printed += fprintf(fp, "\n");
Wang Nanc339b1a2016-02-24 11:20:44 +0000156 break;
157 default:
158 break;
159 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300160
161 return printed;
Wang Nanc339b1a2016-02-24 11:20:44 +0000162}
163
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200164void trace_event(union perf_event *event)
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200165{
166 unsigned char *raw_event = (void *)event;
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200167
168 if (!dump_trace)
169 return;
170
Wang Nanc339b1a2016-02-24 11:20:44 +0000171 print_binary(raw_event, event->header.size, 16,
172 trace_event_printer, event);
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200173}
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200174
175static struct debug_variable {
176 const char *name;
177 int *ptr;
178} debug_variables[] = {
Jiri Olsacee3ab92014-07-11 14:49:54 +0200179 { .name = "verbose", .ptr = &verbose },
180 { .name = "ordered-events", .ptr = &debug_ordered_events},
Andi Kleenf78eaef2014-11-21 13:38:00 -0800181 { .name = "stderr", .ptr = &redirect_to_stderr},
Jiri Olsaedbe9812015-02-20 23:17:00 +0100182 { .name = "data-convert", .ptr = &debug_data_convert },
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200183 { .name = NULL, }
184};
185
186int perf_debug_option(const char *str)
187{
188 struct debug_variable *var = &debug_variables[0];
189 char *vstr, *s = strdup(str);
190 int v = 1;
191
192 vstr = strchr(s, '=');
193 if (vstr)
194 *vstr++ = 0;
195
196 while (var->name) {
197 if (!strcmp(s, var->name))
198 break;
199 var++;
200 }
201
202 if (!var->name) {
203 pr_err("Unknown debug variable name '%s'\n", s);
204 free(s);
205 return -1;
206 }
207
208 if (vstr) {
209 v = atoi(vstr);
210 /*
211 * Allow only values in range (0, 10),
212 * otherwise set 0.
213 */
214 v = (v < 0) || (v > 10) ? 0 : v;
215 }
216
Namhyung Kim80df1982017-02-17 17:17:37 +0900217 if (quiet)
218 v = -1;
219
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200220 *var->ptr = v;
221 free(s);
222 return 0;
223}
Jiri Olsadd629cc2016-02-14 17:03:45 +0100224
Namhyung Kim80df1982017-02-17 17:17:37 +0900225int perf_quiet_option(void)
226{
227 struct debug_variable *var = &debug_variables[0];
228
229 /* disable all debug messages */
230 while (var->name) {
231 *var->ptr = -1;
232 var++;
233 }
234
Namhyung Kim80df1982017-02-17 17:17:37 +0900235 return 0;
236}
237
Jiri Olsadd629cc2016-02-14 17:03:45 +0100238#define DEBUG_WRAPPER(__n, __l) \
239static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
240{ \
241 va_list args; \
242 int ret; \
243 \
244 va_start(args, fmt); \
245 ret = veprintf(__l, verbose, fmt, args); \
246 va_end(args); \
247 return ret; \
248}
249
250DEBUG_WRAPPER(warning, 0);
251DEBUG_WRAPPER(debug, 1);
252
253void perf_debug_setup(void)
254{
255 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
256}
Arnaldo Carvalho de Melo8c2b7ca2017-04-19 16:21:59 -0300257
258/* Obtain a backtrace and print it to stdout. */
259#ifdef HAVE_BACKTRACE_SUPPORT
260void dump_stack(void)
261{
262 void *array[16];
263 size_t size = backtrace(array, ARRAY_SIZE(array));
264 char **strings = backtrace_symbols(array, size);
265 size_t i;
266
267 printf("Obtained %zd stack frames.\n", size);
268
269 for (i = 0; i < size; i++)
270 printf("%s\n", strings[i]);
271
272 free(strings);
273}
274#else
275void dump_stack(void) {}
276#endif
277
278void sighandler_dump_stack(int sig)
279{
280 psignal(sig, "perf");
281 dump_stack();
282 signal(sig, SIG_DFL);
283 raise(sig);
284}