4 * Takes an nbd transaction log file on stdin and translates it into something
12 #include <sys/types.h>
17 /* We don't want to do syslog output in this program */
21 #include "nbd-helper.h"
23 #define BUFSIZE 131072
24 static char tmpbuf
[BUFSIZE
];
26 static bool g_with_datalog
= false;
28 static inline void doread(int f
, void *buf
, size_t len
) {
32 if((res
=read(f
, buf
, len
)) <=0) {
35 perror ("Error reading transactions");
43 int main(int argc
, char**argv
) {
44 struct nbd_request req
;
46 struct nbd_structured_reply srep
;
57 int readfd
= 0; /* stdin */
61 if(strcmp(argv
[1], "--help") && strcmp(argv
[1], "-h")) {
62 printf("E: unknown option %s.\n", argv
[1]);
65 printf("This is nbd-trdump, part of nbd %s.\n", PACKAGE_VERSION
);
66 printf("Use: %s < transactionlog\n", argv
[0]);
71 /* Read a request or reply from the transaction file */
72 doread(readfd
, &magic
, sizeof(magic
));
75 case NBD_REQUEST_MAGIC
:
76 doread(readfd
, sizeof(magic
)+(char *)(&req
), sizeof(struct nbd_request
)-sizeof(magic
));
77 cookie
= ntohll(req
.cookie
);
78 offset
= ntohll(req
.from
);
80 command
= ntohl(req
.type
);
82 ctext
= getcommandname(command
& NBD_CMD_MASK_COMMAND
);
84 printf("> H=%016llx C=0x%08x (%20s+%4s) O=%016llx L=%08x\n",
85 (long long unsigned int) cookie
,
88 (command
& NBD_CMD_FLAG_FUA
)?"FUA":"NONE",
89 (long long unsigned int) offset
,
91 if (((command
& NBD_CMD_MASK_COMMAND
) == NBD_CMD_WRITE
) &&
94 uint32_t tmplen
= len
;
98 doread(readfd
, tmpbuf
, tmplen
);
104 case NBD_REPLY_MAGIC
:
105 doread(readfd
, sizeof(magic
)+(char *)(&rep
), sizeof(struct nbd_reply
)-sizeof(magic
));
106 cookie
= ntohll(rep
.cookie
);
107 error
= ntohl(rep
.error
);
109 printf("< H=%016llx E=0x%08x\n",
110 (long long unsigned int) cookie
,
114 case NBD_TRACELOG_MAGIC
:
115 doread(readfd
, sizeof(magic
)+(char *)(&req
), sizeof(struct nbd_request
)-sizeof(magic
));
116 cookie
= ntohll(req
.cookie
);
117 offset
= ntohll(req
.from
);
118 len
= ntohl(req
.len
);
119 command
= ntohl(req
.type
);
121 ctext
= gettracelogname(command
);
123 printf("TRACE_OPTION C=0x%08x (%23s) O=%016llx L=%08x\n",
126 (long long unsigned int) offset
,
128 if (offset
== NBD_TRACELOG_FROM_MAGIC
) {
131 case NBD_TRACELOG_SET_DATALOG
:
132 g_with_datalog
= !!len
;
133 printf("TRACE_OPTION DATALOG set to %d.\n", (int)g_with_datalog
);
136 printf("TRACE_OPTION ? Unknown type\n");
139 printf("TRACE_OPTION ? Unknown FROM_MAGIC\n");
142 case NBD_STRUCTURED_REPLY_MAGIC
:
143 doread(readfd
, sizeof(magic
)+(char*)(&srep
), sizeof(struct nbd_structured_reply
)-sizeof(magic
));
144 cookie
= ntohll(srep
.cookie
);
145 flags
= ntohs(srep
.flags
);
146 type
= ntohs(srep
.type
);
147 paylen
= ntohs(srep
.paylen
);
148 ctext
= getstructreplname(type
);
150 printf("<S: H=%016llx T=0x%04x (%20s) F=0x%04x L=0x%04x\n",
151 (long long unsigned int) cookie
,
159 printf("? Unknown transaction type %08x\n",magic
);