]>
Commit | Line | Data |
---|---|---|
9dc39cba FB |
1 | /* |
2 | * QEMU monitor | |
5fafdf24 | 3 | * |
9dc39cba | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
9dc39cba FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
511d2b14 | 24 | #include <dirent.h> |
87ecb68b | 25 | #include "hw/hw.h" |
cae4956e | 26 | #include "hw/qdev.h" |
87ecb68b PB |
27 | #include "hw/usb.h" |
28 | #include "hw/pcmcia.h" | |
29 | #include "hw/pc.h" | |
30 | #include "hw/pci.h" | |
9dd986cc | 31 | #include "hw/watchdog.h" |
45a50b16 | 32 | #include "hw/loader.h" |
87ecb68b PB |
33 | #include "gdbstub.h" |
34 | #include "net.h" | |
68ac40d2 | 35 | #include "net/slirp.h" |
87ecb68b PB |
36 | #include "qemu-char.h" |
37 | #include "sysemu.h" | |
376253ec AL |
38 | #include "monitor.h" |
39 | #include "readline.h" | |
87ecb68b PB |
40 | #include "console.h" |
41 | #include "block.h" | |
42 | #include "audio/audio.h" | |
9307c4c1 | 43 | #include "disas.h" |
df751fa8 | 44 | #include "balloon.h" |
c8256f9d | 45 | #include "qemu-timer.h" |
5bb7910a | 46 | #include "migration.h" |
7ba1e619 | 47 | #include "kvm.h" |
76655d6d | 48 | #include "acl.h" |
f7188bbe | 49 | #include "qint.h" |
8f3cec0b | 50 | #include "qlist.h" |
f7188bbe | 51 | #include "qdict.h" |
5fa737a4 | 52 | #include "qbool.h" |
f7188bbe | 53 | #include "qstring.h" |
8204a918 | 54 | #include "qerror.h" |
9b57c02e | 55 | #include "qjson.h" |
5fa737a4 LC |
56 | #include "json-streamer.h" |
57 | #include "json-parser.h" | |
d08d6f04 | 58 | #include "osdep.h" |
6a5bd307 | 59 | |
9dc39cba | 60 | //#define DEBUG |
81d0912d | 61 | //#define DEBUG_COMPLETION |
9dc39cba | 62 | |
9307c4c1 FB |
63 | /* |
64 | * Supported types: | |
5fafdf24 | 65 | * |
9307c4c1 | 66 | * 'F' filename |
81d0912d | 67 | * 'B' block device name |
9307c4c1 | 68 | * 's' string (accept optional quote) |
92a31b1f FB |
69 | * 'i' 32 bit integer |
70 | * 'l' target long (32 or 64 bit) | |
9307c4c1 FB |
71 | * '/' optional gdb-like print format (like "/10x") |
72 | * | |
fb46660e LC |
73 | * '?' optional type (for all types, except '/') |
74 | * '.' other form of optional type (for 'i' and 'l') | |
75 | * '-' optional parameter (eg. '-f') | |
9307c4c1 FB |
76 | * |
77 | */ | |
78 | ||
c227f099 | 79 | typedef struct mon_cmd_t { |
9dc39cba | 80 | const char *name; |
9307c4c1 | 81 | const char *args_type; |
9dc39cba FB |
82 | const char *params; |
83 | const char *help; | |
a2876f59 | 84 | void (*user_print)(Monitor *mon, const QObject *data); |
910df89d LC |
85 | union { |
86 | void (*info)(Monitor *mon); | |
13c7425e | 87 | void (*info_new)(Monitor *mon, QObject **ret_data); |
af4ce882 | 88 | void (*cmd)(Monitor *mon, const QDict *qdict); |
13917bee | 89 | void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data); |
910df89d | 90 | } mhandler; |
c227f099 | 91 | } mon_cmd_t; |
9dc39cba | 92 | |
f07918fd | 93 | /* file descriptors passed via SCM_RIGHTS */ |
c227f099 AL |
94 | typedef struct mon_fd_t mon_fd_t; |
95 | struct mon_fd_t { | |
f07918fd MM |
96 | char *name; |
97 | int fd; | |
c227f099 | 98 | QLIST_ENTRY(mon_fd_t) next; |
f07918fd MM |
99 | }; |
100 | ||
5fa737a4 LC |
101 | typedef struct MonitorControl { |
102 | QObject *id; | |
4a29a85d | 103 | int print_enabled; |
5fa737a4 LC |
104 | JSONMessageParser parser; |
105 | } MonitorControl; | |
106 | ||
87127161 AL |
107 | struct Monitor { |
108 | CharDriverState *chr; | |
a7aec5da GH |
109 | int mux_out; |
110 | int reset_seen; | |
731b0364 AL |
111 | int flags; |
112 | int suspend_cnt; | |
113 | uint8_t outbuf[1024]; | |
114 | int outbuf_index; | |
115 | ReadLineState *rs; | |
5fa737a4 | 116 | MonitorControl *mc; |
731b0364 AL |
117 | CPUState *mon_cpu; |
118 | BlockDriverCompletionFunc *password_completion_cb; | |
119 | void *password_opaque; | |
8204a918 | 120 | QError *error; |
c227f099 | 121 | QLIST_HEAD(,mon_fd_t) fds; |
72cf2d4f | 122 | QLIST_ENTRY(Monitor) entry; |
87127161 AL |
123 | }; |
124 | ||
72cf2d4f | 125 | static QLIST_HEAD(mon_list, Monitor) mon_list; |
7e2515e8 | 126 | |
c227f099 AL |
127 | static const mon_cmd_t mon_cmds[]; |
128 | static const mon_cmd_t info_cmds[]; | |
9dc39cba | 129 | |
87127161 | 130 | Monitor *cur_mon = NULL; |
376253ec | 131 | |
731b0364 AL |
132 | static void monitor_command_cb(Monitor *mon, const char *cmdline, |
133 | void *opaque); | |
83ab7950 | 134 | |
418173c7 LC |
135 | /* Return true if in control mode, false otherwise */ |
136 | static inline int monitor_ctrl_mode(const Monitor *mon) | |
137 | { | |
138 | return (mon->flags & MONITOR_USE_CONTROL); | |
139 | } | |
140 | ||
731b0364 AL |
141 | static void monitor_read_command(Monitor *mon, int show_prompt) |
142 | { | |
143 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); | |
144 | if (show_prompt) | |
145 | readline_show_prompt(mon->rs); | |
146 | } | |
6a00d601 | 147 | |
cde76ee1 AL |
148 | static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
149 | void *opaque) | |
bb5fc20f | 150 | { |
94171e11 LC |
151 | if (monitor_ctrl_mode(mon)) { |
152 | qemu_error_new(QERR_MISSING_PARAMETER, "password"); | |
153 | return -EINVAL; | |
154 | } else if (mon->rs) { | |
cde76ee1 AL |
155 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); |
156 | /* prompt is printed on return from the command handler */ | |
157 | return 0; | |
158 | } else { | |
159 | monitor_printf(mon, "terminal does not support password prompting\n"); | |
160 | return -ENOTTY; | |
161 | } | |
bb5fc20f AL |
162 | } |
163 | ||
376253ec | 164 | void monitor_flush(Monitor *mon) |
7e2515e8 | 165 | { |
a7aec5da | 166 | if (mon && mon->outbuf_index != 0 && !mon->mux_out) { |
731b0364 AL |
167 | qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index); |
168 | mon->outbuf_index = 0; | |
7e2515e8 FB |
169 | } |
170 | } | |
171 | ||
172 | /* flush at every end of line or if the buffer is full */ | |
376253ec | 173 | static void monitor_puts(Monitor *mon, const char *str) |
7e2515e8 | 174 | { |
60fe76f3 | 175 | char c; |
731b0364 AL |
176 | |
177 | if (!mon) | |
178 | return; | |
179 | ||
7e2515e8 FB |
180 | for(;;) { |
181 | c = *str++; | |
182 | if (c == '\0') | |
183 | break; | |
7ba1260a | 184 | if (c == '\n') |
731b0364 AL |
185 | mon->outbuf[mon->outbuf_index++] = '\r'; |
186 | mon->outbuf[mon->outbuf_index++] = c; | |
187 | if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1) | |
188 | || c == '\n') | |
376253ec | 189 | monitor_flush(mon); |
7e2515e8 FB |
190 | } |
191 | } | |
192 | ||
376253ec | 193 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
9dc39cba | 194 | { |
4a29a85d LC |
195 | if (mon->mc && !mon->mc->print_enabled) { |
196 | qemu_error_new(QERR_UNDEFINED_ERROR); | |
197 | } else { | |
198 | char buf[4096]; | |
199 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
200 | monitor_puts(mon, buf); | |
201 | } | |
9dc39cba FB |
202 | } |
203 | ||
376253ec | 204 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
9dc39cba | 205 | { |
7e2515e8 FB |
206 | va_list ap; |
207 | va_start(ap, fmt); | |
376253ec | 208 | monitor_vprintf(mon, fmt, ap); |
7e2515e8 | 209 | va_end(ap); |
9dc39cba FB |
210 | } |
211 | ||
376253ec | 212 | void monitor_print_filename(Monitor *mon, const char *filename) |
fef30743 TS |
213 | { |
214 | int i; | |
215 | ||
216 | for (i = 0; filename[i]; i++) { | |
28a76be8 AL |
217 | switch (filename[i]) { |
218 | case ' ': | |
219 | case '"': | |
220 | case '\\': | |
221 | monitor_printf(mon, "\\%c", filename[i]); | |
222 | break; | |
223 | case '\t': | |
224 | monitor_printf(mon, "\\t"); | |
225 | break; | |
226 | case '\r': | |
227 | monitor_printf(mon, "\\r"); | |
228 | break; | |
229 | case '\n': | |
230 | monitor_printf(mon, "\\n"); | |
231 | break; | |
232 | default: | |
233 | monitor_printf(mon, "%c", filename[i]); | |
234 | break; | |
235 | } | |
fef30743 TS |
236 | } |
237 | } | |
238 | ||
7fe48483 FB |
239 | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
240 | { | |
241 | va_list ap; | |
242 | va_start(ap, fmt); | |
376253ec | 243 | monitor_vprintf((Monitor *)stream, fmt, ap); |
7fe48483 FB |
244 | va_end(ap); |
245 | return 0; | |
246 | } | |
247 | ||
13c7425e LC |
248 | static void monitor_user_noop(Monitor *mon, const QObject *data) { } |
249 | ||
13917bee LC |
250 | static inline int monitor_handler_ported(const mon_cmd_t *cmd) |
251 | { | |
252 | return cmd->user_print != NULL; | |
253 | } | |
254 | ||
8204a918 LC |
255 | static inline int monitor_has_error(const Monitor *mon) |
256 | { | |
257 | return mon->error != NULL; | |
258 | } | |
259 | ||
9b57c02e LC |
260 | static void monitor_json_emitter(Monitor *mon, const QObject *data) |
261 | { | |
262 | QString *json; | |
263 | ||
264 | json = qobject_to_json(data); | |
265 | assert(json != NULL); | |
266 | ||
4a29a85d | 267 | mon->mc->print_enabled = 1; |
9b57c02e | 268 | monitor_printf(mon, "%s\n", qstring_get_str(json)); |
4a29a85d LC |
269 | mon->mc->print_enabled = 0; |
270 | ||
9b57c02e LC |
271 | QDECREF(json); |
272 | } | |
273 | ||
25b422eb LC |
274 | static void monitor_protocol_emitter(Monitor *mon, QObject *data) |
275 | { | |
276 | QDict *qmp; | |
277 | ||
278 | qmp = qdict_new(); | |
279 | ||
280 | if (!monitor_has_error(mon)) { | |
281 | /* success response */ | |
282 | if (data) { | |
283 | qobject_incref(data); | |
284 | qdict_put_obj(qmp, "return", data); | |
285 | } else { | |
286 | qdict_put(qmp, "return", qstring_from_str("OK")); | |
287 | } | |
288 | } else { | |
289 | /* error response */ | |
77e595e7 | 290 | qdict_put(mon->error->error, "desc", qerror_human(mon->error)); |
25b422eb LC |
291 | qdict_put(qmp, "error", mon->error->error); |
292 | QINCREF(mon->error->error); | |
293 | QDECREF(mon->error); | |
294 | mon->error = NULL; | |
295 | } | |
296 | ||
5fa737a4 LC |
297 | if (mon->mc->id) { |
298 | qdict_put_obj(qmp, "id", mon->mc->id); | |
299 | mon->mc->id = NULL; | |
300 | } | |
301 | ||
25b422eb LC |
302 | monitor_json_emitter(mon, QOBJECT(qmp)); |
303 | QDECREF(qmp); | |
304 | } | |
305 | ||
0d1ea871 LC |
306 | static void timestamp_put(QDict *qdict) |
307 | { | |
308 | int err; | |
309 | QObject *obj; | |
d08d6f04 | 310 | qemu_timeval tv; |
0d1ea871 | 311 | |
d08d6f04 | 312 | err = qemu_gettimeofday(&tv); |
0d1ea871 LC |
313 | if (err < 0) |
314 | return; | |
315 | ||
316 | obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", " | |
317 | "'microseconds': %" PRId64 " }", | |
318 | (int64_t) tv.tv_sec, (int64_t) tv.tv_usec); | |
319 | assert(obj != NULL); | |
320 | ||
321 | qdict_put_obj(qdict, "timestamp", obj); | |
322 | } | |
323 | ||
324 | /** | |
325 | * monitor_protocol_event(): Generate a Monitor event | |
326 | * | |
327 | * Event-specific data can be emitted through the (optional) 'data' parameter. | |
328 | */ | |
329 | void monitor_protocol_event(MonitorEvent event, QObject *data) | |
330 | { | |
331 | QDict *qmp; | |
332 | const char *event_name; | |
333 | Monitor *mon = cur_mon; | |
334 | ||
242cd003 | 335 | assert(event < QEVENT_MAX); |
0d1ea871 LC |
336 | |
337 | if (!monitor_ctrl_mode(mon)) | |
338 | return; | |
339 | ||
340 | switch (event) { | |
242cd003 | 341 | case QEVENT_DEBUG: |
b1a15e7e LC |
342 | event_name = "DEBUG"; |
343 | break; | |
242cd003 | 344 | case QEVENT_SHUTDOWN: |
b1a15e7e LC |
345 | event_name = "SHUTDOWN"; |
346 | break; | |
242cd003 | 347 | case QEVENT_RESET: |
b1a15e7e LC |
348 | event_name = "RESET"; |
349 | break; | |
242cd003 | 350 | case QEVENT_POWERDOWN: |
b1a15e7e LC |
351 | event_name = "POWERDOWN"; |
352 | break; | |
242cd003 | 353 | case QEVENT_STOP: |
b1a15e7e LC |
354 | event_name = "STOP"; |
355 | break; | |
0d1ea871 LC |
356 | default: |
357 | abort(); | |
358 | break; | |
359 | } | |
360 | ||
361 | qmp = qdict_new(); | |
362 | timestamp_put(qmp); | |
363 | qdict_put(qmp, "event", qstring_from_str(event_name)); | |
364 | if (data) | |
365 | qdict_put_obj(qmp, "data", data); | |
366 | ||
367 | monitor_json_emitter(mon, QOBJECT(qmp)); | |
368 | QDECREF(qmp); | |
369 | } | |
370 | ||
9dc39cba FB |
371 | static int compare_cmd(const char *name, const char *list) |
372 | { | |
373 | const char *p, *pstart; | |
374 | int len; | |
375 | len = strlen(name); | |
376 | p = list; | |
377 | for(;;) { | |
378 | pstart = p; | |
379 | p = strchr(p, '|'); | |
380 | if (!p) | |
381 | p = pstart + strlen(pstart); | |
382 | if ((p - pstart) == len && !memcmp(pstart, name, len)) | |
383 | return 1; | |
384 | if (*p == '\0') | |
385 | break; | |
386 | p++; | |
387 | } | |
388 | return 0; | |
389 | } | |
390 | ||
c227f099 | 391 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
376253ec | 392 | const char *prefix, const char *name) |
9dc39cba | 393 | { |
c227f099 | 394 | const mon_cmd_t *cmd; |
9dc39cba FB |
395 | |
396 | for(cmd = cmds; cmd->name != NULL; cmd++) { | |
397 | if (!name || !strcmp(name, cmd->name)) | |
376253ec AL |
398 | monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name, |
399 | cmd->params, cmd->help); | |
9dc39cba FB |
400 | } |
401 | } | |
402 | ||
376253ec | 403 | static void help_cmd(Monitor *mon, const char *name) |
9dc39cba FB |
404 | { |
405 | if (name && !strcmp(name, "info")) { | |
376253ec | 406 | help_cmd_dump(mon, info_cmds, "info ", NULL); |
9dc39cba | 407 | } else { |
376253ec | 408 | help_cmd_dump(mon, mon_cmds, "", name); |
f193c797 | 409 | if (name && !strcmp(name, "log")) { |
8662d656 | 410 | const CPULogItem *item; |
376253ec AL |
411 | monitor_printf(mon, "Log items (comma separated):\n"); |
412 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); | |
f193c797 | 413 | for(item = cpu_log_items; item->mask != 0; item++) { |
376253ec | 414 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
f193c797 FB |
415 | } |
416 | } | |
9dc39cba FB |
417 | } |
418 | } | |
419 | ||
d54908a5 | 420 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
38183186 | 421 | { |
d54908a5 | 422 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
38183186 LC |
423 | } |
424 | ||
d54908a5 | 425 | static void do_commit(Monitor *mon, const QDict *qdict) |
9dc39cba | 426 | { |
751c6a17 GH |
427 | int all_devices; |
428 | DriveInfo *dinfo; | |
d54908a5 | 429 | const char *device = qdict_get_str(qdict, "device"); |
2dc7b602 | 430 | |
7954c734 | 431 | all_devices = !strcmp(device, "all"); |
72cf2d4f | 432 | QTAILQ_FOREACH(dinfo, &drives, next) { |
751c6a17 | 433 | if (!all_devices) |
73006d2a | 434 | if (strcmp(bdrv_get_device_name(dinfo->bdrv), device)) |
751c6a17 GH |
435 | continue; |
436 | bdrv_commit(dinfo->bdrv); | |
9dc39cba FB |
437 | } |
438 | } | |
439 | ||
13c7425e | 440 | static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data) |
9dc39cba | 441 | { |
c227f099 | 442 | const mon_cmd_t *cmd; |
d54908a5 | 443 | const char *item = qdict_get_try_str(qdict, "item"); |
9dc39cba | 444 | |
956f1a0d LC |
445 | if (!item) { |
446 | assert(monitor_ctrl_mode(mon) == 0); | |
9dc39cba | 447 | goto help; |
956f1a0d | 448 | } |
13c7425e LC |
449 | |
450 | for (cmd = info_cmds; cmd->name != NULL; cmd++) { | |
5fafdf24 | 451 | if (compare_cmd(item, cmd->name)) |
13c7425e | 452 | break; |
9dc39cba | 453 | } |
13c7425e | 454 | |
956f1a0d LC |
455 | if (cmd->name == NULL) { |
456 | if (monitor_ctrl_mode(mon)) { | |
457 | qemu_error_new(QERR_COMMAND_NOT_FOUND, item); | |
458 | return; | |
459 | } | |
13c7425e | 460 | goto help; |
956f1a0d | 461 | } |
13c7425e LC |
462 | |
463 | if (monitor_handler_ported(cmd)) { | |
464 | cmd->mhandler.info_new(mon, ret_data); | |
25b422eb LC |
465 | |
466 | if (!monitor_ctrl_mode(mon)) { | |
467 | /* | |
468 | * User Protocol function is called here, Monitor Protocol is | |
469 | * handled by monitor_call_handler() | |
470 | */ | |
471 | if (*ret_data) | |
472 | cmd->user_print(mon, *ret_data); | |
473 | } | |
13c7425e | 474 | } else { |
956f1a0d LC |
475 | if (monitor_ctrl_mode(mon)) { |
476 | /* handler not converted yet */ | |
477 | qemu_error_new(QERR_COMMAND_NOT_FOUND, item); | |
478 | } else { | |
479 | cmd->mhandler.info(mon); | |
480 | } | |
13c7425e LC |
481 | } |
482 | ||
9dc39cba | 483 | return; |
13c7425e LC |
484 | |
485 | help: | |
486 | help_cmd(mon, "info"); | |
9dc39cba FB |
487 | } |
488 | ||
45e914cf LC |
489 | static void do_info_version_print(Monitor *mon, const QObject *data) |
490 | { | |
491 | QDict *qdict; | |
492 | ||
493 | qdict = qobject_to_qdict(data); | |
494 | ||
495 | monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"), | |
496 | qdict_get_str(qdict, "package")); | |
497 | } | |
498 | ||
ab2d3187 LC |
499 | /** |
500 | * do_info_version(): Show QEMU version | |
45e914cf LC |
501 | * |
502 | * Return a QDict with the following information: | |
503 | * | |
504 | * - "qemu": QEMU's version | |
505 | * - "package": package's version | |
506 | * | |
507 | * Example: | |
508 | * | |
509 | * { "qemu": "0.11.50", "package": "" } | |
ab2d3187 LC |
510 | */ |
511 | static void do_info_version(Monitor *mon, QObject **ret_data) | |
9bc9d1c7 | 512 | { |
45e914cf LC |
513 | *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }", |
514 | QEMU_VERSION, QEMU_PKGVERSION); | |
9bc9d1c7 FB |
515 | } |
516 | ||
376253ec | 517 | static void do_info_name(Monitor *mon) |
c35734b2 TS |
518 | { |
519 | if (qemu_name) | |
376253ec | 520 | monitor_printf(mon, "%s\n", qemu_name); |
c35734b2 TS |
521 | } |
522 | ||
1a728677 LC |
523 | static QObject *get_cmd_dict(const char *name) |
524 | { | |
525 | const char *p; | |
526 | ||
527 | /* Remove '|' from some commands */ | |
528 | p = strchr(name, '|'); | |
529 | if (p) { | |
530 | p++; | |
531 | } else { | |
532 | p = name; | |
533 | } | |
534 | ||
535 | return qobject_from_jsonf("{ 'name': %s }", p); | |
536 | } | |
537 | ||
e3bba9d0 LC |
538 | /** |
539 | * do_info_commands(): List QMP available commands | |
540 | * | |
1a728677 LC |
541 | * Each command is represented by a QDict, the returned QObject is a QList |
542 | * of all commands. | |
543 | * | |
544 | * The QDict contains: | |
545 | * | |
546 | * - "name": command's name | |
547 | * | |
548 | * Example: | |
549 | * | |
550 | * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] } | |
e3bba9d0 LC |
551 | */ |
552 | static void do_info_commands(Monitor *mon, QObject **ret_data) | |
553 | { | |
554 | QList *cmd_list; | |
555 | const mon_cmd_t *cmd; | |
556 | ||
557 | cmd_list = qlist_new(); | |
558 | ||
559 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
560 | if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) { | |
1a728677 | 561 | qlist_append_obj(cmd_list, get_cmd_dict(cmd->name)); |
e3bba9d0 LC |
562 | } |
563 | } | |
564 | ||
565 | for (cmd = info_cmds; cmd->name != NULL; cmd++) { | |
566 | if (monitor_handler_ported(cmd)) { | |
567 | char buf[128]; | |
568 | snprintf(buf, sizeof(buf), "query-%s", cmd->name); | |
1a728677 | 569 | qlist_append_obj(cmd_list, get_cmd_dict(buf)); |
e3bba9d0 LC |
570 | } |
571 | } | |
572 | ||
573 | *ret_data = QOBJECT(cmd_list); | |
574 | } | |
575 | ||
bf4f74c0 | 576 | #if defined(TARGET_I386) |
376253ec | 577 | static void do_info_hpet(Monitor *mon) |
16b29ae1 | 578 | { |
376253ec AL |
579 | monitor_printf(mon, "HPET is %s by QEMU\n", |
580 | (no_hpet) ? "disabled" : "enabled"); | |
16b29ae1 | 581 | } |
bf4f74c0 | 582 | #endif |
16b29ae1 | 583 | |
376253ec | 584 | static void do_info_uuid(Monitor *mon) |
a36e69dd | 585 | { |
376253ec AL |
586 | monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], |
587 | qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], | |
588 | qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], | |
589 | qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], | |
590 | qemu_uuid[14], qemu_uuid[15]); | |
a36e69dd TS |
591 | } |
592 | ||
6a00d601 | 593 | /* get the current CPU defined by the user */ |
9596ebb7 | 594 | static int mon_set_cpu(int cpu_index) |
6a00d601 FB |
595 | { |
596 | CPUState *env; | |
597 | ||
598 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
599 | if (env->cpu_index == cpu_index) { | |
731b0364 | 600 | cur_mon->mon_cpu = env; |
6a00d601 FB |
601 | return 0; |
602 | } | |
603 | } | |
604 | return -1; | |
605 | } | |
606 | ||
9596ebb7 | 607 | static CPUState *mon_get_cpu(void) |
6a00d601 | 608 | { |
731b0364 | 609 | if (!cur_mon->mon_cpu) { |
6a00d601 FB |
610 | mon_set_cpu(0); |
611 | } | |
4c0960c0 | 612 | cpu_synchronize_state(cur_mon->mon_cpu); |
731b0364 | 613 | return cur_mon->mon_cpu; |
6a00d601 FB |
614 | } |
615 | ||
376253ec | 616 | static void do_info_registers(Monitor *mon) |
9307c4c1 | 617 | { |
6a00d601 FB |
618 | CPUState *env; |
619 | env = mon_get_cpu(); | |
620 | if (!env) | |
621 | return; | |
9307c4c1 | 622 | #ifdef TARGET_I386 |
376253ec | 623 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
d24b15a8 | 624 | X86_DUMP_FPU); |
9307c4c1 | 625 | #else |
376253ec | 626 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
7fe48483 | 627 | 0); |
9307c4c1 FB |
628 | #endif |
629 | } | |
630 | ||
8f3cec0b LC |
631 | static void print_cpu_iter(QObject *obj, void *opaque) |
632 | { | |
633 | QDict *cpu; | |
634 | int active = ' '; | |
635 | Monitor *mon = opaque; | |
636 | ||
637 | assert(qobject_type(obj) == QTYPE_QDICT); | |
638 | cpu = qobject_to_qdict(obj); | |
639 | ||
55483ad6 | 640 | if (qdict_get_bool(cpu, "current")) { |
8f3cec0b | 641 | active = '*'; |
55483ad6 | 642 | } |
8f3cec0b LC |
643 | |
644 | monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU")); | |
645 | ||
646 | #if defined(TARGET_I386) | |
647 | monitor_printf(mon, "pc=0x" TARGET_FMT_lx, | |
648 | (target_ulong) qdict_get_int(cpu, "pc")); | |
649 | #elif defined(TARGET_PPC) | |
650 | monitor_printf(mon, "nip=0x" TARGET_FMT_lx, | |
651 | (target_long) qdict_get_int(cpu, "nip")); | |
652 | #elif defined(TARGET_SPARC) | |
653 | monitor_printf(mon, "pc=0x " TARGET_FMT_lx, | |
654 | (target_long) qdict_get_int(cpu, "pc")); | |
655 | monitor_printf(mon, "npc=0x" TARGET_FMT_lx, | |
656 | (target_long) qdict_get_int(cpu, "npc")); | |
657 | #elif defined(TARGET_MIPS) | |
658 | monitor_printf(mon, "PC=0x" TARGET_FMT_lx, | |
659 | (target_long) qdict_get_int(cpu, "PC")); | |
660 | #endif | |
661 | ||
55483ad6 | 662 | if (qdict_get_bool(cpu, "halted")) { |
8f3cec0b | 663 | monitor_printf(mon, " (halted)"); |
55483ad6 | 664 | } |
8f3cec0b LC |
665 | |
666 | monitor_printf(mon, "\n"); | |
667 | } | |
668 | ||
669 | static void monitor_print_cpus(Monitor *mon, const QObject *data) | |
670 | { | |
671 | QList *cpu_list; | |
672 | ||
673 | assert(qobject_type(data) == QTYPE_QLIST); | |
674 | cpu_list = qobject_to_qlist(data); | |
675 | qlist_iter(cpu_list, print_cpu_iter, mon); | |
676 | } | |
677 | ||
678 | /** | |
679 | * do_info_cpus(): Show CPU information | |
680 | * | |
55483ad6 | 681 | * Return a QList. Each CPU is represented by a QDict, which contains: |
8f3cec0b | 682 | * |
55483ad6 LC |
683 | * - "cpu": CPU index |
684 | * - "current": true if this is the current CPU, false otherwise | |
685 | * - "halted": true if the cpu is halted, false otherwise | |
686 | * - Current program counter. The key's name depends on the architecture: | |
687 | * "pc": i386/x86)64 | |
688 | * "nip": PPC | |
689 | * "pc" and "npc": sparc | |
690 | * "PC": mips | |
8f3cec0b | 691 | * |
55483ad6 LC |
692 | * Example: |
693 | * | |
694 | * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 }, | |
695 | * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ] | |
8f3cec0b LC |
696 | */ |
697 | static void do_info_cpus(Monitor *mon, QObject **ret_data) | |
6a00d601 FB |
698 | { |
699 | CPUState *env; | |
8f3cec0b LC |
700 | QList *cpu_list; |
701 | ||
702 | cpu_list = qlist_new(); | |
6a00d601 FB |
703 | |
704 | /* just to set the default cpu if not already done */ | |
705 | mon_get_cpu(); | |
706 | ||
707 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
55483ad6 LC |
708 | QDict *cpu; |
709 | QObject *obj; | |
8f3cec0b | 710 | |
4c0960c0 | 711 | cpu_synchronize_state(env); |
8f3cec0b | 712 | |
55483ad6 LC |
713 | obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }", |
714 | env->cpu_index, env == mon->mon_cpu, | |
715 | env->halted); | |
716 | assert(obj != NULL); | |
717 | ||
718 | cpu = qobject_to_qdict(obj); | |
8f3cec0b | 719 | |
6a00d601 | 720 | #if defined(TARGET_I386) |
8f3cec0b | 721 | qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base)); |
e80e1cc4 | 722 | #elif defined(TARGET_PPC) |
8f3cec0b | 723 | qdict_put(cpu, "nip", qint_from_int(env->nip)); |
ba3c64fb | 724 | #elif defined(TARGET_SPARC) |
8f3cec0b LC |
725 | qdict_put(cpu, "pc", qint_from_int(env->pc)); |
726 | qdict_put(cpu, "npc", qint_from_int(env->npc)); | |
ead9360e | 727 | #elif defined(TARGET_MIPS) |
8f3cec0b | 728 | qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC)); |
ce5232c5 | 729 | #endif |
8f3cec0b LC |
730 | |
731 | qlist_append(cpu_list, cpu); | |
6a00d601 | 732 | } |
8f3cec0b LC |
733 | |
734 | *ret_data = QOBJECT(cpu_list); | |
6a00d601 FB |
735 | } |
736 | ||
d54908a5 | 737 | static void do_cpu_set(Monitor *mon, const QDict *qdict) |
6a00d601 | 738 | { |
d54908a5 | 739 | int index = qdict_get_int(qdict, "index"); |
6a00d601 | 740 | if (mon_set_cpu(index) < 0) |
376253ec | 741 | monitor_printf(mon, "Invalid CPU index\n"); |
6a00d601 FB |
742 | } |
743 | ||
376253ec | 744 | static void do_info_jit(Monitor *mon) |
e3db7226 | 745 | { |
376253ec | 746 | dump_exec_info((FILE *)mon, monitor_fprintf); |
e3db7226 FB |
747 | } |
748 | ||
376253ec | 749 | static void do_info_history(Monitor *mon) |
aa455485 FB |
750 | { |
751 | int i; | |
7e2515e8 | 752 | const char *str; |
3b46e624 | 753 | |
cde76ee1 AL |
754 | if (!mon->rs) |
755 | return; | |
7e2515e8 FB |
756 | i = 0; |
757 | for(;;) { | |
731b0364 | 758 | str = readline_get_history(mon->rs, i); |
7e2515e8 FB |
759 | if (!str) |
760 | break; | |
376253ec | 761 | monitor_printf(mon, "%d: '%s'\n", i, str); |
8e3a9fd2 | 762 | i++; |
aa455485 FB |
763 | } |
764 | } | |
765 | ||
76a66253 JM |
766 | #if defined(TARGET_PPC) |
767 | /* XXX: not implemented in other targets */ | |
376253ec | 768 | static void do_info_cpu_stats(Monitor *mon) |
76a66253 JM |
769 | { |
770 | CPUState *env; | |
771 | ||
772 | env = mon_get_cpu(); | |
376253ec | 773 | cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0); |
76a66253 JM |
774 | } |
775 | #endif | |
776 | ||
b223f35f LC |
777 | /** |
778 | * do_quit(): Quit QEMU execution | |
779 | */ | |
780 | static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
9dc39cba FB |
781 | { |
782 | exit(0); | |
783 | } | |
784 | ||
376253ec | 785 | static int eject_device(Monitor *mon, BlockDriverState *bs, int force) |
9dc39cba FB |
786 | { |
787 | if (bdrv_is_inserted(bs)) { | |
788 | if (!force) { | |
789 | if (!bdrv_is_removable(bs)) { | |
2c2a6bb8 MA |
790 | qemu_error_new(QERR_DEVICE_NOT_REMOVABLE, |
791 | bdrv_get_device_name(bs)); | |
9dc39cba FB |
792 | return -1; |
793 | } | |
794 | if (bdrv_is_locked(bs)) { | |
2c2a6bb8 | 795 | qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); |
9dc39cba FB |
796 | return -1; |
797 | } | |
798 | } | |
799 | bdrv_close(bs); | |
800 | } | |
801 | return 0; | |
802 | } | |
803 | ||
e1c923a6 | 804 | static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data) |
9dc39cba FB |
805 | { |
806 | BlockDriverState *bs; | |
f18c16de LC |
807 | int force = qdict_get_int(qdict, "force"); |
808 | const char *filename = qdict_get_str(qdict, "filename"); | |
9dc39cba | 809 | |
9307c4c1 | 810 | bs = bdrv_find(filename); |
9dc39cba | 811 | if (!bs) { |
2c2a6bb8 | 812 | qemu_error_new(QERR_DEVICE_NOT_FOUND, filename); |
9dc39cba FB |
813 | return; |
814 | } | |
376253ec | 815 | eject_device(mon, bs, force); |
9dc39cba FB |
816 | } |
817 | ||
a3a55a2e LC |
818 | static void do_block_set_passwd(Monitor *mon, const QDict *qdict, |
819 | QObject **ret_data) | |
820 | { | |
821 | BlockDriverState *bs; | |
822 | ||
823 | bs = bdrv_find(qdict_get_str(qdict, "device")); | |
824 | if (!bs) { | |
825 | qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device")); | |
826 | return; | |
827 | } | |
828 | ||
829 | if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) { | |
830 | qemu_error_new(QERR_INVALID_PASSWORD); | |
831 | } | |
832 | } | |
833 | ||
376253ec AL |
834 | static void do_change_block(Monitor *mon, const char *device, |
835 | const char *filename, const char *fmt) | |
9dc39cba FB |
836 | { |
837 | BlockDriverState *bs; | |
2ecea9b8 | 838 | BlockDriver *drv = NULL; |
9dc39cba | 839 | |
9307c4c1 | 840 | bs = bdrv_find(device); |
9dc39cba | 841 | if (!bs) { |
ec3b82af | 842 | qemu_error_new(QERR_DEVICE_NOT_FOUND, device); |
9dc39cba FB |
843 | return; |
844 | } | |
2ecea9b8 | 845 | if (fmt) { |
eb852011 | 846 | drv = bdrv_find_whitelisted_format(fmt); |
2ecea9b8 | 847 | if (!drv) { |
ec3b82af | 848 | qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt); |
2ecea9b8 AJ |
849 | return; |
850 | } | |
851 | } | |
376253ec | 852 | if (eject_device(mon, bs, 0) < 0) |
9dc39cba | 853 | return; |
2ecea9b8 | 854 | bdrv_open2(bs, filename, 0, drv); |
376253ec | 855 | monitor_read_bdrv_key_start(mon, bs, NULL, NULL); |
bb5fc20f AL |
856 | } |
857 | ||
ec3b82af | 858 | static void change_vnc_password(const char *password) |
bb5fc20f AL |
859 | { |
860 | if (vnc_display_password(NULL, password) < 0) | |
ec3b82af | 861 | qemu_error_new(QERR_SET_PASSWD_FAILED); |
bb5fc20f | 862 | |
2895e075 MA |
863 | } |
864 | ||
865 | static void change_vnc_password_cb(Monitor *mon, const char *password, | |
866 | void *opaque) | |
867 | { | |
ec3b82af | 868 | change_vnc_password(password); |
731b0364 | 869 | monitor_read_command(mon, 1); |
9dc39cba FB |
870 | } |
871 | ||
376253ec | 872 | static void do_change_vnc(Monitor *mon, const char *target, const char *arg) |
e25a5822 | 873 | { |
70848515 | 874 | if (strcmp(target, "passwd") == 0 || |
28a76be8 AL |
875 | strcmp(target, "password") == 0) { |
876 | if (arg) { | |
bb5fc20f | 877 | char password[9]; |
28a76be8 AL |
878 | strncpy(password, arg, sizeof(password)); |
879 | password[sizeof(password) - 1] = '\0'; | |
ec3b82af | 880 | change_vnc_password(password); |
bb5fc20f | 881 | } else { |
376253ec | 882 | monitor_read_password(mon, change_vnc_password_cb, NULL); |
bb5fc20f | 883 | } |
70848515 | 884 | } else { |
28a76be8 | 885 | if (vnc_display_open(NULL, target) < 0) |
ec3b82af | 886 | qemu_error_new(QERR_VNC_SERVER_FAILED, target); |
70848515 | 887 | } |
e25a5822 TS |
888 | } |
889 | ||
ec3b82af MA |
890 | /** |
891 | * do_change(): Change a removable medium, or VNC configuration | |
892 | */ | |
893 | static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
e25a5822 | 894 | { |
1d4daa91 LC |
895 | const char *device = qdict_get_str(qdict, "device"); |
896 | const char *target = qdict_get_str(qdict, "target"); | |
897 | const char *arg = qdict_get_try_str(qdict, "arg"); | |
e25a5822 | 898 | if (strcmp(device, "vnc") == 0) { |
28a76be8 | 899 | do_change_vnc(mon, target, arg); |
e25a5822 | 900 | } else { |
28a76be8 | 901 | do_change_block(mon, device, target, arg); |
e25a5822 TS |
902 | } |
903 | } | |
904 | ||
d54908a5 | 905 | static void do_screen_dump(Monitor *mon, const QDict *qdict) |
59a983b9 | 906 | { |
d54908a5 | 907 | vga_hw_screen_dump(qdict_get_str(qdict, "filename")); |
59a983b9 FB |
908 | } |
909 | ||
d54908a5 | 910 | static void do_logfile(Monitor *mon, const QDict *qdict) |
e735b91c | 911 | { |
d54908a5 | 912 | cpu_set_log_filename(qdict_get_str(qdict, "filename")); |
e735b91c PB |
913 | } |
914 | ||
d54908a5 | 915 | static void do_log(Monitor *mon, const QDict *qdict) |
f193c797 FB |
916 | { |
917 | int mask; | |
d54908a5 | 918 | const char *items = qdict_get_str(qdict, "items"); |
3b46e624 | 919 | |
9307c4c1 | 920 | if (!strcmp(items, "none")) { |
f193c797 FB |
921 | mask = 0; |
922 | } else { | |
9307c4c1 | 923 | mask = cpu_str_to_log_mask(items); |
f193c797 | 924 | if (!mask) { |
376253ec | 925 | help_cmd(mon, "log"); |
f193c797 FB |
926 | return; |
927 | } | |
928 | } | |
929 | cpu_set_log(mask); | |
930 | } | |
931 | ||
d54908a5 | 932 | static void do_singlestep(Monitor *mon, const QDict *qdict) |
1b530a6d | 933 | { |
d54908a5 | 934 | const char *option = qdict_get_try_str(qdict, "option"); |
1b530a6d AJ |
935 | if (!option || !strcmp(option, "on")) { |
936 | singlestep = 1; | |
937 | } else if (!strcmp(option, "off")) { | |
938 | singlestep = 0; | |
939 | } else { | |
940 | monitor_printf(mon, "unexpected option %s\n", option); | |
941 | } | |
942 | } | |
943 | ||
e0c97bde LC |
944 | /** |
945 | * do_stop(): Stop VM execution | |
946 | */ | |
947 | static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
8a7ddc38 FB |
948 | { |
949 | vm_stop(EXCP_INTERRUPT); | |
950 | } | |
951 | ||
bb5fc20f | 952 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs); |
c0f4ce77 | 953 | |
376253ec AL |
954 | struct bdrv_iterate_context { |
955 | Monitor *mon; | |
956 | int err; | |
957 | }; | |
958 | ||
a1f896a0 LC |
959 | /** |
960 | * do_cont(): Resume emulation. | |
961 | */ | |
962 | static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
8a7ddc38 | 963 | { |
376253ec | 964 | struct bdrv_iterate_context context = { mon, 0 }; |
c0f4ce77 | 965 | |
376253ec | 966 | bdrv_iterate(encrypted_bdrv_it, &context); |
c0f4ce77 | 967 | /* only resume the vm if all keys are set and valid */ |
376253ec | 968 | if (!context.err) |
c0f4ce77 | 969 | vm_start(); |
8a7ddc38 FB |
970 | } |
971 | ||
bb5fc20f AL |
972 | static void bdrv_key_cb(void *opaque, int err) |
973 | { | |
376253ec AL |
974 | Monitor *mon = opaque; |
975 | ||
bb5fc20f AL |
976 | /* another key was set successfully, retry to continue */ |
977 | if (!err) | |
a1f896a0 | 978 | do_cont(mon, NULL, NULL); |
bb5fc20f AL |
979 | } |
980 | ||
981 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs) | |
982 | { | |
376253ec | 983 | struct bdrv_iterate_context *context = opaque; |
bb5fc20f | 984 | |
376253ec AL |
985 | if (!context->err && bdrv_key_required(bs)) { |
986 | context->err = -EBUSY; | |
987 | monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb, | |
988 | context->mon); | |
bb5fc20f AL |
989 | } |
990 | } | |
991 | ||
d54908a5 | 992 | static void do_gdbserver(Monitor *mon, const QDict *qdict) |
59030a8c | 993 | { |
d54908a5 | 994 | const char *device = qdict_get_try_str(qdict, "device"); |
59030a8c AL |
995 | if (!device) |
996 | device = "tcp::" DEFAULT_GDBSTUB_PORT; | |
997 | if (gdbserver_start(device) < 0) { | |
998 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", | |
999 | device); | |
1000 | } else if (strcmp(device, "none") == 0) { | |
36556b20 | 1001 | monitor_printf(mon, "Disabled gdbserver\n"); |
8a7ddc38 | 1002 | } else { |
59030a8c AL |
1003 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
1004 | device); | |
8a7ddc38 FB |
1005 | } |
1006 | } | |
1007 | ||
d54908a5 | 1008 | static void do_watchdog_action(Monitor *mon, const QDict *qdict) |
9dd986cc | 1009 | { |
d54908a5 | 1010 | const char *action = qdict_get_str(qdict, "action"); |
9dd986cc RJ |
1011 | if (select_watchdog_action(action) == -1) { |
1012 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); | |
1013 | } | |
1014 | } | |
1015 | ||
376253ec | 1016 | static void monitor_printc(Monitor *mon, int c) |
9307c4c1 | 1017 | { |
376253ec | 1018 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1019 | switch(c) { |
1020 | case '\'': | |
376253ec | 1021 | monitor_printf(mon, "\\'"); |
9307c4c1 FB |
1022 | break; |
1023 | case '\\': | |
376253ec | 1024 | monitor_printf(mon, "\\\\"); |
9307c4c1 FB |
1025 | break; |
1026 | case '\n': | |
376253ec | 1027 | monitor_printf(mon, "\\n"); |
9307c4c1 FB |
1028 | break; |
1029 | case '\r': | |
376253ec | 1030 | monitor_printf(mon, "\\r"); |
9307c4c1 FB |
1031 | break; |
1032 | default: | |
1033 | if (c >= 32 && c <= 126) { | |
376253ec | 1034 | monitor_printf(mon, "%c", c); |
9307c4c1 | 1035 | } else { |
376253ec | 1036 | monitor_printf(mon, "\\x%02x", c); |
9307c4c1 FB |
1037 | } |
1038 | break; | |
1039 | } | |
376253ec | 1040 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1041 | } |
1042 | ||
376253ec | 1043 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
c227f099 | 1044 | target_phys_addr_t addr, int is_physical) |
9307c4c1 | 1045 | { |
6a00d601 | 1046 | CPUState *env; |
9307c4c1 FB |
1047 | int nb_per_line, l, line_size, i, max_digits, len; |
1048 | uint8_t buf[16]; | |
1049 | uint64_t v; | |
1050 | ||
1051 | if (format == 'i') { | |
1052 | int flags; | |
1053 | flags = 0; | |
6a00d601 FB |
1054 | env = mon_get_cpu(); |
1055 | if (!env && !is_physical) | |
1056 | return; | |
9307c4c1 | 1057 | #ifdef TARGET_I386 |
4c27ba27 | 1058 | if (wsize == 2) { |
9307c4c1 | 1059 | flags = 1; |
4c27ba27 FB |
1060 | } else if (wsize == 4) { |
1061 | flags = 0; | |
1062 | } else { | |
6a15fd12 | 1063 | /* as default we use the current CS size */ |
4c27ba27 | 1064 | flags = 0; |
6a15fd12 FB |
1065 | if (env) { |
1066 | #ifdef TARGET_X86_64 | |
5fafdf24 | 1067 | if ((env->efer & MSR_EFER_LMA) && |
6a15fd12 FB |
1068 | (env->segs[R_CS].flags & DESC_L_MASK)) |
1069 | flags = 2; | |
1070 | else | |
1071 | #endif | |
1072 | if (!(env->segs[R_CS].flags & DESC_B_MASK)) | |
1073 | flags = 1; | |
1074 | } | |
4c27ba27 FB |
1075 | } |
1076 | #endif | |
376253ec | 1077 | monitor_disas(mon, env, addr, count, is_physical, flags); |
9307c4c1 FB |
1078 | return; |
1079 | } | |
1080 | ||
1081 | len = wsize * count; | |
1082 | if (wsize == 1) | |
1083 | line_size = 8; | |
1084 | else | |
1085 | line_size = 16; | |
1086 | nb_per_line = line_size / wsize; | |
1087 | max_digits = 0; | |
1088 | ||
1089 | switch(format) { | |
1090 | case 'o': | |
1091 | max_digits = (wsize * 8 + 2) / 3; | |
1092 | break; | |
1093 | default: | |
1094 | case 'x': | |
1095 | max_digits = (wsize * 8) / 4; | |
1096 | break; | |
1097 | case 'u': | |
1098 | case 'd': | |
1099 | max_digits = (wsize * 8 * 10 + 32) / 33; | |
1100 | break; | |
1101 | case 'c': | |
1102 | wsize = 1; | |
1103 | break; | |
1104 | } | |
1105 | ||
1106 | while (len > 0) { | |
7743e588 | 1107 | if (is_physical) |
376253ec | 1108 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
7743e588 | 1109 | else |
376253ec | 1110 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
9307c4c1 FB |
1111 | l = len; |
1112 | if (l > line_size) | |
1113 | l = line_size; | |
1114 | if (is_physical) { | |
1115 | cpu_physical_memory_rw(addr, buf, l, 0); | |
1116 | } else { | |
6a00d601 FB |
1117 | env = mon_get_cpu(); |
1118 | if (!env) | |
1119 | break; | |
c8f79b67 | 1120 | if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) { |
376253ec | 1121 | monitor_printf(mon, " Cannot access memory\n"); |
c8f79b67 AL |
1122 | break; |
1123 | } | |
9307c4c1 | 1124 | } |
5fafdf24 | 1125 | i = 0; |
9307c4c1 FB |
1126 | while (i < l) { |
1127 | switch(wsize) { | |
1128 | default: | |
1129 | case 1: | |
1130 | v = ldub_raw(buf + i); | |
1131 | break; | |
1132 | case 2: | |
1133 | v = lduw_raw(buf + i); | |
1134 | break; | |
1135 | case 4: | |
92a31b1f | 1136 | v = (uint32_t)ldl_raw(buf + i); |
9307c4c1 FB |
1137 | break; |
1138 | case 8: | |
1139 | v = ldq_raw(buf + i); | |
1140 | break; | |
1141 | } | |
376253ec | 1142 | monitor_printf(mon, " "); |
9307c4c1 FB |
1143 | switch(format) { |
1144 | case 'o': | |
376253ec | 1145 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
9307c4c1 FB |
1146 | break; |
1147 | case 'x': | |
376253ec | 1148 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
9307c4c1 FB |
1149 | break; |
1150 | case 'u': | |
376253ec | 1151 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
9307c4c1 FB |
1152 | break; |
1153 | case 'd': | |
376253ec | 1154 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
9307c4c1 FB |
1155 | break; |
1156 | case 'c': | |
376253ec | 1157 | monitor_printc(mon, v); |
9307c4c1 FB |
1158 | break; |
1159 | } | |
1160 | i += wsize; | |
1161 | } | |
376253ec | 1162 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1163 | addr += l; |
1164 | len -= l; | |
1165 | } | |
1166 | } | |
1167 | ||
1bd1442e | 1168 | static void do_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1169 | { |
1bd1442e LC |
1170 | int count = qdict_get_int(qdict, "count"); |
1171 | int format = qdict_get_int(qdict, "format"); | |
1172 | int size = qdict_get_int(qdict, "size"); | |
1173 | target_long addr = qdict_get_int(qdict, "addr"); | |
1174 | ||
376253ec | 1175 | memory_dump(mon, count, format, size, addr, 0); |
9307c4c1 FB |
1176 | } |
1177 | ||
1bd1442e | 1178 | static void do_physical_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1179 | { |
1bd1442e LC |
1180 | int count = qdict_get_int(qdict, "count"); |
1181 | int format = qdict_get_int(qdict, "format"); | |
1182 | int size = qdict_get_int(qdict, "size"); | |
c227f099 | 1183 | target_phys_addr_t addr = qdict_get_int(qdict, "addr"); |
1bd1442e | 1184 | |
376253ec | 1185 | memory_dump(mon, count, format, size, addr, 1); |
9307c4c1 FB |
1186 | } |
1187 | ||
1bd1442e | 1188 | static void do_print(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1189 | { |
1bd1442e | 1190 | int format = qdict_get_int(qdict, "format"); |
c227f099 | 1191 | target_phys_addr_t val = qdict_get_int(qdict, "val"); |
1bd1442e | 1192 | |
7743e588 | 1193 | #if TARGET_PHYS_ADDR_BITS == 32 |
9307c4c1 FB |
1194 | switch(format) { |
1195 | case 'o': | |
376253ec | 1196 | monitor_printf(mon, "%#o", val); |
9307c4c1 FB |
1197 | break; |
1198 | case 'x': | |
376253ec | 1199 | monitor_printf(mon, "%#x", val); |
9307c4c1 FB |
1200 | break; |
1201 | case 'u': | |
376253ec | 1202 | monitor_printf(mon, "%u", val); |
9307c4c1 FB |
1203 | break; |
1204 | default: | |
1205 | case 'd': | |
376253ec | 1206 | monitor_printf(mon, "%d", val); |
9307c4c1 FB |
1207 | break; |
1208 | case 'c': | |
376253ec | 1209 | monitor_printc(mon, val); |
9307c4c1 FB |
1210 | break; |
1211 | } | |
92a31b1f FB |
1212 | #else |
1213 | switch(format) { | |
1214 | case 'o': | |
376253ec | 1215 | monitor_printf(mon, "%#" PRIo64, val); |
92a31b1f FB |
1216 | break; |
1217 | case 'x': | |
376253ec | 1218 | monitor_printf(mon, "%#" PRIx64, val); |
92a31b1f FB |
1219 | break; |
1220 | case 'u': | |
376253ec | 1221 | monitor_printf(mon, "%" PRIu64, val); |
92a31b1f FB |
1222 | break; |
1223 | default: | |
1224 | case 'd': | |
376253ec | 1225 | monitor_printf(mon, "%" PRId64, val); |
92a31b1f FB |
1226 | break; |
1227 | case 'c': | |
376253ec | 1228 | monitor_printc(mon, val); |
92a31b1f FB |
1229 | break; |
1230 | } | |
1231 | #endif | |
376253ec | 1232 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1233 | } |
1234 | ||
57e09454 | 1235 | static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data) |
b371dc59 FB |
1236 | { |
1237 | FILE *f; | |
afe67ef2 LC |
1238 | uint32_t size = qdict_get_int(qdict, "size"); |
1239 | const char *filename = qdict_get_str(qdict, "filename"); | |
1240 | target_long addr = qdict_get_int(qdict, "val"); | |
b371dc59 FB |
1241 | uint32_t l; |
1242 | CPUState *env; | |
1243 | uint8_t buf[1024]; | |
1244 | ||
1245 | env = mon_get_cpu(); | |
1246 | if (!env) | |
1247 | return; | |
1248 | ||
1249 | f = fopen(filename, "wb"); | |
1250 | if (!f) { | |
376253ec | 1251 | monitor_printf(mon, "could not open '%s'\n", filename); |
b371dc59 FB |
1252 | return; |
1253 | } | |
1254 | while (size != 0) { | |
1255 | l = sizeof(buf); | |
1256 | if (l > size) | |
1257 | l = size; | |
1258 | cpu_memory_rw_debug(env, addr, buf, l, 0); | |
1259 | fwrite(buf, 1, l, f); | |
1260 | addr += l; | |
1261 | size -= l; | |
1262 | } | |
1263 | fclose(f); | |
1264 | } | |
1265 | ||
18f5a8bf LC |
1266 | static void do_physical_memory_save(Monitor *mon, const QDict *qdict, |
1267 | QObject **ret_data) | |
a8bdf7a6 AJ |
1268 | { |
1269 | FILE *f; | |
1270 | uint32_t l; | |
1271 | uint8_t buf[1024]; | |
afe67ef2 LC |
1272 | uint32_t size = qdict_get_int(qdict, "size"); |
1273 | const char *filename = qdict_get_str(qdict, "filename"); | |
c227f099 | 1274 | target_phys_addr_t addr = qdict_get_int(qdict, "val"); |
a8bdf7a6 AJ |
1275 | |
1276 | f = fopen(filename, "wb"); | |
1277 | if (!f) { | |
376253ec | 1278 | monitor_printf(mon, "could not open '%s'\n", filename); |
a8bdf7a6 AJ |
1279 | return; |
1280 | } | |
1281 | while (size != 0) { | |
1282 | l = sizeof(buf); | |
1283 | if (l > size) | |
1284 | l = size; | |
1285 | cpu_physical_memory_rw(addr, buf, l, 0); | |
1286 | fwrite(buf, 1, l, f); | |
1287 | fflush(f); | |
1288 | addr += l; | |
1289 | size -= l; | |
1290 | } | |
1291 | fclose(f); | |
1292 | } | |
1293 | ||
f18c16de | 1294 | static void do_sum(Monitor *mon, const QDict *qdict) |
e4cf1adc FB |
1295 | { |
1296 | uint32_t addr; | |
1297 | uint8_t buf[1]; | |
1298 | uint16_t sum; | |
f18c16de LC |
1299 | uint32_t start = qdict_get_int(qdict, "start"); |
1300 | uint32_t size = qdict_get_int(qdict, "size"); | |
e4cf1adc FB |
1301 | |
1302 | sum = 0; | |
1303 | for(addr = start; addr < (start + size); addr++) { | |
1304 | cpu_physical_memory_rw(addr, buf, 1, 0); | |
1305 | /* BSD sum algorithm ('sum' Unix command) */ | |
1306 | sum = (sum >> 1) | (sum << 15); | |
1307 | sum += buf[0]; | |
1308 | } | |
376253ec | 1309 | monitor_printf(mon, "%05d\n", sum); |
e4cf1adc FB |
1310 | } |
1311 | ||
a3a91a35 FB |
1312 | typedef struct { |
1313 | int keycode; | |
1314 | const char *name; | |
1315 | } KeyDef; | |
1316 | ||
1317 | static const KeyDef key_defs[] = { | |
1318 | { 0x2a, "shift" }, | |
1319 | { 0x36, "shift_r" }, | |
3b46e624 | 1320 | |
a3a91a35 FB |
1321 | { 0x38, "alt" }, |
1322 | { 0xb8, "alt_r" }, | |
2ba27c7f TS |
1323 | { 0x64, "altgr" }, |
1324 | { 0xe4, "altgr_r" }, | |
a3a91a35 FB |
1325 | { 0x1d, "ctrl" }, |
1326 | { 0x9d, "ctrl_r" }, | |
1327 | ||
1328 | { 0xdd, "menu" }, | |
1329 | ||
1330 | { 0x01, "esc" }, | |
1331 | ||
1332 | { 0x02, "1" }, | |
1333 | { 0x03, "2" }, | |
1334 | { 0x04, "3" }, | |
1335 | { 0x05, "4" }, | |
1336 | { 0x06, "5" }, | |
1337 | { 0x07, "6" }, | |
1338 | { 0x08, "7" }, | |
1339 | { 0x09, "8" }, | |
1340 | { 0x0a, "9" }, | |
1341 | { 0x0b, "0" }, | |
64866c3d FB |
1342 | { 0x0c, "minus" }, |
1343 | { 0x0d, "equal" }, | |
a3a91a35 FB |
1344 | { 0x0e, "backspace" }, |
1345 | ||
1346 | { 0x0f, "tab" }, | |
1347 | { 0x10, "q" }, | |
1348 | { 0x11, "w" }, | |
1349 | { 0x12, "e" }, | |
1350 | { 0x13, "r" }, | |
1351 | { 0x14, "t" }, | |
1352 | { 0x15, "y" }, | |
1353 | { 0x16, "u" }, | |
1354 | { 0x17, "i" }, | |
1355 | { 0x18, "o" }, | |
1356 | { 0x19, "p" }, | |
1357 | ||
1358 | { 0x1c, "ret" }, | |
1359 | ||
1360 | { 0x1e, "a" }, | |
1361 | { 0x1f, "s" }, | |
1362 | { 0x20, "d" }, | |
1363 | { 0x21, "f" }, | |
1364 | { 0x22, "g" }, | |
1365 | { 0x23, "h" }, | |
1366 | { 0x24, "j" }, | |
1367 | { 0x25, "k" }, | |
1368 | { 0x26, "l" }, | |
1369 | ||
1370 | { 0x2c, "z" }, | |
1371 | { 0x2d, "x" }, | |
1372 | { 0x2e, "c" }, | |
1373 | { 0x2f, "v" }, | |
1374 | { 0x30, "b" }, | |
1375 | { 0x31, "n" }, | |
1376 | { 0x32, "m" }, | |
9155fc45 AJ |
1377 | { 0x33, "comma" }, |
1378 | { 0x34, "dot" }, | |
1379 | { 0x35, "slash" }, | |
3b46e624 | 1380 | |
4d3b6f6e AZ |
1381 | { 0x37, "asterisk" }, |
1382 | ||
a3a91a35 | 1383 | { 0x39, "spc" }, |
00ffa62a | 1384 | { 0x3a, "caps_lock" }, |
a3a91a35 FB |
1385 | { 0x3b, "f1" }, |
1386 | { 0x3c, "f2" }, | |
1387 | { 0x3d, "f3" }, | |
1388 | { 0x3e, "f4" }, | |
1389 | { 0x3f, "f5" }, | |
1390 | { 0x40, "f6" }, | |
1391 | { 0x41, "f7" }, | |
1392 | { 0x42, "f8" }, | |
1393 | { 0x43, "f9" }, | |
1394 | { 0x44, "f10" }, | |
00ffa62a | 1395 | { 0x45, "num_lock" }, |
a3a91a35 FB |
1396 | { 0x46, "scroll_lock" }, |
1397 | ||
64866c3d FB |
1398 | { 0xb5, "kp_divide" }, |
1399 | { 0x37, "kp_multiply" }, | |
0cfec834 | 1400 | { 0x4a, "kp_subtract" }, |
64866c3d FB |
1401 | { 0x4e, "kp_add" }, |
1402 | { 0x9c, "kp_enter" }, | |
1403 | { 0x53, "kp_decimal" }, | |
f2289cb6 | 1404 | { 0x54, "sysrq" }, |
64866c3d FB |
1405 | |
1406 | { 0x52, "kp_0" }, | |
1407 | { 0x4f, "kp_1" }, | |
1408 | { 0x50, "kp_2" }, | |
1409 | { 0x51, "kp_3" }, | |
1410 | { 0x4b, "kp_4" }, | |
1411 | { 0x4c, "kp_5" }, | |
1412 | { 0x4d, "kp_6" }, | |
1413 | { 0x47, "kp_7" }, | |
1414 | { 0x48, "kp_8" }, | |
1415 | { 0x49, "kp_9" }, | |
3b46e624 | 1416 | |
a3a91a35 FB |
1417 | { 0x56, "<" }, |
1418 | ||
1419 | { 0x57, "f11" }, | |
1420 | { 0x58, "f12" }, | |
1421 | ||
1422 | { 0xb7, "print" }, | |
1423 | ||
1424 | { 0xc7, "home" }, | |
1425 | { 0xc9, "pgup" }, | |
1426 | { 0xd1, "pgdn" }, | |
1427 | { 0xcf, "end" }, | |
1428 | ||
1429 | { 0xcb, "left" }, | |
1430 | { 0xc8, "up" }, | |
1431 | { 0xd0, "down" }, | |
1432 | { 0xcd, "right" }, | |
1433 | ||
1434 | { 0xd2, "insert" }, | |
1435 | { 0xd3, "delete" }, | |
c0b5b109 BS |
1436 | #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64) |
1437 | { 0xf0, "stop" }, | |
1438 | { 0xf1, "again" }, | |
1439 | { 0xf2, "props" }, | |
1440 | { 0xf3, "undo" }, | |
1441 | { 0xf4, "front" }, | |
1442 | { 0xf5, "copy" }, | |
1443 | { 0xf6, "open" }, | |
1444 | { 0xf7, "paste" }, | |
1445 | { 0xf8, "find" }, | |
1446 | { 0xf9, "cut" }, | |
1447 | { 0xfa, "lf" }, | |
1448 | { 0xfb, "help" }, | |
1449 | { 0xfc, "meta_l" }, | |
1450 | { 0xfd, "meta_r" }, | |
1451 | { 0xfe, "compose" }, | |
1452 | #endif | |
a3a91a35 FB |
1453 | { 0, NULL }, |
1454 | }; | |
1455 | ||
1456 | static int get_keycode(const char *key) | |
1457 | { | |
1458 | const KeyDef *p; | |
64866c3d FB |
1459 | char *endp; |
1460 | int ret; | |
a3a91a35 FB |
1461 | |
1462 | for(p = key_defs; p->name != NULL; p++) { | |
1463 | if (!strcmp(key, p->name)) | |
1464 | return p->keycode; | |
1465 | } | |
64866c3d FB |
1466 | if (strstart(key, "0x", NULL)) { |
1467 | ret = strtoul(key, &endp, 0); | |
1468 | if (*endp == '\0' && ret >= 0x01 && ret <= 0xff) | |
1469 | return ret; | |
1470 | } | |
a3a91a35 FB |
1471 | return -1; |
1472 | } | |
1473 | ||
c8256f9d AZ |
1474 | #define MAX_KEYCODES 16 |
1475 | static uint8_t keycodes[MAX_KEYCODES]; | |
1476 | static int nb_pending_keycodes; | |
1477 | static QEMUTimer *key_timer; | |
1478 | ||
1479 | static void release_keys(void *opaque) | |
1480 | { | |
1481 | int keycode; | |
1482 | ||
1483 | while (nb_pending_keycodes > 0) { | |
1484 | nb_pending_keycodes--; | |
1485 | keycode = keycodes[nb_pending_keycodes]; | |
1486 | if (keycode & 0x80) | |
1487 | kbd_put_keycode(0xe0); | |
1488 | kbd_put_keycode(keycode | 0x80); | |
1489 | } | |
1490 | } | |
1491 | ||
1d4daa91 | 1492 | static void do_sendkey(Monitor *mon, const QDict *qdict) |
a3a91a35 | 1493 | { |
3401c0d9 AZ |
1494 | char keyname_buf[16]; |
1495 | char *separator; | |
1496 | int keyname_len, keycode, i; | |
1d4daa91 LC |
1497 | const char *string = qdict_get_str(qdict, "string"); |
1498 | int has_hold_time = qdict_haskey(qdict, "hold_time"); | |
1499 | int hold_time = qdict_get_try_int(qdict, "hold_time", -1); | |
3401c0d9 | 1500 | |
c8256f9d AZ |
1501 | if (nb_pending_keycodes > 0) { |
1502 | qemu_del_timer(key_timer); | |
1503 | release_keys(NULL); | |
1504 | } | |
1505 | if (!has_hold_time) | |
1506 | hold_time = 100; | |
1507 | i = 0; | |
3401c0d9 AZ |
1508 | while (1) { |
1509 | separator = strchr(string, '-'); | |
1510 | keyname_len = separator ? separator - string : strlen(string); | |
1511 | if (keyname_len > 0) { | |
1512 | pstrcpy(keyname_buf, sizeof(keyname_buf), string); | |
1513 | if (keyname_len > sizeof(keyname_buf) - 1) { | |
376253ec | 1514 | monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf); |
3401c0d9 | 1515 | return; |
a3a91a35 | 1516 | } |
c8256f9d | 1517 | if (i == MAX_KEYCODES) { |
376253ec | 1518 | monitor_printf(mon, "too many keys\n"); |
3401c0d9 AZ |
1519 | return; |
1520 | } | |
1521 | keyname_buf[keyname_len] = 0; | |
1522 | keycode = get_keycode(keyname_buf); | |
1523 | if (keycode < 0) { | |
376253ec | 1524 | monitor_printf(mon, "unknown key: '%s'\n", keyname_buf); |
3401c0d9 AZ |
1525 | return; |
1526 | } | |
c8256f9d | 1527 | keycodes[i++] = keycode; |
a3a91a35 | 1528 | } |
3401c0d9 | 1529 | if (!separator) |
a3a91a35 | 1530 | break; |
3401c0d9 | 1531 | string = separator + 1; |
a3a91a35 | 1532 | } |
c8256f9d | 1533 | nb_pending_keycodes = i; |
a3a91a35 | 1534 | /* key down events */ |
c8256f9d | 1535 | for (i = 0; i < nb_pending_keycodes; i++) { |
a3a91a35 FB |
1536 | keycode = keycodes[i]; |
1537 | if (keycode & 0x80) | |
1538 | kbd_put_keycode(0xe0); | |
1539 | kbd_put_keycode(keycode & 0x7f); | |
1540 | } | |
c8256f9d | 1541 | /* delayed key up events */ |
f227f17d | 1542 | qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) + |
6ee093c9 | 1543 | muldiv64(get_ticks_per_sec(), hold_time, 1000)); |
a3a91a35 FB |
1544 | } |
1545 | ||
13224a87 FB |
1546 | static int mouse_button_state; |
1547 | ||
1d4daa91 | 1548 | static void do_mouse_move(Monitor *mon, const QDict *qdict) |
13224a87 FB |
1549 | { |
1550 | int dx, dy, dz; | |
1d4daa91 LC |
1551 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
1552 | const char *dy_str = qdict_get_str(qdict, "dy_str"); | |
1553 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); | |
13224a87 FB |
1554 | dx = strtol(dx_str, NULL, 0); |
1555 | dy = strtol(dy_str, NULL, 0); | |
1556 | dz = 0; | |
5fafdf24 | 1557 | if (dz_str) |
13224a87 FB |
1558 | dz = strtol(dz_str, NULL, 0); |
1559 | kbd_mouse_event(dx, dy, dz, mouse_button_state); | |
1560 | } | |
1561 | ||
d54908a5 | 1562 | static void do_mouse_button(Monitor *mon, const QDict *qdict) |
13224a87 | 1563 | { |
d54908a5 | 1564 | int button_state = qdict_get_int(qdict, "button_state"); |
13224a87 FB |
1565 | mouse_button_state = button_state; |
1566 | kbd_mouse_event(0, 0, 0, mouse_button_state); | |
1567 | } | |
1568 | ||
aa93e39c | 1569 | static void do_ioport_read(Monitor *mon, const QDict *qdict) |
3440557b | 1570 | { |
aa93e39c LC |
1571 | int size = qdict_get_int(qdict, "size"); |
1572 | int addr = qdict_get_int(qdict, "addr"); | |
1573 | int has_index = qdict_haskey(qdict, "index"); | |
3440557b FB |
1574 | uint32_t val; |
1575 | int suffix; | |
1576 | ||
1577 | if (has_index) { | |
aa93e39c | 1578 | int index = qdict_get_int(qdict, "index"); |
afcea8cb | 1579 | cpu_outb(addr & IOPORTS_MASK, index & 0xff); |
3440557b FB |
1580 | addr++; |
1581 | } | |
1582 | addr &= 0xffff; | |
1583 | ||
1584 | switch(size) { | |
1585 | default: | |
1586 | case 1: | |
afcea8cb | 1587 | val = cpu_inb(addr); |
3440557b FB |
1588 | suffix = 'b'; |
1589 | break; | |
1590 | case 2: | |
afcea8cb | 1591 | val = cpu_inw(addr); |
3440557b FB |
1592 | suffix = 'w'; |
1593 | break; | |
1594 | case 4: | |
afcea8cb | 1595 | val = cpu_inl(addr); |
3440557b FB |
1596 | suffix = 'l'; |
1597 | break; | |
1598 | } | |
376253ec AL |
1599 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
1600 | suffix, addr, size * 2, val); | |
3440557b | 1601 | } |
a3a91a35 | 1602 | |
1bd1442e | 1603 | static void do_ioport_write(Monitor *mon, const QDict *qdict) |
f114784f | 1604 | { |
1bd1442e LC |
1605 | int size = qdict_get_int(qdict, "size"); |
1606 | int addr = qdict_get_int(qdict, "addr"); | |
1607 | int val = qdict_get_int(qdict, "val"); | |
1608 | ||
f114784f JK |
1609 | addr &= IOPORTS_MASK; |
1610 | ||
1611 | switch (size) { | |
1612 | default: | |
1613 | case 1: | |
afcea8cb | 1614 | cpu_outb(addr, val); |
f114784f JK |
1615 | break; |
1616 | case 2: | |
afcea8cb | 1617 | cpu_outw(addr, val); |
f114784f JK |
1618 | break; |
1619 | case 4: | |
afcea8cb | 1620 | cpu_outl(addr, val); |
f114784f JK |
1621 | break; |
1622 | } | |
1623 | } | |
1624 | ||
d54908a5 | 1625 | static void do_boot_set(Monitor *mon, const QDict *qdict) |
0ecdffbb AJ |
1626 | { |
1627 | int res; | |
d54908a5 | 1628 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
0ecdffbb | 1629 | |
76e30d0f JK |
1630 | res = qemu_boot_set(bootdevice); |
1631 | if (res == 0) { | |
1632 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); | |
1633 | } else if (res > 0) { | |
1634 | monitor_printf(mon, "setting boot device list failed\n"); | |
0ecdffbb | 1635 | } else { |
376253ec AL |
1636 | monitor_printf(mon, "no function defined to set boot device list for " |
1637 | "this architecture\n"); | |
0ecdffbb AJ |
1638 | } |
1639 | } | |
1640 | ||
c80d259e LC |
1641 | /** |
1642 | * do_system_reset(): Issue a machine reset | |
1643 | */ | |
1644 | static void do_system_reset(Monitor *mon, const QDict *qdict, | |
1645 | QObject **ret_data) | |
e4f9082b FB |
1646 | { |
1647 | qemu_system_reset_request(); | |
1648 | } | |
1649 | ||
43076664 LC |
1650 | /** |
1651 | * do_system_powerdown(): Issue a machine powerdown | |
1652 | */ | |
1653 | static void do_system_powerdown(Monitor *mon, const QDict *qdict, | |
1654 | QObject **ret_data) | |
3475187d FB |
1655 | { |
1656 | qemu_system_powerdown_request(); | |
1657 | } | |
1658 | ||
b86bda5b | 1659 | #if defined(TARGET_I386) |
376253ec | 1660 | static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask) |
b86bda5b | 1661 | { |
376253ec AL |
1662 | monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n", |
1663 | addr, | |
1664 | pte & mask, | |
1665 | pte & PG_GLOBAL_MASK ? 'G' : '-', | |
1666 | pte & PG_PSE_MASK ? 'P' : '-', | |
1667 | pte & PG_DIRTY_MASK ? 'D' : '-', | |
1668 | pte & PG_ACCESSED_MASK ? 'A' : '-', | |
1669 | pte & PG_PCD_MASK ? 'C' : '-', | |
1670 | pte & PG_PWT_MASK ? 'T' : '-', | |
1671 | pte & PG_USER_MASK ? 'U' : '-', | |
1672 | pte & PG_RW_MASK ? 'W' : '-'); | |
b86bda5b FB |
1673 | } |
1674 | ||
376253ec | 1675 | static void tlb_info(Monitor *mon) |
b86bda5b | 1676 | { |
6a00d601 | 1677 | CPUState *env; |
b86bda5b FB |
1678 | int l1, l2; |
1679 | uint32_t pgd, pde, pte; | |
1680 | ||
6a00d601 FB |
1681 | env = mon_get_cpu(); |
1682 | if (!env) | |
1683 | return; | |
1684 | ||
b86bda5b | 1685 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1686 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1687 | return; |
1688 | } | |
1689 | pgd = env->cr[3] & ~0xfff; | |
1690 | for(l1 = 0; l1 < 1024; l1++) { | |
1691 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1692 | pde = le32_to_cpu(pde); | |
1693 | if (pde & PG_PRESENT_MASK) { | |
1694 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
376253ec | 1695 | print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1)); |
b86bda5b FB |
1696 | } else { |
1697 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1698 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1699 | (uint8_t *)&pte, 4); |
1700 | pte = le32_to_cpu(pte); | |
1701 | if (pte & PG_PRESENT_MASK) { | |
376253ec | 1702 | print_pte(mon, (l1 << 22) + (l2 << 12), |
5fafdf24 | 1703 | pte & ~PG_PSE_MASK, |
b86bda5b FB |
1704 | ~0xfff); |
1705 | } | |
1706 | } | |
1707 | } | |
1708 | } | |
1709 | } | |
1710 | } | |
1711 | ||
376253ec | 1712 | static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot, |
b86bda5b FB |
1713 | uint32_t end, int prot) |
1714 | { | |
9746b15b FB |
1715 | int prot1; |
1716 | prot1 = *plast_prot; | |
1717 | if (prot != prot1) { | |
b86bda5b | 1718 | if (*pstart != -1) { |
376253ec AL |
1719 | monitor_printf(mon, "%08x-%08x %08x %c%c%c\n", |
1720 | *pstart, end, end - *pstart, | |
1721 | prot1 & PG_USER_MASK ? 'u' : '-', | |
1722 | 'r', | |
1723 | prot1 & PG_RW_MASK ? 'w' : '-'); | |
b86bda5b FB |
1724 | } |
1725 | if (prot != 0) | |
1726 | *pstart = end; | |
1727 | else | |
1728 | *pstart = -1; | |
1729 | *plast_prot = prot; | |
1730 | } | |
1731 | } | |
1732 | ||
376253ec | 1733 | static void mem_info(Monitor *mon) |
b86bda5b | 1734 | { |
6a00d601 | 1735 | CPUState *env; |
b86bda5b FB |
1736 | int l1, l2, prot, last_prot; |
1737 | uint32_t pgd, pde, pte, start, end; | |
1738 | ||
6a00d601 FB |
1739 | env = mon_get_cpu(); |
1740 | if (!env) | |
1741 | return; | |
1742 | ||
b86bda5b | 1743 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1744 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1745 | return; |
1746 | } | |
1747 | pgd = env->cr[3] & ~0xfff; | |
1748 | last_prot = 0; | |
1749 | start = -1; | |
1750 | for(l1 = 0; l1 < 1024; l1++) { | |
1751 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1752 | pde = le32_to_cpu(pde); | |
1753 | end = l1 << 22; | |
1754 | if (pde & PG_PRESENT_MASK) { | |
1755 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1756 | prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
376253ec | 1757 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1758 | } else { |
1759 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1760 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1761 | (uint8_t *)&pte, 4); |
1762 | pte = le32_to_cpu(pte); | |
1763 | end = (l1 << 22) + (l2 << 12); | |
1764 | if (pte & PG_PRESENT_MASK) { | |
1765 | prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
1766 | } else { | |
1767 | prot = 0; | |
1768 | } | |
376253ec | 1769 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1770 | } |
1771 | } | |
1772 | } else { | |
1773 | prot = 0; | |
376253ec | 1774 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1775 | } |
1776 | } | |
1777 | } | |
1778 | #endif | |
1779 | ||
7c664e2f AJ |
1780 | #if defined(TARGET_SH4) |
1781 | ||
376253ec | 1782 | static void print_tlb(Monitor *mon, int idx, tlb_t *tlb) |
7c664e2f | 1783 | { |
376253ec AL |
1784 | monitor_printf(mon, " tlb%i:\t" |
1785 | "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t" | |
1786 | "v=%hhu shared=%hhu cached=%hhu prot=%hhu " | |
1787 | "dirty=%hhu writethrough=%hhu\n", | |
1788 | idx, | |
1789 | tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size, | |
1790 | tlb->v, tlb->sh, tlb->c, tlb->pr, | |
1791 | tlb->d, tlb->wt); | |
7c664e2f AJ |
1792 | } |
1793 | ||
376253ec | 1794 | static void tlb_info(Monitor *mon) |
7c664e2f AJ |
1795 | { |
1796 | CPUState *env = mon_get_cpu(); | |
1797 | int i; | |
1798 | ||
376253ec | 1799 | monitor_printf (mon, "ITLB:\n"); |
7c664e2f | 1800 | for (i = 0 ; i < ITLB_SIZE ; i++) |
376253ec AL |
1801 | print_tlb (mon, i, &env->itlb[i]); |
1802 | monitor_printf (mon, "UTLB:\n"); | |
7c664e2f | 1803 | for (i = 0 ; i < UTLB_SIZE ; i++) |
376253ec | 1804 | print_tlb (mon, i, &env->utlb[i]); |
7c664e2f AJ |
1805 | } |
1806 | ||
1807 | #endif | |
1808 | ||
2af5ba71 | 1809 | static void do_info_kvm_print(Monitor *mon, const QObject *data) |
7ba1e619 | 1810 | { |
2af5ba71 LC |
1811 | QDict *qdict; |
1812 | ||
1813 | qdict = qobject_to_qdict(data); | |
1814 | ||
376253ec | 1815 | monitor_printf(mon, "kvm support: "); |
2af5ba71 LC |
1816 | if (qdict_get_bool(qdict, "present")) { |
1817 | monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ? | |
1818 | "enabled" : "disabled"); | |
1819 | } else { | |
1820 | monitor_printf(mon, "not compiled\n"); | |
1821 | } | |
1822 | } | |
1823 | ||
1824 | /** | |
1825 | * do_info_kvm(): Show KVM information | |
1826 | * | |
1827 | * Return a QDict with the following information: | |
1828 | * | |
1829 | * - "enabled": true if KVM support is enabled, false otherwise | |
1830 | * - "present": true if QEMU has KVM support, false otherwise | |
1831 | * | |
1832 | * Example: | |
1833 | * | |
1834 | * { "enabled": true, "present": true } | |
1835 | */ | |
1836 | static void do_info_kvm(Monitor *mon, QObject **ret_data) | |
1837 | { | |
1838 | #ifdef CONFIG_KVM | |
1839 | *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }", | |
1840 | kvm_enabled()); | |
7ba1e619 | 1841 | #else |
2af5ba71 | 1842 | *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }"); |
7ba1e619 AL |
1843 | #endif |
1844 | } | |
1845 | ||
030ea37b AL |
1846 | static void do_info_numa(Monitor *mon) |
1847 | { | |
b28b6230 | 1848 | int i; |
030ea37b AL |
1849 | CPUState *env; |
1850 | ||
1851 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); | |
1852 | for (i = 0; i < nb_numa_nodes; i++) { | |
1853 | monitor_printf(mon, "node %d cpus:", i); | |
1854 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
1855 | if (env->numa_node == i) { | |
1856 | monitor_printf(mon, " %d", env->cpu_index); | |
1857 | } | |
1858 | } | |
1859 | monitor_printf(mon, "\n"); | |
1860 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, | |
1861 | node_mem[i] >> 20); | |
1862 | } | |
1863 | } | |
1864 | ||
5f1ce948 FB |
1865 | #ifdef CONFIG_PROFILER |
1866 | ||
e9a6625e AJ |
1867 | int64_t qemu_time; |
1868 | int64_t dev_time; | |
1869 | ||
376253ec | 1870 | static void do_info_profile(Monitor *mon) |
5f1ce948 FB |
1871 | { |
1872 | int64_t total; | |
1873 | total = qemu_time; | |
1874 | if (total == 0) | |
1875 | total = 1; | |
376253ec | 1876 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 1877 | dev_time, dev_time / (double)get_ticks_per_sec()); |
376253ec | 1878 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 1879 | qemu_time, qemu_time / (double)get_ticks_per_sec()); |
5f1ce948 | 1880 | qemu_time = 0; |
5f1ce948 | 1881 | dev_time = 0; |
5f1ce948 FB |
1882 | } |
1883 | #else | |
376253ec | 1884 | static void do_info_profile(Monitor *mon) |
5f1ce948 | 1885 | { |
376253ec | 1886 | monitor_printf(mon, "Internal profiler not compiled\n"); |
5f1ce948 FB |
1887 | } |
1888 | #endif | |
1889 | ||
ec36b695 | 1890 | /* Capture support */ |
72cf2d4f | 1891 | static QLIST_HEAD (capture_list_head, CaptureState) capture_head; |
ec36b695 | 1892 | |
376253ec | 1893 | static void do_info_capture(Monitor *mon) |
ec36b695 FB |
1894 | { |
1895 | int i; | |
1896 | CaptureState *s; | |
1897 | ||
1898 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
376253ec | 1899 | monitor_printf(mon, "[%d]: ", i); |
ec36b695 FB |
1900 | s->ops.info (s->opaque); |
1901 | } | |
1902 | } | |
1903 | ||
2313086a | 1904 | #ifdef HAS_AUDIO |
d54908a5 | 1905 | static void do_stop_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
1906 | { |
1907 | int i; | |
d54908a5 | 1908 | int n = qdict_get_int(qdict, "n"); |
ec36b695 FB |
1909 | CaptureState *s; |
1910 | ||
1911 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
1912 | if (i == n) { | |
1913 | s->ops.destroy (s->opaque); | |
72cf2d4f | 1914 | QLIST_REMOVE (s, entries); |
ec36b695 FB |
1915 | qemu_free (s); |
1916 | return; | |
1917 | } | |
1918 | } | |
1919 | } | |
1920 | ||
c1925484 LC |
1921 | static void do_wav_capture(Monitor *mon, const QDict *qdict) |
1922 | { | |
1923 | const char *path = qdict_get_str(qdict, "path"); | |
1924 | int has_freq = qdict_haskey(qdict, "freq"); | |
1925 | int freq = qdict_get_try_int(qdict, "freq", -1); | |
1926 | int has_bits = qdict_haskey(qdict, "bits"); | |
1927 | int bits = qdict_get_try_int(qdict, "bits", -1); | |
1928 | int has_channels = qdict_haskey(qdict, "nchannels"); | |
1929 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); | |
ec36b695 FB |
1930 | CaptureState *s; |
1931 | ||
1932 | s = qemu_mallocz (sizeof (*s)); | |
ec36b695 FB |
1933 | |
1934 | freq = has_freq ? freq : 44100; | |
1935 | bits = has_bits ? bits : 16; | |
1936 | nchannels = has_channels ? nchannels : 2; | |
1937 | ||
1938 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
376253ec | 1939 | monitor_printf(mon, "Faied to add wave capture\n"); |
ec36b695 FB |
1940 | qemu_free (s); |
1941 | } | |
72cf2d4f | 1942 | QLIST_INSERT_HEAD (&capture_head, s, entries); |
ec36b695 FB |
1943 | } |
1944 | #endif | |
1945 | ||
dc1c0b74 | 1946 | #if defined(TARGET_I386) |
d54908a5 | 1947 | static void do_inject_nmi(Monitor *mon, const QDict *qdict) |
dc1c0b74 AJ |
1948 | { |
1949 | CPUState *env; | |
d54908a5 | 1950 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
dc1c0b74 AJ |
1951 | |
1952 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
1953 | if (env->cpu_index == cpu_index) { | |
1954 | cpu_interrupt(env, CPU_INTERRUPT_NMI); | |
1955 | break; | |
1956 | } | |
1957 | } | |
1958 | #endif | |
1959 | ||
c0e8520e | 1960 | static void do_info_status_print(Monitor *mon, const QObject *data) |
6f9c5ee7 | 1961 | { |
c0e8520e LC |
1962 | QDict *qdict; |
1963 | ||
1964 | qdict = qobject_to_qdict(data); | |
1965 | ||
1966 | monitor_printf(mon, "VM status: "); | |
1967 | if (qdict_get_bool(qdict, "running")) { | |
1968 | monitor_printf(mon, "running"); | |
1969 | if (qdict_get_bool(qdict, "singlestep")) { | |
1970 | monitor_printf(mon, " (single step mode)"); | |
1b530a6d | 1971 | } |
c0e8520e LC |
1972 | } else { |
1973 | monitor_printf(mon, "paused"); | |
1974 | } | |
1975 | ||
1976 | monitor_printf(mon, "\n"); | |
1977 | } | |
1978 | ||
1979 | /** | |
1980 | * do_info_status(): VM status | |
1981 | * | |
1982 | * Return a QDict with the following information: | |
1983 | * | |
1984 | * - "running": true if the VM is running, or false if it is paused | |
1985 | * - "singlestep": true if the VM is in single step mode, false otherwise | |
1986 | * | |
1987 | * Example: | |
1988 | * | |
1989 | * { "running": true, "singlestep": false } | |
1990 | */ | |
1991 | static void do_info_status(Monitor *mon, QObject **ret_data) | |
1992 | { | |
1993 | *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }", | |
1994 | vm_running, singlestep); | |
6f9c5ee7 AJ |
1995 | } |
1996 | ||
83fb1de2 LC |
1997 | /** |
1998 | * do_balloon(): Request VM to change its memory allocation | |
1999 | */ | |
2000 | static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
df751fa8 | 2001 | { |
d54908a5 | 2002 | int value = qdict_get_int(qdict, "value"); |
c227f099 | 2003 | ram_addr_t target = value; |
df751fa8 AL |
2004 | qemu_balloon(target << 20); |
2005 | } | |
2006 | ||
cc1d9c70 LC |
2007 | static void monitor_print_balloon(Monitor *mon, const QObject *data) |
2008 | { | |
7f179671 LC |
2009 | QDict *qdict; |
2010 | ||
2011 | qdict = qobject_to_qdict(data); | |
2012 | ||
2013 | monitor_printf(mon, "balloon: actual=%" PRId64 "\n", | |
2014 | qdict_get_int(qdict, "balloon") >> 20); | |
cc1d9c70 LC |
2015 | } |
2016 | ||
2017 | /** | |
2018 | * do_info_balloon(): Balloon information | |
7f179671 LC |
2019 | * |
2020 | * Return a QDict with the following information: | |
2021 | * | |
2022 | * - "balloon": current balloon value in bytes | |
2023 | * | |
2024 | * Example: | |
2025 | * | |
2026 | * { "balloon": 1073741824 } | |
cc1d9c70 LC |
2027 | */ |
2028 | static void do_info_balloon(Monitor *mon, QObject **ret_data) | |
df751fa8 | 2029 | { |
c227f099 | 2030 | ram_addr_t actual; |
df751fa8 AL |
2031 | |
2032 | actual = qemu_balloon_status(); | |
bd322087 | 2033 | if (kvm_enabled() && !kvm_has_sync_mmu()) |
5d6c37fb | 2034 | qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); |
bd322087 | 2035 | else if (actual == 0) |
5d6c37fb | 2036 | qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon"); |
df751fa8 | 2037 | else |
7f179671 LC |
2038 | *ret_data = qobject_from_jsonf("{ 'balloon': %" PRId64 "}", |
2039 | (int64_t) actual); | |
df751fa8 AL |
2040 | } |
2041 | ||
15dfcd45 | 2042 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
76655d6d | 2043 | { |
15dfcd45 | 2044 | qemu_acl *acl = qemu_acl_find(name); |
76655d6d | 2045 | |
76655d6d | 2046 | if (!acl) { |
15dfcd45 | 2047 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
76655d6d | 2048 | } |
15dfcd45 JK |
2049 | return acl; |
2050 | } | |
2051 | ||
d54908a5 | 2052 | static void do_acl_show(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2053 | { |
d54908a5 | 2054 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2055 | qemu_acl *acl = find_acl(mon, aclname); |
2056 | qemu_acl_entry *entry; | |
2057 | int i = 0; | |
76655d6d | 2058 | |
15dfcd45 | 2059 | if (acl) { |
28a76be8 | 2060 | monitor_printf(mon, "policy: %s\n", |
76655d6d | 2061 | acl->defaultDeny ? "deny" : "allow"); |
72cf2d4f | 2062 | QTAILQ_FOREACH(entry, &acl->entries, next) { |
28a76be8 AL |
2063 | i++; |
2064 | monitor_printf(mon, "%d: %s %s\n", i, | |
15dfcd45 | 2065 | entry->deny ? "deny" : "allow", entry->match); |
28a76be8 | 2066 | } |
15dfcd45 JK |
2067 | } |
2068 | } | |
2069 | ||
d54908a5 | 2070 | static void do_acl_reset(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2071 | { |
d54908a5 | 2072 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2073 | qemu_acl *acl = find_acl(mon, aclname); |
2074 | ||
2075 | if (acl) { | |
28a76be8 AL |
2076 | qemu_acl_reset(acl); |
2077 | monitor_printf(mon, "acl: removed all rules\n"); | |
15dfcd45 JK |
2078 | } |
2079 | } | |
2080 | ||
f18c16de | 2081 | static void do_acl_policy(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2082 | { |
f18c16de LC |
2083 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2084 | const char *policy = qdict_get_str(qdict, "policy"); | |
15dfcd45 | 2085 | qemu_acl *acl = find_acl(mon, aclname); |
28a76be8 | 2086 | |
15dfcd45 JK |
2087 | if (acl) { |
2088 | if (strcmp(policy, "allow") == 0) { | |
28a76be8 AL |
2089 | acl->defaultDeny = 0; |
2090 | monitor_printf(mon, "acl: policy set to 'allow'\n"); | |
15dfcd45 | 2091 | } else if (strcmp(policy, "deny") == 0) { |
28a76be8 AL |
2092 | acl->defaultDeny = 1; |
2093 | monitor_printf(mon, "acl: policy set to 'deny'\n"); | |
2094 | } else { | |
15dfcd45 JK |
2095 | monitor_printf(mon, "acl: unknown policy '%s', " |
2096 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 | 2097 | } |
15dfcd45 JK |
2098 | } |
2099 | } | |
28a76be8 | 2100 | |
1bd1442e | 2101 | static void do_acl_add(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2102 | { |
1bd1442e LC |
2103 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2104 | const char *match = qdict_get_str(qdict, "match"); | |
2105 | const char *policy = qdict_get_str(qdict, "policy"); | |
2106 | int has_index = qdict_haskey(qdict, "index"); | |
2107 | int index = qdict_get_try_int(qdict, "index", -1); | |
15dfcd45 JK |
2108 | qemu_acl *acl = find_acl(mon, aclname); |
2109 | int deny, ret; | |
2110 | ||
2111 | if (acl) { | |
2112 | if (strcmp(policy, "allow") == 0) { | |
2113 | deny = 0; | |
2114 | } else if (strcmp(policy, "deny") == 0) { | |
2115 | deny = 1; | |
2116 | } else { | |
2117 | monitor_printf(mon, "acl: unknown policy '%s', " | |
2118 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 AL |
2119 | return; |
2120 | } | |
28a76be8 AL |
2121 | if (has_index) |
2122 | ret = qemu_acl_insert(acl, deny, match, index); | |
2123 | else | |
2124 | ret = qemu_acl_append(acl, deny, match); | |
2125 | if (ret < 0) | |
2126 | monitor_printf(mon, "acl: unable to add acl entry\n"); | |
2127 | else | |
2128 | monitor_printf(mon, "acl: added rule at position %d\n", ret); | |
15dfcd45 JK |
2129 | } |
2130 | } | |
28a76be8 | 2131 | |
f18c16de | 2132 | static void do_acl_remove(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2133 | { |
f18c16de LC |
2134 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2135 | const char *match = qdict_get_str(qdict, "match"); | |
15dfcd45 JK |
2136 | qemu_acl *acl = find_acl(mon, aclname); |
2137 | int ret; | |
28a76be8 | 2138 | |
15dfcd45 | 2139 | if (acl) { |
28a76be8 AL |
2140 | ret = qemu_acl_remove(acl, match); |
2141 | if (ret < 0) | |
2142 | monitor_printf(mon, "acl: no matching acl entry\n"); | |
2143 | else | |
2144 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); | |
76655d6d AL |
2145 | } |
2146 | } | |
2147 | ||
79c4f6b0 | 2148 | #if defined(TARGET_I386) |
37b7ad48 | 2149 | static void do_inject_mce(Monitor *mon, const QDict *qdict) |
79c4f6b0 HY |
2150 | { |
2151 | CPUState *cenv; | |
37b7ad48 LC |
2152 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
2153 | int bank = qdict_get_int(qdict, "bank"); | |
2154 | uint64_t status = qdict_get_int(qdict, "status"); | |
2155 | uint64_t mcg_status = qdict_get_int(qdict, "mcg_status"); | |
2156 | uint64_t addr = qdict_get_int(qdict, "addr"); | |
2157 | uint64_t misc = qdict_get_int(qdict, "misc"); | |
79c4f6b0 HY |
2158 | |
2159 | for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) | |
2160 | if (cenv->cpu_index == cpu_index && cenv->mcg_cap) { | |
2161 | cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc); | |
2162 | break; | |
2163 | } | |
2164 | } | |
2165 | #endif | |
2166 | ||
f0d6000a | 2167 | static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data) |
f07918fd | 2168 | { |
d54908a5 | 2169 | const char *fdname = qdict_get_str(qdict, "fdname"); |
c227f099 | 2170 | mon_fd_t *monfd; |
f07918fd MM |
2171 | int fd; |
2172 | ||
2173 | fd = qemu_chr_get_msgfd(mon->chr); | |
2174 | if (fd == -1) { | |
7cdfcfe1 | 2175 | qemu_error_new(QERR_FD_NOT_SUPPLIED); |
f07918fd MM |
2176 | return; |
2177 | } | |
2178 | ||
2179 | if (qemu_isdigit(fdname[0])) { | |
7cdfcfe1 | 2180 | qemu_error_new(QERR_INVALID_PARAMETER, "fdname"); |
f07918fd MM |
2181 | return; |
2182 | } | |
2183 | ||
2184 | fd = dup(fd); | |
2185 | if (fd == -1) { | |
7cdfcfe1 MA |
2186 | if (errno == EMFILE) |
2187 | qemu_error_new(QERR_TOO_MANY_FILES); | |
2188 | else | |
2189 | qemu_error_new(QERR_UNDEFINED_ERROR); | |
f07918fd MM |
2190 | return; |
2191 | } | |
2192 | ||
72cf2d4f | 2193 | QLIST_FOREACH(monfd, &mon->fds, next) { |
f07918fd MM |
2194 | if (strcmp(monfd->name, fdname) != 0) { |
2195 | continue; | |
2196 | } | |
2197 | ||
2198 | close(monfd->fd); | |
2199 | monfd->fd = fd; | |
2200 | return; | |
2201 | } | |
2202 | ||
c227f099 | 2203 | monfd = qemu_mallocz(sizeof(mon_fd_t)); |
f07918fd MM |
2204 | monfd->name = qemu_strdup(fdname); |
2205 | monfd->fd = fd; | |
2206 | ||
72cf2d4f | 2207 | QLIST_INSERT_HEAD(&mon->fds, monfd, next); |
f07918fd MM |
2208 | } |
2209 | ||
18f3a515 | 2210 | static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data) |
f07918fd | 2211 | { |
d54908a5 | 2212 | const char *fdname = qdict_get_str(qdict, "fdname"); |
c227f099 | 2213 | mon_fd_t *monfd; |
f07918fd | 2214 | |
72cf2d4f | 2215 | QLIST_FOREACH(monfd, &mon->fds, next) { |
f07918fd MM |
2216 | if (strcmp(monfd->name, fdname) != 0) { |
2217 | continue; | |
2218 | } | |
2219 | ||
72cf2d4f | 2220 | QLIST_REMOVE(monfd, next); |
f07918fd MM |
2221 | close(monfd->fd); |
2222 | qemu_free(monfd->name); | |
2223 | qemu_free(monfd); | |
2224 | return; | |
2225 | } | |
2226 | ||
063c1a09 | 2227 | qemu_error_new(QERR_FD_NOT_FOUND, fdname); |
f07918fd MM |
2228 | } |
2229 | ||
d54908a5 | 2230 | static void do_loadvm(Monitor *mon, const QDict *qdict) |
c8d41b2c JQ |
2231 | { |
2232 | int saved_vm_running = vm_running; | |
d54908a5 | 2233 | const char *name = qdict_get_str(qdict, "name"); |
c8d41b2c JQ |
2234 | |
2235 | vm_stop(0); | |
2236 | ||
05f2401e | 2237 | if (load_vmstate(mon, name) >= 0 && saved_vm_running) |
c8d41b2c JQ |
2238 | vm_start(); |
2239 | } | |
2240 | ||
7768e04c MM |
2241 | int monitor_get_fd(Monitor *mon, const char *fdname) |
2242 | { | |
c227f099 | 2243 | mon_fd_t *monfd; |
7768e04c | 2244 | |
72cf2d4f | 2245 | QLIST_FOREACH(monfd, &mon->fds, next) { |
7768e04c MM |
2246 | int fd; |
2247 | ||
2248 | if (strcmp(monfd->name, fdname) != 0) { | |
2249 | continue; | |
2250 | } | |
2251 | ||
2252 | fd = monfd->fd; | |
2253 | ||
2254 | /* caller takes ownership of fd */ | |
72cf2d4f | 2255 | QLIST_REMOVE(monfd, next); |
7768e04c MM |
2256 | qemu_free(monfd->name); |
2257 | qemu_free(monfd); | |
2258 | ||
2259 | return fd; | |
2260 | } | |
2261 | ||
2262 | return -1; | |
2263 | } | |
2264 | ||
c227f099 | 2265 | static const mon_cmd_t mon_cmds[] = { |
2313086a | 2266 | #include "qemu-monitor.h" |
5fafdf24 | 2267 | { NULL, NULL, }, |
9dc39cba FB |
2268 | }; |
2269 | ||
2313086a | 2270 | /* Please update qemu-monitor.hx when adding or changing commands */ |
c227f099 | 2271 | static const mon_cmd_t info_cmds[] = { |
d7f9b689 LC |
2272 | { |
2273 | .name = "version", | |
2274 | .args_type = "", | |
d7f9b689 LC |
2275 | .params = "", |
2276 | .help = "show the version of QEMU", | |
45e914cf | 2277 | .user_print = do_info_version_print, |
ab2d3187 | 2278 | .mhandler.info_new = do_info_version, |
d7f9b689 | 2279 | }, |
e3bba9d0 LC |
2280 | { |
2281 | .name = "commands", | |
2282 | .args_type = "", | |
2283 | .params = "", | |
2284 | .help = "list QMP available commands", | |
2285 | .user_print = monitor_user_noop, | |
2286 | .mhandler.info_new = do_info_commands, | |
2287 | }, | |
d7f9b689 LC |
2288 | { |
2289 | .name = "network", | |
2290 | .args_type = "", | |
d7f9b689 LC |
2291 | .params = "", |
2292 | .help = "show the network state", | |
910df89d | 2293 | .mhandler.info = do_info_network, |
d7f9b689 LC |
2294 | }, |
2295 | { | |
2296 | .name = "chardev", | |
2297 | .args_type = "", | |
d7f9b689 LC |
2298 | .params = "", |
2299 | .help = "show the character devices", | |
910df89d | 2300 | .mhandler.info = qemu_chr_info, |
d7f9b689 LC |
2301 | }, |
2302 | { | |
2303 | .name = "block", | |
2304 | .args_type = "", | |
d7f9b689 LC |
2305 | .params = "", |
2306 | .help = "show the block devices", | |
910df89d | 2307 | .mhandler.info = bdrv_info, |
d7f9b689 LC |
2308 | }, |
2309 | { | |
2310 | .name = "blockstats", | |
2311 | .args_type = "", | |
d7f9b689 LC |
2312 | .params = "", |
2313 | .help = "show block device statistics", | |
910df89d | 2314 | .mhandler.info = bdrv_info_stats, |
d7f9b689 LC |
2315 | }, |
2316 | { | |
2317 | .name = "registers", | |
2318 | .args_type = "", | |
d7f9b689 LC |
2319 | .params = "", |
2320 | .help = "show the cpu registers", | |
910df89d | 2321 | .mhandler.info = do_info_registers, |
d7f9b689 LC |
2322 | }, |
2323 | { | |
2324 | .name = "cpus", | |
2325 | .args_type = "", | |
d7f9b689 LC |
2326 | .params = "", |
2327 | .help = "show infos for each CPU", | |
8f3cec0b LC |
2328 | .user_print = monitor_print_cpus, |
2329 | .mhandler.info_new = do_info_cpus, | |
d7f9b689 LC |
2330 | }, |
2331 | { | |
2332 | .name = "history", | |
2333 | .args_type = "", | |
d7f9b689 LC |
2334 | .params = "", |
2335 | .help = "show the command line history", | |
910df89d | 2336 | .mhandler.info = do_info_history, |
d7f9b689 LC |
2337 | }, |
2338 | { | |
2339 | .name = "irq", | |
2340 | .args_type = "", | |
d7f9b689 LC |
2341 | .params = "", |
2342 | .help = "show the interrupts statistics (if available)", | |
910df89d | 2343 | .mhandler.info = irq_info, |
d7f9b689 LC |
2344 | }, |
2345 | { | |
2346 | .name = "pic", | |
2347 | .args_type = "", | |
d7f9b689 LC |
2348 | .params = "", |
2349 | .help = "show i8259 (PIC) state", | |
910df89d | 2350 | .mhandler.info = pic_info, |
d7f9b689 LC |
2351 | }, |
2352 | { | |
2353 | .name = "pci", | |
2354 | .args_type = "", | |
d7f9b689 LC |
2355 | .params = "", |
2356 | .help = "show PCI info", | |
910df89d | 2357 | .mhandler.info = pci_info, |
d7f9b689 | 2358 | }, |
7c664e2f | 2359 | #if defined(TARGET_I386) || defined(TARGET_SH4) |
d7f9b689 LC |
2360 | { |
2361 | .name = "tlb", | |
2362 | .args_type = "", | |
d7f9b689 LC |
2363 | .params = "", |
2364 | .help = "show virtual to physical memory mappings", | |
910df89d | 2365 | .mhandler.info = tlb_info, |
d7f9b689 | 2366 | }, |
7c664e2f AJ |
2367 | #endif |
2368 | #if defined(TARGET_I386) | |
d7f9b689 LC |
2369 | { |
2370 | .name = "mem", | |
2371 | .args_type = "", | |
d7f9b689 LC |
2372 | .params = "", |
2373 | .help = "show the active virtual memory mappings", | |
910df89d | 2374 | .mhandler.info = mem_info, |
d7f9b689 LC |
2375 | }, |
2376 | { | |
2377 | .name = "hpet", | |
2378 | .args_type = "", | |
d7f9b689 LC |
2379 | .params = "", |
2380 | .help = "show state of HPET", | |
910df89d | 2381 | .mhandler.info = do_info_hpet, |
d7f9b689 | 2382 | }, |
b86bda5b | 2383 | #endif |
d7f9b689 LC |
2384 | { |
2385 | .name = "jit", | |
2386 | .args_type = "", | |
d7f9b689 LC |
2387 | .params = "", |
2388 | .help = "show dynamic compiler info", | |
910df89d | 2389 | .mhandler.info = do_info_jit, |
d7f9b689 LC |
2390 | }, |
2391 | { | |
2392 | .name = "kvm", | |
2393 | .args_type = "", | |
d7f9b689 LC |
2394 | .params = "", |
2395 | .help = "show KVM information", | |
2af5ba71 LC |
2396 | .user_print = do_info_kvm_print, |
2397 | .mhandler.info_new = do_info_kvm, | |
d7f9b689 LC |
2398 | }, |
2399 | { | |
2400 | .name = "numa", | |
2401 | .args_type = "", | |
d7f9b689 LC |
2402 | .params = "", |
2403 | .help = "show NUMA information", | |
910df89d | 2404 | .mhandler.info = do_info_numa, |
d7f9b689 LC |
2405 | }, |
2406 | { | |
2407 | .name = "usb", | |
2408 | .args_type = "", | |
d7f9b689 LC |
2409 | .params = "", |
2410 | .help = "show guest USB devices", | |
910df89d | 2411 | .mhandler.info = usb_info, |
d7f9b689 LC |
2412 | }, |
2413 | { | |
2414 | .name = "usbhost", | |
2415 | .args_type = "", | |
d7f9b689 LC |
2416 | .params = "", |
2417 | .help = "show host USB devices", | |
910df89d | 2418 | .mhandler.info = usb_host_info, |
d7f9b689 LC |
2419 | }, |
2420 | { | |
2421 | .name = "profile", | |
2422 | .args_type = "", | |
d7f9b689 LC |
2423 | .params = "", |
2424 | .help = "show profiling information", | |
910df89d | 2425 | .mhandler.info = do_info_profile, |
d7f9b689 LC |
2426 | }, |
2427 | { | |
2428 | .name = "capture", | |
2429 | .args_type = "", | |
d7f9b689 LC |
2430 | .params = "", |
2431 | .help = "show capture information", | |
910df89d | 2432 | .mhandler.info = do_info_capture, |
d7f9b689 LC |
2433 | }, |
2434 | { | |
2435 | .name = "snapshots", | |
2436 | .args_type = "", | |
d7f9b689 LC |
2437 | .params = "", |
2438 | .help = "show the currently saved VM snapshots", | |
910df89d | 2439 | .mhandler.info = do_info_snapshots, |
d7f9b689 LC |
2440 | }, |
2441 | { | |
2442 | .name = "status", | |
2443 | .args_type = "", | |
d7f9b689 LC |
2444 | .params = "", |
2445 | .help = "show the current VM status (running|paused)", | |
c0e8520e LC |
2446 | .user_print = do_info_status_print, |
2447 | .mhandler.info_new = do_info_status, | |
d7f9b689 LC |
2448 | }, |
2449 | { | |
2450 | .name = "pcmcia", | |
2451 | .args_type = "", | |
d7f9b689 LC |
2452 | .params = "", |
2453 | .help = "show guest PCMCIA status", | |
910df89d | 2454 | .mhandler.info = pcmcia_info, |
d7f9b689 LC |
2455 | }, |
2456 | { | |
2457 | .name = "mice", | |
2458 | .args_type = "", | |
d7f9b689 LC |
2459 | .params = "", |
2460 | .help = "show which guest mouse is receiving events", | |
910df89d | 2461 | .mhandler.info = do_info_mice, |
d7f9b689 LC |
2462 | }, |
2463 | { | |
2464 | .name = "vnc", | |
2465 | .args_type = "", | |
d7f9b689 LC |
2466 | .params = "", |
2467 | .help = "show the vnc server status", | |
910df89d | 2468 | .mhandler.info = do_info_vnc, |
d7f9b689 LC |
2469 | }, |
2470 | { | |
2471 | .name = "name", | |
2472 | .args_type = "", | |
d7f9b689 LC |
2473 | .params = "", |
2474 | .help = "show the current VM name", | |
910df89d | 2475 | .mhandler.info = do_info_name, |
d7f9b689 LC |
2476 | }, |
2477 | { | |
2478 | .name = "uuid", | |
2479 | .args_type = "", | |
d7f9b689 LC |
2480 | .params = "", |
2481 | .help = "show the current VM UUID", | |
910df89d | 2482 | .mhandler.info = do_info_uuid, |
d7f9b689 | 2483 | }, |
76a66253 | 2484 | #if defined(TARGET_PPC) |
d7f9b689 LC |
2485 | { |
2486 | .name = "cpustats", | |
2487 | .args_type = "", | |
d7f9b689 LC |
2488 | .params = "", |
2489 | .help = "show CPU statistics", | |
910df89d | 2490 | .mhandler.info = do_info_cpu_stats, |
d7f9b689 | 2491 | }, |
76a66253 | 2492 | #endif |
31a60e22 | 2493 | #if defined(CONFIG_SLIRP) |
d7f9b689 LC |
2494 | { |
2495 | .name = "usernet", | |
2496 | .args_type = "", | |
d7f9b689 LC |
2497 | .params = "", |
2498 | .help = "show user network stack connection states", | |
910df89d | 2499 | .mhandler.info = do_info_usernet, |
d7f9b689 | 2500 | }, |
31a60e22 | 2501 | #endif |
d7f9b689 LC |
2502 | { |
2503 | .name = "migrate", | |
2504 | .args_type = "", | |
d7f9b689 LC |
2505 | .params = "", |
2506 | .help = "show migration status", | |
910df89d | 2507 | .mhandler.info = do_info_migrate, |
d7f9b689 LC |
2508 | }, |
2509 | { | |
2510 | .name = "balloon", | |
2511 | .args_type = "", | |
d7f9b689 LC |
2512 | .params = "", |
2513 | .help = "show balloon information", | |
cc1d9c70 LC |
2514 | .user_print = monitor_print_balloon, |
2515 | .mhandler.info_new = do_info_balloon, | |
d7f9b689 LC |
2516 | }, |
2517 | { | |
2518 | .name = "qtree", | |
2519 | .args_type = "", | |
d7f9b689 LC |
2520 | .params = "", |
2521 | .help = "show device tree", | |
910df89d | 2522 | .mhandler.info = do_info_qtree, |
d7f9b689 LC |
2523 | }, |
2524 | { | |
2525 | .name = "qdm", | |
2526 | .args_type = "", | |
d7f9b689 LC |
2527 | .params = "", |
2528 | .help = "show qdev device model list", | |
910df89d | 2529 | .mhandler.info = do_info_qdm, |
d7f9b689 LC |
2530 | }, |
2531 | { | |
2532 | .name = "roms", | |
2533 | .args_type = "", | |
d7f9b689 LC |
2534 | .params = "", |
2535 | .help = "show roms", | |
910df89d | 2536 | .mhandler.info = do_info_roms, |
d7f9b689 LC |
2537 | }, |
2538 | { | |
2539 | .name = NULL, | |
2540 | }, | |
9dc39cba FB |
2541 | }; |
2542 | ||
9307c4c1 FB |
2543 | /*******************************************************************/ |
2544 | ||
2545 | static const char *pch; | |
2546 | static jmp_buf expr_env; | |
2547 | ||
92a31b1f FB |
2548 | #define MD_TLONG 0 |
2549 | #define MD_I32 1 | |
2550 | ||
9307c4c1 FB |
2551 | typedef struct MonitorDef { |
2552 | const char *name; | |
2553 | int offset; | |
8662d656 | 2554 | target_long (*get_value)(const struct MonitorDef *md, int val); |
92a31b1f | 2555 | int type; |
9307c4c1 FB |
2556 | } MonitorDef; |
2557 | ||
57206fd4 | 2558 | #if defined(TARGET_I386) |
8662d656 | 2559 | static target_long monitor_get_pc (const struct MonitorDef *md, int val) |
57206fd4 | 2560 | { |
6a00d601 FB |
2561 | CPUState *env = mon_get_cpu(); |
2562 | if (!env) | |
2563 | return 0; | |
2564 | return env->eip + env->segs[R_CS].base; | |
57206fd4 FB |
2565 | } |
2566 | #endif | |
2567 | ||
a541f297 | 2568 | #if defined(TARGET_PPC) |
8662d656 | 2569 | static target_long monitor_get_ccr (const struct MonitorDef *md, int val) |
a541f297 | 2570 | { |
6a00d601 | 2571 | CPUState *env = mon_get_cpu(); |
a541f297 FB |
2572 | unsigned int u; |
2573 | int i; | |
2574 | ||
6a00d601 FB |
2575 | if (!env) |
2576 | return 0; | |
2577 | ||
a541f297 FB |
2578 | u = 0; |
2579 | for (i = 0; i < 8; i++) | |
28a76be8 | 2580 | u |= env->crf[i] << (32 - (4 * i)); |
a541f297 FB |
2581 | |
2582 | return u; | |
2583 | } | |
2584 | ||
8662d656 | 2585 | static target_long monitor_get_msr (const struct MonitorDef *md, int val) |
a541f297 | 2586 | { |
6a00d601 FB |
2587 | CPUState *env = mon_get_cpu(); |
2588 | if (!env) | |
2589 | return 0; | |
0411a972 | 2590 | return env->msr; |
a541f297 FB |
2591 | } |
2592 | ||
8662d656 | 2593 | static target_long monitor_get_xer (const struct MonitorDef *md, int val) |
a541f297 | 2594 | { |
6a00d601 FB |
2595 | CPUState *env = mon_get_cpu(); |
2596 | if (!env) | |
2597 | return 0; | |
3d7b417e | 2598 | return env->xer; |
a541f297 | 2599 | } |
9fddaa0c | 2600 | |
8662d656 | 2601 | static target_long monitor_get_decr (const struct MonitorDef *md, int val) |
9fddaa0c | 2602 | { |
6a00d601 FB |
2603 | CPUState *env = mon_get_cpu(); |
2604 | if (!env) | |
2605 | return 0; | |
2606 | return cpu_ppc_load_decr(env); | |
9fddaa0c FB |
2607 | } |
2608 | ||
8662d656 | 2609 | static target_long monitor_get_tbu (const struct MonitorDef *md, int val) |
9fddaa0c | 2610 | { |
6a00d601 FB |
2611 | CPUState *env = mon_get_cpu(); |
2612 | if (!env) | |
2613 | return 0; | |
2614 | return cpu_ppc_load_tbu(env); | |
9fddaa0c FB |
2615 | } |
2616 | ||
8662d656 | 2617 | static target_long monitor_get_tbl (const struct MonitorDef *md, int val) |
9fddaa0c | 2618 | { |
6a00d601 FB |
2619 | CPUState *env = mon_get_cpu(); |
2620 | if (!env) | |
2621 | return 0; | |
2622 | return cpu_ppc_load_tbl(env); | |
9fddaa0c | 2623 | } |
a541f297 FB |
2624 | #endif |
2625 | ||
e95c8d51 | 2626 | #if defined(TARGET_SPARC) |
7b936c0c | 2627 | #ifndef TARGET_SPARC64 |
8662d656 | 2628 | static target_long monitor_get_psr (const struct MonitorDef *md, int val) |
e95c8d51 | 2629 | { |
6a00d601 FB |
2630 | CPUState *env = mon_get_cpu(); |
2631 | if (!env) | |
2632 | return 0; | |
2633 | return GET_PSR(env); | |
e95c8d51 | 2634 | } |
7b936c0c | 2635 | #endif |
e95c8d51 | 2636 | |
8662d656 | 2637 | static target_long monitor_get_reg(const struct MonitorDef *md, int val) |
e95c8d51 | 2638 | { |
6a00d601 FB |
2639 | CPUState *env = mon_get_cpu(); |
2640 | if (!env) | |
2641 | return 0; | |
2642 | return env->regwptr[val]; | |
e95c8d51 FB |
2643 | } |
2644 | #endif | |
2645 | ||
8662d656 | 2646 | static const MonitorDef monitor_defs[] = { |
9307c4c1 | 2647 | #ifdef TARGET_I386 |
57206fd4 FB |
2648 | |
2649 | #define SEG(name, seg) \ | |
92a31b1f | 2650 | { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\ |
57206fd4 | 2651 | { name ".base", offsetof(CPUState, segs[seg].base) },\ |
92a31b1f | 2652 | { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 }, |
57206fd4 | 2653 | |
9307c4c1 FB |
2654 | { "eax", offsetof(CPUState, regs[0]) }, |
2655 | { "ecx", offsetof(CPUState, regs[1]) }, | |
2656 | { "edx", offsetof(CPUState, regs[2]) }, | |
2657 | { "ebx", offsetof(CPUState, regs[3]) }, | |
2658 | { "esp|sp", offsetof(CPUState, regs[4]) }, | |
2659 | { "ebp|fp", offsetof(CPUState, regs[5]) }, | |
2660 | { "esi", offsetof(CPUState, regs[6]) }, | |
01038d2a | 2661 | { "edi", offsetof(CPUState, regs[7]) }, |
92a31b1f FB |
2662 | #ifdef TARGET_X86_64 |
2663 | { "r8", offsetof(CPUState, regs[8]) }, | |
2664 | { "r9", offsetof(CPUState, regs[9]) }, | |
2665 | { "r10", offsetof(CPUState, regs[10]) }, | |
2666 | { "r11", offsetof(CPUState, regs[11]) }, | |
2667 | { "r12", offsetof(CPUState, regs[12]) }, | |
2668 | { "r13", offsetof(CPUState, regs[13]) }, | |
2669 | { "r14", offsetof(CPUState, regs[14]) }, | |
2670 | { "r15", offsetof(CPUState, regs[15]) }, | |
2671 | #endif | |
9307c4c1 | 2672 | { "eflags", offsetof(CPUState, eflags) }, |
57206fd4 FB |
2673 | { "eip", offsetof(CPUState, eip) }, |
2674 | SEG("cs", R_CS) | |
2675 | SEG("ds", R_DS) | |
2676 | SEG("es", R_ES) | |
01038d2a | 2677 | SEG("ss", R_SS) |
57206fd4 FB |
2678 | SEG("fs", R_FS) |
2679 | SEG("gs", R_GS) | |
2680 | { "pc", 0, monitor_get_pc, }, | |
a541f297 | 2681 | #elif defined(TARGET_PPC) |
ff937dba | 2682 | /* General purpose registers */ |
a541f297 FB |
2683 | { "r0", offsetof(CPUState, gpr[0]) }, |
2684 | { "r1", offsetof(CPUState, gpr[1]) }, | |
2685 | { "r2", offsetof(CPUState, gpr[2]) }, | |
2686 | { "r3", offsetof(CPUState, gpr[3]) }, | |
2687 | { "r4", offsetof(CPUState, gpr[4]) }, | |
2688 | { "r5", offsetof(CPUState, gpr[5]) }, | |
2689 | { "r6", offsetof(CPUState, gpr[6]) }, | |
2690 | { "r7", offsetof(CPUState, gpr[7]) }, | |
2691 | { "r8", offsetof(CPUState, gpr[8]) }, | |
2692 | { "r9", offsetof(CPUState, gpr[9]) }, | |
2693 | { "r10", offsetof(CPUState, gpr[10]) }, | |
2694 | { "r11", offsetof(CPUState, gpr[11]) }, | |
2695 | { "r12", offsetof(CPUState, gpr[12]) }, | |
2696 | { "r13", offsetof(CPUState, gpr[13]) }, | |
2697 | { "r14", offsetof(CPUState, gpr[14]) }, | |
2698 | { "r15", offsetof(CPUState, gpr[15]) }, | |
2699 | { "r16", offsetof(CPUState, gpr[16]) }, | |
2700 | { "r17", offsetof(CPUState, gpr[17]) }, | |
2701 | { "r18", offsetof(CPUState, gpr[18]) }, | |
2702 | { "r19", offsetof(CPUState, gpr[19]) }, | |
2703 | { "r20", offsetof(CPUState, gpr[20]) }, | |
2704 | { "r21", offsetof(CPUState, gpr[21]) }, | |
2705 | { "r22", offsetof(CPUState, gpr[22]) }, | |
2706 | { "r23", offsetof(CPUState, gpr[23]) }, | |
2707 | { "r24", offsetof(CPUState, gpr[24]) }, | |
2708 | { "r25", offsetof(CPUState, gpr[25]) }, | |
2709 | { "r26", offsetof(CPUState, gpr[26]) }, | |
2710 | { "r27", offsetof(CPUState, gpr[27]) }, | |
2711 | { "r28", offsetof(CPUState, gpr[28]) }, | |
2712 | { "r29", offsetof(CPUState, gpr[29]) }, | |
2713 | { "r30", offsetof(CPUState, gpr[30]) }, | |
2714 | { "r31", offsetof(CPUState, gpr[31]) }, | |
ff937dba JM |
2715 | /* Floating point registers */ |
2716 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2717 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2718 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2719 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2720 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2721 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2722 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2723 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2724 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2725 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2726 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2727 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2728 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2729 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2730 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2731 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2732 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2733 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2734 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2735 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2736 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2737 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2738 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2739 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2740 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2741 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2742 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2743 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2744 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2745 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2746 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2747 | { "f31", offsetof(CPUState, fpr[31]) }, | |
2748 | { "fpscr", offsetof(CPUState, fpscr) }, | |
2749 | /* Next instruction pointer */ | |
57206fd4 | 2750 | { "nip|pc", offsetof(CPUState, nip) }, |
a541f297 FB |
2751 | { "lr", offsetof(CPUState, lr) }, |
2752 | { "ctr", offsetof(CPUState, ctr) }, | |
9fddaa0c | 2753 | { "decr", 0, &monitor_get_decr, }, |
a541f297 | 2754 | { "ccr", 0, &monitor_get_ccr, }, |
ff937dba | 2755 | /* Machine state register */ |
a541f297 FB |
2756 | { "msr", 0, &monitor_get_msr, }, |
2757 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
2758 | { "tbu", 0, &monitor_get_tbu, }, |
2759 | { "tbl", 0, &monitor_get_tbl, }, | |
ff937dba JM |
2760 | #if defined(TARGET_PPC64) |
2761 | /* Address space register */ | |
2762 | { "asr", offsetof(CPUState, asr) }, | |
2763 | #endif | |
2764 | /* Segment registers */ | |
a541f297 FB |
2765 | { "sdr1", offsetof(CPUState, sdr1) }, |
2766 | { "sr0", offsetof(CPUState, sr[0]) }, | |
2767 | { "sr1", offsetof(CPUState, sr[1]) }, | |
2768 | { "sr2", offsetof(CPUState, sr[2]) }, | |
2769 | { "sr3", offsetof(CPUState, sr[3]) }, | |
2770 | { "sr4", offsetof(CPUState, sr[4]) }, | |
2771 | { "sr5", offsetof(CPUState, sr[5]) }, | |
2772 | { "sr6", offsetof(CPUState, sr[6]) }, | |
2773 | { "sr7", offsetof(CPUState, sr[7]) }, | |
2774 | { "sr8", offsetof(CPUState, sr[8]) }, | |
2775 | { "sr9", offsetof(CPUState, sr[9]) }, | |
2776 | { "sr10", offsetof(CPUState, sr[10]) }, | |
2777 | { "sr11", offsetof(CPUState, sr[11]) }, | |
2778 | { "sr12", offsetof(CPUState, sr[12]) }, | |
2779 | { "sr13", offsetof(CPUState, sr[13]) }, | |
2780 | { "sr14", offsetof(CPUState, sr[14]) }, | |
2781 | { "sr15", offsetof(CPUState, sr[15]) }, | |
2782 | /* Too lazy to put BATs and SPRs ... */ | |
e95c8d51 FB |
2783 | #elif defined(TARGET_SPARC) |
2784 | { "g0", offsetof(CPUState, gregs[0]) }, | |
2785 | { "g1", offsetof(CPUState, gregs[1]) }, | |
2786 | { "g2", offsetof(CPUState, gregs[2]) }, | |
2787 | { "g3", offsetof(CPUState, gregs[3]) }, | |
2788 | { "g4", offsetof(CPUState, gregs[4]) }, | |
2789 | { "g5", offsetof(CPUState, gregs[5]) }, | |
2790 | { "g6", offsetof(CPUState, gregs[6]) }, | |
2791 | { "g7", offsetof(CPUState, gregs[7]) }, | |
2792 | { "o0", 0, monitor_get_reg }, | |
2793 | { "o1", 1, monitor_get_reg }, | |
2794 | { "o2", 2, monitor_get_reg }, | |
2795 | { "o3", 3, monitor_get_reg }, | |
2796 | { "o4", 4, monitor_get_reg }, | |
2797 | { "o5", 5, monitor_get_reg }, | |
2798 | { "o6", 6, monitor_get_reg }, | |
2799 | { "o7", 7, monitor_get_reg }, | |
2800 | { "l0", 8, monitor_get_reg }, | |
2801 | { "l1", 9, monitor_get_reg }, | |
2802 | { "l2", 10, monitor_get_reg }, | |
2803 | { "l3", 11, monitor_get_reg }, | |
2804 | { "l4", 12, monitor_get_reg }, | |
2805 | { "l5", 13, monitor_get_reg }, | |
2806 | { "l6", 14, monitor_get_reg }, | |
2807 | { "l7", 15, monitor_get_reg }, | |
2808 | { "i0", 16, monitor_get_reg }, | |
2809 | { "i1", 17, monitor_get_reg }, | |
2810 | { "i2", 18, monitor_get_reg }, | |
2811 | { "i3", 19, monitor_get_reg }, | |
2812 | { "i4", 20, monitor_get_reg }, | |
2813 | { "i5", 21, monitor_get_reg }, | |
2814 | { "i6", 22, monitor_get_reg }, | |
2815 | { "i7", 23, monitor_get_reg }, | |
2816 | { "pc", offsetof(CPUState, pc) }, | |
2817 | { "npc", offsetof(CPUState, npc) }, | |
2818 | { "y", offsetof(CPUState, y) }, | |
7b936c0c | 2819 | #ifndef TARGET_SPARC64 |
e95c8d51 FB |
2820 | { "psr", 0, &monitor_get_psr, }, |
2821 | { "wim", offsetof(CPUState, wim) }, | |
7b936c0c | 2822 | #endif |
e95c8d51 FB |
2823 | { "tbr", offsetof(CPUState, tbr) }, |
2824 | { "fsr", offsetof(CPUState, fsr) }, | |
2825 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2826 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2827 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2828 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2829 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2830 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2831 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2832 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2833 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2834 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2835 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2836 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2837 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2838 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2839 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2840 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2841 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2842 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2843 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2844 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2845 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2846 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2847 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2848 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2849 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2850 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2851 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2852 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2853 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2854 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2855 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2856 | { "f31", offsetof(CPUState, fpr[31]) }, | |
7b936c0c FB |
2857 | #ifdef TARGET_SPARC64 |
2858 | { "f32", offsetof(CPUState, fpr[32]) }, | |
2859 | { "f34", offsetof(CPUState, fpr[34]) }, | |
2860 | { "f36", offsetof(CPUState, fpr[36]) }, | |
2861 | { "f38", offsetof(CPUState, fpr[38]) }, | |
2862 | { "f40", offsetof(CPUState, fpr[40]) }, | |
2863 | { "f42", offsetof(CPUState, fpr[42]) }, | |
2864 | { "f44", offsetof(CPUState, fpr[44]) }, | |
2865 | { "f46", offsetof(CPUState, fpr[46]) }, | |
2866 | { "f48", offsetof(CPUState, fpr[48]) }, | |
2867 | { "f50", offsetof(CPUState, fpr[50]) }, | |
2868 | { "f52", offsetof(CPUState, fpr[52]) }, | |
2869 | { "f54", offsetof(CPUState, fpr[54]) }, | |
2870 | { "f56", offsetof(CPUState, fpr[56]) }, | |
2871 | { "f58", offsetof(CPUState, fpr[58]) }, | |
2872 | { "f60", offsetof(CPUState, fpr[60]) }, | |
2873 | { "f62", offsetof(CPUState, fpr[62]) }, | |
2874 | { "asi", offsetof(CPUState, asi) }, | |
2875 | { "pstate", offsetof(CPUState, pstate) }, | |
2876 | { "cansave", offsetof(CPUState, cansave) }, | |
2877 | { "canrestore", offsetof(CPUState, canrestore) }, | |
2878 | { "otherwin", offsetof(CPUState, otherwin) }, | |
2879 | { "wstate", offsetof(CPUState, wstate) }, | |
2880 | { "cleanwin", offsetof(CPUState, cleanwin) }, | |
2881 | { "fprs", offsetof(CPUState, fprs) }, | |
2882 | #endif | |
9307c4c1 FB |
2883 | #endif |
2884 | { NULL }, | |
2885 | }; | |
2886 | ||
376253ec | 2887 | static void expr_error(Monitor *mon, const char *msg) |
9dc39cba | 2888 | { |
376253ec | 2889 | monitor_printf(mon, "%s\n", msg); |
9307c4c1 FB |
2890 | longjmp(expr_env, 1); |
2891 | } | |
2892 | ||
6a00d601 | 2893 | /* return 0 if OK, -1 if not found, -2 if no CPU defined */ |
92a31b1f | 2894 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 2895 | { |
8662d656 | 2896 | const MonitorDef *md; |
92a31b1f FB |
2897 | void *ptr; |
2898 | ||
9307c4c1 FB |
2899 | for(md = monitor_defs; md->name != NULL; md++) { |
2900 | if (compare_cmd(name, md->name)) { | |
2901 | if (md->get_value) { | |
e95c8d51 | 2902 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 2903 | } else { |
6a00d601 FB |
2904 | CPUState *env = mon_get_cpu(); |
2905 | if (!env) | |
2906 | return -2; | |
2907 | ptr = (uint8_t *)env + md->offset; | |
92a31b1f FB |
2908 | switch(md->type) { |
2909 | case MD_I32: | |
2910 | *pval = *(int32_t *)ptr; | |
2911 | break; | |
2912 | case MD_TLONG: | |
2913 | *pval = *(target_long *)ptr; | |
2914 | break; | |
2915 | default: | |
2916 | *pval = 0; | |
2917 | break; | |
2918 | } | |
9307c4c1 FB |
2919 | } |
2920 | return 0; | |
2921 | } | |
2922 | } | |
2923 | return -1; | |
2924 | } | |
2925 | ||
2926 | static void next(void) | |
2927 | { | |
660f11be | 2928 | if (*pch != '\0') { |
9307c4c1 | 2929 | pch++; |
cd390083 | 2930 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2931 | pch++; |
2932 | } | |
2933 | } | |
2934 | ||
376253ec | 2935 | static int64_t expr_sum(Monitor *mon); |
9307c4c1 | 2936 | |
376253ec | 2937 | static int64_t expr_unary(Monitor *mon) |
9307c4c1 | 2938 | { |
c2efc95d | 2939 | int64_t n; |
9307c4c1 | 2940 | char *p; |
6a00d601 | 2941 | int ret; |
9307c4c1 FB |
2942 | |
2943 | switch(*pch) { | |
2944 | case '+': | |
2945 | next(); | |
376253ec | 2946 | n = expr_unary(mon); |
9307c4c1 FB |
2947 | break; |
2948 | case '-': | |
2949 | next(); | |
376253ec | 2950 | n = -expr_unary(mon); |
9307c4c1 FB |
2951 | break; |
2952 | case '~': | |
2953 | next(); | |
376253ec | 2954 | n = ~expr_unary(mon); |
9307c4c1 FB |
2955 | break; |
2956 | case '(': | |
2957 | next(); | |
376253ec | 2958 | n = expr_sum(mon); |
9307c4c1 | 2959 | if (*pch != ')') { |
376253ec | 2960 | expr_error(mon, "')' expected"); |
9307c4c1 FB |
2961 | } |
2962 | next(); | |
2963 | break; | |
81d0912d FB |
2964 | case '\'': |
2965 | pch++; | |
2966 | if (*pch == '\0') | |
376253ec | 2967 | expr_error(mon, "character constant expected"); |
81d0912d FB |
2968 | n = *pch; |
2969 | pch++; | |
2970 | if (*pch != '\'') | |
376253ec | 2971 | expr_error(mon, "missing terminating \' character"); |
81d0912d FB |
2972 | next(); |
2973 | break; | |
9307c4c1 FB |
2974 | case '$': |
2975 | { | |
2976 | char buf[128], *q; | |
69b34976 | 2977 | target_long reg=0; |
3b46e624 | 2978 | |
9307c4c1 FB |
2979 | pch++; |
2980 | q = buf; | |
2981 | while ((*pch >= 'a' && *pch <= 'z') || | |
2982 | (*pch >= 'A' && *pch <= 'Z') || | |
2983 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 2984 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
2985 | if ((q - buf) < sizeof(buf) - 1) |
2986 | *q++ = *pch; | |
2987 | pch++; | |
2988 | } | |
cd390083 | 2989 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2990 | pch++; |
2991 | *q = 0; | |
7743e588 | 2992 | ret = get_monitor_def(®, buf); |
6a00d601 | 2993 | if (ret == -1) |
376253ec | 2994 | expr_error(mon, "unknown register"); |
5fafdf24 | 2995 | else if (ret == -2) |
376253ec | 2996 | expr_error(mon, "no cpu defined"); |
7743e588 | 2997 | n = reg; |
9307c4c1 FB |
2998 | } |
2999 | break; | |
3000 | case '\0': | |
376253ec | 3001 | expr_error(mon, "unexpected end of expression"); |
9307c4c1 FB |
3002 | n = 0; |
3003 | break; | |
3004 | default: | |
7743e588 | 3005 | #if TARGET_PHYS_ADDR_BITS > 32 |
4f4fbf77 FB |
3006 | n = strtoull(pch, &p, 0); |
3007 | #else | |
9307c4c1 | 3008 | n = strtoul(pch, &p, 0); |
4f4fbf77 | 3009 | #endif |
9307c4c1 | 3010 | if (pch == p) { |
376253ec | 3011 | expr_error(mon, "invalid char in expression"); |
9307c4c1 FB |
3012 | } |
3013 | pch = p; | |
cd390083 | 3014 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3015 | pch++; |
3016 | break; | |
3017 | } | |
3018 | return n; | |
3019 | } | |
3020 | ||
3021 | ||
376253ec | 3022 | static int64_t expr_prod(Monitor *mon) |
9307c4c1 | 3023 | { |
c2efc95d | 3024 | int64_t val, val2; |
92a31b1f | 3025 | int op; |
3b46e624 | 3026 | |
376253ec | 3027 | val = expr_unary(mon); |
9307c4c1 FB |
3028 | for(;;) { |
3029 | op = *pch; | |
3030 | if (op != '*' && op != '/' && op != '%') | |
3031 | break; | |
3032 | next(); | |
376253ec | 3033 | val2 = expr_unary(mon); |
9307c4c1 FB |
3034 | switch(op) { |
3035 | default: | |
3036 | case '*': | |
3037 | val *= val2; | |
3038 | break; | |
3039 | case '/': | |
3040 | case '%': | |
5fafdf24 | 3041 | if (val2 == 0) |
376253ec | 3042 | expr_error(mon, "division by zero"); |
9307c4c1 FB |
3043 | if (op == '/') |
3044 | val /= val2; | |
3045 | else | |
3046 | val %= val2; | |
3047 | break; | |
3048 | } | |
3049 | } | |
3050 | return val; | |
3051 | } | |
3052 | ||
376253ec | 3053 | static int64_t expr_logic(Monitor *mon) |
9307c4c1 | 3054 | { |
c2efc95d | 3055 | int64_t val, val2; |
92a31b1f | 3056 | int op; |
9307c4c1 | 3057 | |
376253ec | 3058 | val = expr_prod(mon); |
9307c4c1 FB |
3059 | for(;;) { |
3060 | op = *pch; | |
3061 | if (op != '&' && op != '|' && op != '^') | |
3062 | break; | |
3063 | next(); | |
376253ec | 3064 | val2 = expr_prod(mon); |
9307c4c1 FB |
3065 | switch(op) { |
3066 | default: | |
3067 | case '&': | |
3068 | val &= val2; | |
3069 | break; | |
3070 | case '|': | |
3071 | val |= val2; | |
3072 | break; | |
3073 | case '^': | |
3074 | val ^= val2; | |
3075 | break; | |
3076 | } | |
3077 | } | |
3078 | return val; | |
3079 | } | |
3080 | ||
376253ec | 3081 | static int64_t expr_sum(Monitor *mon) |
9307c4c1 | 3082 | { |
c2efc95d | 3083 | int64_t val, val2; |
92a31b1f | 3084 | int op; |
9307c4c1 | 3085 | |
376253ec | 3086 | val = expr_logic(mon); |
9307c4c1 FB |
3087 | for(;;) { |
3088 | op = *pch; | |
3089 | if (op != '+' && op != '-') | |
3090 | break; | |
3091 | next(); | |
376253ec | 3092 | val2 = expr_logic(mon); |
9307c4c1 FB |
3093 | if (op == '+') |
3094 | val += val2; | |
3095 | else | |
3096 | val -= val2; | |
3097 | } | |
3098 | return val; | |
3099 | } | |
3100 | ||
376253ec | 3101 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
9307c4c1 FB |
3102 | { |
3103 | pch = *pp; | |
3104 | if (setjmp(expr_env)) { | |
3105 | *pp = pch; | |
3106 | return -1; | |
3107 | } | |
cd390083 | 3108 | while (qemu_isspace(*pch)) |
9307c4c1 | 3109 | pch++; |
376253ec | 3110 | *pval = expr_sum(mon); |
9307c4c1 FB |
3111 | *pp = pch; |
3112 | return 0; | |
3113 | } | |
3114 | ||
3115 | static int get_str(char *buf, int buf_size, const char **pp) | |
3116 | { | |
3117 | const char *p; | |
3118 | char *q; | |
3119 | int c; | |
3120 | ||
81d0912d | 3121 | q = buf; |
9307c4c1 | 3122 | p = *pp; |
cd390083 | 3123 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3124 | p++; |
3125 | if (*p == '\0') { | |
3126 | fail: | |
81d0912d | 3127 | *q = '\0'; |
9307c4c1 FB |
3128 | *pp = p; |
3129 | return -1; | |
3130 | } | |
9307c4c1 FB |
3131 | if (*p == '\"') { |
3132 | p++; | |
3133 | while (*p != '\0' && *p != '\"') { | |
3134 | if (*p == '\\') { | |
3135 | p++; | |
3136 | c = *p++; | |
3137 | switch(c) { | |
3138 | case 'n': | |
3139 | c = '\n'; | |
3140 | break; | |
3141 | case 'r': | |
3142 | c = '\r'; | |
3143 | break; | |
3144 | case '\\': | |
3145 | case '\'': | |
3146 | case '\"': | |
3147 | break; | |
3148 | default: | |
3149 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
3150 | goto fail; | |
3151 | } | |
3152 | if ((q - buf) < buf_size - 1) { | |
3153 | *q++ = c; | |
3154 | } | |
3155 | } else { | |
3156 | if ((q - buf) < buf_size - 1) { | |
3157 | *q++ = *p; | |
3158 | } | |
3159 | p++; | |
3160 | } | |
3161 | } | |
3162 | if (*p != '\"') { | |
5b60212f | 3163 | qemu_printf("unterminated string\n"); |
9307c4c1 FB |
3164 | goto fail; |
3165 | } | |
3166 | p++; | |
3167 | } else { | |
cd390083 | 3168 | while (*p != '\0' && !qemu_isspace(*p)) { |
9307c4c1 FB |
3169 | if ((q - buf) < buf_size - 1) { |
3170 | *q++ = *p; | |
3171 | } | |
3172 | p++; | |
3173 | } | |
9307c4c1 | 3174 | } |
81d0912d | 3175 | *q = '\0'; |
9307c4c1 FB |
3176 | *pp = p; |
3177 | return 0; | |
3178 | } | |
3179 | ||
4590fd80 LC |
3180 | /* |
3181 | * Store the command-name in cmdname, and return a pointer to | |
3182 | * the remaining of the command string. | |
3183 | */ | |
3184 | static const char *get_command_name(const char *cmdline, | |
3185 | char *cmdname, size_t nlen) | |
3186 | { | |
3187 | size_t len; | |
3188 | const char *p, *pstart; | |
3189 | ||
3190 | p = cmdline; | |
3191 | while (qemu_isspace(*p)) | |
3192 | p++; | |
3193 | if (*p == '\0') | |
3194 | return NULL; | |
3195 | pstart = p; | |
3196 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | |
3197 | p++; | |
3198 | len = p - pstart; | |
3199 | if (len > nlen - 1) | |
3200 | len = nlen - 1; | |
3201 | memcpy(cmdname, pstart, len); | |
3202 | cmdname[len] = '\0'; | |
3203 | return p; | |
3204 | } | |
3205 | ||
4d76d2ba LC |
3206 | /** |
3207 | * Read key of 'type' into 'key' and return the current | |
3208 | * 'type' pointer. | |
3209 | */ | |
3210 | static char *key_get_info(const char *type, char **key) | |
3211 | { | |
3212 | size_t len; | |
3213 | char *p, *str; | |
3214 | ||
3215 | if (*type == ',') | |
3216 | type++; | |
3217 | ||
3218 | p = strchr(type, ':'); | |
3219 | if (!p) { | |
3220 | *key = NULL; | |
3221 | return NULL; | |
3222 | } | |
3223 | len = p - type; | |
3224 | ||
3225 | str = qemu_malloc(len + 1); | |
3226 | memcpy(str, type, len); | |
3227 | str[len] = '\0'; | |
3228 | ||
3229 | *key = str; | |
3230 | return ++p; | |
3231 | } | |
3232 | ||
9307c4c1 FB |
3233 | static int default_fmt_format = 'x'; |
3234 | static int default_fmt_size = 4; | |
3235 | ||
3236 | #define MAX_ARGS 16 | |
3237 | ||
fbc3d96c LS |
3238 | static int is_valid_option(const char *c, const char *typestr) |
3239 | { | |
3240 | char option[3]; | |
3241 | ||
3242 | option[0] = '-'; | |
3243 | option[1] = *c; | |
3244 | option[2] = '\0'; | |
3245 | ||
3246 | typestr = strstr(typestr, option); | |
3247 | return (typestr != NULL); | |
3248 | } | |
3249 | ||
7fd669a1 LC |
3250 | static const mon_cmd_t *monitor_find_command(const char *cmdname) |
3251 | { | |
3252 | const mon_cmd_t *cmd; | |
3253 | ||
3254 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
3255 | if (compare_cmd(cmdname, cmd->name)) { | |
3256 | return cmd; | |
3257 | } | |
3258 | } | |
3259 | ||
3260 | return NULL; | |
3261 | } | |
3262 | ||
c227f099 | 3263 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
55f81d96 | 3264 | const char *cmdline, |
55f81d96 | 3265 | QDict *qdict) |
9307c4c1 | 3266 | { |
4590fd80 | 3267 | const char *p, *typestr; |
53773581 | 3268 | int c; |
c227f099 | 3269 | const mon_cmd_t *cmd; |
9307c4c1 FB |
3270 | char cmdname[256]; |
3271 | char buf[1024]; | |
4d76d2ba | 3272 | char *key; |
9dc39cba FB |
3273 | |
3274 | #ifdef DEBUG | |
376253ec | 3275 | monitor_printf(mon, "command='%s'\n", cmdline); |
9dc39cba | 3276 | #endif |
3b46e624 | 3277 | |
9307c4c1 | 3278 | /* extract the command name */ |
4590fd80 LC |
3279 | p = get_command_name(cmdline, cmdname, sizeof(cmdname)); |
3280 | if (!p) | |
55f81d96 | 3281 | return NULL; |
3b46e624 | 3282 | |
7fd669a1 LC |
3283 | cmd = monitor_find_command(cmdname); |
3284 | if (!cmd) { | |
d91d9bf6 | 3285 | monitor_printf(mon, "unknown command: '%s'\n", cmdname); |
55f81d96 | 3286 | return NULL; |
9307c4c1 | 3287 | } |
9307c4c1 | 3288 | |
9307c4c1 FB |
3289 | /* parse the parameters */ |
3290 | typestr = cmd->args_type; | |
9dc39cba | 3291 | for(;;) { |
4d76d2ba LC |
3292 | typestr = key_get_info(typestr, &key); |
3293 | if (!typestr) | |
9dc39cba | 3294 | break; |
4d76d2ba | 3295 | c = *typestr; |
9307c4c1 FB |
3296 | typestr++; |
3297 | switch(c) { | |
3298 | case 'F': | |
81d0912d | 3299 | case 'B': |
9307c4c1 FB |
3300 | case 's': |
3301 | { | |
3302 | int ret; | |
3b46e624 | 3303 | |
cd390083 | 3304 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3305 | p++; |
3306 | if (*typestr == '?') { | |
3307 | typestr++; | |
3308 | if (*p == '\0') { | |
3309 | /* no optional string: NULL argument */ | |
53773581 | 3310 | break; |
9307c4c1 FB |
3311 | } |
3312 | } | |
3313 | ret = get_str(buf, sizeof(buf), &p); | |
3314 | if (ret < 0) { | |
81d0912d FB |
3315 | switch(c) { |
3316 | case 'F': | |
376253ec AL |
3317 | monitor_printf(mon, "%s: filename expected\n", |
3318 | cmdname); | |
81d0912d FB |
3319 | break; |
3320 | case 'B': | |
376253ec AL |
3321 | monitor_printf(mon, "%s: block device name expected\n", |
3322 | cmdname); | |
81d0912d FB |
3323 | break; |
3324 | default: | |
376253ec | 3325 | monitor_printf(mon, "%s: string expected\n", cmdname); |
81d0912d FB |
3326 | break; |
3327 | } | |
9307c4c1 FB |
3328 | goto fail; |
3329 | } | |
53773581 | 3330 | qdict_put(qdict, key, qstring_from_str(buf)); |
9307c4c1 | 3331 | } |
9dc39cba | 3332 | break; |
9307c4c1 FB |
3333 | case '/': |
3334 | { | |
3335 | int count, format, size; | |
3b46e624 | 3336 | |
cd390083 | 3337 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3338 | p++; |
3339 | if (*p == '/') { | |
3340 | /* format found */ | |
3341 | p++; | |
3342 | count = 1; | |
cd390083 | 3343 | if (qemu_isdigit(*p)) { |
9307c4c1 | 3344 | count = 0; |
cd390083 | 3345 | while (qemu_isdigit(*p)) { |
9307c4c1 FB |
3346 | count = count * 10 + (*p - '0'); |
3347 | p++; | |
3348 | } | |
3349 | } | |
3350 | size = -1; | |
3351 | format = -1; | |
3352 | for(;;) { | |
3353 | switch(*p) { | |
3354 | case 'o': | |
3355 | case 'd': | |
3356 | case 'u': | |
3357 | case 'x': | |
3358 | case 'i': | |
3359 | case 'c': | |
3360 | format = *p++; | |
3361 | break; | |
3362 | case 'b': | |
3363 | size = 1; | |
3364 | p++; | |
3365 | break; | |
3366 | case 'h': | |
3367 | size = 2; | |
3368 | p++; | |
3369 | break; | |
3370 | case 'w': | |
3371 | size = 4; | |
3372 | p++; | |
3373 | break; | |
3374 | case 'g': | |
3375 | case 'L': | |
3376 | size = 8; | |
3377 | p++; | |
3378 | break; | |
3379 | default: | |
3380 | goto next; | |
3381 | } | |
3382 | } | |
3383 | next: | |
cd390083 | 3384 | if (*p != '\0' && !qemu_isspace(*p)) { |
376253ec AL |
3385 | monitor_printf(mon, "invalid char in format: '%c'\n", |
3386 | *p); | |
9307c4c1 FB |
3387 | goto fail; |
3388 | } | |
9307c4c1 FB |
3389 | if (format < 0) |
3390 | format = default_fmt_format; | |
4c27ba27 FB |
3391 | if (format != 'i') { |
3392 | /* for 'i', not specifying a size gives -1 as size */ | |
3393 | if (size < 0) | |
3394 | size = default_fmt_size; | |
e90f009b | 3395 | default_fmt_size = size; |
4c27ba27 | 3396 | } |
9307c4c1 FB |
3397 | default_fmt_format = format; |
3398 | } else { | |
3399 | count = 1; | |
3400 | format = default_fmt_format; | |
4c27ba27 FB |
3401 | if (format != 'i') { |
3402 | size = default_fmt_size; | |
3403 | } else { | |
3404 | size = -1; | |
3405 | } | |
9307c4c1 | 3406 | } |
f7188bbe LC |
3407 | qdict_put(qdict, "count", qint_from_int(count)); |
3408 | qdict_put(qdict, "format", qint_from_int(format)); | |
3409 | qdict_put(qdict, "size", qint_from_int(size)); | |
9307c4c1 | 3410 | } |
9dc39cba | 3411 | break; |
9307c4c1 | 3412 | case 'i': |
92a31b1f | 3413 | case 'l': |
9307c4c1 | 3414 | { |
c2efc95d | 3415 | int64_t val; |
7743e588 | 3416 | |
cd390083 | 3417 | while (qemu_isspace(*p)) |
9307c4c1 | 3418 | p++; |
3440557b | 3419 | if (*typestr == '?' || *typestr == '.') { |
3440557b | 3420 | if (*typestr == '?') { |
53773581 LC |
3421 | if (*p == '\0') { |
3422 | typestr++; | |
3423 | break; | |
3424 | } | |
3440557b FB |
3425 | } else { |
3426 | if (*p == '.') { | |
3427 | p++; | |
cd390083 | 3428 | while (qemu_isspace(*p)) |
3440557b | 3429 | p++; |
3440557b | 3430 | } else { |
53773581 LC |
3431 | typestr++; |
3432 | break; | |
3440557b FB |
3433 | } |
3434 | } | |
13224a87 | 3435 | typestr++; |
9307c4c1 | 3436 | } |
376253ec | 3437 | if (get_expr(mon, &val, &p)) |
9307c4c1 | 3438 | goto fail; |
675ebef9 LC |
3439 | /* Check if 'i' is greater than 32-bit */ |
3440 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { | |
3441 | monitor_printf(mon, "\'%s\' has failed: ", cmdname); | |
3442 | monitor_printf(mon, "integer is for 32-bit values\n"); | |
3443 | goto fail; | |
3444 | } | |
53773581 | 3445 | qdict_put(qdict, key, qint_from_int(val)); |
9307c4c1 FB |
3446 | } |
3447 | break; | |
3448 | case '-': | |
3449 | { | |
fbc3d96c LS |
3450 | const char *tmp = p; |
3451 | int has_option, skip_key = 0; | |
9307c4c1 | 3452 | /* option */ |
3b46e624 | 3453 | |
9307c4c1 FB |
3454 | c = *typestr++; |
3455 | if (c == '\0') | |
3456 | goto bad_type; | |
cd390083 | 3457 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3458 | p++; |
3459 | has_option = 0; | |
3460 | if (*p == '-') { | |
3461 | p++; | |
fbc3d96c LS |
3462 | if(c != *p) { |
3463 | if(!is_valid_option(p, typestr)) { | |
3464 | ||
3465 | monitor_printf(mon, "%s: unsupported option -%c\n", | |
3466 | cmdname, *p); | |
3467 | goto fail; | |
3468 | } else { | |
3469 | skip_key = 1; | |
3470 | } | |
3471 | } | |
3472 | if(skip_key) { | |
3473 | p = tmp; | |
3474 | } else { | |
3475 | p++; | |
3476 | has_option = 1; | |
9307c4c1 | 3477 | } |
9307c4c1 | 3478 | } |
f7188bbe | 3479 | qdict_put(qdict, key, qint_from_int(has_option)); |
9307c4c1 FB |
3480 | } |
3481 | break; | |
3482 | default: | |
3483 | bad_type: | |
376253ec | 3484 | monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c); |
9307c4c1 FB |
3485 | goto fail; |
3486 | } | |
4d76d2ba LC |
3487 | qemu_free(key); |
3488 | key = NULL; | |
9dc39cba | 3489 | } |
9307c4c1 | 3490 | /* check that all arguments were parsed */ |
cd390083 | 3491 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3492 | p++; |
3493 | if (*p != '\0') { | |
376253ec AL |
3494 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
3495 | cmdname); | |
9307c4c1 | 3496 | goto fail; |
9dc39cba | 3497 | } |
9307c4c1 | 3498 | |
55f81d96 | 3499 | return cmd; |
ac7531ec | 3500 | |
55f81d96 | 3501 | fail: |
4d76d2ba | 3502 | qemu_free(key); |
55f81d96 LC |
3503 | return NULL; |
3504 | } | |
3505 | ||
8204a918 LC |
3506 | static void monitor_print_error(Monitor *mon) |
3507 | { | |
3508 | qerror_print(mon->error); | |
3509 | QDECREF(mon->error); | |
3510 | mon->error = NULL; | |
3511 | } | |
3512 | ||
99e2fc16 LC |
3513 | static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd, |
3514 | const QDict *params) | |
3515 | { | |
3516 | QObject *data = NULL; | |
3517 | ||
3518 | cmd->mhandler.cmd_new(mon, params, &data); | |
25b422eb LC |
3519 | |
3520 | if (monitor_ctrl_mode(mon)) { | |
3521 | /* Monitor Protocol */ | |
3522 | monitor_protocol_emitter(mon, data); | |
3523 | } else { | |
3524 | /* User Protocol */ | |
3525 | if (data) | |
3526 | cmd->user_print(mon, data); | |
3527 | } | |
99e2fc16 LC |
3528 | |
3529 | qobject_decref(data); | |
3530 | } | |
3531 | ||
f3c157c4 | 3532 | static void handle_user_command(Monitor *mon, const char *cmdline) |
55f81d96 | 3533 | { |
55f81d96 | 3534 | QDict *qdict; |
c227f099 | 3535 | const mon_cmd_t *cmd; |
55f81d96 LC |
3536 | |
3537 | qdict = qdict_new(); | |
3538 | ||
590fb3b7 | 3539 | cmd = monitor_parse_command(mon, cmdline, qdict); |
13917bee LC |
3540 | if (!cmd) |
3541 | goto out; | |
3542 | ||
3543 | qemu_errors_to_mon(mon); | |
3544 | ||
3545 | if (monitor_handler_ported(cmd)) { | |
99e2fc16 | 3546 | monitor_call_handler(mon, cmd, qdict); |
13917bee | 3547 | } else { |
af4ce882 | 3548 | cmd->mhandler.cmd(mon, qdict); |
55f81d96 LC |
3549 | } |
3550 | ||
8204a918 LC |
3551 | if (monitor_has_error(mon)) |
3552 | monitor_print_error(mon); | |
3553 | ||
3554 | qemu_errors_to_previous(); | |
13917bee LC |
3555 | |
3556 | out: | |
f7188bbe | 3557 | QDECREF(qdict); |
9dc39cba FB |
3558 | } |
3559 | ||
81d0912d FB |
3560 | static void cmd_completion(const char *name, const char *list) |
3561 | { | |
3562 | const char *p, *pstart; | |
3563 | char cmd[128]; | |
3564 | int len; | |
3565 | ||
3566 | p = list; | |
3567 | for(;;) { | |
3568 | pstart = p; | |
3569 | p = strchr(p, '|'); | |
3570 | if (!p) | |
3571 | p = pstart + strlen(pstart); | |
3572 | len = p - pstart; | |
3573 | if (len > sizeof(cmd) - 2) | |
3574 | len = sizeof(cmd) - 2; | |
3575 | memcpy(cmd, pstart, len); | |
3576 | cmd[len] = '\0'; | |
3577 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
731b0364 | 3578 | readline_add_completion(cur_mon->rs, cmd); |
81d0912d FB |
3579 | } |
3580 | if (*p == '\0') | |
3581 | break; | |
3582 | p++; | |
3583 | } | |
3584 | } | |
3585 | ||
3586 | static void file_completion(const char *input) | |
3587 | { | |
3588 | DIR *ffs; | |
3589 | struct dirent *d; | |
3590 | char path[1024]; | |
3591 | char file[1024], file_prefix[1024]; | |
3592 | int input_path_len; | |
3593 | const char *p; | |
3594 | ||
5fafdf24 | 3595 | p = strrchr(input, '/'); |
81d0912d FB |
3596 | if (!p) { |
3597 | input_path_len = 0; | |
3598 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 3599 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
3600 | } else { |
3601 | input_path_len = p - input + 1; | |
3602 | memcpy(path, input, input_path_len); | |
3603 | if (input_path_len > sizeof(path) - 1) | |
3604 | input_path_len = sizeof(path) - 1; | |
3605 | path[input_path_len] = '\0'; | |
3606 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
3607 | } | |
3608 | #ifdef DEBUG_COMPLETION | |
376253ec AL |
3609 | monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n", |
3610 | input, path, file_prefix); | |
81d0912d FB |
3611 | #endif |
3612 | ffs = opendir(path); | |
3613 | if (!ffs) | |
3614 | return; | |
3615 | for(;;) { | |
3616 | struct stat sb; | |
3617 | d = readdir(ffs); | |
3618 | if (!d) | |
3619 | break; | |
3620 | if (strstart(d->d_name, file_prefix, NULL)) { | |
3621 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
3622 | if (input_path_len < sizeof(file)) |
3623 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
3624 | d->d_name); | |
81d0912d FB |
3625 | /* stat the file to find out if it's a directory. |
3626 | * In that case add a slash to speed up typing long paths | |
3627 | */ | |
3628 | stat(file, &sb); | |
3629 | if(S_ISDIR(sb.st_mode)) | |
363a37d5 | 3630 | pstrcat(file, sizeof(file), "/"); |
731b0364 | 3631 | readline_add_completion(cur_mon->rs, file); |
81d0912d FB |
3632 | } |
3633 | } | |
3634 | closedir(ffs); | |
3635 | } | |
3636 | ||
51de9760 | 3637 | static void block_completion_it(void *opaque, BlockDriverState *bs) |
81d0912d | 3638 | { |
51de9760 | 3639 | const char *name = bdrv_get_device_name(bs); |
81d0912d FB |
3640 | const char *input = opaque; |
3641 | ||
3642 | if (input[0] == '\0' || | |
3643 | !strncmp(name, (char *)input, strlen(input))) { | |
731b0364 | 3644 | readline_add_completion(cur_mon->rs, name); |
81d0912d FB |
3645 | } |
3646 | } | |
3647 | ||
3648 | /* NOTE: this parser is an approximate form of the real command parser */ | |
3649 | static void parse_cmdline(const char *cmdline, | |
3650 | int *pnb_args, char **args) | |
3651 | { | |
3652 | const char *p; | |
3653 | int nb_args, ret; | |
3654 | char buf[1024]; | |
3655 | ||
3656 | p = cmdline; | |
3657 | nb_args = 0; | |
3658 | for(;;) { | |
cd390083 | 3659 | while (qemu_isspace(*p)) |
81d0912d FB |
3660 | p++; |
3661 | if (*p == '\0') | |
3662 | break; | |
3663 | if (nb_args >= MAX_ARGS) | |
3664 | break; | |
3665 | ret = get_str(buf, sizeof(buf), &p); | |
3666 | args[nb_args] = qemu_strdup(buf); | |
3667 | nb_args++; | |
3668 | if (ret < 0) | |
3669 | break; | |
3670 | } | |
3671 | *pnb_args = nb_args; | |
3672 | } | |
3673 | ||
4d76d2ba LC |
3674 | static const char *next_arg_type(const char *typestr) |
3675 | { | |
3676 | const char *p = strchr(typestr, ':'); | |
3677 | return (p != NULL ? ++p : typestr); | |
3678 | } | |
3679 | ||
4c36ba32 | 3680 | static void monitor_find_completion(const char *cmdline) |
81d0912d FB |
3681 | { |
3682 | const char *cmdname; | |
3683 | char *args[MAX_ARGS]; | |
3684 | int nb_args, i, len; | |
3685 | const char *ptype, *str; | |
c227f099 | 3686 | const mon_cmd_t *cmd; |
64866c3d | 3687 | const KeyDef *key; |
81d0912d FB |
3688 | |
3689 | parse_cmdline(cmdline, &nb_args, args); | |
3690 | #ifdef DEBUG_COMPLETION | |
3691 | for(i = 0; i < nb_args; i++) { | |
376253ec | 3692 | monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]); |
81d0912d FB |
3693 | } |
3694 | #endif | |
3695 | ||
3696 | /* if the line ends with a space, it means we want to complete the | |
3697 | next arg */ | |
3698 | len = strlen(cmdline); | |
cd390083 | 3699 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { |
81d0912d FB |
3700 | if (nb_args >= MAX_ARGS) |
3701 | return; | |
3702 | args[nb_args++] = qemu_strdup(""); | |
3703 | } | |
3704 | if (nb_args <= 1) { | |
3705 | /* command completion */ | |
3706 | if (nb_args == 0) | |
3707 | cmdname = ""; | |
3708 | else | |
3709 | cmdname = args[0]; | |
731b0364 | 3710 | readline_set_completion_index(cur_mon->rs, strlen(cmdname)); |
376253ec | 3711 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
3712 | cmd_completion(cmdname, cmd->name); |
3713 | } | |
3714 | } else { | |
3715 | /* find the command */ | |
376253ec | 3716 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
3717 | if (compare_cmd(args[0], cmd->name)) |
3718 | goto found; | |
3719 | } | |
3720 | return; | |
3721 | found: | |
4d76d2ba | 3722 | ptype = next_arg_type(cmd->args_type); |
81d0912d FB |
3723 | for(i = 0; i < nb_args - 2; i++) { |
3724 | if (*ptype != '\0') { | |
4d76d2ba | 3725 | ptype = next_arg_type(ptype); |
81d0912d | 3726 | while (*ptype == '?') |
4d76d2ba | 3727 | ptype = next_arg_type(ptype); |
81d0912d FB |
3728 | } |
3729 | } | |
3730 | str = args[nb_args - 1]; | |
2a1704a7 BS |
3731 | if (*ptype == '-' && ptype[1] != '\0') { |
3732 | ptype += 2; | |
3733 | } | |
81d0912d FB |
3734 | switch(*ptype) { |
3735 | case 'F': | |
3736 | /* file completion */ | |
731b0364 | 3737 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3738 | file_completion(str); |
3739 | break; | |
3740 | case 'B': | |
3741 | /* block device name completion */ | |
731b0364 | 3742 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3743 | bdrv_iterate(block_completion_it, (void *)str); |
3744 | break; | |
7fe48483 FB |
3745 | case 's': |
3746 | /* XXX: more generic ? */ | |
3747 | if (!strcmp(cmd->name, "info")) { | |
731b0364 | 3748 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
7fe48483 FB |
3749 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
3750 | cmd_completion(str, cmd->name); | |
3751 | } | |
64866c3d | 3752 | } else if (!strcmp(cmd->name, "sendkey")) { |
e600d1ef BS |
3753 | char *sep = strrchr(str, '-'); |
3754 | if (sep) | |
3755 | str = sep + 1; | |
731b0364 | 3756 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
64866c3d FB |
3757 | for(key = key_defs; key->name != NULL; key++) { |
3758 | cmd_completion(str, key->name); | |
3759 | } | |
f3353c6b JK |
3760 | } else if (!strcmp(cmd->name, "help|?")) { |
3761 | readline_set_completion_index(cur_mon->rs, strlen(str)); | |
3762 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
3763 | cmd_completion(str, cmd->name); | |
3764 | } | |
7fe48483 FB |
3765 | } |
3766 | break; | |
81d0912d FB |
3767 | default: |
3768 | break; | |
3769 | } | |
3770 | } | |
3771 | for(i = 0; i < nb_args; i++) | |
3772 | qemu_free(args[i]); | |
3773 | } | |
3774 | ||
731b0364 | 3775 | static int monitor_can_read(void *opaque) |
9dc39cba | 3776 | { |
731b0364 AL |
3777 | Monitor *mon = opaque; |
3778 | ||
3779 | return (mon->suspend_cnt == 0) ? 128 : 0; | |
9dc39cba FB |
3780 | } |
3781 | ||
5fa737a4 LC |
3782 | typedef struct CmdArgs { |
3783 | QString *name; | |
3784 | int type; | |
3785 | int flag; | |
3786 | int optional; | |
3787 | } CmdArgs; | |
3788 | ||
3789 | static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args) | |
3790 | { | |
3791 | if (!cmd_args->optional) { | |
3792 | qemu_error_new(QERR_MISSING_PARAMETER, name); | |
3793 | return -1; | |
3794 | } | |
3795 | ||
3796 | if (cmd_args->type == '-') { | |
3797 | /* handlers expect a value, they need to be changed */ | |
3798 | qdict_put(args, name, qint_from_int(0)); | |
3799 | } | |
3800 | ||
3801 | return 0; | |
3802 | } | |
3803 | ||
3804 | static int check_arg(const CmdArgs *cmd_args, QDict *args) | |
3805 | { | |
3806 | QObject *value; | |
3807 | const char *name; | |
3808 | ||
3809 | name = qstring_get_str(cmd_args->name); | |
3810 | ||
3811 | if (!args) { | |
3812 | return check_opt(cmd_args, name, args); | |
3813 | } | |
3814 | ||
3815 | value = qdict_get(args, name); | |
3816 | if (!value) { | |
3817 | return check_opt(cmd_args, name, args); | |
3818 | } | |
3819 | ||
3820 | switch (cmd_args->type) { | |
3821 | case 'F': | |
3822 | case 'B': | |
3823 | case 's': | |
3824 | if (qobject_type(value) != QTYPE_QSTRING) { | |
3825 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string"); | |
3826 | return -1; | |
3827 | } | |
3828 | break; | |
3829 | case '/': { | |
3830 | int i; | |
3831 | const char *keys[] = { "count", "format", "size", NULL }; | |
3832 | ||
3833 | for (i = 0; keys[i]; i++) { | |
3834 | QObject *obj = qdict_get(args, keys[i]); | |
3835 | if (!obj) { | |
3836 | qemu_error_new(QERR_MISSING_PARAMETER, name); | |
3837 | return -1; | |
3838 | } | |
3839 | if (qobject_type(obj) != QTYPE_QINT) { | |
3840 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
3841 | return -1; | |
3842 | } | |
3843 | } | |
3844 | break; | |
3845 | } | |
3846 | case 'i': | |
3847 | case 'l': | |
3848 | if (qobject_type(value) != QTYPE_QINT) { | |
3849 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
3850 | return -1; | |
3851 | } | |
3852 | break; | |
3853 | case '-': | |
3854 | if (qobject_type(value) != QTYPE_QINT && | |
3855 | qobject_type(value) != QTYPE_QBOOL) { | |
3856 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool"); | |
3857 | return -1; | |
3858 | } | |
3859 | if (qobject_type(value) == QTYPE_QBOOL) { | |
3860 | /* handlers expect a QInt, they need to be changed */ | |
3861 | qdict_put(args, name, | |
3862 | qint_from_int(qbool_get_int(qobject_to_qbool(value)))); | |
3863 | } | |
3864 | break; | |
3865 | default: | |
3866 | /* impossible */ | |
3867 | abort(); | |
3868 | } | |
3869 | ||
3870 | return 0; | |
3871 | } | |
3872 | ||
3873 | static void cmd_args_init(CmdArgs *cmd_args) | |
3874 | { | |
3875 | cmd_args->name = qstring_new(); | |
3876 | cmd_args->type = cmd_args->flag = cmd_args->optional = 0; | |
3877 | } | |
3878 | ||
3879 | /* | |
3880 | * This is not trivial, we have to parse Monitor command's argument | |
3881 | * type syntax to be able to check the arguments provided by clients. | |
3882 | * | |
3883 | * In the near future we will be using an array for that and will be | |
3884 | * able to drop all this parsing... | |
3885 | */ | |
3886 | static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args) | |
3887 | { | |
3888 | int err; | |
3889 | const char *p; | |
3890 | CmdArgs cmd_args; | |
3891 | ||
dd5121bd | 3892 | if (cmd->args_type == NULL) { |
5fa737a4 LC |
3893 | return (qdict_size(args) == 0 ? 0 : -1); |
3894 | } | |
3895 | ||
3896 | err = 0; | |
3897 | cmd_args_init(&cmd_args); | |
3898 | ||
3899 | for (p = cmd->args_type;; p++) { | |
3900 | if (*p == ':') { | |
3901 | cmd_args.type = *++p; | |
3902 | p++; | |
3903 | if (cmd_args.type == '-') { | |
3904 | cmd_args.flag = *p++; | |
3905 | cmd_args.optional = 1; | |
3906 | } else if (*p == '?') { | |
3907 | cmd_args.optional = 1; | |
3908 | p++; | |
3909 | } | |
3910 | ||
3911 | assert(*p == ',' || *p == '\0'); | |
3912 | err = check_arg(&cmd_args, args); | |
3913 | ||
3914 | QDECREF(cmd_args.name); | |
3915 | cmd_args_init(&cmd_args); | |
3916 | ||
3917 | if (err < 0) { | |
3918 | break; | |
3919 | } | |
3920 | } else { | |
3921 | qstring_append_chr(cmd_args.name, *p); | |
3922 | } | |
3923 | ||
3924 | if (*p == '\0') { | |
3925 | break; | |
3926 | } | |
3927 | } | |
3928 | ||
3929 | QDECREF(cmd_args.name); | |
3930 | return err; | |
3931 | } | |
3932 | ||
3933 | static void handle_qmp_command(JSONMessageParser *parser, QList *tokens) | |
3934 | { | |
3935 | int err; | |
3936 | QObject *obj; | |
3937 | QDict *input, *args; | |
5fa737a4 LC |
3938 | const mon_cmd_t *cmd; |
3939 | Monitor *mon = cur_mon; | |
5e23f480 | 3940 | const char *cmd_name, *info_item; |
5fa737a4 LC |
3941 | |
3942 | args = NULL; | |
3943 | qemu_errors_to_mon(mon); | |
3944 | ||
3945 | obj = json_parser_parse(tokens, NULL); | |
3946 | if (!obj) { | |
3947 | // FIXME: should be triggered in json_parser_parse() | |
3948 | qemu_error_new(QERR_JSON_PARSING); | |
3949 | goto err_out; | |
3950 | } else if (qobject_type(obj) != QTYPE_QDICT) { | |
3951 | qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object"); | |
3952 | qobject_decref(obj); | |
3953 | goto err_out; | |
3954 | } | |
3955 | ||
3956 | input = qobject_to_qdict(obj); | |
3957 | ||
3958 | mon->mc->id = qdict_get(input, "id"); | |
3959 | qobject_incref(mon->mc->id); | |
3960 | ||
3961 | obj = qdict_get(input, "execute"); | |
3962 | if (!obj) { | |
3963 | qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute"); | |
3964 | goto err_input; | |
3965 | } else if (qobject_type(obj) != QTYPE_QSTRING) { | |
3966 | qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string"); | |
3967 | goto err_input; | |
3968 | } | |
3969 | ||
3970 | cmd_name = qstring_get_str(qobject_to_qstring(obj)); | |
5e23f480 LC |
3971 | |
3972 | /* | |
3973 | * XXX: We need this special case until we get info handlers | |
3974 | * converted into 'query-' commands | |
3975 | */ | |
3976 | if (compare_cmd(cmd_name, "info")) { | |
5fa737a4 LC |
3977 | qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name); |
3978 | goto err_input; | |
5e23f480 LC |
3979 | } else if (strstart(cmd_name, "query-", &info_item)) { |
3980 | cmd = monitor_find_command("info"); | |
3981 | qdict_put_obj(input, "arguments", | |
3982 | qobject_from_jsonf("{ 'item': %s }", info_item)); | |
3983 | } else { | |
3984 | cmd = monitor_find_command(cmd_name); | |
3985 | if (!cmd) { | |
3986 | qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name); | |
3987 | goto err_input; | |
3988 | } | |
5fa737a4 LC |
3989 | } |
3990 | ||
3991 | obj = qdict_get(input, "arguments"); | |
3992 | if (!obj) { | |
3993 | args = qdict_new(); | |
3994 | } else { | |
3995 | args = qobject_to_qdict(obj); | |
3996 | QINCREF(args); | |
3997 | } | |
3998 | ||
3999 | QDECREF(input); | |
4000 | ||
4001 | err = monitor_check_qmp_args(cmd, args); | |
4002 | if (err < 0) { | |
4003 | goto err_out; | |
4004 | } | |
4005 | ||
4006 | monitor_call_handler(mon, cmd, args); | |
4007 | goto out; | |
4008 | ||
4009 | err_input: | |
4010 | QDECREF(input); | |
4011 | err_out: | |
4012 | monitor_protocol_emitter(mon, NULL); | |
4013 | out: | |
4014 | QDECREF(args); | |
4015 | qemu_errors_to_previous(); | |
4016 | } | |
4017 | ||
9b57c02e LC |
4018 | /** |
4019 | * monitor_control_read(): Read and handle QMP input | |
4020 | */ | |
4021 | static void monitor_control_read(void *opaque, const uint8_t *buf, int size) | |
4022 | { | |
4023 | Monitor *old_mon = cur_mon; | |
4024 | ||
4025 | cur_mon = opaque; | |
4026 | ||
5fa737a4 | 4027 | json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size); |
9b57c02e LC |
4028 | |
4029 | cur_mon = old_mon; | |
4030 | } | |
4031 | ||
731b0364 | 4032 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 4033 | { |
731b0364 | 4034 | Monitor *old_mon = cur_mon; |
7e2515e8 | 4035 | int i; |
376253ec | 4036 | |
731b0364 AL |
4037 | cur_mon = opaque; |
4038 | ||
cde76ee1 AL |
4039 | if (cur_mon->rs) { |
4040 | for (i = 0; i < size; i++) | |
4041 | readline_handle_byte(cur_mon->rs, buf[i]); | |
4042 | } else { | |
4043 | if (size == 0 || buf[size - 1] != 0) | |
4044 | monitor_printf(cur_mon, "corrupted command\n"); | |
4045 | else | |
f3c157c4 | 4046 | handle_user_command(cur_mon, (char *)buf); |
cde76ee1 | 4047 | } |
9dc39cba | 4048 | |
731b0364 AL |
4049 | cur_mon = old_mon; |
4050 | } | |
d8f44609 | 4051 | |
376253ec | 4052 | static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque) |
aa455485 | 4053 | { |
731b0364 | 4054 | monitor_suspend(mon); |
f3c157c4 | 4055 | handle_user_command(mon, cmdline); |
731b0364 | 4056 | monitor_resume(mon); |
d8f44609 AL |
4057 | } |
4058 | ||
cde76ee1 | 4059 | int monitor_suspend(Monitor *mon) |
d8f44609 | 4060 | { |
cde76ee1 AL |
4061 | if (!mon->rs) |
4062 | return -ENOTTY; | |
731b0364 | 4063 | mon->suspend_cnt++; |
cde76ee1 | 4064 | return 0; |
d8f44609 AL |
4065 | } |
4066 | ||
376253ec | 4067 | void monitor_resume(Monitor *mon) |
d8f44609 | 4068 | { |
cde76ee1 AL |
4069 | if (!mon->rs) |
4070 | return; | |
731b0364 AL |
4071 | if (--mon->suspend_cnt == 0) |
4072 | readline_show_prompt(mon->rs); | |
aa455485 FB |
4073 | } |
4074 | ||
9b57c02e LC |
4075 | /** |
4076 | * monitor_control_event(): Print QMP gretting | |
4077 | */ | |
4078 | static void monitor_control_event(void *opaque, int event) | |
4079 | { | |
4080 | if (event == CHR_EVENT_OPENED) { | |
4081 | QObject *data; | |
4082 | Monitor *mon = opaque; | |
4083 | ||
5fa737a4 LC |
4084 | json_message_parser_init(&mon->mc->parser, handle_qmp_command); |
4085 | ||
9b57c02e LC |
4086 | data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }"); |
4087 | assert(data != NULL); | |
4088 | ||
4089 | monitor_json_emitter(mon, data); | |
4090 | qobject_decref(data); | |
4091 | } | |
4092 | } | |
4093 | ||
731b0364 | 4094 | static void monitor_event(void *opaque, int event) |
86e94dea | 4095 | { |
376253ec AL |
4096 | Monitor *mon = opaque; |
4097 | ||
2724b180 AL |
4098 | switch (event) { |
4099 | case CHR_EVENT_MUX_IN: | |
a7aec5da GH |
4100 | mon->mux_out = 0; |
4101 | if (mon->reset_seen) { | |
4102 | readline_restart(mon->rs); | |
4103 | monitor_resume(mon); | |
4104 | monitor_flush(mon); | |
4105 | } else { | |
4106 | mon->suspend_cnt = 0; | |
4107 | } | |
2724b180 AL |
4108 | break; |
4109 | ||
4110 | case CHR_EVENT_MUX_OUT: | |
a7aec5da GH |
4111 | if (mon->reset_seen) { |
4112 | if (mon->suspend_cnt == 0) { | |
4113 | monitor_printf(mon, "\n"); | |
4114 | } | |
4115 | monitor_flush(mon); | |
4116 | monitor_suspend(mon); | |
4117 | } else { | |
4118 | mon->suspend_cnt++; | |
4119 | } | |
4120 | mon->mux_out = 1; | |
2724b180 | 4121 | break; |
86e94dea | 4122 | |
b6b8df56 | 4123 | case CHR_EVENT_OPENED: |
2724b180 AL |
4124 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " |
4125 | "information\n", QEMU_VERSION); | |
a7aec5da | 4126 | if (!mon->mux_out) { |
2724b180 | 4127 | readline_show_prompt(mon->rs); |
a7aec5da GH |
4128 | } |
4129 | mon->reset_seen = 1; | |
2724b180 AL |
4130 | break; |
4131 | } | |
86e94dea TS |
4132 | } |
4133 | ||
76655d6d AL |
4134 | |
4135 | /* | |
4136 | * Local variables: | |
4137 | * c-indent-level: 4 | |
4138 | * c-basic-offset: 4 | |
4139 | * tab-width: 8 | |
4140 | * End: | |
4141 | */ | |
4142 | ||
731b0364 | 4143 | void monitor_init(CharDriverState *chr, int flags) |
aa455485 | 4144 | { |
731b0364 | 4145 | static int is_first_init = 1; |
87127161 | 4146 | Monitor *mon; |
20d8a3ed TS |
4147 | |
4148 | if (is_first_init) { | |
c8256f9d | 4149 | key_timer = qemu_new_timer(vm_clock, release_keys, NULL); |
20d8a3ed TS |
4150 | is_first_init = 0; |
4151 | } | |
87127161 AL |
4152 | |
4153 | mon = qemu_mallocz(sizeof(*mon)); | |
20d8a3ed | 4154 | |
87127161 | 4155 | mon->chr = chr; |
731b0364 | 4156 | mon->flags = flags; |
cde76ee1 AL |
4157 | if (flags & MONITOR_USE_READLINE) { |
4158 | mon->rs = readline_init(mon, monitor_find_completion); | |
4159 | monitor_read_command(mon, 0); | |
4160 | } | |
87127161 | 4161 | |
9b57c02e | 4162 | if (monitor_ctrl_mode(mon)) { |
5fa737a4 | 4163 | mon->mc = qemu_mallocz(sizeof(MonitorControl)); |
9b57c02e LC |
4164 | /* Control mode requires special handlers */ |
4165 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read, | |
4166 | monitor_control_event, mon); | |
4167 | } else { | |
4168 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, | |
4169 | monitor_event, mon); | |
4170 | } | |
87127161 | 4171 | |
72cf2d4f | 4172 | QLIST_INSERT_HEAD(&mon_list, mon, entry); |
731b0364 | 4173 | if (!cur_mon || (flags & MONITOR_IS_DEFAULT)) |
87127161 | 4174 | cur_mon = mon; |
aa455485 FB |
4175 | } |
4176 | ||
376253ec | 4177 | static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque) |
81d0912d | 4178 | { |
bb5fc20f AL |
4179 | BlockDriverState *bs = opaque; |
4180 | int ret = 0; | |
81d0912d | 4181 | |
bb5fc20f | 4182 | if (bdrv_set_key(bs, password) != 0) { |
376253ec | 4183 | monitor_printf(mon, "invalid password\n"); |
bb5fc20f | 4184 | ret = -EPERM; |
9dc39cba | 4185 | } |
731b0364 AL |
4186 | if (mon->password_completion_cb) |
4187 | mon->password_completion_cb(mon->password_opaque, ret); | |
bb5fc20f | 4188 | |
731b0364 | 4189 | monitor_read_command(mon, 1); |
9dc39cba | 4190 | } |
c0f4ce77 | 4191 | |
376253ec | 4192 | void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs, |
bb5fc20f AL |
4193 | BlockDriverCompletionFunc *completion_cb, |
4194 | void *opaque) | |
c0f4ce77 | 4195 | { |
cde76ee1 AL |
4196 | int err; |
4197 | ||
bb5fc20f AL |
4198 | if (!bdrv_key_required(bs)) { |
4199 | if (completion_cb) | |
4200 | completion_cb(opaque, 0); | |
4201 | return; | |
4202 | } | |
c0f4ce77 | 4203 | |
94171e11 LC |
4204 | if (monitor_ctrl_mode(mon)) { |
4205 | qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs)); | |
4206 | return; | |
4207 | } | |
4208 | ||
376253ec AL |
4209 | monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs), |
4210 | bdrv_get_encrypted_filename(bs)); | |
bb5fc20f | 4211 | |
731b0364 AL |
4212 | mon->password_completion_cb = completion_cb; |
4213 | mon->password_opaque = opaque; | |
bb5fc20f | 4214 | |
cde76ee1 AL |
4215 | err = monitor_read_password(mon, bdrv_password_cb, bs); |
4216 | ||
4217 | if (err && completion_cb) | |
4218 | completion_cb(opaque, err); | |
c0f4ce77 | 4219 | } |
ac7531ec GH |
4220 | |
4221 | typedef struct QemuErrorSink QemuErrorSink; | |
4222 | struct QemuErrorSink { | |
4223 | enum { | |
4224 | ERR_SINK_FILE, | |
4225 | ERR_SINK_MONITOR, | |
4226 | } dest; | |
4227 | union { | |
4228 | FILE *fp; | |
4229 | Monitor *mon; | |
4230 | }; | |
4231 | QemuErrorSink *previous; | |
4232 | }; | |
4233 | ||
528e93a9 | 4234 | static QemuErrorSink *qemu_error_sink; |
ac7531ec GH |
4235 | |
4236 | void qemu_errors_to_file(FILE *fp) | |
4237 | { | |
4238 | QemuErrorSink *sink; | |
4239 | ||
4240 | sink = qemu_mallocz(sizeof(*sink)); | |
4241 | sink->dest = ERR_SINK_FILE; | |
4242 | sink->fp = fp; | |
4243 | sink->previous = qemu_error_sink; | |
4244 | qemu_error_sink = sink; | |
4245 | } | |
4246 | ||
4247 | void qemu_errors_to_mon(Monitor *mon) | |
4248 | { | |
4249 | QemuErrorSink *sink; | |
4250 | ||
4251 | sink = qemu_mallocz(sizeof(*sink)); | |
4252 | sink->dest = ERR_SINK_MONITOR; | |
4253 | sink->mon = mon; | |
4254 | sink->previous = qemu_error_sink; | |
4255 | qemu_error_sink = sink; | |
4256 | } | |
4257 | ||
4258 | void qemu_errors_to_previous(void) | |
4259 | { | |
4260 | QemuErrorSink *sink; | |
4261 | ||
4262 | assert(qemu_error_sink != NULL); | |
4263 | sink = qemu_error_sink; | |
4264 | qemu_error_sink = sink->previous; | |
4265 | qemu_free(sink); | |
4266 | } | |
4267 | ||
4268 | void qemu_error(const char *fmt, ...) | |
4269 | { | |
4270 | va_list args; | |
4271 | ||
4272 | assert(qemu_error_sink != NULL); | |
4273 | switch (qemu_error_sink->dest) { | |
4274 | case ERR_SINK_FILE: | |
4275 | va_start(args, fmt); | |
4276 | vfprintf(qemu_error_sink->fp, fmt, args); | |
4277 | va_end(args); | |
4278 | break; | |
4279 | case ERR_SINK_MONITOR: | |
4280 | va_start(args, fmt); | |
4281 | monitor_vprintf(qemu_error_sink->mon, fmt, args); | |
4282 | va_end(args); | |
4283 | break; | |
4284 | } | |
4285 | } | |
8204a918 LC |
4286 | |
4287 | void qemu_error_internal(const char *file, int linenr, const char *func, | |
4288 | const char *fmt, ...) | |
4289 | { | |
4290 | va_list va; | |
4291 | QError *qerror; | |
4292 | ||
4293 | assert(qemu_error_sink != NULL); | |
4294 | ||
4295 | va_start(va, fmt); | |
4296 | qerror = qerror_from_info(file, linenr, func, fmt, &va); | |
4297 | va_end(va); | |
4298 | ||
4299 | switch (qemu_error_sink->dest) { | |
4300 | case ERR_SINK_FILE: | |
4301 | qerror_print(qerror); | |
4302 | QDECREF(qerror); | |
4303 | break; | |
4304 | case ERR_SINK_MONITOR: | |
4305 | assert(qemu_error_sink->mon->error == NULL); | |
4306 | qemu_error_sink->mon->error = qerror; | |
4307 | break; | |
4308 | } | |
4309 | } |