blob: adb656745ecc9085d9b571aecc263528970be8e8 [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
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03004#include <inttypes.h>
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02005#include <string.h>
6#include <stdarg.h>
7#include <stdio.h>
Arnaldo Carvalho de Melo215a0d32019-07-04 11:21:24 -03008#include <stdlib.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -03009#include <sys/wait.h>
Jiri Olsadd629cc2016-02-14 17:03:45 +010010#include <api/debug.h>
Arnaldo Carvalho de Melo4cb3c6d2019-08-29 17:19:02 -030011#include <linux/kernel.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
Frederic Weisbecker8f288272009-08-16 22:05:48 +020016#include "color.h"
17#include "event.h"
18#include "debug.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030019#include "print_binary.h"
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090020#include "target.h"
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030021#include "ui/helpline.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030022#include "ui/ui.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;
Ravi Bangoriaccd26742019-11-08 15:11:28 +053027int debug_peo_args;
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -020028bool dump_trace = false, quiet = false;
Jiri Olsacee3ab92014-07-11 14:49:54 +020029int debug_ordered_events;
Andi Kleenf78eaef2014-11-21 13:38:00 -080030static int redirect_to_stderr;
Jiri Olsaedbe9812015-02-20 23:17:00 +010031int debug_data_convert;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020032
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030033int veprintf(int level, int var, const char *fmt, va_list args)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020034{
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020035 int ret = 0;
36
Jiri Olsac95688a2014-07-14 23:46:49 +020037 if (var >= level) {
Andi Kleenf78eaef2014-11-21 13:38:00 -080038 if (use_browser >= 1 && !redirect_to_stderr)
Namhyung Kimb56e5332012-11-15 01:47:41 +090039 ui_helpline__vshow(fmt, args);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030040 else
41 ret = vfprintf(stderr, fmt, args);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020042 }
43
44 return ret;
45}
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +020046
Jiri Olsac95688a2014-07-14 23:46:49 +020047int eprintf(int level, int var, const char *fmt, ...)
Jiri Olsaf772abc2013-12-03 14:09:25 +010048{
49 va_list args;
50 int ret;
51
52 va_start(args, fmt);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030053 ret = veprintf(level, var, fmt, args);
Jiri Olsaf772abc2013-12-03 14:09:25 +010054 va_end(args);
55
56 return ret;
57}
58
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030059static int veprintf_time(u64 t, const char *fmt, va_list args)
Jiri Olsacee3ab92014-07-11 14:49:54 +020060{
61 int ret = 0;
62 u64 secs, usecs, nsecs = t;
63
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030064 secs = nsecs / NSEC_PER_SEC;
65 nsecs -= secs * NSEC_PER_SEC;
66 usecs = nsecs / NSEC_PER_USEC;
Jiri Olsacee3ab92014-07-11 14:49:54 +020067
68 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
69 secs, usecs);
70 ret += vfprintf(stderr, fmt, args);
71 return ret;
72}
73
74int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
75{
76 int ret = 0;
77 va_list args;
78
79 if (var >= level) {
80 va_start(args, fmt);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030081 ret = veprintf_time(t, fmt, args);
Jiri Olsacee3ab92014-07-11 14:49:54 +020082 va_end(args);
83 }
84
85 return ret;
86}
87
Jiri Olsaf772abc2013-12-03 14:09:25 +010088/*
89 * Overloading libtraceevent standard info print
90 * function, display with -v in perf.
91 */
92void pr_stat(const char *fmt, ...)
93{
94 va_list args;
95
96 va_start(args, fmt);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030097 veprintf(1, verbose, fmt, args);
Jiri Olsaf772abc2013-12-03 14:09:25 +010098 va_end(args);
Jiri Olsac95688a2014-07-14 23:46:49 +020099 eprintf(1, verbose, "\n");
Jiri Olsaf772abc2013-12-03 14:09:25 +0100100}
101
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200102int dump_printf(const char *fmt, ...)
103{
104 va_list args;
105 int ret = 0;
106
107 if (dump_trace) {
108 va_start(args, fmt);
109 ret = vprintf(fmt, args);
110 va_end(args);
111 }
112
113 return ret;
114}
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200115
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300116static int trace_event_printer(enum binary_printer_ops op,
117 unsigned int val, void *extra, FILE *fp)
Wang Nanc339b1a2016-02-24 11:20:44 +0000118{
119 const char *color = PERF_COLOR_BLUE;
120 union perf_event *event = (union perf_event *)extra;
121 unsigned char ch = (unsigned char)val;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300122 int printed = 0;
Wang Nanc339b1a2016-02-24 11:20:44 +0000123
124 switch (op) {
125 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300126 printed += fprintf(fp, ".");
127 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
128 event->header.size);
Wang Nanc339b1a2016-02-24 11:20:44 +0000129 break;
130 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300131 printed += fprintf(fp, ".");
Wang Nanc339b1a2016-02-24 11:20:44 +0000132 break;
133 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300134 printed += color_fprintf(fp, color, " %04x: ", val);
Wang Nanc339b1a2016-02-24 11:20:44 +0000135 break;
136 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300137 printed += color_fprintf(fp, color, " %02x", val);
Wang Nanc339b1a2016-02-24 11:20:44 +0000138 break;
139 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300140 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000141 break;
142 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300143 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000144 break;
145 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300146 printed += color_fprintf(fp, color, "%c",
Wang Nanc339b1a2016-02-24 11:20:44 +0000147 isprint(ch) ? ch : '.');
148 break;
149 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300150 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000151 break;
152 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300153 printed += color_fprintf(fp, color, "\n");
Wang Nanc339b1a2016-02-24 11:20:44 +0000154 break;
155 case BINARY_PRINT_DATA_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300156 printed += fprintf(fp, "\n");
Wang Nanc339b1a2016-02-24 11:20:44 +0000157 break;
158 default:
159 break;
160 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300161
162 return printed;
Wang Nanc339b1a2016-02-24 11:20:44 +0000163}
164
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200165void trace_event(union perf_event *event)
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200166{
167 unsigned char *raw_event = (void *)event;
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200168
169 if (!dump_trace)
170 return;
171
Wang Nanc339b1a2016-02-24 11:20:44 +0000172 print_binary(raw_event, event->header.size, 16,
173 trace_event_printer, event);
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200174}
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200175
176static struct debug_variable {
177 const char *name;
178 int *ptr;
179} debug_variables[] = {
Jiri Olsacee3ab92014-07-11 14:49:54 +0200180 { .name = "verbose", .ptr = &verbose },
181 { .name = "ordered-events", .ptr = &debug_ordered_events},
Andi Kleenf78eaef2014-11-21 13:38:00 -0800182 { .name = "stderr", .ptr = &redirect_to_stderr},
Jiri Olsaedbe9812015-02-20 23:17:00 +0100183 { .name = "data-convert", .ptr = &debug_data_convert },
Ravi Bangoriaccd26742019-11-08 15:11:28 +0530184 { .name = "perf-event-open", .ptr = &debug_peo_args },
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200185 { .name = NULL, }
186};
187
188int perf_debug_option(const char *str)
189{
190 struct debug_variable *var = &debug_variables[0];
191 char *vstr, *s = strdup(str);
192 int v = 1;
193
194 vstr = strchr(s, '=');
195 if (vstr)
196 *vstr++ = 0;
197
198 while (var->name) {
199 if (!strcmp(s, var->name))
200 break;
201 var++;
202 }
203
204 if (!var->name) {
205 pr_err("Unknown debug variable name '%s'\n", s);
206 free(s);
207 return -1;
208 }
209
210 if (vstr) {
211 v = atoi(vstr);
212 /*
213 * Allow only values in range (0, 10),
214 * otherwise set 0.
215 */
216 v = (v < 0) || (v > 10) ? 0 : v;
217 }
218
Namhyung Kim80df1982017-02-17 17:17:37 +0900219 if (quiet)
220 v = -1;
221
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200222 *var->ptr = v;
223 free(s);
224 return 0;
225}
Jiri Olsadd629cc2016-02-14 17:03:45 +0100226
Namhyung Kim80df1982017-02-17 17:17:37 +0900227int perf_quiet_option(void)
228{
229 struct debug_variable *var = &debug_variables[0];
230
231 /* disable all debug messages */
232 while (var->name) {
233 *var->ptr = -1;
234 var++;
235 }
236
Namhyung Kim80df1982017-02-17 17:17:37 +0900237 return 0;
238}
239
Jiri Olsadd629cc2016-02-14 17:03:45 +0100240#define DEBUG_WRAPPER(__n, __l) \
241static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
242{ \
243 va_list args; \
244 int ret; \
245 \
246 va_start(args, fmt); \
247 ret = veprintf(__l, verbose, fmt, args); \
248 va_end(args); \
249 return ret; \
250}
251
252DEBUG_WRAPPER(warning, 0);
253DEBUG_WRAPPER(debug, 1);
254
255void perf_debug_setup(void)
256{
257 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
258}
Arnaldo Carvalho de Melo8c2b7ca2017-04-19 16:21:59 -0300259
260/* Obtain a backtrace and print it to stdout. */
261#ifdef HAVE_BACKTRACE_SUPPORT
262void dump_stack(void)
263{
264 void *array[16];
265 size_t size = backtrace(array, ARRAY_SIZE(array));
266 char **strings = backtrace_symbols(array, size);
267 size_t i;
268
269 printf("Obtained %zd stack frames.\n", size);
270
271 for (i = 0; i < size; i++)
272 printf("%s\n", strings[i]);
273
274 free(strings);
275}
276#else
277void dump_stack(void) {}
278#endif
279
280void sighandler_dump_stack(int sig)
281{
282 psignal(sig, "perf");
283 dump_stack();
284 signal(sig, SIG_DFL);
285 raise(sig);
286}