xref: /qemu/docs/devel/writing-monitor-commands.rst (revision 6fa6b54f5b931e10e24f773d991a48da4f79e61a)
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
1468e6dc59SJohn Snow
1568e6dc59SJohn SnowOverview
1668e6dc59SJohn Snow--------
174b389b5dSLuiz Capitulino
184b389b5dSLuiz CapitulinoGenerally speaking, the following steps should be taken in order to write a
194b389b5dSLuiz Capitulinonew QMP command.
204b389b5dSLuiz Capitulino
21bfe873e9SMarkus Armbruster1. Define the command and any types it needs in the appropriate QAPI
22bfe873e9SMarkus Armbruster   schema module.
234b389b5dSLuiz Capitulino
244b389b5dSLuiz Capitulino2. Write the QMP command itself, which is a regular C function. Preferably,
254b389b5dSLuiz Capitulino   the command should be exported by some QEMU subsystem. But it can also be
26f1b3ccfaSKevin Wolf   added to the monitor/qmp-cmds.c file
274b389b5dSLuiz Capitulino
284b389b5dSLuiz Capitulino3. At this point the command can be tested under the QMP protocol
294b389b5dSLuiz Capitulino
304b389b5dSLuiz Capitulino4. Write the HMP command equivalent. This is not required and should only be
314b389b5dSLuiz Capitulino   done if it does make sense to have the functionality in HMP. The HMP command
324b389b5dSLuiz Capitulino   is implemented in terms of the QMP command
334b389b5dSLuiz Capitulino
344b389b5dSLuiz CapitulinoThe following sections will demonstrate each of the steps above. We will start
354b389b5dSLuiz Capitulinovery simple and get more complex as we progress.
364b389b5dSLuiz Capitulino
3768e6dc59SJohn Snow
3868e6dc59SJohn SnowTesting
3968e6dc59SJohn Snow-------
404b389b5dSLuiz Capitulino
414b389b5dSLuiz CapitulinoFor all the examples in the next sections, the test setup is the same and is
424b389b5dSLuiz Capitulinoshown here.
434b389b5dSLuiz Capitulino
4468e6dc59SJohn SnowFirst, QEMU should be started like this::
454b389b5dSLuiz Capitulino
46bb46af41SMarkus Armbruster # qemu-system-TARGET [...] \
47c2387413SDaniel P. Berrangé     -chardev socket,id=qmp,port=4444,host=localhost,server=on \
484b389b5dSLuiz Capitulino     -mon chardev=qmp,mode=control,pretty=on
494b389b5dSLuiz Capitulino
5068e6dc59SJohn SnowThen, in a different terminal::
514b389b5dSLuiz Capitulino
524b389b5dSLuiz Capitulino $ telnet localhost 4444
534b389b5dSLuiz Capitulino Trying 127.0.0.1...
544b389b5dSLuiz Capitulino Connected to localhost.
554b389b5dSLuiz Capitulino Escape character is '^]'.
564b389b5dSLuiz Capitulino {
574b389b5dSLuiz Capitulino     "QMP": {
584b389b5dSLuiz Capitulino         "version": {
594b389b5dSLuiz Capitulino             "qemu": {
604b389b5dSLuiz Capitulino                 "micro": 50,
614b389b5dSLuiz Capitulino                 "minor": 15,
624b389b5dSLuiz Capitulino                 "major": 0
634b389b5dSLuiz Capitulino             },
644b389b5dSLuiz Capitulino             "package": ""
654b389b5dSLuiz Capitulino         },
664b389b5dSLuiz Capitulino         "capabilities": [
674b389b5dSLuiz Capitulino         ]
684b389b5dSLuiz Capitulino     }
694b389b5dSLuiz Capitulino }
704b389b5dSLuiz Capitulino
714b389b5dSLuiz CapitulinoThe above output is the QMP server saying you're connected. The server is
7268e6dc59SJohn Snowactually in capabilities negotiation mode. To enter in command mode type::
734b389b5dSLuiz Capitulino
744b389b5dSLuiz Capitulino { "execute": "qmp_capabilities" }
754b389b5dSLuiz Capitulino
7668e6dc59SJohn SnowThen the server should respond::
774b389b5dSLuiz Capitulino
784b389b5dSLuiz Capitulino {
794b389b5dSLuiz Capitulino     "return": {
804b389b5dSLuiz Capitulino     }
814b389b5dSLuiz Capitulino }
824b389b5dSLuiz Capitulino
834b389b5dSLuiz CapitulinoWhich is QMP's way of saying "the latest command executed OK and didn't return
844b389b5dSLuiz Capitulinoany data". Now you're ready to enter the QMP example commands as explained in
854b389b5dSLuiz Capitulinothe following sections.
864b389b5dSLuiz Capitulino
8768e6dc59SJohn Snow
88fa2613afSDaniel P. BerrangéWriting a simple command: hello-world
89fa2613afSDaniel P. Berrangé-------------------------------------
904b389b5dSLuiz Capitulino
914b389b5dSLuiz CapitulinoThat's the most simple QMP command that can be written. Usually, this kind of
924b389b5dSLuiz Capitulinocommand carries some meaningful action in QEMU but here it will just print
934b389b5dSLuiz Capitulino"Hello, world" to the standard output.
944b389b5dSLuiz Capitulino
954b389b5dSLuiz CapitulinoOur command will be called "hello-world". It takes no arguments, nor does it
964b389b5dSLuiz Capitulinoreturn any data.
974b389b5dSLuiz Capitulino
98bfe873e9SMarkus ArmbrusterThe first step is defining the command in the appropriate QAPI schema
99bfe873e9SMarkus Armbrustermodule.  We pick module qapi/misc.json, and add the following line at
10068e6dc59SJohn Snowthe bottom::
1014b389b5dSLuiz Capitulino
1024b389b5dSLuiz Capitulino { 'command': 'hello-world' }
1034b389b5dSLuiz Capitulino
1044b389b5dSLuiz CapitulinoThe "command" keyword defines a new QMP command. It's an JSON object. All
1054b389b5dSLuiz Capitulinoschema entries are JSON objects. The line above will instruct the QAPI to
1064b389b5dSLuiz Capitulinogenerate any prototypes and the necessary code to marshal and unmarshal
1074b389b5dSLuiz Capitulinoprotocol data.
1084b389b5dSLuiz Capitulino
1094b389b5dSLuiz CapitulinoThe next step is to write the "hello-world" implementation. As explained
1104b389b5dSLuiz Capitulinoearlier, it's preferable for commands to live in QEMU subsystems. But
111f1b3ccfaSKevin Wolf"hello-world" doesn't pertain to any, so we put its implementation in
11268e6dc59SJohn Snowmonitor/qmp-cmds.c::
1134b389b5dSLuiz Capitulino
1144b389b5dSLuiz Capitulino void qmp_hello_world(Error **errp)
1154b389b5dSLuiz Capitulino {
1164b389b5dSLuiz Capitulino     printf("Hello, world!\n");
1174b389b5dSLuiz Capitulino }
1184b389b5dSLuiz Capitulino
1194b389b5dSLuiz CapitulinoThere are a few things to be noticed:
1204b389b5dSLuiz Capitulino
12168e6dc59SJohn Snow1. QMP command implementation functions must be prefixed with "qmp\_"
1224b389b5dSLuiz Capitulino2. qmp_hello_world() returns void, this is in accordance with the fact that the
1234b389b5dSLuiz Capitulino   command doesn't return any data
12468e6dc59SJohn Snow3. It takes an "Error \*\*" argument. This is required. Later we will see how to
1254b389b5dSLuiz Capitulino   return errors and take additional arguments. The Error argument should not
1264b389b5dSLuiz Capitulino   be touched if the command doesn't return errors
1274b389b5dSLuiz Capitulino4. We won't add the function's prototype. That's automatically done by the QAPI
1284b389b5dSLuiz Capitulino5. Printing to the terminal is discouraged for QMP commands, we do it here
1294b389b5dSLuiz Capitulino   because it's the easiest way to demonstrate a QMP command
1304b389b5dSLuiz Capitulino
1314b389b5dSLuiz CapitulinoYou're done. Now build qemu, run it as suggested in the "Testing" section,
13268e6dc59SJohn Snowand then type the following QMP command::
1334b389b5dSLuiz Capitulino
1344b389b5dSLuiz Capitulino { "execute": "hello-world" }
1354b389b5dSLuiz Capitulino
1364b389b5dSLuiz CapitulinoThen check the terminal running qemu and look for the "Hello, world" string. If
1374b389b5dSLuiz Capitulinoyou don't see it then something went wrong.
1384b389b5dSLuiz Capitulino
13968e6dc59SJohn Snow
14068e6dc59SJohn SnowArguments
14168e6dc59SJohn Snow~~~~~~~~~
1424b389b5dSLuiz Capitulino
1434b389b5dSLuiz CapitulinoLet's add an argument called "message" to our "hello-world" command. The new
1444b389b5dSLuiz Capitulinoargument will contain the string to be printed to stdout. It's an optional
1454b389b5dSLuiz Capitulinoargument, if it's not present we print our default "Hello, World" string.
1464b389b5dSLuiz Capitulino
1474b389b5dSLuiz CapitulinoThe first change we have to do is to modify the command specification in the
14868e6dc59SJohn Snowschema file to the following::
1494b389b5dSLuiz Capitulino
1504b389b5dSLuiz Capitulino { 'command': 'hello-world', 'data': { '*message': 'str' } }
1514b389b5dSLuiz Capitulino
1524b389b5dSLuiz CapitulinoNotice the new 'data' member in the schema. It's an JSON object whose each
1534b389b5dSLuiz Capitulinoelement is an argument to the command in question. Also notice the asterisk,
1544b389b5dSLuiz Capitulinoit's used to mark the argument optional (that means that you shouldn't use it
1554b389b5dSLuiz Capitulinofor mandatory arguments). Finally, 'str' is the argument's type, which
1564b389b5dSLuiz Capitulinostands for "string". The QAPI also supports integers, booleans, enumerations
1574b389b5dSLuiz Capitulinoand user defined types.
1584b389b5dSLuiz Capitulino
15968e6dc59SJohn SnowNow, let's update our C implementation in monitor/qmp-cmds.c::
1604b389b5dSLuiz Capitulino
1614b389b5dSLuiz Capitulino void qmp_hello_world(bool has_message, const char *message, Error **errp)
1624b389b5dSLuiz Capitulino {
1634b389b5dSLuiz Capitulino     if (has_message) {
1644b389b5dSLuiz Capitulino         printf("%s\n", message);
1654b389b5dSLuiz Capitulino     } else {
1664b389b5dSLuiz Capitulino         printf("Hello, world\n");
1674b389b5dSLuiz Capitulino     }
1684b389b5dSLuiz Capitulino }
1694b389b5dSLuiz Capitulino
1704b389b5dSLuiz CapitulinoThere are two important details to be noticed:
1714b389b5dSLuiz Capitulino
17268e6dc59SJohn Snow1. All optional arguments are accompanied by a 'has\_' boolean, which is set
1734b389b5dSLuiz Capitulino   if the optional argument is present or false otherwise
1744b389b5dSLuiz Capitulino2. The C implementation signature must follow the schema's argument ordering,
1754b389b5dSLuiz Capitulino   which is defined by the "data" member
1764b389b5dSLuiz Capitulino
1774b389b5dSLuiz CapitulinoTime to test our new version of the "hello-world" command. Build qemu, run it as
17868e6dc59SJohn Snowdescribed in the "Testing" section and then send two commands::
1794b389b5dSLuiz Capitulino
1804b389b5dSLuiz Capitulino { "execute": "hello-world" }
1814b389b5dSLuiz Capitulino {
1824b389b5dSLuiz Capitulino     "return": {
1834b389b5dSLuiz Capitulino     }
1844b389b5dSLuiz Capitulino }
1854b389b5dSLuiz Capitulino
1864b389b5dSLuiz Capitulino { "execute": "hello-world", "arguments": { "message": "We love qemu" } }
1874b389b5dSLuiz Capitulino {
1884b389b5dSLuiz Capitulino     "return": {
1894b389b5dSLuiz Capitulino     }
1904b389b5dSLuiz Capitulino }
1914b389b5dSLuiz Capitulino
192bb46af41SMarkus ArmbrusterYou should see "Hello, world" and "We love qemu" in the terminal running qemu,
1934b389b5dSLuiz Capitulinoif you don't see these strings, then something went wrong.
1944b389b5dSLuiz Capitulino
19568e6dc59SJohn Snow
19668e6dc59SJohn SnowErrors
19768e6dc59SJohn Snow~~~~~~
1984b389b5dSLuiz Capitulino
1994b389b5dSLuiz CapitulinoQMP commands should use the error interface exported by the error.h header
200455b0fdeSEric Blakefile. Basically, most errors are set by calling the error_setg() function.
2014b389b5dSLuiz Capitulino
2024b389b5dSLuiz CapitulinoLet's say we don't accept the string "message" to contain the word "love". If
20368e6dc59SJohn Snowit does contain it, we want the "hello-world" command to return an error::
2044b389b5dSLuiz Capitulino
2054b389b5dSLuiz Capitulino void qmp_hello_world(bool has_message, const char *message, Error **errp)
2064b389b5dSLuiz Capitulino {
2074b389b5dSLuiz Capitulino     if (has_message) {
2084b389b5dSLuiz Capitulino         if (strstr(message, "love")) {
209455b0fdeSEric Blake             error_setg(errp, "the word 'love' is not allowed");
2104b389b5dSLuiz Capitulino             return;
2114b389b5dSLuiz Capitulino         }
2124b389b5dSLuiz Capitulino         printf("%s\n", message);
2134b389b5dSLuiz Capitulino     } else {
2144b389b5dSLuiz Capitulino         printf("Hello, world\n");
2154b389b5dSLuiz Capitulino     }
2164b389b5dSLuiz Capitulino }
2174b389b5dSLuiz Capitulino
218455b0fdeSEric BlakeThe first argument to the error_setg() function is the Error pointer
219455b0fdeSEric Blaketo pointer, which is passed to all QMP functions. The next argument is a human
220adb2072eSLuiz Capitulinodescription of the error, this is a free-form printf-like string.
2214b389b5dSLuiz Capitulino
222adb2072eSLuiz CapitulinoLet's test the example above. Build qemu, run it as defined in the "Testing"
22368e6dc59SJohn Snowsection, and then issue the following command::
224adb2072eSLuiz Capitulino
225adb2072eSLuiz Capitulino { "execute": "hello-world", "arguments": { "message": "all you need is love" } }
2264b389b5dSLuiz Capitulino
22768e6dc59SJohn SnowThe QMP server's response should be::
2284b389b5dSLuiz Capitulino
2294b389b5dSLuiz Capitulino {
2304b389b5dSLuiz Capitulino     "error": {
231adb2072eSLuiz Capitulino         "class": "GenericError",
232adb2072eSLuiz Capitulino         "desc": "the word 'love' is not allowed"
2334b389b5dSLuiz Capitulino     }
2344b389b5dSLuiz Capitulino }
2354b389b5dSLuiz Capitulino
236bb46af41SMarkus ArmbrusterNote that error_setg() produces a "GenericError" class.  In general,
237bb46af41SMarkus Armbrusterall QMP errors should have that error class.  There are two exceptions
238bb46af41SMarkus Armbrusterto this rule:
2394b389b5dSLuiz Capitulino
240bb46af41SMarkus Armbruster 1. To support a management application's need to recognize a specific
241bb46af41SMarkus Armbruster    error for special handling
2424b389b5dSLuiz Capitulino
243bb46af41SMarkus Armbruster 2. Backward compatibility
244adb2072eSLuiz Capitulino
245455b0fdeSEric BlakeIf the failure you want to report falls into one of the two cases above,
246455b0fdeSEric Blakeuse error_set() with a second argument of an ErrorClass value.
247adb2072eSLuiz Capitulino
24868e6dc59SJohn Snow
24968e6dc59SJohn SnowCommand Documentation
25068e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~~~~
2514b389b5dSLuiz Capitulino
2524b389b5dSLuiz CapitulinoThere's only one step missing to make "hello-world"'s implementation complete,
2534b389b5dSLuiz Capitulinoand that's its documentation in the schema file.
2544b389b5dSLuiz Capitulino
2554b389b5dSLuiz CapitulinoThere are many examples of such documentation in the schema file already, but
25668e6dc59SJohn Snowhere goes "hello-world"'s new entry for qapi/misc.json::
2574b389b5dSLuiz Capitulino
2584b389b5dSLuiz Capitulino ##
2594eb79bdfSZihao Chang # @hello-world:
2604b389b5dSLuiz Capitulino #
2614b389b5dSLuiz Capitulino # Print a client provided string to the standard output stream.
2624b389b5dSLuiz Capitulino #
2631d8bda12SMarkus Armbruster # @message: string to be printed
2644b389b5dSLuiz Capitulino #
2654b389b5dSLuiz Capitulino # Returns: Nothing on success.
2664b389b5dSLuiz Capitulino #
2674b389b5dSLuiz Capitulino # Notes: if @message is not provided, the "Hello, world" string will
2684b389b5dSLuiz Capitulino #        be printed instead
2694b389b5dSLuiz Capitulino #
2704b389b5dSLuiz Capitulino # Since: <next qemu stable release, eg. 1.0>
2714b389b5dSLuiz Capitulino ##
2724b389b5dSLuiz Capitulino { 'command': 'hello-world', 'data': { '*message': 'str' } }
2734b389b5dSLuiz Capitulino
2744b389b5dSLuiz CapitulinoPlease, note that the "Returns" clause is optional if a command doesn't return
2754b389b5dSLuiz Capitulinoany data nor any errors.
2764b389b5dSLuiz Capitulino
27768e6dc59SJohn Snow
27868e6dc59SJohn SnowImplementing the HMP command
27968e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2804b389b5dSLuiz Capitulino
2814b389b5dSLuiz CapitulinoNow that the QMP command is in place, we can also make it available in the human
2824b389b5dSLuiz Capitulinomonitor (HMP).
2834b389b5dSLuiz Capitulino
2844b389b5dSLuiz CapitulinoWith the introduction of the QAPI, HMP commands make QMP calls. Most of the
2854b389b5dSLuiz Capitulinotime HMP commands are simple wrappers. All HMP commands implementation exist in
286f1b3ccfaSKevin Wolfthe monitor/hmp-cmds.c file.
2874b389b5dSLuiz Capitulino
28868e6dc59SJohn SnowHere's the implementation of the "hello-world" HMP command::
2894b389b5dSLuiz Capitulino
2904b389b5dSLuiz Capitulino void hmp_hello_world(Monitor *mon, const QDict *qdict)
2914b389b5dSLuiz Capitulino {
2924b389b5dSLuiz Capitulino     const char *message = qdict_get_try_str(qdict, "message");
293e940f543SMarkus Armbruster     Error *err = NULL;
2944b389b5dSLuiz Capitulino
295e940f543SMarkus Armbruster     qmp_hello_world(!!message, message, &err);
296*6fa6b54fSDaniel P. Berrangé     if (hmp_handle_error(mon, err)) {
2974b389b5dSLuiz Capitulino         return;
2984b389b5dSLuiz Capitulino     }
2994b389b5dSLuiz Capitulino }
3004b389b5dSLuiz Capitulino
3014b389b5dSLuiz CapitulinoAlso, you have to add the function's prototype to the hmp.h file.
3024b389b5dSLuiz Capitulino
3034b389b5dSLuiz CapitulinoThere are three important points to be noticed:
3044b389b5dSLuiz Capitulino
3054b389b5dSLuiz Capitulino1. The "mon" and "qdict" arguments are mandatory for all HMP functions. The
3064b389b5dSLuiz Capitulino   former is the monitor object. The latter is how the monitor passes
3074b389b5dSLuiz Capitulino   arguments entered by the user to the command implementation
308*6fa6b54fSDaniel P. Berrangé2. hmp_hello_world() performs error checking. In this example we just call
309*6fa6b54fSDaniel P. Berrangé   hmp_handle_error() which prints a message to the user, but we could do
310*6fa6b54fSDaniel P. Berrangé   more, like taking different actions depending on the error
311*6fa6b54fSDaniel P. Berrangé   qmp_hello_world() returns
312e940f543SMarkus Armbruster3. The "err" variable must be initialized to NULL before performing the
3134b389b5dSLuiz Capitulino   QMP call
3144b389b5dSLuiz Capitulino
3154b389b5dSLuiz CapitulinoThere's one last step to actually make the command available to monitor users,
31668e6dc59SJohn Snowwe should add it to the hmp-commands.hx file::
3174b389b5dSLuiz Capitulino
3184b389b5dSLuiz Capitulino    {
3194b389b5dSLuiz Capitulino        .name       = "hello-world",
3204b389b5dSLuiz Capitulino        .args_type  = "message:s?",
3214b389b5dSLuiz Capitulino        .params     = "hello-world [message]",
3224b389b5dSLuiz Capitulino        .help       = "Print message to the standard output",
3232b9e3576SMarc-André Lureau        .cmd        = hmp_hello_world,
3244b389b5dSLuiz Capitulino    },
3254b389b5dSLuiz Capitulino
32668e6dc59SJohn Snow::
32768e6dc59SJohn Snow
3284b389b5dSLuiz Capitulino STEXI
3294b389b5dSLuiz Capitulino @item hello_world @var{message}
3304b389b5dSLuiz Capitulino @findex hello_world
3314b389b5dSLuiz Capitulino Print message to the standard output
3324b389b5dSLuiz Capitulino ETEXI
3334b389b5dSLuiz Capitulino
3344b389b5dSLuiz CapitulinoTo test this you have to open a user monitor and issue the "hello-world"
3354b389b5dSLuiz Capitulinocommand. It might be instructive to check the command's documentation with
3364b389b5dSLuiz CapitulinoHMP's "help" command.
3374b389b5dSLuiz Capitulino
3384b389b5dSLuiz CapitulinoPlease, check the "-monitor" command-line option to know how to open a user
3394b389b5dSLuiz Capitulinomonitor.
3404b389b5dSLuiz Capitulino
34168e6dc59SJohn Snow
342fa2613afSDaniel P. BerrangéWriting more complex commands
343fa2613afSDaniel P. Berrangé-----------------------------
3444b389b5dSLuiz Capitulino
3454b389b5dSLuiz CapitulinoA QMP command is capable of returning any data the QAPI supports like integers,
3464b389b5dSLuiz Capitulinostrings, booleans, enumerations and user defined types.
3474b389b5dSLuiz Capitulino
3484b389b5dSLuiz CapitulinoIn this section we will focus on user defined types. Please, check the QAPI
3494b389b5dSLuiz Capitulinodocumentation for information about the other types.
3504b389b5dSLuiz Capitulino
35168e6dc59SJohn Snow
35268e6dc59SJohn SnowUser Defined Types
35368e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~
3544b389b5dSLuiz Capitulino
355e218052fSMarkus ArmbrusterFIXME This example needs to be redone after commit 6d32717
356e218052fSMarkus Armbruster
3574b389b5dSLuiz CapitulinoFor this example we will write the query-alarm-clock command, which returns
3584b389b5dSLuiz Capitulinoinformation about QEMU's timer alarm. For more information about it, please
3594b389b5dSLuiz Capitulinocheck the "-clock" command-line option.
3604b389b5dSLuiz Capitulino
3614b389b5dSLuiz CapitulinoWe want to return two pieces of information. The first one is the alarm clock's
3624b389b5dSLuiz Capitulinoname. The second one is when the next alarm will fire. The former information is
3634b389b5dSLuiz Capitulinoreturned as a string, the latter is an integer in nanoseconds (which is not
3644b389b5dSLuiz Capitulinovery useful in practice, as the timer has probably already fired when the
3654b389b5dSLuiz Capitulinoinformation reaches the client).
3664b389b5dSLuiz Capitulino
36768e6dc59SJohn SnowThe best way to return that data is to create a new QAPI type, as shown below::
3684b389b5dSLuiz Capitulino
3694b389b5dSLuiz Capitulino ##
3704b389b5dSLuiz Capitulino # @QemuAlarmClock
3714b389b5dSLuiz Capitulino #
3724b389b5dSLuiz Capitulino # QEMU alarm clock information.
3734b389b5dSLuiz Capitulino #
3744b389b5dSLuiz Capitulino # @clock-name: The alarm clock method's name.
3754b389b5dSLuiz Capitulino #
3761d8bda12SMarkus Armbruster # @next-deadline: The time (in nanoseconds) the next alarm will fire.
3774b389b5dSLuiz Capitulino #
3784b389b5dSLuiz Capitulino # Since: 1.0
3794b389b5dSLuiz Capitulino ##
3804b389b5dSLuiz Capitulino { 'type': 'QemuAlarmClock',
3814b389b5dSLuiz Capitulino   'data': { 'clock-name': 'str', '*next-deadline': 'int' } }
3824b389b5dSLuiz Capitulino
3834b389b5dSLuiz CapitulinoThe "type" keyword defines a new QAPI type. Its "data" member contains the
3844b389b5dSLuiz Capitulinotype's members. In this example our members are the "clock-name" and the
3854b389b5dSLuiz Capitulino"next-deadline" one, which is optional.
3864b389b5dSLuiz Capitulino
38768e6dc59SJohn SnowNow let's define the query-alarm-clock command::
3884b389b5dSLuiz Capitulino
3894b389b5dSLuiz Capitulino ##
3904b389b5dSLuiz Capitulino # @query-alarm-clock
3914b389b5dSLuiz Capitulino #
3924b389b5dSLuiz Capitulino # Return information about QEMU's alarm clock.
3934b389b5dSLuiz Capitulino #
3944b389b5dSLuiz Capitulino # Returns a @QemuAlarmClock instance describing the alarm clock method
3954b389b5dSLuiz Capitulino # being currently used by QEMU (this is usually set by the '-clock'
3964b389b5dSLuiz Capitulino # command-line option).
3974b389b5dSLuiz Capitulino #
3984b389b5dSLuiz Capitulino # Since: 1.0
3994b389b5dSLuiz Capitulino ##
4004b389b5dSLuiz Capitulino { 'command': 'query-alarm-clock', 'returns': 'QemuAlarmClock' }
4014b389b5dSLuiz Capitulino
4024b389b5dSLuiz CapitulinoNotice the "returns" keyword. As its name suggests, it's used to define the
4034b389b5dSLuiz Capitulinodata returned by a command.
4044b389b5dSLuiz Capitulino
4054b389b5dSLuiz CapitulinoIt's time to implement the qmp_query_alarm_clock() function, you can put it
40668e6dc59SJohn Snowin the qemu-timer.c file::
4074b389b5dSLuiz Capitulino
4084b389b5dSLuiz Capitulino QemuAlarmClock *qmp_query_alarm_clock(Error **errp)
4094b389b5dSLuiz Capitulino {
4104b389b5dSLuiz Capitulino     QemuAlarmClock *clock;
4114b389b5dSLuiz Capitulino     int64_t deadline;
4124b389b5dSLuiz Capitulino
4134b389b5dSLuiz Capitulino     clock = g_malloc0(sizeof(*clock));
4144b389b5dSLuiz Capitulino
4154b389b5dSLuiz Capitulino     deadline = qemu_next_alarm_deadline();
4164b389b5dSLuiz Capitulino     if (deadline > 0) {
4174b389b5dSLuiz Capitulino         clock->has_next_deadline = true;
4184b389b5dSLuiz Capitulino         clock->next_deadline = deadline;
4194b389b5dSLuiz Capitulino     }
4204b389b5dSLuiz Capitulino     clock->clock_name = g_strdup(alarm_timer->name);
4214b389b5dSLuiz Capitulino
4224b389b5dSLuiz Capitulino     return clock;
4234b389b5dSLuiz Capitulino }
4244b389b5dSLuiz Capitulino
4254b389b5dSLuiz CapitulinoThere are a number of things to be noticed:
4264b389b5dSLuiz Capitulino
4274b389b5dSLuiz Capitulino1. The QemuAlarmClock type is automatically generated by the QAPI framework,
4284b389b5dSLuiz Capitulino   its members correspond to the type's specification in the schema file
4294b389b5dSLuiz Capitulino2. As specified in the schema file, the function returns a QemuAlarmClock
4304b389b5dSLuiz Capitulino   instance and takes no arguments (besides the "errp" one, which is mandatory
4314b389b5dSLuiz Capitulino   for all QMP functions)
4324b389b5dSLuiz Capitulino3. The "clock" variable (which will point to our QAPI type instance) is
4334b389b5dSLuiz Capitulino   allocated by the regular g_malloc0() function. Note that we chose to
434dabdf394SStefan Weil   initialize the memory to zero. This is recommended for all QAPI types, as
4354b389b5dSLuiz Capitulino   it helps avoiding bad surprises (specially with booleans)
4364b389b5dSLuiz Capitulino4. Remember that "next_deadline" is optional? All optional members have a
4374b389b5dSLuiz Capitulino   'has_TYPE_NAME' member that should be properly set by the implementation,
4384b389b5dSLuiz Capitulino   as shown above
4394b389b5dSLuiz Capitulino5. Even static strings, such as "alarm_timer->name", should be dynamically
4404b389b5dSLuiz Capitulino   allocated by the implementation. This is so because the QAPI also generates
4414b389b5dSLuiz Capitulino   a function to free its types and it cannot distinguish between dynamically
4424b389b5dSLuiz Capitulino   or statically allocated strings
443eb815e24SMarkus Armbruster6. You have to include "qapi/qapi-commands-misc.h" in qemu-timer.c
4444b389b5dSLuiz Capitulino
4454b389b5dSLuiz CapitulinoTime to test the new command. Build qemu, run it as described in the "Testing"
44668e6dc59SJohn Snowsection and try this::
4474b389b5dSLuiz Capitulino
4484b389b5dSLuiz Capitulino { "execute": "query-alarm-clock" }
4494b389b5dSLuiz Capitulino {
4504b389b5dSLuiz Capitulino     "return": {
4514b389b5dSLuiz Capitulino         "next-deadline": 2368219,
4524b389b5dSLuiz Capitulino         "clock-name": "dynticks"
4534b389b5dSLuiz Capitulino     }
4544b389b5dSLuiz Capitulino }
4554b389b5dSLuiz Capitulino
4564b389b5dSLuiz Capitulino
45768e6dc59SJohn SnowThe HMP command
45868e6dc59SJohn Snow~~~~~~~~~~~~~~~
45968e6dc59SJohn Snow
46068e6dc59SJohn SnowHere's the HMP counterpart of the query-alarm-clock command::
4614b389b5dSLuiz Capitulino
4624b389b5dSLuiz Capitulino void hmp_info_alarm_clock(Monitor *mon)
4634b389b5dSLuiz Capitulino {
4644b389b5dSLuiz Capitulino     QemuAlarmClock *clock;
465e940f543SMarkus Armbruster     Error *err = NULL;
4664b389b5dSLuiz Capitulino
467e940f543SMarkus Armbruster     clock = qmp_query_alarm_clock(&err);
468*6fa6b54fSDaniel P. Berrangé     if (hmp_handle_error(mon, err)) {
4694b389b5dSLuiz Capitulino         return;
4704b389b5dSLuiz Capitulino     }
4714b389b5dSLuiz Capitulino
4724b389b5dSLuiz Capitulino     monitor_printf(mon, "Alarm clock method in use: '%s'\n", clock->clock_name);
4734b389b5dSLuiz Capitulino     if (clock->has_next_deadline) {
4744b389b5dSLuiz Capitulino         monitor_printf(mon, "Next alarm will fire in %" PRId64 " nanoseconds\n",
4754b389b5dSLuiz Capitulino                        clock->next_deadline);
4764b389b5dSLuiz Capitulino     }
4774b389b5dSLuiz Capitulino
4784b389b5dSLuiz Capitulino    qapi_free_QemuAlarmClock(clock);
4794b389b5dSLuiz Capitulino }
4804b389b5dSLuiz Capitulino
4814b389b5dSLuiz CapitulinoIt's important to notice that hmp_info_alarm_clock() calls
4824b389b5dSLuiz Capitulinoqapi_free_QemuAlarmClock() to free the data returned by qmp_query_alarm_clock().
4834b389b5dSLuiz CapitulinoFor user defined types, the QAPI will generate a qapi_free_QAPI_TYPE_NAME()
4844b389b5dSLuiz Capitulinofunction and that's what you have to use to free the types you define and
4854b389b5dSLuiz Capitulinoqapi_free_QAPI_TYPE_NAMEList() for list types (explained in the next section).
4864b389b5dSLuiz CapitulinoIf the QMP call returns a string, then you should g_free() to free it.
4874b389b5dSLuiz Capitulino
4884b389b5dSLuiz CapitulinoAlso note that hmp_info_alarm_clock() performs error handling. That's not
4894b389b5dSLuiz Capitulinostrictly required if you're sure the QMP function doesn't return errors, but
4904b389b5dSLuiz Capitulinoit's good practice to always check for errors.
4914b389b5dSLuiz Capitulino
4924b389b5dSLuiz CapitulinoAnother important detail is that HMP's "info" commands don't go into the
4934b389b5dSLuiz Capitulinohmp-commands.hx. Instead, they go into the info_cmds[] table, which is defined
49468e6dc59SJohn Snowin the monitor/misc.c file. The entry for the "info alarmclock" follows::
4954b389b5dSLuiz Capitulino
4964b389b5dSLuiz Capitulino    {
4974b389b5dSLuiz Capitulino        .name       = "alarmclock",
4984b389b5dSLuiz Capitulino        .args_type  = "",
4994b389b5dSLuiz Capitulino        .params     = "",
5004b389b5dSLuiz Capitulino        .help       = "show information about the alarm clock",
5012b9e3576SMarc-André Lureau        .cmd        = hmp_info_alarm_clock,
5024b389b5dSLuiz Capitulino    },
5034b389b5dSLuiz Capitulino
5044b389b5dSLuiz CapitulinoTo test this, run qemu and type "info alarmclock" in the user monitor.
5054b389b5dSLuiz Capitulino
50668e6dc59SJohn Snow
50768e6dc59SJohn SnowReturning Lists
50868e6dc59SJohn Snow~~~~~~~~~~~~~~~
5094b389b5dSLuiz Capitulino
5104b389b5dSLuiz CapitulinoFor this example, we're going to return all available methods for the timer
5114b389b5dSLuiz Capitulinoalarm, which is pretty much what the command-line option "-clock ?" does,
5124b389b5dSLuiz Capitulinoexcept that we're also going to inform which method is in use.
5134b389b5dSLuiz Capitulino
51468e6dc59SJohn SnowThis first step is to define a new type::
5154b389b5dSLuiz Capitulino
5164b389b5dSLuiz Capitulino ##
5174b389b5dSLuiz Capitulino # @TimerAlarmMethod
5184b389b5dSLuiz Capitulino #
5194b389b5dSLuiz Capitulino # Timer alarm method information.
5204b389b5dSLuiz Capitulino #
5214b389b5dSLuiz Capitulino # @method-name: The method's name.
5224b389b5dSLuiz Capitulino #
5234b389b5dSLuiz Capitulino # @current: true if this alarm method is currently in use, false otherwise
5244b389b5dSLuiz Capitulino #
5254b389b5dSLuiz Capitulino # Since: 1.0
5264b389b5dSLuiz Capitulino ##
5274b389b5dSLuiz Capitulino { 'type': 'TimerAlarmMethod',
5284b389b5dSLuiz Capitulino   'data': { 'method-name': 'str', 'current': 'bool' } }
5294b389b5dSLuiz Capitulino
5304b389b5dSLuiz CapitulinoThe command will be called "query-alarm-methods", here is its schema
53168e6dc59SJohn Snowspecification::
5324b389b5dSLuiz Capitulino
5334b389b5dSLuiz Capitulino ##
5344b389b5dSLuiz Capitulino # @query-alarm-methods
5354b389b5dSLuiz Capitulino #
5364b389b5dSLuiz Capitulino # Returns information about available alarm methods.
5374b389b5dSLuiz Capitulino #
5384b389b5dSLuiz Capitulino # Returns: a list of @TimerAlarmMethod for each method
5394b389b5dSLuiz Capitulino #
5404b389b5dSLuiz Capitulino # Since: 1.0
5414b389b5dSLuiz Capitulino ##
5424b389b5dSLuiz Capitulino { 'command': 'query-alarm-methods', 'returns': ['TimerAlarmMethod'] }
5434b389b5dSLuiz Capitulino
5444b389b5dSLuiz CapitulinoNotice the syntax for returning lists "'returns': ['TimerAlarmMethod']", this
5454b389b5dSLuiz Capitulinoshould be read as "returns a list of TimerAlarmMethod instances".
5464b389b5dSLuiz Capitulino
54768e6dc59SJohn SnowThe C implementation follows::
5484b389b5dSLuiz Capitulino
5494b389b5dSLuiz Capitulino TimerAlarmMethodList *qmp_query_alarm_methods(Error **errp)
5504b389b5dSLuiz Capitulino {
5514b389b5dSLuiz Capitulino     TimerAlarmMethodList *method_list = NULL;
5524b389b5dSLuiz Capitulino     const struct qemu_alarm_timer *p;
5534b389b5dSLuiz Capitulino     bool current = true;
5544b389b5dSLuiz Capitulino
5554b389b5dSLuiz Capitulino     for (p = alarm_timers; p->name; p++) {
55654aa3de7SEric Blake         TimerAlarmMethod *value = g_malloc0(*value);
55754aa3de7SEric Blake         value->method_name = g_strdup(p->name);
55854aa3de7SEric Blake         value->current = current;
55954aa3de7SEric Blake         QAPI_LIST_PREPEND(method_list, value);
5604b389b5dSLuiz Capitulino         current = false;
5614b389b5dSLuiz Capitulino     }
5624b389b5dSLuiz Capitulino
5634b389b5dSLuiz Capitulino     return method_list;
5644b389b5dSLuiz Capitulino }
5654b389b5dSLuiz Capitulino
5664b389b5dSLuiz CapitulinoThe most important difference from the previous examples is the
5674b389b5dSLuiz CapitulinoTimerAlarmMethodList type, which is automatically generated by the QAPI from
5684b389b5dSLuiz Capitulinothe TimerAlarmMethod type.
5694b389b5dSLuiz Capitulino
5704b389b5dSLuiz CapitulinoEach list node is represented by a TimerAlarmMethodList instance. We have to
5714b389b5dSLuiz Capitulinoallocate it, and that's done inside the for loop: the "info" pointer points to
5724b389b5dSLuiz Capitulinoan allocated node. We also have to allocate the node's contents, which is
5734b389b5dSLuiz Capitulinostored in its "value" member. In our example, the "value" member is a pointer
5744b389b5dSLuiz Capitulinoto an TimerAlarmMethod instance.
5754b389b5dSLuiz Capitulino
5764b389b5dSLuiz CapitulinoNotice that the "current" variable is used as "true" only in the first
5775708b2b7SChen Hanxiaoiteration of the loop. That's because the alarm timer method in use is the
5784b389b5dSLuiz Capitulinofirst element of the alarm_timers array. Also notice that QAPI lists are handled
5794b389b5dSLuiz Capitulinoby hand and we return the head of the list.
5804b389b5dSLuiz Capitulino
5814b389b5dSLuiz CapitulinoNow Build qemu, run it as explained in the "Testing" section and try our new
58268e6dc59SJohn Snowcommand::
5834b389b5dSLuiz Capitulino
5844b389b5dSLuiz Capitulino { "execute": "query-alarm-methods" }
5854b389b5dSLuiz Capitulino {
5864b389b5dSLuiz Capitulino     "return": [
5874b389b5dSLuiz Capitulino         {
5884b389b5dSLuiz Capitulino             "current": false,
5894b389b5dSLuiz Capitulino             "method-name": "unix"
5904b389b5dSLuiz Capitulino         },
5914b389b5dSLuiz Capitulino         {
5924b389b5dSLuiz Capitulino             "current": true,
5934b389b5dSLuiz Capitulino             "method-name": "dynticks"
5944b389b5dSLuiz Capitulino         }
5954b389b5dSLuiz Capitulino     ]
5964b389b5dSLuiz Capitulino }
5974b389b5dSLuiz Capitulino
5984b389b5dSLuiz CapitulinoThe HMP counterpart is a bit more complex than previous examples because it
59968e6dc59SJohn Snowhas to traverse the list, it's shown below for reference::
6004b389b5dSLuiz Capitulino
6014b389b5dSLuiz Capitulino void hmp_info_alarm_methods(Monitor *mon)
6024b389b5dSLuiz Capitulino {
6034b389b5dSLuiz Capitulino     TimerAlarmMethodList *method_list, *method;
604e940f543SMarkus Armbruster     Error *err = NULL;
6054b389b5dSLuiz Capitulino
606e940f543SMarkus Armbruster     method_list = qmp_query_alarm_methods(&err);
607*6fa6b54fSDaniel P. Berrangé     if (hmp_handle_error(mon, err)) {
6084b389b5dSLuiz Capitulino         return;
6094b389b5dSLuiz Capitulino     }
6104b389b5dSLuiz Capitulino
6114b389b5dSLuiz Capitulino     for (method = method_list; method; method = method->next) {
6124b389b5dSLuiz Capitulino         monitor_printf(mon, "%c %s\n", method->value->current ? '*' : ' ',
6134b389b5dSLuiz Capitulino                                        method->value->method_name);
6144b389b5dSLuiz Capitulino     }
6154b389b5dSLuiz Capitulino
6164b389b5dSLuiz Capitulino     qapi_free_TimerAlarmMethodList(method_list);
6174b389b5dSLuiz Capitulino }
618