]> Git Repo - qemu.git/blob - monitor.c
PowerPC merge
[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     { "irq", "", irq_info,
527       "", "show the interrupts statistics (if available)", },
528     { "pic", "", pic_info,
529       "", "show i8259 (PIC) state", },
530     { "pci", "", pci_info,
531       "", "show PCI info", },
532     { NULL, NULL, },
533 };
534
535 /*******************************************************************/
536
537 static const char *pch;
538 static jmp_buf expr_env;
539
540 typedef struct MonitorDef {
541     const char *name;
542     int offset;
543     int (*get_value)(struct MonitorDef *md);
544 } MonitorDef;
545
546 #if defined(TARGET_I386)
547 static int monitor_get_pc (struct MonitorDef *md)
548 {
549     return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base;
550 }
551 #endif
552
553 #if defined(TARGET_PPC)
554 static int monitor_get_ccr (struct MonitorDef *md)
555 {
556     unsigned int u;
557     int i;
558
559     u = 0;
560     for (i = 0; i < 8; i++)
561         u |= cpu_single_env->crf[i] << (32 - (4 * i));
562
563     return u;
564 }
565
566 static int monitor_get_msr (struct MonitorDef *md)
567 {
568     return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
569         (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
570         (cpu_single_env->msr[MSR_EE] << MSR_EE) |
571         (cpu_single_env->msr[MSR_PR] << MSR_PR) |
572         (cpu_single_env->msr[MSR_FP] << MSR_FP) |
573         (cpu_single_env->msr[MSR_ME] << MSR_ME) |
574         (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
575         (cpu_single_env->msr[MSR_SE] << MSR_SE) |
576         (cpu_single_env->msr[MSR_BE] << MSR_BE) |
577         (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
578         (cpu_single_env->msr[MSR_IP] << MSR_IP) |
579         (cpu_single_env->msr[MSR_IR] << MSR_IR) |
580         (cpu_single_env->msr[MSR_DR] << MSR_DR) |
581         (cpu_single_env->msr[MSR_RI] << MSR_RI) |
582         (cpu_single_env->msr[MSR_LE] << MSR_LE);
583 }
584
585 static int monitor_get_xer (struct MonitorDef *md)
586 {
587     return (cpu_single_env->xer[XER_SO] << XER_SO) |
588         (cpu_single_env->xer[XER_OV] << XER_OV) |
589         (cpu_single_env->xer[XER_CA] << XER_CA) |
590         (cpu_single_env->xer[XER_BC] << XER_BC);
591 }
592
593 uint32_t cpu_ppc_load_decr (CPUState *env);
594 static int monitor_get_decr (struct MonitorDef *md)
595 {
596     return cpu_ppc_load_decr(cpu_single_env);
597 }
598
599 uint32_t cpu_ppc_load_tbu (CPUState *env);
600 static int monitor_get_tbu (struct MonitorDef *md)
601 {
602     return cpu_ppc_load_tbu(cpu_single_env);
603 }
604
605 uint32_t cpu_ppc_load_tbl (CPUState *env);
606 static int monitor_get_tbl (struct MonitorDef *md)
607 {
608     return cpu_ppc_load_tbl(cpu_single_env);
609 }
610 #endif
611
612 static MonitorDef monitor_defs[] = {
613 #ifdef TARGET_I386
614
615 #define SEG(name, seg) \
616     { name, offsetof(CPUState, segs[seg].selector) },\
617     { name ".base", offsetof(CPUState, segs[seg].base) },\
618     { name ".limit", offsetof(CPUState, segs[seg].limit) },
619
620     { "eax", offsetof(CPUState, regs[0]) },
621     { "ecx", offsetof(CPUState, regs[1]) },
622     { "edx", offsetof(CPUState, regs[2]) },
623     { "ebx", offsetof(CPUState, regs[3]) },
624     { "esp|sp", offsetof(CPUState, regs[4]) },
625     { "ebp|fp", offsetof(CPUState, regs[5]) },
626     { "esi", offsetof(CPUState, regs[6]) },
627     { "esi", offsetof(CPUState, regs[7]) },
628     { "eflags", offsetof(CPUState, eflags) },
629     { "eip", offsetof(CPUState, eip) },
630     SEG("cs", R_CS)
631     SEG("ds", R_DS)
632     SEG("es", R_ES)
633     SEG("fs", R_FS)
634     SEG("gs", R_GS)
635     { "pc", 0, monitor_get_pc, },
636 #elif defined(TARGET_PPC)
637     { "r0", offsetof(CPUState, gpr[0]) },
638     { "r1", offsetof(CPUState, gpr[1]) },
639     { "r2", offsetof(CPUState, gpr[2]) },
640     { "r3", offsetof(CPUState, gpr[3]) },
641     { "r4", offsetof(CPUState, gpr[4]) },
642     { "r5", offsetof(CPUState, gpr[5]) },
643     { "r6", offsetof(CPUState, gpr[6]) },
644     { "r7", offsetof(CPUState, gpr[7]) },
645     { "r8", offsetof(CPUState, gpr[8]) },
646     { "r9", offsetof(CPUState, gpr[9]) },
647     { "r10", offsetof(CPUState, gpr[10]) },
648     { "r11", offsetof(CPUState, gpr[11]) },
649     { "r12", offsetof(CPUState, gpr[12]) },
650     { "r13", offsetof(CPUState, gpr[13]) },
651     { "r14", offsetof(CPUState, gpr[14]) },
652     { "r15", offsetof(CPUState, gpr[15]) },
653     { "r16", offsetof(CPUState, gpr[16]) },
654     { "r17", offsetof(CPUState, gpr[17]) },
655     { "r18", offsetof(CPUState, gpr[18]) },
656     { "r19", offsetof(CPUState, gpr[19]) },
657     { "r20", offsetof(CPUState, gpr[20]) },
658     { "r21", offsetof(CPUState, gpr[21]) },
659     { "r22", offsetof(CPUState, gpr[22]) },
660     { "r23", offsetof(CPUState, gpr[23]) },
661     { "r24", offsetof(CPUState, gpr[24]) },
662     { "r25", offsetof(CPUState, gpr[25]) },
663     { "r26", offsetof(CPUState, gpr[26]) },
664     { "r27", offsetof(CPUState, gpr[27]) },
665     { "r28", offsetof(CPUState, gpr[28]) },
666     { "r29", offsetof(CPUState, gpr[29]) },
667     { "r30", offsetof(CPUState, gpr[30]) },
668     { "r31", offsetof(CPUState, gpr[31]) },
669     { "nip|pc", offsetof(CPUState, nip) },
670     { "lr", offsetof(CPUState, lr) },
671     { "ctr", offsetof(CPUState, ctr) },
672     { "decr", 0, &monitor_get_decr, },
673     { "ccr", 0, &monitor_get_ccr, },
674     { "msr", 0, &monitor_get_msr, },
675     { "xer", 0, &monitor_get_xer, },
676     { "tbu", 0, &monitor_get_tbu, },
677     { "tbl", 0, &monitor_get_tbl, },
678     { "sdr1", offsetof(CPUState, sdr1) },
679     { "sr0", offsetof(CPUState, sr[0]) },
680     { "sr1", offsetof(CPUState, sr[1]) },
681     { "sr2", offsetof(CPUState, sr[2]) },
682     { "sr3", offsetof(CPUState, sr[3]) },
683     { "sr4", offsetof(CPUState, sr[4]) },
684     { "sr5", offsetof(CPUState, sr[5]) },
685     { "sr6", offsetof(CPUState, sr[6]) },
686     { "sr7", offsetof(CPUState, sr[7]) },
687     { "sr8", offsetof(CPUState, sr[8]) },
688     { "sr9", offsetof(CPUState, sr[9]) },
689     { "sr10", offsetof(CPUState, sr[10]) },
690     { "sr11", offsetof(CPUState, sr[11]) },
691     { "sr12", offsetof(CPUState, sr[12]) },
692     { "sr13", offsetof(CPUState, sr[13]) },
693     { "sr14", offsetof(CPUState, sr[14]) },
694     { "sr15", offsetof(CPUState, sr[15]) },
695     /* Too lazy to put BATs and SPRs ... */
696 #endif
697     { NULL },
698 };
699
700 static void expr_error(const char *fmt) 
701 {
702     term_printf(fmt);
703     term_printf("\n");
704     longjmp(expr_env, 1);
705 }
706
707 static int get_monitor_def(int *pval, const char *name)
708 {
709     MonitorDef *md;
710     for(md = monitor_defs; md->name != NULL; md++) {
711         if (compare_cmd(name, md->name)) {
712             if (md->get_value) {
713                 *pval = md->get_value(md);
714             } else {
715                 *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
716             }
717             return 0;
718         }
719     }
720     return -1;
721 }
722
723 static void next(void)
724 {
725     if (pch != '\0') {
726         pch++;
727         while (isspace(*pch))
728             pch++;
729     }
730 }
731
732 static int expr_sum(void);
733
734 static int expr_unary(void)
735 {
736     int n;
737     char *p;
738
739     switch(*pch) {
740     case '+':
741         next();
742         n = expr_unary();
743         break;
744     case '-':
745         next();
746         n = -expr_unary();
747         break;
748     case '~':
749         next();
750         n = ~expr_unary();
751         break;
752     case '(':
753         next();
754         n = expr_sum();
755         if (*pch != ')') {
756             expr_error("')' expected");
757         }
758         next();
759         break;
760     case '$':
761         {
762             char buf[128], *q;
763             
764             pch++;
765             q = buf;
766             while ((*pch >= 'a' && *pch <= 'z') ||
767                    (*pch >= 'A' && *pch <= 'Z') ||
768                    (*pch >= '0' && *pch <= '9') ||
769                    *pch == '_' || *pch == '.') {
770                 if ((q - buf) < sizeof(buf) - 1)
771                     *q++ = *pch;
772                 pch++;
773             }
774             while (isspace(*pch))
775                 pch++;
776             *q = 0;
777             if (get_monitor_def(&n, buf))
778                 expr_error("unknown register");
779         }
780         break;
781     case '\0':
782         expr_error("unexpected end of expression");
783         n = 0;
784         break;
785     default:
786         n = strtoul(pch, &p, 0);
787         if (pch == p) {
788             expr_error("invalid char in expression");
789         }
790         pch = p;
791         while (isspace(*pch))
792             pch++;
793         break;
794     }
795     return n;
796 }
797
798
799 static int expr_prod(void)
800 {
801     int val, val2, op;
802
803     val = expr_unary();
804     for(;;) {
805         op = *pch;
806         if (op != '*' && op != '/' && op != '%')
807             break;
808         next();
809         val2 = expr_unary();
810         switch(op) {
811         default:
812         case '*':
813             val *= val2;
814             break;
815         case '/':
816         case '%':
817             if (val2 == 0) 
818                 expr_error("division by zero");
819             if (op == '/')
820                 val /= val2;
821             else
822                 val %= val2;
823             break;
824         }
825     }
826     return val;
827 }
828
829 static int expr_logic(void)
830 {
831     int val, val2, op;
832
833     val = expr_prod();
834     for(;;) {
835         op = *pch;
836         if (op != '&' && op != '|' && op != '^')
837             break;
838         next();
839         val2 = expr_prod();
840         switch(op) {
841         default:
842         case '&':
843             val &= val2;
844             break;
845         case '|':
846             val |= val2;
847             break;
848         case '^':
849             val ^= val2;
850             break;
851         }
852     }
853     return val;
854 }
855
856 static int expr_sum(void)
857 {
858     int val, val2, op;
859
860     val = expr_logic();
861     for(;;) {
862         op = *pch;
863         if (op != '+' && op != '-')
864             break;
865         next();
866         val2 = expr_logic();
867         if (op == '+')
868             val += val2;
869         else
870             val -= val2;
871     }
872     return val;
873 }
874
875 static int get_expr(int *pval, const char **pp)
876 {
877     pch = *pp;
878     if (setjmp(expr_env)) {
879         *pp = pch;
880         return -1;
881     }
882     while (isspace(*pch))
883         pch++;
884     *pval = expr_sum();
885     *pp = pch;
886     return 0;
887 }
888
889 static int get_str(char *buf, int buf_size, const char **pp)
890 {
891     const char *p;
892     char *q;
893     int c;
894
895     p = *pp;
896     while (isspace(*p))
897         p++;
898     if (*p == '\0') {
899     fail:
900         *pp = p;
901         return -1;
902     }
903     q = buf;
904     if (*p == '\"') {
905         p++;
906         while (*p != '\0' && *p != '\"') {
907             if (*p == '\\') {
908                 p++;
909                 c = *p++;
910                 switch(c) {
911                 case 'n':
912                     c = '\n';
913                     break;
914                 case 'r':
915                     c = '\r';
916                     break;
917                 case '\\':
918                 case '\'':
919                 case '\"':
920                     break;
921                 default:
922                     qemu_printf("unsupported escape code: '\\%c'\n", c);
923                     goto fail;
924                 }
925                 if ((q - buf) < buf_size - 1) {
926                     *q++ = c;
927                 }
928             } else {
929                 if ((q - buf) < buf_size - 1) {
930                     *q++ = *p;
931                 }
932                 p++;
933             }
934         }
935         if (*p != '\"') {
936             qemu_printf("unterminated string\n");
937             goto fail;
938         }
939         p++;
940     } else {
941         while (*p != '\0' && !isspace(*p)) {
942             if ((q - buf) < buf_size - 1) {
943                 *q++ = *p;
944             }
945             p++;
946         }
947         *q = '\0';
948     }
949     *pp = p;
950     return 0;
951 }
952
953 static int default_fmt_format = 'x';
954 static int default_fmt_size = 4;
955
956 #define MAX_ARGS 16
957
958 static void term_handle_command(const char *cmdline)
959 {
960     const char *p, *pstart, *typestr;
961     char *q;
962     int c, nb_args, len, i, has_arg;
963     term_cmd_t *cmd;
964     char cmdname[256];
965     char buf[1024];
966     void *str_allocated[MAX_ARGS];
967     void *args[MAX_ARGS];
968
969 #ifdef DEBUG
970     term_printf("command='%s'\n", cmdline);
971 #endif
972     
973     /* extract the command name */
974     p = cmdline;
975     q = cmdname;
976     while (isspace(*p))
977         p++;
978     if (*p == '\0')
979         return;
980     pstart = p;
981     while (*p != '\0' && *p != '/' && !isspace(*p))
982         p++;
983     len = p - pstart;
984     if (len > sizeof(cmdname) - 1)
985         len = sizeof(cmdname) - 1;
986     memcpy(cmdname, pstart, len);
987     cmdname[len] = '\0';
988     
989     /* find the command */
990     for(cmd = term_cmds; cmd->name != NULL; cmd++) {
991         if (compare_cmd(cmdname, cmd->name)) 
992             goto found;
993     }
994     term_printf("unknown command: '%s'\n", cmdname);
995     return;
996  found:
997
998     for(i = 0; i < MAX_ARGS; i++)
999         str_allocated[i] = NULL;
1000     
1001     /* parse the parameters */
1002     typestr = cmd->args_type;
1003     nb_args = 0;
1004     for(;;) {
1005         c = *typestr;
1006         if (c == '\0')
1007             break;
1008         typestr++;
1009         switch(c) {
1010         case 'F':
1011         case 's':
1012             {
1013                 int ret;
1014                 char *str;
1015                 
1016                 while (isspace(*p)) 
1017                     p++;
1018                 if (*typestr == '?') {
1019                     typestr++;
1020                     if (*p == '\0') {
1021                         /* no optional string: NULL argument */
1022                         str = NULL;
1023                         goto add_str;
1024                     }
1025                 }
1026                 ret = get_str(buf, sizeof(buf), &p);
1027                 if (ret < 0) {
1028                     if (c == 'F')
1029                         term_printf("%s: filename expected\n", cmdname);
1030                     else
1031                         term_printf("%s: string expected\n", cmdname);
1032                     goto fail;
1033                 }
1034                 str = qemu_malloc(strlen(buf) + 1);
1035                 strcpy(str, buf);
1036                 str_allocated[nb_args] = str;
1037             add_str:
1038                 if (nb_args >= MAX_ARGS) {
1039                 error_args:
1040                     term_printf("%s: too many arguments\n", cmdname);
1041                     goto fail;
1042                 }
1043                 args[nb_args++] = str;
1044             }
1045             break;
1046         case '/':
1047             {
1048                 int count, format, size;
1049                 
1050                 while (isspace(*p))
1051                     p++;
1052                 if (*p == '/') {
1053                     /* format found */
1054                     p++;
1055                     count = 1;
1056                     if (isdigit(*p)) {
1057                         count = 0;
1058                         while (isdigit(*p)) {
1059                             count = count * 10 + (*p - '0');
1060                             p++;
1061                         }
1062                     }
1063                     size = -1;
1064                     format = -1;
1065                     for(;;) {
1066                         switch(*p) {
1067                         case 'o':
1068                         case 'd':
1069                         case 'u':
1070                         case 'x':
1071                         case 'i':
1072                         case 'c':
1073                             format = *p++;
1074                             break;
1075                         case 'b':
1076                             size = 1;
1077                             p++;
1078                             break;
1079                         case 'h':
1080                             size = 2;
1081                             p++;
1082                             break;
1083                         case 'w':
1084                             size = 4;
1085                             p++;
1086                             break;
1087                         case 'g':
1088                         case 'L':
1089                             size = 8;
1090                             p++;
1091                             break;
1092                         default:
1093                             goto next;
1094                         }
1095                     }
1096                 next:
1097                     if (*p != '\0' && !isspace(*p)) {
1098                         term_printf("invalid char in format: '%c'\n", *p);
1099                         goto fail;
1100                     }
1101                     if (format < 0)
1102                         format = default_fmt_format;
1103                     if (format != 'i') {
1104                         /* for 'i', not specifying a size gives -1 as size */
1105                         if (size < 0)
1106                             size = default_fmt_size;
1107                     }
1108                     default_fmt_size = size;
1109                     default_fmt_format = format;
1110                 } else {
1111                     count = 1;
1112                     format = default_fmt_format;
1113                     if (format != 'i') {
1114                         size = default_fmt_size;
1115                     } else {
1116                         size = -1;
1117                     }
1118                 }
1119                 if (nb_args + 3 > MAX_ARGS)
1120                     goto error_args;
1121                 args[nb_args++] = (void*)count;
1122                 args[nb_args++] = (void*)format;
1123                 args[nb_args++] = (void*)size;
1124             }
1125             break;
1126         case 'i':
1127             {
1128                 int val;
1129                 while (isspace(*p)) 
1130                     p++;
1131                 if (*typestr == '?') {
1132                     typestr++;
1133                     if (*p == '\0')
1134                         has_arg = 0;
1135                     else
1136                         has_arg = 1;
1137                     if (nb_args >= MAX_ARGS)
1138                         goto error_args;
1139                     args[nb_args++] = (void *)has_arg;
1140                     if (!has_arg) {
1141                         if (nb_args >= MAX_ARGS)
1142                             goto error_args;
1143                         val = -1;
1144                         goto add_num;
1145                     }
1146                 }
1147                 if (get_expr(&val, &p))
1148                     goto fail;
1149             add_num:
1150                 if (nb_args >= MAX_ARGS)
1151                     goto error_args;
1152                 args[nb_args++] = (void *)val;
1153             }
1154             break;
1155         case '-':
1156             {
1157                 int has_option;
1158                 /* option */
1159                 
1160                 c = *typestr++;
1161                 if (c == '\0')
1162                     goto bad_type;
1163                 while (isspace(*p)) 
1164                     p++;
1165                 has_option = 0;
1166                 if (*p == '-') {
1167                     p++;
1168                     if (*p != c) {
1169                         term_printf("%s: unsupported option -%c\n", 
1170                                     cmdname, *p);
1171                         goto fail;
1172                     }
1173                     p++;
1174                     has_option = 1;
1175                 }
1176                 if (nb_args >= MAX_ARGS)
1177                     goto error_args;
1178                 args[nb_args++] = (void *)has_option;
1179             }
1180             break;
1181         default:
1182         bad_type:
1183             term_printf("%s: unknown type '%c'\n", cmdname, c);
1184             goto fail;
1185         }
1186     }
1187     /* check that all arguments were parsed */
1188     while (isspace(*p))
1189         p++;
1190     if (*p != '\0') {
1191         term_printf("%s: extraneous characters at the end of line\n", 
1192                     cmdname);
1193         goto fail;
1194     }
1195
1196     switch(nb_args) {
1197     case 0:
1198         cmd->handler();
1199         break;
1200     case 1:
1201         cmd->handler(args[0]);
1202         break;
1203     case 2:
1204         cmd->handler(args[0], args[1]);
1205         break;
1206     case 3:
1207         cmd->handler(args[0], args[1], args[2]);
1208         break;
1209     case 4:
1210         cmd->handler(args[0], args[1], args[2], args[3]);
1211         break;
1212     case 5:
1213         cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1214         break;
1215     default:
1216         term_printf("unsupported number of arguments: %d\n", nb_args);
1217         goto fail;
1218     }
1219  fail:
1220     for(i = 0; i < MAX_ARGS; i++)
1221         qemu_free(str_allocated[i]);
1222     return;
1223 }
1224
1225 static void term_show_prompt(void)
1226 {
1227     term_printf("(qemu) ");
1228     fflush(stdout);
1229     term_cmd_buf_index = 0;
1230     term_cmd_buf_size = 0;
1231     term_esc_state = IS_NORM;
1232 }
1233
1234 static void term_print_cmdline (const char *cmdline)
1235 {
1236     term_show_prompt();
1237     term_printf(cmdline);
1238     term_flush();
1239 }
1240
1241 static void term_insert_char(int ch)
1242 {
1243     if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) {
1244         memmove(term_cmd_buf + term_cmd_buf_index + 1,
1245                 term_cmd_buf + term_cmd_buf_index,
1246                 term_cmd_buf_size - term_cmd_buf_index);
1247         term_cmd_buf[term_cmd_buf_index] = ch;
1248         term_cmd_buf_size++;
1249         term_printf("\033[@%c", ch);
1250         term_cmd_buf_index++;
1251         term_flush();
1252     }
1253 }
1254
1255 static void term_backward_char(void)
1256 {
1257     if (term_cmd_buf_index > 0) {
1258         term_cmd_buf_index--;
1259         term_printf("\033[D");
1260         term_flush();
1261     }
1262 }
1263
1264 static void term_forward_char(void)
1265 {
1266     if (term_cmd_buf_index < term_cmd_buf_size) {
1267         term_cmd_buf_index++;
1268         term_printf("\033[C");
1269         term_flush();
1270     }
1271 }
1272
1273 static void term_delete_char(void)
1274 {
1275     if (term_cmd_buf_index < term_cmd_buf_size) {
1276         memmove(term_cmd_buf + term_cmd_buf_index,
1277                 term_cmd_buf + term_cmd_buf_index + 1,
1278                 term_cmd_buf_size - term_cmd_buf_index - 1);
1279         term_printf("\033[P");
1280         term_cmd_buf_size--;
1281         term_flush();
1282     }
1283 }
1284
1285 static void term_backspace(void)
1286 {
1287     if (term_cmd_buf_index > 0) {
1288         term_backward_char();
1289         term_delete_char();
1290     }
1291 }
1292
1293 static void term_bol(void)
1294 {
1295     while (term_cmd_buf_index > 0)
1296         term_backward_char();
1297 }
1298
1299 static void term_eol(void)
1300 {
1301     while (term_cmd_buf_index < term_cmd_buf_size)
1302         term_forward_char();
1303 }
1304
1305 static void term_up_char(void)
1306 {
1307     int idx;
1308
1309     if (term_hist_entry == 0)
1310         return;
1311     if (term_hist_entry == -1) {
1312         /* Find latest entry */
1313         for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1314             if (term_history[idx] == NULL)
1315                 break;
1316         }
1317         term_hist_entry = idx;
1318     }
1319     term_hist_entry--;
1320     if (term_hist_entry >= 0) {
1321         strcpy(term_cmd_buf, term_history[term_hist_entry]);
1322         term_printf("\n");
1323         term_print_cmdline(term_cmd_buf);
1324         term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1325     }
1326 }
1327
1328 static void term_down_char(void)
1329 {
1330     if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1)
1331         return;
1332     if (term_history[++term_hist_entry] != NULL) {
1333         strcpy(term_cmd_buf, term_history[term_hist_entry]);
1334     } else {
1335         term_hist_entry = -1;
1336     }
1337     term_printf("\n");
1338     term_print_cmdline(term_cmd_buf);
1339     term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1340 }
1341
1342 static void term_hist_add(const char *cmdline)
1343 {
1344     char *hist_entry, *new_entry;
1345     int idx;
1346
1347     if (cmdline[0] == '\0')
1348         return;
1349     new_entry = NULL;
1350     if (term_hist_entry != -1) {
1351         /* We were editing an existing history entry: replace it */
1352         hist_entry = term_history[term_hist_entry];
1353         idx = term_hist_entry;
1354         if (strcmp(hist_entry, cmdline) == 0) {
1355             goto same_entry;
1356         }
1357     }
1358     /* Search cmdline in history buffers */
1359     for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1360         hist_entry = term_history[idx];
1361         if (hist_entry == NULL)
1362             break;
1363         if (strcmp(hist_entry, cmdline) == 0) {
1364         same_entry:
1365             new_entry = hist_entry;
1366             /* Put this entry at the end of history */
1367             memmove(&term_history[idx], &term_history[idx + 1],
1368                     &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]);
1369             term_history[TERM_MAX_CMDS - 1] = NULL;
1370             for (; idx < TERM_MAX_CMDS; idx++) {
1371                 if (term_history[idx] == NULL)
1372                     break;
1373             }
1374             break;
1375         }
1376     }
1377     if (idx == TERM_MAX_CMDS) {
1378         /* Need to get one free slot */
1379         free(term_history[0]);
1380         memcpy(term_history, &term_history[1],
1381                &term_history[TERM_MAX_CMDS] - &term_history[1]);
1382         term_history[TERM_MAX_CMDS - 1] = NULL;
1383         idx = TERM_MAX_CMDS - 1;
1384     }
1385     if (new_entry == NULL)
1386         new_entry = strdup(cmdline);
1387     term_history[idx] = new_entry;
1388     term_hist_entry = -1;
1389 }
1390
1391 /* return true if command handled */
1392 static void term_handle_byte(int ch)
1393 {
1394     switch(term_esc_state) {
1395     case IS_NORM:
1396         switch(ch) {
1397         case 1:
1398             term_bol();
1399             break;
1400         case 5:
1401             term_eol();
1402             break;
1403         case 10:
1404         case 13:
1405             term_cmd_buf[term_cmd_buf_size] = '\0';
1406             term_hist_add(term_cmd_buf);
1407             term_printf("\n");
1408             term_handle_command(term_cmd_buf);
1409             term_show_prompt();
1410             break;
1411         case 27:
1412             term_esc_state = IS_ESC;
1413             break;
1414         case 127:
1415         case 8:
1416             term_backspace();
1417             break;
1418         case 155:
1419             term_esc_state = IS_CSI;
1420             break;
1421         default:
1422             if (ch >= 32) {
1423                 term_insert_char(ch);
1424             }
1425             break;
1426         }
1427         break;
1428     case IS_ESC:
1429         if (ch == '[') {
1430             term_esc_state = IS_CSI;
1431             term_esc_param = 0;
1432         } else {
1433             term_esc_state = IS_NORM;
1434         }
1435         break;
1436     case IS_CSI:
1437         switch(ch) {
1438         case 'A':
1439         case 'F':
1440             term_up_char();
1441             break;
1442         case 'B':
1443         case 'E':
1444             term_down_char();
1445             break;
1446         case 'D':
1447             term_backward_char();
1448             break;
1449         case 'C':
1450             term_forward_char();
1451             break;
1452         case '0' ... '9':
1453             term_esc_param = term_esc_param * 10 + (ch - '0');
1454             goto the_end;
1455         case '~':
1456             switch(term_esc_param) {
1457             case 1:
1458                 term_bol();
1459                 break;
1460             case 3:
1461                 term_delete_char();
1462                 break;
1463             case 4:
1464                 term_eol();
1465                 break;
1466             }
1467             break;
1468         default:
1469             break;
1470         }
1471         term_esc_state = IS_NORM;
1472     the_end:
1473         break;
1474     }
1475 }
1476
1477 /*************************************************************/
1478 /* serial console support */
1479
1480 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1481
1482 static int term_got_escape, term_command;
1483
1484 void term_print_help(void)
1485 {
1486     term_printf("\n"
1487                 "C-a h    print this help\n"
1488                 "C-a x    exit emulatior\n"
1489                 "C-a s    save disk data back to file (if -snapshot)\n"
1490                 "C-a b    send break (magic sysrq)\n"
1491                 "C-a c    switch between console and monitor\n"
1492                 "C-a C-a  send C-a\n"
1493                 );
1494 }
1495
1496 /* called when a char is received */
1497 static void term_received_byte(int ch)
1498 {
1499     if (!serial_console) {
1500         /* if no serial console, handle every command */
1501         term_handle_byte(ch);
1502     } else {
1503         if (term_got_escape) {
1504             term_got_escape = 0;
1505             switch(ch) {
1506             case 'h':
1507                 term_print_help();
1508                 break;
1509             case 'x':
1510                 exit(0);
1511                 break;
1512             case 's': 
1513                 {
1514                     int i;
1515                     for (i = 0; i < MAX_DISKS; i++) {
1516                         if (bs_table[i])
1517                             bdrv_commit(bs_table[i]);
1518                     }
1519                 }
1520                 break;
1521             case 'b':
1522                 if (serial_console)
1523                     serial_receive_break(serial_console);
1524                 break;
1525             case 'c':
1526                 if (!term_command) {
1527                     term_show_prompt();
1528                     term_command = 1;
1529                 } else {
1530                     term_command = 0;
1531                 }
1532                 break;
1533             case TERM_ESCAPE:
1534                 goto send_char;
1535             }
1536         } else if (ch == TERM_ESCAPE) {
1537             term_got_escape = 1;
1538         } else {
1539         send_char:
1540             if (term_command) {
1541                 term_handle_byte(ch);
1542             } else {
1543                 if (serial_console)
1544                     serial_receive_byte(serial_console, ch);
1545             }
1546         }
1547     }
1548 }
1549
1550 static int term_can_read(void *opaque)
1551 {
1552     if (serial_console) {
1553         return serial_can_receive(serial_console);
1554     } else {
1555         return 128;
1556     }
1557 }
1558
1559 static void term_read(void *opaque, const uint8_t *buf, int size)
1560 {
1561     int i;
1562     for(i = 0; i < size; i++)
1563         term_received_byte(buf[i]);
1564 }
1565
1566 void monitor_init(void)
1567 {
1568     if (!serial_console) {
1569         term_printf("QEMU %s monitor - type 'help' for more information\n",
1570                     QEMU_VERSION);
1571         term_show_prompt();
1572     }
1573     term_hist_entry = -1;
1574     qemu_add_fd_read_handler(0, term_can_read, term_read, NULL);
1575 }
This page took 0.133365 seconds and 4 git commands to generate.