qemu/tests/libqtest.c
<<
>>
Prefs
   1/*
   2 * QTest
   3 *
   4 * Copyright IBM, Corp. 2012
   5 * Copyright Red Hat, Inc. 2012
   6 * Copyright SUSE LINUX Products GmbH 2013
   7 *
   8 * Authors:
   9 *  Anthony Liguori   <aliguori@us.ibm.com>
  10 *  Paolo Bonzini     <pbonzini@redhat.com>
  11 *  Andreas Färber    <afaerber@suse.de>
  12 *
  13 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  14 * See the COPYING file in the top-level directory.
  15 *
  16 */
  17#include "qemu/osdep.h"
  18#include "libqtest.h"
  19
  20#include <glib.h>
  21#include <sys/socket.h>
  22#include <sys/wait.h>
  23#include <sys/un.h>
  24
  25#include "qapi/qmp/json-parser.h"
  26#include "qapi/qmp/json-streamer.h"
  27#include "qapi/qmp/qjson.h"
  28
  29#define MAX_IRQ 256
  30#define SOCKET_TIMEOUT 5
  31
  32QTestState *global_qtest;
  33
  34struct QTestState
  35{
  36    int fd;
  37    int qmp_fd;
  38    bool irq_level[MAX_IRQ];
  39    GString *rx;
  40    pid_t qemu_pid;  /* our child QEMU process */
  41};
  42
  43static GHookList abrt_hooks;
  44static GList *qtest_instances;
  45static struct sigaction sigact_old;
  46
  47#define g_assert_no_errno(ret) do { \
  48    g_assert_cmpint(ret, !=, -1); \
  49} while (0)
  50
  51static int init_socket(const char *socket_path)
  52{
  53    struct sockaddr_un addr;
  54    int sock;
  55    int ret;
  56
  57    sock = socket(PF_UNIX, SOCK_STREAM, 0);
  58    g_assert_no_errno(sock);
  59
  60    addr.sun_family = AF_UNIX;
  61    snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
  62    qemu_set_cloexec(sock);
  63
  64    do {
  65        ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
  66    } while (ret == -1 && errno == EINTR);
  67    g_assert_no_errno(ret);
  68    ret = listen(sock, 1);
  69    g_assert_no_errno(ret);
  70
  71    return sock;
  72}
  73
  74static int socket_accept(int sock)
  75{
  76    struct sockaddr_un addr;
  77    socklen_t addrlen;
  78    int ret;
  79    struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
  80                               .tv_usec = 0 };
  81
  82    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
  83               sizeof(timeout));
  84
  85    do {
  86        addrlen = sizeof(addr);
  87        ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
  88    } while (ret == -1 && errno == EINTR);
  89    if (ret == -1) {
  90        fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
  91    }
  92    close(sock);
  93
  94    return ret;
  95}
  96
  97static void kill_qemu(QTestState *s)
  98{
  99    if (s->qemu_pid != -1) {
 100        kill(s->qemu_pid, SIGTERM);
 101        waitpid(s->qemu_pid, NULL, 0);
 102    }
 103}
 104
 105static void kill_qemu_hook_func(void *s)
 106{
 107    kill_qemu(s);
 108}
 109
 110static void sigabrt_handler(int signo)
 111{
 112    g_hook_list_invoke(&abrt_hooks, FALSE);
 113}
 114
 115static void setup_sigabrt_handler(void)
 116{
 117    struct sigaction sigact;
 118
 119    /* Catch SIGABRT to clean up on g_assert() failure */
 120    sigact = (struct sigaction){
 121        .sa_handler = sigabrt_handler,
 122        .sa_flags = SA_RESETHAND,
 123    };
 124    sigemptyset(&sigact.sa_mask);
 125    sigaction(SIGABRT, &sigact, &sigact_old);
 126}
 127
 128static void cleanup_sigabrt_handler(void)
 129{
 130    sigaction(SIGABRT, &sigact_old, NULL);
 131}
 132
 133void qtest_add_abrt_handler(GHookFunc fn, const void *data)
 134{
 135    GHook *hook;
 136
 137    /* Only install SIGABRT handler once */
 138    if (!abrt_hooks.is_setup) {
 139        g_hook_list_init(&abrt_hooks, sizeof(GHook));
 140        setup_sigabrt_handler();
 141    }
 142
 143    hook = g_hook_alloc(&abrt_hooks);
 144    hook->func = fn;
 145    hook->data = (void *)data;
 146
 147    g_hook_prepend(&abrt_hooks, hook);
 148}
 149
 150QTestState *qtest_init(const char *extra_args)
 151{
 152    QTestState *s;
 153    int sock, qmpsock, i;
 154    gchar *socket_path;
 155    gchar *qmp_socket_path;
 156    gchar *command;
 157    const char *qemu_binary;
 158
 159    qemu_binary = getenv("QTEST_QEMU_BINARY");
 160    g_assert(qemu_binary != NULL);
 161
 162    s = g_malloc(sizeof(*s));
 163
 164    socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
 165    qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
 166
 167    sock = init_socket(socket_path);
 168    qmpsock = init_socket(qmp_socket_path);
 169
 170    qtest_add_abrt_handler(kill_qemu_hook_func, s);
 171
 172    s->qemu_pid = fork();
 173    if (s->qemu_pid == 0) {
 174        setenv("QEMU_AUDIO_DRV", "none", true);
 175        command = g_strdup_printf("exec %s "
 176                                  "-qtest unix:%s,nowait "
 177                                  "-qtest-log %s "
 178                                  "-qmp unix:%s,nowait "
 179                                  "-machine accel=qtest "
 180                                  "-display none "
 181                                  "%s", qemu_binary, socket_path,
 182                                  getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
 183                                  qmp_socket_path,
 184                                  extra_args ?: "");
 185        execlp("/bin/sh", "sh", "-c", command, NULL);
 186        exit(1);
 187    }
 188
 189    s->fd = socket_accept(sock);
 190    if (s->fd >= 0) {
 191        s->qmp_fd = socket_accept(qmpsock);
 192    }
 193    unlink(socket_path);
 194    unlink(qmp_socket_path);
 195    g_free(socket_path);
 196    g_free(qmp_socket_path);
 197
 198    g_assert(s->fd >= 0 && s->qmp_fd >= 0);
 199
 200    s->rx = g_string_new("");
 201    for (i = 0; i < MAX_IRQ; i++) {
 202        s->irq_level[i] = false;
 203    }
 204
 205    /* Read the QMP greeting and then do the handshake */
 206    qtest_qmp_discard_response(s, "");
 207    qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
 208
 209    if (getenv("QTEST_STOP")) {
 210        kill(s->qemu_pid, SIGSTOP);
 211    }
 212
 213    return s;
 214}
 215
 216void qtest_quit(QTestState *s)
 217{
 218    qtest_instances = g_list_remove(qtest_instances, s);
 219    g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
 220
 221    /* Uninstall SIGABRT handler on last instance */
 222    if (!qtest_instances) {
 223        cleanup_sigabrt_handler();
 224    }
 225
 226    kill_qemu(s);
 227    close(s->fd);
 228    close(s->qmp_fd);
 229    g_string_free(s->rx, true);
 230    g_free(s);
 231}
 232
 233static void socket_send(int fd, const char *buf, size_t size)
 234{
 235    size_t offset;
 236
 237    offset = 0;
 238    while (offset < size) {
 239        ssize_t len;
 240
 241        len = write(fd, buf + offset, size - offset);
 242        if (len == -1 && errno == EINTR) {
 243            continue;
 244        }
 245
 246        g_assert_no_errno(len);
 247        g_assert_cmpint(len, >, 0);
 248
 249        offset += len;
 250    }
 251}
 252
 253static void socket_sendf(int fd, const char *fmt, va_list ap)
 254{
 255    gchar *str = g_strdup_vprintf(fmt, ap);
 256    size_t size = strlen(str);
 257
 258    socket_send(fd, str, size);
 259    g_free(str);
 260}
 261
 262static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
 263{
 264    va_list ap;
 265
 266    va_start(ap, fmt);
 267    socket_sendf(s->fd, fmt, ap);
 268    va_end(ap);
 269}
 270
 271static GString *qtest_recv_line(QTestState *s)
 272{
 273    GString *line;
 274    size_t offset;
 275    char *eol;
 276
 277    while ((eol = strchr(s->rx->str, '\n')) == NULL) {
 278        ssize_t len;
 279        char buffer[1024];
 280
 281        len = read(s->fd, buffer, sizeof(buffer));
 282        if (len == -1 && errno == EINTR) {
 283            continue;
 284        }
 285
 286        if (len == -1 || len == 0) {
 287            fprintf(stderr, "Broken pipe\n");
 288            exit(1);
 289        }
 290
 291        g_string_append_len(s->rx, buffer, len);
 292    }
 293
 294    offset = eol - s->rx->str;
 295    line = g_string_new_len(s->rx->str, offset);
 296    g_string_erase(s->rx, 0, offset + 1);
 297
 298    return line;
 299}
 300
 301static gchar **qtest_rsp(QTestState *s, int expected_args)
 302{
 303    GString *line;
 304    gchar **words;
 305    int i;
 306
 307redo:
 308    line = qtest_recv_line(s);
 309    words = g_strsplit(line->str, " ", 0);
 310    g_string_free(line, TRUE);
 311
 312    if (strcmp(words[0], "IRQ") == 0) {
 313        int irq;
 314
 315        g_assert(words[1] != NULL);
 316        g_assert(words[2] != NULL);
 317
 318        irq = strtoul(words[2], NULL, 0);
 319        g_assert_cmpint(irq, >=, 0);
 320        g_assert_cmpint(irq, <, MAX_IRQ);
 321
 322        if (strcmp(words[1], "raise") == 0) {
 323            s->irq_level[irq] = true;
 324        } else {
 325            s->irq_level[irq] = false;
 326        }
 327
 328        g_strfreev(words);
 329        goto redo;
 330    }
 331
 332    g_assert(words[0] != NULL);
 333    g_assert_cmpstr(words[0], ==, "OK");
 334
 335    if (expected_args) {
 336        for (i = 0; i < expected_args; i++) {
 337            g_assert(words[i] != NULL);
 338        }
 339    } else {
 340        g_strfreev(words);
 341    }
 342
 343    return words;
 344}
 345
 346typedef struct {
 347    JSONMessageParser parser;
 348    QDict *response;
 349} QMPResponseParser;
 350
 351static void qmp_response(JSONMessageParser *parser, GQueue *tokens)
 352{
 353    QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
 354    QObject *obj;
 355
 356    obj = json_parser_parse(tokens, NULL);
 357    if (!obj) {
 358        fprintf(stderr, "QMP JSON response parsing failed\n");
 359        exit(1);
 360    }
 361
 362    g_assert(qobject_type(obj) == QTYPE_QDICT);
 363    g_assert(!qmp->response);
 364    qmp->response = (QDict *)obj;
 365}
 366
 367QDict *qmp_fd_receive(int fd)
 368{
 369    QMPResponseParser qmp;
 370    bool log = getenv("QTEST_LOG") != NULL;
 371
 372    qmp.response = NULL;
 373    json_message_parser_init(&qmp.parser, qmp_response);
 374    while (!qmp.response) {
 375        ssize_t len;
 376        char c;
 377
 378        len = read(fd, &c, 1);
 379        if (len == -1 && errno == EINTR) {
 380            continue;
 381        }
 382
 383        if (len == -1 || len == 0) {
 384            fprintf(stderr, "Broken pipe\n");
 385            exit(1);
 386        }
 387
 388        if (log) {
 389            len = write(2, &c, 1);
 390        }
 391        json_message_parser_feed(&qmp.parser, &c, 1);
 392    }
 393    json_message_parser_destroy(&qmp.parser);
 394
 395    return qmp.response;
 396}
 397
 398QDict *qtest_qmp_receive(QTestState *s)
 399{
 400    return qmp_fd_receive(s->qmp_fd);
 401}
 402
 403/**
 404 * Allow users to send a message without waiting for the reply,
 405 * in the case that they choose to discard all replies up until
 406 * a particular EVENT is received.
 407 */
 408void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
 409{
 410    va_list ap_copy;
 411    QObject *qobj;
 412
 413    /* Going through qobject ensures we escape strings properly.
 414     * This seemingly unnecessary copy is required in case va_list
 415     * is an array type.
 416     */
 417    va_copy(ap_copy, ap);
 418    qobj = qobject_from_jsonv(fmt, &ap_copy);
 419    va_end(ap_copy);
 420
 421    /* No need to send anything for an empty QObject.  */
 422    if (qobj) {
 423        int log = getenv("QTEST_LOG") != NULL;
 424        QString *qstr = qobject_to_json(qobj);
 425        const char *str = qstring_get_str(qstr);
 426        size_t size = qstring_get_length(qstr);
 427
 428        if (log) {
 429            fprintf(stderr, "%s", str);
 430        }
 431        /* Send QMP request */
 432        socket_send(fd, str, size);
 433
 434        QDECREF(qstr);
 435        qobject_decref(qobj);
 436    }
 437}
 438
 439void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
 440{
 441    qmp_fd_sendv(s->qmp_fd, fmt, ap);
 442}
 443
 444QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
 445{
 446    qmp_fd_sendv(fd, fmt, ap);
 447
 448    return qmp_fd_receive(fd);
 449}
 450
 451QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
 452{
 453    qtest_async_qmpv(s, fmt, ap);
 454
 455    /* Receive reply */
 456    return qtest_qmp_receive(s);
 457}
 458
 459QDict *qmp_fd(int fd, const char *fmt, ...)
 460{
 461    va_list ap;
 462    QDict *response;
 463
 464    va_start(ap, fmt);
 465    response = qmp_fdv(fd, fmt, ap);
 466    va_end(ap);
 467    return response;
 468}
 469
 470void qmp_fd_send(int fd, const char *fmt, ...)
 471{
 472    va_list ap;
 473
 474    va_start(ap, fmt);
 475    qmp_fd_sendv(fd, fmt, ap);
 476    va_end(ap);
 477}
 478
 479QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
 480{
 481    va_list ap;
 482    QDict *response;
 483
 484    va_start(ap, fmt);
 485    response = qtest_qmpv(s, fmt, ap);
 486    va_end(ap);
 487    return response;
 488}
 489
 490void qtest_async_qmp(QTestState *s, const char *fmt, ...)
 491{
 492    va_list ap;
 493
 494    va_start(ap, fmt);
 495    qtest_async_qmpv(s, fmt, ap);
 496    va_end(ap);
 497}
 498
 499void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
 500{
 501    QDict *response = qtest_qmpv(s, fmt, ap);
 502    QDECREF(response);
 503}
 504
 505void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
 506{
 507    va_list ap;
 508    QDict *response;
 509
 510    va_start(ap, fmt);
 511    response = qtest_qmpv(s, fmt, ap);
 512    va_end(ap);
 513    QDECREF(response);
 514}
 515
 516void qtest_qmp_eventwait(QTestState *s, const char *event)
 517{
 518    QDict *response;
 519
 520    for (;;) {
 521        response = qtest_qmp_receive(s);
 522        if ((qdict_haskey(response, "event")) &&
 523            (strcmp(qdict_get_str(response, "event"), event) == 0)) {
 524            QDECREF(response);
 525            break;
 526        }
 527        QDECREF(response);
 528    }
 529}
 530
 531char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
 532{
 533    char *cmd;
 534    QDict *resp;
 535    char *ret;
 536
 537    cmd = g_strdup_vprintf(fmt, ap);
 538    resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
 539                     " 'arguments': {'command-line': %s}}",
 540                     cmd);
 541    ret = g_strdup(qdict_get_try_str(resp, "return"));
 542    g_assert(ret);
 543    QDECREF(resp);
 544    g_free(cmd);
 545    return ret;
 546}
 547
 548char *qtest_hmp(QTestState *s, const char *fmt, ...)
 549{
 550    va_list ap;
 551    char *ret;
 552
 553    va_start(ap, fmt);
 554    ret = qtest_hmpv(s, fmt, ap);
 555    va_end(ap);
 556    return ret;
 557}
 558
 559const char *qtest_get_arch(void)
 560{
 561    const char *qemu = getenv("QTEST_QEMU_BINARY");
 562    g_assert(qemu != NULL);
 563    const char *end = strrchr(qemu, '/');
 564
 565    return end + strlen("/qemu-system-");
 566}
 567
 568bool qtest_get_irq(QTestState *s, int num)
 569{
 570    /* dummy operation in order to make sure irq is up to date */
 571    qtest_inb(s, 0);
 572
 573    return s->irq_level[num];
 574}
 575
 576static int64_t qtest_clock_rsp(QTestState *s)
 577{
 578    gchar **words;
 579    int64_t clock;
 580    words = qtest_rsp(s, 2);
 581    clock = g_ascii_strtoll(words[1], NULL, 0);
 582    g_strfreev(words);
 583    return clock;
 584}
 585
 586int64_t qtest_clock_step_next(QTestState *s)
 587{
 588    qtest_sendf(s, "clock_step\n");
 589    return qtest_clock_rsp(s);
 590}
 591
 592int64_t qtest_clock_step(QTestState *s, int64_t step)
 593{
 594    qtest_sendf(s, "clock_step %"PRIi64"\n", step);
 595    return qtest_clock_rsp(s);
 596}
 597
 598int64_t qtest_clock_set(QTestState *s, int64_t val)
 599{
 600    qtest_sendf(s, "clock_set %"PRIi64"\n", val);
 601    return qtest_clock_rsp(s);
 602}
 603
 604void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
 605{
 606    qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
 607    qtest_rsp(s, 0);
 608}
 609
 610void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
 611{
 612    qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
 613    qtest_rsp(s, 0);
 614}
 615
 616static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
 617{
 618    qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
 619    qtest_rsp(s, 0);
 620}
 621
 622void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
 623{
 624    qtest_out(s, "outb", addr, value);
 625}
 626
 627void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
 628{
 629    qtest_out(s, "outw", addr, value);
 630}
 631
 632void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
 633{
 634    qtest_out(s, "outl", addr, value);
 635}
 636
 637static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
 638{
 639    gchar **args;
 640    uint32_t value;
 641
 642    qtest_sendf(s, "%s 0x%x\n", cmd, addr);
 643    args = qtest_rsp(s, 2);
 644    value = strtoul(args[1], NULL, 0);
 645    g_strfreev(args);
 646
 647    return value;
 648}
 649
 650uint8_t qtest_inb(QTestState *s, uint16_t addr)
 651{
 652    return qtest_in(s, "inb", addr);
 653}
 654
 655uint16_t qtest_inw(QTestState *s, uint16_t addr)
 656{
 657    return qtest_in(s, "inw", addr);
 658}
 659
 660uint32_t qtest_inl(QTestState *s, uint16_t addr)
 661{
 662    return qtest_in(s, "inl", addr);
 663}
 664
 665static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
 666                        uint64_t value)
 667{
 668    qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
 669    qtest_rsp(s, 0);
 670}
 671
 672void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
 673{
 674    qtest_write(s, "writeb", addr, value);
 675}
 676
 677void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
 678{
 679    qtest_write(s, "writew", addr, value);
 680}
 681
 682void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
 683{
 684    qtest_write(s, "writel", addr, value);
 685}
 686
 687void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
 688{
 689    qtest_write(s, "writeq", addr, value);
 690}
 691
 692static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
 693{
 694    gchar **args;
 695    uint64_t value;
 696
 697    qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
 698    args = qtest_rsp(s, 2);
 699    value = strtoull(args[1], NULL, 0);
 700    g_strfreev(args);
 701
 702    return value;
 703}
 704
 705uint8_t qtest_readb(QTestState *s, uint64_t addr)
 706{
 707    return qtest_read(s, "readb", addr);
 708}
 709
 710uint16_t qtest_readw(QTestState *s, uint64_t addr)
 711{
 712    return qtest_read(s, "readw", addr);
 713}
 714
 715uint32_t qtest_readl(QTestState *s, uint64_t addr)
 716{
 717    return qtest_read(s, "readl", addr);
 718}
 719
 720uint64_t qtest_readq(QTestState *s, uint64_t addr)
 721{
 722    return qtest_read(s, "readq", addr);
 723}
 724
 725static int hex2nib(char ch)
 726{
 727    if (ch >= '0' && ch <= '9') {
 728        return ch - '0';
 729    } else if (ch >= 'a' && ch <= 'f') {
 730        return 10 + (ch - 'a');
 731    } else if (ch >= 'A' && ch <= 'F') {
 732        return 10 + (ch - 'a');
 733    } else {
 734        return -1;
 735    }
 736}
 737
 738void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
 739{
 740    uint8_t *ptr = data;
 741    gchar **args;
 742    size_t i;
 743
 744    qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
 745    args = qtest_rsp(s, 2);
 746
 747    for (i = 0; i < size; i++) {
 748        ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
 749        ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
 750    }
 751
 752    g_strfreev(args);
 753}
 754
 755void qtest_add_func(const char *str, void (*fn)(void))
 756{
 757    gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
 758    g_test_add_func(path, fn);
 759    g_free(path);
 760}
 761
 762void qtest_add_data_func(const char *str, const void *data,
 763                         void (*fn)(const void *))
 764{
 765    gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
 766    g_test_add_data_func(path, data, fn);
 767    g_free(path);
 768}
 769
 770void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
 771{
 772    gchar *bdata;
 773
 774    bdata = g_base64_encode(data, size);
 775    qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
 776    socket_send(s->fd, bdata, strlen(bdata));
 777    socket_send(s->fd, "\n", 1);
 778    qtest_rsp(s, 0);
 779    g_free(bdata);
 780}
 781
 782void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
 783{
 784    gchar **args;
 785    size_t len;
 786
 787    qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
 788    args = qtest_rsp(s, 2);
 789
 790    g_base64_decode_inplace(args[1], &len);
 791    if (size != len) {
 792        fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
 793                size, len);
 794        len = MIN(len, size);
 795    }
 796
 797    memcpy(data, args[1], len);
 798    g_strfreev(args);
 799}
 800
 801void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
 802{
 803    const uint8_t *ptr = data;
 804    size_t i;
 805    char *enc = g_malloc(2 * size + 1);
 806
 807    for (i = 0; i < size; i++) {
 808        sprintf(&enc[i * 2], "%02x", ptr[i]);
 809    }
 810
 811    qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
 812    qtest_rsp(s, 0);
 813    g_free(enc);
 814}
 815
 816void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
 817{
 818    qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
 819    qtest_rsp(s, 0);
 820}
 821
 822QDict *qmp(const char *fmt, ...)
 823{
 824    va_list ap;
 825    QDict *response;
 826
 827    va_start(ap, fmt);
 828    response = qtest_qmpv(global_qtest, fmt, ap);
 829    va_end(ap);
 830    return response;
 831}
 832
 833void qmp_async(const char *fmt, ...)
 834{
 835    va_list ap;
 836
 837    va_start(ap, fmt);
 838    qtest_async_qmpv(global_qtest, fmt, ap);
 839    va_end(ap);
 840}
 841
 842void qmp_discard_response(const char *fmt, ...)
 843{
 844    va_list ap;
 845
 846    va_start(ap, fmt);
 847    qtest_qmpv_discard_response(global_qtest, fmt, ap);
 848    va_end(ap);
 849}
 850char *hmp(const char *fmt, ...)
 851{
 852    va_list ap;
 853    char *ret;
 854
 855    va_start(ap, fmt);
 856    ret = qtest_hmpv(global_qtest, fmt, ap);
 857    va_end(ap);
 858    return ret;
 859}
 860
 861bool qtest_big_endian(void)
 862{
 863    const char *arch = qtest_get_arch();
 864    int i;
 865
 866    static const struct {
 867        const char *arch;
 868        bool big_endian;
 869    } endianness[] = {
 870        { "aarch64", false },
 871        { "alpha", false },
 872        { "arm", false },
 873        { "cris", false },
 874        { "i386", false },
 875        { "lm32", true },
 876        { "m68k", true },
 877        { "microblaze", true },
 878        { "microblazeel", false },
 879        { "mips", true },
 880        { "mips64", true },
 881        { "mips64el", false },
 882        { "mipsel", false },
 883        { "moxie", true },
 884        { "or32", true },
 885        { "ppc", true },
 886        { "ppc64", true },
 887        { "ppcemb", true },
 888        { "s390x", true },
 889        { "sh4", false },
 890        { "sh4eb", true },
 891        { "sparc", true },
 892        { "sparc64", true },
 893        { "unicore32", false },
 894        { "x86_64", false },
 895        { "xtensa", false },
 896        { "xtensaeb", true },
 897        {},
 898    };
 899
 900    for (i = 0; endianness[i].arch; i++) {
 901        if (strcmp(endianness[i].arch, arch) == 0) {
 902            return endianness[i].big_endian;
 903        }
 904    }
 905
 906    return false;
 907}
 908