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