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