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