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