virtio-net: Unify the logic to update NIC state for RSS
[qemu/ar7.git] / monitor / hmp.c
blob69c1b7e98abb9c53791b7824fddb73748358fe44
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include <dirent.h>
27 #include "hw/qdev-core.h"
28 #include "monitor-internal.h"
29 #include "monitor/hmp.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qnum.h"
32 #include "qemu/config-file.h"
33 #include "qemu/ctype.h"
34 #include "qemu/cutils.h"
35 #include "qemu/log.h"
36 #include "qemu/option.h"
37 #include "qemu/units.h"
38 #include "sysemu/block-backend.h"
39 #include "trace.h"
41 static void monitor_command_cb(void *opaque, const char *cmdline,
42 void *readline_opaque)
44 MonitorHMP *mon = opaque;
46 monitor_suspend(&mon->common);
47 handle_hmp_command(mon, cmdline);
48 monitor_resume(&mon->common);
51 void monitor_read_command(MonitorHMP *mon, int show_prompt)
53 if (!mon->rs) {
54 return;
57 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
58 if (show_prompt) {
59 readline_show_prompt(mon->rs);
63 int monitor_read_password(MonitorHMP *mon, ReadLineFunc *readline_func,
64 void *opaque)
66 if (mon->rs) {
67 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
68 /* prompt is printed on return from the command handler */
69 return 0;
70 } else {
71 monitor_printf(&mon->common,
72 "terminal does not support password prompting\n");
73 return -ENOTTY;
77 static int get_str(char *buf, int buf_size, const char **pp)
79 const char *p;
80 char *q;
81 int c;
83 q = buf;
84 p = *pp;
85 while (qemu_isspace(*p)) {
86 p++;
88 if (*p == '\0') {
89 fail:
90 *q = '\0';
91 *pp = p;
92 return -1;
94 if (*p == '\"') {
95 p++;
96 while (*p != '\0' && *p != '\"') {
97 if (*p == '\\') {
98 p++;
99 c = *p++;
100 switch (c) {
101 case 'n':
102 c = '\n';
103 break;
104 case 'r':
105 c = '\r';
106 break;
107 case '\\':
108 case '\'':
109 case '\"':
110 break;
111 default:
112 printf("unsupported escape code: '\\%c'\n", c);
113 goto fail;
115 if ((q - buf) < buf_size - 1) {
116 *q++ = c;
118 } else {
119 if ((q - buf) < buf_size - 1) {
120 *q++ = *p;
122 p++;
125 if (*p != '\"') {
126 printf("unterminated string\n");
127 goto fail;
129 p++;
130 } else {
131 while (*p != '\0' && !qemu_isspace(*p)) {
132 if ((q - buf) < buf_size - 1) {
133 *q++ = *p;
135 p++;
138 *q = '\0';
139 *pp = p;
140 return 0;
143 #define MAX_ARGS 16
145 static void free_cmdline_args(char **args, int nb_args)
147 int i;
149 assert(nb_args <= MAX_ARGS);
151 for (i = 0; i < nb_args; i++) {
152 g_free(args[i]);
158 * Parse the command line to get valid args.
159 * @cmdline: command line to be parsed.
160 * @pnb_args: location to store the number of args, must NOT be NULL.
161 * @args: location to store the args, which should be freed by caller, must
162 * NOT be NULL.
164 * Returns 0 on success, negative on failure.
166 * NOTE: this parser is an approximate form of the real command parser. Number
167 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
168 * return with failure.
170 static int parse_cmdline(const char *cmdline,
171 int *pnb_args, char **args)
173 const char *p;
174 int nb_args, ret;
175 char buf[1024];
177 p = cmdline;
178 nb_args = 0;
179 for (;;) {
180 while (qemu_isspace(*p)) {
181 p++;
183 if (*p == '\0') {
184 break;
186 if (nb_args >= MAX_ARGS) {
187 goto fail;
189 ret = get_str(buf, sizeof(buf), &p);
190 if (ret < 0) {
191 goto fail;
193 args[nb_args] = g_strdup(buf);
194 nb_args++;
196 *pnb_args = nb_args;
197 return 0;
199 fail:
200 free_cmdline_args(args, nb_args);
201 return -1;
205 * Can command @cmd be executed in preconfig state?
207 static bool cmd_can_preconfig(const HMPCommand *cmd)
209 if (!cmd->flags) {
210 return false;
213 return strchr(cmd->flags, 'p');
216 static bool cmd_available(const HMPCommand *cmd)
218 return phase_check(PHASE_MACHINE_READY) || cmd_can_preconfig(cmd);
221 static void help_cmd_dump_one(Monitor *mon,
222 const HMPCommand *cmd,
223 char **prefix_args,
224 int prefix_args_nb)
226 int i;
228 if (!cmd_available(cmd)) {
229 return;
232 for (i = 0; i < prefix_args_nb; i++) {
233 monitor_printf(mon, "%s ", prefix_args[i]);
235 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
238 /* @args[@arg_index] is the valid command need to find in @cmds */
239 static void help_cmd_dump(Monitor *mon, const HMPCommand *cmds,
240 char **args, int nb_args, int arg_index)
242 const HMPCommand *cmd;
243 size_t i;
245 /* No valid arg need to compare with, dump all in *cmds */
246 if (arg_index >= nb_args) {
247 for (cmd = cmds; cmd->name != NULL; cmd++) {
248 help_cmd_dump_one(mon, cmd, args, arg_index);
250 return;
253 /* Find one entry to dump */
254 for (cmd = cmds; cmd->name != NULL; cmd++) {
255 if (hmp_compare_cmd(args[arg_index], cmd->name) &&
256 cmd_available(cmd)) {
257 if (cmd->sub_table) {
258 /* continue with next arg */
259 help_cmd_dump(mon, cmd->sub_table,
260 args, nb_args, arg_index + 1);
261 } else {
262 help_cmd_dump_one(mon, cmd, args, arg_index);
264 return;
268 /* Command not found */
269 monitor_printf(mon, "unknown command: '");
270 for (i = 0; i <= arg_index; i++) {
271 monitor_printf(mon, "%s%s", args[i], i == arg_index ? "'\n" : " ");
275 void hmp_help_cmd(Monitor *mon, const char *name)
277 char *args[MAX_ARGS];
278 int nb_args = 0;
280 /* 1. parse user input */
281 if (name) {
282 /* special case for log, directly dump and return */
283 if (!strcmp(name, "log")) {
284 const QEMULogItem *item;
285 monitor_printf(mon, "Log items (comma separated):\n");
286 monitor_printf(mon, "%-15s %s\n", "none", "remove all logs");
287 for (item = qemu_log_items; item->mask != 0; item++) {
288 monitor_printf(mon, "%-15s %s\n", item->name, item->help);
290 #ifdef CONFIG_TRACE_LOG
291 monitor_printf(mon, "trace:PATTERN enable trace events\n");
292 monitor_printf(mon, "\nUse \"log trace:help\" to get a list of "
293 "trace events.\n\n");
294 #endif
295 return;
298 if (parse_cmdline(name, &nb_args, args) < 0) {
299 return;
303 /* 2. dump the contents according to parsed args */
304 help_cmd_dump(mon, hmp_cmds, args, nb_args, 0);
306 free_cmdline_args(args, nb_args);
309 /*******************************************************************/
311 static const char *pch;
312 static sigjmp_buf expr_env;
314 static G_NORETURN G_GNUC_PRINTF(2, 3)
315 void expr_error(Monitor *mon, const char *fmt, ...)
317 va_list ap;
318 va_start(ap, fmt);
319 monitor_vprintf(mon, fmt, ap);
320 monitor_printf(mon, "\n");
321 va_end(ap);
322 siglongjmp(expr_env, 1);
325 static void next(void)
327 if (*pch != '\0') {
328 pch++;
329 while (qemu_isspace(*pch)) {
330 pch++;
335 static int64_t expr_sum(Monitor *mon);
337 static int64_t expr_unary(Monitor *mon)
339 int64_t n;
340 char *p;
341 int ret;
343 switch (*pch) {
344 case '+':
345 next();
346 n = expr_unary(mon);
347 break;
348 case '-':
349 next();
350 n = -expr_unary(mon);
351 break;
352 case '~':
353 next();
354 n = ~expr_unary(mon);
355 break;
356 case '(':
357 next();
358 n = expr_sum(mon);
359 if (*pch != ')') {
360 expr_error(mon, "')' expected");
362 next();
363 break;
364 case '\'':
365 pch++;
366 if (*pch == '\0') {
367 expr_error(mon, "character constant expected");
369 n = *pch;
370 pch++;
371 if (*pch != '\'') {
372 expr_error(mon, "missing terminating \' character");
374 next();
375 break;
376 case '$':
378 char buf[128], *q;
379 int64_t reg = 0;
381 pch++;
382 q = buf;
383 while ((*pch >= 'a' && *pch <= 'z') ||
384 (*pch >= 'A' && *pch <= 'Z') ||
385 (*pch >= '0' && *pch <= '9') ||
386 *pch == '_' || *pch == '.') {
387 if ((q - buf) < sizeof(buf) - 1) {
388 *q++ = *pch;
390 pch++;
392 while (qemu_isspace(*pch)) {
393 pch++;
395 *q = 0;
396 ret = get_monitor_def(mon, &reg, buf);
397 if (ret < 0) {
398 expr_error(mon, "unknown register");
400 n = reg;
402 break;
403 case '\0':
404 expr_error(mon, "unexpected end of expression");
405 n = 0;
406 break;
407 default:
408 errno = 0;
409 n = strtoull(pch, &p, 0);
410 if (errno == ERANGE) {
411 expr_error(mon, "number too large");
413 if (pch == p) {
414 expr_error(mon, "invalid char '%c' in expression", *p);
416 pch = p;
417 while (qemu_isspace(*pch)) {
418 pch++;
420 break;
422 return n;
425 static int64_t expr_prod(Monitor *mon)
427 int64_t val, val2;
428 int op;
430 val = expr_unary(mon);
431 for (;;) {
432 op = *pch;
433 if (op != '*' && op != '/' && op != '%') {
434 break;
436 next();
437 val2 = expr_unary(mon);
438 switch (op) {
439 default:
440 case '*':
441 val *= val2;
442 break;
443 case '/':
444 case '%':
445 if (val2 == 0) {
446 expr_error(mon, "division by zero");
448 if (op == '/') {
449 val /= val2;
450 } else {
451 val %= val2;
453 break;
456 return val;
459 static int64_t expr_logic(Monitor *mon)
461 int64_t val, val2;
462 int op;
464 val = expr_prod(mon);
465 for (;;) {
466 op = *pch;
467 if (op != '&' && op != '|' && op != '^') {
468 break;
470 next();
471 val2 = expr_prod(mon);
472 switch (op) {
473 default:
474 case '&':
475 val &= val2;
476 break;
477 case '|':
478 val |= val2;
479 break;
480 case '^':
481 val ^= val2;
482 break;
485 return val;
488 static int64_t expr_sum(Monitor *mon)
490 int64_t val, val2;
491 int op;
493 val = expr_logic(mon);
494 for (;;) {
495 op = *pch;
496 if (op != '+' && op != '-') {
497 break;
499 next();
500 val2 = expr_logic(mon);
501 if (op == '+') {
502 val += val2;
503 } else {
504 val -= val2;
507 return val;
510 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
512 pch = *pp;
513 if (sigsetjmp(expr_env, 0)) {
514 *pp = pch;
515 return -1;
517 while (qemu_isspace(*pch)) {
518 pch++;
520 *pval = expr_sum(mon);
521 *pp = pch;
522 return 0;
525 static int get_double(Monitor *mon, double *pval, const char **pp)
527 const char *p = *pp;
528 char *tailp;
529 double d;
531 d = strtod(p, &tailp);
532 if (tailp == p) {
533 monitor_printf(mon, "Number expected\n");
534 return -1;
536 if (d != d || d - d != 0) {
537 /* NaN or infinity */
538 monitor_printf(mon, "Bad number\n");
539 return -1;
541 *pval = d;
542 *pp = tailp;
543 return 0;
547 * Store the command-name in cmdname, and return a pointer to
548 * the remaining of the command string.
550 static const char *get_command_name(const char *cmdline,
551 char *cmdname, size_t nlen)
553 size_t len;
554 const char *p, *pstart;
556 p = cmdline;
557 while (qemu_isspace(*p)) {
558 p++;
560 if (*p == '\0') {
561 return NULL;
563 pstart = p;
564 while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) {
565 p++;
567 len = p - pstart;
568 if (len > nlen - 1) {
569 len = nlen - 1;
571 memcpy(cmdname, pstart, len);
572 cmdname[len] = '\0';
573 return p;
577 * Read key of 'type' into 'key' and return the current
578 * 'type' pointer.
580 static char *key_get_info(const char *type, char **key)
582 size_t len;
583 char *p, *str;
585 if (*type == ',') {
586 type++;
589 p = strchr(type, ':');
590 if (!p) {
591 *key = NULL;
592 return NULL;
594 len = p - type;
596 str = g_malloc(len + 1);
597 memcpy(str, type, len);
598 str[len] = '\0';
600 *key = str;
601 return ++p;
604 static int default_fmt_format = 'x';
605 static int default_fmt_size = 4;
607 static int is_valid_option(const char *c, const char *typestr)
609 char option[3];
611 option[0] = '-';
612 option[1] = *c;
613 option[2] = '\0';
615 typestr = strstr(typestr, option);
616 return (typestr != NULL);
619 static const HMPCommand *search_dispatch_table(const HMPCommand *disp_table,
620 const char *cmdname)
622 const HMPCommand *cmd;
624 for (cmd = disp_table; cmd->name != NULL; cmd++) {
625 if (hmp_compare_cmd(cmdname, cmd->name)) {
626 return cmd;
630 return NULL;
634 * Parse command name from @cmdp according to command table @table.
635 * If blank, return NULL.
636 * Else, if no valid command can be found, report to @mon, and return
637 * NULL.
638 * Else, change @cmdp to point right behind the name, and return its
639 * command table entry.
640 * Do not assume the return value points into @table! It doesn't when
641 * the command is found in a sub-command table.
643 static const HMPCommand *monitor_parse_command(MonitorHMP *hmp_mon,
644 const char *cmdp_start,
645 const char **cmdp,
646 HMPCommand *table)
648 Monitor *mon = &hmp_mon->common;
649 const char *p;
650 const HMPCommand *cmd;
651 char cmdname[256];
653 /* extract the command name */
654 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
655 if (!p) {
656 return NULL;
659 cmd = search_dispatch_table(table, cmdname);
660 if (!cmd) {
661 monitor_printf(mon, "unknown command: '%.*s'\n",
662 (int)(p - cmdp_start), cmdp_start);
663 return NULL;
665 if (!cmd_available(cmd)) {
666 monitor_printf(mon, "Command '%.*s' not available "
667 "until machine initialization has completed.\n",
668 (int)(p - cmdp_start), cmdp_start);
669 return NULL;
672 /* filter out following useless space */
673 while (qemu_isspace(*p)) {
674 p++;
677 *cmdp = p;
678 /* search sub command */
679 if (cmd->sub_table != NULL && *p != '\0') {
680 return monitor_parse_command(hmp_mon, cmdp_start, cmdp, cmd->sub_table);
683 return cmd;
687 * Parse arguments for @cmd.
688 * If it can't be parsed, report to @mon, and return NULL.
689 * Else, insert command arguments into a QDict, and return it.
690 * Note: On success, caller has to free the QDict structure.
692 static QDict *monitor_parse_arguments(Monitor *mon,
693 const char **endp,
694 const HMPCommand *cmd)
696 const char *typestr;
697 char *key;
698 int c;
699 const char *p = *endp;
700 char buf[1024];
701 QDict *qdict = qdict_new();
703 /* parse the parameters */
704 typestr = cmd->args_type;
705 for (;;) {
706 typestr = key_get_info(typestr, &key);
707 if (!typestr) {
708 break;
710 c = *typestr;
711 typestr++;
712 switch (c) {
713 case 'F':
714 case 'B':
715 case 's':
717 int ret;
719 while (qemu_isspace(*p)) {
720 p++;
722 if (*typestr == '?') {
723 typestr++;
724 if (*p == '\0') {
725 /* no optional string: NULL argument */
726 break;
729 ret = get_str(buf, sizeof(buf), &p);
730 if (ret < 0) {
731 switch (c) {
732 case 'F':
733 monitor_printf(mon, "%s: filename expected\n",
734 cmd->name);
735 break;
736 case 'B':
737 monitor_printf(mon, "%s: block device name expected\n",
738 cmd->name);
739 break;
740 default:
741 monitor_printf(mon, "%s: string expected\n", cmd->name);
742 break;
744 goto fail;
746 qdict_put_str(qdict, key, buf);
748 break;
749 case 'O':
751 QemuOptsList *opts_list;
752 QemuOpts *opts;
754 opts_list = qemu_find_opts(key);
755 if (!opts_list || opts_list->desc->name) {
756 goto bad_type;
758 while (qemu_isspace(*p)) {
759 p++;
761 if (!*p) {
762 break;
764 if (get_str(buf, sizeof(buf), &p) < 0) {
765 goto fail;
767 opts = qemu_opts_parse_noisily(opts_list, buf, true);
768 if (!opts) {
769 goto fail;
771 qemu_opts_to_qdict(opts, qdict);
772 qemu_opts_del(opts);
774 break;
775 case '/':
777 int count, format, size;
779 while (qemu_isspace(*p)) {
780 p++;
782 if (*p == '/') {
783 /* format found */
784 p++;
785 count = 1;
786 if (qemu_isdigit(*p)) {
787 count = 0;
788 while (qemu_isdigit(*p)) {
789 count = count * 10 + (*p - '0');
790 p++;
793 size = -1;
794 format = -1;
795 for (;;) {
796 switch (*p) {
797 case 'o':
798 case 'd':
799 case 'u':
800 case 'x':
801 case 'i':
802 case 'c':
803 format = *p++;
804 break;
805 case 'b':
806 size = 1;
807 p++;
808 break;
809 case 'h':
810 size = 2;
811 p++;
812 break;
813 case 'w':
814 size = 4;
815 p++;
816 break;
817 case 'g':
818 case 'L':
819 size = 8;
820 p++;
821 break;
822 default:
823 goto next;
826 next:
827 if (*p != '\0' && !qemu_isspace(*p)) {
828 monitor_printf(mon, "invalid char in format: '%c'\n",
829 *p);
830 goto fail;
832 if (format < 0) {
833 format = default_fmt_format;
835 if (format != 'i') {
836 /* for 'i', not specifying a size gives -1 as size */
837 if (size < 0) {
838 size = default_fmt_size;
840 default_fmt_size = size;
842 default_fmt_format = format;
843 } else {
844 count = 1;
845 format = default_fmt_format;
846 if (format != 'i') {
847 size = default_fmt_size;
848 } else {
849 size = -1;
852 qdict_put_int(qdict, "count", count);
853 qdict_put_int(qdict, "format", format);
854 qdict_put_int(qdict, "size", size);
856 break;
857 case 'i':
858 case 'l':
859 case 'M':
861 int64_t val;
863 while (qemu_isspace(*p)) {
864 p++;
866 if (*typestr == '?' || *typestr == '.') {
867 if (*typestr == '?') {
868 if (*p == '\0') {
869 typestr++;
870 break;
872 } else {
873 if (*p == '.') {
874 p++;
875 while (qemu_isspace(*p)) {
876 p++;
878 } else {
879 typestr++;
880 break;
883 typestr++;
885 if (get_expr(mon, &val, &p)) {
886 goto fail;
888 /* Check if 'i' is greater than 32-bit */
889 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
890 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
891 monitor_printf(mon, "integer is for 32-bit values\n");
892 goto fail;
893 } else if (c == 'M') {
894 if (val < 0) {
895 monitor_printf(mon, "enter a positive value\n");
896 goto fail;
898 val *= MiB;
900 qdict_put_int(qdict, key, val);
902 break;
903 case 'o':
905 int ret;
906 uint64_t val;
907 const char *end;
909 while (qemu_isspace(*p)) {
910 p++;
912 if (*typestr == '?') {
913 typestr++;
914 if (*p == '\0') {
915 break;
918 ret = qemu_strtosz_MiB(p, &end, &val);
919 if (ret < 0 || val > INT64_MAX) {
920 monitor_printf(mon, "invalid size\n");
921 goto fail;
923 qdict_put_int(qdict, key, val);
924 p = end;
926 break;
927 case 'T':
929 double val;
931 while (qemu_isspace(*p)) {
932 p++;
934 if (*typestr == '?') {
935 typestr++;
936 if (*p == '\0') {
937 break;
940 if (get_double(mon, &val, &p) < 0) {
941 goto fail;
943 if (p[0] && p[1] == 's') {
944 switch (*p) {
945 case 'm':
946 val /= 1e3; p += 2; break;
947 case 'u':
948 val /= 1e6; p += 2; break;
949 case 'n':
950 val /= 1e9; p += 2; break;
953 if (*p && !qemu_isspace(*p)) {
954 monitor_printf(mon, "Unknown unit suffix\n");
955 goto fail;
957 qdict_put(qdict, key, qnum_from_double(val));
959 break;
960 case 'b':
962 const char *beg;
963 bool val;
965 while (qemu_isspace(*p)) {
966 p++;
968 beg = p;
969 while (qemu_isgraph(*p)) {
970 p++;
972 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
973 val = true;
974 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
975 val = false;
976 } else {
977 monitor_printf(mon, "Expected 'on' or 'off'\n");
978 goto fail;
980 qdict_put_bool(qdict, key, val);
982 break;
983 case '-':
985 const char *tmp = p;
986 int skip_key = 0;
987 int ret;
988 /* option */
990 c = *typestr++;
991 if (c == '\0') {
992 goto bad_type;
994 while (qemu_isspace(*p)) {
995 p++;
997 if (*p == '-') {
998 p++;
999 if (c != *p) {
1000 if (!is_valid_option(p, typestr)) {
1001 monitor_printf(mon, "%s: unsupported option -%c\n",
1002 cmd->name, *p);
1003 goto fail;
1004 } else {
1005 skip_key = 1;
1008 if (skip_key) {
1009 p = tmp;
1010 } else if (*typestr == 's') {
1011 /* has option with string value */
1012 typestr++;
1013 tmp = p++;
1014 while (qemu_isspace(*p)) {
1015 p++;
1017 ret = get_str(buf, sizeof(buf), &p);
1018 if (ret < 0) {
1019 monitor_printf(mon, "%s: value expected for -%c\n",
1020 cmd->name, *tmp);
1021 goto fail;
1023 qdict_put_str(qdict, key, buf);
1024 } else {
1025 /* has boolean option */
1026 p++;
1027 qdict_put_bool(qdict, key, true);
1029 } else if (*typestr == 's') {
1030 typestr++;
1033 break;
1034 case 'S':
1036 /* package all remaining string */
1037 int len;
1039 while (qemu_isspace(*p)) {
1040 p++;
1042 if (*typestr == '?') {
1043 typestr++;
1044 if (*p == '\0') {
1045 /* no remaining string: NULL argument */
1046 break;
1049 len = strlen(p);
1050 if (len <= 0) {
1051 monitor_printf(mon, "%s: string expected\n",
1052 cmd->name);
1053 goto fail;
1055 qdict_put_str(qdict, key, p);
1056 p += len;
1058 break;
1059 default:
1060 bad_type:
1061 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
1062 goto fail;
1064 g_free(key);
1065 key = NULL;
1067 /* check that all arguments were parsed */
1068 while (qemu_isspace(*p)) {
1069 p++;
1071 if (*p != '\0') {
1072 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
1073 cmd->name);
1074 goto fail;
1077 return qdict;
1079 fail:
1080 qobject_unref(qdict);
1081 g_free(key);
1082 return NULL;
1085 static void hmp_info_human_readable_text(Monitor *mon,
1086 HumanReadableText *(*handler)(Error **))
1088 Error *err = NULL;
1089 g_autoptr(HumanReadableText) info = handler(&err);
1091 if (hmp_handle_error(mon, err)) {
1092 return;
1095 monitor_puts(mon, info->human_readable_text);
1098 static void handle_hmp_command_exec(Monitor *mon,
1099 const HMPCommand *cmd,
1100 QDict *qdict)
1102 if (cmd->cmd_info_hrt) {
1103 hmp_info_human_readable_text(mon,
1104 cmd->cmd_info_hrt);
1105 } else {
1106 cmd->cmd(mon, qdict);
1110 typedef struct HandleHmpCommandCo {
1111 Monitor *mon;
1112 const HMPCommand *cmd;
1113 QDict *qdict;
1114 bool done;
1115 } HandleHmpCommandCo;
1117 static void handle_hmp_command_co(void *opaque)
1119 HandleHmpCommandCo *data = opaque;
1120 handle_hmp_command_exec(data->mon, data->cmd, data->qdict);
1121 monitor_set_cur(qemu_coroutine_self(), NULL);
1122 data->done = true;
1125 void handle_hmp_command(MonitorHMP *mon, const char *cmdline)
1127 QDict *qdict;
1128 const HMPCommand *cmd;
1129 const char *cmd_start = cmdline;
1131 trace_handle_hmp_command(mon, cmdline);
1133 cmd = monitor_parse_command(mon, cmdline, &cmdline, hmp_cmds);
1134 if (!cmd) {
1135 return;
1138 if (!cmd->cmd && !cmd->cmd_info_hrt) {
1139 /* FIXME: is it useful to try autoload modules here ??? */
1140 monitor_printf(&mon->common, "Command \"%.*s\" is not available.\n",
1141 (int)(cmdline - cmd_start), cmd_start);
1142 return;
1145 qdict = monitor_parse_arguments(&mon->common, &cmdline, cmd);
1146 if (!qdict) {
1147 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
1148 cmdline--;
1150 monitor_printf(&mon->common, "Try \"help %.*s\" for more information\n",
1151 (int)(cmdline - cmd_start), cmd_start);
1152 return;
1155 if (!cmd->coroutine) {
1156 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1157 Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(), &mon->common);
1158 handle_hmp_command_exec(&mon->common, cmd, qdict);
1159 monitor_set_cur(qemu_coroutine_self(), old_mon);
1160 } else {
1161 HandleHmpCommandCo data = {
1162 .mon = &mon->common,
1163 .cmd = cmd,
1164 .qdict = qdict,
1165 .done = false,
1167 Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data);
1168 monitor_set_cur(co, &mon->common);
1169 aio_co_enter(qemu_get_aio_context(), co);
1170 AIO_WAIT_WHILE_UNLOCKED(NULL, !data.done);
1173 qobject_unref(qdict);
1176 static void cmd_completion(MonitorHMP *mon, const char *name, const char *list)
1178 const char *p, *pstart;
1179 char cmd[128];
1180 int len;
1182 p = list;
1183 for (;;) {
1184 pstart = p;
1185 p = qemu_strchrnul(p, '|');
1186 len = p - pstart;
1187 if (len > sizeof(cmd) - 2) {
1188 len = sizeof(cmd) - 2;
1190 memcpy(cmd, pstart, len);
1191 cmd[len] = '\0';
1192 readline_add_completion_of(mon->rs, name, cmd);
1193 if (*p == '\0') {
1194 break;
1196 p++;
1200 static void file_completion(MonitorHMP *mon, const char *input)
1202 DIR *ffs;
1203 struct dirent *d;
1204 char path[1024];
1205 char file[1024], file_prefix[1024];
1206 int input_path_len;
1207 const char *p;
1209 p = strrchr(input, '/');
1210 if (!p) {
1211 input_path_len = 0;
1212 pstrcpy(file_prefix, sizeof(file_prefix), input);
1213 pstrcpy(path, sizeof(path), ".");
1214 } else {
1215 input_path_len = p - input + 1;
1216 memcpy(path, input, input_path_len);
1217 if (input_path_len > sizeof(path) - 1) {
1218 input_path_len = sizeof(path) - 1;
1220 path[input_path_len] = '\0';
1221 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1224 ffs = opendir(path);
1225 if (!ffs) {
1226 return;
1228 for (;;) {
1229 struct stat sb;
1230 d = readdir(ffs);
1231 if (!d) {
1232 break;
1235 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
1236 continue;
1239 if (strstart(d->d_name, file_prefix, NULL)) {
1240 memcpy(file, input, input_path_len);
1241 if (input_path_len < sizeof(file)) {
1242 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
1243 d->d_name);
1246 * stat the file to find out if it's a directory.
1247 * In that case add a slash to speed up typing long paths
1249 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
1250 pstrcat(file, sizeof(file), "/");
1252 readline_add_completion(mon->rs, file);
1255 closedir(ffs);
1258 static const char *next_arg_type(const char *typestr)
1260 const char *p = strchr(typestr, ':');
1261 return (p != NULL ? ++p : typestr);
1264 static void monitor_find_completion_by_table(MonitorHMP *mon,
1265 const HMPCommand *cmd_table,
1266 char **args,
1267 int nb_args)
1269 const char *cmdname;
1270 int i;
1271 const char *ptype, *old_ptype, *str;
1272 const HMPCommand *cmd;
1273 BlockBackend *blk = NULL;
1275 if (nb_args <= 1) {
1276 /* command completion */
1277 if (nb_args == 0) {
1278 cmdname = "";
1279 } else {
1280 cmdname = args[0];
1282 readline_set_completion_index(mon->rs, strlen(cmdname));
1283 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
1284 if (cmd_available(cmd)) {
1285 cmd_completion(mon, cmdname, cmd->name);
1288 } else {
1289 /* find the command */
1290 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
1291 if (hmp_compare_cmd(args[0], cmd->name) &&
1292 cmd_available(cmd)) {
1293 break;
1296 if (!cmd->name) {
1297 return;
1300 if (cmd->sub_table) {
1301 /* do the job again */
1302 monitor_find_completion_by_table(mon, cmd->sub_table,
1303 &args[1], nb_args - 1);
1304 return;
1306 if (cmd->command_completion) {
1307 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
1308 return;
1311 ptype = next_arg_type(cmd->args_type);
1312 for (i = 0; i < nb_args - 2; i++) {
1313 if (*ptype != '\0') {
1314 ptype = next_arg_type(ptype);
1315 while (*ptype == '?') {
1316 ptype = next_arg_type(ptype);
1320 str = args[nb_args - 1];
1321 old_ptype = NULL;
1322 while (*ptype == '-' && old_ptype != ptype) {
1323 old_ptype = ptype;
1324 ptype = next_arg_type(ptype);
1326 switch (*ptype) {
1327 case 'F':
1328 /* file completion */
1329 readline_set_completion_index(mon->rs, strlen(str));
1330 file_completion(mon, str);
1331 break;
1332 case 'B':
1333 /* block device name completion */
1334 readline_set_completion_index(mon->rs, strlen(str));
1335 while ((blk = blk_next(blk)) != NULL) {
1336 readline_add_completion_of(mon->rs, str, blk_name(blk));
1338 break;
1339 case 's':
1340 case 'S':
1341 if (!strcmp(cmd->name, "help|?")) {
1342 monitor_find_completion_by_table(mon, cmd_table,
1343 &args[1], nb_args - 1);
1345 break;
1346 default:
1347 break;
1352 static void monitor_find_completion(void *opaque,
1353 const char *cmdline)
1355 MonitorHMP *mon = opaque;
1356 char *args[MAX_ARGS];
1357 int nb_args, len;
1359 /* 1. parse the cmdline */
1360 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
1361 return;
1365 * if the line ends with a space, it means we want to complete the
1366 * next arg
1368 len = strlen(cmdline);
1369 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
1370 if (nb_args >= MAX_ARGS) {
1371 goto cleanup;
1373 args[nb_args++] = g_strdup("");
1376 /* 2. auto complete according to args */
1377 monitor_find_completion_by_table(mon, hmp_cmds, args, nb_args);
1379 cleanup:
1380 free_cmdline_args(args, nb_args);
1383 static void monitor_read(void *opaque, const uint8_t *buf, int size)
1385 MonitorHMP *mon = container_of(opaque, MonitorHMP, common);
1386 int i;
1388 if (mon->rs) {
1389 for (i = 0; i < size; i++) {
1390 readline_handle_byte(mon->rs, buf[i]);
1392 } else {
1393 if (size == 0 || buf[size - 1] != 0) {
1394 monitor_printf(&mon->common, "corrupted command\n");
1395 } else {
1396 handle_hmp_command(mon, (char *)buf);
1401 static void monitor_event(void *opaque, QEMUChrEvent event)
1403 Monitor *mon = opaque;
1405 switch (event) {
1406 case CHR_EVENT_MUX_IN:
1407 qemu_mutex_lock(&mon->mon_lock);
1408 if (mon->mux_out) {
1409 mon->mux_out = 0;
1410 monitor_resume(mon);
1412 qemu_mutex_unlock(&mon->mon_lock);
1413 break;
1415 case CHR_EVENT_MUX_OUT:
1416 qemu_mutex_lock(&mon->mon_lock);
1417 if (!mon->mux_out) {
1418 if (mon->reset_seen && !mon->suspend_cnt) {
1419 monitor_puts_locked(mon, "\n");
1420 } else {
1421 monitor_flush_locked(mon);
1423 monitor_suspend(mon);
1424 mon->mux_out = 1;
1426 qemu_mutex_unlock(&mon->mon_lock);
1427 break;
1429 case CHR_EVENT_OPENED:
1430 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
1431 "information\n", QEMU_VERSION);
1432 qemu_mutex_lock(&mon->mon_lock);
1433 mon->reset_seen = 1;
1434 if (!mon->mux_out) {
1435 /* Suspend-resume forces the prompt to be printed. */
1436 monitor_suspend(mon);
1437 monitor_resume(mon);
1439 qemu_mutex_unlock(&mon->mon_lock);
1440 mon_refcount++;
1441 break;
1443 case CHR_EVENT_CLOSED:
1444 mon_refcount--;
1445 monitor_fdsets_cleanup();
1446 break;
1448 case CHR_EVENT_BREAK:
1449 /* Ignored */
1450 break;
1456 * These functions just adapt the readline interface in a typesafe way. We
1457 * could cast function pointers but that discards compiler checks.
1459 static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque,
1460 const char *fmt, ...)
1462 MonitorHMP *mon = opaque;
1463 va_list ap;
1464 va_start(ap, fmt);
1465 monitor_vprintf(&mon->common, fmt, ap);
1466 va_end(ap);
1469 static void monitor_readline_flush(void *opaque)
1471 MonitorHMP *mon = opaque;
1472 monitor_flush(&mon->common);
1475 void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
1477 MonitorHMP *mon = g_new0(MonitorHMP, 1);
1479 if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
1480 g_free(mon);
1481 return;
1484 monitor_data_init(&mon->common, false, false, false);
1486 mon->use_readline = use_readline;
1487 if (mon->use_readline) {
1488 mon->rs = readline_init(monitor_readline_printf,
1489 monitor_readline_flush,
1490 mon,
1491 monitor_find_completion);
1492 monitor_read_command(mon, 0);
1495 qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
1496 monitor_event, NULL, &mon->common, NULL, true);
1497 monitor_list_append(&mon->common);