1 /* Block trace command line tool - by D.C. van Moolenbroek */
8 #include <minix/btrace.h>
10 #include <sys/ioctl.h>
12 static btrace_entry buf
[BTBUF_SIZE
];
17 fprintf(stderr
, "usage:\n"
18 "%s start <device> <nr_entries>\n"
19 "%s stop <device> <file>\n"
22 getprogname(), getprogname(), getprogname(), getprogname());
28 btrace_start(char * device
, int nr_entries
)
33 if ((devfd
= open(device
, O_RDONLY
)) < 0) {
34 perror("device open");
39 if ((r
= ioctl(devfd
, BIOCTRACEBUF
, &size
)) < 0) {
40 perror("ioctl(BIOCTRACEBUF)");
45 if ((r
= ioctl(devfd
, BIOCTRACECTL
, &ctl
)) < 0) {
46 perror("ioctl(BIOCTRACECTL)");
49 (void)ioctl(devfd
, BIOCTRACEBUF
, &size
);
58 btrace_stop(char * device
, char * file
)
60 int r
, ctl
, devfd
, outfd
;
63 if ((devfd
= open(device
, O_RDONLY
)) < 0) {
64 perror("device open");
68 if ((outfd
= open(file
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600)) < 0) {
74 if ((r
= ioctl(devfd
, BIOCTRACECTL
, &ctl
)) < 0) {
75 perror("ioctl(BIOCTRACECTL)");
80 if ((r
= ioctl(devfd
, BIOCTRACEGET
, buf
)) < 0) {
81 perror("ioctl(BIOCTRACEGET)");
87 size
= r
* sizeof(buf
[0]);
88 if ((r
= write(outfd
, (char *)buf
, size
)) != size
) {
89 if (r
< 0) perror("write");
90 else fputs("short write\n", stderr
);
97 if ((r
= ioctl(devfd
, BIOCTRACEBUF
, &size
)) < 0) {
98 perror("ioctl(BIOCTRACEBUF)");
106 btrace_reset(char * device
)
111 if ((devfd
= open(device
, O_RDONLY
)) < 0) {
112 perror("device open");
117 (void)ioctl(devfd
, BIOCTRACECTL
, &ctl
);
120 if ((r
= ioctl(devfd
, BIOCTRACEBUF
, &size
)) < 0) {
121 perror("ioctl(BIOCTRACEBUF)");
129 dump_entry(btrace_entry
* entry
)
131 switch (entry
->request
) {
132 case BTREQ_OPEN
: printf("OPEN"); break;
133 case BTREQ_CLOSE
: printf("CLOSE"); break;
134 case BTREQ_READ
: printf("READ"); break;
135 case BTREQ_WRITE
: printf("WRITE"); break;
136 case BTREQ_GATHER
: printf("GATHER"); break;
137 case BTREQ_SCATTER
: printf("SCATTER"); break;
138 case BTREQ_IOCTL
: printf("IOCTL"); break;
141 printf(" request\n");
143 switch (entry
->request
) {
145 printf("- access:\t%x\n", entry
->size
);
151 printf("- position:\t%08lx%08lx\n",
152 ex64hi(entry
->position
), ex64lo(entry
->position
));
153 printf("- size:\t\t%u\n", entry
->size
);
154 printf("- flags:\t%x\n", entry
->flags
);
157 printf("- request:\t%08x\n", entry
->size
);
161 printf("- start:\t%u us\n", entry
->start_time
);
162 printf("- finish:\t%u us\n", entry
->finish_time
);
163 if (entry
->result
== BTRES_INPROGRESS
)
164 printf("- result:\t(in progress)\n");
166 printf("- result:\t%d\n", entry
->result
);
171 btrace_dump(char * file
)
175 if ((infd
= open(file
, O_RDONLY
)) < 0) {
181 if ((r
= read(infd
, (char *)buf
, sizeof(buf
))) <= 0)
186 for (i
= 0; i
< r
; i
++)
190 if (r
< 0) perror("read");
195 int main(int argc
, char ** argv
)
199 setprogname(argv
[0]);
201 if (argc
< 3) usage();
203 if (!strcmp(argv
[1], "start")) {
204 if (argc
< 4) usage();
208 if (num
<= 0) usage();
210 btrace_start(argv
[2], num
);
212 } else if (!strcmp(argv
[1], "stop")) {
213 if (argc
< 4) usage();
215 btrace_stop(argv
[2], argv
[3]);
217 } else if (!strcmp(argv
[1], "reset")) {
218 btrace_reset(argv
[2]);
220 } else if (!strcmp(argv
[1], "dump")) {
221 btrace_dump(argv
[2]);