block: Change bdrv_eject() not to drop the image
[qemu/stefanha.git] / hw / virtio-9p-debug.c
blobc1b0e6f066ca6999127f9189cbb0c7e47749f209
1 /*
2 * Virtio 9p PDU debug
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include "virtio.h"
14 #include "pc.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-debug.h"
18 #define BUG_ON(cond) assert(!(cond))
20 static FILE *llogfile;
22 static struct iovec *get_sg(V9fsPDU *pdu, int rx)
24 if (rx) {
25 return pdu->elem.in_sg;
27 return pdu->elem.out_sg;
30 static int get_sg_count(V9fsPDU *pdu, int rx)
32 if (rx) {
33 return pdu->elem.in_num;
35 return pdu->elem.out_num;
39 static void pprint_int8(V9fsPDU *pdu, int rx, size_t *offsetp,
40 const char *name)
42 size_t copied;
43 int count = get_sg_count(pdu, rx);
44 size_t offset = *offsetp;
45 struct iovec *sg = get_sg(pdu, rx);
46 int8_t value;
48 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
50 BUG_ON(copied != sizeof(value));
51 offset += sizeof(value);
52 fprintf(llogfile, "%s=0x%x", name, value);
53 *offsetp = offset;
56 static void pprint_int16(V9fsPDU *pdu, int rx, size_t *offsetp,
57 const char *name)
59 size_t copied;
60 int count = get_sg_count(pdu, rx);
61 struct iovec *sg = get_sg(pdu, rx);
62 size_t offset = *offsetp;
63 int16_t value;
66 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
68 BUG_ON(copied != sizeof(value));
69 offset += sizeof(value);
70 fprintf(llogfile, "%s=0x%x", name, value);
71 *offsetp = offset;
74 static void pprint_int32(V9fsPDU *pdu, int rx, size_t *offsetp,
75 const char *name)
77 size_t copied;
78 int count = get_sg_count(pdu, rx);
79 struct iovec *sg = get_sg(pdu, rx);
80 size_t offset = *offsetp;
81 int32_t value;
84 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
86 BUG_ON(copied != sizeof(value));
87 offset += sizeof(value);
88 fprintf(llogfile, "%s=0x%x", name, value);
89 *offsetp = offset;
92 static void pprint_int64(V9fsPDU *pdu, int rx, size_t *offsetp,
93 const char *name)
95 size_t copied;
96 int count = get_sg_count(pdu, rx);
97 struct iovec *sg = get_sg(pdu, rx);
98 size_t offset = *offsetp;
99 int64_t value;
102 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
104 BUG_ON(copied != sizeof(value));
105 offset += sizeof(value);
106 fprintf(llogfile, "%s=0x%" PRIx64, name, value);
107 *offsetp = offset;
110 static void pprint_str(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
112 int sg_count = get_sg_count(pdu, rx);
113 struct iovec *sg = get_sg(pdu, rx);
114 size_t offset = *offsetp;
115 uint16_t tmp_size, size;
116 size_t result;
117 size_t copied = 0;
118 int i = 0;
120 /* get the size */
121 copied = do_pdu_unpack(&tmp_size, sg, sg_count, offset, sizeof(tmp_size));
122 BUG_ON(copied != sizeof(tmp_size));
123 size = le16_to_cpupu(&tmp_size);
124 offset += copied;
126 fprintf(llogfile, "%s=", name);
127 for (i = 0; size && i < sg_count; i++) {
128 size_t len;
129 if (offset >= sg[i].iov_len) {
130 /* skip this sg */
131 offset -= sg[i].iov_len;
132 continue;
133 } else {
134 len = MIN(sg[i].iov_len - offset, size);
135 result = fwrite(sg[i].iov_base + offset, 1, len, llogfile);
136 BUG_ON(result != len);
137 size -= len;
138 copied += len;
139 if (size) {
140 offset = 0;
141 continue;
145 *offsetp += copied;
148 static void pprint_qid(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
150 fprintf(llogfile, "%s={", name);
151 pprint_int8(pdu, rx, offsetp, "type");
152 pprint_int32(pdu, rx, offsetp, ", version");
153 pprint_int64(pdu, rx, offsetp, ", path");
154 fprintf(llogfile, "}");
157 static void pprint_stat(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
159 fprintf(llogfile, "%s={", name);
160 pprint_int16(pdu, rx, offsetp, "size");
161 pprint_int16(pdu, rx, offsetp, ", type");
162 pprint_int32(pdu, rx, offsetp, ", dev");
163 pprint_qid(pdu, rx, offsetp, ", qid");
164 pprint_int32(pdu, rx, offsetp, ", mode");
165 pprint_int32(pdu, rx, offsetp, ", atime");
166 pprint_int32(pdu, rx, offsetp, ", mtime");
167 pprint_int64(pdu, rx, offsetp, ", length");
168 pprint_str(pdu, rx, offsetp, ", name");
169 pprint_str(pdu, rx, offsetp, ", uid");
170 pprint_str(pdu, rx, offsetp, ", gid");
171 pprint_str(pdu, rx, offsetp, ", muid");
172 if (dotu) {
173 pprint_str(pdu, rx, offsetp, ", extension");
174 pprint_int32(pdu, rx, offsetp, ", uid");
175 pprint_int32(pdu, rx, offsetp, ", gid");
176 pprint_int32(pdu, rx, offsetp, ", muid");
178 fprintf(llogfile, "}");
181 static void pprint_strs(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
183 int sg_count = get_sg_count(pdu, rx);
184 struct iovec *sg = get_sg(pdu, rx);
185 size_t offset = *offsetp;
186 uint16_t tmp_count, count, i;
187 size_t copied = 0;
189 fprintf(llogfile, "%s={", name);
191 /* Get the count */
192 copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
193 BUG_ON(copied != sizeof(tmp_count));
194 count = le16_to_cpupu(&tmp_count);
195 offset += copied;
197 for (i = 0; i < count; i++) {
198 char str[512];
199 if (i) {
200 fprintf(llogfile, ", ");
202 snprintf(str, sizeof(str), "[%d]", i);
203 pprint_str(pdu, rx, &offset, str);
206 fprintf(llogfile, "}");
208 *offsetp = offset;
211 static void pprint_qids(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
213 int sg_count = get_sg_count(pdu, rx);
214 struct iovec *sg = get_sg(pdu, rx);
215 size_t offset = *offsetp;
216 uint16_t tmp_count, count, i;
217 size_t copied = 0;
219 fprintf(llogfile, "%s={", name);
221 copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
222 BUG_ON(copied != sizeof(tmp_count));
223 count = le16_to_cpupu(&tmp_count);
224 offset += copied;
226 for (i = 0; i < count; i++) {
227 char str[512];
228 if (i) {
229 fprintf(llogfile, ", ");
231 snprintf(str, sizeof(str), "[%d]", i);
232 pprint_qid(pdu, rx, &offset, str);
235 fprintf(llogfile, "}");
237 *offsetp = offset;
240 static void pprint_sg(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
242 struct iovec *sg = get_sg(pdu, rx);
243 unsigned int count;
244 int i;
246 if (rx) {
247 count = pdu->elem.in_num;
248 } else {
249 count = pdu->elem.out_num;
252 fprintf(llogfile, "%s={", name);
253 for (i = 0; i < count; i++) {
254 if (i) {
255 fprintf(llogfile, ", ");
257 fprintf(llogfile, "(%p, 0x%zx)", sg[i].iov_base, sg[i].iov_len);
259 fprintf(llogfile, "}");
262 /* FIXME: read from a directory fid returns serialized stat_t's */
263 #ifdef DEBUG_DATA
264 static void pprint_data(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
266 struct iovec *sg = get_sg(pdu, rx);
267 size_t offset = *offsetp;
268 unsigned int count;
269 int32_t size;
270 int total, i, j;
271 ssize_t len;
273 if (rx) {
274 count = pdu->elem.in_num;
275 } else
276 count = pdu->elem.out_num;
279 BUG_ON((offset + sizeof(size)) > sg[0].iov_len);
281 memcpy(&size, sg[0].iov_base + offset, sizeof(size));
282 offset += sizeof(size);
284 fprintf(llogfile, "size: %x\n", size);
286 sg[0].iov_base += 11; /* skip header */
287 sg[0].iov_len -= 11;
289 total = 0;
290 for (i = 0; i < count; i++) {
291 total += sg[i].iov_len;
292 if (total >= size) {
293 /* trim sg list so writev does the right thing */
294 sg[i].iov_len -= (total - size);
295 i++;
296 break;
300 fprintf(llogfile, "%s={\"", name);
301 fflush(llogfile);
302 for (j = 0; j < i; j++) {
303 if (j) {
304 fprintf(llogfile, "\", \"");
305 fflush(llogfile);
308 do {
309 len = writev(fileno(llogfile), &sg[j], 1);
310 } while (len == -1 && errno == EINTR);
311 fprintf(llogfile, "len == %ld: %m\n", len);
312 BUG_ON(len != sg[j].iov_len);
314 fprintf(llogfile, "\"}");
316 sg[0].iov_base -= 11;
317 sg[0].iov_len += 11;
320 #endif
322 void pprint_pdu(V9fsPDU *pdu)
324 size_t offset = 7;
326 if (llogfile == NULL) {
327 llogfile = fopen("/tmp/pdu.log", "w");
330 BUG_ON(!llogfile);
332 switch (pdu->id) {
333 case P9_TVERSION:
334 fprintf(llogfile, "TVERSION: (");
335 pprint_int32(pdu, 0, &offset, "msize");
336 pprint_str(pdu, 0, &offset, ", version");
337 break;
338 case P9_RVERSION:
339 fprintf(llogfile, "RVERSION: (");
340 pprint_int32(pdu, 1, &offset, "msize");
341 pprint_str(pdu, 1, &offset, ", version");
342 break;
343 case P9_TAUTH:
344 fprintf(llogfile, "TAUTH: (");
345 pprint_int32(pdu, 0, &offset, "afid");
346 pprint_str(pdu, 0, &offset, ", uname");
347 pprint_str(pdu, 0, &offset, ", aname");
348 if (dotu) {
349 pprint_int32(pdu, 0, &offset, ", n_uname");
351 break;
352 case P9_RAUTH:
353 fprintf(llogfile, "RAUTH: (");
354 pprint_qid(pdu, 1, &offset, "qid");
355 break;
356 case P9_TATTACH:
357 fprintf(llogfile, "TATTACH: (");
358 pprint_int32(pdu, 0, &offset, "fid");
359 pprint_int32(pdu, 0, &offset, ", afid");
360 pprint_str(pdu, 0, &offset, ", uname");
361 pprint_str(pdu, 0, &offset, ", aname");
362 if (dotu) {
363 pprint_int32(pdu, 0, &offset, ", n_uname");
365 break;
366 case P9_RATTACH:
367 fprintf(llogfile, "RATTACH: (");
368 pprint_qid(pdu, 1, &offset, "qid");
369 break;
370 case P9_TERROR:
371 fprintf(llogfile, "TERROR: (");
372 break;
373 case P9_RERROR:
374 fprintf(llogfile, "RERROR: (");
375 pprint_str(pdu, 1, &offset, "ename");
376 if (dotu) {
377 pprint_int32(pdu, 1, &offset, ", ecode");
379 break;
380 case P9_TFLUSH:
381 fprintf(llogfile, "TFLUSH: (");
382 pprint_int16(pdu, 0, &offset, "oldtag");
383 break;
384 case P9_RFLUSH:
385 fprintf(llogfile, "RFLUSH: (");
386 break;
387 case P9_TWALK:
388 fprintf(llogfile, "TWALK: (");
389 pprint_int32(pdu, 0, &offset, "fid");
390 pprint_int32(pdu, 0, &offset, ", newfid");
391 pprint_strs(pdu, 0, &offset, ", wnames");
392 break;
393 case P9_RWALK:
394 fprintf(llogfile, "RWALK: (");
395 pprint_qids(pdu, 1, &offset, "wqids");
396 break;
397 case P9_TOPEN:
398 fprintf(llogfile, "TOPEN: (");
399 pprint_int32(pdu, 0, &offset, "fid");
400 pprint_int8(pdu, 0, &offset, ", mode");
401 break;
402 case P9_ROPEN:
403 fprintf(llogfile, "ROPEN: (");
404 pprint_qid(pdu, 1, &offset, "qid");
405 pprint_int32(pdu, 1, &offset, ", iounit");
406 break;
407 case P9_TCREATE:
408 fprintf(llogfile, "TCREATE: (");
409 pprint_int32(pdu, 0, &offset, "fid");
410 pprint_str(pdu, 0, &offset, ", name");
411 pprint_int32(pdu, 0, &offset, ", perm");
412 pprint_int8(pdu, 0, &offset, ", mode");
413 if (dotu) {
414 pprint_str(pdu, 0, &offset, ", extension");
416 break;
417 case P9_RCREATE:
418 fprintf(llogfile, "RCREATE: (");
419 pprint_qid(pdu, 1, &offset, "qid");
420 pprint_int32(pdu, 1, &offset, ", iounit");
421 break;
422 case P9_TREAD:
423 fprintf(llogfile, "TREAD: (");
424 pprint_int32(pdu, 0, &offset, "fid");
425 pprint_int64(pdu, 0, &offset, ", offset");
426 pprint_int32(pdu, 0, &offset, ", count");
427 pprint_sg(pdu, 0, &offset, ", sg");
428 break;
429 case P9_RREAD:
430 fprintf(llogfile, "RREAD: (");
431 pprint_int32(pdu, 1, &offset, "count");
432 pprint_sg(pdu, 1, &offset, ", sg");
433 offset = 7;
434 #ifdef DEBUG_DATA
435 pprint_data(pdu, 1, &offset, ", data");
436 #endif
437 break;
438 case P9_TWRITE:
439 fprintf(llogfile, "TWRITE: (");
440 pprint_int32(pdu, 0, &offset, "fid");
441 pprint_int64(pdu, 0, &offset, ", offset");
442 pprint_int32(pdu, 0, &offset, ", count");
443 break;
444 case P9_RWRITE:
445 fprintf(llogfile, "RWRITE: (");
446 pprint_int32(pdu, 1, &offset, "count");
447 break;
448 case P9_TCLUNK:
449 fprintf(llogfile, "TCLUNK: (");
450 pprint_int32(pdu, 0, &offset, "fid");
451 break;
452 case P9_RCLUNK:
453 fprintf(llogfile, "RCLUNK: (");
454 break;
455 case P9_TREMOVE:
456 fprintf(llogfile, "TREMOVE: (");
457 pprint_int32(pdu, 0, &offset, "fid");
458 break;
459 case P9_RREMOVE:
460 fprintf(llogfile, "RREMOVE: (");
461 break;
462 case P9_TSTAT:
463 fprintf(llogfile, "TSTAT: (");
464 pprint_int32(pdu, 0, &offset, "fid");
465 break;
466 case P9_RSTAT:
467 fprintf(llogfile, "RSTAT: (");
468 offset += 2; /* ignored */
469 pprint_stat(pdu, 1, &offset, "stat");
470 break;
471 case P9_TWSTAT:
472 fprintf(llogfile, "TWSTAT: (");
473 pprint_int32(pdu, 0, &offset, "fid");
474 offset += 2; /* ignored */
475 pprint_stat(pdu, 0, &offset, ", stat");
476 break;
477 case P9_RWSTAT:
478 fprintf(llogfile, "RWSTAT: (");
479 break;
480 default:
481 fprintf(llogfile, "unknown(%d): (", pdu->id);
482 break;
485 fprintf(llogfile, ")\n");
486 /* Flush the log message out */
487 fflush(llogfile);