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