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