summaryrefslogtreecommitdiff
path: root/minix/usr.sbin/btrace/btrace.c
blob: 6b0514220d03ccd7ed730f8fa224727144520a54 (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
/* Block trace command line tool - by D.C. van Moolenbroek */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <minix/btrace.h>
#include <minix/u64.h>
#include <sys/ioctl.h>

static btrace_entry buf[BTBUF_SIZE];

static void __dead
usage(void)
{
	fprintf(stderr, "usage:\n"
	    "%s start <device> <nr_entries>\n"
	    "%s stop <device> <file>\n"
	    "%s reset <device>\n"
	    "%s dump <file>\n",
	    getprogname(), getprogname(), getprogname(), getprogname());

	exit(EXIT_FAILURE);
}

static void
btrace_start(char * device, int nr_entries)
{
	int r, ctl, devfd;
	size_t size;

	if ((devfd = open(device, O_RDONLY)) < 0) {
		perror("device open");
		exit(EXIT_FAILURE);
	}

	size = nr_entries;
	if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) {
		perror("ioctl(BIOCTRACEBUF)");
		exit(EXIT_FAILURE);
	}

	ctl = BTCTL_START;
	if ((r = ioctl(devfd, BIOCTRACECTL, &ctl)) < 0) {
		perror("ioctl(BIOCTRACECTL)");

		size = 0;
		(void)ioctl(devfd, BIOCTRACEBUF, &size);

		exit(EXIT_FAILURE);
	}

	close(devfd);
}

static void
btrace_stop(char * device, char * file)
{
	int r, ctl, devfd, outfd;
	size_t size;

	if ((devfd = open(device, O_RDONLY)) < 0) {
		perror("device open");
		exit(EXIT_FAILURE);
	}

	if ((outfd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) {
		perror("file open");
		exit(EXIT_FAILURE);
	  }

	ctl = BTCTL_STOP;
	if ((r = ioctl(devfd, BIOCTRACECTL, &ctl)) < 0) {
		perror("ioctl(BIOCTRACECTL)");
		exit(EXIT_FAILURE);
	}

	for (;;) {
		if ((r = ioctl(devfd, BIOCTRACEGET, buf)) < 0) {
			perror("ioctl(BIOCTRACEGET)");
			break;
		}

		if (r == 0) break;

		size = r * sizeof(buf[0]);
		if ((r = write(outfd, (char *)buf, size)) != size) {
			if (r < 0) perror("write");
			else fputs("short write\n", stderr);
		}
	}

	close(outfd);

	size = 0;
	if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) {
		perror("ioctl(BIOCTRACEBUF)");
		exit(EXIT_FAILURE);
	}

	close(devfd);
}

static void
btrace_reset(char * device)
{
	size_t size;
	int r, ctl, devfd;

	if ((devfd = open(device, O_RDONLY)) < 0) {
		perror("device open");
		exit(EXIT_FAILURE);
	}

	ctl = BTCTL_STOP;
	(void)ioctl(devfd, BIOCTRACECTL, &ctl);

	size = 0;
	if ((r = ioctl(devfd, BIOCTRACEBUF, &size)) < 0) {
		perror("ioctl(BIOCTRACEBUF)");
		exit(EXIT_FAILURE);
	}

	close(devfd);
}

static void
dump_entry(btrace_entry * entry)
{
	switch (entry->request) {
	case BTREQ_OPEN: printf("OPEN"); break;
	case BTREQ_CLOSE: printf("CLOSE"); break;
	case BTREQ_READ: printf("READ"); break;
	case BTREQ_WRITE: printf("WRITE"); break;
	case BTREQ_GATHER: printf("GATHER"); break;
	case BTREQ_SCATTER: printf("SCATTER"); break;
	case BTREQ_IOCTL: printf("IOCTL"); break;
	}

	printf(" request\n");

	switch (entry->request) {
	case BTREQ_OPEN:
		printf("- access:\t%x\n", entry->size);
		break;
	case BTREQ_READ:
	case BTREQ_WRITE:
	case BTREQ_GATHER:
	case BTREQ_SCATTER:
		printf("- position:\t%08lx%08lx\n",
		    ex64hi(entry->position), ex64lo(entry->position));
		printf("- size:\t\t%u\n", entry->size);
		printf("- flags:\t%x\n", entry->flags);
		break;
	case BTREQ_IOCTL:
		printf("- request:\t%08x\n", entry->size);
		break;
	}

	printf("- start:\t%u us\n", entry->start_time);
	printf("- finish:\t%u us\n", entry->finish_time);
	if (entry->result == BTRES_INPROGRESS)
		printf("- result:\t(in progress)\n");
	else
		printf("- result:\t%d\n", entry->result);
	printf("\n");
}

static void
btrace_dump(char * file)
{
	int i, r, infd;

	if ((infd = open(file, O_RDONLY)) < 0) {
		perror("open");
		exit(EXIT_FAILURE);
	}

	for (;;) {
		if ((r = read(infd, (char *)buf, sizeof(buf))) <= 0)
			break;

		r /= sizeof(buf[0]);

		for (i = 0; i < r; i++)
			dump_entry(&buf[i]);
	}

	if (r < 0) perror("read");

	close(infd);
}

int main(int argc, char ** argv)
{
	int num;

	setprogname(argv[0]);

	if (argc < 3) usage();

	if (!strcmp(argv[1], "start")) {
		if (argc < 4) usage();

		num = atoi(argv[3]);

		if (num <= 0) usage();

		btrace_start(argv[2], num);

	} else if (!strcmp(argv[1], "stop")) {
		if (argc < 4) usage();

		btrace_stop(argv[2], argv[3]);

	} else if (!strcmp(argv[1], "reset")) {
		btrace_reset(argv[2]);

	} else if (!strcmp(argv[1], "dump")) {
		btrace_dump(argv[2]);

	} else
		usage();

	return EXIT_SUCCESS;
}