Caution, rough edges. qapi/introspect.json defines the introspection schema. It should do for uses other than QMP. FIXME it's almost entirely devoid of comments.
The introspection schema does not reflect all the rules and restrictions that apply to QAPI schemata. A valid QAPI schema has an introspection value conforming to the introspection schema, but the converse is not true. Introspection lowers away a number of schema details: * The built-in types are declared with their JSON type. TODO Should we map all the integer types to just int? * Implicit type definitions are made explicit, and given auto-generated names. These names start with ':' so they don't clash with the user's names. Example: a simple union implicitly defines an enumeration type for its discriminator. * All type references are by name. * Base types are flattened. * The top type (named 'any') can hold any value. * The struct and union types are generalized into an object type. * Commands take a single argument and return a single result. Dictionary argument/result or list result is an implicit type definition. The empty object type is used when a command takes no arguments or produces no results. The argument is always of object type, but the introspection schema doesn't reflect that. The 'gen': false directive is omitted as implementation detail. The 'success-response' directive is ommitted as well for now, even though it's not an implementation detail. * Events carry a single data value. Implicit type definition and empty object type use, just like for commands. The value is of object type, but the introspection schema doesn't reflect that. * Types not used by commands or events are omitted. Indirect use counts as use. * Optional members have a default, which can only be null right now Instead of a mandatory "optional" flag, we have an optional default. No default means mandatory, default null means optional without default value. Non-null is available for optional with default. Alternate members can't have defaults, but the introspection schema doesn't reflect that. * Clients should *not* look up types by name, because type names are not ABI. Look up the command or event you're interested in, then follow the references. TODO Should we hide the type names to eliminate the temptation? TODO much of the above should go into docs. New generator scripts/qapi-introspect.py computes an introspection value for its input, and generates a C variable holding it. FIXME it can generate awfully long lines A new test-qmp-input-visitor test case feeds its result for both tests/qapi-schema/qapi-schema-test.json and qapi-schema.json to a QmpInputVisitor to verify it actually conforms to the schema. New QMP command query-schema takes its return value from that variable. Command documentation is incomplete, and marked FIXME. Its reply is some 85KiBytes for me right now. If this turns out to be too much, we have a couple of options: * We can use shorter names in the JSON. Not the QMP style. * Optionally return the sub-schema for commands and events given as arguments. Right now qmp_query_schema() sends the string literal computed by qmp-introspect.py. To compute sub-schema at run time, we'd have to duplicate parts of qapi-introspect.py in C. Unattractive. * Let clients cache the output of query-schema. It changes only on QEMU upgrades, i.e. rarely. Provide a command query-schema-hash. Clients can have a cache indexed by hash, and re-query the schema only when they don't have it cached. Signed-off-by: Markus Armbruster <arm...@redhat.com> --- .gitignore | 1 + Makefile | 9 +- Makefile.objs | 4 +- docs/qapi-code-gen.txt | 34 +++++ monitor.c | 15 +++ qapi-schema.json | 3 + qapi/introspect.json | 71 ++++++++++ qmp-commands.hx | 16 +++ scripts/qapi-commands.py | 2 +- scripts/qapi-event.py | 2 +- scripts/qapi-introspect.py | 172 ++++++++++++++++++++++++ scripts/qapi-types.py | 2 +- scripts/qapi-visit.py | 2 +- scripts/qapi.py | 14 +- tests/.gitignore | 1 + tests/Makefile | 10 +- tests/qapi-schema/alternate-good.out | 1 + tests/qapi-schema/args-member-array.out | 1 + tests/qapi-schema/comments.out | 1 + tests/qapi-schema/empty.out | 1 + tests/qapi-schema/enum-empty.out | 1 + tests/qapi-schema/event-case.out | 1 + tests/qapi-schema/flat-union-reverse-define.out | 1 + tests/qapi-schema/ident-with-escape.out | 1 + tests/qapi-schema/include-relpath.out | 1 + tests/qapi-schema/include-repetition.out | 1 + tests/qapi-schema/include-simple.out | 1 + tests/qapi-schema/indented-expr.out | 1 + tests/qapi-schema/qapi-schema-test.out | 1 + tests/qapi-schema/returns-int.out | 1 + tests/test-qmp-input-strict.c | 55 ++++++++ 31 files changed, 416 insertions(+), 11 deletions(-) create mode 100644 qapi/introspect.json create mode 100644 scripts/qapi-introspect.py diff --git a/.gitignore b/.gitignore index 61bc492..72c6170 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ /qapi-visit.[ch] /qapi-event.[ch] /qmp-commands.h +/qmp-introspect.[ch] /qmp-marshal.c /qemu-doc.html /qemu-tech.html diff --git a/Makefile b/Makefile index 340d9c8..6140e06 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,8 @@ endif GENERATED_HEADERS = config-host.h qemu-options.def GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c +GENERATED_HEADERS += qmp-introspect.h +GENERATED_SOURCES += qmp-introspect.c GENERATED_HEADERS += trace/generated-events.h GENERATED_SOURCES += trace/generated-events.c @@ -263,7 +265,7 @@ $(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py) qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \ $(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \ - $(SRC_PATH)/qapi/event.json + $(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json qapi-types.c qapi-types.h :\ $(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py) @@ -285,6 +287,11 @@ $(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py) $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \ $(gen-out-type) -o "." -m $<, \ " GEN $@") +qmp-introspect.h qmp-introspect.c :\ +$(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py) + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \ + $(gen-out-type) -o "." $<, \ + " GEN $@") QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h) $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN) diff --git a/Makefile.objs b/Makefile.objs index f094eff..b5a9e99 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -1,7 +1,8 @@ ####################################################################### # Common libraries for tools and emulators stub-obj-y = stubs/ -util-obj-y = util/ qobject/ qapi/ qapi-types.o qapi-visit.o qapi-event.o +util-obj-y = util/ qobject/ qapi/ +util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o util-obj-y += crypto/ ####################################################################### @@ -83,6 +84,7 @@ common-obj-$(CONFIG_FDT) += device_tree.o # qapi common-obj-y += qmp-marshal.o +common-obj-y += qmp-introspect.o common-obj-y += qmp.o hmp.o endif diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt index 53e6d9c..ed04770 100644 --- a/docs/qapi-code-gen.txt +++ b/docs/qapi-code-gen.txt @@ -850,3 +850,37 @@ Example: extern const char *const example_QAPIEvent_lookup[]; #endif + +=== scripts/qapi-introspect.py === + +Used to generate the introspection C code for a schema. The following +files are created: + +$(prefix)qmp-introspect.c - Defines a string holding a JSON + description of the schema. +$(prefix)qmp-introspect.h - Declares the above string. + +Example: + + $ python scripts/qapi-event.py --output-dir="qapi-generated" + --prefix="example-" example-schema.json + $ cat qapi-generated/example-qmp-introspect.c +[Uninteresting stuff omitted...] + + const char example_qmp_schema_json[] = "[" + "{ \"arg-type\": \":empty\", \"name\": \"MY_EVENT\", \"meta-type\": \"event\" }, " + "{ \"meta-type\": \"builtin\", \"name\": \"int\", \"json-type\": \"int\" }, " + "{ \"meta-type\": \"builtin\", \"name\": \"str\", \"json-type\": \"string\" }, " + "{ \"meta-type\": \"object\", \"name\": \":empty\", \"members\": [ ] }, " + "{ \"meta-type\": \"object\", \"name\": \":obj-my-command-arg\", \"members\": [{ \"type\": \"UserDefOne\", \"name\": \"arg1\" } ] }, " + "{ \"meta-type\": \"object\", \"name\": \"UserDefOne\", \"members\": [{ \"type\": \"int\", \"name\": \"integer\" }, { \"type\": \"str\", \"name\": \"string\" } ] }, " + "{ \"arg-type\": \":obj-my-command-arg\", \"ret-type\": \"UserDefOne\", \"name\": \"my-command\", \"meta-type\": \"command\" } ]"; + $ cat qapi-generated/example-qmp-introspect.h +[Uninteresting stuff omitted...] + + #ifndef EXAMPLE_QMP_INTROSPECT_H + #define EXAMPLE_QMP_INTROSPECT_H + + extern const char example_qmp_schema_json[]; + + #endif diff --git a/monitor.c b/monitor.c index 16672f1..f49f292 100644 --- a/monitor.c +++ b/monitor.c @@ -74,6 +74,7 @@ #include "block/qapi.h" #include "qapi/qmp-event.h" #include "qapi-event.h" +#include "qmp-introspect.h" #include "sysemu/block-backend.h" /* for hmp_info_irq/pic */ @@ -924,6 +925,20 @@ EventInfoList *qmp_query_events(Error **errp) return ev_list; } +/* + * Minor hack: generated marshalling suppressed for this command + * ('gen': false in the schema) so we can parse the JSON string + * directly into QObject instead of first parsing it with + * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it + * to QObject with generated output marshallers, every time. Instead, + * we do it in test-qmp-input-visitor.c, just to make sure + * qapi-introspect.py's output actually conforms to the schema. + */ +static void qmp_query_schema(QDict *qdict, QObject **ret_data, Error **errp) +{ + *ret_data = qobject_from_json(qmp_schema_json); +} + /* set the current CPU defined by the user */ int monitor_set_cpu(int cpu_index) { diff --git a/qapi-schema.json b/qapi-schema.json index e91e3b9..af19810 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -14,6 +14,9 @@ # Tracing commands { 'include': 'qapi/trace.json' } +# QAPI introspection +{ 'include': 'qapi/introspect.json' } + ## # @LostTickPolicy: # diff --git a/qapi/introspect.json b/qapi/introspect.json new file mode 100644 index 0000000..2002f8f --- /dev/null +++ b/qapi/introspect.json @@ -0,0 +1,71 @@ +# -*- Mode: Python -*- +# +# QAPI introspection +# +# Copyright (C) 2015 Red Hat, Inc. +# +# Authors: +# Markus Armbruster <arm...@redhat.com> +# +# This work is licensed under the terms of the GNU GPL, version 2 or later. +# See the COPYING file in the top-level directory. + +{ 'enum': 'SchemaMetaType', + 'data': [ 'builtin', 'enum', 'array', 'object', 'alternate', + 'command', 'event' ] } + +{ 'struct': 'SchemaInfoBase', + 'data': { 'name': 'str', 'meta-type': 'SchemaMetaType' } } + +{ 'enum': 'JSONType', + 'data': [ 'string', 'number', 'int', 'boolean', 'null', + 'object', 'array', 'value' ] } + +{ 'struct': 'SchemaInfoBuiltin', + 'data': { 'json-type': 'JSONType' } } + +{ 'struct': 'SchemaInfoEnum', + 'data': { 'values': ['str'] } } + +{ 'struct': 'SchemaInfoArray', + 'data': { 'element-type': 'str' } } + +{ 'struct': 'SchemaInfoObjectMember', + 'data': { 'name': 'str', 'type': 'str', '*default': 'any' } } +# @default's type must be null or match @type + +{ 'struct': 'SchemaInfoObjectVariant', + 'data': { 'case': 'str', 'type': 'str' } } + +{ 'struct': 'SchemaInfoObject', + 'data': { 'members': [ 'SchemaInfoObjectMember' ], + '*tag': 'str', + '*variants': [ 'SchemaInfoObjectVariant' ] } } + +{ 'struct': 'SchemaInfoAlternateMember', + 'data': { 'type': 'str' } } + +{ 'struct': 'SchemaInfoAlternate', + 'data': { 'members': [ 'SchemaInfoAlternateMember' ] } } + +{ 'struct': 'SchemaInfoCommand', + 'data': { 'arg-type': 'str', 'ret-type': 'str' } } # TODO success-response + +{ 'struct': 'SchemaInfoEvent', + 'data': { 'arg-type': 'str' } } + +{ 'union': 'SchemaInfo', + 'base': 'SchemaInfoBase', + 'discriminator': 'meta-type', + 'data': { + 'builtin': 'SchemaInfoBuiltin', + 'enum': 'SchemaInfoEnum', + 'array': 'SchemaInfoArray', + 'object': 'SchemaInfoObject', + 'alternate': 'SchemaInfoAlternate', + 'command': 'SchemaInfoCommand', + 'event': 'SchemaInfoEvent' } } + +{ 'command': 'query-schema', + 'returns': [ 'SchemaInfo' ], + 'gen': false } # just to simplify qmp_query_json() diff --git a/qmp-commands.hx b/qmp-commands.hx index b06d74c..3ad3b65 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -2168,6 +2168,22 @@ EQMP }, SQMP +query-schema +------------ + +Return the QMP schema. + +FIXME explain + +EQMP + + { + .name = "query-schema", + .args_type = "", + .mhandler.cmd_new = qmp_query_schema, + }, + +SQMP query-chardev ------------- diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 0603cff..7ff7c31 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -260,7 +260,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor): self.defn = None self.regy = None self.visited_ret_types = None - def visit_begin(self): + def visit_begin(self, schema): self.decl = '' self.defn = '' self.regy = '' diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index 10f4bae..d100099 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -139,7 +139,7 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor): self.decl = None self.defn = None self.event_names = None - def visit_begin(self): + def visit_begin(self, schema): self.decl = '' self.defn = '' self.event_names = [] diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py new file mode 100644 index 0000000..8354d06 --- /dev/null +++ b/scripts/qapi-introspect.py @@ -0,0 +1,172 @@ +# +# QAPI introspection generator +# +# Copyright (C) 2015 Red Hat, Inc. +# +# Authors: +# Markus Armbruster <arm...@redhat.com> +# +# This work is licensed under the terms of the GNU GPL, version 2. +# See the COPYING file in the top-level directory. + +from qapi import * + +# Caveman's json.dumps() replacement (we're stuck at 2.4) +# TODO try to use json.dumps() once we get unstuck +def to_json(obj, level=0): + if obj == None: + ret = 'null' + elif isinstance(obj, str): + ret = '"' + obj.replace('"', r'\"') + '"' + elif isinstance(obj, list): + elts = [to_json(elt, level + 1) + for elt in obj] + ret = '[' + ', '.join(elts) + ' ]' + elif isinstance(obj, dict): + elts = ['"%s": %s' % (key, to_json(obj[key], level + 1)) + for key in sorted(obj.keys())] + ret = '{ ' + ', '.join(elts) + ' }' + else: + assert False # not implemented + if level == 1: + ret = '\n' + ret + return ret + +def to_c_string(string): + return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"' + +class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor): + def __init__(self): + self.defn = None + self.decl = None + self.schema = None + self.jsons = None + self.used_types = None + + def visit_begin(self, schema): + self.schema = schema + self.jsons = [] + self.used_types = [] + return QAPISchemaType # don't visit types for now + + def visit_end(self): + # visit the types that are actually used + for typ in self.used_types: + typ.visit(self) + self.jsons.sort() + # generate C + name = prefix + 'qmp_schema_json' + self.decl = mcgen(''' +extern const char %(c_name)s[]; +''', + c_name=c_name(name)) + lines = to_json(self.jsons).split('\n') + c_string = '\n '.join([to_c_string(line) for line in lines]) + self.defn = mcgen(''' +const char %(c_name)s[] = %(c_string)s; +''', + c_name=c_name(name), + c_string=c_string) + self.schema = None + self.jsons = None + self.used_types = None + + def _use_type(self, typ): + if typ not in self.used_types: + self.used_types.append(typ) + return typ.name + + def _gen_json(self, name, mtype, obj={}): + obj['name'] = name + obj['meta-type'] = mtype + self.jsons.append(obj) + + def _gen_member(self, member): + ret = { 'name': member.name, 'type': self._use_type(member.type) } + if member.optional: + ret['default'] = None + return ret + + def _gen_variants(self, tag_name, variants): + return { 'tag': tag_name or 'kind', + 'variants': [self._gen_variant(v) for v in variants] } + + def _gen_variant(self, variant): + return { 'case': variant.name, 'type': self._use_type(variant.type) } + + def visit_builtin_type(self, name, info, json_type): + self._gen_json(name, 'builtin', { 'json-type': json_type }) + + def visit_enum_type(self, name, info, values): + self._gen_json(name, 'enum', { 'values': values }) + + def visit_array_type(self, name, info, element_type): + self._gen_json(name, 'array', + { 'element-type': self._use_type(element_type) }) + + def visit_object_type_flat(self, name, info, members, variants): + obj = { 'members': [self._gen_member(m) for m in members] } + if variants: + obj.update(self._gen_variants(variants.tag_name, + variants.variants)) + self._gen_json(name, 'object', obj) + + def visit_alternate_type(self, name, info, variants): + self._gen_json(name, 'alternate', + { 'members': [ { 'type': self._use_type(m.type) } + for m in variants.variants] }) + + def visit_command(self, name, info, arg_type, ret_type, + gen, success_response): + arg_type = arg_type or self.schema.the_empty_object_type + ret_type = ret_type or self.schema.the_empty_object_type + self._gen_json(name, 'command', + { 'arg-type': self._use_type(arg_type), + 'ret-type': self._use_type(ret_type) }) + + def visit_event(self, name, info, arg_type): + arg_type = arg_type or self.schema.the_empty_object_type + self._gen_json(name, 'event', { 'arg-type': self._use_type(arg_type) }) + +(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line() + +c_comment = ''' +/* + * QAPI/QMP schema introspection + * + * Copyright (C) 2015 Red Hat, Inc. + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' +h_comment = ''' +/* + * QAPI/QMP schema introspection + * + * Copyright (C) 2015 Red Hat, Inc. + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' + +(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, + 'qmp-introspect.c', 'qmp-introspect.h', + c_comment, h_comment) + +fdef.write(mcgen(''' +#include "%(prefix)sqmp-introspect.h" + +''', + prefix=prefix)) + +schema = QAPISchema(input_file) +gen = QAPISchemaGenIntrospectVisitor() +schema.visit(gen) +fdef.write(gen.defn) +fdecl.write(gen.decl) + +close_output(fdef, fdecl) diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 5b6de4c..9caf7bd 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -197,7 +197,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): self.fwdecl = None self.fwdefn = None self.btin = None - def visit_begin(self): + def visit_begin(self, schema): self.decl = '' self.defn = '' self.fwdecl = '' diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 9cfd2ff..6705eaf 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -334,7 +334,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor): self.decl = None self.defn = None self.btin = None - def visit_begin(self): + def visit_begin(self, schema): self.decl = '' self.defn = '' self.btin = guardstart('QAPI_VISIT_BUILTIN') diff --git a/scripts/qapi.py b/scripts/qapi.py index fd8abad..34c2884 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -768,7 +768,7 @@ class QAPISchemaEntity(object): pass class QAPISchemaVisitor(object): - def visit_begin(self): + def visit_begin(self, schema): pass def visit_end(self): pass @@ -780,6 +780,8 @@ class QAPISchemaVisitor(object): pass def visit_object_type(self, name, info, base, members, variants): pass + def visit_object_type_flat(self, name, info, members, variants): + pass def visit_alternate_type(self, name, info, variants): pass def visit_command(self, name, info, arg_type, ret_type, @@ -901,6 +903,8 @@ class QAPISchemaObjectType(QAPISchemaType): def visit(self, visitor): visitor.visit_object_type(self.name, self.info, self.base, self.local_members, self.variants) + visitor.visit_object_type_flat(self.name, self.info, + self.members, self.variants) class QAPISchemaObjectTypeMember(object): def __init__(self, name, typ, optional): @@ -1050,6 +1054,9 @@ class QAPISchema(object): ('bool', 'boolean', 'bool', 'false'), ('any', 'value', 'QObject' + pointer_suffix , 'NULL')]: self._def_builtin_type(*t) + self.the_empty_object_type = QAPISchemaObjectType(':empty', None, None, + [], None) + self._def_entity(self.the_empty_object_type) def _make_implicit_enum_type(self, name, values): name = name + 'Kind' @@ -1196,9 +1203,10 @@ class QAPISchema(object): ent.check(self) def visit(self, visitor): - visitor.visit_begin() + ignore = visitor.visit_begin(self) for name in sorted(self.entity_dict.keys()): - self.entity_dict[name].visit(visitor) + if not ignore or not isinstance(self.entity_dict[name], ignore): + self.entity_dict[name].visit(visitor) visitor.visit_end() # diff --git a/tests/.gitignore b/tests/.gitignore index ccc92e4..e476d52 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -28,6 +28,7 @@ test-qmp-commands.h test-qmp-event test-qmp-input-strict test-qmp-input-visitor +test-qmp-introspect.[ch] test-qmp-marshal.c test-qmp-output-visitor test-rcu-list diff --git a/tests/Makefile b/tests/Makefile index badf915..398dc4a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -260,7 +260,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \ struct-base-clash.json struct-base-clash-deep.json ) GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h \ - tests/test-qmp-commands.h tests/test-qapi-event.h + tests/test-qmp-commands.h tests/test-qapi-event.h \ + tests/test-qmp-introspect.h test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/check-qlist.o tests/check-qfloat.o tests/check-qjson.o \ @@ -273,7 +274,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/rcutorture.o tests/test-rcu-list.o test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o \ - tests/test-qapi-event.o + tests/test-qapi-event.o tests/test-qmp-introspect.o $(test-obj-y): QEMU_INCLUDES += -Itests QEMU_CFLAGS += -I$(SRC_PATH)/tests @@ -334,6 +335,11 @@ $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-eve $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py \ $(gen-out-type) -o tests -p "test-" $<, \ " GEN $@") +tests/test-qmp-introspect.c tests/test-qmp-introspect.h :\ +$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py) + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \ + $(gen-out-type) -o tests -p "test-" $<, \ + " GEN $@") tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a diff --git a/tests/qapi-schema/alternate-good.out b/tests/qapi-schema/alternate-good.out index 3d765ff..65af727 100644 --- a/tests/qapi-schema/alternate-good.out +++ b/tests/qapi-schema/alternate-good.out @@ -1,3 +1,4 @@ +object :empty alternate Alt case value: int case string: Enum diff --git a/tests/qapi-schema/args-member-array.out b/tests/qapi-schema/args-member-array.out index b67384c..b3b92df 100644 --- a/tests/qapi-schema/args-member-array.out +++ b/tests/qapi-schema/args-member-array.out @@ -1,3 +1,4 @@ +object :empty object :obj-okay-arg member member1: intList optional=False member member2: defList optional=False diff --git a/tests/qapi-schema/comments.out b/tests/qapi-schema/comments.out index 6161b90..9e2c656 100644 --- a/tests/qapi-schema/comments.out +++ b/tests/qapi-schema/comments.out @@ -1 +1,2 @@ +object :empty enum Status ['good', 'bad', 'ugly'] diff --git a/tests/qapi-schema/empty.out b/tests/qapi-schema/empty.out index e69de29..272b161 100644 --- a/tests/qapi-schema/empty.out +++ b/tests/qapi-schema/empty.out @@ -0,0 +1 @@ +object :empty diff --git a/tests/qapi-schema/enum-empty.out b/tests/qapi-schema/enum-empty.out index e09b00f..a449d45 100644 --- a/tests/qapi-schema/enum-empty.out +++ b/tests/qapi-schema/enum-empty.out @@ -1 +1,2 @@ +object :empty enum MyEnum [] diff --git a/tests/qapi-schema/event-case.out b/tests/qapi-schema/event-case.out index b5ae4c2..cdfd264 100644 --- a/tests/qapi-schema/event-case.out +++ b/tests/qapi-schema/event-case.out @@ -1 +1,2 @@ +object :empty event oops None diff --git a/tests/qapi-schema/flat-union-reverse-define.out b/tests/qapi-schema/flat-union-reverse-define.out index 477fb31..a5a9134 100644 --- a/tests/qapi-schema/flat-union-reverse-define.out +++ b/tests/qapi-schema/flat-union-reverse-define.out @@ -1,3 +1,4 @@ +object :empty object TestBase member enum1: TestEnum optional=False enum TestEnum ['value1', 'value2'] diff --git a/tests/qapi-schema/ident-with-escape.out b/tests/qapi-schema/ident-with-escape.out index 9577d1b..f4542b1 100644 --- a/tests/qapi-schema/ident-with-escape.out +++ b/tests/qapi-schema/ident-with-escape.out @@ -1,3 +1,4 @@ +object :empty object :obj-fooA-arg member bar1: str optional=False command fooA :obj-fooA-arg -> None diff --git a/tests/qapi-schema/include-relpath.out b/tests/qapi-schema/include-relpath.out index 6161b90..9e2c656 100644 --- a/tests/qapi-schema/include-relpath.out +++ b/tests/qapi-schema/include-relpath.out @@ -1 +1,2 @@ +object :empty enum Status ['good', 'bad', 'ugly'] diff --git a/tests/qapi-schema/include-repetition.out b/tests/qapi-schema/include-repetition.out index 6161b90..9e2c656 100644 --- a/tests/qapi-schema/include-repetition.out +++ b/tests/qapi-schema/include-repetition.out @@ -1 +1,2 @@ +object :empty enum Status ['good', 'bad', 'ugly'] diff --git a/tests/qapi-schema/include-simple.out b/tests/qapi-schema/include-simple.out index 6161b90..9e2c656 100644 --- a/tests/qapi-schema/include-simple.out +++ b/tests/qapi-schema/include-simple.out @@ -1 +1,2 @@ +object :empty enum Status ['good', 'bad', 'ugly'] diff --git a/tests/qapi-schema/indented-expr.out b/tests/qapi-schema/indented-expr.out index c5af55a..226d300 100644 --- a/tests/qapi-schema/indented-expr.out +++ b/tests/qapi-schema/indented-expr.out @@ -1,3 +1,4 @@ +object :empty command eins None -> None gen=True success_response=True command zwei None -> None diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out index 49c1197..386b057 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -1,3 +1,4 @@ +object :empty object :obj-EVENT_C-arg member a: int optional=True member b: UserDefOne optional=True diff --git a/tests/qapi-schema/returns-int.out b/tests/qapi-schema/returns-int.out index 1ac3e1e..a2da259 100644 --- a/tests/qapi-schema/returns-int.out +++ b/tests/qapi-schema/returns-int.out @@ -1,2 +1,3 @@ +object :empty command guest-get-time None -> int gen=True success_response=True diff --git a/tests/test-qmp-input-strict.c b/tests/test-qmp-input-strict.c index 00c3e29..bfd9d04 100644 --- a/tests/test-qmp-input-strict.c +++ b/tests/test-qmp-input-strict.c @@ -19,6 +19,9 @@ #include "test-qapi-types.h" #include "test-qapi-visit.h" #include "qapi/qmp/types.h" +#include "test-qmp-introspect.h" +#include "qmp-introspect.h" +#include "qapi-visit.h" typedef struct TestInputVisitorData { QObject *obj; @@ -62,6 +65,30 @@ Visitor *validate_test_init(TestInputVisitorData *data, 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) +{ + Visitor *v; + + data->obj = qobject_from_json(json_string); + g_assert(data->obj != NULL); + + data->qiv = qmp_input_visitor_new_strict(data->obj); + g_assert(data->qiv != NULL); + + v = qmp_input_get_visitor(data->qiv); + g_assert(v != NULL); + + return v; +} + typedef struct TestStruct { int64_t integer; @@ -293,6 +320,32 @@ static void test_validate_fail_alternate(TestInputVisitorData *data, qapi_free_UserDefAlternate(tmp); } +static void do_test_validate_qmp_introspect(TestInputVisitorData *data, + const char *schema_json) +{ + SchemaInfoList *schema = NULL; + Error *err = NULL; + Visitor *v; + + v = validate_test_init_raw(data, schema_json); + + visit_type_SchemaInfoList(v, &schema, NULL, &err); + if (err) { + fprintf(stderr, "%s", error_get_pretty(err)); + } + g_assert(!err); + g_assert(schema); + + qapi_free_SchemaInfoList(schema); +} + +static void test_validate_qmp_introspect(TestInputVisitorData *data, + const void *unused) +{ + do_test_validate_qmp_introspect(data, test_qmp_schema_json); + do_test_validate_qmp_introspect(data, qmp_schema_json); +} + static void validate_test_add(const char *testpath, TestInputVisitorData *data, void (*test_func)(TestInputVisitorData *data, const void *user_data)) @@ -333,6 +386,8 @@ int main(int argc, char **argv) &testdata, test_validate_fail_alternate); validate_test_add("/visitor/input-strict/fail/union-native-list", &testdata, test_validate_fail_union_native_list); + validate_test_add("/visitor/input-strict/pass/qmp-introspect", + &testdata, test_validate_qmp_introspect); g_test_run(); -- 2.4.3