xref: /openbmc/qemu/tests/qtest/test-hmp.c (revision 09be34717190c1620f0c6e5c8765b8da354aeb4b)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * Test HMP commands.
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2017 Red Hat Inc.
51e8a1faeSThomas Huth  *
61e8a1faeSThomas Huth  * Author:
71e8a1faeSThomas Huth  *    Thomas Huth <thuth@redhat.com>
81e8a1faeSThomas Huth  *
91e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2
101e8a1faeSThomas Huth  * or later. See the COPYING file in the top-level directory.
111e8a1faeSThomas Huth  *
121e8a1faeSThomas Huth  * This test calls some HMP commands for all machines that the current
131e8a1faeSThomas Huth  * QEMU binary provides, to check whether they terminate successfully
141e8a1faeSThomas Huth  * (i.e. do not crash QEMU).
151e8a1faeSThomas Huth  */
161e8a1faeSThomas Huth 
171e8a1faeSThomas Huth #include "qemu/osdep.h"
18907b5105SMarc-André Lureau #include "libqtest.h"
191e8a1faeSThomas Huth 
201e8a1faeSThomas Huth static int verbose;
211e8a1faeSThomas Huth 
221e8a1faeSThomas Huth static const char *hmp_cmds[] = {
231e8a1faeSThomas Huth     "announce_self",
241e8a1faeSThomas Huth     "boot_set ndc",
251e8a1faeSThomas Huth     "chardev-add null,id=testchardev1",
261e8a1faeSThomas Huth     "chardev-send-break testchardev1",
271e8a1faeSThomas Huth     "chardev-change testchardev1 ringbuf",
281e8a1faeSThomas Huth     "chardev-remove testchardev1",
291e8a1faeSThomas Huth     "commit all",
301e8a1faeSThomas Huth     "cpu 0",
311e8a1faeSThomas Huth     "device_add ?",
321e8a1faeSThomas Huth     "device_add usb-mouse,id=mouse1",
331e8a1faeSThomas Huth     "drive_add ignored format=help",
341e8a1faeSThomas Huth     "mouse_button 7",
351e8a1faeSThomas Huth     "mouse_move 10 10",
361e8a1faeSThomas Huth     "mouse_button 0",
371e8a1faeSThomas Huth     "device_del mouse1",
381e8a1faeSThomas Huth     "dump-guest-memory /dev/null 0 4096",
391e8a1faeSThomas Huth     "dump-guest-memory /dev/null",
401e8a1faeSThomas Huth     "gdbserver",
411e8a1faeSThomas Huth     "gva2gpa 0",
421e8a1faeSThomas Huth     "hostfwd_add tcp::43210-:43210",
431e8a1faeSThomas Huth     "hostfwd_remove tcp::43210-:43210",
441e8a1faeSThomas Huth     "i /w 0",
451e8a1faeSThomas Huth     "log all",
461e8a1faeSThomas Huth     "log none",
471e8a1faeSThomas Huth     "memsave 0 4096 \"/dev/null\"",
48*4d841daeSMarkus Armbruster     "migrate_set_parameter xbzrle-cache-size 64k",
49b21a6e31SMarkus Armbruster     "migrate_set_parameter downtime-limit 1",
50b21a6e31SMarkus Armbruster     "migrate_set_parameter max-bandwidth 1",
511e8a1faeSThomas Huth     "netdev_add user,id=net1",
521e8a1faeSThomas Huth     "set_link net1 off",
531e8a1faeSThomas Huth     "set_link net1 on",
541e8a1faeSThomas Huth     "netdev_del net1",
551e8a1faeSThomas Huth     "nmi",
561e8a1faeSThomas Huth     "o /w 0 0x1234",
571e8a1faeSThomas Huth     "object_add memory-backend-ram,id=mem1,size=256M",
581e8a1faeSThomas Huth     "object_del mem1",
59e9ccfdd9SPeter Maydell     "one-insn-per-tb on",
601e8a1faeSThomas Huth     "pmemsave 0 4096 \"/dev/null\"",
611e8a1faeSThomas Huth     "p $pc + 8",
621e8a1faeSThomas Huth     "qom-list /",
631e8a1faeSThomas Huth     "qom-set /machine initrd test",
6489cf4fe3SDr. David Alan Gilbert     "qom-get /machine initrd",
651e8a1faeSThomas Huth     "screendump /dev/null",
661e8a1faeSThomas Huth     "sendkey x",
671e8a1faeSThomas Huth     "wavcapture /dev/null",
681e8a1faeSThomas Huth     "stopcapture 0",
691e8a1faeSThomas Huth     "sum 0 512",
701e8a1faeSThomas Huth     "x /8i 0x100",
711e8a1faeSThomas Huth     "xp /16x 0",
721e8a1faeSThomas Huth     NULL
731e8a1faeSThomas Huth };
741e8a1faeSThomas Huth 
751e8a1faeSThomas Huth /* Run through the list of pre-defined commands */
test_commands(QTestState * qts)761e8a1faeSThomas Huth static void test_commands(QTestState *qts)
771e8a1faeSThomas Huth {
781e8a1faeSThomas Huth     char *response;
791e8a1faeSThomas Huth     int i;
801e8a1faeSThomas Huth 
811e8a1faeSThomas Huth     for (i = 0; hmp_cmds[i] != NULL; i++) {
821e8a1faeSThomas Huth         response = qtest_hmp(qts, "%s", hmp_cmds[i]);
831e8a1faeSThomas Huth         if (verbose) {
841e8a1faeSThomas Huth             fprintf(stderr,
851e8a1faeSThomas Huth                     "\texecute HMP command: %s\n"
861e8a1faeSThomas Huth                     "\tresult             : %s\n",
871e8a1faeSThomas Huth                     hmp_cmds[i], response);
881e8a1faeSThomas Huth         }
891e8a1faeSThomas Huth         g_free(response);
901e8a1faeSThomas Huth     }
911e8a1faeSThomas Huth 
921e8a1faeSThomas Huth }
931e8a1faeSThomas Huth 
941e8a1faeSThomas Huth /* Run through all info commands and call them blindly (without arguments) */
test_info_commands(QTestState * qts)951e8a1faeSThomas Huth static void test_info_commands(QTestState *qts)
961e8a1faeSThomas Huth {
971e8a1faeSThomas Huth     char *resp, *info, *info_buf, *endp;
981e8a1faeSThomas Huth 
991e8a1faeSThomas Huth     info_buf = info = qtest_hmp(qts, "help info");
1001e8a1faeSThomas Huth 
1011e8a1faeSThomas Huth     while (*info) {
1021e8a1faeSThomas Huth         /* Extract the info command, ignore parameters and description */
1031e8a1faeSThomas Huth         g_assert(strncmp(info, "info ", 5) == 0);
1041e8a1faeSThomas Huth         endp = strchr(&info[5], ' ');
1051e8a1faeSThomas Huth         g_assert(endp != NULL);
1061e8a1faeSThomas Huth         *endp = '\0';
1071e8a1faeSThomas Huth         /* Now run the info command */
1081e8a1faeSThomas Huth         if (verbose) {
1091e8a1faeSThomas Huth             fprintf(stderr, "\t%s\n", info);
1101e8a1faeSThomas Huth         }
1111e8a1faeSThomas Huth         resp = qtest_hmp(qts, "%s", info);
1121e8a1faeSThomas Huth         g_free(resp);
1131e8a1faeSThomas Huth         /* And move forward to the next line */
1141e8a1faeSThomas Huth         info = strchr(endp + 1, '\n');
1151e8a1faeSThomas Huth         if (!info) {
1161e8a1faeSThomas Huth             break;
1171e8a1faeSThomas Huth         }
1181e8a1faeSThomas Huth         info += 1;
1191e8a1faeSThomas Huth     }
1201e8a1faeSThomas Huth 
1211e8a1faeSThomas Huth     g_free(info_buf);
1221e8a1faeSThomas Huth }
1231e8a1faeSThomas Huth 
test_machine(gconstpointer data)1241e8a1faeSThomas Huth static void test_machine(gconstpointer data)
1251e8a1faeSThomas Huth {
1261e8a1faeSThomas Huth     const char *machine = data;
1271e8a1faeSThomas Huth     char *args;
1281e8a1faeSThomas Huth     QTestState *qts;
1291e8a1faeSThomas Huth 
1301e8a1faeSThomas Huth     args = g_strdup_printf("-S -M %s", machine);
1311e8a1faeSThomas Huth     qts = qtest_init(args);
1321e8a1faeSThomas Huth 
1331e8a1faeSThomas Huth     test_info_commands(qts);
1341e8a1faeSThomas Huth     test_commands(qts);
1351e8a1faeSThomas Huth 
1361e8a1faeSThomas Huth     qtest_quit(qts);
1371e8a1faeSThomas Huth     g_free(args);
1381e8a1faeSThomas Huth     g_free((void *)data);
1391e8a1faeSThomas Huth }
1401e8a1faeSThomas Huth 
add_machine_test_case(const char * mname)1411e8a1faeSThomas Huth static void add_machine_test_case(const char *mname)
1421e8a1faeSThomas Huth {
1431e8a1faeSThomas Huth     char *path;
1441e8a1faeSThomas Huth 
1451e8a1faeSThomas Huth     path = g_strdup_printf("hmp/%s", mname);
1461e8a1faeSThomas Huth     qtest_add_data_func(path, g_strdup(mname), test_machine);
1471e8a1faeSThomas Huth     g_free(path);
1481e8a1faeSThomas Huth }
1491e8a1faeSThomas Huth 
main(int argc,char ** argv)1501e8a1faeSThomas Huth int main(int argc, char **argv)
1511e8a1faeSThomas Huth {
1521e8a1faeSThomas Huth     char *v_env = getenv("V");
1531e8a1faeSThomas Huth 
1546eb71c6aSThomas Huth     if (v_env && atoi(v_env) >= 2) {
1551e8a1faeSThomas Huth         verbose = true;
1561e8a1faeSThomas Huth     }
1571e8a1faeSThomas Huth 
1581e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
1591e8a1faeSThomas Huth 
1601e8a1faeSThomas Huth     qtest_cb_for_every_machine(add_machine_test_case, g_test_quick());
1611e8a1faeSThomas Huth 
1621e8a1faeSThomas Huth     /* as none machine has no memory by default, add a test case with memory */
1631e8a1faeSThomas Huth     qtest_add_data_func("hmp/none+2MB", g_strdup("none -m 2"), test_machine);
1641e8a1faeSThomas Huth 
1651e8a1faeSThomas Huth     return g_test_run();
1661e8a1faeSThomas Huth }
167