Rip out dynamic JSON parsing
cover Rip out dynamic JSON parsing
Followup series to conversation about PRId64 and MacOS:
https://lists.gnu.org/archive/html/qemu-devel/2016-11/msg04226.html
We have relatively few users of dynamic JSON (the ability to pass varargs plus a format string with % sequences embedded in the JSON, to create a final QObject dynamically). Most of them live in the testsuite. Converting ALL uses to hand-written QObjects would have been too invasive, so I hope this series hits a nice compromise of adjusting the few users that injected non-strings, while moving the work of string injection out of the JSON parser proper and instead into libqtest. Doing this means the testsuite is doing a lot fewer string->QObject->string round-trips when computing what to send over a QMP wire transaction.
I'm also pleased that this series has a net overall reduction in lines of code, while still adding several new useful features (such as qdict_put_str() in 2/36, QAPI_TO_QOBJECT() in 11/36, qmp_cmd() in 9/36) and fixing a couple of testsuite flaws (7/36, 8/36). Patch 2/36 may be a candidate for splitting into sub-patches, but the series is already long enough that I wanted to post it for feedback on the approaches taken here.
Partially overlaps with my earlier v6 posting of a qapi JSON visitor
(https://lists.gnu.org/archive/html/qemu-devel/2016-10/msg01915.html);
whichever series goes in first has two patches that the other series will use as-is (28/36, 29/36).
Eric Blake (36):
pci: Use struct instead of QDict to pass back parameters
qdict: Add convenience helpers for wrapped puts
qlist: Add convenience helpers for wrapped appends
qmp-event: Avoid dynamic JSON
qmp-dispatch: Avoid dynamic JSON
qobject-input-visitor: Avoid dynamic JSON in tests
fdc-test: Avoid deprecated 'change' command
test-qga: Actually test 0xff sync bytes
qtest: Add a new helper qmp_cmd() and friends
qtest: Avoid dynamic JSON in libqtest
qapi: Add QAPI_TO_QOBJECT() convenience macro
nbd: Use simpler QAPI_TO_QOBJECT()
nfs: Use simpler QAPI_TO_QOBJECT()
qapi: Use simpler QAPI_TO_QOBJECT()
blockdev: Use simpler QAPI_TO_QOBJECT()
qapi: Promote blockdev-change-medium arguments to QAPI type
qtest: Avoid dynamic JSON in ahci-test
qtest: Avoid dynamic JSON in fdc-test
qtest: Change qmp_discard_response() to drop varargs
qtest: Avoid dynamic JSON in device-introspect-test
qtest: Avoid dynamic JSON in tmp105-test
qtest: Avoid dynamic JSON in pc-cpu-test
qtest: Avoid dynamic JSON in virtio-blk-test
qtest: Drop unused qmp_fdv()
qtest: Change qmp_fd_send() to drop varags
qtest: Drop unused qtest_qmp_async()
qtest: Avoid dynamic JSON in qmp_cmd()
qapi: Factor out JSON string escaping
qapi: Add qstring_append_printf()
qtest: Avoid dynamic JSON in qmp_fd_sendv()
qtest: Document calling conventions
qtest: Avoid dynamic JSON in qom-test
qtest: Avoid dynamic JSON in test-x86-cpuid-compat
qapi: Rip out dynamic JSON parser frontend
qapi: Rip out dynamic JSON parser escape sequence support
qapi: Rip out dynamic JSON parser backend
qapi/block-core.json | 11 ++- include/qapi/qmp/json-lexer.h | 1 - include/qapi/qmp/json-parser.h | 6 +- include/qapi/qmp/qdict.h | 8 ++ include/qapi/qmp/qjson.h | 4 +- include/qapi/qmp/qlist.h | 8 ++ include/qapi/qmp/qstring.h | 7 +- include/qapi/qobject-output-visitor.h | 19 ++++ tests/libqtest.h | 78 +++++++-------- block.c | 59 +++++------- block/archipelago.c | 4 +- block/blkdebug.c | 6 +- block/blkverify.c | 11 +-- block/curl.c | 2 +- block/iscsi.c | 2 +- block/nbd.c | 47 ++++----- block/nfs.c | 49 ++++------ block/null.c | 2 +- block/qapi.c | 6 +- block/qcow2.c | 4 +- block/quorum.c | 13 +-- block/raw-posix.c | 8 +- block/raw-win32.c | 4 +- block/ssh.c | 16 ++- block/vvfat.c | 10 +- blockdev.c | 42 +++----- hw/block/xen_disk.c | 2 +- hw/pci/pcie_aer.c | 36 ++++--- hw/usb/xen-usb.c | 12 +-- migration/qjson.c | 12 +-- monitor.c | 30 +++--- qapi/qmp-dispatch.c | 8 +- qapi/qmp-event.c | 15 +-- qapi/qobject-output-visitor.c | 16 +++ qemu-img.c | 6 +- qemu-io.c | 2 +- qemu-nbd.c | 2 +- qga/main.c | 2 +- qobject/json-lexer.c | 40 -------- qobject/json-parser.c | 66 ++++--------- qobject/qdict.c | 2 +- qobject/qjson.c | 177 ++++++++++++++-------------------- qobject/qstring.c | 26 ++++- target-s390x/cpu_models.c | 4 +- tests/ahci-test.c | 26 ++++- tests/check-qdict.c | 142 +++++++++++++-------------- tests/check-qjson.c | 113 +--------------------- tests/check-qlist.c | 2 +- tests/device-introspect-test.c | 15 +-- tests/fdc-test.c | 15 ++- tests/libqtest.c | 147 ++++++++++++++-------------- tests/pc-cpu-test.c | 12 ++- tests/qom-test.c | 15 +-- tests/test-qga.c | 12 ++- tests/test-qmp-commands.c | 30 +++--- tests/test-qmp-event.c | 30 +++--- tests/test-qobject-input-strict.c | 37 +------ tests/test-qobject-input-visitor.c | 75 ++++++-------- tests/test-qobject-output-visitor.c | 6 +- tests/test-x86-cpuid-compat.c | 14 ++- tests/tmp105-test.c | 7 +- tests/virtio-blk-test.c | 22 +++-- util/qemu-option.c | 6 +- 63 files changed, 726 insertions(+), 885 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/36 pci: Use struct instead of QDict to pass back parameters
It's simpler to just use a C struct than it is to bundle things into a QDict in one function just to pull them back out in the caller. Plus, doing this gets rid of one more user of dynamic JSON through qobject_from_jsonf().
--- hw/pci/pcie_aer.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-)
hw/pci/pcie_aer.c +17 -19
@@ -43,6 +43,13 @@#define PCI_ERR_SRC_COR_OFFS 0#define PCI_ERR_SRC_UNCOR_OFFS 2 +typedef struct PCIEErrorInject { +const char *id; +const char *root_bus; +int bus; +int devfn; +}PCIEErrorInject ; +/* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */ static uint32_t pcie_aer_uncor_default_severity (uint32_t status) {
@@ -946,7 +953,8 @@ static int pcie_aer_parse_error_string(const char *error_name, }static int do_pcie_aer_inject_error (Monitor *mon, -const QDict *qdict,QObject **ret_data) +const QDict *qdict, +PCIEErrorInject *ret_data) {const char *id =qdict_get_str (qdict,"id" );const char *error_name;
@@ -1007,34 +1015,24 @@ static int do_pcie_aer_inject_error(Monitor *mon, err.prefix[2 ] =qdict_get_try_int (qdict,"prefix2" ,0 ); err.prefix[3 ] =qdict_get_try_int (qdict,"prefix3" ,0 ); - ret =pcie_aer_inject_error (dev, &err); - *ret_data =qobject_from_jsonf ("{'id': %s, " -"'root_bus': %s, 'bus': %d, 'devfn': %d, " -"'ret': %d}" , - id,pci_root_bus_path (dev), -pci_bus_num (dev->bus), dev->devfn, - ret); -assert (*ret_data); +pcie_aer_inject_error (dev, &err); + ret_data->id = id; + ret_data->root_bus =pci_root_bus_path (dev); + ret_data->bus =pci_bus_num (dev->bus); + ret_data->devfn = dev->devfn;return 0 ; }void hmp_pcie_aer_inject_error (Monitor *mon,const QDict *qdict) { -QObject *data; -int devfn; +PCIEErrorInject data;if (do_pcie_aer_inject_error (mon, qdict, &data) <0 ) {return ; } -assert (qobject_type (data) ==QTYPE_QDICT ); - qdict =qobject_to_qdict (data); - - devfn = (int )qdict_get_int (qdict,"devfn" );monitor_printf (mon,"OK id: %s root bus: %s, bus: %x devfn: %x.%x\n" , -qdict_get_str (qdict,"id" ), -qdict_get_str (qdict,"root_bus" ), - (int )qdict_get_int (qdict,"bus" ), -PCI_SLOT (devfn),PCI_FUNC (devfn)); + data.id, data.root_bus, data.bus, +PCI_SLOT (data.devfn),PCI_FUNC (data.devfn)); }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 01/36] pci: Use struct instead of QDict to pass back parameters
Quoted 6 lines · Eric Blake
It's simpler to just use a C struct than it is to bundle things into a QDict in one function just to pull them back out in the caller. Plus, doing this gets rid of one more user of dynamic JSON through
qobject_from_jsonf().Signed-off-by Eric Blake <[email protected]>
Quoted 77 lines
--- hw/pci/pcie_aer.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-)hw/pci/pcie_aer.c +17
-19@@ -43,6 +43,13 @@#define PCI_ERR_SRC_COR_OFFS 0#define PCI_ERR_SRC_UNCOR_OFFS 2 +typedef struct PCIEErrorInject { +const char *id; +const char *root_bus; +int bus; +int devfn; +}PCIEErrorInject ; +/* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */ static uint32_t pcie_aer_uncor_default_severity (uint32_t status) {@@ -946,7 +953,8 @@ static int pcie_aer_parse_error_string(const char *error_name, }static int do_pcie_aer_inject_error (Monitor *mon, -const QDict *qdict,QObject **ret_data) +const QDict *qdict, +PCIEErrorInject *ret_data) {const char *id =qdict_get_str (qdict,"id" );const char *error_name;@@ -1007,34 +1015,24 @@ static int do_pcie_aer_inject_error(Monitor *mon, err.prefix[2 ] =qdict_get_try_int (qdict,"prefix2" ,0 ); err.prefix[3 ] =qdict_get_try_int (qdict,"prefix3" ,0 ); - ret =pcie_aer_inject_error (dev, &err); - *ret_data =qobject_from_jsonf ("{'id': %s, " -"'root_bus': %s, 'bus': %d, 'devfn': %d, " -"'ret': %d}" , - id,pci_root_bus_path (dev), -pci_bus_num (dev->bus), dev->devfn, - ret); -assert (*ret_data); +pcie_aer_inject_error (dev, &err); + ret_data->id = id; + ret_data->root_bus =pci_root_bus_path (dev); + ret_data->bus =pci_bus_num (dev->bus); + ret_data->devfn = dev->devfn;return 0 ; }void hmp_pcie_aer_inject_error (Monitor *mon,const QDict *qdict) { -QObject *data; -int devfn; +PCIEErrorInject data;if (do_pcie_aer_inject_error (mon, qdict, &data) <0 ) {return ; } -assert (qobject_type (data) ==QTYPE_QDICT ); - qdict =qobject_to_qdict (data); - - devfn = (int )qdict_get_int (qdict,"devfn" );monitor_printf (mon,"OK id: %s root bus: %s, bus: %x devfn: %x.%x\n" , -qdict_get_str (qdict,"id" ), -qdict_get_str (qdict,"root_bus" ), - (int )qdict_get_int (qdict,"bus" ), -PCI_SLOT (devfn),PCI_FUNC (devfn)); + data.id, data.root_bus, data.bus, +PCI_SLOT (data.devfn),PCI_FUNC (data.devfn)); }Signature · 1 line
2.7.4
PATCH 2/36 qdict: Add convenience helpers for wrapped puts
In reply to Eric BlakeQuite a few users of qdict_put() were manually wrapping a non-QObject. We can make such call-sites shorter, by providing common macros to do the tedious work. Also shorten nearby qdict_put_obj(,,QOBJECT()) sequences.
---
I'm okay if you want me to break this patch into smaller pieces.
--- include/qapi/qmp/qdict.h | 8 +++ block.c | 59 +++++++--------- block/archipelago.c | 4 +- block/blkdebug.c | 6 +- block/blkverify.c | 11 ++- block/curl.c | 2 +- block/iscsi.c | 2 +- block/nbd.c | 41 ++++++----- block/nfs.c | 43 +++++------- block/null.c | 2 +- block/qcow2.c | 4 +- block/quorum.c | 13 ++-- block/raw-posix.c | 8 +-- block/raw-win32.c | 4 +- block/ssh.c | 16 ++--- block/vvfat.c | 10 +-- blockdev.c | 28 ++++---- hw/block/xen_disk.c | 2 +- hw/usb/xen-usb.c | 12 ++-- monitor.c | 18 ++--- qapi/qmp-event.c | 2 +- qemu-img.c | 6 +- qemu-io.c | 2 +- qemu-nbd.c | 2 +- qobject/qdict.c | 2 +- target-s390x/cpu_models.c | 4 +- tests/check-qdict.c | 132 ++++++++++++++++++------------------ tests/test-qmp-commands.c | 30 ++++---- tests/test-qmp-event.c | 30 ++++---- tests/test-qobject-output-visitor.c | 6 +- util/qemu-option.c | 6 +- 31 files changed, 245 insertions(+), 270 deletions(-)
include/qapi/qmp/qdict.h +8 -0
@@ -52,6 +52,14 @@ void qdict_destroy_obj(QObject *obj);#define qdict_put (qdict, key, obj) \ qdict_put_obj(qdict, key, QOBJECT(obj)) +/* Helpers for int, bool, and const char*. */ +#define qdict_put_int (qdict, key, value) \ + qdict_put(qdict, key, qint_from_int(value)) +#define qdict_put_bool (qdict, key, value) \ + qdict_put(qdict, key, qbool_from_bool(value)) +#define qdict_put_str (qdict, key, value) \ + qdict_put(qdict, key, qstring_from_str(value)) +/* High level helpers */ double qdict_get_double (const QDict *qdict,const char *key);int64_t qdict_get_int (const QDict *qdict,const char *key);
block.c +24 -35
148 lines not on this page · raw message
block/archipelago.c +2 -2
@@ -432,10 +432,10 @@ static void archipelago_parse_filename(const char *filename, QDict *options,g_free (segment_name); }if (mport != NoPort) { -qdict_put (options,ARCHIPELAGO_OPT_MPORT ,qint_from_int (mport)); +qdict_put_int (options,ARCHIPELAGO_OPT_MPORT , mport); }if (vport != NoPort) { -qdict_put (options,ARCHIPELAGO_OPT_VPORT ,qint_from_int (vport)); +qdict_put_int (options,ARCHIPELAGO_OPT_VPORT , vport); } }
block/blkdebug.c +3 -3
@@ -302,7 +302,7 @@ static void blkdebug_parse_filename(const char *filename, QDict *options,if (!strstart (filename,"blkdebug:" , &filename)) {/* There was no prefix; therefore, all options have to be already present in the QDict (except for the filename) */ -qdict_put (options,"x-image" ,qstring_from_str (filename)); +qdict_put_str (options,"x-image" , filename);return ; }
@@ -321,7 +321,7 @@ static void blkdebug_parse_filename(const char *filename, QDict *options,/* TODO Allow multi-level nesting and set file.filename here */ filename = c +1 ; -qdict_put (options,"x-image" ,qstring_from_str (filename)); +qdict_put_str (options,"x-image" , filename); }static QemuOptsList runtime_opts = {
@@ -708,7 +708,7 @@ static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options) } opts =qdict_new (); -qdict_put_obj (opts,"driver" ,QOBJECT (qstring_from_str ("blkdebug" ))); +qdict_put_str (opts,"driver" ,"blkdebug" );QINCREF (bs->file->bs->full_open_options);qdict_put_obj (opts,"image" ,QOBJECT (bs->file->bs->full_open_options));
block/blkverify.c +5 -6
@@ -69,7 +69,7 @@ static void blkverify_parse_filename(const char *filename, QDict *options,if (!strstart (filename,"blkverify:" , &filename)) {/* There was no prefix; therefore, all options have to be already present in the QDict (except for the filename) */ -qdict_put (options,"x-image" ,qstring_from_str (filename)); +qdict_put_str (options,"x-image" , filename);return ; }
@@ -86,7 +86,7 @@ static void blkverify_parse_filename(const char *filename, QDict *options,/* TODO Allow multi-level nesting and set file.filename here */ filename = c +1 ; -qdict_put (options,"x-image" ,qstring_from_str (filename)); +qdict_put_str (options,"x-image" , filename); }static QemuOptsList runtime_opts = {
@@ -300,13 +300,12 @@ static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options) && s->test_file->bs->full_open_options) {QDict *opts =qdict_new (); -qdict_put_obj (opts,"driver" ,QOBJECT (qstring_from_str ("blkverify" ))); +qdict_put_str (opts,"driver" ,"blkverify" );QINCREF (bs->file->bs->full_open_options); -qdict_put_obj (opts,"raw" ,QOBJECT (bs->file->bs->full_open_options)); +qdict_put (opts,"raw" , bs->file->bs->full_open_options);QINCREF (s->test_file->bs->full_open_options); -qdict_put_obj (opts,"test" , -QOBJECT (s->test_file->bs->full_open_options)); +qdict_put (opts,"test" , s->test_file->bs->full_open_options); bs->full_open_options = opts; }
block/curl.c +1 -1
@@ -531,7 +531,7 @@ static void curl_clean_state(CURLState *s)static void curl_parse_filename (const char *filename,QDict *options,Error **errp) { -qdict_put (options,CURL_BLOCK_OPT_URL ,qstring_from_str (filename)); +qdict_put_str (options,CURL_BLOCK_OPT_URL , filename); }static void curl_detach_aio_context (BlockDriverState *bs)
block/iscsi.c +1 -1
@@ -1965,7 +1965,7 @@ static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp) iscsilun = bs->opaque; bs_options =qdict_new (); -qdict_put (bs_options,"filename" ,qstring_from_str (filename)); +qdict_put_str (bs_options,"filename" , filename); ret =iscsi_open (bs, bs_options,0 ,NULL );QDECREF (bs_options);
block/nbd.c +20 -21
101 lines not on this page · raw message
block/nfs.c +17 -26
84 lines not on this page · raw message
block/null.c +1 -1
@@ -232,7 +232,7 @@ static void null_refresh_filename(BlockDriverState *bs, QDict *opts) bs->drv->format_name); } -qdict_put (opts,"driver" ,qstring_from_str (bs->drv->format_name)); +qdict_put_str (opts,"driver" , bs->drv->format_name); bs->full_open_options = opts; }
block/qcow2.c +2 -2
@@ -2252,7 +2252,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,* table) */ options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str ("qcow2" )); +qdict_put_str (options,"driver" ,"qcow2" ); blk =blk_new_open (filename,NULL , options,BDRV_O_RDWR |BDRV_O_NO_FLUSH , &local_err);if (blk ==NULL ) {
@@ -2313,7 +2313,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,/* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */ options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str ("qcow2" )); +qdict_put_str (options,"driver" ,"qcow2" ); blk =blk_new_open (filename,NULL , options,BDRV_O_RDWR |BDRV_O_NO_BACKING , &local_err);if (blk ==NULL ) {
block/quorum.c +5 -8
@@ -1072,14 +1072,11 @@ static void quorum_refresh_filename(BlockDriverState *bs, QDict *options) } opts =qdict_new (); -qdict_put_obj (opts,"driver" ,QOBJECT (qstring_from_str ("quorum" ))); -qdict_put_obj (opts,QUORUM_OPT_VOTE_THRESHOLD , -QOBJECT (qint_from_int (s->threshold))); -qdict_put_obj (opts,QUORUM_OPT_BLKVERIFY , -QOBJECT (qbool_from_bool (s->is_blkverify))); -qdict_put_obj (opts,QUORUM_OPT_REWRITE , -QOBJECT (qbool_from_bool (s->rewrite_corrupted))); -qdict_put_obj (opts,"children" ,QOBJECT (children)); +qdict_put_str (opts,"driver" ,"quorum" ); +qdict_put_int (opts,QUORUM_OPT_VOTE_THRESHOLD , s->threshold); +qdict_put_bool (opts,QUORUM_OPT_BLKVERIFY , s->is_blkverify); +qdict_put_bool (opts,QUORUM_OPT_REWRITE , s->rewrite_corrupted); +qdict_put (opts,"children" , children); bs->full_open_options = opts; }
block/raw-posix.c +4 -4
@@ -376,7 +376,7 @@ static void raw_parse_filename(const char *filename, QDict *options,* function call can be ignored. */ strstart (filename,"file:" , &filename); -qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +qdict_put_str (options,"filename" , filename); }static QemuOptsList raw_runtime_opts = {
@@ -2061,7 +2061,7 @@ static void hdev_parse_filename(const char *filename, QDict *options,/* The prefix is optional, just as for "file". */ strstart (filename,"host_device:" , &filename); -qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +qdict_put_str (options,"filename" , filename); }static bool hdev_is_sg (BlockDriverState *bs)
@@ -2144,7 +2144,7 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags, goto hdev_open_Mac_error; } -qdict_put (options,"filename" ,qstring_from_str (bsd_path)); +qdict_put_str (options,"filename" , bsd_path); hdev_open_Mac_error:g_free (mediaType);
@@ -2354,7 +2354,7 @@ static void cdrom_parse_filename(const char *filename, QDict *options,/* The prefix is optional, just as for "file". */ strstart (filename,"host_cdrom:" , &filename); - qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename))); + qdict_put_str(options, "filename", filename); }#endif
block/raw-win32.c +2 -2
@@ -282,7 +282,7 @@ static void raw_parse_filename(const char *filename, QDict *options,* function call can be ignored. */ strstart (filename,"file:" , &filename); -qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +qdict_put_str (options,"filename" , filename); }static QemuOptsList raw_runtime_opts = {
@@ -669,7 +669,7 @@ static void hdev_parse_filename(const char *filename, QDict *options,/* The prefix is optional, just as for "file". */ strstart (filename,"host_device:" , &filename); -qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +qdict_put_str (options,"filename" , filename); } static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
block/ssh.c +7 -9
@@ -227,24 +227,23 @@ static int parse_uri(const char *filename, QDict *options, Error **errp) }if (uri->user &&strcmp (uri->user,"" ) !=0 ) { -qdict_put (options,"user" ,qstring_from_str (uri->user)); +qdict_put_str (options,"user" , uri->user); } -qdict_put (options,"server.host" ,qstring_from_str (uri->server)); +qdict_put_str (options,"server.host" , uri->server); port_str =g_strdup_printf ("%d" , uri->port ?:22 ); -qdict_put (options,"server.port" ,qstring_from_str (port_str)); +qdict_put_str (options,"server.port" , port_str);g_free (port_str); -qdict_put (options,"path" ,qstring_from_str (uri->path)); +qdict_put_str (options,"path" , uri->path);/* Pick out any query parameters that we understand, and ignore * the rest. */ for (i =0 ; i < qp->n; ++i) {if (strcmp (qp->p[i].name,"host_key_check" ) ==0 ) { -qdict_put (options,"host_key_check" , -qstring_from_str (qp->p[i].value)); +qdict_put_str (options,"host_key_check" , qp->p[i].value); } }
@@ -574,9 +573,8 @@ static bool ssh_process_legacy_socket_options(QDict *output_opts, }if (host) { -qdict_put (output_opts,"server.host" ,qstring_from_str (host)); -qdict_put (output_opts,"server.port" , -qstring_from_str (port ?:stringify (22 ))); +qdict_put_str (output_opts,"server.host" , host); +qdict_put_str (output_opts,"server.port" , port ?:stringify (22 )); }return true;
block/vvfat.c +5 -5
@@ -1057,10 +1057,10 @@ static void vvfat_parse_filename(const char *filename, QDict *options, }/* Fill in the options QDict */ -qdict_put (options,"dir" ,qstring_from_str (filename)); -qdict_put (options,"fat-type" ,qint_from_int (fat_type)); -qdict_put (options,"floppy" ,qbool_from_bool (floppy)); -qdict_put (options,"rw" ,qbool_from_bool (rw)); +qdict_put_str (options,"dir" , filename); +qdict_put_int (options,"fat-type" , fat_type); +qdict_put_bool (options,"floppy" , floppy); +qdict_put_bool (options,"rw" , rw); } static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
@@ -3019,7 +3019,7 @@ static int enable_write_target(BlockDriverState *bs, Error **errp) } options =qdict_new (); -qdict_put (options,"write-target.driver" ,qstring_from_str ("qcow" )); +qdict_put_str (options,"write-target.driver" ,"qcow" ); s->qcow =bdrv_open_child (s->qcow_filename, options,"write-target" , bs, &child_vvfat_qcow, false, errp);QDECREF (options);
blockdev.c +12 -16
@@ -523,7 +523,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,error_setg (errp,"Cannot specify both 'driver' and 'format'" ); goto early_err; } -qdict_put (bs_opts,"driver" ,qstring_from_str (buf)); +qdict_put_str (bs_opts,"driver" , buf); } on_write_error =BLOCKDEV_ON_ERROR_ENOSPC ;
@@ -899,10 +899,8 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) copy_on_read = false; } -qdict_put (bs_opts,BDRV_OPT_READ_ONLY , -qstring_from_str (read_only ?"on" :"off" )); -qdict_put (bs_opts,"copy-on-read" , -qstring_from_str (copy_on_read ?"on" :"off" )); +qdict_put_str (bs_opts,BDRV_OPT_READ_ONLY , read_only ?"on" :"off" ); +qdict_put_str (bs_opts,"copy-on-read" , copy_on_read ?"on" :"off" );/* Controller type */ value =qemu_opt_get (legacy_opts,"if" );
@@ -1063,7 +1061,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)error_report ("werror is not supported by this bus type" ); goto fail; } -qdict_put (bs_opts,"werror" ,qstring_from_str (werror)); +qdict_put_str (bs_opts,"werror" , werror); } rerror =qemu_opt_get (legacy_opts,"rerror" );
@@ -1073,7 +1071,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)error_report ("rerror is not supported by this bus type" ); goto fail; } -qdict_put (bs_opts,"rerror" ,qstring_from_str (rerror)); +qdict_put_str (bs_opts,"rerror" , rerror); }/* Actual block device init: Functionality shared with blockdev-add */
@@ -1732,10 +1730,9 @@ static void external_snapshot_prepare(BlkActionState *common, options =qdict_new ();if (s->has_snapshot_node_name) { -qdict_put (options,"node-name" , -qstring_from_str (snapshot_node_name)); +qdict_put_str (options,"node-name" , snapshot_node_name); } -qdict_put (options,"driver" ,qstring_from_str (format)); +qdict_put_str (options,"driver" , format); flags |=BDRV_O_NO_BACKING ; }
@@ -2544,11 +2541,10 @@ void qmp_blockdev_change_medium(bool has_device, const char *device, options =qdict_new (); detect_zeroes =blk_get_detect_zeroes_from_root_state (blk); -qdict_put (options,"detect-zeroes" , -qstring_from_str (detect_zeroes ?"on" :"off" )); +qdict_put_str (options,"detect-zeroes" , detect_zeroes ?"on" :"off" );if (has_format) { -qdict_put (options,"driver" ,qstring_from_str (format)); +qdict_put_str (options,"driver" , format); } medium_bs =bdrv_open (filename,NULL , options, bdrv_flags, errp);
@@ -3204,7 +3200,7 @@ static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,if (backup->format) { options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (backup->format)); +qdict_put_str (options,"driver" , backup->format); } target_bs =bdrv_open (backup->target,NULL , options, flags, errp);
@@ -3502,10 +3498,10 @@ void qmp_drive_mirror(DriveMirror *arg, Error **errp) options =qdict_new ();if (arg->has_node_name) { -qdict_put (options,"node-name" ,qstring_from_str (arg->node_name)); +qdict_put_str (options,"node-name" , arg->node_name); }if (format) { -qdict_put (options,"driver" ,qstring_from_str (format)); +qdict_put_str (options,"driver" , format); }/* Mirroring takes care of copy-on-write using the source's backing
hw/block/xen_disk.c +1 -1
@@ -1082,7 +1082,7 @@ static int blk_connect(struct XenDevice *xendev)if (strcmp (blkdev->fileproto,"<unset>" )) { options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (blkdev->fileproto)); +qdict_put_str (options,"driver" , blkdev->fileproto); }/* setup via xenbus -> create new block driver instance */
hw/usb/xen-usb.c +6 -6
@@ -746,16 +746,16 @@ static void usbback_portid_add(struct usbback_info *usbif, unsigned port, portname++; qdict =qdict_new (); -qdict_put (qdict,"driver" ,qstring_from_str ("usb-host" )); +qdict_put_str (qdict,"driver" ,"usb-host" ); tmp =g_strdup_printf ("%s.0" , usbif->xendev.qdev.id); -qdict_put (qdict,"bus" ,qstring_from_str (tmp)); +qdict_put_str (qdict,"bus" , tmp);g_free (tmp); tmp =g_strdup_printf ("%s-%u" , usbif->xendev.qdev.id, port); -qdict_put (qdict,"id" ,qstring_from_str (tmp)); +qdict_put_str (qdict,"id" , tmp);g_free (tmp); -qdict_put (qdict,"port" ,qint_from_int (port)); -qdict_put (qdict,"hostbus" ,qint_from_int (atoi (busid))); -qdict_put (qdict,"hostport" ,qstring_from_str (portname)); +qdict_put_int (qdict,"port" , port); +qdict_put_int (qdict,"hostbus" ,atoi (busid)); +qdict_put_str (qdict,"hostport" , portname); opts =qemu_opts_from_qdict (qemu_find_opts ("device" ), qdict, &local_err);if (local_err) { goto err;
monitor.c +9 -9
@@ -2618,7 +2618,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, } goto fail; } -qdict_put (qdict, key,qstring_from_str (buf)); +qdict_put_str (qdict, key, buf); }break ;case 'O' :
@@ -2720,9 +2720,9 @@ static QDict *monitor_parse_arguments(Monitor *mon, size =-1 ; } } -qdict_put (qdict,"count" ,qint_from_int (count)); -qdict_put (qdict,"format" ,qint_from_int (format)); -qdict_put (qdict,"size" ,qint_from_int (size)); +qdict_put_int (qdict,"count" , count); +qdict_put_int (qdict,"format" , format); +qdict_put_int (qdict,"size" , size); }break ;case 'i' :
@@ -2765,7 +2765,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, } val <<=20 ; } -qdict_put (qdict, key,qint_from_int (val)); +qdict_put_int (qdict, key, val); }break ;case 'o' :
@@ -2787,7 +2787,7 @@ static QDict *monitor_parse_arguments(Monitor *mon,monitor_printf (mon,"invalid size\n" ); goto fail; } -qdict_put (qdict, key,qint_from_int (val)); +qdict_put_int (qdict, key, val); p = end; }break ;
@@ -2843,7 +2843,7 @@ static QDict *monitor_parse_arguments(Monitor *mon,monitor_printf (mon,"Expected 'on' or 'off'\n" ); goto fail; } -qdict_put (qdict, key,qbool_from_bool (val)); +qdict_put_bool (qdict, key, val); }break ;case '-' :
@@ -2874,7 +2874,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, }else {/* has option */ p++; -qdict_put (qdict, key,qbool_from_bool (true)); +qdict_put_bool (qdict, key, true); } } }
@@ -2900,7 +2900,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, cmd->name); goto fail; } -qdict_put (qdict, key,qstring_from_str (p)); +qdict_put_str (qdict, key, p); p += len; }break ;
qapi/qmp-event.c +1 -1
@@ -51,7 +51,7 @@ static void timestamp_put(QDict *qdict)QDict *qmp_event_build_dict (const char *event_name) {QDict *dict =qdict_new (); -qdict_put (dict,"event" ,qstring_from_str (event_name)); +qdict_put_str (dict,"event" , event_name);timestamp_put (dict);return dict; }
qemu-img.c +3 -3
@@ -298,7 +298,7 @@ static BlockBackend *img_open_file(const char *filename,if (fmt) { options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (fmt)); +qdict_put_str (options,"driver" , fmt); } blk =blk_new_open (filename,NULL , options, flags, &local_err);
@@ -2953,7 +2953,7 @@ static int img_rebase(int argc, char **argv)if (bs->backing_format[0 ] !='\0' ) { options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (bs->backing_format)); +qdict_put_str (options,"driver" , bs->backing_format); }bdrv_get_backing_filename (bs, backing_name,sizeof (backing_name));
@@ -2970,7 +2970,7 @@ static int img_rebase(int argc, char **argv)if (out_baseimg[0 ]) {if (out_basefmt) { options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (out_basefmt)); +qdict_put_str (options,"driver" , out_basefmt); }else { options =NULL ; }
qemu-io.c +1 -1
@@ -599,7 +599,7 @@ int main(int argc, char **argv) }else {if (format) { opts =qdict_new (); -qdict_put (opts,"driver" ,qstring_from_str (format)); +qdict_put_str (opts,"driver" , format); }openfile (argv[optind], flags, writethrough, opts); }
qemu-nbd.c +1 -1
@@ -882,7 +882,7 @@ int main(int argc, char **argv) }else {if (fmt) { options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (fmt)); +qdict_put_str (options,"driver" , fmt); } blk =blk_new_open (srcpath,NULL , options, flags, &local_err); }
qobject/qdict.c +1 -1
@@ -487,7 +487,7 @@ void qdict_set_default_str(QDict *dst, const char *key, const char *val)return ; } -qdict_put (dst, key,qstring_from_str (val)); +qdict_put_str (dst, key, val); } static void qdict_flatten_qdict(QDict *qdict, QDict *target,
target-s390x/cpu_models.c +2 -2
@@ -375,12 +375,12 @@ static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,static void qdict_add_disabled_feat (const char *name,void *opaque) { -qdict_put ((QDict *) opaque, name,qbool_from_bool (false)); +qdict_put_bool ((QDict *) opaque, name, false); }static void qdict_add_enabled_feat (const char *name,void *opaque) { -qdict_put ((QDict *) opaque, name,qbool_from_bool (true)); +qdict_put_bool ((QDict *) opaque, name, true); }/* convert S390CPUDef into a static CpuModelInfo */
tests/check-qdict.c +66 -66
300 lines not on this page · raw message
tests/test-qmp-commands.c +15 -15
@@ -92,7 +92,7 @@ static void test_dispatch_cmd(void)QDict *req =qdict_new ();QObject *resp; -qdict_put_obj (req,"execute" ,QOBJECT (qstring_from_str ("user_def_cmd" ))); +qdict_put_str (req,"execute" ,"user_def_cmd" ); resp =qmp_dispatch (QOBJECT (req));assert (resp !=NULL );
@@ -109,7 +109,7 @@ static void test_dispatch_cmd_failure(void)QDict *args =qdict_new ();QObject *resp; -qdict_put_obj (req,"execute" ,QOBJECT (qstring_from_str ("user_def_cmd2" ))); +qdict_put_str (req,"execute" ,"user_def_cmd2" ); resp =qmp_dispatch (QOBJECT (req));assert (resp !=NULL );
@@ -120,10 +120,10 @@ static void test_dispatch_cmd_failure(void)/* check that with extra arguments it throws an error */ req =qdict_new (); -qdict_put (args,"a" ,qint_from_int (66 )); +qdict_put_int (args,"a" ,66 );qdict_put (req,"arguments" , args); -qdict_put_obj (req,"execute" ,QOBJECT (qstring_from_str ("user_def_cmd" ))); +qdict_put_str (req,"execute" ,"user_def_cmd" ); resp =qmp_dispatch (QOBJECT (req));assert (resp !=NULL );
@@ -162,14 +162,14 @@ static void test_dispatch_cmd_io(void)QDict *ret_dict_dict2, *ret_dict_dict2_userdef;QInt *ret3; -qdict_put_obj (ud1a,"integer" ,QOBJECT (qint_from_int (42 ))); -qdict_put_obj (ud1a,"string" ,QOBJECT (qstring_from_str ("hello" ))); -qdict_put_obj (ud1b,"integer" ,QOBJECT (qint_from_int (422 ))); -qdict_put_obj (ud1b,"string" ,QOBJECT (qstring_from_str ("hello2" ))); -qdict_put_obj (args,"ud1a" ,QOBJECT (ud1a)); -qdict_put_obj (args,"ud1b" ,QOBJECT (ud1b)); -qdict_put_obj (req,"arguments" ,QOBJECT (args)); -qdict_put_obj (req,"execute" ,QOBJECT (qstring_from_str ("user_def_cmd2" ))); +qdict_put_int (ud1a,"integer" ,42 ); +qdict_put_str (ud1a,"string" ,"hello" ); +qdict_put_int (ud1b,"integer" ,422 ); +qdict_put_str (ud1b,"string" ,"hello2" ); +qdict_put (args,"ud1a" , ud1a); +qdict_put (args,"ud1b" , ud1b); +qdict_put (req,"arguments" , args); +qdict_put_str (req,"execute" ,"user_def_cmd2" ); ret =qobject_to_qdict (test_qmp_dispatch (req));
@@ -188,9 +188,9 @@ static void test_dispatch_cmd_io(void)assert (!strcmp (qdict_get_str (ret_dict_dict2,"string" ),"blah4" ));QDECREF (ret); -qdict_put (args3,"a" ,qint_from_int (66 )); +qdict_put_int (args3,"a" ,66 );qdict_put (req,"arguments" , args3); -qdict_put (req,"execute" ,qstring_from_str ("guest-get-time" )); +qdict_put_str (req,"execute" ,"guest-get-time" ); ret3 =qobject_to_qint (test_qmp_dispatch (req));assert (qint_get_int (ret3) ==66 );
@@ -242,7 +242,7 @@ static void test_dealloc_partial(void)Visitor *v; ud2_dict =qdict_new (); -qdict_put_obj (ud2_dict,"string0" ,QOBJECT (qstring_from_str (text))); +qdict_put_str (ud2_dict,"string0" , text); v =qobject_input_visitor_new (QOBJECT (ud2_dict), true);visit_type_UserDefTwo (v,NULL , &ud2, &err);
tests/test-qmp-event.c +15 -15
@@ -159,7 +159,7 @@ static void test_event_a(TestEventData *data, {QDict *d; d = data->expect; -qdict_put (d,"event" ,qstring_from_str ("EVENT_A" )); +qdict_put_str (d,"event" ,"EVENT_A" );qapi_event_send_event_a (&error_abort); }
@@ -168,7 +168,7 @@ static void test_event_b(TestEventData *data, {QDict *d; d = data->expect; -qdict_put (d,"event" ,qstring_from_str ("EVENT_B" )); +qdict_put_str (d,"event" ,"EVENT_B" );qapi_event_send_event_b (&error_abort); }
@@ -183,16 +183,16 @@ static void test_event_c(TestEventData *data, b.has_enum1 = false; d_b =qdict_new (); -qdict_put (d_b,"integer" ,qint_from_int (2 )); -qdict_put (d_b,"string" ,qstring_from_str ("test1" )); +qdict_put_int (d_b,"integer" ,2 ); +qdict_put_str (d_b,"string" ,"test1" ); d_data =qdict_new (); -qdict_put (d_data,"a" ,qint_from_int (1 )); +qdict_put_int (d_data,"a" ,1 );qdict_put (d_data,"b" , d_b); -qdict_put (d_data,"c" ,qstring_from_str ("test2" )); +qdict_put_str (d_data,"c" ,"test2" ); d = data->expect; -qdict_put (d,"event" ,qstring_from_str ("EVENT_C" )); +qdict_put_str (d,"event" ,"EVENT_C" );qdict_put (d,"data" , d_data);qapi_event_send_event_c (true,1 , true, &b,"test2" , &error_abort);
@@ -219,22 +219,22 @@ static void test_event_d(TestEventData *data, a.enum2 =ENUM_ONE_VALUE2 ; d_struct1 =qdict_new (); -qdict_put (d_struct1,"integer" ,qint_from_int (2 )); -qdict_put (d_struct1,"string" ,qstring_from_str ("test1" )); -qdict_put (d_struct1,"enum1" ,qstring_from_str ("value1" )); +qdict_put_int (d_struct1,"integer" ,2 ); +qdict_put_str (d_struct1,"string" ,"test1" ); +qdict_put_str (d_struct1,"enum1" ,"value1" ); d_a =qdict_new ();qdict_put (d_a,"struct1" , d_struct1); -qdict_put (d_a,"string" ,qstring_from_str ("test2" )); -qdict_put (d_a,"enum2" ,qstring_from_str ("value2" )); +qdict_put_str (d_a,"string" ,"test2" ); +qdict_put_str (d_a,"enum2" ,"value2" ); d_data =qdict_new ();qdict_put (d_data,"a" , d_a); -qdict_put (d_data,"b" ,qstring_from_str ("test3" )); -qdict_put (d_data,"enum3" ,qstring_from_str ("value3" )); +qdict_put_str (d_data,"b" ,"test3" ); +qdict_put_str (d_data,"enum3" ,"value3" ); d = data->expect; -qdict_put (d,"event" ,qstring_from_str ("EVENT_D" )); +qdict_put_str (d,"event" ,"EVENT_D" );qdict_put (d,"data" , d_data); qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3,
tests/test-qobject-output-visitor.c +3 -3
@@ -356,9 +356,9 @@ static void test_visitor_out_any(TestOutputVisitorData *data,visitor_reset (data); qdict =qdict_new (); -qdict_put (qdict,"integer" ,qint_from_int (-42 )); -qdict_put (qdict,"boolean" ,qbool_from_bool (true)); -qdict_put (qdict,"string" ,qstring_from_str ("foo" )); +qdict_put_int (qdict,"integer" ,-42 ); +qdict_put_bool (qdict,"boolean" , true); +qdict_put_str (qdict,"string" ,"foo" ); qobj =QOBJECT (qdict);visit_type_any (data->ov,NULL , &qobj, &error_abort);qobject_decref (qobj);
util/qemu-option.c +2 -4
@@ -1056,17 +1056,15 @@ void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)QDict *qemu_opts_to_qdict (QemuOpts *opts,QDict *qdict) {QemuOpt *opt; -QObject *val;if (!qdict) { qdict =qdict_new (); }if (opts->id) { -qdict_put (qdict,"id" ,qstring_from_str (opts->id)); +qdict_put_str (qdict,"id" , opts->id); } QTAILQ_FOREACH(opt, &opts->head, next) { - val =QOBJECT (qstring_from_str (opt->str)); -qdict_put_obj (qdict, opt->name, val); +qdict_put_str (qdict, opt->name, opt->str); }return qdict; }
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [Qemu-devel] [PATCH 02/36] qdict: Add convenience helpers for wrapped puts
Quoted 6 lines · Eric Blake
Quite a few users of
qdict_put()were manually wrapping a non-QObject. We can make such call-sites shorter, by providing common macros to do the tedious work. Also shorten nearby qdict_put_obj(,,QOBJECT()) sequences.Signed-off-by Eric Blake <[email protected]>
Thanks, the code looks much better now :)
Berto
PATCH 3/36 qlist: Add convenience helpers for wrapped appends
In reply to Eric BlakeSimilar to the qdict additions of the previous patch, although this time there are not as many clients.
--- include/qapi/qmp/qlist.h | 8 ++++++++ tests/check-qdict.c | 10 +++++----- tests/check-qlist.c | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-)
include/qapi/qmp/qlist.h +8 -0
@@ -29,6 +29,14 @@ typedef struct QList {#define qlist_append (qlist, obj) \ qlist_append_obj(qlist, QOBJECT(obj)) +/* Helpers for int, bool, and const char*. */ +#define qlist_append_int (qlist, value) \ + qlist_append(qlist, qint_from_int(value)) +#define qlist_append_bool (qlist, value) \ + qlist_append(qlist, qbool_from_bool(value)) +#define qlist_append_str (qlist, value) \ + qlist_append(qlist, qstring_from_str(value)) +#define QLIST_FOREACH_ENTRY (qlist, var) \ for ((var) = ((qlist)->head.tqh_first); \ (var); \
tests/check-qdict.c +5 -5
@@ -297,11 +297,11 @@ static void qdict_flatten_test(void)qdict_put_int (dict1,"a" ,0 );qdict_put_int (dict1,"b" ,1 ); -qlist_append_obj (list1,QOBJECT (qint_from_int (23 ))); -qlist_append_obj (list1,QOBJECT (qint_from_int (66 ))); -qlist_append_obj (list1,QOBJECT (dict1)); -qlist_append_obj (list2,QOBJECT (qint_from_int (42 ))); -qlist_append_obj (list2,QOBJECT (list1)); +qlist_append_int (list1,23 ); +qlist_append_int (list1,66 ); +qlist_append (list1, dict1); +qlist_append_int (list2,42 ); +qlist_append (list2, list1);qdict_put_int (dict2,"c" ,2 );qdict_put_int (dict2,"d" ,3 );
tests/check-qlist.c +1 -1
@@ -74,7 +74,7 @@ static void qlist_destroy_test(void) qlist =qlist_new ();for (i =0 ; i <42 ; i++) -qlist_append (qlist,qint_from_int (i)); +qlist_append_int (qlist, i);QDECREF (qlist); }
Signature · 1 line
2.7.4
PATCH 4/36 qmp-event: Avoid dynamic JSON
In reply to Eric BlakeThe qobject_from_jsonf() function implements a pseudo-printf language for creating a QObject through the extension of dynamic JSON; however, it is hard-coded to only parse a subset of formats understood by -Wformat and is not a straight synonym to bare printf(). During a recent cleanup due to problems caused by PRId64, it was questioned whether the maintenance burden of keeping the dynamic JSON extension can be counterbalanced by converting code to use alternative ways of describing QObject.
For this particular use of dynamic JSON, it is just as simple to open-code the correct QObject creation.
--- qapi/qmp-event.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
qapi/qmp-event.c +8 -5
@@ -15,6 +15,8 @@#include "qemu-common.h" #include "qapi/qmp-event.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qint.h" #include "qapi/qmp/qstring.h" #include "qapi/qmp/qjson.h"
@@ -33,15 +35,16 @@ QMPEventFuncEmit qmp_event_get_func_emit(void)static void timestamp_put (QDict *qdict) {int err; -QObject *obj; +QDict *stamp =qdict_new ();qemu_timeval tv; err =qemu_gettimeofday (&tv);/* Put -1 to indicate failure of getting host time */ - obj =qobject_from_jsonf ("{ 'seconds': %lld, 'microseconds': %lld }" , - err <0 ?-1LL : (long long )tv.tv_sec, - err <0 ?-1LL : (long long )tv.tv_usec); -qdict_put_obj (qdict,"timestamp" , obj); +qdict_put_int (stamp,"seconds" , + err <0 ?-1LL : (long long )tv.tv_sec); +qdict_put_int (stamp,"microseconds" , + err <0 ?-1LL : (long long )tv.tv_usec); +qdict_put (qdict,"timestamp" , stamp); }/*
Signature · 1 line
2.7.4
PATCH 5/36 qmp-dispatch: Avoid dynamic JSON
In reply to Eric BlakeThe qobject_from_jsonf() function implements a pseudo-printf language for creating a QObject through the extension of dynamic JSON; however, it is hard-coded to only parse a subset of formats understood by -Wformat and is not a straight synonym to bare printf(). During a recent cleanup due to problems caused by PRId64, it was questioned whether the maintenance burden of keeping the dynamic JSON extension can be counterbalanced by converting code to use alternative ways of describing QObject.
For these two conversions, the open-coded QObject creation is a bit more verbose, but performs slightly faster than going through a parse; futhermore, there is no correpsonding QAPI C type to make for an easier representation than open-coding.
--- monitor.c | 10 +++++++--- qapi/qmp-dispatch.c | 8 +++++--- 2 files changed, 12 insertions(+), 6 deletions(-)
monitor.c +7 -3
@@ -3839,11 +3839,15 @@ void monitor_resume(Monitor *mon)static QObject *get_qmp_greeting (void ) {QObject *ver =NULL ; +QDict *sub, *ret;qmp_marshal_query_version (NULL , &ver,NULL ); - -return qobject_from_jsonf ("{'QMP': {'version': %p, 'capabilities': []}}" , - ver); + sub =qdict_new (); +qdict_put_obj (sub,"version" , ver); +qdict_put (sub,"capabilities" ,qlist_new ()); + ret =qdict_new (); +qdict_put (ret,"QMP" , sub); +return QOBJECT (ret); }static void monitor_qmp_event (void *opaque,int event)
qapi/qmp-dispatch.c +5 -3
@@ -111,9 +111,11 @@ static QObject *do_qmp_dispatch(QObject *request, Error **errp)QObject *qmp_build_error_object (Error *err) { -return qobject_from_jsonf ("{ 'class': %s, 'desc': %s }" , - QapiErrorClass_lookup[error_get_class (err)], -error_get_pretty (err)); +QDict *ret =qdict_new (); + +qdict_put_str (ret,"class" , QapiErrorClass_lookup[error_get_class (err)]); +qdict_put_str (ret,"desc" ,error_get_pretty (err)); +return QOBJECT (ret); }QObject *qmp_dispatch (QObject *request)
Signature · 1 line
2.7.4
PATCH 6/36 qobject-input-visitor: Avoid dynamic JSON in tests
In reply to Eric BlakeAs argued elsewhere, we want to get rid of the pseudo-printf dynamic JSON parsing of qobject_from_jsonv(). In the qobject-input visitor tests, it is trivial to convert to using the direct qobject_from_json() or a hand-built QObject.
While at it, wrap some long lines noticed while auditing callers.
--- tests/test-qobject-input-strict.c | 37 +++---------------- tests/test-qobject-input-visitor.c | 75 ++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 75 deletions(-)
tests/test-qobject-input-strict.c +5 -32
@@ -41,16 +41,15 @@ static void validate_teardown(TestInputVisitorData *data, } } -/* The various test_init functions are provided instead of a test setup +/* The test_init function is provided instead of a test setup function so that the JSON string used by the tests are kept in the test functions (and not in main()). */ -static Visitor *validate_test_init_internal (TestInputVisitorData *data, -const char *json_string, -va_list *ap) +static Visitor *validate_test_init (TestInputVisitorData *data, +const char *json_string) {validate_teardown (data,NULL ); - data->obj =qobject_from_jsonv (json_string, ap); + data->obj =qobject_from_json (json_string);g_assert (data->obj); data->qiv =qobject_input_visitor_new (data->obj, true);
@@ -58,32 +57,6 @@ static Visitor *validate_test_init_internal(TestInputVisitorData *data,return data->qiv; } -static GCC_FMT_ATTR(2, 3) -Visitor *validate_test_init (TestInputVisitorData *data, -const char *json_string, ...) -{ -Visitor *v; -va_list ap; - -va_start (ap, json_string); - v =validate_test_init_internal (data, json_string, &ap); -va_end (ap); -return v; -} - -/* similar to validate_test_init(), but does not expect a string -* literal/format json_string argument and so can be used for -* programatically generated strings (and we can't pass in programatically -* generated strings via %s format parameters since qobject_from_jsonv() -* will wrap those in double-quotes and treat the entire object as a -* string) -*/ -static Visitor *validate_test_init_raw (TestInputVisitorData *data, -const char *json_string) -{ -return validate_test_init_internal (data, json_string,NULL ); -} -static void test_validate_struct (TestInputVisitorData *data,const void *unused)
@@ -315,7 +288,7 @@ static void do_test_validate_qmp_introspect(TestInputVisitorData *data,SchemaInfoList *schema =NULL ;Visitor *v; - v =validate_test_init_raw (data, schema_json); + v =validate_test_init (data, schema_json);visit_type_SchemaInfoList (v,NULL , &schema, &error_abort);g_assert (schema);
tests/test-qobject-input-visitor.c +32 -43
182 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 7/36 fdc-test: Avoid deprecated 'change' command
In reply to Eric BlakeUse the preferred blockdev-change-medium command instead.
--- tests/fdc-test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tests/fdc-test.c +3 -2
@@ -298,8 +298,9 @@ static void test_media_insert(void)/* Insert media in drive. DSKCHK should not be reset until a step pulse * is sent. */ -qmp_discard_response ("{'execute':'change', 'arguments':{" -" 'device':'floppy0', 'target': %s, 'arg': 'raw' }}" , +qmp_discard_response ("{'execute':'blockdev-change-medium', 'arguments':{" +" 'device':'floppy0', 'filename': %s, " +"'format': 'raw' }}" , test_image); dir =inb (FLOPPY_BASE + reg_dir);
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 07/36] fdc-test: Avoid deprecated 'change' command
Quoted 14 lines · Eric Blake
Use the preferred blockdev-change-medium command instead.
Signed-off-by Eric Blake <[email protected]>--- tests/fdc-test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)tests/fdc-test.c +0
-0@@ -298,8 +298,9 @@ static void test_media_insert(void)
tests/fdc-test.c static void test_media_insert(void) · line 306
Quoted 10 lines
tests/fdc-test.c +3
-2/* Insert media in drive. DSKCHK should not be reset until a step pulse * is sent. */ -qmp_discard_response ("{'execute':'change', 'arguments':{" -" 'device':'floppy0', 'target': %s, 'arg': 'raw' }}" , +qmp_discard_response ("{'execute':'blockdev-change-medium', 'arguments':{" +" 'device':'floppy0', 'filename': %s, " +"'format': 'raw' }}" , test_image); dir =inb (FLOPPY_BASE + reg_dir);
Sure, why not?
PATCH 8/36 test-qga: Actually test 0xff sync bytes
In reply to Eric BlakeCommit 62c39b3 introduced test-qga, and at face value, appears to be testing the 'guest-sync' behavior that is recommended for guests in sending 0xff to QGA to force the parser to reset. But this aspect of the test has never actually done anything: the qmp_fd() call chain converts its string argument into QObject, then converts that QObject back to the actual string that is sent over the wire - and the conversion process silently drops the 0xff byte from the string sent to QGA, thus never resetting the QGA parser.
An upcoming patch will get rid of the wasteful round trip through QObject, at which point the string in test-qga will be directly sent over the wire.
But fixing qmp_fd() to actually send 0xff over the wire is not all we have to do - the actual QMP parser loudly complains that 0xff is not valid JSON, and sends an error message _prior_ to actually parsing the 'guest-sync' or 'guest-sync-delimited' command. With 'guest-sync', we cannot easily tell if this error message is a result of our command - which is WHY we invented the 'guest-sync-delimited' command. So for the testsuite, fix things to only check 0xff behavior on 'guest-sync-delimited', and to loop until we've consumed all garbage prior to the requested delimiter, which matches the documented actions that a real QGA client is supposed to do.
Ideally, we'd fix the QGA JSON parser to silently ignore 0xff rather than sending an error message back, at which point we could enhance this test for 'guest-sync' as well as for 'guest-sync-delimited'. But for the sake of this patch, our testing of 'guest-sync' is no worse than it was pre-patch, because we have never been sending 0xff over the wire in the first place.
--- tests/libqtest.c | 8 ++++++++ tests/test-qga.c | 12 +++++++----- 2 files changed, 15 insertions(+), 5 deletions(-)
tests/libqtest.c +8 -0
@@ -430,6 +430,14 @@ void qmp_fd_sendv(int fd, const char *fmt, va_list ap)va_list ap_copy;QObject *qobj; +/* qobject_from_jsonv() silently eats leading 0xff as invalid +* JSON, but we want to test sending them over the wire to force +* resyncs */ +if (*fmt =='\377' ) { +socket_send (fd, fmt,1 ); + fmt++; + } +/* Going through qobject ensures we escape strings properly. * This seemingly unnecessary copy is required in case va_list * is an array type.
tests/test-qga.c +7 -5
@@ -151,9 +151,11 @@ static void test_qga_sync_delimited(gconstpointer fix)qmp_fd_send (fixture->fd, cmd);g_free (cmd); - v =read (fixture->fd, &c,1 ); - g_assert_cmpint(v, ==, 1); - g_assert_cmpint(c, ==, 0xff); +/* Read and ignore garbage until resynchronized */ +do { + v =read (fixture->fd, &c,1 ); + g_assert_cmpint(v, ==, 1); + }while (c !=0xff ); ret =qmp_fd_receive (fixture->fd);g_assert_nonnull (ret);
@@ -172,8 +174,8 @@ static void test_qga_sync(gconstpointer fix)QDict *ret;gchar *cmd; - cmd =g_strdup_printf ("%c{'execute': 'guest-sync'," -" 'arguments': {'id': %u } }" ,0xff , r); + cmd =g_strdup_printf ("{'execute': 'guest-sync'," +" 'arguments': {'id': %u } }" , r); ret =qmp_fd (fixture->fd, cmd);g_free (cmd);
Signature · 1 line
2.7.4
PATCH 9/36 qtest: Add a new helper qmp_cmd() and friends
In reply to Eric BlakeThe qobject_from_jsonv() function implements a pseudo-printf language for creating a QObject through the extension of dynamic JSON; however, it is hard-coded to only parse a subset of formats understood by -Wformat and is not a straight synonym to bare printf(). During a recent cleanup due to problems caused by PRId64, it was questioned whether the maintenance burden of keeping the dynamic JSON extension can be counterbalanced by converting code to use alternative ways of describing QObject.
This particular patch just adds a couple of convenience methods, based on the observation that most testsuite use of dynamic JSON is nested within the arguments portion of a larger command dictionary. Separating the task of generating the arguments from the boilerplate of generating the outer dictionary reduces the conversion effort needed in the various clients.
Because the hand-built QObject might contain '%' characters embedded in strings, we can't directly convert the QObject to string and pass that as a format argument. So for now, we just use a hack of a format string of "%p" to pass the resulting QObject through various varargs calls and into the dynamic JSON parser, where it will then be regurgitated as the QObject that gets converted into the final string to send over the wire. A later patch will clean up libqtest to accept direct string commands, at which point we can tweak qmp_cmd() to avoid the extra no-op trip through the JSON parser.
--- tests/libqtest.h | 31 +++++++++++++++++++++++++++++++ tests/libqtest.c | 26 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)
tests/libqtest.h +31 -0
@@ -58,6 +58,17 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);QDict *qtest_qmp (QTestState *s,const char *fmt, ...);/** +* qtest_qmp_cmd: +* @s: #QTestState instance to operate on. +* @cmd: Command name to send +* @args: Arguments to transfer to the command, or NULL. +* +* Sends a QMP message to QEMU and returns the response. Calling this will +* reduce the reference count of @args. +*/ +QDict *qtest_qmp_cmd (QTestState *s,const char *cmd,QDict *args); + +/** * qtest_async_qmp: * @s: #QTestState instance to operate on. * @fmt...: QMP message to send to qemu
@@ -532,6 +543,16 @@ static inline void qtest_end(void)QDict *qmp (const char *fmt, ...);/** +* qmp_cmd: +* @cmd: Command name to send +* @args: Arguments to transfer to the command, or NULL. +* +* Sends a QMP message to QEMU and returns the response. Calling this will +* reduce the reference count of @args. +*/ +QDict *qmp_cmd (const char *cmd,QDict *args); + +/** * qmp_async: * @fmt...: QMP message to send to qemu *
@@ -548,6 +569,16 @@ void qmp_async(const char *fmt, ...);void qmp_discard_response (const char *fmt, ...);/** +* qmp_cmd_discard_response: +* @cmd: Command name to send +* @args: Arguments to transfer to the command, or NULL. +* +* Sends a QMP message to QEMU and consumes the response. Calling this will +* reduce the reference count of @args. +*/ +void qmp_cmd_discard_response (const char *cmd,QDict *args); + +/** * qmp_receive: * * Reads a QMP message from QEMU and returns the response.
tests/libqtest.c +26 -0
@@ -515,6 +515,18 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...)return response; } +QDict *qtest_qmp_cmd (QTestState *s,const char *cmd,QDict *args) +{ +QDict *dict =qdict_new (); + +if (!args) { + args =qdict_new (); + } +qdict_put_str (dict,"execute" , cmd); +qdict_put (dict,"arguments" , args); +return qtest_qmp (s,"%p" ,QOBJECT (dict)); +} +void qtest_async_qmp (QTestState *s,const char *fmt, ...) {va_list ap;
@@ -894,6 +906,11 @@ QDict *qmp(const char *fmt, ...)return response; } +QDict *qmp_cmd (const char *cmd,QDict *args) +{ +return qtest_qmp_cmd (global_qtest, cmd, args); +} +void qmp_async (const char *fmt, ...) {va_list ap;
@@ -911,6 +928,15 @@ void qmp_discard_response(const char *fmt, ...)qtest_qmpv_discard_response (global_qtest, fmt, ap);va_end (ap); } + +void qmp_cmd_discard_response (const char *cmd,QDict *args) +{ +QDict *response; + + response =qmp_cmd (cmd, args); +QDECREF (response); +} +char *hmp (const char *fmt, ...) {va_list ap;
Signature · 1 line
2.7.4
PATCH 10/36 qtest: Avoid dynamic JSON in libqtest
In reply to Eric BlakeThe qobject_from_jsonf() function implements a pseudo-printf language for creating a QObject through the extension of dynamic JSON; however, it is hard-coded to only parse a subset of formats understood by -Wformat and is not a straight synonym to bare printf(). During a recent cleanup due to problems caused by PRId64, it was questioned whether the maintenance burden of keeping the dynamic JSON extension can be counterbalanced by converting code to use alternative ways of describing QObject.
The only use of dynamic JSON in the testsuite glue code was the creation of a human monitor command over QMP, and is a relatively straightforward update.
--- tests/libqtest.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tests/libqtest.c +3 -3
@@ -580,11 +580,11 @@ char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)char *cmd;QDict *resp;char *ret; +QDict *args =qdict_new (); cmd =g_strdup_vprintf (fmt, ap); - resp =qtest_qmp (s,"{'execute': 'human-monitor-command'," -" 'arguments': {'command-line': %s}}" , - cmd); +qdict_put_str (args,"command-line" , cmd); + resp =qtest_qmp_cmd (s,"human-monitor-command" , args); ret =g_strdup (qdict_get_try_str (resp,"return" ));g_assert (ret);QDECREF (resp);
Signature · 1 line
2.7.4
PATCH 11/36 qapi: Add QAPI_TO_QOBJECT() convenience macro
In reply to Eric BlakeWe have several callers that want to convert a QAPI C type into a QObject; right now all of them have to copy the same boilerplate of creating a visitor. A convenience macro makes this paradigm easier to type.
--- include/qapi/qobject-output-visitor.h | 19 +++++++++++++++++++ qapi/qobject-output-visitor.c | 16 ++++++++++++++++ 2 files changed, 35 insertions(+)
include/qapi/qobject-output-visitor.h +19 -0
@@ -16,6 +16,8 @@#include "qapi/visitor.h" #include "qapi/qmp/qobject.h" +#include "qapi/error.h" +#include "qapi-visit.h" typedef struct QObjectOutputVisitor QObjectOutputVisitor ;
@@ -27,4 +29,21 @@ typedef struct QObjectOutputVisitor QObjectOutputVisitor;*/ Visitor *qobject_output_visitor_new (QObject **result); +QObject *qapi_to_qobject (const void *src, +void (*visit_type)(Visitor *,const char *, +void **,Error **), +Error **errp); + +/* +* Create a QObject from a QAPI object @src of the given @type. +* +* Not usable on QAPI scalars (integers, strings, enums), nor on a +* QAPI object that references the 'any' type. @src must not be NULL. +*/ +#define QAPI_TO_QOBJECT (type, src, err) \ + (qapi_to_qobject(1 ? src : (type *)NULL, \ + (void (*)(Visitor *, const char *, void**, \ + Error **))visit_type_ ## type, \ + err)) +#endif
qapi/qobject-output-visitor.c +16 -0
@@ -252,3 +252,19 @@ Visitor *qobject_output_visitor_new(QObject **result)return &v->visitor; } + +QObject *qapi_to_qobject (const void *src, +void (*visit_type)(Visitor *,const char *, +void **,Error **), +Error **errp) +{ +Visitor *v; +void *s = (void *) src;/* cast away const */ +QObject *dst =NULL ; + + v =qobject_output_visitor_new (&dst); +visit_type (v,NULL , &s, &error_abort); +visit_complete (v, &dst); +visit_free (v); +return dst; +}
Signature · 1 line
2.7.4
PATCH 12/36 nbd: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- block/nbd.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
block/nbd.c +1 -5
@@ -503,7 +503,6 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)BDRVNBDState *s = bs->opaque;QDict *opts =qdict_new ();QObject *saddr_qdict; -Visitor *ov;const char *host =NULL , *port =NULL , *path =NULL ;if (s->saddr->type ==SOCKET_ADDRESS_KIND_INET ) {
@@ -532,10 +531,7 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) "nbd://%s:%s", host, port); } - ov =qobject_output_visitor_new (&saddr_qdict); -visit_type_SocketAddress (ov,NULL , &s->saddr, &error_abort); -visit_complete (ov, &saddr_qdict); -visit_free (ov); + saddr_qdict =QAPI_TO_QOBJECT (SocketAddress, s->saddr, &error_abort);assert (qobject_type (saddr_qdict) ==QTYPE_QDICT );qdict_put_obj (opts,"server" , saddr_qdict);
Signature · 1 line
2.7.4
PATCH 13/36 nfs: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- block/nfs.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
block/nfs.c +1 -5
@@ -766,7 +766,6 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)NFSClient *client = bs->opaque;QDict *opts =qdict_new ();QObject *server_qdict; -Visitor *ov;qdict_put_str (opts,"driver" ,"nfs" );
@@ -787,9 +786,7 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options) "nfs://%s%s", client->server->host, client->path); } - ov =qobject_output_visitor_new (&server_qdict); -visit_type_NFSServer (ov,NULL , &client->server, &error_abort); -visit_complete (ov, &server_qdict); + server_qdict =QAPI_TO_QOBJECT (NFSServer, client->server, &error_abort);assert (qobject_type (server_qdict) ==QTYPE_QDICT );qdict_put_obj (opts,"server" , server_qdict);
@@ -814,7 +811,6 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)qdict_put_int (opts,"debug" , client->debug); } -visit_free (ov);qdict_flatten (opts); bs->full_open_options = opts; }
Signature · 1 line
2.7.4
PATCH 14/36 qapi: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- block/qapi.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
block/qapi.c +2 -4
@@ -691,15 +691,13 @@ void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, ImageInfoSpecific *info_spec) {QObject *obj, *data; -Visitor *v =qobject_output_visitor_new (&obj); -visit_type_ImageInfoSpecific (v,NULL , &info_spec, &error_abort); -visit_complete (v, &obj); + obj =QAPI_TO_QOBJECT (ImageInfoSpecific, info_spec, &error_abort); +assert (qobject_type (obj) ==QTYPE_QDICT ); data =qdict_get (qobject_to_qdict (obj),"data" );dump_qobject (func_fprintf, f,1 , data);qobject_decref (obj); -visit_free (v); } void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
Signature · 1 line
2.7.4
PATCH 15/36 blockdev: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- blockdev.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
blockdev.c +4 -10
@@ -3801,29 +3801,27 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp) {BlockDriverState *bs;QObject *obj; -Visitor *v =qobject_output_visitor_new (&obj);QDict *qdict;Error *local_err =NULL ; -visit_type_BlockdevOptions (v,NULL , &options, &local_err); + obj =QAPI_TO_QOBJECT (BlockdevOptions, options, &local_err);if (local_err) {error_propagate (errp, local_err); - goto fail; +return ; } -visit_complete (v, &obj); qdict =qobject_to_qdict (obj);qdict_flatten (qdict);if (!qdict_get_try_str (qdict,"node-name" )) {error_setg (errp,"'node-name' must be specified for the root node" ); - goto fail; +return ; } bs =bds_tree_init (qdict, errp);if (!bs) { - goto fail; +return ; }QTAILQ_INSERT_TAIL (&monitor_bdrv_states, bs, monitor_list);
@@ -3832,11 +3830,7 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)QTAILQ_REMOVE (&monitor_bdrv_states, bs, monitor_list);bdrv_unref (bs);error_setg (errp,"blockdev-add doesn't support encrypted devices" ); - goto fail; } - -fail: -visit_free (v); }void qmp_x_blockdev_del (const char *node_name,Error **errp)
Signature · 1 line
2.7.4
PATCH 16/36 qapi: Promote blockdev-change-medium arguments to QAPI type
In reply to Eric BlakeHaving a named rather than anonymous C type will make it easier to improve the testsuite in a later patch. No semantic change, to any of the existing code or to the introspection output.
--- qapi/block-core.json | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
qapi/block-core.json +10 -1
@@ -2592,6 +2592,15 @@ # combines blockdev-open-tray, x-blockdev-remove-medium, # x-blockdev-insert-medium and blockdev-close-tray). # +# Since: 2.5 +## +{ 'command': 'blockdev-change-medium', + 'data': 'BlockdevChangeMedium' } + + +## +# @BlockdevChangeMedium: +# # @device: #optional Block device name (deprecated, use @id instead) # # @id: #optional The name or QOM path of the guest device
@@ -2607,7 +2616,7 @@ # # Since: 2.5 ## -{ 'command': 'blockdev-change-medium', +{ 'struct': 'BlockdevChangeMedium', 'data': { '*device': 'str', '*id': 'str', 'filename': 'str',
Signature · 1 line
2.7.4
PATCH 17/36 qtest: Avoid dynamic JSON in ahci-test
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use a hand-built QDict.
Rather than build up a QDict by manual qdict_put*() calls, we can let QAPI do the work for us. The result is more lines of code to initialize the QAPI struct, but the result will force us to track any changes to the qapi (whereas the dynamic JSON string would not detect qapi changes until runtime).
--- tests/ahci-test.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
tests/ahci-test.c +21 -5
@@ -32,6 +32,8 @@#include "qemu-common.h" #include "qemu/host-utils.h" +#include "qapi/qmp/qjson.h" +#include "qapi/qobject-output-visitor.h" #include "hw/pci/pci_ids.h" #include "hw/pci/pci_regs.h"
@@ -1576,6 +1578,7 @@ static void test_atapi_tray(void)uint8_t port, sense, asc;uint64_t iso_size =ATAPI_SECTOR_SIZE ;QDict *rsp; +QObject *args; fd =prepare_iso (iso_size, &tx, &iso); ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "
@@ -1607,11 +1610,24 @@ static void test_atapi_tray(void)atapi_wait_tray (true);/* Re-insert media */ -qmp_discard_response ("{'execute': 'blockdev-add', " -"'arguments': {'node-name': 'node0', " -"'driver': 'raw', " -"'file': { 'driver': 'file', " -"'filename': %s }}}" , iso); + { +BlockdevRef ref = { + .type =QTYPE_QDICT , + .u.definition = { + .driver =BLOCKDEV_DRIVER_FILE , + .u.file.filename = iso, + }, + }; +BlockdevOptions opts = { + .has_node_name = true, + .node_name = (char *)"node0" , + .driver =BLOCKDEV_DRIVER_RAW , + .u.raw.file = &ref, + }; + args =QAPI_TO_QOBJECT (BlockdevOptions, &opts, &error_abort); + } + +qmp_cmd_discard_response ("blockdev-add" ,qobject_to_qdict (args));qmp_discard_response ("{'execute': 'x-blockdev-insert-medium'," "'arguments': { 'device': 'drive0', " "'node-name': 'node0' }}" );
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 17/36] qtest: Avoid dynamic JSON in ahci-test
Quoted 10 lines · Eric Blake
As argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to
qobject_from_jsonv()to instead use a hand-built QDict.Rather than build up a QDict by manual qdict_put*() calls, we can let QAPI do the work for us. The result is more lines of code to initialize the QAPI struct, but the result will force us to track any changes to the qapi (whereas the dynamic JSON string would not detect qapi changes until runtime).
Benefit of doubt that you're right.
Quoted 47 lines
Signed-off-by Eric Blake <[email protected]>--- tests/ahci-test.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)tests/ahci-test.c +14
-5@@ -32,6 +32,8 @@#include "qemu-common.h" #include "qemu/host-utils.h" +#include "qapi/qmp/qjson.h" +#include "qapi/qobject-output-visitor.h" #include "hw/pci/pci_ids.h" #include "hw/pci/pci_regs.h" @@ -1576,6 +1578,7 @@ static void test_atapi_tray(void)uint8_t port, sense, asc;uint64_t iso_size =ATAPI_SECTOR_SIZE ;QDict *rsp; +QObject *args; fd =prepare_iso (iso_size, &tx, &iso); ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "@@ -1607,11 +1610,24 @@ static void test_atapi_tray(void)atapi_wait_tray (true);/* Re-insert media */ -qmp_discard_response ("{'execute': 'blockdev-add', " -"'arguments': {'node-name': 'node0', " -"'driver': 'raw', " -"'file': { 'driver': 'file', " -"'filename': %s }}}" , iso); + { +BlockdevRef ref = { + .type =QTYPE_QDICT , + .u.definition = { + .driver =BLOCKDEV_DRIVER_FILE , + .u.file.filename = iso, + }, + }; +BlockdevOptions opts = { + .has_node_name = true, + .node_name = (char *)"node0" ,
tests/ahci-test.c static void test_atapi_tray(void) · line 1633
Quoted 10 lines
tests/ahci-test.c +7
-0+ .driver = BLOCKDEV_DRIVER_RAW, + .u.raw.file = &ref, + }; + args =QAPI_TO_QOBJECT (BlockdevOptions, &opts, &error_abort); + } + +qmp_cmd_discard_response ("blockdev-add" ,qobject_to_qdict (args));qmp_discard_response ("{'execute': 'x-blockdev-insert-medium'," "'arguments': { 'device': 'drive0', " "'node-name': 'node0' }}" );
I assume qmp_cmd_discard_response takes ownership of the object we just built?
Assuming yes:
Re: [Qemu-devel] [PATCH 17/36] qtest: Avoid dynamic JSON in ahci-test
Quoted 30 lines · John Snow
Quoted 10 lines · Eric Blake
As argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to
qobject_from_jsonv()to instead use a hand-built QDict.Rather than build up a QDict by manual qdict_put*() calls, we can let QAPI do the work for us. The result is more lines of code to initialize the QAPI struct, but the result will force us to track any changes to the qapi (whereas the dynamic JSON string would not detect qapi changes until runtime).
Benefit of doubt that you're right.
tests/ahci-test.c static void test_atapi_tray(void) · line 1633
Quoted 13 lines
Signed-off-by Eric Blake <[email protected]>--- tests/ahci-test.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)tests/ahci-test.c +4
-0+ args =QAPI_TO_QOBJECT (BlockdevOptions, &opts, &error_abort); + } + +qmp_cmd_discard_response ("blockdev-add" ,qobject_to_qdict (args));qmp_discard_response ("{'execute': 'x-blockdev-insert-medium'," "'arguments': { 'device': 'drive0', " "'node-name': 'node0' }}" );
Quoted 2 lines
I assume
qmp_cmd_discard_responsetakes ownership of the object we just built?
Yes. I had to track that down myself, which is why in patch 9, I documented it:
/**
+ * qmp_cmd_discard_response:
+ * @cmd: Command name to send
+ * @args: Arguments to transfer to the command, or NULL.
+ *
+ * Sends a QMP message to QEMU and consumes the response. Calling this will
+ * reduce the reference count of @args.
+ */
+void qmp_cmd_discard_response(const char *cmd, QDict *args);
Quoted 4 lines
Assuming yes:
Reviewed-by John Snow <[email protected]>
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 18/36 qtest: Avoid dynamic JSON in fdc-test
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use a hand-built QDict.
Rather than build up a QDict by manual qdict_put*() calls, we can let QAPI do the work for us. The result is more lines of code to initialize the QAPI struct, but the result will force us to track any changes to the qapi (whereas the dynamic JSON string would not detect qapi changes until runtime).
--- tests/fdc-test.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
tests/fdc-test.c +12 -4
@@ -27,6 +27,8 @@#include "libqtest.h" #include "qemu-common.h" +#include "qapi/qmp/qjson.h" +#include "qapi/qobject-output-visitor.h" #define TEST_IMAGE_SIZE 1440 * 1024
@@ -295,13 +297,19 @@ static void test_read_without_media(void)static void test_media_insert (void ) {uint8_t dir; +QObject *args; +BlockdevChangeMedium bcm = { + .has_device = true, + .device = (char *)"floppy0" , + .filename = test_image, + .has_format = true, + .format = (char *)"raw" , + };/* Insert media in drive. DSKCHK should not be reset until a step pulse * is sent. */ -qmp_discard_response ("{'execute':'blockdev-change-medium', 'arguments':{" -" 'device':'floppy0', 'filename': %s, " -"'format': 'raw' }}" , - test_image); + args =QAPI_TO_QOBJECT (BlockdevChangeMedium, &bcm, &error_abort); +qmp_cmd_discard_response ("blockdev-change-medium" ,qobject_to_qdict (args)); dir =inb (FLOPPY_BASE + reg_dir);assert_bit_set (dir,DSKCHG );
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 18/36] qtest: Avoid dynamic JSON in fdc-test
Quoted 43 lines · Eric Blake
As argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to
qobject_from_jsonv()to instead use a hand-built QDict.Rather than build up a QDict by manual qdict_put*() calls, we can let QAPI do the work for us. The result is more lines of code to initialize the QAPI struct, but the result will force us to track any changes to the qapi (whereas the dynamic JSON string would not detect qapi changes until runtime).
Signed-off-by Eric Blake <[email protected]>--- tests/fdc-test.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)tests/fdc-test.c +10
-0@@ -27,6 +27,8 @@#include "libqtest.h" #include "qemu-common.h" +#include "qapi/qmp/qjson.h" +#include "qapi/qobject-output-visitor.h" #define TEST_IMAGE_SIZE 1440 * 1024@@ -295,13 +297,19 @@ static void test_read_without_media(void)static void test_media_insert (void ) {uint8_t dir; +QObject *args; +BlockdevChangeMedium bcm = { + .has_device = true, + .device = (char *)"floppy0" , + .filename = test_image, + .has_format = true, + .format = (char *)"raw" , + };/* Insert media in drive. DSKCHK should not be reset until a step pulse
tests/fdc-test.c static void test_read_without_media(void) · line 315
Quoted 10 lines
tests/fdc-test.c +2
-4* is sent. */ -qmp_discard_response ("{'execute':'blockdev-change-medium', 'arguments':{" -" 'device':'floppy0', 'filename': %s, " -"'format': 'raw' }}" , - test_image); + args =QAPI_TO_QOBJECT (BlockdevChangeMedium, &bcm, &error_abort); +qmp_cmd_discard_response ("blockdev-change-medium" ,qobject_to_qdict (args)); dir =inb (FLOPPY_BASE + reg_dir);assert_bit_set (dir,DSKCHG );
Same statement as on #17:
PATCH 19/36 qtest: Change qmp_discard_response() to drop varargs
In reply to Eric BlakeNow that no more clients are passing variadic arguments, we can update the contract to state that we will be using normal JSON parsing rather than dynamic JSON for any client using this wrapper.
--- tests/libqtest.h | 18 ++++-------------- tests/libqtest.c | 22 +++++----------------- 2 files changed, 9 insertions(+), 31 deletions(-)
tests/libqtest.h +4 -14
@@ -42,11 +42,11 @@ void qtest_quit(QTestState *s);/** * qtest_qmp_discard_response: * @s: #QTestState instance to operate on. -* @fmt...: QMP message to send to qemu +* @json: QMP message to send to qemu * * Sends a QMP message to QEMU and consumes the response. */ -void qtest_qmp_discard_response (QTestState *s,const char *fmt, ...); +void qtest_qmp_discard_response (QTestState *s,const char *json);/** * qtest_qmp:
@@ -78,16 +78,6 @@ QDict *qtest_qmp_cmd(QTestState *s, const char *cmd, QDict *args);void qtest_async_qmp (QTestState *s,const char *fmt, ...);/** -* qtest_qmpv_discard_response: -* @s: #QTestState instance to operate on. -* @fmt: QMP message to send to QEMU -* @ap: QMP message arguments -* -* Sends a QMP message to QEMU and consumes the response. -*/ -void qtest_qmpv_discard_response (QTestState *s,const char *fmt,va_list ap); - -/** * qtest_qmpv: * @s: #QTestState instance to operate on. * @fmt: QMP message to send to QEMU
@@ -562,11 +552,11 @@ void qmp_async(const char *fmt, ...);/** * qmp_discard_response: -* @fmt...: QMP message to send to qemu +* @json: QMP message to send to qemu * * Sends a QMP message to QEMU and consumes the response. */ -void qmp_discard_response (const char *fmt, ...); +void qmp_discard_response (const char *json);/** * qmp_cmd_discard_response:
tests/libqtest.c +5 -17
@@ -536,20 +536,12 @@ void qtest_async_qmp(QTestState *s, const char *fmt, ...)va_end (ap); } -void qtest_qmpv_discard_response (QTestState *s,const char *fmt,va_list ap) +void qtest_qmp_discard_response (QTestState *s,const char *json) { -QDict *response =qtest_qmpv (s, fmt, ap); -QDECREF (response); -} - -void qtest_qmp_discard_response (QTestState *s,const char *fmt, ...) -{ -va_list ap;QDict *response; -va_start (ap, fmt); - response =qtest_qmpv (s, fmt, ap); -va_end (ap); +assert (!strchr (json,'%' )); + response =qtest_qmp (s, json);QDECREF (response); }
@@ -920,13 +912,9 @@ void qmp_async(const char *fmt, ...)va_end (ap); } -void qmp_discard_response (const char *fmt, ...) +void qmp_discard_response (const char *json) { -va_list ap; - -va_start (ap, fmt); -qtest_qmpv_discard_response (global_qtest, fmt, ap); -va_end (ap); +qtest_qmp_discard_response (global_qtest, json); }void qmp_cmd_discard_response (const char *cmd,QDict *args)
Signature · 1 line
2.7.4
PATCH 20/36 qtest: Avoid dynamic JSON in device-introspect-test
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use a hand-built QDict.
--- tests/device-introspect-test.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
tests/device-introspect-test.c +9 -6
@@ -19,6 +19,8 @@#include "qemu/osdep.h" #include "qemu-common.h" +#include "qapi/qmp/qbool.h" +#include "qapi/qmp/qdict.h" #include "qapi/qmp/qstring.h" #include "libqtest.h"
@@ -28,10 +30,11 @@ static QList *device_type_list(bool abstract) {QDict *resp;QList *ret; +QDict *args =qdict_new (); - resp =qmp ("{'execute': 'qom-list-types'," -" 'arguments': {'implements': 'device', 'abstract': %i}}" , - abstract); +qdict_put_str (args,"implements" ,"device" ); +qdict_put_bool (args,"abstract" , abstract); + resp =qmp_cmd ("qom-list-types" , args);g_assert (qdict_haskey (resp,"return" )); ret =qdict_get_qlist (resp,"return" );QINCREF (ret);
@@ -43,10 +46,10 @@ static void test_one_device(const char *type) {QDict *resp;char *help, *qom_tree; +QDict *args =qdict_new (); - resp =qmp ("{'execute': 'device-list-properties'," -" 'arguments': {'typename': %s}}" , - type); +qdict_put_str (args,"typename" , type); + resp =qmp_cmd ("device-list-properties" , args);QDECREF (resp); help =hmp ("device_add \"%s,help\"" , type);
Signature · 1 line
2.7.4
PATCH 21/36 qtest: Avoid dynamic JSON in tmp105-test
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use a hand-built QDict.
--- tests/tmp105-test.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tests/tmp105-test.c +5 -2
@@ -80,9 +80,12 @@ static int qmp_tmp105_get_temperature(const char *id)static void qmp_tmp105_set_temperature (const char *id,int value) {QDict *response; +QDict *args =qdict_new (); - response =qmp ("{ 'execute': 'qom-set', 'arguments': { 'path': %s, " -"'property': 'temperature', 'value': %d } }" , id, value); +qdict_put_str (args,"path" , id); +qdict_put_str (args,"property" ,"temperature" ); +qdict_put_int (args,"value" , value); + response =qmp_cmd ("qom-set" , args);g_assert (qdict_haskey (response,"return" ));QDECREF (response); }
Signature · 1 line
2.7.4
PATCH 22/36 qtest: Avoid dynamic JSON in pc-cpu-test
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use a hand-built QDict.
--- tests/pc-cpu-test.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
tests/pc-cpu-test.c +7 -5
@@ -37,8 +37,10 @@ static void test_pc_with_cpu_add(gconstpointer data)qtest_start (args);for (i = s->sockets * s->cores * s->threads; i < s->maxcpus; i++) { - response =qmp ("{ 'execute': 'cpu-add'," -" 'arguments': { 'id': %d } }" , i); +QDict *id =qdict_new (); + +qdict_put_int (id,"id" , i); + response =qmp_cmd ("cpu-add" , id);g_assert (response);g_assert (!qdict_haskey (response,"error" ));QDECREF (response);
@@ -53,6 +55,7 @@ static void test_pc_without_cpu_add(gconstpointer data)const PCTestData *s = data;char *args;QDict *response; +QDict *id =qdict_new (); args = g_strdup_printf("-machine %s -cpu %s " "-smp sockets=%u,cores=%u,threads=%u,maxcpus=%u",
@@ -60,9 +63,8 @@ static void test_pc_without_cpu_add(gconstpointer data) s->sockets, s->cores, s->threads, s->maxcpus);qtest_start (args); - response =qmp ("{ 'execute': 'cpu-add'," -" 'arguments': { 'id': %d } }" , - s->sockets * s->cores * s->threads); +qdict_put_int (id,"id" , s->sockets * s->cores * s->threads); + response =qmp_cmd ("cpu-add" , id);g_assert (response);g_assert (qdict_haskey (response,"error" ));QDECREF (response);
Signature · 1 line
2.7.4
PATCH 23/36 qtest: Avoid dynamic JSON in virtio-blk-test
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use a hand-built QDict.
--- tests/virtio-blk-test.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
tests/virtio-blk-test.c +16 -6
@@ -16,6 +16,9 @@#include "libqos/virtio-pci.h" #include "libqos/virtio-mmio.h" #include "libqos/malloc-generic.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qint.h" +#include "qapi/qmp/qstring.h" #include "qemu/bswap.h" #include "standard-headers/linux/virtio_ids.h" #include "standard-headers/linux/virtio_config.h"
@@ -399,6 +402,7 @@ static void pci_config(void)QOSState *qs;int n_size =TEST_IMAGE_SIZE /2 ;uint64_t capacity; +QDict *args =qdict_new (); qs =pci_test_start ();
@@ -409,8 +413,10 @@ static void pci_config(void)qvirtio_set_driver_ok (&dev->vdev); -qmp ("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', " -" 'size': %d } }" , n_size); +qdict_put_str (args,"device" ,"drive0" ); +qdict_put_int (args,"size" , n_size); +qmp_cmd ("block_resize" , args); +qvirtio_wait_config_isr (&dev->vdev,QVIRTIO_BLK_TIMEOUT_US ); capacity =qvirtio_config_readq (&dev->vdev,0 );
@@ -435,6 +441,7 @@ static void pci_msix(void)uint32_t free_head;uint8_t status;char *data; +QDict *args =qdict_new (); qs =pci_test_start ();
@@ -458,8 +465,9 @@ static void pci_msix(void)qvirtio_set_driver_ok (&dev->vdev); -qmp ("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', " -" 'size': %d } }" , n_size); +qdict_put_str (args,"device" ,"drive0" ); +qdict_put_int (args,"size" , n_size); +qmp_cmd ("block_resize" , args);qvirtio_wait_config_isr (&dev->vdev,QVIRTIO_BLK_TIMEOUT_US );
@@ -675,6 +683,7 @@ static void mmio_basic(void)QGuestAllocator *alloc;int n_size =TEST_IMAGE_SIZE /2 ;uint64_t capacity; +QDict *args =qdict_new ();arm_test_start ();
@@ -691,8 +700,9 @@ static void mmio_basic(void)test_basic (&dev->vdev, alloc, vq); -qmp ("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', " -" 'size': %d } }" , n_size); +qdict_put_str (args,"device" ,"drive0" ); +qdict_put_int (args,"size" , n_size); +qmp_cmd ("block_resize" , args);qvirtio_wait_queue_isr (&dev->vdev, vq,QVIRTIO_BLK_TIMEOUT_US );
Signature · 1 line
2.7.4
PATCH 24/36 qtest: Drop unused qmp_fdv()
In reply to Eric BlakeNow that no more clients are passing variadic arguments, we can simplify the testsuite by requiring that qmp_fd() be given the final string to pass over the wire, rather than a dynamic JSON string that has to be parsed into QObject and back out again.
--- tests/libqtest.h | 3 +-- tests/libqtest.c | 17 +++-------------- 2 files changed, 4 insertions(+), 16 deletions(-)
tests/libqtest.h +1 -2
@@ -927,7 +927,6 @@ static inline int64_t clock_set(int64_t val)QDict *qmp_fd_receive (int fd);void qmp_fd_sendv (int fd,const char *fmt,va_list ap);void qmp_fd_send (int fd,const char *fmt, ...); -QDict *qmp_fdv (int fd,const char *fmt,va_list ap); -QDict *qmp_fd (int fd,const char *fmt, ...); +QDict *qmp_fd (int fd,const char *msg);#endif
tests/libqtest.c +3 -14
@@ -469,13 +469,6 @@ void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)qmp_fd_sendv (s->qmp_fd, fmt, ap); } -QDict *qmp_fdv (int fd,const char *fmt,va_list ap) -{ -qmp_fd_sendv (fd, fmt, ap); - -return qmp_fd_receive (fd); -} -QDict *qtest_qmpv (QTestState *s,const char *fmt,va_list ap) {qtest_async_qmpv (s, fmt, ap);
@@ -484,15 +477,11 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)return qtest_qmp_receive (s); } -QDict *qmp_fd (int fd,const char *fmt, ...) +QDict *qmp_fd (int fd,const char *msg) { -va_list ap; -QDict *response; +qmp_fd_send (fd, msg); -va_start (ap, fmt); - response =qmp_fdv (fd, fmt, ap); -va_end (ap); -return response; +return qmp_fd_receive (fd); }void qmp_fd_send (int fd,const char *fmt, ...)
Signature · 1 line
2.7.4
PATCH 25/36 qtest: Change qmp_fd_send() to drop varags
In reply to Eric BlakeNo external clients were using qmp_fd_sendv(). Making it static lets us refactor the public qmp_fd_send() to take the final string to send over the wire, rather than a dynamic JSON string that has to be parsed into QObject and back out again. Note that the refactoring switches roles: previously, qmp_fd_send() called qmp_fd_sendv(), now the dependence is in the opposite direction.
Also, we no longer need to cater to a leading 0xff byte: the only client (test-qga) is already sending a direct string.
--- tests/libqtest.h | 3 +-- tests/libqtest.c | 33 ++++++++++++--------------------- 2 files changed, 13 insertions(+), 23 deletions(-)
tests/libqtest.h +1 -2
@@ -925,8 +925,7 @@ static inline int64_t clock_set(int64_t val) }QDict *qmp_fd_receive (int fd); -void qmp_fd_sendv (int fd,const char *fmt,va_list ap); -void qmp_fd_send (int fd,const char *fmt, ...); +void qmp_fd_send (int fd,const char *msg);QDict *qmp_fd (int fd,const char *msg);#endif
tests/libqtest.c +12 -21
@@ -425,19 +425,11 @@ QDict *qtest_qmp_receive(QTestState *s)* in the case that they choose to discard all replies up until * a particular EVENT is received. */ -void qmp_fd_sendv (int fd,const char *fmt,va_list ap) +static void qmp_fd_sendv (int fd,const char *fmt,va_list ap) {va_list ap_copy;QObject *qobj; -/* qobject_from_jsonv() silently eats leading 0xff as invalid -* JSON, but we want to test sending them over the wire to force -* resyncs */ -if (*fmt =='\377' ) { -socket_send (fd, fmt,1 ); - fmt++; - } -/* Going through qobject ensures we escape strings properly. * This seemingly unnecessary copy is required in case va_list * is an array type.
@@ -448,16 +440,10 @@ void qmp_fd_sendv(int fd, const char *fmt, va_list ap)/* No need to send anything for an empty QObject. */ if (qobj) { -int log =getenv ("QTEST_LOG" ) !=NULL ;QString *qstr =qobject_to_json (qobj);const char *str =qstring_get_str (qstr); -size_t size =qstring_get_length (qstr); -if (log) { -fprintf (stderr,"%s" , str); - } -/* Send QMP request */ -socket_send (fd, str, size); +qmp_fd_send (fd, str);QDECREF (qstr);qobject_decref (qobj);
@@ -484,13 +470,18 @@ QDict *qmp_fd(int fd, const char *msg)return qmp_fd_receive (fd); } -void qmp_fd_send (int fd,const char *fmt, ...) +void qmp_fd_send (int fd,const char *msg) { -va_list ap; +/* No need to send anything for the empty message. */ +if (*msg) { +int log =getenv ("QTEST_LOG" ) !=NULL ; -va_start (ap, fmt); -qmp_fd_sendv (fd, fmt, ap); -va_end (ap); +if (log) { +fprintf (stderr,"%s" , msg); + } +/* Send QMP request */ +socket_send (fd, msg,strlen (msg)); + } }QDict *qtest_qmp (QTestState *s,const char *fmt, ...)
Signature · 1 line
2.7.4
PATCH 26/36 qtest: Drop unused qtest_qmp_async()
In reply to Eric BlakeThere were no external clients of qtest_qmp_async(); delete this wrapper for one less varargs call in the chain.
--- tests/libqtest.h | 19 ------------------- tests/libqtest.c | 18 ++---------------- 2 files changed, 2 insertions(+), 35 deletions(-)
tests/libqtest.h +0 -19
@@ -69,15 +69,6 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);QDict *qtest_qmp_cmd (QTestState *s,const char *cmd,QDict *args);/** -* qtest_async_qmp: -* @s: #QTestState instance to operate on. -* @fmt...: QMP message to send to qemu -* -* Sends a QMP message to QEMU and leaves the response in the stream. -*/ -void qtest_async_qmp (QTestState *s,const char *fmt, ...); - -/** * qtest_qmpv: * @s: #QTestState instance to operate on. * @fmt: QMP message to send to QEMU
@@ -88,16 +79,6 @@ void qtest_async_qmp(QTestState *s, const char *fmt, ...);QDict *qtest_qmpv (QTestState *s,const char *fmt,va_list ap);/** -* qtest_async_qmpv: -* @s: #QTestState instance to operate on. -* @fmt: QMP message to send to QEMU -* @ap: QMP message arguments -* -* Sends a QMP message to QEMU and leaves the response in the stream. -*/ -void qtest_async_qmpv (QTestState *s,const char *fmt,va_list ap); - -/** * qtest_receive: * @s: #QTestState instance to operate on. *
tests/libqtest.c +2 -16
@@ -450,14 +450,9 @@ static void qmp_fd_sendv(int fd, const char *fmt, va_list ap) } } -void qtest_async_qmpv (QTestState *s,const char *fmt,va_list ap) -{ -qmp_fd_sendv (s->qmp_fd, fmt, ap); -} -QDict *qtest_qmpv (QTestState *s,const char *fmt,va_list ap) { -qtest_async_qmpv (s, fmt, ap); +qmp_fd_sendv (s->qmp_fd, fmt, ap);/* Receive reply */ return qtest_qmp_receive (s);
@@ -507,15 +502,6 @@ QDict *qtest_qmp_cmd(QTestState *s, const char *cmd, QDict *args)return qtest_qmp (s,"%p" ,QOBJECT (dict)); } -void qtest_async_qmp (QTestState *s,const char *fmt, ...) -{ -va_list ap; - -va_start (ap, fmt); -qtest_async_qmpv (s, fmt, ap); -va_end (ap); -} -void qtest_qmp_discard_response (QTestState *s,const char *json) {QDict *response;
@@ -888,7 +874,7 @@ void qmp_async(const char *fmt, ...)va_list ap;va_start (ap, fmt); -qtest_async_qmpv (global_qtest, fmt, ap); +qmp_fd_sendv (global_qtest->qmp_fd, fmt, ap);va_end (ap); }
Signature · 1 line
2.7.4
PATCH 27/36 qtest: Avoid dynamic JSON in qmp_cmd()
In reply to Eric BlakeWhen qmp_cmd() was first added, we used a shortcut of a format string of "%p" to pass the QObject intact through the varargs. But now that we have a way to directly invoke strings, we can altogether avoid the no-op trip through the parser by just flattening to a string ourselves.
--- tests/libqtest.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
tests/libqtest.c +10 -1
@@ -493,13 +493,22 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...)QDict *qtest_qmp_cmd (QTestState *s,const char *cmd,QDict *args) {QDict *dict =qdict_new (); +QString *qstr; +QDict *result;if (!args) { args =qdict_new (); }qdict_put_str (dict,"execute" , cmd);qdict_put (dict,"arguments" , args); -return qtest_qmp (s,"%p" ,QOBJECT (dict)); + qstr =qobject_to_json (QOBJECT (dict)); + +qmp_fd_send (s->qmp_fd,qstring_get_str (qstr)); + result =qtest_qmp_receive (s); + +QDECREF (dict); +QDECREF (qstr); +return result; }void qtest_qmp_discard_response (QTestState *s,const char *json)
Signature · 1 line
2.7.4
PATCH 28/36 qapi: Factor out JSON string escaping
In reply to Eric BlakePull out a new qstring_append_json_string() helper, so that all JSON output producers can use the same output escaping rules.
While it appears that vmstate's use of the simpler qjson.c formatter is not currently encountering any string that needs escapes to be valid JSON, it is better to be safe than sorry, and this substitution is not adding any potential asserts during migration.
Adding a return value will let callers that care diagnose the situation where an attempt was made to output invalid JSON (we use substitute characters, and in fact guarantee that our output is ASCII via escapes even if the input was UTF-8). None of the current callers care, but a future patch wants to make it possible to conditionally turn this situation into an error.
---
previously posted in 'Add qapi-to-JSON visitor' series:
v6: comment tweaks
[no v5 due to series split]
v4: keep helper in qobject-json.[ch], tweak variable name and for loop iterator, add return value, drop R-b
v3: rebase to include cleanups in master
v2: no change
--- include/qapi/qmp/qjson.h | 2 + migration/qjson.c | 10 ++-- qobject/qjson.c | 126 ++++++++++++++++++++++++++--------------------- 3 files changed, 76 insertions(+), 62 deletions(-)
include/qapi/qmp/qjson.h +2 -0
@@ -24,4 +24,6 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap) GCC_FMT_ATTR(1, 0);QString *qobject_to_json (const QObject *obj);QString *qobject_to_json_pretty (const QObject *obj); +int qstring_append_json_string (QString *qstring,const char *str); +#endif /* QJSON_H */
migration/qjson.c +4 -6
@@ -26,6 +26,7 @@#include "qemu/osdep.h" #include "qapi/qmp/qstring.h" #include "migration/qjson.h" +#include "qapi/qmp/qjson.h" struct QJSON {QString *str;
@@ -42,9 +43,8 @@ static void json_emit_element(QJSON *json, const char *name) }if (name) { -qstring_append (json->str,"\"" ); -qstring_append (json->str, name); -qstring_append (json->str,"\" : " ); +qstring_append_json_string (json->str, name); +qstring_append (json->str," : " ); } }
@@ -83,9 +83,7 @@ void json_prop_int(QJSON *json, const char *name, int64_t val)void json_prop_str (QJSON *json,const char *name,const char *str) {json_emit_element (json, name); -qstring_append_chr (json->str,'"' ); -qstring_append (json->str, str); -qstring_append_chr (json->str,'"' ); +qstring_append_json_string (json->str, str); }const char *qjson_get_str (QJSON *json)
qobject/qjson.c +70 -56
147 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 29/36 qapi: Add qstring_append_printf()
In reply to Eric BlakeBack in commit 764c1ca (Nov 2009), we added qstring_append_int(). However, it did not see any use until commit 190c882 (Jan 2015). Furthermore, it has a rather limited use case - to print anything else, callers still have to format into a temporary buffer, unless we want to introduce an explosion of new qstring_append_* methods for each useful type to print.
A much better approach is to add a wrapper that merges printf behavior onto qstring_append, via the new qstring_append_printf() (and its vararg counterpart), with a name based on glib's g_string_append_printf(). In fact, with our wrapper in place, we no longer need qstring_append_int().
Other immediate uses for the new function include simplifying two existing clients of qstring_append() on a just-formatted buffer, and the fact that we can take advantage of printf width manipulations for more efficient indentation.
---
previously posted in 'Add qapi-to-JSON visitor' series:
v6: no change
[no v5 due to series split]
v4: retitle, s/_format/_printf/, drop R-b, move up in series
v3: rebase to master
v2: also simplify qstring_append_json_string(), add assertion that format is well-formed
--- include/qapi/qmp/qstring.h | 7 +++++-- migration/qjson.c | 2 +- qobject/qjson.c | 38 ++++++++++---------------------------- qobject/qstring.c | 26 +++++++++++++++++++++----- 4 files changed, 37 insertions(+), 36 deletions(-)
include/qapi/qmp/qstring.h +5 -2
@@ -1,7 +1,7 @@/* * QString Module * -* Copyright (C) 2009 Red Hat Inc. +* Copyright (C) 2009-2016 Red Hat Inc. * * Authors: * Luiz Capitulino <[email protected]>
@@ -27,9 +27,12 @@ QString *qstring_from_str(const char *str);QString *qstring_from_substr (const char *str,int start,int end);size_t qstring_get_length (const QString *qstring);const char *qstring_get_str (const QString *qstring); -void qstring_append_int (QString *qstring,int64_t value);void qstring_append (QString *qstring,const char *str);void qstring_append_chr (QString *qstring,int c); +void qstring_append_printf(QString *qstring, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +void qstring_append_vprintf(QString *qstring, const char *fmt, va_list ap) + GCC_FMT_ATTR(2, 0);QString *qobject_to_qstring (const QObject *obj);void qstring_destroy_obj (QObject *obj);
migration/qjson.c +1 -1
@@ -77,7 +77,7 @@ void json_end_array(QJSON *json)void json_prop_int (QJSON *json,const char *name,int64_t val) {json_emit_element (json, name); -qstring_append_int (json->str, val); +qstring_append_printf (json->str,"%" PRId64, val); }void json_prop_str (QJSON *json,const char *name,const char *str)
qobject/qjson.c +10 -28
@@ -81,16 +81,13 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent);static void to_json_dict_iter (const char *key,QObject *obj,void *opaque) {ToJsonIterState *s = opaque; -int j;if (s->count) {qstring_append (s->str, s->pretty ?"," :", " ); }if (s->pretty) { -qstring_append (s->str,"\n" ); -for (j =0 ; j < s->indent ; j++) -qstring_append (s->str," " ); +qstring_append_printf (s->str,"\n%*s" ,4 * s->indent,"" ); }qstring_append_json_string (s->str, key);
@@ -103,16 +100,13 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)static void to_json_list_iter (QObject *obj,void *opaque) {ToJsonIterState *s = opaque; -int j;if (s->count) {qstring_append (s->str, s->pretty ?"," :", " ); }if (s->pretty) { -qstring_append (s->str,"\n" ); -for (j =0 ; j < s->indent ; j++) -qstring_append (s->str," " ); +qstring_append_printf (s->str,"\n%*s" ,4 * s->indent,"" ); }to_json (obj, s->str, s->pretty, s->indent);
@@ -127,10 +121,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)break ;case QTYPE_QINT : {QInt *val =qobject_to_qint (obj); -char buffer[1024 ]; - -snprintf (buffer,sizeof (buffer),"%" PRId64,qint_get_int (val)); -qstring_append (str, buffer); +qstring_append_printf (str,"%" PRId64,qint_get_int (val));break ; }case QTYPE_QSTRING : {
@@ -151,10 +142,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)qstring_append (str,"{" );qdict_iter (val, to_json_dict_iter, &s);if (pretty) { -int j; -qstring_append (str,"\n" ); -for (j =0 ; j < indent ; j++) -qstring_append (str," " ); +qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); }qstring_append (str,"}" );break ;
@@ -170,10 +158,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)qstring_append (str,"[" );qlist_iter (val, (void *)to_json_list_iter, &s);if (pretty) { -int j; -qstring_append (str,"\n" ); -for (j =0 ; j < indent ; j++) -qstring_append (str," " ); +qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); }qstring_append (str,"]" );break ;
@@ -251,7 +236,6 @@ int qstring_append_json_string(QString *qstring, const char *str) {const char *ptr;int cp; -char buf[16 ];char *end;int result =0 ;
@@ -288,16 +272,14 @@ int qstring_append_json_string(QString *qstring, const char *str) }if (cp >0xFFFF ) {/* beyond BMP; need a surrogate pair */ -snprintf (buf,sizeof (buf),"\\u%04X\\u%04X" , -0xD800 + ((cp -0x10000 ) >>10 ), -0xDC00 + ((cp -0x10000 ) &0x3FF )); +qstring_append_printf (qstring,"\\u%04X\\u%04X" , +0xD800 + ((cp -0x10000 ) >>10 ), +0xDc00 + ((cp -0x10000 ) &0x3FF )); }else if (cp <0x20 || cp >=0x7F ) { -snprintf (buf,sizeof (buf),"\\u%04X" , cp); +qstring_append_printf (qstring,"\\u%04X" , cp); }else { - buf[0 ] = cp; - buf[1 ] =0 ; +qstring_append_chr (qstring, cp); } -qstring_append (qstring, buf); } };
qobject/qstring.c +21 -5
@@ -1,7 +1,7 @@/* * QString Module * -* Copyright (C) 2009 Red Hat Inc. +* Copyright (C) 2009-2016 Red Hat Inc. * * Authors: * Luiz Capitulino <[email protected]>
@@ -88,12 +88,28 @@ void qstring_append(QString *qstring, const char *str) qstring->string[qstring->length] =0 ; } -void qstring_append_int (QString *qstring,int64_t value) +void qstring_append_printf (QString *qstring,const char *fmt, ...) { -char num[32 ]; +va_list ap; -snprintf (num,sizeof (num),"%" PRId64, value); -qstring_append (qstring, num); +va_start (ap, fmt); +qstring_append_vprintf (qstring, fmt, ap); +va_end (ap); +} + +void qstring_append_vprintf (QString *qstring,const char *fmt,va_list ap) +{ +va_list ap2; +int len; + +va_copy (ap2, ap); + len =vsnprintf (NULL ,0 , fmt, ap2); +assert (len >=0 ); +va_end (ap2); + +capacity_increase (qstring, len); +vsnprintf (qstring->string + qstring->length, len +1 , fmt, ap); + qstring->length += len; }/**
Signature · 1 line
2.7.4
PATCH 30/36 qtest: Avoid dynamic JSON in qmp_fd_sendv()
In reply to Eric BlakeThe only format specifier left in any client of qmp() and friends is %s. Rather than having to make our JSON parser support varargs, and use a conversion from string to QObject and back to string, we can instead just directly build the string and substitute %s ourselves. With this, the final caller of qobject_from_jsonv() is eliminated, and followup patches can simplify the parser.
---
Too bad glibc's strchrnul() is not universal; also, this patch could avoid "%.*s" format shenanigans (with the nastiness of converting ptrdiff_t to int) if we want to first enhance QString to support a length-limited append primitive.
--- tests/libqtest.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-)
tests/libqtest.c +26 -16
@@ -427,27 +427,37 @@ QDict *qtest_qmp_receive(QTestState *s)*/ static void qmp_fd_sendv (int fd,const char *fmt,va_list ap) { -va_list ap_copy; -QObject *qobj; +QString *qstr; -/* Going through qobject ensures we escape strings properly. -* This seemingly unnecessary copy is required in case va_list -* is an array type. +if (!strchr (fmt,'%' )) { +return qmp_fd_send (fd, fmt); + } + +/* Construct a single string, turning %s in fmt into proper JSON +* strings. For now, we don't support any other format specifiers. */ -va_copy (ap_copy, ap); - qobj =qobject_from_jsonv (fmt, &ap_copy); -va_end (ap_copy); + qstr =qstring_new (); +while (*fmt) { +/* Too bad strchrnul() is not universal */ +const char *p =strchr (fmt,'%' ); -/* No need to send anything for an empty QObject. */ -if (qobj) { -QString *qstr =qobject_to_json (qobj); -const char *str =qstring_get_str (qstr); +if (!p) { + p =strchr (fmt,'\0' ); + } -qmp_fd_send (fd, str); - -QDECREF (qstr); -qobject_decref (qobj); +assert (p - fmt <INT_MAX ); +qstring_append_printf (qstr,"%.*s" , (int )(p - fmt), fmt); +if (*p) { +assert (p[1 ] =='s' ); + p +=2 ; + qstring_append_json_string(qstr, va_arg(ap, const char *)); + } + fmt = p; } + +qmp_fd_send (fd,qstring_get_str (qstr)); + +QDECREF (qstr); }QDict *qtest_qmpv (QTestState *s,const char *fmt,va_list ap)
Signature · 1 line
2.7.4
PATCH 31/36 qtest: Document calling conventions
In reply to Eric BlakeWe have two flavors of vararg usage in qtest; make it clear that qmp() has different semantics than hmp(), and let the compiler enforce that hmp() is used correctly. Since qmp() only accepts exactly "%s" (asserting on misuse, which would be flagged during 'make check'), I figured that it is probably not worth adding a format attribute to qmp() at this time.
--- tests/libqtest.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
tests/libqtest.h +7 -7
@@ -51,7 +51,7 @@ void qtest_qmp_discard_response(QTestState *s, const char *json);/** * qtest_qmp: * @s: #QTestState instance to operate on. -* @fmt...: QMP message to send to qemu +* @fmt...: QMP message to send to qemu; only recognizes "%s" * * Sends a QMP message to QEMU and returns the response. */
@@ -108,13 +108,13 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);/** * qtest_hmpv: * @s: #QTestState instance to operate on. -* @fmt...: HMP command to send to QEMU +* @fmt...: HMP command to send to QEMU, passed through sprintf() * * Send HMP command to QEMU via QMP's human-monitor-command. * * Returns: the command's output. The caller should g_free() it. */ -char *qtest_hmp (QTestState *s,const char *fmt, ...); +char *qtest_hmp (QTestState *s,const char *fmt, ...)GCC_FMT_ATTR (2 ,3 );/** * qtest_hmpv:
@@ -507,7 +507,7 @@ static inline void qtest_end(void)/** * qmp: -* @fmt...: QMP message to send to qemu +* @fmt...: QMP message to send to qemu; only recognizes "%s" * * Sends a QMP message to QEMU and returns the response. */
@@ -525,7 +525,7 @@ QDict *qmp_cmd(const char *cmd, QDict *args);/** * qmp_async: -* @fmt...: QMP message to send to qemu +* @fmt...: QMP message to send to qemu; only recognizes "%s" * * Sends a QMP message to QEMU and leaves the response in the stream. */
@@ -584,13 +584,13 @@ static inline QDict *qmp_eventwait_ref(const char *event)/** * hmp: -* @fmt...: HMP command to send to QEMU +* @fmt...: HMP command to send to QEMU, passed through printf() * * Send HMP command to QEMU via QMP's human-monitor-command. * * Returns: the command's output. The caller should g_free() it. */ -char *hmp (const char *fmt, ...); +char *hmp (const char *fmt, ...)GCC_FMT_ATTR (1 ,2 );/** * get_irq:
Signature · 1 line
2.7.4
PATCH 32/36 qtest: Avoid dynamic JSON in qom-test
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use hand-built QDict.
---
Optional; the "%s" handling in earlier patches is sufficient to handle this without hand-built QDict, so it is a judgment call which version is more legible.
--- tests/qom-test.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
tests/qom-test.c +8 -7
@@ -46,13 +46,14 @@ static bool is_blacklisted(const char *arch, const char *mach)static void test_properties (const char *path,bool recurse) {char *child_path; -QDict *response, *tuple, *tmp; +QDict *response, *tuple, *tmp, *args;QList *list;QListEntry *entry;g_test_message ("Obtaining properties of %s" , path); - response =qmp ("{ 'execute': 'qom-list'," -" 'arguments': { 'path': %s } }" , path); + args =qdict_new (); +qdict_put_str (args,"path" , path); + response =qmp_cmd ("qom-list" , args);g_assert (response);if (!recurse) {
@@ -75,10 +76,10 @@ static void test_properties(const char *path, bool recurse) }else {const char *prop =qdict_get_str (tuple,"name" );g_test_message ("Testing property %s.%s" , path, prop); - tmp =qmp ("{ 'execute': 'qom-get'," -" 'arguments': { 'path': %s," -" 'property': %s } }" , - path, prop); + args =qdict_new (); +qdict_put_str (args,"path" , path); +qdict_put_str (args,"property" , prop); + tmp =qmp_cmd ("qom-get" , args);/* qom-get may fail but should not, e.g., segfault. */ g_assert (tmp);QDECREF (tmp);
Signature · 1 line
2.7.4
PATCH 33/36 qtest: Avoid dynamic JSON in test-x86-cpuid-compat
In reply to Eric BlakeAs argued elsewhere, it's less code to maintain if we convert from a dynamic string passed to qobject_from_jsonv() to instead use hand-built QDict.
---
Optional; the "%s" handling in earlier patches is sufficient to handle this without hand-built QDict, so it is a judgment call which version is more legible.
--- tests/test-x86-cpuid-compat.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tests/test-x86-cpuid-compat.c +9 -5
@@ -4,6 +4,7 @@#include "qapi/qmp/qdict.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qbool.h" +#include "qapi/qmp/qstring.h" #include "libqtest.h" static char *get_cpu0_qom_path (void )
@@ -25,11 +26,14 @@ static char *get_cpu0_qom_path(void)static QObject *qom_get (const char *path,const char *prop) { -QDict *resp =qmp ("{ 'execute': 'qom-get'," -" 'arguments': { 'path': %s," -" 'property': %s } }" , - path, prop); -QObject *ret =qdict_get (resp,"return" ); +QDict *args =qdict_new (); +QDict *resp; +QObject *ret; + +qdict_put_str (args,"path" , path); +qdict_put_str (args,"property" , prop); + resp =qmp_cmd ("qom-get" , args); + ret =qdict_get (resp,"return" );qobject_incref (ret);QDECREF (resp);return ret;
Signature · 1 line
2.7.4
PATCH 34/36 qapi: Rip out dynamic JSON parser frontend
In reply to Eric BlakeExcept for the testsuite, there are no more callers of qobject_from_json[fv](). We have no need to maintain dynamic JSON parsing just for the check-qjson test, so delete the functions. All callers of json_parser_parse() now pass NULL for the varargs parameter; the next patch will clean that up.
--- include/qapi/qmp/qjson.h | 2 - qobject/qjson.c | 29 +----------- tests/check-qjson.c | 113 +---------------------------------------------- 3 files changed, 3 insertions(+), 141 deletions(-)
include/qapi/qmp/qjson.h +0 -2
@@ -18,8 +18,6 @@#include "qapi/qmp/qstring.h" QObject *qobject_from_json (const char *string); -QObject *qobject_from_jsonf (const char *string, ...)GCC_FMT_ATTR (1 ,2 ); -QObject *qobject_from_jsonv (const char *string,va_list *ap)GCC_FMT_ATTR (1 ,0 );QString *qobject_to_json (const QObject *obj);QString *qobject_to_json_pretty (const QObject *obj);
qobject/qjson.c +2 -27
@@ -22,22 +22,19 @@typedef struct JSONParsingState {JSONMessageParser parser; -va_list *ap;QObject *result; }JSONParsingState ;static void parse_json (JSONMessageParser *parser,GQueue *tokens) {JSONParsingState *s =container_of (parser, JSONParsingState, parser); - s->result =json_parser_parse (tokens, s->ap); + s->result =json_parser_parse (tokens,NULL ); } -QObject *qobject_from_jsonv (const char *string,va_list *ap) +QObject *qobject_from_json (const char *string) {JSONParsingState state = {}; - state.ap = ap; -json_message_parser_init (&state.parser, parse_json);json_message_parser_feed (&state.parser, string,strlen (string));json_message_parser_flush (&state.parser);
@@ -46,28 +43,6 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap)return state.result; } -QObject *qobject_from_json (const char *string) -{ -return qobject_from_jsonv (string,NULL ); -} - -/* -* IMPORTANT: This function aborts on error, thus it must not -* be used with untrusted arguments. -*/ -QObject *qobject_from_jsonf (const char *string, ...) -{ -QObject *obj; -va_list ap; - -va_start (ap, string); - obj =qobject_from_jsonv (string, &ap); -va_end (ap); - -assert (obj !=NULL ); -return obj; -} - typedef struct ToJsonIterState { int indent;
tests/check-qjson.c +1 -112
158 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 35/36 qapi: Rip out dynamic JSON parser escape sequence support
In reply to Eric BlakeNow that there are no callers of dynamic JSON parsing, the lexer does not need to special-case escape sequences.
Since the lexer is directly invoked on user-provided input, this changes behavior when a QMP client passes (bad) JSON, from:
{'execute':'qmp_capabilities','id':%s}
{"error": {"class": "GenericError", "desc": "Invalid JSON syntax"}}
to the noisier:
{'execute':'qmp_capabilities','id':%s}
{"error": {"class": "GenericError", "desc": "Invalid JSON syntax"}} {"error": {"class": "GenericError", "desc": "JSON parse error, invalid keyword 's'"}} {"error": {"class": "GenericError", "desc": "JSON parse error, expecting value"}}
But we've always known our parser is rather noisy when it comes to dealing with invalid JSON, and valid clients won't see the difference.
--- include/qapi/qmp/json-lexer.h | 1 - qobject/json-lexer.c | 40 ---------------------------------------- qobject/json-parser.c | 32 -------------------------------- 3 files changed, 73 deletions(-)
include/qapi/qmp/json-lexer.h +0 -1
@@ -27,7 +27,6 @@ typedef enum json_token_type {JSON_FLOAT ,JSON_KEYWORD ,JSON_STRING , -JSON_ESCAPE ,JSON_SKIP ,JSON_ERROR , } JSONTokenType;
qobject/json-lexer.c +0 -40
@@ -58,12 +58,6 @@ enum json_lexer_state {IN_NONZERO_NUMBER ,IN_NEG_NONZERO_NUMBER ,IN_KEYWORD , -IN_ESCAPE , -IN_ESCAPE_L , -IN_ESCAPE_LL , -IN_ESCAPE_I , -IN_ESCAPE_I6 , -IN_ESCAPE_I64 ,IN_WHITESPACE , IN_START, };
@@ -224,38 +218,6 @@ static const uint8_t json_lexer[][256] = { ['\n'] = IN_WHITESPACE, }, - /* escape */ - [IN_ESCAPE_LL] = { - ['d' ] =JSON_ESCAPE , - }, - - [IN_ESCAPE_L ] = { - ['d' ] =JSON_ESCAPE , - ['l' ] =IN_ESCAPE_LL , - }, - - [IN_ESCAPE_I64 ] = { - ['d' ] =JSON_ESCAPE , - }, - - [IN_ESCAPE_I6 ] = { - ['4' ] =IN_ESCAPE_I64 , - }, - - [IN_ESCAPE_I ] = { - ['6' ] =IN_ESCAPE_I6 , - }, - - [IN_ESCAPE ] = { - ['d' ] =JSON_ESCAPE , - ['i' ] =JSON_ESCAPE , - ['p' ] =JSON_ESCAPE , - ['s' ] =JSON_ESCAPE , - ['f' ] =JSON_ESCAPE , - ['l' ] =IN_ESCAPE_L , - ['I' ] =IN_ESCAPE_I , - }, -/* top level rule */ [IN_START] = { ['"'] = IN_DQ_STRING,
@@ -270,7 +232,6 @@ static const uint8_t json_lexer[][256] = { [','] = JSON_COMMA, [':'] = JSON_COLON, ['a' ... 'z'] = IN_KEYWORD, - ['%'] = IN_ESCAPE, [' '] = IN_WHITESPACE, ['\t'] = IN_WHITESPACE, ['\r'] = IN_WHITESPACE,
@@ -311,7 +272,6 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)case JSON_RSQUARE :case JSON_COLON :case JSON_COMMA : -case JSON_ESCAPE :case JSON_INTEGER :case JSON_FLOAT :case JSON_KEYWORD :
qobject/json-parser.c +0 -32
@@ -450,36 +450,6 @@ static QObject *parse_keyword(JSONParserContext *ctxt)return NULL ; } -static QObject *parse_escape (JSONParserContext *ctxt,va_list *ap) -{ -JSONToken *token; - -if (ap ==NULL ) { -return NULL ; - } - - token =parser_context_pop_token (ctxt); -assert (token && token->type ==JSON_ESCAPE ); - -if (!strcmp (token->str,"%p" )) { - return va_arg(*ap, QObject *); - }else if (!strcmp (token->str,"%i" )) { -return QOBJECT (qbool_from_bool (va_arg (*ap, int))); - }else if (!strcmp (token->str,"%d" )) { -return QOBJECT (qint_from_int (va_arg (*ap, int))); - }else if (!strcmp (token->str,"%ld" )) { -return QOBJECT (qint_from_int (va_arg (*ap, long))); - }else if (!strcmp (token->str,"%lld" ) || - !strcmp (token->str,"%I64d" )) { - return QOBJECT(qint_from_int(va_arg(*ap, long long))); - }else if (!strcmp (token->str,"%s" )) { - return QOBJECT(qstring_from_str(va_arg(*ap, const char *))); - }else if (!strcmp (token->str,"%f" )) { -return QOBJECT (qfloat_from_double (va_arg (*ap, double))); - } -return NULL ; -} -static QObject *parse_literal (JSONParserContext *ctxt) {JSONToken *token;
@@ -537,8 +507,6 @@ static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)return parse_object (ctxt, ap);case JSON_LSQUARE :return parse_array (ctxt, ap); -case JSON_ESCAPE : -return parse_escape (ctxt, ap);case JSON_INTEGER :case JSON_FLOAT :case JSON_STRING :
Signature · 1 line
2.7.4
PATCH 36/36 qapi: Rip out dynamic JSON parser backend
In reply to Eric BlakeNow that there are no callers of dynamic JSON parsing, the parser does not need to handle a va_list argument.
--- include/qapi/qmp/json-parser.h | 6 +++--- monitor.c | 2 +- qga/main.c | 2 +- qobject/json-parser.c | 34 +++++++++++++++++----------------- qobject/qjson.c | 2 +- tests/libqtest.c | 2 +- 6 files changed, 24 insertions(+), 24 deletions(-)
include/qapi/qmp/json-parser.h +3 -3
@@ -1,5 +1,5 @@/* -* JSON Parser +* JSON Parser * * Copyright IBM, Corp. 2009 *
@@ -17,7 +17,7 @@#include "qemu-common.h" #include "qapi/qmp/qlist.h" -QObject *json_parser_parse (GQueue *tokens,va_list *ap); -QObject *json_parser_parse_err (GQueue *tokens,va_list *ap,Error **errp); +QObject *json_parser_parse (GQueue *tokens); +QObject *json_parser_parse_err (GQueue *tokens,Error **errp);#endif
monitor.c +1 -1
@@ -3731,7 +3731,7 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)Monitor *mon = cur_mon;Error *err =NULL ; - req =json_parser_parse_err (tokens,NULL , &err); + req =json_parser_parse_err (tokens, &err);if (err || !req ||qobject_type (req) !=QTYPE_QDICT ) {if (!err) {error_setg (&err,QERR_JSON_PARSING );
qga/main.c +1 -1
@@ -576,7 +576,7 @@ static void process_event(JSONMessageParser *parser, GQueue *tokens)g_assert (s && parser);g_debug ("process_event: called" ); - qdict =qobject_to_qdict (json_parser_parse_err (tokens,NULL , &err)); + qdict =qobject_to_qdict (json_parser_parse_err (tokens, &err));if (err || !qdict) {QDECREF (qdict); qdict =qdict_new ();
qobject/json-parser.c +17 -17
@@ -37,7 +37,7 @@ typedef struct JSONParserContext* 4) deal with premature EOI */ -static QObject *parse_value (JSONParserContext *ctxt,va_list *ap); +static QObject *parse_value (JSONParserContext *ctxt);/** * Error handler
@@ -268,7 +268,7 @@ static void parser_context_free(JSONParserContext *ctxt)/** * Parsing rules */ -static int parse_pair (JSONParserContext *ctxt,QDict *dict,va_list *ap) +static int parse_pair (JSONParserContext *ctxt,QDict *dict) {QObject *key =NULL , *value;JSONToken *peek, *token;
@@ -279,7 +279,7 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) goto out; } - key =parse_value (ctxt, ap); + key =parse_value (ctxt);if (!key ||qobject_type (key) !=QTYPE_QSTRING ) {parse_error (ctxt, peek,"key is not a string in object" ); goto out;
@@ -296,7 +296,7 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) goto out; } - value =parse_value (ctxt, ap); + value =parse_value (ctxt);if (value ==NULL ) {parse_error (ctxt, token,"Missing value in dict" ); goto out;
@@ -314,7 +314,7 @@ out:return -1 ; } -static QObject *parse_object (JSONParserContext *ctxt,va_list *ap) +static QObject *parse_object (JSONParserContext *ctxt) {QDict *dict =NULL ;JSONToken *token, *peek;
@@ -331,7 +331,7 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) }if (peek->type !=JSON_RCURLY ) { -if (parse_pair (ctxt, dict, ap) ==-1 ) { +if (parse_pair (ctxt, dict) ==-1 ) { goto out; }
@@ -347,7 +347,7 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) goto out; } -if (parse_pair (ctxt, dict, ap) ==-1 ) { +if (parse_pair (ctxt, dict) ==-1 ) { goto out; }
@@ -368,7 +368,7 @@ out:return NULL ; } -static QObject *parse_array (JSONParserContext *ctxt,va_list *ap) +static QObject *parse_array (JSONParserContext *ctxt) {QList *list =NULL ;JSONToken *token, *peek;
@@ -387,7 +387,7 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)if (peek->type !=JSON_RSQUARE ) {QObject *obj; - obj =parse_value (ctxt, ap); + obj =parse_value (ctxt);if (obj ==NULL ) {parse_error (ctxt, token,"expecting value" ); goto out;
@@ -407,7 +407,7 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) goto out; } - obj =parse_value (ctxt, ap); + obj =parse_value (ctxt);if (obj ==NULL ) {parse_error (ctxt, token,"expecting value" ); goto out;
@@ -492,7 +492,7 @@ static QObject *parse_literal(JSONParserContext *ctxt) } } -static QObject *parse_value (JSONParserContext *ctxt,va_list *ap) +static QObject *parse_value (JSONParserContext *ctxt) {JSONToken *token;
@@ -504,9 +504,9 @@ static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)switch (token->type) {case JSON_LCURLY : -return parse_object (ctxt, ap); +return parse_object (ctxt);case JSON_LSQUARE : -return parse_array (ctxt, ap); +return parse_array (ctxt);case JSON_INTEGER :case JSON_FLOAT :case JSON_STRING :
@@ -519,12 +519,12 @@ static QObject *parse_value(JSONParserContext *ctxt, va_list *ap) } } -QObject *json_parser_parse (GQueue *tokens,va_list *ap) +QObject *json_parser_parse (GQueue *tokens) { -return json_parser_parse_err (tokens, ap,NULL ); +return json_parser_parse_err (tokens,NULL ); } -QObject *json_parser_parse_err (GQueue *tokens,va_list *ap,Error **errp) +QObject *json_parser_parse_err (GQueue *tokens,Error **errp) {JSONParserContext *ctxt =parser_context_new (tokens);QObject *result;
@@ -533,7 +533,7 @@ QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)return NULL ; } - result =parse_value (ctxt, ap); + result =parse_value (ctxt);error_propagate (errp, ctxt->err);
qobject/qjson.c +1 -1
@@ -28,7 +28,7 @@ typedef struct JSONParsingStatestatic void parse_json (JSONMessageParser *parser,GQueue *tokens) {JSONParsingState *s =container_of (parser, JSONParsingState, parser); - s->result =json_parser_parse (tokens,NULL ); + s->result =json_parser_parse (tokens); }QObject *qobject_from_json (const char *string)
tests/libqtest.c +1 -1
@@ -373,7 +373,7 @@ static void qmp_response(JSONMessageParser *parser, GQueue *tokens)QMPResponseParser *qmp =container_of (parser, QMPResponseParser, parser);QObject *obj; - obj =json_parser_parse (tokens,NULL ); + obj =json_parser_parse (tokens);if (!obj) {fprintf (stderr,"QMP JSON response parsing failed\n" );exit (1 );
Signature · 1 line
2.7.4
Quoted 3 lines · Eric Blake
Followup series to conversation about PRId64 and MacOS:
https://lists.gnu.org/archive/html/qemu-devel/2016-11/msg04226.html
And I meant to add:
Available as a tag at:
git fetch git://repo.or.cz/qemu/ericb.git qapi-dynamic-json-v1
on top of Markus' qapi-next branch (but presumably will apply without much effort on top of the final 2.8 release, since this is 2.9 material)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
----- Original Message -----
Quoted 11 lines
From: "Eric Blake" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected]
Sent: Wednesday, November 30, 2016 8:50:05 PM
Subject: Re: [Qemu-devel] [PATCH 00/36] Rip out dynamic JSON parsingQuoted 3 lines · Eric Blake
Followup series to conversation about PRId64 and MacOS:
https://lists.gnu.org/archive/html/qemu-devel/2016-11/msg04226.html
Quoted 7 lines
And I meant to add:
Available as a tag at:
git fetchgit://repo.or.cz/qemu/ericb.gitqapi-dynamic-json-v1on top of Markus' qapi-next branch (but presumably will apply without much effort on top of the final 2.8 release, since this is 2.9 material)
I don't see the point, honestly. It's harder to use and hardly removes any code (the balance is -160 lines, but about 140 of them come from the parser's test suite).
Using the functionality only in qtest is fine, but it is damn useful there. If we want to remove the % escaping, let's rewrite the qtests in Python as they probably should have been since the beginning.
Paolo