]> git.proxmox.com Git - mirror_qemu.git/blob - monitor.c
added 'info pic' - added 16/32 bit x86 instruction dump
[mirror_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 "vl.h"
25 #include "disas.h"
26
27 //#define DEBUG
28
29 #ifndef offsetof
30 #define offsetof(type, field) ((size_t) &((type *)0)->field)
31 #endif
32
33 #define TERM_CMD_BUF_SIZE 4095
34 #define TERM_MAX_CMDS 64
35
36 #define IS_NORM 0
37 #define IS_ESC 1
38 #define IS_CSI 2
39
40 #define printf do_not_use_printf
41
42 static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1];
43 static int term_cmd_buf_index;
44 static int term_cmd_buf_size;
45 static int term_esc_state;
46 static int term_esc_param;
47
48 static char *term_history[TERM_MAX_CMDS];
49 static int term_hist_entry;
50
51 /*
52 * Supported types:
53 *
54 * 'F' filename
55 * 's' string (accept optional quote)
56 * 'i' integer
57 * '/' optional gdb-like print format (like "/10x")
58 *
59 * '?' optional type (for 'F', 's' and 'i')
60 *
61 */
62
63 typedef struct term_cmd_t {
64 const char *name;
65 const char *args_type;
66 void (*handler)();
67 const char *params;
68 const char *help;
69 } term_cmd_t;
70
71 static term_cmd_t term_cmds[];
72 static term_cmd_t info_cmds[];
73
74 void term_printf(const char *fmt, ...)
75 {
76 va_list ap;
77 va_start(ap, fmt);
78 vprintf(fmt, ap);
79 va_end(ap);
80 }
81
82 void term_flush(void)
83 {
84 fflush(stdout);
85 }
86
87 static int compare_cmd(const char *name, const char *list)
88 {
89 const char *p, *pstart;
90 int len;
91 len = strlen(name);
92 p = list;
93 for(;;) {
94 pstart = p;
95 p = strchr(p, '|');
96 if (!p)
97 p = pstart + strlen(pstart);
98 if ((p - pstart) == len && !memcmp(pstart, name, len))
99 return 1;
100 if (*p == '\0')
101 break;
102 p++;
103 }
104 return 0;
105 }
106
107 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
108 {
109 term_cmd_t *cmd;
110
111 for(cmd = cmds; cmd->name != NULL; cmd++) {
112 if (!name || !strcmp(name, cmd->name))
113 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
114 }
115 }
116
117 static void help_cmd(const char *name)
118 {
119 if (name && !strcmp(name, "info")) {
120 help_cmd1(info_cmds, "info ", NULL);
121 } else {
122 help_cmd1(term_cmds, "", name);
123 if (name && !strcmp(name, "log")) {
124 CPULogItem *item;
125 term_printf("Log items (comma separated):\n");
126 term_printf("%-10s %s\n", "none", "remove all logs");
127 for(item = cpu_log_items; item->mask != 0; item++) {
128 term_printf("%-10s %s\n", item->name, item->help);
129 }
130 }
131 }
132 }
133
134 static void do_help(const char *name)
135 {
136 help_cmd(name);
137 }
138
139 static void do_commit(void)
140 {
141 int i;
142
143 for (i = 0; i < MAX_DISKS; i++) {
144 if (bs_table[i])
145 bdrv_commit(bs_table[i]);
146 }
147 }
148
149 static void do_info(const char *item)
150 {
151 term_cmd_t *cmd;
152
153 if (!item)
154 goto help;
155 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
156 if (compare_cmd(item, cmd->name))
157 goto found;
158 }
159 help:
160 help_cmd("info");
161 return;
162 found:
163 cmd->handler();
164 }
165
166 static void do_info_network(void)
167 {
168 int i, j;
169 NetDriverState *nd;
170
171 for(i = 0; i < nb_nics; i++) {
172 nd = &nd_table[i];
173 term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
174 for(j = 0; j < 6; j++) {
175 if (j > 0)
176 term_printf(":");
177 term_printf("%02x", nd->macaddr[j]);
178 }
179 term_printf("\n");
180 }
181 }
182
183 static void do_info_block(void)
184 {
185 bdrv_info();
186 }
187
188 static void do_info_registers(void)
189 {
190 #ifdef TARGET_I386
191 cpu_dump_state(cpu_single_env, stdout, X86_DUMP_FPU | X86_DUMP_CCOP);
192 #else
193 cpu_dump_state(cpu_single_env, stdout, 0);
194 #endif
195 }
196
197 static void do_info_history (void)
198 {
199 int i;
200
201 for (i = 0; i < TERM_MAX_CMDS; i++) {
202 if (term_history[i] == NULL)
203 break;
204 term_printf("%d: '%s'\n", i, term_history[i]);
205 }
206 }
207
208 static void do_quit(void)
209 {
210 exit(0);
211 }
212
213 static int eject_device(BlockDriverState *bs, int force)
214 {
215 if (bdrv_is_inserted(bs)) {
216 if (!force) {
217 if (!bdrv_is_removable(bs)) {
218 term_printf("device is not removable\n");
219 return -1;
220 }
221 if (bdrv_is_locked(bs)) {
222 term_printf("device is locked\n");
223 return -1;
224 }
225 }
226 bdrv_close(bs);
227 }
228 return 0;
229 }
230
231 static void do_eject(int force, const char *filename)
232 {
233 BlockDriverState *bs;
234
235 term_printf("%d %s\n", force, filename);
236
237 bs = bdrv_find(filename);
238 if (!bs) {
239 term_printf("device not found\n");
240 return;
241 }
242 eject_device(bs, force);
243 }
244
245 static void do_change(const char *device, const char *filename)
246 {
247 BlockDriverState *bs;
248
249 bs = bdrv_find(device);
250 if (!bs) {
251 term_printf("device not found\n");
252 return;
253 }
254 if (eject_device(bs, 0) < 0)
255 return;
256 bdrv_open(bs, filename, 0);
257 }
258
259 static void do_screen_dump(const char *filename)
260 {
261 vga_screen_dump(filename);
262 }
263
264 static void do_log(const char *items)
265 {
266 int mask;
267
268 if (!strcmp(items, "none")) {
269 mask = 0;
270 } else {
271 mask = cpu_str_to_log_mask(items);
272 if (!mask) {
273 help_cmd("log");
274 return;
275 }
276 }
277 cpu_set_log(mask);
278 }
279
280 static void do_savevm(const char *filename)
281 {
282 if (qemu_savevm(filename) < 0)
283 term_printf("I/O error when saving VM to '%s'\n", filename);
284 }
285
286 static void do_loadvm(const char *filename)
287 {
288 if (qemu_loadvm(filename) < 0)
289 term_printf("I/O error when loading VM from '%s'\n", filename);
290 }
291
292 static void do_stop(void)
293 {
294 vm_stop(EXCP_INTERRUPT);
295 }
296
297 static void do_cont(void)
298 {
299 vm_start();
300 }
301
302 #ifdef CONFIG_GDBSTUB
303 static void do_gdbserver(int has_port, int port)
304 {
305 if (!has_port)
306 port = DEFAULT_GDBSTUB_PORT;
307 if (gdbserver_start(port) < 0) {
308 qemu_printf("Could not open gdbserver socket on port %d\n", port);
309 } else {
310 qemu_printf("Waiting gdb connection on port %d\n", port);
311 }
312 }
313 #endif
314
315 static void term_printc(int c)
316 {
317 term_printf("'");
318 switch(c) {
319 case '\'':
320 term_printf("\\'");
321 break;
322 case '\\':
323 term_printf("\\\\");
324 break;
325 case '\n':
326 term_printf("\\n");
327 break;
328 case '\r':
329 term_printf("\\r");
330 break;
331 default:
332 if (c >= 32 && c <= 126) {
333 term_printf("%c", c);
334 } else {
335 term_printf("\\x%02x", c);
336 }
337 break;
338 }
339 term_printf("'");
340 }
341
342 static void memory_dump(int count, int format, int wsize,
343 target_ulong addr, int is_physical)
344 {
345 int nb_per_line, l, line_size, i, max_digits, len;
346 uint8_t buf[16];
347 uint64_t v;
348
349 if (format == 'i') {
350 int flags;
351 flags = 0;
352 #ifdef TARGET_I386
353 if (wsize == 2) {
354 flags = 1;
355 } else if (wsize == 4) {
356 flags = 0;
357 } else {
358 /* as default we use the current CS size */
359 flags = 0;
360 if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
361 flags = 1;
362 }
363 #endif
364 monitor_disas(addr, count, is_physical, flags);
365 return;
366 }
367
368 len = wsize * count;
369 if (wsize == 1)
370 line_size = 8;
371 else
372 line_size = 16;
373 nb_per_line = line_size / wsize;
374 max_digits = 0;
375
376 switch(format) {
377 case 'o':
378 max_digits = (wsize * 8 + 2) / 3;
379 break;
380 default:
381 case 'x':
382 max_digits = (wsize * 8) / 4;
383 break;
384 case 'u':
385 case 'd':
386 max_digits = (wsize * 8 * 10 + 32) / 33;
387 break;
388 case 'c':
389 wsize = 1;
390 break;
391 }
392
393 while (len > 0) {
394 term_printf("0x%08x:", addr);
395 l = len;
396 if (l > line_size)
397 l = line_size;
398 if (is_physical) {
399 cpu_physical_memory_rw(addr, buf, l, 0);
400 } else {
401 cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
402 }
403 i = 0;
404 while (i < l) {
405 switch(wsize) {
406 default:
407 case 1:
408 v = ldub_raw(buf + i);
409 break;
410 case 2:
411 v = lduw_raw(buf + i);
412 break;
413 case 4:
414 v = ldl_raw(buf + i);
415 break;
416 case 8:
417 v = ldq_raw(buf + i);
418 break;
419 }
420 term_printf(" ");
421 switch(format) {
422 case 'o':
423 term_printf("%#*llo", max_digits, v);
424 break;
425 case 'x':
426 term_printf("0x%0*llx", max_digits, v);
427 break;
428 case 'u':
429 term_printf("%*llu", max_digits, v);
430 break;
431 case 'd':
432 term_printf("%*lld", max_digits, v);
433 break;
434 case 'c':
435 term_printc(v);
436 break;
437 }
438 i += wsize;
439 }
440 term_printf("\n");
441 addr += l;
442 len -= l;
443 }
444 }
445
446 static void do_memory_dump(int count, int format, int size, int addr)
447 {
448 memory_dump(count, format, size, addr, 0);
449 }
450
451 static void do_physical_memory_dump(int count, int format, int size, int addr)
452 {
453 memory_dump(count, format, size, addr, 1);
454 }
455
456 static void do_print(int count, int format, int size, int val)
457 {
458 switch(format) {
459 case 'o':
460 term_printf("%#o", val);
461 break;
462 case 'x':
463 term_printf("%#x", val);
464 break;
465 case 'u':
466 term_printf("%u", val);
467 break;
468 default:
469 case 'd':
470 term_printf("%d", val);
471 break;
472 case 'c':
473 term_printc(val);
474 break;
475 }
476 term_printf("\n");
477 }
478
479 static term_cmd_t term_cmds[] = {
480 { "help|?", "s?", do_help,
481 "[cmd]", "show the help" },
482 { "commit", "", do_commit,
483 "", "commit changes to the disk images (if -snapshot is used)" },
484 { "info", "s?", do_info,
485 "subcommand", "show various information about the system state" },
486 { "q|quit", "", do_quit,
487 "", "quit the emulator" },
488 { "eject", "-fs", do_eject,
489 "[-f] device", "eject a removable media (use -f to force it)" },
490 { "change", "sF", do_change,
491 "device filename", "change a removable media" },
492 { "screendump", "F", do_screen_dump,
493 "filename", "save screen into PPM image 'filename'" },
494 { "log", "s", do_log,
495 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
496 { "savevm", "F", do_savevm,
497 "filename", "save the whole virtual machine state to 'filename'" },
498 { "loadvm", "F", do_loadvm,
499 "filename", "restore the whole virtual machine state from 'filename'" },
500 { "stop", "", do_stop,
501 "", "stop emulation", },
502 { "c|cont", "", do_cont,
503 "", "resume emulation", },
504 #ifdef CONFIG_GDBSTUB
505 { "gdbserver", "i?", do_gdbserver,
506 "[port]", "start gdbserver session (default port=1234)", },
507 #endif
508 { "x", "/i", do_memory_dump,
509 "/fmt addr", "virtual memory dump starting at 'addr'", },
510 { "xp", "/i", do_physical_memory_dump,
511 "/fmt addr", "physical memory dump starting at 'addr'", },
512 { "p|print", "/i", do_print,
513 "/fmt expr", "print expression value (use $reg for CPU register access)", },
514 { NULL, NULL, },
515 };
516
517 static term_cmd_t info_cmds[] = {
518 { "network", "", do_info_network,
519 "", "show the network state" },
520 { "block", "", do_info_block,
521 "", "show the block devices" },
522 { "registers", "", do_info_registers,
523 "", "show the cpu registers" },
524 { "history", "", do_info_history,
525 "", "show the command line history", },
526 { "pic", "", pic_info,
527 "", "show i8259 (PIC) state", },
528 { NULL, NULL, },
529 };
530
531 /*******************************************************************/
532
533 static const char *pch;
534 static jmp_buf expr_env;
535
536 typedef struct MonitorDef {
537 const char *name;
538 int offset;
539 int (*get_value)(struct MonitorDef *md);
540 } MonitorDef;
541
542 #if defined(TARGET_PPC)
543 static int monitor_get_ccr (struct MonitorDef *md)
544 {
545 unsigned int u;
546 int i;
547
548 u = 0;
549 for (i = 0; i < 8; i++)
550 u |= cpu_single_env->crf[i] << (32 - (4 * i));
551
552 return u;
553 }
554
555 static int monitor_get_msr (struct MonitorDef *md)
556 {
557 return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
558 (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
559 (cpu_single_env->msr[MSR_EE] << MSR_EE) |
560 (cpu_single_env->msr[MSR_PR] << MSR_PR) |
561 (cpu_single_env->msr[MSR_FP] << MSR_FP) |
562 (cpu_single_env->msr[MSR_ME] << MSR_ME) |
563 (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
564 (cpu_single_env->msr[MSR_SE] << MSR_SE) |
565 (cpu_single_env->msr[MSR_BE] << MSR_BE) |
566 (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
567 (cpu_single_env->msr[MSR_IP] << MSR_IP) |
568 (cpu_single_env->msr[MSR_IR] << MSR_IR) |
569 (cpu_single_env->msr[MSR_DR] << MSR_DR) |
570 (cpu_single_env->msr[MSR_RI] << MSR_RI) |
571 (cpu_single_env->msr[MSR_LE] << MSR_LE);
572 }
573
574 static int monitor_get_xer (struct MonitorDef *md)
575 {
576 return (cpu_single_env->xer[XER_SO] << XER_SO) |
577 (cpu_single_env->xer[XER_OV] << XER_OV) |
578 (cpu_single_env->xer[XER_CA] << XER_CA) |
579 (cpu_single_env->xer[XER_BC] << XER_BC);
580 }
581 #endif
582
583 static MonitorDef monitor_defs[] = {
584 #ifdef TARGET_I386
585 { "eax", offsetof(CPUState, regs[0]) },
586 { "ecx", offsetof(CPUState, regs[1]) },
587 { "edx", offsetof(CPUState, regs[2]) },
588 { "ebx", offsetof(CPUState, regs[3]) },
589 { "esp|sp", offsetof(CPUState, regs[4]) },
590 { "ebp|fp", offsetof(CPUState, regs[5]) },
591 { "esi", offsetof(CPUState, regs[6]) },
592 { "esi", offsetof(CPUState, regs[7]) },
593 { "eflags", offsetof(CPUState, eflags) },
594 { "eip|pc", offsetof(CPUState, eip) },
595 #elif defined(TARGET_PPC)
596 { "r0", offsetof(CPUState, gpr[0]) },
597 { "r1", offsetof(CPUState, gpr[1]) },
598 { "r2", offsetof(CPUState, gpr[2]) },
599 { "r3", offsetof(CPUState, gpr[3]) },
600 { "r4", offsetof(CPUState, gpr[4]) },
601 { "r5", offsetof(CPUState, gpr[5]) },
602 { "r6", offsetof(CPUState, gpr[6]) },
603 { "r7", offsetof(CPUState, gpr[7]) },
604 { "r8", offsetof(CPUState, gpr[8]) },
605 { "r9", offsetof(CPUState, gpr[9]) },
606 { "r10", offsetof(CPUState, gpr[10]) },
607 { "r11", offsetof(CPUState, gpr[11]) },
608 { "r12", offsetof(CPUState, gpr[12]) },
609 { "r13", offsetof(CPUState, gpr[13]) },
610 { "r14", offsetof(CPUState, gpr[14]) },
611 { "r15", offsetof(CPUState, gpr[15]) },
612 { "r16", offsetof(CPUState, gpr[16]) },
613 { "r17", offsetof(CPUState, gpr[17]) },
614 { "r18", offsetof(CPUState, gpr[18]) },
615 { "r19", offsetof(CPUState, gpr[19]) },
616 { "r20", offsetof(CPUState, gpr[20]) },
617 { "r21", offsetof(CPUState, gpr[21]) },
618 { "r22", offsetof(CPUState, gpr[22]) },
619 { "r23", offsetof(CPUState, gpr[23]) },
620 { "r24", offsetof(CPUState, gpr[24]) },
621 { "r25", offsetof(CPUState, gpr[25]) },
622 { "r26", offsetof(CPUState, gpr[26]) },
623 { "r27", offsetof(CPUState, gpr[27]) },
624 { "r28", offsetof(CPUState, gpr[28]) },
625 { "r29", offsetof(CPUState, gpr[29]) },
626 { "r30", offsetof(CPUState, gpr[30]) },
627 { "r31", offsetof(CPUState, gpr[31]) },
628 { "lr", offsetof(CPUState, lr) },
629 { "ctr", offsetof(CPUState, ctr) },
630 { "decr", offsetof(CPUState, decr) },
631 { "ccr", 0, &monitor_get_ccr, },
632 { "msr", 0, &monitor_get_msr, },
633 { "xer", 0, &monitor_get_xer, },
634 { "tbu", offsetof(CPUState, tb[0]) },
635 { "tbl", offsetof(CPUState, tb[1]) },
636 { "sdr1", offsetof(CPUState, sdr1) },
637 { "sr0", offsetof(CPUState, sr[0]) },
638 { "sr1", offsetof(CPUState, sr[1]) },
639 { "sr2", offsetof(CPUState, sr[2]) },
640 { "sr3", offsetof(CPUState, sr[3]) },
641 { "sr4", offsetof(CPUState, sr[4]) },
642 { "sr5", offsetof(CPUState, sr[5]) },
643 { "sr6", offsetof(CPUState, sr[6]) },
644 { "sr7", offsetof(CPUState, sr[7]) },
645 { "sr8", offsetof(CPUState, sr[8]) },
646 { "sr9", offsetof(CPUState, sr[9]) },
647 { "sr10", offsetof(CPUState, sr[10]) },
648 { "sr11", offsetof(CPUState, sr[11]) },
649 { "sr12", offsetof(CPUState, sr[12]) },
650 { "sr13", offsetof(CPUState, sr[13]) },
651 { "sr14", offsetof(CPUState, sr[14]) },
652 { "sr15", offsetof(CPUState, sr[15]) },
653 /* Too lazy to put BATs and SPRs ... */
654 #endif
655 { NULL },
656 };
657
658 static void expr_error(const char *fmt)
659 {
660 term_printf(fmt);
661 term_printf("\n");
662 longjmp(expr_env, 1);
663 }
664
665 static int get_monitor_def(int *pval, const char *name)
666 {
667 MonitorDef *md;
668 for(md = monitor_defs; md->name != NULL; md++) {
669 if (compare_cmd(name, md->name)) {
670 if (md->get_value) {
671 *pval = md->get_value(md);
672 } else {
673 *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
674 }
675 return 0;
676 }
677 }
678 return -1;
679 }
680
681 static void next(void)
682 {
683 if (pch != '\0') {
684 pch++;
685 while (isspace(*pch))
686 pch++;
687 }
688 }
689
690 static int expr_sum(void);
691
692 static int expr_unary(void)
693 {
694 int n;
695 char *p;
696
697 switch(*pch) {
698 case '+':
699 next();
700 n = expr_unary();
701 break;
702 case '-':
703 next();
704 n = -expr_unary();
705 break;
706 case '~':
707 next();
708 n = ~expr_unary();
709 break;
710 case '(':
711 next();
712 n = expr_sum();
713 if (*pch != ')') {
714 expr_error("')' expected");
715 }
716 next();
717 break;
718 case '$':
719 {
720 char buf[128], *q;
721
722 pch++;
723 q = buf;
724 while ((*pch >= 'a' && *pch <= 'z') ||
725 (*pch >= 'A' && *pch <= 'Z') ||
726 (*pch >= '0' && *pch <= '9') ||
727 *pch == '_') {
728 if ((q - buf) < sizeof(buf) - 1)
729 *q++ = *pch;
730 pch++;
731 }
732 while (isspace(*pch))
733 pch++;
734 *q = 0;
735 if (get_monitor_def(&n, buf))
736 expr_error("unknown register");
737 }
738 break;
739 case '\0':
740 expr_error("unexpected end of expression");
741 n = 0;
742 break;
743 default:
744 n = strtoul(pch, &p, 0);
745 if (pch == p) {
746 expr_error("invalid char in expression");
747 }
748 pch = p;
749 while (isspace(*pch))
750 pch++;
751 break;
752 }
753 return n;
754 }
755
756
757 static int expr_prod(void)
758 {
759 int val, val2, op;
760
761 val = expr_unary();
762 for(;;) {
763 op = *pch;
764 if (op != '*' && op != '/' && op != '%')
765 break;
766 next();
767 val2 = expr_unary();
768 switch(op) {
769 default:
770 case '*':
771 val *= val2;
772 break;
773 case '/':
774 case '%':
775 if (val2 == 0)
776 expr_error("divison by zero");
777 if (op == '/')
778 val /= val2;
779 else
780 val %= val2;
781 break;
782 }
783 }
784 return val;
785 }
786
787 static int expr_logic(void)
788 {
789 int val, val2, op;
790
791 val = expr_prod();
792 for(;;) {
793 op = *pch;
794 if (op != '&' && op != '|' && op != '^')
795 break;
796 next();
797 val2 = expr_prod();
798 switch(op) {
799 default:
800 case '&':
801 val &= val2;
802 break;
803 case '|':
804 val |= val2;
805 break;
806 case '^':
807 val ^= val2;
808 break;
809 }
810 }
811 return val;
812 }
813
814 static int expr_sum(void)
815 {
816 int val, val2, op;
817
818 val = expr_logic();
819 for(;;) {
820 op = *pch;
821 if (op != '+' && op != '-')
822 break;
823 next();
824 val2 = expr_logic();
825 if (op == '+')
826 val += val2;
827 else
828 val -= val2;
829 }
830 return val;
831 }
832
833 static int get_expr(int *pval, const char **pp)
834 {
835 pch = *pp;
836 if (setjmp(expr_env)) {
837 *pp = pch;
838 return -1;
839 }
840 while (isspace(*pch))
841 pch++;
842 *pval = expr_sum();
843 *pp = pch;
844 return 0;
845 }
846
847 static int get_str(char *buf, int buf_size, const char **pp)
848 {
849 const char *p;
850 char *q;
851 int c;
852
853 p = *pp;
854 while (isspace(*p))
855 p++;
856 if (*p == '\0') {
857 fail:
858 *pp = p;
859 return -1;
860 }
861 q = buf;
862 if (*p == '\"') {
863 p++;
864 while (*p != '\0' && *p != '\"') {
865 if (*p == '\\') {
866 p++;
867 c = *p++;
868 switch(c) {
869 case 'n':
870 c = '\n';
871 break;
872 case 'r':
873 c = '\r';
874 break;
875 case '\\':
876 case '\'':
877 case '\"':
878 break;
879 default:
880 qemu_printf("unsupported escape code: '\\%c'\n", c);
881 goto fail;
882 }
883 if ((q - buf) < buf_size - 1) {
884 *q++ = c;
885 }
886 } else {
887 if ((q - buf) < buf_size - 1) {
888 *q++ = *p;
889 }
890 p++;
891 }
892 }
893 if (*p != '\"') {
894 qemu_printf("untermintated string\n");
895 goto fail;
896 }
897 p++;
898 } else {
899 while (*p != '\0' && !isspace(*p)) {
900 if ((q - buf) < buf_size - 1) {
901 *q++ = *p;
902 }
903 p++;
904 }
905 *q = '\0';
906 }
907 *pp = p;
908 return 0;
909 }
910
911 static int default_fmt_format = 'x';
912 static int default_fmt_size = 4;
913
914 #define MAX_ARGS 16
915
916 static void term_handle_command(const char *cmdline)
917 {
918 const char *p, *pstart, *typestr;
919 char *q;
920 int c, nb_args, len, i, has_arg;
921 term_cmd_t *cmd;
922 char cmdname[256];
923 char buf[1024];
924 void *str_allocated[MAX_ARGS];
925 void *args[MAX_ARGS];
926
927 #ifdef DEBUG
928 term_printf("command='%s'\n", cmdline);
929 #endif
930
931 /* extract the command name */
932 p = cmdline;
933 q = cmdname;
934 while (isspace(*p))
935 p++;
936 if (*p == '\0')
937 return;
938 pstart = p;
939 while (*p != '\0' && *p != '/' && !isspace(*p))
940 p++;
941 len = p - pstart;
942 if (len > sizeof(cmdname) - 1)
943 len = sizeof(cmdname) - 1;
944 memcpy(cmdname, pstart, len);
945 cmdname[len] = '\0';
946
947 /* find the command */
948 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
949 if (compare_cmd(cmdname, cmd->name))
950 goto found;
951 }
952 term_printf("unknown command: '%s'\n", cmdname);
953 return;
954 found:
955
956 for(i = 0; i < MAX_ARGS; i++)
957 str_allocated[i] = NULL;
958
959 /* parse the parameters */
960 typestr = cmd->args_type;
961 nb_args = 0;
962 for(;;) {
963 c = *typestr;
964 if (c == '\0')
965 break;
966 typestr++;
967 switch(c) {
968 case 'F':
969 case 's':
970 {
971 int ret;
972 char *str;
973
974 while (isspace(*p))
975 p++;
976 if (*typestr == '?') {
977 typestr++;
978 if (*p == '\0') {
979 /* no optional string: NULL argument */
980 str = NULL;
981 goto add_str;
982 }
983 }
984 ret = get_str(buf, sizeof(buf), &p);
985 if (ret < 0) {
986 if (c == 'F')
987 term_printf("%s: filename expected\n", cmdname);
988 else
989 term_printf("%s: string expected\n", cmdname);
990 goto fail;
991 }
992 str = qemu_malloc(strlen(buf) + 1);
993 strcpy(str, buf);
994 str_allocated[nb_args] = str;
995 add_str:
996 if (nb_args >= MAX_ARGS) {
997 error_args:
998 term_printf("%s: too many arguments\n", cmdname);
999 goto fail;
1000 }
1001 args[nb_args++] = str;
1002 }
1003 break;
1004 case '/':
1005 {
1006 int count, format, size;
1007
1008 while (isspace(*p))
1009 p++;
1010 if (*p == '/') {
1011 /* format found */
1012 p++;
1013 count = 1;
1014 if (isdigit(*p)) {
1015 count = 0;
1016 while (isdigit(*p)) {
1017 count = count * 10 + (*p - '0');
1018 p++;
1019 }
1020 }
1021 size = -1;
1022 format = -1;
1023 for(;;) {
1024 switch(*p) {
1025 case 'o':
1026 case 'd':
1027 case 'u':
1028 case 'x':
1029 case 'i':
1030 case 'c':
1031 format = *p++;
1032 break;
1033 case 'b':
1034 size = 1;
1035 p++;
1036 break;
1037 case 'h':
1038 size = 2;
1039 p++;
1040 break;
1041 case 'w':
1042 size = 4;
1043 p++;
1044 break;
1045 case 'g':
1046 case 'L':
1047 size = 8;
1048 p++;
1049 break;
1050 default:
1051 goto next;
1052 }
1053 }
1054 next:
1055 if (*p != '\0' && !isspace(*p)) {
1056 term_printf("invalid char in format: '%c'\n", *p);
1057 goto fail;
1058 }
1059 if (format < 0)
1060 format = default_fmt_format;
1061 if (format != 'i') {
1062 /* for 'i', not specifying a size gives -1 as size */
1063 if (size < 0)
1064 size = default_fmt_size;
1065 }
1066 default_fmt_size = size;
1067 default_fmt_format = format;
1068 } else {
1069 count = 1;
1070 format = default_fmt_format;
1071 if (format != 'i') {
1072 size = default_fmt_size;
1073 } else {
1074 size = -1;
1075 }
1076 }
1077 if (nb_args + 3 > MAX_ARGS)
1078 goto error_args;
1079 args[nb_args++] = (void*)count;
1080 args[nb_args++] = (void*)format;
1081 args[nb_args++] = (void*)size;
1082 }
1083 break;
1084 case 'i':
1085 {
1086 int val;
1087 while (isspace(*p))
1088 p++;
1089 if (*typestr == '?') {
1090 typestr++;
1091 if (*p == '\0')
1092 has_arg = 0;
1093 else
1094 has_arg = 1;
1095 if (nb_args >= MAX_ARGS)
1096 goto error_args;
1097 args[nb_args++] = (void *)has_arg;
1098 if (!has_arg) {
1099 if (nb_args >= MAX_ARGS)
1100 goto error_args;
1101 val = -1;
1102 goto add_num;
1103 }
1104 }
1105 if (get_expr(&val, &p))
1106 goto fail;
1107 add_num:
1108 if (nb_args >= MAX_ARGS)
1109 goto error_args;
1110 args[nb_args++] = (void *)val;
1111 }
1112 break;
1113 case '-':
1114 {
1115 int has_option;
1116 /* option */
1117
1118 c = *typestr++;
1119 if (c == '\0')
1120 goto bad_type;
1121 while (isspace(*p))
1122 p++;
1123 has_option = 0;
1124 if (*p == '-') {
1125 p++;
1126 if (*p != c) {
1127 term_printf("%s: unsupported option -%c\n",
1128 cmdname, *p);
1129 goto fail;
1130 }
1131 p++;
1132 has_option = 1;
1133 }
1134 if (nb_args >= MAX_ARGS)
1135 goto error_args;
1136 args[nb_args++] = (void *)has_option;
1137 }
1138 break;
1139 default:
1140 bad_type:
1141 term_printf("%s: unknown type '%c'\n", cmdname, c);
1142 goto fail;
1143 }
1144 }
1145 /* check that all arguments were parsed */
1146 while (isspace(*p))
1147 p++;
1148 if (*p != '\0') {
1149 term_printf("%s: extraneous characters at the end of line\n",
1150 cmdname);
1151 goto fail;
1152 }
1153
1154 switch(nb_args) {
1155 case 0:
1156 cmd->handler();
1157 break;
1158 case 1:
1159 cmd->handler(args[0]);
1160 break;
1161 case 2:
1162 cmd->handler(args[0], args[1]);
1163 break;
1164 case 3:
1165 cmd->handler(args[0], args[1], args[2]);
1166 break;
1167 case 4:
1168 cmd->handler(args[0], args[1], args[2], args[3]);
1169 break;
1170 case 5:
1171 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1172 break;
1173 default:
1174 term_printf("unsupported number of arguments: %d\n", nb_args);
1175 goto fail;
1176 }
1177 fail:
1178 for(i = 0; i < MAX_ARGS; i++)
1179 qemu_free(str_allocated[i]);
1180 return;
1181 }
1182
1183 static void term_show_prompt(void)
1184 {
1185 term_printf("(qemu) ");
1186 fflush(stdout);
1187 term_cmd_buf_index = 0;
1188 term_cmd_buf_size = 0;
1189 term_esc_state = IS_NORM;
1190 }
1191
1192 static void term_print_cmdline (const char *cmdline)
1193 {
1194 term_show_prompt();
1195 term_printf(cmdline);
1196 term_flush();
1197 }
1198
1199 static void term_insert_char(int ch)
1200 {
1201 if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) {
1202 memmove(term_cmd_buf + term_cmd_buf_index + 1,
1203 term_cmd_buf + term_cmd_buf_index,
1204 term_cmd_buf_size - term_cmd_buf_index);
1205 term_cmd_buf[term_cmd_buf_index] = ch;
1206 term_cmd_buf_size++;
1207 term_printf("\033[@%c", ch);
1208 term_cmd_buf_index++;
1209 term_flush();
1210 }
1211 }
1212
1213 static void term_backward_char(void)
1214 {
1215 if (term_cmd_buf_index > 0) {
1216 term_cmd_buf_index--;
1217 term_printf("\033[D");
1218 term_flush();
1219 }
1220 }
1221
1222 static void term_forward_char(void)
1223 {
1224 if (term_cmd_buf_index < term_cmd_buf_size) {
1225 term_cmd_buf_index++;
1226 term_printf("\033[C");
1227 term_flush();
1228 }
1229 }
1230
1231 static void term_delete_char(void)
1232 {
1233 if (term_cmd_buf_index < term_cmd_buf_size) {
1234 memmove(term_cmd_buf + term_cmd_buf_index,
1235 term_cmd_buf + term_cmd_buf_index + 1,
1236 term_cmd_buf_size - term_cmd_buf_index - 1);
1237 term_printf("\033[P");
1238 term_cmd_buf_size--;
1239 term_flush();
1240 }
1241 }
1242
1243 static void term_backspace(void)
1244 {
1245 if (term_cmd_buf_index > 0) {
1246 term_backward_char();
1247 term_delete_char();
1248 }
1249 }
1250
1251 static void term_bol(void)
1252 {
1253 while (term_cmd_buf_index > 0)
1254 term_backward_char();
1255 }
1256
1257 static void term_eol(void)
1258 {
1259 while (term_cmd_buf_index < term_cmd_buf_size)
1260 term_forward_char();
1261 }
1262
1263 static void term_up_char(void)
1264 {
1265 int idx;
1266
1267 if (term_hist_entry == 0)
1268 return;
1269 if (term_hist_entry == -1) {
1270 /* Find latest entry */
1271 for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1272 if (term_history[idx] == NULL)
1273 break;
1274 }
1275 term_hist_entry = idx;
1276 }
1277 term_hist_entry--;
1278 if (term_hist_entry >= 0) {
1279 strcpy(term_cmd_buf, term_history[term_hist_entry]);
1280 term_printf("\n");
1281 term_print_cmdline(term_cmd_buf);
1282 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1283 }
1284 }
1285
1286 static void term_down_char(void)
1287 {
1288 if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1)
1289 return;
1290 if (term_history[++term_hist_entry] != NULL) {
1291 strcpy(term_cmd_buf, term_history[term_hist_entry]);
1292 } else {
1293 term_hist_entry = -1;
1294 }
1295 term_printf("\n");
1296 term_print_cmdline(term_cmd_buf);
1297 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1298 }
1299
1300 static void term_hist_add(const char *cmdline)
1301 {
1302 char *hist_entry, *new_entry;
1303 int idx;
1304
1305 if (cmdline[0] == '\0')
1306 return;
1307 new_entry = NULL;
1308 if (term_hist_entry != -1) {
1309 /* We were editing an existing history entry: replace it */
1310 hist_entry = term_history[term_hist_entry];
1311 idx = term_hist_entry;
1312 if (strcmp(hist_entry, cmdline) == 0) {
1313 goto same_entry;
1314 }
1315 }
1316 /* Search cmdline in history buffers */
1317 for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1318 hist_entry = term_history[idx];
1319 if (hist_entry == NULL)
1320 break;
1321 if (strcmp(hist_entry, cmdline) == 0) {
1322 same_entry:
1323 new_entry = hist_entry;
1324 /* Put this entry at the end of history */
1325 memmove(&term_history[idx], &term_history[idx + 1],
1326 &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]);
1327 term_history[TERM_MAX_CMDS - 1] = NULL;
1328 for (; idx < TERM_MAX_CMDS; idx++) {
1329 if (term_history[idx] == NULL)
1330 break;
1331 }
1332 break;
1333 }
1334 }
1335 if (idx == TERM_MAX_CMDS) {
1336 /* Need to get one free slot */
1337 free(term_history[0]);
1338 memcpy(term_history, &term_history[1],
1339 &term_history[TERM_MAX_CMDS] - &term_history[1]);
1340 term_history[TERM_MAX_CMDS - 1] = NULL;
1341 idx = TERM_MAX_CMDS - 1;
1342 }
1343 if (new_entry == NULL)
1344 new_entry = strdup(cmdline);
1345 term_history[idx] = new_entry;
1346 term_hist_entry = -1;
1347 }
1348
1349 /* return true if command handled */
1350 static void term_handle_byte(int ch)
1351 {
1352 switch(term_esc_state) {
1353 case IS_NORM:
1354 switch(ch) {
1355 case 1:
1356 term_bol();
1357 break;
1358 case 5:
1359 term_eol();
1360 break;
1361 case 10:
1362 case 13:
1363 term_cmd_buf[term_cmd_buf_size] = '\0';
1364 term_hist_add(term_cmd_buf);
1365 term_printf("\n");
1366 term_handle_command(term_cmd_buf);
1367 term_show_prompt();
1368 break;
1369 case 27:
1370 term_esc_state = IS_ESC;
1371 break;
1372 case 127:
1373 case 8:
1374 term_backspace();
1375 break;
1376 case 155:
1377 term_esc_state = IS_CSI;
1378 break;
1379 default:
1380 if (ch >= 32) {
1381 term_insert_char(ch);
1382 }
1383 break;
1384 }
1385 break;
1386 case IS_ESC:
1387 if (ch == '[') {
1388 term_esc_state = IS_CSI;
1389 term_esc_param = 0;
1390 } else {
1391 term_esc_state = IS_NORM;
1392 }
1393 break;
1394 case IS_CSI:
1395 switch(ch) {
1396 case 'A':
1397 case 'F':
1398 term_up_char();
1399 break;
1400 case 'B':
1401 case 'E':
1402 term_down_char();
1403 break;
1404 case 'D':
1405 term_backward_char();
1406 break;
1407 case 'C':
1408 term_forward_char();
1409 break;
1410 case '0' ... '9':
1411 term_esc_param = term_esc_param * 10 + (ch - '0');
1412 goto the_end;
1413 case '~':
1414 switch(term_esc_param) {
1415 case 1:
1416 term_bol();
1417 break;
1418 case 3:
1419 term_delete_char();
1420 break;
1421 case 4:
1422 term_eol();
1423 break;
1424 }
1425 break;
1426 default:
1427 break;
1428 }
1429 term_esc_state = IS_NORM;
1430 the_end:
1431 break;
1432 }
1433 }
1434
1435 /*************************************************************/
1436 /* serial console support */
1437
1438 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1439
1440 static int term_got_escape, term_command;
1441
1442 void term_print_help(void)
1443 {
1444 term_printf("\n"
1445 "C-a h print this help\n"
1446 "C-a x exit emulatior\n"
1447 "C-a s save disk data back to file (if -snapshot)\n"
1448 "C-a b send break (magic sysrq)\n"
1449 "C-a c switch between console and monitor\n"
1450 "C-a C-a send C-a\n"
1451 );
1452 }
1453
1454 /* called when a char is received */
1455 static void term_received_byte(int ch)
1456 {
1457 if (!serial_console) {
1458 /* if no serial console, handle every command */
1459 term_handle_byte(ch);
1460 } else {
1461 if (term_got_escape) {
1462 term_got_escape = 0;
1463 switch(ch) {
1464 case 'h':
1465 term_print_help();
1466 break;
1467 case 'x':
1468 exit(0);
1469 break;
1470 case 's':
1471 {
1472 int i;
1473 for (i = 0; i < MAX_DISKS; i++) {
1474 if (bs_table[i])
1475 bdrv_commit(bs_table[i]);
1476 }
1477 }
1478 break;
1479 case 'b':
1480 if (serial_console)
1481 serial_receive_break(serial_console);
1482 break;
1483 case 'c':
1484 if (!term_command) {
1485 term_show_prompt();
1486 term_command = 1;
1487 } else {
1488 term_command = 0;
1489 }
1490 break;
1491 case TERM_ESCAPE:
1492 goto send_char;
1493 }
1494 } else if (ch == TERM_ESCAPE) {
1495 term_got_escape = 1;
1496 } else {
1497 send_char:
1498 if (term_command) {
1499 term_handle_byte(ch);
1500 } else {
1501 if (serial_console)
1502 serial_receive_byte(serial_console, ch);
1503 }
1504 }
1505 }
1506 }
1507
1508 static int term_can_read(void *opaque)
1509 {
1510 if (serial_console) {
1511 return serial_can_receive(serial_console);
1512 } else {
1513 return 128;
1514 }
1515 }
1516
1517 static void term_read(void *opaque, const uint8_t *buf, int size)
1518 {
1519 int i;
1520 for(i = 0; i < size; i++)
1521 term_received_byte(buf[i]);
1522 }
1523
1524 void monitor_init(void)
1525 {
1526 if (!serial_console) {
1527 term_printf("QEMU %s monitor - type 'help' for more information\n",
1528 QEMU_VERSION);
1529 term_show_prompt();
1530 }
1531 term_hist_entry = -1;
1532 qemu_add_fd_read_handler(0, term_can_read, term_read, NULL);
1533 }