block: User BdrvChild callback for device name
[qemu.git] / monitor.c
1 /*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
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 */
24 #include "qemu/osdep.h"
25 #include <dirent.h>
26 #include "hw/hw.h"
27 #include "monitor/qdev.h"
28 #include "hw/usb.h"
29 #include "hw/i386/pc.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
34 #include "net/net.h"
35 #include "net/slirp.h"
36 #include "sysemu/char.h"
37 #include "ui/qemu-spice.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/numa.h"
40 #include "monitor/monitor.h"
41 #include "qemu/readline.h"
42 #include "ui/console.h"
43 #include "ui/input.h"
44 #include "sysemu/blockdev.h"
45 #include "sysemu/block-backend.h"
46 #include "audio/audio.h"
47 #include "disas/disas.h"
48 #include "sysemu/balloon.h"
49 #include "qemu/timer.h"
50 #include "migration/migration.h"
51 #include "sysemu/kvm.h"
52 #include "qemu/acl.h"
53 #include "sysemu/tpm.h"
54 #include "qapi/qmp/qerror.h"
55 #include "qapi/qmp/qint.h"
56 #include "qapi/qmp/qfloat.h"
57 #include "qapi/qmp/qlist.h"
58 #include "qapi/qmp/qbool.h"
59 #include "qapi/qmp/qstring.h"
60 #include "qapi/qmp/qjson.h"
61 #include "qapi/qmp/json-streamer.h"
62 #include "qapi/qmp/json-parser.h"
63 #include <qom/object_interfaces.h>
64 #include "cpu.h"
65 #include "trace.h"
66 #include "trace/control.h"
67 #include "monitor/hmp-target.h"
68 #ifdef CONFIG_TRACE_SIMPLE
69 #include "trace/simple.h"
70 #endif
71 #include "exec/memory.h"
72 #include "qmp-commands.h"
73 #include "hmp.h"
74 #include "qemu/thread.h"
75 #include "block/qapi.h"
76 #include "qapi/qmp-event.h"
77 #include "qapi-event.h"
78 #include "qmp-introspect.h"
79 #include "sysemu/block-backend.h"
80 #include "sysemu/qtest.h"
81 #include "qemu/cutils.h"
82
83 /* for hmp_info_irq/pic */
84 #if defined(TARGET_SPARC)
85 #include "hw/sparc/sun4m.h"
86 #endif
87 #include "hw/lm32/lm32_pic.h"
88
89 #if defined(TARGET_S390X)
90 #include "hw/s390x/storage-keys.h"
91 #endif
92
93 /*
94 * Supported types:
95 *
96 * 'F' filename
97 * 'B' block device name
98 * 's' string (accept optional quote)
99 * 'S' it just appends the rest of the string (accept optional quote)
100 * 'O' option string of the form NAME=VALUE,...
101 * parsed according to QemuOptsList given by its name
102 * Example: 'device:O' uses qemu_device_opts.
103 * Restriction: only lists with empty desc are supported
104 * TODO lift the restriction
105 * 'i' 32 bit integer
106 * 'l' target long (32 or 64 bit)
107 * 'M' Non-negative target long (32 or 64 bit), in user mode the
108 * value is multiplied by 2^20 (think Mebibyte)
109 * 'o' octets (aka bytes)
110 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
111 * K, k suffix, which multiplies the value by 2^60 for suffixes E
112 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
113 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
114 * 'T' double
115 * user mode accepts an optional ms, us, ns suffix,
116 * which divides the value by 1e3, 1e6, 1e9, respectively
117 * '/' optional gdb-like print format (like "/10x")
118 *
119 * '?' optional type (for all types, except '/')
120 * '.' other form of optional type (for 'i' and 'l')
121 * 'b' boolean
122 * user mode accepts "on" or "off"
123 * '-' optional parameter (eg. '-f')
124 *
125 */
126
127 typedef struct mon_cmd_t {
128 const char *name;
129 const char *args_type;
130 const char *params;
131 const char *help;
132 union {
133 void (*cmd)(Monitor *mon, const QDict *qdict);
134 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
135 } mhandler;
136 /* @sub_table is a list of 2nd level of commands. If it do not exist,
137 * mhandler should be used. If it exist, sub_table[?].mhandler should be
138 * used, and mhandler of 1st level plays the role of help function.
139 */
140 struct mon_cmd_t *sub_table;
141 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
142 } mon_cmd_t;
143
144 /* file descriptors passed via SCM_RIGHTS */
145 typedef struct mon_fd_t mon_fd_t;
146 struct mon_fd_t {
147 char *name;
148 int fd;
149 QLIST_ENTRY(mon_fd_t) next;
150 };
151
152 /* file descriptor associated with a file descriptor set */
153 typedef struct MonFdsetFd MonFdsetFd;
154 struct MonFdsetFd {
155 int fd;
156 bool removed;
157 char *opaque;
158 QLIST_ENTRY(MonFdsetFd) next;
159 };
160
161 /* file descriptor set containing fds passed via SCM_RIGHTS */
162 typedef struct MonFdset MonFdset;
163 struct MonFdset {
164 int64_t id;
165 QLIST_HEAD(, MonFdsetFd) fds;
166 QLIST_HEAD(, MonFdsetFd) dup_fds;
167 QLIST_ENTRY(MonFdset) next;
168 };
169
170 typedef struct {
171 QObject *id;
172 JSONMessageParser parser;
173 /*
174 * When a client connects, we're in capabilities negotiation mode.
175 * When command qmp_capabilities succeeds, we go into command
176 * mode.
177 */
178 bool in_command_mode; /* are we in command mode? */
179 } MonitorQMP;
180
181 /*
182 * To prevent flooding clients, events can be throttled. The
183 * throttling is calculated globally, rather than per-Monitor
184 * instance.
185 */
186 typedef struct MonitorQAPIEventState {
187 QAPIEvent event; /* Throttling state for this event type and... */
188 QDict *data; /* ... data, see qapi_event_throttle_equal() */
189 QEMUTimer *timer; /* Timer for handling delayed events */
190 QDict *qdict; /* Delayed event (if any) */
191 } MonitorQAPIEventState;
192
193 typedef struct {
194 int64_t rate; /* Minimum time (in ns) between two events */
195 } MonitorQAPIEventConf;
196
197 struct Monitor {
198 CharDriverState *chr;
199 int reset_seen;
200 int flags;
201 int suspend_cnt;
202 bool skip_flush;
203
204 QemuMutex out_lock;
205 QString *outbuf;
206 guint out_watch;
207
208 /* Read under either BQL or out_lock, written with BQL+out_lock. */
209 int mux_out;
210
211 ReadLineState *rs;
212 MonitorQMP qmp;
213 CPUState *mon_cpu;
214 BlockCompletionFunc *password_completion_cb;
215 void *password_opaque;
216 mon_cmd_t *cmd_table;
217 QLIST_HEAD(,mon_fd_t) fds;
218 QLIST_ENTRY(Monitor) entry;
219 };
220
221 /* QMP checker flags */
222 #define QMP_ACCEPT_UNKNOWNS 1
223
224 /* Protects mon_list, monitor_event_state. */
225 static QemuMutex monitor_lock;
226
227 static QLIST_HEAD(mon_list, Monitor) mon_list;
228 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
229 static int mon_refcount;
230
231 static mon_cmd_t mon_cmds[];
232 static mon_cmd_t info_cmds[];
233
234 static const mon_cmd_t qmp_cmds[];
235
236 Monitor *cur_mon;
237
238 static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
239
240 static void monitor_command_cb(void *opaque, const char *cmdline,
241 void *readline_opaque);
242
243 /**
244 * Is @mon a QMP monitor?
245 */
246 static inline bool monitor_is_qmp(const Monitor *mon)
247 {
248 return (mon->flags & MONITOR_USE_CONTROL);
249 }
250
251 /**
252 * Is the current monitor, if any, a QMP monitor?
253 */
254 bool monitor_cur_is_qmp(void)
255 {
256 return cur_mon && monitor_is_qmp(cur_mon);
257 }
258
259 void monitor_read_command(Monitor *mon, int show_prompt)
260 {
261 if (!mon->rs)
262 return;
263
264 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
265 if (show_prompt)
266 readline_show_prompt(mon->rs);
267 }
268
269 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
270 void *opaque)
271 {
272 if (mon->rs) {
273 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
274 /* prompt is printed on return from the command handler */
275 return 0;
276 } else {
277 monitor_printf(mon, "terminal does not support password prompting\n");
278 return -ENOTTY;
279 }
280 }
281
282 static void monitor_flush_locked(Monitor *mon);
283
284 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
285 void *opaque)
286 {
287 Monitor *mon = opaque;
288
289 qemu_mutex_lock(&mon->out_lock);
290 mon->out_watch = 0;
291 monitor_flush_locked(mon);
292 qemu_mutex_unlock(&mon->out_lock);
293 return FALSE;
294 }
295
296 /* Called with mon->out_lock held. */
297 static void monitor_flush_locked(Monitor *mon)
298 {
299 int rc;
300 size_t len;
301 const char *buf;
302
303 if (mon->skip_flush) {
304 return;
305 }
306
307 buf = qstring_get_str(mon->outbuf);
308 len = qstring_get_length(mon->outbuf);
309
310 if (len && !mon->mux_out) {
311 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
312 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
313 /* all flushed or error */
314 QDECREF(mon->outbuf);
315 mon->outbuf = qstring_new();
316 return;
317 }
318 if (rc > 0) {
319 /* partinal write */
320 QString *tmp = qstring_from_str(buf + rc);
321 QDECREF(mon->outbuf);
322 mon->outbuf = tmp;
323 }
324 if (mon->out_watch == 0) {
325 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
326 monitor_unblocked, mon);
327 }
328 }
329 }
330
331 void monitor_flush(Monitor *mon)
332 {
333 qemu_mutex_lock(&mon->out_lock);
334 monitor_flush_locked(mon);
335 qemu_mutex_unlock(&mon->out_lock);
336 }
337
338 /* flush at every end of line */
339 static void monitor_puts(Monitor *mon, const char *str)
340 {
341 char c;
342
343 qemu_mutex_lock(&mon->out_lock);
344 for(;;) {
345 c = *str++;
346 if (c == '\0')
347 break;
348 if (c == '\n') {
349 qstring_append_chr(mon->outbuf, '\r');
350 }
351 qstring_append_chr(mon->outbuf, c);
352 if (c == '\n') {
353 monitor_flush_locked(mon);
354 }
355 }
356 qemu_mutex_unlock(&mon->out_lock);
357 }
358
359 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
360 {
361 char *buf;
362
363 if (!mon)
364 return;
365
366 if (monitor_is_qmp(mon)) {
367 return;
368 }
369
370 buf = g_strdup_vprintf(fmt, ap);
371 monitor_puts(mon, buf);
372 g_free(buf);
373 }
374
375 void monitor_printf(Monitor *mon, const char *fmt, ...)
376 {
377 va_list ap;
378 va_start(ap, fmt);
379 monitor_vprintf(mon, fmt, ap);
380 va_end(ap);
381 }
382
383 int monitor_fprintf(FILE *stream, const char *fmt, ...)
384 {
385 va_list ap;
386 va_start(ap, fmt);
387 monitor_vprintf((Monitor *)stream, fmt, ap);
388 va_end(ap);
389 return 0;
390 }
391
392 static void monitor_json_emitter(Monitor *mon, const QObject *data)
393 {
394 QString *json;
395
396 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
397 qobject_to_json(data);
398 assert(json != NULL);
399
400 qstring_append_chr(json, '\n');
401 monitor_puts(mon, qstring_get_str(json));
402
403 QDECREF(json);
404 }
405
406 static QDict *build_qmp_error_dict(Error *err)
407 {
408 QObject *obj;
409
410 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
411 QapiErrorClass_lookup[error_get_class(err)],
412 error_get_pretty(err));
413
414 return qobject_to_qdict(obj);
415 }
416
417 static void monitor_protocol_emitter(Monitor *mon, QObject *data,
418 Error *err)
419 {
420 QDict *qmp;
421
422 trace_monitor_protocol_emitter(mon);
423
424 if (!err) {
425 /* success response */
426 qmp = qdict_new();
427 if (data) {
428 qobject_incref(data);
429 qdict_put_obj(qmp, "return", data);
430 } else {
431 /* return an empty QDict by default */
432 qdict_put(qmp, "return", qdict_new());
433 }
434 } else {
435 /* error response */
436 qmp = build_qmp_error_dict(err);
437 }
438
439 if (mon->qmp.id) {
440 qdict_put_obj(qmp, "id", mon->qmp.id);
441 mon->qmp.id = NULL;
442 }
443
444 monitor_json_emitter(mon, QOBJECT(qmp));
445 QDECREF(qmp);
446 }
447
448
449 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
450 /* Limit guest-triggerable events to 1 per second */
451 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
452 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
453 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
454 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
455 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
456 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
457 };
458
459 GHashTable *monitor_qapi_event_state;
460
461 /*
462 * Emits the event to every monitor instance, @event is only used for trace
463 * Called with monitor_lock held.
464 */
465 static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
466 {
467 Monitor *mon;
468
469 trace_monitor_protocol_event_emit(event, qdict);
470 QLIST_FOREACH(mon, &mon_list, entry) {
471 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
472 monitor_json_emitter(mon, QOBJECT(qdict));
473 }
474 }
475 }
476
477 static void monitor_qapi_event_handler(void *opaque);
478
479 /*
480 * Queue a new event for emission to Monitor instances,
481 * applying any rate limiting if required.
482 */
483 static void
484 monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
485 {
486 MonitorQAPIEventConf *evconf;
487 MonitorQAPIEventState *evstate;
488
489 assert(event < QAPI_EVENT__MAX);
490 evconf = &monitor_qapi_event_conf[event];
491 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
492
493 qemu_mutex_lock(&monitor_lock);
494
495 if (!evconf->rate) {
496 /* Unthrottled event */
497 monitor_qapi_event_emit(event, qdict);
498 } else {
499 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
500 MonitorQAPIEventState key = { .event = event, .data = data };
501
502 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
503 assert(!evstate || timer_pending(evstate->timer));
504
505 if (evstate) {
506 /*
507 * Timer is pending for (at least) evconf->rate ns after
508 * last send. Store event for sending when timer fires,
509 * replacing a prior stored event if any.
510 */
511 QDECREF(evstate->qdict);
512 evstate->qdict = qdict;
513 QINCREF(evstate->qdict);
514 } else {
515 /*
516 * Last send was (at least) evconf->rate ns ago.
517 * Send immediately, and arm the timer to call
518 * monitor_qapi_event_handler() in evconf->rate ns. Any
519 * events arriving before then will be delayed until then.
520 */
521 int64_t now = qemu_clock_get_ns(event_clock_type);
522
523 monitor_qapi_event_emit(event, qdict);
524
525 evstate = g_new(MonitorQAPIEventState, 1);
526 evstate->event = event;
527 evstate->data = data;
528 QINCREF(evstate->data);
529 evstate->qdict = NULL;
530 evstate->timer = timer_new_ns(event_clock_type,
531 monitor_qapi_event_handler,
532 evstate);
533 g_hash_table_add(monitor_qapi_event_state, evstate);
534 timer_mod_ns(evstate->timer, now + evconf->rate);
535 }
536 }
537
538 qemu_mutex_unlock(&monitor_lock);
539 }
540
541 /*
542 * This function runs evconf->rate ns after sending a throttled
543 * event.
544 * If another event has since been stored, send it.
545 */
546 static void monitor_qapi_event_handler(void *opaque)
547 {
548 MonitorQAPIEventState *evstate = opaque;
549 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
550
551 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
552 qemu_mutex_lock(&monitor_lock);
553
554 if (evstate->qdict) {
555 int64_t now = qemu_clock_get_ns(event_clock_type);
556
557 monitor_qapi_event_emit(evstate->event, evstate->qdict);
558 QDECREF(evstate->qdict);
559 evstate->qdict = NULL;
560 timer_mod_ns(evstate->timer, now + evconf->rate);
561 } else {
562 g_hash_table_remove(monitor_qapi_event_state, evstate);
563 QDECREF(evstate->data);
564 timer_free(evstate->timer);
565 g_free(evstate);
566 }
567
568 qemu_mutex_unlock(&monitor_lock);
569 }
570
571 static unsigned int qapi_event_throttle_hash(const void *key)
572 {
573 const MonitorQAPIEventState *evstate = key;
574 unsigned int hash = evstate->event * 255;
575
576 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
577 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
578 }
579
580 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
581 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
582 }
583
584 return hash;
585 }
586
587 static gboolean qapi_event_throttle_equal(const void *a, const void *b)
588 {
589 const MonitorQAPIEventState *eva = a;
590 const MonitorQAPIEventState *evb = b;
591
592 if (eva->event != evb->event) {
593 return FALSE;
594 }
595
596 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
597 return !strcmp(qdict_get_str(eva->data, "id"),
598 qdict_get_str(evb->data, "id"));
599 }
600
601 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
602 return !strcmp(qdict_get_str(eva->data, "node-name"),
603 qdict_get_str(evb->data, "node-name"));
604 }
605
606 return TRUE;
607 }
608
609 static void monitor_qapi_event_init(void)
610 {
611 if (qtest_enabled()) {
612 event_clock_type = QEMU_CLOCK_VIRTUAL;
613 }
614
615 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
616 qapi_event_throttle_equal);
617 qmp_event_set_func_emit(monitor_qapi_event_queue);
618 }
619
620 static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
621 {
622 cur_mon->qmp.in_command_mode = true;
623 }
624
625 static void handle_hmp_command(Monitor *mon, const char *cmdline);
626
627 static void monitor_data_init(Monitor *mon)
628 {
629 memset(mon, 0, sizeof(Monitor));
630 qemu_mutex_init(&mon->out_lock);
631 mon->outbuf = qstring_new();
632 /* Use *mon_cmds by default. */
633 mon->cmd_table = mon_cmds;
634 }
635
636 static void monitor_data_destroy(Monitor *mon)
637 {
638 QDECREF(mon->outbuf);
639 qemu_mutex_destroy(&mon->out_lock);
640 }
641
642 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
643 int64_t cpu_index, Error **errp)
644 {
645 char *output = NULL;
646 Monitor *old_mon, hmp;
647
648 monitor_data_init(&hmp);
649 hmp.skip_flush = true;
650
651 old_mon = cur_mon;
652 cur_mon = &hmp;
653
654 if (has_cpu_index) {
655 int ret = monitor_set_cpu(cpu_index);
656 if (ret < 0) {
657 cur_mon = old_mon;
658 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
659 "a CPU number");
660 goto out;
661 }
662 }
663
664 handle_hmp_command(&hmp, command_line);
665 cur_mon = old_mon;
666
667 qemu_mutex_lock(&hmp.out_lock);
668 if (qstring_get_length(hmp.outbuf) > 0) {
669 output = g_strdup(qstring_get_str(hmp.outbuf));
670 } else {
671 output = g_strdup("");
672 }
673 qemu_mutex_unlock(&hmp.out_lock);
674
675 out:
676 monitor_data_destroy(&hmp);
677 return output;
678 }
679
680 static int compare_cmd(const char *name, const char *list)
681 {
682 const char *p, *pstart;
683 int len;
684 len = strlen(name);
685 p = list;
686 for(;;) {
687 pstart = p;
688 p = strchr(p, '|');
689 if (!p)
690 p = pstart + strlen(pstart);
691 if ((p - pstart) == len && !memcmp(pstart, name, len))
692 return 1;
693 if (*p == '\0')
694 break;
695 p++;
696 }
697 return 0;
698 }
699
700 static int get_str(char *buf, int buf_size, const char **pp)
701 {
702 const char *p;
703 char *q;
704 int c;
705
706 q = buf;
707 p = *pp;
708 while (qemu_isspace(*p)) {
709 p++;
710 }
711 if (*p == '\0') {
712 fail:
713 *q = '\0';
714 *pp = p;
715 return -1;
716 }
717 if (*p == '\"') {
718 p++;
719 while (*p != '\0' && *p != '\"') {
720 if (*p == '\\') {
721 p++;
722 c = *p++;
723 switch (c) {
724 case 'n':
725 c = '\n';
726 break;
727 case 'r':
728 c = '\r';
729 break;
730 case '\\':
731 case '\'':
732 case '\"':
733 break;
734 default:
735 printf("unsupported escape code: '\\%c'\n", c);
736 goto fail;
737 }
738 if ((q - buf) < buf_size - 1) {
739 *q++ = c;
740 }
741 } else {
742 if ((q - buf) < buf_size - 1) {
743 *q++ = *p;
744 }
745 p++;
746 }
747 }
748 if (*p != '\"') {
749 printf("unterminated string\n");
750 goto fail;
751 }
752 p++;
753 } else {
754 while (*p != '\0' && !qemu_isspace(*p)) {
755 if ((q - buf) < buf_size - 1) {
756 *q++ = *p;
757 }
758 p++;
759 }
760 }
761 *q = '\0';
762 *pp = p;
763 return 0;
764 }
765
766 #define MAX_ARGS 16
767
768 static void free_cmdline_args(char **args, int nb_args)
769 {
770 int i;
771
772 assert(nb_args <= MAX_ARGS);
773
774 for (i = 0; i < nb_args; i++) {
775 g_free(args[i]);
776 }
777
778 }
779
780 /*
781 * Parse the command line to get valid args.
782 * @cmdline: command line to be parsed.
783 * @pnb_args: location to store the number of args, must NOT be NULL.
784 * @args: location to store the args, which should be freed by caller, must
785 * NOT be NULL.
786 *
787 * Returns 0 on success, negative on failure.
788 *
789 * NOTE: this parser is an approximate form of the real command parser. Number
790 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
791 * return with failure.
792 */
793 static int parse_cmdline(const char *cmdline,
794 int *pnb_args, char **args)
795 {
796 const char *p;
797 int nb_args, ret;
798 char buf[1024];
799
800 p = cmdline;
801 nb_args = 0;
802 for (;;) {
803 while (qemu_isspace(*p)) {
804 p++;
805 }
806 if (*p == '\0') {
807 break;
808 }
809 if (nb_args >= MAX_ARGS) {
810 goto fail;
811 }
812 ret = get_str(buf, sizeof(buf), &p);
813 if (ret < 0) {
814 goto fail;
815 }
816 args[nb_args] = g_strdup(buf);
817 nb_args++;
818 }
819 *pnb_args = nb_args;
820 return 0;
821
822 fail:
823 free_cmdline_args(args, nb_args);
824 return -1;
825 }
826
827 static void help_cmd_dump_one(Monitor *mon,
828 const mon_cmd_t *cmd,
829 char **prefix_args,
830 int prefix_args_nb)
831 {
832 int i;
833
834 for (i = 0; i < prefix_args_nb; i++) {
835 monitor_printf(mon, "%s ", prefix_args[i]);
836 }
837 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
838 }
839
840 /* @args[@arg_index] is the valid command need to find in @cmds */
841 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
842 char **args, int nb_args, int arg_index)
843 {
844 const mon_cmd_t *cmd;
845
846 /* No valid arg need to compare with, dump all in *cmds */
847 if (arg_index >= nb_args) {
848 for (cmd = cmds; cmd->name != NULL; cmd++) {
849 help_cmd_dump_one(mon, cmd, args, arg_index);
850 }
851 return;
852 }
853
854 /* Find one entry to dump */
855 for (cmd = cmds; cmd->name != NULL; cmd++) {
856 if (compare_cmd(args[arg_index], cmd->name)) {
857 if (cmd->sub_table) {
858 /* continue with next arg */
859 help_cmd_dump(mon, cmd->sub_table,
860 args, nb_args, arg_index + 1);
861 } else {
862 help_cmd_dump_one(mon, cmd, args, arg_index);
863 }
864 break;
865 }
866 }
867 }
868
869 static void help_cmd(Monitor *mon, const char *name)
870 {
871 char *args[MAX_ARGS];
872 int nb_args = 0;
873
874 /* 1. parse user input */
875 if (name) {
876 /* special case for log, directly dump and return */
877 if (!strcmp(name, "log")) {
878 const QEMULogItem *item;
879 monitor_printf(mon, "Log items (comma separated):\n");
880 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
881 for (item = qemu_log_items; item->mask != 0; item++) {
882 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
883 }
884 return;
885 }
886
887 if (parse_cmdline(name, &nb_args, args) < 0) {
888 return;
889 }
890 }
891
892 /* 2. dump the contents according to parsed args */
893 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
894
895 free_cmdline_args(args, nb_args);
896 }
897
898 static void do_help_cmd(Monitor *mon, const QDict *qdict)
899 {
900 help_cmd(mon, qdict_get_try_str(qdict, "name"));
901 }
902
903 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
904 {
905 const char *tp_name = qdict_get_str(qdict, "name");
906 bool new_state = qdict_get_bool(qdict, "option");
907 Error *local_err = NULL;
908
909 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
910 if (local_err) {
911 error_report_err(local_err);
912 }
913 }
914
915 #ifdef CONFIG_TRACE_SIMPLE
916 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
917 {
918 const char *op = qdict_get_try_str(qdict, "op");
919 const char *arg = qdict_get_try_str(qdict, "arg");
920
921 if (!op) {
922 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
923 } else if (!strcmp(op, "on")) {
924 st_set_trace_file_enabled(true);
925 } else if (!strcmp(op, "off")) {
926 st_set_trace_file_enabled(false);
927 } else if (!strcmp(op, "flush")) {
928 st_flush_trace_buffer();
929 } else if (!strcmp(op, "set")) {
930 if (arg) {
931 st_set_trace_file(arg);
932 }
933 } else {
934 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
935 help_cmd(mon, "trace-file");
936 }
937 }
938 #endif
939
940 static void hmp_info_help(Monitor *mon, const QDict *qdict)
941 {
942 help_cmd(mon, "info");
943 }
944
945 CommandInfoList *qmp_query_commands(Error **errp)
946 {
947 CommandInfoList *info, *cmd_list = NULL;
948 const mon_cmd_t *cmd;
949
950 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
951 info = g_malloc0(sizeof(*info));
952 info->value = g_malloc0(sizeof(*info->value));
953 info->value->name = g_strdup(cmd->name);
954
955 info->next = cmd_list;
956 cmd_list = info;
957 }
958
959 return cmd_list;
960 }
961
962 EventInfoList *qmp_query_events(Error **errp)
963 {
964 EventInfoList *info, *ev_list = NULL;
965 QAPIEvent e;
966
967 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
968 const char *event_name = QAPIEvent_lookup[e];
969 assert(event_name != NULL);
970 info = g_malloc0(sizeof(*info));
971 info->value = g_malloc0(sizeof(*info->value));
972 info->value->name = g_strdup(event_name);
973
974 info->next = ev_list;
975 ev_list = info;
976 }
977
978 return ev_list;
979 }
980
981 /*
982 * Minor hack: generated marshalling suppressed for this command
983 * ('gen': false in the schema) so we can parse the JSON string
984 * directly into QObject instead of first parsing it with
985 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
986 * to QObject with generated output marshallers, every time. Instead,
987 * we do it in test-qmp-input-visitor.c, just to make sure
988 * qapi-introspect.py's output actually conforms to the schema.
989 */
990 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
991 Error **errp)
992 {
993 *ret_data = qobject_from_json(qmp_schema_json);
994 }
995
996 /* set the current CPU defined by the user */
997 int monitor_set_cpu(int cpu_index)
998 {
999 CPUState *cpu;
1000
1001 cpu = qemu_get_cpu(cpu_index);
1002 if (cpu == NULL) {
1003 return -1;
1004 }
1005 cur_mon->mon_cpu = cpu;
1006 return 0;
1007 }
1008
1009 CPUState *mon_get_cpu(void)
1010 {
1011 if (!cur_mon->mon_cpu) {
1012 monitor_set_cpu(0);
1013 }
1014 cpu_synchronize_state(cur_mon->mon_cpu);
1015 return cur_mon->mon_cpu;
1016 }
1017
1018 CPUArchState *mon_get_cpu_env(void)
1019 {
1020 return mon_get_cpu()->env_ptr;
1021 }
1022
1023 int monitor_get_cpu_index(void)
1024 {
1025 return mon_get_cpu()->cpu_index;
1026 }
1027
1028 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
1029 {
1030 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1031 }
1032
1033 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
1034 {
1035 dump_exec_info((FILE *)mon, monitor_fprintf);
1036 dump_drift_info((FILE *)mon, monitor_fprintf);
1037 }
1038
1039 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
1040 {
1041 dump_opcount_info((FILE *)mon, monitor_fprintf);
1042 }
1043
1044 static void hmp_info_history(Monitor *mon, const QDict *qdict)
1045 {
1046 int i;
1047 const char *str;
1048
1049 if (!mon->rs)
1050 return;
1051 i = 0;
1052 for(;;) {
1053 str = readline_get_history(mon->rs, i);
1054 if (!str)
1055 break;
1056 monitor_printf(mon, "%d: '%s'\n", i, str);
1057 i++;
1058 }
1059 }
1060
1061 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
1062 {
1063 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
1064 }
1065
1066 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
1067 {
1068 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1069 TraceEventInfoList *elem;
1070
1071 for (elem = events; elem != NULL; elem = elem->next) {
1072 monitor_printf(mon, "%s : state %u\n",
1073 elem->value->name,
1074 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1075 }
1076 qapi_free_TraceEventInfoList(events);
1077 }
1078
1079 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1080 bool has_port, int64_t port,
1081 bool has_tls_port, int64_t tls_port,
1082 bool has_cert_subject, const char *cert_subject,
1083 Error **errp)
1084 {
1085 if (strcmp(protocol, "spice") == 0) {
1086 if (!qemu_using_spice(errp)) {
1087 return;
1088 }
1089
1090 if (!has_port && !has_tls_port) {
1091 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1092 return;
1093 }
1094
1095 if (qemu_spice_migrate_info(hostname,
1096 has_port ? port : -1,
1097 has_tls_port ? tls_port : -1,
1098 cert_subject)) {
1099 error_setg(errp, QERR_UNDEFINED_ERROR);
1100 return;
1101 }
1102 return;
1103 }
1104
1105 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1106 }
1107
1108 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1109 {
1110 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1111 }
1112
1113 static void hmp_log(Monitor *mon, const QDict *qdict)
1114 {
1115 int mask;
1116 const char *items = qdict_get_str(qdict, "items");
1117
1118 if (!strcmp(items, "none")) {
1119 mask = 0;
1120 } else {
1121 mask = qemu_str_to_log_mask(items);
1122 if (!mask) {
1123 help_cmd(mon, "log");
1124 return;
1125 }
1126 }
1127 qemu_set_log(mask);
1128 }
1129
1130 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1131 {
1132 const char *option = qdict_get_try_str(qdict, "option");
1133 if (!option || !strcmp(option, "on")) {
1134 singlestep = 1;
1135 } else if (!strcmp(option, "off")) {
1136 singlestep = 0;
1137 } else {
1138 monitor_printf(mon, "unexpected option %s\n", option);
1139 }
1140 }
1141
1142 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1143 {
1144 const char *device = qdict_get_try_str(qdict, "device");
1145 if (!device)
1146 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1147 if (gdbserver_start(device) < 0) {
1148 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1149 device);
1150 } else if (strcmp(device, "none") == 0) {
1151 monitor_printf(mon, "Disabled gdbserver\n");
1152 } else {
1153 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1154 device);
1155 }
1156 }
1157
1158 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1159 {
1160 const char *action = qdict_get_str(qdict, "action");
1161 if (select_watchdog_action(action) == -1) {
1162 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1163 }
1164 }
1165
1166 static void monitor_printc(Monitor *mon, int c)
1167 {
1168 monitor_printf(mon, "'");
1169 switch(c) {
1170 case '\'':
1171 monitor_printf(mon, "\\'");
1172 break;
1173 case '\\':
1174 monitor_printf(mon, "\\\\");
1175 break;
1176 case '\n':
1177 monitor_printf(mon, "\\n");
1178 break;
1179 case '\r':
1180 monitor_printf(mon, "\\r");
1181 break;
1182 default:
1183 if (c >= 32 && c <= 126) {
1184 monitor_printf(mon, "%c", c);
1185 } else {
1186 monitor_printf(mon, "\\x%02x", c);
1187 }
1188 break;
1189 }
1190 monitor_printf(mon, "'");
1191 }
1192
1193 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1194 hwaddr addr, int is_physical)
1195 {
1196 int l, line_size, i, max_digits, len;
1197 uint8_t buf[16];
1198 uint64_t v;
1199
1200 if (format == 'i') {
1201 int flags = 0;
1202 #ifdef TARGET_I386
1203 CPUArchState *env = mon_get_cpu_env();
1204 if (wsize == 2) {
1205 flags = 1;
1206 } else if (wsize == 4) {
1207 flags = 0;
1208 } else {
1209 /* as default we use the current CS size */
1210 flags = 0;
1211 if (env) {
1212 #ifdef TARGET_X86_64
1213 if ((env->efer & MSR_EFER_LMA) &&
1214 (env->segs[R_CS].flags & DESC_L_MASK))
1215 flags = 2;
1216 else
1217 #endif
1218 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1219 flags = 1;
1220 }
1221 }
1222 #endif
1223 #ifdef TARGET_PPC
1224 CPUArchState *env = mon_get_cpu_env();
1225 flags = msr_le << 16;
1226 flags |= env->bfd_mach;
1227 #endif
1228 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
1229 return;
1230 }
1231
1232 len = wsize * count;
1233 if (wsize == 1)
1234 line_size = 8;
1235 else
1236 line_size = 16;
1237 max_digits = 0;
1238
1239 switch(format) {
1240 case 'o':
1241 max_digits = (wsize * 8 + 2) / 3;
1242 break;
1243 default:
1244 case 'x':
1245 max_digits = (wsize * 8) / 4;
1246 break;
1247 case 'u':
1248 case 'd':
1249 max_digits = (wsize * 8 * 10 + 32) / 33;
1250 break;
1251 case 'c':
1252 wsize = 1;
1253 break;
1254 }
1255
1256 while (len > 0) {
1257 if (is_physical)
1258 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1259 else
1260 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1261 l = len;
1262 if (l > line_size)
1263 l = line_size;
1264 if (is_physical) {
1265 cpu_physical_memory_read(addr, buf, l);
1266 } else {
1267 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
1268 monitor_printf(mon, " Cannot access memory\n");
1269 break;
1270 }
1271 }
1272 i = 0;
1273 while (i < l) {
1274 switch(wsize) {
1275 default:
1276 case 1:
1277 v = ldub_p(buf + i);
1278 break;
1279 case 2:
1280 v = lduw_p(buf + i);
1281 break;
1282 case 4:
1283 v = (uint32_t)ldl_p(buf + i);
1284 break;
1285 case 8:
1286 v = ldq_p(buf + i);
1287 break;
1288 }
1289 monitor_printf(mon, " ");
1290 switch(format) {
1291 case 'o':
1292 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1293 break;
1294 case 'x':
1295 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1296 break;
1297 case 'u':
1298 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1299 break;
1300 case 'd':
1301 monitor_printf(mon, "%*" PRId64, max_digits, v);
1302 break;
1303 case 'c':
1304 monitor_printc(mon, v);
1305 break;
1306 }
1307 i += wsize;
1308 }
1309 monitor_printf(mon, "\n");
1310 addr += l;
1311 len -= l;
1312 }
1313 }
1314
1315 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1316 {
1317 int count = qdict_get_int(qdict, "count");
1318 int format = qdict_get_int(qdict, "format");
1319 int size = qdict_get_int(qdict, "size");
1320 target_long addr = qdict_get_int(qdict, "addr");
1321
1322 memory_dump(mon, count, format, size, addr, 0);
1323 }
1324
1325 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1326 {
1327 int count = qdict_get_int(qdict, "count");
1328 int format = qdict_get_int(qdict, "format");
1329 int size = qdict_get_int(qdict, "size");
1330 hwaddr addr = qdict_get_int(qdict, "addr");
1331
1332 memory_dump(mon, count, format, size, addr, 1);
1333 }
1334
1335 static void do_print(Monitor *mon, const QDict *qdict)
1336 {
1337 int format = qdict_get_int(qdict, "format");
1338 hwaddr val = qdict_get_int(qdict, "val");
1339
1340 switch(format) {
1341 case 'o':
1342 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1343 break;
1344 case 'x':
1345 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1346 break;
1347 case 'u':
1348 monitor_printf(mon, "%" HWADDR_PRIu, val);
1349 break;
1350 default:
1351 case 'd':
1352 monitor_printf(mon, "%" HWADDR_PRId, val);
1353 break;
1354 case 'c':
1355 monitor_printc(mon, val);
1356 break;
1357 }
1358 monitor_printf(mon, "\n");
1359 }
1360
1361 static void hmp_sum(Monitor *mon, const QDict *qdict)
1362 {
1363 uint32_t addr;
1364 uint16_t sum;
1365 uint32_t start = qdict_get_int(qdict, "start");
1366 uint32_t size = qdict_get_int(qdict, "size");
1367
1368 sum = 0;
1369 for(addr = start; addr < (start + size); addr++) {
1370 uint8_t val = address_space_ldub(&address_space_memory, addr,
1371 MEMTXATTRS_UNSPECIFIED, NULL);
1372 /* BSD sum algorithm ('sum' Unix command) */
1373 sum = (sum >> 1) | (sum << 15);
1374 sum += val;
1375 }
1376 monitor_printf(mon, "%05d\n", sum);
1377 }
1378
1379 static int mouse_button_state;
1380
1381 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1382 {
1383 int dx, dy, dz, button;
1384 const char *dx_str = qdict_get_str(qdict, "dx_str");
1385 const char *dy_str = qdict_get_str(qdict, "dy_str");
1386 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1387
1388 dx = strtol(dx_str, NULL, 0);
1389 dy = strtol(dy_str, NULL, 0);
1390 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1391 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1392
1393 if (dz_str) {
1394 dz = strtol(dz_str, NULL, 0);
1395 if (dz != 0) {
1396 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1397 qemu_input_queue_btn(NULL, button, true);
1398 qemu_input_event_sync();
1399 qemu_input_queue_btn(NULL, button, false);
1400 }
1401 }
1402 qemu_input_event_sync();
1403 }
1404
1405 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1406 {
1407 static uint32_t bmap[INPUT_BUTTON__MAX] = {
1408 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1409 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1410 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1411 };
1412 int button_state = qdict_get_int(qdict, "button_state");
1413
1414 if (mouse_button_state == button_state) {
1415 return;
1416 }
1417 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1418 qemu_input_event_sync();
1419 mouse_button_state = button_state;
1420 }
1421
1422 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1423 {
1424 int size = qdict_get_int(qdict, "size");
1425 int addr = qdict_get_int(qdict, "addr");
1426 int has_index = qdict_haskey(qdict, "index");
1427 uint32_t val;
1428 int suffix;
1429
1430 if (has_index) {
1431 int index = qdict_get_int(qdict, "index");
1432 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1433 addr++;
1434 }
1435 addr &= 0xffff;
1436
1437 switch(size) {
1438 default:
1439 case 1:
1440 val = cpu_inb(addr);
1441 suffix = 'b';
1442 break;
1443 case 2:
1444 val = cpu_inw(addr);
1445 suffix = 'w';
1446 break;
1447 case 4:
1448 val = cpu_inl(addr);
1449 suffix = 'l';
1450 break;
1451 }
1452 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1453 suffix, addr, size * 2, val);
1454 }
1455
1456 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1457 {
1458 int size = qdict_get_int(qdict, "size");
1459 int addr = qdict_get_int(qdict, "addr");
1460 int val = qdict_get_int(qdict, "val");
1461
1462 addr &= IOPORTS_MASK;
1463
1464 switch (size) {
1465 default:
1466 case 1:
1467 cpu_outb(addr, val);
1468 break;
1469 case 2:
1470 cpu_outw(addr, val);
1471 break;
1472 case 4:
1473 cpu_outl(addr, val);
1474 break;
1475 }
1476 }
1477
1478 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1479 {
1480 Error *local_err = NULL;
1481 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1482
1483 qemu_boot_set(bootdevice, &local_err);
1484 if (local_err) {
1485 error_report_err(local_err);
1486 } else {
1487 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1488 }
1489 }
1490
1491 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1492 {
1493 mtree_info((fprintf_function)monitor_printf, mon);
1494 }
1495
1496 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1497 {
1498 int i;
1499 CPUState *cpu;
1500 uint64_t *node_mem;
1501
1502 node_mem = g_new0(uint64_t, nb_numa_nodes);
1503 query_numa_node_mem(node_mem);
1504 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1505 for (i = 0; i < nb_numa_nodes; i++) {
1506 monitor_printf(mon, "node %d cpus:", i);
1507 CPU_FOREACH(cpu) {
1508 if (cpu->numa_node == i) {
1509 monitor_printf(mon, " %d", cpu->cpu_index);
1510 }
1511 }
1512 monitor_printf(mon, "\n");
1513 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1514 node_mem[i] >> 20);
1515 }
1516 g_free(node_mem);
1517 }
1518
1519 #ifdef CONFIG_PROFILER
1520
1521 int64_t tcg_time;
1522 int64_t dev_time;
1523
1524 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1525 {
1526 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1527 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
1528 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1529 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
1530 tcg_time = 0;
1531 dev_time = 0;
1532 }
1533 #else
1534 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1535 {
1536 monitor_printf(mon, "Internal profiler not compiled\n");
1537 }
1538 #endif
1539
1540 /* Capture support */
1541 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1542
1543 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1544 {
1545 int i;
1546 CaptureState *s;
1547
1548 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1549 monitor_printf(mon, "[%d]: ", i);
1550 s->ops.info (s->opaque);
1551 }
1552 }
1553
1554 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1555 {
1556 int i;
1557 int n = qdict_get_int(qdict, "n");
1558 CaptureState *s;
1559
1560 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1561 if (i == n) {
1562 s->ops.destroy (s->opaque);
1563 QLIST_REMOVE (s, entries);
1564 g_free (s);
1565 return;
1566 }
1567 }
1568 }
1569
1570 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1571 {
1572 const char *path = qdict_get_str(qdict, "path");
1573 int has_freq = qdict_haskey(qdict, "freq");
1574 int freq = qdict_get_try_int(qdict, "freq", -1);
1575 int has_bits = qdict_haskey(qdict, "bits");
1576 int bits = qdict_get_try_int(qdict, "bits", -1);
1577 int has_channels = qdict_haskey(qdict, "nchannels");
1578 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1579 CaptureState *s;
1580
1581 s = g_malloc0 (sizeof (*s));
1582
1583 freq = has_freq ? freq : 44100;
1584 bits = has_bits ? bits : 16;
1585 nchannels = has_channels ? nchannels : 2;
1586
1587 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1588 monitor_printf(mon, "Failed to add wave capture\n");
1589 g_free (s);
1590 return;
1591 }
1592 QLIST_INSERT_HEAD (&capture_head, s, entries);
1593 }
1594
1595 static qemu_acl *find_acl(Monitor *mon, const char *name)
1596 {
1597 qemu_acl *acl = qemu_acl_find(name);
1598
1599 if (!acl) {
1600 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1601 }
1602 return acl;
1603 }
1604
1605 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1606 {
1607 const char *aclname = qdict_get_str(qdict, "aclname");
1608 qemu_acl *acl = find_acl(mon, aclname);
1609 qemu_acl_entry *entry;
1610 int i = 0;
1611
1612 if (acl) {
1613 monitor_printf(mon, "policy: %s\n",
1614 acl->defaultDeny ? "deny" : "allow");
1615 QTAILQ_FOREACH(entry, &acl->entries, next) {
1616 i++;
1617 monitor_printf(mon, "%d: %s %s\n", i,
1618 entry->deny ? "deny" : "allow", entry->match);
1619 }
1620 }
1621 }
1622
1623 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1624 {
1625 const char *aclname = qdict_get_str(qdict, "aclname");
1626 qemu_acl *acl = find_acl(mon, aclname);
1627
1628 if (acl) {
1629 qemu_acl_reset(acl);
1630 monitor_printf(mon, "acl: removed all rules\n");
1631 }
1632 }
1633
1634 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1635 {
1636 const char *aclname = qdict_get_str(qdict, "aclname");
1637 const char *policy = qdict_get_str(qdict, "policy");
1638 qemu_acl *acl = find_acl(mon, aclname);
1639
1640 if (acl) {
1641 if (strcmp(policy, "allow") == 0) {
1642 acl->defaultDeny = 0;
1643 monitor_printf(mon, "acl: policy set to 'allow'\n");
1644 } else if (strcmp(policy, "deny") == 0) {
1645 acl->defaultDeny = 1;
1646 monitor_printf(mon, "acl: policy set to 'deny'\n");
1647 } else {
1648 monitor_printf(mon, "acl: unknown policy '%s', "
1649 "expected 'deny' or 'allow'\n", policy);
1650 }
1651 }
1652 }
1653
1654 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1655 {
1656 const char *aclname = qdict_get_str(qdict, "aclname");
1657 const char *match = qdict_get_str(qdict, "match");
1658 const char *policy = qdict_get_str(qdict, "policy");
1659 int has_index = qdict_haskey(qdict, "index");
1660 int index = qdict_get_try_int(qdict, "index", -1);
1661 qemu_acl *acl = find_acl(mon, aclname);
1662 int deny, ret;
1663
1664 if (acl) {
1665 if (strcmp(policy, "allow") == 0) {
1666 deny = 0;
1667 } else if (strcmp(policy, "deny") == 0) {
1668 deny = 1;
1669 } else {
1670 monitor_printf(mon, "acl: unknown policy '%s', "
1671 "expected 'deny' or 'allow'\n", policy);
1672 return;
1673 }
1674 if (has_index)
1675 ret = qemu_acl_insert(acl, deny, match, index);
1676 else
1677 ret = qemu_acl_append(acl, deny, match);
1678 if (ret < 0)
1679 monitor_printf(mon, "acl: unable to add acl entry\n");
1680 else
1681 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1682 }
1683 }
1684
1685 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1686 {
1687 const char *aclname = qdict_get_str(qdict, "aclname");
1688 const char *match = qdict_get_str(qdict, "match");
1689 qemu_acl *acl = find_acl(mon, aclname);
1690 int ret;
1691
1692 if (acl) {
1693 ret = qemu_acl_remove(acl, match);
1694 if (ret < 0)
1695 monitor_printf(mon, "acl: no matching acl entry\n");
1696 else
1697 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1698 }
1699 }
1700
1701 void qmp_getfd(const char *fdname, Error **errp)
1702 {
1703 mon_fd_t *monfd;
1704 int fd;
1705
1706 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
1707 if (fd == -1) {
1708 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1709 return;
1710 }
1711
1712 if (qemu_isdigit(fdname[0])) {
1713 close(fd);
1714 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1715 "a name not starting with a digit");
1716 return;
1717 }
1718
1719 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1720 if (strcmp(monfd->name, fdname) != 0) {
1721 continue;
1722 }
1723
1724 close(monfd->fd);
1725 monfd->fd = fd;
1726 return;
1727 }
1728
1729 monfd = g_malloc0(sizeof(mon_fd_t));
1730 monfd->name = g_strdup(fdname);
1731 monfd->fd = fd;
1732
1733 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1734 }
1735
1736 void qmp_closefd(const char *fdname, Error **errp)
1737 {
1738 mon_fd_t *monfd;
1739
1740 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1741 if (strcmp(monfd->name, fdname) != 0) {
1742 continue;
1743 }
1744
1745 QLIST_REMOVE(monfd, next);
1746 close(monfd->fd);
1747 g_free(monfd->name);
1748 g_free(monfd);
1749 return;
1750 }
1751
1752 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1753 }
1754
1755 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
1756 {
1757 int saved_vm_running = runstate_is_running();
1758 const char *name = qdict_get_str(qdict, "name");
1759
1760 vm_stop(RUN_STATE_RESTORE_VM);
1761
1762 if (load_vmstate(name) == 0 && saved_vm_running) {
1763 vm_start();
1764 }
1765 }
1766
1767 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1768 {
1769 mon_fd_t *monfd;
1770
1771 QLIST_FOREACH(monfd, &mon->fds, next) {
1772 int fd;
1773
1774 if (strcmp(monfd->name, fdname) != 0) {
1775 continue;
1776 }
1777
1778 fd = monfd->fd;
1779
1780 /* caller takes ownership of fd */
1781 QLIST_REMOVE(monfd, next);
1782 g_free(monfd->name);
1783 g_free(monfd);
1784
1785 return fd;
1786 }
1787
1788 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1789 return -1;
1790 }
1791
1792 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1793 {
1794 MonFdsetFd *mon_fdset_fd;
1795 MonFdsetFd *mon_fdset_fd_next;
1796
1797 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1798 if ((mon_fdset_fd->removed ||
1799 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1800 runstate_is_running()) {
1801 close(mon_fdset_fd->fd);
1802 g_free(mon_fdset_fd->opaque);
1803 QLIST_REMOVE(mon_fdset_fd, next);
1804 g_free(mon_fdset_fd);
1805 }
1806 }
1807
1808 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1809 QLIST_REMOVE(mon_fdset, next);
1810 g_free(mon_fdset);
1811 }
1812 }
1813
1814 static void monitor_fdsets_cleanup(void)
1815 {
1816 MonFdset *mon_fdset;
1817 MonFdset *mon_fdset_next;
1818
1819 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1820 monitor_fdset_cleanup(mon_fdset);
1821 }
1822 }
1823
1824 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1825 const char *opaque, Error **errp)
1826 {
1827 int fd;
1828 Monitor *mon = cur_mon;
1829 AddfdInfo *fdinfo;
1830
1831 fd = qemu_chr_fe_get_msgfd(mon->chr);
1832 if (fd == -1) {
1833 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1834 goto error;
1835 }
1836
1837 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1838 has_opaque, opaque, errp);
1839 if (fdinfo) {
1840 return fdinfo;
1841 }
1842
1843 error:
1844 if (fd != -1) {
1845 close(fd);
1846 }
1847 return NULL;
1848 }
1849
1850 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1851 {
1852 MonFdset *mon_fdset;
1853 MonFdsetFd *mon_fdset_fd;
1854 char fd_str[60];
1855
1856 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1857 if (mon_fdset->id != fdset_id) {
1858 continue;
1859 }
1860 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1861 if (has_fd) {
1862 if (mon_fdset_fd->fd != fd) {
1863 continue;
1864 }
1865 mon_fdset_fd->removed = true;
1866 break;
1867 } else {
1868 mon_fdset_fd->removed = true;
1869 }
1870 }
1871 if (has_fd && !mon_fdset_fd) {
1872 goto error;
1873 }
1874 monitor_fdset_cleanup(mon_fdset);
1875 return;
1876 }
1877
1878 error:
1879 if (has_fd) {
1880 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1881 fdset_id, fd);
1882 } else {
1883 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1884 }
1885 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1886 }
1887
1888 FdsetInfoList *qmp_query_fdsets(Error **errp)
1889 {
1890 MonFdset *mon_fdset;
1891 MonFdsetFd *mon_fdset_fd;
1892 FdsetInfoList *fdset_list = NULL;
1893
1894 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1895 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1896 FdsetFdInfoList *fdsetfd_list = NULL;
1897
1898 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1899 fdset_info->value->fdset_id = mon_fdset->id;
1900
1901 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1902 FdsetFdInfoList *fdsetfd_info;
1903
1904 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1905 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1906 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1907 if (mon_fdset_fd->opaque) {
1908 fdsetfd_info->value->has_opaque = true;
1909 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1910 } else {
1911 fdsetfd_info->value->has_opaque = false;
1912 }
1913
1914 fdsetfd_info->next = fdsetfd_list;
1915 fdsetfd_list = fdsetfd_info;
1916 }
1917
1918 fdset_info->value->fds = fdsetfd_list;
1919
1920 fdset_info->next = fdset_list;
1921 fdset_list = fdset_info;
1922 }
1923
1924 return fdset_list;
1925 }
1926
1927 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1928 bool has_opaque, const char *opaque,
1929 Error **errp)
1930 {
1931 MonFdset *mon_fdset = NULL;
1932 MonFdsetFd *mon_fdset_fd;
1933 AddfdInfo *fdinfo;
1934
1935 if (has_fdset_id) {
1936 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1937 /* Break if match found or match impossible due to ordering by ID */
1938 if (fdset_id <= mon_fdset->id) {
1939 if (fdset_id < mon_fdset->id) {
1940 mon_fdset = NULL;
1941 }
1942 break;
1943 }
1944 }
1945 }
1946
1947 if (mon_fdset == NULL) {
1948 int64_t fdset_id_prev = -1;
1949 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1950
1951 if (has_fdset_id) {
1952 if (fdset_id < 0) {
1953 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1954 "a non-negative value");
1955 return NULL;
1956 }
1957 /* Use specified fdset ID */
1958 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1959 mon_fdset_cur = mon_fdset;
1960 if (fdset_id < mon_fdset_cur->id) {
1961 break;
1962 }
1963 }
1964 } else {
1965 /* Use first available fdset ID */
1966 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1967 mon_fdset_cur = mon_fdset;
1968 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1969 fdset_id_prev = mon_fdset_cur->id;
1970 continue;
1971 }
1972 break;
1973 }
1974 }
1975
1976 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1977 if (has_fdset_id) {
1978 mon_fdset->id = fdset_id;
1979 } else {
1980 mon_fdset->id = fdset_id_prev + 1;
1981 }
1982
1983 /* The fdset list is ordered by fdset ID */
1984 if (!mon_fdset_cur) {
1985 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1986 } else if (mon_fdset->id < mon_fdset_cur->id) {
1987 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1988 } else {
1989 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1990 }
1991 }
1992
1993 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1994 mon_fdset_fd->fd = fd;
1995 mon_fdset_fd->removed = false;
1996 if (has_opaque) {
1997 mon_fdset_fd->opaque = g_strdup(opaque);
1998 }
1999 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2000
2001 fdinfo = g_malloc0(sizeof(*fdinfo));
2002 fdinfo->fdset_id = mon_fdset->id;
2003 fdinfo->fd = mon_fdset_fd->fd;
2004
2005 return fdinfo;
2006 }
2007
2008 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2009 {
2010 #ifndef _WIN32
2011 MonFdset *mon_fdset;
2012 MonFdsetFd *mon_fdset_fd;
2013 int mon_fd_flags;
2014
2015 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2016 if (mon_fdset->id != fdset_id) {
2017 continue;
2018 }
2019 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2020 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2021 if (mon_fd_flags == -1) {
2022 return -1;
2023 }
2024
2025 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2026 return mon_fdset_fd->fd;
2027 }
2028 }
2029 errno = EACCES;
2030 return -1;
2031 }
2032 #endif
2033
2034 errno = ENOENT;
2035 return -1;
2036 }
2037
2038 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2039 {
2040 MonFdset *mon_fdset;
2041 MonFdsetFd *mon_fdset_fd_dup;
2042
2043 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2044 if (mon_fdset->id != fdset_id) {
2045 continue;
2046 }
2047 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2048 if (mon_fdset_fd_dup->fd == dup_fd) {
2049 return -1;
2050 }
2051 }
2052 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2053 mon_fdset_fd_dup->fd = dup_fd;
2054 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2055 return 0;
2056 }
2057 return -1;
2058 }
2059
2060 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2061 {
2062 MonFdset *mon_fdset;
2063 MonFdsetFd *mon_fdset_fd_dup;
2064
2065 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2066 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2067 if (mon_fdset_fd_dup->fd == dup_fd) {
2068 if (remove) {
2069 QLIST_REMOVE(mon_fdset_fd_dup, next);
2070 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2071 monitor_fdset_cleanup(mon_fdset);
2072 }
2073 return -1;
2074 } else {
2075 return mon_fdset->id;
2076 }
2077 }
2078 }
2079 }
2080 return -1;
2081 }
2082
2083 int monitor_fdset_dup_fd_find(int dup_fd)
2084 {
2085 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2086 }
2087
2088 void monitor_fdset_dup_fd_remove(int dup_fd)
2089 {
2090 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2091 }
2092
2093 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2094 {
2095 int fd;
2096 Error *local_err = NULL;
2097
2098 if (!qemu_isdigit(fdname[0]) && mon) {
2099 fd = monitor_get_fd(mon, fdname, &local_err);
2100 } else {
2101 fd = qemu_parse_fd(fdname);
2102 if (fd == -1) {
2103 error_setg(&local_err, "Invalid file descriptor number '%s'",
2104 fdname);
2105 }
2106 }
2107 if (local_err) {
2108 error_propagate(errp, local_err);
2109 assert(fd == -1);
2110 } else {
2111 assert(fd != -1);
2112 }
2113
2114 return fd;
2115 }
2116
2117 /* Please update hmp-commands.hx when adding or changing commands */
2118 static mon_cmd_t info_cmds[] = {
2119 #include "hmp-commands-info.h"
2120 { NULL, NULL, },
2121 };
2122
2123 /* mon_cmds and info_cmds would be sorted at runtime */
2124 static mon_cmd_t mon_cmds[] = {
2125 #include "hmp-commands.h"
2126 { NULL, NULL, },
2127 };
2128
2129 static const mon_cmd_t qmp_cmds[] = {
2130 #include "qmp-commands-old.h"
2131 { /* NULL */ },
2132 };
2133
2134 /*******************************************************************/
2135
2136 static const char *pch;
2137 static sigjmp_buf expr_env;
2138
2139
2140 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2141 expr_error(Monitor *mon, const char *fmt, ...)
2142 {
2143 va_list ap;
2144 va_start(ap, fmt);
2145 monitor_vprintf(mon, fmt, ap);
2146 monitor_printf(mon, "\n");
2147 va_end(ap);
2148 siglongjmp(expr_env, 1);
2149 }
2150
2151 /* return 0 if OK, -1 if not found */
2152 static int get_monitor_def(target_long *pval, const char *name)
2153 {
2154 const MonitorDef *md = target_monitor_defs();
2155 void *ptr;
2156 uint64_t tmp = 0;
2157 int ret;
2158
2159 if (md == NULL) {
2160 return -1;
2161 }
2162
2163 for(; md->name != NULL; md++) {
2164 if (compare_cmd(name, md->name)) {
2165 if (md->get_value) {
2166 *pval = md->get_value(md, md->offset);
2167 } else {
2168 CPUArchState *env = mon_get_cpu_env();
2169 ptr = (uint8_t *)env + md->offset;
2170 switch(md->type) {
2171 case MD_I32:
2172 *pval = *(int32_t *)ptr;
2173 break;
2174 case MD_TLONG:
2175 *pval = *(target_long *)ptr;
2176 break;
2177 default:
2178 *pval = 0;
2179 break;
2180 }
2181 }
2182 return 0;
2183 }
2184 }
2185
2186 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2187 if (!ret) {
2188 *pval = (target_long) tmp;
2189 }
2190
2191 return ret;
2192 }
2193
2194 static void next(void)
2195 {
2196 if (*pch != '\0') {
2197 pch++;
2198 while (qemu_isspace(*pch))
2199 pch++;
2200 }
2201 }
2202
2203 static int64_t expr_sum(Monitor *mon);
2204
2205 static int64_t expr_unary(Monitor *mon)
2206 {
2207 int64_t n;
2208 char *p;
2209 int ret;
2210
2211 switch(*pch) {
2212 case '+':
2213 next();
2214 n = expr_unary(mon);
2215 break;
2216 case '-':
2217 next();
2218 n = -expr_unary(mon);
2219 break;
2220 case '~':
2221 next();
2222 n = ~expr_unary(mon);
2223 break;
2224 case '(':
2225 next();
2226 n = expr_sum(mon);
2227 if (*pch != ')') {
2228 expr_error(mon, "')' expected");
2229 }
2230 next();
2231 break;
2232 case '\'':
2233 pch++;
2234 if (*pch == '\0')
2235 expr_error(mon, "character constant expected");
2236 n = *pch;
2237 pch++;
2238 if (*pch != '\'')
2239 expr_error(mon, "missing terminating \' character");
2240 next();
2241 break;
2242 case '$':
2243 {
2244 char buf[128], *q;
2245 target_long reg=0;
2246
2247 pch++;
2248 q = buf;
2249 while ((*pch >= 'a' && *pch <= 'z') ||
2250 (*pch >= 'A' && *pch <= 'Z') ||
2251 (*pch >= '0' && *pch <= '9') ||
2252 *pch == '_' || *pch == '.') {
2253 if ((q - buf) < sizeof(buf) - 1)
2254 *q++ = *pch;
2255 pch++;
2256 }
2257 while (qemu_isspace(*pch))
2258 pch++;
2259 *q = 0;
2260 ret = get_monitor_def(&reg, buf);
2261 if (ret < 0)
2262 expr_error(mon, "unknown register");
2263 n = reg;
2264 }
2265 break;
2266 case '\0':
2267 expr_error(mon, "unexpected end of expression");
2268 n = 0;
2269 break;
2270 default:
2271 errno = 0;
2272 n = strtoull(pch, &p, 0);
2273 if (errno == ERANGE) {
2274 expr_error(mon, "number too large");
2275 }
2276 if (pch == p) {
2277 expr_error(mon, "invalid char '%c' in expression", *p);
2278 }
2279 pch = p;
2280 while (qemu_isspace(*pch))
2281 pch++;
2282 break;
2283 }
2284 return n;
2285 }
2286
2287
2288 static int64_t expr_prod(Monitor *mon)
2289 {
2290 int64_t val, val2;
2291 int op;
2292
2293 val = expr_unary(mon);
2294 for(;;) {
2295 op = *pch;
2296 if (op != '*' && op != '/' && op != '%')
2297 break;
2298 next();
2299 val2 = expr_unary(mon);
2300 switch(op) {
2301 default:
2302 case '*':
2303 val *= val2;
2304 break;
2305 case '/':
2306 case '%':
2307 if (val2 == 0)
2308 expr_error(mon, "division by zero");
2309 if (op == '/')
2310 val /= val2;
2311 else
2312 val %= val2;
2313 break;
2314 }
2315 }
2316 return val;
2317 }
2318
2319 static int64_t expr_logic(Monitor *mon)
2320 {
2321 int64_t val, val2;
2322 int op;
2323
2324 val = expr_prod(mon);
2325 for(;;) {
2326 op = *pch;
2327 if (op != '&' && op != '|' && op != '^')
2328 break;
2329 next();
2330 val2 = expr_prod(mon);
2331 switch(op) {
2332 default:
2333 case '&':
2334 val &= val2;
2335 break;
2336 case '|':
2337 val |= val2;
2338 break;
2339 case '^':
2340 val ^= val2;
2341 break;
2342 }
2343 }
2344 return val;
2345 }
2346
2347 static int64_t expr_sum(Monitor *mon)
2348 {
2349 int64_t val, val2;
2350 int op;
2351
2352 val = expr_logic(mon);
2353 for(;;) {
2354 op = *pch;
2355 if (op != '+' && op != '-')
2356 break;
2357 next();
2358 val2 = expr_logic(mon);
2359 if (op == '+')
2360 val += val2;
2361 else
2362 val -= val2;
2363 }
2364 return val;
2365 }
2366
2367 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2368 {
2369 pch = *pp;
2370 if (sigsetjmp(expr_env, 0)) {
2371 *pp = pch;
2372 return -1;
2373 }
2374 while (qemu_isspace(*pch))
2375 pch++;
2376 *pval = expr_sum(mon);
2377 *pp = pch;
2378 return 0;
2379 }
2380
2381 static int get_double(Monitor *mon, double *pval, const char **pp)
2382 {
2383 const char *p = *pp;
2384 char *tailp;
2385 double d;
2386
2387 d = strtod(p, &tailp);
2388 if (tailp == p) {
2389 monitor_printf(mon, "Number expected\n");
2390 return -1;
2391 }
2392 if (d != d || d - d != 0) {
2393 /* NaN or infinity */
2394 monitor_printf(mon, "Bad number\n");
2395 return -1;
2396 }
2397 *pval = d;
2398 *pp = tailp;
2399 return 0;
2400 }
2401
2402 /*
2403 * Store the command-name in cmdname, and return a pointer to
2404 * the remaining of the command string.
2405 */
2406 static const char *get_command_name(const char *cmdline,
2407 char *cmdname, size_t nlen)
2408 {
2409 size_t len;
2410 const char *p, *pstart;
2411
2412 p = cmdline;
2413 while (qemu_isspace(*p))
2414 p++;
2415 if (*p == '\0')
2416 return NULL;
2417 pstart = p;
2418 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2419 p++;
2420 len = p - pstart;
2421 if (len > nlen - 1)
2422 len = nlen - 1;
2423 memcpy(cmdname, pstart, len);
2424 cmdname[len] = '\0';
2425 return p;
2426 }
2427
2428 /**
2429 * Read key of 'type' into 'key' and return the current
2430 * 'type' pointer.
2431 */
2432 static char *key_get_info(const char *type, char **key)
2433 {
2434 size_t len;
2435 char *p, *str;
2436
2437 if (*type == ',')
2438 type++;
2439
2440 p = strchr(type, ':');
2441 if (!p) {
2442 *key = NULL;
2443 return NULL;
2444 }
2445 len = p - type;
2446
2447 str = g_malloc(len + 1);
2448 memcpy(str, type, len);
2449 str[len] = '\0';
2450
2451 *key = str;
2452 return ++p;
2453 }
2454
2455 static int default_fmt_format = 'x';
2456 static int default_fmt_size = 4;
2457
2458 static int is_valid_option(const char *c, const char *typestr)
2459 {
2460 char option[3];
2461
2462 option[0] = '-';
2463 option[1] = *c;
2464 option[2] = '\0';
2465
2466 typestr = strstr(typestr, option);
2467 return (typestr != NULL);
2468 }
2469
2470 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2471 const char *cmdname)
2472 {
2473 const mon_cmd_t *cmd;
2474
2475 for (cmd = disp_table; cmd->name != NULL; cmd++) {
2476 if (compare_cmd(cmdname, cmd->name)) {
2477 return cmd;
2478 }
2479 }
2480
2481 return NULL;
2482 }
2483
2484 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2485 {
2486 return search_dispatch_table(qmp_cmds, cmdname);
2487 }
2488
2489 /*
2490 * Parse command name from @cmdp according to command table @table.
2491 * If blank, return NULL.
2492 * Else, if no valid command can be found, report to @mon, and return
2493 * NULL.
2494 * Else, change @cmdp to point right behind the name, and return its
2495 * command table entry.
2496 * Do not assume the return value points into @table! It doesn't when
2497 * the command is found in a sub-command table.
2498 */
2499 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2500 const char **cmdp,
2501 mon_cmd_t *table)
2502 {
2503 const char *p;
2504 const mon_cmd_t *cmd;
2505 char cmdname[256];
2506
2507 /* extract the command name */
2508 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
2509 if (!p)
2510 return NULL;
2511
2512 cmd = search_dispatch_table(table, cmdname);
2513 if (!cmd) {
2514 monitor_printf(mon, "unknown command: '%.*s'\n",
2515 (int)(p - *cmdp), *cmdp);
2516 return NULL;
2517 }
2518
2519 /* filter out following useless space */
2520 while (qemu_isspace(*p)) {
2521 p++;
2522 }
2523
2524 *cmdp = p;
2525 /* search sub command */
2526 if (cmd->sub_table != NULL && *p != '\0') {
2527 return monitor_parse_command(mon, cmdp, cmd->sub_table);
2528 }
2529
2530 return cmd;
2531 }
2532
2533 /*
2534 * Parse arguments for @cmd.
2535 * If it can't be parsed, report to @mon, and return NULL.
2536 * Else, insert command arguments into a QDict, and return it.
2537 * Note: On success, caller has to free the QDict structure.
2538 */
2539
2540 static QDict *monitor_parse_arguments(Monitor *mon,
2541 const char **endp,
2542 const mon_cmd_t *cmd)
2543 {
2544 const char *typestr;
2545 char *key;
2546 int c;
2547 const char *p = *endp;
2548 char buf[1024];
2549 QDict *qdict = qdict_new();
2550
2551 /* parse the parameters */
2552 typestr = cmd->args_type;
2553 for(;;) {
2554 typestr = key_get_info(typestr, &key);
2555 if (!typestr)
2556 break;
2557 c = *typestr;
2558 typestr++;
2559 switch(c) {
2560 case 'F':
2561 case 'B':
2562 case 's':
2563 {
2564 int ret;
2565
2566 while (qemu_isspace(*p))
2567 p++;
2568 if (*typestr == '?') {
2569 typestr++;
2570 if (*p == '\0') {
2571 /* no optional string: NULL argument */
2572 break;
2573 }
2574 }
2575 ret = get_str(buf, sizeof(buf), &p);
2576 if (ret < 0) {
2577 switch(c) {
2578 case 'F':
2579 monitor_printf(mon, "%s: filename expected\n",
2580 cmd->name);
2581 break;
2582 case 'B':
2583 monitor_printf(mon, "%s: block device name expected\n",
2584 cmd->name);
2585 break;
2586 default:
2587 monitor_printf(mon, "%s: string expected\n", cmd->name);
2588 break;
2589 }
2590 goto fail;
2591 }
2592 qdict_put(qdict, key, qstring_from_str(buf));
2593 }
2594 break;
2595 case 'O':
2596 {
2597 QemuOptsList *opts_list;
2598 QemuOpts *opts;
2599
2600 opts_list = qemu_find_opts(key);
2601 if (!opts_list || opts_list->desc->name) {
2602 goto bad_type;
2603 }
2604 while (qemu_isspace(*p)) {
2605 p++;
2606 }
2607 if (!*p)
2608 break;
2609 if (get_str(buf, sizeof(buf), &p) < 0) {
2610 goto fail;
2611 }
2612 opts = qemu_opts_parse_noisily(opts_list, buf, true);
2613 if (!opts) {
2614 goto fail;
2615 }
2616 qemu_opts_to_qdict(opts, qdict);
2617 qemu_opts_del(opts);
2618 }
2619 break;
2620 case '/':
2621 {
2622 int count, format, size;
2623
2624 while (qemu_isspace(*p))
2625 p++;
2626 if (*p == '/') {
2627 /* format found */
2628 p++;
2629 count = 1;
2630 if (qemu_isdigit(*p)) {
2631 count = 0;
2632 while (qemu_isdigit(*p)) {
2633 count = count * 10 + (*p - '0');
2634 p++;
2635 }
2636 }
2637 size = -1;
2638 format = -1;
2639 for(;;) {
2640 switch(*p) {
2641 case 'o':
2642 case 'd':
2643 case 'u':
2644 case 'x':
2645 case 'i':
2646 case 'c':
2647 format = *p++;
2648 break;
2649 case 'b':
2650 size = 1;
2651 p++;
2652 break;
2653 case 'h':
2654 size = 2;
2655 p++;
2656 break;
2657 case 'w':
2658 size = 4;
2659 p++;
2660 break;
2661 case 'g':
2662 case 'L':
2663 size = 8;
2664 p++;
2665 break;
2666 default:
2667 goto next;
2668 }
2669 }
2670 next:
2671 if (*p != '\0' && !qemu_isspace(*p)) {
2672 monitor_printf(mon, "invalid char in format: '%c'\n",
2673 *p);
2674 goto fail;
2675 }
2676 if (format < 0)
2677 format = default_fmt_format;
2678 if (format != 'i') {
2679 /* for 'i', not specifying a size gives -1 as size */
2680 if (size < 0)
2681 size = default_fmt_size;
2682 default_fmt_size = size;
2683 }
2684 default_fmt_format = format;
2685 } else {
2686 count = 1;
2687 format = default_fmt_format;
2688 if (format != 'i') {
2689 size = default_fmt_size;
2690 } else {
2691 size = -1;
2692 }
2693 }
2694 qdict_put(qdict, "count", qint_from_int(count));
2695 qdict_put(qdict, "format", qint_from_int(format));
2696 qdict_put(qdict, "size", qint_from_int(size));
2697 }
2698 break;
2699 case 'i':
2700 case 'l':
2701 case 'M':
2702 {
2703 int64_t val;
2704
2705 while (qemu_isspace(*p))
2706 p++;
2707 if (*typestr == '?' || *typestr == '.') {
2708 if (*typestr == '?') {
2709 if (*p == '\0') {
2710 typestr++;
2711 break;
2712 }
2713 } else {
2714 if (*p == '.') {
2715 p++;
2716 while (qemu_isspace(*p))
2717 p++;
2718 } else {
2719 typestr++;
2720 break;
2721 }
2722 }
2723 typestr++;
2724 }
2725 if (get_expr(mon, &val, &p))
2726 goto fail;
2727 /* Check if 'i' is greater than 32-bit */
2728 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2729 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
2730 monitor_printf(mon, "integer is for 32-bit values\n");
2731 goto fail;
2732 } else if (c == 'M') {
2733 if (val < 0) {
2734 monitor_printf(mon, "enter a positive value\n");
2735 goto fail;
2736 }
2737 val <<= 20;
2738 }
2739 qdict_put(qdict, key, qint_from_int(val));
2740 }
2741 break;
2742 case 'o':
2743 {
2744 int64_t val;
2745 char *end;
2746
2747 while (qemu_isspace(*p)) {
2748 p++;
2749 }
2750 if (*typestr == '?') {
2751 typestr++;
2752 if (*p == '\0') {
2753 break;
2754 }
2755 }
2756 val = qemu_strtosz(p, &end);
2757 if (val < 0) {
2758 monitor_printf(mon, "invalid size\n");
2759 goto fail;
2760 }
2761 qdict_put(qdict, key, qint_from_int(val));
2762 p = end;
2763 }
2764 break;
2765 case 'T':
2766 {
2767 double val;
2768
2769 while (qemu_isspace(*p))
2770 p++;
2771 if (*typestr == '?') {
2772 typestr++;
2773 if (*p == '\0') {
2774 break;
2775 }
2776 }
2777 if (get_double(mon, &val, &p) < 0) {
2778 goto fail;
2779 }
2780 if (p[0] && p[1] == 's') {
2781 switch (*p) {
2782 case 'm':
2783 val /= 1e3; p += 2; break;
2784 case 'u':
2785 val /= 1e6; p += 2; break;
2786 case 'n':
2787 val /= 1e9; p += 2; break;
2788 }
2789 }
2790 if (*p && !qemu_isspace(*p)) {
2791 monitor_printf(mon, "Unknown unit suffix\n");
2792 goto fail;
2793 }
2794 qdict_put(qdict, key, qfloat_from_double(val));
2795 }
2796 break;
2797 case 'b':
2798 {
2799 const char *beg;
2800 bool val;
2801
2802 while (qemu_isspace(*p)) {
2803 p++;
2804 }
2805 beg = p;
2806 while (qemu_isgraph(*p)) {
2807 p++;
2808 }
2809 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
2810 val = true;
2811 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
2812 val = false;
2813 } else {
2814 monitor_printf(mon, "Expected 'on' or 'off'\n");
2815 goto fail;
2816 }
2817 qdict_put(qdict, key, qbool_from_bool(val));
2818 }
2819 break;
2820 case '-':
2821 {
2822 const char *tmp = p;
2823 int skip_key = 0;
2824 /* option */
2825
2826 c = *typestr++;
2827 if (c == '\0')
2828 goto bad_type;
2829 while (qemu_isspace(*p))
2830 p++;
2831 if (*p == '-') {
2832 p++;
2833 if(c != *p) {
2834 if(!is_valid_option(p, typestr)) {
2835
2836 monitor_printf(mon, "%s: unsupported option -%c\n",
2837 cmd->name, *p);
2838 goto fail;
2839 } else {
2840 skip_key = 1;
2841 }
2842 }
2843 if(skip_key) {
2844 p = tmp;
2845 } else {
2846 /* has option */
2847 p++;
2848 qdict_put(qdict, key, qbool_from_bool(true));
2849 }
2850 }
2851 }
2852 break;
2853 case 'S':
2854 {
2855 /* package all remaining string */
2856 int len;
2857
2858 while (qemu_isspace(*p)) {
2859 p++;
2860 }
2861 if (*typestr == '?') {
2862 typestr++;
2863 if (*p == '\0') {
2864 /* no remaining string: NULL argument */
2865 break;
2866 }
2867 }
2868 len = strlen(p);
2869 if (len <= 0) {
2870 monitor_printf(mon, "%s: string expected\n",
2871 cmd->name);
2872 goto fail;
2873 }
2874 qdict_put(qdict, key, qstring_from_str(p));
2875 p += len;
2876 }
2877 break;
2878 default:
2879 bad_type:
2880 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
2881 goto fail;
2882 }
2883 g_free(key);
2884 key = NULL;
2885 }
2886 /* check that all arguments were parsed */
2887 while (qemu_isspace(*p))
2888 p++;
2889 if (*p != '\0') {
2890 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2891 cmd->name);
2892 goto fail;
2893 }
2894
2895 return qdict;
2896
2897 fail:
2898 QDECREF(qdict);
2899 g_free(key);
2900 return NULL;
2901 }
2902
2903 static void handle_hmp_command(Monitor *mon, const char *cmdline)
2904 {
2905 QDict *qdict;
2906 const mon_cmd_t *cmd;
2907
2908 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2909 if (!cmd) {
2910 return;
2911 }
2912
2913 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2914 if (!qdict) {
2915 monitor_printf(mon, "Try \"help %s\" for more information\n",
2916 cmd->name);
2917 return;
2918 }
2919
2920 cmd->mhandler.cmd(mon, qdict);
2921 QDECREF(qdict);
2922 }
2923
2924 static void cmd_completion(Monitor *mon, const char *name, const char *list)
2925 {
2926 const char *p, *pstart;
2927 char cmd[128];
2928 int len;
2929
2930 p = list;
2931 for(;;) {
2932 pstart = p;
2933 p = strchr(p, '|');
2934 if (!p)
2935 p = pstart + strlen(pstart);
2936 len = p - pstart;
2937 if (len > sizeof(cmd) - 2)
2938 len = sizeof(cmd) - 2;
2939 memcpy(cmd, pstart, len);
2940 cmd[len] = '\0';
2941 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2942 readline_add_completion(mon->rs, cmd);
2943 }
2944 if (*p == '\0')
2945 break;
2946 p++;
2947 }
2948 }
2949
2950 static void file_completion(Monitor *mon, const char *input)
2951 {
2952 DIR *ffs;
2953 struct dirent *d;
2954 char path[1024];
2955 char file[1024], file_prefix[1024];
2956 int input_path_len;
2957 const char *p;
2958
2959 p = strrchr(input, '/');
2960 if (!p) {
2961 input_path_len = 0;
2962 pstrcpy(file_prefix, sizeof(file_prefix), input);
2963 pstrcpy(path, sizeof(path), ".");
2964 } else {
2965 input_path_len = p - input + 1;
2966 memcpy(path, input, input_path_len);
2967 if (input_path_len > sizeof(path) - 1)
2968 input_path_len = sizeof(path) - 1;
2969 path[input_path_len] = '\0';
2970 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2971 }
2972
2973 ffs = opendir(path);
2974 if (!ffs)
2975 return;
2976 for(;;) {
2977 struct stat sb;
2978 d = readdir(ffs);
2979 if (!d)
2980 break;
2981
2982 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2983 continue;
2984 }
2985
2986 if (strstart(d->d_name, file_prefix, NULL)) {
2987 memcpy(file, input, input_path_len);
2988 if (input_path_len < sizeof(file))
2989 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2990 d->d_name);
2991 /* stat the file to find out if it's a directory.
2992 * In that case add a slash to speed up typing long paths
2993 */
2994 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
2995 pstrcat(file, sizeof(file), "/");
2996 }
2997 readline_add_completion(mon->rs, file);
2998 }
2999 }
3000 closedir(ffs);
3001 }
3002
3003 static const char *next_arg_type(const char *typestr)
3004 {
3005 const char *p = strchr(typestr, ':');
3006 return (p != NULL ? ++p : typestr);
3007 }
3008
3009 static void add_completion_option(ReadLineState *rs, const char *str,
3010 const char *option)
3011 {
3012 if (!str || !option) {
3013 return;
3014 }
3015 if (!strncmp(option, str, strlen(str))) {
3016 readline_add_completion(rs, option);
3017 }
3018 }
3019
3020 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3021 {
3022 size_t len;
3023 ChardevBackendInfoList *list, *start;
3024
3025 if (nb_args != 2) {
3026 return;
3027 }
3028 len = strlen(str);
3029 readline_set_completion_index(rs, len);
3030
3031 start = list = qmp_query_chardev_backends(NULL);
3032 while (list) {
3033 const char *chr_name = list->value->name;
3034
3035 if (!strncmp(chr_name, str, len)) {
3036 readline_add_completion(rs, chr_name);
3037 }
3038 list = list->next;
3039 }
3040 qapi_free_ChardevBackendInfoList(start);
3041 }
3042
3043 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3044 {
3045 size_t len;
3046 int i;
3047
3048 if (nb_args != 2) {
3049 return;
3050 }
3051 len = strlen(str);
3052 readline_set_completion_index(rs, len);
3053 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3054 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3055 }
3056 }
3057
3058 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
3059 {
3060 GSList *list, *elt;
3061 size_t len;
3062
3063 if (nb_args != 2) {
3064 return;
3065 }
3066
3067 len = strlen(str);
3068 readline_set_completion_index(rs, len);
3069 list = elt = object_class_get_list(TYPE_DEVICE, false);
3070 while (elt) {
3071 const char *name;
3072 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3073 TYPE_DEVICE);
3074 name = object_class_get_name(OBJECT_CLASS(dc));
3075
3076 if (!dc->cannot_instantiate_with_device_add_yet
3077 && !strncmp(name, str, len)) {
3078 readline_add_completion(rs, name);
3079 }
3080 elt = elt->next;
3081 }
3082 g_slist_free(list);
3083 }
3084
3085 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
3086 {
3087 GSList *list, *elt;
3088 size_t len;
3089
3090 if (nb_args != 2) {
3091 return;
3092 }
3093
3094 len = strlen(str);
3095 readline_set_completion_index(rs, len);
3096 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3097 while (elt) {
3098 const char *name;
3099
3100 name = object_class_get_name(OBJECT_CLASS(elt->data));
3101 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3102 readline_add_completion(rs, name);
3103 }
3104 elt = elt->next;
3105 }
3106 g_slist_free(list);
3107 }
3108
3109 static void peripheral_device_del_completion(ReadLineState *rs,
3110 const char *str, size_t len)
3111 {
3112 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3113 GSList *list, *item;
3114
3115 list = qdev_build_hotpluggable_device_list(peripheral);
3116 if (!list) {
3117 return;
3118 }
3119
3120 for (item = list; item; item = g_slist_next(item)) {
3121 DeviceState *dev = item->data;
3122
3123 if (dev->id && !strncmp(str, dev->id, len)) {
3124 readline_add_completion(rs, dev->id);
3125 }
3126 }
3127
3128 g_slist_free(list);
3129 }
3130
3131 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3132 {
3133 size_t len;
3134 ChardevInfoList *list, *start;
3135
3136 if (nb_args != 2) {
3137 return;
3138 }
3139 len = strlen(str);
3140 readline_set_completion_index(rs, len);
3141
3142 start = list = qmp_query_chardev(NULL);
3143 while (list) {
3144 ChardevInfo *chr = list->value;
3145
3146 if (!strncmp(chr->label, str, len)) {
3147 readline_add_completion(rs, chr->label);
3148 }
3149 list = list->next;
3150 }
3151 qapi_free_ChardevInfoList(start);
3152 }
3153
3154 static void ringbuf_completion(ReadLineState *rs, const char *str)
3155 {
3156 size_t len;
3157 ChardevInfoList *list, *start;
3158
3159 len = strlen(str);
3160 readline_set_completion_index(rs, len);
3161
3162 start = list = qmp_query_chardev(NULL);
3163 while (list) {
3164 ChardevInfo *chr_info = list->value;
3165
3166 if (!strncmp(chr_info->label, str, len)) {
3167 CharDriverState *chr = qemu_chr_find(chr_info->label);
3168 if (chr && chr_is_ringbuf(chr)) {
3169 readline_add_completion(rs, chr_info->label);
3170 }
3171 }
3172 list = list->next;
3173 }
3174 qapi_free_ChardevInfoList(start);
3175 }
3176
3177 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3178 {
3179 if (nb_args != 2) {
3180 return;
3181 }
3182 ringbuf_completion(rs, str);
3183 }
3184
3185 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3186 {
3187 size_t len;
3188
3189 if (nb_args != 2) {
3190 return;
3191 }
3192
3193 len = strlen(str);
3194 readline_set_completion_index(rs, len);
3195 peripheral_device_del_completion(rs, str, len);
3196 }
3197
3198 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
3199 {
3200 ObjectPropertyInfoList *list, *start;
3201 size_t len;
3202
3203 if (nb_args != 2) {
3204 return;
3205 }
3206 len = strlen(str);
3207 readline_set_completion_index(rs, len);
3208
3209 start = list = qmp_qom_list("/objects", NULL);
3210 while (list) {
3211 ObjectPropertyInfo *info = list->value;
3212
3213 if (!strncmp(info->type, "child<", 5)
3214 && !strncmp(info->name, str, len)) {
3215 readline_add_completion(rs, info->name);
3216 }
3217 list = list->next;
3218 }
3219 qapi_free_ObjectPropertyInfoList(start);
3220 }
3221
3222 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3223 {
3224 int i;
3225 char *sep;
3226 size_t len;
3227
3228 if (nb_args != 2) {
3229 return;
3230 }
3231 sep = strrchr(str, '-');
3232 if (sep) {
3233 str = sep + 1;
3234 }
3235 len = strlen(str);
3236 readline_set_completion_index(rs, len);
3237 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
3238 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3239 readline_add_completion(rs, QKeyCode_lookup[i]);
3240 }
3241 }
3242 }
3243
3244 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3245 {
3246 size_t len;
3247
3248 len = strlen(str);
3249 readline_set_completion_index(rs, len);
3250 if (nb_args == 2) {
3251 NetClientState *ncs[MAX_QUEUE_NUM];
3252 int count, i;
3253 count = qemu_find_net_clients_except(NULL, ncs,
3254 NET_CLIENT_OPTIONS_KIND_NONE,
3255 MAX_QUEUE_NUM);
3256 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3257 const char *name = ncs[i]->name;
3258 if (!strncmp(str, name, len)) {
3259 readline_add_completion(rs, name);
3260 }
3261 }
3262 } else if (nb_args == 3) {
3263 add_completion_option(rs, str, "on");
3264 add_completion_option(rs, str, "off");
3265 }
3266 }
3267
3268 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3269 {
3270 int len, count, i;
3271 NetClientState *ncs[MAX_QUEUE_NUM];
3272
3273 if (nb_args != 2) {
3274 return;
3275 }
3276
3277 len = strlen(str);
3278 readline_set_completion_index(rs, len);
3279 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
3280 MAX_QUEUE_NUM);
3281 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3282 QemuOpts *opts;
3283 const char *name = ncs[i]->name;
3284 if (strncmp(str, name, len)) {
3285 continue;
3286 }
3287 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3288 if (opts) {
3289 readline_add_completion(rs, name);
3290 }
3291 }
3292 }
3293
3294 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3295 {
3296 size_t len;
3297
3298 len = strlen(str);
3299 readline_set_completion_index(rs, len);
3300 if (nb_args == 2) {
3301 TraceEventID id;
3302 for (id = 0; id < trace_event_count(); id++) {
3303 const char *event_name = trace_event_get_name(trace_event_id(id));
3304 if (!strncmp(str, event_name, len)) {
3305 readline_add_completion(rs, event_name);
3306 }
3307 }
3308 } else if (nb_args == 3) {
3309 add_completion_option(rs, str, "on");
3310 add_completion_option(rs, str, "off");
3311 }
3312 }
3313
3314 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3315 {
3316 int i;
3317
3318 if (nb_args != 2) {
3319 return;
3320 }
3321 readline_set_completion_index(rs, strlen(str));
3322 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3323 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3324 }
3325 }
3326
3327 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3328 const char *str)
3329 {
3330 size_t len;
3331
3332 len = strlen(str);
3333 readline_set_completion_index(rs, len);
3334 if (nb_args == 2) {
3335 int i;
3336 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3337 const char *name = MigrationCapability_lookup[i];
3338 if (!strncmp(str, name, len)) {
3339 readline_add_completion(rs, name);
3340 }
3341 }
3342 } else if (nb_args == 3) {
3343 add_completion_option(rs, str, "on");
3344 add_completion_option(rs, str, "off");
3345 }
3346 }
3347
3348 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3349 const char *str)
3350 {
3351 size_t len;
3352
3353 len = strlen(str);
3354 readline_set_completion_index(rs, len);
3355 if (nb_args == 2) {
3356 int i;
3357 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
3358 const char *name = MigrationParameter_lookup[i];
3359 if (!strncmp(str, name, len)) {
3360 readline_add_completion(rs, name);
3361 }
3362 }
3363 }
3364 }
3365
3366 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3367 {
3368 int i;
3369 size_t len;
3370 if (nb_args != 2) {
3371 return;
3372 }
3373 len = strlen(str);
3374 readline_set_completion_index(rs, len);
3375 for (i = 0; host_net_devices[i]; i++) {
3376 if (!strncmp(host_net_devices[i], str, len)) {
3377 readline_add_completion(rs, host_net_devices[i]);
3378 }
3379 }
3380 }
3381
3382 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3383 {
3384 NetClientState *ncs[MAX_QUEUE_NUM];
3385 int count, i, len;
3386
3387 len = strlen(str);
3388 readline_set_completion_index(rs, len);
3389 if (nb_args == 2) {
3390 count = qemu_find_net_clients_except(NULL, ncs,
3391 NET_CLIENT_OPTIONS_KIND_NONE,
3392 MAX_QUEUE_NUM);
3393 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3394 int id;
3395 char name[16];
3396
3397 if (net_hub_id_for_client(ncs[i], &id)) {
3398 continue;
3399 }
3400 snprintf(name, sizeof(name), "%d", id);
3401 if (!strncmp(str, name, len)) {
3402 readline_add_completion(rs, name);
3403 }
3404 }
3405 return;
3406 } else if (nb_args == 3) {
3407 count = qemu_find_net_clients_except(NULL, ncs,
3408 NET_CLIENT_OPTIONS_KIND_NIC,
3409 MAX_QUEUE_NUM);
3410 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3411 int id;
3412 const char *name;
3413
3414 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3415 net_hub_id_for_client(ncs[i], &id)) {
3416 continue;
3417 }
3418 name = ncs[i]->name;
3419 if (!strncmp(str, name, len)) {
3420 readline_add_completion(rs, name);
3421 }
3422 }
3423 return;
3424 }
3425 }
3426
3427 static void vm_completion(ReadLineState *rs, const char *str)
3428