1How to write monitor commands
2=============================
3
4This document is a step-by-step guide on how to write new QMP commands using
5the QAPI framework and HMP commands.
6
7This document doesn't discuss QMP protocol level details, nor does it dive
8into the QAPI framework implementation.
9
10For an in-depth introduction to the QAPI framework, please refer to
11:doc:`qapi-code-gen`.  For the QMP protocol, see the
12:doc:`/interop/qmp-spec`.
13
14New commands may be implemented in QMP only.  New HMP commands should be
15implemented on top of QMP.  The typical HMP command wraps around an
16equivalent QMP command, but HMP convenience commands built from QMP
17building blocks are also fine.  The long term goal is to make all
18existing HMP commands conform to this, to fully isolate HMP from the
19internals of QEMU. Refer to the `Writing a debugging aid returning
20unstructured text`_ section for further guidance on commands that
21would have traditionally been HMP only.
22
23Overview
24--------
25
26Generally speaking, the following steps should be taken in order to write a
27new QMP command.
28
291. Define the command and any types it needs in the appropriate QAPI
30   schema module.
31
322. Write the QMP command itself, which is a regular C function. Preferably,
33   the command should be exported by some QEMU subsystem. But it can also be
34   added to the monitor/qmp-cmds.c file
35
363. At this point the command can be tested under the QMP protocol
37
384. Write the HMP command equivalent. This is not required and should only be
39   done if it does make sense to have the functionality in HMP. The HMP command
40   is implemented in terms of the QMP command
41
42The following sections will demonstrate each of the steps above. We will start
43very simple and get more complex as we progress.
44
45
46Testing
47-------
48
49For all the examples in the next sections, the test setup is the same and is
50shown here.
51
52First, QEMU should be started like this::
53
54 # qemu-system-TARGET [...] \
55     -chardev socket,id=qmp,port=4444,host=localhost,server=on \
56     -mon chardev=qmp,mode=control,pretty=on
57
58Then, in a different terminal::
59
60 $ telnet localhost 4444
61 Trying 127.0.0.1...
62 Connected to localhost.
63 Escape character is '^]'.
64 {
65     "QMP": {
66         "version": {
67             "qemu": {
68                 "micro": 50,
69                 "minor": 2,
70                 "major": 8
71             },
72             "package": ...
73         },
74         "capabilities": [
75             "oob"
76         ]
77     }
78 }
79
80The above output is the QMP server saying you're connected. The server is
81actually in capabilities negotiation mode. To enter in command mode type::
82
83 { "execute": "qmp_capabilities" }
84
85Then the server should respond::
86
87 {
88     "return": {
89     }
90 }
91
92Which is QMP's way of saying "the latest command executed OK and didn't return
93any data". Now you're ready to enter the QMP example commands as explained in
94the following sections.
95
96
97Writing a simple command: hello-world
98-------------------------------------
99
100That's the most simple QMP command that can be written. Usually, this kind of
101command carries some meaningful action in QEMU but here it will just print
102"Hello, world" to the standard output.
103
104Our command will be called "hello-world". It takes no arguments, nor does it
105return any data.
106
107The first step is defining the command in the appropriate QAPI schema
108module.  We pick module qapi/misc.json, and add the following line at
109the bottom::
110
111 ##
112 # @hello-world:
113 #
114 # Since: 9.0
115 ##
116 { 'command': 'hello-world' }
117
118The "command" keyword defines a new QMP command. It instructs QAPI to
119generate any prototypes and the necessary code to marshal and unmarshal
120protocol data.
121
122The next step is to write the "hello-world" implementation. As explained
123earlier, it's preferable for commands to live in QEMU subsystems. But
124"hello-world" doesn't pertain to any, so we put its implementation in
125monitor/qmp-cmds.c::
126
127 void qmp_hello_world(Error **errp)
128 {
129     printf("Hello, world!\n");
130 }
131
132There are a few things to be noticed:
133
1341. QMP command implementation functions must be prefixed with "qmp\_"
1352. qmp_hello_world() returns void, this is in accordance with the fact that the
136   command doesn't return any data
1373. It takes an "Error \*\*" argument. This is required. Later we will see how to
138   return errors and take additional arguments. The Error argument should not
139   be touched if the command doesn't return errors
1404. We won't add the function's prototype. That's automatically done by QAPI
1415. Printing to the terminal is discouraged for QMP commands, we do it here
142   because it's the easiest way to demonstrate a QMP command
143
144You're done. Now build QEMU, run it as suggested in the "Testing" section,
145and then type the following QMP command::
146
147 { "execute": "hello-world" }
148
149Then check the terminal running QEMU and look for the "Hello, world" string. If
150you don't see it then something went wrong.
151
152
153Arguments
154~~~~~~~~~
155
156Let's add arguments to our "hello-world" command.
157
158The first change we have to do is to modify the command specification in the
159schema file to the following::
160
161 ##
162 # @hello-world:
163 #
164 # @message: message to be printed (default: "Hello, world!")
165 #
166 # @times: how many times to print the message (default: 1)
167 #
168 # Since: 9.0
169 ##
170 { 'command': 'hello-world',
171   'data': { '*message': 'str', '*times': 'int' } }
172
173Notice the new 'data' member in the schema. It specifies an argument
174'message' of QAPI type 'str', and an argument 'times' of QAPI type
175'int'.  Also notice the asterisk, it's used to mark the argument
176optional.
177
178Now, let's update our C implementation in monitor/qmp-cmds.c::
179
180 void qmp_hello_world(const char *message, bool has_times, int64_t times,
181                      Error **errp)
182 {
183     if (!message) {
184         message = "Hello, world";
185     }
186     if (!has_times) {
187         times = 1;
188     }
189
190     for (int i = 0; i < times; i++) {
191         printf("%s\n", message);
192     }
193 }
194
195There are two important details to be noticed:
196
1971. Optional arguments other than pointers are accompanied by a 'has\_'
198   boolean, which is set if the optional argument is present or false
199   otherwise
2002. The C implementation signature must follow the schema's argument ordering,
201   which is defined by the "data" member
202
203Time to test our new version of the "hello-world" command. Build QEMU, run it as
204described in the "Testing" section and then send two commands::
205
206 { "execute": "hello-world" }
207 {
208     "return": {
209     }
210 }
211
212 { "execute": "hello-world", "arguments": { "message": "We love QEMU" } }
213 {
214     "return": {
215     }
216 }
217
218You should see "Hello, world" and "We love QEMU" in the terminal running QEMU,
219if you don't see these strings, then something went wrong.
220
221
222Errors
223~~~~~~
224
225QMP commands should use the error interface exported by the error.h header
226file. Basically, most errors are set by calling the error_setg() function.
227
228Let's say we don't accept the string "message" to contain the word "love". If
229it does contain it, we want the "hello-world" command to return an error::
230
231 void qmp_hello_world(const char *message, Error **errp)
232 {
233     if (message) {
234         if (strstr(message, "love")) {
235             error_setg(errp, "the word 'love' is not allowed");
236             return;
237         }
238         printf("%s\n", message);
239     } else {
240         printf("Hello, world\n");
241     }
242 }
243
244The first argument to the error_setg() function is the Error pointer
245to pointer, which is passed to all QMP functions. The next argument is a human
246description of the error, this is a free-form printf-like string.
247
248Let's test the example above. Build QEMU, run it as defined in the "Testing"
249section, and then issue the following command::
250
251 { "execute": "hello-world", "arguments": { "message": "all you need is love" } }
252
253The QMP server's response should be::
254
255 {
256     "error": {
257         "class": "GenericError",
258         "desc": "the word 'love' is not allowed"
259     }
260 }
261
262Note that error_setg() produces a "GenericError" class.  In general,
263all QMP errors should have that error class.  There are two exceptions
264to this rule:
265
266 1. To support a management application's need to recognize a specific
267    error for special handling
268
269 2. Backward compatibility
270
271If the failure you want to report falls into one of the two cases above,
272use error_set() with a second argument of an ErrorClass value.
273
274
275Implementing the HMP command
276~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277
278Now that the QMP command is in place, we can also make it available in the human
279monitor (HMP).
280
281With the introduction of QAPI, HMP commands make QMP calls. Most of the
282time HMP commands are simple wrappers.
283
284Here's the implementation of the "hello-world" HMP command::
285
286 void hmp_hello_world(Monitor *mon, const QDict *qdict)
287 {
288     const char *message = qdict_get_try_str(qdict, "message");
289     Error *err = NULL;
290
291     qmp_hello_world(!!message, message, &err);
292     if (hmp_handle_error(mon, err)) {
293         return;
294     }
295 }
296
297Add it to monitor/hmp-cmds.c.  Also, add its prototype to
298include/monitor/hmp.h.
299
300There are four important points to be noticed:
301
3021. The "mon" and "qdict" arguments are mandatory for all HMP functions. The
303   former is the monitor object. The latter is how the monitor passes
304   arguments entered by the user to the command implementation
3052. We chose not to support the "times" argument in HMP
3063. hmp_hello_world() performs error checking. In this example we just call
307   hmp_handle_error() which prints a message to the user, but we could do
308   more, like taking different actions depending on the error
309   qmp_hello_world() returns
3104. The "err" variable must be initialized to NULL before performing the
311   QMP call
312
313There's one last step to actually make the command available to monitor users,
314we should add it to the hmp-commands.hx file::
315
316    {
317        .name       = "hello-world",
318        .args_type  = "message:s?",
319        .params     = "hello-world [message]",
320        .help       = "Print message to the standard output",
321        .cmd        = hmp_hello_world,
322    },
323
324 SRST
325 ``hello_world`` *message*
326   Print message to the standard output
327 ERST
328
329To test this you have to open a user monitor and issue the "hello-world"
330command. It might be instructive to check the command's documentation with
331HMP's "help" command.
332
333Please check the "-monitor" command-line option to know how to open a user
334monitor.
335
336
337Writing more complex commands
338-----------------------------
339
340A QMP command is capable of returning any data QAPI supports like integers,
341strings, booleans, enumerations and user defined types.
342
343In this section we will focus on user defined types. Please check the QAPI
344documentation for information about the other types.
345
346
347Modelling data in QAPI
348~~~~~~~~~~~~~~~~~~~~~~
349
350For a QMP command that to be considered stable and supported long term,
351there is a requirement returned data should be explicitly modelled
352using fine-grained QAPI types. As a general guide, a caller of the QMP
353command should never need to parse individual returned data fields. If
354a field appears to need parsing, then it should be split into separate
355fields corresponding to each distinct data item. This should be the
356common case for any new QMP command that is intended to be used by
357machines, as opposed to exclusively human operators.
358
359Some QMP commands, however, are only intended as ad hoc debugging aids
360for human operators. While they may return large amounts of formatted
361data, it is not expected that machines will need to parse the result.
362The overhead of defining a fine grained QAPI type for the data may not
363be justified by the potential benefit. In such cases, it is permitted
364to have a command return a simple string that contains formatted data,
365however, it is mandatory for the command to be marked unstable.
366This indicates that the command is not guaranteed to be long term
367stable / liable to change in future and is not following QAPI design
368best practices. An example where this approach is taken is the QMP
369command "x-query-registers". This returns a formatted dump of the
370architecture specific CPU state. The way the data is formatted varies
371across QEMU targets, is liable to change over time, and is only
372intended to be consumed as an opaque string by machines. Refer to the
373`Writing a debugging aid returning unstructured text`_ section for
374an illustration.
375
376User Defined Types
377~~~~~~~~~~~~~~~~~~
378
379For this example we will write the query-option-roms command, which
380returns information about ROMs loaded into the option ROM space. For
381more information about it, please check the "-option-rom" command-line
382option.
383
384For each option ROM, we want to return two pieces of information: the
385ROM image's file name, and its bootindex, if any.  We need to create a
386new QAPI type for that, as shown below::
387
388 ##
389 # @OptionRomInfo:
390 #
391 # @filename: option ROM image file name
392 #
393 # @bootindex: option ROM's bootindex
394 #
395 # Since: 9.0
396 ##
397 { 'struct': 'OptionRomInfo',
398   'data': { 'filename': 'str', '*bootindex': 'int' } }
399
400The "struct" keyword defines a new QAPI type. Its "data" member
401contains the type's members. In this example our members are
402"filename" and "bootindex". The latter is optional.
403
404Now let's define the query-option-roms command::
405
406 ##
407 # @query-option-roms:
408 #
409 # Query information on ROMs loaded into the option ROM space.
410 #
411 # Returns: OptionRomInfo
412 #
413 # Since: 9.0
414 ##
415 { 'command': 'query-option-roms',
416   'returns': ['OptionRomInfo'] }
417
418Notice the "returns" keyword. As its name suggests, it's used to define the
419data returned by a command.
420
421Notice the syntax ['OptionRomInfo']". This should be read as "returns
422a list of OptionRomInfo".
423
424It's time to implement the qmp_query_option_roms() function.  Add to
425monitor/qmp-cmds.c::
426
427 OptionRomInfoList *qmp_query_option_roms(Error **errp)
428 {
429     OptionRomInfoList *info_list = NULL;
430     OptionRomInfoList **tailp = &info_list;
431     OptionRomInfo *info;
432
433     for (int i = 0; i < nb_option_roms; i++) {
434         info = g_malloc0(sizeof(*info));
435         info->filename = g_strdup(option_rom[i].name);
436         info->has_bootindex = option_rom[i].bootindex >= 0;
437         if (info->has_bootindex) {
438             info->bootindex = option_rom[i].bootindex;
439         }
440         QAPI_LIST_APPEND(tailp, info);
441     }
442
443     return info_list;
444 }
445
446There are a number of things to be noticed:
447
4481. Type OptionRomInfo is automatically generated by the QAPI framework,
449   its members correspond to the type's specification in the schema
450   file
4512. Type OptionRomInfoList is also generated.  It's a singly linked
452   list.
4533. As specified in the schema file, the function returns a
454   OptionRomInfoList, and takes no arguments (besides the "errp" one,
455   which is mandatory for all QMP functions)
4564. The returned object is dynamically allocated
4575. All strings are dynamically allocated. This is so because QAPI also
458   generates a function to free its types and it cannot distinguish
459   between dynamically or statically allocated strings
4606. Remember that "bootindex" is optional? As a non-pointer optional
461   member, it comes with a 'has_bootindex' member that needs to be set
462   by the implementation, as shown above
463
464Time to test the new command. Build QEMU, run it as described in the "Testing"
465section and try this::
466
467 { "execute": "query-option-rom" }
468 {
469     "return": [
470         {
471             "filename": "kvmvapic.bin"
472         }
473     ]
474 }
475
476
477The HMP command
478~~~~~~~~~~~~~~~
479
480Here's the HMP counterpart of the query-option-roms command::
481
482 void hmp_info_option_roms(Monitor *mon, const QDict *qdict)
483 {
484     Error *err = NULL;
485     OptionRomInfoList *info_list, *tail;
486     OptionRomInfo *info;
487
488     info_list = qmp_query_option_roms(&err);
489     if (hmp_handle_error(mon, err)) {
490         return;
491     }
492
493     for (tail = info_list; tail; tail = tail->next) {
494         info = tail->value;
495         monitor_printf(mon, "%s", info->filename);
496         if (info->has_bootindex) {
497             monitor_printf(mon, " %" PRId64, info->bootindex);
498         }
499         monitor_printf(mon, "\n");
500     }
501
502     qapi_free_OptionRomInfoList(info_list);
503 }
504
505It's important to notice that hmp_info_option_roms() calls
506qapi_free_OptionRomInfoList() to free the data returned by
507qmp_query_option_roms().  For user defined types, QAPI will generate a
508qapi_free_QAPI_TYPE_NAME() function, and that's what you have to use to
509free the types you define and qapi_free_QAPI_TYPE_NAMEList() for list
510types (explained in the next section). If the QMP function returns a
511string, then you should g_free() to free it.
512
513Also note that hmp_info_option_roms() performs error handling. That's
514not strictly required when you're sure the QMP function doesn't return
515errors; you could instead pass it &error_abort then.
516
517Another important detail is that HMP's "info" commands go into
518hmp-commands-info.hx, not hmp-commands.hx. The entry for the "info
519option-roms" follows::
520
521     {
522         .name       = "option-roms",
523         .args_type  = "",
524         .params     = "",
525         .help       = "show roms",
526         .cmd        = hmp_info_option_roms,
527     },
528 SRST
529 ``info option-roms``
530   Show the option ROMs.
531 ERST
532
533To test this, run QEMU and type "info option-roms" in the user monitor.
534
535
536Writing a debugging aid returning unstructured text
537---------------------------------------------------
538
539As discussed in section `Modelling data in QAPI`_, it is required that
540commands expecting machine usage be using fine-grained QAPI data types.
541The exception to this rule applies when the command is solely intended
542as a debugging aid and allows for returning unstructured text, such as
543a query command that report aspects of QEMU's internal state that are
544useful only to human operators.
545
546In this example we will consider the existing QMP command
547``x-query-roms`` in qapi/machine.json.  It has no parameters and
548returns a ``HumanReadableText``::
549
550 ##
551 # @x-query-roms:
552 #
553 # Query information on the registered ROMS
554 #
555 # Features:
556 #
557 # @unstable: This command is meant for debugging.
558 #
559 # Returns: registered ROMs
560 #
561 # Since: 6.2
562 ##
563 { 'command': 'x-query-roms',
564   'returns': 'HumanReadableText',
565   'features': [ 'unstable' ] }
566
567The ``HumanReadableText`` struct is defined in qapi/common.json as a
568struct with a string member. It is intended to be used for all
569commands that are returning unstructured text targeted at
570humans. These should all have feature 'unstable'.  Note that the
571feature's documentation states why the command is unstable.  We
572commonly use a ``x-`` command name prefix to make lack of stability
573obvious to human users.
574
575Implementing the QMP command
576~~~~~~~~~~~~~~~~~~~~~~~~~~~~
577
578The QMP implementation will typically involve creating a ``GString``
579object and printing formatted data into it, like this::
580
581 HumanReadableText *qmp_x_query_roms(Error **errp)
582 {
583     g_autoptr(GString) buf = g_string_new("");
584     Rom *rom;
585
586     QTAILQ_FOREACH(rom, &roms, next) {
587        g_string_append_printf("%s size=0x%06zx name=\"%s\"\n",
588                               memory_region_name(rom->mr),
589                               rom->romsize,
590                               rom->name);
591     }
592
593     return human_readable_text_from_str(buf);
594 }
595
596The actual implementation emits more information.  You can find it in
597hw/core/loader.c.
598
599
600Implementing the HMP command
601~~~~~~~~~~~~~~~~~~~~~~~~~~~~
602
603Now that the QMP command is in place, we can also make it available in
604the human monitor (HMP) as shown in previous examples. The HMP
605implementations will all look fairly similar, as all they need do is
606invoke the QMP command and then print the resulting text or error
607message. Here's an implementation of the "info roms" HMP command::
608
609 void hmp_info_roms(Monitor *mon, const QDict *qdict)
610 {
611     Error err = NULL;
612     g_autoptr(HumanReadableText) info = qmp_x_query_roms(&err);
613
614     if (hmp_handle_error(mon, err)) {
615         return;
616     }
617     monitor_puts(mon, info->human_readable_text);
618 }
619
620Also, you have to add the function's prototype to the hmp.h file.
621
622There's one last step to actually make the command available to
623monitor users, we should add it to the hmp-commands-info.hx file::
624
625    {
626        .name       = "roms",
627        .args_type  = "",
628        .params     = "",
629        .help       = "show roms",
630        .cmd        = hmp_info_roms,
631    },
632
633The case of writing a HMP info handler that calls a no-parameter QMP query
634command is quite common. To simplify the implementation there is a general
635purpose HMP info handler for this scenario. All that is required to expose
636a no-parameter QMP query command via HMP is to declare it using the
637'.cmd_info_hrt' field to point to the QMP handler, and leave the '.cmd'
638field NULL::
639
640    {
641        .name         = "roms",
642        .args_type    = "",
643        .params       = "",
644        .help         = "show roms",
645        .cmd_info_hrt = qmp_x_query_roms,
646    },
647
648This is how the actual HMP command is done.
649