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