aboutsummaryrefslogtreecommitdiff
path: root/powerpc/pmu/ebb/trace.c
blob: 251e66ab2aa7f08f6e51493831a1b58bc700547d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright 2014, Michael Ellerman, IBM Corp.
 * Licensed under GPLv2.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

#include "trace.h"


struct trace_buffer *trace_buffer_allocate(u64 size)
{
	struct trace_buffer *tb;

	if (size < sizeof(*tb)) {
		fprintf(stderr, "Error: trace buffer too small\n");
		return NULL;
	}

	tb = mmap(NULL, size, PROT_READ | PROT_WRITE,
		  MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	if (tb == MAP_FAILED) {
		perror("mmap");
		return NULL;
	}

	tb->size = size;
	tb->tail = tb->data;
	tb->overflow = false;

	return tb;
}

static bool trace_check_bounds(struct trace_buffer *tb, void *p)
{
	return p < ((void *)tb + tb->size);
}

static bool trace_check_alloc(struct trace_buffer *tb, void *p)
{
	/*
	 * If we ever overflowed don't allow any more input. This prevents us
	 * from dropping a large item and then later logging a small one. The
	 * buffer should just stop when overflow happened, not be patchy. If
	 * you're overflowing, make your buffer bigger.
	 */
	if (tb->overflow)
		return false;

	if (!trace_check_bounds(tb, p)) {
		tb->overflow = true;
		return false;
	}

	return true;
}

static void *trace_alloc(struct trace_buffer *tb, int bytes)
{
	void *p, *newtail;

	p = tb->tail;
	newtail = tb->tail + bytes;
	if (!trace_check_alloc(tb, newtail))
		return NULL;

	tb->tail = newtail;

	return p;
}

static struct trace_entry *trace_alloc_entry(struct trace_buffer *tb, int payload_size)
{
	struct trace_entry *e;

	e = trace_alloc(tb, sizeof(*e) + payload_size);
	if (e)
		e->length = payload_size;

	return e;
}

int trace_log_reg(struct trace_buffer *tb, u64 reg, u64 value)
{
	struct trace_entry *e;
	u64 *p;

	e = trace_alloc_entry(tb, sizeof(reg) + sizeof(value));
	if (!e)
		return -ENOSPC;

	e->type = TRACE_TYPE_REG;
	p = (u64 *)e->data;
	*p++ = reg;
	*p++ = value;

	return 0;
}

int trace_log_counter(struct trace_buffer *tb, u64 value)
{
	struct trace_entry *e;
	u64 *p;

	e = trace_alloc_entry(tb, sizeof(value));
	if (!e)
		return -ENOSPC;

	e->type = TRACE_TYPE_COUNTER;
	p = (u64 *)e->data;
	*p++ = value;

	return 0;
}

int trace_log_string(struct trace_buffer *tb, char *str)
{
	struct trace_entry *e;
	char *p;
	int len;

	len = strlen(str);

	/* We NULL terminate to make printing easier */
	e = trace_alloc_entry(tb, len + 1);
	if (!e)
		return -ENOSPC;

	e->type = TRACE_TYPE_STRING;
	p = (char *)e->data;
	memcpy(p, str, len);
	p += len;
	*p = '\0';

	return 0;
}

int trace_log_indent(struct trace_buffer *tb)
{
	struct trace_entry *e;

	e = trace_alloc_entry(tb, 0);
	if (!e)
		return -ENOSPC;

	e->type = TRACE_TYPE_INDENT;

	return 0;
}

int trace_log_outdent(struct trace_buffer *tb)
{
	struct trace_entry *e;

	e = trace_alloc_entry(tb, 0);
	if (!e)
		return -ENOSPC;

	e->type = TRACE_TYPE_OUTDENT;

	return 0;
}

static void trace_print_header(int seq, int prefix)
{
	printf("%*s[%d]: ", prefix, "", seq);
}

static char *trace_decode_reg(int reg)
{
	switch (reg) {
		case 769: return "SPRN_MMCR2"; break;
		case 770: return "SPRN_MMCRA"; break;
		case 779: return "SPRN_MMCR0"; break;
		case 804: return "SPRN_EBBHR"; break;
		case 805: return "SPRN_EBBRR"; break;
		case 806: return "SPRN_BESCR"; break;
		case 800: return "SPRN_BESCRS"; break;
		case 801: return "SPRN_BESCRSU"; break;
		case 802: return "SPRN_BESCRR"; break;
		case 803: return "SPRN_BESCRRU"; break;
		case 771: return "SPRN_PMC1"; break;
		case 772: return "SPRN_PMC2"; break;
		case 773: return "SPRN_PMC3"; break;
		case 774: return "SPRN_PMC4"; break;
		case 775: return "SPRN_PMC5"; break;
		case 776: return "SPRN_PMC6"; break;
		case 780: return "SPRN_SIAR"; break;
		case 781: return "SPRN_SDAR"; break;
		case 768: return "SPRN_SIER"; break;
	}

	return NULL;
}

static void trace_print_reg(struct trace_entry *e)
{
	u64 *p, *reg, *value;
	char *name;

	p = (u64 *)e->data;
	reg = p++;
	value = p;

	name = trace_decode_reg(*reg);
	if (name)
		printf("register %-10s = 0x%016llx\n", name, *value);
	else
		printf("register %lld = 0x%016llx\n", *reg, *value);
}

static void trace_print_counter(struct trace_entry *e)
{
	u64 *value;

	value = (u64 *)e->data;
	printf("counter = %lld\n", *value);
}

static void trace_print_string(struct trace_entry *e)
{
	char *str;

	str = (char *)e->data;
	puts(str);
}

#define BASE_PREFIX	2
#define PREFIX_DELTA	8

static void trace_print_entry(struct trace_entry *e, int seq, int *prefix)
{
	switch (e->type) {
	case TRACE_TYPE_REG:
		trace_print_header(seq, *prefix);
		trace_print_reg(e);
		break;
	case TRACE_TYPE_COUNTER:
		trace_print_header(seq, *prefix);
		trace_print_counter(e);
		break;
	case TRACE_TYPE_STRING:
		trace_print_header(seq, *prefix);
		trace_print_string(e);
		break;
	case TRACE_TYPE_INDENT:
		trace_print_header(seq, *prefix);
		puts("{");
		*prefix += PREFIX_DELTA;
		break;
	case TRACE_TYPE_OUTDENT:
		*prefix -= PREFIX_DELTA;
		if (*prefix < BASE_PREFIX)
			*prefix = BASE_PREFIX;
		trace_print_header(seq, *prefix);
		puts("}");
		break;
	default:
		trace_print_header(seq, *prefix);
		printf("entry @ %p type %d\n", e, e->type);
		break;
	}
}

void trace_buffer_print(struct trace_buffer *tb)
{
	struct trace_entry *e;
	int i, prefix;
	void *p;

	printf("Trace buffer dump:\n");
	printf("  address  %p \n", tb);
	printf("  tail     %p\n", tb->tail);
	printf("  size     %llu\n", tb->size);
	printf("  overflow %s\n", tb->overflow ? "TRUE" : "false");
	printf("  Content:\n");

	p = tb->data;

	i = 0;
	prefix = BASE_PREFIX;

	while (trace_check_bounds(tb, p) && p < tb->tail) {
		e = p;

		trace_print_entry(e, i, &prefix);

		i++;
		p = (void *)e + sizeof(*e) + e->length;
	}
}

void trace_print_location(struct trace_buffer *tb)
{
	printf("Trace buffer 0x%llx bytes @ %p\n", tb->size, tb);
}