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