WIP FPC-III support
[linux/fpc-iii.git] / tools / perf / util / debug.c
blob50fd6a4be4e016d8505b7fbd8d5446f63194be97
1 // SPDX-License-Identifier: GPL-2.0
2 /* For general debugging purposes */
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 #ifdef HAVE_BACKTRACE_SUPPORT
14 #include <execinfo.h>
15 #endif
16 #include "color.h"
17 #include "event.h"
18 #include "debug.h"
19 #include "print_binary.h"
20 #include "target.h"
21 #include "ui/helpline.h"
22 #include "ui/ui.h"
23 #include "util/parse-sublevel-options.h"
25 #include <linux/ctype.h>
27 int verbose;
28 int debug_peo_args;
29 bool dump_trace = false, quiet = false;
30 int debug_ordered_events;
31 static int redirect_to_stderr;
32 int debug_data_convert;
33 static FILE *debug_file;
35 void debug_set_file(FILE *file)
37 debug_file = file;
40 int veprintf(int level, int var, const char *fmt, va_list args)
42 int ret = 0;
44 if (var >= level) {
45 if (use_browser >= 1 && !redirect_to_stderr)
46 ui_helpline__vshow(fmt, args);
47 else
48 ret = vfprintf(debug_file, fmt, args);
51 return ret;
54 int eprintf(int level, int var, const char *fmt, ...)
56 va_list args;
57 int ret;
59 va_start(args, fmt);
60 ret = veprintf(level, var, fmt, args);
61 va_end(args);
63 return ret;
66 static int veprintf_time(u64 t, const char *fmt, va_list args)
68 int ret = 0;
69 u64 secs, usecs, nsecs = t;
71 secs = nsecs / NSEC_PER_SEC;
72 nsecs -= secs * NSEC_PER_SEC;
73 usecs = nsecs / NSEC_PER_USEC;
75 ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
76 secs, usecs);
77 ret += vfprintf(stderr, fmt, args);
78 return ret;
81 int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
83 int ret = 0;
84 va_list args;
86 if (var >= level) {
87 va_start(args, fmt);
88 ret = veprintf_time(t, fmt, args);
89 va_end(args);
92 return ret;
96 * Overloading libtraceevent standard info print
97 * function, display with -v in perf.
99 void pr_stat(const char *fmt, ...)
101 va_list args;
103 va_start(args, fmt);
104 veprintf(1, verbose, fmt, args);
105 va_end(args);
106 eprintf(1, verbose, "\n");
109 int dump_printf(const char *fmt, ...)
111 va_list args;
112 int ret = 0;
114 if (dump_trace) {
115 va_start(args, fmt);
116 ret = vprintf(fmt, args);
117 va_end(args);
120 return ret;
123 static int trace_event_printer(enum binary_printer_ops op,
124 unsigned int val, void *extra, FILE *fp)
126 const char *color = PERF_COLOR_BLUE;
127 union perf_event *event = (union perf_event *)extra;
128 unsigned char ch = (unsigned char)val;
129 int printed = 0;
131 switch (op) {
132 case BINARY_PRINT_DATA_BEGIN:
133 printed += fprintf(fp, ".");
134 printed += color_fprintf(fp, color, "\n. ... raw event: size %d bytes\n",
135 event->header.size);
136 break;
137 case BINARY_PRINT_LINE_BEGIN:
138 printed += fprintf(fp, ".");
139 break;
140 case BINARY_PRINT_ADDR:
141 printed += color_fprintf(fp, color, " %04x: ", val);
142 break;
143 case BINARY_PRINT_NUM_DATA:
144 printed += color_fprintf(fp, color, " %02x", val);
145 break;
146 case BINARY_PRINT_NUM_PAD:
147 printed += color_fprintf(fp, color, " ");
148 break;
149 case BINARY_PRINT_SEP:
150 printed += color_fprintf(fp, color, " ");
151 break;
152 case BINARY_PRINT_CHAR_DATA:
153 printed += color_fprintf(fp, color, "%c",
154 isprint(ch) ? ch : '.');
155 break;
156 case BINARY_PRINT_CHAR_PAD:
157 printed += color_fprintf(fp, color, " ");
158 break;
159 case BINARY_PRINT_LINE_END:
160 printed += color_fprintf(fp, color, "\n");
161 break;
162 case BINARY_PRINT_DATA_END:
163 printed += fprintf(fp, "\n");
164 break;
165 default:
166 break;
169 return printed;
172 void trace_event(union perf_event *event)
174 unsigned char *raw_event = (void *)event;
176 if (!dump_trace)
177 return;
179 print_binary(raw_event, event->header.size, 16,
180 trace_event_printer, event);
183 static struct sublevel_option debug_opts[] = {
184 { .name = "verbose", .value_ptr = &verbose },
185 { .name = "ordered-events", .value_ptr = &debug_ordered_events},
186 { .name = "stderr", .value_ptr = &redirect_to_stderr},
187 { .name = "data-convert", .value_ptr = &debug_data_convert },
188 { .name = "perf-event-open", .value_ptr = &debug_peo_args },
189 { .name = NULL, }
192 int perf_debug_option(const char *str)
194 int ret;
196 ret = perf_parse_sublevel_options(str, debug_opts);
197 if (ret)
198 return ret;
200 /* Allow only verbose value in range (0, 10), otherwise set 0. */
201 verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose;
203 return 0;
206 int perf_quiet_option(void)
208 struct sublevel_option *opt = &debug_opts[0];
210 /* disable all debug messages */
211 while (opt->name) {
212 *opt->value_ptr = -1;
213 opt++;
216 return 0;
219 #define DEBUG_WRAPPER(__n, __l) \
220 static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
222 va_list args; \
223 int ret; \
225 va_start(args, fmt); \
226 ret = veprintf(__l, verbose, fmt, args); \
227 va_end(args); \
228 return ret; \
231 DEBUG_WRAPPER(warning, 0);
232 DEBUG_WRAPPER(debug, 1);
234 void perf_debug_setup(void)
236 debug_set_file(stderr);
237 libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
240 /* Obtain a backtrace and print it to stdout. */
241 #ifdef HAVE_BACKTRACE_SUPPORT
242 void dump_stack(void)
244 void *array[16];
245 size_t size = backtrace(array, ARRAY_SIZE(array));
246 char **strings = backtrace_symbols(array, size);
247 size_t i;
249 printf("Obtained %zd stack frames.\n", size);
251 for (i = 0; i < size; i++)
252 printf("%s\n", strings[i]);
254 free(strings);
256 #else
257 void dump_stack(void) {}
258 #endif
260 void sighandler_dump_stack(int sig)
262 psignal(sig, "perf");
263 dump_stack();
264 signal(sig, SIG_DFL);
265 raise(sig);