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