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