]>
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 | ||
92a31b1f FB |
789 | #if TARGET_LONG_BITS == 64 |
790 | #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l)) | |
791 | #else | |
792 | #define GET_TLONG(h, l) (l) | |
793 | #endif | |
794 | ||
1bd1442e | 795 | static void do_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 796 | { |
1bd1442e LC |
797 | int count = qdict_get_int(qdict, "count"); |
798 | int format = qdict_get_int(qdict, "format"); | |
799 | int size = qdict_get_int(qdict, "size"); | |
800 | target_long addr = qdict_get_int(qdict, "addr"); | |
801 | ||
376253ec | 802 | memory_dump(mon, count, format, size, addr, 0); |
9307c4c1 FB |
803 | } |
804 | ||
7743e588 BS |
805 | #if TARGET_PHYS_ADDR_BITS > 32 |
806 | #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l)) | |
807 | #else | |
808 | #define GET_TPHYSADDR(h, l) (l) | |
809 | #endif | |
810 | ||
1bd1442e | 811 | static void do_physical_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 812 | { |
1bd1442e LC |
813 | int count = qdict_get_int(qdict, "count"); |
814 | int format = qdict_get_int(qdict, "format"); | |
815 | int size = qdict_get_int(qdict, "size"); | |
816 | target_phys_addr_t addr = qdict_get_int(qdict, "addr"); | |
817 | ||
376253ec | 818 | memory_dump(mon, count, format, size, addr, 1); |
9307c4c1 FB |
819 | } |
820 | ||
1bd1442e | 821 | static void do_print(Monitor *mon, const QDict *qdict) |
9307c4c1 | 822 | { |
1bd1442e LC |
823 | int format = qdict_get_int(qdict, "format"); |
824 | target_phys_addr_t val = qdict_get_int(qdict, "val"); | |
825 | ||
7743e588 | 826 | #if TARGET_PHYS_ADDR_BITS == 32 |
9307c4c1 FB |
827 | switch(format) { |
828 | case 'o': | |
376253ec | 829 | monitor_printf(mon, "%#o", val); |
9307c4c1 FB |
830 | break; |
831 | case 'x': | |
376253ec | 832 | monitor_printf(mon, "%#x", val); |
9307c4c1 FB |
833 | break; |
834 | case 'u': | |
376253ec | 835 | monitor_printf(mon, "%u", val); |
9307c4c1 FB |
836 | break; |
837 | default: | |
838 | case 'd': | |
376253ec | 839 | monitor_printf(mon, "%d", val); |
9307c4c1 FB |
840 | break; |
841 | case 'c': | |
376253ec | 842 | monitor_printc(mon, val); |
9307c4c1 FB |
843 | break; |
844 | } | |
92a31b1f FB |
845 | #else |
846 | switch(format) { | |
847 | case 'o': | |
376253ec | 848 | monitor_printf(mon, "%#" PRIo64, val); |
92a31b1f FB |
849 | break; |
850 | case 'x': | |
376253ec | 851 | monitor_printf(mon, "%#" PRIx64, val); |
92a31b1f FB |
852 | break; |
853 | case 'u': | |
376253ec | 854 | monitor_printf(mon, "%" PRIu64, val); |
92a31b1f FB |
855 | break; |
856 | default: | |
857 | case 'd': | |
376253ec | 858 | monitor_printf(mon, "%" PRId64, val); |
92a31b1f FB |
859 | break; |
860 | case 'c': | |
376253ec | 861 | monitor_printc(mon, val); |
92a31b1f FB |
862 | break; |
863 | } | |
864 | #endif | |
376253ec | 865 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
866 | } |
867 | ||
afe67ef2 | 868 | static void do_memory_save(Monitor *mon, const QDict *qdict) |
b371dc59 FB |
869 | { |
870 | FILE *f; | |
afe67ef2 LC |
871 | uint32_t size = qdict_get_int(qdict, "size"); |
872 | const char *filename = qdict_get_str(qdict, "filename"); | |
873 | target_long addr = qdict_get_int(qdict, "val"); | |
b371dc59 FB |
874 | uint32_t l; |
875 | CPUState *env; | |
876 | uint8_t buf[1024]; | |
877 | ||
878 | env = mon_get_cpu(); | |
879 | if (!env) | |
880 | return; | |
881 | ||
882 | f = fopen(filename, "wb"); | |
883 | if (!f) { | |
376253ec | 884 | monitor_printf(mon, "could not open '%s'\n", filename); |
b371dc59 FB |
885 | return; |
886 | } | |
887 | while (size != 0) { | |
888 | l = sizeof(buf); | |
889 | if (l > size) | |
890 | l = size; | |
891 | cpu_memory_rw_debug(env, addr, buf, l, 0); | |
892 | fwrite(buf, 1, l, f); | |
893 | addr += l; | |
894 | size -= l; | |
895 | } | |
896 | fclose(f); | |
897 | } | |
898 | ||
afe67ef2 | 899 | static void do_physical_memory_save(Monitor *mon, const QDict *qdict) |
a8bdf7a6 AJ |
900 | { |
901 | FILE *f; | |
902 | uint32_t l; | |
903 | uint8_t buf[1024]; | |
afe67ef2 LC |
904 | uint32_t size = qdict_get_int(qdict, "size"); |
905 | const char *filename = qdict_get_str(qdict, "filename"); | |
906 | target_phys_addr_t addr = qdict_get_int(qdict, "val"); | |
a8bdf7a6 AJ |
907 | |
908 | f = fopen(filename, "wb"); | |
909 | if (!f) { | |
376253ec | 910 | monitor_printf(mon, "could not open '%s'\n", filename); |
a8bdf7a6 AJ |
911 | return; |
912 | } | |
913 | while (size != 0) { | |
914 | l = sizeof(buf); | |
915 | if (l > size) | |
916 | l = size; | |
917 | cpu_physical_memory_rw(addr, buf, l, 0); | |
918 | fwrite(buf, 1, l, f); | |
919 | fflush(f); | |
920 | addr += l; | |
921 | size -= l; | |
922 | } | |
923 | fclose(f); | |
924 | } | |
925 | ||
f18c16de | 926 | static void do_sum(Monitor *mon, const QDict *qdict) |
e4cf1adc FB |
927 | { |
928 | uint32_t addr; | |
929 | uint8_t buf[1]; | |
930 | uint16_t sum; | |
f18c16de LC |
931 | uint32_t start = qdict_get_int(qdict, "start"); |
932 | uint32_t size = qdict_get_int(qdict, "size"); | |
e4cf1adc FB |
933 | |
934 | sum = 0; | |
935 | for(addr = start; addr < (start + size); addr++) { | |
936 | cpu_physical_memory_rw(addr, buf, 1, 0); | |
937 | /* BSD sum algorithm ('sum' Unix command) */ | |
938 | sum = (sum >> 1) | (sum << 15); | |
939 | sum += buf[0]; | |
940 | } | |
376253ec | 941 | monitor_printf(mon, "%05d\n", sum); |
e4cf1adc FB |
942 | } |
943 | ||
a3a91a35 FB |
944 | typedef struct { |
945 | int keycode; | |
946 | const char *name; | |
947 | } KeyDef; | |
948 | ||
949 | static const KeyDef key_defs[] = { | |
950 | { 0x2a, "shift" }, | |
951 | { 0x36, "shift_r" }, | |
3b46e624 | 952 | |
a3a91a35 FB |
953 | { 0x38, "alt" }, |
954 | { 0xb8, "alt_r" }, | |
2ba27c7f TS |
955 | { 0x64, "altgr" }, |
956 | { 0xe4, "altgr_r" }, | |
a3a91a35 FB |
957 | { 0x1d, "ctrl" }, |
958 | { 0x9d, "ctrl_r" }, | |
959 | ||
960 | { 0xdd, "menu" }, | |
961 | ||
962 | { 0x01, "esc" }, | |
963 | ||
964 | { 0x02, "1" }, | |
965 | { 0x03, "2" }, | |
966 | { 0x04, "3" }, | |
967 | { 0x05, "4" }, | |
968 | { 0x06, "5" }, | |
969 | { 0x07, "6" }, | |
970 | { 0x08, "7" }, | |
971 | { 0x09, "8" }, | |
972 | { 0x0a, "9" }, | |
973 | { 0x0b, "0" }, | |
64866c3d FB |
974 | { 0x0c, "minus" }, |
975 | { 0x0d, "equal" }, | |
a3a91a35 FB |
976 | { 0x0e, "backspace" }, |
977 | ||
978 | { 0x0f, "tab" }, | |
979 | { 0x10, "q" }, | |
980 | { 0x11, "w" }, | |
981 | { 0x12, "e" }, | |
982 | { 0x13, "r" }, | |
983 | { 0x14, "t" }, | |
984 | { 0x15, "y" }, | |
985 | { 0x16, "u" }, | |
986 | { 0x17, "i" }, | |
987 | { 0x18, "o" }, | |
988 | { 0x19, "p" }, | |
989 | ||
990 | { 0x1c, "ret" }, | |
991 | ||
992 | { 0x1e, "a" }, | |
993 | { 0x1f, "s" }, | |
994 | { 0x20, "d" }, | |
995 | { 0x21, "f" }, | |
996 | { 0x22, "g" }, | |
997 | { 0x23, "h" }, | |
998 | { 0x24, "j" }, | |
999 | { 0x25, "k" }, | |
1000 | { 0x26, "l" }, | |
1001 | ||
1002 | { 0x2c, "z" }, | |
1003 | { 0x2d, "x" }, | |
1004 | { 0x2e, "c" }, | |
1005 | { 0x2f, "v" }, | |
1006 | { 0x30, "b" }, | |
1007 | { 0x31, "n" }, | |
1008 | { 0x32, "m" }, | |
9155fc45 AJ |
1009 | { 0x33, "comma" }, |
1010 | { 0x34, "dot" }, | |
1011 | { 0x35, "slash" }, | |
3b46e624 | 1012 | |
4d3b6f6e AZ |
1013 | { 0x37, "asterisk" }, |
1014 | ||
a3a91a35 | 1015 | { 0x39, "spc" }, |
00ffa62a | 1016 | { 0x3a, "caps_lock" }, |
a3a91a35 FB |
1017 | { 0x3b, "f1" }, |
1018 | { 0x3c, "f2" }, | |
1019 | { 0x3d, "f3" }, | |
1020 | { 0x3e, "f4" }, | |
1021 | { 0x3f, "f5" }, | |
1022 | { 0x40, "f6" }, | |
1023 | { 0x41, "f7" }, | |
1024 | { 0x42, "f8" }, | |
1025 | { 0x43, "f9" }, | |
1026 | { 0x44, "f10" }, | |
00ffa62a | 1027 | { 0x45, "num_lock" }, |
a3a91a35 FB |
1028 | { 0x46, "scroll_lock" }, |
1029 | ||
64866c3d FB |
1030 | { 0xb5, "kp_divide" }, |
1031 | { 0x37, "kp_multiply" }, | |
0cfec834 | 1032 | { 0x4a, "kp_subtract" }, |
64866c3d FB |
1033 | { 0x4e, "kp_add" }, |
1034 | { 0x9c, "kp_enter" }, | |
1035 | { 0x53, "kp_decimal" }, | |
f2289cb6 | 1036 | { 0x54, "sysrq" }, |
64866c3d FB |
1037 | |
1038 | { 0x52, "kp_0" }, | |
1039 | { 0x4f, "kp_1" }, | |
1040 | { 0x50, "kp_2" }, | |
1041 | { 0x51, "kp_3" }, | |
1042 | { 0x4b, "kp_4" }, | |
1043 | { 0x4c, "kp_5" }, | |
1044 | { 0x4d, "kp_6" }, | |
1045 | { 0x47, "kp_7" }, | |
1046 | { 0x48, "kp_8" }, | |
1047 | { 0x49, "kp_9" }, | |
3b46e624 | 1048 | |
a3a91a35 FB |
1049 | { 0x56, "<" }, |
1050 | ||
1051 | { 0x57, "f11" }, | |
1052 | { 0x58, "f12" }, | |
1053 | ||
1054 | { 0xb7, "print" }, | |
1055 | ||
1056 | { 0xc7, "home" }, | |
1057 | { 0xc9, "pgup" }, | |
1058 | { 0xd1, "pgdn" }, | |
1059 | { 0xcf, "end" }, | |
1060 | ||
1061 | { 0xcb, "left" }, | |
1062 | { 0xc8, "up" }, | |
1063 | { 0xd0, "down" }, | |
1064 | { 0xcd, "right" }, | |
1065 | ||
1066 | { 0xd2, "insert" }, | |
1067 | { 0xd3, "delete" }, | |
c0b5b109 BS |
1068 | #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64) |
1069 | { 0xf0, "stop" }, | |
1070 | { 0xf1, "again" }, | |
1071 | { 0xf2, "props" }, | |
1072 | { 0xf3, "undo" }, | |
1073 | { 0xf4, "front" }, | |
1074 | { 0xf5, "copy" }, | |
1075 | { 0xf6, "open" }, | |
1076 | { 0xf7, "paste" }, | |
1077 | { 0xf8, "find" }, | |
1078 | { 0xf9, "cut" }, | |
1079 | { 0xfa, "lf" }, | |
1080 | { 0xfb, "help" }, | |
1081 | { 0xfc, "meta_l" }, | |
1082 | { 0xfd, "meta_r" }, | |
1083 | { 0xfe, "compose" }, | |
1084 | #endif | |
a3a91a35 FB |
1085 | { 0, NULL }, |
1086 | }; | |
1087 | ||
1088 | static int get_keycode(const char *key) | |
1089 | { | |
1090 | const KeyDef *p; | |
64866c3d FB |
1091 | char *endp; |
1092 | int ret; | |
a3a91a35 FB |
1093 | |
1094 | for(p = key_defs; p->name != NULL; p++) { | |
1095 | if (!strcmp(key, p->name)) | |
1096 | return p->keycode; | |
1097 | } | |
64866c3d FB |
1098 | if (strstart(key, "0x", NULL)) { |
1099 | ret = strtoul(key, &endp, 0); | |
1100 | if (*endp == '\0' && ret >= 0x01 && ret <= 0xff) | |
1101 | return ret; | |
1102 | } | |
a3a91a35 FB |
1103 | return -1; |
1104 | } | |
1105 | ||
c8256f9d AZ |
1106 | #define MAX_KEYCODES 16 |
1107 | static uint8_t keycodes[MAX_KEYCODES]; | |
1108 | static int nb_pending_keycodes; | |
1109 | static QEMUTimer *key_timer; | |
1110 | ||
1111 | static void release_keys(void *opaque) | |
1112 | { | |
1113 | int keycode; | |
1114 | ||
1115 | while (nb_pending_keycodes > 0) { | |
1116 | nb_pending_keycodes--; | |
1117 | keycode = keycodes[nb_pending_keycodes]; | |
1118 | if (keycode & 0x80) | |
1119 | kbd_put_keycode(0xe0); | |
1120 | kbd_put_keycode(keycode | 0x80); | |
1121 | } | |
1122 | } | |
1123 | ||
1d4daa91 | 1124 | static void do_sendkey(Monitor *mon, const QDict *qdict) |
a3a91a35 | 1125 | { |
3401c0d9 AZ |
1126 | char keyname_buf[16]; |
1127 | char *separator; | |
1128 | int keyname_len, keycode, i; | |
1d4daa91 LC |
1129 | const char *string = qdict_get_str(qdict, "string"); |
1130 | int has_hold_time = qdict_haskey(qdict, "hold_time"); | |
1131 | int hold_time = qdict_get_try_int(qdict, "hold_time", -1); | |
3401c0d9 | 1132 | |
c8256f9d AZ |
1133 | if (nb_pending_keycodes > 0) { |
1134 | qemu_del_timer(key_timer); | |
1135 | release_keys(NULL); | |
1136 | } | |
1137 | if (!has_hold_time) | |
1138 | hold_time = 100; | |
1139 | i = 0; | |
3401c0d9 AZ |
1140 | while (1) { |
1141 | separator = strchr(string, '-'); | |
1142 | keyname_len = separator ? separator - string : strlen(string); | |
1143 | if (keyname_len > 0) { | |
1144 | pstrcpy(keyname_buf, sizeof(keyname_buf), string); | |
1145 | if (keyname_len > sizeof(keyname_buf) - 1) { | |
376253ec | 1146 | monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf); |
3401c0d9 | 1147 | return; |
a3a91a35 | 1148 | } |
c8256f9d | 1149 | if (i == MAX_KEYCODES) { |
376253ec | 1150 | monitor_printf(mon, "too many keys\n"); |
3401c0d9 AZ |
1151 | return; |
1152 | } | |
1153 | keyname_buf[keyname_len] = 0; | |
1154 | keycode = get_keycode(keyname_buf); | |
1155 | if (keycode < 0) { | |
376253ec | 1156 | monitor_printf(mon, "unknown key: '%s'\n", keyname_buf); |
3401c0d9 AZ |
1157 | return; |
1158 | } | |
c8256f9d | 1159 | keycodes[i++] = keycode; |
a3a91a35 | 1160 | } |
3401c0d9 | 1161 | if (!separator) |
a3a91a35 | 1162 | break; |
3401c0d9 | 1163 | string = separator + 1; |
a3a91a35 | 1164 | } |
c8256f9d | 1165 | nb_pending_keycodes = i; |
a3a91a35 | 1166 | /* key down events */ |
c8256f9d | 1167 | for (i = 0; i < nb_pending_keycodes; i++) { |
a3a91a35 FB |
1168 | keycode = keycodes[i]; |
1169 | if (keycode & 0x80) | |
1170 | kbd_put_keycode(0xe0); | |
1171 | kbd_put_keycode(keycode & 0x7f); | |
1172 | } | |
c8256f9d | 1173 | /* delayed key up events */ |
f227f17d AZ |
1174 | qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) + |
1175 | muldiv64(ticks_per_sec, hold_time, 1000)); | |
a3a91a35 FB |
1176 | } |
1177 | ||
13224a87 FB |
1178 | static int mouse_button_state; |
1179 | ||
1d4daa91 | 1180 | static void do_mouse_move(Monitor *mon, const QDict *qdict) |
13224a87 FB |
1181 | { |
1182 | int dx, dy, dz; | |
1d4daa91 LC |
1183 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
1184 | const char *dy_str = qdict_get_str(qdict, "dy_str"); | |
1185 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); | |
13224a87 FB |
1186 | dx = strtol(dx_str, NULL, 0); |
1187 | dy = strtol(dy_str, NULL, 0); | |
1188 | dz = 0; | |
5fafdf24 | 1189 | if (dz_str) |
13224a87 FB |
1190 | dz = strtol(dz_str, NULL, 0); |
1191 | kbd_mouse_event(dx, dy, dz, mouse_button_state); | |
1192 | } | |
1193 | ||
d54908a5 | 1194 | static void do_mouse_button(Monitor *mon, const QDict *qdict) |
13224a87 | 1195 | { |
d54908a5 | 1196 | int button_state = qdict_get_int(qdict, "button_state"); |
13224a87 FB |
1197 | mouse_button_state = button_state; |
1198 | kbd_mouse_event(0, 0, 0, mouse_button_state); | |
1199 | } | |
1200 | ||
aa93e39c | 1201 | static void do_ioport_read(Monitor *mon, const QDict *qdict) |
3440557b | 1202 | { |
aa93e39c LC |
1203 | int size = qdict_get_int(qdict, "size"); |
1204 | int addr = qdict_get_int(qdict, "addr"); | |
1205 | int has_index = qdict_haskey(qdict, "index"); | |
3440557b FB |
1206 | uint32_t val; |
1207 | int suffix; | |
1208 | ||
1209 | if (has_index) { | |
aa93e39c | 1210 | int index = qdict_get_int(qdict, "index"); |
d56dd6cf | 1211 | cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff); |
3440557b FB |
1212 | addr++; |
1213 | } | |
1214 | addr &= 0xffff; | |
1215 | ||
1216 | switch(size) { | |
1217 | default: | |
1218 | case 1: | |
1219 | val = cpu_inb(NULL, addr); | |
1220 | suffix = 'b'; | |
1221 | break; | |
1222 | case 2: | |
1223 | val = cpu_inw(NULL, addr); | |
1224 | suffix = 'w'; | |
1225 | break; | |
1226 | case 4: | |
1227 | val = cpu_inl(NULL, addr); | |
1228 | suffix = 'l'; | |
1229 | break; | |
1230 | } | |
376253ec AL |
1231 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
1232 | suffix, addr, size * 2, val); | |
3440557b | 1233 | } |
a3a91a35 | 1234 | |
1bd1442e | 1235 | static void do_ioport_write(Monitor *mon, const QDict *qdict) |
f114784f | 1236 | { |
1bd1442e LC |
1237 | int size = qdict_get_int(qdict, "size"); |
1238 | int addr = qdict_get_int(qdict, "addr"); | |
1239 | int val = qdict_get_int(qdict, "val"); | |
1240 | ||
f114784f JK |
1241 | addr &= IOPORTS_MASK; |
1242 | ||
1243 | switch (size) { | |
1244 | default: | |
1245 | case 1: | |
1246 | cpu_outb(NULL, addr, val); | |
1247 | break; | |
1248 | case 2: | |
1249 | cpu_outw(NULL, addr, val); | |
1250 | break; | |
1251 | case 4: | |
1252 | cpu_outl(NULL, addr, val); | |
1253 | break; | |
1254 | } | |
1255 | } | |
1256 | ||
d54908a5 | 1257 | static void do_boot_set(Monitor *mon, const QDict *qdict) |
0ecdffbb AJ |
1258 | { |
1259 | int res; | |
d54908a5 | 1260 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
0ecdffbb | 1261 | |
76e30d0f JK |
1262 | res = qemu_boot_set(bootdevice); |
1263 | if (res == 0) { | |
1264 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); | |
1265 | } else if (res > 0) { | |
1266 | monitor_printf(mon, "setting boot device list failed\n"); | |
0ecdffbb | 1267 | } else { |
376253ec AL |
1268 | monitor_printf(mon, "no function defined to set boot device list for " |
1269 | "this architecture\n"); | |
0ecdffbb AJ |
1270 | } |
1271 | } | |
1272 | ||
f96fc8a0 | 1273 | static void do_system_reset(Monitor *mon, const QDict *qdict) |
e4f9082b FB |
1274 | { |
1275 | qemu_system_reset_request(); | |
1276 | } | |
1277 | ||
f96fc8a0 | 1278 | static void do_system_powerdown(Monitor *mon, const QDict *qdict) |
3475187d FB |
1279 | { |
1280 | qemu_system_powerdown_request(); | |
1281 | } | |
1282 | ||
b86bda5b | 1283 | #if defined(TARGET_I386) |
376253ec | 1284 | static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask) |
b86bda5b | 1285 | { |
376253ec AL |
1286 | monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n", |
1287 | addr, | |
1288 | pte & mask, | |
1289 | pte & PG_GLOBAL_MASK ? 'G' : '-', | |
1290 | pte & PG_PSE_MASK ? 'P' : '-', | |
1291 | pte & PG_DIRTY_MASK ? 'D' : '-', | |
1292 | pte & PG_ACCESSED_MASK ? 'A' : '-', | |
1293 | pte & PG_PCD_MASK ? 'C' : '-', | |
1294 | pte & PG_PWT_MASK ? 'T' : '-', | |
1295 | pte & PG_USER_MASK ? 'U' : '-', | |
1296 | pte & PG_RW_MASK ? 'W' : '-'); | |
b86bda5b FB |
1297 | } |
1298 | ||
376253ec | 1299 | static void tlb_info(Monitor *mon) |
b86bda5b | 1300 | { |
6a00d601 | 1301 | CPUState *env; |
b86bda5b FB |
1302 | int l1, l2; |
1303 | uint32_t pgd, pde, pte; | |
1304 | ||
6a00d601 FB |
1305 | env = mon_get_cpu(); |
1306 | if (!env) | |
1307 | return; | |
1308 | ||
b86bda5b | 1309 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1310 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1311 | return; |
1312 | } | |
1313 | pgd = env->cr[3] & ~0xfff; | |
1314 | for(l1 = 0; l1 < 1024; l1++) { | |
1315 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1316 | pde = le32_to_cpu(pde); | |
1317 | if (pde & PG_PRESENT_MASK) { | |
1318 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
376253ec | 1319 | print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1)); |
b86bda5b FB |
1320 | } else { |
1321 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1322 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1323 | (uint8_t *)&pte, 4); |
1324 | pte = le32_to_cpu(pte); | |
1325 | if (pte & PG_PRESENT_MASK) { | |
376253ec | 1326 | print_pte(mon, (l1 << 22) + (l2 << 12), |
5fafdf24 | 1327 | pte & ~PG_PSE_MASK, |
b86bda5b FB |
1328 | ~0xfff); |
1329 | } | |
1330 | } | |
1331 | } | |
1332 | } | |
1333 | } | |
1334 | } | |
1335 | ||
376253ec | 1336 | static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot, |
b86bda5b FB |
1337 | uint32_t end, int prot) |
1338 | { | |
9746b15b FB |
1339 | int prot1; |
1340 | prot1 = *plast_prot; | |
1341 | if (prot != prot1) { | |
b86bda5b | 1342 | if (*pstart != -1) { |
376253ec AL |
1343 | monitor_printf(mon, "%08x-%08x %08x %c%c%c\n", |
1344 | *pstart, end, end - *pstart, | |
1345 | prot1 & PG_USER_MASK ? 'u' : '-', | |
1346 | 'r', | |
1347 | prot1 & PG_RW_MASK ? 'w' : '-'); | |
b86bda5b FB |
1348 | } |
1349 | if (prot != 0) | |
1350 | *pstart = end; | |
1351 | else | |
1352 | *pstart = -1; | |
1353 | *plast_prot = prot; | |
1354 | } | |
1355 | } | |
1356 | ||
376253ec | 1357 | static void mem_info(Monitor *mon) |
b86bda5b | 1358 | { |
6a00d601 | 1359 | CPUState *env; |
b86bda5b FB |
1360 | int l1, l2, prot, last_prot; |
1361 | uint32_t pgd, pde, pte, start, end; | |
1362 | ||
6a00d601 FB |
1363 | env = mon_get_cpu(); |
1364 | if (!env) | |
1365 | return; | |
1366 | ||
b86bda5b | 1367 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1368 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1369 | return; |
1370 | } | |
1371 | pgd = env->cr[3] & ~0xfff; | |
1372 | last_prot = 0; | |
1373 | start = -1; | |
1374 | for(l1 = 0; l1 < 1024; l1++) { | |
1375 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1376 | pde = le32_to_cpu(pde); | |
1377 | end = l1 << 22; | |
1378 | if (pde & PG_PRESENT_MASK) { | |
1379 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1380 | prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
376253ec | 1381 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1382 | } else { |
1383 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1384 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1385 | (uint8_t *)&pte, 4); |
1386 | pte = le32_to_cpu(pte); | |
1387 | end = (l1 << 22) + (l2 << 12); | |
1388 | if (pte & PG_PRESENT_MASK) { | |
1389 | prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
1390 | } else { | |
1391 | prot = 0; | |
1392 | } | |
376253ec | 1393 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1394 | } |
1395 | } | |
1396 | } else { | |
1397 | prot = 0; | |
376253ec | 1398 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1399 | } |
1400 | } | |
1401 | } | |
1402 | #endif | |
1403 | ||
7c664e2f AJ |
1404 | #if defined(TARGET_SH4) |
1405 | ||
376253ec | 1406 | static void print_tlb(Monitor *mon, int idx, tlb_t *tlb) |
7c664e2f | 1407 | { |
376253ec AL |
1408 | monitor_printf(mon, " tlb%i:\t" |
1409 | "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t" | |
1410 | "v=%hhu shared=%hhu cached=%hhu prot=%hhu " | |
1411 | "dirty=%hhu writethrough=%hhu\n", | |
1412 | idx, | |
1413 | tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size, | |
1414 | tlb->v, tlb->sh, tlb->c, tlb->pr, | |
1415 | tlb->d, tlb->wt); | |
7c664e2f AJ |
1416 | } |
1417 | ||
376253ec | 1418 | static void tlb_info(Monitor *mon) |
7c664e2f AJ |
1419 | { |
1420 | CPUState *env = mon_get_cpu(); | |
1421 | int i; | |
1422 | ||
376253ec | 1423 | monitor_printf (mon, "ITLB:\n"); |
7c664e2f | 1424 | for (i = 0 ; i < ITLB_SIZE ; i++) |
376253ec AL |
1425 | print_tlb (mon, i, &env->itlb[i]); |
1426 | monitor_printf (mon, "UTLB:\n"); | |
7c664e2f | 1427 | for (i = 0 ; i < UTLB_SIZE ; i++) |
376253ec | 1428 | print_tlb (mon, i, &env->utlb[i]); |
7c664e2f AJ |
1429 | } |
1430 | ||
1431 | #endif | |
1432 | ||
376253ec | 1433 | static void do_info_kvm(Monitor *mon) |
7ba1e619 AL |
1434 | { |
1435 | #ifdef CONFIG_KVM | |
376253ec | 1436 | monitor_printf(mon, "kvm support: "); |
7ba1e619 | 1437 | if (kvm_enabled()) |
376253ec | 1438 | monitor_printf(mon, "enabled\n"); |
7ba1e619 | 1439 | else |
376253ec | 1440 | monitor_printf(mon, "disabled\n"); |
7ba1e619 | 1441 | #else |
376253ec | 1442 | monitor_printf(mon, "kvm support: not compiled\n"); |
7ba1e619 AL |
1443 | #endif |
1444 | } | |
1445 | ||
030ea37b AL |
1446 | static void do_info_numa(Monitor *mon) |
1447 | { | |
b28b6230 | 1448 | int i; |
030ea37b AL |
1449 | CPUState *env; |
1450 | ||
1451 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); | |
1452 | for (i = 0; i < nb_numa_nodes; i++) { | |
1453 | monitor_printf(mon, "node %d cpus:", i); | |
1454 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
1455 | if (env->numa_node == i) { | |
1456 | monitor_printf(mon, " %d", env->cpu_index); | |
1457 | } | |
1458 | } | |
1459 | monitor_printf(mon, "\n"); | |
1460 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, | |
1461 | node_mem[i] >> 20); | |
1462 | } | |
1463 | } | |
1464 | ||
5f1ce948 FB |
1465 | #ifdef CONFIG_PROFILER |
1466 | ||
376253ec | 1467 | static void do_info_profile(Monitor *mon) |
5f1ce948 FB |
1468 | { |
1469 | int64_t total; | |
1470 | total = qemu_time; | |
1471 | if (total == 0) | |
1472 | total = 1; | |
376253ec AL |
1473 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
1474 | dev_time, dev_time / (double)ticks_per_sec); | |
1475 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", | |
1476 | qemu_time, qemu_time / (double)ticks_per_sec); | |
5f1ce948 | 1477 | qemu_time = 0; |
5f1ce948 | 1478 | dev_time = 0; |
5f1ce948 FB |
1479 | } |
1480 | #else | |
376253ec | 1481 | static void do_info_profile(Monitor *mon) |
5f1ce948 | 1482 | { |
376253ec | 1483 | monitor_printf(mon, "Internal profiler not compiled\n"); |
5f1ce948 FB |
1484 | } |
1485 | #endif | |
1486 | ||
ec36b695 FB |
1487 | /* Capture support */ |
1488 | static LIST_HEAD (capture_list_head, CaptureState) capture_head; | |
1489 | ||
376253ec | 1490 | static void do_info_capture(Monitor *mon) |
ec36b695 FB |
1491 | { |
1492 | int i; | |
1493 | CaptureState *s; | |
1494 | ||
1495 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
376253ec | 1496 | monitor_printf(mon, "[%d]: ", i); |
ec36b695 FB |
1497 | s->ops.info (s->opaque); |
1498 | } | |
1499 | } | |
1500 | ||
2313086a | 1501 | #ifdef HAS_AUDIO |
d54908a5 | 1502 | static void do_stop_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
1503 | { |
1504 | int i; | |
d54908a5 | 1505 | int n = qdict_get_int(qdict, "n"); |
ec36b695 FB |
1506 | CaptureState *s; |
1507 | ||
1508 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
1509 | if (i == n) { | |
1510 | s->ops.destroy (s->opaque); | |
1511 | LIST_REMOVE (s, entries); | |
1512 | qemu_free (s); | |
1513 | return; | |
1514 | } | |
1515 | } | |
1516 | } | |
1517 | ||
c1925484 LC |
1518 | static void do_wav_capture(Monitor *mon, const QDict *qdict) |
1519 | { | |
1520 | const char *path = qdict_get_str(qdict, "path"); | |
1521 | int has_freq = qdict_haskey(qdict, "freq"); | |
1522 | int freq = qdict_get_try_int(qdict, "freq", -1); | |
1523 | int has_bits = qdict_haskey(qdict, "bits"); | |
1524 | int bits = qdict_get_try_int(qdict, "bits", -1); | |
1525 | int has_channels = qdict_haskey(qdict, "nchannels"); | |
1526 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); | |
ec36b695 FB |
1527 | CaptureState *s; |
1528 | ||
1529 | s = qemu_mallocz (sizeof (*s)); | |
ec36b695 FB |
1530 | |
1531 | freq = has_freq ? freq : 44100; | |
1532 | bits = has_bits ? bits : 16; | |
1533 | nchannels = has_channels ? nchannels : 2; | |
1534 | ||
1535 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
376253ec | 1536 | monitor_printf(mon, "Faied to add wave capture\n"); |
ec36b695 FB |
1537 | qemu_free (s); |
1538 | } | |
1539 | LIST_INSERT_HEAD (&capture_head, s, entries); | |
1540 | } | |
1541 | #endif | |
1542 | ||
dc1c0b74 | 1543 | #if defined(TARGET_I386) |
d54908a5 | 1544 | static void do_inject_nmi(Monitor *mon, const QDict *qdict) |
dc1c0b74 AJ |
1545 | { |
1546 | CPUState *env; | |
d54908a5 | 1547 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
dc1c0b74 AJ |
1548 | |
1549 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
1550 | if (env->cpu_index == cpu_index) { | |
1551 | cpu_interrupt(env, CPU_INTERRUPT_NMI); | |
1552 | break; | |
1553 | } | |
1554 | } | |
1555 | #endif | |
1556 | ||
376253ec | 1557 | static void do_info_status(Monitor *mon) |
6f9c5ee7 | 1558 | { |
1b530a6d AJ |
1559 | if (vm_running) { |
1560 | if (singlestep) { | |
1561 | monitor_printf(mon, "VM status: running (single step mode)\n"); | |
1562 | } else { | |
1563 | monitor_printf(mon, "VM status: running\n"); | |
1564 | } | |
1565 | } else | |
376253ec | 1566 | monitor_printf(mon, "VM status: paused\n"); |
6f9c5ee7 AJ |
1567 | } |
1568 | ||
1569 | ||
d54908a5 | 1570 | static void do_balloon(Monitor *mon, const QDict *qdict) |
df751fa8 | 1571 | { |
d54908a5 | 1572 | int value = qdict_get_int(qdict, "value"); |
df751fa8 AL |
1573 | ram_addr_t target = value; |
1574 | qemu_balloon(target << 20); | |
1575 | } | |
1576 | ||
376253ec | 1577 | static void do_info_balloon(Monitor *mon) |
df751fa8 AL |
1578 | { |
1579 | ram_addr_t actual; | |
1580 | ||
1581 | actual = qemu_balloon_status(); | |
bd322087 | 1582 | if (kvm_enabled() && !kvm_has_sync_mmu()) |
376253ec AL |
1583 | monitor_printf(mon, "Using KVM without synchronous MMU, " |
1584 | "ballooning disabled\n"); | |
bd322087 | 1585 | else if (actual == 0) |
376253ec | 1586 | monitor_printf(mon, "Ballooning not activated in VM\n"); |
df751fa8 | 1587 | else |
376253ec | 1588 | monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20)); |
df751fa8 AL |
1589 | } |
1590 | ||
15dfcd45 | 1591 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
76655d6d | 1592 | { |
15dfcd45 | 1593 | qemu_acl *acl = qemu_acl_find(name); |
76655d6d | 1594 | |
76655d6d | 1595 | if (!acl) { |
15dfcd45 | 1596 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
76655d6d | 1597 | } |
15dfcd45 JK |
1598 | return acl; |
1599 | } | |
1600 | ||
d54908a5 | 1601 | static void do_acl_show(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1602 | { |
d54908a5 | 1603 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
1604 | qemu_acl *acl = find_acl(mon, aclname); |
1605 | qemu_acl_entry *entry; | |
1606 | int i = 0; | |
76655d6d | 1607 | |
15dfcd45 | 1608 | if (acl) { |
28a76be8 | 1609 | monitor_printf(mon, "policy: %s\n", |
76655d6d | 1610 | acl->defaultDeny ? "deny" : "allow"); |
28a76be8 AL |
1611 | TAILQ_FOREACH(entry, &acl->entries, next) { |
1612 | i++; | |
1613 | monitor_printf(mon, "%d: %s %s\n", i, | |
15dfcd45 | 1614 | entry->deny ? "deny" : "allow", entry->match); |
28a76be8 | 1615 | } |
15dfcd45 JK |
1616 | } |
1617 | } | |
1618 | ||
d54908a5 | 1619 | static void do_acl_reset(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1620 | { |
d54908a5 | 1621 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
1622 | qemu_acl *acl = find_acl(mon, aclname); |
1623 | ||
1624 | if (acl) { | |
28a76be8 AL |
1625 | qemu_acl_reset(acl); |
1626 | monitor_printf(mon, "acl: removed all rules\n"); | |
15dfcd45 JK |
1627 | } |
1628 | } | |
1629 | ||
f18c16de | 1630 | static void do_acl_policy(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1631 | { |
f18c16de LC |
1632 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1633 | const char *policy = qdict_get_str(qdict, "policy"); | |
15dfcd45 | 1634 | qemu_acl *acl = find_acl(mon, aclname); |
28a76be8 | 1635 | |
15dfcd45 JK |
1636 | if (acl) { |
1637 | if (strcmp(policy, "allow") == 0) { | |
28a76be8 AL |
1638 | acl->defaultDeny = 0; |
1639 | monitor_printf(mon, "acl: policy set to 'allow'\n"); | |
15dfcd45 | 1640 | } else if (strcmp(policy, "deny") == 0) { |
28a76be8 AL |
1641 | acl->defaultDeny = 1; |
1642 | monitor_printf(mon, "acl: policy set to 'deny'\n"); | |
1643 | } else { | |
15dfcd45 JK |
1644 | monitor_printf(mon, "acl: unknown policy '%s', " |
1645 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 | 1646 | } |
15dfcd45 JK |
1647 | } |
1648 | } | |
28a76be8 | 1649 | |
1bd1442e | 1650 | static void do_acl_add(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1651 | { |
1bd1442e LC |
1652 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1653 | const char *match = qdict_get_str(qdict, "match"); | |
1654 | const char *policy = qdict_get_str(qdict, "policy"); | |
1655 | int has_index = qdict_haskey(qdict, "index"); | |
1656 | int index = qdict_get_try_int(qdict, "index", -1); | |
15dfcd45 JK |
1657 | qemu_acl *acl = find_acl(mon, aclname); |
1658 | int deny, ret; | |
1659 | ||
1660 | if (acl) { | |
1661 | if (strcmp(policy, "allow") == 0) { | |
1662 | deny = 0; | |
1663 | } else if (strcmp(policy, "deny") == 0) { | |
1664 | deny = 1; | |
1665 | } else { | |
1666 | monitor_printf(mon, "acl: unknown policy '%s', " | |
1667 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 AL |
1668 | return; |
1669 | } | |
28a76be8 AL |
1670 | if (has_index) |
1671 | ret = qemu_acl_insert(acl, deny, match, index); | |
1672 | else | |
1673 | ret = qemu_acl_append(acl, deny, match); | |
1674 | if (ret < 0) | |
1675 | monitor_printf(mon, "acl: unable to add acl entry\n"); | |
1676 | else | |
1677 | monitor_printf(mon, "acl: added rule at position %d\n", ret); | |
15dfcd45 JK |
1678 | } |
1679 | } | |
28a76be8 | 1680 | |
f18c16de | 1681 | static void do_acl_remove(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1682 | { |
f18c16de LC |
1683 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1684 | const char *match = qdict_get_str(qdict, "match"); | |
15dfcd45 JK |
1685 | qemu_acl *acl = find_acl(mon, aclname); |
1686 | int ret; | |
28a76be8 | 1687 | |
15dfcd45 | 1688 | if (acl) { |
28a76be8 AL |
1689 | ret = qemu_acl_remove(acl, match); |
1690 | if (ret < 0) | |
1691 | monitor_printf(mon, "acl: no matching acl entry\n"); | |
1692 | else | |
1693 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); | |
76655d6d AL |
1694 | } |
1695 | } | |
1696 | ||
79c4f6b0 HY |
1697 | #if defined(TARGET_I386) |
1698 | static void do_inject_mce(Monitor *mon, | |
1699 | int cpu_index, int bank, | |
1700 | unsigned status_hi, unsigned status_lo, | |
1701 | unsigned mcg_status_hi, unsigned mcg_status_lo, | |
1702 | unsigned addr_hi, unsigned addr_lo, | |
1703 | unsigned misc_hi, unsigned misc_lo) | |
1704 | { | |
1705 | CPUState *cenv; | |
1706 | uint64_t status = ((uint64_t)status_hi << 32) | status_lo; | |
1707 | uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo; | |
1708 | uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo; | |
1709 | uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo; | |
1710 | ||
1711 | for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) | |
1712 | if (cenv->cpu_index == cpu_index && cenv->mcg_cap) { | |
1713 | cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc); | |
1714 | break; | |
1715 | } | |
1716 | } | |
1717 | #endif | |
1718 | ||
d54908a5 | 1719 | static void do_getfd(Monitor *mon, const QDict *qdict) |
f07918fd | 1720 | { |
d54908a5 | 1721 | const char *fdname = qdict_get_str(qdict, "fdname"); |
f07918fd MM |
1722 | mon_fd_t *monfd; |
1723 | int fd; | |
1724 | ||
1725 | fd = qemu_chr_get_msgfd(mon->chr); | |
1726 | if (fd == -1) { | |
1727 | monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n"); | |
1728 | return; | |
1729 | } | |
1730 | ||
1731 | if (qemu_isdigit(fdname[0])) { | |
1732 | monitor_printf(mon, "getfd: monitor names may not begin with a number\n"); | |
1733 | return; | |
1734 | } | |
1735 | ||
1736 | fd = dup(fd); | |
1737 | if (fd == -1) { | |
1738 | monitor_printf(mon, "Failed to dup() file descriptor: %s\n", | |
1739 | strerror(errno)); | |
1740 | return; | |
1741 | } | |
1742 | ||
1743 | LIST_FOREACH(monfd, &mon->fds, next) { | |
1744 | if (strcmp(monfd->name, fdname) != 0) { | |
1745 | continue; | |
1746 | } | |
1747 | ||
1748 | close(monfd->fd); | |
1749 | monfd->fd = fd; | |
1750 | return; | |
1751 | } | |
1752 | ||
1753 | monfd = qemu_mallocz(sizeof(mon_fd_t)); | |
1754 | monfd->name = qemu_strdup(fdname); | |
1755 | monfd->fd = fd; | |
1756 | ||
1757 | LIST_INSERT_HEAD(&mon->fds, monfd, next); | |
1758 | } | |
1759 | ||
d54908a5 | 1760 | static void do_closefd(Monitor *mon, const QDict *qdict) |
f07918fd | 1761 | { |
d54908a5 | 1762 | const char *fdname = qdict_get_str(qdict, "fdname"); |
f07918fd MM |
1763 | mon_fd_t *monfd; |
1764 | ||
1765 | LIST_FOREACH(monfd, &mon->fds, next) { | |
1766 | if (strcmp(monfd->name, fdname) != 0) { | |
1767 | continue; | |
1768 | } | |
1769 | ||
1770 | LIST_REMOVE(monfd, next); | |
1771 | close(monfd->fd); | |
1772 | qemu_free(monfd->name); | |
1773 | qemu_free(monfd); | |
1774 | return; | |
1775 | } | |
1776 | ||
1777 | monitor_printf(mon, "Failed to find file descriptor named %s\n", | |
1778 | fdname); | |
1779 | } | |
1780 | ||
d54908a5 | 1781 | static void do_loadvm(Monitor *mon, const QDict *qdict) |
c8d41b2c JQ |
1782 | { |
1783 | int saved_vm_running = vm_running; | |
d54908a5 | 1784 | const char *name = qdict_get_str(qdict, "name"); |
c8d41b2c JQ |
1785 | |
1786 | vm_stop(0); | |
1787 | ||
05f2401e | 1788 | if (load_vmstate(mon, name) >= 0 && saved_vm_running) |
c8d41b2c JQ |
1789 | vm_start(); |
1790 | } | |
1791 | ||
7768e04c MM |
1792 | int monitor_get_fd(Monitor *mon, const char *fdname) |
1793 | { | |
1794 | mon_fd_t *monfd; | |
1795 | ||
1796 | LIST_FOREACH(monfd, &mon->fds, next) { | |
1797 | int fd; | |
1798 | ||
1799 | if (strcmp(monfd->name, fdname) != 0) { | |
1800 | continue; | |
1801 | } | |
1802 | ||
1803 | fd = monfd->fd; | |
1804 | ||
1805 | /* caller takes ownership of fd */ | |
1806 | LIST_REMOVE(monfd, next); | |
1807 | qemu_free(monfd->name); | |
1808 | qemu_free(monfd); | |
1809 | ||
1810 | return fd; | |
1811 | } | |
1812 | ||
1813 | return -1; | |
1814 | } | |
1815 | ||
376253ec | 1816 | static const mon_cmd_t mon_cmds[] = { |
2313086a | 1817 | #include "qemu-monitor.h" |
5fafdf24 | 1818 | { NULL, NULL, }, |
9dc39cba FB |
1819 | }; |
1820 | ||
2313086a | 1821 | /* Please update qemu-monitor.hx when adding or changing commands */ |
376253ec | 1822 | static const mon_cmd_t info_cmds[] = { |
9bc9d1c7 | 1823 | { "version", "", do_info_version, |
d2c639d6 | 1824 | "", "show the version of QEMU" }, |
9307c4c1 | 1825 | { "network", "", do_info_network, |
9dc39cba | 1826 | "", "show the network state" }, |
5ccfae10 AL |
1827 | { "chardev", "", qemu_chr_info, |
1828 | "", "show the character devices" }, | |
376253ec | 1829 | { "block", "", bdrv_info, |
9dc39cba | 1830 | "", "show the block devices" }, |
376253ec | 1831 | { "blockstats", "", bdrv_info_stats, |
a36e69dd | 1832 | "", "show block device statistics" }, |
9307c4c1 FB |
1833 | { "registers", "", do_info_registers, |
1834 | "", "show the cpu registers" }, | |
6a00d601 FB |
1835 | { "cpus", "", do_info_cpus, |
1836 | "", "show infos for each CPU" }, | |
aa455485 FB |
1837 | { "history", "", do_info_history, |
1838 | "", "show the command line history", }, | |
4a0fb71e FB |
1839 | { "irq", "", irq_info, |
1840 | "", "show the interrupts statistics (if available)", }, | |
4c27ba27 FB |
1841 | { "pic", "", pic_info, |
1842 | "", "show i8259 (PIC) state", }, | |
86e0c048 FB |
1843 | { "pci", "", pci_info, |
1844 | "", "show PCI info", }, | |
7c664e2f | 1845 | #if defined(TARGET_I386) || defined(TARGET_SH4) |
b86bda5b FB |
1846 | { "tlb", "", tlb_info, |
1847 | "", "show virtual to physical memory mappings", }, | |
7c664e2f AJ |
1848 | #endif |
1849 | #if defined(TARGET_I386) | |
b86bda5b FB |
1850 | { "mem", "", mem_info, |
1851 | "", "show the active virtual memory mappings", }, | |
16b29ae1 AL |
1852 | { "hpet", "", do_info_hpet, |
1853 | "", "show state of HPET", }, | |
b86bda5b | 1854 | #endif |
e3db7226 FB |
1855 | { "jit", "", do_info_jit, |
1856 | "", "show dynamic compiler info", }, | |
7ba1e619 | 1857 | { "kvm", "", do_info_kvm, |
d2c639d6 | 1858 | "", "show KVM information", }, |
030ea37b AL |
1859 | { "numa", "", do_info_numa, |
1860 | "", "show NUMA information", }, | |
a594cfbf FB |
1861 | { "usb", "", usb_info, |
1862 | "", "show guest USB devices", }, | |
1863 | { "usbhost", "", usb_host_info, | |
1864 | "", "show host USB devices", }, | |
5f1ce948 FB |
1865 | { "profile", "", do_info_profile, |
1866 | "", "show profiling information", }, | |
ec36b695 | 1867 | { "capture", "", do_info_capture, |
17100159 | 1868 | "", "show capture information" }, |
faea38e7 | 1869 | { "snapshots", "", do_info_snapshots, |
17100159 | 1870 | "", "show the currently saved VM snapshots" }, |
6f9c5ee7 AJ |
1871 | { "status", "", do_info_status, |
1872 | "", "show the current VM status (running|paused)" }, | |
201a51fc AZ |
1873 | { "pcmcia", "", pcmcia_info, |
1874 | "", "show guest PCMCIA status" }, | |
455204eb TS |
1875 | { "mice", "", do_info_mice, |
1876 | "", "show which guest mouse is receiving events" }, | |
a9ce8590 FB |
1877 | { "vnc", "", do_info_vnc, |
1878 | "", "show the vnc server status"}, | |
c35734b2 TS |
1879 | { "name", "", do_info_name, |
1880 | "", "show the current VM name" }, | |
f1f23ad5 BS |
1881 | { "uuid", "", do_info_uuid, |
1882 | "", "show the current VM UUID" }, | |
76a66253 JM |
1883 | #if defined(TARGET_PPC) |
1884 | { "cpustats", "", do_info_cpu_stats, | |
1885 | "", "show CPU statistics", }, | |
1886 | #endif | |
31a60e22 | 1887 | #if defined(CONFIG_SLIRP) |
6dbe553f JK |
1888 | { "usernet", "", do_info_usernet, |
1889 | "", "show user network stack connection states", }, | |
31a60e22 | 1890 | #endif |
5bb7910a | 1891 | { "migrate", "", do_info_migrate, "", "show migration status" }, |
df751fa8 AL |
1892 | { "balloon", "", do_info_balloon, |
1893 | "", "show balloon information" }, | |
cae4956e GH |
1894 | { "qtree", "", do_info_qtree, |
1895 | "", "show device tree" }, | |
f6c64e0e GH |
1896 | { "qdm", "", do_info_qdm, |
1897 | "", "show qdev device model list" }, | |
9dc39cba FB |
1898 | { NULL, NULL, }, |
1899 | }; | |
1900 | ||
9307c4c1 FB |
1901 | /*******************************************************************/ |
1902 | ||
1903 | static const char *pch; | |
1904 | static jmp_buf expr_env; | |
1905 | ||
92a31b1f FB |
1906 | #define MD_TLONG 0 |
1907 | #define MD_I32 1 | |
1908 | ||
9307c4c1 FB |
1909 | typedef struct MonitorDef { |
1910 | const char *name; | |
1911 | int offset; | |
8662d656 | 1912 | target_long (*get_value)(const struct MonitorDef *md, int val); |
92a31b1f | 1913 | int type; |
9307c4c1 FB |
1914 | } MonitorDef; |
1915 | ||
57206fd4 | 1916 | #if defined(TARGET_I386) |
8662d656 | 1917 | static target_long monitor_get_pc (const struct MonitorDef *md, int val) |
57206fd4 | 1918 | { |
6a00d601 FB |
1919 | CPUState *env = mon_get_cpu(); |
1920 | if (!env) | |
1921 | return 0; | |
1922 | return env->eip + env->segs[R_CS].base; | |
57206fd4 FB |
1923 | } |
1924 | #endif | |
1925 | ||
a541f297 | 1926 | #if defined(TARGET_PPC) |
8662d656 | 1927 | static target_long monitor_get_ccr (const struct MonitorDef *md, int val) |
a541f297 | 1928 | { |
6a00d601 | 1929 | CPUState *env = mon_get_cpu(); |
a541f297 FB |
1930 | unsigned int u; |
1931 | int i; | |
1932 | ||
6a00d601 FB |
1933 | if (!env) |
1934 | return 0; | |
1935 | ||
a541f297 FB |
1936 | u = 0; |
1937 | for (i = 0; i < 8; i++) | |
28a76be8 | 1938 | u |= env->crf[i] << (32 - (4 * i)); |
a541f297 FB |
1939 | |
1940 | return u; | |
1941 | } | |
1942 | ||
8662d656 | 1943 | static target_long monitor_get_msr (const struct MonitorDef *md, int val) |
a541f297 | 1944 | { |
6a00d601 FB |
1945 | CPUState *env = mon_get_cpu(); |
1946 | if (!env) | |
1947 | return 0; | |
0411a972 | 1948 | return env->msr; |
a541f297 FB |
1949 | } |
1950 | ||
8662d656 | 1951 | static target_long monitor_get_xer (const struct MonitorDef *md, int val) |
a541f297 | 1952 | { |
6a00d601 FB |
1953 | CPUState *env = mon_get_cpu(); |
1954 | if (!env) | |
1955 | return 0; | |
3d7b417e | 1956 | return env->xer; |
a541f297 | 1957 | } |
9fddaa0c | 1958 | |
8662d656 | 1959 | static target_long monitor_get_decr (const struct MonitorDef *md, int val) |
9fddaa0c | 1960 | { |
6a00d601 FB |
1961 | CPUState *env = mon_get_cpu(); |
1962 | if (!env) | |
1963 | return 0; | |
1964 | return cpu_ppc_load_decr(env); | |
9fddaa0c FB |
1965 | } |
1966 | ||
8662d656 | 1967 | static target_long monitor_get_tbu (const struct MonitorDef *md, int val) |
9fddaa0c | 1968 | { |
6a00d601 FB |
1969 | CPUState *env = mon_get_cpu(); |
1970 | if (!env) | |
1971 | return 0; | |
1972 | return cpu_ppc_load_tbu(env); | |
9fddaa0c FB |
1973 | } |
1974 | ||
8662d656 | 1975 | static target_long monitor_get_tbl (const struct MonitorDef *md, int val) |
9fddaa0c | 1976 | { |
6a00d601 FB |
1977 | CPUState *env = mon_get_cpu(); |
1978 | if (!env) | |
1979 | return 0; | |
1980 | return cpu_ppc_load_tbl(env); | |
9fddaa0c | 1981 | } |
a541f297 FB |
1982 | #endif |
1983 | ||
e95c8d51 | 1984 | #if defined(TARGET_SPARC) |
7b936c0c | 1985 | #ifndef TARGET_SPARC64 |
8662d656 | 1986 | static target_long monitor_get_psr (const struct MonitorDef *md, int val) |
e95c8d51 | 1987 | { |
6a00d601 FB |
1988 | CPUState *env = mon_get_cpu(); |
1989 | if (!env) | |
1990 | return 0; | |
1991 | return GET_PSR(env); | |
e95c8d51 | 1992 | } |
7b936c0c | 1993 | #endif |
e95c8d51 | 1994 | |
8662d656 | 1995 | static target_long monitor_get_reg(const struct MonitorDef *md, int val) |
e95c8d51 | 1996 | { |
6a00d601 FB |
1997 | CPUState *env = mon_get_cpu(); |
1998 | if (!env) | |
1999 | return 0; | |
2000 | return env->regwptr[val]; | |
e95c8d51 FB |
2001 | } |
2002 | #endif | |
2003 | ||
8662d656 | 2004 | static const MonitorDef monitor_defs[] = { |
9307c4c1 | 2005 | #ifdef TARGET_I386 |
57206fd4 FB |
2006 | |
2007 | #define SEG(name, seg) \ | |
92a31b1f | 2008 | { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\ |
57206fd4 | 2009 | { name ".base", offsetof(CPUState, segs[seg].base) },\ |
92a31b1f | 2010 | { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 }, |
57206fd4 | 2011 | |
9307c4c1 FB |
2012 | { "eax", offsetof(CPUState, regs[0]) }, |
2013 | { "ecx", offsetof(CPUState, regs[1]) }, | |
2014 | { "edx", offsetof(CPUState, regs[2]) }, | |
2015 | { "ebx", offsetof(CPUState, regs[3]) }, | |
2016 | { "esp|sp", offsetof(CPUState, regs[4]) }, | |
2017 | { "ebp|fp", offsetof(CPUState, regs[5]) }, | |
2018 | { "esi", offsetof(CPUState, regs[6]) }, | |
01038d2a | 2019 | { "edi", offsetof(CPUState, regs[7]) }, |
92a31b1f FB |
2020 | #ifdef TARGET_X86_64 |
2021 | { "r8", offsetof(CPUState, regs[8]) }, | |
2022 | { "r9", offsetof(CPUState, regs[9]) }, | |
2023 | { "r10", offsetof(CPUState, regs[10]) }, | |
2024 | { "r11", offsetof(CPUState, regs[11]) }, | |
2025 | { "r12", offsetof(CPUState, regs[12]) }, | |
2026 | { "r13", offsetof(CPUState, regs[13]) }, | |
2027 | { "r14", offsetof(CPUState, regs[14]) }, | |
2028 | { "r15", offsetof(CPUState, regs[15]) }, | |
2029 | #endif | |
9307c4c1 | 2030 | { "eflags", offsetof(CPUState, eflags) }, |
57206fd4 FB |
2031 | { "eip", offsetof(CPUState, eip) }, |
2032 | SEG("cs", R_CS) | |
2033 | SEG("ds", R_DS) | |
2034 | SEG("es", R_ES) | |
01038d2a | 2035 | SEG("ss", R_SS) |
57206fd4 FB |
2036 | SEG("fs", R_FS) |
2037 | SEG("gs", R_GS) | |
2038 | { "pc", 0, monitor_get_pc, }, | |
a541f297 | 2039 | #elif defined(TARGET_PPC) |
ff937dba | 2040 | /* General purpose registers */ |
a541f297 FB |
2041 | { "r0", offsetof(CPUState, gpr[0]) }, |
2042 | { "r1", offsetof(CPUState, gpr[1]) }, | |
2043 | { "r2", offsetof(CPUState, gpr[2]) }, | |
2044 | { "r3", offsetof(CPUState, gpr[3]) }, | |
2045 | { "r4", offsetof(CPUState, gpr[4]) }, | |
2046 | { "r5", offsetof(CPUState, gpr[5]) }, | |
2047 | { "r6", offsetof(CPUState, gpr[6]) }, | |
2048 | { "r7", offsetof(CPUState, gpr[7]) }, | |
2049 | { "r8", offsetof(CPUState, gpr[8]) }, | |
2050 | { "r9", offsetof(CPUState, gpr[9]) }, | |
2051 | { "r10", offsetof(CPUState, gpr[10]) }, | |
2052 | { "r11", offsetof(CPUState, gpr[11]) }, | |
2053 | { "r12", offsetof(CPUState, gpr[12]) }, | |
2054 | { "r13", offsetof(CPUState, gpr[13]) }, | |
2055 | { "r14", offsetof(CPUState, gpr[14]) }, | |
2056 | { "r15", offsetof(CPUState, gpr[15]) }, | |
2057 | { "r16", offsetof(CPUState, gpr[16]) }, | |
2058 | { "r17", offsetof(CPUState, gpr[17]) }, | |
2059 | { "r18", offsetof(CPUState, gpr[18]) }, | |
2060 | { "r19", offsetof(CPUState, gpr[19]) }, | |
2061 | { "r20", offsetof(CPUState, gpr[20]) }, | |
2062 | { "r21", offsetof(CPUState, gpr[21]) }, | |
2063 | { "r22", offsetof(CPUState, gpr[22]) }, | |
2064 | { "r23", offsetof(CPUState, gpr[23]) }, | |
2065 | { "r24", offsetof(CPUState, gpr[24]) }, | |
2066 | { "r25", offsetof(CPUState, gpr[25]) }, | |
2067 | { "r26", offsetof(CPUState, gpr[26]) }, | |
2068 | { "r27", offsetof(CPUState, gpr[27]) }, | |
2069 | { "r28", offsetof(CPUState, gpr[28]) }, | |
2070 | { "r29", offsetof(CPUState, gpr[29]) }, | |
2071 | { "r30", offsetof(CPUState, gpr[30]) }, | |
2072 | { "r31", offsetof(CPUState, gpr[31]) }, | |
ff937dba JM |
2073 | /* Floating point registers */ |
2074 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2075 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2076 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2077 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2078 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2079 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2080 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2081 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2082 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2083 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2084 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2085 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2086 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2087 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2088 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2089 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2090 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2091 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2092 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2093 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2094 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2095 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2096 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2097 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2098 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2099 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2100 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2101 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2102 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2103 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2104 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2105 | { "f31", offsetof(CPUState, fpr[31]) }, | |
2106 | { "fpscr", offsetof(CPUState, fpscr) }, | |
2107 | /* Next instruction pointer */ | |
57206fd4 | 2108 | { "nip|pc", offsetof(CPUState, nip) }, |
a541f297 FB |
2109 | { "lr", offsetof(CPUState, lr) }, |
2110 | { "ctr", offsetof(CPUState, ctr) }, | |
9fddaa0c | 2111 | { "decr", 0, &monitor_get_decr, }, |
a541f297 | 2112 | { "ccr", 0, &monitor_get_ccr, }, |
ff937dba | 2113 | /* Machine state register */ |
a541f297 FB |
2114 | { "msr", 0, &monitor_get_msr, }, |
2115 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
2116 | { "tbu", 0, &monitor_get_tbu, }, |
2117 | { "tbl", 0, &monitor_get_tbl, }, | |
ff937dba JM |
2118 | #if defined(TARGET_PPC64) |
2119 | /* Address space register */ | |
2120 | { "asr", offsetof(CPUState, asr) }, | |
2121 | #endif | |
2122 | /* Segment registers */ | |
a541f297 FB |
2123 | { "sdr1", offsetof(CPUState, sdr1) }, |
2124 | { "sr0", offsetof(CPUState, sr[0]) }, | |
2125 | { "sr1", offsetof(CPUState, sr[1]) }, | |
2126 | { "sr2", offsetof(CPUState, sr[2]) }, | |
2127 | { "sr3", offsetof(CPUState, sr[3]) }, | |
2128 | { "sr4", offsetof(CPUState, sr[4]) }, | |
2129 | { "sr5", offsetof(CPUState, sr[5]) }, | |
2130 | { "sr6", offsetof(CPUState, sr[6]) }, | |
2131 | { "sr7", offsetof(CPUState, sr[7]) }, | |
2132 | { "sr8", offsetof(CPUState, sr[8]) }, | |
2133 | { "sr9", offsetof(CPUState, sr[9]) }, | |
2134 | { "sr10", offsetof(CPUState, sr[10]) }, | |
2135 | { "sr11", offsetof(CPUState, sr[11]) }, | |
2136 | { "sr12", offsetof(CPUState, sr[12]) }, | |
2137 | { "sr13", offsetof(CPUState, sr[13]) }, | |
2138 | { "sr14", offsetof(CPUState, sr[14]) }, | |
2139 | { "sr15", offsetof(CPUState, sr[15]) }, | |
2140 | /* Too lazy to put BATs and SPRs ... */ | |
e95c8d51 FB |
2141 | #elif defined(TARGET_SPARC) |
2142 | { "g0", offsetof(CPUState, gregs[0]) }, | |
2143 | { "g1", offsetof(CPUState, gregs[1]) }, | |
2144 | { "g2", offsetof(CPUState, gregs[2]) }, | |
2145 | { "g3", offsetof(CPUState, gregs[3]) }, | |
2146 | { "g4", offsetof(CPUState, gregs[4]) }, | |
2147 | { "g5", offsetof(CPUState, gregs[5]) }, | |
2148 | { "g6", offsetof(CPUState, gregs[6]) }, | |
2149 | { "g7", offsetof(CPUState, gregs[7]) }, | |
2150 | { "o0", 0, monitor_get_reg }, | |
2151 | { "o1", 1, monitor_get_reg }, | |
2152 | { "o2", 2, monitor_get_reg }, | |
2153 | { "o3", 3, monitor_get_reg }, | |
2154 | { "o4", 4, monitor_get_reg }, | |
2155 | { "o5", 5, monitor_get_reg }, | |
2156 | { "o6", 6, monitor_get_reg }, | |
2157 | { "o7", 7, monitor_get_reg }, | |
2158 | { "l0", 8, monitor_get_reg }, | |
2159 | { "l1", 9, monitor_get_reg }, | |
2160 | { "l2", 10, monitor_get_reg }, | |
2161 | { "l3", 11, monitor_get_reg }, | |
2162 | { "l4", 12, monitor_get_reg }, | |
2163 | { "l5", 13, monitor_get_reg }, | |
2164 | { "l6", 14, monitor_get_reg }, | |
2165 | { "l7", 15, monitor_get_reg }, | |
2166 | { "i0", 16, monitor_get_reg }, | |
2167 | { "i1", 17, monitor_get_reg }, | |
2168 | { "i2", 18, monitor_get_reg }, | |
2169 | { "i3", 19, monitor_get_reg }, | |
2170 | { "i4", 20, monitor_get_reg }, | |
2171 | { "i5", 21, monitor_get_reg }, | |
2172 | { "i6", 22, monitor_get_reg }, | |
2173 | { "i7", 23, monitor_get_reg }, | |
2174 | { "pc", offsetof(CPUState, pc) }, | |
2175 | { "npc", offsetof(CPUState, npc) }, | |
2176 | { "y", offsetof(CPUState, y) }, | |
7b936c0c | 2177 | #ifndef TARGET_SPARC64 |
e95c8d51 FB |
2178 | { "psr", 0, &monitor_get_psr, }, |
2179 | { "wim", offsetof(CPUState, wim) }, | |
7b936c0c | 2180 | #endif |
e95c8d51 FB |
2181 | { "tbr", offsetof(CPUState, tbr) }, |
2182 | { "fsr", offsetof(CPUState, fsr) }, | |
2183 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2184 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2185 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2186 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2187 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2188 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2189 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2190 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2191 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2192 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2193 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2194 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2195 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2196 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2197 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2198 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2199 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2200 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2201 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2202 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2203 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2204 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2205 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2206 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2207 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2208 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2209 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2210 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2211 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2212 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2213 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2214 | { "f31", offsetof(CPUState, fpr[31]) }, | |
7b936c0c FB |
2215 | #ifdef TARGET_SPARC64 |
2216 | { "f32", offsetof(CPUState, fpr[32]) }, | |
2217 | { "f34", offsetof(CPUState, fpr[34]) }, | |
2218 | { "f36", offsetof(CPUState, fpr[36]) }, | |
2219 | { "f38", offsetof(CPUState, fpr[38]) }, | |
2220 | { "f40", offsetof(CPUState, fpr[40]) }, | |
2221 | { "f42", offsetof(CPUState, fpr[42]) }, | |
2222 | { "f44", offsetof(CPUState, fpr[44]) }, | |
2223 | { "f46", offsetof(CPUState, fpr[46]) }, | |
2224 | { "f48", offsetof(CPUState, fpr[48]) }, | |
2225 | { "f50", offsetof(CPUState, fpr[50]) }, | |
2226 | { "f52", offsetof(CPUState, fpr[52]) }, | |
2227 | { "f54", offsetof(CPUState, fpr[54]) }, | |
2228 | { "f56", offsetof(CPUState, fpr[56]) }, | |
2229 | { "f58", offsetof(CPUState, fpr[58]) }, | |
2230 | { "f60", offsetof(CPUState, fpr[60]) }, | |
2231 | { "f62", offsetof(CPUState, fpr[62]) }, | |
2232 | { "asi", offsetof(CPUState, asi) }, | |
2233 | { "pstate", offsetof(CPUState, pstate) }, | |
2234 | { "cansave", offsetof(CPUState, cansave) }, | |
2235 | { "canrestore", offsetof(CPUState, canrestore) }, | |
2236 | { "otherwin", offsetof(CPUState, otherwin) }, | |
2237 | { "wstate", offsetof(CPUState, wstate) }, | |
2238 | { "cleanwin", offsetof(CPUState, cleanwin) }, | |
2239 | { "fprs", offsetof(CPUState, fprs) }, | |
2240 | #endif | |
9307c4c1 FB |
2241 | #endif |
2242 | { NULL }, | |
2243 | }; | |
2244 | ||
376253ec | 2245 | static void expr_error(Monitor *mon, const char *msg) |
9dc39cba | 2246 | { |
376253ec | 2247 | monitor_printf(mon, "%s\n", msg); |
9307c4c1 FB |
2248 | longjmp(expr_env, 1); |
2249 | } | |
2250 | ||
6a00d601 | 2251 | /* return 0 if OK, -1 if not found, -2 if no CPU defined */ |
92a31b1f | 2252 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 2253 | { |
8662d656 | 2254 | const MonitorDef *md; |
92a31b1f FB |
2255 | void *ptr; |
2256 | ||
9307c4c1 FB |
2257 | for(md = monitor_defs; md->name != NULL; md++) { |
2258 | if (compare_cmd(name, md->name)) { | |
2259 | if (md->get_value) { | |
e95c8d51 | 2260 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 2261 | } else { |
6a00d601 FB |
2262 | CPUState *env = mon_get_cpu(); |
2263 | if (!env) | |
2264 | return -2; | |
2265 | ptr = (uint8_t *)env + md->offset; | |
92a31b1f FB |
2266 | switch(md->type) { |
2267 | case MD_I32: | |
2268 | *pval = *(int32_t *)ptr; | |
2269 | break; | |
2270 | case MD_TLONG: | |
2271 | *pval = *(target_long *)ptr; | |
2272 | break; | |
2273 | default: | |
2274 | *pval = 0; | |
2275 | break; | |
2276 | } | |
9307c4c1 FB |
2277 | } |
2278 | return 0; | |
2279 | } | |
2280 | } | |
2281 | return -1; | |
2282 | } | |
2283 | ||
2284 | static void next(void) | |
2285 | { | |
660f11be | 2286 | if (*pch != '\0') { |
9307c4c1 | 2287 | pch++; |
cd390083 | 2288 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2289 | pch++; |
2290 | } | |
2291 | } | |
2292 | ||
376253ec | 2293 | static int64_t expr_sum(Monitor *mon); |
9307c4c1 | 2294 | |
376253ec | 2295 | static int64_t expr_unary(Monitor *mon) |
9307c4c1 | 2296 | { |
c2efc95d | 2297 | int64_t n; |
9307c4c1 | 2298 | char *p; |
6a00d601 | 2299 | int ret; |
9307c4c1 FB |
2300 | |
2301 | switch(*pch) { | |
2302 | case '+': | |
2303 | next(); | |
376253ec | 2304 | n = expr_unary(mon); |
9307c4c1 FB |
2305 | break; |
2306 | case '-': | |
2307 | next(); | |
376253ec | 2308 | n = -expr_unary(mon); |
9307c4c1 FB |
2309 | break; |
2310 | case '~': | |
2311 | next(); | |
376253ec | 2312 | n = ~expr_unary(mon); |
9307c4c1 FB |
2313 | break; |
2314 | case '(': | |
2315 | next(); | |
376253ec | 2316 | n = expr_sum(mon); |
9307c4c1 | 2317 | if (*pch != ')') { |
376253ec | 2318 | expr_error(mon, "')' expected"); |
9307c4c1 FB |
2319 | } |
2320 | next(); | |
2321 | break; | |
81d0912d FB |
2322 | case '\'': |
2323 | pch++; | |
2324 | if (*pch == '\0') | |
376253ec | 2325 | expr_error(mon, "character constant expected"); |
81d0912d FB |
2326 | n = *pch; |
2327 | pch++; | |
2328 | if (*pch != '\'') | |
376253ec | 2329 | expr_error(mon, "missing terminating \' character"); |
81d0912d FB |
2330 | next(); |
2331 | break; | |
9307c4c1 FB |
2332 | case '$': |
2333 | { | |
2334 | char buf[128], *q; | |
69b34976 | 2335 | target_long reg=0; |
3b46e624 | 2336 | |
9307c4c1 FB |
2337 | pch++; |
2338 | q = buf; | |
2339 | while ((*pch >= 'a' && *pch <= 'z') || | |
2340 | (*pch >= 'A' && *pch <= 'Z') || | |
2341 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 2342 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
2343 | if ((q - buf) < sizeof(buf) - 1) |
2344 | *q++ = *pch; | |
2345 | pch++; | |
2346 | } | |
cd390083 | 2347 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2348 | pch++; |
2349 | *q = 0; | |
7743e588 | 2350 | ret = get_monitor_def(®, buf); |
6a00d601 | 2351 | if (ret == -1) |
376253ec | 2352 | expr_error(mon, "unknown register"); |
5fafdf24 | 2353 | else if (ret == -2) |
376253ec | 2354 | expr_error(mon, "no cpu defined"); |
7743e588 | 2355 | n = reg; |
9307c4c1 FB |
2356 | } |
2357 | break; | |
2358 | case '\0': | |
376253ec | 2359 | expr_error(mon, "unexpected end of expression"); |
9307c4c1 FB |
2360 | n = 0; |
2361 | break; | |
2362 | default: | |
7743e588 | 2363 | #if TARGET_PHYS_ADDR_BITS > 32 |
4f4fbf77 FB |
2364 | n = strtoull(pch, &p, 0); |
2365 | #else | |
9307c4c1 | 2366 | n = strtoul(pch, &p, 0); |
4f4fbf77 | 2367 | #endif |
9307c4c1 | 2368 | if (pch == p) { |
376253ec | 2369 | expr_error(mon, "invalid char in expression"); |
9307c4c1 FB |
2370 | } |
2371 | pch = p; | |
cd390083 | 2372 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2373 | pch++; |
2374 | break; | |
2375 | } | |
2376 | return n; | |
2377 | } | |
2378 | ||
2379 | ||
376253ec | 2380 | static int64_t expr_prod(Monitor *mon) |
9307c4c1 | 2381 | { |
c2efc95d | 2382 | int64_t val, val2; |
92a31b1f | 2383 | int op; |
3b46e624 | 2384 | |
376253ec | 2385 | val = expr_unary(mon); |
9307c4c1 FB |
2386 | for(;;) { |
2387 | op = *pch; | |
2388 | if (op != '*' && op != '/' && op != '%') | |
2389 | break; | |
2390 | next(); | |
376253ec | 2391 | val2 = expr_unary(mon); |
9307c4c1 FB |
2392 | switch(op) { |
2393 | default: | |
2394 | case '*': | |
2395 | val *= val2; | |
2396 | break; | |
2397 | case '/': | |
2398 | case '%': | |
5fafdf24 | 2399 | if (val2 == 0) |
376253ec | 2400 | expr_error(mon, "division by zero"); |
9307c4c1 FB |
2401 | if (op == '/') |
2402 | val /= val2; | |
2403 | else | |
2404 | val %= val2; | |
2405 | break; | |
2406 | } | |
2407 | } | |
2408 | return val; | |
2409 | } | |
2410 | ||
376253ec | 2411 | static int64_t expr_logic(Monitor *mon) |
9307c4c1 | 2412 | { |
c2efc95d | 2413 | int64_t val, val2; |
92a31b1f | 2414 | int op; |
9307c4c1 | 2415 | |
376253ec | 2416 | val = expr_prod(mon); |
9307c4c1 FB |
2417 | for(;;) { |
2418 | op = *pch; | |
2419 | if (op != '&' && op != '|' && op != '^') | |
2420 | break; | |
2421 | next(); | |
376253ec | 2422 | val2 = expr_prod(mon); |
9307c4c1 FB |
2423 | switch(op) { |
2424 | default: | |
2425 | case '&': | |
2426 | val &= val2; | |
2427 | break; | |
2428 | case '|': | |
2429 | val |= val2; | |
2430 | break; | |
2431 | case '^': | |
2432 | val ^= val2; | |
2433 | break; | |
2434 | } | |
2435 | } | |
2436 | return val; | |
2437 | } | |
2438 | ||
376253ec | 2439 | static int64_t expr_sum(Monitor *mon) |
9307c4c1 | 2440 | { |
c2efc95d | 2441 | int64_t val, val2; |
92a31b1f | 2442 | int op; |
9307c4c1 | 2443 | |
376253ec | 2444 | val = expr_logic(mon); |
9307c4c1 FB |
2445 | for(;;) { |
2446 | op = *pch; | |
2447 | if (op != '+' && op != '-') | |
2448 | break; | |
2449 | next(); | |
376253ec | 2450 | val2 = expr_logic(mon); |
9307c4c1 FB |
2451 | if (op == '+') |
2452 | val += val2; | |
2453 | else | |
2454 | val -= val2; | |
2455 | } | |
2456 | return val; | |
2457 | } | |
2458 | ||
376253ec | 2459 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
9307c4c1 FB |
2460 | { |
2461 | pch = *pp; | |
2462 | if (setjmp(expr_env)) { | |
2463 | *pp = pch; | |
2464 | return -1; | |
2465 | } | |
cd390083 | 2466 | while (qemu_isspace(*pch)) |
9307c4c1 | 2467 | pch++; |
376253ec | 2468 | *pval = expr_sum(mon); |
9307c4c1 FB |
2469 | *pp = pch; |
2470 | return 0; | |
2471 | } | |
2472 | ||
2473 | static int get_str(char *buf, int buf_size, const char **pp) | |
2474 | { | |
2475 | const char *p; | |
2476 | char *q; | |
2477 | int c; | |
2478 | ||
81d0912d | 2479 | q = buf; |
9307c4c1 | 2480 | p = *pp; |
cd390083 | 2481 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2482 | p++; |
2483 | if (*p == '\0') { | |
2484 | fail: | |
81d0912d | 2485 | *q = '\0'; |
9307c4c1 FB |
2486 | *pp = p; |
2487 | return -1; | |
2488 | } | |
9307c4c1 FB |
2489 | if (*p == '\"') { |
2490 | p++; | |
2491 | while (*p != '\0' && *p != '\"') { | |
2492 | if (*p == '\\') { | |
2493 | p++; | |
2494 | c = *p++; | |
2495 | switch(c) { | |
2496 | case 'n': | |
2497 | c = '\n'; | |
2498 | break; | |
2499 | case 'r': | |
2500 | c = '\r'; | |
2501 | break; | |
2502 | case '\\': | |
2503 | case '\'': | |
2504 | case '\"': | |
2505 | break; | |
2506 | default: | |
2507 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
2508 | goto fail; | |
2509 | } | |
2510 | if ((q - buf) < buf_size - 1) { | |
2511 | *q++ = c; | |
2512 | } | |
2513 | } else { | |
2514 | if ((q - buf) < buf_size - 1) { | |
2515 | *q++ = *p; | |
2516 | } | |
2517 | p++; | |
2518 | } | |
2519 | } | |
2520 | if (*p != '\"') { | |
5b60212f | 2521 | qemu_printf("unterminated string\n"); |
9307c4c1 FB |
2522 | goto fail; |
2523 | } | |
2524 | p++; | |
2525 | } else { | |
cd390083 | 2526 | while (*p != '\0' && !qemu_isspace(*p)) { |
9307c4c1 FB |
2527 | if ((q - buf) < buf_size - 1) { |
2528 | *q++ = *p; | |
2529 | } | |
2530 | p++; | |
2531 | } | |
9307c4c1 | 2532 | } |
81d0912d | 2533 | *q = '\0'; |
9307c4c1 FB |
2534 | *pp = p; |
2535 | return 0; | |
2536 | } | |
2537 | ||
4590fd80 LC |
2538 | /* |
2539 | * Store the command-name in cmdname, and return a pointer to | |
2540 | * the remaining of the command string. | |
2541 | */ | |
2542 | static const char *get_command_name(const char *cmdline, | |
2543 | char *cmdname, size_t nlen) | |
2544 | { | |
2545 | size_t len; | |
2546 | const char *p, *pstart; | |
2547 | ||
2548 | p = cmdline; | |
2549 | while (qemu_isspace(*p)) | |
2550 | p++; | |
2551 | if (*p == '\0') | |
2552 | return NULL; | |
2553 | pstart = p; | |
2554 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | |
2555 | p++; | |
2556 | len = p - pstart; | |
2557 | if (len > nlen - 1) | |
2558 | len = nlen - 1; | |
2559 | memcpy(cmdname, pstart, len); | |
2560 | cmdname[len] = '\0'; | |
2561 | return p; | |
2562 | } | |
2563 | ||
4d76d2ba LC |
2564 | /** |
2565 | * Read key of 'type' into 'key' and return the current | |
2566 | * 'type' pointer. | |
2567 | */ | |
2568 | static char *key_get_info(const char *type, char **key) | |
2569 | { | |
2570 | size_t len; | |
2571 | char *p, *str; | |
2572 | ||
2573 | if (*type == ',') | |
2574 | type++; | |
2575 | ||
2576 | p = strchr(type, ':'); | |
2577 | if (!p) { | |
2578 | *key = NULL; | |
2579 | return NULL; | |
2580 | } | |
2581 | len = p - type; | |
2582 | ||
2583 | str = qemu_malloc(len + 1); | |
2584 | memcpy(str, type, len); | |
2585 | str[len] = '\0'; | |
2586 | ||
2587 | *key = str; | |
2588 | return ++p; | |
2589 | } | |
2590 | ||
9307c4c1 FB |
2591 | static int default_fmt_format = 'x'; |
2592 | static int default_fmt_size = 4; | |
2593 | ||
2594 | #define MAX_ARGS 16 | |
2595 | ||
376253ec | 2596 | static void monitor_handle_command(Monitor *mon, const char *cmdline) |
9307c4c1 | 2597 | { |
4590fd80 LC |
2598 | const char *p, *typestr; |
2599 | int c, nb_args, i, has_arg; | |
376253ec | 2600 | const mon_cmd_t *cmd; |
9307c4c1 FB |
2601 | char cmdname[256]; |
2602 | char buf[1024]; | |
4d76d2ba | 2603 | char *key; |
f7188bbe | 2604 | QDict *qdict; |
9307c4c1 FB |
2605 | void *str_allocated[MAX_ARGS]; |
2606 | void *args[MAX_ARGS]; | |
f96fc8a0 | 2607 | void (*handler_d)(Monitor *mon, const QDict *qdict); |
79c4f6b0 HY |
2608 | void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2, |
2609 | void *arg3, void *arg4, void *arg5, void *arg6, | |
2610 | void *arg7, void *arg8, void *arg9); | |
9dc39cba FB |
2611 | |
2612 | #ifdef DEBUG | |
376253ec | 2613 | monitor_printf(mon, "command='%s'\n", cmdline); |
9dc39cba | 2614 | #endif |
3b46e624 | 2615 | |
9307c4c1 | 2616 | /* extract the command name */ |
4590fd80 LC |
2617 | p = get_command_name(cmdline, cmdname, sizeof(cmdname)); |
2618 | if (!p) | |
9307c4c1 | 2619 | return; |
3b46e624 | 2620 | |
9307c4c1 | 2621 | /* find the command */ |
376253ec | 2622 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
5fafdf24 | 2623 | if (compare_cmd(cmdname, cmd->name)) |
d91d9bf6 LC |
2624 | break; |
2625 | } | |
2626 | ||
2627 | if (cmd->name == NULL) { | |
2628 | monitor_printf(mon, "unknown command: '%s'\n", cmdname); | |
2629 | return; | |
9307c4c1 | 2630 | } |
9307c4c1 | 2631 | |
f7188bbe LC |
2632 | qdict = qdict_new(); |
2633 | ||
9307c4c1 FB |
2634 | for(i = 0; i < MAX_ARGS; i++) |
2635 | str_allocated[i] = NULL; | |
3b46e624 | 2636 | |
9307c4c1 FB |
2637 | /* parse the parameters */ |
2638 | typestr = cmd->args_type; | |
2639 | nb_args = 0; | |
9dc39cba | 2640 | for(;;) { |
4d76d2ba LC |
2641 | typestr = key_get_info(typestr, &key); |
2642 | if (!typestr) | |
9dc39cba | 2643 | break; |
4d76d2ba | 2644 | c = *typestr; |
9307c4c1 FB |
2645 | typestr++; |
2646 | switch(c) { | |
2647 | case 'F': | |
81d0912d | 2648 | case 'B': |
9307c4c1 FB |
2649 | case 's': |
2650 | { | |
2651 | int ret; | |
2652 | char *str; | |
3b46e624 | 2653 | |
cd390083 | 2654 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2655 | p++; |
2656 | if (*typestr == '?') { | |
2657 | typestr++; | |
2658 | if (*p == '\0') { | |
2659 | /* no optional string: NULL argument */ | |
2660 | str = NULL; | |
2661 | goto add_str; | |
2662 | } | |
2663 | } | |
2664 | ret = get_str(buf, sizeof(buf), &p); | |
2665 | if (ret < 0) { | |
81d0912d FB |
2666 | switch(c) { |
2667 | case 'F': | |
376253ec AL |
2668 | monitor_printf(mon, "%s: filename expected\n", |
2669 | cmdname); | |
81d0912d FB |
2670 | break; |
2671 | case 'B': | |
376253ec AL |
2672 | monitor_printf(mon, "%s: block device name expected\n", |
2673 | cmdname); | |
81d0912d FB |
2674 | break; |
2675 | default: | |
376253ec | 2676 | monitor_printf(mon, "%s: string expected\n", cmdname); |
81d0912d FB |
2677 | break; |
2678 | } | |
9307c4c1 FB |
2679 | goto fail; |
2680 | } | |
2681 | str = qemu_malloc(strlen(buf) + 1); | |
363a37d5 | 2682 | pstrcpy(str, sizeof(buf), buf); |
9307c4c1 FB |
2683 | str_allocated[nb_args] = str; |
2684 | add_str: | |
2685 | if (nb_args >= MAX_ARGS) { | |
2686 | error_args: | |
376253ec | 2687 | monitor_printf(mon, "%s: too many arguments\n", cmdname); |
9307c4c1 FB |
2688 | goto fail; |
2689 | } | |
2690 | args[nb_args++] = str; | |
f7188bbe LC |
2691 | if (str) |
2692 | qdict_put(qdict, key, qstring_from_str(str)); | |
9307c4c1 | 2693 | } |
9dc39cba | 2694 | break; |
9307c4c1 FB |
2695 | case '/': |
2696 | { | |
2697 | int count, format, size; | |
3b46e624 | 2698 | |
cd390083 | 2699 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2700 | p++; |
2701 | if (*p == '/') { | |
2702 | /* format found */ | |
2703 | p++; | |
2704 | count = 1; | |
cd390083 | 2705 | if (qemu_isdigit(*p)) { |
9307c4c1 | 2706 | count = 0; |
cd390083 | 2707 | while (qemu_isdigit(*p)) { |
9307c4c1 FB |
2708 | count = count * 10 + (*p - '0'); |
2709 | p++; | |
2710 | } | |
2711 | } | |
2712 | size = -1; | |
2713 | format = -1; | |
2714 | for(;;) { | |
2715 | switch(*p) { | |
2716 | case 'o': | |
2717 | case 'd': | |
2718 | case 'u': | |
2719 | case 'x': | |
2720 | case 'i': | |
2721 | case 'c': | |
2722 | format = *p++; | |
2723 | break; | |
2724 | case 'b': | |
2725 | size = 1; | |
2726 | p++; | |
2727 | break; | |
2728 | case 'h': | |
2729 | size = 2; | |
2730 | p++; | |
2731 | break; | |
2732 | case 'w': | |
2733 | size = 4; | |
2734 | p++; | |
2735 | break; | |
2736 | case 'g': | |
2737 | case 'L': | |
2738 | size = 8; | |
2739 | p++; | |
2740 | break; | |
2741 | default: | |
2742 | goto next; | |
2743 | } | |
2744 | } | |
2745 | next: | |
cd390083 | 2746 | if (*p != '\0' && !qemu_isspace(*p)) { |
376253ec AL |
2747 | monitor_printf(mon, "invalid char in format: '%c'\n", |
2748 | *p); | |
9307c4c1 FB |
2749 | goto fail; |
2750 | } | |
9307c4c1 FB |
2751 | if (format < 0) |
2752 | format = default_fmt_format; | |
4c27ba27 FB |
2753 | if (format != 'i') { |
2754 | /* for 'i', not specifying a size gives -1 as size */ | |
2755 | if (size < 0) | |
2756 | size = default_fmt_size; | |
e90f009b | 2757 | default_fmt_size = size; |
4c27ba27 | 2758 | } |
9307c4c1 FB |
2759 | default_fmt_format = format; |
2760 | } else { | |
2761 | count = 1; | |
2762 | format = default_fmt_format; | |
4c27ba27 FB |
2763 | if (format != 'i') { |
2764 | size = default_fmt_size; | |
2765 | } else { | |
2766 | size = -1; | |
2767 | } | |
9307c4c1 FB |
2768 | } |
2769 | if (nb_args + 3 > MAX_ARGS) | |
2770 | goto error_args; | |
1c5bf3bf JM |
2771 | args[nb_args++] = (void*)(long)count; |
2772 | args[nb_args++] = (void*)(long)format; | |
2773 | args[nb_args++] = (void*)(long)size; | |
f7188bbe LC |
2774 | qdict_put(qdict, "count", qint_from_int(count)); |
2775 | qdict_put(qdict, "format", qint_from_int(format)); | |
2776 | qdict_put(qdict, "size", qint_from_int(size)); | |
9307c4c1 | 2777 | } |
9dc39cba | 2778 | break; |
9307c4c1 | 2779 | case 'i': |
92a31b1f | 2780 | case 'l': |
9307c4c1 | 2781 | { |
c2efc95d | 2782 | int64_t val; |
f7188bbe | 2783 | int dict_add = 1; |
7743e588 | 2784 | |
cd390083 | 2785 | while (qemu_isspace(*p)) |
9307c4c1 | 2786 | p++; |
3440557b | 2787 | if (*typestr == '?' || *typestr == '.') { |
3440557b FB |
2788 | if (*typestr == '?') { |
2789 | if (*p == '\0') | |
2790 | has_arg = 0; | |
2791 | else | |
2792 | has_arg = 1; | |
2793 | } else { | |
2794 | if (*p == '.') { | |
2795 | p++; | |
cd390083 | 2796 | while (qemu_isspace(*p)) |
3440557b FB |
2797 | p++; |
2798 | has_arg = 1; | |
2799 | } else { | |
2800 | has_arg = 0; | |
2801 | } | |
2802 | } | |
13224a87 | 2803 | typestr++; |
9307c4c1 FB |
2804 | if (nb_args >= MAX_ARGS) |
2805 | goto error_args; | |
f7188bbe | 2806 | dict_add = has_arg; |
1c5bf3bf | 2807 | args[nb_args++] = (void *)(long)has_arg; |
9307c4c1 FB |
2808 | if (!has_arg) { |
2809 | if (nb_args >= MAX_ARGS) | |
2810 | goto error_args; | |
2811 | val = -1; | |
2812 | goto add_num; | |
2813 | } | |
2814 | } | |
376253ec | 2815 | if (get_expr(mon, &val, &p)) |
9307c4c1 FB |
2816 | goto fail; |
2817 | add_num: | |
92a31b1f FB |
2818 | if (c == 'i') { |
2819 | if (nb_args >= MAX_ARGS) | |
2820 | goto error_args; | |
1c5bf3bf | 2821 | args[nb_args++] = (void *)(long)val; |
f7188bbe LC |
2822 | if (dict_add) |
2823 | qdict_put(qdict, key, qint_from_int(val)); | |
92a31b1f FB |
2824 | } else { |
2825 | if ((nb_args + 1) >= MAX_ARGS) | |
2826 | goto error_args; | |
7743e588 | 2827 | #if TARGET_PHYS_ADDR_BITS > 32 |
1c5bf3bf | 2828 | args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff); |
92a31b1f FB |
2829 | #else |
2830 | args[nb_args++] = (void *)0; | |
2831 | #endif | |
1c5bf3bf | 2832 | args[nb_args++] = (void *)(long)(val & 0xffffffff); |
f7188bbe | 2833 | qdict_put(qdict, key, qint_from_int(val)); |
92a31b1f | 2834 | } |
9307c4c1 FB |
2835 | } |
2836 | break; | |
2837 | case '-': | |
2838 | { | |
2839 | int has_option; | |
2840 | /* option */ | |
3b46e624 | 2841 | |
9307c4c1 FB |
2842 | c = *typestr++; |
2843 | if (c == '\0') | |
2844 | goto bad_type; | |
cd390083 | 2845 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2846 | p++; |
2847 | has_option = 0; | |
2848 | if (*p == '-') { | |
2849 | p++; | |
2850 | if (*p != c) { | |
376253ec AL |
2851 | monitor_printf(mon, "%s: unsupported option -%c\n", |
2852 | cmdname, *p); | |
9307c4c1 FB |
2853 | goto fail; |
2854 | } | |
2855 | p++; | |
2856 | has_option = 1; | |
2857 | } | |
2858 | if (nb_args >= MAX_ARGS) | |
2859 | goto error_args; | |
1c5bf3bf | 2860 | args[nb_args++] = (void *)(long)has_option; |
f7188bbe | 2861 | qdict_put(qdict, key, qint_from_int(has_option)); |
9307c4c1 FB |
2862 | } |
2863 | break; | |
2864 | default: | |
2865 | bad_type: | |
376253ec | 2866 | monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c); |
9307c4c1 FB |
2867 | goto fail; |
2868 | } | |
4d76d2ba LC |
2869 | qemu_free(key); |
2870 | key = NULL; | |
9dc39cba | 2871 | } |
9307c4c1 | 2872 | /* check that all arguments were parsed */ |
cd390083 | 2873 | while (qemu_isspace(*p)) |
9307c4c1 FB |
2874 | p++; |
2875 | if (*p != '\0') { | |
376253ec AL |
2876 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
2877 | cmdname); | |
9307c4c1 | 2878 | goto fail; |
9dc39cba | 2879 | } |
9307c4c1 | 2880 | |
ac7531ec | 2881 | qemu_errors_to_mon(mon); |
9307c4c1 FB |
2882 | switch(nb_args) { |
2883 | case 0: | |
d54908a5 | 2884 | case 1: |
f18c16de | 2885 | case 2: |
1d4daa91 | 2886 | case 3: |
afe67ef2 | 2887 | case 4: |
1bd1442e | 2888 | case 5: |
aa93e39c | 2889 | case 6: |
c1925484 | 2890 | case 7: |
f96fc8a0 LC |
2891 | handler_d = cmd->handler; |
2892 | handler_d(mon, qdict); | |
9307c4c1 | 2893 | break; |
79c4f6b0 HY |
2894 | case 10: |
2895 | handler_10 = cmd->handler; | |
2896 | handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5], | |
2897 | args[6], args[7], args[8], args[9]); | |
2898 | break; | |
9307c4c1 | 2899 | default: |
376253ec | 2900 | monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args); |
ac7531ec | 2901 | break; |
9dc39cba | 2902 | } |
ac7531ec GH |
2903 | qemu_errors_to_previous(); |
2904 | ||
9307c4c1 | 2905 | fail: |
4d76d2ba | 2906 | qemu_free(key); |
9307c4c1 FB |
2907 | for(i = 0; i < MAX_ARGS; i++) |
2908 | qemu_free(str_allocated[i]); | |
f7188bbe | 2909 | QDECREF(qdict); |
9dc39cba FB |
2910 | } |
2911 | ||
81d0912d FB |
2912 | static void cmd_completion(const char *name, const char *list) |
2913 | { | |
2914 | const char *p, *pstart; | |
2915 | char cmd[128]; | |
2916 | int len; | |
2917 | ||
2918 | p = list; | |
2919 | for(;;) { | |
2920 | pstart = p; | |
2921 | p = strchr(p, '|'); | |
2922 | if (!p) | |
2923 | p = pstart + strlen(pstart); | |
2924 | len = p - pstart; | |
2925 | if (len > sizeof(cmd) - 2) | |
2926 | len = sizeof(cmd) - 2; | |
2927 | memcpy(cmd, pstart, len); | |
2928 | cmd[len] = '\0'; | |
2929 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
731b0364 | 2930 | readline_add_completion(cur_mon->rs, cmd); |
81d0912d FB |
2931 | } |
2932 | if (*p == '\0') | |
2933 | break; | |
2934 | p++; | |
2935 | } | |
2936 | } | |
2937 | ||
2938 | static void file_completion(const char *input) | |
2939 | { | |
2940 | DIR *ffs; | |
2941 | struct dirent *d; | |
2942 | char path[1024]; | |
2943 | char file[1024], file_prefix[1024]; | |
2944 | int input_path_len; | |
2945 | const char *p; | |
2946 | ||
5fafdf24 | 2947 | p = strrchr(input, '/'); |
81d0912d FB |
2948 | if (!p) { |
2949 | input_path_len = 0; | |
2950 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 2951 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
2952 | } else { |
2953 | input_path_len = p - input + 1; | |
2954 | memcpy(path, input, input_path_len); | |
2955 | if (input_path_len > sizeof(path) - 1) | |
2956 | input_path_len = sizeof(path) - 1; | |
2957 | path[input_path_len] = '\0'; | |
2958 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
2959 | } | |
2960 | #ifdef DEBUG_COMPLETION | |
376253ec AL |
2961 | monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n", |
2962 | input, path, file_prefix); | |
81d0912d FB |
2963 | #endif |
2964 | ffs = opendir(path); | |
2965 | if (!ffs) | |
2966 | return; | |
2967 | for(;;) { | |
2968 | struct stat sb; | |
2969 | d = readdir(ffs); | |
2970 | if (!d) | |
2971 | break; | |
2972 | if (strstart(d->d_name, file_prefix, NULL)) { | |
2973 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
2974 | if (input_path_len < sizeof(file)) |
2975 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
2976 | d->d_name); | |
81d0912d FB |
2977 | /* stat the file to find out if it's a directory. |
2978 | * In that case add a slash to speed up typing long paths | |
2979 | */ | |
2980 | stat(file, &sb); | |
2981 | if(S_ISDIR(sb.st_mode)) | |
363a37d5 | 2982 | pstrcat(file, sizeof(file), "/"); |
731b0364 | 2983 | readline_add_completion(cur_mon->rs, file); |
81d0912d FB |
2984 | } |
2985 | } | |
2986 | closedir(ffs); | |
2987 | } | |
2988 | ||
51de9760 | 2989 | static void block_completion_it(void *opaque, BlockDriverState *bs) |
81d0912d | 2990 | { |
51de9760 | 2991 | const char *name = bdrv_get_device_name(bs); |
81d0912d FB |
2992 | const char *input = opaque; |
2993 | ||
2994 | if (input[0] == '\0' || | |
2995 | !strncmp(name, (char *)input, strlen(input))) { | |
731b0364 | 2996 | readline_add_completion(cur_mon->rs, name); |
81d0912d FB |
2997 | } |
2998 | } | |
2999 | ||
3000 | /* NOTE: this parser is an approximate form of the real command parser */ | |
3001 | static void parse_cmdline(const char *cmdline, | |
3002 | int *pnb_args, char **args) | |
3003 | { | |
3004 | const char *p; | |
3005 | int nb_args, ret; | |
3006 | char buf[1024]; | |
3007 | ||
3008 | p = cmdline; | |
3009 | nb_args = 0; | |
3010 | for(;;) { | |
cd390083 | 3011 | while (qemu_isspace(*p)) |
81d0912d FB |
3012 | p++; |
3013 | if (*p == '\0') | |
3014 | break; | |
3015 | if (nb_args >= MAX_ARGS) | |
3016 | break; | |
3017 | ret = get_str(buf, sizeof(buf), &p); | |
3018 | args[nb_args] = qemu_strdup(buf); | |
3019 | nb_args++; | |
3020 | if (ret < 0) | |
3021 | break; | |
3022 | } | |
3023 | *pnb_args = nb_args; | |
3024 | } | |
3025 | ||
4d76d2ba LC |
3026 | static const char *next_arg_type(const char *typestr) |
3027 | { | |
3028 | const char *p = strchr(typestr, ':'); | |
3029 | return (p != NULL ? ++p : typestr); | |
3030 | } | |
3031 | ||
4c36ba32 | 3032 | static void monitor_find_completion(const char *cmdline) |
81d0912d FB |
3033 | { |
3034 | const char *cmdname; | |
3035 | char *args[MAX_ARGS]; | |
3036 | int nb_args, i, len; | |
3037 | const char *ptype, *str; | |
376253ec | 3038 | const mon_cmd_t *cmd; |
64866c3d | 3039 | const KeyDef *key; |
81d0912d FB |
3040 | |
3041 | parse_cmdline(cmdline, &nb_args, args); | |
3042 | #ifdef DEBUG_COMPLETION | |
3043 | for(i = 0; i < nb_args; i++) { | |
376253ec | 3044 | monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]); |
81d0912d FB |
3045 | } |
3046 | #endif | |
3047 | ||
3048 | /* if the line ends with a space, it means we want to complete the | |
3049 | next arg */ | |
3050 | len = strlen(cmdline); | |
cd390083 | 3051 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { |
81d0912d FB |
3052 | if (nb_args >= MAX_ARGS) |
3053 | return; | |
3054 | args[nb_args++] = qemu_strdup(""); | |
3055 | } | |
3056 | if (nb_args <= 1) { | |
3057 | /* command completion */ | |
3058 | if (nb_args == 0) | |
3059 | cmdname = ""; | |
3060 | else | |
3061 | cmdname = args[0]; | |
731b0364 | 3062 | readline_set_completion_index(cur_mon->rs, strlen(cmdname)); |
376253ec | 3063 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
3064 | cmd_completion(cmdname, cmd->name); |
3065 | } | |
3066 | } else { | |
3067 | /* find the command */ | |
376253ec | 3068 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
3069 | if (compare_cmd(args[0], cmd->name)) |
3070 | goto found; | |
3071 | } | |
3072 | return; | |
3073 | found: | |
4d76d2ba | 3074 | ptype = next_arg_type(cmd->args_type); |
81d0912d FB |
3075 | for(i = 0; i < nb_args - 2; i++) { |
3076 | if (*ptype != '\0') { | |
4d76d2ba | 3077 | ptype = next_arg_type(ptype); |
81d0912d | 3078 | while (*ptype == '?') |
4d76d2ba | 3079 | ptype = next_arg_type(ptype); |
81d0912d FB |
3080 | } |
3081 | } | |
3082 | str = args[nb_args - 1]; | |
2a1704a7 BS |
3083 | if (*ptype == '-' && ptype[1] != '\0') { |
3084 | ptype += 2; | |
3085 | } | |
81d0912d FB |
3086 | switch(*ptype) { |
3087 | case 'F': | |
3088 | /* file completion */ | |
731b0364 | 3089 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3090 | file_completion(str); |
3091 | break; | |
3092 | case 'B': | |
3093 | /* block device name completion */ | |
731b0364 | 3094 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3095 | bdrv_iterate(block_completion_it, (void *)str); |
3096 | break; | |
7fe48483 FB |
3097 | case 's': |
3098 | /* XXX: more generic ? */ | |
3099 | if (!strcmp(cmd->name, "info")) { | |
731b0364 | 3100 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
7fe48483 FB |
3101 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
3102 | cmd_completion(str, cmd->name); | |
3103 | } | |
64866c3d | 3104 | } else if (!strcmp(cmd->name, "sendkey")) { |
e600d1ef BS |
3105 | char *sep = strrchr(str, '-'); |
3106 | if (sep) | |
3107 | str = sep + 1; | |
731b0364 | 3108 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
64866c3d FB |
3109 | for(key = key_defs; key->name != NULL; key++) { |
3110 | cmd_completion(str, key->name); | |
3111 | } | |
f3353c6b JK |
3112 | } else if (!strcmp(cmd->name, "help|?")) { |
3113 | readline_set_completion_index(cur_mon->rs, strlen(str)); | |
3114 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
3115 | cmd_completion(str, cmd->name); | |
3116 | } | |
7fe48483 FB |
3117 | } |
3118 | break; | |
81d0912d FB |
3119 | default: |
3120 | break; | |
3121 | } | |
3122 | } | |
3123 | for(i = 0; i < nb_args; i++) | |
3124 | qemu_free(args[i]); | |
3125 | } | |
3126 | ||
731b0364 | 3127 | static int monitor_can_read(void *opaque) |
9dc39cba | 3128 | { |
731b0364 AL |
3129 | Monitor *mon = opaque; |
3130 | ||
3131 | return (mon->suspend_cnt == 0) ? 128 : 0; | |
9dc39cba FB |
3132 | } |
3133 | ||
731b0364 | 3134 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 3135 | { |
731b0364 | 3136 | Monitor *old_mon = cur_mon; |
7e2515e8 | 3137 | int i; |
376253ec | 3138 | |
731b0364 AL |
3139 | cur_mon = opaque; |
3140 | ||
cde76ee1 AL |
3141 | if (cur_mon->rs) { |
3142 | for (i = 0; i < size; i++) | |
3143 | readline_handle_byte(cur_mon->rs, buf[i]); | |
3144 | } else { | |
3145 | if (size == 0 || buf[size - 1] != 0) | |
3146 | monitor_printf(cur_mon, "corrupted command\n"); | |
3147 | else | |
3148 | monitor_handle_command(cur_mon, (char *)buf); | |
3149 | } | |
9dc39cba | 3150 | |
731b0364 AL |
3151 | cur_mon = old_mon; |
3152 | } | |
d8f44609 | 3153 | |
376253ec | 3154 | static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque) |
aa455485 | 3155 | { |
731b0364 | 3156 | monitor_suspend(mon); |
376253ec | 3157 | monitor_handle_command(mon, cmdline); |
731b0364 | 3158 | monitor_resume(mon); |
d8f44609 AL |
3159 | } |
3160 | ||
cde76ee1 | 3161 | int monitor_suspend(Monitor *mon) |
d8f44609 | 3162 | { |
cde76ee1 AL |
3163 | if (!mon->rs) |
3164 | return -ENOTTY; | |
731b0364 | 3165 | mon->suspend_cnt++; |
cde76ee1 | 3166 | return 0; |
d8f44609 AL |
3167 | } |
3168 | ||
376253ec | 3169 | void monitor_resume(Monitor *mon) |
d8f44609 | 3170 | { |
cde76ee1 AL |
3171 | if (!mon->rs) |
3172 | return; | |
731b0364 AL |
3173 | if (--mon->suspend_cnt == 0) |
3174 | readline_show_prompt(mon->rs); | |
aa455485 FB |
3175 | } |
3176 | ||
731b0364 | 3177 | static void monitor_event(void *opaque, int event) |
86e94dea | 3178 | { |
376253ec AL |
3179 | Monitor *mon = opaque; |
3180 | ||
2724b180 AL |
3181 | switch (event) { |
3182 | case CHR_EVENT_MUX_IN: | |
3183 | readline_restart(mon->rs); | |
3184 | monitor_resume(mon); | |
3185 | monitor_flush(mon); | |
3186 | break; | |
3187 | ||
3188 | case CHR_EVENT_MUX_OUT: | |
3189 | if (mon->suspend_cnt == 0) | |
3190 | monitor_printf(mon, "\n"); | |
3191 | monitor_flush(mon); | |
3192 | monitor_suspend(mon); | |
3193 | break; | |
86e94dea | 3194 | |
2724b180 AL |
3195 | case CHR_EVENT_RESET: |
3196 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " | |
3197 | "information\n", QEMU_VERSION); | |
3198 | if (mon->chr->focus == 0) | |
3199 | readline_show_prompt(mon->rs); | |
3200 | break; | |
3201 | } | |
86e94dea TS |
3202 | } |
3203 | ||
76655d6d AL |
3204 | |
3205 | /* | |
3206 | * Local variables: | |
3207 | * c-indent-level: 4 | |
3208 | * c-basic-offset: 4 | |
3209 | * tab-width: 8 | |
3210 | * End: | |
3211 | */ | |
3212 | ||
731b0364 | 3213 | void monitor_init(CharDriverState *chr, int flags) |
aa455485 | 3214 | { |
731b0364 | 3215 | static int is_first_init = 1; |
87127161 | 3216 | Monitor *mon; |
20d8a3ed TS |
3217 | |
3218 | if (is_first_init) { | |
c8256f9d | 3219 | key_timer = qemu_new_timer(vm_clock, release_keys, NULL); |
20d8a3ed TS |
3220 | is_first_init = 0; |
3221 | } | |
87127161 AL |
3222 | |
3223 | mon = qemu_mallocz(sizeof(*mon)); | |
20d8a3ed | 3224 | |
87127161 | 3225 | mon->chr = chr; |
731b0364 | 3226 | mon->flags = flags; |
2724b180 AL |
3227 | if (mon->chr->focus != 0) |
3228 | mon->suspend_cnt = 1; /* mux'ed monitors start suspended */ | |
cde76ee1 AL |
3229 | if (flags & MONITOR_USE_READLINE) { |
3230 | mon->rs = readline_init(mon, monitor_find_completion); | |
3231 | monitor_read_command(mon, 0); | |
3232 | } | |
87127161 | 3233 | |
731b0364 AL |
3234 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event, |
3235 | mon); | |
87127161 AL |
3236 | |
3237 | LIST_INSERT_HEAD(&mon_list, mon, entry); | |
731b0364 | 3238 | if (!cur_mon || (flags & MONITOR_IS_DEFAULT)) |
87127161 | 3239 | cur_mon = mon; |
aa455485 FB |
3240 | } |
3241 | ||
376253ec | 3242 | static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque) |
81d0912d | 3243 | { |
bb5fc20f AL |
3244 | BlockDriverState *bs = opaque; |
3245 | int ret = 0; | |
81d0912d | 3246 | |
bb5fc20f | 3247 | if (bdrv_set_key(bs, password) != 0) { |
376253ec | 3248 | monitor_printf(mon, "invalid password\n"); |
bb5fc20f | 3249 | ret = -EPERM; |
9dc39cba | 3250 | } |
731b0364 AL |
3251 | if (mon->password_completion_cb) |
3252 | mon->password_completion_cb(mon->password_opaque, ret); | |
bb5fc20f | 3253 | |
731b0364 | 3254 | monitor_read_command(mon, 1); |
9dc39cba | 3255 | } |
c0f4ce77 | 3256 | |
376253ec | 3257 | void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs, |
bb5fc20f AL |
3258 | BlockDriverCompletionFunc *completion_cb, |
3259 | void *opaque) | |
c0f4ce77 | 3260 | { |
cde76ee1 AL |
3261 | int err; |
3262 | ||
bb5fc20f AL |
3263 | if (!bdrv_key_required(bs)) { |
3264 | if (completion_cb) | |
3265 | completion_cb(opaque, 0); | |
3266 | return; | |
3267 | } | |
c0f4ce77 | 3268 | |
376253ec AL |
3269 | monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs), |
3270 | bdrv_get_encrypted_filename(bs)); | |
bb5fc20f | 3271 | |
731b0364 AL |
3272 | mon->password_completion_cb = completion_cb; |
3273 | mon->password_opaque = opaque; | |
bb5fc20f | 3274 | |
cde76ee1 AL |
3275 | err = monitor_read_password(mon, bdrv_password_cb, bs); |
3276 | ||
3277 | if (err && completion_cb) | |
3278 | completion_cb(opaque, err); | |
c0f4ce77 | 3279 | } |
ac7531ec GH |
3280 | |
3281 | typedef struct QemuErrorSink QemuErrorSink; | |
3282 | struct QemuErrorSink { | |
3283 | enum { | |
3284 | ERR_SINK_FILE, | |
3285 | ERR_SINK_MONITOR, | |
3286 | } dest; | |
3287 | union { | |
3288 | FILE *fp; | |
3289 | Monitor *mon; | |
3290 | }; | |
3291 | QemuErrorSink *previous; | |
3292 | }; | |
3293 | ||
528e93a9 | 3294 | static QemuErrorSink *qemu_error_sink; |
ac7531ec GH |
3295 | |
3296 | void qemu_errors_to_file(FILE *fp) | |
3297 | { | |
3298 | QemuErrorSink *sink; | |
3299 | ||
3300 | sink = qemu_mallocz(sizeof(*sink)); | |
3301 | sink->dest = ERR_SINK_FILE; | |
3302 | sink->fp = fp; | |
3303 | sink->previous = qemu_error_sink; | |
3304 | qemu_error_sink = sink; | |
3305 | } | |
3306 | ||
3307 | void qemu_errors_to_mon(Monitor *mon) | |
3308 | { | |
3309 | QemuErrorSink *sink; | |
3310 | ||
3311 | sink = qemu_mallocz(sizeof(*sink)); | |
3312 | sink->dest = ERR_SINK_MONITOR; | |
3313 | sink->mon = mon; | |
3314 | sink->previous = qemu_error_sink; | |
3315 | qemu_error_sink = sink; | |
3316 | } | |
3317 | ||
3318 | void qemu_errors_to_previous(void) | |
3319 | { | |
3320 | QemuErrorSink *sink; | |
3321 | ||
3322 | assert(qemu_error_sink != NULL); | |
3323 | sink = qemu_error_sink; | |
3324 | qemu_error_sink = sink->previous; | |
3325 | qemu_free(sink); | |
3326 | } | |
3327 | ||
3328 | void qemu_error(const char *fmt, ...) | |
3329 | { | |
3330 | va_list args; | |
3331 | ||
3332 | assert(qemu_error_sink != NULL); | |
3333 | switch (qemu_error_sink->dest) { | |
3334 | case ERR_SINK_FILE: | |
3335 | va_start(args, fmt); | |
3336 | vfprintf(qemu_error_sink->fp, fmt, args); | |
3337 | va_end(args); | |
3338 | break; | |
3339 | case ERR_SINK_MONITOR: | |
3340 | va_start(args, fmt); | |
3341 | monitor_vprintf(qemu_error_sink->mon, fmt, args); | |
3342 | va_end(args); | |
3343 | break; | |
3344 | } | |
3345 | } |