xref: /linux/tools/perf/util/debug.c (revision c506c96b61fa96c9a52ad4d25e895e45c1692650)
1 /* For general debugging purposes */
2 
3 #include "../perf.h"
4 
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 
9 #include "cache.h"
10 #include "color.h"
11 #include "event.h"
12 #include "debug.h"
13 #include "util.h"
14 #include "target.h"
15 
16 int verbose;
17 bool dump_trace = false, quiet = false;
18 
19 static int _eprintf(int level, const char *fmt, va_list args)
20 {
21 	int ret = 0;
22 
23 	if (verbose >= level) {
24 		if (use_browser >= 1)
25 			ui_helpline__vshow(fmt, args);
26 		else
27 			ret = vfprintf(stderr, fmt, args);
28 		va_end(args);
29 	}
30 
31 	return ret;
32 }
33 
34 int eprintf(int level, const char *fmt, ...)
35 {
36 	va_list args;
37 	int ret;
38 
39 	va_start(args, fmt);
40 	ret = _eprintf(level, fmt, args);
41 	va_end(args);
42 
43 	return ret;
44 }
45 
46 /*
47  * Overloading libtraceevent standard info print
48  * function, display with -v in perf.
49  */
50 void pr_stat(const char *fmt, ...)
51 {
52 	va_list args;
53 
54 	va_start(args, fmt);
55 	_eprintf(1, fmt, args);
56 	va_end(args);
57 	eprintf(1, "\n");
58 }
59 
60 int dump_printf(const char *fmt, ...)
61 {
62 	va_list args;
63 	int ret = 0;
64 
65 	if (dump_trace) {
66 		va_start(args, fmt);
67 		ret = vprintf(fmt, args);
68 		va_end(args);
69 	}
70 
71 	return ret;
72 }
73 
74 void trace_event(union perf_event *event)
75 {
76 	unsigned char *raw_event = (void *)event;
77 	const char *color = PERF_COLOR_BLUE;
78 	int i, j;
79 
80 	if (!dump_trace)
81 		return;
82 
83 	printf(".");
84 	color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n",
85 		      event->header.size);
86 
87 	for (i = 0; i < event->header.size; i++) {
88 		if ((i & 15) == 0) {
89 			printf(".");
90 			color_fprintf(stdout, color, "  %04x: ", i);
91 		}
92 
93 		color_fprintf(stdout, color, " %02x", raw_event[i]);
94 
95 		if (((i & 15) == 15) || i == event->header.size-1) {
96 			color_fprintf(stdout, color, "  ");
97 			for (j = 0; j < 15-(i & 15); j++)
98 				color_fprintf(stdout, color, "   ");
99 			for (j = i & ~15; j <= i; j++) {
100 				color_fprintf(stdout, color, "%c",
101 					      isprint(raw_event[j]) ?
102 					      raw_event[j] : '.');
103 			}
104 			color_fprintf(stdout, color, "\n");
105 		}
106 	}
107 	printf(".\n");
108 }
109