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