blob: 2c06abf6dcd263d741a7fb2b7ba109554b56a691 [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>
Jiri Olsabcbd79d2021-01-02 23:04:22 +010013#include <sys/time.h>
Arnaldo Carvalho de Melo8c2b7ca2017-04-19 16:21:59 -030014#ifdef HAVE_BACKTRACE_SUPPORT
15#include <execinfo.h>
16#endif
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"
Namhyung Kim16ad2ff2012-05-07 14:09:02 +090021#include "target.h"
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030022#include "ui/helpline.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030023#include "ui/ui.h"
Changbin Dua80abe22020-08-08 10:31:30 +080024#include "util/parse-sublevel-options.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020025
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030026#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030027
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -020028int verbose;
Ravi Bangoriaccd26742019-11-08 15:11:28 +053029int debug_peo_args;
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -020030bool dump_trace = false, quiet = false;
Jiri Olsacee3ab92014-07-11 14:49:54 +020031int debug_ordered_events;
Andi Kleenf78eaef2014-11-21 13:38:00 -080032static int redirect_to_stderr;
Jiri Olsaedbe9812015-02-20 23:17:00 +010033int debug_data_convert;
Jiri Olsa8abceac2020-12-12 11:43:51 +010034static FILE *debug_file;
Jiri Olsabcbd79d2021-01-02 23:04:22 +010035bool debug_display_time;
Jiri Olsa8abceac2020-12-12 11:43:51 +010036
37void debug_set_file(FILE *file)
38{
39 debug_file = file;
40}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020041
Jiri Olsabcbd79d2021-01-02 23:04:22 +010042void 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, &ltime) == NULL)
60 return 0;
61
62 strftime(date, sizeof(date), "%F %H:%M:%S", &ltime);
63 return fprintf(file, "[%s.%06lu] ", date, (long)tod.tv_usec);
64}
65
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030066int veprintf(int level, int var, const char *fmt, va_list args)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020067{
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020068 int ret = 0;
69
Jiri Olsac95688a2014-07-14 23:46:49 +020070 if (var >= level) {
Jiri Olsabcbd79d2021-01-02 23:04:22 +010071 if (use_browser >= 1 && !redirect_to_stderr) {
Namhyung Kimb56e5332012-11-15 01:47:41 +090072 ui_helpline__vshow(fmt, args);
Jiri Olsabcbd79d2021-01-02 23:04:22 +010073 } else {
74 ret = fprintf_time(debug_file);
75 ret += vfprintf(debug_file, fmt, args);
76 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +020077 }
78
79 return ret;
80}
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +020081
Jiri Olsac95688a2014-07-14 23:46:49 +020082int eprintf(int level, int var, const char *fmt, ...)
Jiri Olsaf772abc2013-12-03 14:09:25 +010083{
84 va_list args;
85 int ret;
86
87 va_start(args, fmt);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030088 ret = veprintf(level, var, fmt, args);
Jiri Olsaf772abc2013-12-03 14:09:25 +010089 va_end(args);
90
91 return ret;
92}
93
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -030094static int veprintf_time(u64 t, const char *fmt, va_list args)
Jiri Olsacee3ab92014-07-11 14:49:54 +020095{
96 int ret = 0;
97 u64 secs, usecs, nsecs = t;
98
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030099 secs = nsecs / NSEC_PER_SEC;
100 nsecs -= secs * NSEC_PER_SEC;
101 usecs = nsecs / NSEC_PER_USEC;
Jiri Olsacee3ab92014-07-11 14:49:54 +0200102
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);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -0300116 ret = veprintf_time(t, fmt, args);
Jiri Olsacee3ab92014-07-11 14:49:54 +0200117 va_end(args);
118 }
119
120 return ret;
121}
122
Jiri Olsaf772abc2013-12-03 14:09:25 +0100123/*
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);
Arnaldo Carvalho de Melobedbdd42016-02-16 11:48:38 -0300132 veprintf(1, verbose, fmt, args);
Jiri Olsaf772abc2013-12-03 14:09:25 +0100133 va_end(args);
Jiri Olsac95688a2014-07-14 23:46:49 +0200134 eprintf(1, verbose, "\n");
Jiri Olsaf772abc2013-12-03 14:09:25 +0100135}
136
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200137int 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}
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200150
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300151static int trace_event_printer(enum binary_printer_ops op,
152 unsigned int val, void *extra, FILE *fp)
Wang Nanc339b1a2016-02-24 11:20:44 +0000153{
154 const char *color = PERF_COLOR_BLUE;
155 union perf_event *event = (union perf_event *)extra;
156 unsigned char ch = (unsigned char)val;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300157 int printed = 0;
Wang Nanc339b1a2016-02-24 11:20:44 +0000158
159 switch (op) {
160 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300161 printed += fprintf(fp, ".");
162 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
163 event->header.size);
Wang Nanc339b1a2016-02-24 11:20:44 +0000164 break;
165 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300166 printed += fprintf(fp, ".");
Wang Nanc339b1a2016-02-24 11:20:44 +0000167 break;
168 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300169 printed += color_fprintf(fp, color, " %04x: ", val);
Wang Nanc339b1a2016-02-24 11:20:44 +0000170 break;
171 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300172 printed += color_fprintf(fp, color, " %02x", val);
Wang Nanc339b1a2016-02-24 11:20:44 +0000173 break;
174 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300175 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000176 break;
177 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300178 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000179 break;
180 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300181 printed += color_fprintf(fp, color, "%c",
Wang Nanc339b1a2016-02-24 11:20:44 +0000182 isprint(ch) ? ch : '.');
183 break;
184 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300185 printed += color_fprintf(fp, color, " ");
Wang Nanc339b1a2016-02-24 11:20:44 +0000186 break;
187 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300188 printed += color_fprintf(fp, color, "\n");
Wang Nanc339b1a2016-02-24 11:20:44 +0000189 break;
190 case BINARY_PRINT_DATA_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300191 printed += fprintf(fp, "\n");
Wang Nanc339b1a2016-02-24 11:20:44 +0000192 break;
193 default:
194 break;
195 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -0300196
197 return printed;
Wang Nanc339b1a2016-02-24 11:20:44 +0000198}
199
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200200void trace_event(union perf_event *event)
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200201{
202 unsigned char *raw_event = (void *)event;
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200203
204 if (!dump_trace)
205 return;
206
Wang Nanc339b1a2016-02-24 11:20:44 +0000207 print_binary(raw_event, event->header.size, 16,
208 trace_event_printer, event);
Frederic Weisbecker8f288272009-08-16 22:05:48 +0200209}
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200210
Changbin Dua80abe22020-08-08 10:31:30 +0800211static 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 },
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200217 { .name = NULL, }
218};
219
220int perf_debug_option(const char *str)
221{
Changbin Dua80abe22020-08-08 10:31:30 +0800222 int ret;
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200223
Changbin Dua80abe22020-08-08 10:31:30 +0800224 ret = perf_parse_sublevel_options(str, debug_opts);
225 if (ret)
226 return ret;
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200227
Changbin Dua80abe22020-08-08 10:31:30 +0800228 /* Allow only verbose value in range (0, 10), otherwise set 0. */
229 verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200230
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200231 return 0;
232}
Jiri Olsadd629cc2016-02-14 17:03:45 +0100233
Namhyung Kim80df1982017-02-17 17:17:37 +0900234int perf_quiet_option(void)
235{
Changbin Dua80abe22020-08-08 10:31:30 +0800236 struct sublevel_option *opt = &debug_opts[0];
Namhyung Kim80df1982017-02-17 17:17:37 +0900237
238 /* disable all debug messages */
Changbin Dua80abe22020-08-08 10:31:30 +0800239 while (opt->name) {
240 *opt->value_ptr = -1;
241 opt++;
Namhyung Kim80df1982017-02-17 17:17:37 +0900242 }
243
Namhyung Kim80df1982017-02-17 17:17:37 +0900244 return 0;
245}
246
Jiri Olsadd629cc2016-02-14 17:03:45 +0100247#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{
Jiri Olsa8abceac2020-12-12 11:43:51 +0100264 debug_set_file(stderr);
Jiri Olsadd629cc2016-02-14 17:03:45 +0100265 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
266}
Arnaldo Carvalho de Melo8c2b7ca2017-04-19 16:21:59 -0300267
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}