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