xref: /qemu/monitor/monitor-internal.h (revision 54e91d1523b412b4cff7cb36c898fa9dc133e886)
15bce308aSKevin Wolf /*
25bce308aSKevin Wolf  * QEMU monitor
35bce308aSKevin Wolf  *
45bce308aSKevin Wolf  * Copyright (c) 2003-2004 Fabrice Bellard
55bce308aSKevin Wolf  *
65bce308aSKevin Wolf  * Permission is hereby granted, free of charge, to any person obtaining a copy
75bce308aSKevin Wolf  * of this software and associated documentation files (the "Software"), to deal
85bce308aSKevin Wolf  * in the Software without restriction, including without limitation the rights
95bce308aSKevin Wolf  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
105bce308aSKevin Wolf  * copies of the Software, and to permit persons to whom the Software is
115bce308aSKevin Wolf  * furnished to do so, subject to the following conditions:
125bce308aSKevin Wolf  *
135bce308aSKevin Wolf  * The above copyright notice and this permission notice shall be included in
145bce308aSKevin Wolf  * all copies or substantial portions of the Software.
155bce308aSKevin Wolf  *
165bce308aSKevin Wolf  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
175bce308aSKevin Wolf  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
185bce308aSKevin Wolf  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
195bce308aSKevin Wolf  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
205bce308aSKevin Wolf  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
215bce308aSKevin Wolf  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
225bce308aSKevin Wolf  * THE SOFTWARE.
235bce308aSKevin Wolf  */
245bce308aSKevin Wolf 
255bce308aSKevin Wolf #ifndef MONITOR_INTERNAL_H
265bce308aSKevin Wolf #define MONITOR_INTERNAL_H
275bce308aSKevin Wolf 
285bce308aSKevin Wolf #include "chardev/char-fe.h"
295bce308aSKevin Wolf #include "monitor/monitor.h"
30fa4dcf57SKevin Wolf #include "qapi/qapi-types-control.h"
31*153b0989SDaniel P. Berrangé #include "qapi/qmp-registry.h"
32407bc4bfSDaniel P. Berrangé #include "qobject/json-parser.h"
335bce308aSKevin Wolf #include "qemu/readline.h"
3432cad1ffSPhilippe Mathieu-Daudé #include "system/iothread.h"
355bce308aSKevin Wolf 
365bce308aSKevin Wolf /*
375bce308aSKevin Wolf  * Supported types:
385bce308aSKevin Wolf  *
395bce308aSKevin Wolf  * 'F'          filename
405bce308aSKevin Wolf  * 'B'          block device name
415bce308aSKevin Wolf  * 's'          string (accept optional quote)
425bce308aSKevin Wolf  * 'S'          it just appends the rest of the string (accept optional quote)
435bce308aSKevin Wolf  * 'O'          option string of the form NAME=VALUE,...
445bce308aSKevin Wolf  *              parsed according to QemuOptsList given by its name
455bce308aSKevin Wolf  *              Example: 'device:O' uses qemu_device_opts.
465bce308aSKevin Wolf  *              Restriction: only lists with empty desc are supported
475bce308aSKevin Wolf  *              TODO lift the restriction
485bce308aSKevin Wolf  * 'i'          32 bit integer
495bce308aSKevin Wolf  * 'l'          target long (32 or 64 bit)
505bce308aSKevin Wolf  * 'M'          Non-negative target long (32 or 64 bit), in user mode the
515bce308aSKevin Wolf  *              value is multiplied by 2^20 (think Mebibyte)
525bce308aSKevin Wolf  * 'o'          octets (aka bytes)
535bce308aSKevin Wolf  *              user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
545bce308aSKevin Wolf  *              K, k suffix, which multiplies the value by 2^60 for suffixes E
555bce308aSKevin Wolf  *              and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
565bce308aSKevin Wolf  *              2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
575bce308aSKevin Wolf  * 'T'          double
585bce308aSKevin Wolf  *              user mode accepts an optional ms, us, ns suffix,
595bce308aSKevin Wolf  *              which divides the value by 1e3, 1e6, 1e9, respectively
605bce308aSKevin Wolf  * '/'          optional gdb-like print format (like "/10x")
615bce308aSKevin Wolf  *
625bce308aSKevin Wolf  * '?'          optional type (for all types, except '/')
635bce308aSKevin Wolf  * '.'          other form of optional type (for 'i' and 'l')
645bce308aSKevin Wolf  * 'b'          boolean
655bce308aSKevin Wolf  *              user mode accepts "on" or "off"
6626fcd766SStefan Reiter  * '-'          optional parameter (eg. '-f'); if followed by a 's', it
6726fcd766SStefan Reiter  *              specifies an optional string param (e.g. '-fs' allows '-f foo')
685bce308aSKevin Wolf  *
695bce308aSKevin Wolf  */
705bce308aSKevin Wolf 
715bce308aSKevin Wolf typedef struct HMPCommand {
725bce308aSKevin Wolf     const char *name;
735bce308aSKevin Wolf     const char *args_type;
745bce308aSKevin Wolf     const char *params;
755bce308aSKevin Wolf     const char *help;
765bce308aSKevin Wolf     const char *flags; /* p=preconfig */
775bce308aSKevin Wolf     void (*cmd)(Monitor *mon, const QDict *qdict);
78f9429c67SDaniel P. Berrangé     /*
79f9429c67SDaniel P. Berrangé      * If implementing a command that takes no arguments and simply
80f9429c67SDaniel P. Berrangé      * prints formatted data, then leave @cmd NULL, and then set
81f9429c67SDaniel P. Berrangé      * @cmd_info_hrt to the corresponding QMP handler that returns
82f9429c67SDaniel P. Berrangé      * the formatted text.
83f9429c67SDaniel P. Berrangé      */
84f9429c67SDaniel P. Berrangé     HumanReadableText *(*cmd_info_hrt)(Error **errp);
85bb4b9eadSKevin Wolf     bool coroutine;
865bce308aSKevin Wolf     /*
875bce308aSKevin Wolf      * @sub_table is a list of 2nd level of commands. If it does not exist,
885bce308aSKevin Wolf      * cmd should be used. If it exists, sub_table[?].cmd should be
895bce308aSKevin Wolf      * used, and cmd of 1st level plays the role of help function.
905bce308aSKevin Wolf      */
915bce308aSKevin Wolf     struct HMPCommand *sub_table;
925bce308aSKevin Wolf     void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
935bce308aSKevin Wolf } HMPCommand;
945bce308aSKevin Wolf 
955bce308aSKevin Wolf struct Monitor {
965bce308aSKevin Wolf     CharBackend chr;
975bce308aSKevin Wolf     int suspend_cnt;            /* Needs to be accessed atomically */
9892082416SKevin Wolf     bool is_qmp;
995bce308aSKevin Wolf     bool skip_flush;
1005bce308aSKevin Wolf     bool use_io_thread;
1015bce308aSKevin Wolf 
102ddfb0baaSMarkus Armbruster     char *mon_cpu_path;
1035bce308aSKevin Wolf     QTAILQ_ENTRY(Monitor) entry;
1045bce308aSKevin Wolf 
1055bce308aSKevin Wolf     /*
1065bce308aSKevin Wolf      * The per-monitor lock. We can't access guest memory when holding
1075bce308aSKevin Wolf      * the lock.
1085bce308aSKevin Wolf      */
1095bce308aSKevin Wolf     QemuMutex mon_lock;
1105bce308aSKevin Wolf 
1115bce308aSKevin Wolf     /*
1125bce308aSKevin Wolf      * Members that are protected by the per-monitor lock
1135bce308aSKevin Wolf      */
1145bce308aSKevin Wolf     QLIST_HEAD(, mon_fd_t) fds;
11520076f4aSMarkus Armbruster     GString *outbuf;
1165bce308aSKevin Wolf     guint out_watch;
1175bce308aSKevin Wolf     int mux_out;
1186ee7c82dSPaolo Bonzini     int reset_seen;
1195bce308aSKevin Wolf };
1205bce308aSKevin Wolf 
1215bce308aSKevin Wolf struct MonitorHMP {
1225bce308aSKevin Wolf     Monitor common;
12392082416SKevin Wolf     bool use_readline;
1245bce308aSKevin Wolf     /*
1255bce308aSKevin Wolf      * State used only in the thread "owning" the monitor.
1265bce308aSKevin Wolf      * If @use_io_thread, this is @mon_iothread. (This does not actually happen
1275bce308aSKevin Wolf      * in the current state of the code.)
1285bce308aSKevin Wolf      * Else, it's the main thread.
1295bce308aSKevin Wolf      * These members can be safely accessed without locks.
1305bce308aSKevin Wolf      */
1315bce308aSKevin Wolf     ReadLineState *rs;
1325bce308aSKevin Wolf };
1335bce308aSKevin Wolf 
1345bce308aSKevin Wolf typedef struct {
1355bce308aSKevin Wolf     Monitor common;
1365bce308aSKevin Wolf     JSONMessageParser parser;
13792082416SKevin Wolf     bool pretty;
1385bce308aSKevin Wolf     /*
1395bce308aSKevin Wolf      * When a client connects, we're in capabilities negotiation mode.
1405bce308aSKevin Wolf      * @commands is &qmp_cap_negotiation_commands then.  When command
1415bce308aSKevin Wolf      * qmp_capabilities succeeds, we go into command mode, and
1425bce308aSKevin Wolf      * @command becomes &qmp_commands.
1435bce308aSKevin Wolf      */
144f0ccc00bSMarc-André Lureau     const QmpCommandList *commands;
1455bce308aSKevin Wolf     bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
1465bce308aSKevin Wolf     bool capab[QMP_CAPABILITY__MAX];         /* offered and accepted */
1475bce308aSKevin Wolf     /*
1485bce308aSKevin Wolf      * Protects qmp request/response queue.
1495bce308aSKevin Wolf      * Take monitor_lock first when you need both.
1505bce308aSKevin Wolf      */
1515bce308aSKevin Wolf     QemuMutex qmp_queue_lock;
1525bce308aSKevin Wolf     /* Input queue that holds all the parsed QMP requests */
1535bce308aSKevin Wolf     GQueue *qmp_requests;
1545bce308aSKevin Wolf } MonitorQMP;
1555bce308aSKevin Wolf 
1567e3c0deaSKevin Wolf /**
1577e3c0deaSKevin Wolf  * Is @mon a QMP monitor?
1587e3c0deaSKevin Wolf  */
monitor_is_qmp(const Monitor * mon)1597e3c0deaSKevin Wolf static inline bool monitor_is_qmp(const Monitor *mon)
1607e3c0deaSKevin Wolf {
16192082416SKevin Wolf     return mon->is_qmp;
1627e3c0deaSKevin Wolf }
1637e3c0deaSKevin Wolf 
1647e3c0deaSKevin Wolf typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList;
1657e3c0deaSKevin Wolf extern IOThread *mon_iothread;
1669ce44e2cSKevin Wolf extern Coroutine *qmp_dispatcher_co;
1679ce44e2cSKevin Wolf extern bool qmp_dispatcher_co_shutdown;
1687e3c0deaSKevin Wolf extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
1697e3c0deaSKevin Wolf extern QemuMutex monitor_lock;
1707e3c0deaSKevin Wolf extern MonitorList mon_list;
1717e3c0deaSKevin Wolf 
172ed7bda5dSKevin Wolf extern HMPCommand hmp_cmds[];
173ed7bda5dSKevin Wolf 
17492082416SKevin Wolf void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
1757e3c0deaSKevin Wolf                        bool use_io_thread);
1761d95db74SKevin Wolf void monitor_data_destroy(Monitor *mon);
1777e3c0deaSKevin Wolf int monitor_can_read(void *opaque);
1787e3c0deaSKevin Wolf void monitor_list_append(Monitor *mon);
1797e3c0deaSKevin Wolf void monitor_fdsets_cleanup(void);
1807e3c0deaSKevin Wolf 
1817e3c0deaSKevin Wolf void qmp_send_response(MonitorQMP *mon, const QDict *rsp);
1827e3c0deaSKevin Wolf void monitor_data_destroy_qmp(MonitorQMP *mon);
1839ce44e2cSKevin Wolf void coroutine_fn monitor_qmp_dispatcher_co(void *data);
1849f2d5854SPaolo Bonzini void qmp_dispatcher_co_wake(void);
1857e3c0deaSKevin Wolf 
1862fc5d01bSKevin Wolf int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
187ed7bda5dSKevin Wolf void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
188ed7bda5dSKevin Wolf int hmp_compare_cmd(const char *name, const char *list);
189ed7bda5dSKevin Wolf 
1905bce308aSKevin Wolf #endif
191