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