irqchip: Fix dependencies for archs w/o HAS_IOMEM
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / pmu / ebb / trace.c
blob251e66ab2aa7f08f6e51493831a1b58bc700547d
1 /*
2 * Copyright 2014, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
4 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/mman.h>
12 #include "trace.h"
15 struct trace_buffer *trace_buffer_allocate(u64 size)
17 struct trace_buffer *tb;
19 if (size < sizeof(*tb)) {
20 fprintf(stderr, "Error: trace buffer too small\n");
21 return NULL;
24 tb = mmap(NULL, size, PROT_READ | PROT_WRITE,
25 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
26 if (tb == MAP_FAILED) {
27 perror("mmap");
28 return NULL;
31 tb->size = size;
32 tb->tail = tb->data;
33 tb->overflow = false;
35 return tb;
38 static bool trace_check_bounds(struct trace_buffer *tb, void *p)
40 return p < ((void *)tb + tb->size);
43 static bool trace_check_alloc(struct trace_buffer *tb, void *p)
46 * If we ever overflowed don't allow any more input. This prevents us
47 * from dropping a large item and then later logging a small one. The
48 * buffer should just stop when overflow happened, not be patchy. If
49 * you're overflowing, make your buffer bigger.
51 if (tb->overflow)
52 return false;
54 if (!trace_check_bounds(tb, p)) {
55 tb->overflow = true;
56 return false;
59 return true;
62 static void *trace_alloc(struct trace_buffer *tb, int bytes)
64 void *p, *newtail;
66 p = tb->tail;
67 newtail = tb->tail + bytes;
68 if (!trace_check_alloc(tb, newtail))
69 return NULL;
71 tb->tail = newtail;
73 return p;
76 static struct trace_entry *trace_alloc_entry(struct trace_buffer *tb, int payload_size)
78 struct trace_entry *e;
80 e = trace_alloc(tb, sizeof(*e) + payload_size);
81 if (e)
82 e->length = payload_size;
84 return e;
87 int trace_log_reg(struct trace_buffer *tb, u64 reg, u64 value)
89 struct trace_entry *e;
90 u64 *p;
92 e = trace_alloc_entry(tb, sizeof(reg) + sizeof(value));
93 if (!e)
94 return -ENOSPC;
96 e->type = TRACE_TYPE_REG;
97 p = (u64 *)e->data;
98 *p++ = reg;
99 *p++ = value;
101 return 0;
104 int trace_log_counter(struct trace_buffer *tb, u64 value)
106 struct trace_entry *e;
107 u64 *p;
109 e = trace_alloc_entry(tb, sizeof(value));
110 if (!e)
111 return -ENOSPC;
113 e->type = TRACE_TYPE_COUNTER;
114 p = (u64 *)e->data;
115 *p++ = value;
117 return 0;
120 int trace_log_string(struct trace_buffer *tb, char *str)
122 struct trace_entry *e;
123 char *p;
124 int len;
126 len = strlen(str);
128 /* We NULL terminate to make printing easier */
129 e = trace_alloc_entry(tb, len + 1);
130 if (!e)
131 return -ENOSPC;
133 e->type = TRACE_TYPE_STRING;
134 p = (char *)e->data;
135 memcpy(p, str, len);
136 p += len;
137 *p = '\0';
139 return 0;
142 int trace_log_indent(struct trace_buffer *tb)
144 struct trace_entry *e;
146 e = trace_alloc_entry(tb, 0);
147 if (!e)
148 return -ENOSPC;
150 e->type = TRACE_TYPE_INDENT;
152 return 0;
155 int trace_log_outdent(struct trace_buffer *tb)
157 struct trace_entry *e;
159 e = trace_alloc_entry(tb, 0);
160 if (!e)
161 return -ENOSPC;
163 e->type = TRACE_TYPE_OUTDENT;
165 return 0;
168 static void trace_print_header(int seq, int prefix)
170 printf("%*s[%d]: ", prefix, "", seq);
173 static char *trace_decode_reg(int reg)
175 switch (reg) {
176 case 769: return "SPRN_MMCR2"; break;
177 case 770: return "SPRN_MMCRA"; break;
178 case 779: return "SPRN_MMCR0"; break;
179 case 804: return "SPRN_EBBHR"; break;
180 case 805: return "SPRN_EBBRR"; break;
181 case 806: return "SPRN_BESCR"; break;
182 case 800: return "SPRN_BESCRS"; break;
183 case 801: return "SPRN_BESCRSU"; break;
184 case 802: return "SPRN_BESCRR"; break;
185 case 803: return "SPRN_BESCRRU"; break;
186 case 771: return "SPRN_PMC1"; break;
187 case 772: return "SPRN_PMC2"; break;
188 case 773: return "SPRN_PMC3"; break;
189 case 774: return "SPRN_PMC4"; break;
190 case 775: return "SPRN_PMC5"; break;
191 case 776: return "SPRN_PMC6"; break;
192 case 780: return "SPRN_SIAR"; break;
193 case 781: return "SPRN_SDAR"; break;
194 case 768: return "SPRN_SIER"; break;
197 return NULL;
200 static void trace_print_reg(struct trace_entry *e)
202 u64 *p, *reg, *value;
203 char *name;
205 p = (u64 *)e->data;
206 reg = p++;
207 value = p;
209 name = trace_decode_reg(*reg);
210 if (name)
211 printf("register %-10s = 0x%016llx\n", name, *value);
212 else
213 printf("register %lld = 0x%016llx\n", *reg, *value);
216 static void trace_print_counter(struct trace_entry *e)
218 u64 *value;
220 value = (u64 *)e->data;
221 printf("counter = %lld\n", *value);
224 static void trace_print_string(struct trace_entry *e)
226 char *str;
228 str = (char *)e->data;
229 puts(str);
232 #define BASE_PREFIX 2
233 #define PREFIX_DELTA 8
235 static void trace_print_entry(struct trace_entry *e, int seq, int *prefix)
237 switch (e->type) {
238 case TRACE_TYPE_REG:
239 trace_print_header(seq, *prefix);
240 trace_print_reg(e);
241 break;
242 case TRACE_TYPE_COUNTER:
243 trace_print_header(seq, *prefix);
244 trace_print_counter(e);
245 break;
246 case TRACE_TYPE_STRING:
247 trace_print_header(seq, *prefix);
248 trace_print_string(e);
249 break;
250 case TRACE_TYPE_INDENT:
251 trace_print_header(seq, *prefix);
252 puts("{");
253 *prefix += PREFIX_DELTA;
254 break;
255 case TRACE_TYPE_OUTDENT:
256 *prefix -= PREFIX_DELTA;
257 if (*prefix < BASE_PREFIX)
258 *prefix = BASE_PREFIX;
259 trace_print_header(seq, *prefix);
260 puts("}");
261 break;
262 default:
263 trace_print_header(seq, *prefix);
264 printf("entry @ %p type %d\n", e, e->type);
265 break;
269 void trace_buffer_print(struct trace_buffer *tb)
271 struct trace_entry *e;
272 int i, prefix;
273 void *p;
275 printf("Trace buffer dump:\n");
276 printf(" address %p \n", tb);
277 printf(" tail %p\n", tb->tail);
278 printf(" size %llu\n", tb->size);
279 printf(" overflow %s\n", tb->overflow ? "TRUE" : "false");
280 printf(" Content:\n");
282 p = tb->data;
284 i = 0;
285 prefix = BASE_PREFIX;
287 while (trace_check_bounds(tb, p) && p < tb->tail) {
288 e = p;
290 trace_print_entry(e, i, &prefix);
292 i++;
293 p = (void *)e + sizeof(*e) + e->length;
297 void trace_print_location(struct trace_buffer *tb)
299 printf("Trace buffer 0x%llx bytes @ %p\n", tb->size, tb);