]>
Commit | Line | Data |
---|---|---|
9dc39cba FB |
1 | /* |
2 | * QEMU monitor | |
5fafdf24 | 3 | * |
9dc39cba | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
9dc39cba FB |
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 | */ | |
511d2b14 | 24 | #include <dirent.h> |
87ecb68b | 25 | #include "hw/hw.h" |
b4a42f81 | 26 | #include "monitor/qdev.h" |
87ecb68b PB |
27 | #include "hw/usb.h" |
28 | #include "hw/pcmcia.h" | |
0d09e41a | 29 | #include "hw/i386/pc.h" |
a2cb15b0 | 30 | #include "hw/pci/pci.h" |
0d09e41a | 31 | #include "sysemu/watchdog.h" |
45a50b16 | 32 | #include "hw/loader.h" |
022c62cb | 33 | #include "exec/gdbstub.h" |
1422e32d | 34 | #include "net/net.h" |
68ac40d2 | 35 | #include "net/slirp.h" |
dccfcd0e | 36 | #include "sysemu/char.h" |
7572150c | 37 | #include "ui/qemu-spice.h" |
9c17d615 | 38 | #include "sysemu/sysemu.h" |
83c9089e | 39 | #include "monitor/monitor.h" |
0150cd81 | 40 | #include "qemu/readline.h" |
28ecbaee | 41 | #include "ui/console.h" |
c751a74a | 42 | #include "ui/input.h" |
9c17d615 | 43 | #include "sysemu/blockdev.h" |
87ecb68b | 44 | #include "audio/audio.h" |
76cad711 | 45 | #include "disas/disas.h" |
9c17d615 | 46 | #include "sysemu/balloon.h" |
1de7afc9 | 47 | #include "qemu/timer.h" |
caf71f86 | 48 | #include "migration/migration.h" |
9c17d615 | 49 | #include "sysemu/kvm.h" |
1de7afc9 | 50 | #include "qemu/acl.h" |
bdee56f5 | 51 | #include "sysemu/tpm.h" |
7b1b5d19 PB |
52 | #include "qapi/qmp/qint.h" |
53 | #include "qapi/qmp/qfloat.h" | |
54 | #include "qapi/qmp/qlist.h" | |
55 | #include "qapi/qmp/qbool.h" | |
56 | #include "qapi/qmp/qstring.h" | |
57 | #include "qapi/qmp/qjson.h" | |
58 | #include "qapi/qmp/json-streamer.h" | |
59 | #include "qapi/qmp/json-parser.h" | |
1094fd3a | 60 | #include <qom/object_interfaces.h> |
1de7afc9 | 61 | #include "qemu/osdep.h" |
2b41f10e | 62 | #include "cpu.h" |
89bd820a | 63 | #include "trace.h" |
31965ae2 | 64 | #include "trace/control.h" |
6d8a764e | 65 | #ifdef CONFIG_TRACE_SIMPLE |
31965ae2 | 66 | #include "trace/simple.h" |
22890ab5 | 67 | #endif |
022c62cb | 68 | #include "exec/memory.h" |
f08b6170 | 69 | #include "exec/cpu_ldst.h" |
48a32bed AL |
70 | #include "qmp-commands.h" |
71 | #include "hmp.h" | |
1de7afc9 | 72 | #include "qemu/thread.h" |
b21631f3 | 73 | #include "block/qapi.h" |
43a14cfc WX |
74 | #include "qapi/qmp-event.h" |
75 | #include "qapi-event.h" | |
6a5bd307 | 76 | |
661f1929 JK |
77 | /* for pic/irq_info */ |
78 | #if defined(TARGET_SPARC) | |
0d09e41a | 79 | #include "hw/sparc/sun4m.h" |
661f1929 | 80 | #endif |
0d09e41a | 81 | #include "hw/lm32/lm32_pic.h" |
661f1929 | 82 | |
9dc39cba | 83 | //#define DEBUG |
81d0912d | 84 | //#define DEBUG_COMPLETION |
9dc39cba | 85 | |
9307c4c1 FB |
86 | /* |
87 | * Supported types: | |
5fafdf24 | 88 | * |
9307c4c1 | 89 | * 'F' filename |
81d0912d | 90 | * 'B' block device name |
9307c4c1 | 91 | * 's' string (accept optional quote) |
129be006 | 92 | * 'S' it just appends the rest of the string (accept optional quote) |
361127df MA |
93 | * 'O' option string of the form NAME=VALUE,... |
94 | * parsed according to QemuOptsList given by its name | |
95 | * Example: 'device:O' uses qemu_device_opts. | |
96 | * Restriction: only lists with empty desc are supported | |
97 | * TODO lift the restriction | |
92a31b1f FB |
98 | * 'i' 32 bit integer |
99 | * 'l' target long (32 or 64 bit) | |
91162849 LC |
100 | * 'M' Non-negative target long (32 or 64 bit), in user mode the |
101 | * value is multiplied by 2^20 (think Mebibyte) | |
dbc0c67f | 102 | * 'o' octets (aka bytes) |
5e00984a KW |
103 | * user mode accepts an optional E, e, P, p, T, t, G, g, M, m, |
104 | * K, k suffix, which multiplies the value by 2^60 for suffixes E | |
105 | * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t, | |
106 | * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k | |
fccfb11e MA |
107 | * 'T' double |
108 | * user mode accepts an optional ms, us, ns suffix, | |
109 | * which divides the value by 1e3, 1e6, 1e9, respectively | |
9307c4c1 FB |
110 | * '/' optional gdb-like print format (like "/10x") |
111 | * | |
fb46660e LC |
112 | * '?' optional type (for all types, except '/') |
113 | * '.' other form of optional type (for 'i' and 'l') | |
942cd1f2 MA |
114 | * 'b' boolean |
115 | * user mode accepts "on" or "off" | |
fb46660e | 116 | * '-' optional parameter (eg. '-f') |
9307c4c1 FB |
117 | * |
118 | */ | |
119 | ||
940cc30d AL |
120 | typedef struct MonitorCompletionData MonitorCompletionData; |
121 | struct MonitorCompletionData { | |
122 | Monitor *mon; | |
123 | void (*user_print)(Monitor *mon, const QObject *data); | |
124 | }; | |
125 | ||
c227f099 | 126 | typedef struct mon_cmd_t { |
9dc39cba | 127 | const char *name; |
9307c4c1 | 128 | const char *args_type; |
9dc39cba FB |
129 | const char *params; |
130 | const char *help; | |
a2876f59 | 131 | void (*user_print)(Monitor *mon, const QObject *data); |
910df89d | 132 | union { |
af4ce882 | 133 | void (*cmd)(Monitor *mon, const QDict *qdict); |
261394db | 134 | int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data); |
940cc30d AL |
135 | int (*cmd_async)(Monitor *mon, const QDict *params, |
136 | MonitorCompletion *cb, void *opaque); | |
910df89d | 137 | } mhandler; |
8ac470c1 | 138 | int flags; |
5f3d335f WX |
139 | /* @sub_table is a list of 2nd level of commands. If it do not exist, |
140 | * mhandler should be used. If it exist, sub_table[?].mhandler should be | |
141 | * used, and mhandler of 1st level plays the role of help function. | |
142 | */ | |
143 | struct mon_cmd_t *sub_table; | |
bfa40f77 | 144 | void (*command_completion)(ReadLineState *rs, int nb_args, const char *str); |
c227f099 | 145 | } mon_cmd_t; |
9dc39cba | 146 | |
f07918fd | 147 | /* file descriptors passed via SCM_RIGHTS */ |
c227f099 AL |
148 | typedef struct mon_fd_t mon_fd_t; |
149 | struct mon_fd_t { | |
f07918fd MM |
150 | char *name; |
151 | int fd; | |
c227f099 | 152 | QLIST_ENTRY(mon_fd_t) next; |
f07918fd MM |
153 | }; |
154 | ||
ba1c048a CB |
155 | /* file descriptor associated with a file descriptor set */ |
156 | typedef struct MonFdsetFd MonFdsetFd; | |
157 | struct MonFdsetFd { | |
158 | int fd; | |
159 | bool removed; | |
160 | char *opaque; | |
161 | QLIST_ENTRY(MonFdsetFd) next; | |
162 | }; | |
163 | ||
164 | /* file descriptor set containing fds passed via SCM_RIGHTS */ | |
165 | typedef struct MonFdset MonFdset; | |
166 | struct MonFdset { | |
167 | int64_t id; | |
168 | QLIST_HEAD(, MonFdsetFd) fds; | |
adb696f3 | 169 | QLIST_HEAD(, MonFdsetFd) dup_fds; |
ba1c048a CB |
170 | QLIST_ENTRY(MonFdset) next; |
171 | }; | |
172 | ||
5fa737a4 LC |
173 | typedef struct MonitorControl { |
174 | QObject *id; | |
175 | JSONMessageParser parser; | |
4a7e1190 | 176 | int command_mode; |
5fa737a4 LC |
177 | } MonitorControl; |
178 | ||
afeecec2 DB |
179 | /* |
180 | * To prevent flooding clients, events can be throttled. The | |
181 | * throttling is calculated globally, rather than per-Monitor | |
182 | * instance. | |
183 | */ | |
184 | typedef struct MonitorEventState { | |
185 | MonitorEvent event; /* Event being tracked */ | |
186 | int64_t rate; /* Period over which to throttle. 0 to disable */ | |
187 | int64_t last; /* Time at which event was last emitted */ | |
188 | QEMUTimer *timer; /* Timer for handling delayed events */ | |
189 | QObject *data; /* Event pending delayed dispatch */ | |
190 | } MonitorEventState; | |
191 | ||
43a14cfc WX |
192 | typedef struct MonitorQAPIEventState { |
193 | QAPIEvent event; /* Event being tracked */ | |
194 | int64_t rate; /* Minimum time (in ns) between two events */ | |
195 | int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */ | |
196 | QEMUTimer *timer; /* Timer for handling delayed events */ | |
197 | QObject *data; /* Event pending delayed dispatch */ | |
198 | } MonitorQAPIEventState; | |
199 | ||
87127161 AL |
200 | struct Monitor { |
201 | CharDriverState *chr; | |
a7aec5da GH |
202 | int mux_out; |
203 | int reset_seen; | |
731b0364 AL |
204 | int flags; |
205 | int suspend_cnt; | |
48c043d0 | 206 | bool skip_flush; |
e1f2641b | 207 | QString *outbuf; |
293d2a00 | 208 | guint watch; |
731b0364 | 209 | ReadLineState *rs; |
5fa737a4 | 210 | MonitorControl *mc; |
cb446eca | 211 | CPUState *mon_cpu; |
731b0364 AL |
212 | BlockDriverCompletionFunc *password_completion_cb; |
213 | void *password_opaque; | |
7717239d | 214 | mon_cmd_t *cmd_table; |
8204a918 | 215 | QError *error; |
c227f099 | 216 | QLIST_HEAD(,mon_fd_t) fds; |
72cf2d4f | 217 | QLIST_ENTRY(Monitor) entry; |
87127161 AL |
218 | }; |
219 | ||
2dbc8db0 LC |
220 | /* QMP checker flags */ |
221 | #define QMP_ACCEPT_UNKNOWNS 1 | |
222 | ||
72cf2d4f | 223 | static QLIST_HEAD(mon_list, Monitor) mon_list; |
ba1c048a | 224 | static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; |
efb87c16 | 225 | static int mon_refcount; |
7e2515e8 | 226 | |
816f8925 WX |
227 | static mon_cmd_t mon_cmds[]; |
228 | static mon_cmd_t info_cmds[]; | |
9dc39cba | 229 | |
f36b4afb LC |
230 | static const mon_cmd_t qmp_cmds[]; |
231 | ||
8631b608 MA |
232 | Monitor *cur_mon; |
233 | Monitor *default_mon; | |
376253ec | 234 | |
c60bf339 SH |
235 | static void monitor_command_cb(void *opaque, const char *cmdline, |
236 | void *readline_opaque); | |
83ab7950 | 237 | |
09069b19 LC |
238 | static inline int qmp_cmd_mode(const Monitor *mon) |
239 | { | |
240 | return (mon->mc ? mon->mc->command_mode : 0); | |
241 | } | |
242 | ||
418173c7 LC |
243 | /* Return true if in control mode, false otherwise */ |
244 | static inline int monitor_ctrl_mode(const Monitor *mon) | |
245 | { | |
246 | return (mon->flags & MONITOR_USE_CONTROL); | |
247 | } | |
248 | ||
6620d3ce MA |
249 | /* Return non-zero iff we have a current monitor, and it is in QMP mode. */ |
250 | int monitor_cur_is_qmp(void) | |
251 | { | |
252 | return cur_mon && monitor_ctrl_mode(cur_mon); | |
253 | } | |
254 | ||
7060b478 | 255 | void monitor_read_command(Monitor *mon, int show_prompt) |
731b0364 | 256 | { |
183e6e52 LC |
257 | if (!mon->rs) |
258 | return; | |
259 | ||
731b0364 AL |
260 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); |
261 | if (show_prompt) | |
262 | readline_show_prompt(mon->rs); | |
263 | } | |
6a00d601 | 264 | |
7060b478 AL |
265 | int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
266 | void *opaque) | |
bb5fc20f | 267 | { |
94171e11 | 268 | if (monitor_ctrl_mode(mon)) { |
ab5b027e | 269 | qerror_report(QERR_MISSING_PARAMETER, "password"); |
94171e11 LC |
270 | return -EINVAL; |
271 | } else if (mon->rs) { | |
cde76ee1 AL |
272 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); |
273 | /* prompt is printed on return from the command handler */ | |
274 | return 0; | |
275 | } else { | |
276 | monitor_printf(mon, "terminal does not support password prompting\n"); | |
277 | return -ENOTTY; | |
278 | } | |
bb5fc20f AL |
279 | } |
280 | ||
f628926b GH |
281 | static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond, |
282 | void *opaque) | |
283 | { | |
293d2a00 LE |
284 | Monitor *mon = opaque; |
285 | ||
286 | mon->watch = 0; | |
287 | monitor_flush(mon); | |
f628926b GH |
288 | return FALSE; |
289 | } | |
290 | ||
376253ec | 291 | void monitor_flush(Monitor *mon) |
7e2515e8 | 292 | { |
f628926b | 293 | int rc; |
e1f2641b LC |
294 | size_t len; |
295 | const char *buf; | |
296 | ||
48c043d0 LC |
297 | if (mon->skip_flush) { |
298 | return; | |
299 | } | |
300 | ||
e1f2641b LC |
301 | buf = qstring_get_str(mon->outbuf); |
302 | len = qstring_get_length(mon->outbuf); | |
f628926b | 303 | |
a4cc73d6 | 304 | if (len && !mon->mux_out) { |
e1f2641b | 305 | rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len); |
056f49ff SP |
306 | if ((rc < 0 && errno != EAGAIN) || (rc == len)) { |
307 | /* all flushed or error */ | |
e1f2641b LC |
308 | QDECREF(mon->outbuf); |
309 | mon->outbuf = qstring_new(); | |
f628926b GH |
310 | return; |
311 | } | |
312 | if (rc > 0) { | |
313 | /* partinal write */ | |
e1f2641b LC |
314 | QString *tmp = qstring_from_str(buf + rc); |
315 | QDECREF(mon->outbuf); | |
316 | mon->outbuf = tmp; | |
f628926b | 317 | } |
293d2a00 LE |
318 | if (mon->watch == 0) { |
319 | mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT, | |
320 | monitor_unblocked, mon); | |
321 | } | |
7e2515e8 FB |
322 | } |
323 | } | |
324 | ||
e1f2641b | 325 | /* flush at every end of line */ |
376253ec | 326 | static void monitor_puts(Monitor *mon, const char *str) |
7e2515e8 | 327 | { |
60fe76f3 | 328 | char c; |
731b0364 | 329 | |
7e2515e8 FB |
330 | for(;;) { |
331 | c = *str++; | |
332 | if (c == '\0') | |
333 | break; | |
e1f2641b LC |
334 | if (c == '\n') { |
335 | qstring_append_chr(mon->outbuf, '\r'); | |
336 | } | |
337 | qstring_append_chr(mon->outbuf, c); | |
338 | if (c == '\n') { | |
376253ec | 339 | monitor_flush(mon); |
e1f2641b | 340 | } |
7e2515e8 FB |
341 | } |
342 | } | |
343 | ||
376253ec | 344 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
9dc39cba | 345 | { |
e1f2641b | 346 | char *buf; |
b8b08266 | 347 | |
2daa1191 LC |
348 | if (!mon) |
349 | return; | |
350 | ||
b8b08266 | 351 | if (monitor_ctrl_mode(mon)) { |
b8b08266 | 352 | return; |
4a29a85d | 353 | } |
b8b08266 | 354 | |
e1f2641b | 355 | buf = g_strdup_vprintf(fmt, ap); |
b8b08266 | 356 | monitor_puts(mon, buf); |
e1f2641b | 357 | g_free(buf); |
9dc39cba FB |
358 | } |
359 | ||
376253ec | 360 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
9dc39cba | 361 | { |
7e2515e8 FB |
362 | va_list ap; |
363 | va_start(ap, fmt); | |
376253ec | 364 | monitor_vprintf(mon, fmt, ap); |
7e2515e8 | 365 | va_end(ap); |
9dc39cba FB |
366 | } |
367 | ||
8b7968f7 SW |
368 | static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream, |
369 | const char *fmt, ...) | |
7fe48483 FB |
370 | { |
371 | va_list ap; | |
372 | va_start(ap, fmt); | |
376253ec | 373 | monitor_vprintf((Monitor *)stream, fmt, ap); |
7fe48483 FB |
374 | va_end(ap); |
375 | return 0; | |
376 | } | |
377 | ||
13c7425e LC |
378 | static void monitor_user_noop(Monitor *mon, const QObject *data) { } |
379 | ||
9e80721e | 380 | static inline int handler_is_qobject(const mon_cmd_t *cmd) |
13917bee LC |
381 | { |
382 | return cmd->user_print != NULL; | |
383 | } | |
384 | ||
4903de0c | 385 | static inline bool handler_is_async(const mon_cmd_t *cmd) |
940cc30d | 386 | { |
8ac470c1 | 387 | return cmd->flags & MONITOR_CMD_ASYNC; |
940cc30d AL |
388 | } |
389 | ||
8204a918 LC |
390 | static inline int monitor_has_error(const Monitor *mon) |
391 | { | |
392 | return mon->error != NULL; | |
393 | } | |
394 | ||
9b57c02e LC |
395 | static void monitor_json_emitter(Monitor *mon, const QObject *data) |
396 | { | |
397 | QString *json; | |
398 | ||
83a27d4d LC |
399 | json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) : |
400 | qobject_to_json(data); | |
9b57c02e LC |
401 | assert(json != NULL); |
402 | ||
b8b08266 LC |
403 | qstring_append_chr(json, '\n'); |
404 | monitor_puts(mon, qstring_get_str(json)); | |
4a29a85d | 405 | |
9b57c02e LC |
406 | QDECREF(json); |
407 | } | |
408 | ||
de253f14 LC |
409 | static QDict *build_qmp_error_dict(const QError *err) |
410 | { | |
411 | QObject *obj; | |
412 | ||
413 | obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }", | |
414 | ErrorClass_lookup[err->err_class], | |
415 | qerror_human(err)); | |
416 | ||
417 | return qobject_to_qdict(obj); | |
418 | } | |
419 | ||
25b422eb LC |
420 | static void monitor_protocol_emitter(Monitor *mon, QObject *data) |
421 | { | |
422 | QDict *qmp; | |
423 | ||
89bd820a SH |
424 | trace_monitor_protocol_emitter(mon); |
425 | ||
25b422eb LC |
426 | if (!monitor_has_error(mon)) { |
427 | /* success response */ | |
de253f14 | 428 | qmp = qdict_new(); |
25b422eb LC |
429 | if (data) { |
430 | qobject_incref(data); | |
431 | qdict_put_obj(qmp, "return", data); | |
432 | } else { | |
0abc6579 LC |
433 | /* return an empty QDict by default */ |
434 | qdict_put(qmp, "return", qdict_new()); | |
25b422eb LC |
435 | } |
436 | } else { | |
437 | /* error response */ | |
de253f14 | 438 | qmp = build_qmp_error_dict(mon->error); |
25b422eb LC |
439 | QDECREF(mon->error); |
440 | mon->error = NULL; | |
441 | } | |
442 | ||
5fa737a4 LC |
443 | if (mon->mc->id) { |
444 | qdict_put_obj(qmp, "id", mon->mc->id); | |
445 | mon->mc->id = NULL; | |
446 | } | |
447 | ||
25b422eb LC |
448 | monitor_json_emitter(mon, QOBJECT(qmp)); |
449 | QDECREF(qmp); | |
450 | } | |
451 | ||
0d1ea871 LC |
452 | static void timestamp_put(QDict *qdict) |
453 | { | |
454 | int err; | |
455 | QObject *obj; | |
d08d6f04 | 456 | qemu_timeval tv; |
0d1ea871 | 457 | |
d08d6f04 | 458 | err = qemu_gettimeofday(&tv); |
0d1ea871 LC |
459 | if (err < 0) |
460 | return; | |
461 | ||
462 | obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", " | |
463 | "'microseconds': %" PRId64 " }", | |
464 | (int64_t) tv.tv_sec, (int64_t) tv.tv_usec); | |
0d1ea871 LC |
465 | qdict_put_obj(qdict, "timestamp", obj); |
466 | } | |
467 | ||
4860853d DB |
468 | |
469 | static const char *monitor_event_names[] = { | |
470 | [QEVENT_SHUTDOWN] = "SHUTDOWN", | |
471 | [QEVENT_RESET] = "RESET", | |
472 | [QEVENT_POWERDOWN] = "POWERDOWN", | |
473 | [QEVENT_STOP] = "STOP", | |
474 | [QEVENT_RESUME] = "RESUME", | |
475 | [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED", | |
476 | [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED", | |
477 | [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED", | |
478 | [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR", | |
479 | [QEVENT_RTC_CHANGE] = "RTC_CHANGE", | |
480 | [QEVENT_WATCHDOG] = "WATCHDOG", | |
481 | [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED", | |
482 | [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED", | |
483 | [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED", | |
484 | [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED", | |
485 | [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED", | |
32c81a4a | 486 | [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR", |
a66a2a36 | 487 | [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY", |
0402a5d6 | 488 | [QEVENT_DEVICE_DELETED] = "DEVICE_DELETED", |
4860853d | 489 | [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED", |
b1be4280 | 490 | [QEVENT_NIC_RX_FILTER_CHANGED] = "NIC_RX_FILTER_CHANGED", |
4860853d | 491 | [QEVENT_SUSPEND] = "SUSPEND", |
25df49f6 | 492 | [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK", |
4860853d | 493 | [QEVENT_WAKEUP] = "WAKEUP", |
973603a8 | 494 | [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE", |
2fdd16e2 | 495 | [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED", |
c401a8a5 | 496 | [QEVENT_GUEST_PANICKED] = "GUEST_PANICKED", |
a40f1c2a | 497 | [QEVENT_BLOCK_IMAGE_CORRUPTED] = "BLOCK_IMAGE_CORRUPTED", |
95c6bff3 BC |
498 | [QEVENT_QUORUM_FAILURE] = "QUORUM_FAILURE", |
499 | [QEVENT_QUORUM_REPORT_BAD] = "QUORUM_REPORT_BAD", | |
4860853d DB |
500 | }; |
501 | QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX) | |
502 | ||
79f32024 | 503 | static MonitorEventState monitor_event_state[QEVENT_MAX]; |
43a14cfc WX |
504 | static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX]; |
505 | ||
506 | /* | |
507 | * Emits the event to every monitor instance, @event is only used for trace | |
508 | */ | |
509 | static void monitor_qapi_event_emit(QAPIEvent event, QObject *data) | |
510 | { | |
511 | Monitor *mon; | |
512 | ||
513 | trace_monitor_protocol_event_emit(event, data); | |
514 | QLIST_FOREACH(mon, &mon_list, entry) { | |
515 | if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) { | |
516 | monitor_json_emitter(mon, data); | |
517 | } | |
518 | } | |
519 | } | |
520 | ||
521 | /* | |
522 | * Queue a new event for emission to Monitor instances, | |
523 | * applying any rate limiting if required. | |
524 | */ | |
525 | static void | |
526 | monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp) | |
527 | { | |
528 | MonitorQAPIEventState *evstate; | |
529 | assert(event < QAPI_EVENT_MAX); | |
530 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
531 | ||
532 | evstate = &(monitor_qapi_event_state[event]); | |
533 | trace_monitor_protocol_event_queue(event, | |
534 | data, | |
535 | evstate->rate, | |
536 | evstate->last, | |
537 | now); | |
538 | ||
539 | /* Rate limit of 0 indicates no throttling */ | |
540 | if (!evstate->rate) { | |
541 | monitor_qapi_event_emit(event, QOBJECT(data)); | |
542 | evstate->last = now; | |
543 | } else { | |
544 | int64_t delta = now - evstate->last; | |
545 | if (evstate->data || | |
546 | delta < evstate->rate) { | |
547 | /* If there's an existing event pending, replace | |
548 | * it with the new event, otherwise schedule a | |
549 | * timer for delayed emission | |
550 | */ | |
551 | if (evstate->data) { | |
552 | qobject_decref(evstate->data); | |
553 | } else { | |
554 | int64_t then = evstate->last + evstate->rate; | |
555 | timer_mod_ns(evstate->timer, then); | |
556 | } | |
557 | evstate->data = QOBJECT(data); | |
558 | qobject_incref(evstate->data); | |
559 | } else { | |
560 | monitor_qapi_event_emit(event, QOBJECT(data)); | |
561 | evstate->last = now; | |
562 | } | |
563 | } | |
564 | } | |
565 | ||
566 | /* | |
567 | * The callback invoked by QemuTimer when a delayed | |
568 | * event is ready to be emitted | |
569 | */ | |
570 | static void monitor_qapi_event_handler(void *opaque) | |
571 | { | |
572 | MonitorQAPIEventState *evstate = opaque; | |
573 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
574 | ||
575 | trace_monitor_protocol_event_handler(evstate->event, | |
576 | evstate->data, | |
577 | evstate->last, | |
578 | now); | |
579 | if (evstate->data) { | |
580 | monitor_qapi_event_emit(evstate->event, evstate->data); | |
581 | qobject_decref(evstate->data); | |
582 | evstate->data = NULL; | |
583 | } | |
584 | evstate->last = now; | |
585 | } | |
586 | ||
587 | /* | |
588 | * @event: the event ID to be limited | |
589 | * @rate: the rate limit in milliseconds | |
590 | * | |
591 | * Sets a rate limit on a particular event, so no | |
592 | * more than 1 event will be emitted within @rate | |
593 | * milliseconds | |
594 | */ | |
595 | static void __attribute__((__unused__)) | |
596 | monitor_qapi_event_throttle(QAPIEvent event, int64_t rate) | |
597 | { | |
598 | MonitorQAPIEventState *evstate; | |
599 | assert(event < QAPI_EVENT_MAX); | |
600 | ||
601 | evstate = &(monitor_qapi_event_state[event]); | |
602 | ||
603 | trace_monitor_protocol_event_throttle(event, rate); | |
604 | evstate->event = event; | |
605 | evstate->rate = rate * SCALE_MS; | |
606 | evstate->last = 0; | |
607 | evstate->data = NULL; | |
608 | evstate->timer = timer_new(QEMU_CLOCK_REALTIME, | |
609 | SCALE_MS, | |
610 | monitor_qapi_event_handler, | |
611 | evstate); | |
612 | } | |
613 | ||
614 | static void monitor_qapi_event_init(void) | |
615 | { | |
e010ad8f WX |
616 | /* Limit guest-triggerable events to 1 per second */ |
617 | monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000); | |
99eaf09c | 618 | monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000); |
e010ad8f | 619 | |
43a14cfc WX |
620 | qmp_event_set_func_emit(monitor_qapi_event_queue); |
621 | } | |
622 | ||
afeecec2 DB |
623 | |
624 | /* | |
625 | * Emits the event to every monitor instance | |
626 | */ | |
627 | static void | |
628 | monitor_protocol_event_emit(MonitorEvent event, | |
629 | QObject *data) | |
630 | { | |
631 | Monitor *mon; | |
632 | ||
633 | trace_monitor_protocol_event_emit(event, data); | |
634 | QLIST_FOREACH(mon, &mon_list, entry) { | |
635 | if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) { | |
636 | monitor_json_emitter(mon, data); | |
637 | } | |
638 | } | |
639 | } | |
640 | ||
641 | ||
642 | /* | |
643 | * Queue a new event for emission to Monitor instances, | |
644 | * applying any rate limiting if required. | |
645 | */ | |
646 | static void | |
647 | monitor_protocol_event_queue(MonitorEvent event, | |
648 | QObject *data) | |
649 | { | |
650 | MonitorEventState *evstate; | |
bc72ad67 | 651 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
afeecec2 DB |
652 | assert(event < QEVENT_MAX); |
653 | ||
afeecec2 DB |
654 | evstate = &(monitor_event_state[event]); |
655 | trace_monitor_protocol_event_queue(event, | |
656 | data, | |
657 | evstate->rate, | |
658 | evstate->last, | |
659 | now); | |
660 | ||
661 | /* Rate limit of 0 indicates no throttling */ | |
662 | if (!evstate->rate) { | |
663 | monitor_protocol_event_emit(event, data); | |
664 | evstate->last = now; | |
665 | } else { | |
666 | int64_t delta = now - evstate->last; | |
667 | if (evstate->data || | |
668 | delta < evstate->rate) { | |
669 | /* If there's an existing event pending, replace | |
670 | * it with the new event, otherwise schedule a | |
671 | * timer for delayed emission | |
672 | */ | |
673 | if (evstate->data) { | |
674 | qobject_decref(evstate->data); | |
675 | } else { | |
676 | int64_t then = evstate->last + evstate->rate; | |
bc72ad67 | 677 | timer_mod_ns(evstate->timer, then); |
afeecec2 DB |
678 | } |
679 | evstate->data = data; | |
680 | qobject_incref(evstate->data); | |
681 | } else { | |
682 | monitor_protocol_event_emit(event, data); | |
683 | evstate->last = now; | |
684 | } | |
685 | } | |
afeecec2 DB |
686 | } |
687 | ||
688 | ||
689 | /* | |
690 | * The callback invoked by QemuTimer when a delayed | |
691 | * event is ready to be emitted | |
692 | */ | |
693 | static void monitor_protocol_event_handler(void *opaque) | |
694 | { | |
695 | MonitorEventState *evstate = opaque; | |
bc72ad67 | 696 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
afeecec2 | 697 | |
afeecec2 DB |
698 | |
699 | trace_monitor_protocol_event_handler(evstate->event, | |
700 | evstate->data, | |
701 | evstate->last, | |
702 | now); | |
703 | if (evstate->data) { | |
704 | monitor_protocol_event_emit(evstate->event, evstate->data); | |
705 | qobject_decref(evstate->data); | |
706 | evstate->data = NULL; | |
707 | } | |
708 | evstate->last = now; | |
afeecec2 DB |
709 | } |
710 | ||
711 | ||
712 | /* | |
713 | * @event: the event ID to be limited | |
714 | * @rate: the rate limit in milliseconds | |
715 | * | |
716 | * Sets a rate limit on a particular event, so no | |
717 | * more than 1 event will be emitted within @rate | |
718 | * milliseconds | |
719 | */ | |
43a14cfc | 720 | static void __attribute__((__unused__)) |
afeecec2 DB |
721 | monitor_protocol_event_throttle(MonitorEvent event, |
722 | int64_t rate) | |
723 | { | |
724 | MonitorEventState *evstate; | |
725 | assert(event < QEVENT_MAX); | |
726 | ||
727 | evstate = &(monitor_event_state[event]); | |
728 | ||
729 | trace_monitor_protocol_event_throttle(event, rate); | |
730 | evstate->event = event; | |
731 | evstate->rate = rate * SCALE_MS; | |
bc72ad67 | 732 | evstate->timer = timer_new(QEMU_CLOCK_REALTIME, |
afeecec2 DB |
733 | SCALE_MS, |
734 | monitor_protocol_event_handler, | |
735 | evstate); | |
736 | evstate->last = 0; | |
737 | evstate->data = NULL; | |
738 | } | |
739 | ||
740 | ||
741 | /* Global, one-time initializer to configure the rate limiting | |
742 | * and initialize state */ | |
743 | static void monitor_protocol_event_init(void) | |
744 | { | |
afeecec2 | 745 | /* Limit RTC & BALLOON events to 1 per second */ |
afeecec2 | 746 | monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000); |
c88a1de5 BC |
747 | /* limit the rate of quorum events to avoid hammering the management */ |
748 | monitor_protocol_event_throttle(QEVENT_QUORUM_REPORT_BAD, 1000); | |
749 | monitor_protocol_event_throttle(QEVENT_QUORUM_FAILURE, 1000); | |
afeecec2 DB |
750 | } |
751 | ||
0d1ea871 LC |
752 | /** |
753 | * monitor_protocol_event(): Generate a Monitor event | |
754 | * | |
755 | * Event-specific data can be emitted through the (optional) 'data' parameter. | |
756 | */ | |
757 | void monitor_protocol_event(MonitorEvent event, QObject *data) | |
758 | { | |
759 | QDict *qmp; | |
760 | const char *event_name; | |
0d1ea871 | 761 | |
242cd003 | 762 | assert(event < QEVENT_MAX); |
0d1ea871 | 763 | |
4860853d DB |
764 | event_name = monitor_event_names[event]; |
765 | assert(event_name != NULL); | |
0d1ea871 LC |
766 | |
767 | qmp = qdict_new(); | |
768 | timestamp_put(qmp); | |
769 | qdict_put(qmp, "event", qstring_from_str(event_name)); | |
3d72f9a2 LC |
770 | if (data) { |
771 | qobject_incref(data); | |
0d1ea871 | 772 | qdict_put_obj(qmp, "data", data); |
3d72f9a2 | 773 | } |
0d1ea871 | 774 | |
afeecec2 DB |
775 | trace_monitor_protocol_event(event, event_name, qmp); |
776 | monitor_protocol_event_queue(event, QOBJECT(qmp)); | |
0d1ea871 LC |
777 | QDECREF(qmp); |
778 | } | |
779 | ||
ef4b7eee LC |
780 | static int do_qmp_capabilities(Monitor *mon, const QDict *params, |
781 | QObject **ret_data) | |
4a7e1190 LC |
782 | { |
783 | /* Will setup QMP capabilities in the future */ | |
784 | if (monitor_ctrl_mode(mon)) { | |
785 | mon->mc->command_mode = 1; | |
786 | } | |
ef4b7eee LC |
787 | |
788 | return 0; | |
4a7e1190 LC |
789 | } |
790 | ||
0268d97c LC |
791 | static void handle_user_command(Monitor *mon, const char *cmdline); |
792 | ||
b01fe89e WX |
793 | static void monitor_data_init(Monitor *mon) |
794 | { | |
795 | memset(mon, 0, sizeof(Monitor)); | |
796 | mon->outbuf = qstring_new(); | |
7717239d WX |
797 | /* Use *mon_cmds by default. */ |
798 | mon->cmd_table = mon_cmds; | |
b01fe89e WX |
799 | } |
800 | ||
801 | static void monitor_data_destroy(Monitor *mon) | |
802 | { | |
803 | QDECREF(mon->outbuf); | |
804 | } | |
805 | ||
d51a67b4 LC |
806 | char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, |
807 | int64_t cpu_index, Error **errp) | |
0268d97c | 808 | { |
d51a67b4 | 809 | char *output = NULL; |
0268d97c | 810 | Monitor *old_mon, hmp; |
0268d97c | 811 | |
b01fe89e | 812 | monitor_data_init(&hmp); |
48c043d0 | 813 | hmp.skip_flush = true; |
0268d97c LC |
814 | |
815 | old_mon = cur_mon; | |
816 | cur_mon = &hmp; | |
817 | ||
d51a67b4 LC |
818 | if (has_cpu_index) { |
819 | int ret = monitor_set_cpu(cpu_index); | |
0268d97c LC |
820 | if (ret < 0) { |
821 | cur_mon = old_mon; | |
d51a67b4 LC |
822 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
823 | "a CPU number"); | |
0268d97c LC |
824 | goto out; |
825 | } | |
826 | } | |
827 | ||
d51a67b4 | 828 | handle_user_command(&hmp, command_line); |
0268d97c LC |
829 | cur_mon = old_mon; |
830 | ||
48c043d0 LC |
831 | if (qstring_get_length(hmp.outbuf) > 0) { |
832 | output = g_strdup(qstring_get_str(hmp.outbuf)); | |
d51a67b4 LC |
833 | } else { |
834 | output = g_strdup(""); | |
0268d97c LC |
835 | } |
836 | ||
837 | out: | |
b01fe89e | 838 | monitor_data_destroy(&hmp); |
d51a67b4 | 839 | return output; |
0268d97c LC |
840 | } |
841 | ||
9dc39cba FB |
842 | static int compare_cmd(const char *name, const char *list) |
843 | { | |
844 | const char *p, *pstart; | |
845 | int len; | |
846 | len = strlen(name); | |
847 | p = list; | |
848 | for(;;) { | |
849 | pstart = p; | |
850 | p = strchr(p, '|'); | |
851 | if (!p) | |
852 | p = pstart + strlen(pstart); | |
853 | if ((p - pstart) == len && !memcmp(pstart, name, len)) | |
854 | return 1; | |
855 | if (*p == '\0') | |
856 | break; | |
857 | p++; | |
858 | } | |
859 | return 0; | |
860 | } | |
861 | ||
f5438c05 WX |
862 | static int get_str(char *buf, int buf_size, const char **pp) |
863 | { | |
864 | const char *p; | |
865 | char *q; | |
866 | int c; | |
867 | ||
868 | q = buf; | |
869 | p = *pp; | |
870 | while (qemu_isspace(*p)) { | |
871 | p++; | |
872 | } | |
873 | if (*p == '\0') { | |
874 | fail: | |
875 | *q = '\0'; | |
876 | *pp = p; | |
877 | return -1; | |
878 | } | |
879 | if (*p == '\"') { | |
880 | p++; | |
881 | while (*p != '\0' && *p != '\"') { | |
882 | if (*p == '\\') { | |
883 | p++; | |
884 | c = *p++; | |
885 | switch (c) { | |
886 | case 'n': | |
887 | c = '\n'; | |
888 | break; | |
889 | case 'r': | |
890 | c = '\r'; | |
891 | break; | |
892 | case '\\': | |
893 | case '\'': | |
894 | case '\"': | |
895 | break; | |
896 | default: | |
897 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
898 | goto fail; | |
899 | } | |
900 | if ((q - buf) < buf_size - 1) { | |
901 | *q++ = c; | |
902 | } | |
903 | } else { | |
904 | if ((q - buf) < buf_size - 1) { | |
905 | *q++ = *p; | |
906 | } | |
907 | p++; | |
908 | } | |
909 | } | |
910 | if (*p != '\"') { | |
911 | qemu_printf("unterminated string\n"); | |
912 | goto fail; | |
913 | } | |
914 | p++; | |
915 | } else { | |
916 | while (*p != '\0' && !qemu_isspace(*p)) { | |
917 | if ((q - buf) < buf_size - 1) { | |
918 | *q++ = *p; | |
919 | } | |
920 | p++; | |
921 | } | |
922 | } | |
923 | *q = '\0'; | |
924 | *pp = p; | |
925 | return 0; | |
926 | } | |
927 | ||
928 | #define MAX_ARGS 16 | |
929 | ||
dcc70cdf WX |
930 | static void free_cmdline_args(char **args, int nb_args) |
931 | { | |
932 | int i; | |
933 | ||
934 | assert(nb_args <= MAX_ARGS); | |
935 | ||
936 | for (i = 0; i < nb_args; i++) { | |
937 | g_free(args[i]); | |
938 | } | |
939 | ||
940 | } | |
941 | ||
942 | /* | |
943 | * Parse the command line to get valid args. | |
944 | * @cmdline: command line to be parsed. | |
945 | * @pnb_args: location to store the number of args, must NOT be NULL. | |
946 | * @args: location to store the args, which should be freed by caller, must | |
947 | * NOT be NULL. | |
948 | * | |
949 | * Returns 0 on success, negative on failure. | |
950 | * | |
951 | * NOTE: this parser is an approximate form of the real command parser. Number | |
952 | * of args have a limit of MAX_ARGS. If cmdline contains more, it will | |
953 | * return with failure. | |
954 | */ | |
955 | static int parse_cmdline(const char *cmdline, | |
956 | int *pnb_args, char **args) | |
f5438c05 WX |
957 | { |
958 | const char *p; | |
959 | int nb_args, ret; | |
960 | char buf[1024]; | |
961 | ||
962 | p = cmdline; | |
963 | nb_args = 0; | |
964 | for (;;) { | |
965 | while (qemu_isspace(*p)) { | |
966 | p++; | |
967 | } | |
968 | if (*p == '\0') { | |
969 | break; | |
970 | } | |
971 | if (nb_args >= MAX_ARGS) { | |
dcc70cdf | 972 | goto fail; |
f5438c05 WX |
973 | } |
974 | ret = get_str(buf, sizeof(buf), &p); | |
f5438c05 | 975 | if (ret < 0) { |
dcc70cdf | 976 | goto fail; |
f5438c05 | 977 | } |
dcc70cdf WX |
978 | args[nb_args] = g_strdup(buf); |
979 | nb_args++; | |
f5438c05 WX |
980 | } |
981 | *pnb_args = nb_args; | |
dcc70cdf WX |
982 | return 0; |
983 | ||
984 | fail: | |
985 | free_cmdline_args(args, nb_args); | |
986 | return -1; | |
f5438c05 WX |
987 | } |
988 | ||
66855495 WX |
989 | static void help_cmd_dump_one(Monitor *mon, |
990 | const mon_cmd_t *cmd, | |
991 | char **prefix_args, | |
992 | int prefix_args_nb) | |
993 | { | |
994 | int i; | |
995 | ||
996 | for (i = 0; i < prefix_args_nb; i++) { | |
997 | monitor_printf(mon, "%s ", prefix_args[i]); | |
998 | } | |
999 | monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help); | |
1000 | } | |
1001 | ||
1002 | /* @args[@arg_index] is the valid command need to find in @cmds */ | |
c227f099 | 1003 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
66855495 | 1004 | char **args, int nb_args, int arg_index) |
9dc39cba | 1005 | { |
c227f099 | 1006 | const mon_cmd_t *cmd; |
9dc39cba | 1007 | |
66855495 WX |
1008 | /* No valid arg need to compare with, dump all in *cmds */ |
1009 | if (arg_index >= nb_args) { | |
1010 | for (cmd = cmds; cmd->name != NULL; cmd++) { | |
1011 | help_cmd_dump_one(mon, cmd, args, arg_index); | |
1012 | } | |
1013 | return; | |
1014 | } | |
1015 | ||
1016 | /* Find one entry to dump */ | |
1017 | for (cmd = cmds; cmd->name != NULL; cmd++) { | |
1018 | if (compare_cmd(args[arg_index], cmd->name)) { | |
1019 | if (cmd->sub_table) { | |
1020 | /* continue with next arg */ | |
1021 | help_cmd_dump(mon, cmd->sub_table, | |
1022 | args, nb_args, arg_index + 1); | |
1023 | } else { | |
1024 | help_cmd_dump_one(mon, cmd, args, arg_index); | |
1025 | } | |
1026 | break; | |
1027 | } | |
9dc39cba FB |
1028 | } |
1029 | } | |
1030 | ||
376253ec | 1031 | static void help_cmd(Monitor *mon, const char *name) |
9dc39cba | 1032 | { |
66855495 WX |
1033 | char *args[MAX_ARGS]; |
1034 | int nb_args = 0; | |
1035 | ||
1036 | /* 1. parse user input */ | |
1037 | if (name) { | |
1038 | /* special case for log, directly dump and return */ | |
1039 | if (!strcmp(name, "log")) { | |
38dad9e5 | 1040 | const QEMULogItem *item; |
376253ec AL |
1041 | monitor_printf(mon, "Log items (comma separated):\n"); |
1042 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); | |
38dad9e5 | 1043 | for (item = qemu_log_items; item->mask != 0; item++) { |
376253ec | 1044 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
f193c797 | 1045 | } |
66855495 WX |
1046 | return; |
1047 | } | |
1048 | ||
1049 | if (parse_cmdline(name, &nb_args, args) < 0) { | |
1050 | return; | |
f193c797 | 1051 | } |
9dc39cba | 1052 | } |
66855495 WX |
1053 | |
1054 | /* 2. dump the contents according to parsed args */ | |
1055 | help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0); | |
1056 | ||
1057 | free_cmdline_args(args, nb_args); | |
9dc39cba FB |
1058 | } |
1059 | ||
d54908a5 | 1060 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
38183186 | 1061 | { |
d54908a5 | 1062 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
38183186 LC |
1063 | } |
1064 | ||
fc764105 | 1065 | static void do_trace_event_set_state(Monitor *mon, const QDict *qdict) |
22890ab5 PS |
1066 | { |
1067 | const char *tp_name = qdict_get_str(qdict, "name"); | |
1068 | bool new_state = qdict_get_bool(qdict, "option"); | |
f871d689 | 1069 | |
ca285c3f LV |
1070 | bool found = false; |
1071 | TraceEvent *ev = NULL; | |
1072 | while ((ev = trace_event_pattern(tp_name, ev)) != NULL) { | |
1073 | found = true; | |
1074 | if (!trace_event_get_state_static(ev)) { | |
1075 | monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name); | |
1076 | } else { | |
1077 | trace_event_set_state_dynamic(ev, new_state); | |
1078 | } | |
1079 | } | |
1080 | if (!trace_event_is_pattern(tp_name) && !found) { | |
f871d689 BS |
1081 | monitor_printf(mon, "unknown event name \"%s\"\n", tp_name); |
1082 | } | |
22890ab5 | 1083 | } |
c5ceb523 | 1084 | |
c45a8168 | 1085 | #ifdef CONFIG_TRACE_SIMPLE |
c5ceb523 SH |
1086 | static void do_trace_file(Monitor *mon, const QDict *qdict) |
1087 | { | |
1088 | const char *op = qdict_get_try_str(qdict, "op"); | |
1089 | const char *arg = qdict_get_try_str(qdict, "arg"); | |
1090 | ||
1091 | if (!op) { | |
1092 | st_print_trace_file_status((FILE *)mon, &monitor_fprintf); | |
1093 | } else if (!strcmp(op, "on")) { | |
1094 | st_set_trace_file_enabled(true); | |
1095 | } else if (!strcmp(op, "off")) { | |
1096 | st_set_trace_file_enabled(false); | |
1097 | } else if (!strcmp(op, "flush")) { | |
1098 | st_flush_trace_buffer(); | |
1099 | } else if (!strcmp(op, "set")) { | |
1100 | if (arg) { | |
1101 | st_set_trace_file(arg); | |
1102 | } | |
1103 | } else { | |
1104 | monitor_printf(mon, "unexpected argument \"%s\"\n", op); | |
1105 | help_cmd(mon, "trace-file"); | |
1106 | } | |
1107 | } | |
22890ab5 PS |
1108 | #endif |
1109 | ||
940cc30d AL |
1110 | static void user_monitor_complete(void *opaque, QObject *ret_data) |
1111 | { | |
1112 | MonitorCompletionData *data = (MonitorCompletionData *)opaque; | |
1113 | ||
1114 | if (ret_data) { | |
1115 | data->user_print(data->mon, ret_data); | |
1116 | } | |
1117 | monitor_resume(data->mon); | |
7267c094 | 1118 | g_free(data); |
940cc30d AL |
1119 | } |
1120 | ||
1121 | static void qmp_monitor_complete(void *opaque, QObject *ret_data) | |
1122 | { | |
1123 | monitor_protocol_emitter(opaque, ret_data); | |
1124 | } | |
1125 | ||
5af7bbae LC |
1126 | static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd, |
1127 | const QDict *params) | |
940cc30d | 1128 | { |
5af7bbae | 1129 | return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon); |
940cc30d AL |
1130 | } |
1131 | ||
940cc30d AL |
1132 | static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd, |
1133 | const QDict *params) | |
1134 | { | |
1135 | int ret; | |
1136 | ||
7267c094 | 1137 | MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data)); |
940cc30d AL |
1138 | cb_data->mon = mon; |
1139 | cb_data->user_print = cmd->user_print; | |
1140 | monitor_suspend(mon); | |
1141 | ret = cmd->mhandler.cmd_async(mon, params, | |
1142 | user_monitor_complete, cb_data); | |
1143 | if (ret < 0) { | |
1144 | monitor_resume(mon); | |
7267c094 | 1145 | g_free(cb_data); |
940cc30d AL |
1146 | } |
1147 | } | |
1148 | ||
84c44613 | 1149 | static void do_info_help(Monitor *mon, const QDict *qdict) |
9dc39cba | 1150 | { |
13c7425e | 1151 | help_cmd(mon, "info"); |
9dc39cba FB |
1152 | } |
1153 | ||
aa9b79bc | 1154 | CommandInfoList *qmp_query_commands(Error **errp) |
e3bba9d0 | 1155 | { |
aa9b79bc | 1156 | CommandInfoList *info, *cmd_list = NULL; |
e3bba9d0 LC |
1157 | const mon_cmd_t *cmd; |
1158 | ||
f36b4afb | 1159 | for (cmd = qmp_cmds; cmd->name != NULL; cmd++) { |
40e5a01d LC |
1160 | info = g_malloc0(sizeof(*info)); |
1161 | info->value = g_malloc0(sizeof(*info->value)); | |
1162 | info->value->name = g_strdup(cmd->name); | |
e3bba9d0 | 1163 | |
aa9b79bc LC |
1164 | info->next = cmd_list; |
1165 | cmd_list = info; | |
e3bba9d0 LC |
1166 | } |
1167 | ||
aa9b79bc | 1168 | return cmd_list; |
a36e69dd TS |
1169 | } |
1170 | ||
4860853d DB |
1171 | EventInfoList *qmp_query_events(Error **errp) |
1172 | { | |
1173 | EventInfoList *info, *ev_list = NULL; | |
1174 | MonitorEvent e; | |
1175 | ||
1176 | for (e = 0 ; e < QEVENT_MAX ; e++) { | |
1177 | const char *event_name = monitor_event_names[e]; | |
1178 | assert(event_name != NULL); | |
1179 | info = g_malloc0(sizeof(*info)); | |
1180 | info->value = g_malloc0(sizeof(*info->value)); | |
1181 | info->value->name = g_strdup(event_name); | |
1182 | ||
1183 | info->next = ev_list; | |
1184 | ev_list = info; | |
1185 | } | |
1186 | ||
1187 | return ev_list; | |
1188 | } | |
1189 | ||
b025c8b4 LC |
1190 | /* set the current CPU defined by the user */ |
1191 | int monitor_set_cpu(int cpu_index) | |
6a00d601 | 1192 | { |
55e5c285 | 1193 | CPUState *cpu; |
6a00d601 | 1194 | |
1c8bb3cc AF |
1195 | cpu = qemu_get_cpu(cpu_index); |
1196 | if (cpu == NULL) { | |
1197 | return -1; | |
6a00d601 | 1198 | } |
cb446eca | 1199 | cur_mon->mon_cpu = cpu; |
1c8bb3cc | 1200 | return 0; |
6a00d601 FB |
1201 | } |
1202 | ||
9349b4f9 | 1203 | static CPUArchState *mon_get_cpu(void) |
6a00d601 | 1204 | { |
731b0364 | 1205 | if (!cur_mon->mon_cpu) { |
b025c8b4 | 1206 | monitor_set_cpu(0); |
6a00d601 | 1207 | } |
4c0960c0 | 1208 | cpu_synchronize_state(cur_mon->mon_cpu); |
cb446eca | 1209 | return cur_mon->mon_cpu->env_ptr; |
6a00d601 FB |
1210 | } |
1211 | ||
99b7796f LC |
1212 | int monitor_get_cpu_index(void) |
1213 | { | |
55e5c285 AF |
1214 | CPUState *cpu = ENV_GET_CPU(mon_get_cpu()); |
1215 | return cpu->cpu_index; | |
99b7796f LC |
1216 | } |
1217 | ||
84f2d0ea | 1218 | static void do_info_registers(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1219 | { |
878096ee | 1220 | CPUState *cpu; |
9349b4f9 | 1221 | CPUArchState *env; |
6a00d601 | 1222 | env = mon_get_cpu(); |
878096ee AF |
1223 | cpu = ENV_GET_CPU(env); |
1224 | cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU); | |
9307c4c1 FB |
1225 | } |
1226 | ||
84f2d0ea | 1227 | static void do_info_jit(Monitor *mon, const QDict *qdict) |
e3db7226 | 1228 | { |
376253ec | 1229 | dump_exec_info((FILE *)mon, monitor_fprintf); |
e3db7226 FB |
1230 | } |
1231 | ||
84f2d0ea | 1232 | static void do_info_history(Monitor *mon, const QDict *qdict) |
aa455485 FB |
1233 | { |
1234 | int i; | |
7e2515e8 | 1235 | const char *str; |
3b46e624 | 1236 | |
cde76ee1 AL |
1237 | if (!mon->rs) |
1238 | return; | |
7e2515e8 FB |
1239 | i = 0; |
1240 | for(;;) { | |
731b0364 | 1241 | str = readline_get_history(mon->rs, i); |
7e2515e8 FB |
1242 | if (!str) |
1243 | break; | |
376253ec | 1244 | monitor_printf(mon, "%d: '%s'\n", i, str); |
8e3a9fd2 | 1245 | i++; |
aa455485 FB |
1246 | } |
1247 | } | |
1248 | ||
84f2d0ea | 1249 | static void do_info_cpu_stats(Monitor *mon, const QDict *qdict) |
76a66253 | 1250 | { |
878096ee | 1251 | CPUState *cpu; |
9349b4f9 | 1252 | CPUArchState *env; |
76a66253 JM |
1253 | |
1254 | env = mon_get_cpu(); | |
878096ee AF |
1255 | cpu = ENV_GET_CPU(env); |
1256 | cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0); | |
76a66253 | 1257 | } |
76a66253 | 1258 | |
84f2d0ea | 1259 | static void do_trace_print_events(Monitor *mon, const QDict *qdict) |
22890ab5 | 1260 | { |
fc764105 | 1261 | trace_print_events((FILE *)mon, &monitor_fprintf); |
22890ab5 | 1262 | } |
22890ab5 | 1263 | |
edc5cb1a YH |
1264 | static int client_migrate_info(Monitor *mon, const QDict *qdict, |
1265 | MonitorCompletion cb, void *opaque) | |
e866e239 GH |
1266 | { |
1267 | const char *protocol = qdict_get_str(qdict, "protocol"); | |
1268 | const char *hostname = qdict_get_str(qdict, "hostname"); | |
1269 | const char *subject = qdict_get_try_str(qdict, "cert-subject"); | |
1270 | int port = qdict_get_try_int(qdict, "port", -1); | |
1271 | int tls_port = qdict_get_try_int(qdict, "tls-port", -1); | |
1272 | int ret; | |
1273 | ||
1274 | if (strcmp(protocol, "spice") == 0) { | |
1275 | if (!using_spice) { | |
1276 | qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice"); | |
1277 | return -1; | |
1278 | } | |
1279 | ||
6ec5dae5 YH |
1280 | if (port == -1 && tls_port == -1) { |
1281 | qerror_report(QERR_MISSING_PARAMETER, "port/tls-port"); | |
1282 | return -1; | |
1283 | } | |
1284 | ||
edc5cb1a YH |
1285 | ret = qemu_spice_migrate_info(hostname, port, tls_port, subject, |
1286 | cb, opaque); | |
e866e239 GH |
1287 | if (ret != 0) { |
1288 | qerror_report(QERR_UNDEFINED_ERROR); | |
1289 | return -1; | |
1290 | } | |
1291 | return 0; | |
1292 | } | |
1293 | ||
1294 | qerror_report(QERR_INVALID_PARAMETER, "protocol"); | |
1295 | return -1; | |
1296 | } | |
1297 | ||
d54908a5 | 1298 | static void do_logfile(Monitor *mon, const QDict *qdict) |
e735b91c | 1299 | { |
9a7e5424 | 1300 | qemu_set_log_filename(qdict_get_str(qdict, "filename")); |
e735b91c PB |
1301 | } |
1302 | ||
d54908a5 | 1303 | static void do_log(Monitor *mon, const QDict *qdict) |
f193c797 FB |
1304 | { |
1305 | int mask; | |
d54908a5 | 1306 | const char *items = qdict_get_str(qdict, "items"); |
3b46e624 | 1307 | |
9307c4c1 | 1308 | if (!strcmp(items, "none")) { |
f193c797 FB |
1309 | mask = 0; |
1310 | } else { | |
4fde1eba | 1311 | mask = qemu_str_to_log_mask(items); |
f193c797 | 1312 | if (!mask) { |
376253ec | 1313 | help_cmd(mon, "log"); |
f193c797 FB |
1314 | return; |
1315 | } | |
1316 | } | |
24537a01 | 1317 | qemu_set_log(mask); |
f193c797 FB |
1318 | } |
1319 | ||
d54908a5 | 1320 | static void do_singlestep(Monitor *mon, const QDict *qdict) |
1b530a6d | 1321 | { |
d54908a5 | 1322 | const char *option = qdict_get_try_str(qdict, "option"); |
1b530a6d AJ |
1323 | if (!option || !strcmp(option, "on")) { |
1324 | singlestep = 1; | |
1325 | } else if (!strcmp(option, "off")) { | |
1326 | singlestep = 0; | |
1327 | } else { | |
1328 | monitor_printf(mon, "unexpected option %s\n", option); | |
1329 | } | |
1330 | } | |
1331 | ||
d54908a5 | 1332 | static void do_gdbserver(Monitor *mon, const QDict *qdict) |
59030a8c | 1333 | { |
d54908a5 | 1334 | const char *device = qdict_get_try_str(qdict, "device"); |
59030a8c AL |
1335 | if (!device) |
1336 | device = "tcp::" DEFAULT_GDBSTUB_PORT; | |
1337 | if (gdbserver_start(device) < 0) { | |
1338 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", | |
1339 | device); | |
1340 | } else if (strcmp(device, "none") == 0) { | |
36556b20 | 1341 | monitor_printf(mon, "Disabled gdbserver\n"); |
8a7ddc38 | 1342 | } else { |
59030a8c AL |
1343 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
1344 | device); | |
8a7ddc38 FB |
1345 | } |
1346 | } | |
1347 | ||
d54908a5 | 1348 | static void do_watchdog_action(Monitor *mon, const QDict *qdict) |
9dd986cc | 1349 | { |
d54908a5 | 1350 | const char *action = qdict_get_str(qdict, "action"); |
9dd986cc RJ |
1351 | if (select_watchdog_action(action) == -1) { |
1352 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); | |
1353 | } | |
1354 | } | |
1355 | ||
376253ec | 1356 | static void monitor_printc(Monitor *mon, int c) |
9307c4c1 | 1357 | { |
376253ec | 1358 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1359 | switch(c) { |
1360 | case '\'': | |
376253ec | 1361 | monitor_printf(mon, "\\'"); |
9307c4c1 FB |
1362 | break; |
1363 | case '\\': | |
376253ec | 1364 | monitor_printf(mon, "\\\\"); |
9307c4c1 FB |
1365 | break; |
1366 | case '\n': | |
376253ec | 1367 | monitor_printf(mon, "\\n"); |
9307c4c1 FB |
1368 | break; |
1369 | case '\r': | |
376253ec | 1370 | monitor_printf(mon, "\\r"); |
9307c4c1 FB |
1371 | break; |
1372 | default: | |
1373 | if (c >= 32 && c <= 126) { | |
376253ec | 1374 | monitor_printf(mon, "%c", c); |
9307c4c1 | 1375 | } else { |
376253ec | 1376 | monitor_printf(mon, "\\x%02x", c); |
9307c4c1 FB |
1377 | } |
1378 | break; | |
1379 | } | |
376253ec | 1380 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1381 | } |
1382 | ||
376253ec | 1383 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
a8170e5e | 1384 | hwaddr addr, int is_physical) |
9307c4c1 | 1385 | { |
9349b4f9 | 1386 | CPUArchState *env; |
23842aab | 1387 | int l, line_size, i, max_digits, len; |
9307c4c1 FB |
1388 | uint8_t buf[16]; |
1389 | uint64_t v; | |
1390 | ||
1391 | if (format == 'i') { | |
1392 | int flags; | |
1393 | flags = 0; | |
6a00d601 | 1394 | env = mon_get_cpu(); |
9307c4c1 | 1395 | #ifdef TARGET_I386 |
4c27ba27 | 1396 | if (wsize == 2) { |
9307c4c1 | 1397 | flags = 1; |
4c27ba27 FB |
1398 | } else if (wsize == 4) { |
1399 | flags = 0; | |
1400 | } else { | |
6a15fd12 | 1401 | /* as default we use the current CS size */ |
4c27ba27 | 1402 | flags = 0; |
6a15fd12 FB |
1403 | if (env) { |
1404 | #ifdef TARGET_X86_64 | |
5fafdf24 | 1405 | if ((env->efer & MSR_EFER_LMA) && |
6a15fd12 FB |
1406 | (env->segs[R_CS].flags & DESC_L_MASK)) |
1407 | flags = 2; | |
1408 | else | |
1409 | #endif | |
1410 | if (!(env->segs[R_CS].flags & DESC_B_MASK)) | |
1411 | flags = 1; | |
1412 | } | |
4c27ba27 | 1413 | } |
1c38f843 TM |
1414 | #endif |
1415 | #ifdef TARGET_PPC | |
1416 | flags = msr_le << 16; | |
1417 | flags |= env->bfd_mach; | |
4c27ba27 | 1418 | #endif |
376253ec | 1419 | monitor_disas(mon, env, addr, count, is_physical, flags); |
9307c4c1 FB |
1420 | return; |
1421 | } | |
1422 | ||
1423 | len = wsize * count; | |
1424 | if (wsize == 1) | |
1425 | line_size = 8; | |
1426 | else | |
1427 | line_size = 16; | |
9307c4c1 FB |
1428 | max_digits = 0; |
1429 | ||
1430 | switch(format) { | |
1431 | case 'o': | |
1432 | max_digits = (wsize * 8 + 2) / 3; | |
1433 | break; | |
1434 | default: | |
1435 | case 'x': | |
1436 | max_digits = (wsize * 8) / 4; | |
1437 | break; | |
1438 | case 'u': | |
1439 | case 'd': | |
1440 | max_digits = (wsize * 8 * 10 + 32) / 33; | |
1441 | break; | |
1442 | case 'c': | |
1443 | wsize = 1; | |
1444 | break; | |
1445 | } | |
1446 | ||
1447 | while (len > 0) { | |
7743e588 | 1448 | if (is_physical) |
376253ec | 1449 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
7743e588 | 1450 | else |
376253ec | 1451 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
9307c4c1 FB |
1452 | l = len; |
1453 | if (l > line_size) | |
1454 | l = line_size; | |
1455 | if (is_physical) { | |
54f7b4a3 | 1456 | cpu_physical_memory_read(addr, buf, l); |
9307c4c1 | 1457 | } else { |
6a00d601 | 1458 | env = mon_get_cpu(); |
f17ec444 | 1459 | if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) { |
376253ec | 1460 | monitor_printf(mon, " Cannot access memory\n"); |
c8f79b67 AL |
1461 | break; |
1462 | } | |
9307c4c1 | 1463 | } |
5fafdf24 | 1464 | i = 0; |
9307c4c1 FB |
1465 | while (i < l) { |
1466 | switch(wsize) { | |
1467 | default: | |
1468 | case 1: | |
1469 | v = ldub_raw(buf + i); | |
1470 | break; | |
1471 | case 2: | |
1472 | v = lduw_raw(buf + i); | |
1473 | break; | |
1474 | case 4: | |
92a31b1f | 1475 | v = (uint32_t)ldl_raw(buf + i); |
9307c4c1 FB |
1476 | break; |
1477 | case 8: | |
1478 | v = ldq_raw(buf + i); | |
1479 | break; | |
1480 | } | |
376253ec | 1481 | monitor_printf(mon, " "); |
9307c4c1 FB |
1482 | switch(format) { |
1483 | case 'o': | |
376253ec | 1484 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
9307c4c1 FB |
1485 | break; |
1486 | case 'x': | |
376253ec | 1487 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
9307c4c1 FB |
1488 | break; |
1489 | case 'u': | |
376253ec | 1490 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
9307c4c1 FB |
1491 | break; |
1492 | case 'd': | |
376253ec | 1493 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
9307c4c1 FB |
1494 | break; |
1495 | case 'c': | |
376253ec | 1496 | monitor_printc(mon, v); |
9307c4c1 FB |
1497 | break; |
1498 | } | |
1499 | i += wsize; | |
1500 | } | |
376253ec | 1501 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1502 | addr += l; |
1503 | len -= l; | |
1504 | } | |
1505 | } | |
1506 | ||
1bd1442e | 1507 | static void do_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1508 | { |
1bd1442e LC |
1509 | int count = qdict_get_int(qdict, "count"); |
1510 | int format = qdict_get_int(qdict, "format"); | |
1511 | int size = qdict_get_int(qdict, "size"); | |
1512 | target_long addr = qdict_get_int(qdict, "addr"); | |
1513 | ||
376253ec | 1514 | memory_dump(mon, count, format, size, addr, 0); |
9307c4c1 FB |
1515 | } |
1516 | ||
1bd1442e | 1517 | static void do_physical_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1518 | { |
1bd1442e LC |
1519 | int count = qdict_get_int(qdict, "count"); |
1520 | int format = qdict_get_int(qdict, "format"); | |
1521 | int size = qdict_get_int(qdict, "size"); | |
a8170e5e | 1522 | hwaddr addr = qdict_get_int(qdict, "addr"); |
1bd1442e | 1523 | |
376253ec | 1524 | memory_dump(mon, count, format, size, addr, 1); |
9307c4c1 FB |
1525 | } |
1526 | ||
1bd1442e | 1527 | static void do_print(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1528 | { |
1bd1442e | 1529 | int format = qdict_get_int(qdict, "format"); |
a8170e5e | 1530 | hwaddr val = qdict_get_int(qdict, "val"); |
1bd1442e | 1531 | |
9307c4c1 FB |
1532 | switch(format) { |
1533 | case 'o': | |
a8170e5e | 1534 | monitor_printf(mon, "%#" HWADDR_PRIo, val); |
9307c4c1 FB |
1535 | break; |
1536 | case 'x': | |
a8170e5e | 1537 | monitor_printf(mon, "%#" HWADDR_PRIx, val); |
9307c4c1 FB |
1538 | break; |
1539 | case 'u': | |
a8170e5e | 1540 | monitor_printf(mon, "%" HWADDR_PRIu, val); |
9307c4c1 FB |
1541 | break; |
1542 | default: | |
1543 | case 'd': | |
a8170e5e | 1544 | monitor_printf(mon, "%" HWADDR_PRId, val); |
9307c4c1 FB |
1545 | break; |
1546 | case 'c': | |
376253ec | 1547 | monitor_printc(mon, val); |
9307c4c1 FB |
1548 | break; |
1549 | } | |
376253ec | 1550 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1551 | } |
1552 | ||
f18c16de | 1553 | static void do_sum(Monitor *mon, const QDict *qdict) |
e4cf1adc FB |
1554 | { |
1555 | uint32_t addr; | |
e4cf1adc | 1556 | uint16_t sum; |
f18c16de LC |
1557 | uint32_t start = qdict_get_int(qdict, "start"); |
1558 | uint32_t size = qdict_get_int(qdict, "size"); | |
e4cf1adc FB |
1559 | |
1560 | sum = 0; | |
1561 | for(addr = start; addr < (start + size); addr++) { | |
2c17449b | 1562 | uint8_t val = ldub_phys(&address_space_memory, addr); |
e4cf1adc FB |
1563 | /* BSD sum algorithm ('sum' Unix command) */ |
1564 | sum = (sum >> 1) | (sum << 15); | |
54f7b4a3 | 1565 | sum += val; |
e4cf1adc | 1566 | } |
376253ec | 1567 | monitor_printf(mon, "%05d\n", sum); |
e4cf1adc FB |
1568 | } |
1569 | ||
13224a87 FB |
1570 | static int mouse_button_state; |
1571 | ||
1d4daa91 | 1572 | static void do_mouse_move(Monitor *mon, const QDict *qdict) |
13224a87 | 1573 | { |
c751a74a | 1574 | int dx, dy, dz, button; |
1d4daa91 LC |
1575 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
1576 | const char *dy_str = qdict_get_str(qdict, "dy_str"); | |
1577 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); | |
c751a74a | 1578 | |
13224a87 FB |
1579 | dx = strtol(dx_str, NULL, 0); |
1580 | dy = strtol(dy_str, NULL, 0); | |
c751a74a GH |
1581 | qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx); |
1582 | qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy); | |
1583 | ||
1584 | if (dz_str) { | |
13224a87 | 1585 | dz = strtol(dz_str, NULL, 0); |
c751a74a GH |
1586 | if (dz != 0) { |
1587 | button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN; | |
1588 | qemu_input_queue_btn(NULL, button, true); | |
1589 | qemu_input_event_sync(); | |
1590 | qemu_input_queue_btn(NULL, button, false); | |
1591 | } | |
1592 | } | |
1593 | qemu_input_event_sync(); | |
13224a87 FB |
1594 | } |
1595 | ||
d54908a5 | 1596 | static void do_mouse_button(Monitor *mon, const QDict *qdict) |
13224a87 | 1597 | { |
c751a74a GH |
1598 | static uint32_t bmap[INPUT_BUTTON_MAX] = { |
1599 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, | |
1600 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, | |
1601 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, | |
1602 | }; | |
d54908a5 | 1603 | int button_state = qdict_get_int(qdict, "button_state"); |
c751a74a GH |
1604 | |
1605 | if (mouse_button_state == button_state) { | |
1606 | return; | |
1607 | } | |
1608 | qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state); | |
1609 | qemu_input_event_sync(); | |
13224a87 | 1610 | mouse_button_state = button_state; |
13224a87 FB |
1611 | } |
1612 | ||
aa93e39c | 1613 | static void do_ioport_read(Monitor *mon, const QDict *qdict) |
3440557b | 1614 | { |
aa93e39c LC |
1615 | int size = qdict_get_int(qdict, "size"); |
1616 | int addr = qdict_get_int(qdict, "addr"); | |
1617 | int has_index = qdict_haskey(qdict, "index"); | |
3440557b FB |
1618 | uint32_t val; |
1619 | int suffix; | |
1620 | ||
1621 | if (has_index) { | |
aa93e39c | 1622 | int index = qdict_get_int(qdict, "index"); |
afcea8cb | 1623 | cpu_outb(addr & IOPORTS_MASK, index & 0xff); |
3440557b FB |
1624 | addr++; |
1625 | } | |
1626 | addr &= 0xffff; | |
1627 | ||
1628 | switch(size) { | |
1629 | default: | |
1630 | case 1: | |
afcea8cb | 1631 | val = cpu_inb(addr); |
3440557b FB |
1632 | suffix = 'b'; |
1633 | break; | |
1634 | case 2: | |
afcea8cb | 1635 | val = cpu_inw(addr); |
3440557b FB |
1636 | suffix = 'w'; |
1637 | break; | |
1638 | case 4: | |
afcea8cb | 1639 | val = cpu_inl(addr); |
3440557b FB |
1640 | suffix = 'l'; |
1641 | break; | |
1642 | } | |
376253ec AL |
1643 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
1644 | suffix, addr, size * 2, val); | |
3440557b | 1645 | } |
a3a91a35 | 1646 | |
1bd1442e | 1647 | static void do_ioport_write(Monitor *mon, const QDict *qdict) |
f114784f | 1648 | { |
1bd1442e LC |
1649 | int size = qdict_get_int(qdict, "size"); |
1650 | int addr = qdict_get_int(qdict, "addr"); | |
1651 | int val = qdict_get_int(qdict, "val"); | |
1652 | ||
f114784f JK |
1653 | addr &= IOPORTS_MASK; |
1654 | ||
1655 | switch (size) { | |
1656 | default: | |
1657 | case 1: | |
afcea8cb | 1658 | cpu_outb(addr, val); |
f114784f JK |
1659 | break; |
1660 | case 2: | |
afcea8cb | 1661 | cpu_outw(addr, val); |
f114784f JK |
1662 | break; |
1663 | case 4: | |
afcea8cb | 1664 | cpu_outl(addr, val); |
f114784f JK |
1665 | break; |
1666 | } | |
1667 | } | |
1668 | ||
d54908a5 | 1669 | static void do_boot_set(Monitor *mon, const QDict *qdict) |
0ecdffbb AJ |
1670 | { |
1671 | int res; | |
d54908a5 | 1672 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
0ecdffbb | 1673 | |
76e30d0f JK |
1674 | res = qemu_boot_set(bootdevice); |
1675 | if (res == 0) { | |
1676 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); | |
1677 | } else if (res > 0) { | |
1678 | monitor_printf(mon, "setting boot device list failed\n"); | |
0ecdffbb | 1679 | } else { |
376253ec AL |
1680 | monitor_printf(mon, "no function defined to set boot device list for " |
1681 | "this architecture\n"); | |
0ecdffbb AJ |
1682 | } |
1683 | } | |
1684 | ||
b86bda5b | 1685 | #if defined(TARGET_I386) |
a8170e5e AK |
1686 | static void print_pte(Monitor *mon, hwaddr addr, |
1687 | hwaddr pte, | |
1688 | hwaddr mask) | |
b86bda5b | 1689 | { |
d65aaf37 BS |
1690 | #ifdef TARGET_X86_64 |
1691 | if (addr & (1ULL << 47)) { | |
1692 | addr |= -1LL << 48; | |
1693 | } | |
1694 | #endif | |
1695 | monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx | |
1696 | " %c%c%c%c%c%c%c%c%c\n", | |
376253ec AL |
1697 | addr, |
1698 | pte & mask, | |
d65aaf37 | 1699 | pte & PG_NX_MASK ? 'X' : '-', |
376253ec AL |
1700 | pte & PG_GLOBAL_MASK ? 'G' : '-', |
1701 | pte & PG_PSE_MASK ? 'P' : '-', | |
1702 | pte & PG_DIRTY_MASK ? 'D' : '-', | |
1703 | pte & PG_ACCESSED_MASK ? 'A' : '-', | |
1704 | pte & PG_PCD_MASK ? 'C' : '-', | |
1705 | pte & PG_PWT_MASK ? 'T' : '-', | |
1706 | pte & PG_USER_MASK ? 'U' : '-', | |
1707 | pte & PG_RW_MASK ? 'W' : '-'); | |
b86bda5b FB |
1708 | } |
1709 | ||
9349b4f9 | 1710 | static void tlb_info_32(Monitor *mon, CPUArchState *env) |
b86bda5b | 1711 | { |
94ac5cd2 | 1712 | unsigned int l1, l2; |
b86bda5b FB |
1713 | uint32_t pgd, pde, pte; |
1714 | ||
b86bda5b FB |
1715 | pgd = env->cr[3] & ~0xfff; |
1716 | for(l1 = 0; l1 < 1024; l1++) { | |
b8b79323 | 1717 | cpu_physical_memory_read(pgd + l1 * 4, &pde, 4); |
b86bda5b FB |
1718 | pde = le32_to_cpu(pde); |
1719 | if (pde & PG_PRESENT_MASK) { | |
1720 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
d65aaf37 BS |
1721 | /* 4M pages */ |
1722 | print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1)); | |
b86bda5b FB |
1723 | } else { |
1724 | for(l2 = 0; l2 < 1024; l2++) { | |
b8b79323 | 1725 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4); |
b86bda5b FB |
1726 | pte = le32_to_cpu(pte); |
1727 | if (pte & PG_PRESENT_MASK) { | |
376253ec | 1728 | print_pte(mon, (l1 << 22) + (l2 << 12), |
5fafdf24 | 1729 | pte & ~PG_PSE_MASK, |
b86bda5b FB |
1730 | ~0xfff); |
1731 | } | |
1732 | } | |
1733 | } | |
1734 | } | |
1735 | } | |
1736 | } | |
1737 | ||
9349b4f9 | 1738 | static void tlb_info_pae32(Monitor *mon, CPUArchState *env) |
d65aaf37 | 1739 | { |
94ac5cd2 | 1740 | unsigned int l1, l2, l3; |
d65aaf37 BS |
1741 | uint64_t pdpe, pde, pte; |
1742 | uint64_t pdp_addr, pd_addr, pt_addr; | |
1743 | ||
1744 | pdp_addr = env->cr[3] & ~0x1f; | |
1745 | for (l1 = 0; l1 < 4; l1++) { | |
b8b79323 | 1746 | cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8); |
d65aaf37 BS |
1747 | pdpe = le64_to_cpu(pdpe); |
1748 | if (pdpe & PG_PRESENT_MASK) { | |
1749 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
1750 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1751 | cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8); |
d65aaf37 BS |
1752 | pde = le64_to_cpu(pde); |
1753 | if (pde & PG_PRESENT_MASK) { | |
1754 | if (pde & PG_PSE_MASK) { | |
1755 | /* 2M pages with PAE, CR4.PSE is ignored */ | |
1756 | print_pte(mon, (l1 << 30 ) + (l2 << 21), pde, | |
a8170e5e | 1757 | ~((hwaddr)(1 << 20) - 1)); |
d65aaf37 BS |
1758 | } else { |
1759 | pt_addr = pde & 0x3fffffffff000ULL; | |
1760 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 1761 | cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8); |
d65aaf37 BS |
1762 | pte = le64_to_cpu(pte); |
1763 | if (pte & PG_PRESENT_MASK) { | |
1764 | print_pte(mon, (l1 << 30 ) + (l2 << 21) | |
1765 | + (l3 << 12), | |
1766 | pte & ~PG_PSE_MASK, | |
a8170e5e | 1767 | ~(hwaddr)0xfff); |
d65aaf37 BS |
1768 | } |
1769 | } | |
1770 | } | |
1771 | } | |
1772 | } | |
1773 | } | |
1774 | } | |
1775 | } | |
1776 | ||
1777 | #ifdef TARGET_X86_64 | |
9349b4f9 | 1778 | static void tlb_info_64(Monitor *mon, CPUArchState *env) |
d65aaf37 BS |
1779 | { |
1780 | uint64_t l1, l2, l3, l4; | |
1781 | uint64_t pml4e, pdpe, pde, pte; | |
1782 | uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr; | |
1783 | ||
1784 | pml4_addr = env->cr[3] & 0x3fffffffff000ULL; | |
1785 | for (l1 = 0; l1 < 512; l1++) { | |
b8b79323 | 1786 | cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8); |
d65aaf37 BS |
1787 | pml4e = le64_to_cpu(pml4e); |
1788 | if (pml4e & PG_PRESENT_MASK) { | |
1789 | pdp_addr = pml4e & 0x3fffffffff000ULL; | |
1790 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1791 | cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8); |
d65aaf37 BS |
1792 | pdpe = le64_to_cpu(pdpe); |
1793 | if (pdpe & PG_PRESENT_MASK) { | |
1794 | if (pdpe & PG_PSE_MASK) { | |
1795 | /* 1G pages, CR4.PSE is ignored */ | |
1796 | print_pte(mon, (l1 << 39) + (l2 << 30), pdpe, | |
1797 | 0x3ffffc0000000ULL); | |
1798 | } else { | |
1799 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
1800 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 1801 | cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8); |
d65aaf37 BS |
1802 | pde = le64_to_cpu(pde); |
1803 | if (pde & PG_PRESENT_MASK) { | |
1804 | if (pde & PG_PSE_MASK) { | |
1805 | /* 2M pages, CR4.PSE is ignored */ | |
1806 | print_pte(mon, (l1 << 39) + (l2 << 30) + | |
1807 | (l3 << 21), pde, | |
1808 | 0x3ffffffe00000ULL); | |
1809 | } else { | |
1810 | pt_addr = pde & 0x3fffffffff000ULL; | |
1811 | for (l4 = 0; l4 < 512; l4++) { | |
1812 | cpu_physical_memory_read(pt_addr | |
1813 | + l4 * 8, | |
b8b79323 | 1814 | &pte, 8); |
d65aaf37 BS |
1815 | pte = le64_to_cpu(pte); |
1816 | if (pte & PG_PRESENT_MASK) { | |
1817 | print_pte(mon, (l1 << 39) + | |
1818 | (l2 << 30) + | |
1819 | (l3 << 21) + (l4 << 12), | |
1820 | pte & ~PG_PSE_MASK, | |
1821 | 0x3fffffffff000ULL); | |
1822 | } | |
1823 | } | |
1824 | } | |
1825 | } | |
1826 | } | |
1827 | } | |
1828 | } | |
1829 | } | |
1830 | } | |
1831 | } | |
1832 | } | |
1833 | #endif | |
1834 | ||
84f2d0ea | 1835 | static void tlb_info(Monitor *mon, const QDict *qdict) |
d65aaf37 | 1836 | { |
9349b4f9 | 1837 | CPUArchState *env; |
d65aaf37 BS |
1838 | |
1839 | env = mon_get_cpu(); | |
1840 | ||
1841 | if (!(env->cr[0] & CR0_PG_MASK)) { | |
1842 | monitor_printf(mon, "PG disabled\n"); | |
1843 | return; | |
1844 | } | |
1845 | if (env->cr[4] & CR4_PAE_MASK) { | |
1846 | #ifdef TARGET_X86_64 | |
1847 | if (env->hflags & HF_LMA_MASK) { | |
1848 | tlb_info_64(mon, env); | |
1849 | } else | |
1850 | #endif | |
1851 | { | |
1852 | tlb_info_pae32(mon, env); | |
1853 | } | |
1854 | } else { | |
1855 | tlb_info_32(mon, env); | |
1856 | } | |
1857 | } | |
1858 | ||
a8170e5e | 1859 | static void mem_print(Monitor *mon, hwaddr *pstart, |
1b3cba6e | 1860 | int *plast_prot, |
a8170e5e | 1861 | hwaddr end, int prot) |
b86bda5b | 1862 | { |
9746b15b FB |
1863 | int prot1; |
1864 | prot1 = *plast_prot; | |
1865 | if (prot != prot1) { | |
b86bda5b | 1866 | if (*pstart != -1) { |
1b3cba6e BS |
1867 | monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " " |
1868 | TARGET_FMT_plx " %c%c%c\n", | |
376253ec AL |
1869 | *pstart, end, end - *pstart, |
1870 | prot1 & PG_USER_MASK ? 'u' : '-', | |
1871 | 'r', | |
1872 | prot1 & PG_RW_MASK ? 'w' : '-'); | |
b86bda5b FB |
1873 | } |
1874 | if (prot != 0) | |
1875 | *pstart = end; | |
1876 | else | |
1877 | *pstart = -1; | |
1878 | *plast_prot = prot; | |
1879 | } | |
1880 | } | |
1881 | ||
9349b4f9 | 1882 | static void mem_info_32(Monitor *mon, CPUArchState *env) |
b86bda5b | 1883 | { |
b49ca72d AC |
1884 | unsigned int l1, l2; |
1885 | int prot, last_prot; | |
1b3cba6e | 1886 | uint32_t pgd, pde, pte; |
a8170e5e | 1887 | hwaddr start, end; |
6a00d601 | 1888 | |
b86bda5b FB |
1889 | pgd = env->cr[3] & ~0xfff; |
1890 | last_prot = 0; | |
1891 | start = -1; | |
1892 | for(l1 = 0; l1 < 1024; l1++) { | |
b8b79323 | 1893 | cpu_physical_memory_read(pgd + l1 * 4, &pde, 4); |
b86bda5b FB |
1894 | pde = le32_to_cpu(pde); |
1895 | end = l1 << 22; | |
1896 | if (pde & PG_PRESENT_MASK) { | |
1897 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1898 | prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
376253ec | 1899 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1900 | } else { |
1901 | for(l2 = 0; l2 < 1024; l2++) { | |
b8b79323 | 1902 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4); |
b86bda5b FB |
1903 | pte = le32_to_cpu(pte); |
1904 | end = (l1 << 22) + (l2 << 12); | |
1905 | if (pte & PG_PRESENT_MASK) { | |
c76c8416 AC |
1906 | prot = pte & pde & |
1907 | (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
b86bda5b FB |
1908 | } else { |
1909 | prot = 0; | |
1910 | } | |
376253ec | 1911 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1912 | } |
1913 | } | |
1914 | } else { | |
1915 | prot = 0; | |
376253ec | 1916 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1917 | } |
1918 | } | |
8a94b8ca | 1919 | /* Flush last range */ |
a8170e5e | 1920 | mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0); |
b86bda5b | 1921 | } |
1b3cba6e | 1922 | |
9349b4f9 | 1923 | static void mem_info_pae32(Monitor *mon, CPUArchState *env) |
1b3cba6e | 1924 | { |
b49ca72d AC |
1925 | unsigned int l1, l2, l3; |
1926 | int prot, last_prot; | |
1b3cba6e BS |
1927 | uint64_t pdpe, pde, pte; |
1928 | uint64_t pdp_addr, pd_addr, pt_addr; | |
a8170e5e | 1929 | hwaddr start, end; |
1b3cba6e BS |
1930 | |
1931 | pdp_addr = env->cr[3] & ~0x1f; | |
1932 | last_prot = 0; | |
1933 | start = -1; | |
1934 | for (l1 = 0; l1 < 4; l1++) { | |
b8b79323 | 1935 | cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8); |
1b3cba6e BS |
1936 | pdpe = le64_to_cpu(pdpe); |
1937 | end = l1 << 30; | |
1938 | if (pdpe & PG_PRESENT_MASK) { | |
1939 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
1940 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1941 | cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8); |
1b3cba6e BS |
1942 | pde = le64_to_cpu(pde); |
1943 | end = (l1 << 30) + (l2 << 21); | |
1944 | if (pde & PG_PRESENT_MASK) { | |
1945 | if (pde & PG_PSE_MASK) { | |
1946 | prot = pde & (PG_USER_MASK | PG_RW_MASK | | |
1947 | PG_PRESENT_MASK); | |
1948 | mem_print(mon, &start, &last_prot, end, prot); | |
1949 | } else { | |
1950 | pt_addr = pde & 0x3fffffffff000ULL; | |
1951 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 1952 | cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8); |
1b3cba6e BS |
1953 | pte = le64_to_cpu(pte); |
1954 | end = (l1 << 30) + (l2 << 21) + (l3 << 12); | |
1955 | if (pte & PG_PRESENT_MASK) { | |
c76c8416 AC |
1956 | prot = pte & pde & (PG_USER_MASK | PG_RW_MASK | |
1957 | PG_PRESENT_MASK); | |
1b3cba6e BS |
1958 | } else { |
1959 | prot = 0; | |
1960 | } | |
1961 | mem_print(mon, &start, &last_prot, end, prot); | |
1962 | } | |
1963 | } | |
1964 | } else { | |
1965 | prot = 0; | |
1966 | mem_print(mon, &start, &last_prot, end, prot); | |
1967 | } | |
1968 | } | |
1969 | } else { | |
1970 | prot = 0; | |
1971 | mem_print(mon, &start, &last_prot, end, prot); | |
1972 | } | |
1973 | } | |
8a94b8ca | 1974 | /* Flush last range */ |
a8170e5e | 1975 | mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0); |
1b3cba6e BS |
1976 | } |
1977 | ||
1978 | ||
1979 | #ifdef TARGET_X86_64 | |
9349b4f9 | 1980 | static void mem_info_64(Monitor *mon, CPUArchState *env) |
1b3cba6e BS |
1981 | { |
1982 | int prot, last_prot; | |
1983 | uint64_t l1, l2, l3, l4; | |
1984 | uint64_t pml4e, pdpe, pde, pte; | |
1985 | uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end; | |
1986 | ||
1987 | pml4_addr = env->cr[3] & 0x3fffffffff000ULL; | |
1988 | last_prot = 0; | |
1989 | start = -1; | |
1990 | for (l1 = 0; l1 < 512; l1++) { | |
b8b79323 | 1991 | cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8); |
1b3cba6e BS |
1992 | pml4e = le64_to_cpu(pml4e); |
1993 | end = l1 << 39; | |
1994 | if (pml4e & PG_PRESENT_MASK) { | |
1995 | pdp_addr = pml4e & 0x3fffffffff000ULL; | |
1996 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1997 | cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8); |
1b3cba6e BS |
1998 | pdpe = le64_to_cpu(pdpe); |
1999 | end = (l1 << 39) + (l2 << 30); | |
2000 | if (pdpe & PG_PRESENT_MASK) { | |
2001 | if (pdpe & PG_PSE_MASK) { | |
2d5b5074 BS |
2002 | prot = pdpe & (PG_USER_MASK | PG_RW_MASK | |
2003 | PG_PRESENT_MASK); | |
c76c8416 | 2004 | prot &= pml4e; |
1b3cba6e BS |
2005 | mem_print(mon, &start, &last_prot, end, prot); |
2006 | } else { | |
2007 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
2008 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 2009 | cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8); |
1b3cba6e BS |
2010 | pde = le64_to_cpu(pde); |
2011 | end = (l1 << 39) + (l2 << 30) + (l3 << 21); | |
2012 | if (pde & PG_PRESENT_MASK) { | |
2013 | if (pde & PG_PSE_MASK) { | |
2014 | prot = pde & (PG_USER_MASK | PG_RW_MASK | | |
2015 | PG_PRESENT_MASK); | |
c76c8416 | 2016 | prot &= pml4e & pdpe; |
1b3cba6e BS |
2017 | mem_print(mon, &start, &last_prot, end, prot); |
2018 | } else { | |
2019 | pt_addr = pde & 0x3fffffffff000ULL; | |
2020 | for (l4 = 0; l4 < 512; l4++) { | |
2021 | cpu_physical_memory_read(pt_addr | |
2022 | + l4 * 8, | |
b8b79323 | 2023 | &pte, 8); |
1b3cba6e BS |
2024 | pte = le64_to_cpu(pte); |
2025 | end = (l1 << 39) + (l2 << 30) + | |
2026 | (l3 << 21) + (l4 << 12); | |
2027 | if (pte & PG_PRESENT_MASK) { | |
2028 | prot = pte & (PG_USER_MASK | PG_RW_MASK | | |
2029 | PG_PRESENT_MASK); | |
c76c8416 | 2030 | prot &= pml4e & pdpe & pde; |
1b3cba6e BS |
2031 | } else { |
2032 | prot = 0; | |
2033 | } | |
2034 | mem_print(mon, &start, &last_prot, end, prot); | |
2035 | } | |
2036 | } | |
2037 | } else { | |
2038 | prot = 0; | |
2039 | mem_print(mon, &start, &last_prot, end, prot); | |
2040 | } | |
2041 | } | |
2042 | } | |
2043 | } else { | |
2044 | prot = 0; | |
2045 | mem_print(mon, &start, &last_prot, end, prot); | |
2046 | } | |
2047 | } | |
2048 | } else { | |
2049 | prot = 0; | |
2050 | mem_print(mon, &start, &last_prot, end, prot); | |
2051 | } | |
2052 | } | |
8a94b8ca | 2053 | /* Flush last range */ |
a8170e5e | 2054 | mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0); |
1b3cba6e BS |
2055 | } |
2056 | #endif | |
2057 | ||
84f2d0ea | 2058 | static void mem_info(Monitor *mon, const QDict *qdict) |
1b3cba6e | 2059 | { |
9349b4f9 | 2060 | CPUArchState *env; |
1b3cba6e BS |
2061 | |
2062 | env = mon_get_cpu(); | |
2063 | ||
2064 | if (!(env->cr[0] & CR0_PG_MASK)) { | |
2065 | monitor_printf(mon, "PG disabled\n"); | |
2066 | return; | |
2067 | } | |
2068 | if (env->cr[4] & CR4_PAE_MASK) { | |
2069 | #ifdef TARGET_X86_64 | |
2070 | if (env->hflags & HF_LMA_MASK) { | |
2071 | mem_info_64(mon, env); | |
2072 | } else | |
2073 | #endif | |
2074 | { | |
2075 | mem_info_pae32(mon, env); | |
2076 | } | |
2077 | } else { | |
2078 | mem_info_32(mon, env); | |
2079 | } | |
2080 | } | |
b86bda5b FB |
2081 | #endif |
2082 | ||
7c664e2f AJ |
2083 | #if defined(TARGET_SH4) |
2084 | ||
376253ec | 2085 | static void print_tlb(Monitor *mon, int idx, tlb_t *tlb) |
7c664e2f | 2086 | { |
376253ec AL |
2087 | monitor_printf(mon, " tlb%i:\t" |
2088 | "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t" | |
2089 | "v=%hhu shared=%hhu cached=%hhu prot=%hhu " | |
2090 | "dirty=%hhu writethrough=%hhu\n", | |
2091 | idx, | |
2092 | tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size, | |
2093 | tlb->v, tlb->sh, tlb->c, tlb->pr, | |
2094 | tlb->d, tlb->wt); | |
7c664e2f AJ |
2095 | } |
2096 | ||
84f2d0ea | 2097 | static void tlb_info(Monitor *mon, const QDict *qdict) |
7c664e2f | 2098 | { |
9349b4f9 | 2099 | CPUArchState *env = mon_get_cpu(); |
7c664e2f AJ |
2100 | int i; |
2101 | ||
376253ec | 2102 | monitor_printf (mon, "ITLB:\n"); |
7c664e2f | 2103 | for (i = 0 ; i < ITLB_SIZE ; i++) |
376253ec AL |
2104 | print_tlb (mon, i, &env->itlb[i]); |
2105 | monitor_printf (mon, "UTLB:\n"); | |
7c664e2f | 2106 | for (i = 0 ; i < UTLB_SIZE ; i++) |
376253ec | 2107 | print_tlb (mon, i, &env->utlb[i]); |
7c664e2f AJ |
2108 | } |
2109 | ||
2110 | #endif | |
2111 | ||
692f737c | 2112 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA) |
84f2d0ea | 2113 | static void tlb_info(Monitor *mon, const QDict *qdict) |
d41160a3 | 2114 | { |
9349b4f9 | 2115 | CPUArchState *env1 = mon_get_cpu(); |
d41160a3 BS |
2116 | |
2117 | dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1); | |
2118 | } | |
2119 | #endif | |
2120 | ||
84f2d0ea | 2121 | static void do_info_mtree(Monitor *mon, const QDict *qdict) |
314e2987 BS |
2122 | { |
2123 | mtree_info((fprintf_function)monitor_printf, mon); | |
2124 | } | |
2125 | ||
84f2d0ea | 2126 | static void do_info_numa(Monitor *mon, const QDict *qdict) |
030ea37b | 2127 | { |
b28b6230 | 2128 | int i; |
1b1ed8dc | 2129 | CPUState *cpu; |
030ea37b AL |
2130 | |
2131 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); | |
2132 | for (i = 0; i < nb_numa_nodes; i++) { | |
2133 | monitor_printf(mon, "node %d cpus:", i); | |
bdc44640 | 2134 | CPU_FOREACH(cpu) { |
1b1ed8dc | 2135 | if (cpu->numa_node == i) { |
55e5c285 | 2136 | monitor_printf(mon, " %d", cpu->cpu_index); |
030ea37b AL |
2137 | } |
2138 | } | |
2139 | monitor_printf(mon, "\n"); | |
2140 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, | |
8c85901e | 2141 | numa_info[i].node_mem >> 20); |
030ea37b AL |
2142 | } |
2143 | } | |
2144 | ||
5f1ce948 FB |
2145 | #ifdef CONFIG_PROFILER |
2146 | ||
e9a6625e AJ |
2147 | int64_t qemu_time; |
2148 | int64_t dev_time; | |
2149 | ||
84f2d0ea | 2150 | static void do_info_profile(Monitor *mon, const QDict *qdict) |
5f1ce948 | 2151 | { |
376253ec | 2152 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 2153 | dev_time, dev_time / (double)get_ticks_per_sec()); |
376253ec | 2154 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 2155 | qemu_time, qemu_time / (double)get_ticks_per_sec()); |
5f1ce948 | 2156 | qemu_time = 0; |
5f1ce948 | 2157 | dev_time = 0; |
5f1ce948 FB |
2158 | } |
2159 | #else | |
84f2d0ea | 2160 | static void do_info_profile(Monitor *mon, const QDict *qdict) |
5f1ce948 | 2161 | { |
376253ec | 2162 | monitor_printf(mon, "Internal profiler not compiled\n"); |
5f1ce948 FB |
2163 | } |
2164 | #endif | |
2165 | ||
ec36b695 | 2166 | /* Capture support */ |
72cf2d4f | 2167 | static QLIST_HEAD (capture_list_head, CaptureState) capture_head; |
ec36b695 | 2168 | |
84f2d0ea | 2169 | static void do_info_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
2170 | { |
2171 | int i; | |
2172 | CaptureState *s; | |
2173 | ||
2174 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
376253ec | 2175 | monitor_printf(mon, "[%d]: ", i); |
ec36b695 FB |
2176 | s->ops.info (s->opaque); |
2177 | } | |
2178 | } | |
2179 | ||
d54908a5 | 2180 | static void do_stop_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
2181 | { |
2182 | int i; | |
d54908a5 | 2183 | int n = qdict_get_int(qdict, "n"); |
ec36b695 FB |
2184 | CaptureState *s; |
2185 | ||
2186 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
2187 | if (i == n) { | |
2188 | s->ops.destroy (s->opaque); | |
72cf2d4f | 2189 | QLIST_REMOVE (s, entries); |
7267c094 | 2190 | g_free (s); |
ec36b695 FB |
2191 | return; |
2192 | } | |
2193 | } | |
2194 | } | |
2195 | ||
c1925484 LC |
2196 | static void do_wav_capture(Monitor *mon, const QDict *qdict) |
2197 | { | |
2198 | const char *path = qdict_get_str(qdict, "path"); | |
2199 | int has_freq = qdict_haskey(qdict, "freq"); | |
2200 | int freq = qdict_get_try_int(qdict, "freq", -1); | |
2201 | int has_bits = qdict_haskey(qdict, "bits"); | |
2202 | int bits = qdict_get_try_int(qdict, "bits", -1); | |
2203 | int has_channels = qdict_haskey(qdict, "nchannels"); | |
2204 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); | |
ec36b695 FB |
2205 | CaptureState *s; |
2206 | ||
7267c094 | 2207 | s = g_malloc0 (sizeof (*s)); |
ec36b695 FB |
2208 | |
2209 | freq = has_freq ? freq : 44100; | |
2210 | bits = has_bits ? bits : 16; | |
2211 | nchannels = has_channels ? nchannels : 2; | |
2212 | ||
2213 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
d00b2618 | 2214 | monitor_printf(mon, "Failed to add wave capture\n"); |
7267c094 | 2215 | g_free (s); |
d00b2618 | 2216 | return; |
ec36b695 | 2217 | } |
72cf2d4f | 2218 | QLIST_INSERT_HEAD (&capture_head, s, entries); |
ec36b695 | 2219 | } |
ec36b695 | 2220 | |
15dfcd45 | 2221 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
76655d6d | 2222 | { |
15dfcd45 | 2223 | qemu_acl *acl = qemu_acl_find(name); |
76655d6d | 2224 | |
76655d6d | 2225 | if (!acl) { |
15dfcd45 | 2226 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
76655d6d | 2227 | } |
15dfcd45 JK |
2228 | return acl; |
2229 | } | |
2230 | ||
d54908a5 | 2231 | static void do_acl_show(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2232 | { |
d54908a5 | 2233 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2234 | qemu_acl *acl = find_acl(mon, aclname); |
2235 | qemu_acl_entry *entry; | |
2236 | int i = 0; | |
76655d6d | 2237 | |
15dfcd45 | 2238 | if (acl) { |
28a76be8 | 2239 | monitor_printf(mon, "policy: %s\n", |
76655d6d | 2240 | acl->defaultDeny ? "deny" : "allow"); |
72cf2d4f | 2241 | QTAILQ_FOREACH(entry, &acl->entries, next) { |
28a76be8 AL |
2242 | i++; |
2243 | monitor_printf(mon, "%d: %s %s\n", i, | |
15dfcd45 | 2244 | entry->deny ? "deny" : "allow", entry->match); |
28a76be8 | 2245 | } |
15dfcd45 JK |
2246 | } |
2247 | } | |
2248 | ||
d54908a5 | 2249 | static void do_acl_reset(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2250 | { |
d54908a5 | 2251 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2252 | qemu_acl *acl = find_acl(mon, aclname); |
2253 | ||
2254 | if (acl) { | |
28a76be8 AL |
2255 | qemu_acl_reset(acl); |
2256 | monitor_printf(mon, "acl: removed all rules\n"); | |
15dfcd45 JK |
2257 | } |
2258 | } | |
2259 | ||
f18c16de | 2260 | static void do_acl_policy(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2261 | { |
f18c16de LC |
2262 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2263 | const char *policy = qdict_get_str(qdict, "policy"); | |
15dfcd45 | 2264 | qemu_acl *acl = find_acl(mon, aclname); |
28a76be8 | 2265 | |
15dfcd45 JK |
2266 | if (acl) { |
2267 | if (strcmp(policy, "allow") == 0) { | |
28a76be8 AL |
2268 | acl->defaultDeny = 0; |
2269 | monitor_printf(mon, "acl: policy set to 'allow'\n"); | |
15dfcd45 | 2270 | } else if (strcmp(policy, "deny") == 0) { |
28a76be8 AL |
2271 | acl->defaultDeny = 1; |
2272 | monitor_printf(mon, "acl: policy set to 'deny'\n"); | |
2273 | } else { | |
15dfcd45 JK |
2274 | monitor_printf(mon, "acl: unknown policy '%s', " |
2275 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 | 2276 | } |
15dfcd45 JK |
2277 | } |
2278 | } | |
28a76be8 | 2279 | |
1bd1442e | 2280 | static void do_acl_add(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2281 | { |
1bd1442e LC |
2282 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2283 | const char *match = qdict_get_str(qdict, "match"); | |
2284 | const char *policy = qdict_get_str(qdict, "policy"); | |
2285 | int has_index = qdict_haskey(qdict, "index"); | |
2286 | int index = qdict_get_try_int(qdict, "index", -1); | |
15dfcd45 JK |
2287 | qemu_acl *acl = find_acl(mon, aclname); |
2288 | int deny, ret; | |
2289 | ||
2290 | if (acl) { | |
2291 | if (strcmp(policy, "allow") == 0) { | |
2292 | deny = 0; | |
2293 | } else if (strcmp(policy, "deny") == 0) { | |
2294 | deny = 1; | |
2295 | } else { | |
2296 | monitor_printf(mon, "acl: unknown policy '%s', " | |
2297 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 AL |
2298 | return; |
2299 | } | |
28a76be8 AL |
2300 | if (has_index) |
2301 | ret = qemu_acl_insert(acl, deny, match, index); | |
2302 | else | |
2303 | ret = qemu_acl_append(acl, deny, match); | |
2304 | if (ret < 0) | |
2305 | monitor_printf(mon, "acl: unable to add acl entry\n"); | |
2306 | else | |
2307 | monitor_printf(mon, "acl: added rule at position %d\n", ret); | |
15dfcd45 JK |
2308 | } |
2309 | } | |
28a76be8 | 2310 | |
f18c16de | 2311 | static void do_acl_remove(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2312 | { |
f18c16de LC |
2313 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2314 | const char *match = qdict_get_str(qdict, "match"); | |
15dfcd45 JK |
2315 | qemu_acl *acl = find_acl(mon, aclname); |
2316 | int ret; | |
28a76be8 | 2317 | |
15dfcd45 | 2318 | if (acl) { |
28a76be8 AL |
2319 | ret = qemu_acl_remove(acl, match); |
2320 | if (ret < 0) | |
2321 | monitor_printf(mon, "acl: no matching acl entry\n"); | |
2322 | else | |
2323 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); | |
76655d6d AL |
2324 | } |
2325 | } | |
2326 | ||
79c4f6b0 | 2327 | #if defined(TARGET_I386) |
37b7ad48 | 2328 | static void do_inject_mce(Monitor *mon, const QDict *qdict) |
79c4f6b0 | 2329 | { |
8c5cf3b6 | 2330 | X86CPU *cpu; |
55e5c285 | 2331 | CPUState *cs; |
37b7ad48 LC |
2332 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
2333 | int bank = qdict_get_int(qdict, "bank"); | |
2334 | uint64_t status = qdict_get_int(qdict, "status"); | |
2335 | uint64_t mcg_status = qdict_get_int(qdict, "mcg_status"); | |
2336 | uint64_t addr = qdict_get_int(qdict, "addr"); | |
2337 | uint64_t misc = qdict_get_int(qdict, "misc"); | |
747461c7 | 2338 | int flags = MCE_INJECT_UNCOND_AO; |
79c4f6b0 | 2339 | |
747461c7 JK |
2340 | if (qdict_get_try_bool(qdict, "broadcast", 0)) { |
2341 | flags |= MCE_INJECT_BROADCAST; | |
2342 | } | |
c51a944b AF |
2343 | cs = qemu_get_cpu(cpu_index); |
2344 | if (cs != NULL) { | |
2345 | cpu = X86_CPU(cs); | |
2346 | cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc, | |
2347 | flags); | |
31ce5e0c | 2348 | } |
79c4f6b0 HY |
2349 | } |
2350 | #endif | |
2351 | ||
208c9d1b | 2352 | void qmp_getfd(const char *fdname, Error **errp) |
f07918fd | 2353 | { |
c227f099 | 2354 | mon_fd_t *monfd; |
f07918fd MM |
2355 | int fd; |
2356 | ||
208c9d1b | 2357 | fd = qemu_chr_fe_get_msgfd(cur_mon->chr); |
f07918fd | 2358 | if (fd == -1) { |
208c9d1b CB |
2359 | error_set(errp, QERR_FD_NOT_SUPPLIED); |
2360 | return; | |
f07918fd MM |
2361 | } |
2362 | ||
2363 | if (qemu_isdigit(fdname[0])) { | |
0b9f0e2f | 2364 | close(fd); |
208c9d1b CB |
2365 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname", |
2366 | "a name not starting with a digit"); | |
2367 | return; | |
f07918fd MM |
2368 | } |
2369 | ||
208c9d1b | 2370 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
f07918fd MM |
2371 | if (strcmp(monfd->name, fdname) != 0) { |
2372 | continue; | |
2373 | } | |
2374 | ||
2375 | close(monfd->fd); | |
2376 | monfd->fd = fd; | |
208c9d1b | 2377 | return; |
f07918fd MM |
2378 | } |
2379 | ||
7267c094 AL |
2380 | monfd = g_malloc0(sizeof(mon_fd_t)); |
2381 | monfd->name = g_strdup(fdname); | |
f07918fd MM |
2382 | monfd->fd = fd; |
2383 | ||
208c9d1b | 2384 | QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next); |
f07918fd MM |
2385 | } |
2386 | ||
208c9d1b | 2387 | void qmp_closefd(const char *fdname, Error **errp) |
f07918fd | 2388 | { |
c227f099 | 2389 | mon_fd_t *monfd; |
f07918fd | 2390 | |
208c9d1b | 2391 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
f07918fd MM |
2392 | if (strcmp(monfd->name, fdname) != 0) { |
2393 | continue; | |
2394 | } | |
2395 | ||
72cf2d4f | 2396 | QLIST_REMOVE(monfd, next); |
f07918fd | 2397 | close(monfd->fd); |
7267c094 AL |
2398 | g_free(monfd->name); |
2399 | g_free(monfd); | |
208c9d1b | 2400 | return; |
f07918fd MM |
2401 | } |
2402 | ||
208c9d1b | 2403 | error_set(errp, QERR_FD_NOT_FOUND, fdname); |
f07918fd MM |
2404 | } |
2405 | ||
d54908a5 | 2406 | static void do_loadvm(Monitor *mon, const QDict *qdict) |
c8d41b2c | 2407 | { |
1354869c | 2408 | int saved_vm_running = runstate_is_running(); |
d54908a5 | 2409 | const char *name = qdict_get_str(qdict, "name"); |
c8d41b2c | 2410 | |
0461d5a6 | 2411 | vm_stop(RUN_STATE_RESTORE_VM); |
c8d41b2c | 2412 | |
f0aa7a8b | 2413 | if (load_vmstate(name) == 0 && saved_vm_running) { |
c8d41b2c | 2414 | vm_start(); |
f0aa7a8b | 2415 | } |
c8d41b2c JQ |
2416 | } |
2417 | ||
a9940fc4 | 2418 | int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) |
7768e04c | 2419 | { |
c227f099 | 2420 | mon_fd_t *monfd; |
7768e04c | 2421 | |
72cf2d4f | 2422 | QLIST_FOREACH(monfd, &mon->fds, next) { |
7768e04c MM |
2423 | int fd; |
2424 | ||
2425 | if (strcmp(monfd->name, fdname) != 0) { | |
2426 | continue; | |
2427 | } | |
2428 | ||
2429 | fd = monfd->fd; | |
2430 | ||
2431 | /* caller takes ownership of fd */ | |
72cf2d4f | 2432 | QLIST_REMOVE(monfd, next); |
7267c094 AL |
2433 | g_free(monfd->name); |
2434 | g_free(monfd); | |
7768e04c MM |
2435 | |
2436 | return fd; | |
2437 | } | |
2438 | ||
a9940fc4 | 2439 | error_setg(errp, "File descriptor named '%s' has not been found", fdname); |
7768e04c MM |
2440 | return -1; |
2441 | } | |
2442 | ||
ba1c048a CB |
2443 | static void monitor_fdset_cleanup(MonFdset *mon_fdset) |
2444 | { | |
2445 | MonFdsetFd *mon_fdset_fd; | |
2446 | MonFdsetFd *mon_fdset_fd_next; | |
2447 | ||
2448 | QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) { | |
ebe52b59 CB |
2449 | if ((mon_fdset_fd->removed || |
2450 | (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) && | |
2451 | runstate_is_running()) { | |
ba1c048a CB |
2452 | close(mon_fdset_fd->fd); |
2453 | g_free(mon_fdset_fd->opaque); | |
2454 | QLIST_REMOVE(mon_fdset_fd, next); | |
2455 | g_free(mon_fdset_fd); | |
2456 | } | |
2457 | } | |
2458 | ||
adb696f3 | 2459 | if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) { |
ba1c048a CB |
2460 | QLIST_REMOVE(mon_fdset, next); |
2461 | g_free(mon_fdset); | |
2462 | } | |
2463 | } | |
2464 | ||
efb87c16 CB |
2465 | static void monitor_fdsets_cleanup(void) |
2466 | { | |
2467 | MonFdset *mon_fdset; | |
2468 | MonFdset *mon_fdset_next; | |
2469 | ||
2470 | QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) { | |
2471 | monitor_fdset_cleanup(mon_fdset); | |
2472 | } | |
2473 | } | |
2474 | ||
ba1c048a CB |
2475 | AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque, |
2476 | const char *opaque, Error **errp) | |
2477 | { | |
2478 | int fd; | |
2479 | Monitor *mon = cur_mon; | |
ba1c048a CB |
2480 | AddfdInfo *fdinfo; |
2481 | ||
2482 | fd = qemu_chr_fe_get_msgfd(mon->chr); | |
2483 | if (fd == -1) { | |
2484 | error_set(errp, QERR_FD_NOT_SUPPLIED); | |
2485 | goto error; | |
2486 | } | |
2487 | ||
e446f70d CB |
2488 | fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id, |
2489 | has_opaque, opaque, errp); | |
2490 | if (fdinfo) { | |
2491 | return fdinfo; | |
ba1c048a | 2492 | } |
ba1c048a CB |
2493 | |
2494 | error: | |
2495 | if (fd != -1) { | |
2496 | close(fd); | |
2497 | } | |
2498 | return NULL; | |
2499 | } | |
2500 | ||
2501 | void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) | |
2502 | { | |
2503 | MonFdset *mon_fdset; | |
2504 | MonFdsetFd *mon_fdset_fd; | |
2505 | char fd_str[60]; | |
2506 | ||
2507 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2508 | if (mon_fdset->id != fdset_id) { | |
2509 | continue; | |
2510 | } | |
2511 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2512 | if (has_fd) { | |
2513 | if (mon_fdset_fd->fd != fd) { | |
2514 | continue; | |
2515 | } | |
2516 | mon_fdset_fd->removed = true; | |
2517 | break; | |
2518 | } else { | |
2519 | mon_fdset_fd->removed = true; | |
2520 | } | |
2521 | } | |
2522 | if (has_fd && !mon_fdset_fd) { | |
2523 | goto error; | |
2524 | } | |
2525 | monitor_fdset_cleanup(mon_fdset); | |
2526 | return; | |
2527 | } | |
2528 | ||
2529 | error: | |
2530 | if (has_fd) { | |
2531 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64, | |
2532 | fdset_id, fd); | |
2533 | } else { | |
2534 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id); | |
2535 | } | |
2536 | error_set(errp, QERR_FD_NOT_FOUND, fd_str); | |
2537 | } | |
2538 | ||
2539 | FdsetInfoList *qmp_query_fdsets(Error **errp) | |
2540 | { | |
2541 | MonFdset *mon_fdset; | |
2542 | MonFdsetFd *mon_fdset_fd; | |
2543 | FdsetInfoList *fdset_list = NULL; | |
2544 | ||
2545 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2546 | FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info)); | |
2547 | FdsetFdInfoList *fdsetfd_list = NULL; | |
2548 | ||
2549 | fdset_info->value = g_malloc0(sizeof(*fdset_info->value)); | |
2550 | fdset_info->value->fdset_id = mon_fdset->id; | |
2551 | ||
2552 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2553 | FdsetFdInfoList *fdsetfd_info; | |
2554 | ||
2555 | fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info)); | |
2556 | fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value)); | |
2557 | fdsetfd_info->value->fd = mon_fdset_fd->fd; | |
2558 | if (mon_fdset_fd->opaque) { | |
2559 | fdsetfd_info->value->has_opaque = true; | |
2560 | fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque); | |
2561 | } else { | |
2562 | fdsetfd_info->value->has_opaque = false; | |
2563 | } | |
2564 | ||
2565 | fdsetfd_info->next = fdsetfd_list; | |
2566 | fdsetfd_list = fdsetfd_info; | |
2567 | } | |
2568 | ||
2569 | fdset_info->value->fds = fdsetfd_list; | |
2570 | ||
2571 | fdset_info->next = fdset_list; | |
2572 | fdset_list = fdset_info; | |
2573 | } | |
2574 | ||
2575 | return fdset_list; | |
2576 | } | |
2577 | ||
e446f70d CB |
2578 | AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id, |
2579 | bool has_opaque, const char *opaque, | |
2580 | Error **errp) | |
2581 | { | |
2582 | MonFdset *mon_fdset = NULL; | |
2583 | MonFdsetFd *mon_fdset_fd; | |
2584 | AddfdInfo *fdinfo; | |
2585 | ||
2586 | if (has_fdset_id) { | |
2587 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2588 | /* Break if match found or match impossible due to ordering by ID */ | |
2589 | if (fdset_id <= mon_fdset->id) { | |
2590 | if (fdset_id < mon_fdset->id) { | |
2591 | mon_fdset = NULL; | |
2592 | } | |
2593 | break; | |
2594 | } | |
2595 | } | |
2596 | } | |
2597 | ||
2598 | if (mon_fdset == NULL) { | |
2599 | int64_t fdset_id_prev = -1; | |
2600 | MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets); | |
2601 | ||
2602 | if (has_fdset_id) { | |
2603 | if (fdset_id < 0) { | |
2604 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id", | |
2605 | "a non-negative value"); | |
2606 | return NULL; | |
2607 | } | |
2608 | /* Use specified fdset ID */ | |
2609 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2610 | mon_fdset_cur = mon_fdset; | |
2611 | if (fdset_id < mon_fdset_cur->id) { | |
2612 | break; | |
2613 | } | |
2614 | } | |
2615 | } else { | |
2616 | /* Use first available fdset ID */ | |
2617 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2618 | mon_fdset_cur = mon_fdset; | |
2619 | if (fdset_id_prev == mon_fdset_cur->id - 1) { | |
2620 | fdset_id_prev = mon_fdset_cur->id; | |
2621 | continue; | |
2622 | } | |
2623 | break; | |
2624 | } | |
2625 | } | |
2626 | ||
2627 | mon_fdset = g_malloc0(sizeof(*mon_fdset)); | |
2628 | if (has_fdset_id) { | |
2629 | mon_fdset->id = fdset_id; | |
2630 | } else { | |
2631 | mon_fdset->id = fdset_id_prev + 1; | |
2632 | } | |
2633 | ||
2634 | /* The fdset list is ordered by fdset ID */ | |
2635 | if (!mon_fdset_cur) { | |
2636 | QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next); | |
2637 | } else if (mon_fdset->id < mon_fdset_cur->id) { | |
2638 | QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next); | |
2639 | } else { | |
2640 | QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next); | |
2641 | } | |
2642 | } | |
2643 | ||
2644 | mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd)); | |
2645 | mon_fdset_fd->fd = fd; | |
2646 | mon_fdset_fd->removed = false; | |
2647 | if (has_opaque) { | |
2648 | mon_fdset_fd->opaque = g_strdup(opaque); | |
2649 | } | |
2650 | QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next); | |
2651 | ||
2652 | fdinfo = g_malloc0(sizeof(*fdinfo)); | |
2653 | fdinfo->fdset_id = mon_fdset->id; | |
2654 | fdinfo->fd = mon_fdset_fd->fd; | |
2655 | ||
2656 | return fdinfo; | |
2657 | } | |
2658 | ||
adb696f3 CB |
2659 | int monitor_fdset_get_fd(int64_t fdset_id, int flags) |
2660 | { | |
b2dc64c3 | 2661 | #ifndef _WIN32 |
adb696f3 CB |
2662 | MonFdset *mon_fdset; |
2663 | MonFdsetFd *mon_fdset_fd; | |
2664 | int mon_fd_flags; | |
2665 | ||
adb696f3 CB |
2666 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
2667 | if (mon_fdset->id != fdset_id) { | |
2668 | continue; | |
2669 | } | |
2670 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2671 | mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL); | |
2672 | if (mon_fd_flags == -1) { | |
2673 | return -1; | |
2674 | } | |
2675 | ||
2676 | if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) { | |
2677 | return mon_fdset_fd->fd; | |
2678 | } | |
2679 | } | |
2680 | errno = EACCES; | |
2681 | return -1; | |
2682 | } | |
2683 | #endif | |
2684 | ||
2685 | errno = ENOENT; | |
2686 | return -1; | |
2687 | } | |
2688 | ||
2689 | int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd) | |
2690 | { | |
2691 | MonFdset *mon_fdset; | |
2692 | MonFdsetFd *mon_fdset_fd_dup; | |
2693 | ||
2694 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2695 | if (mon_fdset->id != fdset_id) { | |
2696 | continue; | |
2697 | } | |
2698 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { | |
2699 | if (mon_fdset_fd_dup->fd == dup_fd) { | |
2700 | return -1; | |
2701 | } | |
2702 | } | |
2703 | mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup)); | |
2704 | mon_fdset_fd_dup->fd = dup_fd; | |
2705 | QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next); | |
2706 | return 0; | |
2707 | } | |
2708 | return -1; | |
2709 | } | |
2710 | ||
2711 | static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) | |
2712 | { | |
2713 | MonFdset *mon_fdset; | |
2714 | MonFdsetFd *mon_fdset_fd_dup; | |
2715 | ||
2716 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2717 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { | |
2718 | if (mon_fdset_fd_dup->fd == dup_fd) { | |
2719 | if (remove) { | |
2720 | QLIST_REMOVE(mon_fdset_fd_dup, next); | |
2721 | if (QLIST_EMPTY(&mon_fdset->dup_fds)) { | |
2722 | monitor_fdset_cleanup(mon_fdset); | |
2723 | } | |
2724 | } | |
2725 | return mon_fdset->id; | |
2726 | } | |
2727 | } | |
2728 | } | |
2729 | return -1; | |
2730 | } | |
2731 | ||
2732 | int monitor_fdset_dup_fd_find(int dup_fd) | |
2733 | { | |
2734 | return monitor_fdset_dup_fd_find_remove(dup_fd, false); | |
2735 | } | |
2736 | ||
2737 | int monitor_fdset_dup_fd_remove(int dup_fd) | |
2738 | { | |
2739 | return monitor_fdset_dup_fd_find_remove(dup_fd, true); | |
2740 | } | |
2741 | ||
a96ed02f NB |
2742 | int monitor_handle_fd_param(Monitor *mon, const char *fdname) |
2743 | { | |
2744 | int fd; | |
a9940fc4 | 2745 | Error *local_err = NULL; |
a96ed02f | 2746 | |
5906366e LE |
2747 | fd = monitor_handle_fd_param2(mon, fdname, &local_err); |
2748 | if (local_err) { | |
2749 | qerror_report_err(local_err); | |
2750 | error_free(local_err); | |
2751 | } | |
2752 | return fd; | |
2753 | } | |
2754 | ||
2755 | int monitor_handle_fd_param2(Monitor *mon, const char *fdname, Error **errp) | |
2756 | { | |
2757 | int fd; | |
2758 | Error *local_err = NULL; | |
a96ed02f | 2759 | |
5906366e | 2760 | if (!qemu_isdigit(fdname[0]) && mon) { |
a9940fc4 | 2761 | fd = monitor_get_fd(mon, fdname, &local_err); |
5906366e LE |
2762 | } else { |
2763 | fd = qemu_parse_fd(fdname); | |
a96ed02f | 2764 | if (fd == -1) { |
5906366e LE |
2765 | error_setg(&local_err, "Invalid file descriptor number '%s'", |
2766 | fdname); | |
a96ed02f | 2767 | } |
5906366e LE |
2768 | } |
2769 | if (local_err) { | |
2770 | error_propagate(errp, local_err); | |
2771 | assert(fd == -1); | |
a96ed02f | 2772 | } else { |
5906366e | 2773 | assert(fd != -1); |
a96ed02f NB |
2774 | } |
2775 | ||
2776 | return fd; | |
2777 | } | |
2778 | ||
acd0a093 | 2779 | /* Please update hmp-commands.hx when adding or changing commands */ |
816f8925 | 2780 | static mon_cmd_t info_cmds[] = { |
d7f9b689 LC |
2781 | { |
2782 | .name = "version", | |
2783 | .args_type = "", | |
d7f9b689 LC |
2784 | .params = "", |
2785 | .help = "show the version of QEMU", | |
5f11cb00 | 2786 | .mhandler.cmd = hmp_info_version, |
d7f9b689 LC |
2787 | }, |
2788 | { | |
2789 | .name = "network", | |
2790 | .args_type = "", | |
d7f9b689 LC |
2791 | .params = "", |
2792 | .help = "show the network state", | |
5f11cb00 | 2793 | .mhandler.cmd = do_info_network, |
d7f9b689 LC |
2794 | }, |
2795 | { | |
2796 | .name = "chardev", | |
2797 | .args_type = "", | |
d7f9b689 LC |
2798 | .params = "", |
2799 | .help = "show the character devices", | |
5f11cb00 | 2800 | .mhandler.cmd = hmp_info_chardev, |
d7f9b689 LC |
2801 | }, |
2802 | { | |
2803 | .name = "block", | |
e73fe2b4 WX |
2804 | .args_type = "verbose:-v,device:B?", |
2805 | .params = "[-v] [device]", | |
2806 | .help = "show info of one block device or all block devices " | |
2807 | "(and details of images with -v option)", | |
5f11cb00 | 2808 | .mhandler.cmd = hmp_info_block, |
d7f9b689 LC |
2809 | }, |
2810 | { | |
2811 | .name = "blockstats", | |
2812 | .args_type = "", | |
d7f9b689 LC |
2813 | .params = "", |
2814 | .help = "show block device statistics", | |
5f11cb00 | 2815 | .mhandler.cmd = hmp_info_blockstats, |
d7f9b689 | 2816 | }, |
fb5458cd SH |
2817 | { |
2818 | .name = "block-jobs", | |
2819 | .args_type = "", | |
2820 | .params = "", | |
2821 | .help = "show progress of ongoing block device operations", | |
5f11cb00 | 2822 | .mhandler.cmd = hmp_info_block_jobs, |
fb5458cd | 2823 | }, |
d7f9b689 LC |
2824 | { |
2825 | .name = "registers", | |
2826 | .args_type = "", | |
d7f9b689 LC |
2827 | .params = "", |
2828 | .help = "show the cpu registers", | |
5f11cb00 | 2829 | .mhandler.cmd = do_info_registers, |
d7f9b689 LC |
2830 | }, |
2831 | { | |
2832 | .name = "cpus", | |
2833 | .args_type = "", | |
d7f9b689 LC |
2834 | .params = "", |
2835 | .help = "show infos for each CPU", | |
5f11cb00 | 2836 | .mhandler.cmd = hmp_info_cpus, |
d7f9b689 LC |
2837 | }, |
2838 | { | |
2839 | .name = "history", | |
2840 | .args_type = "", | |
d7f9b689 LC |
2841 | .params = "", |
2842 | .help = "show the command line history", | |
5f11cb00 | 2843 | .mhandler.cmd = do_info_history, |
d7f9b689 | 2844 | }, |
661f1929 JK |
2845 | #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \ |
2846 | defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64)) | |
d7f9b689 LC |
2847 | { |
2848 | .name = "irq", | |
2849 | .args_type = "", | |
d7f9b689 LC |
2850 | .params = "", |
2851 | .help = "show the interrupts statistics (if available)", | |
661f1929 | 2852 | #ifdef TARGET_SPARC |
5f11cb00 | 2853 | .mhandler.cmd = sun4m_irq_info, |
661f1929 | 2854 | #elif defined(TARGET_LM32) |
5f11cb00 | 2855 | .mhandler.cmd = lm32_irq_info, |
661f1929 | 2856 | #else |
5f11cb00 | 2857 | .mhandler.cmd = irq_info, |
661f1929 | 2858 | #endif |
d7f9b689 LC |
2859 | }, |
2860 | { | |
2861 | .name = "pic", | |
2862 | .args_type = "", | |
d7f9b689 LC |
2863 | .params = "", |
2864 | .help = "show i8259 (PIC) state", | |
661f1929 | 2865 | #ifdef TARGET_SPARC |
5f11cb00 | 2866 | .mhandler.cmd = sun4m_pic_info, |
661f1929 | 2867 | #elif defined(TARGET_LM32) |
5f11cb00 | 2868 | .mhandler.cmd = lm32_do_pic_info, |
661f1929 | 2869 | #else |
5f11cb00 | 2870 | .mhandler.cmd = pic_info, |
661f1929 | 2871 | #endif |
d7f9b689 | 2872 | }, |
661f1929 | 2873 | #endif |
d7f9b689 LC |
2874 | { |
2875 | .name = "pci", | |
2876 | .args_type = "", | |
d7f9b689 LC |
2877 | .params = "", |
2878 | .help = "show PCI info", | |
5f11cb00 | 2879 | .mhandler.cmd = hmp_info_pci, |
d7f9b689 | 2880 | }, |
bebabbc7 | 2881 | #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \ |
692f737c | 2882 | defined(TARGET_PPC) || defined(TARGET_XTENSA) |
d7f9b689 LC |
2883 | { |
2884 | .name = "tlb", | |
2885 | .args_type = "", | |
d7f9b689 LC |
2886 | .params = "", |
2887 | .help = "show virtual to physical memory mappings", | |
5f11cb00 | 2888 | .mhandler.cmd = tlb_info, |
d7f9b689 | 2889 | }, |
7c664e2f AJ |
2890 | #endif |
2891 | #if defined(TARGET_I386) | |
d7f9b689 LC |
2892 | { |
2893 | .name = "mem", | |
2894 | .args_type = "", | |
d7f9b689 LC |
2895 | .params = "", |
2896 | .help = "show the active virtual memory mappings", | |
5f11cb00 | 2897 | .mhandler.cmd = mem_info, |
d7f9b689 | 2898 | }, |
b86bda5b | 2899 | #endif |
314e2987 BS |
2900 | { |
2901 | .name = "mtree", | |
2902 | .args_type = "", | |
2903 | .params = "", | |
2904 | .help = "show memory tree", | |
5f11cb00 | 2905 | .mhandler.cmd = do_info_mtree, |
314e2987 | 2906 | }, |
d7f9b689 LC |
2907 | { |
2908 | .name = "jit", | |
2909 | .args_type = "", | |
d7f9b689 LC |
2910 | .params = "", |
2911 | .help = "show dynamic compiler info", | |
5f11cb00 | 2912 | .mhandler.cmd = do_info_jit, |
d7f9b689 LC |
2913 | }, |
2914 | { | |
2915 | .name = "kvm", | |
2916 | .args_type = "", | |
d7f9b689 LC |
2917 | .params = "", |
2918 | .help = "show KVM information", | |
5f11cb00 | 2919 | .mhandler.cmd = hmp_info_kvm, |
d7f9b689 LC |
2920 | }, |
2921 | { | |
2922 | .name = "numa", | |
2923 | .args_type = "", | |
d7f9b689 LC |
2924 | .params = "", |
2925 | .help = "show NUMA information", | |
5f11cb00 | 2926 | .mhandler.cmd = do_info_numa, |
d7f9b689 LC |
2927 | }, |
2928 | { | |
2929 | .name = "usb", | |
2930 | .args_type = "", | |
d7f9b689 LC |
2931 | .params = "", |
2932 | .help = "show guest USB devices", | |
5f11cb00 | 2933 | .mhandler.cmd = usb_info, |
d7f9b689 LC |
2934 | }, |
2935 | { | |
2936 | .name = "usbhost", | |
2937 | .args_type = "", | |
d7f9b689 LC |
2938 | .params = "", |
2939 | .help = "show host USB devices", | |
5f11cb00 | 2940 | .mhandler.cmd = usb_host_info, |
d7f9b689 LC |
2941 | }, |
2942 | { | |
2943 | .name = "profile", | |
2944 | .args_type = "", | |
d7f9b689 LC |
2945 | .params = "", |
2946 | .help = "show profiling information", | |
5f11cb00 | 2947 | .mhandler.cmd = do_info_profile, |
d7f9b689 LC |
2948 | }, |
2949 | { | |
2950 | .name = "capture", | |
2951 | .args_type = "", | |
d7f9b689 LC |
2952 | .params = "", |
2953 | .help = "show capture information", | |
5f11cb00 | 2954 | .mhandler.cmd = do_info_capture, |
d7f9b689 LC |
2955 | }, |
2956 | { | |
2957 | .name = "snapshots", | |
2958 | .args_type = "", | |
d7f9b689 LC |
2959 | .params = "", |
2960 | .help = "show the currently saved VM snapshots", | |
5f11cb00 | 2961 | .mhandler.cmd = do_info_snapshots, |
d7f9b689 LC |
2962 | }, |
2963 | { | |
2964 | .name = "status", | |
2965 | .args_type = "", | |
d7f9b689 LC |
2966 | .params = "", |
2967 | .help = "show the current VM status (running|paused)", | |
5f11cb00 | 2968 | .mhandler.cmd = hmp_info_status, |
d7f9b689 LC |
2969 | }, |
2970 | { | |
2971 | .name = "pcmcia", | |
2972 | .args_type = "", | |
d7f9b689 LC |
2973 | .params = "", |
2974 | .help = "show guest PCMCIA status", | |
5f11cb00 | 2975 | .mhandler.cmd = pcmcia_info, |
d7f9b689 LC |
2976 | }, |
2977 | { | |
2978 | .name = "mice", | |
2979 | .args_type = "", | |
d7f9b689 LC |
2980 | .params = "", |
2981 | .help = "show which guest mouse is receiving events", | |
5f11cb00 | 2982 | .mhandler.cmd = hmp_info_mice, |
d7f9b689 LC |
2983 | }, |
2984 | { | |
2985 | .name = "vnc", | |
2986 | .args_type = "", | |
d7f9b689 LC |
2987 | .params = "", |
2988 | .help = "show the vnc server status", | |
5f11cb00 | 2989 | .mhandler.cmd = hmp_info_vnc, |
d7f9b689 | 2990 | }, |
cb42a870 GH |
2991 | #if defined(CONFIG_SPICE) |
2992 | { | |
2993 | .name = "spice", | |
2994 | .args_type = "", | |
2995 | .params = "", | |
2996 | .help = "show the spice server status", | |
5f11cb00 | 2997 | .mhandler.cmd = hmp_info_spice, |
cb42a870 GH |
2998 | }, |
2999 | #endif | |
d7f9b689 LC |
3000 | { |
3001 | .name = "name", | |
3002 | .args_type = "", | |
d7f9b689 LC |
3003 | .params = "", |
3004 | .help = "show the current VM name", | |
5f11cb00 | 3005 | .mhandler.cmd = hmp_info_name, |
d7f9b689 LC |
3006 | }, |
3007 | { | |
3008 | .name = "uuid", | |
3009 | .args_type = "", | |
d7f9b689 LC |
3010 | .params = "", |
3011 | .help = "show the current VM UUID", | |
5f11cb00 | 3012 | .mhandler.cmd = hmp_info_uuid, |
d7f9b689 | 3013 | }, |
d7f9b689 LC |
3014 | { |
3015 | .name = "cpustats", | |
3016 | .args_type = "", | |
d7f9b689 LC |
3017 | .params = "", |
3018 | .help = "show CPU statistics", | |
5f11cb00 | 3019 | .mhandler.cmd = do_info_cpu_stats, |
d7f9b689 | 3020 | }, |
31a60e22 | 3021 | #if defined(CONFIG_SLIRP) |
d7f9b689 LC |
3022 | { |
3023 | .name = "usernet", | |
3024 | .args_type = "", | |
d7f9b689 LC |
3025 | .params = "", |
3026 | .help = "show user network stack connection states", | |
5f11cb00 | 3027 | .mhandler.cmd = do_info_usernet, |
d7f9b689 | 3028 | }, |
31a60e22 | 3029 | #endif |
d7f9b689 LC |
3030 | { |
3031 | .name = "migrate", | |
3032 | .args_type = "", | |
d7f9b689 LC |
3033 | .params = "", |
3034 | .help = "show migration status", | |
5f11cb00 | 3035 | .mhandler.cmd = hmp_info_migrate, |
d7f9b689 | 3036 | }, |
bbf6da32 OW |
3037 | { |
3038 | .name = "migrate_capabilities", | |
3039 | .args_type = "", | |
3040 | .params = "", | |
3041 | .help = "show current migration capabilities", | |
5f11cb00 | 3042 | .mhandler.cmd = hmp_info_migrate_capabilities, |
bbf6da32 | 3043 | }, |
9e1ba4cc OW |
3044 | { |
3045 | .name = "migrate_cache_size", | |
3046 | .args_type = "", | |
3047 | .params = "", | |
3048 | .help = "show current migration xbzrle cache size", | |
5f11cb00 | 3049 | .mhandler.cmd = hmp_info_migrate_cache_size, |
9e1ba4cc | 3050 | }, |
d7f9b689 LC |
3051 | { |
3052 | .name = "balloon", | |
3053 | .args_type = "", | |
d7f9b689 LC |
3054 | .params = "", |
3055 | .help = "show balloon information", | |
5f11cb00 | 3056 | .mhandler.cmd = hmp_info_balloon, |
d7f9b689 LC |
3057 | }, |
3058 | { | |
3059 | .name = "qtree", | |
3060 | .args_type = "", | |
d7f9b689 LC |
3061 | .params = "", |
3062 | .help = "show device tree", | |
5f11cb00 | 3063 | .mhandler.cmd = do_info_qtree, |
d7f9b689 LC |
3064 | }, |
3065 | { | |
3066 | .name = "qdm", | |
3067 | .args_type = "", | |
d7f9b689 LC |
3068 | .params = "", |
3069 | .help = "show qdev device model list", | |
5f11cb00 | 3070 | .mhandler.cmd = do_info_qdm, |
d7f9b689 LC |
3071 | }, |
3072 | { | |
3073 | .name = "roms", | |
3074 | .args_type = "", | |
d7f9b689 LC |
3075 | .params = "", |
3076 | .help = "show roms", | |
5f11cb00 | 3077 | .mhandler.cmd = do_info_roms, |
d7f9b689 | 3078 | }, |
22890ab5 PS |
3079 | { |
3080 | .name = "trace-events", | |
3081 | .args_type = "", | |
3082 | .params = "", | |
3083 | .help = "show available trace-events & their state", | |
5f11cb00 | 3084 | .mhandler.cmd = do_trace_print_events, |
22890ab5 | 3085 | }, |
d1a0cf73 SB |
3086 | { |
3087 | .name = "tpm", | |
3088 | .args_type = "", | |
3089 | .params = "", | |
3090 | .help = "show the TPM device", | |
3091 | .mhandler.cmd = hmp_info_tpm, | |
3092 | }, | |
eb1539b2 HT |
3093 | { |
3094 | .name = "memdev", | |
3095 | .args_type = "", | |
3096 | .params = "", | |
3097 | .help = "show the memory device", | |
3098 | .mhandler.cmd = hmp_info_memdev, | |
3099 | }, | |
d7f9b689 LC |
3100 | { |
3101 | .name = NULL, | |
3102 | }, | |
9dc39cba FB |
3103 | }; |
3104 | ||
a13ced59 WX |
3105 | /* mon_cmds and info_cmds would be sorted at runtime */ |
3106 | static mon_cmd_t mon_cmds[] = { | |
3107 | #include "hmp-commands.h" | |
3108 | { NULL, NULL, }, | |
3109 | }; | |
3110 | ||
f36b4afb | 3111 | static const mon_cmd_t qmp_cmds[] = { |
e3193601 | 3112 | #include "qmp-commands-old.h" |
f36b4afb LC |
3113 | { /* NULL */ }, |
3114 | }; | |
3115 | ||
9307c4c1 FB |
3116 | /*******************************************************************/ |
3117 | ||
3118 | static const char *pch; | |
6ab7e546 | 3119 | static sigjmp_buf expr_env; |
9307c4c1 | 3120 | |
92a31b1f FB |
3121 | #define MD_TLONG 0 |
3122 | #define MD_I32 1 | |
3123 | ||
9307c4c1 FB |
3124 | typedef struct MonitorDef { |
3125 | const char *name; | |
3126 | int offset; | |
8662d656 | 3127 | target_long (*get_value)(const struct MonitorDef *md, int val); |
92a31b1f | 3128 | int type; |
9307c4c1 FB |
3129 | } MonitorDef; |
3130 | ||
57206fd4 | 3131 | #if defined(TARGET_I386) |
8662d656 | 3132 | static target_long monitor_get_pc (const struct MonitorDef *md, int val) |
57206fd4 | 3133 | { |
9349b4f9 | 3134 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 3135 | return env->eip + env->segs[R_CS].base; |
57206fd4 FB |
3136 | } |
3137 | #endif | |
3138 | ||
a541f297 | 3139 | #if defined(TARGET_PPC) |
8662d656 | 3140 | static target_long monitor_get_ccr (const struct MonitorDef *md, int val) |
a541f297 | 3141 | { |
9349b4f9 | 3142 | CPUArchState *env = mon_get_cpu(); |
a541f297 FB |
3143 | unsigned int u; |
3144 | int i; | |
3145 | ||
3146 | u = 0; | |
3147 | for (i = 0; i < 8; i++) | |
28a76be8 | 3148 | u |= env->crf[i] << (32 - (4 * i)); |
a541f297 FB |
3149 | |
3150 | return u; | |
3151 | } | |
3152 | ||
8662d656 | 3153 | static target_long monitor_get_msr (const struct MonitorDef *md, int val) |
a541f297 | 3154 | { |
9349b4f9 | 3155 | CPUArchState *env = mon_get_cpu(); |
0411a972 | 3156 | return env->msr; |
a541f297 FB |
3157 | } |
3158 | ||
8662d656 | 3159 | static target_long monitor_get_xer (const struct MonitorDef *md, int val) |
a541f297 | 3160 | { |
9349b4f9 | 3161 | CPUArchState *env = mon_get_cpu(); |
3d7b417e | 3162 | return env->xer; |
a541f297 | 3163 | } |
9fddaa0c | 3164 | |
8662d656 | 3165 | static target_long monitor_get_decr (const struct MonitorDef *md, int val) |
9fddaa0c | 3166 | { |
9349b4f9 | 3167 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 3168 | return cpu_ppc_load_decr(env); |
9fddaa0c FB |
3169 | } |
3170 | ||
8662d656 | 3171 | static target_long monitor_get_tbu (const struct MonitorDef *md, int val) |
9fddaa0c | 3172 | { |
9349b4f9 | 3173 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 3174 | return cpu_ppc_load_tbu(env); |
9fddaa0c FB |
3175 | } |
3176 | ||
8662d656 | 3177 | static target_long monitor_get_tbl (const struct MonitorDef *md, int val) |
9fddaa0c | 3178 | { |
9349b4f9 | 3179 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 3180 | return cpu_ppc_load_tbl(env); |
9fddaa0c | 3181 | } |
a541f297 FB |
3182 | #endif |
3183 | ||
e95c8d51 | 3184 | #if defined(TARGET_SPARC) |
7b936c0c | 3185 | #ifndef TARGET_SPARC64 |
8662d656 | 3186 | static target_long monitor_get_psr (const struct MonitorDef *md, int val) |
e95c8d51 | 3187 | { |
9349b4f9 | 3188 | CPUArchState *env = mon_get_cpu(); |
5a834bb4 BS |
3189 | |
3190 | return cpu_get_psr(env); | |
e95c8d51 | 3191 | } |
7b936c0c | 3192 | #endif |
e95c8d51 | 3193 | |
8662d656 | 3194 | static target_long monitor_get_reg(const struct MonitorDef *md, int val) |
e95c8d51 | 3195 | { |
9349b4f9 | 3196 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 3197 | return env->regwptr[val]; |
e95c8d51 FB |
3198 | } |
3199 | #endif | |
3200 | ||
8662d656 | 3201 | static const MonitorDef monitor_defs[] = { |
9307c4c1 | 3202 | #ifdef TARGET_I386 |
57206fd4 FB |
3203 | |
3204 | #define SEG(name, seg) \ | |
e59d167f AF |
3205 | { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\ |
3206 | { name ".base", offsetof(CPUX86State, segs[seg].base) },\ | |
3207 | { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 }, | |
3208 | ||
3209 | { "eax", offsetof(CPUX86State, regs[0]) }, | |
3210 | { "ecx", offsetof(CPUX86State, regs[1]) }, | |
3211 | { "edx", offsetof(CPUX86State, regs[2]) }, | |
3212 | { "ebx", offsetof(CPUX86State, regs[3]) }, | |
3213 | { "esp|sp", offsetof(CPUX86State, regs[4]) }, | |
3214 | { "ebp|fp", offsetof(CPUX86State, regs[5]) }, | |
3215 | { "esi", offsetof(CPUX86State, regs[6]) }, | |
3216 | { "edi", offsetof(CPUX86State, regs[7]) }, | |
92a31b1f | 3217 | #ifdef TARGET_X86_64 |
e59d167f AF |
3218 | { "r8", offsetof(CPUX86State, regs[8]) }, |
3219 | { "r9", offsetof(CPUX86State, regs[9]) }, | |
3220 | { "r10", offsetof(CPUX86State, regs[10]) }, | |
3221 | { "r11", offsetof(CPUX86State, regs[11]) }, | |
3222 | { "r12", offsetof(CPUX86State, regs[12]) }, | |
3223 | { "r13", offsetof(CPUX86State, regs[13]) }, | |
3224 | { "r14", offsetof(CPUX86State, regs[14]) }, | |
3225 | { "r15", offsetof(CPUX86State, regs[15]) }, | |
92a31b1f | 3226 | #endif |
e59d167f AF |
3227 | { "eflags", offsetof(CPUX86State, eflags) }, |
3228 | { "eip", offsetof(CPUX86State, eip) }, | |
57206fd4 FB |
3229 | SEG("cs", R_CS) |
3230 | SEG("ds", R_DS) | |
3231 | SEG("es", R_ES) | |
01038d2a | 3232 | SEG("ss", R_SS) |
57206fd4 FB |
3233 | SEG("fs", R_FS) |
3234 | SEG("gs", R_GS) | |
3235 | { "pc", 0, monitor_get_pc, }, | |
a541f297 | 3236 | #elif defined(TARGET_PPC) |
ff937dba | 3237 | /* General purpose registers */ |
e59d167f AF |
3238 | { "r0", offsetof(CPUPPCState, gpr[0]) }, |
3239 | { "r1", offsetof(CPUPPCState, gpr[1]) }, | |
3240 | { "r2", offsetof(CPUPPCState, gpr[2]) }, | |
3241 | { "r3", offsetof(CPUPPCState, gpr[3]) }, | |
3242 | { "r4", offsetof(CPUPPCState, gpr[4]) }, | |
3243 | { "r5", offsetof(CPUPPCState, gpr[5]) }, | |
3244 | { "r6", offsetof(CPUPPCState, gpr[6]) }, | |
3245 | { "r7", offsetof(CPUPPCState, gpr[7]) }, | |
3246 | { "r8", offsetof(CPUPPCState, gpr[8]) }, | |
3247 | { "r9", offsetof(CPUPPCState, gpr[9]) }, | |
3248 | { "r10", offsetof(CPUPPCState, gpr[10]) }, | |
3249 | { "r11", offsetof(CPUPPCState, gpr[11]) }, | |
3250 | { "r12", offsetof(CPUPPCState, gpr[12]) }, | |
3251 | { "r13", offsetof(CPUPPCState, gpr[13]) }, | |
3252 | { "r14", offsetof(CPUPPCState, gpr[14]) }, | |
3253 | { "r15", offsetof(CPUPPCState, gpr[15]) }, | |
3254 | { "r16", offsetof(CPUPPCState, gpr[16]) }, | |
3255 | { "r17", offsetof(CPUPPCState, gpr[17]) }, | |
3256 | { "r18", offsetof(CPUPPCState, gpr[18]) }, | |
3257 | { "r19", offsetof(CPUPPCState, gpr[19]) }, | |
3258 | { "r20", offsetof(CPUPPCState, gpr[20]) }, | |
3259 | { "r21", offsetof(CPUPPCState, gpr[21]) }, | |
3260 | { "r22", offsetof(CPUPPCState, gpr[22]) }, | |
3261 | { "r23", offsetof(CPUPPCState, gpr[23]) }, | |
3262 | { "r24", offsetof(CPUPPCState, gpr[24]) }, | |
3263 | { "r25", offsetof(CPUPPCState, gpr[25]) }, | |
3264 | { "r26", offsetof(CPUPPCState, gpr[26]) }, | |
3265 | { "r27", offsetof(CPUPPCState, gpr[27]) }, | |
3266 | { "r28", offsetof(CPUPPCState, gpr[28]) }, | |
3267 | { "r29", offsetof(CPUPPCState, gpr[29]) }, | |
3268 | { "r30", offsetof(CPUPPCState, gpr[30]) }, | |
3269 | { "r31", offsetof(CPUPPCState, gpr[31]) }, | |
ff937dba | 3270 | /* Floating point registers */ |
e59d167f AF |
3271 | { "f0", offsetof(CPUPPCState, fpr[0]) }, |
3272 | { "f1", offsetof(CPUPPCState, fpr[1]) }, | |
3273 | { "f2", offsetof(CPUPPCState, fpr[2]) }, | |
3274 | { "f3", offsetof(CPUPPCState, fpr[3]) }, | |
3275 | { "f4", offsetof(CPUPPCState, fpr[4]) }, | |
3276 | { "f5", offsetof(CPUPPCState, fpr[5]) }, | |
3277 | { "f6", offsetof(CPUPPCState, fpr[6]) }, | |
3278 | { "f7", offsetof(CPUPPCState, fpr[7]) }, | |
3279 | { "f8", offsetof(CPUPPCState, fpr[8]) }, | |
3280 | { "f9", offsetof(CPUPPCState, fpr[9]) }, | |
3281 | { "f10", offsetof(CPUPPCState, fpr[10]) }, | |
3282 | { "f11", offsetof(CPUPPCState, fpr[11]) }, | |
3283 | { "f12", offsetof(CPUPPCState, fpr[12]) }, | |
3284 | { "f13", offsetof(CPUPPCState, fpr[13]) }, | |
3285 | { "f14", offsetof(CPUPPCState, fpr[14]) }, | |
3286 | { "f15", offsetof(CPUPPCState, fpr[15]) }, | |
3287 | { "f16", offsetof(CPUPPCState, fpr[16]) }, | |
3288 | { "f17", offsetof(CPUPPCState, fpr[17]) }, | |
3289 | { "f18", offsetof(CPUPPCState, fpr[18]) }, | |
3290 | { "f19", offsetof(CPUPPCState, fpr[19]) }, | |
3291 | { "f20", offsetof(CPUPPCState, fpr[20]) }, | |
3292 | { "f21", offsetof(CPUPPCState, fpr[21]) }, | |
3293 | { "f22", offsetof(CPUPPCState, fpr[22]) }, | |
3294 | { "f23", offsetof(CPUPPCState, fpr[23]) }, | |
3295 | { "f24", offsetof(CPUPPCState, fpr[24]) }, | |
3296 | { "f25", offsetof(CPUPPCState, fpr[25]) }, | |
3297 | { "f26", offsetof(CPUPPCState, fpr[26]) }, | |
3298 | { "f27", offsetof(CPUPPCState, fpr[27]) }, | |
3299 | { "f28", offsetof(CPUPPCState, fpr[28]) }, | |
3300 | { "f29", offsetof(CPUPPCState, fpr[29]) }, | |
3301 | { "f30", offsetof(CPUPPCState, fpr[30]) }, | |
3302 | { "f31", offsetof(CPUPPCState, fpr[31]) }, | |
3303 | { "fpscr", offsetof(CPUPPCState, fpscr) }, | |
ff937dba | 3304 | /* Next instruction pointer */ |
e59d167f AF |
3305 | { "nip|pc", offsetof(CPUPPCState, nip) }, |
3306 | { "lr", offsetof(CPUPPCState, lr) }, | |
3307 | { "ctr", offsetof(CPUPPCState, ctr) }, | |
9fddaa0c | 3308 | { "decr", 0, &monitor_get_decr, }, |
a541f297 | 3309 | { "ccr", 0, &monitor_get_ccr, }, |
ff937dba | 3310 | /* Machine state register */ |
a541f297 FB |
3311 | { "msr", 0, &monitor_get_msr, }, |
3312 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
3313 | { "tbu", 0, &monitor_get_tbu, }, |
3314 | { "tbl", 0, &monitor_get_tbl, }, | |
ff937dba | 3315 | /* Segment registers */ |
e59d167f AF |
3316 | { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) }, |
3317 | { "sr0", offsetof(CPUPPCState, sr[0]) }, | |
3318 | { "sr1", offsetof(CPUPPCState, sr[1]) }, | |
3319 | { "sr2", offsetof(CPUPPCState, sr[2]) }, | |
3320 | { "sr3", offsetof(CPUPPCState, sr[3]) }, | |
3321 | { "sr4", offsetof(CPUPPCState, sr[4]) }, | |
3322 | { "sr5", offsetof(CPUPPCState, sr[5]) }, | |
3323 | { "sr6", offsetof(CPUPPCState, sr[6]) }, | |
3324 | { "sr7", offsetof(CPUPPCState, sr[7]) }, | |
3325 | { "sr8", offsetof(CPUPPCState, sr[8]) }, | |
3326 | { "sr9", offsetof(CPUPPCState, sr[9]) }, | |
3327 | { "sr10", offsetof(CPUPPCState, sr[10]) }, | |
3328 | { "sr11", offsetof(CPUPPCState, sr[11]) }, | |
3329 | { "sr12", offsetof(CPUPPCState, sr[12]) }, | |
3330 | { "sr13", offsetof(CPUPPCState, sr[13]) }, | |
3331 | { "sr14", offsetof(CPUPPCState, sr[14]) }, | |
3332 | { "sr15", offsetof(CPUPPCState, sr[15]) }, | |
90dc8812 | 3333 | /* Too lazy to put BATs... */ |
e59d167f AF |
3334 | { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) }, |
3335 | ||
3336 | { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) }, | |
3337 | { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) }, | |
04f1f784 TM |
3338 | { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) }, |
3339 | { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) }, | |
3340 | { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) }, | |
e59d167f AF |
3341 | { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) }, |
3342 | { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) }, | |
3343 | { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) }, | |
3344 | { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) }, | |
3345 | { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) }, | |
3346 | { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) }, | |
3347 | { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) }, | |
3348 | { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) }, | |
3349 | { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) }, | |
3350 | { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) }, | |
3351 | { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) }, | |
3352 | { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) }, | |
3353 | { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) }, | |
3354 | { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) }, | |
3355 | { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) }, | |
3356 | { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) }, | |
3357 | { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) }, | |
3358 | { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) }, | |
3359 | { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) }, | |
3360 | { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) }, | |
3361 | { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) }, | |
3362 | { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) }, | |
3363 | { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) }, | |
3364 | { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) }, | |
3365 | { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) }, | |
3366 | { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) }, | |
3367 | { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) }, | |
3368 | { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) }, | |
3369 | { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) }, | |
3370 | { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) }, | |
3371 | { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) }, | |
3372 | { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) }, | |
3373 | { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) }, | |
3374 | { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) }, | |
3375 | { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) }, | |
3376 | { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) }, | |
3377 | { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) }, | |
3378 | { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) }, | |
3379 | { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) }, | |
3380 | { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) }, | |
3381 | { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) }, | |
3382 | { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) }, | |
3383 | { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) }, | |
3384 | { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) }, | |
3385 | { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) }, | |
3386 | { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) }, | |
3387 | { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) }, | |
3388 | { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) }, | |
3389 | { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) }, | |
3390 | { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) }, | |
3391 | { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) }, | |
3392 | { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) }, | |
3393 | { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) }, | |
3394 | { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) }, | |
3395 | { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) }, | |
3396 | { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) }, | |
3397 | { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) }, | |
3398 | { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) }, | |
3399 | { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) }, | |
3400 | { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) }, | |
3401 | { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) }, | |
3402 | { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) }, | |
3403 | { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) }, | |
3404 | { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) }, | |
90dc8812 | 3405 | |
e95c8d51 | 3406 | #elif defined(TARGET_SPARC) |
e59d167f AF |
3407 | { "g0", offsetof(CPUSPARCState, gregs[0]) }, |
3408 | { "g1", offsetof(CPUSPARCState, gregs[1]) }, | |
3409 | { "g2", offsetof(CPUSPARCState, gregs[2]) }, | |
3410 | { "g3", offsetof(CPUSPARCState, gregs[3]) }, | |
3411 | { "g4", offsetof(CPUSPARCState, gregs[4]) }, | |
3412 | { "g5", offsetof(CPUSPARCState, gregs[5]) }, | |
3413 | { "g6", offsetof(CPUSPARCState, gregs[6]) }, | |
3414 | { "g7", offsetof(CPUSPARCState, gregs[7]) }, | |
e95c8d51 FB |
3415 | { "o0", 0, monitor_get_reg }, |
3416 | { "o1", 1, monitor_get_reg }, | |
3417 | { "o2", 2, monitor_get_reg }, | |
3418 | { "o3", 3, monitor_get_reg }, | |
3419 | { "o4", 4, monitor_get_reg }, | |
3420 | { "o5", 5, monitor_get_reg }, | |
3421 | { "o6", 6, monitor_get_reg }, | |
3422 | { "o7", 7, monitor_get_reg }, | |
3423 | { "l0", 8, monitor_get_reg }, | |
3424 | { "l1", 9, monitor_get_reg }, | |
3425 | { "l2", 10, monitor_get_reg }, | |
3426 | { "l3", 11, monitor_get_reg }, | |
3427 | { "l4", 12, monitor_get_reg }, | |
3428 | { "l5", 13, monitor_get_reg }, | |
3429 | { "l6", 14, monitor_get_reg }, | |
3430 | { "l7", 15, monitor_get_reg }, | |
3431 | { "i0", 16, monitor_get_reg }, | |
3432 | { "i1", 17, monitor_get_reg }, | |
3433 | { "i2", 18, monitor_get_reg }, | |
3434 | { "i3", 19, monitor_get_reg }, | |
3435 | { "i4", 20, monitor_get_reg }, | |
3436 | { "i5", 21, monitor_get_reg }, | |
3437 | { "i6", 22, monitor_get_reg }, | |
3438 | { "i7", 23, monitor_get_reg }, | |
e59d167f AF |
3439 | { "pc", offsetof(CPUSPARCState, pc) }, |
3440 | { "npc", offsetof(CPUSPARCState, npc) }, | |
3441 | { "y", offsetof(CPUSPARCState, y) }, | |
7b936c0c | 3442 | #ifndef TARGET_SPARC64 |
e95c8d51 | 3443 | { "psr", 0, &monitor_get_psr, }, |
e59d167f | 3444 | { "wim", offsetof(CPUSPARCState, wim) }, |
7b936c0c | 3445 | #endif |
e59d167f AF |
3446 | { "tbr", offsetof(CPUSPARCState, tbr) }, |
3447 | { "fsr", offsetof(CPUSPARCState, fsr) }, | |
3448 | { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) }, | |
3449 | { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) }, | |
3450 | { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) }, | |
3451 | { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) }, | |
3452 | { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) }, | |
3453 | { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) }, | |
3454 | { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) }, | |
3455 | { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) }, | |
3456 | { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) }, | |
3457 | { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) }, | |
3458 | { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) }, | |
3459 | { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) }, | |
3460 | { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) }, | |
3461 | { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) }, | |
3462 | { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) }, | |
3463 | { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) }, | |
3464 | { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) }, | |
3465 | { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) }, | |
3466 | { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) }, | |
3467 | { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) }, | |
3468 | { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) }, | |
3469 | { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) }, | |
3470 | { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) }, | |
3471 | { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) }, | |
3472 | { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) }, | |
3473 | { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) }, | |
3474 | { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) }, | |
3475 | { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) }, | |
3476 | { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) }, | |
3477 | { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) }, | |
3478 | { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) }, | |
3479 | { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) }, | |
7b936c0c | 3480 | #ifdef TARGET_SPARC64 |
e59d167f AF |
3481 | { "f32", offsetof(CPUSPARCState, fpr[16]) }, |
3482 | { "f34", offsetof(CPUSPARCState, fpr[17]) }, | |
3483 | { "f36", offsetof(CPUSPARCState, fpr[18]) }, | |
3484 | { "f38", offsetof(CPUSPARCState, fpr[19]) }, | |
3485 | { "f40", offsetof(CPUSPARCState, fpr[20]) }, | |
3486 | { "f42", offsetof(CPUSPARCState, fpr[21]) }, | |
3487 | { "f44", offsetof(CPUSPARCState, fpr[22]) }, | |
3488 | { "f46", offsetof(CPUSPARCState, fpr[23]) }, | |
3489 | { "f48", offsetof(CPUSPARCState, fpr[24]) }, | |
3490 | { "f50", offsetof(CPUSPARCState, fpr[25]) }, | |
3491 | { "f52", offsetof(CPUSPARCState, fpr[26]) }, | |
3492 | { "f54", offsetof(CPUSPARCState, fpr[27]) }, | |
3493 | { "f56", offsetof(CPUSPARCState, fpr[28]) }, | |
3494 | { "f58", offsetof(CPUSPARCState, fpr[29]) }, | |
3495 | { "f60", offsetof(CPUSPARCState, fpr[30]) }, | |
3496 | { "f62", offsetof(CPUSPARCState, fpr[31]) }, | |
3497 | { "asi", offsetof(CPUSPARCState, asi) }, | |
3498 | { "pstate", offsetof(CPUSPARCState, pstate) }, | |
3499 | { "cansave", offsetof(CPUSPARCState, cansave) }, | |
3500 | { "canrestore", offsetof(CPUSPARCState, canrestore) }, | |
3501 | { "otherwin", offsetof(CPUSPARCState, otherwin) }, | |
3502 | { "wstate", offsetof(CPUSPARCState, wstate) }, | |
3503 | { "cleanwin", offsetof(CPUSPARCState, cleanwin) }, | |
3504 | { "fprs", offsetof(CPUSPARCState, fprs) }, | |
7b936c0c | 3505 | #endif |
9307c4c1 FB |
3506 | #endif |
3507 | { NULL }, | |
3508 | }; | |
3509 | ||
9c3175cc SW |
3510 | static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN |
3511 | expr_error(Monitor *mon, const char *fmt, ...) | |
9dc39cba | 3512 | { |
277acfe8 FZ |
3513 | va_list ap; |
3514 | va_start(ap, fmt); | |
3515 | monitor_vprintf(mon, fmt, ap); | |
3516 | monitor_printf(mon, "\n"); | |
3517 | va_end(ap); | |
6ab7e546 | 3518 | siglongjmp(expr_env, 1); |
9307c4c1 FB |
3519 | } |
3520 | ||
09b9418c | 3521 | /* return 0 if OK, -1 if not found */ |
92a31b1f | 3522 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 3523 | { |
8662d656 | 3524 | const MonitorDef *md; |
92a31b1f FB |
3525 | void *ptr; |
3526 | ||
9307c4c1 FB |
3527 | for(md = monitor_defs; md->name != NULL; md++) { |
3528 | if (compare_cmd(name, md->name)) { | |
3529 | if (md->get_value) { | |
e95c8d51 | 3530 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 3531 | } else { |
9349b4f9 | 3532 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 3533 | ptr = (uint8_t *)env + md->offset; |
92a31b1f FB |
3534 | switch(md->type) { |
3535 | case MD_I32: | |
3536 | *pval = *(int32_t *)ptr; | |
3537 | break; | |
3538 | case MD_TLONG: | |
3539 | *pval = *(target_long *)ptr; | |
3540 | break; | |
3541 | default: | |
3542 | *pval = 0; | |
3543 | break; | |
3544 | } | |
9307c4c1 FB |
3545 | } |
3546 | return 0; | |
3547 | } | |
3548 | } | |
3549 | return -1; | |
3550 | } | |
3551 | ||
3552 | static void next(void) | |
3553 | { | |
660f11be | 3554 | if (*pch != '\0') { |
9307c4c1 | 3555 | pch++; |
cd390083 | 3556 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3557 | pch++; |
3558 | } | |
3559 | } | |
3560 | ||
376253ec | 3561 | static int64_t expr_sum(Monitor *mon); |
9307c4c1 | 3562 | |
376253ec | 3563 | static int64_t expr_unary(Monitor *mon) |
9307c4c1 | 3564 | { |
c2efc95d | 3565 | int64_t n; |
9307c4c1 | 3566 | char *p; |
6a00d601 | 3567 | int ret; |
9307c4c1 FB |
3568 | |
3569 | switch(*pch) { | |
3570 | case '+': | |
3571 | next(); | |
376253ec | 3572 | n = expr_unary(mon); |
9307c4c1 FB |
3573 | break; |
3574 | case '-': | |
3575 | next(); | |
376253ec | 3576 | n = -expr_unary(mon); |
9307c4c1 FB |
3577 | break; |
3578 | case '~': | |
3579 | next(); | |
376253ec | 3580 | n = ~expr_unary(mon); |
9307c4c1 FB |
3581 | break; |
3582 | case '(': | |
3583 | next(); | |
376253ec | 3584 | n = expr_sum(mon); |
9307c4c1 | 3585 | if (*pch != ')') { |
376253ec | 3586 | expr_error(mon, "')' expected"); |
9307c4c1 FB |
3587 | } |
3588 | next(); | |
3589 | break; | |
81d0912d FB |
3590 | case '\'': |
3591 | pch++; | |
3592 | if (*pch == '\0') | |
376253ec | 3593 | expr_error(mon, "character constant expected"); |
81d0912d FB |
3594 | n = *pch; |
3595 | pch++; | |
3596 | if (*pch != '\'') | |
376253ec | 3597 | expr_error(mon, "missing terminating \' character"); |
81d0912d FB |
3598 | next(); |
3599 | break; | |
9307c4c1 FB |
3600 | case '$': |
3601 | { | |
3602 | char buf[128], *q; | |
69b34976 | 3603 | target_long reg=0; |
3b46e624 | 3604 | |
9307c4c1 FB |
3605 | pch++; |
3606 | q = buf; | |
3607 | while ((*pch >= 'a' && *pch <= 'z') || | |
3608 | (*pch >= 'A' && *pch <= 'Z') || | |
3609 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 3610 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
3611 | if ((q - buf) < sizeof(buf) - 1) |
3612 | *q++ = *pch; | |
3613 | pch++; | |
3614 | } | |
cd390083 | 3615 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3616 | pch++; |
3617 | *q = 0; | |
7743e588 | 3618 | ret = get_monitor_def(®, buf); |
09b9418c | 3619 | if (ret < 0) |
376253ec | 3620 | expr_error(mon, "unknown register"); |
7743e588 | 3621 | n = reg; |
9307c4c1 FB |
3622 | } |
3623 | break; | |
3624 | case '\0': | |
376253ec | 3625 | expr_error(mon, "unexpected end of expression"); |
9307c4c1 FB |
3626 | n = 0; |
3627 | break; | |
3628 | default: | |
6b0e33be | 3629 | errno = 0; |
4f4fbf77 | 3630 | n = strtoull(pch, &p, 0); |
6b0e33be LC |
3631 | if (errno == ERANGE) { |
3632 | expr_error(mon, "number too large"); | |
3633 | } | |
9307c4c1 | 3634 | if (pch == p) { |
277acfe8 | 3635 | expr_error(mon, "invalid char '%c' in expression", *p); |
9307c4c1 FB |
3636 | } |
3637 | pch = p; | |
cd390083 | 3638 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3639 | pch++; |
3640 | break; | |
3641 | } | |
3642 | return n; | |
3643 | } | |
3644 | ||
3645 | ||
376253ec | 3646 | static int64_t expr_prod(Monitor *mon) |
9307c4c1 | 3647 | { |
c2efc95d | 3648 | int64_t val, val2; |
92a31b1f | 3649 | int op; |
3b46e624 | 3650 | |
376253ec | 3651 | val = expr_unary(mon); |
9307c4c1 FB |
3652 | for(;;) { |
3653 | op = *pch; | |
3654 | if (op != '*' && op != '/' && op != '%') | |
3655 | break; | |
3656 | next(); | |
376253ec | 3657 | val2 = expr_unary(mon); |
9307c4c1 FB |
3658 | switch(op) { |
3659 | default: | |
3660 | case '*': | |
3661 | val *= val2; | |
3662 | break; | |
3663 | case '/': | |
3664 | case '%': | |
5fafdf24 | 3665 | if (val2 == 0) |
376253ec | 3666 | expr_error(mon, "division by zero"); |
9307c4c1 FB |
3667 | if (op == '/') |
3668 | val /= val2; | |
3669 | else | |
3670 | val %= val2; | |
3671 | break; | |
3672 | } | |
3673 | } | |
3674 | return val; | |
3675 | } | |
3676 | ||
376253ec | 3677 | static int64_t expr_logic(Monitor *mon) |
9307c4c1 | 3678 | { |
c2efc95d | 3679 | int64_t val, val2; |
92a31b1f | 3680 | int op; |
9307c4c1 | 3681 | |
376253ec | 3682 | val = expr_prod(mon); |
9307c4c1 FB |
3683 | for(;;) { |
3684 | op = *pch; | |
3685 | if (op != '&' && op != '|' && op != '^') | |
3686 | break; | |
3687 | next(); | |
376253ec | 3688 | val2 = expr_prod(mon); |
9307c4c1 FB |
3689 | switch(op) { |
3690 | default: | |
3691 | case '&': | |
3692 | val &= val2; | |
3693 | break; | |
3694 | case '|': | |
3695 | val |= val2; | |
3696 | break; | |
3697 | case '^': | |
3698 | val ^= val2; | |
3699 | break; | |
3700 | } | |
3701 | } | |
3702 | return val; | |
3703 | } | |
3704 | ||
376253ec | 3705 | static int64_t expr_sum(Monitor *mon) |
9307c4c1 | 3706 | { |
c2efc95d | 3707 | int64_t val, val2; |
92a31b1f | 3708 | int op; |
9307c4c1 | 3709 | |
376253ec | 3710 | val = expr_logic(mon); |
9307c4c1 FB |
3711 | for(;;) { |
3712 | op = *pch; | |
3713 | if (op != '+' && op != '-') | |
3714 | break; | |
3715 | next(); | |
376253ec | 3716 | val2 = expr_logic(mon); |
9307c4c1 FB |
3717 | if (op == '+') |
3718 | val += val2; | |
3719 | else | |
3720 | val -= val2; | |
3721 | } | |
3722 | return val; | |
3723 | } | |
3724 | ||
376253ec | 3725 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
9307c4c1 FB |
3726 | { |
3727 | pch = *pp; | |
6ab7e546 | 3728 | if (sigsetjmp(expr_env, 0)) { |
9307c4c1 FB |
3729 | *pp = pch; |
3730 | return -1; | |
3731 | } | |
cd390083 | 3732 | while (qemu_isspace(*pch)) |
9307c4c1 | 3733 | pch++; |
376253ec | 3734 | *pval = expr_sum(mon); |
9307c4c1 FB |
3735 | *pp = pch; |
3736 | return 0; | |
3737 | } | |
3738 | ||
3350a4dd MA |
3739 | static int get_double(Monitor *mon, double *pval, const char **pp) |
3740 | { | |
3741 | const char *p = *pp; | |
3742 | char *tailp; | |
3743 | double d; | |
3744 | ||
3745 | d = strtod(p, &tailp); | |
3746 | if (tailp == p) { | |
3747 | monitor_printf(mon, "Number expected\n"); | |
3748 | return -1; | |
3749 | } | |
3750 | if (d != d || d - d != 0) { | |
3751 | /* NaN or infinity */ | |
3752 | monitor_printf(mon, "Bad number\n"); | |
3753 | return -1; | |
3754 | } | |
3755 | *pval = d; | |
3756 | *pp = tailp; | |
3757 | return 0; | |
3758 | } | |
3759 | ||
4590fd80 LC |
3760 | /* |
3761 | * Store the command-name in cmdname, and return a pointer to | |
3762 | * the remaining of the command string. | |
3763 | */ | |
3764 | static const char *get_command_name(const char *cmdline, | |
3765 | char *cmdname, size_t nlen) | |
3766 | { | |
3767 | size_t len; | |
3768 | const char *p, *pstart; | |
3769 | ||
3770 | p = cmdline; | |
3771 | while (qemu_isspace(*p)) | |
3772 | p++; | |
3773 | if (*p == '\0') | |
3774 | return NULL; | |
3775 | pstart = p; | |
3776 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | |
3777 | p++; | |
3778 | len = p - pstart; | |
3779 | if (len > nlen - 1) | |
3780 | len = nlen - 1; | |
3781 | memcpy(cmdname, pstart, len); | |
3782 | cmdname[len] = '\0'; | |
3783 | return p; | |
3784 | } | |
3785 | ||
4d76d2ba LC |
3786 | /** |
3787 | * Read key of 'type' into 'key' and return the current | |
3788 | * 'type' pointer. | |
3789 | */ | |
3790 | static char *key_get_info(const char *type, char **key) | |
3791 | { | |
3792 | size_t len; | |
3793 | char *p, *str; | |
3794 | ||
3795 | if (*type == ',') | |
3796 | type++; | |
3797 | ||
3798 | p = strchr(type, ':'); | |
3799 | if (!p) { | |
3800 | *key = NULL; | |
3801 | return NULL; | |
3802 | } | |
3803 | len = p - type; | |
3804 | ||
7267c094 | 3805 | str = g_malloc(len + 1); |
4d76d2ba LC |
3806 | memcpy(str, type, len); |
3807 | str[len] = '\0'; | |
3808 | ||
3809 | *key = str; | |
3810 | return ++p; | |
3811 | } | |
3812 | ||
9307c4c1 FB |
3813 | static int default_fmt_format = 'x'; |
3814 | static int default_fmt_size = 4; | |
3815 | ||
fbc3d96c LS |
3816 | static int is_valid_option(const char *c, const char *typestr) |
3817 | { | |
3818 | char option[3]; | |
3819 | ||
3820 | option[0] = '-'; | |
3821 | option[1] = *c; | |
3822 | option[2] = '\0'; | |
3823 | ||
3824 | typestr = strstr(typestr, option); | |
3825 | return (typestr != NULL); | |
3826 | } | |
3827 | ||
945c5ac8 LC |
3828 | static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table, |
3829 | const char *cmdname) | |
7fd669a1 LC |
3830 | { |
3831 | const mon_cmd_t *cmd; | |
3832 | ||
945c5ac8 | 3833 | for (cmd = disp_table; cmd->name != NULL; cmd++) { |
7fd669a1 LC |
3834 | if (compare_cmd(cmdname, cmd->name)) { |
3835 | return cmd; | |
3836 | } | |
3837 | } | |
3838 | ||
3839 | return NULL; | |
3840 | } | |
3841 | ||
bead3ce1 LC |
3842 | static const mon_cmd_t *qmp_find_cmd(const char *cmdname) |
3843 | { | |
f36b4afb | 3844 | return search_dispatch_table(qmp_cmds, cmdname); |
bead3ce1 LC |
3845 | } |
3846 | ||
5f3d335f WX |
3847 | /* |
3848 | * Parse @cmdline according to command table @table. | |
3849 | * If @cmdline is blank, return NULL. | |
3850 | * If it can't be parsed, report to @mon, and return NULL. | |
3851 | * Else, insert command arguments into @qdict, and return the command. | |
085d8134 PM |
3852 | * If a sub-command table exists, and if @cmdline contains an additional string |
3853 | * for a sub-command, this function will try to search the sub-command table. | |
3854 | * If no additional string for a sub-command is present, this function will | |
3855 | * return the command found in @table. | |
5f3d335f WX |
3856 | * Do not assume the returned command points into @table! It doesn't |
3857 | * when the command is a sub-command. | |
3858 | */ | |
c227f099 | 3859 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
55f81d96 | 3860 | const char *cmdline, |
5f3d335f WX |
3861 | int start, |
3862 | mon_cmd_t *table, | |
55f81d96 | 3863 | QDict *qdict) |
9307c4c1 | 3864 | { |
4590fd80 | 3865 | const char *p, *typestr; |
53773581 | 3866 | int c; |
c227f099 | 3867 | const mon_cmd_t *cmd; |
9307c4c1 FB |
3868 | char cmdname[256]; |
3869 | char buf[1024]; | |
4d76d2ba | 3870 | char *key; |
9dc39cba FB |
3871 | |
3872 | #ifdef DEBUG | |
5f3d335f | 3873 | monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start); |
9dc39cba | 3874 | #endif |
3b46e624 | 3875 | |
9307c4c1 | 3876 | /* extract the command name */ |
5f3d335f | 3877 | p = get_command_name(cmdline + start, cmdname, sizeof(cmdname)); |
4590fd80 | 3878 | if (!p) |
55f81d96 | 3879 | return NULL; |
3b46e624 | 3880 | |
5f3d335f | 3881 | cmd = search_dispatch_table(table, cmdname); |
7fd669a1 | 3882 | if (!cmd) { |
5f3d335f WX |
3883 | monitor_printf(mon, "unknown command: '%.*s'\n", |
3884 | (int)(p - cmdline), cmdline); | |
55f81d96 | 3885 | return NULL; |
9307c4c1 | 3886 | } |
9307c4c1 | 3887 | |
5f3d335f WX |
3888 | /* filter out following useless space */ |
3889 | while (qemu_isspace(*p)) { | |
3890 | p++; | |
3891 | } | |
3892 | /* search sub command */ | |
3893 | if (cmd->sub_table != NULL) { | |
3894 | /* check if user set additional command */ | |
3895 | if (*p == '\0') { | |
3896 | return cmd; | |
3897 | } | |
3898 | return monitor_parse_command(mon, cmdline, p - cmdline, | |
3899 | cmd->sub_table, qdict); | |
3900 | } | |
3901 | ||
9307c4c1 FB |
3902 | /* parse the parameters */ |
3903 | typestr = cmd->args_type; | |
9dc39cba | 3904 | for(;;) { |
4d76d2ba LC |
3905 | typestr = key_get_info(typestr, &key); |
3906 | if (!typestr) | |
9dc39cba | 3907 | break; |
4d76d2ba | 3908 | c = *typestr; |
9307c4c1 FB |
3909 | typestr++; |
3910 | switch(c) { | |
3911 | case 'F': | |
81d0912d | 3912 | case 'B': |
9307c4c1 FB |
3913 | case 's': |
3914 | { | |
3915 | int ret; | |
3b46e624 | 3916 | |
cd390083 | 3917 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3918 | p++; |
3919 | if (*typestr == '?') { | |
3920 | typestr++; | |
3921 | if (*p == '\0') { | |
3922 | /* no optional string: NULL argument */ | |
53773581 | 3923 | break; |
9307c4c1 FB |
3924 | } |
3925 | } | |
3926 | ret = get_str(buf, sizeof(buf), &p); | |
3927 | if (ret < 0) { | |
81d0912d FB |
3928 | switch(c) { |
3929 | case 'F': | |
376253ec AL |
3930 | monitor_printf(mon, "%s: filename expected\n", |
3931 | cmdname); | |
81d0912d FB |
3932 | break; |
3933 | case 'B': | |
376253ec AL |
3934 | monitor_printf(mon, "%s: block device name expected\n", |
3935 | cmdname); | |
81d0912d FB |
3936 | break; |
3937 | default: | |
376253ec | 3938 | monitor_printf(mon, "%s: string expected\n", cmdname); |
81d0912d FB |
3939 | break; |
3940 | } | |
9307c4c1 FB |
3941 | goto fail; |
3942 | } | |
53773581 | 3943 | qdict_put(qdict, key, qstring_from_str(buf)); |
9307c4c1 | 3944 | } |
9dc39cba | 3945 | break; |
361127df MA |
3946 | case 'O': |
3947 | { | |
3948 | QemuOptsList *opts_list; | |
3949 | QemuOpts *opts; | |
3950 | ||
3951 | opts_list = qemu_find_opts(key); | |
3952 | if (!opts_list || opts_list->desc->name) { | |
3953 | goto bad_type; | |
3954 | } | |
3955 | while (qemu_isspace(*p)) { | |
3956 | p++; | |
3957 | } | |
3958 | if (!*p) | |
3959 | break; | |
3960 | if (get_str(buf, sizeof(buf), &p) < 0) { | |
3961 | goto fail; | |
3962 | } | |
3963 | opts = qemu_opts_parse(opts_list, buf, 1); | |
3964 | if (!opts) { | |
3965 | goto fail; | |
3966 | } | |
3967 | qemu_opts_to_qdict(opts, qdict); | |
3968 | qemu_opts_del(opts); | |
3969 | } | |
3970 | break; | |
9307c4c1 FB |
3971 | case '/': |
3972 | { | |
3973 | int count, format, size; | |
3b46e624 | 3974 | |
cd390083 | 3975 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3976 | p++; |
3977 | if (*p == '/') { | |
3978 | /* format found */ | |
3979 | p++; | |
3980 | count = 1; | |
cd390083 | 3981 | if (qemu_isdigit(*p)) { |
9307c4c1 | 3982 | count = 0; |
cd390083 | 3983 | while (qemu_isdigit(*p)) { |
9307c4c1 FB |
3984 | count = count * 10 + (*p - '0'); |
3985 | p++; | |
3986 | } | |
3987 | } | |
3988 | size = -1; | |
3989 | format = -1; | |
3990 | for(;;) { | |
3991 | switch(*p) { | |
3992 | case 'o': | |
3993 | case 'd': | |
3994 | case 'u': | |
3995 | case 'x': | |
3996 | case 'i': | |
3997 | case 'c': | |
3998 | format = *p++; | |
3999 | break; | |
4000 | case 'b': | |
4001 | size = 1; | |
4002 | p++; | |
4003 | break; | |
4004 | case 'h': | |
4005 | size = 2; | |
4006 | p++; | |
4007 | break; | |
4008 | case 'w': | |
4009 | size = 4; | |
4010 | p++; | |
4011 | break; | |
4012 | case 'g': | |
4013 | case 'L': | |
4014 | size = 8; | |
4015 | p++; | |
4016 | break; | |
4017 | default: | |
4018 | goto next; | |
4019 | } | |
4020 | } | |
4021 | next: | |
cd390083 | 4022 | if (*p != '\0' && !qemu_isspace(*p)) { |
376253ec AL |
4023 | monitor_printf(mon, "invalid char in format: '%c'\n", |
4024 | *p); | |
9307c4c1 FB |
4025 | goto fail; |
4026 | } | |
9307c4c1 FB |
4027 | if (format < 0) |
4028 | format = default_fmt_format; | |
4c27ba27 FB |
4029 | if (format != 'i') { |
4030 | /* for 'i', not specifying a size gives -1 as size */ | |
4031 | if (size < 0) | |
4032 | size = default_fmt_size; | |
e90f009b | 4033 | default_fmt_size = size; |
4c27ba27 | 4034 | } |
9307c4c1 FB |
4035 | default_fmt_format = format; |
4036 | } else { | |
4037 | count = 1; | |
4038 | format = default_fmt_format; | |
4c27ba27 FB |
4039 | if (format != 'i') { |
4040 | size = default_fmt_size; | |
4041 | } else { | |
4042 | size = -1; | |
4043 | } | |
9307c4c1 | 4044 | } |
f7188bbe LC |
4045 | qdict_put(qdict, "count", qint_from_int(count)); |
4046 | qdict_put(qdict, "format", qint_from_int(format)); | |
4047 | qdict_put(qdict, "size", qint_from_int(size)); | |
9307c4c1 | 4048 | } |
9dc39cba | 4049 | break; |
9307c4c1 | 4050 | case 'i': |
92a31b1f | 4051 | case 'l': |
b6e098d7 | 4052 | case 'M': |
9307c4c1 | 4053 | { |
c2efc95d | 4054 | int64_t val; |
7743e588 | 4055 | |
cd390083 | 4056 | while (qemu_isspace(*p)) |
9307c4c1 | 4057 | p++; |
3440557b | 4058 | if (*typestr == '?' || *typestr == '.') { |
3440557b | 4059 | if (*typestr == '?') { |
53773581 LC |
4060 | if (*p == '\0') { |
4061 | typestr++; | |
4062 | break; | |
4063 | } | |
3440557b FB |
4064 | } else { |
4065 | if (*p == '.') { | |
4066 | p++; | |
cd390083 | 4067 | while (qemu_isspace(*p)) |
3440557b | 4068 | p++; |
3440557b | 4069 | } else { |
53773581 LC |
4070 | typestr++; |
4071 | break; | |
3440557b FB |
4072 | } |
4073 | } | |
13224a87 | 4074 | typestr++; |
9307c4c1 | 4075 | } |
376253ec | 4076 | if (get_expr(mon, &val, &p)) |
9307c4c1 | 4077 | goto fail; |
675ebef9 LC |
4078 | /* Check if 'i' is greater than 32-bit */ |
4079 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { | |
4080 | monitor_printf(mon, "\'%s\' has failed: ", cmdname); | |
4081 | monitor_printf(mon, "integer is for 32-bit values\n"); | |
4082 | goto fail; | |
b6e098d7 | 4083 | } else if (c == 'M') { |
91162849 LC |
4084 | if (val < 0) { |
4085 | monitor_printf(mon, "enter a positive value\n"); | |
4086 | goto fail; | |
4087 | } | |
b6e098d7 | 4088 | val <<= 20; |
675ebef9 | 4089 | } |
53773581 | 4090 | qdict_put(qdict, key, qint_from_int(val)); |
9307c4c1 FB |
4091 | } |
4092 | break; | |
dbc0c67f JS |
4093 | case 'o': |
4094 | { | |
70b4f4bb | 4095 | int64_t val; |
dbc0c67f JS |
4096 | char *end; |
4097 | ||
4098 | while (qemu_isspace(*p)) { | |
4099 | p++; | |
4100 | } | |
4101 | if (*typestr == '?') { | |
4102 | typestr++; | |
4103 | if (*p == '\0') { | |
4104 | break; | |
4105 | } | |
4106 | } | |
4107 | val = strtosz(p, &end); | |
4108 | if (val < 0) { | |
4109 | monitor_printf(mon, "invalid size\n"); | |
4110 | goto fail; | |
4111 | } | |
4112 | qdict_put(qdict, key, qint_from_int(val)); | |
4113 | p = end; | |
4114 | } | |
4115 | break; | |
fccfb11e | 4116 | case 'T': |
3350a4dd MA |
4117 | { |
4118 | double val; | |
4119 | ||
4120 | while (qemu_isspace(*p)) | |
4121 | p++; | |
4122 | if (*typestr == '?') { | |
4123 | typestr++; | |
4124 | if (*p == '\0') { | |
4125 | break; | |
4126 | } | |
4127 | } | |
4128 | if (get_double(mon, &val, &p) < 0) { | |
4129 | goto fail; | |
4130 | } | |
07de3e60 | 4131 | if (p[0] && p[1] == 's') { |
fccfb11e MA |
4132 | switch (*p) { |
4133 | case 'm': | |
4134 | val /= 1e3; p += 2; break; | |
4135 | case 'u': | |
4136 | val /= 1e6; p += 2; break; | |
4137 | case 'n': | |
4138 | val /= 1e9; p += 2; break; | |
4139 | } | |
4140 | } | |
3350a4dd MA |
4141 | if (*p && !qemu_isspace(*p)) { |
4142 | monitor_printf(mon, "Unknown unit suffix\n"); | |
4143 | goto fail; | |
4144 | } | |
4145 | qdict_put(qdict, key, qfloat_from_double(val)); | |
4146 | } | |
4147 | break; | |
942cd1f2 MA |
4148 | case 'b': |
4149 | { | |
4150 | const char *beg; | |
4151 | int val; | |
4152 | ||
4153 | while (qemu_isspace(*p)) { | |
4154 | p++; | |
4155 | } | |
4156 | beg = p; | |
4157 | while (qemu_isgraph(*p)) { | |
4158 | p++; | |
4159 | } | |
4160 | if (p - beg == 2 && !memcmp(beg, "on", p - beg)) { | |
4161 | val = 1; | |
4162 | } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) { | |
4163 | val = 0; | |
4164 | } else { | |
4165 | monitor_printf(mon, "Expected 'on' or 'off'\n"); | |
4166 | goto fail; | |
4167 | } | |
4168 | qdict_put(qdict, key, qbool_from_int(val)); | |
4169 | } | |
4170 | break; | |
9307c4c1 FB |
4171 | case '-': |
4172 | { | |
fbc3d96c | 4173 | const char *tmp = p; |
eb159d13 | 4174 | int skip_key = 0; |
9307c4c1 | 4175 | /* option */ |
3b46e624 | 4176 | |
9307c4c1 FB |
4177 | c = *typestr++; |
4178 | if (c == '\0') | |
4179 | goto bad_type; | |
cd390083 | 4180 | while (qemu_isspace(*p)) |
9307c4c1 | 4181 | p++; |
9307c4c1 FB |
4182 | if (*p == '-') { |
4183 | p++; | |
fbc3d96c LS |
4184 | if(c != *p) { |
4185 | if(!is_valid_option(p, typestr)) { | |
4186 | ||
4187 | monitor_printf(mon, "%s: unsupported option -%c\n", | |
4188 | cmdname, *p); | |
4189 | goto fail; | |
4190 | } else { | |
4191 | skip_key = 1; | |
4192 | } | |
4193 | } | |
4194 | if(skip_key) { | |
4195 | p = tmp; | |
4196 | } else { | |
eb159d13 | 4197 | /* has option */ |
fbc3d96c | 4198 | p++; |
eb159d13 | 4199 | qdict_put(qdict, key, qbool_from_int(1)); |
9307c4c1 | 4200 | } |
9307c4c1 | 4201 | } |
9307c4c1 FB |
4202 | } |
4203 | break; | |
129be006 WX |
4204 | case 'S': |
4205 | { | |
4206 | /* package all remaining string */ | |
4207 | int len; | |
4208 | ||
4209 | while (qemu_isspace(*p)) { | |
4210 | p++; | |
4211 | } | |
4212 | if (*typestr == '?') { | |
4213 | typestr++; | |
4214 | if (*p == '\0') { | |
4215 | /* no remaining string: NULL argument */ | |
4216 | break; | |
4217 | } | |
4218 | } | |
4219 | len = strlen(p); | |
4220 | if (len <= 0) { | |
4221 | monitor_printf(mon, "%s: string expected\n", | |
4222 | cmdname); | |
4223 | break; | |
4224 | } | |
4225 | qdict_put(qdict, key, qstring_from_str(p)); | |
4226 | p += len; | |
4227 | } | |
4228 | break; | |
9307c4c1 FB |
4229 | default: |
4230 | bad_type: | |
376253ec | 4231 | monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c); |
9307c4c1 FB |
4232 | goto fail; |
4233 | } | |
7267c094 | 4234 | g_free(key); |
4d76d2ba | 4235 | key = NULL; |
9dc39cba | 4236 | } |
9307c4c1 | 4237 | /* check that all arguments were parsed */ |
cd390083 | 4238 | while (qemu_isspace(*p)) |
9307c4c1 FB |
4239 | p++; |
4240 | if (*p != '\0') { | |
376253ec AL |
4241 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
4242 | cmdname); | |
9307c4c1 | 4243 | goto fail; |
9dc39cba | 4244 | } |
9307c4c1 | 4245 | |
55f81d96 | 4246 | return cmd; |
ac7531ec | 4247 | |
55f81d96 | 4248 | fail: |
7267c094 | 4249 | g_free(key); |
55f81d96 LC |
4250 | return NULL; |
4251 | } | |
4252 | ||
d6f46833 MA |
4253 | void monitor_set_error(Monitor *mon, QError *qerror) |
4254 | { | |
4255 | /* report only the first error */ | |
4256 | if (!mon->error) { | |
4257 | mon->error = qerror; | |
4258 | } else { | |
d6f46833 MA |
4259 | QDECREF(qerror); |
4260 | } | |
4261 | } | |
4262 | ||
bb89c2e9 LC |
4263 | static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret) |
4264 | { | |
6d441430 LC |
4265 | if (ret && !monitor_has_error(mon)) { |
4266 | /* | |
4267 | * If it returns failure, it must have passed on error. | |
4268 | * | |
4269 | * Action: Report an internal error to the client if in QMP. | |
4270 | */ | |
4271 | qerror_report(QERR_UNDEFINED_ERROR); | |
6d441430 | 4272 | } |
bb89c2e9 LC |
4273 | } |
4274 | ||
f3c157c4 | 4275 | static void handle_user_command(Monitor *mon, const char *cmdline) |
55f81d96 | 4276 | { |
55f81d96 | 4277 | QDict *qdict; |
c227f099 | 4278 | const mon_cmd_t *cmd; |
55f81d96 LC |
4279 | |
4280 | qdict = qdict_new(); | |
4281 | ||
7717239d | 4282 | cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict); |
13917bee LC |
4283 | if (!cmd) |
4284 | goto out; | |
4285 | ||
4903de0c | 4286 | if (handler_is_async(cmd)) { |
940cc30d | 4287 | user_async_cmd_handler(mon, cmd, qdict); |
9e80721e | 4288 | } else if (handler_is_qobject(cmd)) { |
de79ba6f LC |
4289 | QObject *data = NULL; |
4290 | ||
4291 | /* XXX: ignores the error code */ | |
4292 | cmd->mhandler.cmd_new(mon, qdict, &data); | |
4293 | assert(!monitor_has_error(mon)); | |
4294 | if (data) { | |
4295 | cmd->user_print(mon, data); | |
4296 | qobject_decref(data); | |
4297 | } | |
13917bee | 4298 | } else { |
af4ce882 | 4299 | cmd->mhandler.cmd(mon, qdict); |
55f81d96 LC |
4300 | } |
4301 | ||
13917bee | 4302 | out: |
f7188bbe | 4303 | QDECREF(qdict); |
9dc39cba FB |
4304 | } |
4305 | ||
cd5c6bba | 4306 | static void cmd_completion(Monitor *mon, const char *name, const char *list) |
81d0912d FB |
4307 | { |
4308 | const char *p, *pstart; | |
4309 | char cmd[128]; | |
4310 | int len; | |
4311 | ||
4312 | p = list; | |
4313 | for(;;) { | |
4314 | pstart = p; | |
4315 | p = strchr(p, '|'); | |
4316 | if (!p) | |
4317 | p = pstart + strlen(pstart); | |
4318 | len = p - pstart; | |
4319 | if (len > sizeof(cmd) - 2) | |
4320 | len = sizeof(cmd) - 2; | |
4321 | memcpy(cmd, pstart, len); | |
4322 | cmd[len] = '\0'; | |
4323 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
cd5c6bba | 4324 | readline_add_completion(mon->rs, cmd); |
81d0912d FB |
4325 | } |
4326 | if (*p == '\0') | |
4327 | break; | |
4328 | p++; | |
4329 | } | |
4330 | } | |
4331 | ||
cb8f68b1 | 4332 | static void file_completion(Monitor *mon, const char *input) |
81d0912d FB |
4333 | { |
4334 | DIR *ffs; | |
4335 | struct dirent *d; | |
4336 | char path[1024]; | |
4337 | char file[1024], file_prefix[1024]; | |
4338 | int input_path_len; | |
4339 | const char *p; | |
4340 | ||
5fafdf24 | 4341 | p = strrchr(input, '/'); |
81d0912d FB |
4342 | if (!p) { |
4343 | input_path_len = 0; | |
4344 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 4345 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
4346 | } else { |
4347 | input_path_len = p - input + 1; | |
4348 | memcpy(path, input, input_path_len); | |
4349 | if (input_path_len > sizeof(path) - 1) | |
4350 | input_path_len = sizeof(path) - 1; | |
4351 | path[input_path_len] = '\0'; | |
4352 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
4353 | } | |
4354 | #ifdef DEBUG_COMPLETION | |
cb8f68b1 | 4355 | monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n", |
376253ec | 4356 | input, path, file_prefix); |
81d0912d FB |
4357 | #endif |
4358 | ffs = opendir(path); | |
4359 | if (!ffs) | |
4360 | return; | |
4361 | for(;;) { | |
4362 | struct stat sb; | |
4363 | d = readdir(ffs); | |
4364 | if (!d) | |
4365 | break; | |
46c7fc18 KK |
4366 | |
4367 | if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) { | |
4368 | continue; | |
4369 | } | |
4370 | ||
81d0912d FB |
4371 | if (strstart(d->d_name, file_prefix, NULL)) { |
4372 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
4373 | if (input_path_len < sizeof(file)) |
4374 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
4375 | d->d_name); | |
81d0912d FB |
4376 | /* stat the file to find out if it's a directory. |
4377 | * In that case add a slash to speed up typing long paths | |
4378 | */ | |
c951d9a6 | 4379 | if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
363a37d5 | 4380 | pstrcat(file, sizeof(file), "/"); |
c951d9a6 | 4381 | } |
cb8f68b1 | 4382 | readline_add_completion(mon->rs, file); |
81d0912d FB |
4383 | } |
4384 | } | |
4385 | closedir(ffs); | |
4386 | } | |
4387 | ||
599a926a WX |
4388 | typedef struct MonitorBlockComplete { |
4389 | Monitor *mon; | |
4390 | const char *input; | |
4391 | } MonitorBlockComplete; | |
4392 | ||
51de9760 | 4393 | static void block_completion_it(void *opaque, BlockDriverState *bs) |
81d0912d | 4394 | { |
51de9760 | 4395 | const char *name = bdrv_get_device_name(bs); |
599a926a WX |
4396 | MonitorBlockComplete *mbc = opaque; |
4397 | Monitor *mon = mbc->mon; | |
4398 | const char *input = mbc->input; | |
81d0912d FB |
4399 | |
4400 | if (input[0] == '\0' || | |
4401 | !strncmp(name, (char *)input, strlen(input))) { | |
599a926a | 4402 | readline_add_completion(mon->rs, name); |
81d0912d FB |
4403 | } |
4404 | } | |
4405 | ||
4d76d2ba LC |
4406 | static const char *next_arg_type(const char *typestr) |
4407 | { | |
4408 | const char *p = strchr(typestr, ':'); | |
4409 | return (p != NULL ? ++p : typestr); | |
4410 | } | |
4411 | ||
40d19394 HB |
4412 | static void add_completion_option(ReadLineState *rs, const char *str, |
4413 | const char *option) | |
4414 | { | |
4415 | if (!str || !option) { | |
4416 | return; | |
4417 | } | |
4418 | if (!strncmp(option, str, strlen(str))) { | |
4419 | readline_add_completion(rs, option); | |
4420 | } | |
4421 | } | |
4422 | ||
13e315da HB |
4423 | void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
4424 | { | |
4425 | size_t len; | |
4426 | ChardevBackendInfoList *list, *start; | |
4427 | ||
4428 | if (nb_args != 2) { | |
4429 | return; | |
4430 | } | |
4431 | len = strlen(str); | |
4432 | readline_set_completion_index(rs, len); | |
4433 | ||
4434 | start = list = qmp_query_chardev_backends(NULL); | |
4435 | while (list) { | |
4436 | const char *chr_name = list->value->name; | |
4437 | ||
4438 | if (!strncmp(chr_name, str, len)) { | |
4439 | readline_add_completion(rs, chr_name); | |
4440 | } | |
4441 | list = list->next; | |
4442 | } | |
4443 | qapi_free_ChardevBackendInfoList(start); | |
4444 | } | |
4445 | ||
b162b49a HB |
4446 | void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
4447 | { | |
4448 | size_t len; | |
4449 | int i; | |
4450 | ||
4451 | if (nb_args != 2) { | |
4452 | return; | |
4453 | } | |
4454 | len = strlen(str); | |
4455 | readline_set_completion_index(rs, len); | |
4456 | for (i = 0; NetClientOptionsKind_lookup[i]; i++) { | |
4457 | add_completion_option(rs, str, NetClientOptionsKind_lookup[i]); | |
4458 | } | |
4459 | } | |
4460 | ||
2da1b3ab | 4461 | void device_add_completion(ReadLineState *rs, int nb_args, const char *str) |
992d3e64 HB |
4462 | { |
4463 | GSList *list, *elt; | |
4464 | size_t len; | |
4465 | ||
2da1b3ab HB |
4466 | if (nb_args != 2) { |
4467 | return; | |
4468 | } | |
4469 | ||
992d3e64 HB |
4470 | len = strlen(str); |
4471 | readline_set_completion_index(rs, len); | |
4472 | list = elt = object_class_get_list(TYPE_DEVICE, false); | |
4473 | while (elt) { | |
4474 | const char *name; | |
4475 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, | |
4476 | TYPE_DEVICE); | |
4477 | name = object_class_get_name(OBJECT_CLASS(dc)); | |
2da1b3ab HB |
4478 | |
4479 | if (!dc->cannot_instantiate_with_device_add_yet | |
4480 | && !strncmp(name, str, len)) { | |
992d3e64 HB |
4481 | readline_add_completion(rs, name); |
4482 | } | |
4483 | elt = elt->next; | |
4484 | } | |
4485 | g_slist_free(list); | |
4486 | } | |
4487 | ||
bfa40f77 | 4488 | void object_add_completion(ReadLineState *rs, int nb_args, const char *str) |
1094fd3a HB |
4489 | { |
4490 | GSList *list, *elt; | |
4491 | size_t len; | |
4492 | ||
bfa40f77 HB |
4493 | if (nb_args != 2) { |
4494 | return; | |
4495 | } | |
4496 | ||
1094fd3a HB |
4497 | len = strlen(str); |
4498 | readline_set_completion_index(rs, len); | |
4499 | list = elt = object_class_get_list(TYPE_USER_CREATABLE, false); | |
4500 | while (elt) { | |
4501 | const char *name; | |
4502 | ||
4503 | name = object_class_get_name(OBJECT_CLASS(elt->data)); | |
4504 | if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) { | |
4505 | readline_add_completion(rs, name); | |
4506 | } | |
4507 | elt = elt->next; | |
4508 | } | |
4509 | g_slist_free(list); | |
4510 | } | |
4511 | ||
2da1b3ab HB |
4512 | static void device_del_bus_completion(ReadLineState *rs, BusState *bus, |
4513 | const char *str, size_t len) | |
fca72d9b HB |
4514 | { |
4515 | BusChild *kid; | |
4516 | ||
4517 | QTAILQ_FOREACH(kid, &bus->children, sibling) { | |
4518 | DeviceState *dev = kid->child; | |
4519 | BusState *dev_child; | |
4520 | ||
4521 | if (dev->id && !strncmp(str, dev->id, len)) { | |
4522 | readline_add_completion(rs, dev->id); | |
4523 | } | |
4524 | ||
4525 | QLIST_FOREACH(dev_child, &dev->child_bus, sibling) { | |
2da1b3ab | 4526 | device_del_bus_completion(rs, dev_child, str, len); |
fca72d9b HB |
4527 | } |
4528 | } | |
4529 | } | |
4530 | ||
6297d9a2 HB |
4531 | void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str) |
4532 | { | |
4533 | size_t len; | |
4534 | ChardevInfoList *list, *start; | |
4535 | ||
4536 | if (nb_args != 2) { | |
4537 | return; | |
4538 | } | |
4539 | len = strlen(str); | |
4540 | readline_set_completion_index(rs, len); | |
4541 | ||
4542 | start = list = qmp_query_chardev(NULL); | |
4543 | while (list) { | |
4544 | ChardevInfo *chr = list->value; | |
4545 | ||
4546 | if (!strncmp(chr->label, str, len)) { | |
4547 | readline_add_completion(rs, chr->label); | |
4548 | } | |
4549 | list = list->next; | |
4550 | } | |
4551 | qapi_free_ChardevInfoList(start); | |
4552 | } | |
4553 | ||
8e597779 HB |
4554 | static void ringbuf_completion(ReadLineState *rs, const char *str) |
4555 | { | |
4556 | size_t len; | |
4557 | ChardevInfoList *list, *start; | |
4558 | ||
4559 | len = strlen(str); | |
4560 | readline_set_completion_index(rs, len); | |
4561 | ||
4562 | start = list = qmp_query_chardev(NULL); | |
4563 | while (list) { | |
4564 | ChardevInfo *chr_info = list->value; | |
4565 | ||
4566 | if (!strncmp(chr_info->label, str, len)) { | |
4567 | CharDriverState *chr = qemu_chr_find(chr_info->label); | |
4568 | if (chr && chr_is_ringbuf(chr)) { | |
4569 | readline_add_completion(rs, chr_info->label); | |
4570 | } | |
4571 | } | |
4572 | list = list->next; | |
4573 | } | |
4574 | qapi_free_ChardevInfoList(start); | |
4575 | } | |
4576 | ||
4577 | void ringbuf_read_completion(ReadLineState *rs, int nb_args, const char *str) | |
4578 | { | |
4579 | if (nb_args != 2) { | |
4580 | return; | |
4581 | } | |
4582 | ringbuf_completion(rs, str); | |
4583 | } | |
4584 | ||
4585 | void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str) | |
4586 | { | |
4587 | if (nb_args != 2) { | |
4588 | return; | |
4589 | } | |
4590 | ringbuf_completion(rs, str); | |
4591 | } | |
4592 | ||
2da1b3ab HB |
4593 | void device_del_completion(ReadLineState *rs, int nb_args, const char *str) |
4594 | { | |
4595 | size_t len; | |
4596 | ||
4597 | if (nb_args != 2) { | |
4598 | return; | |
4599 | } | |
4600 | ||
4601 | len = strlen(str); | |
4602 | readline_set_completion_index(rs, len); | |
4603 | device_del_bus_completion(rs, sysbus_get_default(), str, len); | |
4604 | } | |
4605 | ||
bfa40f77 | 4606 | void object_del_completion(ReadLineState *rs, int nb_args, const char *str) |
b48fa074 HB |
4607 | { |
4608 | ObjectPropertyInfoList *list, *start; | |
4609 | size_t len; | |
4610 | ||
bfa40f77 HB |
4611 | if (nb_args != 2) { |
4612 | return; | |
4613 | } | |
b48fa074 HB |
4614 | len = strlen(str); |
4615 | readline_set_completion_index(rs, len); | |
4616 | ||
4617 | start = list = qmp_qom_list("/objects", NULL); | |
4618 | while (list) { | |
4619 | ObjectPropertyInfo *info = list->value; | |
4620 | ||
4621 | if (!strncmp(info->type, "child<", 5) | |
4622 | && !strncmp(info->name, str, len)) { | |
4623 | readline_add_completion(rs, info->name); | |
4624 | } | |
4625 | list = list->next; | |
4626 | } | |
4627 | qapi_free_ObjectPropertyInfoList(start); | |
4628 | } | |
4629 | ||
29136cd8 HB |
4630 | void sendkey_completion(ReadLineState *rs, int nb_args, const char *str) |
4631 | { | |
4632 | int i; | |
4633 | char *sep; | |
4634 | size_t len; | |
4635 | ||
4636 | if (nb_args != 2) { | |
4637 | return; | |
4638 | } | |
4639 | sep = strrchr(str, '-'); | |
4640 | if (sep) { | |
4641 | str = sep + 1; | |
4642 | } | |
4643 | len = strlen(str); | |
4644 | readline_set_completion_index(rs, len); | |
4645 | for (i = 0; i < Q_KEY_CODE_MAX; i++) { | |
4646 | if (!strncmp(str, QKeyCode_lookup[i], len)) { | |
4647 | readline_add_completion(rs, QKeyCode_lookup[i]); | |
4648 | } | |
4649 | } | |
4650 | } | |
4651 | ||
40d19394 HB |
4652 | void set_link_completion(ReadLineState *rs, int nb_args, const char *str) |
4653 | { | |
4654 | size_t len; | |
4655 | ||
4656 | len = strlen(str); | |
4657 | readline_set_completion_index(rs, len); | |
4658 | if (nb_args == 2) { | |
4659 | NetClientState *ncs[255]; | |
4660 | int count, i; | |
4661 | count = qemu_find_net_clients_except(NULL, ncs, | |
4662 | NET_CLIENT_OPTIONS_KIND_NONE, 255); | |
4663 | for (i = 0; i < count; i++) { | |
4664 | const char *name = ncs[i]->name; | |
4665 | if (!strncmp(str, name, len)) { | |
4666 | readline_add_completion(rs, name); | |
4667 | } | |
4668 | } | |
4669 | } else if (nb_args == 3) { | |
4670 | add_completion_option(rs, str, "on"); | |
4671 | add_completion_option(rs, str, "off"); | |
4672 | } | |
4673 | } | |
4674 | ||
11b389f2 HB |
4675 | void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str) |
4676 | { | |
4677 | int len, count, i; | |
4678 | NetClientState *ncs[255]; | |
4679 | ||
4680 | if (nb_args != 2) { | |
4681 | return; | |
4682 | } | |
4683 | ||
4684 | len = strlen(str); | |
4685 | readline_set_completion_index(rs, len); | |
4686 | count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC, | |
4687 | 255); | |
4688 | for (i = 0; i < count; i++) { | |
4689 | QemuOpts *opts; | |
4690 | const char *name = ncs[i]->name; | |
4691 | if (strncmp(str, name, len)) { | |
4692 | continue; | |
4693 | } | |
4694 | opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name); | |
4695 | if (opts) { | |
4696 | readline_add_completion(rs, name); | |
4697 | } | |
4698 | } | |
4699 | } | |
4700 | ||
d0ece345 HB |
4701 | void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str) |
4702 | { | |
4703 | if (nb_args != 2) { | |
4704 | return; | |
4705 | } | |
4706 | readline_set_completion_index(rs, strlen(str)); | |
4707 | add_completion_option(rs, str, "reset"); | |
4708 | add_completion_option(rs, str, "shutdown"); | |
4709 | add_completion_option(rs, str, "poweroff"); | |
4710 | add_completion_option(rs, str, "pause"); | |
4711 | add_completion_option(rs, str, "debug"); | |
4712 | add_completion_option(rs, str, "none"); | |
4713 | } | |
4714 | ||
c68a0409 HB |
4715 | void migrate_set_capability_completion(ReadLineState *rs, int nb_args, |
4716 | const char *str) | |
4717 | { | |
4718 | size_t len; | |
4719 | ||
4720 | len = strlen(str); | |
4721 | readline_set_completion_index(rs, len); | |
4722 | if (nb_args == 2) { | |
4723 | int i; | |
4724 | for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) { | |
4725 | const char *name = MigrationCapability_lookup[i]; | |
4726 | if (!strncmp(str, name, len)) { | |
4727 | readline_add_completion(rs, name); | |
4728 | } | |
4729 | } | |
4730 | } else if (nb_args == 3) { | |
4731 | add_completion_option(rs, str, "on"); | |
4732 | add_completion_option(rs, str, "off"); | |
4733 | } | |
4734 | } | |
4735 | ||
e3bb532c HB |
4736 | void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str) |
4737 | { | |
4738 | int i; | |
4739 | size_t len; | |
4740 | if (nb_args != 2) { | |
4741 | return; | |
4742 | } | |
4743 | len = strlen(str); | |
4744 | readline_set_completion_index(rs, len); | |
4745 | for (i = 0; host_net_devices[i]; i++) { | |
4746 | if (!strncmp(host_net_devices[i], str, len)) { | |
4747 | readline_add_completion(rs, host_net_devices[i]); | |
4748 | } | |
4749 | } | |
4750 | } | |
4751 | ||
ddd6b45c HB |
4752 | void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str) |
4753 | { | |
4754 | NetClientState *ncs[255]; | |
4755 | int count, i, len; | |
4756 | ||
4757 | len = strlen(str); | |
4758 | readline_set_completion_index(rs, len); | |
4759 | if (nb_args == 2) { | |
4760 | count = qemu_find_net_clients_except(NULL, ncs, | |
4761 | NET_CLIENT_OPTIONS_KIND_NONE, 255); | |
4762 | for (i = 0; i < count; i++) { | |
4763 | int id; | |
4764 | char name[16]; | |
4765 | ||
4766 | if (net_hub_id_for_client(ncs[i], &id)) { | |
4767 | continue; | |
4768 | } | |
4769 | snprintf(name, sizeof(name), "%d", id); | |
4770 | if (!strncmp(str, name, len)) { | |
4771 | readline_add_completion(rs, name); | |
4772 | } | |
4773 | } | |
4774 | return; | |
4775 | } else if (nb_args == 3) { | |
4776 | count = qemu_find_net_clients_except(NULL, ncs, | |
4777 | NET_CLIENT_OPTIONS_KIND_NIC, 255); | |
4778 | for (i = 0; i < count; i++) { | |
4779 | const char *name; | |
4780 | ||
4781 | name = ncs[i]->name; | |
4782 | if (!strncmp(str, name, len)) { | |
4783 | readline_add_completion(rs, name); | |
4784 | } | |
4785 | } | |
4786 | return; | |
4787 | } | |
4788 | } | |
4789 | ||
b21631f3 HB |
4790 | static void vm_completion(ReadLineState *rs, const char *str) |
4791 | { | |
4792 | size_t len; | |
4793 | BlockDriverState *bs = NULL; | |
4794 | ||
4795 | len = strlen(str); | |
4796 | readline_set_completion_index(rs, len); | |
4797 | while ((bs = bdrv_next(bs))) { | |
4798 | SnapshotInfoList *snapshots, *snapshot; | |
4799 | ||
4800 | if (!bdrv_can_snapshot(bs)) { | |
4801 | continue; | |
4802 | } | |
4803 | if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) { | |
4804 | continue; | |
4805 | } | |
4806 | snapshot = snapshots; | |
4807 | while (snapshot) { | |
4808 | char *completion = snapshot->value->name; | |
4809 | if (!strncmp(str, completion, len)) { | |
4810 | readline_add_completion(rs, completion); | |
4811 | } | |
4812 | completion = snapshot->value->id; | |
4813 | if (!strncmp(str, completion, len)) { | |
4814 | readline_add_completion(rs, completion); | |
4815 | } | |
4816 | snapshot = snapshot->next; | |
4817 | } | |
4818 | qapi_free_SnapshotInfoList(snapshots); | |
4819 | } | |
4820 | ||
4821 | } | |
4822 | ||
4823 | void delvm_completion(ReadLineState *rs, int nb_args, const char *str) | |
4824 | { | |
4825 | if (nb_args == 2) { | |
4826 | vm_completion(rs, str); | |
4827 | } | |
4828 | } | |
4829 | ||
4830 | void loadvm_completion(ReadLineState *rs, int nb_args, const char *str) | |
4831 | { | |
4832 | if (nb_args == 2) { | |
4833 | vm_completion(rs, str); | |
4834 | } | |
4835 | } | |
4836 | ||
c35b6400 WX |
4837 | static void monitor_find_completion_by_table(Monitor *mon, |
4838 | const mon_cmd_t *cmd_table, | |
4839 | char **args, | |
4840 | int nb_args) | |
81d0912d FB |
4841 | { |
4842 | const char *cmdname; | |
c35b6400 | 4843 | int i; |
81d0912d | 4844 | const char *ptype, *str; |
c227f099 | 4845 | const mon_cmd_t *cmd; |
599a926a | 4846 | MonitorBlockComplete mbs; |
81d0912d | 4847 | |
81d0912d FB |
4848 | if (nb_args <= 1) { |
4849 | /* command completion */ | |
4850 | if (nb_args == 0) | |
4851 | cmdname = ""; | |
4852 | else | |
4853 | cmdname = args[0]; | |
d2674b2c | 4854 | readline_set_completion_index(mon->rs, strlen(cmdname)); |
c35b6400 | 4855 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
cd5c6bba | 4856 | cmd_completion(mon, cmdname, cmd->name); |
81d0912d FB |
4857 | } |
4858 | } else { | |
4859 | /* find the command */ | |
c35b6400 | 4860 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
03a63484 JK |
4861 | if (compare_cmd(args[0], cmd->name)) { |
4862 | break; | |
4863 | } | |
81d0912d | 4864 | } |
03a63484 | 4865 | if (!cmd->name) { |
c35b6400 | 4866 | return; |
03a63484 JK |
4867 | } |
4868 | ||
d903a779 WX |
4869 | if (cmd->sub_table) { |
4870 | /* do the job again */ | |
4871 | return monitor_find_completion_by_table(mon, cmd->sub_table, | |
4872 | &args[1], nb_args - 1); | |
4873 | } | |
bfa40f77 HB |
4874 | if (cmd->command_completion) { |
4875 | return cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]); | |
4876 | } | |
d903a779 | 4877 | |
4d76d2ba | 4878 | ptype = next_arg_type(cmd->args_type); |
81d0912d FB |
4879 | for(i = 0; i < nb_args - 2; i++) { |
4880 | if (*ptype != '\0') { | |
4d76d2ba | 4881 | ptype = next_arg_type(ptype); |
81d0912d | 4882 | while (*ptype == '?') |
4d76d2ba | 4883 | ptype = next_arg_type(ptype); |
81d0912d FB |
4884 | } |
4885 | } | |
4886 | str = args[nb_args - 1]; | |
2a1704a7 | 4887 | if (*ptype == '-' && ptype[1] != '\0') { |
3b6dbf27 | 4888 | ptype = next_arg_type(ptype); |
2a1704a7 | 4889 | } |
81d0912d FB |
4890 | switch(*ptype) { |
4891 | case 'F': | |
4892 | /* file completion */ | |
d2674b2c | 4893 | readline_set_completion_index(mon->rs, strlen(str)); |
cb8f68b1 | 4894 | file_completion(mon, str); |
81d0912d FB |
4895 | break; |
4896 | case 'B': | |
4897 | /* block device name completion */ | |
599a926a WX |
4898 | mbs.mon = mon; |
4899 | mbs.input = str; | |
4900 | readline_set_completion_index(mon->rs, strlen(str)); | |
4901 | bdrv_iterate(block_completion_it, &mbs); | |
81d0912d | 4902 | break; |
7fe48483 | 4903 | case 's': |
129be006 | 4904 | case 'S': |
29136cd8 | 4905 | if (!strcmp(cmd->name, "help|?")) { |
7ca0e061 WX |
4906 | monitor_find_completion_by_table(mon, cmd_table, |
4907 | &args[1], nb_args - 1); | |
7fe48483 FB |
4908 | } |
4909 | break; | |
81d0912d FB |
4910 | default: |
4911 | break; | |
4912 | } | |
4913 | } | |
c35b6400 WX |
4914 | } |
4915 | ||
c60bf339 | 4916 | static void monitor_find_completion(void *opaque, |
c35b6400 WX |
4917 | const char *cmdline) |
4918 | { | |
c60bf339 | 4919 | Monitor *mon = opaque; |
c35b6400 WX |
4920 | char *args[MAX_ARGS]; |
4921 | int nb_args, len; | |
4922 | ||
4923 | /* 1. parse the cmdline */ | |
4924 | if (parse_cmdline(cmdline, &nb_args, args) < 0) { | |
4925 | return; | |
4926 | } | |
4927 | #ifdef DEBUG_COMPLETION | |
4928 | for (i = 0; i < nb_args; i++) { | |
4929 | monitor_printf(mon, "arg%d = '%s'\n", i, args[i]); | |
4930 | } | |
4931 | #endif | |
4932 | ||
4933 | /* if the line ends with a space, it means we want to complete the | |
4934 | next arg */ | |
4935 | len = strlen(cmdline); | |
4936 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { | |
4937 | if (nb_args >= MAX_ARGS) { | |
4938 | goto cleanup; | |
4939 | } | |
4940 | args[nb_args++] = g_strdup(""); | |
4941 | } | |
4942 | ||
4943 | /* 2. auto complete according to args */ | |
4944 | monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args); | |
03a63484 JK |
4945 | |
4946 | cleanup: | |
dcc70cdf | 4947 | free_cmdline_args(args, nb_args); |
81d0912d FB |
4948 | } |
4949 | ||
731b0364 | 4950 | static int monitor_can_read(void *opaque) |
9dc39cba | 4951 | { |
731b0364 AL |
4952 | Monitor *mon = opaque; |
4953 | ||
c62313bb | 4954 | return (mon->suspend_cnt == 0) ? 1 : 0; |
9dc39cba FB |
4955 | } |
4956 | ||
09069b19 | 4957 | static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name) |
5fa737a4 | 4958 | { |
09069b19 LC |
4959 | int is_cap = compare_cmd(cmd_name, "qmp_capabilities"); |
4960 | return (qmp_cmd_mode(mon) ? is_cap : !is_cap); | |
5fa737a4 LC |
4961 | } |
4962 | ||
4af9193a LC |
4963 | /* |
4964 | * Argument validation rules: | |
4965 | * | |
4966 | * 1. The argument must exist in cmd_args qdict | |
4967 | * 2. The argument type must be the expected one | |
4968 | * | |
4969 | * Special case: If the argument doesn't exist in cmd_args and | |
4970 | * the QMP_ACCEPT_UNKNOWNS flag is set, then the | |
4971 | * checking is skipped for it. | |
4972 | */ | |
4973 | static int check_client_args_type(const QDict *client_args, | |
4974 | const QDict *cmd_args, int flags) | |
5fa737a4 | 4975 | { |
4af9193a | 4976 | const QDictEntry *ent; |
5fa737a4 | 4977 | |
4af9193a LC |
4978 | for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){ |
4979 | QObject *obj; | |
4980 | QString *arg_type; | |
4981 | const QObject *client_arg = qdict_entry_value(ent); | |
4982 | const char *client_arg_name = qdict_entry_key(ent); | |
4983 | ||
4984 | obj = qdict_get(cmd_args, client_arg_name); | |
4985 | if (!obj) { | |
4986 | if (flags & QMP_ACCEPT_UNKNOWNS) { | |
4987 | /* handler accepts unknowns */ | |
4988 | continue; | |
4989 | } | |
4990 | /* client arg doesn't exist */ | |
4991 | qerror_report(QERR_INVALID_PARAMETER, client_arg_name); | |
4992 | return -1; | |
4993 | } | |
5fa737a4 | 4994 | |
4af9193a LC |
4995 | arg_type = qobject_to_qstring(obj); |
4996 | assert(arg_type != NULL); | |
5fa737a4 | 4997 | |
4af9193a LC |
4998 | /* check if argument's type is correct */ |
4999 | switch (qstring_get_str(arg_type)[0]) { | |
5fa737a4 LC |
5000 | case 'F': |
5001 | case 'B': | |
5002 | case 's': | |
4af9193a LC |
5003 | if (qobject_type(client_arg) != QTYPE_QSTRING) { |
5004 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
5005 | "string"); | |
5fa737a4 LC |
5006 | return -1; |
5007 | } | |
4af9193a | 5008 | break; |
5fa737a4 LC |
5009 | case 'i': |
5010 | case 'l': | |
b6e098d7 | 5011 | case 'M': |
dbc0c67f | 5012 | case 'o': |
4af9193a LC |
5013 | if (qobject_type(client_arg) != QTYPE_QINT) { |
5014 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
5015 | "int"); | |
5016 | return -1; | |
5fa737a4 LC |
5017 | } |
5018 | break; | |
fccfb11e | 5019 | case 'T': |
4af9193a LC |
5020 | if (qobject_type(client_arg) != QTYPE_QINT && |
5021 | qobject_type(client_arg) != QTYPE_QFLOAT) { | |
5022 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
5023 | "number"); | |
5024 | return -1; | |
3350a4dd MA |
5025 | } |
5026 | break; | |
942cd1f2 | 5027 | case 'b': |
5fa737a4 | 5028 | case '-': |
4af9193a LC |
5029 | if (qobject_type(client_arg) != QTYPE_QBOOL) { |
5030 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
5031 | "bool"); | |
5032 | return -1; | |
5fa737a4 LC |
5033 | } |
5034 | break; | |
361127df | 5035 | case 'O': |
4af9193a LC |
5036 | assert(flags & QMP_ACCEPT_UNKNOWNS); |
5037 | break; | |
b9f8978c PB |
5038 | case 'q': |
5039 | /* Any QObject can be passed. */ | |
5040 | break; | |
4af9193a LC |
5041 | case '/': |
5042 | case '.': | |
5043 | /* | |
5044 | * These types are not supported by QMP and thus are not | |
5045 | * handled here. Fall through. | |
5046 | */ | |
5fa737a4 | 5047 | default: |
5fa737a4 | 5048 | abort(); |
4af9193a LC |
5049 | } |
5050 | } | |
5051 | ||
5052 | return 0; | |
5053 | } | |
5054 | ||
2dbc8db0 LC |
5055 | /* |
5056 | * - Check if the client has passed all mandatory args | |
5057 | * - Set special flags for argument validation | |
5058 | */ | |
5059 | static int check_mandatory_args(const QDict *cmd_args, | |
5060 | const QDict *client_args, int *flags) | |
5061 | { | |
5062 | const QDictEntry *ent; | |
5063 | ||
5064 | for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) { | |
5065 | const char *cmd_arg_name = qdict_entry_key(ent); | |
5066 | QString *type = qobject_to_qstring(qdict_entry_value(ent)); | |
5067 | assert(type != NULL); | |
5068 | ||
5069 | if (qstring_get_str(type)[0] == 'O') { | |
5070 | assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0); | |
5071 | *flags |= QMP_ACCEPT_UNKNOWNS; | |
5072 | } else if (qstring_get_str(type)[0] != '-' && | |
5073 | qstring_get_str(type)[1] != '?' && | |
5074 | !qdict_haskey(client_args, cmd_arg_name)) { | |
5075 | qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name); | |
5076 | return -1; | |
5077 | } | |
5fa737a4 LC |
5078 | } |
5079 | ||
5080 | return 0; | |
5081 | } | |
5082 | ||
2dbc8db0 | 5083 | static QDict *qdict_from_args_type(const char *args_type) |
5fa737a4 | 5084 | { |
2dbc8db0 LC |
5085 | int i; |
5086 | QDict *qdict; | |
5087 | QString *key, *type, *cur_qs; | |
5088 | ||
5089 | assert(args_type != NULL); | |
5090 | ||
5091 | qdict = qdict_new(); | |
5092 | ||
5093 | if (args_type == NULL || args_type[0] == '\0') { | |
5094 | /* no args, empty qdict */ | |
5095 | goto out; | |
5096 | } | |
5097 | ||
5098 | key = qstring_new(); | |
5099 | type = qstring_new(); | |
5100 | ||
5101 | cur_qs = key; | |
5102 | ||
5103 | for (i = 0;; i++) { | |
5104 | switch (args_type[i]) { | |
5105 | case ',': | |
5106 | case '\0': | |
5107 | qdict_put(qdict, qstring_get_str(key), type); | |
5108 | QDECREF(key); | |
5109 | if (args_type[i] == '\0') { | |
5110 | goto out; | |
5111 | } | |
5112 | type = qstring_new(); /* qdict has ref */ | |
5113 | cur_qs = key = qstring_new(); | |
5114 | break; | |
5115 | case ':': | |
5116 | cur_qs = type; | |
5117 | break; | |
5118 | default: | |
5119 | qstring_append_chr(cur_qs, args_type[i]); | |
5120 | break; | |
5121 | } | |
5122 | } | |
5123 | ||
5124 | out: | |
5125 | return qdict; | |
5fa737a4 LC |
5126 | } |
5127 | ||
2dbc8db0 LC |
5128 | /* |
5129 | * Client argument checking rules: | |
5130 | * | |
5131 | * 1. Client must provide all mandatory arguments | |
4af9193a LC |
5132 | * 2. Each argument provided by the client must be expected |
5133 | * 3. Each argument provided by the client must have the type expected | |
5134 | * by the command | |
2dbc8db0 LC |
5135 | */ |
5136 | static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args) | |
361127df | 5137 | { |
2dbc8db0 LC |
5138 | int flags, err; |
5139 | QDict *cmd_args; | |
5140 | ||
5141 | cmd_args = qdict_from_args_type(cmd->args_type); | |
5142 | ||
5143 | flags = 0; | |
5144 | err = check_mandatory_args(cmd_args, client_args, &flags); | |
5145 | if (err) { | |
5146 | goto out; | |
5147 | } | |
5148 | ||
4af9193a | 5149 | err = check_client_args_type(client_args, cmd_args, flags); |
2dbc8db0 LC |
5150 | |
5151 | out: | |
5152 | QDECREF(cmd_args); | |
5153 | return err; | |
361127df MA |
5154 | } |
5155 | ||
5fa737a4 | 5156 | /* |
c917c8f3 | 5157 | * Input object checking rules |
5fa737a4 | 5158 | * |
c917c8f3 LC |
5159 | * 1. Input object must be a dict |
5160 | * 2. The "execute" key must exist | |
5161 | * 3. The "execute" key must be a string | |
5162 | * 4. If the "arguments" key exists, it must be a dict | |
5163 | * 5. If the "id" key exists, it can be anything (ie. json-value) | |
5164 | * 6. Any argument not listed above is considered invalid | |
5fa737a4 | 5165 | */ |
c917c8f3 | 5166 | static QDict *qmp_check_input_obj(QObject *input_obj) |
5fa737a4 | 5167 | { |
c917c8f3 LC |
5168 | const QDictEntry *ent; |
5169 | int has_exec_key = 0; | |
5170 | QDict *input_dict; | |
5fa737a4 | 5171 | |
c917c8f3 LC |
5172 | if (qobject_type(input_obj) != QTYPE_QDICT) { |
5173 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object"); | |
5174 | return NULL; | |
5fa737a4 LC |
5175 | } |
5176 | ||
c917c8f3 | 5177 | input_dict = qobject_to_qdict(input_obj); |
5fa737a4 | 5178 | |
c917c8f3 LC |
5179 | for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){ |
5180 | const char *arg_name = qdict_entry_key(ent); | |
5181 | const QObject *arg_obj = qdict_entry_value(ent); | |
5fa737a4 | 5182 | |
c917c8f3 LC |
5183 | if (!strcmp(arg_name, "execute")) { |
5184 | if (qobject_type(arg_obj) != QTYPE_QSTRING) { | |
5185 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute", | |
5186 | "string"); | |
5187 | return NULL; | |
361127df | 5188 | } |
c917c8f3 LC |
5189 | has_exec_key = 1; |
5190 | } else if (!strcmp(arg_name, "arguments")) { | |
5191 | if (qobject_type(arg_obj) != QTYPE_QDICT) { | |
5192 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments", | |
5193 | "object"); | |
5194 | return NULL; | |
5fa737a4 | 5195 | } |
c917c8f3 LC |
5196 | } else if (!strcmp(arg_name, "id")) { |
5197 | /* FIXME: check duplicated IDs for async commands */ | |
5fa737a4 | 5198 | } else { |
c917c8f3 LC |
5199 | qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name); |
5200 | return NULL; | |
5fa737a4 LC |
5201 | } |
5202 | } | |
5203 | ||
c917c8f3 LC |
5204 | if (!has_exec_key) { |
5205 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute"); | |
5206 | return NULL; | |
5207 | } | |
5fa737a4 | 5208 | |
c917c8f3 | 5209 | return input_dict; |
09069b19 LC |
5210 | } |
5211 | ||
fc29df75 LC |
5212 | static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd, |
5213 | const QDict *params) | |
5214 | { | |
5215 | int ret; | |
5216 | QObject *data = NULL; | |
5217 | ||
fc29df75 LC |
5218 | ret = cmd->mhandler.cmd_new(mon, params, &data); |
5219 | handler_audit(mon, cmd, ret); | |
5220 | monitor_protocol_emitter(mon, data); | |
5221 | qobject_decref(data); | |
5222 | } | |
5223 | ||
5fa737a4 LC |
5224 | static void handle_qmp_command(JSONMessageParser *parser, QList *tokens) |
5225 | { | |
5226 | int err; | |
5227 | QObject *obj; | |
5228 | QDict *input, *args; | |
5fa737a4 | 5229 | const mon_cmd_t *cmd; |
40e5a01d | 5230 | const char *cmd_name; |
5fa737a4 LC |
5231 | Monitor *mon = cur_mon; |
5232 | ||
e4940c60 | 5233 | args = input = NULL; |
5fa737a4 LC |
5234 | |
5235 | obj = json_parser_parse(tokens, NULL); | |
5236 | if (!obj) { | |
5237 | // FIXME: should be triggered in json_parser_parse() | |
ab5b027e | 5238 | qerror_report(QERR_JSON_PARSING); |
5fa737a4 | 5239 | goto err_out; |
5fa737a4 LC |
5240 | } |
5241 | ||
c917c8f3 LC |
5242 | input = qmp_check_input_obj(obj); |
5243 | if (!input) { | |
5fa737a4 LC |
5244 | qobject_decref(obj); |
5245 | goto err_out; | |
5246 | } | |
5247 | ||
5fa737a4 LC |
5248 | mon->mc->id = qdict_get(input, "id"); |
5249 | qobject_incref(mon->mc->id); | |
5250 | ||
0bbab46d | 5251 | cmd_name = qdict_get_str(input, "execute"); |
89bd820a | 5252 | trace_handle_qmp_command(mon, cmd_name); |
09069b19 | 5253 | if (invalid_qmp_mode(mon, cmd_name)) { |
ab5b027e | 5254 | qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name); |
e4940c60 | 5255 | goto err_out; |
09069b19 LC |
5256 | } |
5257 | ||
e3193601 | 5258 | cmd = qmp_find_cmd(cmd_name); |
d1249eaa | 5259 | if (!cmd) { |
ab5b027e | 5260 | qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name); |
e4940c60 | 5261 | goto err_out; |
5fa737a4 LC |
5262 | } |
5263 | ||
5264 | obj = qdict_get(input, "arguments"); | |
5265 | if (!obj) { | |
5266 | args = qdict_new(); | |
5267 | } else { | |
5268 | args = qobject_to_qdict(obj); | |
5269 | QINCREF(args); | |
5270 | } | |
5271 | ||
2dbc8db0 | 5272 | err = qmp_check_client_args(cmd, args); |
5fa737a4 LC |
5273 | if (err < 0) { |
5274 | goto err_out; | |
5275 | } | |
5276 | ||
40e5a01d | 5277 | if (handler_is_async(cmd)) { |
5af7bbae LC |
5278 | err = qmp_async_cmd_handler(mon, cmd, args); |
5279 | if (err) { | |
5280 | /* emit the error response */ | |
5281 | goto err_out; | |
5282 | } | |
940cc30d | 5283 | } else { |
fc29df75 | 5284 | qmp_call_cmd(mon, cmd, args); |
940cc30d | 5285 | } |
e4940c60 | 5286 | |
5fa737a4 LC |
5287 | goto out; |
5288 | ||
5fa737a4 LC |
5289 | err_out: |
5290 | monitor_protocol_emitter(mon, NULL); | |
5291 | out: | |
e4940c60 | 5292 | QDECREF(input); |
5fa737a4 | 5293 | QDECREF(args); |
5fa737a4 LC |
5294 | } |
5295 | ||
9b57c02e LC |
5296 | /** |
5297 | * monitor_control_read(): Read and handle QMP input | |
5298 | */ | |
5299 | static void monitor_control_read(void *opaque, const uint8_t *buf, int size) | |
5300 | { | |
5301 | Monitor *old_mon = cur_mon; | |
5302 | ||
5303 | cur_mon = opaque; | |
5304 | ||
5fa737a4 | 5305 | json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size); |
9b57c02e LC |
5306 | |
5307 | cur_mon = old_mon; | |
5308 | } | |
5309 | ||
731b0364 | 5310 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 5311 | { |
731b0364 | 5312 | Monitor *old_mon = cur_mon; |
7e2515e8 | 5313 | int i; |
376253ec | 5314 | |
731b0364 AL |
5315 | cur_mon = opaque; |
5316 | ||
cde76ee1 AL |
5317 | if (cur_mon->rs) { |
5318 | for (i = 0; i < size; i++) | |
5319 | readline_handle_byte(cur_mon->rs, buf[i]); | |
5320 | } else { | |
5321 | if (size == 0 || buf[size - 1] != 0) | |
5322 | monitor_printf(cur_mon, "corrupted command\n"); | |
5323 | else | |
f3c157c4 | 5324 | handle_user_command(cur_mon, (char *)buf); |
cde76ee1 | 5325 | } |
9dc39cba | 5326 | |
731b0364 AL |
5327 | cur_mon = old_mon; |
5328 | } | |
d8f44609 | 5329 | |
c60bf339 SH |
5330 | static void monitor_command_cb(void *opaque, const char *cmdline, |
5331 | void *readline_opaque) | |
aa455485 | 5332 | { |
c60bf339 SH |
5333 | Monitor *mon = opaque; |
5334 | ||
731b0364 | 5335 | monitor_suspend(mon); |
f3c157c4 | 5336 | handle_user_command(mon, cmdline); |
731b0364 | 5337 | monitor_resume(mon); |
d8f44609 AL |
5338 | } |
5339 | ||
cde76ee1 | 5340 | int monitor_suspend(Monitor *mon) |
d8f44609 | 5341 | { |
cde76ee1 AL |
5342 | if (!mon->rs) |
5343 | return -ENOTTY; | |
731b0364 | 5344 | mon->suspend_cnt++; |
cde76ee1 | 5345 | return 0; |
d8f44609 AL |
5346 | } |
5347 | ||
376253ec | 5348 | void monitor_resume(Monitor *mon) |
d8f44609 | 5349 | { |
cde76ee1 AL |
5350 | if (!mon->rs) |
5351 | return; | |
731b0364 AL |
5352 | if (--mon->suspend_cnt == 0) |
5353 | readline_show_prompt(mon->rs); | |
aa455485 FB |
5354 | } |
5355 | ||
ca9567e2 LC |
5356 | static QObject *get_qmp_greeting(void) |
5357 | { | |
b9c15f16 | 5358 | QObject *ver = NULL; |
ca9567e2 | 5359 | |
b9c15f16 | 5360 | qmp_marshal_input_query_version(NULL, NULL, &ver); |
ca9567e2 LC |
5361 | return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver); |
5362 | } | |
5363 | ||
9b57c02e LC |
5364 | /** |
5365 | * monitor_control_event(): Print QMP gretting | |
5366 | */ | |
5367 | static void monitor_control_event(void *opaque, int event) | |
5368 | { | |
47116d1c LC |
5369 | QObject *data; |
5370 | Monitor *mon = opaque; | |
9b57c02e | 5371 | |
47116d1c LC |
5372 | switch (event) { |
5373 | case CHR_EVENT_OPENED: | |
4a7e1190 | 5374 | mon->mc->command_mode = 0; |
ca9567e2 | 5375 | data = get_qmp_greeting(); |
9b57c02e LC |
5376 | monitor_json_emitter(mon, data); |
5377 | qobject_decref(data); | |
efb87c16 | 5378 | mon_refcount++; |
47116d1c LC |
5379 | break; |
5380 | case CHR_EVENT_CLOSED: | |
5381 | json_message_parser_destroy(&mon->mc->parser); | |
58617a79 | 5382 | json_message_parser_init(&mon->mc->parser, handle_qmp_command); |
efb87c16 CB |
5383 | mon_refcount--; |
5384 | monitor_fdsets_cleanup(); | |
47116d1c | 5385 | break; |
9b57c02e LC |
5386 | } |
5387 | } | |
5388 | ||
731b0364 | 5389 | static void monitor_event(void *opaque, int event) |
86e94dea | 5390 | { |
376253ec AL |
5391 | Monitor *mon = opaque; |
5392 | ||
2724b180 AL |
5393 | switch (event) { |
5394 | case CHR_EVENT_MUX_IN: | |
a7aec5da GH |
5395 | mon->mux_out = 0; |
5396 | if (mon->reset_seen) { | |
5397 | readline_restart(mon->rs); | |
5398 | monitor_resume(mon); | |
5399 | monitor_flush(mon); | |
5400 | } else { | |
5401 | mon->suspend_cnt = 0; | |
5402 | } | |
2724b180 AL |
5403 | break; |
5404 | ||
5405 | case CHR_EVENT_MUX_OUT: | |
a7aec5da GH |
5406 | if (mon->reset_seen) { |
5407 | if (mon->suspend_cnt == 0) { | |
5408 | monitor_printf(mon, "\n"); | |
5409 | } | |
5410 | monitor_flush(mon); | |
5411 | monitor_suspend(mon); | |
5412 | } else { | |
5413 | mon->suspend_cnt++; | |
5414 | } | |
5415 | mon->mux_out = 1; | |
2724b180 | 5416 | break; |
86e94dea | 5417 | |
b6b8df56 | 5418 | case CHR_EVENT_OPENED: |
2724b180 AL |
5419 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " |
5420 | "information\n", QEMU_VERSION); | |
a7aec5da | 5421 | if (!mon->mux_out) { |
2724b180 | 5422 | readline_show_prompt(mon->rs); |
a7aec5da GH |
5423 | } |
5424 | mon->reset_seen = 1; | |
efb87c16 CB |
5425 | mon_refcount++; |
5426 | break; | |
5427 | ||
5428 | case CHR_EVENT_CLOSED: | |
5429 | mon_refcount--; | |
5430 | monitor_fdsets_cleanup(); | |
2724b180 AL |
5431 | break; |
5432 | } | |
86e94dea TS |
5433 | } |
5434 | ||
816f8925 WX |
5435 | static int |
5436 | compare_mon_cmd(const void *a, const void *b) | |
5437 | { | |
5438 | return strcmp(((const mon_cmd_t *)a)->name, | |
5439 | ((const mon_cmd_t *)b)->name); | |
5440 | } | |
5441 | ||
5442 | static void sortcmdlist(void) | |
5443 | { | |
5444 | int array_num; | |
5445 | int elem_size = sizeof(mon_cmd_t); | |
5446 | ||
5447 | array_num = sizeof(mon_cmds)/elem_size-1; | |
5448 | qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd); | |
5449 | ||
5450 | array_num = sizeof(info_cmds)/elem_size-1; | |
5451 | qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd); | |
5452 | } | |
5453 | ||
76655d6d AL |
5454 | |
5455 | /* | |
5456 | * Local variables: | |
5457 | * c-indent-level: 4 | |
5458 | * c-basic-offset: 4 | |
5459 | * tab-width: 8 | |
5460 | * End: | |
5461 | */ | |
5462 | ||
c60bf339 SH |
5463 | /* These functions just adapt the readline interface in a typesafe way. We |
5464 | * could cast function pointers but that discards compiler checks. | |
5465 | */ | |
d5d1507b SW |
5466 | static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque, |
5467 | const char *fmt, ...) | |
c60bf339 SH |
5468 | { |
5469 | va_list ap; | |
5470 | va_start(ap, fmt); | |
5471 | monitor_vprintf(opaque, fmt, ap); | |
5472 | va_end(ap); | |
5473 | } | |
5474 | ||
5475 | static void monitor_readline_flush(void *opaque) | |
5476 | { | |
5477 | monitor_flush(opaque); | |
5478 | } | |
5479 | ||
731b0364 | 5480 | void monitor_init(CharDriverState *chr, int flags) |
aa455485 | 5481 | { |
731b0364 | 5482 | static int is_first_init = 1; |
87127161 | 5483 | Monitor *mon; |
20d8a3ed TS |
5484 | |
5485 | if (is_first_init) { | |
afeecec2 | 5486 | monitor_protocol_event_init(); |
43a14cfc | 5487 | monitor_qapi_event_init(); |
d038317c | 5488 | sortcmdlist(); |
20d8a3ed TS |
5489 | is_first_init = 0; |
5490 | } | |
87127161 | 5491 | |
b01fe89e WX |
5492 | mon = g_malloc(sizeof(*mon)); |
5493 | monitor_data_init(mon); | |
20d8a3ed | 5494 | |
87127161 | 5495 | mon->chr = chr; |
731b0364 | 5496 | mon->flags = flags; |
cde76ee1 | 5497 | if (flags & MONITOR_USE_READLINE) { |
c60bf339 SH |
5498 | mon->rs = readline_init(monitor_readline_printf, |
5499 | monitor_readline_flush, | |
5500 | mon, | |
5501 | monitor_find_completion); | |
cde76ee1 AL |
5502 | monitor_read_command(mon, 0); |
5503 | } | |
87127161 | 5504 | |
9b57c02e | 5505 | if (monitor_ctrl_mode(mon)) { |
7267c094 | 5506 | mon->mc = g_malloc0(sizeof(MonitorControl)); |
9b57c02e LC |
5507 | /* Control mode requires special handlers */ |
5508 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read, | |
5509 | monitor_control_event, mon); | |
15f31519 | 5510 | qemu_chr_fe_set_echo(chr, true); |
26efaca3 AL |
5511 | |
5512 | json_message_parser_init(&mon->mc->parser, handle_qmp_command); | |
9b57c02e LC |
5513 | } else { |
5514 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, | |
5515 | monitor_event, mon); | |
5516 | } | |
87127161 | 5517 | |
72cf2d4f | 5518 | QLIST_INSERT_HEAD(&mon_list, mon, entry); |
8631b608 MA |
5519 | if (!default_mon || (flags & MONITOR_IS_DEFAULT)) |
5520 | default_mon = mon; | |
aa455485 FB |
5521 | } |
5522 | ||
c60bf339 SH |
5523 | static void bdrv_password_cb(void *opaque, const char *password, |
5524 | void *readline_opaque) | |
81d0912d | 5525 | { |
c60bf339 SH |
5526 | Monitor *mon = opaque; |
5527 | BlockDriverState *bs = readline_opaque; | |
bb5fc20f | 5528 | int ret = 0; |
81d0912d | 5529 | |
bb5fc20f | 5530 | if (bdrv_set_key(bs, password) != 0) { |
376253ec | 5531 | monitor_printf(mon, "invalid password\n"); |
bb5fc20f | 5532 | ret = -EPERM; |
9dc39cba | 5533 | } |
731b0364 AL |
5534 | if (mon->password_completion_cb) |
5535 | mon->password_completion_cb(mon->password_opaque, ret); | |
bb5fc20f | 5536 | |
731b0364 | 5537 | monitor_read_command(mon, 1); |
9dc39cba | 5538 | } |
c0f4ce77 | 5539 | |
7060b478 AL |
5540 | ReadLineState *monitor_get_rs(Monitor *mon) |
5541 | { | |
5542 | return mon->rs; | |
5543 | } | |
5544 | ||
0bbc47bb LC |
5545 | int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs, |
5546 | BlockDriverCompletionFunc *completion_cb, | |
5547 | void *opaque) | |
c0f4ce77 | 5548 | { |
cde76ee1 AL |
5549 | int err; |
5550 | ||
bb5fc20f AL |
5551 | if (!bdrv_key_required(bs)) { |
5552 | if (completion_cb) | |
5553 | completion_cb(opaque, 0); | |
0bbc47bb | 5554 | return 0; |
bb5fc20f | 5555 | } |
c0f4ce77 | 5556 | |
94171e11 | 5557 | if (monitor_ctrl_mode(mon)) { |
903a8814 LC |
5558 | qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), |
5559 | bdrv_get_encrypted_filename(bs)); | |
0bbc47bb | 5560 | return -1; |
94171e11 LC |
5561 | } |
5562 | ||
376253ec AL |
5563 | monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs), |
5564 | bdrv_get_encrypted_filename(bs)); | |
bb5fc20f | 5565 | |
731b0364 AL |
5566 | mon->password_completion_cb = completion_cb; |
5567 | mon->password_opaque = opaque; | |
bb5fc20f | 5568 | |
cde76ee1 AL |
5569 | err = monitor_read_password(mon, bdrv_password_cb, bs); |
5570 | ||
5571 | if (err && completion_cb) | |
5572 | completion_cb(opaque, err); | |
0bbc47bb LC |
5573 | |
5574 | return err; | |
c0f4ce77 | 5575 | } |
e42e818b LC |
5576 | |
5577 | int monitor_read_block_device_key(Monitor *mon, const char *device, | |
5578 | BlockDriverCompletionFunc *completion_cb, | |
5579 | void *opaque) | |
5580 | { | |
5581 | BlockDriverState *bs; | |
5582 | ||
5583 | bs = bdrv_find(device); | |
5584 | if (!bs) { | |
5585 | monitor_printf(mon, "Device not found %s\n", device); | |
5586 | return -1; | |
5587 | } | |
5588 | ||
5589 | return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque); | |
5590 | } | |
4d454574 PB |
5591 | |
5592 | QemuOptsList qemu_mon_opts = { | |
5593 | .name = "mon", | |
5594 | .implied_opt_name = "chardev", | |
5595 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), | |
5596 | .desc = { | |
5597 | { | |
5598 | .name = "mode", | |
5599 | .type = QEMU_OPT_STRING, | |
5600 | },{ | |
5601 | .name = "chardev", | |
5602 | .type = QEMU_OPT_STRING, | |
5603 | },{ | |
5604 | .name = "default", | |
5605 | .type = QEMU_OPT_BOOL, | |
5606 | },{ | |
5607 | .name = "pretty", | |
5608 | .type = QEMU_OPT_BOOL, | |
5609 | }, | |
5610 | { /* end of list */ } | |
5611 | }, | |
5612 | }; |