1 /* Block trace command line tool */
7 #include <minix/types.h>
8 #include <minix/btrace.h>
10 #include <sys/ioc_block.h>
12 static btrace_entry buf
[BTBUF_SIZE
];
14 static void usage(char *name
)
17 "%s start <device> <nr_entries>\n"
18 "%s stop <device> <file>\n"
21 name
, name
, name
, name
);
26 static void btrace_start(char *device
, int nr_entries
)
31 if ((devfd
= open(device
, O_RDONLY
)) < 0) {
32 perror("device open");
37 if ((r
= ioctl(devfd
, BIOCTRACEBUF
, &size
)) < 0) {
38 perror("ioctl(BIOCTRACEBUF)");
43 if ((r
= ioctl(devfd
, BIOCTRACECTL
, &ctl
)) < 0) {
44 perror("ioctl(BIOCTRACECTL)");
47 (void) ioctl(devfd
, BIOCTRACEBUF
, &size
);
55 static void btrace_stop(char *device
, char *file
)
57 int r
, ctl
, devfd
, outfd
;
60 if ((devfd
= open(device
, O_RDONLY
)) < 0) {
61 perror("device open");
65 if ((outfd
= open(file
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) < 0) {
71 if ((r
= ioctl(devfd
, BIOCTRACECTL
, &ctl
)) < 0) {
72 perror("ioctl(BIOCTRACECTL)");
77 if ((r
= ioctl(devfd
, BIOCTRACEGET
, buf
)) < 0) {
78 perror("ioctl(BIOCTRACEGET)");
84 size
= r
* sizeof(buf
[0]);
85 if ((r
= write(outfd
, (char *) buf
, size
)) != size
) {
86 if (r
< 0) perror("write");
87 else fputs("short write\n", stderr
);
94 if ((r
= ioctl(devfd
, BIOCTRACEBUF
, &size
)) < 0) {
95 perror("ioctl(BIOCTRACEBUF)");
102 static void btrace_reset(char *device
)
107 if ((devfd
= open(device
, O_RDONLY
)) < 0) {
108 perror("device open");
113 (void) ioctl(devfd
, BIOCTRACECTL
, &ctl
);
116 if ((r
= ioctl(devfd
, BIOCTRACEBUF
, &size
)) < 0) {
117 perror("ioctl(BIOCTRACEBUF)");
124 static void dump_entry(btrace_entry
*entry
)
126 switch (entry
->request
) {
127 case BTREQ_OPEN
: printf("OPEN"); break;
128 case BTREQ_CLOSE
: printf("CLOSE"); break;
129 case BTREQ_READ
: printf("READ"); break;
130 case BTREQ_WRITE
: printf("WRITE"); break;
131 case BTREQ_GATHER
: printf("GATHER"); break;
132 case BTREQ_SCATTER
: printf("SCATTER"); break;
133 case BTREQ_IOCTL
: printf("IOCTL"); break;
136 printf(" request\n");
138 switch (entry
->request
) {
140 printf("- access:\t%x\n", entry
->size
);
146 printf("- position:\t%08lx%08lx\n",
147 ex64hi(entry
->position
), ex64lo(entry
->position
));
148 printf("- size:\t\t%u\n", entry
->size
);
149 printf("- flags:\t%x\n", entry
->flags
);
152 printf("- request:\t%08x\n", entry
->size
);
156 printf("- start:\t%u us\n", entry
->start_time
);
157 printf("- finish:\t%u us\n", entry
->finish_time
);
158 if (entry
->result
== BTRES_INPROGRESS
)
159 printf("- result:\t(in progress)\n");
161 printf("- result:\t%d\n", entry
->result
);
165 static void btrace_dump(char *file
)
169 if ((infd
= open(file
, O_RDONLY
)) < 0) {
175 if ((r
= read(infd
, (char *) buf
, sizeof(buf
))) <= 0)
180 for (i
= 0; i
< r
; i
++)
184 if (r
< 0) perror("read");
189 int main(int argc
, char **argv
)
192 char *name
= argv
[0];
194 if (argc
< 3) usage(name
);
196 if (!strcmp(argv
[1], "start")) {
197 if (argc
< 4) usage(name
);
201 if (num
<= 0) usage(name
);
203 btrace_start(argv
[2], num
);
205 else if (!strcmp(argv
[1], "stop")) {
206 if (argc
< 4) usage(name
);
208 btrace_stop(argv
[2], argv
[3]);
210 else if (!strcmp(argv
[1], "reset")) {
211 btrace_reset(argv
[2]);
213 else if (!strcmp(argv
[1], "dump")) {
214 btrace_dump(argv
[2]);