xref: /qemu/docs/devel/writing-monitor-commands.rst (revision 3d312f417d6c3baf7e2b3f321e17ed9d74471ff4)
10e33e3d2SDaniel P. BerrangéHow to write monitor commands
20e33e3d2SDaniel P. Berrangé=============================
34b389b5dSLuiz Capitulino
44b389b5dSLuiz CapitulinoThis document is a step-by-step guide on how to write new QMP commands using
50e33e3d2SDaniel P. Berrangéthe QAPI framework and HMP commands.
64b389b5dSLuiz Capitulino
74b389b5dSLuiz CapitulinoThis document doesn't discuss QMP protocol level details, nor does it dive
84b389b5dSLuiz Capitulinointo the QAPI framework implementation.
94b389b5dSLuiz Capitulino
104b389b5dSLuiz CapitulinoFor an in-depth introduction to the QAPI framework, please refer to
11b3125e73SPhilippe Mathieu-Daudédocs/devel/qapi-code-gen.txt. For documentation about the QMP protocol,
12cfb41b88SCleber Rosastart with docs/interop/qmp-intro.txt.
134b389b5dSLuiz Capitulino
14*3d312f41SDaniel P. BerrangéNew commands may be implemented in QMP only.  New HMP commands should be
15*3d312f41SDaniel P. Berrangéimplemented on top of QMP.  The typical HMP command wraps around an
16*3d312f41SDaniel P. Berrangéequivalent QMP command, but HMP convenience commands built from QMP
17*3d312f41SDaniel P. Berrangébuilding blocks are also fine.  The long term goal is to make all
18*3d312f41SDaniel P. Berrangéexisting HMP commands conform to this, to fully isolate HMP from the
19*3d312f41SDaniel P. Berrangéinternals of QEMU. Refer to the `Writing a debugging aid returning
20*3d312f41SDaniel P. Berrangéunstructured text`_ section for further guidance on commands that
21*3d312f41SDaniel P. Berrangéwould have traditionally been HMP only.
2268e6dc59SJohn Snow
2368e6dc59SJohn SnowOverview
2468e6dc59SJohn Snow--------
254b389b5dSLuiz Capitulino
264b389b5dSLuiz CapitulinoGenerally speaking, the following steps should be taken in order to write a
274b389b5dSLuiz Capitulinonew QMP command.
284b389b5dSLuiz Capitulino
29bfe873e9SMarkus Armbruster1. Define the command and any types it needs in the appropriate QAPI
30bfe873e9SMarkus Armbruster   schema module.
314b389b5dSLuiz Capitulino
324b389b5dSLuiz Capitulino2. Write the QMP command itself, which is a regular C function. Preferably,
334b389b5dSLuiz Capitulino   the command should be exported by some QEMU subsystem. But it can also be
34f1b3ccfaSKevin Wolf   added to the monitor/qmp-cmds.c file
354b389b5dSLuiz Capitulino
364b389b5dSLuiz Capitulino3. At this point the command can be tested under the QMP protocol
374b389b5dSLuiz Capitulino
384b389b5dSLuiz Capitulino4. Write the HMP command equivalent. This is not required and should only be
394b389b5dSLuiz Capitulino   done if it does make sense to have the functionality in HMP. The HMP command
404b389b5dSLuiz Capitulino   is implemented in terms of the QMP command
414b389b5dSLuiz Capitulino
424b389b5dSLuiz CapitulinoThe following sections will demonstrate each of the steps above. We will start
434b389b5dSLuiz Capitulinovery simple and get more complex as we progress.
444b389b5dSLuiz Capitulino
4568e6dc59SJohn Snow
4668e6dc59SJohn SnowTesting
4768e6dc59SJohn Snow-------
484b389b5dSLuiz Capitulino
494b389b5dSLuiz CapitulinoFor all the examples in the next sections, the test setup is the same and is
504b389b5dSLuiz Capitulinoshown here.
514b389b5dSLuiz Capitulino
5268e6dc59SJohn SnowFirst, QEMU should be started like this::
534b389b5dSLuiz Capitulino
54bb46af41SMarkus Armbruster # qemu-system-TARGET [...] \
55c2387413SDaniel P. Berrangé     -chardev socket,id=qmp,port=4444,host=localhost,server=on \
564b389b5dSLuiz Capitulino     -mon chardev=qmp,mode=control,pretty=on
574b389b5dSLuiz Capitulino
5868e6dc59SJohn SnowThen, in a different terminal::
594b389b5dSLuiz Capitulino
604b389b5dSLuiz Capitulino $ telnet localhost 4444
614b389b5dSLuiz Capitulino Trying 127.0.0.1...
624b389b5dSLuiz Capitulino Connected to localhost.
634b389b5dSLuiz Capitulino Escape character is '^]'.
644b389b5dSLuiz Capitulino {
654b389b5dSLuiz Capitulino     "QMP": {
664b389b5dSLuiz Capitulino         "version": {
674b389b5dSLuiz Capitulino             "qemu": {
684b389b5dSLuiz Capitulino                 "micro": 50,
694b389b5dSLuiz Capitulino                 "minor": 15,
704b389b5dSLuiz Capitulino                 "major": 0
714b389b5dSLuiz Capitulino             },
724b389b5dSLuiz Capitulino             "package": ""
734b389b5dSLuiz Capitulino         },
744b389b5dSLuiz Capitulino         "capabilities": [
754b389b5dSLuiz Capitulino         ]
764b389b5dSLuiz Capitulino     }
774b389b5dSLuiz Capitulino }
784b389b5dSLuiz Capitulino
794b389b5dSLuiz CapitulinoThe above output is the QMP server saying you're connected. The server is
8068e6dc59SJohn Snowactually in capabilities negotiation mode. To enter in command mode type::
814b389b5dSLuiz Capitulino
824b389b5dSLuiz Capitulino { "execute": "qmp_capabilities" }
834b389b5dSLuiz Capitulino
8468e6dc59SJohn SnowThen the server should respond::
854b389b5dSLuiz Capitulino
864b389b5dSLuiz Capitulino {
874b389b5dSLuiz Capitulino     "return": {
884b389b5dSLuiz Capitulino     }
894b389b5dSLuiz Capitulino }
904b389b5dSLuiz Capitulino
914b389b5dSLuiz CapitulinoWhich is QMP's way of saying "the latest command executed OK and didn't return
924b389b5dSLuiz Capitulinoany data". Now you're ready to enter the QMP example commands as explained in
934b389b5dSLuiz Capitulinothe following sections.
944b389b5dSLuiz Capitulino
9568e6dc59SJohn Snow
96fa2613afSDaniel P. BerrangéWriting a simple command: hello-world
97fa2613afSDaniel P. Berrangé-------------------------------------
984b389b5dSLuiz Capitulino
994b389b5dSLuiz CapitulinoThat's the most simple QMP command that can be written. Usually, this kind of
1004b389b5dSLuiz Capitulinocommand carries some meaningful action in QEMU but here it will just print
1014b389b5dSLuiz Capitulino"Hello, world" to the standard output.
1024b389b5dSLuiz Capitulino
1034b389b5dSLuiz CapitulinoOur command will be called "hello-world". It takes no arguments, nor does it
1044b389b5dSLuiz Capitulinoreturn any data.
1054b389b5dSLuiz Capitulino
106bfe873e9SMarkus ArmbrusterThe first step is defining the command in the appropriate QAPI schema
107bfe873e9SMarkus Armbrustermodule.  We pick module qapi/misc.json, and add the following line at
10868e6dc59SJohn Snowthe bottom::
1094b389b5dSLuiz Capitulino
1104b389b5dSLuiz Capitulino { 'command': 'hello-world' }
1114b389b5dSLuiz Capitulino
1124b389b5dSLuiz CapitulinoThe "command" keyword defines a new QMP command. It's an JSON object. All
1134b389b5dSLuiz Capitulinoschema entries are JSON objects. The line above will instruct the QAPI to
1144b389b5dSLuiz Capitulinogenerate any prototypes and the necessary code to marshal and unmarshal
1154b389b5dSLuiz Capitulinoprotocol data.
1164b389b5dSLuiz Capitulino
1174b389b5dSLuiz CapitulinoThe next step is to write the "hello-world" implementation. As explained
1184b389b5dSLuiz Capitulinoearlier, it's preferable for commands to live in QEMU subsystems. But
119f1b3ccfaSKevin Wolf"hello-world" doesn't pertain to any, so we put its implementation in
12068e6dc59SJohn Snowmonitor/qmp-cmds.c::
1214b389b5dSLuiz Capitulino
1224b389b5dSLuiz Capitulino void qmp_hello_world(Error **errp)
1234b389b5dSLuiz Capitulino {
1244b389b5dSLuiz Capitulino     printf("Hello, world!\n");
1254b389b5dSLuiz Capitulino }
1264b389b5dSLuiz Capitulino
1274b389b5dSLuiz CapitulinoThere are a few things to be noticed:
1284b389b5dSLuiz Capitulino
12968e6dc59SJohn Snow1. QMP command implementation functions must be prefixed with "qmp\_"
1304b389b5dSLuiz Capitulino2. qmp_hello_world() returns void, this is in accordance with the fact that the
1314b389b5dSLuiz Capitulino   command doesn't return any data
13268e6dc59SJohn Snow3. It takes an "Error \*\*" argument. This is required. Later we will see how to
1334b389b5dSLuiz Capitulino   return errors and take additional arguments. The Error argument should not
1344b389b5dSLuiz Capitulino   be touched if the command doesn't return errors
1354b389b5dSLuiz Capitulino4. We won't add the function's prototype. That's automatically done by the QAPI
1364b389b5dSLuiz Capitulino5. Printing to the terminal is discouraged for QMP commands, we do it here
1374b389b5dSLuiz Capitulino   because it's the easiest way to demonstrate a QMP command
1384b389b5dSLuiz Capitulino
1394b389b5dSLuiz CapitulinoYou're done. Now build qemu, run it as suggested in the "Testing" section,
14068e6dc59SJohn Snowand then type the following QMP command::
1414b389b5dSLuiz Capitulino
1424b389b5dSLuiz Capitulino { "execute": "hello-world" }
1434b389b5dSLuiz Capitulino
1444b389b5dSLuiz CapitulinoThen check the terminal running qemu and look for the "Hello, world" string. If
1454b389b5dSLuiz Capitulinoyou don't see it then something went wrong.
1464b389b5dSLuiz Capitulino
14768e6dc59SJohn Snow
14868e6dc59SJohn SnowArguments
14968e6dc59SJohn Snow~~~~~~~~~
1504b389b5dSLuiz Capitulino
1514b389b5dSLuiz CapitulinoLet's add an argument called "message" to our "hello-world" command. The new
1524b389b5dSLuiz Capitulinoargument will contain the string to be printed to stdout. It's an optional
1534b389b5dSLuiz Capitulinoargument, if it's not present we print our default "Hello, World" string.
1544b389b5dSLuiz Capitulino
1554b389b5dSLuiz CapitulinoThe first change we have to do is to modify the command specification in the
15668e6dc59SJohn Snowschema file to the following::
1574b389b5dSLuiz Capitulino
1584b389b5dSLuiz Capitulino { 'command': 'hello-world', 'data': { '*message': 'str' } }
1594b389b5dSLuiz Capitulino
1604b389b5dSLuiz CapitulinoNotice the new 'data' member in the schema. It's an JSON object whose each
1614b389b5dSLuiz Capitulinoelement is an argument to the command in question. Also notice the asterisk,
1624b389b5dSLuiz Capitulinoit's used to mark the argument optional (that means that you shouldn't use it
1634b389b5dSLuiz Capitulinofor mandatory arguments). Finally, 'str' is the argument's type, which
1644b389b5dSLuiz Capitulinostands for "string". The QAPI also supports integers, booleans, enumerations
1654b389b5dSLuiz Capitulinoand user defined types.
1664b389b5dSLuiz Capitulino
16768e6dc59SJohn SnowNow, let's update our C implementation in monitor/qmp-cmds.c::
1684b389b5dSLuiz Capitulino
1694b389b5dSLuiz Capitulino void qmp_hello_world(bool has_message, const char *message, Error **errp)
1704b389b5dSLuiz Capitulino {
1714b389b5dSLuiz Capitulino     if (has_message) {
1724b389b5dSLuiz Capitulino         printf("%s\n", message);
1734b389b5dSLuiz Capitulino     } else {
1744b389b5dSLuiz Capitulino         printf("Hello, world\n");
1754b389b5dSLuiz Capitulino     }
1764b389b5dSLuiz Capitulino }
1774b389b5dSLuiz Capitulino
1784b389b5dSLuiz CapitulinoThere are two important details to be noticed:
1794b389b5dSLuiz Capitulino
18068e6dc59SJohn Snow1. All optional arguments are accompanied by a 'has\_' boolean, which is set
1814b389b5dSLuiz Capitulino   if the optional argument is present or false otherwise
1824b389b5dSLuiz Capitulino2. The C implementation signature must follow the schema's argument ordering,
1834b389b5dSLuiz Capitulino   which is defined by the "data" member
1844b389b5dSLuiz Capitulino
1854b389b5dSLuiz CapitulinoTime to test our new version of the "hello-world" command. Build qemu, run it as
18668e6dc59SJohn Snowdescribed in the "Testing" section and then send two commands::
1874b389b5dSLuiz Capitulino
1884b389b5dSLuiz Capitulino { "execute": "hello-world" }
1894b389b5dSLuiz Capitulino {
1904b389b5dSLuiz Capitulino     "return": {
1914b389b5dSLuiz Capitulino     }
1924b389b5dSLuiz Capitulino }
1934b389b5dSLuiz Capitulino
1944b389b5dSLuiz Capitulino { "execute": "hello-world", "arguments": { "message": "We love qemu" } }
1954b389b5dSLuiz Capitulino {
1964b389b5dSLuiz Capitulino     "return": {
1974b389b5dSLuiz Capitulino     }
1984b389b5dSLuiz Capitulino }
1994b389b5dSLuiz Capitulino
200bb46af41SMarkus ArmbrusterYou should see "Hello, world" and "We love qemu" in the terminal running qemu,
2014b389b5dSLuiz Capitulinoif you don't see these strings, then something went wrong.
2024b389b5dSLuiz Capitulino
20368e6dc59SJohn Snow
20468e6dc59SJohn SnowErrors
20568e6dc59SJohn Snow~~~~~~
2064b389b5dSLuiz Capitulino
2074b389b5dSLuiz CapitulinoQMP commands should use the error interface exported by the error.h header
208455b0fdeSEric Blakefile. Basically, most errors are set by calling the error_setg() function.
2094b389b5dSLuiz Capitulino
2104b389b5dSLuiz CapitulinoLet's say we don't accept the string "message" to contain the word "love". If
21168e6dc59SJohn Snowit does contain it, we want the "hello-world" command to return an error::
2124b389b5dSLuiz Capitulino
2134b389b5dSLuiz Capitulino void qmp_hello_world(bool has_message, const char *message, Error **errp)
2144b389b5dSLuiz Capitulino {
2154b389b5dSLuiz Capitulino     if (has_message) {
2164b389b5dSLuiz Capitulino         if (strstr(message, "love")) {
217455b0fdeSEric Blake             error_setg(errp, "the word 'love' is not allowed");
2184b389b5dSLuiz Capitulino             return;
2194b389b5dSLuiz Capitulino         }
2204b389b5dSLuiz Capitulino         printf("%s\n", message);
2214b389b5dSLuiz Capitulino     } else {
2224b389b5dSLuiz Capitulino         printf("Hello, world\n");
2234b389b5dSLuiz Capitulino     }
2244b389b5dSLuiz Capitulino }
2254b389b5dSLuiz Capitulino
226455b0fdeSEric BlakeThe first argument to the error_setg() function is the Error pointer
227455b0fdeSEric Blaketo pointer, which is passed to all QMP functions. The next argument is a human
228adb2072eSLuiz Capitulinodescription of the error, this is a free-form printf-like string.
2294b389b5dSLuiz Capitulino
230adb2072eSLuiz CapitulinoLet's test the example above. Build qemu, run it as defined in the "Testing"
23168e6dc59SJohn Snowsection, and then issue the following command::
232adb2072eSLuiz Capitulino
233adb2072eSLuiz Capitulino { "execute": "hello-world", "arguments": { "message": "all you need is love" } }
2344b389b5dSLuiz Capitulino
23568e6dc59SJohn SnowThe QMP server's response should be::
2364b389b5dSLuiz Capitulino
2374b389b5dSLuiz Capitulino {
2384b389b5dSLuiz Capitulino     "error": {
239adb2072eSLuiz Capitulino         "class": "GenericError",
240adb2072eSLuiz Capitulino         "desc": "the word 'love' is not allowed"
2414b389b5dSLuiz Capitulino     }
2424b389b5dSLuiz Capitulino }
2434b389b5dSLuiz Capitulino
244bb46af41SMarkus ArmbrusterNote that error_setg() produces a "GenericError" class.  In general,
245bb46af41SMarkus Armbrusterall QMP errors should have that error class.  There are two exceptions
246bb46af41SMarkus Armbrusterto this rule:
2474b389b5dSLuiz Capitulino
248bb46af41SMarkus Armbruster 1. To support a management application's need to recognize a specific
249bb46af41SMarkus Armbruster    error for special handling
2504b389b5dSLuiz Capitulino
251bb46af41SMarkus Armbruster 2. Backward compatibility
252adb2072eSLuiz Capitulino
253455b0fdeSEric BlakeIf the failure you want to report falls into one of the two cases above,
254455b0fdeSEric Blakeuse error_set() with a second argument of an ErrorClass value.
255adb2072eSLuiz Capitulino
25668e6dc59SJohn Snow
25768e6dc59SJohn SnowCommand Documentation
25868e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~~~~
2594b389b5dSLuiz Capitulino
2604b389b5dSLuiz CapitulinoThere's only one step missing to make "hello-world"'s implementation complete,
2614b389b5dSLuiz Capitulinoand that's its documentation in the schema file.
2624b389b5dSLuiz Capitulino
2634b389b5dSLuiz CapitulinoThere are many examples of such documentation in the schema file already, but
26468e6dc59SJohn Snowhere goes "hello-world"'s new entry for qapi/misc.json::
2654b389b5dSLuiz Capitulino
2664b389b5dSLuiz Capitulino ##
2674eb79bdfSZihao Chang # @hello-world:
2684b389b5dSLuiz Capitulino #
2694b389b5dSLuiz Capitulino # Print a client provided string to the standard output stream.
2704b389b5dSLuiz Capitulino #
2711d8bda12SMarkus Armbruster # @message: string to be printed
2724b389b5dSLuiz Capitulino #
2734b389b5dSLuiz Capitulino # Returns: Nothing on success.
2744b389b5dSLuiz Capitulino #
2754b389b5dSLuiz Capitulino # Notes: if @message is not provided, the "Hello, world" string will
2764b389b5dSLuiz Capitulino #        be printed instead
2774b389b5dSLuiz Capitulino #
2784b389b5dSLuiz Capitulino # Since: <next qemu stable release, eg. 1.0>
2794b389b5dSLuiz Capitulino ##
2804b389b5dSLuiz Capitulino { 'command': 'hello-world', 'data': { '*message': 'str' } }
2814b389b5dSLuiz Capitulino
2824b389b5dSLuiz CapitulinoPlease, note that the "Returns" clause is optional if a command doesn't return
2834b389b5dSLuiz Capitulinoany data nor any errors.
2844b389b5dSLuiz Capitulino
28568e6dc59SJohn Snow
28668e6dc59SJohn SnowImplementing the HMP command
28768e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2884b389b5dSLuiz Capitulino
2894b389b5dSLuiz CapitulinoNow that the QMP command is in place, we can also make it available in the human
2904b389b5dSLuiz Capitulinomonitor (HMP).
2914b389b5dSLuiz Capitulino
2924b389b5dSLuiz CapitulinoWith the introduction of the QAPI, HMP commands make QMP calls. Most of the
2934b389b5dSLuiz Capitulinotime HMP commands are simple wrappers. All HMP commands implementation exist in
294f1b3ccfaSKevin Wolfthe monitor/hmp-cmds.c file.
2954b389b5dSLuiz Capitulino
29668e6dc59SJohn SnowHere's the implementation of the "hello-world" HMP command::
2974b389b5dSLuiz Capitulino
2984b389b5dSLuiz Capitulino void hmp_hello_world(Monitor *mon, const QDict *qdict)
2994b389b5dSLuiz Capitulino {
3004b389b5dSLuiz Capitulino     const char *message = qdict_get_try_str(qdict, "message");
301e940f543SMarkus Armbruster     Error *err = NULL;
3024b389b5dSLuiz Capitulino
303e940f543SMarkus Armbruster     qmp_hello_world(!!message, message, &err);
3046fa6b54fSDaniel P. Berrangé     if (hmp_handle_error(mon, err)) {
3054b389b5dSLuiz Capitulino         return;
3064b389b5dSLuiz Capitulino     }
3074b389b5dSLuiz Capitulino }
3084b389b5dSLuiz Capitulino
3094b389b5dSLuiz CapitulinoAlso, you have to add the function's prototype to the hmp.h file.
3104b389b5dSLuiz Capitulino
3114b389b5dSLuiz CapitulinoThere are three important points to be noticed:
3124b389b5dSLuiz Capitulino
3134b389b5dSLuiz Capitulino1. The "mon" and "qdict" arguments are mandatory for all HMP functions. The
3144b389b5dSLuiz Capitulino   former is the monitor object. The latter is how the monitor passes
3154b389b5dSLuiz Capitulino   arguments entered by the user to the command implementation
3166fa6b54fSDaniel P. Berrangé2. hmp_hello_world() performs error checking. In this example we just call
3176fa6b54fSDaniel P. Berrangé   hmp_handle_error() which prints a message to the user, but we could do
3186fa6b54fSDaniel P. Berrangé   more, like taking different actions depending on the error
3196fa6b54fSDaniel P. Berrangé   qmp_hello_world() returns
320e940f543SMarkus Armbruster3. The "err" variable must be initialized to NULL before performing the
3214b389b5dSLuiz Capitulino   QMP call
3224b389b5dSLuiz Capitulino
3234b389b5dSLuiz CapitulinoThere's one last step to actually make the command available to monitor users,
32468e6dc59SJohn Snowwe should add it to the hmp-commands.hx file::
3254b389b5dSLuiz Capitulino
3264b389b5dSLuiz Capitulino    {
3274b389b5dSLuiz Capitulino        .name       = "hello-world",
3284b389b5dSLuiz Capitulino        .args_type  = "message:s?",
3294b389b5dSLuiz Capitulino        .params     = "hello-world [message]",
3304b389b5dSLuiz Capitulino        .help       = "Print message to the standard output",
3312b9e3576SMarc-André Lureau        .cmd        = hmp_hello_world,
3324b389b5dSLuiz Capitulino    },
3334b389b5dSLuiz Capitulino
33468e6dc59SJohn Snow::
33568e6dc59SJohn Snow
3364b389b5dSLuiz Capitulino STEXI
3374b389b5dSLuiz Capitulino @item hello_world @var{message}
3384b389b5dSLuiz Capitulino @findex hello_world
3394b389b5dSLuiz Capitulino Print message to the standard output
3404b389b5dSLuiz Capitulino ETEXI
3414b389b5dSLuiz Capitulino
3424b389b5dSLuiz CapitulinoTo test this you have to open a user monitor and issue the "hello-world"
3434b389b5dSLuiz Capitulinocommand. It might be instructive to check the command's documentation with
3444b389b5dSLuiz CapitulinoHMP's "help" command.
3454b389b5dSLuiz Capitulino
3464b389b5dSLuiz CapitulinoPlease, check the "-monitor" command-line option to know how to open a user
3474b389b5dSLuiz Capitulinomonitor.
3484b389b5dSLuiz Capitulino
34968e6dc59SJohn Snow
350fa2613afSDaniel P. BerrangéWriting more complex commands
351fa2613afSDaniel P. Berrangé-----------------------------
3524b389b5dSLuiz Capitulino
3534b389b5dSLuiz CapitulinoA QMP command is capable of returning any data the QAPI supports like integers,
3544b389b5dSLuiz Capitulinostrings, booleans, enumerations and user defined types.
3554b389b5dSLuiz Capitulino
3564b389b5dSLuiz CapitulinoIn this section we will focus on user defined types. Please, check the QAPI
3574b389b5dSLuiz Capitulinodocumentation for information about the other types.
3584b389b5dSLuiz Capitulino
35968e6dc59SJohn Snow
360f2de406fSDaniel P. BerrangéModelling data in QAPI
361f2de406fSDaniel P. Berrangé~~~~~~~~~~~~~~~~~~~~~~
362f2de406fSDaniel P. Berrangé
363f2de406fSDaniel P. BerrangéFor a QMP command that to be considered stable and supported long term,
364f2de406fSDaniel P. Berrangéthere is a requirement returned data should be explicitly modelled
365f2de406fSDaniel P. Berrangéusing fine-grained QAPI types. As a general guide, a caller of the QMP
366f2de406fSDaniel P. Berrangécommand should never need to parse individual returned data fields. If
367f2de406fSDaniel P. Berrangéa field appears to need parsing, then it should be split into separate
368f2de406fSDaniel P. Berrangéfields corresponding to each distinct data item. This should be the
369f2de406fSDaniel P. Berrangécommon case for any new QMP command that is intended to be used by
370f2de406fSDaniel P. Berrangémachines, as opposed to exclusively human operators.
371f2de406fSDaniel P. Berrangé
372f2de406fSDaniel P. BerrangéSome QMP commands, however, are only intended as ad hoc debugging aids
373f2de406fSDaniel P. Berrangéfor human operators. While they may return large amounts of formatted
374f2de406fSDaniel P. Berrangédata, it is not expected that machines will need to parse the result.
375f2de406fSDaniel P. BerrangéThe overhead of defining a fine grained QAPI type for the data may not
376f2de406fSDaniel P. Berrangébe justified by the potential benefit. In such cases, it is permitted
377f2de406fSDaniel P. Berrangéto have a command return a simple string that contains formatted data,
378f2de406fSDaniel P. Berrangéhowever, it is mandatory for the command to use the 'x-' name prefix.
379f2de406fSDaniel P. BerrangéThis indicates that the command is not guaranteed to be long term
380f2de406fSDaniel P. Berrangéstable / liable to change in future and is not following QAPI design
381f2de406fSDaniel P. Berrangébest practices. An example where this approach is taken is the QMP
382f2de406fSDaniel P. Berrangécommand "x-query-registers". This returns a formatted dump of the
383f2de406fSDaniel P. Berrangéarchitecture specific CPU state. The way the data is formatted varies
384f2de406fSDaniel P. Berrangéacross QEMU targets, is liable to change over time, and is only
385a45cfcbbSDaniel P. Berrangéintended to be consumed as an opaque string by machines. Refer to the
386a45cfcbbSDaniel P. Berrangé`Writing a debugging aid returning unstructured text`_ section for
387a45cfcbbSDaniel P. Berrangéan illustration.
388f2de406fSDaniel P. Berrangé
38968e6dc59SJohn SnowUser Defined Types
39068e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~
3914b389b5dSLuiz Capitulino
392e218052fSMarkus ArmbrusterFIXME This example needs to be redone after commit 6d32717
393e218052fSMarkus Armbruster
3944b389b5dSLuiz CapitulinoFor this example we will write the query-alarm-clock command, which returns
3954b389b5dSLuiz Capitulinoinformation about QEMU's timer alarm. For more information about it, please
3964b389b5dSLuiz Capitulinocheck the "-clock" command-line option.
3974b389b5dSLuiz Capitulino
3984b389b5dSLuiz CapitulinoWe want to return two pieces of information. The first one is the alarm clock's
3994b389b5dSLuiz Capitulinoname. The second one is when the next alarm will fire. The former information is
4004b389b5dSLuiz Capitulinoreturned as a string, the latter is an integer in nanoseconds (which is not
4014b389b5dSLuiz Capitulinovery useful in practice, as the timer has probably already fired when the
4024b389b5dSLuiz Capitulinoinformation reaches the client).
4034b389b5dSLuiz Capitulino
40468e6dc59SJohn SnowThe best way to return that data is to create a new QAPI type, as shown below::
4054b389b5dSLuiz Capitulino
4064b389b5dSLuiz Capitulino ##
4074b389b5dSLuiz Capitulino # @QemuAlarmClock
4084b389b5dSLuiz Capitulino #
4094b389b5dSLuiz Capitulino # QEMU alarm clock information.
4104b389b5dSLuiz Capitulino #
4114b389b5dSLuiz Capitulino # @clock-name: The alarm clock method's name.
4124b389b5dSLuiz Capitulino #
4131d8bda12SMarkus Armbruster # @next-deadline: The time (in nanoseconds) the next alarm will fire.
4144b389b5dSLuiz Capitulino #
4154b389b5dSLuiz Capitulino # Since: 1.0
4164b389b5dSLuiz Capitulino ##
4174b389b5dSLuiz Capitulino { 'type': 'QemuAlarmClock',
4184b389b5dSLuiz Capitulino   'data': { 'clock-name': 'str', '*next-deadline': 'int' } }
4194b389b5dSLuiz Capitulino
4204b389b5dSLuiz CapitulinoThe "type" keyword defines a new QAPI type. Its "data" member contains the
4214b389b5dSLuiz Capitulinotype's members. In this example our members are the "clock-name" and the
4224b389b5dSLuiz Capitulino"next-deadline" one, which is optional.
4234b389b5dSLuiz Capitulino
42468e6dc59SJohn SnowNow let's define the query-alarm-clock command::
4254b389b5dSLuiz Capitulino
4264b389b5dSLuiz Capitulino ##
4274b389b5dSLuiz Capitulino # @query-alarm-clock
4284b389b5dSLuiz Capitulino #
4294b389b5dSLuiz Capitulino # Return information about QEMU's alarm clock.
4304b389b5dSLuiz Capitulino #
4314b389b5dSLuiz Capitulino # Returns a @QemuAlarmClock instance describing the alarm clock method
4324b389b5dSLuiz Capitulino # being currently used by QEMU (this is usually set by the '-clock'
4334b389b5dSLuiz Capitulino # command-line option).
4344b389b5dSLuiz Capitulino #
4354b389b5dSLuiz Capitulino # Since: 1.0
4364b389b5dSLuiz Capitulino ##
4374b389b5dSLuiz Capitulino { 'command': 'query-alarm-clock', 'returns': 'QemuAlarmClock' }
4384b389b5dSLuiz Capitulino
4394b389b5dSLuiz CapitulinoNotice the "returns" keyword. As its name suggests, it's used to define the
4404b389b5dSLuiz Capitulinodata returned by a command.
4414b389b5dSLuiz Capitulino
4424b389b5dSLuiz CapitulinoIt's time to implement the qmp_query_alarm_clock() function, you can put it
44368e6dc59SJohn Snowin the qemu-timer.c file::
4444b389b5dSLuiz Capitulino
4454b389b5dSLuiz Capitulino QemuAlarmClock *qmp_query_alarm_clock(Error **errp)
4464b389b5dSLuiz Capitulino {
4474b389b5dSLuiz Capitulino     QemuAlarmClock *clock;
4484b389b5dSLuiz Capitulino     int64_t deadline;
4494b389b5dSLuiz Capitulino
4504b389b5dSLuiz Capitulino     clock = g_malloc0(sizeof(*clock));
4514b389b5dSLuiz Capitulino
4524b389b5dSLuiz Capitulino     deadline = qemu_next_alarm_deadline();
4534b389b5dSLuiz Capitulino     if (deadline > 0) {
4544b389b5dSLuiz Capitulino         clock->has_next_deadline = true;
4554b389b5dSLuiz Capitulino         clock->next_deadline = deadline;
4564b389b5dSLuiz Capitulino     }
4574b389b5dSLuiz Capitulino     clock->clock_name = g_strdup(alarm_timer->name);
4584b389b5dSLuiz Capitulino
4594b389b5dSLuiz Capitulino     return clock;
4604b389b5dSLuiz Capitulino }
4614b389b5dSLuiz Capitulino
4624b389b5dSLuiz CapitulinoThere are a number of things to be noticed:
4634b389b5dSLuiz Capitulino
4644b389b5dSLuiz Capitulino1. The QemuAlarmClock type is automatically generated by the QAPI framework,
4654b389b5dSLuiz Capitulino   its members correspond to the type's specification in the schema file
4664b389b5dSLuiz Capitulino2. As specified in the schema file, the function returns a QemuAlarmClock
4674b389b5dSLuiz Capitulino   instance and takes no arguments (besides the "errp" one, which is mandatory
4684b389b5dSLuiz Capitulino   for all QMP functions)
4694b389b5dSLuiz Capitulino3. The "clock" variable (which will point to our QAPI type instance) is
4704b389b5dSLuiz Capitulino   allocated by the regular g_malloc0() function. Note that we chose to
471dabdf394SStefan Weil   initialize the memory to zero. This is recommended for all QAPI types, as
4724b389b5dSLuiz Capitulino   it helps avoiding bad surprises (specially with booleans)
4734b389b5dSLuiz Capitulino4. Remember that "next_deadline" is optional? All optional members have a
4744b389b5dSLuiz Capitulino   'has_TYPE_NAME' member that should be properly set by the implementation,
4754b389b5dSLuiz Capitulino   as shown above
4764b389b5dSLuiz Capitulino5. Even static strings, such as "alarm_timer->name", should be dynamically
4774b389b5dSLuiz Capitulino   allocated by the implementation. This is so because the QAPI also generates
4784b389b5dSLuiz Capitulino   a function to free its types and it cannot distinguish between dynamically
4794b389b5dSLuiz Capitulino   or statically allocated strings
480eb815e24SMarkus Armbruster6. You have to include "qapi/qapi-commands-misc.h" in qemu-timer.c
4814b389b5dSLuiz Capitulino
4824b389b5dSLuiz CapitulinoTime to test the new command. Build qemu, run it as described in the "Testing"
48368e6dc59SJohn Snowsection and try this::
4844b389b5dSLuiz Capitulino
4854b389b5dSLuiz Capitulino { "execute": "query-alarm-clock" }
4864b389b5dSLuiz Capitulino {
4874b389b5dSLuiz Capitulino     "return": {
4884b389b5dSLuiz Capitulino         "next-deadline": 2368219,
4894b389b5dSLuiz Capitulino         "clock-name": "dynticks"
4904b389b5dSLuiz Capitulino     }
4914b389b5dSLuiz Capitulino }
4924b389b5dSLuiz Capitulino
4934b389b5dSLuiz Capitulino
49468e6dc59SJohn SnowThe HMP command
49568e6dc59SJohn Snow~~~~~~~~~~~~~~~
49668e6dc59SJohn Snow
49768e6dc59SJohn SnowHere's the HMP counterpart of the query-alarm-clock command::
4984b389b5dSLuiz Capitulino
4994b389b5dSLuiz Capitulino void hmp_info_alarm_clock(Monitor *mon)
5004b389b5dSLuiz Capitulino {
5014b389b5dSLuiz Capitulino     QemuAlarmClock *clock;
502e940f543SMarkus Armbruster     Error *err = NULL;
5034b389b5dSLuiz Capitulino
504e940f543SMarkus Armbruster     clock = qmp_query_alarm_clock(&err);
5056fa6b54fSDaniel P. Berrangé     if (hmp_handle_error(mon, err)) {
5064b389b5dSLuiz Capitulino         return;
5074b389b5dSLuiz Capitulino     }
5084b389b5dSLuiz Capitulino
5094b389b5dSLuiz Capitulino     monitor_printf(mon, "Alarm clock method in use: '%s'\n", clock->clock_name);
5104b389b5dSLuiz Capitulino     if (clock->has_next_deadline) {
5114b389b5dSLuiz Capitulino         monitor_printf(mon, "Next alarm will fire in %" PRId64 " nanoseconds\n",
5124b389b5dSLuiz Capitulino                        clock->next_deadline);
5134b389b5dSLuiz Capitulino     }
5144b389b5dSLuiz Capitulino
5154b389b5dSLuiz Capitulino    qapi_free_QemuAlarmClock(clock);
5164b389b5dSLuiz Capitulino }
5174b389b5dSLuiz Capitulino
5184b389b5dSLuiz CapitulinoIt's important to notice that hmp_info_alarm_clock() calls
5194b389b5dSLuiz Capitulinoqapi_free_QemuAlarmClock() to free the data returned by qmp_query_alarm_clock().
5204b389b5dSLuiz CapitulinoFor user defined types, the QAPI will generate a qapi_free_QAPI_TYPE_NAME()
5214b389b5dSLuiz Capitulinofunction and that's what you have to use to free the types you define and
5224b389b5dSLuiz Capitulinoqapi_free_QAPI_TYPE_NAMEList() for list types (explained in the next section).
5234b389b5dSLuiz CapitulinoIf the QMP call returns a string, then you should g_free() to free it.
5244b389b5dSLuiz Capitulino
5254b389b5dSLuiz CapitulinoAlso note that hmp_info_alarm_clock() performs error handling. That's not
5264b389b5dSLuiz Capitulinostrictly required if you're sure the QMP function doesn't return errors, but
5274b389b5dSLuiz Capitulinoit's good practice to always check for errors.
5284b389b5dSLuiz Capitulino
5294b389b5dSLuiz CapitulinoAnother important detail is that HMP's "info" commands don't go into the
5304b389b5dSLuiz Capitulinohmp-commands.hx. Instead, they go into the info_cmds[] table, which is defined
53168e6dc59SJohn Snowin the monitor/misc.c file. The entry for the "info alarmclock" follows::
5324b389b5dSLuiz Capitulino
5334b389b5dSLuiz Capitulino    {
5344b389b5dSLuiz Capitulino        .name       = "alarmclock",
5354b389b5dSLuiz Capitulino        .args_type  = "",
5364b389b5dSLuiz Capitulino        .params     = "",
5374b389b5dSLuiz Capitulino        .help       = "show information about the alarm clock",
5382b9e3576SMarc-André Lureau        .cmd        = hmp_info_alarm_clock,
5394b389b5dSLuiz Capitulino    },
5404b389b5dSLuiz Capitulino
5414b389b5dSLuiz CapitulinoTo test this, run qemu and type "info alarmclock" in the user monitor.
5424b389b5dSLuiz Capitulino
54368e6dc59SJohn Snow
54468e6dc59SJohn SnowReturning Lists
54568e6dc59SJohn Snow~~~~~~~~~~~~~~~
5464b389b5dSLuiz Capitulino
5474b389b5dSLuiz CapitulinoFor this example, we're going to return all available methods for the timer
5484b389b5dSLuiz Capitulinoalarm, which is pretty much what the command-line option "-clock ?" does,
5494b389b5dSLuiz Capitulinoexcept that we're also going to inform which method is in use.
5504b389b5dSLuiz Capitulino
55168e6dc59SJohn SnowThis first step is to define a new type::
5524b389b5dSLuiz Capitulino
5534b389b5dSLuiz Capitulino ##
5544b389b5dSLuiz Capitulino # @TimerAlarmMethod
5554b389b5dSLuiz Capitulino #
5564b389b5dSLuiz Capitulino # Timer alarm method information.
5574b389b5dSLuiz Capitulino #
5584b389b5dSLuiz Capitulino # @method-name: The method's name.
5594b389b5dSLuiz Capitulino #
5604b389b5dSLuiz Capitulino # @current: true if this alarm method is currently in use, false otherwise
5614b389b5dSLuiz Capitulino #
5624b389b5dSLuiz Capitulino # Since: 1.0
5634b389b5dSLuiz Capitulino ##
5644b389b5dSLuiz Capitulino { 'type': 'TimerAlarmMethod',
5654b389b5dSLuiz Capitulino   'data': { 'method-name': 'str', 'current': 'bool' } }
5664b389b5dSLuiz Capitulino
5674b389b5dSLuiz CapitulinoThe command will be called "query-alarm-methods", here is its schema
56868e6dc59SJohn Snowspecification::
5694b389b5dSLuiz Capitulino
5704b389b5dSLuiz Capitulino ##
5714b389b5dSLuiz Capitulino # @query-alarm-methods
5724b389b5dSLuiz Capitulino #
5734b389b5dSLuiz Capitulino # Returns information about available alarm methods.
5744b389b5dSLuiz Capitulino #
5754b389b5dSLuiz Capitulino # Returns: a list of @TimerAlarmMethod for each method
5764b389b5dSLuiz Capitulino #
5774b389b5dSLuiz Capitulino # Since: 1.0
5784b389b5dSLuiz Capitulino ##
5794b389b5dSLuiz Capitulino { 'command': 'query-alarm-methods', 'returns': ['TimerAlarmMethod'] }
5804b389b5dSLuiz Capitulino
5814b389b5dSLuiz CapitulinoNotice the syntax for returning lists "'returns': ['TimerAlarmMethod']", this
5824b389b5dSLuiz Capitulinoshould be read as "returns a list of TimerAlarmMethod instances".
5834b389b5dSLuiz Capitulino
58468e6dc59SJohn SnowThe C implementation follows::
5854b389b5dSLuiz Capitulino
5864b389b5dSLuiz Capitulino TimerAlarmMethodList *qmp_query_alarm_methods(Error **errp)
5874b389b5dSLuiz Capitulino {
5884b389b5dSLuiz Capitulino     TimerAlarmMethodList *method_list = NULL;
5894b389b5dSLuiz Capitulino     const struct qemu_alarm_timer *p;
5904b389b5dSLuiz Capitulino     bool current = true;
5914b389b5dSLuiz Capitulino
5924b389b5dSLuiz Capitulino     for (p = alarm_timers; p->name; p++) {
59354aa3de7SEric Blake         TimerAlarmMethod *value = g_malloc0(*value);
59454aa3de7SEric Blake         value->method_name = g_strdup(p->name);
59554aa3de7SEric Blake         value->current = current;
59654aa3de7SEric Blake         QAPI_LIST_PREPEND(method_list, value);
5974b389b5dSLuiz Capitulino         current = false;
5984b389b5dSLuiz Capitulino     }
5994b389b5dSLuiz Capitulino
6004b389b5dSLuiz Capitulino     return method_list;
6014b389b5dSLuiz Capitulino }
6024b389b5dSLuiz Capitulino
6034b389b5dSLuiz CapitulinoThe most important difference from the previous examples is the
6044b389b5dSLuiz CapitulinoTimerAlarmMethodList type, which is automatically generated by the QAPI from
6054b389b5dSLuiz Capitulinothe TimerAlarmMethod type.
6064b389b5dSLuiz Capitulino
6074b389b5dSLuiz CapitulinoEach list node is represented by a TimerAlarmMethodList instance. We have to
6084b389b5dSLuiz Capitulinoallocate it, and that's done inside the for loop: the "info" pointer points to
6094b389b5dSLuiz Capitulinoan allocated node. We also have to allocate the node's contents, which is
6104b389b5dSLuiz Capitulinostored in its "value" member. In our example, the "value" member is a pointer
6114b389b5dSLuiz Capitulinoto an TimerAlarmMethod instance.
6124b389b5dSLuiz Capitulino
6134b389b5dSLuiz CapitulinoNotice that the "current" variable is used as "true" only in the first
6145708b2b7SChen Hanxiaoiteration of the loop. That's because the alarm timer method in use is the
6154b389b5dSLuiz Capitulinofirst element of the alarm_timers array. Also notice that QAPI lists are handled
6164b389b5dSLuiz Capitulinoby hand and we return the head of the list.
6174b389b5dSLuiz Capitulino
6184b389b5dSLuiz CapitulinoNow Build qemu, run it as explained in the "Testing" section and try our new
61968e6dc59SJohn Snowcommand::
6204b389b5dSLuiz Capitulino
6214b389b5dSLuiz Capitulino { "execute": "query-alarm-methods" }
6224b389b5dSLuiz Capitulino {
6234b389b5dSLuiz Capitulino     "return": [
6244b389b5dSLuiz Capitulino         {
6254b389b5dSLuiz Capitulino             "current": false,
6264b389b5dSLuiz Capitulino             "method-name": "unix"
6274b389b5dSLuiz Capitulino         },
6284b389b5dSLuiz Capitulino         {
6294b389b5dSLuiz Capitulino             "current": true,
6304b389b5dSLuiz Capitulino             "method-name": "dynticks"
6314b389b5dSLuiz Capitulino         }
6324b389b5dSLuiz Capitulino     ]
6334b389b5dSLuiz Capitulino }
6344b389b5dSLuiz Capitulino
6354b389b5dSLuiz CapitulinoThe HMP counterpart is a bit more complex than previous examples because it
63668e6dc59SJohn Snowhas to traverse the list, it's shown below for reference::
6374b389b5dSLuiz Capitulino
6384b389b5dSLuiz Capitulino void hmp_info_alarm_methods(Monitor *mon)
6394b389b5dSLuiz Capitulino {
6404b389b5dSLuiz Capitulino     TimerAlarmMethodList *method_list, *method;
641e940f543SMarkus Armbruster     Error *err = NULL;
6424b389b5dSLuiz Capitulino
643e940f543SMarkus Armbruster     method_list = qmp_query_alarm_methods(&err);
6446fa6b54fSDaniel P. Berrangé     if (hmp_handle_error(mon, err)) {
6454b389b5dSLuiz Capitulino         return;
6464b389b5dSLuiz Capitulino     }
6474b389b5dSLuiz Capitulino
6484b389b5dSLuiz Capitulino     for (method = method_list; method; method = method->next) {
6494b389b5dSLuiz Capitulino         monitor_printf(mon, "%c %s\n", method->value->current ? '*' : ' ',
6504b389b5dSLuiz Capitulino                                        method->value->method_name);
6514b389b5dSLuiz Capitulino     }
6524b389b5dSLuiz Capitulino
6534b389b5dSLuiz Capitulino     qapi_free_TimerAlarmMethodList(method_list);
6544b389b5dSLuiz Capitulino }
655a45cfcbbSDaniel P. Berrangé
656a45cfcbbSDaniel P. BerrangéWriting a debugging aid returning unstructured text
657a45cfcbbSDaniel P. Berrangé---------------------------------------------------
658a45cfcbbSDaniel P. Berrangé
659a45cfcbbSDaniel P. BerrangéAs discussed in section `Modelling data in QAPI`_, it is required that
660a45cfcbbSDaniel P. Berrangécommands expecting machine usage be using fine-grained QAPI data types.
661a45cfcbbSDaniel P. BerrangéThe exception to this rule applies when the command is solely intended
662a45cfcbbSDaniel P. Berrangéas a debugging aid and allows for returning unstructured text. This is
663a45cfcbbSDaniel P. Berrangécommonly needed for query commands that report aspects of QEMU's
664a45cfcbbSDaniel P. Berrangéinternal state that are useful to human operators.
665a45cfcbbSDaniel P. Berrangé
666a45cfcbbSDaniel P. BerrangéIn this example we will consider a simplified variant of the HMP
667a45cfcbbSDaniel P. Berrangécommand ``info roms``. Following the earlier rules, this command will
668a45cfcbbSDaniel P. Berrangéneed to live under the ``x-`` name prefix, so its QMP implementation
669a45cfcbbSDaniel P. Berrangéwill be called ``x-query-roms``. It will have no parameters and will
670a45cfcbbSDaniel P. Berrangéreturn a single text string::
671a45cfcbbSDaniel P. Berrangé
672a45cfcbbSDaniel P. Berrangé { 'struct': 'HumanReadableText',
673a45cfcbbSDaniel P. Berrangé   'data': { 'human-readable-text': 'str' } }
674a45cfcbbSDaniel P. Berrangé
675a45cfcbbSDaniel P. Berrangé { 'command': 'x-query-roms',
676a45cfcbbSDaniel P. Berrangé   'returns': 'HumanReadableText' }
677a45cfcbbSDaniel P. Berrangé
678a45cfcbbSDaniel P. BerrangéThe ``HumanReadableText`` struct is intended to be used for all
679a45cfcbbSDaniel P. Berrangécommands, under the ``x-`` name prefix that are returning unstructured
680a45cfcbbSDaniel P. Berrangétext targetted at humans. It should never be used for commands outside
681a45cfcbbSDaniel P. Berrangéthe ``x-`` name prefix, as those should be using structured QAPI types.
682a45cfcbbSDaniel P. Berrangé
683a45cfcbbSDaniel P. BerrangéImplementing the QMP command
684a45cfcbbSDaniel P. Berrangé~~~~~~~~~~~~~~~~~~~~~~~~~~~~
685a45cfcbbSDaniel P. Berrangé
686a45cfcbbSDaniel P. BerrangéThe QMP implementation will typically involve creating a ``GString``
687a45cfcbbSDaniel P. Berrangéobject and printing formatted data into it::
688a45cfcbbSDaniel P. Berrangé
689a45cfcbbSDaniel P. Berrangé HumanReadableText *qmp_x_query_roms(Error **errp)
690a45cfcbbSDaniel P. Berrangé {
691a45cfcbbSDaniel P. Berrangé     g_autoptr(GString) buf = g_string_new("");
692a45cfcbbSDaniel P. Berrangé     Rom *rom;
693a45cfcbbSDaniel P. Berrangé
694a45cfcbbSDaniel P. Berrangé     QTAILQ_FOREACH(rom, &roms, next) {
695a45cfcbbSDaniel P. Berrangé        g_string_append_printf("%s size=0x%06zx name=\"%s\"\n",
696a45cfcbbSDaniel P. Berrangé                               memory_region_name(rom->mr),
697a45cfcbbSDaniel P. Berrangé                               rom->romsize,
698a45cfcbbSDaniel P. Berrangé                               rom->name);
699a45cfcbbSDaniel P. Berrangé     }
700a45cfcbbSDaniel P. Berrangé
701a45cfcbbSDaniel P. Berrangé     return human_readable_text_from_str(buf);
702a45cfcbbSDaniel P. Berrangé }
703a45cfcbbSDaniel P. Berrangé
704a45cfcbbSDaniel P. Berrangé
705a45cfcbbSDaniel P. BerrangéImplementing the HMP command
706a45cfcbbSDaniel P. Berrangé~~~~~~~~~~~~~~~~~~~~~~~~~~~~
707a45cfcbbSDaniel P. Berrangé
708a45cfcbbSDaniel P. BerrangéNow that the QMP command is in place, we can also make it available in
709a45cfcbbSDaniel P. Berrangéthe human monitor (HMP) as shown in previous examples. The HMP
710a45cfcbbSDaniel P. Berrangéimplementations will all look fairly similar, as all they need do is
711a45cfcbbSDaniel P. Berrangéinvoke the QMP command and then print the resulting text or error
712a45cfcbbSDaniel P. Berrangémessage. Here's the implementation of the "info roms" HMP command::
713a45cfcbbSDaniel P. Berrangé
714a45cfcbbSDaniel P. Berrangé void hmp_info_roms(Monitor *mon, const QDict *qdict)
715a45cfcbbSDaniel P. Berrangé {
716a45cfcbbSDaniel P. Berrangé     Error err = NULL;
717a45cfcbbSDaniel P. Berrangé     g_autoptr(HumanReadableText) info = qmp_x_query_roms(&err);
718a45cfcbbSDaniel P. Berrangé
719a45cfcbbSDaniel P. Berrangé     if (hmp_handle_error(mon, err)) {
720a45cfcbbSDaniel P. Berrangé         return;
721a45cfcbbSDaniel P. Berrangé     }
722a45cfcbbSDaniel P. Berrangé     monitor_printf(mon, "%s", info->human_readable_text);
723a45cfcbbSDaniel P. Berrangé }
724a45cfcbbSDaniel P. Berrangé
725a45cfcbbSDaniel P. BerrangéAlso, you have to add the function's prototype to the hmp.h file.
726a45cfcbbSDaniel P. Berrangé
727a45cfcbbSDaniel P. BerrangéThere's one last step to actually make the command available to
728a45cfcbbSDaniel P. Berrangémonitor users, we should add it to the hmp-commands-info.hx file::
729a45cfcbbSDaniel P. Berrangé
730a45cfcbbSDaniel P. Berrangé    {
731a45cfcbbSDaniel P. Berrangé        .name       = "roms",
732a45cfcbbSDaniel P. Berrangé        .args_type  = "",
733a45cfcbbSDaniel P. Berrangé        .params     = "",
734a45cfcbbSDaniel P. Berrangé        .help       = "show roms",
735a45cfcbbSDaniel P. Berrangé        .cmd        = hmp_info_roms,
736a45cfcbbSDaniel P. Berrangé    },
737a45cfcbbSDaniel P. Berrangé
738a45cfcbbSDaniel P. BerrangéThe case of writing a HMP info handler that calls a no-parameter QMP query
739a45cfcbbSDaniel P. Berrangécommand is quite common. To simplify the implementation there is a general
740a45cfcbbSDaniel P. Berrangépurpose HMP info handler for this scenario. All that is required to expose
741a45cfcbbSDaniel P. Berrangéa no-parameter QMP query command via HMP is to declare it using the
742a45cfcbbSDaniel P. Berrangé'.cmd_info_hrt' field to point to the QMP handler, and leave the '.cmd'
743a45cfcbbSDaniel P. Berrangéfield NULL::
744a45cfcbbSDaniel P. Berrangé
745a45cfcbbSDaniel P. Berrangé    {
746a45cfcbbSDaniel P. Berrangé        .name         = "roms",
747a45cfcbbSDaniel P. Berrangé        .args_type    = "",
748a45cfcbbSDaniel P. Berrangé        .params       = "",
749a45cfcbbSDaniel P. Berrangé        .help         = "show roms",
750a45cfcbbSDaniel P. Berrangé        .cmd_info_hrt = qmp_x_query_roms,
751a45cfcbbSDaniel P. Berrangé    },
752