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