virtio-9p-debug.c 15.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
/*
 * Virtio 9p PDU debug
 *
 * Copyright IBM, Corp. 2010
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */
#include "virtio.h"
#include "pc.h"
#include "virtio-9p.h"
#include "virtio-9p-debug.h"

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

static FILE *llogfile;

static struct iovec *get_sg(V9fsPDU *pdu, int rx)
{
    if (rx) {
        return pdu->elem.in_sg;
    }
    return pdu->elem.out_sg;
}

static int get_sg_count(V9fsPDU *pdu, int rx)
{
    if (rx) {
        return pdu->elem.in_num;
    }
    return pdu->elem.out_num;

}

static void pprint_int8(V9fsPDU *pdu, int rx, size_t *offsetp,
                        const char *name)
{
    size_t copied;
    int count = get_sg_count(pdu, rx);
    size_t offset = *offsetp;
    struct iovec *sg = get_sg(pdu, rx);
    int8_t value;

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

    BUG_ON(copied != sizeof(value));
    offset += sizeof(value);
    fprintf(llogfile, "%s=0x%x", name, value);
    *offsetp = offset;
}

static void pprint_int16(V9fsPDU *pdu, int rx, size_t *offsetp,
                        const char *name)
{
    size_t copied;
    int count = get_sg_count(pdu, rx);
    struct iovec *sg = get_sg(pdu, rx);
    size_t offset = *offsetp;
    int16_t value;


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

    BUG_ON(copied != sizeof(value));
    offset += sizeof(value);
    fprintf(llogfile, "%s=0x%x", name, value);
    *offsetp = offset;
}

static void pprint_int32(V9fsPDU *pdu, int rx, size_t *offsetp,
                        const char *name)
{
    size_t copied;
    int count = get_sg_count(pdu, rx);
    struct iovec *sg = get_sg(pdu, rx);
    size_t offset = *offsetp;
    int32_t value;


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

    BUG_ON(copied != sizeof(value));
    offset += sizeof(value);
    fprintf(llogfile, "%s=0x%x", name, value);
    *offsetp = offset;
}

static void pprint_int64(V9fsPDU *pdu, int rx, size_t *offsetp,
                        const char *name)
{
    size_t copied;
    int count = get_sg_count(pdu, rx);
    struct iovec *sg = get_sg(pdu, rx);
    size_t offset = *offsetp;
    int64_t value;


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

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

static void pprint_str(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
{
    int sg_count = get_sg_count(pdu, rx);
    struct iovec *sg = get_sg(pdu, rx);
    size_t offset = *offsetp;
    uint16_t tmp_size, size;
    size_t result;
    size_t copied = 0;
    int i = 0;

    /* get the size */
    copied = do_pdu_unpack(&tmp_size, sg, sg_count, offset, sizeof(tmp_size));
    BUG_ON(copied != sizeof(tmp_size));
    size = le16_to_cpupu(&tmp_size);
    offset += copied;

    fprintf(llogfile, "%s=", name);
    for (i = 0; size && i < sg_count; i++) {
        size_t len;
        if (offset >= sg[i].iov_len) {
            /* skip this sg */
            offset -= sg[i].iov_len;
            continue;
        } else {
            len = MIN(sg[i].iov_len - offset, size);
            result = fwrite(sg[i].iov_base + offset, 1, len, llogfile);
            BUG_ON(result != len);
            size -= len;
            copied += len;
            if (size) {
                offset = 0;
                continue;
            }
        }
    }
    *offsetp += copied;
}

static void pprint_qid(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
{
    fprintf(llogfile, "%s={", name);
    pprint_int8(pdu, rx, offsetp, "type");
    pprint_int32(pdu, rx, offsetp, ", version");
    pprint_int64(pdu, rx, offsetp, ", path");
    fprintf(llogfile, "}");
}

static void pprint_stat(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
{
    fprintf(llogfile, "%s={", name);
    pprint_int16(pdu, rx, offsetp, "size");
    pprint_int16(pdu, rx, offsetp, ", type");
    pprint_int32(pdu, rx, offsetp, ", dev");
    pprint_qid(pdu, rx, offsetp, ", qid");
    pprint_int32(pdu, rx, offsetp, ", mode");
    pprint_int32(pdu, rx, offsetp, ", atime");
    pprint_int32(pdu, rx, offsetp, ", mtime");
    pprint_int64(pdu, rx, offsetp, ", length");
    pprint_str(pdu, rx, offsetp, ", name");
    pprint_str(pdu, rx, offsetp, ", uid");
    pprint_str(pdu, rx, offsetp, ", gid");
    pprint_str(pdu, rx, offsetp, ", muid");
    if (dotu) {
        pprint_str(pdu, rx, offsetp, ", extension");
        pprint_int32(pdu, rx, offsetp, ", uid");
        pprint_int32(pdu, rx, offsetp, ", gid");
        pprint_int32(pdu, rx, offsetp, ", muid");
    }
    fprintf(llogfile, "}");
}

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



205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
static void pprint_strs(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
{
    int sg_count = get_sg_count(pdu, rx);
    struct iovec *sg = get_sg(pdu, rx);
    size_t offset = *offsetp;
    uint16_t tmp_count, count, i;
    size_t copied = 0;

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

    /* Get the count */
    copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
    BUG_ON(copied != sizeof(tmp_count));
    count = le16_to_cpupu(&tmp_count);
    offset += copied;

    for (i = 0; i < count; i++) {
        char str[512];
        if (i) {
            fprintf(llogfile, ", ");
        }
        snprintf(str, sizeof(str), "[%d]", i);
        pprint_str(pdu, rx, &offset, str);
    }

    fprintf(llogfile, "}");

    *offsetp = offset;
}

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

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

    copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
    BUG_ON(copied != sizeof(tmp_count));
    count = le16_to_cpupu(&tmp_count);
    offset += copied;

    for (i = 0; i < count; i++) {
        char str[512];
        if (i) {
            fprintf(llogfile, ", ");
        }
        snprintf(str, sizeof(str), "[%d]", i);
        pprint_qid(pdu, rx, &offset, str);
    }

    fprintf(llogfile, "}");

    *offsetp = offset;
}

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

    if (rx) {
        count = pdu->elem.in_num;
    } else {
        count = pdu->elem.out_num;
    }

    fprintf(llogfile, "%s={", name);
    for (i = 0; i < count; i++) {
        if (i) {
            fprintf(llogfile, ", ");
        }
        fprintf(llogfile, "(%p, 0x%zx)", sg[i].iov_base, sg[i].iov_len);
    }
    fprintf(llogfile, "}");
}

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

    if (rx) {
        count = pdu->elem.in_num;
    } else
        count = pdu->elem.out_num;
    }

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

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

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

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

    total = 0;
    for (i = 0; i < count; i++) {
        total += sg[i].iov_len;
        if (total >= size) {
            /* trim sg list so writev does the right thing */
            sg[i].iov_len -= (total - size);
            i++;
            break;
        }
    }

    fprintf(llogfile, "%s={\"", name);
    fflush(llogfile);
    for (j = 0; j < i; j++) {
        if (j) {
            fprintf(llogfile, "\", \"");
            fflush(llogfile);
        }

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

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

}
#endif

void pprint_pdu(V9fsPDU *pdu)
{
    size_t offset = 7;

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

354 355
    BUG_ON(!llogfile);

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

    fprintf(llogfile, ")\n");
540 541
    /* Flush the log message out */
    fflush(llogfile);
542
}