Statistics
| Branch: | Revision:

root / hw / virtio-9p-debug.c @ 5268cecc

History | View | Annotate | Download (16.3 kB)

1
/*
2
 * Virtio 9p PDU debug
3
 *
4
 * Copyright IBM, Corp. 2010
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 */
13
#include "virtio.h"
14
#include "pc.h"
15
#include "virtio-9p.h"
16
#include "virtio-9p-debug.h"
17

    
18
#define BUG_ON(cond) assert(!(cond))
19

    
20
static FILE *llogfile;
21

    
22
static struct iovec *get_sg(V9fsPDU *pdu, int rx)
23
{
24
    if (rx) {
25
        return pdu->elem.in_sg;
26
    }
27
    return pdu->elem.out_sg;
28
}
29

    
30
static int get_sg_count(V9fsPDU *pdu, int rx)
31
{
32
    if (rx) {
33
        return pdu->elem.in_num;
34
    }
35
    return pdu->elem.out_num;
36

    
37
}
38

    
39
static void pprint_int8(V9fsPDU *pdu, int rx, size_t *offsetp,
40
                        const char *name)
41
{
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;
47

    
48
    copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
49

    
50
    BUG_ON(copied != sizeof(value));
51
    offset += sizeof(value);
52
    fprintf(llogfile, "%s=0x%x", name, value);
53
    *offsetp = offset;
54
}
55

    
56
static void pprint_int16(V9fsPDU *pdu, int rx, size_t *offsetp,
57
                        const char *name)
58
{
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;
64

    
65

    
66
    copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
67

    
68
    BUG_ON(copied != sizeof(value));
69
    offset += sizeof(value);
70
    fprintf(llogfile, "%s=0x%x", name, value);
71
    *offsetp = offset;
72
}
73

    
74
static void pprint_int32(V9fsPDU *pdu, int rx, size_t *offsetp,
75
                        const char *name)
76
{
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;
82

    
83

    
84
    copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
85

    
86
    BUG_ON(copied != sizeof(value));
87
    offset += sizeof(value);
88
    fprintf(llogfile, "%s=0x%x", name, value);
89
    *offsetp = offset;
90
}
91

    
92
static void pprint_int64(V9fsPDU *pdu, int rx, size_t *offsetp,
93
                        const char *name)
94
{
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;
100

    
101

    
102
    copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
103

    
104
    BUG_ON(copied != sizeof(value));
105
    offset += sizeof(value);
106
    fprintf(llogfile, "%s=0x%" PRIx64, name, value);
107
    *offsetp = offset;
108
}
109

    
110
static void pprint_str(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
111
{
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;
119

    
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;
125

    
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;
142
            }
143
        }
144
    }
145
    *offsetp += copied;
146
}
147

    
148
static void pprint_qid(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
149
{
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, "}");
155
}
156

    
157
static void pprint_stat(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
158
{
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");
177
    }
178
    fprintf(llogfile, "}");
179
}
180

    
181
static void pprint_stat_dotl(V9fsPDU *pdu, int rx, size_t *offsetp,
182
                                                  const char *name)
183
{
184
    fprintf(llogfile, "%s={", name);
185
    pprint_qid(pdu, rx, offsetp, "qid");
186
    pprint_int32(pdu, rx, offsetp, ", st_mode");
187
    pprint_int64(pdu, rx, offsetp, ", st_nlink");
188
    pprint_int32(pdu, rx, offsetp, ", st_uid");
189
    pprint_int32(pdu, rx, offsetp, ", st_gid");
190
    pprint_int64(pdu, rx, offsetp, ", st_rdev");
191
    pprint_int64(pdu, rx, offsetp, ", st_size");
192
    pprint_int64(pdu, rx, offsetp, ", st_blksize");
193
    pprint_int64(pdu, rx, offsetp, ", st_blocks");
194
    pprint_int64(pdu, rx, offsetp, ", atime");
195
    pprint_int64(pdu, rx, offsetp, ", atime_nsec");
196
    pprint_int64(pdu, rx, offsetp, ", mtime");
197
    pprint_int64(pdu, rx, offsetp, ", mtime_nsec");
198
    pprint_int64(pdu, rx, offsetp, ", ctime");
199
    pprint_int64(pdu, rx, offsetp, ", ctime_nsec");
200
    fprintf(llogfile, "}");
201
}
202

    
203

    
204

    
205
static void pprint_strs(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
206
{
207
    int sg_count = get_sg_count(pdu, rx);
208
    struct iovec *sg = get_sg(pdu, rx);
209
    size_t offset = *offsetp;
210
    uint16_t tmp_count, count, i;
211
    size_t copied = 0;
212

    
213
    fprintf(llogfile, "%s={", name);
214

    
215
    /* Get the count */
216
    copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
217
    BUG_ON(copied != sizeof(tmp_count));
218
    count = le16_to_cpupu(&tmp_count);
219
    offset += copied;
220

    
221
    for (i = 0; i < count; i++) {
222
        char str[512];
223
        if (i) {
224
            fprintf(llogfile, ", ");
225
        }
226
        snprintf(str, sizeof(str), "[%d]", i);
227
        pprint_str(pdu, rx, &offset, str);
228
    }
229

    
230
    fprintf(llogfile, "}");
231

    
232
    *offsetp = offset;
233
}
234

    
235
static void pprint_qids(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
236
{
237
    int sg_count = get_sg_count(pdu, rx);
238
    struct iovec *sg = get_sg(pdu, rx);
239
    size_t offset = *offsetp;
240
    uint16_t tmp_count, count, i;
241
    size_t copied = 0;
242

    
243
    fprintf(llogfile, "%s={", name);
244

    
245
    copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
246
    BUG_ON(copied != sizeof(tmp_count));
247
    count = le16_to_cpupu(&tmp_count);
248
    offset += copied;
249

    
250
    for (i = 0; i < count; i++) {
251
        char str[512];
252
        if (i) {
253
            fprintf(llogfile, ", ");
254
        }
255
        snprintf(str, sizeof(str), "[%d]", i);
256
        pprint_qid(pdu, rx, &offset, str);
257
    }
258

    
259
    fprintf(llogfile, "}");
260

    
261
    *offsetp = offset;
262
}
263

    
264
static void pprint_sg(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
265
{
266
    struct iovec *sg = get_sg(pdu, rx);
267
    unsigned int count;
268
    int i;
269

    
270
    if (rx) {
271
        count = pdu->elem.in_num;
272
    } else {
273
        count = pdu->elem.out_num;
274
    }
275

    
276
    fprintf(llogfile, "%s={", name);
277
    for (i = 0; i < count; i++) {
278
        if (i) {
279
            fprintf(llogfile, ", ");
280
        }
281
        fprintf(llogfile, "(%p, 0x%zx)", sg[i].iov_base, sg[i].iov_len);
282
    }
283
    fprintf(llogfile, "}");
284
}
285

    
286
/* FIXME: read from a directory fid returns serialized stat_t's */
287
#ifdef DEBUG_DATA
288
static void pprint_data(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
289
{
290
    struct iovec *sg = get_sg(pdu, rx);
291
    size_t offset = *offsetp;
292
    unsigned int count;
293
    int32_t size;
294
    int total, i, j;
295
    ssize_t len;
296

    
297
    if (rx) {
298
        count = pdu->elem.in_num;
299
    } else
300
        count = pdu->elem.out_num;
301
    }
302

    
303
    BUG_ON((offset + sizeof(size)) > sg[0].iov_len);
304

    
305
    memcpy(&size, sg[0].iov_base + offset, sizeof(size));
306
    offset += sizeof(size);
307

    
308
    fprintf(llogfile, "size: %x\n", size);
309

    
310
    sg[0].iov_base += 11; /* skip header */
311
    sg[0].iov_len -= 11;
312

    
313
    total = 0;
314
    for (i = 0; i < count; i++) {
315
        total += sg[i].iov_len;
316
        if (total >= size) {
317
            /* trim sg list so writev does the right thing */
318
            sg[i].iov_len -= (total - size);
319
            i++;
320
            break;
321
        }
322
    }
323

    
324
    fprintf(llogfile, "%s={\"", name);
325
    fflush(llogfile);
326
    for (j = 0; j < i; j++) {
327
        if (j) {
328
            fprintf(llogfile, "\", \"");
329
            fflush(llogfile);
330
        }
331

    
332
        do {
333
            len = writev(fileno(llogfile), &sg[j], 1);
334
        } while (len == -1 && errno == EINTR);
335
        fprintf(llogfile, "len == %ld: %m\n", len);
336
        BUG_ON(len != sg[j].iov_len);
337
    }
338
    fprintf(llogfile, "\"}");
339

    
340
    sg[0].iov_base -= 11;
341
    sg[0].iov_len += 11;
342

    
343
}
344
#endif
345

    
346
void pprint_pdu(V9fsPDU *pdu)
347
{
348
    size_t offset = 7;
349

    
350
    if (llogfile == NULL) {
351
        llogfile = fopen("/tmp/pdu.log", "w");
352
    }
353

    
354
    BUG_ON(!llogfile);
355

    
356
    switch (pdu->id) {
357
    case P9_TREADDIR:
358
        fprintf(llogfile, "TREADDIR: (");
359
        pprint_int32(pdu, 0, &offset, "fid");
360
        pprint_int64(pdu, 0, &offset, ", initial offset");
361
        pprint_int32(pdu, 0, &offset, ", max count");
362
        break;
363
    case P9_RREADDIR:
364
        fprintf(llogfile, "RREADDIR: (");
365
        pprint_int32(pdu, 1, &offset, "count");
366
#ifdef DEBUG_DATA
367
        pprint_data(pdu, 1, &offset, ", data");
368
#endif
369
        break;
370
    case P9_TVERSION:
371
        fprintf(llogfile, "TVERSION: (");
372
        pprint_int32(pdu, 0, &offset, "msize");
373
        pprint_str(pdu, 0, &offset, ", version");
374
        break;
375
    case P9_RVERSION:
376
        fprintf(llogfile, "RVERSION: (");
377
        pprint_int32(pdu, 1, &offset, "msize");
378
        pprint_str(pdu, 1, &offset, ", version");
379
        break;
380
    case P9_TGETATTR:
381
        fprintf(llogfile, "TGETATTR: (");
382
        pprint_int32(pdu, 0, &offset, "fid");
383
        break;
384
    case P9_RGETATTR:
385
        fprintf(llogfile, "RGETATTR: (");
386
        pprint_stat_dotl(pdu, 1, &offset, "getattr");
387
        break;
388
    case P9_TAUTH:
389
        fprintf(llogfile, "TAUTH: (");
390
        pprint_int32(pdu, 0, &offset, "afid");
391
        pprint_str(pdu, 0, &offset, ", uname");
392
        pprint_str(pdu, 0, &offset, ", aname");
393
        if (dotu) {
394
            pprint_int32(pdu, 0, &offset, ", n_uname");
395
        }
396
        break;
397
    case P9_RAUTH:
398
        fprintf(llogfile, "RAUTH: (");
399
        pprint_qid(pdu, 1, &offset, "qid");
400
        break;
401
    case P9_TATTACH:
402
        fprintf(llogfile, "TATTACH: (");
403
        pprint_int32(pdu, 0, &offset, "fid");
404
        pprint_int32(pdu, 0, &offset, ", afid");
405
        pprint_str(pdu, 0, &offset, ", uname");
406
        pprint_str(pdu, 0, &offset, ", aname");
407
        if (dotu) {
408
            pprint_int32(pdu, 0, &offset, ", n_uname");
409
        }
410
        break;
411
    case P9_RATTACH:
412
        fprintf(llogfile, "RATTACH: (");
413
        pprint_qid(pdu, 1, &offset, "qid");
414
        break;
415
    case P9_TERROR:
416
        fprintf(llogfile, "TERROR: (");
417
        break;
418
    case P9_RERROR:
419
        fprintf(llogfile, "RERROR: (");
420
        pprint_str(pdu, 1, &offset, "ename");
421
        if (dotu) {
422
            pprint_int32(pdu, 1, &offset, ", ecode");
423
        }
424
        break;
425
    case P9_TFLUSH:
426
        fprintf(llogfile, "TFLUSH: (");
427
        pprint_int16(pdu, 0, &offset, "oldtag");
428
        break;
429
    case P9_RFLUSH:
430
        fprintf(llogfile, "RFLUSH: (");
431
        break;
432
    case P9_TWALK:
433
        fprintf(llogfile, "TWALK: (");
434
        pprint_int32(pdu, 0, &offset, "fid");
435
        pprint_int32(pdu, 0, &offset, ", newfid");
436
        pprint_strs(pdu, 0, &offset, ", wnames");
437
        break;
438
    case P9_RWALK:
439
        fprintf(llogfile, "RWALK: (");
440
        pprint_qids(pdu, 1, &offset, "wqids");
441
        break;
442
    case P9_TOPEN:
443
        fprintf(llogfile, "TOPEN: (");
444
        pprint_int32(pdu, 0, &offset, "fid");
445
        pprint_int8(pdu, 0, &offset, ", mode");
446
        break;
447
    case P9_ROPEN:
448
        fprintf(llogfile, "ROPEN: (");
449
        pprint_qid(pdu, 1, &offset, "qid");
450
        pprint_int32(pdu, 1, &offset, ", iounit");
451
        break;
452
    case P9_TCREATE:
453
        fprintf(llogfile, "TCREATE: (");
454
        pprint_int32(pdu, 0, &offset, "fid");
455
        pprint_str(pdu, 0, &offset, ", name");
456
        pprint_int32(pdu, 0, &offset, ", perm");
457
        pprint_int8(pdu, 0, &offset, ", mode");
458
        if (dotu) {
459
            pprint_str(pdu, 0, &offset, ", extension");
460
        }
461
        break;
462
    case P9_RCREATE:
463
        fprintf(llogfile, "RCREATE: (");
464
        pprint_qid(pdu, 1, &offset, "qid");
465
        pprint_int32(pdu, 1, &offset, ", iounit");
466
        break;
467
    case P9_TSYMLINK:
468
        fprintf(llogfile, "TSYMLINK: (");
469
        pprint_int32(pdu, 0, &offset, "fid");
470
        pprint_str(pdu, 0, &offset, ", name");
471
        pprint_str(pdu, 0, &offset, ", symname");
472
        pprint_int32(pdu, 0, &offset, ", gid");
473
        break;
474
    case P9_RSYMLINK:
475
        fprintf(llogfile, "RSYMLINK: (");
476
        pprint_qid(pdu, 1, &offset, "qid");
477
        break;
478
    case P9_TLCREATE:
479
        fprintf(llogfile, "TLCREATE: (");
480
        pprint_int32(pdu, 0, &offset, "dfid");
481
        pprint_str(pdu, 0, &offset, ", name");
482
        pprint_int32(pdu, 0, &offset, ", flags");
483
        pprint_int32(pdu, 0, &offset, ", mode");
484
        pprint_int32(pdu, 0, &offset, ", gid");
485
        break;
486
    case P9_RLCREATE:
487
        fprintf(llogfile, "RLCREATE: (");
488
        pprint_qid(pdu, 1, &offset, "qid");
489
        pprint_int32(pdu, 1, &offset, ", iounit");
490
        break;
491
    case P9_TMKNOD:
492
        fprintf(llogfile, "TMKNOD: (");
493
        pprint_int32(pdu, 0, &offset, "fid");
494
        pprint_str(pdu, 0, &offset, "name");
495
        pprint_int32(pdu, 0, &offset, "mode");
496
        pprint_int32(pdu, 0, &offset, "major");
497
        pprint_int32(pdu, 0, &offset, "minor");
498
        pprint_int32(pdu, 0, &offset, "gid");
499
        break;
500
    case P9_RMKNOD:
501
        fprintf(llogfile, "RMKNOD: )");
502
        pprint_qid(pdu, 0, &offset, "qid");
503
        break;
504
    case P9_TREAD:
505
        fprintf(llogfile, "TREAD: (");
506
        pprint_int32(pdu, 0, &offset, "fid");
507
        pprint_int64(pdu, 0, &offset, ", offset");
508
        pprint_int32(pdu, 0, &offset, ", count");
509
        pprint_sg(pdu, 0, &offset, ", sg");
510
        break;
511
    case P9_RREAD:
512
        fprintf(llogfile, "RREAD: (");
513
        pprint_int32(pdu, 1, &offset, "count");
514
        pprint_sg(pdu, 1, &offset, ", sg");
515
        offset = 7;
516
#ifdef DEBUG_DATA
517
        pprint_data(pdu, 1, &offset, ", data");
518
#endif
519
        break;
520
    case P9_TWRITE:
521
        fprintf(llogfile, "TWRITE: (");
522
        pprint_int32(pdu, 0, &offset, "fid");
523
        pprint_int64(pdu, 0, &offset, ", offset");
524
        pprint_int32(pdu, 0, &offset, ", count");
525
        break;
526
    case P9_RWRITE:
527
        fprintf(llogfile, "RWRITE: (");
528
        pprint_int32(pdu, 1, &offset, "count");
529
        break;
530
    case P9_TCLUNK:
531
        fprintf(llogfile, "TCLUNK: (");
532
        pprint_int32(pdu, 0, &offset, "fid");
533
        break;
534
    case P9_RCLUNK:
535
        fprintf(llogfile, "RCLUNK: (");
536
        break;
537
    case P9_TLINK:
538
        fprintf(llogfile, "TLINK: (");
539
        pprint_int32(pdu, 0, &offset, "fid");
540
        pprint_str(pdu, 0, &offset, ", oldpath");
541
        pprint_str(pdu, 0, &offset, ", newpath");
542
        break;
543
    case P9_RLINK:
544
        fprintf(llogfile, "RLINK: (");
545
        break;
546
    case P9_TREMOVE:
547
        fprintf(llogfile, "TREMOVE: (");
548
        pprint_int32(pdu, 0, &offset, "fid");
549
        break;
550
    case P9_RREMOVE:
551
        fprintf(llogfile, "RREMOVE: (");
552
        break;
553
    case P9_TSTAT:
554
        fprintf(llogfile, "TSTAT: (");
555
        pprint_int32(pdu, 0, &offset, "fid");
556
        break;
557
    case P9_RSTAT:
558
        fprintf(llogfile, "RSTAT: (");
559
        offset += 2; /* ignored */
560
        pprint_stat(pdu, 1, &offset, "stat");
561
        break;
562
    case P9_TWSTAT:
563
        fprintf(llogfile, "TWSTAT: (");
564
        pprint_int32(pdu, 0, &offset, "fid");
565
        offset += 2; /* ignored */
566
        pprint_stat(pdu, 0, &offset, ", stat");
567
        break;
568
    case P9_RWSTAT:
569
        fprintf(llogfile, "RWSTAT: (");
570
        break;
571
    default:
572
        fprintf(llogfile, "unknown(%d): (", pdu->id);
573
        break;
574
    }
575

    
576
    fprintf(llogfile, ")\n");
577
    /* Flush the log message out */
578
    fflush(llogfile);
579
}