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