]>
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 | */ | |
511d2b14 | 24 | #include <dirent.h> |
87ecb68b | 25 | #include "hw/hw.h" |
cae4956e | 26 | #include "hw/qdev.h" |
87ecb68b PB |
27 | #include "hw/usb.h" |
28 | #include "hw/pcmcia.h" | |
29 | #include "hw/pc.h" | |
30 | #include "hw/pci.h" | |
9dd986cc | 31 | #include "hw/watchdog.h" |
87ecb68b PB |
32 | #include "gdbstub.h" |
33 | #include "net.h" | |
34 | #include "qemu-char.h" | |
35 | #include "sysemu.h" | |
376253ec AL |
36 | #include "monitor.h" |
37 | #include "readline.h" | |
87ecb68b PB |
38 | #include "console.h" |
39 | #include "block.h" | |
40 | #include "audio/audio.h" | |
9307c4c1 | 41 | #include "disas.h" |
df751fa8 | 42 | #include "balloon.h" |
c8256f9d | 43 | #include "qemu-timer.h" |
5bb7910a | 44 | #include "migration.h" |
7ba1e619 | 45 | #include "kvm.h" |
76655d6d | 46 | #include "acl.h" |
f7188bbe LC |
47 | #include "qint.h" |
48 | #include "qdict.h" | |
49 | #include "qstring.h" | |
6a5bd307 | 50 | |
9dc39cba | 51 | //#define DEBUG |
81d0912d | 52 | //#define DEBUG_COMPLETION |
9dc39cba | 53 | |
9307c4c1 FB |
54 | /* |
55 | * Supported types: | |
5fafdf24 | 56 | * |
9307c4c1 | 57 | * 'F' filename |
81d0912d | 58 | * 'B' block device name |
9307c4c1 | 59 | * 's' string (accept optional quote) |
92a31b1f FB |
60 | * 'i' 32 bit integer |
61 | * 'l' target long (32 or 64 bit) | |
9307c4c1 FB |
62 | * '/' optional gdb-like print format (like "/10x") |
63 | * | |
64 | * '?' optional type (for 'F', 's' and 'i') | |
65 | * | |
66 | */ | |
67 | ||
376253ec | 68 | typedef struct mon_cmd_t { |
9dc39cba | 69 | const char *name; |
9307c4c1 | 70 | const char *args_type; |
a5f1b965 | 71 | void *handler; |
9dc39cba FB |
72 | const char *params; |
73 | const char *help; | |
376253ec | 74 | } mon_cmd_t; |
9dc39cba | 75 | |
f07918fd MM |
76 | /* file descriptors passed via SCM_RIGHTS */ |
77 | typedef struct mon_fd_t mon_fd_t; | |
78 | struct mon_fd_t { | |
79 | char *name; | |
80 | int fd; | |
81 | LIST_ENTRY(mon_fd_t) next; | |
82 | }; | |
83 | ||
87127161 AL |
84 | struct Monitor { |
85 | CharDriverState *chr; | |
731b0364 AL |
86 | int flags; |
87 | int suspend_cnt; | |
88 | uint8_t outbuf[1024]; | |
89 | int outbuf_index; | |
90 | ReadLineState *rs; | |
91 | CPUState *mon_cpu; | |
92 | BlockDriverCompletionFunc *password_completion_cb; | |
93 | void *password_opaque; | |
f07918fd | 94 | LIST_HEAD(,mon_fd_t) fds; |
87127161 AL |
95 | LIST_ENTRY(Monitor) entry; |
96 | }; | |
97 | ||
98 | static LIST_HEAD(mon_list, Monitor) mon_list; | |
7e2515e8 | 99 | |
376253ec AL |
100 | static const mon_cmd_t mon_cmds[]; |
101 | static const mon_cmd_t info_cmds[]; | |
9dc39cba | 102 | |
87127161 | 103 | Monitor *cur_mon = NULL; |
376253ec | 104 | |
731b0364 AL |
105 | static void monitor_command_cb(Monitor *mon, const char *cmdline, |
106 | void *opaque); | |
83ab7950 | 107 | |
731b0364 AL |
108 | static void monitor_read_command(Monitor *mon, int show_prompt) |
109 | { | |
110 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); | |
111 | if (show_prompt) | |
112 | readline_show_prompt(mon->rs); | |
113 | } | |
6a00d601 | 114 | |
cde76ee1 AL |
115 | static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
116 | void *opaque) | |
bb5fc20f | 117 | { |
cde76ee1 AL |
118 | if (mon->rs) { |
119 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); | |
120 | /* prompt is printed on return from the command handler */ | |
121 | return 0; | |
122 | } else { | |
123 | monitor_printf(mon, "terminal does not support password prompting\n"); | |
124 | return -ENOTTY; | |
125 | } | |
bb5fc20f AL |
126 | } |
127 | ||
376253ec | 128 | void monitor_flush(Monitor *mon) |
7e2515e8 | 129 | { |
731b0364 AL |
130 | if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) { |
131 | qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index); | |
132 | mon->outbuf_index = 0; | |
7e2515e8 FB |
133 | } |
134 | } | |
135 | ||
136 | /* flush at every end of line or if the buffer is full */ | |
376253ec | 137 | static void monitor_puts(Monitor *mon, const char *str) |
7e2515e8 | 138 | { |
60fe76f3 | 139 | char c; |
731b0364 AL |
140 | |
141 | if (!mon) | |
142 | return; | |
143 | ||
7e2515e8 FB |
144 | for(;;) { |
145 | c = *str++; | |
146 | if (c == '\0') | |
147 | break; | |
7ba1260a | 148 | if (c == '\n') |
731b0364 AL |
149 | mon->outbuf[mon->outbuf_index++] = '\r'; |
150 | mon->outbuf[mon->outbuf_index++] = c; | |
151 | if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1) | |
152 | || c == '\n') | |
376253ec | 153 | monitor_flush(mon); |
7e2515e8 FB |
154 | } |
155 | } | |
156 | ||
376253ec | 157 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
9dc39cba | 158 | { |
81d0912d | 159 | char buf[4096]; |
81d0912d | 160 | vsnprintf(buf, sizeof(buf), fmt, ap); |
376253ec | 161 | monitor_puts(mon, buf); |
9dc39cba FB |
162 | } |
163 | ||
376253ec | 164 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
9dc39cba | 165 | { |
7e2515e8 FB |
166 | va_list ap; |
167 | va_start(ap, fmt); | |
376253ec | 168 | monitor_vprintf(mon, fmt, ap); |
7e2515e8 | 169 | va_end(ap); |
9dc39cba FB |
170 | } |
171 | ||
376253ec | 172 | void monitor_print_filename(Monitor *mon, const char *filename) |
fef30743 TS |
173 | { |
174 | int i; | |
175 | ||
176 | for (i = 0; filename[i]; i++) { | |
28a76be8 AL |
177 | switch (filename[i]) { |
178 | case ' ': | |
179 | case '"': | |
180 | case '\\': | |
181 | monitor_printf(mon, "\\%c", filename[i]); | |
182 | break; | |
183 | case '\t': | |
184 | monitor_printf(mon, "\\t"); | |
185 | break; | |
186 | case '\r': | |
187 | monitor_printf(mon, "\\r"); | |
188 | break; | |
189 | case '\n': | |
190 | monitor_printf(mon, "\\n"); | |
191 | break; | |
192 | default: | |
193 | monitor_printf(mon, "%c", filename[i]); | |
194 | break; | |
195 | } | |
fef30743 TS |
196 | } |
197 | } | |
198 | ||
7fe48483 FB |
199 | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
200 | { | |
201 | va_list ap; | |
202 | va_start(ap, fmt); | |
376253ec | 203 | monitor_vprintf((Monitor *)stream, fmt, ap); |
7fe48483 FB |
204 | va_end(ap); |
205 | return 0; | |
206 | } | |
207 | ||
9dc39cba FB |
208 | static int compare_cmd(const char *name, const char *list) |
209 | { | |
210 | const char *p, *pstart; | |
211 | int len; | |
212 | len = strlen(name); | |
213 | p = list; | |
214 | for(;;) { | |
215 | pstart = p; | |
216 | p = strchr(p, '|'); | |
217 | if (!p) | |
218 | p = pstart + strlen(pstart); | |
219 | if ((p - pstart) == len && !memcmp(pstart, name, len)) | |
220 | return 1; | |
221 | if (*p == '\0') | |
222 | break; | |
223 | p++; | |
224 | } | |
225 | return 0; | |
226 | } | |
227 | ||
376253ec AL |
228 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
229 | const char *prefix, const char *name) | |
9dc39cba | 230 | { |
376253ec | 231 | const mon_cmd_t *cmd; |
9dc39cba FB |
232 | |
233 | for(cmd = cmds; cmd->name != NULL; cmd++) { | |
234 | if (!name || !strcmp(name, cmd->name)) | |
376253ec AL |
235 | monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name, |
236 | cmd->params, cmd->help); | |
9dc39cba FB |
237 | } |
238 | } | |
239 | ||
376253ec | 240 | static void help_cmd(Monitor *mon, const char *name) |
9dc39cba FB |
241 | { |
242 | if (name && !strcmp(name, "info")) { | |
376253ec | 243 | help_cmd_dump(mon, info_cmds, "info ", NULL); |
9dc39cba | 244 | } else { |
376253ec | 245 | help_cmd_dump(mon, mon_cmds, "", name); |
f193c797 | 246 | if (name && !strcmp(name, "log")) { |
8662d656 | 247 | const CPULogItem *item; |
376253ec AL |
248 | monitor_printf(mon, "Log items (comma separated):\n"); |
249 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); | |
f193c797 | 250 | for(item = cpu_log_items; item->mask != 0; item++) { |
376253ec | 251 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
f193c797 FB |
252 | } |
253 | } | |
9dc39cba FB |
254 | } |
255 | } | |
256 | ||
d54908a5 | 257 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
38183186 | 258 | { |
d54908a5 | 259 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
38183186 LC |
260 | } |
261 | ||
d54908a5 | 262 | static void do_commit(Monitor *mon, const QDict *qdict) |
9dc39cba | 263 | { |
751c6a17 GH |
264 | int all_devices; |
265 | DriveInfo *dinfo; | |
d54908a5 | 266 | const char *device = qdict_get_str(qdict, "device"); |
2dc7b602 | 267 | |
7954c734 | 268 | all_devices = !strcmp(device, "all"); |
751c6a17 GH |
269 | TAILQ_FOREACH(dinfo, &drives, next) { |
270 | if (!all_devices) | |
73006d2a | 271 | if (strcmp(bdrv_get_device_name(dinfo->bdrv), device)) |
751c6a17 GH |
272 | continue; |
273 | bdrv_commit(dinfo->bdrv); | |
9dc39cba FB |
274 | } |
275 | } | |
276 | ||
d54908a5 | 277 | static void do_info(Monitor *mon, const QDict *qdict) |
9dc39cba | 278 | { |
376253ec | 279 | const mon_cmd_t *cmd; |
d54908a5 | 280 | const char *item = qdict_get_try_str(qdict, "item"); |
376253ec | 281 | void (*handler)(Monitor *); |
9dc39cba | 282 | |
9307c4c1 | 283 | if (!item) |
9dc39cba | 284 | goto help; |
9dc39cba | 285 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
5fafdf24 | 286 | if (compare_cmd(item, cmd->name)) |
9dc39cba FB |
287 | goto found; |
288 | } | |
289 | help: | |
376253ec | 290 | help_cmd(mon, "info"); |
9dc39cba FB |
291 | return; |
292 | found: | |
a5f1b965 | 293 | handler = cmd->handler; |
376253ec | 294 | handler(mon); |
9dc39cba FB |
295 | } |
296 | ||
376253ec | 297 | static void do_info_version(Monitor *mon) |
9bc9d1c7 | 298 | { |
4a19f1ec | 299 | monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION); |
9bc9d1c7 FB |
300 | } |
301 | ||
376253ec | 302 | static void do_info_name(Monitor *mon) |
c35734b2 TS |
303 | { |
304 | if (qemu_name) | |
376253ec | 305 | monitor_printf(mon, "%s\n", qemu_name); |
c35734b2 TS |
306 | } |
307 | ||
bf4f74c0 | 308 | #if defined(TARGET_I386) |
376253ec | 309 | static void do_info_hpet(Monitor *mon) |
16b29ae1 | 310 | { |
376253ec AL |
311 | monitor_printf(mon, "HPET is %s by QEMU\n", |
312 | (no_hpet) ? "disabled" : "enabled"); | |
16b29ae1 | 313 | } |
bf4f74c0 | 314 | #endif |
16b29ae1 | 315 | |
376253ec | 316 | static void do_info_uuid(Monitor *mon) |
a36e69dd | 317 | { |
376253ec AL |
318 | monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], |
319 | qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], | |
320 | qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], | |
321 | qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], | |
322 | qemu_uuid[14], qemu_uuid[15]); | |
a36e69dd TS |
323 | } |
324 | ||
6a00d601 | 325 | /* get the current CPU defined by the user */ |
9596ebb7 | 326 | static int mon_set_cpu(int cpu_index) |
6a00d601 FB |
327 | { |
328 | CPUState *env; | |
329 | ||
330 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
331 | if (env->cpu_index == cpu_index) { | |
731b0364 | 332 | cur_mon->mon_cpu = env; |
6a00d601 FB |
333 | return 0; |
334 | } | |
335 | } | |
336 | return -1; | |
337 | } | |
338 | ||
9596ebb7 | 339 | static CPUState *mon_get_cpu(void) |
6a00d601 | 340 | { |
731b0364 | 341 | if (!cur_mon->mon_cpu) { |
6a00d601 FB |
342 | mon_set_cpu(0); |
343 | } | |
4c0960c0 | 344 | cpu_synchronize_state(cur_mon->mon_cpu); |
731b0364 | 345 | return cur_mon->mon_cpu; |
6a00d601 FB |
346 | } |
347 | ||
376253ec | 348 | static void do_info_registers(Monitor *mon) |
9307c4c1 | 349 | { |
6a00d601 FB |
350 | CPUState *env; |
351 | env = mon_get_cpu(); | |
352 | if (!env) | |
353 | return; | |
9307c4c1 | 354 | #ifdef TARGET_I386 |
376253ec | 355 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
d24b15a8 | 356 | X86_DUMP_FPU); |
9307c4c1 | 357 | #else |
376253ec | 358 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
7fe48483 | 359 | 0); |
9307c4c1 FB |
360 | #endif |
361 | } | |
362 | ||
376253ec | 363 | static void do_info_cpus(Monitor *mon) |
6a00d601 FB |
364 | { |
365 | CPUState *env; | |
366 | ||
367 | /* just to set the default cpu if not already done */ | |
368 | mon_get_cpu(); | |
369 | ||
370 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
4c0960c0 | 371 | cpu_synchronize_state(env); |
376253ec | 372 | monitor_printf(mon, "%c CPU #%d:", |
731b0364 | 373 | (env == mon->mon_cpu) ? '*' : ' ', |
376253ec | 374 | env->cpu_index); |
6a00d601 | 375 | #if defined(TARGET_I386) |
376253ec AL |
376 | monitor_printf(mon, " pc=0x" TARGET_FMT_lx, |
377 | env->eip + env->segs[R_CS].base); | |
e80e1cc4 | 378 | #elif defined(TARGET_PPC) |
376253ec | 379 | monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip); |
ba3c64fb | 380 | #elif defined(TARGET_SPARC) |
376253ec AL |
381 | monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, |
382 | env->pc, env->npc); | |
ead9360e | 383 | #elif defined(TARGET_MIPS) |
376253ec | 384 | monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC); |
ce5232c5 | 385 | #endif |
ead9360e | 386 | if (env->halted) |
376253ec AL |
387 | monitor_printf(mon, " (halted)"); |
388 | monitor_printf(mon, "\n"); | |
6a00d601 FB |
389 | } |
390 | } | |
391 | ||
d54908a5 | 392 | static void do_cpu_set(Monitor *mon, const QDict *qdict) |
6a00d601 | 393 | { |
d54908a5 | 394 | int index = qdict_get_int(qdict, "index"); |
6a00d601 | 395 | if (mon_set_cpu(index) < 0) |
376253ec | 396 | monitor_printf(mon, "Invalid CPU index\n"); |
6a00d601 FB |
397 | } |
398 | ||
376253ec | 399 | static void do_info_jit(Monitor *mon) |
e3db7226 | 400 | { |
376253ec | 401 | dump_exec_info((FILE *)mon, monitor_fprintf); |
e3db7226 FB |
402 | } |
403 | ||
376253ec | 404 | static void do_info_history(Monitor *mon) |
aa455485 FB |
405 | { |
406 | int i; | |
7e2515e8 | 407 | const char *str; |
3b46e624 | 408 | |
cde76ee1 AL |
409 | if (!mon->rs) |
410 | return; | |
7e2515e8 FB |
411 | i = 0; |
412 | for(;;) { | |
731b0364 | 413 | str = readline_get_history(mon->rs, i); |
7e2515e8 FB |
414 | if (!str) |
415 | break; | |
376253ec | 416 | monitor_printf(mon, "%d: '%s'\n", i, str); |
8e3a9fd2 | 417 | i++; |
aa455485 FB |
418 | } |
419 | } | |
420 | ||
76a66253 JM |
421 | #if defined(TARGET_PPC) |
422 | /* XXX: not implemented in other targets */ | |
376253ec | 423 | static void do_info_cpu_stats(Monitor *mon) |
76a66253 JM |
424 | { |
425 | CPUState *env; | |
426 | ||
427 | env = mon_get_cpu(); | |
376253ec | 428 | cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0); |
76a66253 JM |
429 | } |
430 | #endif | |
431 | ||
f96fc8a0 | 432 | static void do_quit(Monitor *mon, const QDict *qdict) |
9dc39cba FB |
433 | { |
434 | exit(0); | |
435 | } | |
436 | ||
376253ec | 437 | static int eject_device(Monitor *mon, BlockDriverState *bs, int force) |
9dc39cba FB |
438 | { |
439 | if (bdrv_is_inserted(bs)) { | |
440 | if (!force) { | |
441 | if (!bdrv_is_removable(bs)) { | |
376253ec | 442 | monitor_printf(mon, "device is not removable\n"); |
9dc39cba FB |
443 | return -1; |
444 | } | |
445 | if (bdrv_is_locked(bs)) { | |
376253ec | 446 | monitor_printf(mon, "device is locked\n"); |
9dc39cba FB |
447 | return -1; |
448 | } | |
449 | } | |
450 | bdrv_close(bs); | |
451 | } | |
452 | return 0; | |
453 | } | |
454 | ||
f18c16de | 455 | static void do_eject(Monitor *mon, const QDict *qdict) |
9dc39cba FB |
456 | { |
457 | BlockDriverState *bs; | |
f18c16de LC |
458 | int force = qdict_get_int(qdict, "force"); |
459 | const char *filename = qdict_get_str(qdict, "filename"); | |
9dc39cba | 460 | |
9307c4c1 | 461 | bs = bdrv_find(filename); |
9dc39cba | 462 | if (!bs) { |
376253ec | 463 | monitor_printf(mon, "device not found\n"); |
9dc39cba FB |
464 | return; |
465 | } | |
376253ec | 466 | eject_device(mon, bs, force); |
9dc39cba FB |
467 | } |
468 | ||
376253ec AL |
469 | static void do_change_block(Monitor *mon, const char *device, |
470 | const char *filename, const char *fmt) | |
9dc39cba FB |
471 | { |
472 | BlockDriverState *bs; | |
2ecea9b8 | 473 | BlockDriver *drv = NULL; |
9dc39cba | 474 | |
9307c4c1 | 475 | bs = bdrv_find(device); |
9dc39cba | 476 | if (!bs) { |
376253ec | 477 | monitor_printf(mon, "device not found\n"); |
9dc39cba FB |
478 | return; |
479 | } | |
2ecea9b8 AJ |
480 | if (fmt) { |
481 | drv = bdrv_find_format(fmt); | |
482 | if (!drv) { | |
376253ec | 483 | monitor_printf(mon, "invalid format %s\n", fmt); |
2ecea9b8 AJ |
484 | return; |
485 | } | |
486 | } | |
376253ec | 487 | if (eject_device(mon, bs, 0) < 0) |
9dc39cba | 488 | return; |
2ecea9b8 | 489 | bdrv_open2(bs, filename, 0, drv); |
376253ec | 490 | monitor_read_bdrv_key_start(mon, bs, NULL, NULL); |
bb5fc20f AL |
491 | } |
492 | ||
376253ec AL |
493 | static void change_vnc_password_cb(Monitor *mon, const char *password, |
494 | void *opaque) | |
bb5fc20f AL |
495 | { |
496 | if (vnc_display_password(NULL, password) < 0) | |
376253ec | 497 | monitor_printf(mon, "could not set VNC server password\n"); |
bb5fc20f | 498 | |
731b0364 | 499 | monitor_read_command(mon, 1); |
9dc39cba FB |
500 | } |
501 | ||
376253ec | 502 | static void do_change_vnc(Monitor *mon, const char *target, const char *arg) |
e25a5822 | 503 | { |
70848515 | 504 | if (strcmp(target, "passwd") == 0 || |
28a76be8 AL |
505 | strcmp(target, "password") == 0) { |
506 | if (arg) { | |
bb5fc20f | 507 | char password[9]; |
28a76be8 AL |
508 | strncpy(password, arg, sizeof(password)); |
509 | password[sizeof(password) - 1] = '\0'; | |
376253ec | 510 | change_vnc_password_cb(mon, password, NULL); |
bb5fc20f | 511 | } else { |
376253ec | 512 | monitor_read_password(mon, change_vnc_password_cb, NULL); |
bb5fc20f | 513 | } |
70848515 | 514 | } else { |
28a76be8 | 515 | if (vnc_display_open(NULL, target) < 0) |
376253ec | 516 | monitor_printf(mon, "could not start VNC server on %s\n", target); |
70848515 | 517 | } |
e25a5822 TS |
518 | } |
519 | ||
1d4daa91 | 520 | static void do_change(Monitor *mon, const QDict *qdict) |
e25a5822 | 521 | { |
1d4daa91 LC |
522 | const char *device = qdict_get_str(qdict, "device"); |
523 | const char *target = qdict_get_str(qdict, "target"); | |
524 | const char *arg = qdict_get_try_str(qdict, "arg"); | |
e25a5822 | 525 | if (strcmp(device, "vnc") == 0) { |
28a76be8 | 526 | do_change_vnc(mon, target, arg); |
e25a5822 | 527 | } else { |
28a76be8 | 528 | do_change_block(mon, device, target, arg); |
e25a5822 TS |
529 | } |
530 | } | |
531 | ||
d54908a5 | 532 | static void do_screen_dump(Monitor *mon, const QDict *qdict) |
59a983b9 | 533 | { |
d54908a5 | 534 | vga_hw_screen_dump(qdict_get_str(qdict, "filename")); |
59a983b9 FB |
535 | } |
536 | ||
d54908a5 | 537 | static void do_logfile(Monitor *mon, const QDict *qdict) |
e735b91c | 538 | { |
d54908a5 | 539 | cpu_set_log_filename(qdict_get_str(qdict, "filename")); |
e735b91c PB |
540 | } |
541 | ||
d54908a5 | 542 | static void do_log(Monitor *mon, const QDict *qdict) |
f193c797 FB |
543 | { |
544 | int mask; | |
d54908a5 | 545 | const char *items = qdict_get_str(qdict, "items"); |
3b46e624 | 546 | |
9307c4c1 | 547 | if (!strcmp(items, "none")) { |
f193c797 FB |
548 | mask = 0; |
549 | } else { | |
9307c4c1 | 550 | mask = cpu_str_to_log_mask(items); |
f193c797 | 551 | if (!mask) { |
376253ec | 552 | help_cmd(mon, "log"); |
f193c797 FB |
553 | return; |
554 | } | |
555 | } | |
556 | cpu_set_log(mask); | |
557 | } | |
558 | ||
d54908a5 | 559 | static void do_singlestep(Monitor *mon, const QDict *qdict) |
1b530a6d | 560 | { |
d54908a5 | 561 | const char *option = qdict_get_try_str(qdict, "option"); |
1b530a6d AJ |
562 | if (!option || !strcmp(option, "on")) { |
563 | singlestep = 1; | |
564 | } else if (!strcmp(option, "off")) { | |
565 | singlestep = 0; | |
566 | } else { | |
567 | monitor_printf(mon, "unexpected option %s\n", option); | |
568 | } | |
569 | } | |
570 | ||
f96fc8a0 | 571 | static void do_stop(Monitor *mon, const QDict *qdict) |
8a7ddc38 FB |
572 | { |
573 | vm_stop(EXCP_INTERRUPT); | |
574 | } | |
575 | ||
bb5fc20f | 576 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs); |
c0f4ce77 | 577 | |
376253ec AL |
578 | struct bdrv_iterate_context { |
579 | Monitor *mon; | |
580 | int err; | |
581 | }; | |
582 | ||
f96fc8a0 | 583 | static void do_cont(Monitor *mon, const QDict *qdict) |
8a7ddc38 | 584 | { |
376253ec | 585 | struct bdrv_iterate_context context = { mon, 0 }; |
c0f4ce77 | 586 | |
376253ec | 587 | bdrv_iterate(encrypted_bdrv_it, &context); |
c0f4ce77 | 588 | /* only resume the vm if all keys are set and valid */ |
376253ec | 589 | if (!context.err) |
c0f4ce77 | 590 | vm_start(); |
8a7ddc38 FB |
591 | } |
592 | ||
bb5fc20f AL |
593 | static void bdrv_key_cb(void *opaque, int err) |
594 | { | |
376253ec AL |
595 | Monitor *mon = opaque; |
596 | ||
bb5fc20f AL |
597 | /* another key was set successfully, retry to continue */ |
598 | if (!err) | |
f96fc8a0 | 599 | do_cont(mon, NULL); |
bb5fc20f AL |
600 | } |
601 | ||
602 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs) | |
603 | { | |
376253ec | 604 | struct bdrv_iterate_context *context = opaque; |
bb5fc20f | 605 | |
376253ec AL |
606 | if (!context->err && bdrv_key_required(bs)) { |
607 | context->err = -EBUSY; | |
608 | monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb, | |
609 | context->mon); | |
bb5fc20f AL |
610 | } |
611 | } | |
612 | ||
d54908a5 | 613 | static void do_gdbserver(Monitor *mon, const QDict *qdict) |
59030a8c | 614 | { |
d54908a5 | 615 | const char *device = qdict_get_try_str(qdict, "device"); |
59030a8c AL |
616 | if (!device) |
617 | device = "tcp::" DEFAULT_GDBSTUB_PORT; | |
618 | if (gdbserver_start(device) < 0) { | |
619 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", | |
620 | device); | |
621 | } else if (strcmp(device, "none") == 0) { | |
36556b20 | 622 | monitor_printf(mon, "Disabled gdbserver\n"); |
8a7ddc38 | 623 | } else { |
59030a8c AL |
624 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
625 | device); | |
8a7ddc38 FB |
626 | } |
627 | } | |
628 | ||
d54908a5 | 629 | static void do_watchdog_action(Monitor *mon, const QDict *qdict) |
9dd986cc | 630 | { |
d54908a5 | 631 | const char *action = qdict_get_str(qdict, "action"); |
9dd986cc RJ |
632 | if (select_watchdog_action(action) == -1) { |
633 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); | |
634 | } | |
635 | } | |
636 | ||
376253ec | 637 | static void monitor_printc(Monitor *mon, int c) |
9307c4c1 | 638 | { |
376253ec | 639 | monitor_printf(mon, "'"); |
9307c4c1 FB |
640 | switch(c) { |
641 | case '\'': | |
376253ec | 642 | monitor_printf(mon, "\\'"); |
9307c4c1 FB |
643 | break; |
644 | case '\\': | |
376253ec | 645 | monitor_printf(mon, "\\\\"); |
9307c4c1 FB |
646 | break; |
647 | case '\n': | |
376253ec | 648 | monitor_printf(mon, "\\n"); |
9307c4c1 FB |
649 | break; |
650 | case '\r': | |
376253ec | 651 | monitor_printf(mon, "\\r"); |
9307c4c1 FB |
652 | break; |
653 | default: | |
654 | if (c >= 32 && c <= 126) { | |
376253ec | 655 | monitor_printf(mon, "%c", c); |
9307c4c1 | 656 | } else { |
376253ec | 657 | monitor_printf(mon, "\\x%02x", c); |
9307c4c1 FB |
658 | } |
659 | break; | |
660 | } | |
376253ec | 661 | monitor_printf(mon, "'"); |
9307c4c1 FB |
662 | } |
663 | ||
376253ec | 664 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
7743e588 | 665 | target_phys_addr_t addr, int is_physical) |
9307c4c1 | 666 | { |
6a00d601 | 667 | CPUState *env; |
9307c4c1 FB |
668 | int nb_per_line, l, line_size, i, max_digits, len; |
669 | uint8_t buf[16]; | |
670 | uint64_t v; | |
671 | ||
672 | if (format == 'i') { | |
673 | int flags; | |
674 | flags = 0; | |
6a00d601 FB |
675 | env = mon_get_cpu(); |
676 | if (!env && !is_physical) | |
677 | return; | |
9307c4c1 | 678 | #ifdef TARGET_I386 |
4c27ba27 | 679 | if (wsize == 2) { |
9307c4c1 | 680 | flags = 1; |
4c27ba27 FB |
681 | } else if (wsize == 4) { |
682 | flags = 0; | |
683 | } else { | |
6a15fd12 | 684 | /* as default we use the current CS size */ |
4c27ba27 | 685 | flags = 0; |
6a15fd12 FB |
686 | if (env) { |
687 | #ifdef TARGET_X86_64 | |
5fafdf24 | 688 | if ((env->efer & MSR_EFER_LMA) && |
6a15fd12 FB |
689 | (env->segs[R_CS].flags & DESC_L_MASK)) |
690 | flags = 2; | |
691 | else | |
692 | #endif | |
693 | if (!(env->segs[R_CS].flags & DESC_B_MASK)) | |
694 | flags = 1; | |
695 | } | |
4c27ba27 FB |
696 | } |
697 | #endif | |
376253ec | 698 | monitor_disas(mon, env, addr, count, is_physical, flags); |
9307c4c1 FB |
699 | return; |
700 | } | |
701 | ||
702 | len = wsize * count; | |
703 | if (wsize == 1) | |
704 | line_size = 8; | |
705 | else | |
706 | line_size = 16; | |
707 | nb_per_line = line_size / wsize; | |
708 | max_digits = 0; | |
709 | ||
710 | switch(format) { | |
711 | case 'o': | |
712 | max_digits = (wsize * 8 + 2) / 3; | |
713 | break; | |
714 | default: | |
715 | case 'x': | |
716 | max_digits = (wsize * 8) / 4; | |
717 | break; | |
718 | case 'u': | |
719 | case 'd': | |
720 | max_digits = (wsize * 8 * 10 + 32) / 33; | |
721 | break; | |
722 | case 'c': | |
723 | wsize = 1; | |
724 | break; | |
725 | } | |
726 | ||
727 | while (len > 0) { | |
7743e588 | 728 | if (is_physical) |
376253ec | 729 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
7743e588 | 730 | else |
376253ec | 731 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
9307c4c1 FB |
732 | l = len; |
733 | if (l > line_size) | |
734 | l = line_size; | |
735 | if (is_physical) { | |
736 | cpu_physical_memory_rw(addr, buf, l, 0); | |
737 | } else { | |
6a00d601 FB |
738 | env = mon_get_cpu(); |
739 | if (!env) | |
740 | break; | |
c8f79b67 | 741 | if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) { |
376253ec | 742 | monitor_printf(mon, " Cannot access memory\n"); |
c8f79b67 AL |
743 | break; |
744 | } | |
9307c4c1 | 745 | } |
5fafdf24 | 746 | i = 0; |
9307c4c1 FB |
747 | while (i < l) { |
748 | switch(wsize) { | |
749 | default: | |
750 | case 1: | |
751 | v = ldub_raw(buf + i); | |
752 | break; | |
753 | case 2: | |
754 | v = lduw_raw(buf + i); | |
755 | break; | |
756 | case 4: | |
92a31b1f | 757 | v = (uint32_t)ldl_raw(buf + i); |
9307c4c1 FB |
758 | break; |
759 | case 8: | |
760 | v = ldq_raw(buf + i); | |
761 | break; | |
762 | } | |
376253ec | 763 | monitor_printf(mon, " "); |
9307c4c1 FB |
764 | switch(format) { |
765 | case 'o': | |
376253ec | 766 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
9307c4c1 FB |
767 | break; |
768 | case 'x': | |
376253ec | 769 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
9307c4c1 FB |
770 | break; |
771 | case 'u': | |
376253ec | 772 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
9307c4c1 FB |
773 | break; |
774 | case 'd': | |
376253ec | 775 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
9307c4c1 FB |
776 | break; |
777 | case 'c': | |
376253ec | 778 | monitor_printc(mon, v); |
9307c4c1 FB |
779 | break; |
780 | } | |
781 | i += wsize; | |
782 | } | |
376253ec | 783 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
784 | addr += l; |
785 | len -= l; | |
786 | } | |
787 | } | |
788 | ||
1bd1442e | 789 | static void do_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 790 | { |
1bd1442e LC |
791 | int count = qdict_get_int(qdict, "count"); |
792 | int format = qdict_get_int(qdict, "format"); | |
793 | int size = qdict_get_int(qdict, "size"); | |
794 | target_long addr = qdict_get_int(qdict, "addr"); | |
795 | ||
376253ec | 796 | memory_dump(mon, count, format, size, addr, 0); |
9307c4c1 FB |
797 | } |
798 | ||
1bd1442e | 799 | static void do_physical_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 800 | { |
1bd1442e LC |
801 | int count = qdict_get_int(qdict, "count"); |
802 | int format = qdict_get_int(qdict, "format"); | |
803 | int size = qdict_get_int(qdict, "size"); | |
804 | target_phys_addr_t addr = qdict_get_int(qdict, "addr"); | |
805 | ||
376253ec | 806 | memory_dump(mon, count, format, size, addr, 1); |
9307c4c1 FB |
807 | } |
808 | ||
1bd1442e | 809 | static void do_print(Monitor *mon, const QDict *qdict) |
9307c4c1 | 810 | { |
1bd1442e LC |
811 | int format = qdict_get_int(qdict, "format"); |
812 | target_phys_addr_t val = qdict_get_int(qdict, "val"); | |
813 | ||
7743e588 | 814 | #if TARGET_PHYS_ADDR_BITS == 32 |
9307c4c1 FB |
815 | switch(format) { |
816 | case 'o': | |
376253ec | 817 | monitor_printf(mon, "%#o", val); |
9307c4c1 FB |
818 | break; |
819 | case 'x': | |
376253ec | 820 | monitor_printf(mon, "%#x", val); |
9307c4c1 FB |
821 | break; |
822 | case 'u': | |
376253ec | 823 | monitor_printf(mon, "%u", val); |
9307c4c1 FB |
824 | break; |
825 | default: | |
826 | case 'd': | |
376253ec | 827 | monitor_printf(mon, "%d", val); |
9307c4c1 FB |
828 | break; |
829 | case 'c': | |
376253ec | 830 | monitor_printc(mon, val); |
9307c4c1 FB |
831 | break; |
832 | } | |
92a31b1f FB |
833 | #else |
834 | switch(format) { | |
835 | case 'o': | |
376253ec | 836 | monitor_printf(mon, "%#" PRIo64, val); |
92a31b1f FB |
837 | break; |
838 | case 'x': | |
376253ec | 839 | monitor_printf(mon, "%#" PRIx64, val); |
92a31b1f FB |
840 | break; |
841 | case 'u': | |
376253ec | 842 | monitor_printf(mon, "%" PRIu64, val); |
92a31b1f FB |
843 | break; |
844 | default: | |
845 | case 'd': | |
376253ec | 846 | monitor_printf(mon, "%" PRId64, val); |
92a31b1f FB |
847 | break; |
848 | case 'c': | |
376253ec | 849 | monitor_printc(mon, val); |
92a31b1f FB |
850 | break; |
851 | } | |
852 | #endif | |
376253ec | 853 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
854 | } |
855 | ||
afe67ef2 | 856 | static void do_memory_save(Monitor *mon, const QDict *qdict) |
b371dc59 FB |
857 | { |
858 | FILE *f; | |
afe67ef2 LC |
859 | uint32_t size = qdict_get_int(qdict, "size"); |
860 | const char *filename = qdict_get_str(qdict, "filename"); | |
861 | target_long addr = qdict_get_int(qdict, "val"); | |
b371dc59 FB |
862 | uint32_t l; |
863 | CPUState *env; | |
864 | uint8_t buf[1024]; | |
865 | ||
866 | env = mon_get_cpu(); | |
867 | if (!env) | |
868 | return; | |
869 | ||
870 | f = fopen(filename, "wb"); | |
871 | if (!f) { | |
376253ec | 872 | monitor_printf(mon, "could not open '%s'\n", filename); |
b371dc59 FB |
873 | return; |
874 | } | |
875 | while (size != 0) { | |
876 | l = sizeof(buf); | |
877 | if (l > size) | |
878 | l = size; | |
879 | cpu_memory_rw_debug(env, addr, buf, l, 0); | |
880 | fwrite(buf, 1, l, f); | |
881 | addr += l; | |
882 | size -= l; | |
883 | } | |
884 | fclose(f); | |
885 | } | |
886 | ||
afe67ef2 | 887 | static void do_physical_memory_save(Monitor *mon, const QDict *qdict) |
a8bdf7a6 AJ |
888 | { |
889 | FILE *f; | |
890 | uint32_t l; | |
891 | uint8_t buf[1024]; | |
afe67ef2 LC |
892 | uint32_t size = qdict_get_int(qdict, "size"); |
893 | const char *filename = qdict_get_str(qdict, "filename"); | |
894 | target_phys_addr_t addr = qdict_get_int(qdict, "val"); | |
a8bdf7a6 AJ |
895 | |
896 | f = fopen(filename, "wb"); | |
897 | if (!f) { | |
376253ec | 898 | monitor_printf(mon, "could not open '%s'\n", filename); |
a8bdf7a6 AJ |
899 | return; |
900 | } | |
901 | while (size != 0) { | |
902 | l = sizeof(buf); | |
903 | if (l > size) | |
904 | l = size; | |
905 | cpu_physical_memory_rw(addr, buf, l, 0); | |
906 | fwrite(buf, 1, l, f); | |
907 | fflush(f); | |
908 | addr += l; | |
909 | size -= l; | |
910 | } | |
911 | fclose(f); | |
912 | } | |
913 | ||
f18c16de | 914 | static void do_sum(Monitor *mon, const QDict *qdict) |
e4cf1adc FB |
915 | { |
916 | uint32_t addr; | |
917 | uint8_t buf[1]; | |
918 | uint16_t sum; | |
f18c16de LC |
919 | uint32_t start = qdict_get_int(qdict, "start"); |
920 | uint32_t size = qdict_get_int(qdict, "size"); | |
e4cf1adc FB |
921 | |
922 | sum = 0; | |
923 | for(addr = start; addr < (start + size); addr++) { | |
924 | cpu_physical_memory_rw(addr, buf, 1, 0); | |
925 | /* BSD sum algorithm ('sum' Unix command) */ | |
926 | sum = (sum >> 1) | (sum << 15); | |
927 | sum += buf[0]; | |
928 | } | |
376253ec | 929 | monitor_printf(mon, "%05d\n", sum); |
e4cf1adc FB |
930 | } |
931 | ||
a3a91a35 FB |
932 | typedef struct { |
933 | int keycode; | |
934 | const char *name; | |
935 | } KeyDef; | |
936 | ||
937 | static const KeyDef key_defs[] = { | |
938 | { 0x2a, "shift" }, | |
939 | { 0x36, "shift_r" }, | |
3b46e624 | 940 | |
a3a91a35 FB |
941 | { 0x38, "alt" }, |
942 | { 0xb8, "alt_r" }, | |
2ba27c7f TS |
943 | { 0x64, "altgr" }, |
944 | { 0xe4, "altgr_r" }, | |
a3a91a35 FB |
945 | { 0x1d, "ctrl" }, |
946 | { 0x9d, "ctrl_r" }, | |
947 | ||
948 | { 0xdd, "menu" }, | |
949 | ||
950 | { 0x01, "esc" }, | |
951 | ||
952 | { 0x02, "1" }, | |
953 | { 0x03, "2" }, | |
954 | { 0x04, "3" }, | |
955 | { 0x05, "4" }, | |
956 | { 0x06, "5" }, | |
957 | { 0x07, "6" }, | |
958 | { 0x08, "7" }, | |
959 | { 0x09, "8" }, | |
960 | { 0x0a, "9" }, | |
961 | { 0x0b, "0" }, | |
64866c3d FB |
962 | { 0x0c, "minus" }, |
963 | { 0x0d, "equal" }, | |
a3a91a35 FB |
964 | { 0x0e, "backspace" }, |
965 | ||
966 | { 0x0f, "tab" }, | |
967 | { 0x10, "q" }, | |
968 | { 0x11, "w" }, | |
969 | { 0x12, "e" }, | |
970 | { 0x13, "r" }, | |
971 | { 0x14, "t" }, | |
972 | { 0x15, "y" }, | |
973 | { 0x16, "u" }, | |
974 | { 0x17, "i" }, | |
975 | { 0x18, "o" }, | |
976 | { 0x19, "p" }, | |
977 | ||
978 | { 0x1c, "ret" }, | |
979 | ||
980 | { 0x1e, "a" }, | |
981 | { 0x1f, "s" }, | |
982 | { 0x20, "d" }, | |
983 | { 0x21, "f" }, | |
984 | { 0x22, "g" }, | |
985 | { 0x23, "h" }, | |
986 | { 0x24, "j" }, | |
987 | { 0x25, "k" }, | |
988 | { 0x26, "l" }, | |
989 | ||
990 | { 0x2c, "z" }, | |
991 | { 0x2d, "x" }, | |
992 | { 0x2e, "c" }, | |
993 | { 0x2f, "v" }, | |
994 | { 0x30, "b" }, | |
995 | { 0x31, "n" }, | |
996 | { 0x32, "m" }, | |
9155fc45 AJ |
997 | { 0x33, "comma" }, |
998 | { 0x34, "dot" }, | |
999 | { 0x35, "slash" }, | |
3b46e624 | 1000 | |
4d3b6f6e AZ |
1001 | { 0x37, "asterisk" }, |
1002 | ||
a3a91a35 | 1003 | { 0x39, "spc" }, |
00ffa62a | 1004 | { 0x3a, "caps_lock" }, |
a3a91a35 FB |
1005 | { 0x3b, "f1" }, |
1006 | { 0x3c, "f2" }, | |
1007 | { 0x3d, "f3" }, | |
1008 | { 0x3e, "f4" }, | |
1009 | { 0x3f, "f5" }, | |
1010 | { 0x40, "f6" }, | |
1011 | { 0x41, "f7" }, | |
1012 | { 0x42, "f8" }, | |
1013 | { 0x43, "f9" }, | |
1014 | { 0x44, "f10" }, | |
00ffa62a | 1015 | { 0x45, "num_lock" }, |
a3a91a35 FB |
1016 | { 0x46, "scroll_lock" }, |
1017 | ||
64866c3d FB |
1018 | { 0xb5, "kp_divide" }, |
1019 | { 0x37, "kp_multiply" }, | |
0cfec834 | 1020 | { 0x4a, "kp_subtract" }, |
64866c3d FB |
1021 | { 0x4e, "kp_add" }, |
1022 | { 0x9c, "kp_enter" }, | |
1023 | { 0x53, "kp_decimal" }, | |
f2289cb6 | 1024 | { 0x54, "sysrq" }, |
64866c3d FB |
1025 | |
1026 | { 0x52, "kp_0" }, | |
1027 | { 0x4f, "kp_1" }, | |
1028 | { 0x50, "kp_2" }, | |
1029 | { 0x51, "kp_3" }, | |
1030 | { 0x4b, "kp_4" }, | |
1031 | { 0x4c, "kp_5" }, | |
1032 | { 0x4d, "kp_6" }, | |
1033 | { 0x47, "kp_7" }, | |
1034 | { 0x48, "kp_8" }, | |
1035 | { 0x49, "kp_9" }, | |
3b46e624 | 1036 | |
a3a91a35 FB |
1037 | { 0x56, "<" }, |
1038 | ||
1039 | { 0x57, "f11" }, | |
1040 | { 0x58, "f12" }, | |
1041 | ||
1042 | { 0xb7, "print" }, | |
1043 | ||
1044 | { 0xc7, "home" }, | |
1045 | { 0xc9, "pgup" }, | |
1046 | { 0xd1, "pgdn" }, | |
1047 | { 0xcf, "end" }, | |
1048 | ||
1049 | { 0xcb, "left" }, | |
1050 | { 0xc8, "up" }, | |
1051 | { 0xd0, "down" }, | |
1052 | { 0xcd, "right" }, | |
1053 | ||
1054 | { 0xd2, "insert" }, | |
1055 | { 0xd3, "delete" }, | |
c0b5b109 BS |
1056 | #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64) |
1057 | { 0xf0, "stop" }, | |
1058 | { 0xf1, "again" }, | |
1059 | { 0xf2, "props" }, | |
1060 | { 0xf3, "undo" }, | |
1061 | { 0xf4, "front" }, | |
1062 | { 0xf5, "copy" }, | |
1063 | { 0xf6, "open" }, | |
1064 | { 0xf7, "paste" }, | |
1065 | { 0xf8, "find" }, | |
1066 | { 0xf9, "cut" }, | |
1067 | { 0xfa, "lf" }, | |
1068 | { 0xfb, "help" }, | |
1069 | { 0xfc, "meta_l" }, | |
1070 | { 0xfd, "meta_r" }, | |
1071 | { 0xfe, "compose" }, | |
1072 | #endif | |
a3a91a35 FB |
1073 | { 0, NULL }, |
1074 | }; | |
1075 | ||
1076 | static int get_keycode(const char *key) | |
1077 | { | |
1078 | const KeyDef *p; | |
64866c3d FB |
1079 | char *endp; |
1080 | int ret; | |
a3a91a35 FB |
1081 | |
1082 | for(p = key_defs; p->name != NULL; p++) { | |
1083 | if (!strcmp(key, p->name)) | |
1084 | return p->keycode; | |
1085 | } | |
64866c3d FB |
1086 | if (strstart(key, "0x", NULL)) { |
1087 | ret = strtoul(key, &endp, 0); | |
1088 | if (*endp == '\0' && ret >= 0x01 && ret <= 0xff) | |
1089 | return ret; | |
1090 | } | |
a3a91a35 FB |
1091 | return -1; |
1092 | } | |
1093 | ||
c8256f9d AZ |
1094 | #define MAX_KEYCODES 16 |
1095 | static uint8_t keycodes[MAX_KEYCODES]; | |
1096 | static int nb_pending_keycodes; | |
1097 | static QEMUTimer *key_timer; | |
1098 | ||
1099 | static void release_keys(void *opaque) | |
1100 | { | |
1101 | int keycode; | |
1102 | ||
1103 | while (nb_pending_keycodes > 0) { | |
1104 | nb_pending_keycodes--; | |
1105 | keycode = keycodes[nb_pending_keycodes]; | |
1106 | if (keycode & 0x80) | |
1107 | kbd_put_keycode(0xe0); | |
1108 | kbd_put_keycode(keycode | 0x80); | |
1109 | } | |
1110 | } | |
1111 | ||
1d4daa91 | 1112 | static void do_sendkey(Monitor *mon, const QDict *qdict) |
a3a91a35 | 1113 | { |
3401c0d9 AZ |
1114 | char keyname_buf[16]; |
1115 | char *separator; | |
1116 | int keyname_len, keycode, i; | |
1d4daa91 LC |
1117 | const char *string = qdict_get_str(qdict, "string"); |
1118 | int has_hold_time = qdict_haskey(qdict, "hold_time"); | |
1119 | int hold_time = qdict_get_try_int(qdict, "hold_time", -1); | |
3401c0d9 | 1120 | |
c8256f9d AZ |
1121 | if (nb_pending_keycodes > 0) { |
1122 | qemu_del_timer(key_timer); | |
1123 | release_keys(NULL); | |
1124 | } | |
1125 | if (!has_hold_time) | |
1126 | hold_time = 100; | |
1127 | i = 0; | |
3401c0d9 AZ |
1128 | while (1) { |
1129 | separator = strchr(string, '-'); | |
1130 | keyname_len = separator ? separator - string : strlen(string); | |
1131 | if (keyname_len > 0) { | |
1132 | pstrcpy(keyname_buf, sizeof(keyname_buf), string); | |
1133 | if (keyname_len > sizeof(keyname_buf) - 1) { | |
376253ec | 1134 | monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf); |
3401c0d9 | 1135 | return; |
a3a91a35 | 1136 | } |
c8256f9d | 1137 | if (i == MAX_KEYCODES) { |
376253ec | 1138 | monitor_printf(mon, "too many keys\n"); |
3401c0d9 AZ |
1139 | return; |
1140 | } | |
1141 | keyname_buf[keyname_len] = 0; | |
1142 | keycode = get_keycode(keyname_buf); | |
1143 | if (keycode < 0) { | |
376253ec | 1144 | monitor_printf(mon, "unknown key: '%s'\n", keyname_buf); |
3401c0d9 AZ |
1145 | return; |
1146 | } | |
c8256f9d | 1147 | keycodes[i++] = keycode; |
a3a91a35 | 1148 | } |
3401c0d9 | 1149 | if (!separator) |
a3a91a35 | 1150 | break; |
3401c0d9 | 1151 | string = separator + 1; |
a3a91a35 | 1152 | } |
c8256f9d | 1153 | nb_pending_keycodes = i; |
a3a91a35 | 1154 | /* key down events */ |
c8256f9d | 1155 | for (i = 0; i < nb_pending_keycodes; i++) { |
a3a91a35 FB |
1156 | keycode = keycodes[i]; |
1157 | if (keycode & 0x80) | |
1158 | kbd_put_keycode(0xe0); | |
1159 | kbd_put_keycode(keycode & 0x7f); | |
1160 | } | |
c8256f9d | 1161 | /* delayed key up events */ |
f227f17d AZ |
1162 | qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) + |
1163 | muldiv64(ticks_per_sec, hold_time, 1000)); | |
a3a91a35 FB |
1164 | } |
1165 | ||
13224a87 FB |
1166 | static int mouse_button_state; |
1167 | ||
1d4daa91 | 1168 | static void do_mouse_move(Monitor *mon, const QDict *qdict) |
13224a87 FB |
1169 | { |
1170 | int dx, dy, dz; | |
1d4daa91 LC |
1171 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
1172 | const char *dy_str = qdict_get_str(qdict, "dy_str"); | |
1173 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); | |
13224a87 FB |
1174 | dx = strtol(dx_str, NULL, 0); |
1175 | dy = strtol(dy_str, NULL, 0); | |
1176 | dz = 0; | |
5fafdf24 | 1177 | if (dz_str) |
13224a87 FB |
1178 | dz = strtol(dz_str, NULL, 0); |
1179 | kbd_mouse_event(dx, dy, dz, mouse_button_state); | |
1180 | } | |
1181 | ||
d54908a5 | 1182 | static void do_mouse_button(Monitor *mon, const QDict *qdict) |
13224a87 | 1183 | { |
d54908a5 | 1184 | int button_state = qdict_get_int(qdict, "button_state"); |
13224a87 FB |
1185 | mouse_button_state = button_state; |
1186 | kbd_mouse_event(0, 0, 0, mouse_button_state); | |
1187 | } | |
1188 | ||
aa93e39c | 1189 | static void do_ioport_read(Monitor *mon, const QDict *qdict) |
3440557b | 1190 | { |
aa93e39c LC |
1191 | int size = qdict_get_int(qdict, "size"); |
1192 | int addr = qdict_get_int(qdict, "addr"); | |
1193 | int has_index = qdict_haskey(qdict, "index"); | |
3440557b FB |
1194 | uint32_t val; |
1195 | int suffix; | |
1196 | ||
1197 | if (has_index) { | |
aa93e39c | 1198 | int index = qdict_get_int(qdict, "index"); |
d56dd6cf | 1199 | cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff); |
3440557b FB |
1200 | addr++; |
1201 | } | |
1202 | addr &= 0xffff; | |
1203 | ||
1204 | switch(size) { | |
1205 | default: | |
1206 | case 1: | |
1207 | val = cpu_inb(NULL, addr); | |
1208 | suffix = 'b'; | |
1209 | break; | |
1210 | case 2: | |
1211 | val = cpu_inw(NULL, addr); | |
1212 | suffix = 'w'; | |
1213 | break; | |
1214 | case 4: | |
1215 | val = cpu_inl(NULL, addr); | |
1216 | suffix = 'l'; | |
1217 | break; | |
1218 | } | |
376253ec AL |
1219 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
1220 | suffix, addr, size * 2, val); | |
3440557b | 1221 | } |
a3a91a35 | 1222 | |
1bd1442e | 1223 | static void do_ioport_write(Monitor *mon, const QDict *qdict) |
f114784f | 1224 | { |
1bd1442e LC |
1225 | int size = qdict_get_int(qdict, "size"); |
1226 | int addr = qdict_get_int(qdict, "addr"); | |
1227 | int val = qdict_get_int(qdict, "val"); | |
1228 | ||
f114784f JK |
1229 | addr &= IOPORTS_MASK; |
1230 | ||
1231 | switch (size) { | |
1232 | default: | |
1233 | case 1: | |
1234 | cpu_outb(NULL, addr, val); | |
1235 | break; | |
1236 | case 2: | |
1237 | cpu_outw(NULL, addr, val); | |
1238 | break; | |
1239 | case 4: | |
1240 | cpu_outl(NULL, addr, val); | |
1241 | break; | |
1242 | } | |
1243 | } | |
1244 | ||
d54908a5 | 1245 | static void do_boot_set(Monitor *mon, const QDict *qdict) |
0ecdffbb AJ |
1246 | { |
1247 | int res; | |
d54908a5 | 1248 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
0ecdffbb | 1249 | |
76e30d0f JK |
1250 | res = qemu_boot_set(bootdevice); |
1251 | if (res == 0) { | |
1252 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); | |
1253 | } else if (res > 0) { | |
1254 | monitor_printf(mon, "setting boot device list failed\n"); | |
0ecdffbb | 1255 | } else { |
376253ec AL |
1256 | monitor_printf(mon, "no function defined to set boot device list for " |
1257 | "this architecture\n"); | |
0ecdffbb AJ |
1258 | } |
1259 | } | |
1260 | ||
f96fc8a0 | 1261 | static void do_system_reset(Monitor *mon, const QDict *qdict) |
e4f9082b FB |
1262 | { |
1263 | qemu_system_reset_request(); | |
1264 | } | |
1265 | ||
f96fc8a0 | 1266 | static void do_system_powerdown(Monitor *mon, const QDict *qdict) |
3475187d FB |
1267 | { |
1268 | qemu_system_powerdown_request(); | |
1269 | } | |
1270 | ||
b86bda5b | 1271 | #if defined(TARGET_I386) |
376253ec | 1272 | static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask) |
b86bda5b | 1273 | { |
376253ec AL |
1274 | monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n", |
1275 | addr, | |
1276 | pte & mask, | |
1277 | pte & PG_GLOBAL_MASK ? 'G' : '-', | |
1278 | pte & PG_PSE_MASK ? 'P' : '-', | |
1279 | pte & PG_DIRTY_MASK ? 'D' : '-', | |
1280 | pte & PG_ACCESSED_MASK ? 'A' : '-', | |
1281 | pte & PG_PCD_MASK ? 'C' : '-', | |
1282 | pte & PG_PWT_MASK ? 'T' : '-', | |
1283 | pte & PG_USER_MASK ? 'U' : '-', | |
1284 | pte & PG_RW_MASK ? 'W' : '-'); | |
b86bda5b FB |
1285 | } |
1286 | ||
376253ec | 1287 | static void tlb_info(Monitor *mon) |
b86bda5b | 1288 | { |
6a00d601 | 1289 | CPUState *env; |
b86bda5b FB |
1290 | int l1, l2; |
1291 | uint32_t pgd, pde, pte; | |
1292 | ||
6a00d601 FB |
1293 | env = mon_get_cpu(); |
1294 | if (!env) | |
1295 | return; | |
1296 | ||
b86bda5b | 1297 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1298 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1299 | return; |
1300 | } | |
1301 | pgd = env->cr[3] & ~0xfff; | |
1302 | for(l1 = 0; l1 < 1024; l1++) { | |
1303 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1304 | pde = le32_to_cpu(pde); | |
1305 | if (pde & PG_PRESENT_MASK) { | |
1306 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
376253ec | 1307 | print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1)); |
b86bda5b FB |
1308 | } else { |
1309 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1310 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1311 | (uint8_t *)&pte, 4); |
1312 | pte = le32_to_cpu(pte); | |
1313 | if (pte & PG_PRESENT_MASK) { | |
376253ec | 1314 | print_pte(mon, (l1 << 22) + (l2 << 12), |
5fafdf24 | 1315 | pte & ~PG_PSE_MASK, |
b86bda5b FB |
1316 | ~0xfff); |
1317 | } | |
1318 | } | |
1319 | } | |
1320 | } | |
1321 | } | |
1322 | } | |
1323 | ||
376253ec | 1324 | static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot, |
b86bda5b FB |
1325 | uint32_t end, int prot) |
1326 | { | |
9746b15b FB |
1327 | int prot1; |
1328 | prot1 = *plast_prot; | |
1329 | if (prot != prot1) { | |
b86bda5b | 1330 | if (*pstart != -1) { |
376253ec AL |
1331 | monitor_printf(mon, "%08x-%08x %08x %c%c%c\n", |
1332 | *pstart, end, end - *pstart, | |
1333 | prot1 & PG_USER_MASK ? 'u' : '-', | |
1334 | 'r', | |
1335 | prot1 & PG_RW_MASK ? 'w' : '-'); | |
b86bda5b FB |
1336 | } |
1337 | if (prot != 0) | |
1338 | *pstart = end; | |
1339 | else | |
1340 | *pstart = -1; | |
1341 | *plast_prot = prot; | |
1342 | } | |
1343 | } | |
1344 | ||
376253ec | 1345 | static void mem_info(Monitor *mon) |
b86bda5b | 1346 | { |
6a00d601 | 1347 | CPUState *env; |
b86bda5b FB |
1348 | int l1, l2, prot, last_prot; |
1349 | uint32_t pgd, pde, pte, start, end; | |
1350 | ||
6a00d601 FB |
1351 | env = mon_get_cpu(); |
1352 | if (!env) | |
1353 | return; | |
1354 | ||
b86bda5b | 1355 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1356 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1357 | return; |
1358 | } | |
1359 | pgd = env->cr[3] & ~0xfff; | |
1360 | last_prot = 0; | |
1361 | start = -1; | |
1362 | for(l1 = 0; l1 < 1024; l1++) { | |
1363 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1364 | pde = le32_to_cpu(pde); | |
1365 | end = l1 << 22; | |
1366 | if (pde & PG_PRESENT_MASK) { | |
1367 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1368 | prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
376253ec | 1369 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1370 | } else { |
1371 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1372 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1373 | (uint8_t *)&pte, 4); |
1374 | pte = le32_to_cpu(pte); | |
1375 | end = (l1 << 22) + (l2 << 12); | |
1376 | if (pte & PG_PRESENT_MASK) { | |
1377 | prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
1378 | } else { | |
1379 | prot = 0; | |
1380 | } | |
376253ec | 1381 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1382 | } |
1383 | } | |
1384 | } else { | |
1385 | prot = 0; | |
376253ec | 1386 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1387 | } |
1388 | } | |
1389 | } | |
1390 | #endif | |
1391 | ||
7c664e2f AJ |
1392 | #if defined(TARGET_SH4) |
1393 | ||
376253ec | 1394 | static void print_tlb(Monitor *mon, int idx, tlb_t *tlb) |
7c664e2f | 1395 | { |
376253ec AL |
1396 | monitor_printf(mon, " tlb%i:\t" |
1397 | "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t" | |
1398 | "v=%hhu shared=%hhu cached=%hhu prot=%hhu " | |
1399 | "dirty=%hhu writethrough=%hhu\n", | |
1400 | idx, | |
1401 | tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size, | |
1402 | tlb->v, tlb->sh, tlb->c, tlb->pr, | |
1403 | tlb->d, tlb->wt); | |
7c664e2f AJ |
1404 | } |
1405 | ||
376253ec | 1406 | static void tlb_info(Monitor *mon) |
7c664e2f AJ |
1407 | { |
1408 | CPUState *env = mon_get_cpu(); | |
1409 | int i; | |
1410 | ||
376253ec | 1411 | monitor_printf (mon, "ITLB:\n"); |
7c664e2f | 1412 | for (i = 0 ; i < ITLB_SIZE ; i++) |
376253ec AL |
1413 | print_tlb (mon, i, &env->itlb[i]); |
1414 | monitor_printf (mon, "UTLB:\n"); | |
7c664e2f | 1415 | for (i = 0 ; i < UTLB_SIZE ; i++) |
376253ec | 1416 | print_tlb (mon, i, &env->utlb[i]); |
7c664e2f AJ |
1417 | } |
1418 | ||
1419 | #endif | |
1420 | ||
376253ec | 1421 | static void do_info_kvm(Monitor *mon) |
7ba1e619 AL |
1422 | { |
1423 | #ifdef CONFIG_KVM | |
376253ec | 1424 | monitor_printf(mon, "kvm support: "); |
7ba1e619 | 1425 | if (kvm_enabled()) |
376253ec | 1426 | monitor_printf(mon, "enabled\n"); |
7ba1e619 | 1427 | else |
376253ec | 1428 | monitor_printf(mon, "disabled\n"); |
7ba1e619 | 1429 | #else |
376253ec | 1430 | monitor_printf(mon, "kvm support: not compiled\n"); |
7ba1e619 AL |
1431 | #endif |
1432 | } | |
1433 | ||
030ea37b AL |
1434 | static void do_info_numa(Monitor *mon) |
1435 | { | |
b28b6230 | 1436 | int i; |
030ea37b AL |
1437 | CPUState *env; |
1438 | ||
1439 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); | |
1440 | for (i = 0; i < nb_numa_nodes; i++) { | |
1441 | monitor_printf(mon, "node %d cpus:", i); | |
1442 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
1443 | if (env->numa_node == i) { | |
1444 | monitor_printf(mon, " %d", env->cpu_index); | |
1445 | } | |
1446 | } | |
1447 | monitor_printf(mon, "\n"); | |
1448 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, | |
1449 | node_mem[i] >> 20); | |
1450 | } | |
1451 | } | |
1452 | ||
5f1ce948 FB |
1453 | #ifdef CONFIG_PROFILER |
1454 | ||
376253ec | 1455 | static void do_info_profile(Monitor *mon) |
5f1ce948 FB |
1456 | { |
1457 | int64_t total; | |
1458 | total = qemu_time; | |
1459 | if (total == 0) | |
1460 | total = 1; | |
376253ec AL |
1461 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
1462 | dev_time, dev_time / (double)ticks_per_sec); | |
1463 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", | |
1464 | qemu_time, qemu_time / (double)ticks_per_sec); | |
5f1ce948 | 1465 | qemu_time = 0; |
5f1ce948 | 1466 | dev_time = 0; |
5f1ce948 FB |
1467 | } |
1468 | #else | |
376253ec | 1469 | static void do_info_profile(Monitor *mon) |
5f1ce948 | 1470 | { |
376253ec | 1471 | monitor_printf(mon, "Internal profiler not compiled\n"); |
5f1ce948 FB |
1472 | } |
1473 | #endif | |
1474 | ||
ec36b695 FB |
1475 | /* Capture support */ |
1476 | static LIST_HEAD (capture_list_head, CaptureState) capture_head; | |
1477 | ||
376253ec | 1478 | static void do_info_capture(Monitor *mon) |
ec36b695 FB |
1479 | { |
1480 | int i; | |
1481 | CaptureState *s; | |
1482 | ||
1483 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
376253ec | 1484 | monitor_printf(mon, "[%d]: ", i); |
ec36b695 FB |
1485 | s->ops.info (s->opaque); |
1486 | } | |
1487 | } | |
1488 | ||
2313086a | 1489 | #ifdef HAS_AUDIO |
d54908a5 | 1490 | static void do_stop_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
1491 | { |
1492 | int i; | |
d54908a5 | 1493 | int n = qdict_get_int(qdict, "n"); |
ec36b695 FB |
1494 | CaptureState *s; |
1495 | ||
1496 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
1497 | if (i == n) { | |
1498 | s->ops.destroy (s->opaque); | |
1499 | LIST_REMOVE (s, entries); | |
1500 | qemu_free (s); | |
1501 | return; | |
1502 | } | |
1503 | } | |
1504 | } | |
1505 | ||
c1925484 LC |
1506 | static void do_wav_capture(Monitor *mon, const QDict *qdict) |
1507 | { | |
1508 | const char *path = qdict_get_str(qdict, "path"); | |
1509 | int has_freq = qdict_haskey(qdict, "freq"); | |
1510 | int freq = qdict_get_try_int(qdict, "freq", -1); | |
1511 | int has_bits = qdict_haskey(qdict, "bits"); | |
1512 | int bits = qdict_get_try_int(qdict, "bits", -1); | |
1513 | int has_channels = qdict_haskey(qdict, "nchannels"); | |
1514 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); | |
ec36b695 FB |
1515 | CaptureState *s; |
1516 | ||
1517 | s = qemu_mallocz (sizeof (*s)); | |
ec36b695 FB |
1518 | |
1519 | freq = has_freq ? freq : 44100; | |
1520 | bits = has_bits ? bits : 16; | |
1521 | nchannels = has_channels ? nchannels : 2; | |
1522 | ||
1523 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
376253ec | 1524 | monitor_printf(mon, "Faied to add wave capture\n"); |
ec36b695 FB |
1525 | qemu_free (s); |
1526 | } | |
1527 | LIST_INSERT_HEAD (&capture_head, s, entries); | |
1528 | } | |
1529 | #endif | |
1530 | ||
dc1c0b74 | 1531 | #if defined(TARGET_I386) |
d54908a5 | 1532 | static void do_inject_nmi(Monitor *mon, const QDict *qdict) |
dc1c0b74 AJ |
1533 | { |
1534 | CPUState *env; | |
d54908a5 | 1535 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
dc1c0b74 AJ |
1536 | |
1537 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
1538 | if (env->cpu_index == cpu_index) { | |
1539 | cpu_interrupt(env, CPU_INTERRUPT_NMI); | |
1540 | break; | |
1541 | } | |
1542 | } | |
1543 | #endif | |
1544 | ||
376253ec | 1545 | static void do_info_status(Monitor *mon) |
6f9c5ee7 | 1546 | { |
1b530a6d AJ |
1547 | if (vm_running) { |
1548 | if (singlestep) { | |
1549 | monitor_printf(mon, "VM status: running (single step mode)\n"); | |
1550 | } else { | |
1551 | monitor_printf(mon, "VM status: running\n"); | |
1552 | } | |
1553 | } else | |
376253ec | 1554 | monitor_printf(mon, "VM status: paused\n"); |
6f9c5ee7 AJ |
1555 | } |
1556 | ||
1557 | ||
d54908a5 | 1558 | static void do_balloon(Monitor *mon, const QDict *qdict) |
df751fa8 | 1559 | { |
d54908a5 | 1560 | int value = qdict_get_int(qdict, "value"); |
df751fa8 AL |
1561 | ram_addr_t target = value; |
1562 | qemu_balloon(target << 20); | |
1563 | } | |
1564 | ||
376253ec | 1565 | static void do_info_balloon(Monitor *mon) |
df751fa8 AL |
1566 | { |
1567 | ram_addr_t actual; | |
1568 | ||
1569 | actual = qemu_balloon_status(); | |
bd322087 | 1570 | if (kvm_enabled() && !kvm_has_sync_mmu()) |
376253ec AL |
1571 | monitor_printf(mon, "Using KVM without synchronous MMU, " |
1572 | "ballooning disabled\n"); | |
bd322087 | 1573 | else if (actual == 0) |
376253ec | 1574 | monitor_printf(mon, "Ballooning not activated in VM\n"); |
df751fa8 | 1575 | else |
376253ec | 1576 | monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20)); |
df751fa8 AL |
1577 | } |
1578 | ||
15dfcd45 | 1579 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
76655d6d | 1580 | { |
15dfcd45 | 1581 | qemu_acl *acl = qemu_acl_find(name); |
76655d6d | 1582 | |
76655d6d | 1583 | if (!acl) { |
15dfcd45 | 1584 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
76655d6d | 1585 | } |
15dfcd45 JK |
1586 | return acl; |
1587 | } | |
1588 | ||
d54908a5 | 1589 | static void do_acl_show(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1590 | { |
d54908a5 | 1591 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
1592 | qemu_acl *acl = find_acl(mon, aclname); |
1593 | qemu_acl_entry *entry; | |
1594 | int i = 0; | |
76655d6d | 1595 | |
15dfcd45 | 1596 | if (acl) { |
28a76be8 | 1597 | monitor_printf(mon, "policy: %s\n", |
76655d6d | 1598 | acl->defaultDeny ? "deny" : "allow"); |
28a76be8 AL |
1599 | TAILQ_FOREACH(entry, &acl->entries, next) { |
1600 | i++; | |
1601 | monitor_printf(mon, "%d: %s %s\n", i, | |
15dfcd45 | 1602 | entry->deny ? "deny" : "allow", entry->match); |
28a76be8 | 1603 | } |
15dfcd45 JK |
1604 | } |
1605 | } | |
1606 | ||
d54908a5 | 1607 | static void do_acl_reset(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1608 | { |
d54908a5 | 1609 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
1610 | qemu_acl *acl = find_acl(mon, aclname); |
1611 | ||
1612 | if (acl) { | |
28a76be8 AL |
1613 | qemu_acl_reset(acl); |
1614 | monitor_printf(mon, "acl: removed all rules\n"); | |
15dfcd45 JK |
1615 | } |
1616 | } | |
1617 | ||
f18c16de | 1618 | static void do_acl_policy(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1619 | { |
f18c16de LC |
1620 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1621 | const char *policy = qdict_get_str(qdict, "policy"); | |
15dfcd45 | 1622 | qemu_acl *acl = find_acl(mon, aclname); |
28a76be8 | 1623 | |
15dfcd45 JK |
1624 | if (acl) { |
1625 | if (strcmp(policy, "allow") == 0) { | |
28a76be8 AL |
1626 | acl->defaultDeny = 0; |
1627 | monitor_printf(mon, "acl: policy set to 'allow'\n"); | |
15dfcd45 | 1628 | } else if (strcmp(policy, "deny") == 0) { |
28a76be8 AL |
1629 | acl->defaultDeny = 1; |
1630 | monitor_printf(mon, "acl: policy set to 'deny'\n"); | |
1631 | } else { | |
15dfcd45 JK |
1632 | monitor_printf(mon, "acl: unknown policy '%s', " |
1633 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 | 1634 | } |
15dfcd45 JK |
1635 | } |
1636 | } | |
28a76be8 | 1637 | |
1bd1442e | 1638 | static void do_acl_add(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1639 | { |
1bd1442e LC |
1640 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1641 | const char *match = qdict_get_str(qdict, "match"); | |
1642 | const char *policy = qdict_get_str(qdict, "policy"); | |
1643 | int has_index = qdict_haskey(qdict, "index"); | |
1644 | int index = qdict_get_try_int(qdict, "index", -1); | |
15dfcd45 JK |
1645 | qemu_acl *acl = find_acl(mon, aclname); |
1646 | int deny, ret; | |
1647 | ||
1648 | if (acl) { | |
1649 | if (strcmp(policy, "allow") == 0) { | |
1650 | deny = 0; | |
1651 | } else if (strcmp(policy, "deny") == 0) { | |
1652 | deny = 1; | |
1653 | } else { | |
1654 | monitor_printf(mon, "acl: unknown policy '%s', " | |
1655 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 AL |
1656 | return; |
1657 | } | |
28a76be8 AL |
1658 | if (has_index) |
1659 | ret = qemu_acl_insert(acl, deny, match, index); | |
1660 | else | |
1661 | ret = qemu_acl_append(acl, deny, match); | |
1662 | if (ret < 0) | |
1663 | monitor_printf(mon, "acl: unable to add acl entry\n"); | |
1664 | else | |
1665 | monitor_printf(mon, "acl: added rule at position %d\n", ret); | |
15dfcd45 JK |
1666 | } |
1667 | } | |
28a76be8 | 1668 | |
f18c16de | 1669 | static void do_acl_remove(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1670 | { |
f18c16de LC |
1671 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1672 | const char *match = qdict_get_str(qdict, "match"); | |
15dfcd45 JK |
1673 | qemu_acl *acl = find_acl(mon, aclname); |
1674 | int ret; | |
28a76be8 | 1675 | |
15dfcd45 | 1676 | if (acl) { |
28a76be8 AL |
1677 | ret = qemu_acl_remove(acl, match); |
1678 | if (ret < 0) | |
1679 | monitor_printf(mon, "acl: no matching acl entry\n"); | |
1680 | else | |
1681 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); | |
76655d6d AL |
1682 | } |
1683 | } | |
1684 | ||
79c4f6b0 | 1685 | #if defined(TARGET_I386) |
37b7ad48 | 1686 | static void do_inject_mce(Monitor *mon, const QDict *qdict) |
79c4f6b0 HY |
1687 | { |
1688 | CPUState *cenv; | |
37b7ad48 LC |
1689 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
1690 | int bank = qdict_get_int(qdict, "bank"); | |
1691 | uint64_t status = qdict_get_int(qdict, "status"); | |
1692 | uint64_t mcg_status = qdict_get_int(qdict, "mcg_status"); | |
1693 | uint64_t addr = qdict_get_int(qdict, "addr"); | |
1694 | uint64_t misc = qdict_get_int(qdict, "misc"); | |
79c4f6b0 HY |
1695 | |
1696 | for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) | |
1697 | if (cenv->cpu_index == cpu_index && cenv->mcg_cap) { | |
1698 | cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc); | |
1699 | break; | |
1700 | } | |
1701 | } | |
1702 | #endif | |
1703 | ||
d54908a5 | 1704 | static void do_getfd(Monitor *mon, const QDict *qdict) |
f07918fd | 1705 | { |
d54908a5 | 1706 | const char *fdname = qdict_get_str(qdict, "fdname"); |
f07918fd MM |
1707 | mon_fd_t *monfd; |
1708 | int fd; | |
1709 | ||
1710 | fd = qemu_chr_get_msgfd(mon->chr); | |
1711 | if (fd == -1) { | |
1712 | monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n"); | |
1713 | return; | |
1714 | } | |
1715 | ||
1716 | if (qemu_isdigit(fdname[0])) { | |
1717 | monitor_printf(mon, "getfd: monitor names may not begin with a number\n"); | |
1718 | return; | |
1719 | } | |
1720 | ||
1721 | fd = dup(fd); | |
1722 | if (fd == -1) { | |
1723 | monitor_printf(mon, "Failed to dup() file descriptor: %s\n", | |
1724 | strerror(errno)); | |
1725 | return; | |
1726 | } | |
1727 | ||
1728 | LIST_FOREACH(monfd, &mon->fds, next) { | |
1729 | if (strcmp(monfd->name, fdname) != 0) { | |
1730 | continue; | |
1731 | } | |
1732 | ||
1733 | close(monfd->fd); | |
1734 | monfd->fd = fd; | |
1735 | return; | |
1736 | } | |
1737 | ||
1738 | monfd = qemu_mallocz(sizeof(mon_fd_t)); | |
1739 | monfd->name = qemu_strdup(fdname); | |
1740 | monfd->fd = fd; | |
1741 | ||
1742 | LIST_INSERT_HEAD(&mon->fds, monfd, next); | |
1743 | } | |
1744 | ||
d54908a5 | 1745 | static void do_closefd(Monitor *mon, const QDict *qdict) |
f07918fd | 1746 | { |
d54908a5 | 1747 | const char *fdname = qdict_get_str(qdict, "fdname"); |
f07918fd MM |
1748 | mon_fd_t *monfd; |
1749 | ||
1750 | LIST_FOREACH(monfd, &mon->fds, next) { | |
1751 | if (strcmp(monfd->name, fdname) != 0) { | |
1752 | continue; | |
1753 | } | |
1754 | ||
1755 | LIST_REMOVE(monfd, next); | |
1756 | close(monfd->fd); | |
1757 | qemu_free(monfd->name); | |
1758 | qemu_free(monfd); | |
1759 | return; | |
1760 | } | |
1761 | ||
1762 | monitor_printf(mon, "Failed to find file descriptor named %s\n", | |
1763 | fdname); | |
1764 | } | |
1765 | ||
d54908a5 | 1766 | static void do_loadvm(Monitor *mon, const QDict *qdict) |
c8d41b2c JQ |
1767 | { |
1768 | int saved_vm_running = vm_running; | |
d54908a5 | 1769 | const char *name = qdict_get_str(qdict, "name"); |
c8d41b2c JQ |
1770 | |
1771 | vm_stop(0); | |
1772 | ||
05f2401e | 1773 | if (load_vmstate(mon, name) >= 0 && saved_vm_running) |
c8d41b2c JQ |
1774 | vm_start(); |
1775 | } | |
1776 | ||
7768e04c MM |
1777 | int monitor_get_fd(Monitor *mon, const char *fdname) |
1778 | { | |
1779 | mon_fd_t *monfd; | |
1780 | ||
1781 | LIST_FOREACH(monfd, &mon->fds, next) { | |
1782 | int fd; | |
1783 | ||
1784 | if (strcmp(monfd->name, fdname) != 0) { | |
1785 | continue; | |
1786 | } | |
1787 | ||
1788 | fd = monfd->fd; | |
1789 | ||
1790 | /* caller takes ownership of fd */ | |
1791 | LIST_REMOVE(monfd, next); | |
1792 | qemu_free(monfd->name); | |
1793 | qemu_free(monfd); | |
1794 | ||
1795 | return fd; | |
1796 | } | |
1797 | ||
1798 | return -1; | |
1799 | } | |
1800 | ||
376253ec | 1801 | static const mon_cmd_t mon_cmds[] = { |
2313086a | 1802 | #include "qemu-monitor.h" |
5fafdf24 | 1803 | { NULL, NULL, }, |
9dc39cba FB |
1804 | }; |
1805 | ||
2313086a | 1806 | /* Please update qemu-monitor.hx when adding or changing commands */ |
376253ec | 1807 | static const mon_cmd_t info_cmds[] = { |
9bc9d1c7 | 1808 | { "version", "", do_info_version, |
d2c639d6 | 1809 | "", "show the version of QEMU" }, |
9307c4c1 | 1810 | { "network", "", do_info_network, |
9dc39cba | 1811 | "", "show the network state" }, |
5ccfae10 AL |
1812 | { "chardev", "", qemu_chr_info, |
1813 | "", "show the character devices" }, | |
376253ec | 1814 | { "block", "", bdrv_info, |
9dc39cba | 1815 | "", "show the block devices" }, |
376253ec | 1816 | { "blockstats", "", bdrv_info_stats, |
a36e69dd | 1817 | "", "show block device statistics" }, |
9307c4c1 FB |
1818 | { "registers", "", do_info_registers, |
1819 | "", "show the cpu registers" }, | |
6a00d601 FB |
1820 | { "cpus", "", do_info_cpus, |
1821 | "", "show infos for each CPU" }, | |
aa455485 FB |
1822 | { "history", "", do_info_history, |
1823 | "", "show the command line history", }, | |
4a0fb71e FB |
1824 | { "irq", "", irq_info, |
1825 | "", "show the interrupts statistics (if available)", }, | |
4c27ba27 FB |
1826 | { "pic", "", pic_info, |
1827 | "", "show i8259 (PIC) state", }, | |
86e0c048 FB |
1828 | { "pci", "", pci_info, |
1829 | "", "show PCI info", }, | |
7c664e2f | 1830 | #if defined(TARGET_I386) || defined(TARGET_SH4) |
b86bda5b FB |
1831 | { "tlb", "", tlb_info, |
1832 | "", "show virtual to physical memory mappings", }, | |
7c664e2f AJ |
1833 | #endif |
1834 | #if defined(TARGET_I386) | |
b86bda5b FB |
1835 | { "mem", "", mem_info, |
1836 | "", "show the active virtual memory mappings", }, | |
16b29ae1 AL |
1837 | { "hpet", "", do_info_hpet, |
1838 | "", "show state of HPET", }, | |
b86bda5b | 1839 | #endif |
e3db7226 FB |
1840 | { "jit", "", do_info_jit, |
1841 | "", "show dynamic compiler info", }, | |
7ba1e619 | 1842 | { "kvm", "", do_info_kvm, |
d2c639d6 | 1843 | "", "show KVM information", }, |
030ea37b AL |
1844 | { "numa", "", do_info_numa, |
1845 | "", "show NUMA information", }, | |
a594cfbf FB |
1846 | { "usb", "", usb_info, |
1847 | "", "show guest USB devices", }, | |
1848 | { "usbhost", "", usb_host_info, | |
1849 | "", "show host USB devices", }, | |
5f1ce948 FB |
1850 | { "profile", "", do_info_profile, |
1851 | "", "show profiling information", }, | |
ec36b695 | 1852 | { "capture", "", do_info_capture, |
17100159 | 1853 | "", "show capture information" }, |
faea38e7 | 1854 | { "snapshots", "", do_info_snapshots, |
17100159 | 1855 | "", "show the currently saved VM snapshots" }, |
6f9c5ee7 AJ |
1856 | { "status", "", do_info_status, |
1857 | "", "show the current VM status (running|paused)" }, | |
201a51fc AZ |
1858 | { "pcmcia", "", pcmcia_info, |
1859 | "", "show guest PCMCIA status" }, | |
455204eb TS |
1860 | { "mice", "", do_info_mice, |
1861 | "", "show which guest mouse is receiving events" }, | |
a9ce8590 FB |
1862 | { "vnc", "", do_info_vnc, |
1863 | "", "show the vnc server status"}, | |
c35734b2 TS |
1864 | { "name", "", do_info_name, |
1865 | "", "show the current VM name" }, | |
f1f23ad5 BS |
1866 | { "uuid", "", do_info_uuid, |
1867 | "", "show the current VM UUID" }, | |
76a66253 JM |
1868 | #if defined(TARGET_PPC) |
1869 | { "cpustats", "", do_info_cpu_stats, | |
1870 | "", "show CPU statistics", }, | |
1871 | #endif | |
31a60e22 | 1872 | #if defined(CONFIG_SLIRP) |
6dbe553f JK |
1873 | { "usernet", "", do_info_usernet, |
1874 | "", "show user network stack connection states", }, | |
31a60e22 | 1875 | #endif |
5bb7910a | 1876 | { "migrate", "", do_info_migrate, "", "show migration status" }, |
df751fa8 AL |
1877 | { "balloon", "", do_info_balloon, |
1878 | "", "show balloon information" }, | |
cae4956e GH |
1879 | { "qtree", "", do_info_qtree, |
1880 | "", "show device tree" }, | |
f6c64e0e GH |
1881 | { "qdm", "", do_info_qdm, |
1882 | "", "show qdev device model list" }, | |
9dc39cba FB |
1883 | { NULL, NULL, }, |
1884 | }; | |
1885 | ||
9307c4c1 FB |
1886 | /*******************************************************************/ |
1887 | ||
1888 | static const char *pch; | |
1889 | static jmp_buf expr_env; | |
1890 | ||
92a31b1f FB |
1891 | #define MD_TLONG 0 |
1892 | #define MD_I32 1 | |
1893 | ||
9307c4c1 FB |
1894 | typedef struct MonitorDef { |
1895 | const char *name; | |
1896 | int offset; | |
8662d656 | 1897 | target_long (*get_value)(const struct MonitorDef *md, int val); |
92a31b1f | 1898 | int type; |
9307c4c1 FB |
1899 | } MonitorDef; |
1900 | ||
57206fd4 | 1901 | #if defined(TARGET_I386) |
8662d656 | 1902 | static target_long monitor_get_pc (const struct MonitorDef *md, int val) |
57206fd4 | 1903 | { |
6a00d601 FB |
1904 | CPUState *env = mon_get_cpu(); |
1905 | if (!env) | |
1906 | return 0; | |
1907 | return env->eip + env->segs[R_CS].base; | |
57206fd4 FB |
1908 | } |
1909 | #endif | |
1910 | ||
a541f297 | 1911 | #if defined(TARGET_PPC) |
8662d656 | 1912 | static target_long monitor_get_ccr (const struct MonitorDef *md, int val) |
a541f297 | 1913 | { |
6a00d601 | 1914 | CPUState *env = mon_get_cpu(); |
a541f297 FB |
1915 | unsigned int u; |
1916 | int i; | |
1917 | ||
6a00d601 FB |
1918 | if (!env) |
1919 | return 0; | |
1920 | ||
a541f297 FB |
1921 | u = 0; |
1922 | for (i = 0; i < 8; i++) | |
28a76be8 | 1923 | u |= env->crf[i] << (32 - (4 * i)); |
a541f297 FB |
1924 | |
1925 | return u; | |
1926 | } | |
1927 | ||
8662d656 | 1928 | static target_long monitor_get_msr (const struct MonitorDef *md, int val) |
a541f297 | 1929 | { |
6a00d601 FB |
1930 | CPUState *env = mon_get_cpu(); |
1931 | if (!env) | |
1932 | return 0; | |
0411a972 | 1933 | return env->msr; |
a541f297 FB |
1934 | } |
1935 | ||
8662d656 | 1936 | static target_long monitor_get_xer (const struct MonitorDef *md, int val) |
a541f297 | 1937 | { |
6a00d601 FB |
1938 | CPUState *env = mon_get_cpu(); |
1939 | if (!env) | |
1940 | return 0; | |
3d7b417e | 1941 | return env->xer; |
a541f297 | 1942 | } |
9fddaa0c | 1943 | |
8662d656 | 1944 | static target_long monitor_get_decr (const struct MonitorDef *md, int val) |
9fddaa0c | 1945 | { |
6a00d601 FB |
1946 | CPUState *env = mon_get_cpu(); |
1947 | if (!env) | |
1948 | return 0; | |
1949 | return cpu_ppc_load_decr(env); | |
9fddaa0c FB |
1950 | } |
1951 | ||
8662d656 | 1952 | static target_long monitor_get_tbu (const struct MonitorDef *md, int val) |
9fddaa0c | 1953 | { |
6a00d601 FB |
1954 | CPUState *env = mon_get_cpu(); |
1955 | if (!env) | |
1956 | return 0; | |
1957 | return cpu_ppc_load_tbu(env); | |
9fddaa0c FB |
1958 | } |
1959 | ||
8662d656 | 1960 | static target_long monitor_get_tbl (const struct MonitorDef *md, int val) |
9fddaa0c | 1961 | { |
6a00d601 FB |
1962 | CPUState *env = mon_get_cpu(); |
1963 | if (!env) | |
1964 | return 0; | |
1965 | return cpu_ppc_load_tbl(env); | |
9fddaa0c | 1966 | } |
a541f297 FB |
1967 | #endif |
1968 | ||
e95c8d51 | 1969 | #if defined(TARGET_SPARC) |
7b936c0c | 1970 | #ifndef TARGET_SPARC64 |
8662d656 | 1971 | static target_long monitor_get_psr (const struct MonitorDef *md, int val) |
e95c8d51 | 1972 | { |
6a00d601 FB |
1973 | CPUState *env = mon_get_cpu(); |
1974 | if (!env) | |
1975 | return 0; | |
1976 | return GET_PSR(env); | |
e95c8d51 | 1977 | } |
7b936c0c | 1978 | #endif |
e95c8d51 | 1979 | |
8662d656 | 1980 | static target_long monitor_get_reg(const struct MonitorDef *md, int val) |
e95c8d51 | 1981 | { |
6a00d601 FB |
1982 | CPUState *env = mon_get_cpu(); |
1983 | if (!env) | |
1984 | return 0; | |
1985 | return env->regwptr[val]; | |
e95c8d51 FB |
1986 | } |
1987 | #endif | |
1988 | ||
8662d656 | 1989 | static const MonitorDef monitor_defs[] = { |
9307c4c1 | 1990 | #ifdef TARGET_I386 |
57206fd4 FB |
1991 | |
1992 | #define SEG(name, seg) \ | |
92a31b1f | 1993 | { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\ |
57206fd4 | 1994 | { name ".base", offsetof(CPUState, segs[seg].base) },\ |
92a31b1f | 1995 | { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 }, |
57206fd4 | 1996 | |
9307c4c1 FB |
1997 | { "eax", offsetof(CPUState, regs[0]) }, |
1998 | { "ecx", offsetof(CPUState, regs[1]) }, | |
1999 | { "edx", offsetof(CPUState, regs[2]) }, | |
2000 | { "ebx", offsetof(CPUState, regs[3]) }, | |
2001 | { "esp|sp", offsetof(CPUState, regs[4]) }, | |
2002 | { "ebp|fp", offsetof(CPUState, regs[5]) }, | |
2003 | { "esi", offsetof(CPUState, regs[6]) }, | |
01038d2a | 2004 | { "edi", offsetof(CPUState, regs[7]) }, |
92a31b1f FB |
2005 | #ifdef TARGET_X86_64 |
2006 | { "r8", offsetof(CPUState, regs[8]) }, | |
2007 | { "r9", offsetof(CPUState, regs[9]) }, | |
2008 | { "r10", offsetof(CPUState, regs[10]) }, | |
2009 | { "r11", offsetof(CPUState, regs[11]) }, | |
2010 | { "r12", offsetof(CPUState, regs[12]) }, | |
2011 | { "r13", offsetof(CPUState, regs[13]) }, | |
2012 | { "r14", offsetof(CPUState, regs[14]) }, | |
2013 | { "r15", offsetof(CPUState, regs[15]) }, | |
2014 | #endif | |
9307c4c1 | 2015 | { "eflags", offsetof(CPUState, eflags) }, |
57206fd4 FB |
2016 | { "eip", offsetof(CPUState, eip) }, |
2017 | SEG("cs", R_CS) | |
2018 | SEG("ds", R_DS) | |
2019 | SEG("es", R_ES) | |
01038d2a | 2020 | SEG("ss", R_SS) |
57206fd4 FB |
2021 | SEG("fs", R_FS) |
2022 | SEG("gs", R_GS) | |
2023 | { "pc", 0, monitor_get_pc, }, | |
a541f297 | 2024 | #elif defined(TARGET_PPC) |
ff937dba | 2025 | /* General purpose registers */ |
a541f297 FB |
2026 | { "r0", offsetof(CPUState, gpr[0]) }, |
2027 | { "r1", offsetof(CPUState, gpr[1]) }, | |
2028 | { "r2", offsetof(CPUState, gpr[2]) }, | |
2029 | { "r3", offsetof(CPUState, gpr[3]) }, | |
2030 | { "r4", offsetof(CPUState, gpr[4]) }, | |
2031 | { "r5", offsetof(CPUState, gpr[5]) }, | |
2032 | { "r6", offsetof(CPUState, gpr[6]) }, | |
2033 | { "r7", offsetof(CPUState, gpr[7]) }, | |
2034 | { "r8", offsetof(CPUState, gpr[8]) }, | |
2035 | { "r9", offsetof(CPUState, gpr[9]) }, | |
2036 | { "r10", offsetof(CPUState, gpr[10]) }, | |
2037 | { "r11", offsetof(CPUState, gpr[11]) }, | |
2038 | { "r12", offsetof(CPUState, gpr[12]) }, | |
2039 | { "r13", offsetof(CPUState, gpr[13]) }, | |
2040 | { "r14", offsetof(CPUState, gpr[14]) }, | |
2041 | { "r15", offsetof(CPUState, gpr[15]) }, | |
2042 | { "r16", offsetof(CPUState, gpr[16]) }, | |
2043 | { "r17", offsetof(CPUState, gpr[17]) }, | |
2044 | { "r18", offsetof(CPUState, gpr[18]) }, | |
2045 | { "r19", offsetof(CPUState, gpr[19]) }, | |
2046 | { "r20", offsetof(CPUState, gpr[20]) }, | |
2047 | { "r21", offsetof(CPUState, gpr[21]) }, | |
2048 | { "r22", offsetof(CPUState, gpr[22]) }, | |
2049 | { "r23", offsetof(CPUState, gpr[23]) }, | |
2050 | { "r24", offsetof(CPUState, gpr[24]) }, | |
2051 | { "r25", offsetof(CPUState, gpr[25]) }, | |
2052 | { "r26", offsetof(CPUState, gpr[26]) }, | |
2053 | { "r27", offsetof(CPUState, gpr[27]) }, | |
2054 | { "r28", offsetof(CPUState, gpr[28]) }, | |
2055 | { "r29", offsetof(CPUState, gpr[29]) }, | |
2056 | { "r30", offsetof(CPUState, gpr[30]) }, | |
2057 | { "r31", offsetof(CPUState, gpr[31]) }, | |
ff937dba JM |
2058 | /* Floating point registers */ |
2059 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2060 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2061 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2062 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2063 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2064 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2065 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2066 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2067 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2068 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2069 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2070 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2071 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2072 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2073 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2074 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2075 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2076 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2077 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2078 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2079 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2080 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2081 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2082 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2083 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2084 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2085 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2086 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2087 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2088 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2089 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2090 | { "f31", offsetof(CPUState, fpr[31]) }, | |
2091 | { "fpscr", offsetof(CPUState, fpscr) }, | |
2092 | /* Next instruction pointer */ | |
57206fd4 | 2093 | { "nip|pc", offsetof(CPUState, nip) }, |
a541f297 FB |
2094 | { "lr", offsetof(CPUState, lr) }, |
2095 | { "ctr", offsetof(CPUState, ctr) }, | |
9fddaa0c | 2096 | { "decr", 0, &monitor_get_decr, }, |
a541f297 | 2097 | { "ccr", 0, &monitor_get_ccr, }, |
ff937dba | 2098 | /* Machine state register */ |
a541f297 FB |
2099 | { "msr", 0, &monitor_get_msr, }, |
2100 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
2101 | { "tbu", 0, &monitor_get_tbu, }, |
2102 | { "tbl", 0, &monitor_get_tbl, }, | |
ff937dba JM |
2103 | #if defined(TARGET_PPC64) |
2104 | /* Address space register */ | |
2105 | { "asr", offsetof(CPUState, asr) }, | |
2106 | #endif | |
2107 | /* Segment registers */ | |
a541f297 FB |
2108 | { "sdr1", offsetof(CPUState, sdr1) }, |
2109 | { "sr0", offsetof(CPUState, sr[0]) }, | |
2110 | { "sr1", offsetof(CPUState, sr[1]) }, | |
2111 | { "sr2", offsetof(CPUState, sr[2]) }, | |
2112 | { "sr3", offsetof(CPUState, sr[3]) }, | |
2113 | { "sr4", offsetof(CPUState, sr[4]) }, | |
2114 | { "sr5", offsetof(CPUState, sr[5]) }, | |
2115 | { "sr6", offsetof(CPUState, sr[6]) }, | |
2116 | { "sr7", offsetof(CPUState, sr[7]) }, | |
2117 | { "sr8", offsetof(CPUState, sr[8]) }, | |
2118 | { "sr9", offsetof(CPUState, sr[9]) }, | |
2119 | { "sr10", offsetof(CPUState, sr[10]) }, | |
2120 | { "sr11", offsetof(CPUState, sr[11]) }, | |
2121 | { "sr12", offsetof(CPUState, sr[12]) }, | |
2122 | { "sr13", offsetof(CPUState, sr[13]) }, | |
2123 | { "sr14", offsetof(CPUState, sr[14]) }, | |
2124 | { "sr15", offsetof(CPUState, sr[15]) }, | |
2125 | /* Too lazy to put BATs and SPRs ... */ | |
e95c8d51 FB |
2126 | #elif defined(TARGET_SPARC) |
2127 | { "g0", offsetof(CPUState, gregs[0]) }, | |
2128 | { "g1", offsetof(CPUState, gregs[1]) }, | |
2129 | { "g2", offsetof(CPUState, gregs[2]) }, | |
2130 | { "g3", offsetof(CPUState, gregs[3]) }, | |
2131 | { "g4", offsetof(CPUState, gregs[4]) }, | |
2132 | { "g5", offsetof(CPUState, gregs[5]) }, | |
2133 | { "g6", offsetof(CPUState, gregs[6]) }, | |
2134 | { "g7", offsetof(CPUState, gregs[7]) }, | |
2135 | { "o0", 0, monitor_get_reg }, | |
2136 | { "o1", 1, monitor_get_reg }, | |
2137 | { "o2", 2, monitor_get_reg }, | |
2138 | { "o3", 3, monitor_get_reg }, | |
2139 | { "o4", 4, monitor_get_reg }, | |
2140 | { "o5", 5, monitor_get_reg }, | |
2141 | { "o6", 6, monitor_get_reg }, | |
2142 | { "o7", 7, monitor_get_reg }, | |
2143 | { "l0", 8, monitor_get_reg }, | |
2144 | { "l1", 9, monitor_get_reg }, | |
2145 | { "l2", 10, monitor_get_reg }, | |
2146 | { "l3", 11, monitor_get_reg }, | |
2147 | { "l4", 12, monitor_get_reg }, | |
2148 | { "l5", 13, monitor_get_reg }, | |
2149 | { "l6", 14, monitor_get_reg }, | |
2150 | { "l7", 15, monitor_get_reg }, | |
2151 | { "i0", 16, monitor_get_reg }, | |
2152 | { "i1", 17, monitor_get_reg }, | |
2153 | { "i2", 18, monitor_get_reg }, | |
2154 | { "i3", 19, monitor_get_reg }, | |
2155 | { "i4", 20, monitor_get_reg }, | |
2156 | { "i5", 21, monitor_get_reg }, | |
2157 | { "i6", 22, monitor_get_reg }, | |
2158 | { "i7", 23, monitor_get_reg }, | |
2159 | { "pc", offsetof(CPUState, pc) }, | |
2160 | { "npc", offsetof(CPUState, npc) }, | |
2161 | { "y", offsetof(CPUState, y) }, | |
7b936c0c | 2162 | #ifndef TARGET_SPARC64 |
e95c8d51 FB |
2163 | { "psr", 0, &monitor_get_psr, }, |
2164 | { "wim", offsetof(CPUState, wim) }, | |
7b936c0c | 2165 | #endif |
e95c8d51 FB |
2166 | { "tbr", offsetof(CPUState, tbr) }, |
2167 | { "fsr", offsetof(CPUState, fsr) }, | |
2168 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2169 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2170 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2171 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2172 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2173 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2174 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2175 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2176 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2177 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2178 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2179 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2180 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2181 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2182 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2183 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2184 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2185 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2186 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2187 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2188 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2189 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2190 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2191 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2192 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2193 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2194 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2195 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2196 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2197 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2198 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2199 | { "f31", offsetof(CPUState, fpr[31]) }, | |
7b936c0c FB |
2200 | #ifdef TARGET_SPARC64 |
2201 | { "f32", offsetof(CPUState, fpr[32]) }, | |
2202 | { "f34", offsetof(CPUState, fpr[34]) }, | |
2203 | { "f36", offsetof(CPUState, fpr[36]) }, | |
2204 | { "f38", offsetof(CPUState, fpr[38]) }, | |
2205 | { "f40", offsetof(CPUState, fpr[40]) }, | |
2206 | { "f42", offsetof(CPUState, fpr[42]) }, | |
2207 | { "f44", offsetof(CPUState, fpr[44]) }, | |
2208 | { "f46", offsetof(CPUState, fpr[46]) }, | |
2209 | { "f48", offsetof(CPUState, fpr[48]) }, | |
2210 | { "f50", offsetof(CPUState, fpr[50]) }, | |
2211 | { "f52", offsetof(CPUState, fpr[52]) }, | |
2212 | { "f54", offsetof(CPUState, fpr[54]) }, | |
2213 | { "f56", offsetof(CPUState, fpr[56]) }, | |
2214 | { "f58", offsetof(CPUState, fpr[58]) }, | |
2215 | { "f60", offsetof(CPUState, fpr[60]) }, | |
2216 | { "f62", offsetof(CPUState, fpr[62]) }, | |
2217 | { "asi", offsetof(CPUState, asi) }, | |
2218 | { "pstate", offsetof(CPUState, pstate) }, | |
2219 | { "cansave", offsetof(CPUState, cansave) }, | |
2220 | { "canrestore", offsetof(CPUState, canrestore) }, | |
2221 | { "otherwin", offsetof(CPUState, otherwin) }, | |
2222 | { "wstate", offsetof(CPUState, wstate) }, | |
2223 | { "cleanwin", offsetof(CPUState, cleanwin) }, | |
2224 | { "fprs", offsetof(CPUState, fprs) }, | |
2225 | #endif | |
9307c4c1 FB |
2226 | #endif |
2227 | { NULL }, | |
2228 | }; | |
2229 | ||
376253ec | 2230 | static void expr_error(Monitor *mon, const char *msg) |
9dc39cba | 2231 | { |
376253ec | 2232 | monitor_printf(mon, "%s\n", msg); |
9307c4c1 FB |
2233 | longjmp(expr_env, 1); |
2234 | } | |
2235 | ||
6a00d601 | 2236 | /* return 0 if OK, -1 if not found, -2 if no CPU defined */ |
92a31b1f | 2237 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 2238 | { |
8662d656 | 2239 | const MonitorDef *md; |
92a31b1f FB |
2240 | void *ptr; |
2241 | ||
9307c4c1 FB |
2242 | for(md = monitor_defs; md->name != NULL; md++) { |
2243 | if (compare_cmd(name, md->name)) { | |
2244 | if (md->get_value) { | |
e95c8d51 | 2245 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 2246 | } else { |
6a00d601 FB |
2247 | CPUState *env = mon_get_cpu(); |
2248 | if (!env) | |
2249 | return -2; | |
2250 | ptr = (uint8_t *)env + md->offset; | |
92a31b1f FB |
2251 | switch(md->type) { |
2252 | case MD_I32: | |
2253 | *pval = *(int32_t *)ptr; | |
2254 | break; | |
2255 | case MD_TLONG: | |
2256 | *pval = *(target_long *)ptr; | |
2257 | break; | |
2258 | default: | |
2259 | *pval = 0; | |
2260 | break; | |
2261 | } | |
9307c4c1 FB |
2262 | } |
2263 | return 0; | |
2264 | } | |
2265 | } | |
2266 | return -1; | |
2267 | } | |
2268 | ||
2269 | static void next(void) | |
2270 | { | |
660f11be | 2271 | if (*pch != '\0') { |
9307c4c1 | 2272 | pch++; |
cd390083 | 2273 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2274 | pch++; |
2275 | } | |
2276 | } | |
2277 | ||
376253ec | 2278 | static int64_t expr_sum(Monitor *mon); |
9307c4c1 | 2279 | |
376253ec | 2280 | static int64_t expr_unary(Monitor *mon) |
9307c4c1 | 2281 | { |
c2efc95d | 2282 | int64_t n; |
9307c4c1 | 2283 | char *p; |
6a00d601 | 2284 | int ret; |
9307c4c1 FB |
2285 | |
2286 | switch(*pch) { | |
2287 | case '+': | |
2288 | next(); | |
376253ec | 2289 | n = expr_unary(mon); |
9307c4c1 FB |
2290 | break; |
2291 | case '-': | |
2292 | next(); | |
376253ec | 2293 | n = -expr_unary(mon); |
9307c4c1 FB |
2294 | break; |
2295 | case '~': | |
2296 | next(); | |
376253ec | 2297 | n = ~expr_unary(mon); |
9307c4c1 FB |
2298 | break; |
2299 | case '(': | |
2300 | next(); | |
376253ec | 2301 | n = expr_sum(mon); |
9307c4c1 | 2302 | if (*pch != ')') { |
376253ec | 2303 | expr_error(mon, "')' expected"); |
9307c4c1 FB |
2304 | } |
2305 | next(); | |
2306 | break; | |
81d0912d FB |
2307 | case '\'': |
2308 | pch++; | |
2309 | if (*pch == '\0') | |
376253ec | 2310 | expr_error(mon, "character constant expected"); |
81d0912d FB |
2311 | n = *pch; |
2312 | pch++; | |
2313 | if (*pch != '\'') | |
376253ec | 2314 | expr_error(mon, "missing terminating \' character"); |
81d0912d FB |
2315 | next(); |
2316 | break; | |
9307c4c1 FB |
2317 | case '$': |
2318 | { | |
2319 | char buf[128], *q; | |
69b34976 | 2320 | target_long reg=0; |
3b46e624 | 2321 | |
9307c4c1 FB |
2322 | pch++; |
2323 | q = buf; | |
2324 | while ((*pch >= 'a' && *pch <= 'z') || | |
2325 | (*pch >= 'A' && *pch <= 'Z') || | |
2326 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 2327 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
2328 | if ((q - buf) < sizeof(buf) - 1) |
2329 | *q++ = *pch; | |
2330 | pch++; | |
2331 | } | |
cd390083 | 2332 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2333 | pch++; |
2334 | *q = 0; | |
7743e588 | 2335 | ret = get_monitor_def(®, buf); |
6a00d601 | 2336 | if (ret == -1) |
376253ec | 2337 | expr_error(mon, "unknown register"); |
5fafdf24 | 2338 | else if (ret == -2) |
376253ec | 2339 | expr_error(mon, "no cpu defined"); |
7743e588 | 2340 | n = reg; |
9307c4c1 FB |
2341 | } |
2342 | break; | |
2343 | case '\0': | |
376253ec | 2344 | expr_error(mon, "unexpected end of expression"); |
9307c4c1 FB |
2345 | n = 0; |
2346 | break; | |
2347 | default: | |
7743e588 | 2348 | #if TARGET_PHYS_ADDR_BITS > 32 |
4f4fbf77 FB |
2349 | n = strtoull(pch, &p, 0); |
2350 | #else | |
9307c4c1 | 2351 | n = strtoul(pch, &p, 0); |
4f4fbf77 | 2352 | #endif |
9307c4c1 | 2353 | if (pch == p) { |
376253ec | 2354 | expr_error(mon, "invalid char in expression"); |
9307c4c1 FB |
2355 | } |
2356 | pch = p; | |
cd390083 | 2357 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2358 | pch++; |
2359 | break; | |
2360 | } | |
2361 | return n; | |
2362 | } | |
2363 | ||
2364 | ||
376253ec | 2365 | static int64_t expr_prod(Monitor *mon) |
9307c4c1 | 2366 | { |
c2efc95d | 2367 | int64_t val, val2; |
92a31b1f | 2368 | int op; |
3b46e624 | 2369 | |
376253ec | 2370 | val = expr_unary(mon); |
9307c4c1 FB |
2371 | for(;;) { |
2372 | op = *pch; | |
2373 | if (op != '*' && op != '/' && op != '%') | |
2374 | break; | |
2375 | next(); | |
376253ec | 2376 | val2 = expr_unary(mon); |
9307c4c1 FB |
2377 | switch(op) { |
2378 | default: | |
2379 | case '*': | |
2380 | val *= val2; | |
2381 | break; | |
2382 | case '/': | |
2383 | case '%': | |
5fafdf24 | 2384 | if (val2 == 0) |
376253ec | 2385 | expr_error(mon, "division by zero"); |
9307c4c1 FB |
2386 | if (op == '/') |
2387 | val /= val2; | |
2388 | else | |
2389 | val %= val2; | |
2390 | break; | |
2391 | } | |
2392 | } | |
2393 | return val; | |
2394 | } | |
2395 | ||
376253ec | 2396 | static int64_t expr_logic(Monitor *mon) |
9307c4c1 | 2397 | { |
c2efc95d | 2398 | int64_t val, val2; |
92a31b1f | 2399 | int op; |
9307c4c1 | 2400 | |
376253ec | 2401 | val = expr_prod(mon); |
9307c4c1 FB |
2402 | for(;;) { |
2403 | op = *pch; | |
2404 | if (op != '&' && op != '|' && op != '^') | |
2405 | break; | |
2406 | next(); | |
376253ec | 2407 | val2 = expr_prod(mon); |
9307c4c1 FB |
2408 | switch(op) { |
2409 | default: | |
2410 | case '&': | |
2411 | val &= val2; | |
2412 | break; | |
2413 | case '|': | |
2414 | val |= val2; | |
2415 | break; | |
2416 | case '^': | |
2417 | val ^= val2; | |
2418 | break; | |
2419 | } | |
2420 | } | |
2421 | return val; | |
2422 | } | |
2423 | ||
376253ec | 2424 | static int64_t expr_sum(Monitor *mon) |
9307c4c1 | 2425 | { |
c2efc95d | 2426 | int64_t val, val2; |
92a31b1f | 2427 | int op; |
9307c4c1 | 2428 | |
376253ec | 2429 | val = expr_logic(mon); |
9307c4c1 FB |
2430 | for(;;) { |
2431 | op = *pch; | |
2432 | if (op != '+' && op != '-') | |
2433 | break; | |
2434 | next(); | |
376253ec | 2435 | val2 = expr_logic(mon); |
9307c4c1 FB |
2436 | if (op == '+') |
2437 | val += val2; | |
2438 | else | |
2439 | val -= val2; | |
2440 | } | |
2441 | return val; | |
2442 | } | |
2443 | ||
376253ec | 2444 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
9307c4c1 FB |
2445 | { |
2446 | pch = *pp; | |
2447 | if (setjmp(expr_env)) { | |
2448 | *pp = pch; | |
2449 | return -1; | |
2450 | } | |
cd390083 | 2451 | while (qemu_isspace(*pch)) |
9307c4c1 | 2452 | pch++; |
376253ec | 2453 | *pval = expr_sum(mon); |
9307c4c1 FB |
2454 | *pp = pch; |
2455 | return 0; | |
2456 | } | |
2457 | ||
2458 | static int get_str(char *buf, int buf_size, const char **pp) | |
2459 | { | |
2460 | const char *p; | |
2461 | char *q; | |
2462 | int c; | |
2463 | ||
81d0912d | 2464 | q = buf; |
9307c4c1 | 2465 | p = *pp; |
cd390083 | 2466 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2467 | p++; |
2468 | if (*p == '\0') { | |
2469 | fail: | |
81d0912d | 2470 | *q = '\0'; |
9307c4c1 FB |
2471 | *pp = p; |
2472 | return -1; | |
2473 | } | |
9307c4c1 FB |
2474 | if (*p == '\"') { |
2475 | p++; | |
2476 | while (*p != '\0' && *p != '\"') { | |
2477 | if (*p == '\\') { | |
2478 | p++; | |
2479 | c = *p++; | |
2480 | switch(c) { | |
2481 | case 'n': | |
2482 | c = '\n'; | |
2483 | break; | |
2484 | case 'r': | |
2485 | c = '\r'; | |
2486 | break; | |
2487 | case '\\': | |
2488 | case '\'': | |
2489 | case '\"': | |
2490 | break; | |
2491 | default: | |
2492 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
2493 | goto fail; | |
2494 | } | |
2495 | if ((q - buf) < buf_size - 1) { | |
2496 | *q++ = c; | |
2497 | } | |
2498 | } else { | |
2499 | if ((q - buf) < buf_size - 1) { | |
2500 | *q++ = *p; | |
2501 | } | |
2502 | p++; | |
2503 | } | |
2504 | } | |
2505 | if (*p != '\"') { | |
5b60212f | 2506 | qemu_printf("unterminated string\n"); |
9307c4c1 FB |
2507 | goto fail; |
2508 | } | |
2509 | p++; | |
2510 | } else { | |
cd390083 | 2511 | while (*p != '\0' && !qemu_isspace(*p)) { |
9307c4c1 FB |
2512 | if ((q - buf) < buf_size - 1) { |
2513 | *q++ = *p; | |
2514 | } | |
2515 | p++; | |
2516 | } | |
9307c4c1 | 2517 | } |
81d0912d | 2518 | *q = '\0'; |
9307c4c1 FB |
2519 | *pp = p; |
2520 | return 0; | |
2521 | } | |
2522 | ||
4590fd80 LC |
2523 | /* |
2524 | * Store the command-name in cmdname, and return a pointer to | |
2525 | * the remaining of the command string. | |
2526 | */ | |
2527 | static const char *get_command_name(const char *cmdline, | |
2528 | char *cmdname, size_t nlen) | |
2529 | { | |
2530 | size_t len; | |
2531 | const char *p, *pstart; | |
2532 | ||
2533 | p = cmdline; | |
2534 | while (qemu_isspace(*p)) | |
2535 | p++; | |
2536 | if (*p == '\0') | |
2537 | return NULL; | |
2538 | pstart = p; | |
2539 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | |
2540 | p++; | |
2541 | len = p - pstart; | |
2542 | if (len > nlen - 1) | |
2543 | len = nlen - 1; | |
2544 | memcpy(cmdname, pstart, len); | |
2545 | cmdname[len] = '\0'; | |
2546 | return p; | |
2547 | } | |
2548 | ||
4d76d2ba LC |
2549 | /** |
2550 | * Read key of 'type' into 'key' and return the current | |
2551 | * 'type' pointer. | |
2552 | */ | |
2553 | static char *key_get_info(const char *type, char **key) | |
2554 | { | |
2555 | size_t len; | |
2556 | char *p, *str; | |
2557 | ||
2558 | if (*type == ',') | |
2559 | type++; | |
2560 | ||
2561 | p = strchr(type, ':'); | |
2562 | if (!p) { | |
2563 | *key = NULL; | |
2564 | return NULL; | |
2565 | } | |
2566 | len = p - type; | |
2567 | ||
2568 | str = qemu_malloc(len + 1); | |
2569 | memcpy(str, type, len); | |
2570 | str[len] = '\0'; | |
2571 | ||
2572 | *key = str; | |
2573 | return ++p; | |
2574 | } | |
2575 | ||
9307c4c1 FB |
2576 | static int default_fmt_format = 'x'; |
2577 | static int default_fmt_size = 4; | |
2578 | ||
2579 | #define MAX_ARGS 16 | |
2580 | ||
55f81d96 LC |
2581 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
2582 | const char *cmdline, | |
55f81d96 | 2583 | QDict *qdict) |
9307c4c1 | 2584 | { |
4590fd80 | 2585 | const char *p, *typestr; |
53773581 | 2586 | int c; |
376253ec | 2587 | const mon_cmd_t *cmd; |
9307c4c1 FB |
2588 | char cmdname[256]; |
2589 | char buf[1024]; | |
4d76d2ba | 2590 | char *key; |
9dc39cba FB |
2591 | |
2592 | #ifdef DEBUG | |
376253ec | 2593 | monitor_printf(mon, "command='%s'\n", cmdline); |
9dc39cba | 2594 | #endif |
3b46e624 | 2595 | |
9307c4c1 | 2596 | /* extract the command name */ |
4590fd80 LC |
2597 | p = get_command_name(cmdline, cmdname, sizeof(cmdname)); |
2598 | if (!p) | |
55f81d96 | 2599 | return NULL; |
3b46e624 | 2600 | |
9307c4c1 | 2601 | /* find the command */ |
376253ec | 2602 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
5fafdf24 | 2603 | if (compare_cmd(cmdname, cmd->name)) |
d91d9bf6 LC |
2604 | break; |
2605 | } | |
2606 | ||
2607 | if (cmd->name == NULL) { | |
2608 | monitor_printf(mon, "unknown command: '%s'\n", cmdname); | |
55f81d96 | 2609 | return NULL; |
9307c4c1 | 2610 | } |
9307c4c1 | 2611 | |
9307c4c1 FB |
2612 | /* parse the parameters */ |
2613 | typestr = cmd->args_type; | |
9dc39cba | 2614 | for(;;) { |
4d76d2ba LC |
2615 | typestr = key_get_info(typestr, &key); |
2616 | if (!typestr) | |
9dc39cba | 2617 | break; |
4d76d2ba | 2618 | c = *typestr; |
9307c4c1 FB |
2619 | typestr++; |
2620 | switch(c) { | |
2621 | case 'F': | |
81d0912d | 2622 | case 'B': |
9307c4c1 FB |
2623 | case 's': |
2624 | { | |
2625 | int ret; | |
3b46e624 | 2626 | |
cd390083 | 2627 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2628 | p++; |
2629 | if (*typestr == '?') { | |
2630 | typestr++; | |
2631 | if (*p == '\0') { | |
2632 | /* no optional string: NULL argument */ | |
53773581 | 2633 | break; |
9307c4c1 FB |
2634 | } |
2635 | } | |
2636 | ret = get_str(buf, sizeof(buf), &p); | |
2637 | if (ret < 0) { | |
81d0912d FB |
2638 | switch(c) { |
2639 | case 'F': | |
376253ec AL |
2640 | monitor_printf(mon, "%s: filename expected\n", |
2641 | cmdname); | |
81d0912d FB |
2642 | break; |
2643 | case 'B': | |
376253ec AL |
2644 | monitor_printf(mon, "%s: block device name expected\n", |
2645 | cmdname); | |
81d0912d FB |
2646 | break; |
2647 | default: | |
376253ec | 2648 | monitor_printf(mon, "%s: string expected\n", cmdname); |
81d0912d FB |
2649 | break; |
2650 | } | |
9307c4c1 FB |
2651 | goto fail; |
2652 | } | |
53773581 | 2653 | qdict_put(qdict, key, qstring_from_str(buf)); |
9307c4c1 | 2654 | } |
9dc39cba | 2655 | break; |
9307c4c1 FB |
2656 | case '/': |
2657 | { | |
2658 | int count, format, size; | |
3b46e624 | 2659 | |
cd390083 | 2660 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2661 | p++; |
2662 | if (*p == '/') { | |
2663 | /* format found */ | |
2664 | p++; | |
2665 | count = 1; | |
cd390083 | 2666 | if (qemu_isdigit(*p)) { |
9307c4c1 | 2667 | count = 0; |
cd390083 | 2668 | while (qemu_isdigit(*p)) { |
9307c4c1 FB |
2669 | count = count * 10 + (*p - '0'); |
2670 | p++; | |
2671 | } | |
2672 | } | |
2673 | size = -1; | |
2674 | format = -1; | |
2675 | for(;;) { | |
2676 | switch(*p) { | |
2677 | case 'o': | |
2678 | case 'd': | |
2679 | case 'u': | |
2680 | case 'x': | |
2681 | case 'i': | |
2682 | case 'c': | |
2683 | format = *p++; | |
2684 | break; | |
2685 | case 'b': | |
2686 | size = 1; | |
2687 | p++; | |
2688 | break; | |
2689 | case 'h': | |
2690 | size = 2; | |
2691 | p++; | |
2692 | break; | |
2693 | case 'w': | |
2694 | size = 4; | |
2695 | p++; | |
2696 | break; | |
2697 | case 'g': | |
2698 | case 'L': | |
2699 | size = 8; | |
2700 | p++; | |
2701 | break; | |
2702 | default: | |
2703 | goto next; | |
2704 | } | |
2705 | } | |
2706 | next: | |
cd390083 | 2707 | if (*p != '\0' && !qemu_isspace(*p)) { |
376253ec AL |
2708 | monitor_printf(mon, "invalid char in format: '%c'\n", |
2709 | *p); | |
9307c4c1 FB |
2710 | goto fail; |
2711 | } | |
9307c4c1 FB |
2712 | if (format < 0) |
2713 | format = default_fmt_format; | |
4c27ba27 FB |
2714 | if (format != 'i') { |
2715 | /* for 'i', not specifying a size gives -1 as size */ | |
2716 | if (size < 0) | |
2717 | size = default_fmt_size; | |
e90f009b | 2718 | default_fmt_size = size; |
4c27ba27 | 2719 | } |
9307c4c1 FB |
2720 | default_fmt_format = format; |
2721 | } else { | |
2722 | count = 1; | |
2723 | format = default_fmt_format; | |
4c27ba27 FB |
2724 | if (format != 'i') { |
2725 | size = default_fmt_size; | |
2726 | } else { | |
2727 | size = -1; | |
2728 | } | |
9307c4c1 | 2729 | } |
f7188bbe LC |
2730 | qdict_put(qdict, "count", qint_from_int(count)); |
2731 | qdict_put(qdict, "format", qint_from_int(format)); | |
2732 | qdict_put(qdict, "size", qint_from_int(size)); | |
9307c4c1 | 2733 | } |
9dc39cba | 2734 | break; |
9307c4c1 | 2735 | case 'i': |
92a31b1f | 2736 | case 'l': |
9307c4c1 | 2737 | { |
c2efc95d | 2738 | int64_t val; |
7743e588 | 2739 | |
cd390083 | 2740 | while (qemu_isspace(*p)) |
9307c4c1 | 2741 | p++; |
3440557b | 2742 | if (*typestr == '?' || *typestr == '.') { |
3440557b | 2743 | if (*typestr == '?') { |
53773581 LC |
2744 | if (*p == '\0') { |
2745 | typestr++; | |
2746 | break; | |
2747 | } | |
3440557b FB |
2748 | } else { |
2749 | if (*p == '.') { | |
2750 | p++; | |
cd390083 | 2751 | while (qemu_isspace(*p)) |
3440557b | 2752 | p++; |
3440557b | 2753 | } else { |
53773581 LC |
2754 | typestr++; |
2755 | break; | |
3440557b FB |
2756 | } |
2757 | } | |
13224a87 | 2758 | typestr++; |
9307c4c1 | 2759 | } |
376253ec | 2760 | if (get_expr(mon, &val, &p)) |
9307c4c1 | 2761 | goto fail; |
675ebef9 LC |
2762 | /* Check if 'i' is greater than 32-bit */ |
2763 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { | |
2764 | monitor_printf(mon, "\'%s\' has failed: ", cmdname); | |
2765 | monitor_printf(mon, "integer is for 32-bit values\n"); | |
2766 | goto fail; | |
2767 | } | |
53773581 | 2768 | qdict_put(qdict, key, qint_from_int(val)); |
9307c4c1 FB |
2769 | } |
2770 | break; | |
2771 | case '-': | |
2772 | { | |
2773 | int has_option; | |
2774 | /* option */ | |
3b46e624 | 2775 | |
9307c4c1 FB |
2776 | c = *typestr++; |
2777 | if (c == '\0') | |
2778 | goto bad_type; | |
cd390083 | 2779 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2780 | p++; |
2781 | has_option = 0; | |
2782 | if (*p == '-') { | |
2783 | p++; | |
2784 | if (*p != c) { | |
376253ec AL |
2785 | monitor_printf(mon, "%s: unsupported option -%c\n", |
2786 | cmdname, *p); | |
9307c4c1 FB |
2787 | goto fail; |
2788 | } | |
2789 | p++; | |
2790 | has_option = 1; | |
2791 | } | |
f7188bbe | 2792 | qdict_put(qdict, key, qint_from_int(has_option)); |
9307c4c1 FB |
2793 | } |
2794 | break; | |
2795 | default: | |
2796 | bad_type: | |
376253ec | 2797 | monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c); |
9307c4c1 FB |
2798 | goto fail; |
2799 | } | |
4d76d2ba LC |
2800 | qemu_free(key); |
2801 | key = NULL; | |
9dc39cba | 2802 | } |
9307c4c1 | 2803 | /* check that all arguments were parsed */ |
cd390083 | 2804 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2805 | p++; |
2806 | if (*p != '\0') { | |
376253ec AL |
2807 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
2808 | cmdname); | |
9307c4c1 | 2809 | goto fail; |
9dc39cba | 2810 | } |
9307c4c1 | 2811 | |
55f81d96 | 2812 | return cmd; |
ac7531ec | 2813 | |
55f81d96 | 2814 | fail: |
4d76d2ba | 2815 | qemu_free(key); |
55f81d96 LC |
2816 | return NULL; |
2817 | } | |
2818 | ||
2819 | static void monitor_handle_command(Monitor *mon, const char *cmdline) | |
2820 | { | |
55f81d96 LC |
2821 | QDict *qdict; |
2822 | const mon_cmd_t *cmd; | |
55f81d96 LC |
2823 | |
2824 | qdict = qdict_new(); | |
2825 | ||
590fb3b7 | 2826 | cmd = monitor_parse_command(mon, cmdline, qdict); |
55f81d96 LC |
2827 | if (cmd) { |
2828 | void (*handler)(Monitor *mon, const QDict *qdict); | |
2829 | ||
2830 | qemu_errors_to_mon(mon); | |
2831 | ||
2832 | handler = cmd->handler; | |
2833 | handler(mon, qdict); | |
2834 | ||
2835 | qemu_errors_to_previous(); | |
2836 | } | |
2837 | ||
f7188bbe | 2838 | QDECREF(qdict); |
9dc39cba FB |
2839 | } |
2840 | ||
81d0912d FB |
2841 | static void cmd_completion(const char *name, const char *list) |
2842 | { | |
2843 | const char *p, *pstart; | |
2844 | char cmd[128]; | |
2845 | int len; | |
2846 | ||
2847 | p = list; | |
2848 | for(;;) { | |
2849 | pstart = p; | |
2850 | p = strchr(p, '|'); | |
2851 | if (!p) | |
2852 | p = pstart + strlen(pstart); | |
2853 | len = p - pstart; | |
2854 | if (len > sizeof(cmd) - 2) | |
2855 | len = sizeof(cmd) - 2; | |
2856 | memcpy(cmd, pstart, len); | |
2857 | cmd[len] = '\0'; | |
2858 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
731b0364 | 2859 | readline_add_completion(cur_mon->rs, cmd); |
81d0912d FB |
2860 | } |
2861 | if (*p == '\0') | |
2862 | break; | |
2863 | p++; | |
2864 | } | |
2865 | } | |
2866 | ||
2867 | static void file_completion(const char *input) | |
2868 | { | |
2869 | DIR *ffs; | |
2870 | struct dirent *d; | |
2871 | char path[1024]; | |
2872 | char file[1024], file_prefix[1024]; | |
2873 | int input_path_len; | |
2874 | const char *p; | |
2875 | ||
5fafdf24 | 2876 | p = strrchr(input, '/'); |
81d0912d FB |
2877 | if (!p) { |
2878 | input_path_len = 0; | |
2879 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 2880 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
2881 | } else { |
2882 | input_path_len = p - input + 1; | |
2883 | memcpy(path, input, input_path_len); | |
2884 | if (input_path_len > sizeof(path) - 1) | |
2885 | input_path_len = sizeof(path) - 1; | |
2886 | path[input_path_len] = '\0'; | |
2887 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
2888 | } | |
2889 | #ifdef DEBUG_COMPLETION | |
376253ec AL |
2890 | monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n", |
2891 | input, path, file_prefix); | |
81d0912d FB |
2892 | #endif |
2893 | ffs = opendir(path); | |
2894 | if (!ffs) | |
2895 | return; | |
2896 | for(;;) { | |
2897 | struct stat sb; | |
2898 | d = readdir(ffs); | |
2899 | if (!d) | |
2900 | break; | |
2901 | if (strstart(d->d_name, file_prefix, NULL)) { | |
2902 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
2903 | if (input_path_len < sizeof(file)) |
2904 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
2905 | d->d_name); | |
81d0912d FB |
2906 | /* stat the file to find out if it's a directory. |
2907 | * In that case add a slash to speed up typing long paths | |
2908 | */ | |
2909 | stat(file, &sb); | |
2910 | if(S_ISDIR(sb.st_mode)) | |
363a37d5 | 2911 | pstrcat(file, sizeof(file), "/"); |
731b0364 | 2912 | readline_add_completion(cur_mon->rs, file); |
81d0912d FB |
2913 | } |
2914 | } | |
2915 | closedir(ffs); | |
2916 | } | |
2917 | ||
51de9760 | 2918 | static void block_completion_it(void *opaque, BlockDriverState *bs) |
81d0912d | 2919 | { |
51de9760 | 2920 | const char *name = bdrv_get_device_name(bs); |
81d0912d FB |
2921 | const char *input = opaque; |
2922 | ||
2923 | if (input[0] == '\0' || | |
2924 | !strncmp(name, (char *)input, strlen(input))) { | |
731b0364 | 2925 | readline_add_completion(cur_mon->rs, name); |
81d0912d FB |
2926 | } |
2927 | } | |
2928 | ||
2929 | /* NOTE: this parser is an approximate form of the real command parser */ | |
2930 | static void parse_cmdline(const char *cmdline, | |
2931 | int *pnb_args, char **args) | |
2932 | { | |
2933 | const char *p; | |
2934 | int nb_args, ret; | |
2935 | char buf[1024]; | |
2936 | ||
2937 | p = cmdline; | |
2938 | nb_args = 0; | |
2939 | for(;;) { | |
cd390083 | 2940 | while (qemu_isspace(*p)) |
81d0912d FB |
2941 | p++; |
2942 | if (*p == '\0') | |
2943 | break; | |
2944 | if (nb_args >= MAX_ARGS) | |
2945 | break; | |
2946 | ret = get_str(buf, sizeof(buf), &p); | |
2947 | args[nb_args] = qemu_strdup(buf); | |
2948 | nb_args++; | |
2949 | if (ret < 0) | |
2950 | break; | |
2951 | } | |
2952 | *pnb_args = nb_args; | |
2953 | } | |
2954 | ||
4d76d2ba LC |
2955 | static const char *next_arg_type(const char *typestr) |
2956 | { | |
2957 | const char *p = strchr(typestr, ':'); | |
2958 | return (p != NULL ? ++p : typestr); | |
2959 | } | |
2960 | ||
4c36ba32 | 2961 | static void monitor_find_completion(const char *cmdline) |
81d0912d FB |
2962 | { |
2963 | const char *cmdname; | |
2964 | char *args[MAX_ARGS]; | |
2965 | int nb_args, i, len; | |
2966 | const char *ptype, *str; | |
376253ec | 2967 | const mon_cmd_t *cmd; |
64866c3d | 2968 | const KeyDef *key; |
81d0912d FB |
2969 | |
2970 | parse_cmdline(cmdline, &nb_args, args); | |
2971 | #ifdef DEBUG_COMPLETION | |
2972 | for(i = 0; i < nb_args; i++) { | |
376253ec | 2973 | monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]); |
81d0912d FB |
2974 | } |
2975 | #endif | |
2976 | ||
2977 | /* if the line ends with a space, it means we want to complete the | |
2978 | next arg */ | |
2979 | len = strlen(cmdline); | |
cd390083 | 2980 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { |
81d0912d FB |
2981 | if (nb_args >= MAX_ARGS) |
2982 | return; | |
2983 | args[nb_args++] = qemu_strdup(""); | |
2984 | } | |
2985 | if (nb_args <= 1) { | |
2986 | /* command completion */ | |
2987 | if (nb_args == 0) | |
2988 | cmdname = ""; | |
2989 | else | |
2990 | cmdname = args[0]; | |
731b0364 | 2991 | readline_set_completion_index(cur_mon->rs, strlen(cmdname)); |
376253ec | 2992 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
2993 | cmd_completion(cmdname, cmd->name); |
2994 | } | |
2995 | } else { | |
2996 | /* find the command */ | |
376253ec | 2997 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
2998 | if (compare_cmd(args[0], cmd->name)) |
2999 | goto found; | |
3000 | } | |
3001 | return; | |
3002 | found: | |
4d76d2ba | 3003 | ptype = next_arg_type(cmd->args_type); |
81d0912d FB |
3004 | for(i = 0; i < nb_args - 2; i++) { |
3005 | if (*ptype != '\0') { | |
4d76d2ba | 3006 | ptype = next_arg_type(ptype); |
81d0912d | 3007 | while (*ptype == '?') |
4d76d2ba | 3008 | ptype = next_arg_type(ptype); |
81d0912d FB |
3009 | } |
3010 | } | |
3011 | str = args[nb_args - 1]; | |
2a1704a7 BS |
3012 | if (*ptype == '-' && ptype[1] != '\0') { |
3013 | ptype += 2; | |
3014 | } | |
81d0912d FB |
3015 | switch(*ptype) { |
3016 | case 'F': | |
3017 | /* file completion */ | |
731b0364 | 3018 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3019 | file_completion(str); |
3020 | break; | |
3021 | case 'B': | |
3022 | /* block device name completion */ | |
731b0364 | 3023 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3024 | bdrv_iterate(block_completion_it, (void *)str); |
3025 | break; | |
7fe48483 FB |
3026 | case 's': |
3027 | /* XXX: more generic ? */ | |
3028 | if (!strcmp(cmd->name, "info")) { | |
731b0364 | 3029 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
7fe48483 FB |
3030 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
3031 | cmd_completion(str, cmd->name); | |
3032 | } | |
64866c3d | 3033 | } else if (!strcmp(cmd->name, "sendkey")) { |
e600d1ef BS |
3034 | char *sep = strrchr(str, '-'); |
3035 | if (sep) | |
3036 | str = sep + 1; | |
731b0364 | 3037 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
64866c3d FB |
3038 | for(key = key_defs; key->name != NULL; key++) { |
3039 | cmd_completion(str, key->name); | |
3040 | } | |
f3353c6b JK |
3041 | } else if (!strcmp(cmd->name, "help|?")) { |
3042 | readline_set_completion_index(cur_mon->rs, strlen(str)); | |
3043 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
3044 | cmd_completion(str, cmd->name); | |
3045 | } | |
7fe48483 FB |
3046 | } |
3047 | break; | |
81d0912d FB |
3048 | default: |
3049 | break; | |
3050 | } | |
3051 | } | |
3052 | for(i = 0; i < nb_args; i++) | |
3053 | qemu_free(args[i]); | |
3054 | } | |
3055 | ||
731b0364 | 3056 | static int monitor_can_read(void *opaque) |
9dc39cba | 3057 | { |
731b0364 AL |
3058 | Monitor *mon = opaque; |
3059 | ||
3060 | return (mon->suspend_cnt == 0) ? 128 : 0; | |
9dc39cba FB |
3061 | } |
3062 | ||
731b0364 | 3063 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 3064 | { |
731b0364 | 3065 | Monitor *old_mon = cur_mon; |
7e2515e8 | 3066 | int i; |
376253ec | 3067 | |
731b0364 AL |
3068 | cur_mon = opaque; |
3069 | ||
cde76ee1 AL |
3070 | if (cur_mon->rs) { |
3071 | for (i = 0; i < size; i++) | |
3072 | readline_handle_byte(cur_mon->rs, buf[i]); | |
3073 | } else { | |
3074 | if (size == 0 || buf[size - 1] != 0) | |
3075 | monitor_printf(cur_mon, "corrupted command\n"); | |
3076 | else | |
3077 | monitor_handle_command(cur_mon, (char *)buf); | |
3078 | } | |
9dc39cba | 3079 | |
731b0364 AL |
3080 | cur_mon = old_mon; |
3081 | } | |
d8f44609 | 3082 | |
376253ec | 3083 | static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque) |
aa455485 | 3084 | { |
731b0364 | 3085 | monitor_suspend(mon); |
376253ec | 3086 | monitor_handle_command(mon, cmdline); |
731b0364 | 3087 | monitor_resume(mon); |
d8f44609 AL |
3088 | } |
3089 | ||
cde76ee1 | 3090 | int monitor_suspend(Monitor *mon) |
d8f44609 | 3091 | { |
cde76ee1 AL |
3092 | if (!mon->rs) |
3093 | return -ENOTTY; | |
731b0364 | 3094 | mon->suspend_cnt++; |
cde76ee1 | 3095 | return 0; |
d8f44609 AL |
3096 | } |
3097 | ||
376253ec | 3098 | void monitor_resume(Monitor *mon) |
d8f44609 | 3099 | { |
cde76ee1 AL |
3100 | if (!mon->rs) |
3101 | return; | |
731b0364 AL |
3102 | if (--mon->suspend_cnt == 0) |
3103 | readline_show_prompt(mon->rs); | |
aa455485 FB |
3104 | } |
3105 | ||
731b0364 | 3106 | static void monitor_event(void *opaque, int event) |
86e94dea | 3107 | { |
376253ec AL |
3108 | Monitor *mon = opaque; |
3109 | ||
2724b180 AL |
3110 | switch (event) { |
3111 | case CHR_EVENT_MUX_IN: | |
3112 | readline_restart(mon->rs); | |
3113 | monitor_resume(mon); | |
3114 | monitor_flush(mon); | |
3115 | break; | |
3116 | ||
3117 | case CHR_EVENT_MUX_OUT: | |
3118 | if (mon->suspend_cnt == 0) | |
3119 | monitor_printf(mon, "\n"); | |
3120 | monitor_flush(mon); | |
3121 | monitor_suspend(mon); | |
3122 | break; | |
86e94dea | 3123 | |
2724b180 AL |
3124 | case CHR_EVENT_RESET: |
3125 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " | |
3126 | "information\n", QEMU_VERSION); | |
3127 | if (mon->chr->focus == 0) | |
3128 | readline_show_prompt(mon->rs); | |
3129 | break; | |
3130 | } | |
86e94dea TS |
3131 | } |
3132 | ||
76655d6d AL |
3133 | |
3134 | /* | |
3135 | * Local variables: | |
3136 | * c-indent-level: 4 | |
3137 | * c-basic-offset: 4 | |
3138 | * tab-width: 8 | |
3139 | * End: | |
3140 | */ | |
3141 | ||
731b0364 | 3142 | void monitor_init(CharDriverState *chr, int flags) |
aa455485 | 3143 | { |
731b0364 | 3144 | static int is_first_init = 1; |
87127161 | 3145 | Monitor *mon; |
20d8a3ed TS |
3146 | |
3147 | if (is_first_init) { | |
c8256f9d | 3148 | key_timer = qemu_new_timer(vm_clock, release_keys, NULL); |
20d8a3ed TS |
3149 | is_first_init = 0; |
3150 | } | |
87127161 AL |
3151 | |
3152 | mon = qemu_mallocz(sizeof(*mon)); | |
20d8a3ed | 3153 | |
87127161 | 3154 | mon->chr = chr; |
731b0364 | 3155 | mon->flags = flags; |
2724b180 AL |
3156 | if (mon->chr->focus != 0) |
3157 | mon->suspend_cnt = 1; /* mux'ed monitors start suspended */ | |
cde76ee1 AL |
3158 | if (flags & MONITOR_USE_READLINE) { |
3159 | mon->rs = readline_init(mon, monitor_find_completion); | |
3160 | monitor_read_command(mon, 0); | |
3161 | } | |
87127161 | 3162 | |
731b0364 AL |
3163 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event, |
3164 | mon); | |
87127161 AL |
3165 | |
3166 | LIST_INSERT_HEAD(&mon_list, mon, entry); | |
731b0364 | 3167 | if (!cur_mon || (flags & MONITOR_IS_DEFAULT)) |
87127161 | 3168 | cur_mon = mon; |
aa455485 FB |
3169 | } |
3170 | ||
376253ec | 3171 | static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque) |
81d0912d | 3172 | { |
bb5fc20f AL |
3173 | BlockDriverState *bs = opaque; |
3174 | int ret = 0; | |
81d0912d | 3175 | |
bb5fc20f | 3176 | if (bdrv_set_key(bs, password) != 0) { |
376253ec | 3177 | monitor_printf(mon, "invalid password\n"); |
bb5fc20f | 3178 | ret = -EPERM; |
9dc39cba | 3179 | } |
731b0364 AL |
3180 | if (mon->password_completion_cb) |
3181 | mon->password_completion_cb(mon->password_opaque, ret); | |
bb5fc20f | 3182 | |
731b0364 | 3183 | monitor_read_command(mon, 1); |
9dc39cba | 3184 | } |
c0f4ce77 | 3185 | |
376253ec | 3186 | void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs, |
bb5fc20f AL |
3187 | BlockDriverCompletionFunc *completion_cb, |
3188 | void *opaque) | |
c0f4ce77 | 3189 | { |
cde76ee1 AL |
3190 | int err; |
3191 | ||
bb5fc20f AL |
3192 | if (!bdrv_key_required(bs)) { |
3193 | if (completion_cb) | |
3194 | completion_cb(opaque, 0); | |
3195 | return; | |
3196 | } | |
c0f4ce77 | 3197 | |
376253ec AL |
3198 | monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs), |
3199 | bdrv_get_encrypted_filename(bs)); | |
bb5fc20f | 3200 | |
731b0364 AL |
3201 | mon->password_completion_cb = completion_cb; |
3202 | mon->password_opaque = opaque; | |
bb5fc20f | 3203 | |
cde76ee1 AL |
3204 | err = monitor_read_password(mon, bdrv_password_cb, bs); |
3205 | ||
3206 | if (err && completion_cb) | |
3207 | completion_cb(opaque, err); | |
c0f4ce77 | 3208 | } |
ac7531ec GH |
3209 | |
3210 | typedef struct QemuErrorSink QemuErrorSink; | |
3211 | struct QemuErrorSink { | |
3212 | enum { | |
3213 | ERR_SINK_FILE, | |
3214 | ERR_SINK_MONITOR, | |
3215 | } dest; | |
3216 | union { | |
3217 | FILE *fp; | |
3218 | Monitor *mon; | |
3219 | }; | |
3220 | QemuErrorSink *previous; | |
3221 | }; | |
3222 | ||
528e93a9 | 3223 | static QemuErrorSink *qemu_error_sink; |
ac7531ec GH |
3224 | |
3225 | void qemu_errors_to_file(FILE *fp) | |
3226 | { | |
3227 | QemuErrorSink *sink; | |
3228 | ||
3229 | sink = qemu_mallocz(sizeof(*sink)); | |
3230 | sink->dest = ERR_SINK_FILE; | |
3231 | sink->fp = fp; | |
3232 | sink->previous = qemu_error_sink; | |
3233 | qemu_error_sink = sink; | |
3234 | } | |
3235 | ||
3236 | void qemu_errors_to_mon(Monitor *mon) | |
3237 | { | |
3238 | QemuErrorSink *sink; | |
3239 | ||
3240 | sink = qemu_mallocz(sizeof(*sink)); | |
3241 | sink->dest = ERR_SINK_MONITOR; | |
3242 | sink->mon = mon; | |
3243 | sink->previous = qemu_error_sink; | |
3244 | qemu_error_sink = sink; | |
3245 | } | |
3246 | ||
3247 | void qemu_errors_to_previous(void) | |
3248 | { | |
3249 | QemuErrorSink *sink; | |
3250 | ||
3251 | assert(qemu_error_sink != NULL); | |
3252 | sink = qemu_error_sink; | |
3253 | qemu_error_sink = sink->previous; | |
3254 | qemu_free(sink); | |
3255 | } | |
3256 | ||
3257 | void qemu_error(const char *fmt, ...) | |
3258 | { | |
3259 | va_list args; | |
3260 | ||
3261 | assert(qemu_error_sink != NULL); | |
3262 | switch (qemu_error_sink->dest) { | |
3263 | case ERR_SINK_FILE: | |
3264 | va_start(args, fmt); | |
3265 | vfprintf(qemu_error_sink->fp, fmt, args); | |
3266 | va_end(args); | |
3267 | break; | |
3268 | case ERR_SINK_MONITOR: | |
3269 | va_start(args, fmt); | |
3270 | monitor_vprintf(qemu_error_sink->mon, fmt, args); | |
3271 | va_end(args); | |
3272 | break; | |
3273 | } | |
3274 | } |