blob: 252de856812f41eb45a88866240950861b943822 [file] [log] [blame]
Kevin Wolf5bce3082019-06-13 17:33:59 +02001/*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#ifndef MONITOR_INTERNAL_H
26#define MONITOR_INTERNAL_H
27
28#include "chardev/char-fe.h"
29#include "monitor/monitor.h"
Kevin Wolffa4dcf52020-01-29 11:22:37 +010030#include "qapi/qapi-types-control.h"
Kevin Wolf5bce3082019-06-13 17:33:59 +020031#include "qapi/qmp/dispatch.h"
32#include "qapi/qmp/json-parser.h"
33#include "qemu/readline.h"
Kevin Wolf7e3c0de2019-06-13 17:34:00 +020034#include "sysemu/iothread.h"
Kevin Wolf5bce3082019-06-13 17:33:59 +020035
36/*
37 * Supported types:
38 *
39 * 'F' filename
40 * 'B' block device name
41 * 's' string (accept optional quote)
42 * 'S' it just appends the rest of the string (accept optional quote)
43 * 'O' option string of the form NAME=VALUE,...
44 * parsed according to QemuOptsList given by its name
45 * Example: 'device:O' uses qemu_device_opts.
46 * Restriction: only lists with empty desc are supported
47 * TODO lift the restriction
48 * 'i' 32 bit integer
49 * 'l' target long (32 or 64 bit)
50 * 'M' Non-negative target long (32 or 64 bit), in user mode the
51 * value is multiplied by 2^20 (think Mebibyte)
52 * 'o' octets (aka bytes)
53 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
54 * K, k suffix, which multiplies the value by 2^60 for suffixes E
55 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
56 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
57 * 'T' double
58 * user mode accepts an optional ms, us, ns suffix,
59 * which divides the value by 1e3, 1e6, 1e9, respectively
60 * '/' optional gdb-like print format (like "/10x")
61 *
62 * '?' optional type (for all types, except '/')
63 * '.' other form of optional type (for 'i' and 'l')
64 * 'b' boolean
65 * user mode accepts "on" or "off"
Stefan Reiter26fcd762022-02-25 09:49:47 +010066 * '-' optional parameter (eg. '-f'); if followed by a 's', it
67 * specifies an optional string param (e.g. '-fs' allows '-f foo')
Kevin Wolf5bce3082019-06-13 17:33:59 +020068 *
69 */
70
71typedef struct HMPCommand {
72 const char *name;
73 const char *args_type;
74 const char *params;
75 const char *help;
76 const char *flags; /* p=preconfig */
77 void (*cmd)(Monitor *mon, const QDict *qdict);
Daniel P. Berrangéf9429c62021-10-08 15:09:00 +010078 /*
79 * If implementing a command that takes no arguments and simply
80 * prints formatted data, then leave @cmd NULL, and then set
81 * @cmd_info_hrt to the corresponding QMP handler that returns
82 * the formatted text.
83 */
84 HumanReadableText *(*cmd_info_hrt)(Error **errp);
Kevin Wolfbb4b9ea2020-10-05 17:58:51 +020085 bool coroutine;
Kevin Wolf5bce3082019-06-13 17:33:59 +020086 /*
87 * @sub_table is a list of 2nd level of commands. If it does not exist,
88 * cmd should be used. If it exists, sub_table[?].cmd should be
89 * used, and cmd of 1st level plays the role of help function.
90 */
91 struct HMPCommand *sub_table;
92 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
93} HMPCommand;
94
95struct Monitor {
96 CharBackend chr;
Kevin Wolf5bce3082019-06-13 17:33:59 +020097 int suspend_cnt; /* Needs to be accessed atomically */
Kevin Wolf92082412019-06-13 17:34:03 +020098 bool is_qmp;
Kevin Wolf5bce3082019-06-13 17:33:59 +020099 bool skip_flush;
100 bool use_io_thread;
101
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +0200102 char *mon_cpu_path;
Kevin Wolf5bce3082019-06-13 17:33:59 +0200103 QTAILQ_ENTRY(Monitor) entry;
104
105 /*
106 * The per-monitor lock. We can't access guest memory when holding
107 * the lock.
108 */
109 QemuMutex mon_lock;
110
111 /*
112 * Members that are protected by the per-monitor lock
113 */
114 QLIST_HEAD(, mon_fd_t) fds;
Markus Armbruster20076f42020-12-11 18:11:34 +0100115 GString *outbuf;
Kevin Wolf5bce3082019-06-13 17:33:59 +0200116 guint out_watch;
Kevin Wolf5bce3082019-06-13 17:33:59 +0200117 int mux_out;
Paolo Bonzini6ee7c822023-03-03 13:32:13 +0100118 int reset_seen;
Kevin Wolf5bce3082019-06-13 17:33:59 +0200119};
120
121struct MonitorHMP {
122 Monitor common;
Kevin Wolf92082412019-06-13 17:34:03 +0200123 bool use_readline;
Kevin Wolf5bce3082019-06-13 17:33:59 +0200124 /*
125 * State used only in the thread "owning" the monitor.
126 * If @use_io_thread, this is @mon_iothread. (This does not actually happen
127 * in the current state of the code.)
128 * Else, it's the main thread.
129 * These members can be safely accessed without locks.
130 */
131 ReadLineState *rs;
132};
133
134typedef struct {
135 Monitor common;
136 JSONMessageParser parser;
Kevin Wolf92082412019-06-13 17:34:03 +0200137 bool pretty;
Kevin Wolf5bce3082019-06-13 17:33:59 +0200138 /*
139 * When a client connects, we're in capabilities negotiation mode.
140 * @commands is &qmp_cap_negotiation_commands then. When command
141 * qmp_capabilities succeeds, we go into command mode, and
142 * @command becomes &qmp_commands.
143 */
Marc-André Lureauf0ccc002020-03-16 18:18:24 +0100144 const QmpCommandList *commands;
Kevin Wolf5bce3082019-06-13 17:33:59 +0200145 bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
146 bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */
147 /*
148 * Protects qmp request/response queue.
149 * Take monitor_lock first when you need both.
150 */
151 QemuMutex qmp_queue_lock;
152 /* Input queue that holds all the parsed QMP requests */
153 GQueue *qmp_requests;
154} MonitorQMP;
155
Kevin Wolf7e3c0de2019-06-13 17:34:00 +0200156/**
157 * Is @mon a QMP monitor?
158 */
159static inline bool monitor_is_qmp(const Monitor *mon)
160{
Kevin Wolf92082412019-06-13 17:34:03 +0200161 return mon->is_qmp;
Kevin Wolf7e3c0de2019-06-13 17:34:00 +0200162}
163
164typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList;
165extern IOThread *mon_iothread;
Kevin Wolf9ce44e22020-10-05 17:58:50 +0200166extern Coroutine *qmp_dispatcher_co;
167extern bool qmp_dispatcher_co_shutdown;
Kevin Wolf7e3c0de2019-06-13 17:34:00 +0200168extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
169extern QemuMutex monitor_lock;
170extern MonitorList mon_list;
171extern int mon_refcount;
172
Kevin Wolfed7bda52019-06-13 17:34:01 +0200173extern HMPCommand hmp_cmds[];
174
Kevin Wolf92082412019-06-13 17:34:03 +0200175void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
Kevin Wolf7e3c0de2019-06-13 17:34:00 +0200176 bool use_io_thread);
Kevin Wolf1d95db72019-06-13 17:34:02 +0200177void monitor_data_destroy(Monitor *mon);
Kevin Wolf7e3c0de2019-06-13 17:34:00 +0200178int monitor_can_read(void *opaque);
179void monitor_list_append(Monitor *mon);
180void monitor_fdsets_cleanup(void);
181
182void qmp_send_response(MonitorQMP *mon, const QDict *rsp);
183void monitor_data_destroy_qmp(MonitorQMP *mon);
Kevin Wolf9ce44e22020-10-05 17:58:50 +0200184void coroutine_fn monitor_qmp_dispatcher_co(void *data);
Paolo Bonzini9f2d5852023-03-03 13:51:44 +0100185void qmp_dispatcher_co_wake(void);
Kevin Wolf7e3c0de2019-06-13 17:34:00 +0200186
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100187int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
Kevin Wolfed7bda52019-06-13 17:34:01 +0200188void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
189int hmp_compare_cmd(const char *name, const char *list);
190
Kevin Wolf5bce3082019-06-13 17:33:59 +0200191#endif