]>
Commit | Line | Data |
---|---|---|
9dc39cba FB |
1 | /* |
2 | * QEMU monitor | |
5fafdf24 | 3 | * |
9dc39cba | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
9dc39cba FB |
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 | */ | |
87ecb68b PB |
24 | #include "hw/hw.h" |
25 | #include "hw/usb.h" | |
26 | #include "hw/pcmcia.h" | |
27 | #include "hw/pc.h" | |
28 | #include "hw/pci.h" | |
29 | #include "gdbstub.h" | |
30 | #include "net.h" | |
31 | #include "qemu-char.h" | |
32 | #include "sysemu.h" | |
33 | #include "console.h" | |
34 | #include "block.h" | |
35 | #include "audio/audio.h" | |
9307c4c1 | 36 | #include "disas.h" |
81d0912d | 37 | #include <dirent.h> |
c8256f9d | 38 | #include "qemu-timer.h" |
5bb7910a | 39 | #include "migration.h" |
6a5bd307 | 40 | |
9dc39cba | 41 | //#define DEBUG |
81d0912d | 42 | //#define DEBUG_COMPLETION |
9dc39cba | 43 | |
9307c4c1 FB |
44 | /* |
45 | * Supported types: | |
5fafdf24 | 46 | * |
9307c4c1 | 47 | * 'F' filename |
81d0912d | 48 | * 'B' block device name |
9307c4c1 | 49 | * 's' string (accept optional quote) |
92a31b1f FB |
50 | * 'i' 32 bit integer |
51 | * 'l' target long (32 or 64 bit) | |
9307c4c1 FB |
52 | * '/' optional gdb-like print format (like "/10x") |
53 | * | |
54 | * '?' optional type (for 'F', 's' and 'i') | |
55 | * | |
56 | */ | |
57 | ||
9dc39cba FB |
58 | typedef struct term_cmd_t { |
59 | const char *name; | |
9307c4c1 | 60 | const char *args_type; |
a5f1b965 | 61 | void *handler; |
9dc39cba FB |
62 | const char *params; |
63 | const char *help; | |
64 | } term_cmd_t; | |
65 | ||
20d8a3ed TS |
66 | #define MAX_MON 4 |
67 | static CharDriverState *monitor_hd[MAX_MON]; | |
86e94dea | 68 | static int hide_banner; |
7e2515e8 | 69 | |
8662d656 BS |
70 | static const term_cmd_t term_cmds[]; |
71 | static const term_cmd_t info_cmds[]; | |
9dc39cba | 72 | |
60fe76f3 | 73 | static uint8_t term_outbuf[1024]; |
7e2515e8 | 74 | static int term_outbuf_index; |
81d0912d | 75 | |
83ab7950 AL |
76 | static void monitor_start_input(void); |
77 | ||
b1d8e52e | 78 | static CPUState *mon_cpu = NULL; |
6a00d601 | 79 | |
7e2515e8 FB |
80 | void term_flush(void) |
81 | { | |
20d8a3ed | 82 | int i; |
7e2515e8 | 83 | if (term_outbuf_index > 0) { |
20d8a3ed TS |
84 | for (i = 0; i < MAX_MON; i++) |
85 | if (monitor_hd[i] && monitor_hd[i]->focus == 0) | |
86 | qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index); | |
7e2515e8 FB |
87 | term_outbuf_index = 0; |
88 | } | |
89 | } | |
90 | ||
91 | /* flush at every end of line or if the buffer is full */ | |
92 | void term_puts(const char *str) | |
93 | { | |
60fe76f3 | 94 | char c; |
7e2515e8 FB |
95 | for(;;) { |
96 | c = *str++; | |
97 | if (c == '\0') | |
98 | break; | |
7ba1260a FB |
99 | if (c == '\n') |
100 | term_outbuf[term_outbuf_index++] = '\r'; | |
7e2515e8 | 101 | term_outbuf[term_outbuf_index++] = c; |
7ba1260a | 102 | if (term_outbuf_index >= (sizeof(term_outbuf) - 1) || |
7e2515e8 FB |
103 | c == '\n') |
104 | term_flush(); | |
105 | } | |
106 | } | |
107 | ||
108 | void term_vprintf(const char *fmt, va_list ap) | |
9dc39cba | 109 | { |
81d0912d | 110 | char buf[4096]; |
81d0912d | 111 | vsnprintf(buf, sizeof(buf), fmt, ap); |
7e2515e8 | 112 | term_puts(buf); |
9dc39cba FB |
113 | } |
114 | ||
7e2515e8 | 115 | void term_printf(const char *fmt, ...) |
9dc39cba | 116 | { |
7e2515e8 FB |
117 | va_list ap; |
118 | va_start(ap, fmt); | |
119 | term_vprintf(fmt, ap); | |
120 | va_end(ap); | |
9dc39cba FB |
121 | } |
122 | ||
fef30743 TS |
123 | void term_print_filename(const char *filename) |
124 | { | |
125 | int i; | |
126 | ||
127 | for (i = 0; filename[i]; i++) { | |
128 | switch (filename[i]) { | |
129 | case ' ': | |
130 | case '"': | |
131 | case '\\': | |
132 | term_printf("\\%c", filename[i]); | |
133 | break; | |
134 | case '\t': | |
135 | term_printf("\\t"); | |
136 | break; | |
137 | case '\r': | |
138 | term_printf("\\r"); | |
139 | break; | |
140 | case '\n': | |
141 | term_printf("\\n"); | |
142 | break; | |
143 | default: | |
144 | term_printf("%c", filename[i]); | |
145 | break; | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
7fe48483 FB |
150 | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
151 | { | |
152 | va_list ap; | |
153 | va_start(ap, fmt); | |
154 | term_vprintf(fmt, ap); | |
155 | va_end(ap); | |
156 | return 0; | |
157 | } | |
158 | ||
9dc39cba FB |
159 | static int compare_cmd(const char *name, const char *list) |
160 | { | |
161 | const char *p, *pstart; | |
162 | int len; | |
163 | len = strlen(name); | |
164 | p = list; | |
165 | for(;;) { | |
166 | pstart = p; | |
167 | p = strchr(p, '|'); | |
168 | if (!p) | |
169 | p = pstart + strlen(pstart); | |
170 | if ((p - pstart) == len && !memcmp(pstart, name, len)) | |
171 | return 1; | |
172 | if (*p == '\0') | |
173 | break; | |
174 | p++; | |
175 | } | |
176 | return 0; | |
177 | } | |
178 | ||
8662d656 | 179 | static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name) |
9dc39cba | 180 | { |
8662d656 | 181 | const term_cmd_t *cmd; |
9dc39cba FB |
182 | |
183 | for(cmd = cmds; cmd->name != NULL; cmd++) { | |
184 | if (!name || !strcmp(name, cmd->name)) | |
185 | term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help); | |
186 | } | |
187 | } | |
188 | ||
189 | static void help_cmd(const char *name) | |
190 | { | |
191 | if (name && !strcmp(name, "info")) { | |
192 | help_cmd1(info_cmds, "info ", NULL); | |
193 | } else { | |
194 | help_cmd1(term_cmds, "", name); | |
f193c797 | 195 | if (name && !strcmp(name, "log")) { |
8662d656 | 196 | const CPULogItem *item; |
f193c797 FB |
197 | term_printf("Log items (comma separated):\n"); |
198 | term_printf("%-10s %s\n", "none", "remove all logs"); | |
199 | for(item = cpu_log_items; item->mask != 0; item++) { | |
200 | term_printf("%-10s %s\n", item->name, item->help); | |
201 | } | |
202 | } | |
9dc39cba FB |
203 | } |
204 | } | |
205 | ||
9307c4c1 | 206 | static void do_help(const char *name) |
9dc39cba | 207 | { |
9307c4c1 | 208 | help_cmd(name); |
9dc39cba FB |
209 | } |
210 | ||
7954c734 | 211 | static void do_commit(const char *device) |
9dc39cba | 212 | { |
7954c734 | 213 | int i, all_devices; |
2dc7b602 | 214 | |
7954c734 | 215 | all_devices = !strcmp(device, "all"); |
e4bcb14c | 216 | for (i = 0; i < nb_drives; i++) { |
5fafdf24 | 217 | if (all_devices || |
e4bcb14c TS |
218 | !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device)) |
219 | bdrv_commit(drives_table[i].bdrv); | |
9dc39cba FB |
220 | } |
221 | } | |
222 | ||
9307c4c1 | 223 | static void do_info(const char *item) |
9dc39cba | 224 | { |
8662d656 | 225 | const term_cmd_t *cmd; |
a5f1b965 | 226 | void (*handler)(void); |
9dc39cba | 227 | |
9307c4c1 | 228 | if (!item) |
9dc39cba | 229 | goto help; |
9dc39cba | 230 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
5fafdf24 | 231 | if (compare_cmd(item, cmd->name)) |
9dc39cba FB |
232 | goto found; |
233 | } | |
234 | help: | |
9307c4c1 | 235 | help_cmd("info"); |
9dc39cba FB |
236 | return; |
237 | found: | |
a5f1b965 BS |
238 | handler = cmd->handler; |
239 | handler(); | |
9dc39cba FB |
240 | } |
241 | ||
9bc9d1c7 FB |
242 | static void do_info_version(void) |
243 | { | |
244 | term_printf("%s\n", QEMU_VERSION); | |
245 | } | |
246 | ||
c35734b2 TS |
247 | static void do_info_name(void) |
248 | { | |
249 | if (qemu_name) | |
250 | term_printf("%s\n", qemu_name); | |
251 | } | |
252 | ||
f1f23ad5 BS |
253 | static void do_info_uuid(void) |
254 | { | |
255 | term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2], | |
256 | qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6], | |
257 | qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10], | |
258 | qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14], | |
259 | qemu_uuid[15]); | |
260 | } | |
261 | ||
9307c4c1 | 262 | static void do_info_block(void) |
9dc39cba FB |
263 | { |
264 | bdrv_info(); | |
265 | } | |
266 | ||
a36e69dd TS |
267 | static void do_info_blockstats(void) |
268 | { | |
269 | bdrv_info_stats(); | |
270 | } | |
271 | ||
6a00d601 | 272 | /* get the current CPU defined by the user */ |
9596ebb7 | 273 | static int mon_set_cpu(int cpu_index) |
6a00d601 FB |
274 | { |
275 | CPUState *env; | |
276 | ||
277 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
278 | if (env->cpu_index == cpu_index) { | |
279 | mon_cpu = env; | |
280 | return 0; | |
281 | } | |
282 | } | |
283 | return -1; | |
284 | } | |
285 | ||
9596ebb7 | 286 | static CPUState *mon_get_cpu(void) |
6a00d601 FB |
287 | { |
288 | if (!mon_cpu) { | |
289 | mon_set_cpu(0); | |
290 | } | |
291 | return mon_cpu; | |
292 | } | |
293 | ||
9307c4c1 FB |
294 | static void do_info_registers(void) |
295 | { | |
6a00d601 FB |
296 | CPUState *env; |
297 | env = mon_get_cpu(); | |
298 | if (!env) | |
299 | return; | |
9307c4c1 | 300 | #ifdef TARGET_I386 |
6a00d601 | 301 | cpu_dump_state(env, NULL, monitor_fprintf, |
d24b15a8 | 302 | X86_DUMP_FPU); |
9307c4c1 | 303 | #else |
5fafdf24 | 304 | cpu_dump_state(env, NULL, monitor_fprintf, |
7fe48483 | 305 | 0); |
9307c4c1 FB |
306 | #endif |
307 | } | |
308 | ||
6a00d601 FB |
309 | static void do_info_cpus(void) |
310 | { | |
311 | CPUState *env; | |
312 | ||
313 | /* just to set the default cpu if not already done */ | |
314 | mon_get_cpu(); | |
315 | ||
316 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
5fafdf24 | 317 | term_printf("%c CPU #%d:", |
6a00d601 FB |
318 | (env == mon_cpu) ? '*' : ' ', |
319 | env->cpu_index); | |
320 | #if defined(TARGET_I386) | |
321 | term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base); | |
e80e1cc4 FB |
322 | #elif defined(TARGET_PPC) |
323 | term_printf(" nip=0x" TARGET_FMT_lx, env->nip); | |
ba3c64fb FB |
324 | #elif defined(TARGET_SPARC) |
325 | term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc); | |
ead9360e | 326 | #elif defined(TARGET_MIPS) |
b5dc7732 | 327 | term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC); |
ce5232c5 | 328 | #endif |
ead9360e TS |
329 | if (env->halted) |
330 | term_printf(" (halted)"); | |
6a00d601 FB |
331 | term_printf("\n"); |
332 | } | |
333 | } | |
334 | ||
335 | static void do_cpu_set(int index) | |
336 | { | |
337 | if (mon_set_cpu(index) < 0) | |
338 | term_printf("Invalid CPU index\n"); | |
339 | } | |
340 | ||
e3db7226 FB |
341 | static void do_info_jit(void) |
342 | { | |
343 | dump_exec_info(NULL, monitor_fprintf); | |
344 | } | |
345 | ||
aa455485 FB |
346 | static void do_info_history (void) |
347 | { | |
348 | int i; | |
7e2515e8 | 349 | const char *str; |
3b46e624 | 350 | |
7e2515e8 FB |
351 | i = 0; |
352 | for(;;) { | |
353 | str = readline_get_history(i); | |
354 | if (!str) | |
355 | break; | |
356 | term_printf("%d: '%s'\n", i, str); | |
8e3a9fd2 | 357 | i++; |
aa455485 FB |
358 | } |
359 | } | |
360 | ||
76a66253 JM |
361 | #if defined(TARGET_PPC) |
362 | /* XXX: not implemented in other targets */ | |
363 | static void do_info_cpu_stats (void) | |
364 | { | |
365 | CPUState *env; | |
366 | ||
367 | env = mon_get_cpu(); | |
368 | cpu_dump_statistics(env, NULL, &monitor_fprintf, 0); | |
369 | } | |
370 | #endif | |
371 | ||
9307c4c1 | 372 | static void do_quit(void) |
9dc39cba FB |
373 | { |
374 | exit(0); | |
375 | } | |
376 | ||
377 | static int eject_device(BlockDriverState *bs, int force) | |
378 | { | |
379 | if (bdrv_is_inserted(bs)) { | |
380 | if (!force) { | |
381 | if (!bdrv_is_removable(bs)) { | |
382 | term_printf("device is not removable\n"); | |
383 | return -1; | |
384 | } | |
385 | if (bdrv_is_locked(bs)) { | |
386 | term_printf("device is locked\n"); | |
387 | return -1; | |
388 | } | |
389 | } | |
390 | bdrv_close(bs); | |
391 | } | |
392 | return 0; | |
393 | } | |
394 | ||
9307c4c1 | 395 | static void do_eject(int force, const char *filename) |
9dc39cba FB |
396 | { |
397 | BlockDriverState *bs; | |
9dc39cba | 398 | |
9307c4c1 | 399 | bs = bdrv_find(filename); |
9dc39cba FB |
400 | if (!bs) { |
401 | term_printf("device not found\n"); | |
402 | return; | |
403 | } | |
404 | eject_device(bs, force); | |
405 | } | |
406 | ||
2ecea9b8 | 407 | static void do_change_block(const char *device, const char *filename, const char *fmt) |
9dc39cba FB |
408 | { |
409 | BlockDriverState *bs; | |
2ecea9b8 | 410 | BlockDriver *drv = NULL; |
9dc39cba | 411 | |
9307c4c1 | 412 | bs = bdrv_find(device); |
9dc39cba FB |
413 | if (!bs) { |
414 | term_printf("device not found\n"); | |
415 | return; | |
416 | } | |
2ecea9b8 AJ |
417 | if (fmt) { |
418 | drv = bdrv_find_format(fmt); | |
419 | if (!drv) { | |
420 | term_printf("invalid format %s\n", fmt); | |
421 | return; | |
422 | } | |
423 | } | |
9dc39cba FB |
424 | if (eject_device(bs, 0) < 0) |
425 | return; | |
2ecea9b8 | 426 | bdrv_open2(bs, filename, 0, drv); |
2bac6019 | 427 | qemu_key_check(bs, filename); |
9dc39cba FB |
428 | } |
429 | ||
e25a5822 TS |
430 | static void do_change_vnc(const char *target) |
431 | { | |
70848515 TS |
432 | if (strcmp(target, "passwd") == 0 || |
433 | strcmp(target, "password") == 0) { | |
434 | char password[9]; | |
435 | monitor_readline("Password: ", 1, password, sizeof(password)-1); | |
436 | password[sizeof(password)-1] = '\0'; | |
437 | if (vnc_display_password(NULL, password) < 0) | |
438 | term_printf("could not set VNC server password\n"); | |
439 | } else { | |
440 | if (vnc_display_open(NULL, target) < 0) | |
441 | term_printf("could not start VNC server on %s\n", target); | |
442 | } | |
e25a5822 TS |
443 | } |
444 | ||
2ecea9b8 | 445 | static void do_change(const char *device, const char *target, const char *fmt) |
e25a5822 TS |
446 | { |
447 | if (strcmp(device, "vnc") == 0) { | |
448 | do_change_vnc(target); | |
449 | } else { | |
2ecea9b8 | 450 | do_change_block(device, target, fmt); |
e25a5822 TS |
451 | } |
452 | } | |
453 | ||
9307c4c1 | 454 | static void do_screen_dump(const char *filename) |
59a983b9 | 455 | { |
95219897 | 456 | vga_hw_screen_dump(filename); |
59a983b9 FB |
457 | } |
458 | ||
e735b91c PB |
459 | static void do_logfile(const char *filename) |
460 | { | |
461 | cpu_set_log_filename(filename); | |
462 | } | |
463 | ||
9307c4c1 | 464 | static void do_log(const char *items) |
f193c797 FB |
465 | { |
466 | int mask; | |
3b46e624 | 467 | |
9307c4c1 | 468 | if (!strcmp(items, "none")) { |
f193c797 FB |
469 | mask = 0; |
470 | } else { | |
9307c4c1 | 471 | mask = cpu_str_to_log_mask(items); |
f193c797 | 472 | if (!mask) { |
9307c4c1 | 473 | help_cmd("log"); |
f193c797 FB |
474 | return; |
475 | } | |
476 | } | |
477 | cpu_set_log(mask); | |
478 | } | |
479 | ||
9307c4c1 | 480 | static void do_stop(void) |
8a7ddc38 FB |
481 | { |
482 | vm_stop(EXCP_INTERRUPT); | |
483 | } | |
484 | ||
9307c4c1 | 485 | static void do_cont(void) |
8a7ddc38 FB |
486 | { |
487 | vm_start(); | |
488 | } | |
489 | ||
67b915a5 | 490 | #ifdef CONFIG_GDBSTUB |
cfc3475a | 491 | static void do_gdbserver(const char *port) |
8a7ddc38 | 492 | { |
cfc3475a | 493 | if (!port) |
9307c4c1 | 494 | port = DEFAULT_GDBSTUB_PORT; |
cfc3475a PB |
495 | if (gdbserver_start(port) < 0) { |
496 | qemu_printf("Could not open gdbserver socket on port '%s'\n", port); | |
8a7ddc38 | 497 | } else { |
cfc3475a | 498 | qemu_printf("Waiting gdb connection on port '%s'\n", port); |
8a7ddc38 FB |
499 | } |
500 | } | |
67b915a5 | 501 | #endif |
8a7ddc38 | 502 | |
9307c4c1 FB |
503 | static void term_printc(int c) |
504 | { | |
505 | term_printf("'"); | |
506 | switch(c) { | |
507 | case '\'': | |
508 | term_printf("\\'"); | |
509 | break; | |
510 | case '\\': | |
511 | term_printf("\\\\"); | |
512 | break; | |
513 | case '\n': | |
514 | term_printf("\\n"); | |
515 | break; | |
516 | case '\r': | |
517 | term_printf("\\r"); | |
518 | break; | |
519 | default: | |
520 | if (c >= 32 && c <= 126) { | |
521 | term_printf("%c", c); | |
522 | } else { | |
523 | term_printf("\\x%02x", c); | |
524 | } | |
525 | break; | |
526 | } | |
527 | term_printf("'"); | |
528 | } | |
529 | ||
5fafdf24 | 530 | static void memory_dump(int count, int format, int wsize, |
7743e588 | 531 | target_phys_addr_t addr, int is_physical) |
9307c4c1 | 532 | { |
6a00d601 | 533 | CPUState *env; |
9307c4c1 FB |
534 | int nb_per_line, l, line_size, i, max_digits, len; |
535 | uint8_t buf[16]; | |
536 | uint64_t v; | |
537 | ||
538 | if (format == 'i') { | |
539 | int flags; | |
540 | flags = 0; | |
6a00d601 FB |
541 | env = mon_get_cpu(); |
542 | if (!env && !is_physical) | |
543 | return; | |
9307c4c1 | 544 | #ifdef TARGET_I386 |
4c27ba27 | 545 | if (wsize == 2) { |
9307c4c1 | 546 | flags = 1; |
4c27ba27 FB |
547 | } else if (wsize == 4) { |
548 | flags = 0; | |
549 | } else { | |
6a15fd12 | 550 | /* as default we use the current CS size */ |
4c27ba27 | 551 | flags = 0; |
6a15fd12 FB |
552 | if (env) { |
553 | #ifdef TARGET_X86_64 | |
5fafdf24 | 554 | if ((env->efer & MSR_EFER_LMA) && |
6a15fd12 FB |
555 | (env->segs[R_CS].flags & DESC_L_MASK)) |
556 | flags = 2; | |
557 | else | |
558 | #endif | |
559 | if (!(env->segs[R_CS].flags & DESC_B_MASK)) | |
560 | flags = 1; | |
561 | } | |
4c27ba27 FB |
562 | } |
563 | #endif | |
6a00d601 | 564 | monitor_disas(env, addr, count, is_physical, flags); |
9307c4c1 FB |
565 | return; |
566 | } | |
567 | ||
568 | len = wsize * count; | |
569 | if (wsize == 1) | |
570 | line_size = 8; | |
571 | else | |
572 | line_size = 16; | |
573 | nb_per_line = line_size / wsize; | |
574 | max_digits = 0; | |
575 | ||
576 | switch(format) { | |
577 | case 'o': | |
578 | max_digits = (wsize * 8 + 2) / 3; | |
579 | break; | |
580 | default: | |
581 | case 'x': | |
582 | max_digits = (wsize * 8) / 4; | |
583 | break; | |
584 | case 'u': | |
585 | case 'd': | |
586 | max_digits = (wsize * 8 * 10 + 32) / 33; | |
587 | break; | |
588 | case 'c': | |
589 | wsize = 1; | |
590 | break; | |
591 | } | |
592 | ||
593 | while (len > 0) { | |
7743e588 BS |
594 | if (is_physical) |
595 | term_printf(TARGET_FMT_plx ":", addr); | |
596 | else | |
597 | term_printf(TARGET_FMT_lx ":", (target_ulong)addr); | |
9307c4c1 FB |
598 | l = len; |
599 | if (l > line_size) | |
600 | l = line_size; | |
601 | if (is_physical) { | |
602 | cpu_physical_memory_rw(addr, buf, l, 0); | |
603 | } else { | |
6a00d601 FB |
604 | env = mon_get_cpu(); |
605 | if (!env) | |
606 | break; | |
c8f79b67 AL |
607 | if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) { |
608 | term_printf(" Cannot access memory\n"); | |
609 | break; | |
610 | } | |
9307c4c1 | 611 | } |
5fafdf24 | 612 | i = 0; |
9307c4c1 FB |
613 | while (i < l) { |
614 | switch(wsize) { | |
615 | default: | |
616 | case 1: | |
617 | v = ldub_raw(buf + i); | |
618 | break; | |
619 | case 2: | |
620 | v = lduw_raw(buf + i); | |
621 | break; | |
622 | case 4: | |
92a31b1f | 623 | v = (uint32_t)ldl_raw(buf + i); |
9307c4c1 FB |
624 | break; |
625 | case 8: | |
626 | v = ldq_raw(buf + i); | |
627 | break; | |
628 | } | |
629 | term_printf(" "); | |
630 | switch(format) { | |
631 | case 'o': | |
26a76461 | 632 | term_printf("%#*" PRIo64, max_digits, v); |
9307c4c1 FB |
633 | break; |
634 | case 'x': | |
26a76461 | 635 | term_printf("0x%0*" PRIx64, max_digits, v); |
9307c4c1 FB |
636 | break; |
637 | case 'u': | |
26a76461 | 638 | term_printf("%*" PRIu64, max_digits, v); |
9307c4c1 FB |
639 | break; |
640 | case 'd': | |
26a76461 | 641 | term_printf("%*" PRId64, max_digits, v); |
9307c4c1 FB |
642 | break; |
643 | case 'c': | |
644 | term_printc(v); | |
645 | break; | |
646 | } | |
647 | i += wsize; | |
648 | } | |
649 | term_printf("\n"); | |
650 | addr += l; | |
651 | len -= l; | |
652 | } | |
653 | } | |
654 | ||
92a31b1f FB |
655 | #if TARGET_LONG_BITS == 64 |
656 | #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l)) | |
657 | #else | |
658 | #define GET_TLONG(h, l) (l) | |
659 | #endif | |
660 | ||
5fafdf24 | 661 | static void do_memory_dump(int count, int format, int size, |
92a31b1f | 662 | uint32_t addrh, uint32_t addrl) |
9307c4c1 | 663 | { |
92a31b1f | 664 | target_long addr = GET_TLONG(addrh, addrl); |
9307c4c1 FB |
665 | memory_dump(count, format, size, addr, 0); |
666 | } | |
667 | ||
7743e588 BS |
668 | #if TARGET_PHYS_ADDR_BITS > 32 |
669 | #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l)) | |
670 | #else | |
671 | #define GET_TPHYSADDR(h, l) (l) | |
672 | #endif | |
673 | ||
92a31b1f FB |
674 | static void do_physical_memory_dump(int count, int format, int size, |
675 | uint32_t addrh, uint32_t addrl) | |
676 | ||
9307c4c1 | 677 | { |
7743e588 | 678 | target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl); |
9307c4c1 FB |
679 | memory_dump(count, format, size, addr, 1); |
680 | } | |
681 | ||
92a31b1f | 682 | static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall) |
9307c4c1 | 683 | { |
7743e588 BS |
684 | target_phys_addr_t val = GET_TPHYSADDR(valh, vall); |
685 | #if TARGET_PHYS_ADDR_BITS == 32 | |
9307c4c1 FB |
686 | switch(format) { |
687 | case 'o': | |
688 | term_printf("%#o", val); | |
689 | break; | |
690 | case 'x': | |
691 | term_printf("%#x", val); | |
692 | break; | |
693 | case 'u': | |
694 | term_printf("%u", val); | |
695 | break; | |
696 | default: | |
697 | case 'd': | |
698 | term_printf("%d", val); | |
699 | break; | |
700 | case 'c': | |
701 | term_printc(val); | |
702 | break; | |
703 | } | |
92a31b1f FB |
704 | #else |
705 | switch(format) { | |
706 | case 'o': | |
26a76461 | 707 | term_printf("%#" PRIo64, val); |
92a31b1f FB |
708 | break; |
709 | case 'x': | |
26a76461 | 710 | term_printf("%#" PRIx64, val); |
92a31b1f FB |
711 | break; |
712 | case 'u': | |
26a76461 | 713 | term_printf("%" PRIu64, val); |
92a31b1f FB |
714 | break; |
715 | default: | |
716 | case 'd': | |
26a76461 | 717 | term_printf("%" PRId64, val); |
92a31b1f FB |
718 | break; |
719 | case 'c': | |
720 | term_printc(val); | |
721 | break; | |
722 | } | |
723 | #endif | |
9307c4c1 FB |
724 | term_printf("\n"); |
725 | } | |
726 | ||
5fafdf24 | 727 | static void do_memory_save(unsigned int valh, unsigned int vall, |
b371dc59 FB |
728 | uint32_t size, const char *filename) |
729 | { | |
730 | FILE *f; | |
731 | target_long addr = GET_TLONG(valh, vall); | |
732 | uint32_t l; | |
733 | CPUState *env; | |
734 | uint8_t buf[1024]; | |
735 | ||
736 | env = mon_get_cpu(); | |
737 | if (!env) | |
738 | return; | |
739 | ||
740 | f = fopen(filename, "wb"); | |
741 | if (!f) { | |
742 | term_printf("could not open '%s'\n", filename); | |
743 | return; | |
744 | } | |
745 | while (size != 0) { | |
746 | l = sizeof(buf); | |
747 | if (l > size) | |
748 | l = size; | |
749 | cpu_memory_rw_debug(env, addr, buf, l, 0); | |
750 | fwrite(buf, 1, l, f); | |
751 | addr += l; | |
752 | size -= l; | |
753 | } | |
754 | fclose(f); | |
755 | } | |
756 | ||
a8bdf7a6 AJ |
757 | static void do_physical_memory_save(unsigned int valh, unsigned int vall, |
758 | uint32_t size, const char *filename) | |
759 | { | |
760 | FILE *f; | |
761 | uint32_t l; | |
762 | uint8_t buf[1024]; | |
339dea27 | 763 | target_phys_addr_t addr = GET_TPHYSADDR(valh, vall); |
a8bdf7a6 AJ |
764 | |
765 | f = fopen(filename, "wb"); | |
766 | if (!f) { | |
767 | term_printf("could not open '%s'\n", filename); | |
768 | return; | |
769 | } | |
770 | while (size != 0) { | |
771 | l = sizeof(buf); | |
772 | if (l > size) | |
773 | l = size; | |
774 | cpu_physical_memory_rw(addr, buf, l, 0); | |
775 | fwrite(buf, 1, l, f); | |
776 | fflush(f); | |
777 | addr += l; | |
778 | size -= l; | |
779 | } | |
780 | fclose(f); | |
781 | } | |
782 | ||
e4cf1adc FB |
783 | static void do_sum(uint32_t start, uint32_t size) |
784 | { | |
785 | uint32_t addr; | |
786 | uint8_t buf[1]; | |
787 | uint16_t sum; | |
788 | ||
789 | sum = 0; | |
790 | for(addr = start; addr < (start + size); addr++) { | |
791 | cpu_physical_memory_rw(addr, buf, 1, 0); | |
792 | /* BSD sum algorithm ('sum' Unix command) */ | |
793 | sum = (sum >> 1) | (sum << 15); | |
794 | sum += buf[0]; | |
795 | } | |
796 | term_printf("%05d\n", sum); | |
797 | } | |
798 | ||
a3a91a35 FB |
799 | typedef struct { |
800 | int keycode; | |
801 | const char *name; | |
802 | } KeyDef; | |
803 | ||
804 | static const KeyDef key_defs[] = { | |
805 | { 0x2a, "shift" }, | |
806 | { 0x36, "shift_r" }, | |
3b46e624 | 807 | |
a3a91a35 FB |
808 | { 0x38, "alt" }, |
809 | { 0xb8, "alt_r" }, | |
2ba27c7f TS |
810 | { 0x64, "altgr" }, |
811 | { 0xe4, "altgr_r" }, | |
a3a91a35 FB |
812 | { 0x1d, "ctrl" }, |
813 | { 0x9d, "ctrl_r" }, | |
814 | ||
815 | { 0xdd, "menu" }, | |
816 | ||
817 | { 0x01, "esc" }, | |
818 | ||
819 | { 0x02, "1" }, | |
820 | { 0x03, "2" }, | |
821 | { 0x04, "3" }, | |
822 | { 0x05, "4" }, | |
823 | { 0x06, "5" }, | |
824 | { 0x07, "6" }, | |
825 | { 0x08, "7" }, | |
826 | { 0x09, "8" }, | |
827 | { 0x0a, "9" }, | |
828 | { 0x0b, "0" }, | |
64866c3d FB |
829 | { 0x0c, "minus" }, |
830 | { 0x0d, "equal" }, | |
a3a91a35 FB |
831 | { 0x0e, "backspace" }, |
832 | ||
833 | { 0x0f, "tab" }, | |
834 | { 0x10, "q" }, | |
835 | { 0x11, "w" }, | |
836 | { 0x12, "e" }, | |
837 | { 0x13, "r" }, | |
838 | { 0x14, "t" }, | |
839 | { 0x15, "y" }, | |
840 | { 0x16, "u" }, | |
841 | { 0x17, "i" }, | |
842 | { 0x18, "o" }, | |
843 | { 0x19, "p" }, | |
844 | ||
845 | { 0x1c, "ret" }, | |
846 | ||
847 | { 0x1e, "a" }, | |
848 | { 0x1f, "s" }, | |
849 | { 0x20, "d" }, | |
850 | { 0x21, "f" }, | |
851 | { 0x22, "g" }, | |
852 | { 0x23, "h" }, | |
853 | { 0x24, "j" }, | |
854 | { 0x25, "k" }, | |
855 | { 0x26, "l" }, | |
856 | ||
857 | { 0x2c, "z" }, | |
858 | { 0x2d, "x" }, | |
859 | { 0x2e, "c" }, | |
860 | { 0x2f, "v" }, | |
861 | { 0x30, "b" }, | |
862 | { 0x31, "n" }, | |
863 | { 0x32, "m" }, | |
9155fc45 AJ |
864 | { 0x33, "comma" }, |
865 | { 0x34, "dot" }, | |
866 | { 0x35, "slash" }, | |
3b46e624 | 867 | |
4d3b6f6e AZ |
868 | { 0x37, "asterisk" }, |
869 | ||
a3a91a35 | 870 | { 0x39, "spc" }, |
00ffa62a | 871 | { 0x3a, "caps_lock" }, |
a3a91a35 FB |
872 | { 0x3b, "f1" }, |
873 | { 0x3c, "f2" }, | |
874 | { 0x3d, "f3" }, | |
875 | { 0x3e, "f4" }, | |
876 | { 0x3f, "f5" }, | |
877 | { 0x40, "f6" }, | |
878 | { 0x41, "f7" }, | |
879 | { 0x42, "f8" }, | |
880 | { 0x43, "f9" }, | |
881 | { 0x44, "f10" }, | |
00ffa62a | 882 | { 0x45, "num_lock" }, |
a3a91a35 FB |
883 | { 0x46, "scroll_lock" }, |
884 | ||
64866c3d FB |
885 | { 0xb5, "kp_divide" }, |
886 | { 0x37, "kp_multiply" }, | |
0cfec834 | 887 | { 0x4a, "kp_subtract" }, |
64866c3d FB |
888 | { 0x4e, "kp_add" }, |
889 | { 0x9c, "kp_enter" }, | |
890 | { 0x53, "kp_decimal" }, | |
f2289cb6 | 891 | { 0x54, "sysrq" }, |
64866c3d FB |
892 | |
893 | { 0x52, "kp_0" }, | |
894 | { 0x4f, "kp_1" }, | |
895 | { 0x50, "kp_2" }, | |
896 | { 0x51, "kp_3" }, | |
897 | { 0x4b, "kp_4" }, | |
898 | { 0x4c, "kp_5" }, | |
899 | { 0x4d, "kp_6" }, | |
900 | { 0x47, "kp_7" }, | |
901 | { 0x48, "kp_8" }, | |
902 | { 0x49, "kp_9" }, | |
3b46e624 | 903 | |
a3a91a35 FB |
904 | { 0x56, "<" }, |
905 | ||
906 | { 0x57, "f11" }, | |
907 | { 0x58, "f12" }, | |
908 | ||
909 | { 0xb7, "print" }, | |
910 | ||
911 | { 0xc7, "home" }, | |
912 | { 0xc9, "pgup" }, | |
913 | { 0xd1, "pgdn" }, | |
914 | { 0xcf, "end" }, | |
915 | ||
916 | { 0xcb, "left" }, | |
917 | { 0xc8, "up" }, | |
918 | { 0xd0, "down" }, | |
919 | { 0xcd, "right" }, | |
920 | ||
921 | { 0xd2, "insert" }, | |
922 | { 0xd3, "delete" }, | |
c0b5b109 BS |
923 | #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64) |
924 | { 0xf0, "stop" }, | |
925 | { 0xf1, "again" }, | |
926 | { 0xf2, "props" }, | |
927 | { 0xf3, "undo" }, | |
928 | { 0xf4, "front" }, | |
929 | { 0xf5, "copy" }, | |
930 | { 0xf6, "open" }, | |
931 | { 0xf7, "paste" }, | |
932 | { 0xf8, "find" }, | |
933 | { 0xf9, "cut" }, | |
934 | { 0xfa, "lf" }, | |
935 | { 0xfb, "help" }, | |
936 | { 0xfc, "meta_l" }, | |
937 | { 0xfd, "meta_r" }, | |
938 | { 0xfe, "compose" }, | |
939 | #endif | |
a3a91a35 FB |
940 | { 0, NULL }, |
941 | }; | |
942 | ||
943 | static int get_keycode(const char *key) | |
944 | { | |
945 | const KeyDef *p; | |
64866c3d FB |
946 | char *endp; |
947 | int ret; | |
a3a91a35 FB |
948 | |
949 | for(p = key_defs; p->name != NULL; p++) { | |
950 | if (!strcmp(key, p->name)) | |
951 | return p->keycode; | |
952 | } | |
64866c3d FB |
953 | if (strstart(key, "0x", NULL)) { |
954 | ret = strtoul(key, &endp, 0); | |
955 | if (*endp == '\0' && ret >= 0x01 && ret <= 0xff) | |
956 | return ret; | |
957 | } | |
a3a91a35 FB |
958 | return -1; |
959 | } | |
960 | ||
c8256f9d AZ |
961 | #define MAX_KEYCODES 16 |
962 | static uint8_t keycodes[MAX_KEYCODES]; | |
963 | static int nb_pending_keycodes; | |
964 | static QEMUTimer *key_timer; | |
965 | ||
966 | static void release_keys(void *opaque) | |
967 | { | |
968 | int keycode; | |
969 | ||
970 | while (nb_pending_keycodes > 0) { | |
971 | nb_pending_keycodes--; | |
972 | keycode = keycodes[nb_pending_keycodes]; | |
973 | if (keycode & 0x80) | |
974 | kbd_put_keycode(0xe0); | |
975 | kbd_put_keycode(keycode | 0x80); | |
976 | } | |
977 | } | |
978 | ||
979 | static void do_sendkey(const char *string, int has_hold_time, int hold_time) | |
a3a91a35 | 980 | { |
3401c0d9 AZ |
981 | char keyname_buf[16]; |
982 | char *separator; | |
983 | int keyname_len, keycode, i; | |
984 | ||
c8256f9d AZ |
985 | if (nb_pending_keycodes > 0) { |
986 | qemu_del_timer(key_timer); | |
987 | release_keys(NULL); | |
988 | } | |
989 | if (!has_hold_time) | |
990 | hold_time = 100; | |
991 | i = 0; | |
3401c0d9 AZ |
992 | while (1) { |
993 | separator = strchr(string, '-'); | |
994 | keyname_len = separator ? separator - string : strlen(string); | |
995 | if (keyname_len > 0) { | |
996 | pstrcpy(keyname_buf, sizeof(keyname_buf), string); | |
997 | if (keyname_len > sizeof(keyname_buf) - 1) { | |
998 | term_printf("invalid key: '%s...'\n", keyname_buf); | |
999 | return; | |
a3a91a35 | 1000 | } |
c8256f9d | 1001 | if (i == MAX_KEYCODES) { |
3401c0d9 AZ |
1002 | term_printf("too many keys\n"); |
1003 | return; | |
1004 | } | |
1005 | keyname_buf[keyname_len] = 0; | |
1006 | keycode = get_keycode(keyname_buf); | |
1007 | if (keycode < 0) { | |
1008 | term_printf("unknown key: '%s'\n", keyname_buf); | |
1009 | return; | |
1010 | } | |
c8256f9d | 1011 | keycodes[i++] = keycode; |
a3a91a35 | 1012 | } |
3401c0d9 | 1013 | if (!separator) |
a3a91a35 | 1014 | break; |
3401c0d9 | 1015 | string = separator + 1; |
a3a91a35 | 1016 | } |
c8256f9d | 1017 | nb_pending_keycodes = i; |
a3a91a35 | 1018 | /* key down events */ |
c8256f9d | 1019 | for (i = 0; i < nb_pending_keycodes; i++) { |
a3a91a35 FB |
1020 | keycode = keycodes[i]; |
1021 | if (keycode & 0x80) | |
1022 | kbd_put_keycode(0xe0); | |
1023 | kbd_put_keycode(keycode & 0x7f); | |
1024 | } | |
c8256f9d | 1025 | /* delayed key up events */ |
f227f17d AZ |
1026 | qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) + |
1027 | muldiv64(ticks_per_sec, hold_time, 1000)); | |
a3a91a35 FB |
1028 | } |
1029 | ||
13224a87 FB |
1030 | static int mouse_button_state; |
1031 | ||
5fafdf24 | 1032 | static void do_mouse_move(const char *dx_str, const char *dy_str, |
13224a87 FB |
1033 | const char *dz_str) |
1034 | { | |
1035 | int dx, dy, dz; | |
1036 | dx = strtol(dx_str, NULL, 0); | |
1037 | dy = strtol(dy_str, NULL, 0); | |
1038 | dz = 0; | |
5fafdf24 | 1039 | if (dz_str) |
13224a87 FB |
1040 | dz = strtol(dz_str, NULL, 0); |
1041 | kbd_mouse_event(dx, dy, dz, mouse_button_state); | |
1042 | } | |
1043 | ||
1044 | static void do_mouse_button(int button_state) | |
1045 | { | |
1046 | mouse_button_state = button_state; | |
1047 | kbd_mouse_event(0, 0, 0, mouse_button_state); | |
1048 | } | |
1049 | ||
3440557b FB |
1050 | static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index) |
1051 | { | |
1052 | uint32_t val; | |
1053 | int suffix; | |
1054 | ||
1055 | if (has_index) { | |
1056 | cpu_outb(NULL, addr & 0xffff, index & 0xff); | |
1057 | addr++; | |
1058 | } | |
1059 | addr &= 0xffff; | |
1060 | ||
1061 | switch(size) { | |
1062 | default: | |
1063 | case 1: | |
1064 | val = cpu_inb(NULL, addr); | |
1065 | suffix = 'b'; | |
1066 | break; | |
1067 | case 2: | |
1068 | val = cpu_inw(NULL, addr); | |
1069 | suffix = 'w'; | |
1070 | break; | |
1071 | case 4: | |
1072 | val = cpu_inl(NULL, addr); | |
1073 | suffix = 'l'; | |
1074 | break; | |
1075 | } | |
1076 | term_printf("port%c[0x%04x] = %#0*x\n", | |
1077 | suffix, addr, size * 2, val); | |
1078 | } | |
a3a91a35 | 1079 | |
3b4366de BS |
1080 | /* boot_set handler */ |
1081 | static QEMUBootSetHandler *qemu_boot_set_handler = NULL; | |
1082 | static void *boot_opaque; | |
1083 | ||
1084 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) | |
1085 | { | |
1086 | qemu_boot_set_handler = func; | |
1087 | boot_opaque = opaque; | |
1088 | } | |
1089 | ||
0ecdffbb AJ |
1090 | static void do_boot_set(const char *bootdevice) |
1091 | { | |
1092 | int res; | |
1093 | ||
1094 | if (qemu_boot_set_handler) { | |
3b4366de | 1095 | res = qemu_boot_set_handler(boot_opaque, bootdevice); |
0ecdffbb AJ |
1096 | if (res == 0) |
1097 | term_printf("boot device list now set to %s\n", bootdevice); | |
1098 | else | |
1099 | term_printf("setting boot device list failed with error %i\n", res); | |
1100 | } else { | |
1101 | term_printf("no function defined to set boot device list for this architecture\n"); | |
1102 | } | |
1103 | } | |
1104 | ||
e4f9082b FB |
1105 | static void do_system_reset(void) |
1106 | { | |
1107 | qemu_system_reset_request(); | |
1108 | } | |
1109 | ||
3475187d FB |
1110 | static void do_system_powerdown(void) |
1111 | { | |
1112 | qemu_system_powerdown_request(); | |
1113 | } | |
1114 | ||
b86bda5b FB |
1115 | #if defined(TARGET_I386) |
1116 | static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask) | |
1117 | { | |
5fafdf24 | 1118 | term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", |
b86bda5b FB |
1119 | addr, |
1120 | pte & mask, | |
1121 | pte & PG_GLOBAL_MASK ? 'G' : '-', | |
1122 | pte & PG_PSE_MASK ? 'P' : '-', | |
1123 | pte & PG_DIRTY_MASK ? 'D' : '-', | |
1124 | pte & PG_ACCESSED_MASK ? 'A' : '-', | |
1125 | pte & PG_PCD_MASK ? 'C' : '-', | |
1126 | pte & PG_PWT_MASK ? 'T' : '-', | |
1127 | pte & PG_USER_MASK ? 'U' : '-', | |
1128 | pte & PG_RW_MASK ? 'W' : '-'); | |
1129 | } | |
1130 | ||
1131 | static void tlb_info(void) | |
1132 | { | |
6a00d601 | 1133 | CPUState *env; |
b86bda5b FB |
1134 | int l1, l2; |
1135 | uint32_t pgd, pde, pte; | |
1136 | ||
6a00d601 FB |
1137 | env = mon_get_cpu(); |
1138 | if (!env) | |
1139 | return; | |
1140 | ||
b86bda5b FB |
1141 | if (!(env->cr[0] & CR0_PG_MASK)) { |
1142 | term_printf("PG disabled\n"); | |
1143 | return; | |
1144 | } | |
1145 | pgd = env->cr[3] & ~0xfff; | |
1146 | for(l1 = 0; l1 < 1024; l1++) { | |
1147 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1148 | pde = le32_to_cpu(pde); | |
1149 | if (pde & PG_PRESENT_MASK) { | |
1150 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1151 | print_pte((l1 << 22), pde, ~((1 << 20) - 1)); | |
1152 | } else { | |
1153 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1154 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1155 | (uint8_t *)&pte, 4); |
1156 | pte = le32_to_cpu(pte); | |
1157 | if (pte & PG_PRESENT_MASK) { | |
5fafdf24 TS |
1158 | print_pte((l1 << 22) + (l2 << 12), |
1159 | pte & ~PG_PSE_MASK, | |
b86bda5b FB |
1160 | ~0xfff); |
1161 | } | |
1162 | } | |
1163 | } | |
1164 | } | |
1165 | } | |
1166 | } | |
1167 | ||
5fafdf24 | 1168 | static void mem_print(uint32_t *pstart, int *plast_prot, |
b86bda5b FB |
1169 | uint32_t end, int prot) |
1170 | { | |
9746b15b FB |
1171 | int prot1; |
1172 | prot1 = *plast_prot; | |
1173 | if (prot != prot1) { | |
b86bda5b FB |
1174 | if (*pstart != -1) { |
1175 | term_printf("%08x-%08x %08x %c%c%c\n", | |
5fafdf24 | 1176 | *pstart, end, end - *pstart, |
9746b15b | 1177 | prot1 & PG_USER_MASK ? 'u' : '-', |
b86bda5b | 1178 | 'r', |
9746b15b | 1179 | prot1 & PG_RW_MASK ? 'w' : '-'); |
b86bda5b FB |
1180 | } |
1181 | if (prot != 0) | |
1182 | *pstart = end; | |
1183 | else | |
1184 | *pstart = -1; | |
1185 | *plast_prot = prot; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | static void mem_info(void) | |
1190 | { | |
6a00d601 | 1191 | CPUState *env; |
b86bda5b FB |
1192 | int l1, l2, prot, last_prot; |
1193 | uint32_t pgd, pde, pte, start, end; | |
1194 | ||
6a00d601 FB |
1195 | env = mon_get_cpu(); |
1196 | if (!env) | |
1197 | return; | |
1198 | ||
b86bda5b FB |
1199 | if (!(env->cr[0] & CR0_PG_MASK)) { |
1200 | term_printf("PG disabled\n"); | |
1201 | return; | |
1202 | } | |
1203 | pgd = env->cr[3] & ~0xfff; | |
1204 | last_prot = 0; | |
1205 | start = -1; | |
1206 | for(l1 = 0; l1 < 1024; l1++) { | |
1207 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1208 | pde = le32_to_cpu(pde); | |
1209 | end = l1 << 22; | |
1210 | if (pde & PG_PRESENT_MASK) { | |
1211 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1212 | prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
1213 | mem_print(&start, &last_prot, end, prot); | |
1214 | } else { | |
1215 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1216 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1217 | (uint8_t *)&pte, 4); |
1218 | pte = le32_to_cpu(pte); | |
1219 | end = (l1 << 22) + (l2 << 12); | |
1220 | if (pte & PG_PRESENT_MASK) { | |
1221 | prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
1222 | } else { | |
1223 | prot = 0; | |
1224 | } | |
1225 | mem_print(&start, &last_prot, end, prot); | |
1226 | } | |
1227 | } | |
1228 | } else { | |
1229 | prot = 0; | |
1230 | mem_print(&start, &last_prot, end, prot); | |
1231 | } | |
1232 | } | |
1233 | } | |
1234 | #endif | |
1235 | ||
0f4c6415 FB |
1236 | static void do_info_kqemu(void) |
1237 | { | |
1238 | #ifdef USE_KQEMU | |
6a00d601 | 1239 | CPUState *env; |
0f4c6415 FB |
1240 | int val; |
1241 | val = 0; | |
6a00d601 FB |
1242 | env = mon_get_cpu(); |
1243 | if (!env) { | |
1244 | term_printf("No cpu initialized yet"); | |
1245 | return; | |
1246 | } | |
1247 | val = env->kqemu_enabled; | |
5f1ce948 FB |
1248 | term_printf("kqemu support: "); |
1249 | switch(val) { | |
1250 | default: | |
1251 | case 0: | |
1252 | term_printf("disabled\n"); | |
1253 | break; | |
1254 | case 1: | |
1255 | term_printf("enabled for user code\n"); | |
1256 | break; | |
1257 | case 2: | |
1258 | term_printf("enabled for user and kernel code\n"); | |
1259 | break; | |
1260 | } | |
0f4c6415 | 1261 | #else |
5f1ce948 | 1262 | term_printf("kqemu support: not compiled\n"); |
0f4c6415 | 1263 | #endif |
5fafdf24 | 1264 | } |
0f4c6415 | 1265 | |
5f1ce948 FB |
1266 | #ifdef CONFIG_PROFILER |
1267 | ||
1268 | int64_t kqemu_time; | |
1269 | int64_t qemu_time; | |
1270 | int64_t kqemu_exec_count; | |
1271 | int64_t dev_time; | |
1272 | int64_t kqemu_ret_int_count; | |
1273 | int64_t kqemu_ret_excp_count; | |
1274 | int64_t kqemu_ret_intr_count; | |
1275 | ||
1276 | static void do_info_profile(void) | |
1277 | { | |
1278 | int64_t total; | |
1279 | total = qemu_time; | |
1280 | if (total == 0) | |
1281 | total = 1; | |
26a76461 | 1282 | term_printf("async time %" PRId64 " (%0.3f)\n", |
5f1ce948 | 1283 | dev_time, dev_time / (double)ticks_per_sec); |
26a76461 | 1284 | term_printf("qemu time %" PRId64 " (%0.3f)\n", |
5f1ce948 | 1285 | qemu_time, qemu_time / (double)ticks_per_sec); |
26a76461 | 1286 | term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n", |
5f1ce948 FB |
1287 | kqemu_time, kqemu_time / (double)ticks_per_sec, |
1288 | kqemu_time / (double)total * 100.0, | |
1289 | kqemu_exec_count, | |
1290 | kqemu_ret_int_count, | |
1291 | kqemu_ret_excp_count, | |
1292 | kqemu_ret_intr_count); | |
1293 | qemu_time = 0; | |
1294 | kqemu_time = 0; | |
1295 | kqemu_exec_count = 0; | |
1296 | dev_time = 0; | |
1297 | kqemu_ret_int_count = 0; | |
1298 | kqemu_ret_excp_count = 0; | |
1299 | kqemu_ret_intr_count = 0; | |
1300 | #ifdef USE_KQEMU | |
1301 | kqemu_record_dump(); | |
1302 | #endif | |
1303 | } | |
1304 | #else | |
1305 | static void do_info_profile(void) | |
1306 | { | |
1307 | term_printf("Internal profiler not compiled\n"); | |
1308 | } | |
1309 | #endif | |
1310 | ||
ec36b695 FB |
1311 | /* Capture support */ |
1312 | static LIST_HEAD (capture_list_head, CaptureState) capture_head; | |
1313 | ||
1314 | static void do_info_capture (void) | |
1315 | { | |
1316 | int i; | |
1317 | CaptureState *s; | |
1318 | ||
1319 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
1320 | term_printf ("[%d]: ", i); | |
1321 | s->ops.info (s->opaque); | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | static void do_stop_capture (int n) | |
1326 | { | |
1327 | int i; | |
1328 | CaptureState *s; | |
1329 | ||
1330 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
1331 | if (i == n) { | |
1332 | s->ops.destroy (s->opaque); | |
1333 | LIST_REMOVE (s, entries); | |
1334 | qemu_free (s); | |
1335 | return; | |
1336 | } | |
1337 | } | |
1338 | } | |
1339 | ||
1340 | #ifdef HAS_AUDIO | |
ec36b695 FB |
1341 | static void do_wav_capture (const char *path, |
1342 | int has_freq, int freq, | |
1343 | int has_bits, int bits, | |
1344 | int has_channels, int nchannels) | |
1345 | { | |
1346 | CaptureState *s; | |
1347 | ||
1348 | s = qemu_mallocz (sizeof (*s)); | |
1349 | if (!s) { | |
1350 | term_printf ("Not enough memory to add wave capture\n"); | |
1351 | return; | |
1352 | } | |
1353 | ||
1354 | freq = has_freq ? freq : 44100; | |
1355 | bits = has_bits ? bits : 16; | |
1356 | nchannels = has_channels ? nchannels : 2; | |
1357 | ||
1358 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
1359 | term_printf ("Faied to add wave capture\n"); | |
1360 | qemu_free (s); | |
1361 | } | |
1362 | LIST_INSERT_HEAD (&capture_head, s, entries); | |
1363 | } | |
1364 | #endif | |
1365 | ||
dc1c0b74 AJ |
1366 | #if defined(TARGET_I386) |
1367 | static void do_inject_nmi(int cpu_index) | |
1368 | { | |
1369 | CPUState *env; | |
1370 | ||
1371 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
1372 | if (env->cpu_index == cpu_index) { | |
1373 | cpu_interrupt(env, CPU_INTERRUPT_NMI); | |
1374 | break; | |
1375 | } | |
1376 | } | |
1377 | #endif | |
1378 | ||
8662d656 | 1379 | static const term_cmd_t term_cmds[] = { |
5fafdf24 | 1380 | { "help|?", "s?", do_help, |
9dc39cba | 1381 | "[cmd]", "show the help" }, |
5fafdf24 | 1382 | { "commit", "s", do_commit, |
7954c734 | 1383 | "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" }, |
9307c4c1 | 1384 | { "info", "s?", do_info, |
9dc39cba | 1385 | "subcommand", "show various information about the system state" }, |
9307c4c1 | 1386 | { "q|quit", "", do_quit, |
9dc39cba | 1387 | "", "quit the emulator" }, |
81d0912d | 1388 | { "eject", "-fB", do_eject, |
e598752a | 1389 | "[-f] device", "eject a removable medium (use -f to force it)" }, |
2ecea9b8 AJ |
1390 | { "change", "BFs?", do_change, |
1391 | "device filename [format]", "change a removable medium, optional format" }, | |
5fafdf24 | 1392 | { "screendump", "F", do_screen_dump, |
59a983b9 | 1393 | "filename", "save screen into PPM image 'filename'" }, |
788228c0 | 1394 | { "logfile", "F", do_logfile, |
e735b91c | 1395 | "filename", "output logs to 'filename'" }, |
9307c4c1 | 1396 | { "log", "s", do_log, |
5fafdf24 | 1397 | "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, |
faea38e7 | 1398 | { "savevm", "s?", do_savevm, |
5fafdf24 | 1399 | "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" }, |
faea38e7 | 1400 | { "loadvm", "s", do_loadvm, |
5fafdf24 | 1401 | "tag|id", "restore a VM snapshot from its tag or id" }, |
faea38e7 | 1402 | { "delvm", "s", do_delvm, |
5fafdf24 TS |
1403 | "tag|id", "delete a VM snapshot from its tag or id" }, |
1404 | { "stop", "", do_stop, | |
9307c4c1 | 1405 | "", "stop emulation", }, |
5fafdf24 | 1406 | { "c|cont", "", do_cont, |
9307c4c1 | 1407 | "", "resume emulation", }, |
67b915a5 | 1408 | #ifdef CONFIG_GDBSTUB |
5fafdf24 | 1409 | { "gdbserver", "s?", do_gdbserver, |
9307c4c1 | 1410 | "[port]", "start gdbserver session (default port=1234)", }, |
67b915a5 | 1411 | #endif |
5fafdf24 | 1412 | { "x", "/l", do_memory_dump, |
9307c4c1 | 1413 | "/fmt addr", "virtual memory dump starting at 'addr'", }, |
5fafdf24 | 1414 | { "xp", "/l", do_physical_memory_dump, |
9307c4c1 | 1415 | "/fmt addr", "physical memory dump starting at 'addr'", }, |
5fafdf24 | 1416 | { "p|print", "/l", do_print, |
9307c4c1 | 1417 | "/fmt expr", "print expression value (use $reg for CPU register access)", }, |
5fafdf24 | 1418 | { "i", "/ii.", do_ioport_read, |
3440557b FB |
1419 | "/fmt addr", "I/O port read" }, |
1420 | ||
c8256f9d AZ |
1421 | { "sendkey", "si?", do_sendkey, |
1422 | "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" }, | |
5fafdf24 | 1423 | { "system_reset", "", do_system_reset, |
e4f9082b | 1424 | "", "reset the system" }, |
5fafdf24 | 1425 | { "system_powerdown", "", do_system_powerdown, |
3475187d | 1426 | "", "send system power down event" }, |
5fafdf24 | 1427 | { "sum", "ii", do_sum, |
e4cf1adc | 1428 | "addr size", "compute the checksum of a memory region" }, |
a594cfbf FB |
1429 | { "usb_add", "s", do_usb_add, |
1430 | "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" }, | |
1431 | { "usb_del", "s", do_usb_del, | |
1432 | "device", "remove USB device 'bus.addr'" }, | |
5fafdf24 | 1433 | { "cpu", "i", do_cpu_set, |
6a00d601 | 1434 | "index", "set the default CPU" }, |
5fafdf24 | 1435 | { "mouse_move", "sss?", do_mouse_move, |
13224a87 | 1436 | "dx dy [dz]", "send mouse move events" }, |
5fafdf24 | 1437 | { "mouse_button", "i", do_mouse_button, |
13224a87 | 1438 | "state", "change mouse button state (1=L, 2=M, 4=R)" }, |
455204eb TS |
1439 | { "mouse_set", "i", do_mouse_set, |
1440 | "index", "set which mouse device receives events" }, | |
ec36b695 FB |
1441 | #ifdef HAS_AUDIO |
1442 | { "wavcapture", "si?i?i?", do_wav_capture, | |
1443 | "path [frequency bits channels]", | |
1444 | "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" }, | |
1445 | #endif | |
1446 | { "stopcapture", "i", do_stop_capture, | |
1447 | "capture index", "stop capture" }, | |
5fafdf24 | 1448 | { "memsave", "lis", do_memory_save, |
b371dc59 | 1449 | "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", }, |
a8bdf7a6 AJ |
1450 | { "pmemsave", "lis", do_physical_memory_save, |
1451 | "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", }, | |
0ecdffbb AJ |
1452 | { "boot_set", "s", do_boot_set, |
1453 | "bootdevice", "define new values for the boot device list" }, | |
dc1c0b74 AJ |
1454 | #if defined(TARGET_I386) |
1455 | { "nmi", "i", do_inject_nmi, | |
1456 | "cpu", "inject an NMI on the given CPU", }, | |
1457 | #endif | |
5bb7910a AL |
1458 | { "migrate", "-ds", do_migrate, |
1459 | "[-d] uri", "migrate to URI (using -d to not wait for completion)" }, | |
1460 | { "migrate_cancel", "", do_migrate_cancel, | |
1461 | "", "cancel the current VM migration" }, | |
1462 | { "migrate_set_speed", "s", do_migrate_set_speed, | |
1463 | "value", "set maximum speed (in bytes) for migrations" }, | |
5fafdf24 | 1464 | { NULL, NULL, }, |
9dc39cba FB |
1465 | }; |
1466 | ||
8662d656 | 1467 | static const term_cmd_t info_cmds[] = { |
9bc9d1c7 FB |
1468 | { "version", "", do_info_version, |
1469 | "", "show the version of qemu" }, | |
9307c4c1 | 1470 | { "network", "", do_info_network, |
9dc39cba | 1471 | "", "show the network state" }, |
5ccfae10 AL |
1472 | { "chardev", "", qemu_chr_info, |
1473 | "", "show the character devices" }, | |
9307c4c1 | 1474 | { "block", "", do_info_block, |
9dc39cba | 1475 | "", "show the block devices" }, |
a36e69dd TS |
1476 | { "blockstats", "", do_info_blockstats, |
1477 | "", "show block device statistics" }, | |
9307c4c1 FB |
1478 | { "registers", "", do_info_registers, |
1479 | "", "show the cpu registers" }, | |
6a00d601 FB |
1480 | { "cpus", "", do_info_cpus, |
1481 | "", "show infos for each CPU" }, | |
aa455485 FB |
1482 | { "history", "", do_info_history, |
1483 | "", "show the command line history", }, | |
4a0fb71e FB |
1484 | { "irq", "", irq_info, |
1485 | "", "show the interrupts statistics (if available)", }, | |
4c27ba27 FB |
1486 | { "pic", "", pic_info, |
1487 | "", "show i8259 (PIC) state", }, | |
86e0c048 FB |
1488 | { "pci", "", pci_info, |
1489 | "", "show PCI info", }, | |
b86bda5b FB |
1490 | #if defined(TARGET_I386) |
1491 | { "tlb", "", tlb_info, | |
1492 | "", "show virtual to physical memory mappings", }, | |
1493 | { "mem", "", mem_info, | |
1494 | "", "show the active virtual memory mappings", }, | |
1495 | #endif | |
e3db7226 FB |
1496 | { "jit", "", do_info_jit, |
1497 | "", "show dynamic compiler info", }, | |
0f4c6415 FB |
1498 | { "kqemu", "", do_info_kqemu, |
1499 | "", "show kqemu information", }, | |
a594cfbf FB |
1500 | { "usb", "", usb_info, |
1501 | "", "show guest USB devices", }, | |
1502 | { "usbhost", "", usb_host_info, | |
1503 | "", "show host USB devices", }, | |
5f1ce948 FB |
1504 | { "profile", "", do_info_profile, |
1505 | "", "show profiling information", }, | |
ec36b695 | 1506 | { "capture", "", do_info_capture, |
17100159 | 1507 | "", "show capture information" }, |
faea38e7 | 1508 | { "snapshots", "", do_info_snapshots, |
17100159 | 1509 | "", "show the currently saved VM snapshots" }, |
201a51fc AZ |
1510 | { "pcmcia", "", pcmcia_info, |
1511 | "", "show guest PCMCIA status" }, | |
455204eb TS |
1512 | { "mice", "", do_info_mice, |
1513 | "", "show which guest mouse is receiving events" }, | |
a9ce8590 FB |
1514 | { "vnc", "", do_info_vnc, |
1515 | "", "show the vnc server status"}, | |
c35734b2 TS |
1516 | { "name", "", do_info_name, |
1517 | "", "show the current VM name" }, | |
f1f23ad5 BS |
1518 | { "uuid", "", do_info_uuid, |
1519 | "", "show the current VM UUID" }, | |
76a66253 JM |
1520 | #if defined(TARGET_PPC) |
1521 | { "cpustats", "", do_info_cpu_stats, | |
1522 | "", "show CPU statistics", }, | |
1523 | #endif | |
31a60e22 BS |
1524 | #if defined(CONFIG_SLIRP) |
1525 | { "slirp", "", do_info_slirp, | |
1526 | "", "show SLIRP statistics", }, | |
1527 | #endif | |
5bb7910a | 1528 | { "migrate", "", do_info_migrate, "", "show migration status" }, |
9dc39cba FB |
1529 | { NULL, NULL, }, |
1530 | }; | |
1531 | ||
9307c4c1 FB |
1532 | /*******************************************************************/ |
1533 | ||
1534 | static const char *pch; | |
1535 | static jmp_buf expr_env; | |
1536 | ||
92a31b1f FB |
1537 | #define MD_TLONG 0 |
1538 | #define MD_I32 1 | |
1539 | ||
9307c4c1 FB |
1540 | typedef struct MonitorDef { |
1541 | const char *name; | |
1542 | int offset; | |
8662d656 | 1543 | target_long (*get_value)(const struct MonitorDef *md, int val); |
92a31b1f | 1544 | int type; |
9307c4c1 FB |
1545 | } MonitorDef; |
1546 | ||
57206fd4 | 1547 | #if defined(TARGET_I386) |
8662d656 | 1548 | static target_long monitor_get_pc (const struct MonitorDef *md, int val) |
57206fd4 | 1549 | { |
6a00d601 FB |
1550 | CPUState *env = mon_get_cpu(); |
1551 | if (!env) | |
1552 | return 0; | |
1553 | return env->eip + env->segs[R_CS].base; | |
57206fd4 FB |
1554 | } |
1555 | #endif | |
1556 | ||
a541f297 | 1557 | #if defined(TARGET_PPC) |
8662d656 | 1558 | static target_long monitor_get_ccr (const struct MonitorDef *md, int val) |
a541f297 | 1559 | { |
6a00d601 | 1560 | CPUState *env = mon_get_cpu(); |
a541f297 FB |
1561 | unsigned int u; |
1562 | int i; | |
1563 | ||
6a00d601 FB |
1564 | if (!env) |
1565 | return 0; | |
1566 | ||
a541f297 FB |
1567 | u = 0; |
1568 | for (i = 0; i < 8; i++) | |
6a00d601 | 1569 | u |= env->crf[i] << (32 - (4 * i)); |
a541f297 FB |
1570 | |
1571 | return u; | |
1572 | } | |
1573 | ||
8662d656 | 1574 | static target_long monitor_get_msr (const struct MonitorDef *md, int val) |
a541f297 | 1575 | { |
6a00d601 FB |
1576 | CPUState *env = mon_get_cpu(); |
1577 | if (!env) | |
1578 | return 0; | |
0411a972 | 1579 | return env->msr; |
a541f297 FB |
1580 | } |
1581 | ||
8662d656 | 1582 | static target_long monitor_get_xer (const struct MonitorDef *md, int val) |
a541f297 | 1583 | { |
6a00d601 FB |
1584 | CPUState *env = mon_get_cpu(); |
1585 | if (!env) | |
1586 | return 0; | |
3d7b417e | 1587 | return env->xer; |
a541f297 | 1588 | } |
9fddaa0c | 1589 | |
8662d656 | 1590 | static target_long monitor_get_decr (const struct MonitorDef *md, int val) |
9fddaa0c | 1591 | { |
6a00d601 FB |
1592 | CPUState *env = mon_get_cpu(); |
1593 | if (!env) | |
1594 | return 0; | |
1595 | return cpu_ppc_load_decr(env); | |
9fddaa0c FB |
1596 | } |
1597 | ||
8662d656 | 1598 | static target_long monitor_get_tbu (const struct MonitorDef *md, int val) |
9fddaa0c | 1599 | { |
6a00d601 FB |
1600 | CPUState *env = mon_get_cpu(); |
1601 | if (!env) | |
1602 | return 0; | |
1603 | return cpu_ppc_load_tbu(env); | |
9fddaa0c FB |
1604 | } |
1605 | ||
8662d656 | 1606 | static target_long monitor_get_tbl (const struct MonitorDef *md, int val) |
9fddaa0c | 1607 | { |
6a00d601 FB |
1608 | CPUState *env = mon_get_cpu(); |
1609 | if (!env) | |
1610 | return 0; | |
1611 | return cpu_ppc_load_tbl(env); | |
9fddaa0c | 1612 | } |
a541f297 FB |
1613 | #endif |
1614 | ||
e95c8d51 | 1615 | #if defined(TARGET_SPARC) |
7b936c0c | 1616 | #ifndef TARGET_SPARC64 |
8662d656 | 1617 | static target_long monitor_get_psr (const struct MonitorDef *md, int val) |
e95c8d51 | 1618 | { |
6a00d601 FB |
1619 | CPUState *env = mon_get_cpu(); |
1620 | if (!env) | |
1621 | return 0; | |
1622 | return GET_PSR(env); | |
e95c8d51 | 1623 | } |
7b936c0c | 1624 | #endif |
e95c8d51 | 1625 | |
8662d656 | 1626 | static target_long monitor_get_reg(const struct MonitorDef *md, int val) |
e95c8d51 | 1627 | { |
6a00d601 FB |
1628 | CPUState *env = mon_get_cpu(); |
1629 | if (!env) | |
1630 | return 0; | |
1631 | return env->regwptr[val]; | |
e95c8d51 FB |
1632 | } |
1633 | #endif | |
1634 | ||
8662d656 | 1635 | static const MonitorDef monitor_defs[] = { |
9307c4c1 | 1636 | #ifdef TARGET_I386 |
57206fd4 FB |
1637 | |
1638 | #define SEG(name, seg) \ | |
92a31b1f | 1639 | { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\ |
57206fd4 | 1640 | { name ".base", offsetof(CPUState, segs[seg].base) },\ |
92a31b1f | 1641 | { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 }, |
57206fd4 | 1642 | |
9307c4c1 FB |
1643 | { "eax", offsetof(CPUState, regs[0]) }, |
1644 | { "ecx", offsetof(CPUState, regs[1]) }, | |
1645 | { "edx", offsetof(CPUState, regs[2]) }, | |
1646 | { "ebx", offsetof(CPUState, regs[3]) }, | |
1647 | { "esp|sp", offsetof(CPUState, regs[4]) }, | |
1648 | { "ebp|fp", offsetof(CPUState, regs[5]) }, | |
1649 | { "esi", offsetof(CPUState, regs[6]) }, | |
01038d2a | 1650 | { "edi", offsetof(CPUState, regs[7]) }, |
92a31b1f FB |
1651 | #ifdef TARGET_X86_64 |
1652 | { "r8", offsetof(CPUState, regs[8]) }, | |
1653 | { "r9", offsetof(CPUState, regs[9]) }, | |
1654 | { "r10", offsetof(CPUState, regs[10]) }, | |
1655 | { "r11", offsetof(CPUState, regs[11]) }, | |
1656 | { "r12", offsetof(CPUState, regs[12]) }, | |
1657 | { "r13", offsetof(CPUState, regs[13]) }, | |
1658 | { "r14", offsetof(CPUState, regs[14]) }, | |
1659 | { "r15", offsetof(CPUState, regs[15]) }, | |
1660 | #endif | |
9307c4c1 | 1661 | { "eflags", offsetof(CPUState, eflags) }, |
57206fd4 FB |
1662 | { "eip", offsetof(CPUState, eip) }, |
1663 | SEG("cs", R_CS) | |
1664 | SEG("ds", R_DS) | |
1665 | SEG("es", R_ES) | |
01038d2a | 1666 | SEG("ss", R_SS) |
57206fd4 FB |
1667 | SEG("fs", R_FS) |
1668 | SEG("gs", R_GS) | |
1669 | { "pc", 0, monitor_get_pc, }, | |
a541f297 | 1670 | #elif defined(TARGET_PPC) |
ff937dba | 1671 | /* General purpose registers */ |
a541f297 FB |
1672 | { "r0", offsetof(CPUState, gpr[0]) }, |
1673 | { "r1", offsetof(CPUState, gpr[1]) }, | |
1674 | { "r2", offsetof(CPUState, gpr[2]) }, | |
1675 | { "r3", offsetof(CPUState, gpr[3]) }, | |
1676 | { "r4", offsetof(CPUState, gpr[4]) }, | |
1677 | { "r5", offsetof(CPUState, gpr[5]) }, | |
1678 | { "r6", offsetof(CPUState, gpr[6]) }, | |
1679 | { "r7", offsetof(CPUState, gpr[7]) }, | |
1680 | { "r8", offsetof(CPUState, gpr[8]) }, | |
1681 | { "r9", offsetof(CPUState, gpr[9]) }, | |
1682 | { "r10", offsetof(CPUState, gpr[10]) }, | |
1683 | { "r11", offsetof(CPUState, gpr[11]) }, | |
1684 | { "r12", offsetof(CPUState, gpr[12]) }, | |
1685 | { "r13", offsetof(CPUState, gpr[13]) }, | |
1686 | { "r14", offsetof(CPUState, gpr[14]) }, | |
1687 | { "r15", offsetof(CPUState, gpr[15]) }, | |
1688 | { "r16", offsetof(CPUState, gpr[16]) }, | |
1689 | { "r17", offsetof(CPUState, gpr[17]) }, | |
1690 | { "r18", offsetof(CPUState, gpr[18]) }, | |
1691 | { "r19", offsetof(CPUState, gpr[19]) }, | |
1692 | { "r20", offsetof(CPUState, gpr[20]) }, | |
1693 | { "r21", offsetof(CPUState, gpr[21]) }, | |
1694 | { "r22", offsetof(CPUState, gpr[22]) }, | |
1695 | { "r23", offsetof(CPUState, gpr[23]) }, | |
1696 | { "r24", offsetof(CPUState, gpr[24]) }, | |
1697 | { "r25", offsetof(CPUState, gpr[25]) }, | |
1698 | { "r26", offsetof(CPUState, gpr[26]) }, | |
1699 | { "r27", offsetof(CPUState, gpr[27]) }, | |
1700 | { "r28", offsetof(CPUState, gpr[28]) }, | |
1701 | { "r29", offsetof(CPUState, gpr[29]) }, | |
1702 | { "r30", offsetof(CPUState, gpr[30]) }, | |
1703 | { "r31", offsetof(CPUState, gpr[31]) }, | |
ff937dba JM |
1704 | /* Floating point registers */ |
1705 | { "f0", offsetof(CPUState, fpr[0]) }, | |
1706 | { "f1", offsetof(CPUState, fpr[1]) }, | |
1707 | { "f2", offsetof(CPUState, fpr[2]) }, | |
1708 | { "f3", offsetof(CPUState, fpr[3]) }, | |
1709 | { "f4", offsetof(CPUState, fpr[4]) }, | |
1710 | { "f5", offsetof(CPUState, fpr[5]) }, | |
1711 | { "f6", offsetof(CPUState, fpr[6]) }, | |
1712 | { "f7", offsetof(CPUState, fpr[7]) }, | |
1713 | { "f8", offsetof(CPUState, fpr[8]) }, | |
1714 | { "f9", offsetof(CPUState, fpr[9]) }, | |
1715 | { "f10", offsetof(CPUState, fpr[10]) }, | |
1716 | { "f11", offsetof(CPUState, fpr[11]) }, | |
1717 | { "f12", offsetof(CPUState, fpr[12]) }, | |
1718 | { "f13", offsetof(CPUState, fpr[13]) }, | |
1719 | { "f14", offsetof(CPUState, fpr[14]) }, | |
1720 | { "f15", offsetof(CPUState, fpr[15]) }, | |
1721 | { "f16", offsetof(CPUState, fpr[16]) }, | |
1722 | { "f17", offsetof(CPUState, fpr[17]) }, | |
1723 | { "f18", offsetof(CPUState, fpr[18]) }, | |
1724 | { "f19", offsetof(CPUState, fpr[19]) }, | |
1725 | { "f20", offsetof(CPUState, fpr[20]) }, | |
1726 | { "f21", offsetof(CPUState, fpr[21]) }, | |
1727 | { "f22", offsetof(CPUState, fpr[22]) }, | |
1728 | { "f23", offsetof(CPUState, fpr[23]) }, | |
1729 | { "f24", offsetof(CPUState, fpr[24]) }, | |
1730 | { "f25", offsetof(CPUState, fpr[25]) }, | |
1731 | { "f26", offsetof(CPUState, fpr[26]) }, | |
1732 | { "f27", offsetof(CPUState, fpr[27]) }, | |
1733 | { "f28", offsetof(CPUState, fpr[28]) }, | |
1734 | { "f29", offsetof(CPUState, fpr[29]) }, | |
1735 | { "f30", offsetof(CPUState, fpr[30]) }, | |
1736 | { "f31", offsetof(CPUState, fpr[31]) }, | |
1737 | { "fpscr", offsetof(CPUState, fpscr) }, | |
1738 | /* Next instruction pointer */ | |
57206fd4 | 1739 | { "nip|pc", offsetof(CPUState, nip) }, |
a541f297 FB |
1740 | { "lr", offsetof(CPUState, lr) }, |
1741 | { "ctr", offsetof(CPUState, ctr) }, | |
9fddaa0c | 1742 | { "decr", 0, &monitor_get_decr, }, |
a541f297 | 1743 | { "ccr", 0, &monitor_get_ccr, }, |
ff937dba | 1744 | /* Machine state register */ |
a541f297 FB |
1745 | { "msr", 0, &monitor_get_msr, }, |
1746 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
1747 | { "tbu", 0, &monitor_get_tbu, }, |
1748 | { "tbl", 0, &monitor_get_tbl, }, | |
ff937dba JM |
1749 | #if defined(TARGET_PPC64) |
1750 | /* Address space register */ | |
1751 | { "asr", offsetof(CPUState, asr) }, | |
1752 | #endif | |
1753 | /* Segment registers */ | |
a541f297 FB |
1754 | { "sdr1", offsetof(CPUState, sdr1) }, |
1755 | { "sr0", offsetof(CPUState, sr[0]) }, | |
1756 | { "sr1", offsetof(CPUState, sr[1]) }, | |
1757 | { "sr2", offsetof(CPUState, sr[2]) }, | |
1758 | { "sr3", offsetof(CPUState, sr[3]) }, | |
1759 | { "sr4", offsetof(CPUState, sr[4]) }, | |
1760 | { "sr5", offsetof(CPUState, sr[5]) }, | |
1761 | { "sr6", offsetof(CPUState, sr[6]) }, | |
1762 | { "sr7", offsetof(CPUState, sr[7]) }, | |
1763 | { "sr8", offsetof(CPUState, sr[8]) }, | |
1764 | { "sr9", offsetof(CPUState, sr[9]) }, | |
1765 | { "sr10", offsetof(CPUState, sr[10]) }, | |
1766 | { "sr11", offsetof(CPUState, sr[11]) }, | |
1767 | { "sr12", offsetof(CPUState, sr[12]) }, | |
1768 | { "sr13", offsetof(CPUState, sr[13]) }, | |
1769 | { "sr14", offsetof(CPUState, sr[14]) }, | |
1770 | { "sr15", offsetof(CPUState, sr[15]) }, | |
1771 | /* Too lazy to put BATs and SPRs ... */ | |
e95c8d51 FB |
1772 | #elif defined(TARGET_SPARC) |
1773 | { "g0", offsetof(CPUState, gregs[0]) }, | |
1774 | { "g1", offsetof(CPUState, gregs[1]) }, | |
1775 | { "g2", offsetof(CPUState, gregs[2]) }, | |
1776 | { "g3", offsetof(CPUState, gregs[3]) }, | |
1777 | { "g4", offsetof(CPUState, gregs[4]) }, | |
1778 | { "g5", offsetof(CPUState, gregs[5]) }, | |
1779 | { "g6", offsetof(CPUState, gregs[6]) }, | |
1780 | { "g7", offsetof(CPUState, gregs[7]) }, | |
1781 | { "o0", 0, monitor_get_reg }, | |
1782 | { "o1", 1, monitor_get_reg }, | |
1783 | { "o2", 2, monitor_get_reg }, | |
1784 | { "o3", 3, monitor_get_reg }, | |
1785 | { "o4", 4, monitor_get_reg }, | |
1786 | { "o5", 5, monitor_get_reg }, | |
1787 | { "o6", 6, monitor_get_reg }, | |
1788 | { "o7", 7, monitor_get_reg }, | |
1789 | { "l0", 8, monitor_get_reg }, | |
1790 | { "l1", 9, monitor_get_reg }, | |
1791 | { "l2", 10, monitor_get_reg }, | |
1792 | { "l3", 11, monitor_get_reg }, | |
1793 | { "l4", 12, monitor_get_reg }, | |
1794 | { "l5", 13, monitor_get_reg }, | |
1795 | { "l6", 14, monitor_get_reg }, | |
1796 | { "l7", 15, monitor_get_reg }, | |
1797 | { "i0", 16, monitor_get_reg }, | |
1798 | { "i1", 17, monitor_get_reg }, | |
1799 | { "i2", 18, monitor_get_reg }, | |
1800 | { "i3", 19, monitor_get_reg }, | |
1801 | { "i4", 20, monitor_get_reg }, | |
1802 | { "i5", 21, monitor_get_reg }, | |
1803 | { "i6", 22, monitor_get_reg }, | |
1804 | { "i7", 23, monitor_get_reg }, | |
1805 | { "pc", offsetof(CPUState, pc) }, | |
1806 | { "npc", offsetof(CPUState, npc) }, | |
1807 | { "y", offsetof(CPUState, y) }, | |
7b936c0c | 1808 | #ifndef TARGET_SPARC64 |
e95c8d51 FB |
1809 | { "psr", 0, &monitor_get_psr, }, |
1810 | { "wim", offsetof(CPUState, wim) }, | |
7b936c0c | 1811 | #endif |
e95c8d51 FB |
1812 | { "tbr", offsetof(CPUState, tbr) }, |
1813 | { "fsr", offsetof(CPUState, fsr) }, | |
1814 | { "f0", offsetof(CPUState, fpr[0]) }, | |
1815 | { "f1", offsetof(CPUState, fpr[1]) }, | |
1816 | { "f2", offsetof(CPUState, fpr[2]) }, | |
1817 | { "f3", offsetof(CPUState, fpr[3]) }, | |
1818 | { "f4", offsetof(CPUState, fpr[4]) }, | |
1819 | { "f5", offsetof(CPUState, fpr[5]) }, | |
1820 | { "f6", offsetof(CPUState, fpr[6]) }, | |
1821 | { "f7", offsetof(CPUState, fpr[7]) }, | |
1822 | { "f8", offsetof(CPUState, fpr[8]) }, | |
1823 | { "f9", offsetof(CPUState, fpr[9]) }, | |
1824 | { "f10", offsetof(CPUState, fpr[10]) }, | |
1825 | { "f11", offsetof(CPUState, fpr[11]) }, | |
1826 | { "f12", offsetof(CPUState, fpr[12]) }, | |
1827 | { "f13", offsetof(CPUState, fpr[13]) }, | |
1828 | { "f14", offsetof(CPUState, fpr[14]) }, | |
1829 | { "f15", offsetof(CPUState, fpr[15]) }, | |
1830 | { "f16", offsetof(CPUState, fpr[16]) }, | |
1831 | { "f17", offsetof(CPUState, fpr[17]) }, | |
1832 | { "f18", offsetof(CPUState, fpr[18]) }, | |
1833 | { "f19", offsetof(CPUState, fpr[19]) }, | |
1834 | { "f20", offsetof(CPUState, fpr[20]) }, | |
1835 | { "f21", offsetof(CPUState, fpr[21]) }, | |
1836 | { "f22", offsetof(CPUState, fpr[22]) }, | |
1837 | { "f23", offsetof(CPUState, fpr[23]) }, | |
1838 | { "f24", offsetof(CPUState, fpr[24]) }, | |
1839 | { "f25", offsetof(CPUState, fpr[25]) }, | |
1840 | { "f26", offsetof(CPUState, fpr[26]) }, | |
1841 | { "f27", offsetof(CPUState, fpr[27]) }, | |
1842 | { "f28", offsetof(CPUState, fpr[28]) }, | |
1843 | { "f29", offsetof(CPUState, fpr[29]) }, | |
1844 | { "f30", offsetof(CPUState, fpr[30]) }, | |
1845 | { "f31", offsetof(CPUState, fpr[31]) }, | |
7b936c0c FB |
1846 | #ifdef TARGET_SPARC64 |
1847 | { "f32", offsetof(CPUState, fpr[32]) }, | |
1848 | { "f34", offsetof(CPUState, fpr[34]) }, | |
1849 | { "f36", offsetof(CPUState, fpr[36]) }, | |
1850 | { "f38", offsetof(CPUState, fpr[38]) }, | |
1851 | { "f40", offsetof(CPUState, fpr[40]) }, | |
1852 | { "f42", offsetof(CPUState, fpr[42]) }, | |
1853 | { "f44", offsetof(CPUState, fpr[44]) }, | |
1854 | { "f46", offsetof(CPUState, fpr[46]) }, | |
1855 | { "f48", offsetof(CPUState, fpr[48]) }, | |
1856 | { "f50", offsetof(CPUState, fpr[50]) }, | |
1857 | { "f52", offsetof(CPUState, fpr[52]) }, | |
1858 | { "f54", offsetof(CPUState, fpr[54]) }, | |
1859 | { "f56", offsetof(CPUState, fpr[56]) }, | |
1860 | { "f58", offsetof(CPUState, fpr[58]) }, | |
1861 | { "f60", offsetof(CPUState, fpr[60]) }, | |
1862 | { "f62", offsetof(CPUState, fpr[62]) }, | |
1863 | { "asi", offsetof(CPUState, asi) }, | |
1864 | { "pstate", offsetof(CPUState, pstate) }, | |
1865 | { "cansave", offsetof(CPUState, cansave) }, | |
1866 | { "canrestore", offsetof(CPUState, canrestore) }, | |
1867 | { "otherwin", offsetof(CPUState, otherwin) }, | |
1868 | { "wstate", offsetof(CPUState, wstate) }, | |
1869 | { "cleanwin", offsetof(CPUState, cleanwin) }, | |
1870 | { "fprs", offsetof(CPUState, fprs) }, | |
1871 | #endif | |
9307c4c1 FB |
1872 | #endif |
1873 | { NULL }, | |
1874 | }; | |
1875 | ||
5fafdf24 | 1876 | static void expr_error(const char *fmt) |
9dc39cba | 1877 | { |
9307c4c1 FB |
1878 | term_printf(fmt); |
1879 | term_printf("\n"); | |
1880 | longjmp(expr_env, 1); | |
1881 | } | |
1882 | ||
6a00d601 | 1883 | /* return 0 if OK, -1 if not found, -2 if no CPU defined */ |
92a31b1f | 1884 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 1885 | { |
8662d656 | 1886 | const MonitorDef *md; |
92a31b1f FB |
1887 | void *ptr; |
1888 | ||
9307c4c1 FB |
1889 | for(md = monitor_defs; md->name != NULL; md++) { |
1890 | if (compare_cmd(name, md->name)) { | |
1891 | if (md->get_value) { | |
e95c8d51 | 1892 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 1893 | } else { |
6a00d601 FB |
1894 | CPUState *env = mon_get_cpu(); |
1895 | if (!env) | |
1896 | return -2; | |
1897 | ptr = (uint8_t *)env + md->offset; | |
92a31b1f FB |
1898 | switch(md->type) { |
1899 | case MD_I32: | |
1900 | *pval = *(int32_t *)ptr; | |
1901 | break; | |
1902 | case MD_TLONG: | |
1903 | *pval = *(target_long *)ptr; | |
1904 | break; | |
1905 | default: | |
1906 | *pval = 0; | |
1907 | break; | |
1908 | } | |
9307c4c1 FB |
1909 | } |
1910 | return 0; | |
1911 | } | |
1912 | } | |
1913 | return -1; | |
1914 | } | |
1915 | ||
1916 | static void next(void) | |
1917 | { | |
1918 | if (pch != '\0') { | |
1919 | pch++; | |
1920 | while (isspace(*pch)) | |
1921 | pch++; | |
1922 | } | |
1923 | } | |
1924 | ||
c2efc95d | 1925 | static int64_t expr_sum(void); |
9307c4c1 | 1926 | |
c2efc95d | 1927 | static int64_t expr_unary(void) |
9307c4c1 | 1928 | { |
c2efc95d | 1929 | int64_t n; |
9307c4c1 | 1930 | char *p; |
6a00d601 | 1931 | int ret; |
9307c4c1 FB |
1932 | |
1933 | switch(*pch) { | |
1934 | case '+': | |
1935 | next(); | |
1936 | n = expr_unary(); | |
1937 | break; | |
1938 | case '-': | |
1939 | next(); | |
1940 | n = -expr_unary(); | |
1941 | break; | |
1942 | case '~': | |
1943 | next(); | |
1944 | n = ~expr_unary(); | |
1945 | break; | |
1946 | case '(': | |
1947 | next(); | |
1948 | n = expr_sum(); | |
1949 | if (*pch != ')') { | |
1950 | expr_error("')' expected"); | |
1951 | } | |
1952 | next(); | |
1953 | break; | |
81d0912d FB |
1954 | case '\'': |
1955 | pch++; | |
1956 | if (*pch == '\0') | |
1957 | expr_error("character constant expected"); | |
1958 | n = *pch; | |
1959 | pch++; | |
1960 | if (*pch != '\'') | |
1961 | expr_error("missing terminating \' character"); | |
1962 | next(); | |
1963 | break; | |
9307c4c1 FB |
1964 | case '$': |
1965 | { | |
1966 | char buf[128], *q; | |
69b34976 | 1967 | target_long reg=0; |
3b46e624 | 1968 | |
9307c4c1 FB |
1969 | pch++; |
1970 | q = buf; | |
1971 | while ((*pch >= 'a' && *pch <= 'z') || | |
1972 | (*pch >= 'A' && *pch <= 'Z') || | |
1973 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 1974 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
1975 | if ((q - buf) < sizeof(buf) - 1) |
1976 | *q++ = *pch; | |
1977 | pch++; | |
1978 | } | |
1979 | while (isspace(*pch)) | |
1980 | pch++; | |
1981 | *q = 0; | |
7743e588 | 1982 | ret = get_monitor_def(®, buf); |
6a00d601 | 1983 | if (ret == -1) |
9307c4c1 | 1984 | expr_error("unknown register"); |
5fafdf24 | 1985 | else if (ret == -2) |
6a00d601 | 1986 | expr_error("no cpu defined"); |
7743e588 | 1987 | n = reg; |
9307c4c1 FB |
1988 | } |
1989 | break; | |
1990 | case '\0': | |
1991 | expr_error("unexpected end of expression"); | |
1992 | n = 0; | |
1993 | break; | |
1994 | default: | |
7743e588 | 1995 | #if TARGET_PHYS_ADDR_BITS > 32 |
4f4fbf77 FB |
1996 | n = strtoull(pch, &p, 0); |
1997 | #else | |
9307c4c1 | 1998 | n = strtoul(pch, &p, 0); |
4f4fbf77 | 1999 | #endif |
9307c4c1 FB |
2000 | if (pch == p) { |
2001 | expr_error("invalid char in expression"); | |
2002 | } | |
2003 | pch = p; | |
2004 | while (isspace(*pch)) | |
2005 | pch++; | |
2006 | break; | |
2007 | } | |
2008 | return n; | |
2009 | } | |
2010 | ||
2011 | ||
c2efc95d | 2012 | static int64_t expr_prod(void) |
9307c4c1 | 2013 | { |
c2efc95d | 2014 | int64_t val, val2; |
92a31b1f | 2015 | int op; |
3b46e624 | 2016 | |
9307c4c1 FB |
2017 | val = expr_unary(); |
2018 | for(;;) { | |
2019 | op = *pch; | |
2020 | if (op != '*' && op != '/' && op != '%') | |
2021 | break; | |
2022 | next(); | |
2023 | val2 = expr_unary(); | |
2024 | switch(op) { | |
2025 | default: | |
2026 | case '*': | |
2027 | val *= val2; | |
2028 | break; | |
2029 | case '/': | |
2030 | case '%': | |
5fafdf24 | 2031 | if (val2 == 0) |
5b60212f | 2032 | expr_error("division by zero"); |
9307c4c1 FB |
2033 | if (op == '/') |
2034 | val /= val2; | |
2035 | else | |
2036 | val %= val2; | |
2037 | break; | |
2038 | } | |
2039 | } | |
2040 | return val; | |
2041 | } | |
2042 | ||
c2efc95d | 2043 | static int64_t expr_logic(void) |
9307c4c1 | 2044 | { |
c2efc95d | 2045 | int64_t val, val2; |
92a31b1f | 2046 | int op; |
9307c4c1 FB |
2047 | |
2048 | val = expr_prod(); | |
2049 | for(;;) { | |
2050 | op = *pch; | |
2051 | if (op != '&' && op != '|' && op != '^') | |
2052 | break; | |
2053 | next(); | |
2054 | val2 = expr_prod(); | |
2055 | switch(op) { | |
2056 | default: | |
2057 | case '&': | |
2058 | val &= val2; | |
2059 | break; | |
2060 | case '|': | |
2061 | val |= val2; | |
2062 | break; | |
2063 | case '^': | |
2064 | val ^= val2; | |
2065 | break; | |
2066 | } | |
2067 | } | |
2068 | return val; | |
2069 | } | |
2070 | ||
c2efc95d | 2071 | static int64_t expr_sum(void) |
9307c4c1 | 2072 | { |
c2efc95d | 2073 | int64_t val, val2; |
92a31b1f | 2074 | int op; |
9307c4c1 FB |
2075 | |
2076 | val = expr_logic(); | |
2077 | for(;;) { | |
2078 | op = *pch; | |
2079 | if (op != '+' && op != '-') | |
2080 | break; | |
2081 | next(); | |
2082 | val2 = expr_logic(); | |
2083 | if (op == '+') | |
2084 | val += val2; | |
2085 | else | |
2086 | val -= val2; | |
2087 | } | |
2088 | return val; | |
2089 | } | |
2090 | ||
c2efc95d | 2091 | static int get_expr(int64_t *pval, const char **pp) |
9307c4c1 FB |
2092 | { |
2093 | pch = *pp; | |
2094 | if (setjmp(expr_env)) { | |
2095 | *pp = pch; | |
2096 | return -1; | |
2097 | } | |
2098 | while (isspace(*pch)) | |
2099 | pch++; | |
2100 | *pval = expr_sum(); | |
2101 | *pp = pch; | |
2102 | return 0; | |
2103 | } | |
2104 | ||
2105 | static int get_str(char *buf, int buf_size, const char **pp) | |
2106 | { | |
2107 | const char *p; | |
2108 | char *q; | |
2109 | int c; | |
2110 | ||
81d0912d | 2111 | q = buf; |
9307c4c1 FB |
2112 | p = *pp; |
2113 | while (isspace(*p)) | |
2114 | p++; | |
2115 | if (*p == '\0') { | |
2116 | fail: | |
81d0912d | 2117 | *q = '\0'; |
9307c4c1 FB |
2118 | *pp = p; |
2119 | return -1; | |
2120 | } | |
9307c4c1 FB |
2121 | if (*p == '\"') { |
2122 | p++; | |
2123 | while (*p != '\0' && *p != '\"') { | |
2124 | if (*p == '\\') { | |
2125 | p++; | |
2126 | c = *p++; | |
2127 | switch(c) { | |
2128 | case 'n': | |
2129 | c = '\n'; | |
2130 | break; | |
2131 | case 'r': | |
2132 | c = '\r'; | |
2133 | break; | |
2134 | case '\\': | |
2135 | case '\'': | |
2136 | case '\"': | |
2137 | break; | |
2138 | default: | |
2139 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
2140 | goto fail; | |
2141 | } | |
2142 | if ((q - buf) < buf_size - 1) { | |
2143 | *q++ = c; | |
2144 | } | |
2145 | } else { | |
2146 | if ((q - buf) < buf_size - 1) { | |
2147 | *q++ = *p; | |
2148 | } | |
2149 | p++; | |
2150 | } | |
2151 | } | |
2152 | if (*p != '\"') { | |
5b60212f | 2153 | qemu_printf("unterminated string\n"); |
9307c4c1 FB |
2154 | goto fail; |
2155 | } | |
2156 | p++; | |
2157 | } else { | |
2158 | while (*p != '\0' && !isspace(*p)) { | |
2159 | if ((q - buf) < buf_size - 1) { | |
2160 | *q++ = *p; | |
2161 | } | |
2162 | p++; | |
2163 | } | |
9307c4c1 | 2164 | } |
81d0912d | 2165 | *q = '\0'; |
9307c4c1 FB |
2166 | *pp = p; |
2167 | return 0; | |
2168 | } | |
2169 | ||
2170 | static int default_fmt_format = 'x'; | |
2171 | static int default_fmt_size = 4; | |
2172 | ||
2173 | #define MAX_ARGS 16 | |
2174 | ||
7e2515e8 | 2175 | static void monitor_handle_command(const char *cmdline) |
9307c4c1 FB |
2176 | { |
2177 | const char *p, *pstart, *typestr; | |
2178 | char *q; | |
2179 | int c, nb_args, len, i, has_arg; | |
8662d656 | 2180 | const term_cmd_t *cmd; |
9307c4c1 FB |
2181 | char cmdname[256]; |
2182 | char buf[1024]; | |
2183 | void *str_allocated[MAX_ARGS]; | |
2184 | void *args[MAX_ARGS]; | |
a5f1b965 BS |
2185 | void (*handler_0)(void); |
2186 | void (*handler_1)(void *arg0); | |
2187 | void (*handler_2)(void *arg0, void *arg1); | |
2188 | void (*handler_3)(void *arg0, void *arg1, void *arg2); | |
2189 | void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3); | |
2190 | void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3, | |
2191 | void *arg4); | |
2192 | void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3, | |
2193 | void *arg4, void *arg5); | |
2194 | void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3, | |
2195 | void *arg4, void *arg5, void *arg6); | |
9dc39cba FB |
2196 | |
2197 | #ifdef DEBUG | |
2198 | term_printf("command='%s'\n", cmdline); | |
2199 | #endif | |
3b46e624 | 2200 | |
9307c4c1 | 2201 | /* extract the command name */ |
9dc39cba | 2202 | p = cmdline; |
9307c4c1 FB |
2203 | q = cmdname; |
2204 | while (isspace(*p)) | |
2205 | p++; | |
2206 | if (*p == '\0') | |
2207 | return; | |
2208 | pstart = p; | |
2209 | while (*p != '\0' && *p != '/' && !isspace(*p)) | |
2210 | p++; | |
2211 | len = p - pstart; | |
2212 | if (len > sizeof(cmdname) - 1) | |
2213 | len = sizeof(cmdname) - 1; | |
2214 | memcpy(cmdname, pstart, len); | |
2215 | cmdname[len] = '\0'; | |
3b46e624 | 2216 | |
9307c4c1 FB |
2217 | /* find the command */ |
2218 | for(cmd = term_cmds; cmd->name != NULL; cmd++) { | |
5fafdf24 | 2219 | if (compare_cmd(cmdname, cmd->name)) |
9307c4c1 FB |
2220 | goto found; |
2221 | } | |
2222 | term_printf("unknown command: '%s'\n", cmdname); | |
2223 | return; | |
2224 | found: | |
2225 | ||
2226 | for(i = 0; i < MAX_ARGS; i++) | |
2227 | str_allocated[i] = NULL; | |
3b46e624 | 2228 | |
9307c4c1 FB |
2229 | /* parse the parameters */ |
2230 | typestr = cmd->args_type; | |
2231 | nb_args = 0; | |
9dc39cba | 2232 | for(;;) { |
9307c4c1 FB |
2233 | c = *typestr; |
2234 | if (c == '\0') | |
9dc39cba | 2235 | break; |
9307c4c1 FB |
2236 | typestr++; |
2237 | switch(c) { | |
2238 | case 'F': | |
81d0912d | 2239 | case 'B': |
9307c4c1 FB |
2240 | case 's': |
2241 | { | |
2242 | int ret; | |
2243 | char *str; | |
3b46e624 | 2244 | |
5fafdf24 | 2245 | while (isspace(*p)) |
9307c4c1 FB |
2246 | p++; |
2247 | if (*typestr == '?') { | |
2248 | typestr++; | |
2249 | if (*p == '\0') { | |
2250 | /* no optional string: NULL argument */ | |
2251 | str = NULL; | |
2252 | goto add_str; | |
2253 | } | |
2254 | } | |
2255 | ret = get_str(buf, sizeof(buf), &p); | |
2256 | if (ret < 0) { | |
81d0912d FB |
2257 | switch(c) { |
2258 | case 'F': | |
9307c4c1 | 2259 | term_printf("%s: filename expected\n", cmdname); |
81d0912d FB |
2260 | break; |
2261 | case 'B': | |
2262 | term_printf("%s: block device name expected\n", cmdname); | |
2263 | break; | |
2264 | default: | |
9307c4c1 | 2265 | term_printf("%s: string expected\n", cmdname); |
81d0912d FB |
2266 | break; |
2267 | } | |
9307c4c1 FB |
2268 | goto fail; |
2269 | } | |
2270 | str = qemu_malloc(strlen(buf) + 1); | |
363a37d5 | 2271 | pstrcpy(str, sizeof(buf), buf); |
9307c4c1 FB |
2272 | str_allocated[nb_args] = str; |
2273 | add_str: | |
2274 | if (nb_args >= MAX_ARGS) { | |
2275 | error_args: | |
2276 | term_printf("%s: too many arguments\n", cmdname); | |
2277 | goto fail; | |
2278 | } | |
2279 | args[nb_args++] = str; | |
2280 | } | |
9dc39cba | 2281 | break; |
9307c4c1 FB |
2282 | case '/': |
2283 | { | |
2284 | int count, format, size; | |
3b46e624 | 2285 | |
9307c4c1 FB |
2286 | while (isspace(*p)) |
2287 | p++; | |
2288 | if (*p == '/') { | |
2289 | /* format found */ | |
2290 | p++; | |
2291 | count = 1; | |
2292 | if (isdigit(*p)) { | |
2293 | count = 0; | |
2294 | while (isdigit(*p)) { | |
2295 | count = count * 10 + (*p - '0'); | |
2296 | p++; | |
2297 | } | |
2298 | } | |
2299 | size = -1; | |
2300 | format = -1; | |
2301 | for(;;) { | |
2302 | switch(*p) { | |
2303 | case 'o': | |
2304 | case 'd': | |
2305 | case 'u': | |
2306 | case 'x': | |
2307 | case 'i': | |
2308 | case 'c': | |
2309 | format = *p++; | |
2310 | break; | |
2311 | case 'b': | |
2312 | size = 1; | |
2313 | p++; | |
2314 | break; | |
2315 | case 'h': | |
2316 | size = 2; | |
2317 | p++; | |
2318 | break; | |
2319 | case 'w': | |
2320 | size = 4; | |
2321 | p++; | |
2322 | break; | |
2323 | case 'g': | |
2324 | case 'L': | |
2325 | size = 8; | |
2326 | p++; | |
2327 | break; | |
2328 | default: | |
2329 | goto next; | |
2330 | } | |
2331 | } | |
2332 | next: | |
2333 | if (*p != '\0' && !isspace(*p)) { | |
2334 | term_printf("invalid char in format: '%c'\n", *p); | |
2335 | goto fail; | |
2336 | } | |
9307c4c1 FB |
2337 | if (format < 0) |
2338 | format = default_fmt_format; | |
4c27ba27 FB |
2339 | if (format != 'i') { |
2340 | /* for 'i', not specifying a size gives -1 as size */ | |
2341 | if (size < 0) | |
2342 | size = default_fmt_size; | |
e90f009b | 2343 | default_fmt_size = size; |
4c27ba27 | 2344 | } |
9307c4c1 FB |
2345 | default_fmt_format = format; |
2346 | } else { | |
2347 | count = 1; | |
2348 | format = default_fmt_format; | |
4c27ba27 FB |
2349 | if (format != 'i') { |
2350 | size = default_fmt_size; | |
2351 | } else { | |
2352 | size = -1; | |
2353 | } | |
9307c4c1 FB |
2354 | } |
2355 | if (nb_args + 3 > MAX_ARGS) | |
2356 | goto error_args; | |
1c5bf3bf JM |
2357 | args[nb_args++] = (void*)(long)count; |
2358 | args[nb_args++] = (void*)(long)format; | |
2359 | args[nb_args++] = (void*)(long)size; | |
9307c4c1 | 2360 | } |
9dc39cba | 2361 | break; |
9307c4c1 | 2362 | case 'i': |
92a31b1f | 2363 | case 'l': |
9307c4c1 | 2364 | { |
c2efc95d | 2365 | int64_t val; |
7743e588 | 2366 | |
5fafdf24 | 2367 | while (isspace(*p)) |
9307c4c1 | 2368 | p++; |
3440557b | 2369 | if (*typestr == '?' || *typestr == '.') { |
3440557b FB |
2370 | if (*typestr == '?') { |
2371 | if (*p == '\0') | |
2372 | has_arg = 0; | |
2373 | else | |
2374 | has_arg = 1; | |
2375 | } else { | |
2376 | if (*p == '.') { | |
2377 | p++; | |
5fafdf24 | 2378 | while (isspace(*p)) |
3440557b FB |
2379 | p++; |
2380 | has_arg = 1; | |
2381 | } else { | |
2382 | has_arg = 0; | |
2383 | } | |
2384 | } | |
13224a87 | 2385 | typestr++; |
9307c4c1 FB |
2386 | if (nb_args >= MAX_ARGS) |
2387 | goto error_args; | |
1c5bf3bf | 2388 | args[nb_args++] = (void *)(long)has_arg; |
9307c4c1 FB |
2389 | if (!has_arg) { |
2390 | if (nb_args >= MAX_ARGS) | |
2391 | goto error_args; | |
2392 | val = -1; | |
2393 | goto add_num; | |
2394 | } | |
2395 | } | |
2396 | if (get_expr(&val, &p)) | |
2397 | goto fail; | |
2398 | add_num: | |
92a31b1f FB |
2399 | if (c == 'i') { |
2400 | if (nb_args >= MAX_ARGS) | |
2401 | goto error_args; | |
1c5bf3bf | 2402 | args[nb_args++] = (void *)(long)val; |
92a31b1f FB |
2403 | } else { |
2404 | if ((nb_args + 1) >= MAX_ARGS) | |
2405 | goto error_args; | |
7743e588 | 2406 | #if TARGET_PHYS_ADDR_BITS > 32 |
1c5bf3bf | 2407 | args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff); |
92a31b1f FB |
2408 | #else |
2409 | args[nb_args++] = (void *)0; | |
2410 | #endif | |
1c5bf3bf | 2411 | args[nb_args++] = (void *)(long)(val & 0xffffffff); |
92a31b1f | 2412 | } |
9307c4c1 FB |
2413 | } |
2414 | break; | |
2415 | case '-': | |
2416 | { | |
2417 | int has_option; | |
2418 | /* option */ | |
3b46e624 | 2419 | |
9307c4c1 FB |
2420 | c = *typestr++; |
2421 | if (c == '\0') | |
2422 | goto bad_type; | |
5fafdf24 | 2423 | while (isspace(*p)) |
9307c4c1 FB |
2424 | p++; |
2425 | has_option = 0; | |
2426 | if (*p == '-') { | |
2427 | p++; | |
2428 | if (*p != c) { | |
5fafdf24 | 2429 | term_printf("%s: unsupported option -%c\n", |
9307c4c1 FB |
2430 | cmdname, *p); |
2431 | goto fail; | |
2432 | } | |
2433 | p++; | |
2434 | has_option = 1; | |
2435 | } | |
2436 | if (nb_args >= MAX_ARGS) | |
2437 | goto error_args; | |
1c5bf3bf | 2438 | args[nb_args++] = (void *)(long)has_option; |
9307c4c1 FB |
2439 | } |
2440 | break; | |
2441 | default: | |
2442 | bad_type: | |
2443 | term_printf("%s: unknown type '%c'\n", cmdname, c); | |
2444 | goto fail; | |
2445 | } | |
9dc39cba | 2446 | } |
9307c4c1 FB |
2447 | /* check that all arguments were parsed */ |
2448 | while (isspace(*p)) | |
2449 | p++; | |
2450 | if (*p != '\0') { | |
5fafdf24 | 2451 | term_printf("%s: extraneous characters at the end of line\n", |
9307c4c1 FB |
2452 | cmdname); |
2453 | goto fail; | |
9dc39cba | 2454 | } |
9307c4c1 FB |
2455 | |
2456 | switch(nb_args) { | |
2457 | case 0: | |
a5f1b965 BS |
2458 | handler_0 = cmd->handler; |
2459 | handler_0(); | |
9307c4c1 FB |
2460 | break; |
2461 | case 1: | |
a5f1b965 BS |
2462 | handler_1 = cmd->handler; |
2463 | handler_1(args[0]); | |
9307c4c1 FB |
2464 | break; |
2465 | case 2: | |
a5f1b965 BS |
2466 | handler_2 = cmd->handler; |
2467 | handler_2(args[0], args[1]); | |
9307c4c1 FB |
2468 | break; |
2469 | case 3: | |
a5f1b965 BS |
2470 | handler_3 = cmd->handler; |
2471 | handler_3(args[0], args[1], args[2]); | |
9307c4c1 FB |
2472 | break; |
2473 | case 4: | |
a5f1b965 BS |
2474 | handler_4 = cmd->handler; |
2475 | handler_4(args[0], args[1], args[2], args[3]); | |
9307c4c1 FB |
2476 | break; |
2477 | case 5: | |
a5f1b965 BS |
2478 | handler_5 = cmd->handler; |
2479 | handler_5(args[0], args[1], args[2], args[3], args[4]); | |
9307c4c1 | 2480 | break; |
3440557b | 2481 | case 6: |
a5f1b965 BS |
2482 | handler_6 = cmd->handler; |
2483 | handler_6(args[0], args[1], args[2], args[3], args[4], args[5]); | |
3440557b | 2484 | break; |
ec36b695 | 2485 | case 7: |
a5f1b965 BS |
2486 | handler_7 = cmd->handler; |
2487 | handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); | |
ec36b695 | 2488 | break; |
9307c4c1 FB |
2489 | default: |
2490 | term_printf("unsupported number of arguments: %d\n", nb_args); | |
2491 | goto fail; | |
9dc39cba | 2492 | } |
9307c4c1 FB |
2493 | fail: |
2494 | for(i = 0; i < MAX_ARGS; i++) | |
2495 | qemu_free(str_allocated[i]); | |
9dc39cba | 2496 | return; |
9dc39cba FB |
2497 | } |
2498 | ||
81d0912d FB |
2499 | static void cmd_completion(const char *name, const char *list) |
2500 | { | |
2501 | const char *p, *pstart; | |
2502 | char cmd[128]; | |
2503 | int len; | |
2504 | ||
2505 | p = list; | |
2506 | for(;;) { | |
2507 | pstart = p; | |
2508 | p = strchr(p, '|'); | |
2509 | if (!p) | |
2510 | p = pstart + strlen(pstart); | |
2511 | len = p - pstart; | |
2512 | if (len > sizeof(cmd) - 2) | |
2513 | len = sizeof(cmd) - 2; | |
2514 | memcpy(cmd, pstart, len); | |
2515 | cmd[len] = '\0'; | |
2516 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
2517 | add_completion(cmd); | |
2518 | } | |
2519 | if (*p == '\0') | |
2520 | break; | |
2521 | p++; | |
2522 | } | |
2523 | } | |
2524 | ||
2525 | static void file_completion(const char *input) | |
2526 | { | |
2527 | DIR *ffs; | |
2528 | struct dirent *d; | |
2529 | char path[1024]; | |
2530 | char file[1024], file_prefix[1024]; | |
2531 | int input_path_len; | |
2532 | const char *p; | |
2533 | ||
5fafdf24 | 2534 | p = strrchr(input, '/'); |
81d0912d FB |
2535 | if (!p) { |
2536 | input_path_len = 0; | |
2537 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 2538 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
2539 | } else { |
2540 | input_path_len = p - input + 1; | |
2541 | memcpy(path, input, input_path_len); | |
2542 | if (input_path_len > sizeof(path) - 1) | |
2543 | input_path_len = sizeof(path) - 1; | |
2544 | path[input_path_len] = '\0'; | |
2545 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
2546 | } | |
2547 | #ifdef DEBUG_COMPLETION | |
2548 | term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix); | |
2549 | #endif | |
2550 | ffs = opendir(path); | |
2551 | if (!ffs) | |
2552 | return; | |
2553 | for(;;) { | |
2554 | struct stat sb; | |
2555 | d = readdir(ffs); | |
2556 | if (!d) | |
2557 | break; | |
2558 | if (strstart(d->d_name, file_prefix, NULL)) { | |
2559 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
2560 | if (input_path_len < sizeof(file)) |
2561 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
2562 | d->d_name); | |
81d0912d FB |
2563 | /* stat the file to find out if it's a directory. |
2564 | * In that case add a slash to speed up typing long paths | |
2565 | */ | |
2566 | stat(file, &sb); | |
2567 | if(S_ISDIR(sb.st_mode)) | |
363a37d5 | 2568 | pstrcat(file, sizeof(file), "/"); |
81d0912d FB |
2569 | add_completion(file); |
2570 | } | |
2571 | } | |
2572 | closedir(ffs); | |
2573 | } | |
2574 | ||
2575 | static void block_completion_it(void *opaque, const char *name) | |
2576 | { | |
2577 | const char *input = opaque; | |
2578 | ||
2579 | if (input[0] == '\0' || | |
2580 | !strncmp(name, (char *)input, strlen(input))) { | |
2581 | add_completion(name); | |
2582 | } | |
2583 | } | |
2584 | ||
2585 | /* NOTE: this parser is an approximate form of the real command parser */ | |
2586 | static void parse_cmdline(const char *cmdline, | |
2587 | int *pnb_args, char **args) | |
2588 | { | |
2589 | const char *p; | |
2590 | int nb_args, ret; | |
2591 | char buf[1024]; | |
2592 | ||
2593 | p = cmdline; | |
2594 | nb_args = 0; | |
2595 | for(;;) { | |
2596 | while (isspace(*p)) | |
2597 | p++; | |
2598 | if (*p == '\0') | |
2599 | break; | |
2600 | if (nb_args >= MAX_ARGS) | |
2601 | break; | |
2602 | ret = get_str(buf, sizeof(buf), &p); | |
2603 | args[nb_args] = qemu_strdup(buf); | |
2604 | nb_args++; | |
2605 | if (ret < 0) | |
2606 | break; | |
2607 | } | |
2608 | *pnb_args = nb_args; | |
2609 | } | |
2610 | ||
7e2515e8 | 2611 | void readline_find_completion(const char *cmdline) |
81d0912d FB |
2612 | { |
2613 | const char *cmdname; | |
2614 | char *args[MAX_ARGS]; | |
2615 | int nb_args, i, len; | |
2616 | const char *ptype, *str; | |
8662d656 | 2617 | const term_cmd_t *cmd; |
64866c3d | 2618 | const KeyDef *key; |
81d0912d FB |
2619 | |
2620 | parse_cmdline(cmdline, &nb_args, args); | |
2621 | #ifdef DEBUG_COMPLETION | |
2622 | for(i = 0; i < nb_args; i++) { | |
2623 | term_printf("arg%d = '%s'\n", i, (char *)args[i]); | |
2624 | } | |
2625 | #endif | |
2626 | ||
2627 | /* if the line ends with a space, it means we want to complete the | |
2628 | next arg */ | |
2629 | len = strlen(cmdline); | |
2630 | if (len > 0 && isspace(cmdline[len - 1])) { | |
2631 | if (nb_args >= MAX_ARGS) | |
2632 | return; | |
2633 | args[nb_args++] = qemu_strdup(""); | |
2634 | } | |
2635 | if (nb_args <= 1) { | |
2636 | /* command completion */ | |
2637 | if (nb_args == 0) | |
2638 | cmdname = ""; | |
2639 | else | |
2640 | cmdname = args[0]; | |
2641 | completion_index = strlen(cmdname); | |
2642 | for(cmd = term_cmds; cmd->name != NULL; cmd++) { | |
2643 | cmd_completion(cmdname, cmd->name); | |
2644 | } | |
2645 | } else { | |
2646 | /* find the command */ | |
2647 | for(cmd = term_cmds; cmd->name != NULL; cmd++) { | |
2648 | if (compare_cmd(args[0], cmd->name)) | |
2649 | goto found; | |
2650 | } | |
2651 | return; | |
2652 | found: | |
2653 | ptype = cmd->args_type; | |
2654 | for(i = 0; i < nb_args - 2; i++) { | |
2655 | if (*ptype != '\0') { | |
2656 | ptype++; | |
2657 | while (*ptype == '?') | |
2658 | ptype++; | |
2659 | } | |
2660 | } | |
2661 | str = args[nb_args - 1]; | |
2662 | switch(*ptype) { | |
2663 | case 'F': | |
2664 | /* file completion */ | |
2665 | completion_index = strlen(str); | |
2666 | file_completion(str); | |
2667 | break; | |
2668 | case 'B': | |
2669 | /* block device name completion */ | |
2670 | completion_index = strlen(str); | |
2671 | bdrv_iterate(block_completion_it, (void *)str); | |
2672 | break; | |
7fe48483 FB |
2673 | case 's': |
2674 | /* XXX: more generic ? */ | |
2675 | if (!strcmp(cmd->name, "info")) { | |
2676 | completion_index = strlen(str); | |
2677 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { | |
2678 | cmd_completion(str, cmd->name); | |
2679 | } | |
64866c3d FB |
2680 | } else if (!strcmp(cmd->name, "sendkey")) { |
2681 | completion_index = strlen(str); | |
2682 | for(key = key_defs; key->name != NULL; key++) { | |
2683 | cmd_completion(str, key->name); | |
2684 | } | |
7fe48483 FB |
2685 | } |
2686 | break; | |
81d0912d FB |
2687 | default: |
2688 | break; | |
2689 | } | |
2690 | } | |
2691 | for(i = 0; i < nb_args; i++) | |
2692 | qemu_free(args[i]); | |
2693 | } | |
2694 | ||
7e2515e8 | 2695 | static int term_can_read(void *opaque) |
9dc39cba | 2696 | { |
7e2515e8 | 2697 | return 128; |
9dc39cba FB |
2698 | } |
2699 | ||
7e2515e8 | 2700 | static void term_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 2701 | { |
7e2515e8 FB |
2702 | int i; |
2703 | for(i = 0; i < size; i++) | |
2704 | readline_handle_byte(buf[i]); | |
9dc39cba FB |
2705 | } |
2706 | ||
d8f44609 AL |
2707 | static int monitor_suspended; |
2708 | ||
7e2515e8 | 2709 | static void monitor_handle_command1(void *opaque, const char *cmdline) |
aa455485 | 2710 | { |
7e2515e8 | 2711 | monitor_handle_command(cmdline); |
d8f44609 AL |
2712 | if (!monitor_suspended) |
2713 | monitor_start_input(); | |
2714 | else | |
2715 | monitor_suspended = 2; | |
2716 | } | |
2717 | ||
2718 | void monitor_suspend(void) | |
2719 | { | |
2720 | monitor_suspended = 1; | |
2721 | } | |
2722 | ||
2723 | void monitor_resume(void) | |
2724 | { | |
2725 | if (monitor_suspended == 2) | |
2726 | monitor_start_input(); | |
2727 | monitor_suspended = 0; | |
aa455485 FB |
2728 | } |
2729 | ||
83ab7950 | 2730 | static void monitor_start_input(void) |
aa455485 | 2731 | { |
7e2515e8 | 2732 | readline_start("(qemu) ", 0, monitor_handle_command1, NULL); |
aa455485 FB |
2733 | } |
2734 | ||
86e94dea TS |
2735 | static void term_event(void *opaque, int event) |
2736 | { | |
2737 | if (event != CHR_EVENT_RESET) | |
2738 | return; | |
2739 | ||
2740 | if (!hide_banner) | |
2741 | term_printf("QEMU %s monitor - type 'help' for more information\n", | |
2742 | QEMU_VERSION); | |
2743 | monitor_start_input(); | |
2744 | } | |
2745 | ||
20d8a3ed TS |
2746 | static int is_first_init = 1; |
2747 | ||
7e2515e8 | 2748 | void monitor_init(CharDriverState *hd, int show_banner) |
aa455485 | 2749 | { |
20d8a3ed TS |
2750 | int i; |
2751 | ||
2752 | if (is_first_init) { | |
c8256f9d AZ |
2753 | key_timer = qemu_new_timer(vm_clock, release_keys, NULL); |
2754 | if (!key_timer) | |
2755 | return; | |
20d8a3ed TS |
2756 | for (i = 0; i < MAX_MON; i++) { |
2757 | monitor_hd[i] = NULL; | |
2758 | } | |
2759 | is_first_init = 0; | |
2760 | } | |
2761 | for (i = 0; i < MAX_MON; i++) { | |
2762 | if (monitor_hd[i] == NULL) { | |
2763 | monitor_hd[i] = hd; | |
2764 | break; | |
2765 | } | |
2766 | } | |
2767 | ||
86e94dea TS |
2768 | hide_banner = !show_banner; |
2769 | ||
e5b0bc44 | 2770 | qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL); |
83ab7950 AL |
2771 | |
2772 | readline_start("", 0, monitor_handle_command1, NULL); | |
aa455485 FB |
2773 | } |
2774 | ||
7e2515e8 FB |
2775 | /* XXX: use threads ? */ |
2776 | /* modal monitor readline */ | |
2777 | static int monitor_readline_started; | |
2778 | static char *monitor_readline_buf; | |
2779 | static int monitor_readline_buf_size; | |
81d0912d | 2780 | |
7e2515e8 | 2781 | static void monitor_readline_cb(void *opaque, const char *input) |
81d0912d | 2782 | { |
7e2515e8 FB |
2783 | pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input); |
2784 | monitor_readline_started = 0; | |
81d0912d FB |
2785 | } |
2786 | ||
7e2515e8 FB |
2787 | void monitor_readline(const char *prompt, int is_password, |
2788 | char *buf, int buf_size) | |
9dc39cba | 2789 | { |
20d8a3ed | 2790 | int i; |
bc0129d9 | 2791 | int old_focus[MAX_MON]; |
20d8a3ed | 2792 | |
7e2515e8 | 2793 | if (is_password) { |
bc0129d9 AL |
2794 | for (i = 0; i < MAX_MON; i++) { |
2795 | old_focus[i] = 0; | |
2796 | if (monitor_hd[i]) { | |
2797 | old_focus[i] = monitor_hd[i]->focus; | |
2798 | monitor_hd[i]->focus = 0; | |
20d8a3ed | 2799 | qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS); |
bc0129d9 AL |
2800 | } |
2801 | } | |
9dc39cba | 2802 | } |
bc0129d9 | 2803 | |
7e2515e8 FB |
2804 | readline_start(prompt, is_password, monitor_readline_cb, NULL); |
2805 | monitor_readline_buf = buf; | |
2806 | monitor_readline_buf_size = buf_size; | |
2807 | monitor_readline_started = 1; | |
2808 | while (monitor_readline_started) { | |
2809 | main_loop_wait(10); | |
9dc39cba | 2810 | } |
bc0129d9 AL |
2811 | /* restore original focus */ |
2812 | if (is_password) { | |
2813 | for (i = 0; i < MAX_MON; i++) | |
2814 | if (old_focus[i]) | |
2815 | monitor_hd[i]->focus = old_focus[i]; | |
2816 | } | |
9dc39cba | 2817 | } |