]> git.proxmox.com Git - mirror_qemu.git/blame - monitor.c
qdev: add return value to init() callbacks.
[mirror_qemu.git] / monitor.c
CommitLineData
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"
cae4956e 26#include "hw/qdev.h"
87ecb68b
PB
27#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
9dd986cc 31#include "hw/watchdog.h"
87ecb68b
PB
32#include "gdbstub.h"
33#include "net.h"
34#include "qemu-char.h"
35#include "sysemu.h"
376253ec
AL
36#include "monitor.h"
37#include "readline.h"
87ecb68b
PB
38#include "console.h"
39#include "block.h"
40#include "audio/audio.h"
9307c4c1 41#include "disas.h"
df751fa8 42#include "balloon.h"
c8256f9d 43#include "qemu-timer.h"
5bb7910a 44#include "migration.h"
7ba1e619 45#include "kvm.h"
76655d6d 46#include "acl.h"
6a5bd307 47
9dc39cba 48//#define DEBUG
81d0912d 49//#define DEBUG_COMPLETION
9dc39cba 50
9307c4c1
FB
51/*
52 * Supported types:
5fafdf24 53 *
9307c4c1 54 * 'F' filename
81d0912d 55 * 'B' block device name
9307c4c1 56 * 's' string (accept optional quote)
92a31b1f
FB
57 * 'i' 32 bit integer
58 * 'l' target long (32 or 64 bit)
9307c4c1
FB
59 * '/' optional gdb-like print format (like "/10x")
60 *
61 * '?' optional type (for 'F', 's' and 'i')
62 *
63 */
64
376253ec 65typedef struct mon_cmd_t {
9dc39cba 66 const char *name;
9307c4c1 67 const char *args_type;
a5f1b965 68 void *handler;
9dc39cba
FB
69 const char *params;
70 const char *help;
376253ec 71} mon_cmd_t;
9dc39cba 72
f07918fd
MM
73/* file descriptors passed via SCM_RIGHTS */
74typedef struct mon_fd_t mon_fd_t;
75struct mon_fd_t {
76 char *name;
77 int fd;
78 LIST_ENTRY(mon_fd_t) next;
79};
80
87127161
AL
81struct Monitor {
82 CharDriverState *chr;
731b0364
AL
83 int flags;
84 int suspend_cnt;
85 uint8_t outbuf[1024];
86 int outbuf_index;
87 ReadLineState *rs;
88 CPUState *mon_cpu;
89 BlockDriverCompletionFunc *password_completion_cb;
90 void *password_opaque;
f07918fd 91 LIST_HEAD(,mon_fd_t) fds;
87127161
AL
92 LIST_ENTRY(Monitor) entry;
93};
94
95static LIST_HEAD(mon_list, Monitor) mon_list;
7e2515e8 96
376253ec
AL
97static const mon_cmd_t mon_cmds[];
98static const mon_cmd_t info_cmds[];
9dc39cba 99
87127161 100Monitor *cur_mon = NULL;
376253ec 101
731b0364
AL
102static void monitor_command_cb(Monitor *mon, const char *cmdline,
103 void *opaque);
83ab7950 104
731b0364
AL
105static void monitor_read_command(Monitor *mon, int show_prompt)
106{
107 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
108 if (show_prompt)
109 readline_show_prompt(mon->rs);
110}
6a00d601 111
cde76ee1
AL
112static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
113 void *opaque)
bb5fc20f 114{
cde76ee1
AL
115 if (mon->rs) {
116 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
117 /* prompt is printed on return from the command handler */
118 return 0;
119 } else {
120 monitor_printf(mon, "terminal does not support password prompting\n");
121 return -ENOTTY;
122 }
bb5fc20f
AL
123}
124
376253ec 125void monitor_flush(Monitor *mon)
7e2515e8 126{
731b0364
AL
127 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
128 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
129 mon->outbuf_index = 0;
7e2515e8
FB
130 }
131}
132
133/* flush at every end of line or if the buffer is full */
376253ec 134static void monitor_puts(Monitor *mon, const char *str)
7e2515e8 135{
60fe76f3 136 char c;
731b0364
AL
137
138 if (!mon)
139 return;
140
7e2515e8
FB
141 for(;;) {
142 c = *str++;
143 if (c == '\0')
144 break;
7ba1260a 145 if (c == '\n')
731b0364
AL
146 mon->outbuf[mon->outbuf_index++] = '\r';
147 mon->outbuf[mon->outbuf_index++] = c;
148 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
149 || c == '\n')
376253ec 150 monitor_flush(mon);
7e2515e8
FB
151 }
152}
153
376253ec 154void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
9dc39cba 155{
81d0912d 156 char buf[4096];
81d0912d 157 vsnprintf(buf, sizeof(buf), fmt, ap);
376253ec 158 monitor_puts(mon, buf);
9dc39cba
FB
159}
160
376253ec 161void monitor_printf(Monitor *mon, const char *fmt, ...)
9dc39cba 162{
7e2515e8
FB
163 va_list ap;
164 va_start(ap, fmt);
376253ec 165 monitor_vprintf(mon, fmt, ap);
7e2515e8 166 va_end(ap);
9dc39cba
FB
167}
168
376253ec 169void monitor_print_filename(Monitor *mon, const char *filename)
fef30743
TS
170{
171 int i;
172
173 for (i = 0; filename[i]; i++) {
28a76be8
AL
174 switch (filename[i]) {
175 case ' ':
176 case '"':
177 case '\\':
178 monitor_printf(mon, "\\%c", filename[i]);
179 break;
180 case '\t':
181 monitor_printf(mon, "\\t");
182 break;
183 case '\r':
184 monitor_printf(mon, "\\r");
185 break;
186 case '\n':
187 monitor_printf(mon, "\\n");
188 break;
189 default:
190 monitor_printf(mon, "%c", filename[i]);
191 break;
192 }
fef30743
TS
193 }
194}
195
7fe48483
FB
196static int monitor_fprintf(FILE *stream, const char *fmt, ...)
197{
198 va_list ap;
199 va_start(ap, fmt);
376253ec 200 monitor_vprintf((Monitor *)stream, fmt, ap);
7fe48483
FB
201 va_end(ap);
202 return 0;
203}
204
9dc39cba
FB
205static int compare_cmd(const char *name, const char *list)
206{
207 const char *p, *pstart;
208 int len;
209 len = strlen(name);
210 p = list;
211 for(;;) {
212 pstart = p;
213 p = strchr(p, '|');
214 if (!p)
215 p = pstart + strlen(pstart);
216 if ((p - pstart) == len && !memcmp(pstart, name, len))
217 return 1;
218 if (*p == '\0')
219 break;
220 p++;
221 }
222 return 0;
223}
224
376253ec
AL
225static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
226 const char *prefix, const char *name)
9dc39cba 227{
376253ec 228 const mon_cmd_t *cmd;
9dc39cba
FB
229
230 for(cmd = cmds; cmd->name != NULL; cmd++) {
231 if (!name || !strcmp(name, cmd->name))
376253ec
AL
232 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
233 cmd->params, cmd->help);
9dc39cba
FB
234 }
235}
236
376253ec 237static void help_cmd(Monitor *mon, const char *name)
9dc39cba
FB
238{
239 if (name && !strcmp(name, "info")) {
376253ec 240 help_cmd_dump(mon, info_cmds, "info ", NULL);
9dc39cba 241 } else {
376253ec 242 help_cmd_dump(mon, mon_cmds, "", name);
f193c797 243 if (name && !strcmp(name, "log")) {
8662d656 244 const CPULogItem *item;
376253ec
AL
245 monitor_printf(mon, "Log items (comma separated):\n");
246 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
f193c797 247 for(item = cpu_log_items; item->mask != 0; item++) {
376253ec 248 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
f193c797
FB
249 }
250 }
9dc39cba
FB
251 }
252}
253
376253ec 254static void do_commit(Monitor *mon, const char *device)
9dc39cba 255{
751c6a17
GH
256 int all_devices;
257 DriveInfo *dinfo;
2dc7b602 258
7954c734 259 all_devices = !strcmp(device, "all");
751c6a17
GH
260 TAILQ_FOREACH(dinfo, &drives, next) {
261 if (!all_devices)
73006d2a 262 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
751c6a17
GH
263 continue;
264 bdrv_commit(dinfo->bdrv);
9dc39cba
FB
265 }
266}
267
376253ec 268static void do_info(Monitor *mon, const char *item)
9dc39cba 269{
376253ec
AL
270 const mon_cmd_t *cmd;
271 void (*handler)(Monitor *);
9dc39cba 272
9307c4c1 273 if (!item)
9dc39cba 274 goto help;
9dc39cba 275 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
5fafdf24 276 if (compare_cmd(item, cmd->name))
9dc39cba
FB
277 goto found;
278 }
279 help:
376253ec 280 help_cmd(mon, "info");
9dc39cba
FB
281 return;
282 found:
a5f1b965 283 handler = cmd->handler;
376253ec 284 handler(mon);
9dc39cba
FB
285}
286
376253ec 287static void do_info_version(Monitor *mon)
9bc9d1c7 288{
4a19f1ec 289 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
9bc9d1c7
FB
290}
291
376253ec 292static void do_info_name(Monitor *mon)
c35734b2
TS
293{
294 if (qemu_name)
376253ec 295 monitor_printf(mon, "%s\n", qemu_name);
c35734b2
TS
296}
297
bf4f74c0 298#if defined(TARGET_I386)
376253ec 299static void do_info_hpet(Monitor *mon)
16b29ae1 300{
376253ec
AL
301 monitor_printf(mon, "HPET is %s by QEMU\n",
302 (no_hpet) ? "disabled" : "enabled");
16b29ae1 303}
bf4f74c0 304#endif
16b29ae1 305
376253ec 306static void do_info_uuid(Monitor *mon)
a36e69dd 307{
376253ec
AL
308 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
309 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
310 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
311 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
312 qemu_uuid[14], qemu_uuid[15]);
a36e69dd
TS
313}
314
6a00d601 315/* get the current CPU defined by the user */
9596ebb7 316static int mon_set_cpu(int cpu_index)
6a00d601
FB
317{
318 CPUState *env;
319
320 for(env = first_cpu; env != NULL; env = env->next_cpu) {
321 if (env->cpu_index == cpu_index) {
731b0364 322 cur_mon->mon_cpu = env;
6a00d601
FB
323 return 0;
324 }
325 }
326 return -1;
327}
328
9596ebb7 329static CPUState *mon_get_cpu(void)
6a00d601 330{
731b0364 331 if (!cur_mon->mon_cpu) {
6a00d601
FB
332 mon_set_cpu(0);
333 }
4c0960c0 334 cpu_synchronize_state(cur_mon->mon_cpu);
731b0364 335 return cur_mon->mon_cpu;
6a00d601
FB
336}
337
376253ec 338static void do_info_registers(Monitor *mon)
9307c4c1 339{
6a00d601
FB
340 CPUState *env;
341 env = mon_get_cpu();
342 if (!env)
343 return;
9307c4c1 344#ifdef TARGET_I386
376253ec 345 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
d24b15a8 346 X86_DUMP_FPU);
9307c4c1 347#else
376253ec 348 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
7fe48483 349 0);
9307c4c1
FB
350#endif
351}
352
376253ec 353static void do_info_cpus(Monitor *mon)
6a00d601
FB
354{
355 CPUState *env;
356
357 /* just to set the default cpu if not already done */
358 mon_get_cpu();
359
360 for(env = first_cpu; env != NULL; env = env->next_cpu) {
4c0960c0 361 cpu_synchronize_state(env);
376253ec 362 monitor_printf(mon, "%c CPU #%d:",
731b0364 363 (env == mon->mon_cpu) ? '*' : ' ',
376253ec 364 env->cpu_index);
6a00d601 365#if defined(TARGET_I386)
376253ec
AL
366 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
367 env->eip + env->segs[R_CS].base);
e80e1cc4 368#elif defined(TARGET_PPC)
376253ec 369 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
ba3c64fb 370#elif defined(TARGET_SPARC)
376253ec
AL
371 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
372 env->pc, env->npc);
ead9360e 373#elif defined(TARGET_MIPS)
376253ec 374 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
ce5232c5 375#endif
ead9360e 376 if (env->halted)
376253ec
AL
377 monitor_printf(mon, " (halted)");
378 monitor_printf(mon, "\n");
6a00d601
FB
379 }
380}
381
376253ec 382static void do_cpu_set(Monitor *mon, int index)
6a00d601
FB
383{
384 if (mon_set_cpu(index) < 0)
376253ec 385 monitor_printf(mon, "Invalid CPU index\n");
6a00d601
FB
386}
387
376253ec 388static void do_info_jit(Monitor *mon)
e3db7226 389{
376253ec 390 dump_exec_info((FILE *)mon, monitor_fprintf);
e3db7226
FB
391}
392
376253ec 393static void do_info_history(Monitor *mon)
aa455485
FB
394{
395 int i;
7e2515e8 396 const char *str;
3b46e624 397
cde76ee1
AL
398 if (!mon->rs)
399 return;
7e2515e8
FB
400 i = 0;
401 for(;;) {
731b0364 402 str = readline_get_history(mon->rs, i);
7e2515e8
FB
403 if (!str)
404 break;
376253ec 405 monitor_printf(mon, "%d: '%s'\n", i, str);
8e3a9fd2 406 i++;
aa455485
FB
407 }
408}
409
76a66253
JM
410#if defined(TARGET_PPC)
411/* XXX: not implemented in other targets */
376253ec 412static void do_info_cpu_stats(Monitor *mon)
76a66253
JM
413{
414 CPUState *env;
415
416 env = mon_get_cpu();
376253ec 417 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
76a66253
JM
418}
419#endif
420
376253ec 421static void do_quit(Monitor *mon)
9dc39cba
FB
422{
423 exit(0);
424}
425
376253ec 426static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
9dc39cba
FB
427{
428 if (bdrv_is_inserted(bs)) {
429 if (!force) {
430 if (!bdrv_is_removable(bs)) {
376253ec 431 monitor_printf(mon, "device is not removable\n");
9dc39cba
FB
432 return -1;
433 }
434 if (bdrv_is_locked(bs)) {
376253ec 435 monitor_printf(mon, "device is locked\n");
9dc39cba
FB
436 return -1;
437 }
438 }
439 bdrv_close(bs);
440 }
441 return 0;
442}
443
376253ec 444static void do_eject(Monitor *mon, int force, const char *filename)
9dc39cba
FB
445{
446 BlockDriverState *bs;
9dc39cba 447
9307c4c1 448 bs = bdrv_find(filename);
9dc39cba 449 if (!bs) {
376253ec 450 monitor_printf(mon, "device not found\n");
9dc39cba
FB
451 return;
452 }
376253ec 453 eject_device(mon, bs, force);
9dc39cba
FB
454}
455
376253ec
AL
456static void do_change_block(Monitor *mon, const char *device,
457 const char *filename, const char *fmt)
9dc39cba
FB
458{
459 BlockDriverState *bs;
2ecea9b8 460 BlockDriver *drv = NULL;
9dc39cba 461
9307c4c1 462 bs = bdrv_find(device);
9dc39cba 463 if (!bs) {
376253ec 464 monitor_printf(mon, "device not found\n");
9dc39cba
FB
465 return;
466 }
2ecea9b8
AJ
467 if (fmt) {
468 drv = bdrv_find_format(fmt);
469 if (!drv) {
376253ec 470 monitor_printf(mon, "invalid format %s\n", fmt);
2ecea9b8
AJ
471 return;
472 }
473 }
376253ec 474 if (eject_device(mon, bs, 0) < 0)
9dc39cba 475 return;
2ecea9b8 476 bdrv_open2(bs, filename, 0, drv);
376253ec 477 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
bb5fc20f
AL
478}
479
376253ec
AL
480static void change_vnc_password_cb(Monitor *mon, const char *password,
481 void *opaque)
bb5fc20f
AL
482{
483 if (vnc_display_password(NULL, password) < 0)
376253ec 484 monitor_printf(mon, "could not set VNC server password\n");
bb5fc20f 485
731b0364 486 monitor_read_command(mon, 1);
9dc39cba
FB
487}
488
376253ec 489static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
e25a5822 490{
70848515 491 if (strcmp(target, "passwd") == 0 ||
28a76be8
AL
492 strcmp(target, "password") == 0) {
493 if (arg) {
bb5fc20f 494 char password[9];
28a76be8
AL
495 strncpy(password, arg, sizeof(password));
496 password[sizeof(password) - 1] = '\0';
376253ec 497 change_vnc_password_cb(mon, password, NULL);
bb5fc20f 498 } else {
376253ec 499 monitor_read_password(mon, change_vnc_password_cb, NULL);
bb5fc20f 500 }
70848515 501 } else {
28a76be8 502 if (vnc_display_open(NULL, target) < 0)
376253ec 503 monitor_printf(mon, "could not start VNC server on %s\n", target);
70848515 504 }
e25a5822
TS
505}
506
376253ec
AL
507static void do_change(Monitor *mon, const char *device, const char *target,
508 const char *arg)
e25a5822
TS
509{
510 if (strcmp(device, "vnc") == 0) {
28a76be8 511 do_change_vnc(mon, target, arg);
e25a5822 512 } else {
28a76be8 513 do_change_block(mon, device, target, arg);
e25a5822
TS
514 }
515}
516
376253ec 517static void do_screen_dump(Monitor *mon, const char *filename)
59a983b9 518{
95219897 519 vga_hw_screen_dump(filename);
59a983b9
FB
520}
521
376253ec 522static void do_logfile(Monitor *mon, const char *filename)
e735b91c
PB
523{
524 cpu_set_log_filename(filename);
525}
526
376253ec 527static void do_log(Monitor *mon, const char *items)
f193c797
FB
528{
529 int mask;
3b46e624 530
9307c4c1 531 if (!strcmp(items, "none")) {
f193c797
FB
532 mask = 0;
533 } else {
9307c4c1 534 mask = cpu_str_to_log_mask(items);
f193c797 535 if (!mask) {
376253ec 536 help_cmd(mon, "log");
f193c797
FB
537 return;
538 }
539 }
540 cpu_set_log(mask);
541}
542
1b530a6d
AJ
543static void do_singlestep(Monitor *mon, const char *option)
544{
545 if (!option || !strcmp(option, "on")) {
546 singlestep = 1;
547 } else if (!strcmp(option, "off")) {
548 singlestep = 0;
549 } else {
550 monitor_printf(mon, "unexpected option %s\n", option);
551 }
552}
553
376253ec 554static void do_stop(Monitor *mon)
8a7ddc38
FB
555{
556 vm_stop(EXCP_INTERRUPT);
557}
558
bb5fc20f 559static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
c0f4ce77 560
376253ec
AL
561struct bdrv_iterate_context {
562 Monitor *mon;
563 int err;
564};
565
566static void do_cont(Monitor *mon)
8a7ddc38 567{
376253ec 568 struct bdrv_iterate_context context = { mon, 0 };
c0f4ce77 569
376253ec 570 bdrv_iterate(encrypted_bdrv_it, &context);
c0f4ce77 571 /* only resume the vm if all keys are set and valid */
376253ec 572 if (!context.err)
c0f4ce77 573 vm_start();
8a7ddc38
FB
574}
575
bb5fc20f
AL
576static void bdrv_key_cb(void *opaque, int err)
577{
376253ec
AL
578 Monitor *mon = opaque;
579
bb5fc20f
AL
580 /* another key was set successfully, retry to continue */
581 if (!err)
376253ec 582 do_cont(mon);
bb5fc20f
AL
583}
584
585static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
586{
376253ec 587 struct bdrv_iterate_context *context = opaque;
bb5fc20f 588
376253ec
AL
589 if (!context->err && bdrv_key_required(bs)) {
590 context->err = -EBUSY;
591 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
592 context->mon);
bb5fc20f
AL
593 }
594}
595
59030a8c
AL
596static void do_gdbserver(Monitor *mon, const char *device)
597{
598 if (!device)
599 device = "tcp::" DEFAULT_GDBSTUB_PORT;
600 if (gdbserver_start(device) < 0) {
601 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
602 device);
603 } else if (strcmp(device, "none") == 0) {
36556b20 604 monitor_printf(mon, "Disabled gdbserver\n");
8a7ddc38 605 } else {
59030a8c
AL
606 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
607 device);
8a7ddc38
FB
608 }
609}
610
9dd986cc
RJ
611static void do_watchdog_action(Monitor *mon, const char *action)
612{
613 if (select_watchdog_action(action) == -1) {
614 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
615 }
616}
617
376253ec 618static void monitor_printc(Monitor *mon, int c)
9307c4c1 619{
376253ec 620 monitor_printf(mon, "'");
9307c4c1
FB
621 switch(c) {
622 case '\'':
376253ec 623 monitor_printf(mon, "\\'");
9307c4c1
FB
624 break;
625 case '\\':
376253ec 626 monitor_printf(mon, "\\\\");
9307c4c1
FB
627 break;
628 case '\n':
376253ec 629 monitor_printf(mon, "\\n");
9307c4c1
FB
630 break;
631 case '\r':
376253ec 632 monitor_printf(mon, "\\r");
9307c4c1
FB
633 break;
634 default:
635 if (c >= 32 && c <= 126) {
376253ec 636 monitor_printf(mon, "%c", c);
9307c4c1 637 } else {
376253ec 638 monitor_printf(mon, "\\x%02x", c);
9307c4c1
FB
639 }
640 break;
641 }
376253ec 642 monitor_printf(mon, "'");
9307c4c1
FB
643}
644
376253ec 645static void memory_dump(Monitor *mon, int count, int format, int wsize,
7743e588 646 target_phys_addr_t addr, int is_physical)
9307c4c1 647{
6a00d601 648 CPUState *env;
9307c4c1
FB
649 int nb_per_line, l, line_size, i, max_digits, len;
650 uint8_t buf[16];
651 uint64_t v;
652
653 if (format == 'i') {
654 int flags;
655 flags = 0;
6a00d601
FB
656 env = mon_get_cpu();
657 if (!env && !is_physical)
658 return;
9307c4c1 659#ifdef TARGET_I386
4c27ba27 660 if (wsize == 2) {
9307c4c1 661 flags = 1;
4c27ba27
FB
662 } else if (wsize == 4) {
663 flags = 0;
664 } else {
6a15fd12 665 /* as default we use the current CS size */
4c27ba27 666 flags = 0;
6a15fd12
FB
667 if (env) {
668#ifdef TARGET_X86_64
5fafdf24 669 if ((env->efer & MSR_EFER_LMA) &&
6a15fd12
FB
670 (env->segs[R_CS].flags & DESC_L_MASK))
671 flags = 2;
672 else
673#endif
674 if (!(env->segs[R_CS].flags & DESC_B_MASK))
675 flags = 1;
676 }
4c27ba27
FB
677 }
678#endif
376253ec 679 monitor_disas(mon, env, addr, count, is_physical, flags);
9307c4c1
FB
680 return;
681 }
682
683 len = wsize * count;
684 if (wsize == 1)
685 line_size = 8;
686 else
687 line_size = 16;
688 nb_per_line = line_size / wsize;
689 max_digits = 0;
690
691 switch(format) {
692 case 'o':
693 max_digits = (wsize * 8 + 2) / 3;
694 break;
695 default:
696 case 'x':
697 max_digits = (wsize * 8) / 4;
698 break;
699 case 'u':
700 case 'd':
701 max_digits = (wsize * 8 * 10 + 32) / 33;
702 break;
703 case 'c':
704 wsize = 1;
705 break;
706 }
707
708 while (len > 0) {
7743e588 709 if (is_physical)
376253ec 710 monitor_printf(mon, TARGET_FMT_plx ":", addr);
7743e588 711 else
376253ec 712 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
9307c4c1
FB
713 l = len;
714 if (l > line_size)
715 l = line_size;
716 if (is_physical) {
717 cpu_physical_memory_rw(addr, buf, l, 0);
718 } else {
6a00d601
FB
719 env = mon_get_cpu();
720 if (!env)
721 break;
c8f79b67 722 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
376253ec 723 monitor_printf(mon, " Cannot access memory\n");
c8f79b67
AL
724 break;
725 }
9307c4c1 726 }
5fafdf24 727 i = 0;
9307c4c1
FB
728 while (i < l) {
729 switch(wsize) {
730 default:
731 case 1:
732 v = ldub_raw(buf + i);
733 break;
734 case 2:
735 v = lduw_raw(buf + i);
736 break;
737 case 4:
92a31b1f 738 v = (uint32_t)ldl_raw(buf + i);
9307c4c1
FB
739 break;
740 case 8:
741 v = ldq_raw(buf + i);
742 break;
743 }
376253ec 744 monitor_printf(mon, " ");
9307c4c1
FB
745 switch(format) {
746 case 'o':
376253ec 747 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
9307c4c1
FB
748 break;
749 case 'x':
376253ec 750 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
9307c4c1
FB
751 break;
752 case 'u':
376253ec 753 monitor_printf(mon, "%*" PRIu64, max_digits, v);
9307c4c1
FB
754 break;
755 case 'd':
376253ec 756 monitor_printf(mon, "%*" PRId64, max_digits, v);
9307c4c1
FB
757 break;
758 case 'c':
376253ec 759 monitor_printc(mon, v);
9307c4c1
FB
760 break;
761 }
762 i += wsize;
763 }
376253ec 764 monitor_printf(mon, "\n");
9307c4c1
FB
765 addr += l;
766 len -= l;
767 }
768}
769
92a31b1f
FB
770#if TARGET_LONG_BITS == 64
771#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
772#else
773#define GET_TLONG(h, l) (l)
774#endif
775
376253ec 776static void do_memory_dump(Monitor *mon, int count, int format, int size,
92a31b1f 777 uint32_t addrh, uint32_t addrl)
9307c4c1 778{
92a31b1f 779 target_long addr = GET_TLONG(addrh, addrl);
376253ec 780 memory_dump(mon, count, format, size, addr, 0);
9307c4c1
FB
781}
782
7743e588
BS
783#if TARGET_PHYS_ADDR_BITS > 32
784#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
785#else
786#define GET_TPHYSADDR(h, l) (l)
787#endif
788
376253ec
AL
789static void do_physical_memory_dump(Monitor *mon, int count, int format,
790 int size, uint32_t addrh, uint32_t addrl)
92a31b1f 791
9307c4c1 792{
7743e588 793 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
376253ec 794 memory_dump(mon, count, format, size, addr, 1);
9307c4c1
FB
795}
796
376253ec
AL
797static void do_print(Monitor *mon, int count, int format, int size,
798 unsigned int valh, unsigned int vall)
9307c4c1 799{
7743e588
BS
800 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
801#if TARGET_PHYS_ADDR_BITS == 32
9307c4c1
FB
802 switch(format) {
803 case 'o':
376253ec 804 monitor_printf(mon, "%#o", val);
9307c4c1
FB
805 break;
806 case 'x':
376253ec 807 monitor_printf(mon, "%#x", val);
9307c4c1
FB
808 break;
809 case 'u':
376253ec 810 monitor_printf(mon, "%u", val);
9307c4c1
FB
811 break;
812 default:
813 case 'd':
376253ec 814 monitor_printf(mon, "%d", val);
9307c4c1
FB
815 break;
816 case 'c':
376253ec 817 monitor_printc(mon, val);
9307c4c1
FB
818 break;
819 }
92a31b1f
FB
820#else
821 switch(format) {
822 case 'o':
376253ec 823 monitor_printf(mon, "%#" PRIo64, val);
92a31b1f
FB
824 break;
825 case 'x':
376253ec 826 monitor_printf(mon, "%#" PRIx64, val);
92a31b1f
FB
827 break;
828 case 'u':
376253ec 829 monitor_printf(mon, "%" PRIu64, val);
92a31b1f
FB
830 break;
831 default:
832 case 'd':
376253ec 833 monitor_printf(mon, "%" PRId64, val);
92a31b1f
FB
834 break;
835 case 'c':
376253ec 836 monitor_printc(mon, val);
92a31b1f
FB
837 break;
838 }
839#endif
376253ec 840 monitor_printf(mon, "\n");
9307c4c1
FB
841}
842
376253ec 843static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
b371dc59
FB
844 uint32_t size, const char *filename)
845{
846 FILE *f;
847 target_long addr = GET_TLONG(valh, vall);
848 uint32_t l;
849 CPUState *env;
850 uint8_t buf[1024];
851
852 env = mon_get_cpu();
853 if (!env)
854 return;
855
856 f = fopen(filename, "wb");
857 if (!f) {
376253ec 858 monitor_printf(mon, "could not open '%s'\n", filename);
b371dc59
FB
859 return;
860 }
861 while (size != 0) {
862 l = sizeof(buf);
863 if (l > size)
864 l = size;
865 cpu_memory_rw_debug(env, addr, buf, l, 0);
866 fwrite(buf, 1, l, f);
867 addr += l;
868 size -= l;
869 }
870 fclose(f);
871}
872
376253ec
AL
873static void do_physical_memory_save(Monitor *mon, unsigned int valh,
874 unsigned int vall, uint32_t size,
875 const char *filename)
a8bdf7a6
AJ
876{
877 FILE *f;
878 uint32_t l;
879 uint8_t buf[1024];
339dea27 880 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
a8bdf7a6
AJ
881
882 f = fopen(filename, "wb");
883 if (!f) {
376253ec 884 monitor_printf(mon, "could not open '%s'\n", filename);
a8bdf7a6
AJ
885 return;
886 }
887 while (size != 0) {
888 l = sizeof(buf);
889 if (l > size)
890 l = size;
891 cpu_physical_memory_rw(addr, buf, l, 0);
892 fwrite(buf, 1, l, f);
893 fflush(f);
894 addr += l;
895 size -= l;
896 }
897 fclose(f);
898}
899
376253ec 900static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
e4cf1adc
FB
901{
902 uint32_t addr;
903 uint8_t buf[1];
904 uint16_t sum;
905
906 sum = 0;
907 for(addr = start; addr < (start + size); addr++) {
908 cpu_physical_memory_rw(addr, buf, 1, 0);
909 /* BSD sum algorithm ('sum' Unix command) */
910 sum = (sum >> 1) | (sum << 15);
911 sum += buf[0];
912 }
376253ec 913 monitor_printf(mon, "%05d\n", sum);
e4cf1adc
FB
914}
915
a3a91a35
FB
916typedef struct {
917 int keycode;
918 const char *name;
919} KeyDef;
920
921static const KeyDef key_defs[] = {
922 { 0x2a, "shift" },
923 { 0x36, "shift_r" },
3b46e624 924
a3a91a35
FB
925 { 0x38, "alt" },
926 { 0xb8, "alt_r" },
2ba27c7f
TS
927 { 0x64, "altgr" },
928 { 0xe4, "altgr_r" },
a3a91a35
FB
929 { 0x1d, "ctrl" },
930 { 0x9d, "ctrl_r" },
931
932 { 0xdd, "menu" },
933
934 { 0x01, "esc" },
935
936 { 0x02, "1" },
937 { 0x03, "2" },
938 { 0x04, "3" },
939 { 0x05, "4" },
940 { 0x06, "5" },
941 { 0x07, "6" },
942 { 0x08, "7" },
943 { 0x09, "8" },
944 { 0x0a, "9" },
945 { 0x0b, "0" },
64866c3d
FB
946 { 0x0c, "minus" },
947 { 0x0d, "equal" },
a3a91a35
FB
948 { 0x0e, "backspace" },
949
950 { 0x0f, "tab" },
951 { 0x10, "q" },
952 { 0x11, "w" },
953 { 0x12, "e" },
954 { 0x13, "r" },
955 { 0x14, "t" },
956 { 0x15, "y" },
957 { 0x16, "u" },
958 { 0x17, "i" },
959 { 0x18, "o" },
960 { 0x19, "p" },
961
962 { 0x1c, "ret" },
963
964 { 0x1e, "a" },
965 { 0x1f, "s" },
966 { 0x20, "d" },
967 { 0x21, "f" },
968 { 0x22, "g" },
969 { 0x23, "h" },
970 { 0x24, "j" },
971 { 0x25, "k" },
972 { 0x26, "l" },
973
974 { 0x2c, "z" },
975 { 0x2d, "x" },
976 { 0x2e, "c" },
977 { 0x2f, "v" },
978 { 0x30, "b" },
979 { 0x31, "n" },
980 { 0x32, "m" },
9155fc45
AJ
981 { 0x33, "comma" },
982 { 0x34, "dot" },
983 { 0x35, "slash" },
3b46e624 984
4d3b6f6e
AZ
985 { 0x37, "asterisk" },
986
a3a91a35 987 { 0x39, "spc" },
00ffa62a 988 { 0x3a, "caps_lock" },
a3a91a35
FB
989 { 0x3b, "f1" },
990 { 0x3c, "f2" },
991 { 0x3d, "f3" },
992 { 0x3e, "f4" },
993 { 0x3f, "f5" },
994 { 0x40, "f6" },
995 { 0x41, "f7" },
996 { 0x42, "f8" },
997 { 0x43, "f9" },
998 { 0x44, "f10" },
00ffa62a 999 { 0x45, "num_lock" },
a3a91a35
FB
1000 { 0x46, "scroll_lock" },
1001
64866c3d
FB
1002 { 0xb5, "kp_divide" },
1003 { 0x37, "kp_multiply" },
0cfec834 1004 { 0x4a, "kp_subtract" },
64866c3d
FB
1005 { 0x4e, "kp_add" },
1006 { 0x9c, "kp_enter" },
1007 { 0x53, "kp_decimal" },
f2289cb6 1008 { 0x54, "sysrq" },
64866c3d
FB
1009
1010 { 0x52, "kp_0" },
1011 { 0x4f, "kp_1" },
1012 { 0x50, "kp_2" },
1013 { 0x51, "kp_3" },
1014 { 0x4b, "kp_4" },
1015 { 0x4c, "kp_5" },
1016 { 0x4d, "kp_6" },
1017 { 0x47, "kp_7" },
1018 { 0x48, "kp_8" },
1019 { 0x49, "kp_9" },
3b46e624 1020
a3a91a35
FB
1021 { 0x56, "<" },
1022
1023 { 0x57, "f11" },
1024 { 0x58, "f12" },
1025
1026 { 0xb7, "print" },
1027
1028 { 0xc7, "home" },
1029 { 0xc9, "pgup" },
1030 { 0xd1, "pgdn" },
1031 { 0xcf, "end" },
1032
1033 { 0xcb, "left" },
1034 { 0xc8, "up" },
1035 { 0xd0, "down" },
1036 { 0xcd, "right" },
1037
1038 { 0xd2, "insert" },
1039 { 0xd3, "delete" },
c0b5b109
BS
1040#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1041 { 0xf0, "stop" },
1042 { 0xf1, "again" },
1043 { 0xf2, "props" },
1044 { 0xf3, "undo" },
1045 { 0xf4, "front" },
1046 { 0xf5, "copy" },
1047 { 0xf6, "open" },
1048 { 0xf7, "paste" },
1049 { 0xf8, "find" },
1050 { 0xf9, "cut" },
1051 { 0xfa, "lf" },
1052 { 0xfb, "help" },
1053 { 0xfc, "meta_l" },
1054 { 0xfd, "meta_r" },
1055 { 0xfe, "compose" },
1056#endif
a3a91a35
FB
1057 { 0, NULL },
1058};
1059
1060static int get_keycode(const char *key)
1061{
1062 const KeyDef *p;
64866c3d
FB
1063 char *endp;
1064 int ret;
a3a91a35
FB
1065
1066 for(p = key_defs; p->name != NULL; p++) {
1067 if (!strcmp(key, p->name))
1068 return p->keycode;
1069 }
64866c3d
FB
1070 if (strstart(key, "0x", NULL)) {
1071 ret = strtoul(key, &endp, 0);
1072 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1073 return ret;
1074 }
a3a91a35
FB
1075 return -1;
1076}
1077
c8256f9d
AZ
1078#define MAX_KEYCODES 16
1079static uint8_t keycodes[MAX_KEYCODES];
1080static int nb_pending_keycodes;
1081static QEMUTimer *key_timer;
1082
1083static void release_keys(void *opaque)
1084{
1085 int keycode;
1086
1087 while (nb_pending_keycodes > 0) {
1088 nb_pending_keycodes--;
1089 keycode = keycodes[nb_pending_keycodes];
1090 if (keycode & 0x80)
1091 kbd_put_keycode(0xe0);
1092 kbd_put_keycode(keycode | 0x80);
1093 }
1094}
1095
376253ec
AL
1096static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1097 int hold_time)
a3a91a35 1098{
3401c0d9
AZ
1099 char keyname_buf[16];
1100 char *separator;
1101 int keyname_len, keycode, i;
1102
c8256f9d
AZ
1103 if (nb_pending_keycodes > 0) {
1104 qemu_del_timer(key_timer);
1105 release_keys(NULL);
1106 }
1107 if (!has_hold_time)
1108 hold_time = 100;
1109 i = 0;
3401c0d9
AZ
1110 while (1) {
1111 separator = strchr(string, '-');
1112 keyname_len = separator ? separator - string : strlen(string);
1113 if (keyname_len > 0) {
1114 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1115 if (keyname_len > sizeof(keyname_buf) - 1) {
376253ec 1116 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
3401c0d9 1117 return;
a3a91a35 1118 }
c8256f9d 1119 if (i == MAX_KEYCODES) {
376253ec 1120 monitor_printf(mon, "too many keys\n");
3401c0d9
AZ
1121 return;
1122 }
1123 keyname_buf[keyname_len] = 0;
1124 keycode = get_keycode(keyname_buf);
1125 if (keycode < 0) {
376253ec 1126 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
3401c0d9
AZ
1127 return;
1128 }
c8256f9d 1129 keycodes[i++] = keycode;
a3a91a35 1130 }
3401c0d9 1131 if (!separator)
a3a91a35 1132 break;
3401c0d9 1133 string = separator + 1;
a3a91a35 1134 }
c8256f9d 1135 nb_pending_keycodes = i;
a3a91a35 1136 /* key down events */
c8256f9d 1137 for (i = 0; i < nb_pending_keycodes; i++) {
a3a91a35
FB
1138 keycode = keycodes[i];
1139 if (keycode & 0x80)
1140 kbd_put_keycode(0xe0);
1141 kbd_put_keycode(keycode & 0x7f);
1142 }
c8256f9d 1143 /* delayed key up events */
f227f17d
AZ
1144 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1145 muldiv64(ticks_per_sec, hold_time, 1000));
a3a91a35
FB
1146}
1147
13224a87
FB
1148static int mouse_button_state;
1149
376253ec 1150static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
13224a87
FB
1151 const char *dz_str)
1152{
1153 int dx, dy, dz;
1154 dx = strtol(dx_str, NULL, 0);
1155 dy = strtol(dy_str, NULL, 0);
1156 dz = 0;
5fafdf24 1157 if (dz_str)
13224a87
FB
1158 dz = strtol(dz_str, NULL, 0);
1159 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1160}
1161
376253ec 1162static void do_mouse_button(Monitor *mon, int button_state)
13224a87
FB
1163{
1164 mouse_button_state = button_state;
1165 kbd_mouse_event(0, 0, 0, mouse_button_state);
1166}
1167
376253ec
AL
1168static void do_ioport_read(Monitor *mon, int count, int format, int size,
1169 int addr, int has_index, int index)
3440557b
FB
1170{
1171 uint32_t val;
1172 int suffix;
1173
1174 if (has_index) {
d56dd6cf 1175 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
3440557b
FB
1176 addr++;
1177 }
1178 addr &= 0xffff;
1179
1180 switch(size) {
1181 default:
1182 case 1:
1183 val = cpu_inb(NULL, addr);
1184 suffix = 'b';
1185 break;
1186 case 2:
1187 val = cpu_inw(NULL, addr);
1188 suffix = 'w';
1189 break;
1190 case 4:
1191 val = cpu_inl(NULL, addr);
1192 suffix = 'l';
1193 break;
1194 }
376253ec
AL
1195 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1196 suffix, addr, size * 2, val);
3440557b 1197}
a3a91a35 1198
f114784f
JK
1199static void do_ioport_write(Monitor *mon, int count, int format, int size,
1200 int addr, int val)
1201{
1202 addr &= IOPORTS_MASK;
1203
1204 switch (size) {
1205 default:
1206 case 1:
1207 cpu_outb(NULL, addr, val);
1208 break;
1209 case 2:
1210 cpu_outw(NULL, addr, val);
1211 break;
1212 case 4:
1213 cpu_outl(NULL, addr, val);
1214 break;
1215 }
1216}
1217
376253ec 1218static void do_boot_set(Monitor *mon, const char *bootdevice)
0ecdffbb
AJ
1219{
1220 int res;
1221
76e30d0f
JK
1222 res = qemu_boot_set(bootdevice);
1223 if (res == 0) {
1224 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1225 } else if (res > 0) {
1226 monitor_printf(mon, "setting boot device list failed\n");
0ecdffbb 1227 } else {
376253ec
AL
1228 monitor_printf(mon, "no function defined to set boot device list for "
1229 "this architecture\n");
0ecdffbb
AJ
1230 }
1231}
1232
376253ec 1233static void do_system_reset(Monitor *mon)
e4f9082b
FB
1234{
1235 qemu_system_reset_request();
1236}
1237
376253ec 1238static void do_system_powerdown(Monitor *mon)
3475187d
FB
1239{
1240 qemu_system_powerdown_request();
1241}
1242
b86bda5b 1243#if defined(TARGET_I386)
376253ec 1244static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
b86bda5b 1245{
376253ec
AL
1246 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1247 addr,
1248 pte & mask,
1249 pte & PG_GLOBAL_MASK ? 'G' : '-',
1250 pte & PG_PSE_MASK ? 'P' : '-',
1251 pte & PG_DIRTY_MASK ? 'D' : '-',
1252 pte & PG_ACCESSED_MASK ? 'A' : '-',
1253 pte & PG_PCD_MASK ? 'C' : '-',
1254 pte & PG_PWT_MASK ? 'T' : '-',
1255 pte & PG_USER_MASK ? 'U' : '-',
1256 pte & PG_RW_MASK ? 'W' : '-');
b86bda5b
FB
1257}
1258
376253ec 1259static void tlb_info(Monitor *mon)
b86bda5b 1260{
6a00d601 1261 CPUState *env;
b86bda5b
FB
1262 int l1, l2;
1263 uint32_t pgd, pde, pte;
1264
6a00d601
FB
1265 env = mon_get_cpu();
1266 if (!env)
1267 return;
1268
b86bda5b 1269 if (!(env->cr[0] & CR0_PG_MASK)) {
376253ec 1270 monitor_printf(mon, "PG disabled\n");
b86bda5b
FB
1271 return;
1272 }
1273 pgd = env->cr[3] & ~0xfff;
1274 for(l1 = 0; l1 < 1024; l1++) {
1275 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1276 pde = le32_to_cpu(pde);
1277 if (pde & PG_PRESENT_MASK) {
1278 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
376253ec 1279 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
b86bda5b
FB
1280 } else {
1281 for(l2 = 0; l2 < 1024; l2++) {
5fafdf24 1282 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
b86bda5b
FB
1283 (uint8_t *)&pte, 4);
1284 pte = le32_to_cpu(pte);
1285 if (pte & PG_PRESENT_MASK) {
376253ec 1286 print_pte(mon, (l1 << 22) + (l2 << 12),
5fafdf24 1287 pte & ~PG_PSE_MASK,
b86bda5b
FB
1288 ~0xfff);
1289 }
1290 }
1291 }
1292 }
1293 }
1294}
1295
376253ec 1296static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
b86bda5b
FB
1297 uint32_t end, int prot)
1298{
9746b15b
FB
1299 int prot1;
1300 prot1 = *plast_prot;
1301 if (prot != prot1) {
b86bda5b 1302 if (*pstart != -1) {
376253ec
AL
1303 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1304 *pstart, end, end - *pstart,
1305 prot1 & PG_USER_MASK ? 'u' : '-',
1306 'r',
1307 prot1 & PG_RW_MASK ? 'w' : '-');
b86bda5b
FB
1308 }
1309 if (prot != 0)
1310 *pstart = end;
1311 else
1312 *pstart = -1;
1313 *plast_prot = prot;
1314 }
1315}
1316
376253ec 1317static void mem_info(Monitor *mon)
b86bda5b 1318{
6a00d601 1319 CPUState *env;
b86bda5b
FB
1320 int l1, l2, prot, last_prot;
1321 uint32_t pgd, pde, pte, start, end;
1322
6a00d601
FB
1323 env = mon_get_cpu();
1324 if (!env)
1325 return;
1326
b86bda5b 1327 if (!(env->cr[0] & CR0_PG_MASK)) {
376253ec 1328 monitor_printf(mon, "PG disabled\n");
b86bda5b
FB
1329 return;
1330 }
1331 pgd = env->cr[3] & ~0xfff;
1332 last_prot = 0;
1333 start = -1;
1334 for(l1 = 0; l1 < 1024; l1++) {
1335 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1336 pde = le32_to_cpu(pde);
1337 end = l1 << 22;
1338 if (pde & PG_PRESENT_MASK) {
1339 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1340 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
376253ec 1341 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
1342 } else {
1343 for(l2 = 0; l2 < 1024; l2++) {
5fafdf24 1344 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
b86bda5b
FB
1345 (uint8_t *)&pte, 4);
1346 pte = le32_to_cpu(pte);
1347 end = (l1 << 22) + (l2 << 12);
1348 if (pte & PG_PRESENT_MASK) {
1349 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1350 } else {
1351 prot = 0;
1352 }
376253ec 1353 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
1354 }
1355 }
1356 } else {
1357 prot = 0;
376253ec 1358 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
1359 }
1360 }
1361}
1362#endif
1363
7c664e2f
AJ
1364#if defined(TARGET_SH4)
1365
376253ec 1366static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
7c664e2f 1367{
376253ec
AL
1368 monitor_printf(mon, " tlb%i:\t"
1369 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1370 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1371 "dirty=%hhu writethrough=%hhu\n",
1372 idx,
1373 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1374 tlb->v, tlb->sh, tlb->c, tlb->pr,
1375 tlb->d, tlb->wt);
7c664e2f
AJ
1376}
1377
376253ec 1378static void tlb_info(Monitor *mon)
7c664e2f
AJ
1379{
1380 CPUState *env = mon_get_cpu();
1381 int i;
1382
376253ec 1383 monitor_printf (mon, "ITLB:\n");
7c664e2f 1384 for (i = 0 ; i < ITLB_SIZE ; i++)
376253ec
AL
1385 print_tlb (mon, i, &env->itlb[i]);
1386 monitor_printf (mon, "UTLB:\n");
7c664e2f 1387 for (i = 0 ; i < UTLB_SIZE ; i++)
376253ec 1388 print_tlb (mon, i, &env->utlb[i]);
7c664e2f
AJ
1389}
1390
1391#endif
1392
376253ec 1393static void do_info_kvm(Monitor *mon)
7ba1e619
AL
1394{
1395#ifdef CONFIG_KVM
376253ec 1396 monitor_printf(mon, "kvm support: ");
7ba1e619 1397 if (kvm_enabled())
376253ec 1398 monitor_printf(mon, "enabled\n");
7ba1e619 1399 else
376253ec 1400 monitor_printf(mon, "disabled\n");
7ba1e619 1401#else
376253ec 1402 monitor_printf(mon, "kvm support: not compiled\n");
7ba1e619
AL
1403#endif
1404}
1405
030ea37b
AL
1406static void do_info_numa(Monitor *mon)
1407{
b28b6230 1408 int i;
030ea37b
AL
1409 CPUState *env;
1410
1411 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1412 for (i = 0; i < nb_numa_nodes; i++) {
1413 monitor_printf(mon, "node %d cpus:", i);
1414 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1415 if (env->numa_node == i) {
1416 monitor_printf(mon, " %d", env->cpu_index);
1417 }
1418 }
1419 monitor_printf(mon, "\n");
1420 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1421 node_mem[i] >> 20);
1422 }
1423}
1424
5f1ce948
FB
1425#ifdef CONFIG_PROFILER
1426
376253ec 1427static void do_info_profile(Monitor *mon)
5f1ce948
FB
1428{
1429 int64_t total;
1430 total = qemu_time;
1431 if (total == 0)
1432 total = 1;
376253ec
AL
1433 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1434 dev_time, dev_time / (double)ticks_per_sec);
1435 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1436 qemu_time, qemu_time / (double)ticks_per_sec);
5f1ce948 1437 qemu_time = 0;
5f1ce948 1438 dev_time = 0;
5f1ce948
FB
1439}
1440#else
376253ec 1441static void do_info_profile(Monitor *mon)
5f1ce948 1442{
376253ec 1443 monitor_printf(mon, "Internal profiler not compiled\n");
5f1ce948
FB
1444}
1445#endif
1446
ec36b695
FB
1447/* Capture support */
1448static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1449
376253ec 1450static void do_info_capture(Monitor *mon)
ec36b695
FB
1451{
1452 int i;
1453 CaptureState *s;
1454
1455 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
376253ec 1456 monitor_printf(mon, "[%d]: ", i);
ec36b695
FB
1457 s->ops.info (s->opaque);
1458 }
1459}
1460
2313086a 1461#ifdef HAS_AUDIO
376253ec 1462static void do_stop_capture(Monitor *mon, int n)
ec36b695
FB
1463{
1464 int i;
1465 CaptureState *s;
1466
1467 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1468 if (i == n) {
1469 s->ops.destroy (s->opaque);
1470 LIST_REMOVE (s, entries);
1471 qemu_free (s);
1472 return;
1473 }
1474 }
1475}
1476
376253ec
AL
1477static void do_wav_capture(Monitor *mon, const char *path,
1478 int has_freq, int freq,
1479 int has_bits, int bits,
1480 int has_channels, int nchannels)
ec36b695
FB
1481{
1482 CaptureState *s;
1483
1484 s = qemu_mallocz (sizeof (*s));
ec36b695
FB
1485
1486 freq = has_freq ? freq : 44100;
1487 bits = has_bits ? bits : 16;
1488 nchannels = has_channels ? nchannels : 2;
1489
1490 if (wav_start_capture (s, path, freq, bits, nchannels)) {
376253ec 1491 monitor_printf(mon, "Faied to add wave capture\n");
ec36b695
FB
1492 qemu_free (s);
1493 }
1494 LIST_INSERT_HEAD (&capture_head, s, entries);
1495}
1496#endif
1497
dc1c0b74 1498#if defined(TARGET_I386)
376253ec 1499static void do_inject_nmi(Monitor *mon, int cpu_index)
dc1c0b74
AJ
1500{
1501 CPUState *env;
1502
1503 for (env = first_cpu; env != NULL; env = env->next_cpu)
1504 if (env->cpu_index == cpu_index) {
1505 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1506 break;
1507 }
1508}
1509#endif
1510
376253ec 1511static void do_info_status(Monitor *mon)
6f9c5ee7 1512{
1b530a6d
AJ
1513 if (vm_running) {
1514 if (singlestep) {
1515 monitor_printf(mon, "VM status: running (single step mode)\n");
1516 } else {
1517 monitor_printf(mon, "VM status: running\n");
1518 }
1519 } else
376253ec 1520 monitor_printf(mon, "VM status: paused\n");
6f9c5ee7
AJ
1521}
1522
1523
376253ec 1524static void do_balloon(Monitor *mon, int value)
df751fa8
AL
1525{
1526 ram_addr_t target = value;
1527 qemu_balloon(target << 20);
1528}
1529
376253ec 1530static void do_info_balloon(Monitor *mon)
df751fa8
AL
1531{
1532 ram_addr_t actual;
1533
1534 actual = qemu_balloon_status();
bd322087 1535 if (kvm_enabled() && !kvm_has_sync_mmu())
376253ec
AL
1536 monitor_printf(mon, "Using KVM without synchronous MMU, "
1537 "ballooning disabled\n");
bd322087 1538 else if (actual == 0)
376253ec 1539 monitor_printf(mon, "Ballooning not activated in VM\n");
df751fa8 1540 else
376253ec 1541 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
df751fa8
AL
1542}
1543
15dfcd45 1544static qemu_acl *find_acl(Monitor *mon, const char *name)
76655d6d 1545{
15dfcd45 1546 qemu_acl *acl = qemu_acl_find(name);
76655d6d 1547
76655d6d 1548 if (!acl) {
15dfcd45 1549 monitor_printf(mon, "acl: unknown list '%s'\n", name);
76655d6d 1550 }
15dfcd45
JK
1551 return acl;
1552}
1553
1554static void do_acl_show(Monitor *mon, const char *aclname)
1555{
1556 qemu_acl *acl = find_acl(mon, aclname);
1557 qemu_acl_entry *entry;
1558 int i = 0;
76655d6d 1559
15dfcd45 1560 if (acl) {
28a76be8 1561 monitor_printf(mon, "policy: %s\n",
76655d6d 1562 acl->defaultDeny ? "deny" : "allow");
28a76be8
AL
1563 TAILQ_FOREACH(entry, &acl->entries, next) {
1564 i++;
1565 monitor_printf(mon, "%d: %s %s\n", i,
15dfcd45 1566 entry->deny ? "deny" : "allow", entry->match);
28a76be8 1567 }
15dfcd45
JK
1568 }
1569}
1570
1571static void do_acl_reset(Monitor *mon, const char *aclname)
1572{
1573 qemu_acl *acl = find_acl(mon, aclname);
1574
1575 if (acl) {
28a76be8
AL
1576 qemu_acl_reset(acl);
1577 monitor_printf(mon, "acl: removed all rules\n");
15dfcd45
JK
1578 }
1579}
1580
1581static void do_acl_policy(Monitor *mon, const char *aclname,
1582 const char *policy)
1583{
1584 qemu_acl *acl = find_acl(mon, aclname);
28a76be8 1585
15dfcd45
JK
1586 if (acl) {
1587 if (strcmp(policy, "allow") == 0) {
28a76be8
AL
1588 acl->defaultDeny = 0;
1589 monitor_printf(mon, "acl: policy set to 'allow'\n");
15dfcd45 1590 } else if (strcmp(policy, "deny") == 0) {
28a76be8
AL
1591 acl->defaultDeny = 1;
1592 monitor_printf(mon, "acl: policy set to 'deny'\n");
1593 } else {
15dfcd45
JK
1594 monitor_printf(mon, "acl: unknown policy '%s', "
1595 "expected 'deny' or 'allow'\n", policy);
28a76be8 1596 }
15dfcd45
JK
1597 }
1598}
28a76be8 1599
15dfcd45
JK
1600static void do_acl_add(Monitor *mon, const char *aclname,
1601 const char *match, const char *policy,
1602 int has_index, int index)
1603{
1604 qemu_acl *acl = find_acl(mon, aclname);
1605 int deny, ret;
1606
1607 if (acl) {
1608 if (strcmp(policy, "allow") == 0) {
1609 deny = 0;
1610 } else if (strcmp(policy, "deny") == 0) {
1611 deny = 1;
1612 } else {
1613 monitor_printf(mon, "acl: unknown policy '%s', "
1614 "expected 'deny' or 'allow'\n", policy);
28a76be8
AL
1615 return;
1616 }
28a76be8
AL
1617 if (has_index)
1618 ret = qemu_acl_insert(acl, deny, match, index);
1619 else
1620 ret = qemu_acl_append(acl, deny, match);
1621 if (ret < 0)
1622 monitor_printf(mon, "acl: unable to add acl entry\n");
1623 else
1624 monitor_printf(mon, "acl: added rule at position %d\n", ret);
15dfcd45
JK
1625 }
1626}
28a76be8 1627
15dfcd45
JK
1628static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1629{
1630 qemu_acl *acl = find_acl(mon, aclname);
1631 int ret;
28a76be8 1632
15dfcd45 1633 if (acl) {
28a76be8
AL
1634 ret = qemu_acl_remove(acl, match);
1635 if (ret < 0)
1636 monitor_printf(mon, "acl: no matching acl entry\n");
1637 else
1638 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
76655d6d
AL
1639 }
1640}
1641
79c4f6b0
HY
1642#if defined(TARGET_I386)
1643static void do_inject_mce(Monitor *mon,
1644 int cpu_index, int bank,
1645 unsigned status_hi, unsigned status_lo,
1646 unsigned mcg_status_hi, unsigned mcg_status_lo,
1647 unsigned addr_hi, unsigned addr_lo,
1648 unsigned misc_hi, unsigned misc_lo)
1649{
1650 CPUState *cenv;
1651 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1652 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1653 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1654 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1655
1656 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1657 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1658 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1659 break;
1660 }
1661}
1662#endif
1663
f07918fd
MM
1664static void do_getfd(Monitor *mon, const char *fdname)
1665{
1666 mon_fd_t *monfd;
1667 int fd;
1668
1669 fd = qemu_chr_get_msgfd(mon->chr);
1670 if (fd == -1) {
1671 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1672 return;
1673 }
1674
1675 if (qemu_isdigit(fdname[0])) {
1676 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1677 return;
1678 }
1679
1680 fd = dup(fd);
1681 if (fd == -1) {
1682 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1683 strerror(errno));
1684 return;
1685 }
1686
1687 LIST_FOREACH(monfd, &mon->fds, next) {
1688 if (strcmp(monfd->name, fdname) != 0) {
1689 continue;
1690 }
1691
1692 close(monfd->fd);
1693 monfd->fd = fd;
1694 return;
1695 }
1696
1697 monfd = qemu_mallocz(sizeof(mon_fd_t));
1698 monfd->name = qemu_strdup(fdname);
1699 monfd->fd = fd;
1700
1701 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1702}
1703
1704static void do_closefd(Monitor *mon, const char *fdname)
1705{
1706 mon_fd_t *monfd;
1707
1708 LIST_FOREACH(monfd, &mon->fds, next) {
1709 if (strcmp(monfd->name, fdname) != 0) {
1710 continue;
1711 }
1712
1713 LIST_REMOVE(monfd, next);
1714 close(monfd->fd);
1715 qemu_free(monfd->name);
1716 qemu_free(monfd);
1717 return;
1718 }
1719
1720 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1721 fdname);
1722}
1723
c8d41b2c
JQ
1724static void do_loadvm(Monitor *mon, const char *name)
1725{
1726 int saved_vm_running = vm_running;
1727
1728 vm_stop(0);
1729
05f2401e 1730 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
c8d41b2c
JQ
1731 vm_start();
1732}
1733
7768e04c
MM
1734int monitor_get_fd(Monitor *mon, const char *fdname)
1735{
1736 mon_fd_t *monfd;
1737
1738 LIST_FOREACH(monfd, &mon->fds, next) {
1739 int fd;
1740
1741 if (strcmp(monfd->name, fdname) != 0) {
1742 continue;
1743 }
1744
1745 fd = monfd->fd;
1746
1747 /* caller takes ownership of fd */
1748 LIST_REMOVE(monfd, next);
1749 qemu_free(monfd->name);
1750 qemu_free(monfd);
1751
1752 return fd;
1753 }
1754
1755 return -1;
1756}
1757
376253ec 1758static const mon_cmd_t mon_cmds[] = {
2313086a 1759#include "qemu-monitor.h"
5fafdf24 1760 { NULL, NULL, },
9dc39cba
FB
1761};
1762
2313086a 1763/* Please update qemu-monitor.hx when adding or changing commands */
376253ec 1764static const mon_cmd_t info_cmds[] = {
9bc9d1c7 1765 { "version", "", do_info_version,
d2c639d6 1766 "", "show the version of QEMU" },
9307c4c1 1767 { "network", "", do_info_network,
9dc39cba 1768 "", "show the network state" },
5ccfae10
AL
1769 { "chardev", "", qemu_chr_info,
1770 "", "show the character devices" },
376253ec 1771 { "block", "", bdrv_info,
9dc39cba 1772 "", "show the block devices" },
376253ec 1773 { "blockstats", "", bdrv_info_stats,
a36e69dd 1774 "", "show block device statistics" },
9307c4c1
FB
1775 { "registers", "", do_info_registers,
1776 "", "show the cpu registers" },
6a00d601
FB
1777 { "cpus", "", do_info_cpus,
1778 "", "show infos for each CPU" },
aa455485
FB
1779 { "history", "", do_info_history,
1780 "", "show the command line history", },
4a0fb71e
FB
1781 { "irq", "", irq_info,
1782 "", "show the interrupts statistics (if available)", },
4c27ba27
FB
1783 { "pic", "", pic_info,
1784 "", "show i8259 (PIC) state", },
86e0c048
FB
1785 { "pci", "", pci_info,
1786 "", "show PCI info", },
7c664e2f 1787#if defined(TARGET_I386) || defined(TARGET_SH4)
b86bda5b
FB
1788 { "tlb", "", tlb_info,
1789 "", "show virtual to physical memory mappings", },
7c664e2f
AJ
1790#endif
1791#if defined(TARGET_I386)
b86bda5b
FB
1792 { "mem", "", mem_info,
1793 "", "show the active virtual memory mappings", },
16b29ae1
AL
1794 { "hpet", "", do_info_hpet,
1795 "", "show state of HPET", },
b86bda5b 1796#endif
e3db7226
FB
1797 { "jit", "", do_info_jit,
1798 "", "show dynamic compiler info", },
7ba1e619 1799 { "kvm", "", do_info_kvm,
d2c639d6 1800 "", "show KVM information", },
030ea37b
AL
1801 { "numa", "", do_info_numa,
1802 "", "show NUMA information", },
a594cfbf
FB
1803 { "usb", "", usb_info,
1804 "", "show guest USB devices", },
1805 { "usbhost", "", usb_host_info,
1806 "", "show host USB devices", },
5f1ce948
FB
1807 { "profile", "", do_info_profile,
1808 "", "show profiling information", },
ec36b695 1809 { "capture", "", do_info_capture,
17100159 1810 "", "show capture information" },
faea38e7 1811 { "snapshots", "", do_info_snapshots,
17100159 1812 "", "show the currently saved VM snapshots" },
6f9c5ee7
AJ
1813 { "status", "", do_info_status,
1814 "", "show the current VM status (running|paused)" },
201a51fc
AZ
1815 { "pcmcia", "", pcmcia_info,
1816 "", "show guest PCMCIA status" },
455204eb
TS
1817 { "mice", "", do_info_mice,
1818 "", "show which guest mouse is receiving events" },
a9ce8590
FB
1819 { "vnc", "", do_info_vnc,
1820 "", "show the vnc server status"},
c35734b2
TS
1821 { "name", "", do_info_name,
1822 "", "show the current VM name" },
f1f23ad5
BS
1823 { "uuid", "", do_info_uuid,
1824 "", "show the current VM UUID" },
76a66253
JM
1825#if defined(TARGET_PPC)
1826 { "cpustats", "", do_info_cpu_stats,
1827 "", "show CPU statistics", },
1828#endif
31a60e22 1829#if defined(CONFIG_SLIRP)
6dbe553f
JK
1830 { "usernet", "", do_info_usernet,
1831 "", "show user network stack connection states", },
31a60e22 1832#endif
5bb7910a 1833 { "migrate", "", do_info_migrate, "", "show migration status" },
df751fa8
AL
1834 { "balloon", "", do_info_balloon,
1835 "", "show balloon information" },
cae4956e
GH
1836 { "qtree", "", do_info_qtree,
1837 "", "show device tree" },
f6c64e0e
GH
1838 { "qdm", "", do_info_qdm,
1839 "", "show qdev device model list" },
9dc39cba
FB
1840 { NULL, NULL, },
1841};
1842
9307c4c1
FB
1843/*******************************************************************/
1844
1845static const char *pch;
1846static jmp_buf expr_env;
1847
92a31b1f
FB
1848#define MD_TLONG 0
1849#define MD_I32 1
1850
9307c4c1
FB
1851typedef struct MonitorDef {
1852 const char *name;
1853 int offset;
8662d656 1854 target_long (*get_value)(const struct MonitorDef *md, int val);
92a31b1f 1855 int type;
9307c4c1
FB
1856} MonitorDef;
1857
57206fd4 1858#if defined(TARGET_I386)
8662d656 1859static target_long monitor_get_pc (const struct MonitorDef *md, int val)
57206fd4 1860{
6a00d601
FB
1861 CPUState *env = mon_get_cpu();
1862 if (!env)
1863 return 0;
1864 return env->eip + env->segs[R_CS].base;
57206fd4
FB
1865}
1866#endif
1867
a541f297 1868#if defined(TARGET_PPC)
8662d656 1869static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
a541f297 1870{
6a00d601 1871 CPUState *env = mon_get_cpu();
a541f297
FB
1872 unsigned int u;
1873 int i;
1874
6a00d601
FB
1875 if (!env)
1876 return 0;
1877
a541f297
FB
1878 u = 0;
1879 for (i = 0; i < 8; i++)
28a76be8 1880 u |= env->crf[i] << (32 - (4 * i));
a541f297
FB
1881
1882 return u;
1883}
1884
8662d656 1885static target_long monitor_get_msr (const struct MonitorDef *md, int val)
a541f297 1886{
6a00d601
FB
1887 CPUState *env = mon_get_cpu();
1888 if (!env)
1889 return 0;
0411a972 1890 return env->msr;
a541f297
FB
1891}
1892
8662d656 1893static target_long monitor_get_xer (const struct MonitorDef *md, int val)
a541f297 1894{
6a00d601
FB
1895 CPUState *env = mon_get_cpu();
1896 if (!env)
1897 return 0;
3d7b417e 1898 return env->xer;
a541f297 1899}
9fddaa0c 1900
8662d656 1901static target_long monitor_get_decr (const struct MonitorDef *md, int val)
9fddaa0c 1902{
6a00d601
FB
1903 CPUState *env = mon_get_cpu();
1904 if (!env)
1905 return 0;
1906 return cpu_ppc_load_decr(env);
9fddaa0c
FB
1907}
1908
8662d656 1909static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
9fddaa0c 1910{
6a00d601
FB
1911 CPUState *env = mon_get_cpu();
1912 if (!env)
1913 return 0;
1914 return cpu_ppc_load_tbu(env);
9fddaa0c
FB
1915}
1916
8662d656 1917static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
9fddaa0c 1918{
6a00d601
FB
1919 CPUState *env = mon_get_cpu();
1920 if (!env)
1921 return 0;
1922 return cpu_ppc_load_tbl(env);
9fddaa0c 1923}
a541f297
FB
1924#endif
1925
e95c8d51 1926#if defined(TARGET_SPARC)
7b936c0c 1927#ifndef TARGET_SPARC64
8662d656 1928static target_long monitor_get_psr (const struct MonitorDef *md, int val)
e95c8d51 1929{
6a00d601
FB
1930 CPUState *env = mon_get_cpu();
1931 if (!env)
1932 return 0;
1933 return GET_PSR(env);
e95c8d51 1934}
7b936c0c 1935#endif
e95c8d51 1936
8662d656 1937static target_long monitor_get_reg(const struct MonitorDef *md, int val)
e95c8d51 1938{
6a00d601
FB
1939 CPUState *env = mon_get_cpu();
1940 if (!env)
1941 return 0;
1942 return env->regwptr[val];
e95c8d51
FB
1943}
1944#endif
1945
8662d656 1946static const MonitorDef monitor_defs[] = {
9307c4c1 1947#ifdef TARGET_I386
57206fd4
FB
1948
1949#define SEG(name, seg) \
92a31b1f 1950 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
57206fd4 1951 { name ".base", offsetof(CPUState, segs[seg].base) },\
92a31b1f 1952 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
57206fd4 1953
9307c4c1
FB
1954 { "eax", offsetof(CPUState, regs[0]) },
1955 { "ecx", offsetof(CPUState, regs[1]) },
1956 { "edx", offsetof(CPUState, regs[2]) },
1957 { "ebx", offsetof(CPUState, regs[3]) },
1958 { "esp|sp", offsetof(CPUState, regs[4]) },
1959 { "ebp|fp", offsetof(CPUState, regs[5]) },
1960 { "esi", offsetof(CPUState, regs[6]) },
01038d2a 1961 { "edi", offsetof(CPUState, regs[7]) },
92a31b1f
FB
1962#ifdef TARGET_X86_64
1963 { "r8", offsetof(CPUState, regs[8]) },
1964 { "r9", offsetof(CPUState, regs[9]) },
1965 { "r10", offsetof(CPUState, regs[10]) },
1966 { "r11", offsetof(CPUState, regs[11]) },
1967 { "r12", offsetof(CPUState, regs[12]) },
1968 { "r13", offsetof(CPUState, regs[13]) },
1969 { "r14", offsetof(CPUState, regs[14]) },
1970 { "r15", offsetof(CPUState, regs[15]) },
1971#endif
9307c4c1 1972 { "eflags", offsetof(CPUState, eflags) },
57206fd4
FB
1973 { "eip", offsetof(CPUState, eip) },
1974 SEG("cs", R_CS)
1975 SEG("ds", R_DS)
1976 SEG("es", R_ES)
01038d2a 1977 SEG("ss", R_SS)
57206fd4
FB
1978 SEG("fs", R_FS)
1979 SEG("gs", R_GS)
1980 { "pc", 0, monitor_get_pc, },
a541f297 1981#elif defined(TARGET_PPC)
ff937dba 1982 /* General purpose registers */
a541f297
FB
1983 { "r0", offsetof(CPUState, gpr[0]) },
1984 { "r1", offsetof(CPUState, gpr[1]) },
1985 { "r2", offsetof(CPUState, gpr[2]) },
1986 { "r3", offsetof(CPUState, gpr[3]) },
1987 { "r4", offsetof(CPUState, gpr[4]) },
1988 { "r5", offsetof(CPUState, gpr[5]) },
1989 { "r6", offsetof(CPUState, gpr[6]) },
1990 { "r7", offsetof(CPUState, gpr[7]) },
1991 { "r8", offsetof(CPUState, gpr[8]) },
1992 { "r9", offsetof(CPUState, gpr[9]) },
1993 { "r10", offsetof(CPUState, gpr[10]) },
1994 { "r11", offsetof(CPUState, gpr[11]) },
1995 { "r12", offsetof(CPUState, gpr[12]) },
1996 { "r13", offsetof(CPUState, gpr[13]) },
1997 { "r14", offsetof(CPUState, gpr[14]) },
1998 { "r15", offsetof(CPUState, gpr[15]) },
1999 { "r16", offsetof(CPUState, gpr[16]) },
2000 { "r17", offsetof(CPUState, gpr[17]) },
2001 { "r18", offsetof(CPUState, gpr[18]) },
2002 { "r19", offsetof(CPUState, gpr[19]) },
2003 { "r20", offsetof(CPUState, gpr[20]) },
2004 { "r21", offsetof(CPUState, gpr[21]) },
2005 { "r22", offsetof(CPUState, gpr[22]) },
2006 { "r23", offsetof(CPUState, gpr[23]) },
2007 { "r24", offsetof(CPUState, gpr[24]) },
2008 { "r25", offsetof(CPUState, gpr[25]) },
2009 { "r26", offsetof(CPUState, gpr[26]) },
2010 { "r27", offsetof(CPUState, gpr[27]) },
2011 { "r28", offsetof(CPUState, gpr[28]) },
2012 { "r29", offsetof(CPUState, gpr[29]) },
2013 { "r30", offsetof(CPUState, gpr[30]) },
2014 { "r31", offsetof(CPUState, gpr[31]) },
ff937dba
JM
2015 /* Floating point registers */
2016 { "f0", offsetof(CPUState, fpr[0]) },
2017 { "f1", offsetof(CPUState, fpr[1]) },
2018 { "f2", offsetof(CPUState, fpr[2]) },
2019 { "f3", offsetof(CPUState, fpr[3]) },
2020 { "f4", offsetof(CPUState, fpr[4]) },
2021 { "f5", offsetof(CPUState, fpr[5]) },
2022 { "f6", offsetof(CPUState, fpr[6]) },
2023 { "f7", offsetof(CPUState, fpr[7]) },
2024 { "f8", offsetof(CPUState, fpr[8]) },
2025 { "f9", offsetof(CPUState, fpr[9]) },
2026 { "f10", offsetof(CPUState, fpr[10]) },
2027 { "f11", offsetof(CPUState, fpr[11]) },
2028 { "f12", offsetof(CPUState, fpr[12]) },
2029 { "f13", offsetof(CPUState, fpr[13]) },
2030 { "f14", offsetof(CPUState, fpr[14]) },
2031 { "f15", offsetof(CPUState, fpr[15]) },
2032 { "f16", offsetof(CPUState, fpr[16]) },
2033 { "f17", offsetof(CPUState, fpr[17]) },
2034 { "f18", offsetof(CPUState, fpr[18]) },
2035 { "f19", offsetof(CPUState, fpr[19]) },
2036 { "f20", offsetof(CPUState, fpr[20]) },
2037 { "f21", offsetof(CPUState, fpr[21]) },
2038 { "f22", offsetof(CPUState, fpr[22]) },
2039 { "f23", offsetof(CPUState, fpr[23]) },
2040 { "f24", offsetof(CPUState, fpr[24]) },
2041 { "f25", offsetof(CPUState, fpr[25]) },
2042 { "f26", offsetof(CPUState, fpr[26]) },
2043 { "f27", offsetof(CPUState, fpr[27]) },
2044 { "f28", offsetof(CPUState, fpr[28]) },
2045 { "f29", offsetof(CPUState, fpr[29]) },
2046 { "f30", offsetof(CPUState, fpr[30]) },
2047 { "f31", offsetof(CPUState, fpr[31]) },
2048 { "fpscr", offsetof(CPUState, fpscr) },
2049 /* Next instruction pointer */
57206fd4 2050 { "nip|pc", offsetof(CPUState, nip) },
a541f297
FB
2051 { "lr", offsetof(CPUState, lr) },
2052 { "ctr", offsetof(CPUState, ctr) },
9fddaa0c 2053 { "decr", 0, &monitor_get_decr, },
a541f297 2054 { "ccr", 0, &monitor_get_ccr, },
ff937dba 2055 /* Machine state register */
a541f297
FB
2056 { "msr", 0, &monitor_get_msr, },
2057 { "xer", 0, &monitor_get_xer, },
9fddaa0c
FB
2058 { "tbu", 0, &monitor_get_tbu, },
2059 { "tbl", 0, &monitor_get_tbl, },
ff937dba
JM
2060#if defined(TARGET_PPC64)
2061 /* Address space register */
2062 { "asr", offsetof(CPUState, asr) },
2063#endif
2064 /* Segment registers */
a541f297
FB
2065 { "sdr1", offsetof(CPUState, sdr1) },
2066 { "sr0", offsetof(CPUState, sr[0]) },
2067 { "sr1", offsetof(CPUState, sr[1]) },
2068 { "sr2", offsetof(CPUState, sr[2]) },
2069 { "sr3", offsetof(CPUState, sr[3]) },
2070 { "sr4", offsetof(CPUState, sr[4]) },
2071 { "sr5", offsetof(CPUState, sr[5]) },
2072 { "sr6", offsetof(CPUState, sr[6]) },
2073 { "sr7", offsetof(CPUState, sr[7]) },
2074 { "sr8", offsetof(CPUState, sr[8]) },
2075 { "sr9", offsetof(CPUState, sr[9]) },
2076 { "sr10", offsetof(CPUState, sr[10]) },
2077 { "sr11", offsetof(CPUState, sr[11]) },
2078 { "sr12", offsetof(CPUState, sr[12]) },
2079 { "sr13", offsetof(CPUState, sr[13]) },
2080 { "sr14", offsetof(CPUState, sr[14]) },
2081 { "sr15", offsetof(CPUState, sr[15]) },
2082 /* Too lazy to put BATs and SPRs ... */
e95c8d51
FB
2083#elif defined(TARGET_SPARC)
2084 { "g0", offsetof(CPUState, gregs[0]) },
2085 { "g1", offsetof(CPUState, gregs[1]) },
2086 { "g2", offsetof(CPUState, gregs[2]) },
2087 { "g3", offsetof(CPUState, gregs[3]) },
2088 { "g4", offsetof(CPUState, gregs[4]) },
2089 { "g5", offsetof(CPUState, gregs[5]) },
2090 { "g6", offsetof(CPUState, gregs[6]) },
2091 { "g7", offsetof(CPUState, gregs[7]) },
2092 { "o0", 0, monitor_get_reg },
2093 { "o1", 1, monitor_get_reg },
2094 { "o2", 2, monitor_get_reg },
2095 { "o3", 3, monitor_get_reg },
2096 { "o4", 4, monitor_get_reg },
2097 { "o5", 5, monitor_get_reg },
2098 { "o6", 6, monitor_get_reg },
2099 { "o7", 7, monitor_get_reg },
2100 { "l0", 8, monitor_get_reg },
2101 { "l1", 9, monitor_get_reg },
2102 { "l2", 10, monitor_get_reg },
2103 { "l3", 11, monitor_get_reg },
2104 { "l4", 12, monitor_get_reg },
2105 { "l5", 13, monitor_get_reg },
2106 { "l6", 14, monitor_get_reg },
2107 { "l7", 15, monitor_get_reg },
2108 { "i0", 16, monitor_get_reg },
2109 { "i1", 17, monitor_get_reg },
2110 { "i2", 18, monitor_get_reg },
2111 { "i3", 19, monitor_get_reg },
2112 { "i4", 20, monitor_get_reg },
2113 { "i5", 21, monitor_get_reg },
2114 { "i6", 22, monitor_get_reg },
2115 { "i7", 23, monitor_get_reg },
2116 { "pc", offsetof(CPUState, pc) },
2117 { "npc", offsetof(CPUState, npc) },
2118 { "y", offsetof(CPUState, y) },
7b936c0c 2119#ifndef TARGET_SPARC64
e95c8d51
FB
2120 { "psr", 0, &monitor_get_psr, },
2121 { "wim", offsetof(CPUState, wim) },
7b936c0c 2122#endif
e95c8d51
FB
2123 { "tbr", offsetof(CPUState, tbr) },
2124 { "fsr", offsetof(CPUState, fsr) },
2125 { "f0", offsetof(CPUState, fpr[0]) },
2126 { "f1", offsetof(CPUState, fpr[1]) },
2127 { "f2", offsetof(CPUState, fpr[2]) },
2128 { "f3", offsetof(CPUState, fpr[3]) },
2129 { "f4", offsetof(CPUState, fpr[4]) },
2130 { "f5", offsetof(CPUState, fpr[5]) },
2131 { "f6", offsetof(CPUState, fpr[6]) },
2132 { "f7", offsetof(CPUState, fpr[7]) },
2133 { "f8", offsetof(CPUState, fpr[8]) },
2134 { "f9", offsetof(CPUState, fpr[9]) },
2135 { "f10", offsetof(CPUState, fpr[10]) },
2136 { "f11", offsetof(CPUState, fpr[11]) },
2137 { "f12", offsetof(CPUState, fpr[12]) },
2138 { "f13", offsetof(CPUState, fpr[13]) },
2139 { "f14", offsetof(CPUState, fpr[14]) },
2140 { "f15", offsetof(CPUState, fpr[15]) },
2141 { "f16", offsetof(CPUState, fpr[16]) },
2142 { "f17", offsetof(CPUState, fpr[17]) },
2143 { "f18", offsetof(CPUState, fpr[18]) },
2144 { "f19", offsetof(CPUState, fpr[19]) },
2145 { "f20", offsetof(CPUState, fpr[20]) },
2146 { "f21", offsetof(CPUState, fpr[21]) },
2147 { "f22", offsetof(CPUState, fpr[22]) },
2148 { "f23", offsetof(CPUState, fpr[23]) },
2149 { "f24", offsetof(CPUState, fpr[24]) },
2150 { "f25", offsetof(CPUState, fpr[25]) },
2151 { "f26", offsetof(CPUState, fpr[26]) },
2152 { "f27", offsetof(CPUState, fpr[27]) },
2153 { "f28", offsetof(CPUState, fpr[28]) },
2154 { "f29", offsetof(CPUState, fpr[29]) },
2155 { "f30", offsetof(CPUState, fpr[30]) },
2156 { "f31", offsetof(CPUState, fpr[31]) },
7b936c0c
FB
2157#ifdef TARGET_SPARC64
2158 { "f32", offsetof(CPUState, fpr[32]) },
2159 { "f34", offsetof(CPUState, fpr[34]) },
2160 { "f36", offsetof(CPUState, fpr[36]) },
2161 { "f38", offsetof(CPUState, fpr[38]) },
2162 { "f40", offsetof(CPUState, fpr[40]) },
2163 { "f42", offsetof(CPUState, fpr[42]) },
2164 { "f44", offsetof(CPUState, fpr[44]) },
2165 { "f46", offsetof(CPUState, fpr[46]) },
2166 { "f48", offsetof(CPUState, fpr[48]) },
2167 { "f50", offsetof(CPUState, fpr[50]) },
2168 { "f52", offsetof(CPUState, fpr[52]) },
2169 { "f54", offsetof(CPUState, fpr[54]) },
2170 { "f56", offsetof(CPUState, fpr[56]) },
2171 { "f58", offsetof(CPUState, fpr[58]) },
2172 { "f60", offsetof(CPUState, fpr[60]) },
2173 { "f62", offsetof(CPUState, fpr[62]) },
2174 { "asi", offsetof(CPUState, asi) },
2175 { "pstate", offsetof(CPUState, pstate) },
2176 { "cansave", offsetof(CPUState, cansave) },
2177 { "canrestore", offsetof(CPUState, canrestore) },
2178 { "otherwin", offsetof(CPUState, otherwin) },
2179 { "wstate", offsetof(CPUState, wstate) },
2180 { "cleanwin", offsetof(CPUState, cleanwin) },
2181 { "fprs", offsetof(CPUState, fprs) },
2182#endif
9307c4c1
FB
2183#endif
2184 { NULL },
2185};
2186
376253ec 2187static void expr_error(Monitor *mon, const char *msg)
9dc39cba 2188{
376253ec 2189 monitor_printf(mon, "%s\n", msg);
9307c4c1
FB
2190 longjmp(expr_env, 1);
2191}
2192
6a00d601 2193/* return 0 if OK, -1 if not found, -2 if no CPU defined */
92a31b1f 2194static int get_monitor_def(target_long *pval, const char *name)
9307c4c1 2195{
8662d656 2196 const MonitorDef *md;
92a31b1f
FB
2197 void *ptr;
2198
9307c4c1
FB
2199 for(md = monitor_defs; md->name != NULL; md++) {
2200 if (compare_cmd(name, md->name)) {
2201 if (md->get_value) {
e95c8d51 2202 *pval = md->get_value(md, md->offset);
9307c4c1 2203 } else {
6a00d601
FB
2204 CPUState *env = mon_get_cpu();
2205 if (!env)
2206 return -2;
2207 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
2208 switch(md->type) {
2209 case MD_I32:
2210 *pval = *(int32_t *)ptr;
2211 break;
2212 case MD_TLONG:
2213 *pval = *(target_long *)ptr;
2214 break;
2215 default:
2216 *pval = 0;
2217 break;
2218 }
9307c4c1
FB
2219 }
2220 return 0;
2221 }
2222 }
2223 return -1;
2224}
2225
2226static void next(void)
2227{
660f11be 2228 if (*pch != '\0') {
9307c4c1 2229 pch++;
cd390083 2230 while (qemu_isspace(*pch))
9307c4c1
FB
2231 pch++;
2232 }
2233}
2234
376253ec 2235static int64_t expr_sum(Monitor *mon);
9307c4c1 2236
376253ec 2237static int64_t expr_unary(Monitor *mon)
9307c4c1 2238{
c2efc95d 2239 int64_t n;
9307c4c1 2240 char *p;
6a00d601 2241 int ret;
9307c4c1
FB
2242
2243 switch(*pch) {
2244 case '+':
2245 next();
376253ec 2246 n = expr_unary(mon);
9307c4c1
FB
2247 break;
2248 case '-':
2249 next();
376253ec 2250 n = -expr_unary(mon);
9307c4c1
FB
2251 break;
2252 case '~':
2253 next();
376253ec 2254 n = ~expr_unary(mon);
9307c4c1
FB
2255 break;
2256 case '(':
2257 next();
376253ec 2258 n = expr_sum(mon);
9307c4c1 2259 if (*pch != ')') {
376253ec 2260 expr_error(mon, "')' expected");
9307c4c1
FB
2261 }
2262 next();
2263 break;
81d0912d
FB
2264 case '\'':
2265 pch++;
2266 if (*pch == '\0')
376253ec 2267 expr_error(mon, "character constant expected");
81d0912d
FB
2268 n = *pch;
2269 pch++;
2270 if (*pch != '\'')
376253ec 2271 expr_error(mon, "missing terminating \' character");
81d0912d
FB
2272 next();
2273 break;
9307c4c1
FB
2274 case '$':
2275 {
2276 char buf[128], *q;
69b34976 2277 target_long reg=0;
3b46e624 2278
9307c4c1
FB
2279 pch++;
2280 q = buf;
2281 while ((*pch >= 'a' && *pch <= 'z') ||
2282 (*pch >= 'A' && *pch <= 'Z') ||
2283 (*pch >= '0' && *pch <= '9') ||
57206fd4 2284 *pch == '_' || *pch == '.') {
9307c4c1
FB
2285 if ((q - buf) < sizeof(buf) - 1)
2286 *q++ = *pch;
2287 pch++;
2288 }
cd390083 2289 while (qemu_isspace(*pch))
9307c4c1
FB
2290 pch++;
2291 *q = 0;
7743e588 2292 ret = get_monitor_def(&reg, buf);
6a00d601 2293 if (ret == -1)
376253ec 2294 expr_error(mon, "unknown register");
5fafdf24 2295 else if (ret == -2)
376253ec 2296 expr_error(mon, "no cpu defined");
7743e588 2297 n = reg;
9307c4c1
FB
2298 }
2299 break;
2300 case '\0':
376253ec 2301 expr_error(mon, "unexpected end of expression");
9307c4c1
FB
2302 n = 0;
2303 break;
2304 default:
7743e588 2305#if TARGET_PHYS_ADDR_BITS > 32
4f4fbf77
FB
2306 n = strtoull(pch, &p, 0);
2307#else
9307c4c1 2308 n = strtoul(pch, &p, 0);
4f4fbf77 2309#endif
9307c4c1 2310 if (pch == p) {
376253ec 2311 expr_error(mon, "invalid char in expression");
9307c4c1
FB
2312 }
2313 pch = p;
cd390083 2314 while (qemu_isspace(*pch))
9307c4c1
FB
2315 pch++;
2316 break;
2317 }
2318 return n;
2319}
2320
2321
376253ec 2322static int64_t expr_prod(Monitor *mon)
9307c4c1 2323{
c2efc95d 2324 int64_t val, val2;
92a31b1f 2325 int op;
3b46e624 2326
376253ec 2327 val = expr_unary(mon);
9307c4c1
FB
2328 for(;;) {
2329 op = *pch;
2330 if (op != '*' && op != '/' && op != '%')
2331 break;
2332 next();
376253ec 2333 val2 = expr_unary(mon);
9307c4c1
FB
2334 switch(op) {
2335 default:
2336 case '*':
2337 val *= val2;
2338 break;
2339 case '/':
2340 case '%':
5fafdf24 2341 if (val2 == 0)
376253ec 2342 expr_error(mon, "division by zero");
9307c4c1
FB
2343 if (op == '/')
2344 val /= val2;
2345 else
2346 val %= val2;
2347 break;
2348 }
2349 }
2350 return val;
2351}
2352
376253ec 2353static int64_t expr_logic(Monitor *mon)
9307c4c1 2354{
c2efc95d 2355 int64_t val, val2;
92a31b1f 2356 int op;
9307c4c1 2357
376253ec 2358 val = expr_prod(mon);
9307c4c1
FB
2359 for(;;) {
2360 op = *pch;
2361 if (op != '&' && op != '|' && op != '^')
2362 break;
2363 next();
376253ec 2364 val2 = expr_prod(mon);
9307c4c1
FB
2365 switch(op) {
2366 default:
2367 case '&':
2368 val &= val2;
2369 break;
2370 case '|':
2371 val |= val2;
2372 break;
2373 case '^':
2374 val ^= val2;
2375 break;
2376 }
2377 }
2378 return val;
2379}
2380
376253ec 2381static int64_t expr_sum(Monitor *mon)
9307c4c1 2382{
c2efc95d 2383 int64_t val, val2;
92a31b1f 2384 int op;
9307c4c1 2385
376253ec 2386 val = expr_logic(mon);
9307c4c1
FB
2387 for(;;) {
2388 op = *pch;
2389 if (op != '+' && op != '-')
2390 break;
2391 next();
376253ec 2392 val2 = expr_logic(mon);
9307c4c1
FB
2393 if (op == '+')
2394 val += val2;
2395 else
2396 val -= val2;
2397 }
2398 return val;
2399}
2400
376253ec 2401static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
9307c4c1
FB
2402{
2403 pch = *pp;
2404 if (setjmp(expr_env)) {
2405 *pp = pch;
2406 return -1;
2407 }
cd390083 2408 while (qemu_isspace(*pch))
9307c4c1 2409 pch++;
376253ec 2410 *pval = expr_sum(mon);
9307c4c1
FB
2411 *pp = pch;
2412 return 0;
2413}
2414
2415static int get_str(char *buf, int buf_size, const char **pp)
2416{
2417 const char *p;
2418 char *q;
2419 int c;
2420
81d0912d 2421 q = buf;
9307c4c1 2422 p = *pp;
cd390083 2423 while (qemu_isspace(*p))
9307c4c1
FB
2424 p++;
2425 if (*p == '\0') {
2426 fail:
81d0912d 2427 *q = '\0';
9307c4c1
FB
2428 *pp = p;
2429 return -1;
2430 }
9307c4c1
FB
2431 if (*p == '\"') {
2432 p++;
2433 while (*p != '\0' && *p != '\"') {
2434 if (*p == '\\') {
2435 p++;
2436 c = *p++;
2437 switch(c) {
2438 case 'n':
2439 c = '\n';
2440 break;
2441 case 'r':
2442 c = '\r';
2443 break;
2444 case '\\':
2445 case '\'':
2446 case '\"':
2447 break;
2448 default:
2449 qemu_printf("unsupported escape code: '\\%c'\n", c);
2450 goto fail;
2451 }
2452 if ((q - buf) < buf_size - 1) {
2453 *q++ = c;
2454 }
2455 } else {
2456 if ((q - buf) < buf_size - 1) {
2457 *q++ = *p;
2458 }
2459 p++;
2460 }
2461 }
2462 if (*p != '\"') {
5b60212f 2463 qemu_printf("unterminated string\n");
9307c4c1
FB
2464 goto fail;
2465 }
2466 p++;
2467 } else {
cd390083 2468 while (*p != '\0' && !qemu_isspace(*p)) {
9307c4c1
FB
2469 if ((q - buf) < buf_size - 1) {
2470 *q++ = *p;
2471 }
2472 p++;
2473 }
9307c4c1 2474 }
81d0912d 2475 *q = '\0';
9307c4c1
FB
2476 *pp = p;
2477 return 0;
2478}
2479
4590fd80
LC
2480/*
2481 * Store the command-name in cmdname, and return a pointer to
2482 * the remaining of the command string.
2483 */
2484static const char *get_command_name(const char *cmdline,
2485 char *cmdname, size_t nlen)
2486{
2487 size_t len;
2488 const char *p, *pstart;
2489
2490 p = cmdline;
2491 while (qemu_isspace(*p))
2492 p++;
2493 if (*p == '\0')
2494 return NULL;
2495 pstart = p;
2496 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2497 p++;
2498 len = p - pstart;
2499 if (len > nlen - 1)
2500 len = nlen - 1;
2501 memcpy(cmdname, pstart, len);
2502 cmdname[len] = '\0';
2503 return p;
2504}
2505
9307c4c1
FB
2506static int default_fmt_format = 'x';
2507static int default_fmt_size = 4;
2508
2509#define MAX_ARGS 16
2510
376253ec 2511static void monitor_handle_command(Monitor *mon, const char *cmdline)
9307c4c1 2512{
4590fd80
LC
2513 const char *p, *typestr;
2514 int c, nb_args, i, has_arg;
376253ec 2515 const mon_cmd_t *cmd;
9307c4c1
FB
2516 char cmdname[256];
2517 char buf[1024];
2518 void *str_allocated[MAX_ARGS];
2519 void *args[MAX_ARGS];
376253ec
AL
2520 void (*handler_0)(Monitor *mon);
2521 void (*handler_1)(Monitor *mon, void *arg0);
2522 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2523 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2524 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2525 void *arg3);
2526 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2527 void *arg3, void *arg4);
2528 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2529 void *arg3, void *arg4, void *arg5);
2530 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2531 void *arg3, void *arg4, void *arg5, void *arg6);
79c4f6b0
HY
2532 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2533 void *arg3, void *arg4, void *arg5, void *arg6,
2534 void *arg7);
2535 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2536 void *arg3, void *arg4, void *arg5, void *arg6,
2537 void *arg7, void *arg8);
2538 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2539 void *arg3, void *arg4, void *arg5, void *arg6,
2540 void *arg7, void *arg8, void *arg9);
9dc39cba
FB
2541
2542#ifdef DEBUG
376253ec 2543 monitor_printf(mon, "command='%s'\n", cmdline);
9dc39cba 2544#endif
3b46e624 2545
9307c4c1 2546 /* extract the command name */
4590fd80
LC
2547 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2548 if (!p)
9307c4c1 2549 return;
3b46e624 2550
9307c4c1 2551 /* find the command */
376253ec 2552 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
5fafdf24 2553 if (compare_cmd(cmdname, cmd->name))
d91d9bf6
LC
2554 break;
2555 }
2556
2557 if (cmd->name == NULL) {
2558 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2559 return;
9307c4c1 2560 }
9307c4c1
FB
2561
2562 for(i = 0; i < MAX_ARGS; i++)
2563 str_allocated[i] = NULL;
3b46e624 2564
9307c4c1
FB
2565 /* parse the parameters */
2566 typestr = cmd->args_type;
2567 nb_args = 0;
9dc39cba 2568 for(;;) {
9307c4c1
FB
2569 c = *typestr;
2570 if (c == '\0')
9dc39cba 2571 break;
9307c4c1
FB
2572 typestr++;
2573 switch(c) {
2574 case 'F':
81d0912d 2575 case 'B':
9307c4c1
FB
2576 case 's':
2577 {
2578 int ret;
2579 char *str;
3b46e624 2580
cd390083 2581 while (qemu_isspace(*p))
9307c4c1
FB
2582 p++;
2583 if (*typestr == '?') {
2584 typestr++;
2585 if (*p == '\0') {
2586 /* no optional string: NULL argument */
2587 str = NULL;
2588 goto add_str;
2589 }
2590 }
2591 ret = get_str(buf, sizeof(buf), &p);
2592 if (ret < 0) {
81d0912d
FB
2593 switch(c) {
2594 case 'F':
376253ec
AL
2595 monitor_printf(mon, "%s: filename expected\n",
2596 cmdname);
81d0912d
FB
2597 break;
2598 case 'B':
376253ec
AL
2599 monitor_printf(mon, "%s: block device name expected\n",
2600 cmdname);
81d0912d
FB
2601 break;
2602 default:
376253ec 2603 monitor_printf(mon, "%s: string expected\n", cmdname);
81d0912d
FB
2604 break;
2605 }
9307c4c1
FB
2606 goto fail;
2607 }
2608 str = qemu_malloc(strlen(buf) + 1);
363a37d5 2609 pstrcpy(str, sizeof(buf), buf);
9307c4c1
FB
2610 str_allocated[nb_args] = str;
2611 add_str:
2612 if (nb_args >= MAX_ARGS) {
2613 error_args:
376253ec 2614 monitor_printf(mon, "%s: too many arguments\n", cmdname);
9307c4c1
FB
2615 goto fail;
2616 }
2617 args[nb_args++] = str;
2618 }
9dc39cba 2619 break;
9307c4c1
FB
2620 case '/':
2621 {
2622 int count, format, size;
3b46e624 2623
cd390083 2624 while (qemu_isspace(*p))
9307c4c1
FB
2625 p++;
2626 if (*p == '/') {
2627 /* format found */
2628 p++;
2629 count = 1;
cd390083 2630 if (qemu_isdigit(*p)) {
9307c4c1 2631 count = 0;
cd390083 2632 while (qemu_isdigit(*p)) {
9307c4c1
FB
2633 count = count * 10 + (*p - '0');
2634 p++;
2635 }
2636 }
2637 size = -1;
2638 format = -1;
2639 for(;;) {
2640 switch(*p) {
2641 case 'o':
2642 case 'd':
2643 case 'u':
2644 case 'x':
2645 case 'i':
2646 case 'c':
2647 format = *p++;
2648 break;
2649 case 'b':
2650 size = 1;
2651 p++;
2652 break;
2653 case 'h':
2654 size = 2;
2655 p++;
2656 break;
2657 case 'w':
2658 size = 4;
2659 p++;
2660 break;
2661 case 'g':
2662 case 'L':
2663 size = 8;
2664 p++;
2665 break;
2666 default:
2667 goto next;
2668 }
2669 }
2670 next:
cd390083 2671 if (*p != '\0' && !qemu_isspace(*p)) {
376253ec
AL
2672 monitor_printf(mon, "invalid char in format: '%c'\n",
2673 *p);
9307c4c1
FB
2674 goto fail;
2675 }
9307c4c1
FB
2676 if (format < 0)
2677 format = default_fmt_format;
4c27ba27
FB
2678 if (format != 'i') {
2679 /* for 'i', not specifying a size gives -1 as size */
2680 if (size < 0)
2681 size = default_fmt_size;
e90f009b 2682 default_fmt_size = size;
4c27ba27 2683 }
9307c4c1
FB
2684 default_fmt_format = format;
2685 } else {
2686 count = 1;
2687 format = default_fmt_format;
4c27ba27
FB
2688 if (format != 'i') {
2689 size = default_fmt_size;
2690 } else {
2691 size = -1;
2692 }
9307c4c1
FB
2693 }
2694 if (nb_args + 3 > MAX_ARGS)
2695 goto error_args;
1c5bf3bf
JM
2696 args[nb_args++] = (void*)(long)count;
2697 args[nb_args++] = (void*)(long)format;
2698 args[nb_args++] = (void*)(long)size;
9307c4c1 2699 }
9dc39cba 2700 break;
9307c4c1 2701 case 'i':
92a31b1f 2702 case 'l':
9307c4c1 2703 {
c2efc95d 2704 int64_t val;
7743e588 2705
cd390083 2706 while (qemu_isspace(*p))
9307c4c1 2707 p++;
3440557b 2708 if (*typestr == '?' || *typestr == '.') {
3440557b
FB
2709 if (*typestr == '?') {
2710 if (*p == '\0')
2711 has_arg = 0;
2712 else
2713 has_arg = 1;
2714 } else {
2715 if (*p == '.') {
2716 p++;
cd390083 2717 while (qemu_isspace(*p))
3440557b
FB
2718 p++;
2719 has_arg = 1;
2720 } else {
2721 has_arg = 0;
2722 }
2723 }
13224a87 2724 typestr++;
9307c4c1
FB
2725 if (nb_args >= MAX_ARGS)
2726 goto error_args;
1c5bf3bf 2727 args[nb_args++] = (void *)(long)has_arg;
9307c4c1
FB
2728 if (!has_arg) {
2729 if (nb_args >= MAX_ARGS)
2730 goto error_args;
2731 val = -1;
2732 goto add_num;
2733 }
2734 }
376253ec 2735 if (get_expr(mon, &val, &p))
9307c4c1
FB
2736 goto fail;
2737 add_num:
92a31b1f
FB
2738 if (c == 'i') {
2739 if (nb_args >= MAX_ARGS)
2740 goto error_args;
1c5bf3bf 2741 args[nb_args++] = (void *)(long)val;
92a31b1f
FB
2742 } else {
2743 if ((nb_args + 1) >= MAX_ARGS)
2744 goto error_args;
7743e588 2745#if TARGET_PHYS_ADDR_BITS > 32
1c5bf3bf 2746 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
92a31b1f
FB
2747#else
2748 args[nb_args++] = (void *)0;
2749#endif
1c5bf3bf 2750 args[nb_args++] = (void *)(long)(val & 0xffffffff);
92a31b1f 2751 }
9307c4c1
FB
2752 }
2753 break;
2754 case '-':
2755 {
2756 int has_option;
2757 /* option */
3b46e624 2758
9307c4c1
FB
2759 c = *typestr++;
2760 if (c == '\0')
2761 goto bad_type;
cd390083 2762 while (qemu_isspace(*p))
9307c4c1
FB
2763 p++;
2764 has_option = 0;
2765 if (*p == '-') {
2766 p++;
2767 if (*p != c) {
376253ec
AL
2768 monitor_printf(mon, "%s: unsupported option -%c\n",
2769 cmdname, *p);
9307c4c1
FB
2770 goto fail;
2771 }
2772 p++;
2773 has_option = 1;
2774 }
2775 if (nb_args >= MAX_ARGS)
2776 goto error_args;
1c5bf3bf 2777 args[nb_args++] = (void *)(long)has_option;
9307c4c1
FB
2778 }
2779 break;
2780 default:
2781 bad_type:
376253ec 2782 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
9307c4c1
FB
2783 goto fail;
2784 }
9dc39cba 2785 }
9307c4c1 2786 /* check that all arguments were parsed */
cd390083 2787 while (qemu_isspace(*p))
9307c4c1
FB
2788 p++;
2789 if (*p != '\0') {
376253ec
AL
2790 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2791 cmdname);
9307c4c1 2792 goto fail;
9dc39cba 2793 }
9307c4c1
FB
2794
2795 switch(nb_args) {
2796 case 0:
a5f1b965 2797 handler_0 = cmd->handler;
376253ec 2798 handler_0(mon);
9307c4c1
FB
2799 break;
2800 case 1:
a5f1b965 2801 handler_1 = cmd->handler;
376253ec 2802 handler_1(mon, args[0]);
9307c4c1
FB
2803 break;
2804 case 2:
a5f1b965 2805 handler_2 = cmd->handler;
376253ec 2806 handler_2(mon, args[0], args[1]);
9307c4c1
FB
2807 break;
2808 case 3:
a5f1b965 2809 handler_3 = cmd->handler;
376253ec 2810 handler_3(mon, args[0], args[1], args[2]);
9307c4c1
FB
2811 break;
2812 case 4:
a5f1b965 2813 handler_4 = cmd->handler;
376253ec 2814 handler_4(mon, args[0], args[1], args[2], args[3]);
9307c4c1
FB
2815 break;
2816 case 5:
a5f1b965 2817 handler_5 = cmd->handler;
376253ec 2818 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
9307c4c1 2819 break;
3440557b 2820 case 6:
a5f1b965 2821 handler_6 = cmd->handler;
376253ec 2822 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
3440557b 2823 break;
ec36b695 2824 case 7:
a5f1b965 2825 handler_7 = cmd->handler;
376253ec
AL
2826 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2827 args[6]);
ec36b695 2828 break;
79c4f6b0
HY
2829 case 8:
2830 handler_8 = cmd->handler;
2831 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2832 args[6], args[7]);
2833 break;
2834 case 9:
2835 handler_9 = cmd->handler;
2836 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2837 args[6], args[7], args[8]);
2838 break;
2839 case 10:
2840 handler_10 = cmd->handler;
2841 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2842 args[6], args[7], args[8], args[9]);
2843 break;
9307c4c1 2844 default:
376253ec 2845 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
9307c4c1 2846 goto fail;
9dc39cba 2847 }
9307c4c1
FB
2848 fail:
2849 for(i = 0; i < MAX_ARGS; i++)
2850 qemu_free(str_allocated[i]);
9dc39cba
FB
2851}
2852
81d0912d
FB
2853static void cmd_completion(const char *name, const char *list)
2854{
2855 const char *p, *pstart;
2856 char cmd[128];
2857 int len;
2858
2859 p = list;
2860 for(;;) {
2861 pstart = p;
2862 p = strchr(p, '|');
2863 if (!p)
2864 p = pstart + strlen(pstart);
2865 len = p - pstart;
2866 if (len > sizeof(cmd) - 2)
2867 len = sizeof(cmd) - 2;
2868 memcpy(cmd, pstart, len);
2869 cmd[len] = '\0';
2870 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
731b0364 2871 readline_add_completion(cur_mon->rs, cmd);
81d0912d
FB
2872 }
2873 if (*p == '\0')
2874 break;
2875 p++;
2876 }
2877}
2878
2879static void file_completion(const char *input)
2880{
2881 DIR *ffs;
2882 struct dirent *d;
2883 char path[1024];
2884 char file[1024], file_prefix[1024];
2885 int input_path_len;
2886 const char *p;
2887
5fafdf24 2888 p = strrchr(input, '/');
81d0912d
FB
2889 if (!p) {
2890 input_path_len = 0;
2891 pstrcpy(file_prefix, sizeof(file_prefix), input);
363a37d5 2892 pstrcpy(path, sizeof(path), ".");
81d0912d
FB
2893 } else {
2894 input_path_len = p - input + 1;
2895 memcpy(path, input, input_path_len);
2896 if (input_path_len > sizeof(path) - 1)
2897 input_path_len = sizeof(path) - 1;
2898 path[input_path_len] = '\0';
2899 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2900 }
2901#ifdef DEBUG_COMPLETION
376253ec
AL
2902 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2903 input, path, file_prefix);
81d0912d
FB
2904#endif
2905 ffs = opendir(path);
2906 if (!ffs)
2907 return;
2908 for(;;) {
2909 struct stat sb;
2910 d = readdir(ffs);
2911 if (!d)
2912 break;
2913 if (strstart(d->d_name, file_prefix, NULL)) {
2914 memcpy(file, input, input_path_len);
363a37d5
BS
2915 if (input_path_len < sizeof(file))
2916 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2917 d->d_name);
81d0912d
FB
2918 /* stat the file to find out if it's a directory.
2919 * In that case add a slash to speed up typing long paths
2920 */
2921 stat(file, &sb);
2922 if(S_ISDIR(sb.st_mode))
363a37d5 2923 pstrcat(file, sizeof(file), "/");
731b0364 2924 readline_add_completion(cur_mon->rs, file);
81d0912d
FB
2925 }
2926 }
2927 closedir(ffs);
2928}
2929
51de9760 2930static void block_completion_it(void *opaque, BlockDriverState *bs)
81d0912d 2931{
51de9760 2932 const char *name = bdrv_get_device_name(bs);
81d0912d
FB
2933 const char *input = opaque;
2934
2935 if (input[0] == '\0' ||
2936 !strncmp(name, (char *)input, strlen(input))) {
731b0364 2937 readline_add_completion(cur_mon->rs, name);
81d0912d
FB
2938 }
2939}
2940
2941/* NOTE: this parser is an approximate form of the real command parser */
2942static void parse_cmdline(const char *cmdline,
2943 int *pnb_args, char **args)
2944{
2945 const char *p;
2946 int nb_args, ret;
2947 char buf[1024];
2948
2949 p = cmdline;
2950 nb_args = 0;
2951 for(;;) {
cd390083 2952 while (qemu_isspace(*p))
81d0912d
FB
2953 p++;
2954 if (*p == '\0')
2955 break;
2956 if (nb_args >= MAX_ARGS)
2957 break;
2958 ret = get_str(buf, sizeof(buf), &p);
2959 args[nb_args] = qemu_strdup(buf);
2960 nb_args++;
2961 if (ret < 0)
2962 break;
2963 }
2964 *pnb_args = nb_args;
2965}
2966
4c36ba32 2967static void monitor_find_completion(const char *cmdline)
81d0912d
FB
2968{
2969 const char *cmdname;
2970 char *args[MAX_ARGS];
2971 int nb_args, i, len;
2972 const char *ptype, *str;
376253ec 2973 const mon_cmd_t *cmd;
64866c3d 2974 const KeyDef *key;
81d0912d
FB
2975
2976 parse_cmdline(cmdline, &nb_args, args);
2977#ifdef DEBUG_COMPLETION
2978 for(i = 0; i < nb_args; i++) {
376253ec 2979 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
81d0912d
FB
2980 }
2981#endif
2982
2983 /* if the line ends with a space, it means we want to complete the
2984 next arg */
2985 len = strlen(cmdline);
cd390083 2986 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
81d0912d
FB
2987 if (nb_args >= MAX_ARGS)
2988 return;
2989 args[nb_args++] = qemu_strdup("");
2990 }
2991 if (nb_args <= 1) {
2992 /* command completion */
2993 if (nb_args == 0)
2994 cmdname = "";
2995 else
2996 cmdname = args[0];
731b0364 2997 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
376253ec 2998 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
81d0912d
FB
2999 cmd_completion(cmdname, cmd->name);
3000 }
3001 } else {
3002 /* find the command */
376253ec 3003 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
81d0912d
FB
3004 if (compare_cmd(args[0], cmd->name))
3005 goto found;
3006 }
3007 return;
3008 found:
3009 ptype = cmd->args_type;
3010 for(i = 0; i < nb_args - 2; i++) {
3011 if (*ptype != '\0') {
3012 ptype++;
3013 while (*ptype == '?')
3014 ptype++;
3015 }
3016 }
3017 str = args[nb_args - 1];
2a1704a7
BS
3018 if (*ptype == '-' && ptype[1] != '\0') {
3019 ptype += 2;
3020 }
81d0912d
FB
3021 switch(*ptype) {
3022 case 'F':
3023 /* file completion */
731b0364 3024 readline_set_completion_index(cur_mon->rs, strlen(str));
81d0912d
FB
3025 file_completion(str);
3026 break;
3027 case 'B':
3028 /* block device name completion */
731b0364 3029 readline_set_completion_index(cur_mon->rs, strlen(str));
81d0912d
FB
3030 bdrv_iterate(block_completion_it, (void *)str);
3031 break;
7fe48483
FB
3032 case 's':
3033 /* XXX: more generic ? */
3034 if (!strcmp(cmd->name, "info")) {
731b0364 3035 readline_set_completion_index(cur_mon->rs, strlen(str));
7fe48483
FB
3036 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3037 cmd_completion(str, cmd->name);
3038 }
64866c3d 3039 } else if (!strcmp(cmd->name, "sendkey")) {
e600d1ef
BS
3040 char *sep = strrchr(str, '-');
3041 if (sep)
3042 str = sep + 1;
731b0364 3043 readline_set_completion_index(cur_mon->rs, strlen(str));
64866c3d
FB
3044 for(key = key_defs; key->name != NULL; key++) {
3045 cmd_completion(str, key->name);
3046 }
f3353c6b
JK
3047 } else if (!strcmp(cmd->name, "help|?")) {
3048 readline_set_completion_index(cur_mon->rs, strlen(str));
3049 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3050 cmd_completion(str, cmd->name);
3051 }
7fe48483
FB
3052 }
3053 break;
81d0912d
FB
3054 default:
3055 break;
3056 }
3057 }
3058 for(i = 0; i < nb_args; i++)
3059 qemu_free(args[i]);
3060}
3061
731b0364 3062static int monitor_can_read(void *opaque)
9dc39cba 3063{
731b0364
AL
3064 Monitor *mon = opaque;
3065
3066 return (mon->suspend_cnt == 0) ? 128 : 0;
9dc39cba
FB
3067}
3068
731b0364 3069static void monitor_read(void *opaque, const uint8_t *buf, int size)
9dc39cba 3070{
731b0364 3071 Monitor *old_mon = cur_mon;
7e2515e8 3072 int i;
376253ec 3073
731b0364
AL
3074 cur_mon = opaque;
3075
cde76ee1
AL
3076 if (cur_mon->rs) {
3077 for (i = 0; i < size; i++)
3078 readline_handle_byte(cur_mon->rs, buf[i]);
3079 } else {
3080 if (size == 0 || buf[size - 1] != 0)
3081 monitor_printf(cur_mon, "corrupted command\n");
3082 else
3083 monitor_handle_command(cur_mon, (char *)buf);
3084 }
9dc39cba 3085
731b0364
AL
3086 cur_mon = old_mon;
3087}
d8f44609 3088
376253ec 3089static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
aa455485 3090{
731b0364 3091 monitor_suspend(mon);
376253ec 3092 monitor_handle_command(mon, cmdline);
731b0364 3093 monitor_resume(mon);
d8f44609
AL
3094}
3095
cde76ee1 3096int monitor_suspend(Monitor *mon)
d8f44609 3097{
cde76ee1
AL
3098 if (!mon->rs)
3099 return -ENOTTY;
731b0364 3100 mon->suspend_cnt++;
cde76ee1 3101 return 0;
d8f44609
AL
3102}
3103
376253ec 3104void monitor_resume(Monitor *mon)
d8f44609 3105{
cde76ee1
AL
3106 if (!mon->rs)
3107 return;
731b0364
AL
3108 if (--mon->suspend_cnt == 0)
3109 readline_show_prompt(mon->rs);
aa455485
FB
3110}
3111
731b0364 3112static void monitor_event(void *opaque, int event)
86e94dea 3113{
376253ec
AL
3114 Monitor *mon = opaque;
3115
2724b180
AL
3116 switch (event) {
3117 case CHR_EVENT_MUX_IN:
3118 readline_restart(mon->rs);
3119 monitor_resume(mon);
3120 monitor_flush(mon);
3121 break;
3122
3123 case CHR_EVENT_MUX_OUT:
3124 if (mon->suspend_cnt == 0)
3125 monitor_printf(mon, "\n");
3126 monitor_flush(mon);
3127 monitor_suspend(mon);
3128 break;
86e94dea 3129
2724b180
AL
3130 case CHR_EVENT_RESET:
3131 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3132 "information\n", QEMU_VERSION);
3133 if (mon->chr->focus == 0)
3134 readline_show_prompt(mon->rs);
3135 break;
3136 }
86e94dea
TS
3137}
3138
76655d6d
AL
3139
3140/*
3141 * Local variables:
3142 * c-indent-level: 4
3143 * c-basic-offset: 4
3144 * tab-width: 8
3145 * End:
3146 */
3147
731b0364 3148void monitor_init(CharDriverState *chr, int flags)
aa455485 3149{
731b0364 3150 static int is_first_init = 1;
87127161 3151 Monitor *mon;
20d8a3ed
TS
3152
3153 if (is_first_init) {
c8256f9d 3154 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
20d8a3ed
TS
3155 is_first_init = 0;
3156 }
87127161
AL
3157
3158 mon = qemu_mallocz(sizeof(*mon));
20d8a3ed 3159
87127161 3160 mon->chr = chr;
731b0364 3161 mon->flags = flags;
2724b180
AL
3162 if (mon->chr->focus != 0)
3163 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
cde76ee1
AL
3164 if (flags & MONITOR_USE_READLINE) {
3165 mon->rs = readline_init(mon, monitor_find_completion);
3166 monitor_read_command(mon, 0);
3167 }
87127161 3168
731b0364
AL
3169 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3170 mon);
87127161
AL
3171
3172 LIST_INSERT_HEAD(&mon_list, mon, entry);
731b0364 3173 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
87127161 3174 cur_mon = mon;
aa455485
FB
3175}
3176
376253ec 3177static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
81d0912d 3178{
bb5fc20f
AL
3179 BlockDriverState *bs = opaque;
3180 int ret = 0;
81d0912d 3181
bb5fc20f 3182 if (bdrv_set_key(bs, password) != 0) {
376253ec 3183 monitor_printf(mon, "invalid password\n");
bb5fc20f 3184 ret = -EPERM;
9dc39cba 3185 }
731b0364
AL
3186 if (mon->password_completion_cb)
3187 mon->password_completion_cb(mon->password_opaque, ret);
bb5fc20f 3188
731b0364 3189 monitor_read_command(mon, 1);
9dc39cba 3190}
c0f4ce77 3191
376253ec 3192void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
bb5fc20f
AL
3193 BlockDriverCompletionFunc *completion_cb,
3194 void *opaque)
c0f4ce77 3195{
cde76ee1
AL
3196 int err;
3197
bb5fc20f
AL
3198 if (!bdrv_key_required(bs)) {
3199 if (completion_cb)
3200 completion_cb(opaque, 0);
3201 return;
3202 }
c0f4ce77 3203
376253ec
AL
3204 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3205 bdrv_get_encrypted_filename(bs));
bb5fc20f 3206
731b0364
AL
3207 mon->password_completion_cb = completion_cb;
3208 mon->password_opaque = opaque;
bb5fc20f 3209
cde76ee1
AL
3210 err = monitor_read_password(mon, bdrv_password_cb, bs);
3211
3212 if (err && completion_cb)
3213 completion_cb(opaque, err);
c0f4ce77 3214}