root / monitor.c @ ffddfee3
History | View | Annotate | Download (43.9 kB)
1 | 9dc39cba | bellard | /*
|
---|---|---|---|
2 | 9dc39cba | bellard | * QEMU monitor
|
3 | 9dc39cba | bellard | *
|
4 | 9dc39cba | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 9dc39cba | bellard | *
|
6 | 9dc39cba | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 9dc39cba | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 9dc39cba | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 9dc39cba | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 9dc39cba | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 9dc39cba | bellard | * furnished to do so, subject to the following conditions:
|
12 | 9dc39cba | bellard | *
|
13 | 9dc39cba | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 9dc39cba | bellard | * all copies or substantial portions of the Software.
|
15 | 9dc39cba | bellard | *
|
16 | 9dc39cba | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 9dc39cba | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 9dc39cba | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 9dc39cba | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 9dc39cba | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 9dc39cba | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 9dc39cba | bellard | * THE SOFTWARE.
|
23 | 9dc39cba | bellard | */
|
24 | 9dc39cba | bellard | #include "vl.h" |
25 | 9307c4c1 | bellard | #include "disas.h" |
26 | 9dc39cba | bellard | |
27 | 9dc39cba | bellard | //#define DEBUG
|
28 | 9dc39cba | bellard | |
29 | 9307c4c1 | bellard | #ifndef offsetof
|
30 | 9307c4c1 | bellard | #define offsetof(type, field) ((size_t) &((type *)0)->field) |
31 | 9307c4c1 | bellard | #endif
|
32 | 9307c4c1 | bellard | |
33 | 9dc39cba | bellard | #define TERM_CMD_BUF_SIZE 4095 |
34 | aa455485 | bellard | #define TERM_MAX_CMDS 64 |
35 | 9dc39cba | bellard | |
36 | 9dc39cba | bellard | #define IS_NORM 0 |
37 | 9dc39cba | bellard | #define IS_ESC 1 |
38 | 9dc39cba | bellard | #define IS_CSI 2 |
39 | 9dc39cba | bellard | |
40 | 9dc39cba | bellard | #define printf do_not_use_printf
|
41 | 9dc39cba | bellard | |
42 | 9dc39cba | bellard | static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1]; |
43 | 9dc39cba | bellard | static int term_cmd_buf_index; |
44 | 9dc39cba | bellard | static int term_cmd_buf_size; |
45 | 9dc39cba | bellard | static int term_esc_state; |
46 | 9dc39cba | bellard | static int term_esc_param; |
47 | 9dc39cba | bellard | |
48 | aa455485 | bellard | static char *term_history[TERM_MAX_CMDS]; |
49 | aa455485 | bellard | static int term_hist_entry; |
50 | aa455485 | bellard | |
51 | 9307c4c1 | bellard | /*
|
52 | 9307c4c1 | bellard | * Supported types:
|
53 | 9307c4c1 | bellard | *
|
54 | 9307c4c1 | bellard | * 'F' filename
|
55 | 9307c4c1 | bellard | * 's' string (accept optional quote)
|
56 | 9307c4c1 | bellard | * 'i' integer
|
57 | 9307c4c1 | bellard | * '/' optional gdb-like print format (like "/10x")
|
58 | 9307c4c1 | bellard | *
|
59 | 9307c4c1 | bellard | * '?' optional type (for 'F', 's' and 'i')
|
60 | 9307c4c1 | bellard | *
|
61 | 9307c4c1 | bellard | */
|
62 | 9307c4c1 | bellard | |
63 | 9dc39cba | bellard | typedef struct term_cmd_t { |
64 | 9dc39cba | bellard | const char *name; |
65 | 9307c4c1 | bellard | const char *args_type; |
66 | 9307c4c1 | bellard | void (*handler)();
|
67 | 9dc39cba | bellard | const char *params; |
68 | 9dc39cba | bellard | const char *help; |
69 | 9dc39cba | bellard | } term_cmd_t; |
70 | 9dc39cba | bellard | |
71 | 9dc39cba | bellard | static term_cmd_t term_cmds[];
|
72 | 9dc39cba | bellard | static term_cmd_t info_cmds[];
|
73 | 9dc39cba | bellard | |
74 | 9dc39cba | bellard | void term_printf(const char *fmt, ...) |
75 | 9dc39cba | bellard | { |
76 | 9dc39cba | bellard | va_list ap; |
77 | 9dc39cba | bellard | va_start(ap, fmt); |
78 | 9dc39cba | bellard | vprintf(fmt, ap); |
79 | 9dc39cba | bellard | va_end(ap); |
80 | 9dc39cba | bellard | } |
81 | 9dc39cba | bellard | |
82 | 9dc39cba | bellard | void term_flush(void) |
83 | 9dc39cba | bellard | { |
84 | 9dc39cba | bellard | fflush(stdout); |
85 | 9dc39cba | bellard | } |
86 | 9dc39cba | bellard | |
87 | 9dc39cba | bellard | static int compare_cmd(const char *name, const char *list) |
88 | 9dc39cba | bellard | { |
89 | 9dc39cba | bellard | const char *p, *pstart; |
90 | 9dc39cba | bellard | int len;
|
91 | 9dc39cba | bellard | len = strlen(name); |
92 | 9dc39cba | bellard | p = list; |
93 | 9dc39cba | bellard | for(;;) {
|
94 | 9dc39cba | bellard | pstart = p; |
95 | 9dc39cba | bellard | p = strchr(p, '|');
|
96 | 9dc39cba | bellard | if (!p)
|
97 | 9dc39cba | bellard | p = pstart + strlen(pstart); |
98 | 9dc39cba | bellard | if ((p - pstart) == len && !memcmp(pstart, name, len))
|
99 | 9dc39cba | bellard | return 1; |
100 | 9dc39cba | bellard | if (*p == '\0') |
101 | 9dc39cba | bellard | break;
|
102 | 9dc39cba | bellard | p++; |
103 | 9dc39cba | bellard | } |
104 | 9dc39cba | bellard | return 0; |
105 | 9dc39cba | bellard | } |
106 | 9dc39cba | bellard | |
107 | 9dc39cba | bellard | static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name) |
108 | 9dc39cba | bellard | { |
109 | 9dc39cba | bellard | term_cmd_t *cmd; |
110 | 9dc39cba | bellard | |
111 | 9dc39cba | bellard | for(cmd = cmds; cmd->name != NULL; cmd++) { |
112 | 9dc39cba | bellard | if (!name || !strcmp(name, cmd->name))
|
113 | 9dc39cba | bellard | term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
|
114 | 9dc39cba | bellard | } |
115 | 9dc39cba | bellard | } |
116 | 9dc39cba | bellard | |
117 | 9dc39cba | bellard | static void help_cmd(const char *name) |
118 | 9dc39cba | bellard | { |
119 | 9dc39cba | bellard | if (name && !strcmp(name, "info")) { |
120 | 9dc39cba | bellard | help_cmd1(info_cmds, "info ", NULL); |
121 | 9dc39cba | bellard | } else {
|
122 | 9dc39cba | bellard | help_cmd1(term_cmds, "", name);
|
123 | f193c797 | bellard | if (name && !strcmp(name, "log")) { |
124 | f193c797 | bellard | CPULogItem *item; |
125 | f193c797 | bellard | term_printf("Log items (comma separated):\n");
|
126 | f193c797 | bellard | term_printf("%-10s %s\n", "none", "remove all logs"); |
127 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
128 | f193c797 | bellard | term_printf("%-10s %s\n", item->name, item->help);
|
129 | f193c797 | bellard | } |
130 | f193c797 | bellard | } |
131 | 9dc39cba | bellard | } |
132 | 9dc39cba | bellard | } |
133 | 9dc39cba | bellard | |
134 | 9307c4c1 | bellard | static void do_help(const char *name) |
135 | 9dc39cba | bellard | { |
136 | 9307c4c1 | bellard | help_cmd(name); |
137 | 9dc39cba | bellard | } |
138 | 9dc39cba | bellard | |
139 | 9307c4c1 | bellard | static void do_commit(void) |
140 | 9dc39cba | bellard | { |
141 | 9dc39cba | bellard | int i;
|
142 | 9dc39cba | bellard | |
143 | 9dc39cba | bellard | for (i = 0; i < MAX_DISKS; i++) { |
144 | 9dc39cba | bellard | if (bs_table[i])
|
145 | 9dc39cba | bellard | bdrv_commit(bs_table[i]); |
146 | 9dc39cba | bellard | } |
147 | 9dc39cba | bellard | } |
148 | 9dc39cba | bellard | |
149 | 9307c4c1 | bellard | static void do_info(const char *item) |
150 | 9dc39cba | bellard | { |
151 | 9dc39cba | bellard | term_cmd_t *cmd; |
152 | 9dc39cba | bellard | |
153 | 9307c4c1 | bellard | if (!item)
|
154 | 9dc39cba | bellard | goto help;
|
155 | 9dc39cba | bellard | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
156 | 9307c4c1 | bellard | if (compare_cmd(item, cmd->name))
|
157 | 9dc39cba | bellard | goto found;
|
158 | 9dc39cba | bellard | } |
159 | 9dc39cba | bellard | help:
|
160 | 9307c4c1 | bellard | help_cmd("info");
|
161 | 9dc39cba | bellard | return;
|
162 | 9dc39cba | bellard | found:
|
163 | 9307c4c1 | bellard | cmd->handler(); |
164 | 9dc39cba | bellard | } |
165 | 9dc39cba | bellard | |
166 | 9307c4c1 | bellard | static void do_info_network(void) |
167 | 9dc39cba | bellard | { |
168 | 9dc39cba | bellard | int i, j;
|
169 | 9dc39cba | bellard | NetDriverState *nd; |
170 | 9dc39cba | bellard | |
171 | 9dc39cba | bellard | for(i = 0; i < nb_nics; i++) { |
172 | 9dc39cba | bellard | nd = &nd_table[i]; |
173 | 9dc39cba | bellard | term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
|
174 | 9dc39cba | bellard | for(j = 0; j < 6; j++) { |
175 | 9dc39cba | bellard | if (j > 0) |
176 | 9dc39cba | bellard | term_printf(":");
|
177 | 9dc39cba | bellard | term_printf("%02x", nd->macaddr[j]);
|
178 | 9dc39cba | bellard | } |
179 | 9dc39cba | bellard | term_printf("\n");
|
180 | 9dc39cba | bellard | } |
181 | 9dc39cba | bellard | } |
182 | 9dc39cba | bellard | |
183 | 9307c4c1 | bellard | static void do_info_block(void) |
184 | 9dc39cba | bellard | { |
185 | 9dc39cba | bellard | bdrv_info(); |
186 | 9dc39cba | bellard | } |
187 | 9dc39cba | bellard | |
188 | 9307c4c1 | bellard | static void do_info_registers(void) |
189 | 9307c4c1 | bellard | { |
190 | 9307c4c1 | bellard | #ifdef TARGET_I386
|
191 | 9307c4c1 | bellard | cpu_dump_state(cpu_single_env, stdout, X86_DUMP_FPU | X86_DUMP_CCOP); |
192 | 9307c4c1 | bellard | #else
|
193 | 9307c4c1 | bellard | cpu_dump_state(cpu_single_env, stdout, 0);
|
194 | 9307c4c1 | bellard | #endif
|
195 | 9307c4c1 | bellard | } |
196 | 9307c4c1 | bellard | |
197 | aa455485 | bellard | static void do_info_history (void) |
198 | aa455485 | bellard | { |
199 | aa455485 | bellard | int i;
|
200 | aa455485 | bellard | |
201 | aa455485 | bellard | for (i = 0; i < TERM_MAX_CMDS; i++) { |
202 | aa455485 | bellard | if (term_history[i] == NULL) |
203 | aa455485 | bellard | break;
|
204 | aa455485 | bellard | term_printf("%d: '%s'\n", i, term_history[i]);
|
205 | aa455485 | bellard | } |
206 | aa455485 | bellard | } |
207 | aa455485 | bellard | |
208 | 9307c4c1 | bellard | static void do_quit(void) |
209 | 9dc39cba | bellard | { |
210 | 9dc39cba | bellard | exit(0);
|
211 | 9dc39cba | bellard | } |
212 | 9dc39cba | bellard | |
213 | 9dc39cba | bellard | static int eject_device(BlockDriverState *bs, int force) |
214 | 9dc39cba | bellard | { |
215 | 9dc39cba | bellard | if (bdrv_is_inserted(bs)) {
|
216 | 9dc39cba | bellard | if (!force) {
|
217 | 9dc39cba | bellard | if (!bdrv_is_removable(bs)) {
|
218 | 9dc39cba | bellard | term_printf("device is not removable\n");
|
219 | 9dc39cba | bellard | return -1; |
220 | 9dc39cba | bellard | } |
221 | 9dc39cba | bellard | if (bdrv_is_locked(bs)) {
|
222 | 9dc39cba | bellard | term_printf("device is locked\n");
|
223 | 9dc39cba | bellard | return -1; |
224 | 9dc39cba | bellard | } |
225 | 9dc39cba | bellard | } |
226 | 9dc39cba | bellard | bdrv_close(bs); |
227 | 9dc39cba | bellard | } |
228 | 9dc39cba | bellard | return 0; |
229 | 9dc39cba | bellard | } |
230 | 9dc39cba | bellard | |
231 | 9307c4c1 | bellard | static void do_eject(int force, const char *filename) |
232 | 9dc39cba | bellard | { |
233 | 9dc39cba | bellard | BlockDriverState *bs; |
234 | 9dc39cba | bellard | |
235 | 9307c4c1 | bellard | term_printf("%d %s\n", force, filename);
|
236 | 9307c4c1 | bellard | |
237 | 9307c4c1 | bellard | bs = bdrv_find(filename); |
238 | 9dc39cba | bellard | if (!bs) {
|
239 | 9dc39cba | bellard | term_printf("device not found\n");
|
240 | 9dc39cba | bellard | return;
|
241 | 9dc39cba | bellard | } |
242 | 9dc39cba | bellard | eject_device(bs, force); |
243 | 9dc39cba | bellard | } |
244 | 9dc39cba | bellard | |
245 | 9307c4c1 | bellard | static void do_change(const char *device, const char *filename) |
246 | 9dc39cba | bellard | { |
247 | 9dc39cba | bellard | BlockDriverState *bs; |
248 | 9dc39cba | bellard | |
249 | 9307c4c1 | bellard | bs = bdrv_find(device); |
250 | 9dc39cba | bellard | if (!bs) {
|
251 | 9dc39cba | bellard | term_printf("device not found\n");
|
252 | 9dc39cba | bellard | return;
|
253 | 9dc39cba | bellard | } |
254 | 9dc39cba | bellard | if (eject_device(bs, 0) < 0) |
255 | 9dc39cba | bellard | return;
|
256 | 9307c4c1 | bellard | bdrv_open(bs, filename, 0);
|
257 | 9dc39cba | bellard | } |
258 | 9dc39cba | bellard | |
259 | 9307c4c1 | bellard | static void do_screen_dump(const char *filename) |
260 | 59a983b9 | bellard | { |
261 | 9307c4c1 | bellard | vga_screen_dump(filename); |
262 | 59a983b9 | bellard | } |
263 | 59a983b9 | bellard | |
264 | 9307c4c1 | bellard | static void do_log(const char *items) |
265 | f193c797 | bellard | { |
266 | f193c797 | bellard | int mask;
|
267 | f193c797 | bellard | |
268 | 9307c4c1 | bellard | if (!strcmp(items, "none")) { |
269 | f193c797 | bellard | mask = 0;
|
270 | f193c797 | bellard | } else {
|
271 | 9307c4c1 | bellard | mask = cpu_str_to_log_mask(items); |
272 | f193c797 | bellard | if (!mask) {
|
273 | 9307c4c1 | bellard | help_cmd("log");
|
274 | f193c797 | bellard | return;
|
275 | f193c797 | bellard | } |
276 | f193c797 | bellard | } |
277 | f193c797 | bellard | cpu_set_log(mask); |
278 | f193c797 | bellard | } |
279 | f193c797 | bellard | |
280 | 9307c4c1 | bellard | static void do_savevm(const char *filename) |
281 | 8a7ddc38 | bellard | { |
282 | 9307c4c1 | bellard | if (qemu_savevm(filename) < 0) |
283 | 9307c4c1 | bellard | term_printf("I/O error when saving VM to '%s'\n", filename);
|
284 | 8a7ddc38 | bellard | } |
285 | 8a7ddc38 | bellard | |
286 | 9307c4c1 | bellard | static void do_loadvm(const char *filename) |
287 | 8a7ddc38 | bellard | { |
288 | 9307c4c1 | bellard | if (qemu_loadvm(filename) < 0) |
289 | 9307c4c1 | bellard | term_printf("I/O error when loading VM from '%s'\n", filename);
|
290 | 8a7ddc38 | bellard | } |
291 | 8a7ddc38 | bellard | |
292 | 9307c4c1 | bellard | static void do_stop(void) |
293 | 8a7ddc38 | bellard | { |
294 | 8a7ddc38 | bellard | vm_stop(EXCP_INTERRUPT); |
295 | 8a7ddc38 | bellard | } |
296 | 8a7ddc38 | bellard | |
297 | 9307c4c1 | bellard | static void do_cont(void) |
298 | 8a7ddc38 | bellard | { |
299 | 8a7ddc38 | bellard | vm_start(); |
300 | 8a7ddc38 | bellard | } |
301 | 8a7ddc38 | bellard | |
302 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
303 | 9307c4c1 | bellard | static void do_gdbserver(int has_port, int port) |
304 | 8a7ddc38 | bellard | { |
305 | 9307c4c1 | bellard | if (!has_port)
|
306 | 9307c4c1 | bellard | port = DEFAULT_GDBSTUB_PORT; |
307 | 8a7ddc38 | bellard | if (gdbserver_start(port) < 0) { |
308 | 8a7ddc38 | bellard | qemu_printf("Could not open gdbserver socket on port %d\n", port);
|
309 | 8a7ddc38 | bellard | } else {
|
310 | 8a7ddc38 | bellard | qemu_printf("Waiting gdb connection on port %d\n", port);
|
311 | 8a7ddc38 | bellard | } |
312 | 8a7ddc38 | bellard | } |
313 | 67b915a5 | bellard | #endif
|
314 | 8a7ddc38 | bellard | |
315 | 9307c4c1 | bellard | static void term_printc(int c) |
316 | 9307c4c1 | bellard | { |
317 | 9307c4c1 | bellard | term_printf("'");
|
318 | 9307c4c1 | bellard | switch(c) {
|
319 | 9307c4c1 | bellard | case '\'': |
320 | 9307c4c1 | bellard | term_printf("\\'");
|
321 | 9307c4c1 | bellard | break;
|
322 | 9307c4c1 | bellard | case '\\': |
323 | 9307c4c1 | bellard | term_printf("\\\\");
|
324 | 9307c4c1 | bellard | break;
|
325 | 9307c4c1 | bellard | case '\n': |
326 | 9307c4c1 | bellard | term_printf("\\n");
|
327 | 9307c4c1 | bellard | break;
|
328 | 9307c4c1 | bellard | case '\r': |
329 | 9307c4c1 | bellard | term_printf("\\r");
|
330 | 9307c4c1 | bellard | break;
|
331 | 9307c4c1 | bellard | default:
|
332 | 9307c4c1 | bellard | if (c >= 32 && c <= 126) { |
333 | 9307c4c1 | bellard | term_printf("%c", c);
|
334 | 9307c4c1 | bellard | } else {
|
335 | 9307c4c1 | bellard | term_printf("\\x%02x", c);
|
336 | 9307c4c1 | bellard | } |
337 | 9307c4c1 | bellard | break;
|
338 | 9307c4c1 | bellard | } |
339 | 9307c4c1 | bellard | term_printf("'");
|
340 | 9307c4c1 | bellard | } |
341 | 9307c4c1 | bellard | |
342 | 9307c4c1 | bellard | static void memory_dump(int count, int format, int wsize, |
343 | 9307c4c1 | bellard | target_ulong addr, int is_physical)
|
344 | 9307c4c1 | bellard | { |
345 | 9307c4c1 | bellard | int nb_per_line, l, line_size, i, max_digits, len;
|
346 | 9307c4c1 | bellard | uint8_t buf[16];
|
347 | 9307c4c1 | bellard | uint64_t v; |
348 | 9307c4c1 | bellard | |
349 | 9307c4c1 | bellard | if (format == 'i') { |
350 | 9307c4c1 | bellard | int flags;
|
351 | 9307c4c1 | bellard | flags = 0;
|
352 | 9307c4c1 | bellard | #ifdef TARGET_I386
|
353 | 4c27ba27 | bellard | if (wsize == 2) { |
354 | 9307c4c1 | bellard | flags = 1;
|
355 | 4c27ba27 | bellard | } else if (wsize == 4) { |
356 | 4c27ba27 | bellard | flags = 0;
|
357 | 4c27ba27 | bellard | } else {
|
358 | 4c27ba27 | bellard | /* as default we use the current CS size */
|
359 | 4c27ba27 | bellard | flags = 0;
|
360 | 4c27ba27 | bellard | if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
|
361 | 4c27ba27 | bellard | flags = 1;
|
362 | 4c27ba27 | bellard | } |
363 | 4c27ba27 | bellard | #endif
|
364 | 9307c4c1 | bellard | monitor_disas(addr, count, is_physical, flags); |
365 | 9307c4c1 | bellard | return;
|
366 | 9307c4c1 | bellard | } |
367 | 9307c4c1 | bellard | |
368 | 9307c4c1 | bellard | len = wsize * count; |
369 | 9307c4c1 | bellard | if (wsize == 1) |
370 | 9307c4c1 | bellard | line_size = 8;
|
371 | 9307c4c1 | bellard | else
|
372 | 9307c4c1 | bellard | line_size = 16;
|
373 | 9307c4c1 | bellard | nb_per_line = line_size / wsize; |
374 | 9307c4c1 | bellard | max_digits = 0;
|
375 | 9307c4c1 | bellard | |
376 | 9307c4c1 | bellard | switch(format) {
|
377 | 9307c4c1 | bellard | case 'o': |
378 | 9307c4c1 | bellard | max_digits = (wsize * 8 + 2) / 3; |
379 | 9307c4c1 | bellard | break;
|
380 | 9307c4c1 | bellard | default:
|
381 | 9307c4c1 | bellard | case 'x': |
382 | 9307c4c1 | bellard | max_digits = (wsize * 8) / 4; |
383 | 9307c4c1 | bellard | break;
|
384 | 9307c4c1 | bellard | case 'u': |
385 | 9307c4c1 | bellard | case 'd': |
386 | 9307c4c1 | bellard | max_digits = (wsize * 8 * 10 + 32) / 33; |
387 | 9307c4c1 | bellard | break;
|
388 | 9307c4c1 | bellard | case 'c': |
389 | 9307c4c1 | bellard | wsize = 1;
|
390 | 9307c4c1 | bellard | break;
|
391 | 9307c4c1 | bellard | } |
392 | 9307c4c1 | bellard | |
393 | 9307c4c1 | bellard | while (len > 0) { |
394 | 9307c4c1 | bellard | term_printf("0x%08x:", addr);
|
395 | 9307c4c1 | bellard | l = len; |
396 | 9307c4c1 | bellard | if (l > line_size)
|
397 | 9307c4c1 | bellard | l = line_size; |
398 | 9307c4c1 | bellard | if (is_physical) {
|
399 | 9307c4c1 | bellard | cpu_physical_memory_rw(addr, buf, l, 0);
|
400 | 9307c4c1 | bellard | } else {
|
401 | 9307c4c1 | bellard | cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
|
402 | 9307c4c1 | bellard | } |
403 | 9307c4c1 | bellard | i = 0;
|
404 | 9307c4c1 | bellard | while (i < l) {
|
405 | 9307c4c1 | bellard | switch(wsize) {
|
406 | 9307c4c1 | bellard | default:
|
407 | 9307c4c1 | bellard | case 1: |
408 | 9307c4c1 | bellard | v = ldub_raw(buf + i); |
409 | 9307c4c1 | bellard | break;
|
410 | 9307c4c1 | bellard | case 2: |
411 | 9307c4c1 | bellard | v = lduw_raw(buf + i); |
412 | 9307c4c1 | bellard | break;
|
413 | 9307c4c1 | bellard | case 4: |
414 | 9307c4c1 | bellard | v = ldl_raw(buf + i); |
415 | 9307c4c1 | bellard | break;
|
416 | 9307c4c1 | bellard | case 8: |
417 | 9307c4c1 | bellard | v = ldq_raw(buf + i); |
418 | 9307c4c1 | bellard | break;
|
419 | 9307c4c1 | bellard | } |
420 | 9307c4c1 | bellard | term_printf(" ");
|
421 | 9307c4c1 | bellard | switch(format) {
|
422 | 9307c4c1 | bellard | case 'o': |
423 | 9307c4c1 | bellard | term_printf("%#*llo", max_digits, v);
|
424 | 9307c4c1 | bellard | break;
|
425 | 9307c4c1 | bellard | case 'x': |
426 | 9307c4c1 | bellard | term_printf("0x%0*llx", max_digits, v);
|
427 | 9307c4c1 | bellard | break;
|
428 | 9307c4c1 | bellard | case 'u': |
429 | 9307c4c1 | bellard | term_printf("%*llu", max_digits, v);
|
430 | 9307c4c1 | bellard | break;
|
431 | 9307c4c1 | bellard | case 'd': |
432 | 9307c4c1 | bellard | term_printf("%*lld", max_digits, v);
|
433 | 9307c4c1 | bellard | break;
|
434 | 9307c4c1 | bellard | case 'c': |
435 | 9307c4c1 | bellard | term_printc(v); |
436 | 9307c4c1 | bellard | break;
|
437 | 9307c4c1 | bellard | } |
438 | 9307c4c1 | bellard | i += wsize; |
439 | 9307c4c1 | bellard | } |
440 | 9307c4c1 | bellard | term_printf("\n");
|
441 | 9307c4c1 | bellard | addr += l; |
442 | 9307c4c1 | bellard | len -= l; |
443 | 9307c4c1 | bellard | } |
444 | 9307c4c1 | bellard | } |
445 | 9307c4c1 | bellard | |
446 | 9307c4c1 | bellard | static void do_memory_dump(int count, int format, int size, int addr) |
447 | 9307c4c1 | bellard | { |
448 | 9307c4c1 | bellard | memory_dump(count, format, size, addr, 0);
|
449 | 9307c4c1 | bellard | } |
450 | 9307c4c1 | bellard | |
451 | 9307c4c1 | bellard | static void do_physical_memory_dump(int count, int format, int size, int addr) |
452 | 9307c4c1 | bellard | { |
453 | 9307c4c1 | bellard | memory_dump(count, format, size, addr, 1);
|
454 | 9307c4c1 | bellard | } |
455 | 9307c4c1 | bellard | |
456 | 9307c4c1 | bellard | static void do_print(int count, int format, int size, int val) |
457 | 9307c4c1 | bellard | { |
458 | 9307c4c1 | bellard | switch(format) {
|
459 | 9307c4c1 | bellard | case 'o': |
460 | 9307c4c1 | bellard | term_printf("%#o", val);
|
461 | 9307c4c1 | bellard | break;
|
462 | 9307c4c1 | bellard | case 'x': |
463 | 9307c4c1 | bellard | term_printf("%#x", val);
|
464 | 9307c4c1 | bellard | break;
|
465 | 9307c4c1 | bellard | case 'u': |
466 | 9307c4c1 | bellard | term_printf("%u", val);
|
467 | 9307c4c1 | bellard | break;
|
468 | 9307c4c1 | bellard | default:
|
469 | 9307c4c1 | bellard | case 'd': |
470 | 9307c4c1 | bellard | term_printf("%d", val);
|
471 | 9307c4c1 | bellard | break;
|
472 | 9307c4c1 | bellard | case 'c': |
473 | 9307c4c1 | bellard | term_printc(val); |
474 | 9307c4c1 | bellard | break;
|
475 | 9307c4c1 | bellard | } |
476 | 9307c4c1 | bellard | term_printf("\n");
|
477 | 9307c4c1 | bellard | } |
478 | 9307c4c1 | bellard | |
479 | a3a91a35 | bellard | typedef struct { |
480 | a3a91a35 | bellard | int keycode;
|
481 | a3a91a35 | bellard | const char *name; |
482 | a3a91a35 | bellard | } KeyDef; |
483 | a3a91a35 | bellard | |
484 | a3a91a35 | bellard | static const KeyDef key_defs[] = { |
485 | a3a91a35 | bellard | { 0x2a, "shift" }, |
486 | a3a91a35 | bellard | { 0x36, "shift_r" }, |
487 | a3a91a35 | bellard | |
488 | a3a91a35 | bellard | { 0x38, "alt" }, |
489 | a3a91a35 | bellard | { 0xb8, "alt_r" }, |
490 | a3a91a35 | bellard | { 0x1d, "ctrl" }, |
491 | a3a91a35 | bellard | { 0x9d, "ctrl_r" }, |
492 | a3a91a35 | bellard | |
493 | a3a91a35 | bellard | { 0xdd, "menu" }, |
494 | a3a91a35 | bellard | |
495 | a3a91a35 | bellard | { 0x01, "esc" }, |
496 | a3a91a35 | bellard | |
497 | a3a91a35 | bellard | { 0x02, "1" }, |
498 | a3a91a35 | bellard | { 0x03, "2" }, |
499 | a3a91a35 | bellard | { 0x04, "3" }, |
500 | a3a91a35 | bellard | { 0x05, "4" }, |
501 | a3a91a35 | bellard | { 0x06, "5" }, |
502 | a3a91a35 | bellard | { 0x07, "6" }, |
503 | a3a91a35 | bellard | { 0x08, "7" }, |
504 | a3a91a35 | bellard | { 0x09, "8" }, |
505 | a3a91a35 | bellard | { 0x0a, "9" }, |
506 | a3a91a35 | bellard | { 0x0b, "0" }, |
507 | a3a91a35 | bellard | { 0x0e, "backspace" }, |
508 | a3a91a35 | bellard | |
509 | a3a91a35 | bellard | { 0x0f, "tab" }, |
510 | a3a91a35 | bellard | { 0x10, "q" }, |
511 | a3a91a35 | bellard | { 0x11, "w" }, |
512 | a3a91a35 | bellard | { 0x12, "e" }, |
513 | a3a91a35 | bellard | { 0x13, "r" }, |
514 | a3a91a35 | bellard | { 0x14, "t" }, |
515 | a3a91a35 | bellard | { 0x15, "y" }, |
516 | a3a91a35 | bellard | { 0x16, "u" }, |
517 | a3a91a35 | bellard | { 0x17, "i" }, |
518 | a3a91a35 | bellard | { 0x18, "o" }, |
519 | a3a91a35 | bellard | { 0x19, "p" }, |
520 | a3a91a35 | bellard | |
521 | a3a91a35 | bellard | { 0x1c, "ret" }, |
522 | a3a91a35 | bellard | |
523 | a3a91a35 | bellard | { 0x1e, "a" }, |
524 | a3a91a35 | bellard | { 0x1f, "s" }, |
525 | a3a91a35 | bellard | { 0x20, "d" }, |
526 | a3a91a35 | bellard | { 0x21, "f" }, |
527 | a3a91a35 | bellard | { 0x22, "g" }, |
528 | a3a91a35 | bellard | { 0x23, "h" }, |
529 | a3a91a35 | bellard | { 0x24, "j" }, |
530 | a3a91a35 | bellard | { 0x25, "k" }, |
531 | a3a91a35 | bellard | { 0x26, "l" }, |
532 | a3a91a35 | bellard | |
533 | a3a91a35 | bellard | { 0x2c, "z" }, |
534 | a3a91a35 | bellard | { 0x2d, "x" }, |
535 | a3a91a35 | bellard | { 0x2e, "c" }, |
536 | a3a91a35 | bellard | { 0x2f, "v" }, |
537 | a3a91a35 | bellard | { 0x30, "b" }, |
538 | a3a91a35 | bellard | { 0x31, "n" }, |
539 | a3a91a35 | bellard | { 0x32, "m" }, |
540 | a3a91a35 | bellard | |
541 | a3a91a35 | bellard | { 0x39, "spc" }, |
542 | 00ffa62a | bellard | { 0x3a, "caps_lock" }, |
543 | a3a91a35 | bellard | { 0x3b, "f1" }, |
544 | a3a91a35 | bellard | { 0x3c, "f2" }, |
545 | a3a91a35 | bellard | { 0x3d, "f3" }, |
546 | a3a91a35 | bellard | { 0x3e, "f4" }, |
547 | a3a91a35 | bellard | { 0x3f, "f5" }, |
548 | a3a91a35 | bellard | { 0x40, "f6" }, |
549 | a3a91a35 | bellard | { 0x41, "f7" }, |
550 | a3a91a35 | bellard | { 0x42, "f8" }, |
551 | a3a91a35 | bellard | { 0x43, "f9" }, |
552 | a3a91a35 | bellard | { 0x44, "f10" }, |
553 | 00ffa62a | bellard | { 0x45, "num_lock" }, |
554 | a3a91a35 | bellard | { 0x46, "scroll_lock" }, |
555 | a3a91a35 | bellard | |
556 | a3a91a35 | bellard | { 0x56, "<" }, |
557 | a3a91a35 | bellard | |
558 | a3a91a35 | bellard | { 0x57, "f11" }, |
559 | a3a91a35 | bellard | { 0x58, "f12" }, |
560 | a3a91a35 | bellard | |
561 | a3a91a35 | bellard | { 0xb7, "print" }, |
562 | a3a91a35 | bellard | |
563 | a3a91a35 | bellard | { 0xc7, "home" }, |
564 | a3a91a35 | bellard | { 0xc9, "pgup" }, |
565 | a3a91a35 | bellard | { 0xd1, "pgdn" }, |
566 | a3a91a35 | bellard | { 0xcf, "end" }, |
567 | a3a91a35 | bellard | |
568 | a3a91a35 | bellard | { 0xcb, "left" }, |
569 | a3a91a35 | bellard | { 0xc8, "up" }, |
570 | a3a91a35 | bellard | { 0xd0, "down" }, |
571 | a3a91a35 | bellard | { 0xcd, "right" }, |
572 | a3a91a35 | bellard | |
573 | a3a91a35 | bellard | { 0xd2, "insert" }, |
574 | a3a91a35 | bellard | { 0xd3, "delete" }, |
575 | a3a91a35 | bellard | { 0, NULL }, |
576 | a3a91a35 | bellard | }; |
577 | a3a91a35 | bellard | |
578 | a3a91a35 | bellard | static int get_keycode(const char *key) |
579 | a3a91a35 | bellard | { |
580 | a3a91a35 | bellard | const KeyDef *p;
|
581 | a3a91a35 | bellard | |
582 | a3a91a35 | bellard | for(p = key_defs; p->name != NULL; p++) { |
583 | a3a91a35 | bellard | if (!strcmp(key, p->name))
|
584 | a3a91a35 | bellard | return p->keycode;
|
585 | a3a91a35 | bellard | } |
586 | a3a91a35 | bellard | return -1; |
587 | a3a91a35 | bellard | } |
588 | a3a91a35 | bellard | |
589 | a3a91a35 | bellard | static void do_send_key(const char *string) |
590 | a3a91a35 | bellard | { |
591 | a3a91a35 | bellard | char keybuf[16], *q; |
592 | a3a91a35 | bellard | uint8_t keycodes[16];
|
593 | a3a91a35 | bellard | const char *p; |
594 | a3a91a35 | bellard | int nb_keycodes, keycode, i;
|
595 | a3a91a35 | bellard | |
596 | a3a91a35 | bellard | nb_keycodes = 0;
|
597 | a3a91a35 | bellard | p = string; |
598 | a3a91a35 | bellard | while (*p != '\0') { |
599 | a3a91a35 | bellard | q = keybuf; |
600 | a3a91a35 | bellard | while (*p != '\0' && *p != '-') { |
601 | a3a91a35 | bellard | if ((q - keybuf) < sizeof(keybuf) - 1) { |
602 | a3a91a35 | bellard | *q++ = *p; |
603 | a3a91a35 | bellard | } |
604 | a3a91a35 | bellard | p++; |
605 | a3a91a35 | bellard | } |
606 | a3a91a35 | bellard | *q = '\0';
|
607 | a3a91a35 | bellard | keycode = get_keycode(keybuf); |
608 | a3a91a35 | bellard | if (keycode < 0) { |
609 | a3a91a35 | bellard | term_printf("unknown key: '%s'\n", keybuf);
|
610 | a3a91a35 | bellard | return;
|
611 | a3a91a35 | bellard | } |
612 | a3a91a35 | bellard | keycodes[nb_keycodes++] = keycode; |
613 | a3a91a35 | bellard | if (*p == '\0') |
614 | a3a91a35 | bellard | break;
|
615 | a3a91a35 | bellard | p++; |
616 | a3a91a35 | bellard | } |
617 | a3a91a35 | bellard | /* key down events */
|
618 | a3a91a35 | bellard | for(i = 0; i < nb_keycodes; i++) { |
619 | a3a91a35 | bellard | keycode = keycodes[i]; |
620 | a3a91a35 | bellard | if (keycode & 0x80) |
621 | a3a91a35 | bellard | kbd_put_keycode(0xe0);
|
622 | a3a91a35 | bellard | kbd_put_keycode(keycode & 0x7f);
|
623 | a3a91a35 | bellard | } |
624 | a3a91a35 | bellard | /* key up events */
|
625 | a3a91a35 | bellard | for(i = nb_keycodes - 1; i >= 0; i--) { |
626 | a3a91a35 | bellard | keycode = keycodes[i]; |
627 | a3a91a35 | bellard | if (keycode & 0x80) |
628 | a3a91a35 | bellard | kbd_put_keycode(0xe0);
|
629 | a3a91a35 | bellard | kbd_put_keycode(keycode | 0x80);
|
630 | a3a91a35 | bellard | } |
631 | a3a91a35 | bellard | } |
632 | a3a91a35 | bellard | |
633 | 3440557b | bellard | static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index) |
634 | 3440557b | bellard | { |
635 | 3440557b | bellard | uint32_t val; |
636 | 3440557b | bellard | int suffix;
|
637 | 3440557b | bellard | |
638 | 3440557b | bellard | if (has_index) {
|
639 | 3440557b | bellard | cpu_outb(NULL, addr & 0xffff, index & 0xff); |
640 | 3440557b | bellard | addr++; |
641 | 3440557b | bellard | } |
642 | 3440557b | bellard | addr &= 0xffff;
|
643 | 3440557b | bellard | |
644 | 3440557b | bellard | switch(size) {
|
645 | 3440557b | bellard | default:
|
646 | 3440557b | bellard | case 1: |
647 | 3440557b | bellard | val = cpu_inb(NULL, addr);
|
648 | 3440557b | bellard | suffix = 'b';
|
649 | 3440557b | bellard | break;
|
650 | 3440557b | bellard | case 2: |
651 | 3440557b | bellard | val = cpu_inw(NULL, addr);
|
652 | 3440557b | bellard | suffix = 'w';
|
653 | 3440557b | bellard | break;
|
654 | 3440557b | bellard | case 4: |
655 | 3440557b | bellard | val = cpu_inl(NULL, addr);
|
656 | 3440557b | bellard | suffix = 'l';
|
657 | 3440557b | bellard | break;
|
658 | 3440557b | bellard | } |
659 | 3440557b | bellard | term_printf("port%c[0x%04x] = %#0*x\n",
|
660 | 3440557b | bellard | suffix, addr, size * 2, val);
|
661 | 3440557b | bellard | } |
662 | a3a91a35 | bellard | |
663 | e4f9082b | bellard | static void do_system_reset(void) |
664 | e4f9082b | bellard | { |
665 | e4f9082b | bellard | qemu_system_reset_request(); |
666 | e4f9082b | bellard | } |
667 | e4f9082b | bellard | |
668 | 9dc39cba | bellard | static term_cmd_t term_cmds[] = {
|
669 | 9307c4c1 | bellard | { "help|?", "s?", do_help, |
670 | 9dc39cba | bellard | "[cmd]", "show the help" }, |
671 | 9307c4c1 | bellard | { "commit", "", do_commit, |
672 | 9dc39cba | bellard | "", "commit changes to the disk images (if -snapshot is used)" }, |
673 | 9307c4c1 | bellard | { "info", "s?", do_info, |
674 | 9dc39cba | bellard | "subcommand", "show various information about the system state" }, |
675 | 9307c4c1 | bellard | { "q|quit", "", do_quit, |
676 | 9dc39cba | bellard | "", "quit the emulator" }, |
677 | 9307c4c1 | bellard | { "eject", "-fs", do_eject, |
678 | 9dc39cba | bellard | "[-f] device", "eject a removable media (use -f to force it)" }, |
679 | 9307c4c1 | bellard | { "change", "sF", do_change, |
680 | 9dc39cba | bellard | "device filename", "change a removable media" }, |
681 | 9307c4c1 | bellard | { "screendump", "F", do_screen_dump, |
682 | 59a983b9 | bellard | "filename", "save screen into PPM image 'filename'" }, |
683 | 9307c4c1 | bellard | { "log", "s", do_log, |
684 | f193c797 | bellard | "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, |
685 | 9307c4c1 | bellard | { "savevm", "F", do_savevm, |
686 | 8a7ddc38 | bellard | "filename", "save the whole virtual machine state to 'filename'" }, |
687 | 9307c4c1 | bellard | { "loadvm", "F", do_loadvm, |
688 | 8a7ddc38 | bellard | "filename", "restore the whole virtual machine state from 'filename'" }, |
689 | 9307c4c1 | bellard | { "stop", "", do_stop, |
690 | 9307c4c1 | bellard | "", "stop emulation", }, |
691 | 9307c4c1 | bellard | { "c|cont", "", do_cont, |
692 | 9307c4c1 | bellard | "", "resume emulation", }, |
693 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
694 | 9307c4c1 | bellard | { "gdbserver", "i?", do_gdbserver, |
695 | 9307c4c1 | bellard | "[port]", "start gdbserver session (default port=1234)", }, |
696 | 67b915a5 | bellard | #endif
|
697 | 9307c4c1 | bellard | { "x", "/i", do_memory_dump, |
698 | 9307c4c1 | bellard | "/fmt addr", "virtual memory dump starting at 'addr'", }, |
699 | 9307c4c1 | bellard | { "xp", "/i", do_physical_memory_dump, |
700 | 9307c4c1 | bellard | "/fmt addr", "physical memory dump starting at 'addr'", }, |
701 | 9307c4c1 | bellard | { "p|print", "/i", do_print, |
702 | 9307c4c1 | bellard | "/fmt expr", "print expression value (use $reg for CPU register access)", }, |
703 | 3440557b | bellard | { "i", "/ii.", do_ioport_read, |
704 | 3440557b | bellard | "/fmt addr", "I/O port read" }, |
705 | 3440557b | bellard | |
706 | a3a91a35 | bellard | { "sendkey", "s", do_send_key, |
707 | a3a91a35 | bellard | "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" }, |
708 | e4f9082b | bellard | { "system_reset", "", do_system_reset, |
709 | e4f9082b | bellard | "", "reset the system" }, |
710 | f193c797 | bellard | { NULL, NULL, }, |
711 | 9dc39cba | bellard | }; |
712 | 9dc39cba | bellard | |
713 | 9dc39cba | bellard | static term_cmd_t info_cmds[] = {
|
714 | 9307c4c1 | bellard | { "network", "", do_info_network, |
715 | 9dc39cba | bellard | "", "show the network state" }, |
716 | 9307c4c1 | bellard | { "block", "", do_info_block, |
717 | 9dc39cba | bellard | "", "show the block devices" }, |
718 | 9307c4c1 | bellard | { "registers", "", do_info_registers, |
719 | 9307c4c1 | bellard | "", "show the cpu registers" }, |
720 | aa455485 | bellard | { "history", "", do_info_history, |
721 | aa455485 | bellard | "", "show the command line history", }, |
722 | 4a0fb71e | bellard | { "irq", "", irq_info, |
723 | 4a0fb71e | bellard | "", "show the interrupts statistics (if available)", }, |
724 | 4c27ba27 | bellard | { "pic", "", pic_info, |
725 | 4c27ba27 | bellard | "", "show i8259 (PIC) state", }, |
726 | 86e0c048 | bellard | { "pci", "", pci_info, |
727 | 86e0c048 | bellard | "", "show PCI info", }, |
728 | 9dc39cba | bellard | { NULL, NULL, }, |
729 | 9dc39cba | bellard | }; |
730 | 9dc39cba | bellard | |
731 | 9307c4c1 | bellard | /*******************************************************************/
|
732 | 9307c4c1 | bellard | |
733 | 9307c4c1 | bellard | static const char *pch; |
734 | 9307c4c1 | bellard | static jmp_buf expr_env;
|
735 | 9307c4c1 | bellard | |
736 | 9307c4c1 | bellard | typedef struct MonitorDef { |
737 | 9307c4c1 | bellard | const char *name; |
738 | 9307c4c1 | bellard | int offset;
|
739 | 9307c4c1 | bellard | int (*get_value)(struct MonitorDef *md); |
740 | 9307c4c1 | bellard | } MonitorDef; |
741 | 9307c4c1 | bellard | |
742 | 57206fd4 | bellard | #if defined(TARGET_I386)
|
743 | 57206fd4 | bellard | static int monitor_get_pc (struct MonitorDef *md) |
744 | 57206fd4 | bellard | { |
745 | 57206fd4 | bellard | return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base; |
746 | 57206fd4 | bellard | } |
747 | 57206fd4 | bellard | #endif
|
748 | 57206fd4 | bellard | |
749 | a541f297 | bellard | #if defined(TARGET_PPC)
|
750 | a541f297 | bellard | static int monitor_get_ccr (struct MonitorDef *md) |
751 | a541f297 | bellard | { |
752 | a541f297 | bellard | unsigned int u; |
753 | a541f297 | bellard | int i;
|
754 | a541f297 | bellard | |
755 | a541f297 | bellard | u = 0;
|
756 | a541f297 | bellard | for (i = 0; i < 8; i++) |
757 | a541f297 | bellard | u |= cpu_single_env->crf[i] << (32 - (4 * i)); |
758 | a541f297 | bellard | |
759 | a541f297 | bellard | return u;
|
760 | a541f297 | bellard | } |
761 | a541f297 | bellard | |
762 | a541f297 | bellard | static int monitor_get_msr (struct MonitorDef *md) |
763 | a541f297 | bellard | { |
764 | a541f297 | bellard | return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
|
765 | a541f297 | bellard | (cpu_single_env->msr[MSR_ILE] << MSR_ILE) | |
766 | a541f297 | bellard | (cpu_single_env->msr[MSR_EE] << MSR_EE) | |
767 | a541f297 | bellard | (cpu_single_env->msr[MSR_PR] << MSR_PR) | |
768 | a541f297 | bellard | (cpu_single_env->msr[MSR_FP] << MSR_FP) | |
769 | a541f297 | bellard | (cpu_single_env->msr[MSR_ME] << MSR_ME) | |
770 | a541f297 | bellard | (cpu_single_env->msr[MSR_FE0] << MSR_FE0) | |
771 | a541f297 | bellard | (cpu_single_env->msr[MSR_SE] << MSR_SE) | |
772 | a541f297 | bellard | (cpu_single_env->msr[MSR_BE] << MSR_BE) | |
773 | a541f297 | bellard | (cpu_single_env->msr[MSR_FE1] << MSR_FE1) | |
774 | a541f297 | bellard | (cpu_single_env->msr[MSR_IP] << MSR_IP) | |
775 | a541f297 | bellard | (cpu_single_env->msr[MSR_IR] << MSR_IR) | |
776 | a541f297 | bellard | (cpu_single_env->msr[MSR_DR] << MSR_DR) | |
777 | a541f297 | bellard | (cpu_single_env->msr[MSR_RI] << MSR_RI) | |
778 | a541f297 | bellard | (cpu_single_env->msr[MSR_LE] << MSR_LE); |
779 | a541f297 | bellard | } |
780 | a541f297 | bellard | |
781 | a541f297 | bellard | static int monitor_get_xer (struct MonitorDef *md) |
782 | a541f297 | bellard | { |
783 | a541f297 | bellard | return (cpu_single_env->xer[XER_SO] << XER_SO) |
|
784 | a541f297 | bellard | (cpu_single_env->xer[XER_OV] << XER_OV) | |
785 | a541f297 | bellard | (cpu_single_env->xer[XER_CA] << XER_CA) | |
786 | a541f297 | bellard | (cpu_single_env->xer[XER_BC] << XER_BC); |
787 | a541f297 | bellard | } |
788 | 9fddaa0c | bellard | |
789 | 9fddaa0c | bellard | uint32_t cpu_ppc_load_decr (CPUState *env); |
790 | 9fddaa0c | bellard | static int monitor_get_decr (struct MonitorDef *md) |
791 | 9fddaa0c | bellard | { |
792 | 9fddaa0c | bellard | return cpu_ppc_load_decr(cpu_single_env);
|
793 | 9fddaa0c | bellard | } |
794 | 9fddaa0c | bellard | |
795 | 9fddaa0c | bellard | uint32_t cpu_ppc_load_tbu (CPUState *env); |
796 | 9fddaa0c | bellard | static int monitor_get_tbu (struct MonitorDef *md) |
797 | 9fddaa0c | bellard | { |
798 | 9fddaa0c | bellard | return cpu_ppc_load_tbu(cpu_single_env);
|
799 | 9fddaa0c | bellard | } |
800 | 9fddaa0c | bellard | |
801 | 9fddaa0c | bellard | uint32_t cpu_ppc_load_tbl (CPUState *env); |
802 | 9fddaa0c | bellard | static int monitor_get_tbl (struct MonitorDef *md) |
803 | 9fddaa0c | bellard | { |
804 | 9fddaa0c | bellard | return cpu_ppc_load_tbl(cpu_single_env);
|
805 | 9fddaa0c | bellard | } |
806 | a541f297 | bellard | #endif
|
807 | a541f297 | bellard | |
808 | 9307c4c1 | bellard | static MonitorDef monitor_defs[] = {
|
809 | 9307c4c1 | bellard | #ifdef TARGET_I386
|
810 | 57206fd4 | bellard | |
811 | 57206fd4 | bellard | #define SEG(name, seg) \
|
812 | 57206fd4 | bellard | { name, offsetof(CPUState, segs[seg].selector) },\ |
813 | 57206fd4 | bellard | { name ".base", offsetof(CPUState, segs[seg].base) },\
|
814 | 57206fd4 | bellard | { name ".limit", offsetof(CPUState, segs[seg].limit) },
|
815 | 57206fd4 | bellard | |
816 | 9307c4c1 | bellard | { "eax", offsetof(CPUState, regs[0]) }, |
817 | 9307c4c1 | bellard | { "ecx", offsetof(CPUState, regs[1]) }, |
818 | 9307c4c1 | bellard | { "edx", offsetof(CPUState, regs[2]) }, |
819 | 9307c4c1 | bellard | { "ebx", offsetof(CPUState, regs[3]) }, |
820 | 9307c4c1 | bellard | { "esp|sp", offsetof(CPUState, regs[4]) }, |
821 | 9307c4c1 | bellard | { "ebp|fp", offsetof(CPUState, regs[5]) }, |
822 | 9307c4c1 | bellard | { "esi", offsetof(CPUState, regs[6]) }, |
823 | 9307c4c1 | bellard | { "esi", offsetof(CPUState, regs[7]) }, |
824 | 9307c4c1 | bellard | { "eflags", offsetof(CPUState, eflags) },
|
825 | 57206fd4 | bellard | { "eip", offsetof(CPUState, eip) },
|
826 | 57206fd4 | bellard | SEG("cs", R_CS)
|
827 | 57206fd4 | bellard | SEG("ds", R_DS)
|
828 | 57206fd4 | bellard | SEG("es", R_ES)
|
829 | 57206fd4 | bellard | SEG("fs", R_FS)
|
830 | 57206fd4 | bellard | SEG("gs", R_GS)
|
831 | 57206fd4 | bellard | { "pc", 0, monitor_get_pc, }, |
832 | a541f297 | bellard | #elif defined(TARGET_PPC)
|
833 | a541f297 | bellard | { "r0", offsetof(CPUState, gpr[0]) }, |
834 | a541f297 | bellard | { "r1", offsetof(CPUState, gpr[1]) }, |
835 | a541f297 | bellard | { "r2", offsetof(CPUState, gpr[2]) }, |
836 | a541f297 | bellard | { "r3", offsetof(CPUState, gpr[3]) }, |
837 | a541f297 | bellard | { "r4", offsetof(CPUState, gpr[4]) }, |
838 | a541f297 | bellard | { "r5", offsetof(CPUState, gpr[5]) }, |
839 | a541f297 | bellard | { "r6", offsetof(CPUState, gpr[6]) }, |
840 | a541f297 | bellard | { "r7", offsetof(CPUState, gpr[7]) }, |
841 | a541f297 | bellard | { "r8", offsetof(CPUState, gpr[8]) }, |
842 | a541f297 | bellard | { "r9", offsetof(CPUState, gpr[9]) }, |
843 | a541f297 | bellard | { "r10", offsetof(CPUState, gpr[10]) }, |
844 | a541f297 | bellard | { "r11", offsetof(CPUState, gpr[11]) }, |
845 | a541f297 | bellard | { "r12", offsetof(CPUState, gpr[12]) }, |
846 | a541f297 | bellard | { "r13", offsetof(CPUState, gpr[13]) }, |
847 | a541f297 | bellard | { "r14", offsetof(CPUState, gpr[14]) }, |
848 | a541f297 | bellard | { "r15", offsetof(CPUState, gpr[15]) }, |
849 | a541f297 | bellard | { "r16", offsetof(CPUState, gpr[16]) }, |
850 | a541f297 | bellard | { "r17", offsetof(CPUState, gpr[17]) }, |
851 | a541f297 | bellard | { "r18", offsetof(CPUState, gpr[18]) }, |
852 | a541f297 | bellard | { "r19", offsetof(CPUState, gpr[19]) }, |
853 | a541f297 | bellard | { "r20", offsetof(CPUState, gpr[20]) }, |
854 | a541f297 | bellard | { "r21", offsetof(CPUState, gpr[21]) }, |
855 | a541f297 | bellard | { "r22", offsetof(CPUState, gpr[22]) }, |
856 | a541f297 | bellard | { "r23", offsetof(CPUState, gpr[23]) }, |
857 | a541f297 | bellard | { "r24", offsetof(CPUState, gpr[24]) }, |
858 | a541f297 | bellard | { "r25", offsetof(CPUState, gpr[25]) }, |
859 | a541f297 | bellard | { "r26", offsetof(CPUState, gpr[26]) }, |
860 | a541f297 | bellard | { "r27", offsetof(CPUState, gpr[27]) }, |
861 | a541f297 | bellard | { "r28", offsetof(CPUState, gpr[28]) }, |
862 | a541f297 | bellard | { "r29", offsetof(CPUState, gpr[29]) }, |
863 | a541f297 | bellard | { "r30", offsetof(CPUState, gpr[30]) }, |
864 | a541f297 | bellard | { "r31", offsetof(CPUState, gpr[31]) }, |
865 | 57206fd4 | bellard | { "nip|pc", offsetof(CPUState, nip) },
|
866 | a541f297 | bellard | { "lr", offsetof(CPUState, lr) },
|
867 | a541f297 | bellard | { "ctr", offsetof(CPUState, ctr) },
|
868 | 9fddaa0c | bellard | { "decr", 0, &monitor_get_decr, }, |
869 | a541f297 | bellard | { "ccr", 0, &monitor_get_ccr, }, |
870 | a541f297 | bellard | { "msr", 0, &monitor_get_msr, }, |
871 | a541f297 | bellard | { "xer", 0, &monitor_get_xer, }, |
872 | 9fddaa0c | bellard | { "tbu", 0, &monitor_get_tbu, }, |
873 | 9fddaa0c | bellard | { "tbl", 0, &monitor_get_tbl, }, |
874 | a541f297 | bellard | { "sdr1", offsetof(CPUState, sdr1) },
|
875 | a541f297 | bellard | { "sr0", offsetof(CPUState, sr[0]) }, |
876 | a541f297 | bellard | { "sr1", offsetof(CPUState, sr[1]) }, |
877 | a541f297 | bellard | { "sr2", offsetof(CPUState, sr[2]) }, |
878 | a541f297 | bellard | { "sr3", offsetof(CPUState, sr[3]) }, |
879 | a541f297 | bellard | { "sr4", offsetof(CPUState, sr[4]) }, |
880 | a541f297 | bellard | { "sr5", offsetof(CPUState, sr[5]) }, |
881 | a541f297 | bellard | { "sr6", offsetof(CPUState, sr[6]) }, |
882 | a541f297 | bellard | { "sr7", offsetof(CPUState, sr[7]) }, |
883 | a541f297 | bellard | { "sr8", offsetof(CPUState, sr[8]) }, |
884 | a541f297 | bellard | { "sr9", offsetof(CPUState, sr[9]) }, |
885 | a541f297 | bellard | { "sr10", offsetof(CPUState, sr[10]) }, |
886 | a541f297 | bellard | { "sr11", offsetof(CPUState, sr[11]) }, |
887 | a541f297 | bellard | { "sr12", offsetof(CPUState, sr[12]) }, |
888 | a541f297 | bellard | { "sr13", offsetof(CPUState, sr[13]) }, |
889 | a541f297 | bellard | { "sr14", offsetof(CPUState, sr[14]) }, |
890 | a541f297 | bellard | { "sr15", offsetof(CPUState, sr[15]) }, |
891 | a541f297 | bellard | /* Too lazy to put BATs and SPRs ... */
|
892 | 9307c4c1 | bellard | #endif
|
893 | 9307c4c1 | bellard | { NULL },
|
894 | 9307c4c1 | bellard | }; |
895 | 9307c4c1 | bellard | |
896 | 9307c4c1 | bellard | static void expr_error(const char *fmt) |
897 | 9dc39cba | bellard | { |
898 | 9307c4c1 | bellard | term_printf(fmt); |
899 | 9307c4c1 | bellard | term_printf("\n");
|
900 | 9307c4c1 | bellard | longjmp(expr_env, 1);
|
901 | 9307c4c1 | bellard | } |
902 | 9307c4c1 | bellard | |
903 | 9307c4c1 | bellard | static int get_monitor_def(int *pval, const char *name) |
904 | 9307c4c1 | bellard | { |
905 | 9307c4c1 | bellard | MonitorDef *md; |
906 | 9307c4c1 | bellard | for(md = monitor_defs; md->name != NULL; md++) { |
907 | 9307c4c1 | bellard | if (compare_cmd(name, md->name)) {
|
908 | 9307c4c1 | bellard | if (md->get_value) {
|
909 | 9307c4c1 | bellard | *pval = md->get_value(md); |
910 | 9307c4c1 | bellard | } else {
|
911 | 9307c4c1 | bellard | *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset); |
912 | 9307c4c1 | bellard | } |
913 | 9307c4c1 | bellard | return 0; |
914 | 9307c4c1 | bellard | } |
915 | 9307c4c1 | bellard | } |
916 | 9307c4c1 | bellard | return -1; |
917 | 9307c4c1 | bellard | } |
918 | 9307c4c1 | bellard | |
919 | 9307c4c1 | bellard | static void next(void) |
920 | 9307c4c1 | bellard | { |
921 | 9307c4c1 | bellard | if (pch != '\0') { |
922 | 9307c4c1 | bellard | pch++; |
923 | 9307c4c1 | bellard | while (isspace(*pch))
|
924 | 9307c4c1 | bellard | pch++; |
925 | 9307c4c1 | bellard | } |
926 | 9307c4c1 | bellard | } |
927 | 9307c4c1 | bellard | |
928 | 9307c4c1 | bellard | static int expr_sum(void); |
929 | 9307c4c1 | bellard | |
930 | 9307c4c1 | bellard | static int expr_unary(void) |
931 | 9307c4c1 | bellard | { |
932 | 9307c4c1 | bellard | int n;
|
933 | 9307c4c1 | bellard | char *p;
|
934 | 9307c4c1 | bellard | |
935 | 9307c4c1 | bellard | switch(*pch) {
|
936 | 9307c4c1 | bellard | case '+': |
937 | 9307c4c1 | bellard | next(); |
938 | 9307c4c1 | bellard | n = expr_unary(); |
939 | 9307c4c1 | bellard | break;
|
940 | 9307c4c1 | bellard | case '-': |
941 | 9307c4c1 | bellard | next(); |
942 | 9307c4c1 | bellard | n = -expr_unary(); |
943 | 9307c4c1 | bellard | break;
|
944 | 9307c4c1 | bellard | case '~': |
945 | 9307c4c1 | bellard | next(); |
946 | 9307c4c1 | bellard | n = ~expr_unary(); |
947 | 9307c4c1 | bellard | break;
|
948 | 9307c4c1 | bellard | case '(': |
949 | 9307c4c1 | bellard | next(); |
950 | 9307c4c1 | bellard | n = expr_sum(); |
951 | 9307c4c1 | bellard | if (*pch != ')') { |
952 | 9307c4c1 | bellard | expr_error("')' expected");
|
953 | 9307c4c1 | bellard | } |
954 | 9307c4c1 | bellard | next(); |
955 | 9307c4c1 | bellard | break;
|
956 | 9307c4c1 | bellard | case '$': |
957 | 9307c4c1 | bellard | { |
958 | 9307c4c1 | bellard | char buf[128], *q; |
959 | 9307c4c1 | bellard | |
960 | 9307c4c1 | bellard | pch++; |
961 | 9307c4c1 | bellard | q = buf; |
962 | 9307c4c1 | bellard | while ((*pch >= 'a' && *pch <= 'z') || |
963 | 9307c4c1 | bellard | (*pch >= 'A' && *pch <= 'Z') || |
964 | 9307c4c1 | bellard | (*pch >= '0' && *pch <= '9') || |
965 | 57206fd4 | bellard | *pch == '_' || *pch == '.') { |
966 | 9307c4c1 | bellard | if ((q - buf) < sizeof(buf) - 1) |
967 | 9307c4c1 | bellard | *q++ = *pch; |
968 | 9307c4c1 | bellard | pch++; |
969 | 9307c4c1 | bellard | } |
970 | 9307c4c1 | bellard | while (isspace(*pch))
|
971 | 9307c4c1 | bellard | pch++; |
972 | 9307c4c1 | bellard | *q = 0;
|
973 | 9307c4c1 | bellard | if (get_monitor_def(&n, buf))
|
974 | 9307c4c1 | bellard | expr_error("unknown register");
|
975 | 9307c4c1 | bellard | } |
976 | 9307c4c1 | bellard | break;
|
977 | 9307c4c1 | bellard | case '\0': |
978 | 9307c4c1 | bellard | expr_error("unexpected end of expression");
|
979 | 9307c4c1 | bellard | n = 0;
|
980 | 9307c4c1 | bellard | break;
|
981 | 9307c4c1 | bellard | default:
|
982 | 9307c4c1 | bellard | n = strtoul(pch, &p, 0);
|
983 | 9307c4c1 | bellard | if (pch == p) {
|
984 | 9307c4c1 | bellard | expr_error("invalid char in expression");
|
985 | 9307c4c1 | bellard | } |
986 | 9307c4c1 | bellard | pch = p; |
987 | 9307c4c1 | bellard | while (isspace(*pch))
|
988 | 9307c4c1 | bellard | pch++; |
989 | 9307c4c1 | bellard | break;
|
990 | 9307c4c1 | bellard | } |
991 | 9307c4c1 | bellard | return n;
|
992 | 9307c4c1 | bellard | } |
993 | 9307c4c1 | bellard | |
994 | 9307c4c1 | bellard | |
995 | 9307c4c1 | bellard | static int expr_prod(void) |
996 | 9307c4c1 | bellard | { |
997 | 9307c4c1 | bellard | int val, val2, op;
|
998 | 9307c4c1 | bellard | |
999 | 9307c4c1 | bellard | val = expr_unary(); |
1000 | 9307c4c1 | bellard | for(;;) {
|
1001 | 9307c4c1 | bellard | op = *pch; |
1002 | 9307c4c1 | bellard | if (op != '*' && op != '/' && op != '%') |
1003 | 9307c4c1 | bellard | break;
|
1004 | 9307c4c1 | bellard | next(); |
1005 | 9307c4c1 | bellard | val2 = expr_unary(); |
1006 | 9307c4c1 | bellard | switch(op) {
|
1007 | 9307c4c1 | bellard | default:
|
1008 | 9307c4c1 | bellard | case '*': |
1009 | 9307c4c1 | bellard | val *= val2; |
1010 | 9307c4c1 | bellard | break;
|
1011 | 9307c4c1 | bellard | case '/': |
1012 | 9307c4c1 | bellard | case '%': |
1013 | 9307c4c1 | bellard | if (val2 == 0) |
1014 | 5b60212f | bellard | expr_error("division by zero");
|
1015 | 9307c4c1 | bellard | if (op == '/') |
1016 | 9307c4c1 | bellard | val /= val2; |
1017 | 9307c4c1 | bellard | else
|
1018 | 9307c4c1 | bellard | val %= val2; |
1019 | 9307c4c1 | bellard | break;
|
1020 | 9307c4c1 | bellard | } |
1021 | 9307c4c1 | bellard | } |
1022 | 9307c4c1 | bellard | return val;
|
1023 | 9307c4c1 | bellard | } |
1024 | 9307c4c1 | bellard | |
1025 | 9307c4c1 | bellard | static int expr_logic(void) |
1026 | 9307c4c1 | bellard | { |
1027 | 9307c4c1 | bellard | int val, val2, op;
|
1028 | 9307c4c1 | bellard | |
1029 | 9307c4c1 | bellard | val = expr_prod(); |
1030 | 9307c4c1 | bellard | for(;;) {
|
1031 | 9307c4c1 | bellard | op = *pch; |
1032 | 9307c4c1 | bellard | if (op != '&' && op != '|' && op != '^') |
1033 | 9307c4c1 | bellard | break;
|
1034 | 9307c4c1 | bellard | next(); |
1035 | 9307c4c1 | bellard | val2 = expr_prod(); |
1036 | 9307c4c1 | bellard | switch(op) {
|
1037 | 9307c4c1 | bellard | default:
|
1038 | 9307c4c1 | bellard | case '&': |
1039 | 9307c4c1 | bellard | val &= val2; |
1040 | 9307c4c1 | bellard | break;
|
1041 | 9307c4c1 | bellard | case '|': |
1042 | 9307c4c1 | bellard | val |= val2; |
1043 | 9307c4c1 | bellard | break;
|
1044 | 9307c4c1 | bellard | case '^': |
1045 | 9307c4c1 | bellard | val ^= val2; |
1046 | 9307c4c1 | bellard | break;
|
1047 | 9307c4c1 | bellard | } |
1048 | 9307c4c1 | bellard | } |
1049 | 9307c4c1 | bellard | return val;
|
1050 | 9307c4c1 | bellard | } |
1051 | 9307c4c1 | bellard | |
1052 | 9307c4c1 | bellard | static int expr_sum(void) |
1053 | 9307c4c1 | bellard | { |
1054 | 9307c4c1 | bellard | int val, val2, op;
|
1055 | 9307c4c1 | bellard | |
1056 | 9307c4c1 | bellard | val = expr_logic(); |
1057 | 9307c4c1 | bellard | for(;;) {
|
1058 | 9307c4c1 | bellard | op = *pch; |
1059 | 9307c4c1 | bellard | if (op != '+' && op != '-') |
1060 | 9307c4c1 | bellard | break;
|
1061 | 9307c4c1 | bellard | next(); |
1062 | 9307c4c1 | bellard | val2 = expr_logic(); |
1063 | 9307c4c1 | bellard | if (op == '+') |
1064 | 9307c4c1 | bellard | val += val2; |
1065 | 9307c4c1 | bellard | else
|
1066 | 9307c4c1 | bellard | val -= val2; |
1067 | 9307c4c1 | bellard | } |
1068 | 9307c4c1 | bellard | return val;
|
1069 | 9307c4c1 | bellard | } |
1070 | 9307c4c1 | bellard | |
1071 | 9307c4c1 | bellard | static int get_expr(int *pval, const char **pp) |
1072 | 9307c4c1 | bellard | { |
1073 | 9307c4c1 | bellard | pch = *pp; |
1074 | 9307c4c1 | bellard | if (setjmp(expr_env)) {
|
1075 | 9307c4c1 | bellard | *pp = pch; |
1076 | 9307c4c1 | bellard | return -1; |
1077 | 9307c4c1 | bellard | } |
1078 | 9307c4c1 | bellard | while (isspace(*pch))
|
1079 | 9307c4c1 | bellard | pch++; |
1080 | 9307c4c1 | bellard | *pval = expr_sum(); |
1081 | 9307c4c1 | bellard | *pp = pch; |
1082 | 9307c4c1 | bellard | return 0; |
1083 | 9307c4c1 | bellard | } |
1084 | 9307c4c1 | bellard | |
1085 | 9307c4c1 | bellard | static int get_str(char *buf, int buf_size, const char **pp) |
1086 | 9307c4c1 | bellard | { |
1087 | 9307c4c1 | bellard | const char *p; |
1088 | 9307c4c1 | bellard | char *q;
|
1089 | 9307c4c1 | bellard | int c;
|
1090 | 9307c4c1 | bellard | |
1091 | 9307c4c1 | bellard | p = *pp; |
1092 | 9307c4c1 | bellard | while (isspace(*p))
|
1093 | 9307c4c1 | bellard | p++; |
1094 | 9307c4c1 | bellard | if (*p == '\0') { |
1095 | 9307c4c1 | bellard | fail:
|
1096 | 9307c4c1 | bellard | *pp = p; |
1097 | 9307c4c1 | bellard | return -1; |
1098 | 9307c4c1 | bellard | } |
1099 | 9307c4c1 | bellard | q = buf; |
1100 | 9307c4c1 | bellard | if (*p == '\"') { |
1101 | 9307c4c1 | bellard | p++; |
1102 | 9307c4c1 | bellard | while (*p != '\0' && *p != '\"') { |
1103 | 9307c4c1 | bellard | if (*p == '\\') { |
1104 | 9307c4c1 | bellard | p++; |
1105 | 9307c4c1 | bellard | c = *p++; |
1106 | 9307c4c1 | bellard | switch(c) {
|
1107 | 9307c4c1 | bellard | case 'n': |
1108 | 9307c4c1 | bellard | c = '\n';
|
1109 | 9307c4c1 | bellard | break;
|
1110 | 9307c4c1 | bellard | case 'r': |
1111 | 9307c4c1 | bellard | c = '\r';
|
1112 | 9307c4c1 | bellard | break;
|
1113 | 9307c4c1 | bellard | case '\\': |
1114 | 9307c4c1 | bellard | case '\'': |
1115 | 9307c4c1 | bellard | case '\"': |
1116 | 9307c4c1 | bellard | break;
|
1117 | 9307c4c1 | bellard | default:
|
1118 | 9307c4c1 | bellard | qemu_printf("unsupported escape code: '\\%c'\n", c);
|
1119 | 9307c4c1 | bellard | goto fail;
|
1120 | 9307c4c1 | bellard | } |
1121 | 9307c4c1 | bellard | if ((q - buf) < buf_size - 1) { |
1122 | 9307c4c1 | bellard | *q++ = c; |
1123 | 9307c4c1 | bellard | } |
1124 | 9307c4c1 | bellard | } else {
|
1125 | 9307c4c1 | bellard | if ((q - buf) < buf_size - 1) { |
1126 | 9307c4c1 | bellard | *q++ = *p; |
1127 | 9307c4c1 | bellard | } |
1128 | 9307c4c1 | bellard | p++; |
1129 | 9307c4c1 | bellard | } |
1130 | 9307c4c1 | bellard | } |
1131 | 9307c4c1 | bellard | if (*p != '\"') { |
1132 | 5b60212f | bellard | qemu_printf("unterminated string\n");
|
1133 | 9307c4c1 | bellard | goto fail;
|
1134 | 9307c4c1 | bellard | } |
1135 | 9307c4c1 | bellard | p++; |
1136 | 9307c4c1 | bellard | } else {
|
1137 | 9307c4c1 | bellard | while (*p != '\0' && !isspace(*p)) { |
1138 | 9307c4c1 | bellard | if ((q - buf) < buf_size - 1) { |
1139 | 9307c4c1 | bellard | *q++ = *p; |
1140 | 9307c4c1 | bellard | } |
1141 | 9307c4c1 | bellard | p++; |
1142 | 9307c4c1 | bellard | } |
1143 | 9307c4c1 | bellard | *q = '\0';
|
1144 | 9307c4c1 | bellard | } |
1145 | 9307c4c1 | bellard | *pp = p; |
1146 | 9307c4c1 | bellard | return 0; |
1147 | 9307c4c1 | bellard | } |
1148 | 9307c4c1 | bellard | |
1149 | 9307c4c1 | bellard | static int default_fmt_format = 'x'; |
1150 | 9307c4c1 | bellard | static int default_fmt_size = 4; |
1151 | 9307c4c1 | bellard | |
1152 | 9307c4c1 | bellard | #define MAX_ARGS 16 |
1153 | 9307c4c1 | bellard | |
1154 | 9307c4c1 | bellard | static void term_handle_command(const char *cmdline) |
1155 | 9307c4c1 | bellard | { |
1156 | 9307c4c1 | bellard | const char *p, *pstart, *typestr; |
1157 | 9307c4c1 | bellard | char *q;
|
1158 | 9307c4c1 | bellard | int c, nb_args, len, i, has_arg;
|
1159 | 9dc39cba | bellard | term_cmd_t *cmd; |
1160 | 9307c4c1 | bellard | char cmdname[256]; |
1161 | 9307c4c1 | bellard | char buf[1024]; |
1162 | 9307c4c1 | bellard | void *str_allocated[MAX_ARGS];
|
1163 | 9307c4c1 | bellard | void *args[MAX_ARGS];
|
1164 | 9dc39cba | bellard | |
1165 | 9dc39cba | bellard | #ifdef DEBUG
|
1166 | 9dc39cba | bellard | term_printf("command='%s'\n", cmdline);
|
1167 | 9dc39cba | bellard | #endif
|
1168 | 9dc39cba | bellard | |
1169 | 9307c4c1 | bellard | /* extract the command name */
|
1170 | 9dc39cba | bellard | p = cmdline; |
1171 | 9307c4c1 | bellard | q = cmdname; |
1172 | 9307c4c1 | bellard | while (isspace(*p))
|
1173 | 9307c4c1 | bellard | p++; |
1174 | 9307c4c1 | bellard | if (*p == '\0') |
1175 | 9307c4c1 | bellard | return;
|
1176 | 9307c4c1 | bellard | pstart = p; |
1177 | 9307c4c1 | bellard | while (*p != '\0' && *p != '/' && !isspace(*p)) |
1178 | 9307c4c1 | bellard | p++; |
1179 | 9307c4c1 | bellard | len = p - pstart; |
1180 | 9307c4c1 | bellard | if (len > sizeof(cmdname) - 1) |
1181 | 9307c4c1 | bellard | len = sizeof(cmdname) - 1; |
1182 | 9307c4c1 | bellard | memcpy(cmdname, pstart, len); |
1183 | 9307c4c1 | bellard | cmdname[len] = '\0';
|
1184 | 9307c4c1 | bellard | |
1185 | 9307c4c1 | bellard | /* find the command */
|
1186 | 9307c4c1 | bellard | for(cmd = term_cmds; cmd->name != NULL; cmd++) { |
1187 | 9307c4c1 | bellard | if (compare_cmd(cmdname, cmd->name))
|
1188 | 9307c4c1 | bellard | goto found;
|
1189 | 9307c4c1 | bellard | } |
1190 | 9307c4c1 | bellard | term_printf("unknown command: '%s'\n", cmdname);
|
1191 | 9307c4c1 | bellard | return;
|
1192 | 9307c4c1 | bellard | found:
|
1193 | 9307c4c1 | bellard | |
1194 | 9307c4c1 | bellard | for(i = 0; i < MAX_ARGS; i++) |
1195 | 9307c4c1 | bellard | str_allocated[i] = NULL;
|
1196 | 9307c4c1 | bellard | |
1197 | 9307c4c1 | bellard | /* parse the parameters */
|
1198 | 9307c4c1 | bellard | typestr = cmd->args_type; |
1199 | 9307c4c1 | bellard | nb_args = 0;
|
1200 | 9dc39cba | bellard | for(;;) {
|
1201 | 9307c4c1 | bellard | c = *typestr; |
1202 | 9307c4c1 | bellard | if (c == '\0') |
1203 | 9dc39cba | bellard | break;
|
1204 | 9307c4c1 | bellard | typestr++; |
1205 | 9307c4c1 | bellard | switch(c) {
|
1206 | 9307c4c1 | bellard | case 'F': |
1207 | 9307c4c1 | bellard | case 's': |
1208 | 9307c4c1 | bellard | { |
1209 | 9307c4c1 | bellard | int ret;
|
1210 | 9307c4c1 | bellard | char *str;
|
1211 | 9307c4c1 | bellard | |
1212 | 9307c4c1 | bellard | while (isspace(*p))
|
1213 | 9307c4c1 | bellard | p++; |
1214 | 9307c4c1 | bellard | if (*typestr == '?') { |
1215 | 9307c4c1 | bellard | typestr++; |
1216 | 9307c4c1 | bellard | if (*p == '\0') { |
1217 | 9307c4c1 | bellard | /* no optional string: NULL argument */
|
1218 | 9307c4c1 | bellard | str = NULL;
|
1219 | 9307c4c1 | bellard | goto add_str;
|
1220 | 9307c4c1 | bellard | } |
1221 | 9307c4c1 | bellard | } |
1222 | 9307c4c1 | bellard | ret = get_str(buf, sizeof(buf), &p);
|
1223 | 9307c4c1 | bellard | if (ret < 0) { |
1224 | 9307c4c1 | bellard | if (c == 'F') |
1225 | 9307c4c1 | bellard | term_printf("%s: filename expected\n", cmdname);
|
1226 | 9307c4c1 | bellard | else
|
1227 | 9307c4c1 | bellard | term_printf("%s: string expected\n", cmdname);
|
1228 | 9307c4c1 | bellard | goto fail;
|
1229 | 9307c4c1 | bellard | } |
1230 | 9307c4c1 | bellard | str = qemu_malloc(strlen(buf) + 1);
|
1231 | 9307c4c1 | bellard | strcpy(str, buf); |
1232 | 9307c4c1 | bellard | str_allocated[nb_args] = str; |
1233 | 9307c4c1 | bellard | add_str:
|
1234 | 9307c4c1 | bellard | if (nb_args >= MAX_ARGS) {
|
1235 | 9307c4c1 | bellard | error_args:
|
1236 | 9307c4c1 | bellard | term_printf("%s: too many arguments\n", cmdname);
|
1237 | 9307c4c1 | bellard | goto fail;
|
1238 | 9307c4c1 | bellard | } |
1239 | 9307c4c1 | bellard | args[nb_args++] = str; |
1240 | 9307c4c1 | bellard | } |
1241 | 9dc39cba | bellard | break;
|
1242 | 9307c4c1 | bellard | case '/': |
1243 | 9307c4c1 | bellard | { |
1244 | 9307c4c1 | bellard | int count, format, size;
|
1245 | 9307c4c1 | bellard | |
1246 | 9307c4c1 | bellard | while (isspace(*p))
|
1247 | 9307c4c1 | bellard | p++; |
1248 | 9307c4c1 | bellard | if (*p == '/') { |
1249 | 9307c4c1 | bellard | /* format found */
|
1250 | 9307c4c1 | bellard | p++; |
1251 | 9307c4c1 | bellard | count = 1;
|
1252 | 9307c4c1 | bellard | if (isdigit(*p)) {
|
1253 | 9307c4c1 | bellard | count = 0;
|
1254 | 9307c4c1 | bellard | while (isdigit(*p)) {
|
1255 | 9307c4c1 | bellard | count = count * 10 + (*p - '0'); |
1256 | 9307c4c1 | bellard | p++; |
1257 | 9307c4c1 | bellard | } |
1258 | 9307c4c1 | bellard | } |
1259 | 9307c4c1 | bellard | size = -1;
|
1260 | 9307c4c1 | bellard | format = -1;
|
1261 | 9307c4c1 | bellard | for(;;) {
|
1262 | 9307c4c1 | bellard | switch(*p) {
|
1263 | 9307c4c1 | bellard | case 'o': |
1264 | 9307c4c1 | bellard | case 'd': |
1265 | 9307c4c1 | bellard | case 'u': |
1266 | 9307c4c1 | bellard | case 'x': |
1267 | 9307c4c1 | bellard | case 'i': |
1268 | 9307c4c1 | bellard | case 'c': |
1269 | 9307c4c1 | bellard | format = *p++; |
1270 | 9307c4c1 | bellard | break;
|
1271 | 9307c4c1 | bellard | case 'b': |
1272 | 9307c4c1 | bellard | size = 1;
|
1273 | 9307c4c1 | bellard | p++; |
1274 | 9307c4c1 | bellard | break;
|
1275 | 9307c4c1 | bellard | case 'h': |
1276 | 9307c4c1 | bellard | size = 2;
|
1277 | 9307c4c1 | bellard | p++; |
1278 | 9307c4c1 | bellard | break;
|
1279 | 9307c4c1 | bellard | case 'w': |
1280 | 9307c4c1 | bellard | size = 4;
|
1281 | 9307c4c1 | bellard | p++; |
1282 | 9307c4c1 | bellard | break;
|
1283 | 9307c4c1 | bellard | case 'g': |
1284 | 9307c4c1 | bellard | case 'L': |
1285 | 9307c4c1 | bellard | size = 8;
|
1286 | 9307c4c1 | bellard | p++; |
1287 | 9307c4c1 | bellard | break;
|
1288 | 9307c4c1 | bellard | default:
|
1289 | 9307c4c1 | bellard | goto next;
|
1290 | 9307c4c1 | bellard | } |
1291 | 9307c4c1 | bellard | } |
1292 | 9307c4c1 | bellard | next:
|
1293 | 9307c4c1 | bellard | if (*p != '\0' && !isspace(*p)) { |
1294 | 9307c4c1 | bellard | term_printf("invalid char in format: '%c'\n", *p);
|
1295 | 9307c4c1 | bellard | goto fail;
|
1296 | 9307c4c1 | bellard | } |
1297 | 9307c4c1 | bellard | if (format < 0) |
1298 | 9307c4c1 | bellard | format = default_fmt_format; |
1299 | 4c27ba27 | bellard | if (format != 'i') { |
1300 | 4c27ba27 | bellard | /* for 'i', not specifying a size gives -1 as size */
|
1301 | 4c27ba27 | bellard | if (size < 0) |
1302 | 4c27ba27 | bellard | size = default_fmt_size; |
1303 | 4c27ba27 | bellard | } |
1304 | 9307c4c1 | bellard | default_fmt_size = size; |
1305 | 9307c4c1 | bellard | default_fmt_format = format; |
1306 | 9307c4c1 | bellard | } else {
|
1307 | 9307c4c1 | bellard | count = 1;
|
1308 | 9307c4c1 | bellard | format = default_fmt_format; |
1309 | 4c27ba27 | bellard | if (format != 'i') { |
1310 | 4c27ba27 | bellard | size = default_fmt_size; |
1311 | 4c27ba27 | bellard | } else {
|
1312 | 4c27ba27 | bellard | size = -1;
|
1313 | 4c27ba27 | bellard | } |
1314 | 9307c4c1 | bellard | } |
1315 | 9307c4c1 | bellard | if (nb_args + 3 > MAX_ARGS) |
1316 | 9307c4c1 | bellard | goto error_args;
|
1317 | 9307c4c1 | bellard | args[nb_args++] = (void*)count;
|
1318 | 9307c4c1 | bellard | args[nb_args++] = (void*)format;
|
1319 | 9307c4c1 | bellard | args[nb_args++] = (void*)size;
|
1320 | 9307c4c1 | bellard | } |
1321 | 9dc39cba | bellard | break;
|
1322 | 9307c4c1 | bellard | case 'i': |
1323 | 9307c4c1 | bellard | { |
1324 | 9307c4c1 | bellard | int val;
|
1325 | 9307c4c1 | bellard | while (isspace(*p))
|
1326 | 9307c4c1 | bellard | p++; |
1327 | 3440557b | bellard | if (*typestr == '?' || *typestr == '.') { |
1328 | 9307c4c1 | bellard | typestr++; |
1329 | 3440557b | bellard | if (*typestr == '?') { |
1330 | 3440557b | bellard | if (*p == '\0') |
1331 | 3440557b | bellard | has_arg = 0;
|
1332 | 3440557b | bellard | else
|
1333 | 3440557b | bellard | has_arg = 1;
|
1334 | 3440557b | bellard | } else {
|
1335 | 3440557b | bellard | if (*p == '.') { |
1336 | 3440557b | bellard | p++; |
1337 | 3440557b | bellard | while (isspace(*p))
|
1338 | 3440557b | bellard | p++; |
1339 | 3440557b | bellard | has_arg = 1;
|
1340 | 3440557b | bellard | } else {
|
1341 | 3440557b | bellard | has_arg = 0;
|
1342 | 3440557b | bellard | } |
1343 | 3440557b | bellard | } |
1344 | 9307c4c1 | bellard | if (nb_args >= MAX_ARGS)
|
1345 | 9307c4c1 | bellard | goto error_args;
|
1346 | 9307c4c1 | bellard | args[nb_args++] = (void *)has_arg;
|
1347 | 9307c4c1 | bellard | if (!has_arg) {
|
1348 | 9307c4c1 | bellard | if (nb_args >= MAX_ARGS)
|
1349 | 9307c4c1 | bellard | goto error_args;
|
1350 | 9307c4c1 | bellard | val = -1;
|
1351 | 9307c4c1 | bellard | goto add_num;
|
1352 | 9307c4c1 | bellard | } |
1353 | 9307c4c1 | bellard | } |
1354 | 9307c4c1 | bellard | if (get_expr(&val, &p))
|
1355 | 9307c4c1 | bellard | goto fail;
|
1356 | 9307c4c1 | bellard | add_num:
|
1357 | 9307c4c1 | bellard | if (nb_args >= MAX_ARGS)
|
1358 | 9307c4c1 | bellard | goto error_args;
|
1359 | 9307c4c1 | bellard | args[nb_args++] = (void *)val;
|
1360 | 9307c4c1 | bellard | } |
1361 | 9307c4c1 | bellard | break;
|
1362 | 9307c4c1 | bellard | case '-': |
1363 | 9307c4c1 | bellard | { |
1364 | 9307c4c1 | bellard | int has_option;
|
1365 | 9307c4c1 | bellard | /* option */
|
1366 | 9307c4c1 | bellard | |
1367 | 9307c4c1 | bellard | c = *typestr++; |
1368 | 9307c4c1 | bellard | if (c == '\0') |
1369 | 9307c4c1 | bellard | goto bad_type;
|
1370 | 9307c4c1 | bellard | while (isspace(*p))
|
1371 | 9307c4c1 | bellard | p++; |
1372 | 9307c4c1 | bellard | has_option = 0;
|
1373 | 9307c4c1 | bellard | if (*p == '-') { |
1374 | 9307c4c1 | bellard | p++; |
1375 | 9307c4c1 | bellard | if (*p != c) {
|
1376 | 9307c4c1 | bellard | term_printf("%s: unsupported option -%c\n",
|
1377 | 9307c4c1 | bellard | cmdname, *p); |
1378 | 9307c4c1 | bellard | goto fail;
|
1379 | 9307c4c1 | bellard | } |
1380 | 9307c4c1 | bellard | p++; |
1381 | 9307c4c1 | bellard | has_option = 1;
|
1382 | 9307c4c1 | bellard | } |
1383 | 9307c4c1 | bellard | if (nb_args >= MAX_ARGS)
|
1384 | 9307c4c1 | bellard | goto error_args;
|
1385 | 9307c4c1 | bellard | args[nb_args++] = (void *)has_option;
|
1386 | 9307c4c1 | bellard | } |
1387 | 9307c4c1 | bellard | break;
|
1388 | 9307c4c1 | bellard | default:
|
1389 | 9307c4c1 | bellard | bad_type:
|
1390 | 9307c4c1 | bellard | term_printf("%s: unknown type '%c'\n", cmdname, c);
|
1391 | 9307c4c1 | bellard | goto fail;
|
1392 | 9307c4c1 | bellard | } |
1393 | 9dc39cba | bellard | } |
1394 | 9307c4c1 | bellard | /* check that all arguments were parsed */
|
1395 | 9307c4c1 | bellard | while (isspace(*p))
|
1396 | 9307c4c1 | bellard | p++; |
1397 | 9307c4c1 | bellard | if (*p != '\0') { |
1398 | 9307c4c1 | bellard | term_printf("%s: extraneous characters at the end of line\n",
|
1399 | 9307c4c1 | bellard | cmdname); |
1400 | 9307c4c1 | bellard | goto fail;
|
1401 | 9dc39cba | bellard | } |
1402 | 9307c4c1 | bellard | |
1403 | 9307c4c1 | bellard | switch(nb_args) {
|
1404 | 9307c4c1 | bellard | case 0: |
1405 | 9307c4c1 | bellard | cmd->handler(); |
1406 | 9307c4c1 | bellard | break;
|
1407 | 9307c4c1 | bellard | case 1: |
1408 | 9307c4c1 | bellard | cmd->handler(args[0]);
|
1409 | 9307c4c1 | bellard | break;
|
1410 | 9307c4c1 | bellard | case 2: |
1411 | 9307c4c1 | bellard | cmd->handler(args[0], args[1]); |
1412 | 9307c4c1 | bellard | break;
|
1413 | 9307c4c1 | bellard | case 3: |
1414 | 9307c4c1 | bellard | cmd->handler(args[0], args[1], args[2]); |
1415 | 9307c4c1 | bellard | break;
|
1416 | 9307c4c1 | bellard | case 4: |
1417 | 9307c4c1 | bellard | cmd->handler(args[0], args[1], args[2], args[3]); |
1418 | 9307c4c1 | bellard | break;
|
1419 | 9307c4c1 | bellard | case 5: |
1420 | 9307c4c1 | bellard | cmd->handler(args[0], args[1], args[2], args[3], args[4]); |
1421 | 9307c4c1 | bellard | break;
|
1422 | 3440557b | bellard | case 6: |
1423 | 3440557b | bellard | cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]); |
1424 | 3440557b | bellard | break;
|
1425 | 9307c4c1 | bellard | default:
|
1426 | 9307c4c1 | bellard | term_printf("unsupported number of arguments: %d\n", nb_args);
|
1427 | 9307c4c1 | bellard | goto fail;
|
1428 | 9dc39cba | bellard | } |
1429 | 9307c4c1 | bellard | fail:
|
1430 | 9307c4c1 | bellard | for(i = 0; i < MAX_ARGS; i++) |
1431 | 9307c4c1 | bellard | qemu_free(str_allocated[i]); |
1432 | 9dc39cba | bellard | return;
|
1433 | 9dc39cba | bellard | } |
1434 | 9dc39cba | bellard | |
1435 | 9dc39cba | bellard | static void term_show_prompt(void) |
1436 | 9dc39cba | bellard | { |
1437 | 9dc39cba | bellard | term_printf("(qemu) ");
|
1438 | 9dc39cba | bellard | fflush(stdout); |
1439 | 9dc39cba | bellard | term_cmd_buf_index = 0;
|
1440 | 9dc39cba | bellard | term_cmd_buf_size = 0;
|
1441 | 9dc39cba | bellard | term_esc_state = IS_NORM; |
1442 | 9dc39cba | bellard | } |
1443 | 9dc39cba | bellard | |
1444 | aa455485 | bellard | static void term_print_cmdline (const char *cmdline) |
1445 | aa455485 | bellard | { |
1446 | aa455485 | bellard | term_show_prompt(); |
1447 | 95ce326e | bellard | term_printf("%s", cmdline);
|
1448 | aa455485 | bellard | term_flush(); |
1449 | aa455485 | bellard | } |
1450 | aa455485 | bellard | |
1451 | 9dc39cba | bellard | static void term_insert_char(int ch) |
1452 | 9dc39cba | bellard | { |
1453 | 9dc39cba | bellard | if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) {
|
1454 | 9dc39cba | bellard | memmove(term_cmd_buf + term_cmd_buf_index + 1,
|
1455 | 9dc39cba | bellard | term_cmd_buf + term_cmd_buf_index, |
1456 | 9dc39cba | bellard | term_cmd_buf_size - term_cmd_buf_index); |
1457 | 9dc39cba | bellard | term_cmd_buf[term_cmd_buf_index] = ch; |
1458 | 9dc39cba | bellard | term_cmd_buf_size++; |
1459 | 9dc39cba | bellard | term_printf("\033[@%c", ch);
|
1460 | 9dc39cba | bellard | term_cmd_buf_index++; |
1461 | 9dc39cba | bellard | term_flush(); |
1462 | 9dc39cba | bellard | } |
1463 | 9dc39cba | bellard | } |
1464 | 9dc39cba | bellard | |
1465 | 9dc39cba | bellard | static void term_backward_char(void) |
1466 | 9dc39cba | bellard | { |
1467 | 9dc39cba | bellard | if (term_cmd_buf_index > 0) { |
1468 | 9dc39cba | bellard | term_cmd_buf_index--; |
1469 | 9dc39cba | bellard | term_printf("\033[D");
|
1470 | 9dc39cba | bellard | term_flush(); |
1471 | 9dc39cba | bellard | } |
1472 | 9dc39cba | bellard | } |
1473 | 9dc39cba | bellard | |
1474 | 9dc39cba | bellard | static void term_forward_char(void) |
1475 | 9dc39cba | bellard | { |
1476 | 9dc39cba | bellard | if (term_cmd_buf_index < term_cmd_buf_size) {
|
1477 | 9dc39cba | bellard | term_cmd_buf_index++; |
1478 | 9dc39cba | bellard | term_printf("\033[C");
|
1479 | 9dc39cba | bellard | term_flush(); |
1480 | 9dc39cba | bellard | } |
1481 | 9dc39cba | bellard | } |
1482 | 9dc39cba | bellard | |
1483 | 9dc39cba | bellard | static void term_delete_char(void) |
1484 | 9dc39cba | bellard | { |
1485 | 9dc39cba | bellard | if (term_cmd_buf_index < term_cmd_buf_size) {
|
1486 | 9dc39cba | bellard | memmove(term_cmd_buf + term_cmd_buf_index, |
1487 | 9dc39cba | bellard | term_cmd_buf + term_cmd_buf_index + 1,
|
1488 | 9dc39cba | bellard | term_cmd_buf_size - term_cmd_buf_index - 1);
|
1489 | 9dc39cba | bellard | term_printf("\033[P");
|
1490 | 9dc39cba | bellard | term_cmd_buf_size--; |
1491 | 9dc39cba | bellard | term_flush(); |
1492 | 9dc39cba | bellard | } |
1493 | 9dc39cba | bellard | } |
1494 | 9dc39cba | bellard | |
1495 | 9dc39cba | bellard | static void term_backspace(void) |
1496 | 9dc39cba | bellard | { |
1497 | 9dc39cba | bellard | if (term_cmd_buf_index > 0) { |
1498 | 9dc39cba | bellard | term_backward_char(); |
1499 | 9dc39cba | bellard | term_delete_char(); |
1500 | 9dc39cba | bellard | } |
1501 | 9dc39cba | bellard | } |
1502 | 9dc39cba | bellard | |
1503 | 9dc39cba | bellard | static void term_bol(void) |
1504 | 9dc39cba | bellard | { |
1505 | 9dc39cba | bellard | while (term_cmd_buf_index > 0) |
1506 | 9dc39cba | bellard | term_backward_char(); |
1507 | 9dc39cba | bellard | } |
1508 | 9dc39cba | bellard | |
1509 | 9dc39cba | bellard | static void term_eol(void) |
1510 | 9dc39cba | bellard | { |
1511 | 9dc39cba | bellard | while (term_cmd_buf_index < term_cmd_buf_size)
|
1512 | 9dc39cba | bellard | term_forward_char(); |
1513 | 9dc39cba | bellard | } |
1514 | 9dc39cba | bellard | |
1515 | aa455485 | bellard | static void term_up_char(void) |
1516 | aa455485 | bellard | { |
1517 | aa455485 | bellard | int idx;
|
1518 | aa455485 | bellard | |
1519 | aa455485 | bellard | if (term_hist_entry == 0) |
1520 | aa455485 | bellard | return;
|
1521 | aa455485 | bellard | if (term_hist_entry == -1) { |
1522 | aa455485 | bellard | /* Find latest entry */
|
1523 | aa455485 | bellard | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { |
1524 | aa455485 | bellard | if (term_history[idx] == NULL) |
1525 | aa455485 | bellard | break;
|
1526 | aa455485 | bellard | } |
1527 | aa455485 | bellard | term_hist_entry = idx; |
1528 | aa455485 | bellard | } |
1529 | aa455485 | bellard | term_hist_entry--; |
1530 | aa455485 | bellard | if (term_hist_entry >= 0) { |
1531 | 95ce326e | bellard | pstrcpy(term_cmd_buf, sizeof(term_cmd_buf),
|
1532 | 95ce326e | bellard | term_history[term_hist_entry]); |
1533 | aa455485 | bellard | term_printf("\n");
|
1534 | aa455485 | bellard | term_print_cmdline(term_cmd_buf); |
1535 | aa455485 | bellard | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); |
1536 | aa455485 | bellard | } |
1537 | aa455485 | bellard | } |
1538 | aa455485 | bellard | |
1539 | aa455485 | bellard | static void term_down_char(void) |
1540 | aa455485 | bellard | { |
1541 | aa455485 | bellard | if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1) |
1542 | aa455485 | bellard | return;
|
1543 | aa455485 | bellard | if (term_history[++term_hist_entry] != NULL) { |
1544 | 95ce326e | bellard | pstrcpy(term_cmd_buf, sizeof(term_cmd_buf),
|
1545 | 95ce326e | bellard | term_history[term_hist_entry]); |
1546 | aa455485 | bellard | } else {
|
1547 | aa455485 | bellard | term_hist_entry = -1;
|
1548 | aa455485 | bellard | } |
1549 | aa455485 | bellard | term_printf("\n");
|
1550 | aa455485 | bellard | term_print_cmdline(term_cmd_buf); |
1551 | aa455485 | bellard | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); |
1552 | aa455485 | bellard | } |
1553 | aa455485 | bellard | |
1554 | aa455485 | bellard | static void term_hist_add(const char *cmdline) |
1555 | aa455485 | bellard | { |
1556 | aa455485 | bellard | char *hist_entry, *new_entry;
|
1557 | aa455485 | bellard | int idx;
|
1558 | aa455485 | bellard | |
1559 | aa455485 | bellard | if (cmdline[0] == '\0') |
1560 | aa455485 | bellard | return;
|
1561 | aa455485 | bellard | new_entry = NULL;
|
1562 | aa455485 | bellard | if (term_hist_entry != -1) { |
1563 | aa455485 | bellard | /* We were editing an existing history entry: replace it */
|
1564 | aa455485 | bellard | hist_entry = term_history[term_hist_entry]; |
1565 | aa455485 | bellard | idx = term_hist_entry; |
1566 | aa455485 | bellard | if (strcmp(hist_entry, cmdline) == 0) { |
1567 | aa455485 | bellard | goto same_entry;
|
1568 | aa455485 | bellard | } |
1569 | aa455485 | bellard | } |
1570 | aa455485 | bellard | /* Search cmdline in history buffers */
|
1571 | aa455485 | bellard | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { |
1572 | aa455485 | bellard | hist_entry = term_history[idx]; |
1573 | aa455485 | bellard | if (hist_entry == NULL) |
1574 | aa455485 | bellard | break;
|
1575 | aa455485 | bellard | if (strcmp(hist_entry, cmdline) == 0) { |
1576 | aa455485 | bellard | same_entry:
|
1577 | aa455485 | bellard | new_entry = hist_entry; |
1578 | aa455485 | bellard | /* Put this entry at the end of history */
|
1579 | aa455485 | bellard | memmove(&term_history[idx], &term_history[idx + 1],
|
1580 | aa455485 | bellard | &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]);
|
1581 | aa455485 | bellard | term_history[TERM_MAX_CMDS - 1] = NULL; |
1582 | aa455485 | bellard | for (; idx < TERM_MAX_CMDS; idx++) {
|
1583 | aa455485 | bellard | if (term_history[idx] == NULL) |
1584 | aa455485 | bellard | break;
|
1585 | aa455485 | bellard | } |
1586 | aa455485 | bellard | break;
|
1587 | aa455485 | bellard | } |
1588 | aa455485 | bellard | } |
1589 | aa455485 | bellard | if (idx == TERM_MAX_CMDS) {
|
1590 | aa455485 | bellard | /* Need to get one free slot */
|
1591 | aa455485 | bellard | free(term_history[0]);
|
1592 | aa455485 | bellard | memcpy(term_history, &term_history[1],
|
1593 | aa455485 | bellard | &term_history[TERM_MAX_CMDS] - &term_history[1]);
|
1594 | aa455485 | bellard | term_history[TERM_MAX_CMDS - 1] = NULL; |
1595 | aa455485 | bellard | idx = TERM_MAX_CMDS - 1;
|
1596 | aa455485 | bellard | } |
1597 | aa455485 | bellard | if (new_entry == NULL) |
1598 | aa455485 | bellard | new_entry = strdup(cmdline); |
1599 | aa455485 | bellard | term_history[idx] = new_entry; |
1600 | aa455485 | bellard | term_hist_entry = -1;
|
1601 | aa455485 | bellard | } |
1602 | aa455485 | bellard | |
1603 | 9dc39cba | bellard | /* return true if command handled */
|
1604 | 9dc39cba | bellard | static void term_handle_byte(int ch) |
1605 | 9dc39cba | bellard | { |
1606 | 9dc39cba | bellard | switch(term_esc_state) {
|
1607 | 9dc39cba | bellard | case IS_NORM:
|
1608 | 9dc39cba | bellard | switch(ch) {
|
1609 | 9dc39cba | bellard | case 1: |
1610 | 9dc39cba | bellard | term_bol(); |
1611 | 9dc39cba | bellard | break;
|
1612 | 9dc39cba | bellard | case 5: |
1613 | 9dc39cba | bellard | term_eol(); |
1614 | 9dc39cba | bellard | break;
|
1615 | 9dc39cba | bellard | case 10: |
1616 | 9dc39cba | bellard | case 13: |
1617 | 9dc39cba | bellard | term_cmd_buf[term_cmd_buf_size] = '\0';
|
1618 | aa455485 | bellard | term_hist_add(term_cmd_buf); |
1619 | 9dc39cba | bellard | term_printf("\n");
|
1620 | 9dc39cba | bellard | term_handle_command(term_cmd_buf); |
1621 | 9dc39cba | bellard | term_show_prompt(); |
1622 | 9dc39cba | bellard | break;
|
1623 | 9dc39cba | bellard | case 27: |
1624 | 9dc39cba | bellard | term_esc_state = IS_ESC; |
1625 | 9dc39cba | bellard | break;
|
1626 | 9dc39cba | bellard | case 127: |
1627 | 9dc39cba | bellard | case 8: |
1628 | 9dc39cba | bellard | term_backspace(); |
1629 | 9dc39cba | bellard | break;
|
1630 | aa455485 | bellard | case 155: |
1631 | aa455485 | bellard | term_esc_state = IS_CSI; |
1632 | aa455485 | bellard | break;
|
1633 | 9dc39cba | bellard | default:
|
1634 | 9dc39cba | bellard | if (ch >= 32) { |
1635 | 9dc39cba | bellard | term_insert_char(ch); |
1636 | 9dc39cba | bellard | } |
1637 | 9dc39cba | bellard | break;
|
1638 | 9dc39cba | bellard | } |
1639 | 9dc39cba | bellard | break;
|
1640 | 9dc39cba | bellard | case IS_ESC:
|
1641 | 9dc39cba | bellard | if (ch == '[') { |
1642 | 9dc39cba | bellard | term_esc_state = IS_CSI; |
1643 | 9dc39cba | bellard | term_esc_param = 0;
|
1644 | 9dc39cba | bellard | } else {
|
1645 | 9dc39cba | bellard | term_esc_state = IS_NORM; |
1646 | 9dc39cba | bellard | } |
1647 | 9dc39cba | bellard | break;
|
1648 | 9dc39cba | bellard | case IS_CSI:
|
1649 | 9dc39cba | bellard | switch(ch) {
|
1650 | aa455485 | bellard | case 'A': |
1651 | aa455485 | bellard | case 'F': |
1652 | aa455485 | bellard | term_up_char(); |
1653 | aa455485 | bellard | break;
|
1654 | aa455485 | bellard | case 'B': |
1655 | aa455485 | bellard | case 'E': |
1656 | aa455485 | bellard | term_down_char(); |
1657 | aa455485 | bellard | break;
|
1658 | 9dc39cba | bellard | case 'D': |
1659 | 9dc39cba | bellard | term_backward_char(); |
1660 | 9dc39cba | bellard | break;
|
1661 | 9dc39cba | bellard | case 'C': |
1662 | 9dc39cba | bellard | term_forward_char(); |
1663 | 9dc39cba | bellard | break;
|
1664 | 9dc39cba | bellard | case '0' ... '9': |
1665 | 9dc39cba | bellard | term_esc_param = term_esc_param * 10 + (ch - '0'); |
1666 | 9dc39cba | bellard | goto the_end;
|
1667 | 9dc39cba | bellard | case '~': |
1668 | 9dc39cba | bellard | switch(term_esc_param) {
|
1669 | 9dc39cba | bellard | case 1: |
1670 | 9dc39cba | bellard | term_bol(); |
1671 | 9dc39cba | bellard | break;
|
1672 | 9dc39cba | bellard | case 3: |
1673 | 9dc39cba | bellard | term_delete_char(); |
1674 | 9dc39cba | bellard | break;
|
1675 | 9dc39cba | bellard | case 4: |
1676 | 9dc39cba | bellard | term_eol(); |
1677 | 9dc39cba | bellard | break;
|
1678 | 9dc39cba | bellard | } |
1679 | 9dc39cba | bellard | break;
|
1680 | 9dc39cba | bellard | default:
|
1681 | 9dc39cba | bellard | break;
|
1682 | 9dc39cba | bellard | } |
1683 | 9dc39cba | bellard | term_esc_state = IS_NORM; |
1684 | 9dc39cba | bellard | the_end:
|
1685 | 9dc39cba | bellard | break;
|
1686 | 9dc39cba | bellard | } |
1687 | 9dc39cba | bellard | } |
1688 | 9dc39cba | bellard | |
1689 | 9dc39cba | bellard | /*************************************************************/
|
1690 | 9dc39cba | bellard | /* serial console support */
|
1691 | 9dc39cba | bellard | |
1692 | 9dc39cba | bellard | #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ |
1693 | 9dc39cba | bellard | |
1694 | 9dc39cba | bellard | static int term_got_escape, term_command; |
1695 | 9dc39cba | bellard | |
1696 | 9dc39cba | bellard | void term_print_help(void) |
1697 | 9dc39cba | bellard | { |
1698 | 9dc39cba | bellard | term_printf("\n"
|
1699 | 9dc39cba | bellard | "C-a h print this help\n"
|
1700 | 9dc39cba | bellard | "C-a x exit emulatior\n"
|
1701 | 9dc39cba | bellard | "C-a s save disk data back to file (if -snapshot)\n"
|
1702 | 9dc39cba | bellard | "C-a b send break (magic sysrq)\n"
|
1703 | 9dc39cba | bellard | "C-a c switch between console and monitor\n"
|
1704 | 9dc39cba | bellard | "C-a C-a send C-a\n"
|
1705 | 9dc39cba | bellard | ); |
1706 | 9dc39cba | bellard | } |
1707 | 9dc39cba | bellard | |
1708 | 9dc39cba | bellard | /* called when a char is received */
|
1709 | 9dc39cba | bellard | static void term_received_byte(int ch) |
1710 | 9dc39cba | bellard | { |
1711 | 9dc39cba | bellard | if (!serial_console) {
|
1712 | 9dc39cba | bellard | /* if no serial console, handle every command */
|
1713 | 9dc39cba | bellard | term_handle_byte(ch); |
1714 | 9dc39cba | bellard | } else {
|
1715 | 9dc39cba | bellard | if (term_got_escape) {
|
1716 | 9dc39cba | bellard | term_got_escape = 0;
|
1717 | 9dc39cba | bellard | switch(ch) {
|
1718 | 9dc39cba | bellard | case 'h': |
1719 | 9dc39cba | bellard | term_print_help(); |
1720 | 9dc39cba | bellard | break;
|
1721 | 9dc39cba | bellard | case 'x': |
1722 | 9dc39cba | bellard | exit(0);
|
1723 | 9dc39cba | bellard | break;
|
1724 | 9dc39cba | bellard | case 's': |
1725 | 9dc39cba | bellard | { |
1726 | 9dc39cba | bellard | int i;
|
1727 | 9dc39cba | bellard | for (i = 0; i < MAX_DISKS; i++) { |
1728 | 9dc39cba | bellard | if (bs_table[i])
|
1729 | 9dc39cba | bellard | bdrv_commit(bs_table[i]); |
1730 | 9dc39cba | bellard | } |
1731 | 9dc39cba | bellard | } |
1732 | 9dc39cba | bellard | break;
|
1733 | 9dc39cba | bellard | case 'b': |
1734 | 9dc39cba | bellard | if (serial_console)
|
1735 | 9dc39cba | bellard | serial_receive_break(serial_console); |
1736 | 9dc39cba | bellard | break;
|
1737 | 9dc39cba | bellard | case 'c': |
1738 | 9dc39cba | bellard | if (!term_command) {
|
1739 | 9dc39cba | bellard | term_show_prompt(); |
1740 | 9dc39cba | bellard | term_command = 1;
|
1741 | 9dc39cba | bellard | } else {
|
1742 | 9dc39cba | bellard | term_command = 0;
|
1743 | 9dc39cba | bellard | } |
1744 | 9dc39cba | bellard | break;
|
1745 | 9dc39cba | bellard | case TERM_ESCAPE:
|
1746 | 9dc39cba | bellard | goto send_char;
|
1747 | 9dc39cba | bellard | } |
1748 | 9dc39cba | bellard | } else if (ch == TERM_ESCAPE) { |
1749 | 9dc39cba | bellard | term_got_escape = 1;
|
1750 | 9dc39cba | bellard | } else {
|
1751 | 9dc39cba | bellard | send_char:
|
1752 | 9dc39cba | bellard | if (term_command) {
|
1753 | 9dc39cba | bellard | term_handle_byte(ch); |
1754 | 9dc39cba | bellard | } else {
|
1755 | 9dc39cba | bellard | if (serial_console)
|
1756 | 9dc39cba | bellard | serial_receive_byte(serial_console, ch); |
1757 | 9dc39cba | bellard | } |
1758 | 9dc39cba | bellard | } |
1759 | 9dc39cba | bellard | } |
1760 | 9dc39cba | bellard | } |
1761 | 9dc39cba | bellard | |
1762 | 9dc39cba | bellard | static int term_can_read(void *opaque) |
1763 | 9dc39cba | bellard | { |
1764 | 9dc39cba | bellard | if (serial_console) {
|
1765 | 9dc39cba | bellard | return serial_can_receive(serial_console);
|
1766 | 9dc39cba | bellard | } else {
|
1767 | f193c797 | bellard | return 128; |
1768 | 9dc39cba | bellard | } |
1769 | 9dc39cba | bellard | } |
1770 | 9dc39cba | bellard | |
1771 | 9dc39cba | bellard | static void term_read(void *opaque, const uint8_t *buf, int size) |
1772 | 9dc39cba | bellard | { |
1773 | 9dc39cba | bellard | int i;
|
1774 | 9dc39cba | bellard | for(i = 0; i < size; i++) |
1775 | 9dc39cba | bellard | term_received_byte(buf[i]); |
1776 | 9dc39cba | bellard | } |
1777 | 9dc39cba | bellard | |
1778 | 9dc39cba | bellard | void monitor_init(void) |
1779 | 9dc39cba | bellard | { |
1780 | 9dc39cba | bellard | if (!serial_console) {
|
1781 | 9dc39cba | bellard | term_printf("QEMU %s monitor - type 'help' for more information\n",
|
1782 | 9dc39cba | bellard | QEMU_VERSION); |
1783 | 9dc39cba | bellard | term_show_prompt(); |
1784 | 9dc39cba | bellard | } |
1785 | aa455485 | bellard | term_hist_entry = -1;
|
1786 | 8a7ddc38 | bellard | qemu_add_fd_read_handler(0, term_can_read, term_read, NULL); |
1787 | 9dc39cba | bellard | } |