]> git.proxmox.com Git - mirror_qemu.git/blame - docs/qapi-code-gen.txt
vfio: Pass an error object to vfio_get_device
[mirror_qemu.git] / docs / qapi-code-gen.txt
CommitLineData
b84da831
MR
1= How to use the QAPI code generator =
2
6fb55451 3Copyright IBM Corp. 2011
9ee86b85 4Copyright (C) 2012-2016 Red Hat, Inc.
6fb55451
EB
5
6This work is licensed under the terms of the GNU GPL, version 2 or
7later. See the COPYING file in the top-level directory.
8
9== Introduction ==
10
b84da831 11QAPI is a native C API within QEMU which provides management-level
e790e666
EB
12functionality to internal and external users. For external
13users/processes, this interface is made available by a JSON-based wire
14format for the QEMU Monitor Protocol (QMP) for controlling qemu, as
15well as the QEMU Guest Agent (QGA) for communicating with the guest.
363b4262
EB
16The remainder of this document uses "Client JSON Protocol" when
17referring to the wire contents of a QMP or QGA connection.
b84da831 18
363b4262
EB
19To map Client JSON Protocol interfaces to the native C QAPI
20implementations, a JSON-based schema is used to define types and
21function signatures, and a set of scripts is used to generate types,
22signatures, and marshaling/dispatch code. This document will describe
23how the schemas, scripts, and resulting code are used.
b84da831
MR
24
25
26== QMP/Guest agent schema ==
27
e790e666
EB
28A QAPI schema file is designed to be loosely based on JSON
29(http://www.ietf.org/rfc/rfc7159.txt) with changes for quoting style
30and the use of comments; a QAPI schema file is then parsed by a python
31code generation program. A valid QAPI schema consists of a series of
32top-level expressions, with no commas between them. Where
33dictionaries (JSON objects) are used, they are parsed as python
34OrderedDicts so that ordering is preserved (for predictable layout of
35generated C structs and parameter lists). Ordering doesn't matter
36between top-level expressions or the keys within an expression, but
37does matter within dictionary values for 'data' and 'returns' members
38of a single expression. QAPI schema input is written using 'single
363b4262
EB
39quotes' instead of JSON's "double quotes" (in contrast, Client JSON
40Protocol uses no comments, and while input accepts 'single quotes' as
41an extension, output is strict JSON using only "double quotes"). As
42in JSON, trailing commas are not permitted in arrays or dictionaries.
43Input must be ASCII (although QMP supports full Unicode strings, the
44QAPI parser does not). At present, there is no place where a QAPI
45schema requires the use of JSON numbers or null.
e790e666
EB
46
47Comments are allowed; anything between an unquoted # and the following
48newline is ignored. Although there is not yet a documentation
49generator, a form of stylized comments has developed for consistently
50documenting details about an expression and when it was added to the
51schema. The documentation is delimited between two lines of ##, then
52the first line names the expression, an optional overview is provided,
53then individual documentation about each member of 'data' is provided,
54and finally, a 'Since: x.y.z' tag lists the release that introduced
9ee86b85 55the expression. Optional members are tagged with the phrase
e790e666
EB
56'#optional', often with their default value; and extensions added
57after the expression was first released are also given a '(since
58x.y.z)' comment. For example:
59
60 ##
61 # @BlockStats:
62 #
63 # Statistics of a virtual block device or a block backing device.
64 #
65 # @device: #optional If the stats are for a virtual block device, the name
66 # corresponding to the virtual block device.
67 #
68 # @stats: A @BlockDeviceStats for the device.
69 #
70 # @parent: #optional This describes the file block device if it has one.
71 #
72 # @backing: #optional This describes the backing block device if it has one.
73 # (Since 2.0)
74 #
75 # Since: 0.14.0
76 ##
3b2a8b85 77 { 'struct': 'BlockStats',
e790e666
EB
78 'data': {'*device': 'str', 'stats': 'BlockDeviceStats',
79 '*parent': 'BlockStats',
80 '*backing': 'BlockStats'} }
81
82The schema sets up a series of types, as well as commands and events
83that will use those types. Forward references are allowed: the parser
84scans in two passes, where the first pass learns all type names, and
85the second validates the schema and generates the code. This allows
86the definition of complex structs that can have mutually recursive
363b4262
EB
87types, and allows for indefinite nesting of Client JSON Protocol that
88satisfies the schema. A type name should not be defined more than
89once. It is permissible for the schema to contain additional types
90not used by any commands or events in the Client JSON Protocol, for
91the side effect of generated C code used internally.
e790e666 92
7b1b98c4 93There are seven top-level expressions recognized by the parser:
3b2a8b85 94'include', 'command', 'struct', 'enum', 'union', 'alternate', and
7b1b98c4
EB
95'event'. There are several groups of types: simple types (a number of
96built-in types, such as 'int' and 'str'; as well as enumerations),
97complex types (structs and two flavors of unions), and alternate types
98(a choice between other types). The 'command' and 'event' expressions
e790e666
EB
99can refer to existing types by name, or list an anonymous type as a
100dictionary. Listing a type name inside an array refers to a
101single-dimension array of that type; multi-dimension arrays are not
102directly supported (although an array of a complex struct that
103contains an array member is possible).
104
105Types, commands, and events share a common namespace. Therefore,
106generally speaking, type definitions should always use CamelCase for
107user-defined type names, while built-in types are lowercase. Type
108definitions should not end in 'Kind', as this namespace is used for
255960dd
EB
109creating implicit C enums for visiting union types, or in 'List', as
110this namespace is used for creating array types. Command names,
9ee86b85 111and member names within a type, should be all lower case with words
e790e666
EB
112separated by a hyphen. However, some existing older commands and
113complex types use underscore; when extending such expressions,
114consistency is preferred over blindly avoiding underscore. Event
9ee86b85 115names should be ALL_CAPS with words separated by underscore. Member
9fb081e0 116names cannot start with 'has-' or 'has_', as this is reserved for
9ee86b85 117tracking optional members.
e790e666 118
9ee86b85 119Any name (command, event, type, member, or enum value) beginning with
e790e666 120"x-" is marked experimental, and may be withdrawn or changed
59a92fee
EB
121incompatibly in a future release. All names must begin with a letter,
122and contain only ASCII letters, digits, dash, and underscore. There
123are two exceptions: enum values may start with a digit, and any
124extensions added by downstream vendors should start with a prefix
125matching "__RFQDN_" (for the reverse-fully-qualified-domain-name of
126the vendor), even if the rest of the name uses dash (example:
127__com.redhat_drive-mirror). Names beginning with 'q_' are reserved
128for the generator: QMP names that resemble C keywords or other
129problematic strings will be munged in C to use this prefix. For
9ee86b85 130example, a member named "default" in qapi becomes "q_default" in the
59a92fee 131generated C code.
e790e666
EB
132
133In the rest of this document, usage lines are given for each
134expression type, with literal strings written in lower case and
135placeholders written in capitals. If a literal string includes a
136prefix of '*', that key/value pair can be omitted from the expression.
3b2a8b85 137For example, a usage statement that includes '*base':STRUCT-NAME
e790e666 138means that an expression has an optional key 'base', which if present
3b2a8b85 139must have a value that forms a struct name.
e790e666
EB
140
141
142=== Built-in Types ===
143
f133f2db
MA
144The following types are predefined, and map to C as follows:
145
146 Schema C JSON
147 str char * any JSON string, UTF-8
148 number double any JSON number
149 int int64_t a JSON number without fractional part
150 that fits into the C integer type
151 int8 int8_t likewise
152 int16 int16_t likewise
153 int32 int32_t likewise
154 int64 int64_t likewise
155 uint8 uint8_t likewise
156 uint16 uint16_t likewise
157 uint32 uint32_t likewise
158 uint64 uint64_t likewise
159 size uint64_t like uint64_t, except StringInputVisitor
160 accepts size suffixes
161 bool bool JSON true or false
28770e05 162 any QObject * any JSON value
7264f5c5 163 QType QType JSON string matching enum QType values
51631493 164
a719a27c
LV
165
166=== Includes ===
167
e790e666
EB
168Usage: { 'include': STRING }
169
a719a27c
LV
170The QAPI schema definitions can be modularized using the 'include' directive:
171
e790e666 172 { 'include': 'path/to/file.json' }
a719a27c
LV
173
174The directive is evaluated recursively, and include paths are relative to the
e790e666 175file using the directive. Multiple includes of the same file are
4247f839 176idempotent. No other keys should appear in the expression, and the include
e790e666
EB
177value should be a string.
178
179As a matter of style, it is a good idea to have all files be
180self-contained, but at the moment, nothing prevents an included file
181from making a forward reference to a type that is only introduced by
182an outer file. The parser may be made stricter in the future to
183prevent incomplete include files.
a719a27c
LV
184
185
3b2a8b85 186=== Struct types ===
51631493 187
3b2a8b85 188Usage: { 'struct': STRING, 'data': DICT, '*base': STRUCT-NAME }
e790e666 189
02a57ae3
EB
190A struct is a dictionary containing a single 'data' key whose value is
191a dictionary; the dictionary may be empty. This corresponds to a
192struct in C or an Object in JSON. Each value of the 'data' dictionary
193must be the name of a type, or a one-element array containing a type
194name. An example of a struct is:
b84da831 195
3b2a8b85 196 { 'struct': 'MyType',
acf8394e 197 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
b84da831 198
e790e666 199The use of '*' as a prefix to the name means the member is optional in
363b4262 200the corresponding JSON protocol usage.
cc162655
EB
201
202The default initialization value of an optional argument should not be changed
203between versions of QEMU unless the new default maintains backward
204compatibility to the user-visible behavior of the old default.
205
206With proper documentation, this policy still allows some flexibility; for
207example, documenting that a default of 0 picks an optimal buffer size allows
208one release to declare the optimal size at 512 while another release declares
209the optimal size at 4096 - the user-visible behavior is not the bytes used by
210the buffer, but the fact that the buffer was optimal size.
211
212On input structures (only mentioned in the 'data' side of a command), changing
213from mandatory to optional is safe (older clients will supply the option, and
214newer clients can benefit from the default); changing from optional to
215mandatory is backwards incompatible (older clients may be omitting the option,
216and must continue to work).
217
218On output structures (only mentioned in the 'returns' side of a command),
219changing from mandatory to optional is in general unsafe (older clients may be
9ee86b85
EB
220expecting the member, and could crash if it is missing), although it
221can be done if the only way that the optional argument will be omitted
222is when it is triggered by the presence of a new input flag to the
223command that older clients don't know to send. Changing from optional
224to mandatory is safe.
cc162655
EB
225
226A structure that is used in both input and output of various commands
227must consider the backwards compatibility constraints of both directions
228of use.
622f557f 229
3b2a8b85 230A struct definition can specify another struct as its base.
9ee86b85 231In this case, the members of the base type are included as top-level members
363b4262
EB
232of the new struct's dictionary in the Client JSON Protocol wire
233format. An example definition is:
622f557f 234
3b2a8b85
EB
235 { 'struct': 'BlockdevOptionsGenericFormat', 'data': { 'file': 'str' } }
236 { 'struct': 'BlockdevOptionsGenericCOWFormat',
622f557f
KW
237 'base': 'BlockdevOptionsGenericFormat',
238 'data': { '*backing': 'str' } }
239
240An example BlockdevOptionsGenericCOWFormat object on the wire could use
9ee86b85 241both members like this:
622f557f
KW
242
243 { "file": "/some/place/my-image",
244 "backing": "/some/place/my-backing-file" }
245
e790e666 246
51631493
KW
247=== Enumeration types ===
248
e790e666 249Usage: { 'enum': STRING, 'data': ARRAY-OF-STRING }
351d36e4 250 { 'enum': STRING, '*prefix': STRING, 'data': ARRAY-OF-STRING }
e790e666
EB
251
252An enumeration type is a dictionary containing a single 'data' key
253whose value is a list of strings. An example enumeration is:
b84da831
MR
254
255 { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
256
e790e666
EB
257Nothing prevents an empty enumeration, although it is probably not
258useful. The list of strings should be lower case; if an enum name
259represents multiple words, use '-' between words. The string 'max' is
260not allowed as an enum value, and values should not be repeated.
261
351d36e4
DB
262The enum constants will be named by using a heuristic to turn the
263type name into a set of underscore separated words. For the example
264above, 'MyEnum' will turn into 'MY_ENUM' giving a constant name
265of 'MY_ENUM_VALUE1' for the first value. If the default heuristic
9ee86b85 266does not result in a desirable name, the optional 'prefix' member
351d36e4
DB
267can be used when defining the enum.
268
363b4262
EB
269The enumeration values are passed as strings over the Client JSON
270Protocol, but are encoded as C enum integral values in generated code.
271While the C code starts numbering at 0, it is better to use explicit
e790e666
EB
272comparisons to enum values than implicit comparisons to 0; the C code
273will also include a generated enum member ending in _MAX for tracking
274the size of the enum, useful when using common functions for
275converting between strings and enum values. Since the wire format
276always passes by name, it is acceptable to reorder or add new
363b4262
EB
277enumeration members in any location without breaking clients of Client
278JSON Protocol; however, removing enum values would break
9ee86b85
EB
279compatibility. For any struct that has a member that will only contain
280a finite set of string values, using an enum type for that member is
281better than open-coding the member to be type 'str'.
e790e666
EB
282
283
51631493
KW
284=== Union types ===
285
e790e666 286Usage: { 'union': STRING, 'data': DICT }
ac4338f8 287or: { 'union': STRING, 'data': DICT, 'base': STRUCT-NAME-OR-DICT,
e790e666 288 'discriminator': ENUM-MEMBER-OF-BASE }
51631493 289
e790e666 290Union types are used to let the user choose between several different
7b1b98c4 291variants for an object. There are two flavors: simple (no
02a57ae3 292discriminator or base), and flat (both discriminator and base). A union
7b1b98c4 293type is defined using a data dictionary as explained in the following
02a57ae3
EB
294paragraphs. The data dictionary for either type of union must not
295be empty.
51631493 296
e790e666
EB
297A simple union type defines a mapping from automatic discriminator
298values to data types like in this example:
51631493 299
bd59adce
EB
300 { 'struct': 'BlockdevOptionsFile', 'data': { 'filename': 'str' } }
301 { 'struct': 'BlockdevOptionsQcow2',
302 'data': { 'backing': 'str', '*lazy-refcounts': 'bool' } }
51631493 303
bd59adce
EB
304 { 'union': 'BlockdevOptionsSimple',
305 'data': { 'file': 'BlockdevOptionsFile',
306 'qcow2': 'BlockdevOptionsQcow2' } }
51631493 307
363b4262 308In the Client JSON Protocol, a simple union is represented by a
9ee86b85
EB
309dictionary that contains the 'type' member as a discriminator, and a
310'data' member that is of the specified data type corresponding to the
363b4262 311discriminator value, as in these examples:
51631493 312
bd59adce
EB
313 { "type": "file", "data": { "filename": "/some/place/my-image" } }
314 { "type": "qcow2", "data": { "backing": "/some/place/my-image",
315 "lazy-refcounts": true } }
51631493 316
e790e666
EB
317The generated C code uses a struct containing a union. Additionally,
318an implicit C enum 'NameKind' is created, corresponding to the union
319'Name', for accessing the various branches of the union. No branch of
320the union can be named 'max', as this would collide with the implicit
321enum. The value for each branch can be of any type.
51631493 322
ac4338f8
EB
323A flat union definition avoids nesting on the wire, and specifies a
324set of common members that occur in all variants of the union. The
d33c8a7d 325'base' key must specify either a type name (the type must be a
ac4338f8
EB
326struct, not a union), or a dictionary representing an anonymous type.
327All branches of the union must be complex types, and the top-level
328members of the union dictionary on the wire will be combination of
329members from both the base type and the appropriate branch type (when
330merging two dictionaries, there must be no keys in common). The
331'discriminator' member must be the name of a non-optional enum-typed
332member of the base struct.
51631493 333
e790e666 334The following example enhances the above simple union example by
bd59adce
EB
335adding an optional common member 'read-only', renaming the
336discriminator to something more applicable than the simple union's
337default of 'type', and reducing the number of {} required on the wire:
50f2bdc7 338
94a3f0af 339 { 'enum': 'BlockdevDriver', 'data': [ 'file', 'qcow2' ] }
50f2bdc7 340 { 'union': 'BlockdevOptions',
ac4338f8 341 'base': { 'driver': 'BlockdevDriver', '*read-only': 'bool' },
50f2bdc7 342 'discriminator': 'driver',
bd59adce
EB
343 'data': { 'file': 'BlockdevOptionsFile',
344 'qcow2': 'BlockdevOptionsQcow2' } }
50f2bdc7 345
e790e666
EB
346Resulting in these JSON objects:
347
bd59adce 348 { "driver": "file", "read-only": true,
e790e666 349 "filename": "/some/place/my-image" }
bd59adce
EB
350 { "driver": "qcow2", "read-only": false,
351 "backing": "/some/place/my-image", "lazy-refcounts": true }
e790e666
EB
352
353Notice that in a flat union, the discriminator name is controlled by
354the user, but because it must map to a base member with enum type, the
355code generator can ensure that branches exist for all values of the
356enum (although the order of the keys need not match the declaration of
357the enum). In the resulting generated C data types, a flat union is
9ee86b85
EB
358represented as a struct with the base members included directly, and
359then a union of structures for each branch of the struct.
e790e666
EB
360
361A simple union can always be re-written as a flat union where the base
362class has a single member named 'type', and where each branch of the
3b2a8b85 363union has a struct with a single member named 'data'. That is,
50f2bdc7 364
e790e666 365 { 'union': 'Simple', 'data': { 'one': 'str', 'two': 'int' } }
50f2bdc7 366
e790e666 367is identical on the wire to:
50f2bdc7 368
e790e666 369 { 'enum': 'Enum', 'data': ['one', 'two'] }
3b2a8b85
EB
370 { 'struct': 'Branch1', 'data': { 'data': 'str' } }
371 { 'struct': 'Branch2', 'data': { 'data': 'int' } }
ac4338f8 372 { 'union': 'Flat': 'base': { 'type': 'Enum' }, 'discriminator': 'type',
e790e666 373 'data': { 'one': 'Branch1', 'two': 'Branch2' } }
69dd62df 374
e790e666 375
7b1b98c4 376=== Alternate types ===
69dd62df 377
7b1b98c4
EB
378Usage: { 'alternate': STRING, 'data': DICT }
379
380An alternate type is one that allows a choice between two or more JSON
381data types (string, integer, number, or object, but currently not
382array) on the wire. The definition is similar to a simple union type,
383where each branch of the union names a QAPI type. For example:
384
bd59adce 385 { 'alternate': 'BlockdevRef',
69dd62df
KW
386 'data': { 'definition': 'BlockdevOptions',
387 'reference': 'str' } }
388
7b1b98c4 389Unlike a union, the discriminator string is never passed on the wire
363b4262
EB
390for the Client JSON Protocol. Instead, the value's JSON type serves
391as an implicit discriminator, which in turn means that an alternate
392can only express a choice between types represented differently in
393JSON. If a branch is typed as the 'bool' built-in, the alternate
394accepts true and false; if it is typed as any of the various numeric
395built-ins, it accepts a JSON number; if it is typed as a 'str'
396built-in or named enum type, it accepts a JSON string; and if it is
397typed as a complex type (struct or union), it accepts a JSON object.
398Two different complex types, for instance, aren't permitted, because
399both are represented as a JSON object.
7b1b98c4
EB
400
401The example alternate declaration above allows using both of the
402following example objects:
69dd62df
KW
403
404 { "file": "my_existing_block_device_id" }
405 { "file": { "driver": "file",
bd59adce 406 "read-only": false,
63922c64 407 "filename": "/tmp/mydisk.qcow2" } }
69dd62df
KW
408
409
51631493 410=== Commands ===
b84da831 411
e790e666 412Usage: { 'command': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT,
c818408e 413 '*returns': TYPE-NAME, '*boxed': true,
e790e666
EB
414 '*gen': false, '*success-response': false }
415
416Commands are defined by using a dictionary containing several members,
417where three members are most common. The 'command' member is a
363b4262
EB
418mandatory string, and determines the "execute" value passed in a
419Client JSON Protocol command exchange.
e790e666
EB
420
421The 'data' argument maps to the "arguments" dictionary passed in as
363b4262
EB
422part of a Client JSON Protocol command. The 'data' member is optional
423and defaults to {} (an empty dictionary). If present, it must be the
315932b5
MA
424string name of a complex type, or a dictionary that declares an
425anonymous type with the same semantics as a 'struct' expression, with
426one exception noted below when 'gen' is used.
e790e666 427
9ee86b85 428The 'returns' member describes what will appear in the "return" member
363b4262
EB
429of a Client JSON Protocol reply on successful completion of a command.
430The member is optional from the command declaration; if absent, the
9ee86b85 431"return" member will be an empty dictionary. If 'returns' is present,
363b4262
EB
432it must be the string name of a complex or built-in type, a
433one-element array containing the name of a complex or built-in type,
9b090d42
MA
434with one exception noted below when 'gen' is used. Although it is
435permitted to have the 'returns' member name a built-in type or an
436array of built-in types, any command that does this cannot be extended
437to return additional information in the future; thus, new commands
438should strongly consider returning a dictionary-based type or an array
9ee86b85 439of dictionaries, even if the dictionary only contains one member at the
9b090d42 440present.
363b4262
EB
441
442All commands in Client JSON Protocol use a dictionary to report
443failure, with no way to specify that in QAPI. Where the error return
444is different than the usual GenericError class in order to help the
445client react differently to certain error conditions, it is worth
446documenting this in the comments before the command declaration.
e790e666
EB
447
448Some example commands:
449
450 { 'command': 'my-first-command',
451 'data': { 'arg1': 'str', '*arg2': 'str' } }
3b2a8b85 452 { 'struct': 'MyType', 'data': { '*value': 'str' } }
e790e666
EB
453 { 'command': 'my-second-command',
454 'returns': [ 'MyType' ] }
455
363b4262 456which would validate this Client JSON Protocol transaction:
e790e666
EB
457
458 => { "execute": "my-first-command",
459 "arguments": { "arg1": "hello" } }
460 <= { "return": { } }
461 => { "execute": "my-second-command" }
462 <= { "return": [ { "value": "one" }, { } ] }
463
c818408e
EB
464The generator emits a prototype for the user's function implementing
465the command. Normally, 'data' is a dictionary for an anonymous type,
466or names a struct type (possibly empty, but not a union), and its
467members are passed as separate arguments to this function. If the
468command definition includes a key 'boxed' with the boolean value true,
469then 'data' is instead the name of any non-empty complex type
470(struct, union, or alternate), and a pointer to that QAPI type is
471passed as a single argument.
472
473The generator also emits a marshalling function that extracts
474arguments for the user's function out of an input QDict, calls the
475user's function, and if it succeeded, builds an output QObject from
476its return value.
477
e790e666 478In rare cases, QAPI cannot express a type-safe representation of a
2d21291a
MA
479corresponding Client JSON Protocol command. You then have to suppress
480generation of a marshalling function by including a key 'gen' with
481boolean value false, and instead write your own function. Please try
482to avoid adding new commands that rely on this, and instead use
483type-safe unions. For an example of this usage:
e790e666
EB
484
485 { 'command': 'netdev_add',
b8a98326 486 'data': {'type': 'str', 'id': 'str'},
e790e666
EB
487 'gen': false }
488
489Normally, the QAPI schema is used to describe synchronous exchanges,
490where a response is expected. But in some cases, the action of a
491command is expected to change state in a way that a successful
492response is not possible (although the command will still return a
493normal dictionary error on failure). When a successful reply is not
494possible, the command expression should include the optional key
495'success-response' with boolean value false. So far, only QGA makes
9ee86b85 496use of this member.
b84da831 497
b84da831 498
21cd70df
WX
499=== Events ===
500
c818408e
EB
501Usage: { 'event': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT,
502 '*boxed': true }
e790e666
EB
503
504Events are defined with the keyword 'event'. It is not allowed to
505name an event 'MAX', since the generator also produces a C enumeration
506of all event names with a generated _MAX value at the end. When
507'data' is also specified, additional info will be included in the
3b2a8b85 508event, with similar semantics to a 'struct' expression. Finally there
e790e666
EB
509will be C API generated in qapi-event.h; when called by QEMU code, a
510message with timestamp will be emitted on the wire.
21cd70df
WX
511
512An example event is:
513
514{ 'event': 'EVENT_C',
515 'data': { '*a': 'int', 'b': 'str' } }
516
517Resulting in this JSON object:
518
519{ "event": "EVENT_C",
520 "data": { "b": "test string" },
521 "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
b84da831 522
c818408e
EB
523The generator emits a function to send the event. Normally, 'data' is
524a dictionary for an anonymous type, or names a struct type (possibly
525empty, but not a union), and its members are passed as separate
526arguments to this function. If the event definition includes a key
527'boxed' with the boolean value true, then 'data' is instead the name of
528any non-empty complex type (struct, union, or alternate), and a
529pointer to that QAPI type is passed as a single argument.
530
59a2c4ce 531
39a18158
MA
532== Client JSON Protocol introspection ==
533
534Clients of a Client JSON Protocol commonly need to figure out what
535exactly the server (QEMU) supports.
536
537For this purpose, QMP provides introspection via command
538query-qmp-schema. QGA currently doesn't support introspection.
539
39a65e2c
EB
540While Client JSON Protocol wire compatibility should be maintained
541between qemu versions, we cannot make the same guarantees for
542introspection stability. For example, one version of qemu may provide
543a non-variant optional member of a struct, and a later version rework
544the member to instead be non-optional and associated with a variant.
545Likewise, one version of qemu may list a member with open-ended type
546'str', and a later version could convert it to a finite set of strings
547via an enum type; or a member may be converted from a specific type to
548an alternate that represents a choice between the original type and
549something else.
550
39a18158
MA
551query-qmp-schema returns a JSON array of SchemaInfo objects. These
552objects together describe the wire ABI, as defined in the QAPI schema.
f5455044
EB
553There is no specified order to the SchemaInfo objects returned; a
554client must search for a particular name throughout the entire array
555to learn more about that name, but is at least guaranteed that there
556will be no collisions between type, command, and event names.
39a18158
MA
557
558However, the SchemaInfo can't reflect all the rules and restrictions
559that apply to QMP. It's interface introspection (figuring out what's
560there), not interface specification. The specification is in the QAPI
561schema. To understand how QMP is to be used, you need to study the
562QAPI schema.
563
564Like any other command, query-qmp-schema is itself defined in the QAPI
565schema, along with the SchemaInfo type. This text attempts to give an
566overview how things work. For details you need to consult the QAPI
567schema.
568
569SchemaInfo objects have common members "name" and "meta-type", and
570additional variant members depending on the value of meta-type.
571
572Each SchemaInfo object describes a wire ABI entity of a certain
573meta-type: a command, event or one of several kinds of type.
574
1a9a507b
MA
575SchemaInfo for commands and events have the same name as in the QAPI
576schema.
39a18158
MA
577
578Command and event names are part of the wire ABI, but type names are
1a9a507b
MA
579not. Therefore, the SchemaInfo for types have auto-generated
580meaningless names. For readability, the examples in this section use
581meaningful type names instead.
582
583To examine a type, start with a command or event using it, then follow
584references by name.
39a18158
MA
585
586QAPI schema definitions not reachable that way are omitted.
587
588The SchemaInfo for a command has meta-type "command", and variant
589members "arg-type" and "ret-type". On the wire, the "arguments"
590member of a client's "execute" command must conform to the object type
591named by "arg-type". The "return" member that the server passes in a
592success response conforms to the type named by "ret-type".
593
594If the command takes no arguments, "arg-type" names an object type
595without members. Likewise, if the command returns nothing, "ret-type"
596names an object type without members.
597
598Example: the SchemaInfo for command query-qmp-schema
599
600 { "name": "query-qmp-schema", "meta-type": "command",
7599697c 601 "arg-type": "q_empty", "ret-type": "SchemaInfoList" }
39a18158 602
7599697c 603 Type "q_empty" is an automatic object type without members, and type
39a18158
MA
604 "SchemaInfoList" is the array of SchemaInfo type.
605
606The SchemaInfo for an event has meta-type "event", and variant member
607"arg-type". On the wire, a "data" member that the server passes in an
608event conforms to the object type named by "arg-type".
609
610If the event carries no additional information, "arg-type" names an
611object type without members. The event may not have a data member on
612the wire then.
613
614Each command or event defined with dictionary-valued 'data' in the
1a9a507b 615QAPI schema implicitly defines an object type.
39a18158
MA
616
617Example: the SchemaInfo for EVENT_C from section Events
618
619 { "name": "EVENT_C", "meta-type": "event",
7599697c 620 "arg-type": "q_obj-EVENT_C-arg" }
39a18158 621
7599697c 622 Type "q_obj-EVENT_C-arg" is an implicitly defined object type with
39a18158
MA
623 the two members from the event's definition.
624
625The SchemaInfo for struct and union types has meta-type "object".
626
627The SchemaInfo for a struct type has variant member "members".
628
629The SchemaInfo for a union type additionally has variant members "tag"
630and "variants".
631
632"members" is a JSON array describing the object's common members, if
633any. Each element is a JSON object with members "name" (the member's
634name), "type" (the name of its type), and optionally "default". The
635member is optional if "default" is present. Currently, "default" can
636only have value null. Other values are reserved for future
f5455044
EB
637extensions. The "members" array is in no particular order; clients
638must search the entire object when learning whether a particular
639member is supported.
39a18158
MA
640
641Example: the SchemaInfo for MyType from section Struct types
642
643 { "name": "MyType", "meta-type": "object",
644 "members": [
645 { "name": "member1", "type": "str" },
646 { "name": "member2", "type": "int" },
647 { "name": "member3", "type": "str", "default": null } ] }
648
649"tag" is the name of the common member serving as type tag.
650"variants" is a JSON array describing the object's variant members.
651Each element is a JSON object with members "case" (the value of type
652tag this element applies to) and "type" (the name of an object type
f5455044
EB
653that provides the variant members for this type tag value). The
654"variants" array is in no particular order, and is not guaranteed to
655list cases in the same order as the corresponding "tag" enum type.
39a18158
MA
656
657Example: the SchemaInfo for flat union BlockdevOptions from section
658Union types
659
660 { "name": "BlockdevOptions", "meta-type": "object",
661 "members": [
662 { "name": "driver", "type": "BlockdevDriver" },
bd59adce 663 { "name": "read-only", "type": "bool", "default": null } ],
39a18158
MA
664 "tag": "driver",
665 "variants": [
bd59adce
EB
666 { "case": "file", "type": "BlockdevOptionsFile" },
667 { "case": "qcow2", "type": "BlockdevOptionsQcow2" } ] }
39a18158
MA
668
669Note that base types are "flattened": its members are included in the
670"members" array.
671
672A simple union implicitly defines an enumeration type for its implicit
673discriminator (called "type" on the wire, see section Union types).
39a18158
MA
674
675A simple union implicitly defines an object type for each of its
1a9a507b 676variants.
39a18158 677
bd59adce 678Example: the SchemaInfo for simple union BlockdevOptionsSimple from section
39a18158
MA
679Union types
680
bd59adce 681 { "name": "BlockdevOptionsSimple", "meta-type": "object",
39a18158 682 "members": [
bd59adce 683 { "name": "type", "type": "BlockdevOptionsSimpleKind" } ],
39a18158
MA
684 "tag": "type",
685 "variants": [
bd59adce
EB
686 { "case": "file", "type": "q_obj-BlockdevOptionsFile-wrapper" },
687 { "case": "qcow2", "type": "q_obj-BlockdevOptionsQcow2-wrapper" } ] }
39a18158 688
bd59adce
EB
689 Enumeration type "BlockdevOptionsSimpleKind" and the object types
690 "q_obj-BlockdevOptionsFile-wrapper", "q_obj-BlockdevOptionsQcow2-wrapper"
691 are implicitly defined.
39a18158
MA
692
693The SchemaInfo for an alternate type has meta-type "alternate", and
694variant member "members". "members" is a JSON array. Each element is
695a JSON object with member "type", which names a type. Values of the
f5455044
EB
696alternate type conform to exactly one of its member types. There is
697no guarantee on the order in which "members" will be listed.
39a18158 698
bd59adce 699Example: the SchemaInfo for BlockdevRef from section Alternate types
39a18158 700
bd59adce 701 { "name": "BlockdevRef", "meta-type": "alternate",
39a18158
MA
702 "members": [
703 { "type": "BlockdevOptions" },
704 { "type": "str" } ] }
705
706The SchemaInfo for an array type has meta-type "array", and variant
707member "element-type", which names the array's element type. Array
ce5fcb47
EB
708types are implicitly defined. For convenience, the array's name may
709resemble the element type; however, clients should examine member
710"element-type" instead of making assumptions based on parsing member
711"name".
39a18158
MA
712
713Example: the SchemaInfo for ['str']
714
ce5fcb47 715 { "name": "[str]", "meta-type": "array",
39a18158
MA
716 "element-type": "str" }
717
718The SchemaInfo for an enumeration type has meta-type "enum" and
f5455044
EB
719variant member "values". The values are listed in no particular
720order; clients must search the entire enum when learning whether a
721particular value is supported.
39a18158
MA
722
723Example: the SchemaInfo for MyEnum from section Enumeration types
724
725 { "name": "MyEnum", "meta-type": "enum",
726 "values": [ "value1", "value2", "value3" ] }
727
728The SchemaInfo for a built-in type has the same name as the type in
729the QAPI schema (see section Built-in Types), with one exception
730detailed below. It has variant member "json-type" that shows how
731values of this type are encoded on the wire.
732
733Example: the SchemaInfo for str
734
735 { "name": "str", "meta-type": "builtin", "json-type": "string" }
736
737The QAPI schema supports a number of integer types that only differ in
738how they map to C. They are identical as far as SchemaInfo is
739concerned. Therefore, they get all mapped to a single type "int" in
740SchemaInfo.
741
742As explained above, type names are not part of the wire ABI. Not even
743the names of built-in types. Clients should examine member
744"json-type" instead of hard-coding names of built-in types.
745
746
b84da831
MR
747== Code generation ==
748
9ee86b85 749Schemas are fed into five scripts to generate all the code/files that,
39a18158
MA
750paired with the core QAPI libraries, comprise everything required to
751take JSON commands read in by a Client JSON Protocol server, unmarshal
752the arguments into the underlying C types, call into the corresponding
9ee86b85
EB
753C function, map the response back to a Client JSON Protocol response
754to be returned to the user, and introspect the commands.
b84da831 755
9ee86b85
EB
756As an example, we'll use the following schema, which describes a
757single complex user-defined type, along with command which takes a
758list of that type as a parameter, and returns a single element of that
759type. The user is responsible for writing the implementation of
760qmp_my_command(); everything else is produced by the generator.
b84da831 761
87a560c4 762 $ cat example-schema.json
3b2a8b85 763 { 'struct': 'UserDefOne',
9ee86b85 764 'data': { 'integer': 'int', '*string': 'str' } }
b84da831
MR
765
766 { 'command': 'my-command',
9ee86b85 767 'data': { 'arg1': ['UserDefOne'] },
b84da831 768 'returns': 'UserDefOne' }
b84da831 769
59a2c4ce
EB
770 { 'event': 'MY_EVENT' }
771
9ee86b85
EB
772For a more thorough look at generated code, the testsuite includes
773tests/qapi-schema/qapi-schema-tests.json that covers more examples of
774what the generator will accept, and compiles the resulting C code as
775part of 'make check-unit'.
776
b84da831
MR
777=== scripts/qapi-types.py ===
778
9ee86b85
EB
779Used to generate the C types defined by a schema, along with
780supporting code. The following files are created:
b84da831
MR
781
782$(prefix)qapi-types.h - C types corresponding to types defined in
783 the schema you pass in
784$(prefix)qapi-types.c - Cleanup functions for the above C types
785
786The $(prefix) is an optional parameter used as a namespace to keep the
787generated code from one schema/code-generation separated from others so code
788can be generated/used from multiple schemas without clobbering previously
789created code.
790
791Example:
792
87a560c4 793 $ python scripts/qapi-types.py --output-dir="qapi-generated" \
16d80f61 794 --prefix="example-" example-schema.json
9ee86b85
EB
795 $ cat qapi-generated/example-qapi-types.h
796[Uninteresting stuff omitted...]
797
798 #ifndef EXAMPLE_QAPI_TYPES_H
799 #define EXAMPLE_QAPI_TYPES_H
800
801[Built-in types omitted...]
802
803 typedef struct UserDefOne UserDefOne;
804
805 typedef struct UserDefOneList UserDefOneList;
806
807 struct UserDefOne {
808 int64_t integer;
809 bool has_string;
810 char *string;
811 };
812
813 void qapi_free_UserDefOne(UserDefOne *obj);
814
815 struct UserDefOneList {
816 UserDefOneList *next;
817 UserDefOne *value;
818 };
819
820 void qapi_free_UserDefOneList(UserDefOneList *obj);
821
822 #endif
87a560c4 823 $ cat qapi-generated/example-qapi-types.c
6e2bb3ec
MA
824[Uninteresting stuff omitted...]
825
2b162ccb 826 void qapi_free_UserDefOne(UserDefOne *obj)
6e2bb3ec 827 {
6e2bb3ec
MA
828 Visitor *v;
829
830 if (!obj) {
831 return;
832 }
833
2c0ef9f4 834 v = qapi_dealloc_visitor_new();
9ee86b85 835 visit_type_UserDefOne(v, NULL, &obj, NULL);
2c0ef9f4 836 visit_free(v);
6e2bb3ec 837 }
b84da831 838
2b162ccb 839 void qapi_free_UserDefOneList(UserDefOneList *obj)
b84da831 840 {
b84da831
MR
841 Visitor *v;
842
843 if (!obj) {
844 return;
845 }
846
2c0ef9f4 847 v = qapi_dealloc_visitor_new();
9ee86b85 848 visit_type_UserDefOneList(v, NULL, &obj, NULL);
2c0ef9f4 849 visit_free(v);
b84da831 850 }
b84da831 851
b84da831
MR
852=== scripts/qapi-visit.py ===
853
9ee86b85
EB
854Used to generate the visitor functions used to walk through and
855convert between a native QAPI C data structure and some other format
856(such as QObject); the generated functions are named visit_type_FOO()
857and visit_type_FOO_members().
b84da831
MR
858
859The following files are generated:
860
861$(prefix)qapi-visit.c: visitor function for a particular C type, used
862 to automagically convert QObjects into the
863 corresponding C type and vice-versa, as well
864 as for deallocating memory for an existing C
865 type
866
867$(prefix)qapi-visit.h: declarations for previously mentioned visitor
868 functions
869
870Example:
871
87a560c4 872 $ python scripts/qapi-visit.py --output-dir="qapi-generated"
16d80f61 873 --prefix="example-" example-schema.json
9ee86b85
EB
874 $ cat qapi-generated/example-qapi-visit.h
875[Uninteresting stuff omitted...]
876
877 #ifndef EXAMPLE_QAPI_VISIT_H
878 #define EXAMPLE_QAPI_VISIT_H
879
880[Visitors for built-in types omitted...]
881
882 void visit_type_UserDefOne_members(Visitor *v, UserDefOne *obj, Error **errp);
883 void visit_type_UserDefOne(Visitor *v, const char *name, UserDefOne **obj, Error **errp);
884 void visit_type_UserDefOneList(Visitor *v, const char *name, UserDefOneList **obj, Error **errp);
885
886 #endif
87a560c4 887 $ cat qapi-generated/example-qapi-visit.c
6e2bb3ec 888[Uninteresting stuff omitted...]
b84da831 889
9ee86b85 890 void visit_type_UserDefOne_members(Visitor *v, UserDefOne *obj, Error **errp)
6e2bb3ec
MA
891 {
892 Error *err = NULL;
3a864e7c 893
9ee86b85 894 visit_type_int(v, "integer", &obj->integer, &err);
297a3646
MA
895 if (err) {
896 goto out;
897 }
9ee86b85
EB
898 if (visit_optional(v, "string", &obj->has_string)) {
899 visit_type_str(v, "string", &obj->string, &err);
900 if (err) {
901 goto out;
902 }
297a3646 903 }
6e2bb3ec 904
297a3646 905 out:
6e2bb3ec
MA
906 error_propagate(errp, err);
907 }
b84da831 908
9ee86b85 909 void visit_type_UserDefOne(Visitor *v, const char *name, UserDefOne **obj, Error **errp)
b84da831 910 {
297a3646
MA
911 Error *err = NULL;
912
9ee86b85
EB
913 visit_start_struct(v, name, (void **)obj, sizeof(UserDefOne), &err);
914 if (err) {
915 goto out;
916 }
917 if (!*obj) {
918 goto out_obj;
6e2bb3ec 919 }
9ee86b85 920 visit_type_UserDefOne_members(v, *obj, &err);
15c2f669
EB
921 if (err) {
922 goto out_obj;
923 }
924 visit_check_struct(v, &err);
9ee86b85 925 out_obj:
1158bb2a 926 visit_end_struct(v, (void **)obj);
68ab47e4
EB
927 if (err && visit_is_input(v)) {
928 qapi_free_UserDefOne(*obj);
929 *obj = NULL;
930 }
9ee86b85 931 out:
297a3646 932 error_propagate(errp, err);
b84da831
MR
933 }
934
9ee86b85 935 void visit_type_UserDefOneList(Visitor *v, const char *name, UserDefOneList **obj, Error **errp)
b84da831 936 {
6e2bb3ec 937 Error *err = NULL;
d9f62dde
EB
938 UserDefOneList *tail;
939 size_t size = sizeof(**obj);
6e2bb3ec 940
d9f62dde 941 visit_start_list(v, name, (GenericList **)obj, size, &err);
297a3646
MA
942 if (err) {
943 goto out;
944 }
945
d9f62dde
EB
946 for (tail = *obj; tail;
947 tail = (UserDefOneList *)visit_next_list(v, (GenericList *)tail, size)) {
948 visit_type_UserDefOne(v, NULL, &tail->value, &err);
949 if (err) {
950 break;
951 }
b84da831 952 }
297a3646 953
1158bb2a 954 visit_end_list(v, (void **)obj);
68ab47e4
EB
955 if (err && visit_is_input(v)) {
956 qapi_free_UserDefOneList(*obj);
957 *obj = NULL;
958 }
297a3646
MA
959 out:
960 error_propagate(errp, err);
b84da831 961 }
b84da831 962
b84da831
MR
963=== scripts/qapi-commands.py ===
964
9ee86b85
EB
965Used to generate the marshaling/dispatch functions for the commands
966defined in the schema. The generated code implements
bd6092e4
MAL
967qmp_marshal_COMMAND() (registered automatically), and declares
968qmp_COMMAND() that the user must implement. The following files are
969generated:
b84da831
MR
970
971$(prefix)qmp-marshal.c: command marshal/dispatch functions for each
972 QMP command defined in the schema. Functions
973 generated by qapi-visit.py are used to
2542bfd5 974 convert QObjects received from the wire into
b84da831
MR
975 function parameters, and uses the same
976 visitor functions to convert native C return
977 values to QObjects from transmission back
978 over the wire.
979
980$(prefix)qmp-commands.h: Function prototypes for the QMP commands
981 specified in the schema.
982
983Example:
984
59a2c4ce 985 $ python scripts/qapi-commands.py --output-dir="qapi-generated"
16d80f61 986 --prefix="example-" example-schema.json
9ee86b85
EB
987 $ cat qapi-generated/example-qmp-commands.h
988[Uninteresting stuff omitted...]
989
990 #ifndef EXAMPLE_QMP_COMMANDS_H
991 #define EXAMPLE_QMP_COMMANDS_H
992
993 #include "example-qapi-types.h"
994 #include "qapi/qmp/qdict.h"
995 #include "qapi/error.h"
996
997 UserDefOne *qmp_my_command(UserDefOneList *arg1, Error **errp);
998
999 #endif
87a560c4 1000 $ cat qapi-generated/example-qmp-marshal.c
6e2bb3ec 1001[Uninteresting stuff omitted...]
b84da831 1002
56d92b00 1003 static void qmp_marshal_output_UserDefOne(UserDefOne *ret_in, QObject **ret_out, Error **errp)
b84da831 1004 {
2a0f50e8 1005 Error *err = NULL;
b84da831
MR
1006 Visitor *v;
1007
3b098d56 1008 v = qmp_output_visitor_new(ret_out);
9ee86b85 1009 visit_type_UserDefOne(v, "unused", &ret_in, &err);
3b098d56
EB
1010 if (!err) {
1011 visit_complete(v, ret_out);
6e2bb3ec 1012 }
2a0f50e8 1013 error_propagate(errp, err);
2c0ef9f4
EB
1014 visit_free(v);
1015 v = qapi_dealloc_visitor_new();
9ee86b85 1016 visit_type_UserDefOne(v, "unused", &ret_in, NULL);
2c0ef9f4 1017 visit_free(v);
b84da831
MR
1018 }
1019
7fad30f0 1020 static void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp)
b84da831 1021 {
2a0f50e8 1022 Error *err = NULL;
3f99144c 1023 UserDefOne *retval;
b84da831 1024 Visitor *v;
9ee86b85 1025 UserDefOneList *arg1 = NULL;
b84da831 1026
b70ce101 1027 v = qmp_input_visitor_new(QOBJECT(args), true);
ed841535
EB
1028 visit_start_struct(v, NULL, NULL, 0, &err);
1029 if (err) {
1030 goto out;
1031 }
9ee86b85 1032 visit_type_UserDefOneList(v, "arg1", &arg1, &err);
15c2f669
EB
1033 if (!err) {
1034 visit_check_struct(v, &err);
1035 }
1158bb2a 1036 visit_end_struct(v, NULL);
2a0f50e8 1037 if (err) {
b84da831
MR
1038 goto out;
1039 }
297a3646 1040
2a0f50e8
EB
1041 retval = qmp_my_command(arg1, &err);
1042 if (err) {
297a3646 1043 goto out;
6e2bb3ec 1044 }
b84da831 1045
2a0f50e8 1046 qmp_marshal_output_UserDefOne(retval, ret, &err);
297a3646 1047
b84da831 1048 out:
2a0f50e8 1049 error_propagate(errp, err);
2c0ef9f4
EB
1050 visit_free(v);
1051 v = qapi_dealloc_visitor_new();
ed841535 1052 visit_start_struct(v, NULL, NULL, 0, NULL);
9ee86b85 1053 visit_type_UserDefOneList(v, "arg1", &arg1, NULL);
1158bb2a 1054 visit_end_struct(v, NULL);
2c0ef9f4 1055 visit_free(v);
b84da831
MR
1056 }
1057
1058 static void qmp_init_marshal(void)
1059 {
7fad30f0 1060 qmp_register_command("my-command", qmp_marshal_my_command, QCO_NO_OPTIONS);
b84da831
MR
1061 }
1062
1063 qapi_init(qmp_init_marshal);
59a2c4ce
EB
1064
1065=== scripts/qapi-event.py ===
1066
9ee86b85
EB
1067Used to generate the event-related C code defined by a schema, with
1068implementations for qapi_event_send_FOO(). The following files are
1069created:
59a2c4ce
EB
1070
1071$(prefix)qapi-event.h - Function prototypes for each event type, plus an
1072 enumeration of all event names
1073$(prefix)qapi-event.c - Implementation of functions to send an event
1074
1075Example:
1076
1077 $ python scripts/qapi-event.py --output-dir="qapi-generated"
16d80f61 1078 --prefix="example-" example-schema.json
9ee86b85
EB
1079 $ cat qapi-generated/example-qapi-event.h
1080[Uninteresting stuff omitted...]
1081
1082 #ifndef EXAMPLE_QAPI_EVENT_H
1083 #define EXAMPLE_QAPI_EVENT_H
1084
1085 #include "qapi/error.h"
1086 #include "qapi/qmp/qdict.h"
1087 #include "example-qapi-types.h"
1088
1089
1090 void qapi_event_send_my_event(Error **errp);
1091
1092 typedef enum example_QAPIEvent {
1093 EXAMPLE_QAPI_EVENT_MY_EVENT = 0,
1094 EXAMPLE_QAPI_EVENT__MAX = 1,
1095 } example_QAPIEvent;
1096
1097 extern const char *const example_QAPIEvent_lookup[];
1098
1099 #endif
59a2c4ce
EB
1100 $ cat qapi-generated/example-qapi-event.c
1101[Uninteresting stuff omitted...]
1102
1103 void qapi_event_send_my_event(Error **errp)
1104 {
1105 QDict *qmp;
2a0f50e8 1106 Error *err = NULL;
59a2c4ce
EB
1107 QMPEventFuncEmit emit;
1108 emit = qmp_event_get_func_emit();
1109 if (!emit) {
1110 return;
1111 }
1112
1113 qmp = qmp_event_build_dict("MY_EVENT");
1114
2a0f50e8 1115 emit(EXAMPLE_QAPI_EVENT_MY_EVENT, qmp, &err);
59a2c4ce 1116
2a0f50e8 1117 error_propagate(errp, err);
59a2c4ce
EB
1118 QDECREF(qmp);
1119 }
1120
efd2eaa6
MA
1121 const char *const example_QAPIEvent_lookup[] = {
1122 [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT",
7fb1cf16 1123 [EXAMPLE_QAPI_EVENT__MAX] = NULL,
59a2c4ce 1124 };
39a18158
MA
1125
1126=== scripts/qapi-introspect.py ===
1127
1128Used to generate the introspection C code for a schema. The following
1129files are created:
1130
1131$(prefix)qmp-introspect.c - Defines a string holding a JSON
1132 description of the schema.
1133$(prefix)qmp-introspect.h - Declares the above string.
1134
1135Example:
1136
1137 $ python scripts/qapi-introspect.py --output-dir="qapi-generated"
1138 --prefix="example-" example-schema.json
39a18158
MA
1139 $ cat qapi-generated/example-qmp-introspect.h
1140[Uninteresting stuff omitted...]
1141
1142 #ifndef EXAMPLE_QMP_INTROSPECT_H
1143 #define EXAMPLE_QMP_INTROSPECT_H
1144
1145 extern const char example_qmp_schema_json[];
1146
1147 #endif
9ee86b85
EB
1148 $ cat qapi-generated/example-qmp-introspect.c
1149[Uninteresting stuff omitted...]
1150
1151 const char example_qmp_schema_json[] = "["
1152 "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
1153 "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
1154 "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
1155 "{\"members\": [{\"name\": \"arg1\", \"type\": \"[2]\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
1156 "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"default\": null, \"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
1157 "{\"element-type\": \"2\", \"meta-type\": \"array\", \"name\": \"[2]\"}, "
1158 "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
1159 "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";