master
c 1,806 lines 48.5 KB
Raw
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
25 #include "qemu/osdep.h"
26 #include <dirent.h>
27 #include "hw/core/qdev.h"
28 #include "hw/core/sysemu-cpu-ops.h"
29 #include "monitor-internal.h"
30 #include "monitor-hmp-internal.h"
31 #include "monitor/hmp.h"
32 #include "qobject/qdict.h"
33 #include "qobject/qnum.h"
34 #include "qemu/bswap.h"
35 #include "qemu/config-file.h"
36 #include "qemu/ctype.h"
37 #include "qemu/cutils.h"
38 #include "qemu/log.h"
39 #include "qemu/option.h"
40 #include "qemu/base-arch-defs.h"
41 #include "qemu/target-info.h"
42 #include "qemu/units.h"
43 #include "qapi/error.h"
44 #include "qom/object_interfaces.h"
45 #include "exec/gdbstub.h"
46 #include "system/block-backend.h"
47 #include "trace.h"
48
49 OBJECT_DEFINE_TYPE(MonitorHMP, monitor_hmp, MONITOR_HMP, MONITOR);
50
51 MonitorHMP *monitor_cur_hmp(void)
52 {
53 Monitor *mon = monitor_cur();
54 if (mon) {
55 return (MonitorHMP *)object_dynamic_cast(OBJECT(mon),
56 TYPE_MONITOR_HMP);
57 }
58 return NULL;
59 }
60
61 static void monitor_hmp_finalize(Object *obj)
62 {
63 MonitorHMP *hmp = MONITOR_HMP(obj);
64 g_free(hmp->mon_cpu_path);
65 if (hmp->rs) {
66 readline_free(hmp->rs);
67 }
68 }
69
70 static bool monitor_hmp_get_readline(Object *obj, Error **errp)
71 {
72 MonitorHMP *hmp = MONITOR_HMP(obj);
73
74 return hmp->use_readline;
75 }
76
77 static void monitor_hmp_set_readline(Object *obj, bool val, Error **errp)
78 {
79 MonitorHMP *hmp = MONITOR_HMP(obj);
80
81 hmp->use_readline = val;
82 }
83
84 static void monitor_hmp_accept_input(Monitor *mon);
85 static void monitor_hmp_complete(UserCreatable *uc, Error **errp);
86 static bool monitor_hmp_prepare_delete(UserCreatable *uc, Error **errp);
87
88 static void monitor_hmp_class_init(ObjectClass *cls, const void *data)
89 {
90 MonitorClass *moncls = MONITOR_CLASS(cls);
91 UserCreatableClass *ucc = USER_CREATABLE_CLASS(cls);
92
93 object_class_property_add_bool(cls, "readline",
94 monitor_hmp_get_readline,
95 monitor_hmp_set_readline);
96
97 moncls->accept_input = monitor_hmp_accept_input;
98
99 ucc->complete = monitor_hmp_complete;
100 ucc->prepare_delete = monitor_hmp_prepare_delete;
101 }
102
103 static void monitor_hmp_init(Object *obj)
104 {
105 MonitorHMP *hmp = MONITOR_HMP(obj);
106
107 /*
108 * Default to common case for external HMP use,
109 * as opposed to non-interactive internal use
110 * from gdbstub
111 */
112 hmp->use_readline = true;
113 }
114
115 static void monitor_hmp_accept_input(Monitor *mon)
116 {
117 qemu_mutex_lock(&mon->mon_lock);
118 MonitorHMP *hmp = MONITOR_HMP(mon);
119 if (hmp->reset_seen) {
120 assert(hmp->rs);
121 readline_restart(hmp->rs);
122 qemu_chr_fe_accept_input(&mon->chr);
123 qemu_mutex_unlock(&mon->mon_lock);
124 readline_show_prompt(hmp->rs);
125 } else {
126 qemu_chr_fe_accept_input(&mon->chr);
127 qemu_mutex_unlock(&mon->mon_lock);
128 }
129 }
130
131 static void monitor_command_cb(void *opaque, const char *cmdline,
132 void *readline_opaque)
133 {
134 MonitorHMP *hmp = opaque;
135
136 monitor_suspend(&hmp->parent_obj);
137 handle_hmp_command(hmp, cmdline);
138 monitor_resume(&hmp->parent_obj);
139 }
140
141 void monitor_hmp_read_command(MonitorHMP *hmp, int show_prompt)
142 {
143 if (!hmp->rs) {
144 return;
145 }
146
147 readline_start(hmp->rs, "(qemu) ", 0, monitor_command_cb, NULL);
148 if (show_prompt) {
149 readline_show_prompt(hmp->rs);
150 }
151 }
152
153 int monitor_hmp_read_password(MonitorHMP *hmp, ReadLineFunc *readline_func,
154 void *opaque)
155 {
156 if (hmp->rs) {
157 readline_start(hmp->rs, "Password: ", 1, readline_func, opaque);
158 /* prompt is printed on return from the command handler */
159 return 0;
160 } else {
161 monitor_hmp_printf(hmp, "terminal does not support password prompting\n");
162 return -ENOTTY;
163 }
164 }
165
166 static int get_str(char *buf, int buf_size, const char **pp)
167 {
168 const char *p;
169 char *q;
170 int c;
171
172 q = buf;
173 p = *pp;
174 while (qemu_isspace(*p)) {
175 p++;
176 }
177 if (*p == '\0') {
178 fail:
179 *q = '\0';
180 *pp = p;
181 return -1;
182 }
183 if (*p == '\"') {
184 p++;
185 while (*p != '\0' && *p != '\"') {
186 if (*p == '\\') {
187 p++;
188 c = *p++;
189 switch (c) {
190 case 'n':
191 c = '\n';
192 break;
193 case 'r':
194 c = '\r';
195 break;
196 case '\\':
197 case '\'':
198 case '\"':
199 break;
200 default:
201 printf("unsupported escape code: '\\%c'\n", c);
202 goto fail;
203 }
204 if ((q - buf) < buf_size - 1) {
205 *q++ = c;
206 }
207 } else {
208 if ((q - buf) < buf_size - 1) {
209 *q++ = *p;
210 }
211 p++;
212 }
213 }
214 if (*p != '\"') {
215 printf("unterminated string\n");
216 goto fail;
217 }
218 p++;
219 } else {
220 while (*p != '\0' && !qemu_isspace(*p)) {
221 if ((q - buf) < buf_size - 1) {
222 *q++ = *p;
223 }
224 p++;
225 }
226 }
227 *q = '\0';
228 *pp = p;
229 return 0;
230 }
231
232 #define MAX_ARGS 16
233
234 static void free_cmdline_args(char **args, int nb_args)
235 {
236 int i;
237
238 assert(nb_args <= MAX_ARGS);
239
240 for (i = 0; i < nb_args; i++) {
241 g_free(args[i]);
242 }
243
244 }
245
246 /*
247 * Parse the command line to get valid args.
248 * @cmdline: command line to be parsed.
249 * @pnb_args: location to store the number of args, must NOT be NULL.
250 * @args: location to store the args, which should be freed by caller, must
251 * NOT be NULL.
252 *
253 * Returns 0 on success, negative on failure.
254 *
255 * NOTE: this parser is an approximate form of the real command parser. Number
256 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
257 * return with failure.
258 */
259 static int parse_cmdline(const char *cmdline,
260 int *pnb_args, char **args)
261 {
262 const char *p;
263 int nb_args, ret;
264 char buf[1024];
265
266 p = cmdline;
267 nb_args = 0;
268 for (;;) {
269 while (qemu_isspace(*p)) {
270 p++;
271 }
272 if (*p == '\0') {
273 break;
274 }
275 if (nb_args >= MAX_ARGS) {
276 goto fail;
277 }
278 ret = get_str(buf, sizeof(buf), &p);
279 if (ret < 0) {
280 goto fail;
281 }
282 args[nb_args] = g_strdup(buf);
283 nb_args++;
284 }
285 *pnb_args = nb_args;
286 return 0;
287
288 fail:
289 free_cmdline_args(args, nb_args);
290 return -1;
291 }
292
293 /*
294 * Can command @cmd be executed in preconfig state?
295 */
296 static bool cmd_can_preconfig(const HMPCommand *cmd)
297 {
298 if (!cmd->flags) {
299 return false;
300 }
301
302 return strchr(cmd->flags, 'p');
303 }
304
305 static bool cmd_available(const HMPCommand *cmd)
306 {
307 if (cmd->arch_bitmask && !qemu_arch_available(cmd->arch_bitmask)) {
308 return false;
309 }
310 return phase_check(PHASE_MACHINE_READY) || cmd_can_preconfig(cmd);
311 }
312
313 static void help_cmd_dump_one(MonitorHMP *mon,
314 const HMPCommand *cmd,
315 char **prefix_args,
316 int prefix_args_nb)
317 {
318 int i;
319
320 if (!cmd_available(cmd)) {
321 return;
322 }
323
324 for (i = 0; i < prefix_args_nb; i++) {
325 monitor_hmp_printf(mon, "%s ", prefix_args[i]);
326 }
327 monitor_hmp_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
328 }
329
330 /* @args[@arg_index] is the valid command need to find in @cmds */
331 static void help_cmd_dump(MonitorHMP *mon, const HMPCommand *cmds,
332 char **args, int nb_args, int arg_index)
333 {
334 const HMPCommand *cmd;
335 size_t i;
336
337 /* No valid arg need to compare with, dump all in *cmds */
338 if (arg_index >= nb_args) {
339 for (cmd = cmds; cmd->name != NULL; cmd++) {
340 help_cmd_dump_one(mon, cmd, args, arg_index);
341 }
342 return;
343 }
344
345 /* Find one entry to dump */
346 for (cmd = cmds; cmd->name != NULL; cmd++) {
347 if (hmp_compare_cmd(args[arg_index], cmd->name) &&
348 cmd_available(cmd)) {
349 if (cmd->sub_table) {
350 /* continue with next arg */
351 help_cmd_dump(mon, cmd->sub_table,
352 args, nb_args, arg_index + 1);
353 } else {
354 help_cmd_dump_one(mon, cmd, args, arg_index);
355 }
356 return;
357 }
358 }
359
360 /* Command not found */
361 monitor_hmp_printf(mon, "unknown command: '");
362 for (i = 0; i <= arg_index; i++) {
363 monitor_hmp_printf(mon, "%s%s", args[i], i == arg_index ? "'\n" : " ");
364 }
365 }
366
367 void hmp_help_cmd(MonitorHMP *mon, const char *name)
368 {
369 char *args[MAX_ARGS];
370 int nb_args = 0;
371
372 /* 1. parse user input */
373 if (name) {
374 /* special case for log, directly dump and return */
375 if (!strcmp(name, "log")) {
376 const QEMULogItem *item;
377 monitor_hmp_printf(mon, "Log items (comma separated):\n");
378 monitor_hmp_printf(mon, "%-15s %s\n", "none", "remove all logs");
379 for (item = qemu_log_items; item->mask != 0; item++) {
380 monitor_hmp_printf(mon, "%-15s %s\n", item->name, item->help);
381 }
382 #ifdef CONFIG_TRACE_LOG
383 monitor_hmp_printf(mon, "trace:PATTERN enable trace events\n");
384 monitor_hmp_printf(mon, "\nUse \"log trace:help\" to get a list of "
385 "trace events.\n\n");
386 #endif
387 return;
388 }
389
390 if (parse_cmdline(name, &nb_args, args) < 0) {
391 return;
392 }
393 }
394
395 /* 2. dump the contents according to parsed args */
396 help_cmd_dump(mon, hmp_cmds_for_target(false), args, nb_args, 0);
397
398 free_cmdline_args(args, nb_args);
399 }
400
401 /*
402 * Set @pval to the value in the register identified by @name.
403 * return %true if the register is found, %false otherwise.
404 */
405 static bool gdb_get_register(MonitorHMP *hmp, int64_t *pval, const char *name)
406 {
407 g_autoptr(GArray) regs = NULL;
408 CPUState *cs = monitor_hmp_get_cpu(hmp);
409
410 if (cs == NULL) {
411 return false;
412 }
413
414 regs = gdb_get_register_list(cs);
415
416 for (int i = 0; i < regs->len; i++) {
417 GDBRegDesc *reg = &g_array_index(regs, GDBRegDesc, i);
418 g_autoptr(GByteArray) buf = NULL;
419 int reg_size;
420
421 if (!reg->name || g_strcmp0(name, reg->name)) {
422 continue;
423 }
424
425 buf = g_byte_array_new();
426 reg_size = gdb_read_register(cs, buf, reg->gdb_reg);
427 if (reg_size > sizeof(*pval)) {
428 return false;
429 }
430
431 if (target_big_endian()) {
432 *pval = ldn_be_p(buf->data, reg_size);
433 } else {
434 *pval = ldn_le_p(buf->data, reg_size);
435 }
436 return true;
437 }
438 return false;
439 }
440
441 /*******************************************************************/
442
443 static const char *pch;
444 static sigjmp_buf expr_env;
445
446 static int get_monitor_def(MonitorHMP *mon, int64_t *pval, const char *name);
447
448 static G_NORETURN G_GNUC_PRINTF(2, 3)
449 void expr_error(MonitorHMP *mon, const char *fmt, ...)
450 {
451 va_list ap;
452 va_start(ap, fmt);
453 monitor_hmp_vprintf(mon, fmt, ap);
454 monitor_hmp_printf(mon, "\n");
455 va_end(ap);
456 siglongjmp(expr_env, 1);
457 }
458
459 static void next(void)
460 {
461 if (*pch != '\0') {
462 pch++;
463 while (qemu_isspace(*pch)) {
464 pch++;
465 }
466 }
467 }
468
469 static int64_t expr_sum(MonitorHMP *mon);
470
471 static int64_t expr_unary(MonitorHMP *mon)
472 {
473 int64_t n;
474 char *p;
475
476 switch (*pch) {
477 case '+':
478 next();
479 n = expr_unary(mon);
480 break;
481 case '-':
482 next();
483 n = -expr_unary(mon);
484 break;
485 case '~':
486 next();
487 n = ~expr_unary(mon);
488 break;
489 case '(':
490 next();
491 n = expr_sum(mon);
492 if (*pch != ')') {
493 expr_error(mon, "')' expected");
494 }
495 next();
496 break;
497 case '\'':
498 pch++;
499 if (*pch == '\0') {
500 expr_error(mon, "character constant expected");
501 }
502 n = *pch;
503 pch++;
504 if (*pch != '\'') {
505 expr_error(mon, "missing terminating \' character");
506 }
507 next();
508 break;
509 case '$':
510 {
511 char buf[128], *q;
512 int64_t reg = 0;
513
514 pch++;
515 q = buf;
516 while ((*pch >= 'a' && *pch <= 'z') ||
517 (*pch >= 'A' && *pch <= 'Z') ||
518 (*pch >= '0' && *pch <= '9') ||
519 *pch == '_' || *pch == '.') {
520 if ((q - buf) < sizeof(buf) - 1) {
521 *q++ = *pch;
522 }
523 pch++;
524 }
525 while (qemu_isspace(*pch)) {
526 pch++;
527 }
528 *q = 0;
529 if (!gdb_get_register(mon, &reg, buf)
530 && get_monitor_def(mon, &reg, buf) < 0) {
531 expr_error(mon, "unknown register");
532 }
533 n = reg;
534 }
535 break;
536 case '\0':
537 expr_error(mon, "unexpected end of expression");
538 n = 0;
539 break;
540 default:
541 errno = 0;
542 n = strtoull(pch, &p, 0);
543 if (errno == ERANGE) {
544 expr_error(mon, "number too large");
545 }
546 if (pch == p) {
547 expr_error(mon, "invalid char '%c' in expression", *p);
548 }
549 pch = p;
550 while (qemu_isspace(*pch)) {
551 pch++;
552 }
553 break;
554 }
555 return n;
556 }
557
558 static int64_t expr_prod(MonitorHMP *mon)
559 {
560 int64_t val, val2;
561 int op;
562
563 val = expr_unary(mon);
564 for (;;) {
565 op = *pch;
566 if (op != '*' && op != '/' && op != '%') {
567 break;
568 }
569 next();
570 val2 = expr_unary(mon);
571 switch (op) {
572 default:
573 case '*':
574 val *= val2;
575 break;
576 case '/':
577 case '%':
578 if (val2 == 0) {
579 expr_error(mon, "division by zero");
580 }
581 if (op == '/') {
582 val /= val2;
583 } else {
584 val %= val2;
585 }
586 break;
587 }
588 }
589 return val;
590 }
591
592 static int64_t expr_logic(MonitorHMP *mon)
593 {
594 int64_t val, val2;
595 int op;
596
597 val = expr_prod(mon);
598 for (;;) {
599 op = *pch;
600 if (op != '&' && op != '|' && op != '^') {
601 break;
602 }
603 next();
604 val2 = expr_prod(mon);
605 switch (op) {
606 default:
607 case '&':
608 val &= val2;
609 break;
610 case '|':
611 val |= val2;
612 break;
613 case '^':
614 val ^= val2;
615 break;
616 }
617 }
618 return val;
619 }
620
621 static int64_t expr_sum(MonitorHMP *mon)
622 {
623 int64_t val, val2;
624 int op;
625
626 val = expr_logic(mon);
627 for (;;) {
628 op = *pch;
629 if (op != '+' && op != '-') {
630 break;
631 }
632 next();
633 val2 = expr_logic(mon);
634 if (op == '+') {
635 val += val2;
636 } else {
637 val -= val2;
638 }
639 }
640 return val;
641 }
642
643 static int get_expr(MonitorHMP *mon, int64_t *pval, const char **pp)
644 {
645 pch = *pp;
646 if (sigsetjmp(expr_env, 0)) {
647 *pp = pch;
648 return -1;
649 }
650 while (qemu_isspace(*pch)) {
651 pch++;
652 }
653 *pval = expr_sum(mon);
654 *pp = pch;
655 return 0;
656 }
657
658 static int get_double(MonitorHMP *mon, double *pval, const char **pp)
659 {
660 const char *p = *pp;
661 char *tailp;
662 double d;
663
664 d = strtod(p, &tailp);
665 if (tailp == p) {
666 monitor_hmp_printf(mon, "Number expected\n");
667 return -1;
668 }
669 if (d != d || d - d != 0) {
670 /* NaN or infinity */
671 monitor_hmp_printf(mon, "Bad number\n");
672 return -1;
673 }
674 *pval = d;
675 *pp = tailp;
676 return 0;
677 }
678
679 /*
680 * Store the command-name in cmdname, and return a pointer to
681 * the remaining of the command string.
682 */
683 static const char *get_command_name(const char *cmdline,
684 char *cmdname, size_t nlen)
685 {
686 size_t len;
687 const char *p, *pstart;
688
689 p = cmdline;
690 while (qemu_isspace(*p)) {
691 p++;
692 }
693 if (*p == '\0') {
694 return NULL;
695 }
696 pstart = p;
697 while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) {
698 p++;
699 }
700 len = p - pstart;
701 if (len > nlen - 1) {
702 len = nlen - 1;
703 }
704 memcpy(cmdname, pstart, len);
705 cmdname[len] = '\0';
706 return p;
707 }
708
709 /**
710 * Read key of 'type' into 'key' and return the current
711 * 'type' pointer.
712 */
713 static const char *key_get_info(const char *type, char **key)
714 {
715 size_t len;
716 const char *p;
717 char *str;
718
719 if (*type == ',') {
720 type++;
721 }
722
723 p = strchr(type, ':');
724 if (!p) {
725 *key = NULL;
726 return NULL;
727 }
728 len = p - type;
729
730 str = g_malloc(len + 1);
731 memcpy(str, type, len);
732 str[len] = '\0';
733
734 *key = str;
735 return ++p;
736 }
737
738 static int default_fmt_format = 'x';
739 static int default_fmt_size = 4;
740
741 static int is_valid_option(const char *c, const char *typestr)
742 {
743 char option[3];
744
745 option[0] = '-';
746 option[1] = *c;
747 option[2] = '\0';
748
749 typestr = strstr(typestr, option);
750 return (typestr != NULL);
751 }
752
753 static const HMPCommand *search_dispatch_table(const HMPCommand *disp_table,
754 const char *cmdname)
755 {
756 const HMPCommand *cmd;
757
758 for (cmd = disp_table; cmd->name != NULL; cmd++) {
759 if (hmp_compare_cmd(cmdname, cmd->name)) {
760 return cmd;
761 }
762 }
763
764 return NULL;
765 }
766
767 /*
768 * Parse command name from @cmdp according to command table @table.
769 * If blank, return NULL.
770 * Else, if no valid command can be found, report to @mon, and return
771 * NULL.
772 * Else, change @cmdp to point right behind the name, and return its
773 * command table entry.
774 * Do not assume the return value points into @table! It doesn't when
775 * the command is found in a sub-command table.
776 */
777 static const HMPCommand *monitor_parse_command(MonitorHMP *hmp,
778 const char *cmdp_start,
779 const char **cmdp,
780 HMPCommand *table)
781 {
782 const char *p;
783 const HMPCommand *cmd;
784 char cmdname[256];
785
786 /* extract the command name */
787 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
788 if (!p) {
789 return NULL;
790 }
791
792 cmd = search_dispatch_table(table, cmdname);
793 if (!cmd) {
794 monitor_hmp_printf(hmp, "unknown command: '%.*s'\n",
795 (int)(p - cmdp_start), cmdp_start);
796 return NULL;
797 }
798 if (!cmd_available(cmd)) {
799 monitor_hmp_printf(hmp, "Command '%.*s' not available "
800 "until machine initialization has completed.\n",
801 (int)(p - cmdp_start), cmdp_start);
802 return NULL;
803 }
804
805 /* filter out following useless space */
806 while (qemu_isspace(*p)) {
807 p++;
808 }
809
810 *cmdp = p;
811 /* search sub command */
812 if (cmd->sub_table != NULL && *p != '\0') {
813 return monitor_parse_command(hmp, cmdp_start, cmdp, cmd->sub_table);
814 }
815
816 return cmd;
817 }
818
819 /*
820 * Parse arguments for @cmd.
821 * If it can't be parsed, report to @mon, and return NULL.
822 * Else, insert command arguments into a QDict, and return it.
823 * Note: On success, caller has to free the QDict structure.
824 */
825 static QDict *monitor_parse_arguments(MonitorHMP *mon,
826 const char **endp,
827 const HMPCommand *cmd)
828 {
829 const char *typestr;
830 char *key;
831 int c;
832 const char *p = *endp;
833 char buf[1024];
834 QDict *qdict = qdict_new();
835
836 /* parse the parameters */
837 typestr = cmd->args_type;
838 for (;;) {
839 typestr = key_get_info(typestr, &key);
840 if (!typestr) {
841 break;
842 }
843 c = *typestr;
844 typestr++;
845 switch (c) {
846 case 'F':
847 case 'B':
848 case 's':
849 {
850 int ret;
851
852 while (qemu_isspace(*p)) {
853 p++;
854 }
855 if (*typestr == '?') {
856 typestr++;
857 if (*p == '\0') {
858 /* no optional string: NULL argument */
859 break;
860 }
861 }
862 ret = get_str(buf, sizeof(buf), &p);
863 if (ret < 0) {
864 switch (c) {
865 case 'F':
866 monitor_hmp_printf(mon, "%s: filename expected\n",
867 cmd->name);
868 break;
869 case 'B':
870 monitor_hmp_printf(mon, "%s: block device name expected\n",
871 cmd->name);
872 break;
873 default:
874 monitor_hmp_printf(mon, "%s: string expected\n", cmd->name);
875 break;
876 }
877 goto fail;
878 }
879 qdict_put_str(qdict, key, buf);
880 }
881 break;
882 case 'O':
883 {
884 QemuOptsList *opts_list;
885 QemuOpts *opts;
886
887 opts_list = qemu_find_opts(key);
888 if (!opts_list || opts_list->desc->name) {
889 goto bad_type;
890 }
891 while (qemu_isspace(*p)) {
892 p++;
893 }
894 if (!*p) {
895 break;
896 }
897 if (get_str(buf, sizeof(buf), &p) < 0) {
898 goto fail;
899 }
900 opts = qemu_opts_parse_noisily(opts_list, buf, true);
901 if (!opts) {
902 goto fail;
903 }
904 qemu_opts_to_qdict(opts, qdict);
905 qemu_opts_del(opts);
906 }
907 break;
908 case '/':
909 {
910 int count, format, size;
911
912 while (qemu_isspace(*p)) {
913 p++;
914 }
915 if (*p == '/') {
916 /* format found */
917 p++;
918 count = 1;
919 if (qemu_isdigit(*p)) {
920 count = 0;
921 while (qemu_isdigit(*p)) {
922 count = count * 10 + (*p - '0');
923 p++;
924 }
925 }
926 size = -1;
927 format = -1;
928 for (;;) {
929 switch (*p) {
930 case 'o':
931 case 'd':
932 case 'u':
933 case 'x':
934 case 'i':
935 case 'c':
936 format = *p++;
937 break;
938 case 'b':
939 size = 1;
940 p++;
941 break;
942 case 'h':
943 size = 2;
944 p++;
945 break;
946 case 'w':
947 size = 4;
948 p++;
949 break;
950 case 'g':
951 case 'L':
952 size = 8;
953 p++;
954 break;
955 default:
956 goto next;
957 }
958 }
959 next:
960 if (*p != '\0' && !qemu_isspace(*p)) {
961 monitor_hmp_printf(mon, "invalid char in format: '%c'\n",
962 *p);
963 goto fail;
964 }
965 if (format < 0) {
966 format = default_fmt_format;
967 }
968 if (format != 'i') {
969 /* for 'i', not specifying a size gives -1 as size */
970 if (size < 0) {
971 size = default_fmt_size;
972 }
973 default_fmt_size = size;
974 }
975 default_fmt_format = format;
976 } else {
977 count = 1;
978 format = default_fmt_format;
979 if (format != 'i') {
980 size = default_fmt_size;
981 } else {
982 size = -1;
983 }
984 }
985 qdict_put_int(qdict, "count", count);
986 qdict_put_int(qdict, "format", format);
987 qdict_put_int(qdict, "size", size);
988 }
989 break;
990 case 'i':
991 case 'l':
992 case 'M':
993 {
994 int64_t val;
995
996 while (qemu_isspace(*p)) {
997 p++;
998 }
999 if (*typestr == '?' || *typestr == '.') {
1000 if (*typestr == '?') {
1001 if (*p == '\0') {
1002 typestr++;
1003 break;
1004 }
1005 } else {
1006 if (*p == '.') {
1007 p++;
1008 while (qemu_isspace(*p)) {
1009 p++;
1010 }
1011 } else {
1012 typestr++;
1013 break;
1014 }
1015 }
1016 typestr++;
1017 }
1018 if (get_expr(mon, &val, &p)) {
1019 goto fail;
1020 }
1021 /* Check if 'i' is greater than 32-bit */
1022 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
1023 monitor_hmp_printf(mon, "\'%s\' has failed: ", cmd->name);
1024 monitor_hmp_printf(mon, "integer is for 32-bit values\n");
1025 goto fail;
1026 } else if (c == 'M') {
1027 if (val < 0) {
1028 monitor_hmp_printf(mon, "enter a positive value\n");
1029 goto fail;
1030 }
1031 val *= MiB;
1032 }
1033 qdict_put_int(qdict, key, val);
1034 }
1035 break;
1036 case 'o':
1037 {
1038 int ret;
1039 uint64_t val;
1040 const char *end;
1041
1042 while (qemu_isspace(*p)) {
1043 p++;
1044 }
1045 if (*typestr == '?') {
1046 typestr++;
1047 if (*p == '\0') {
1048 break;
1049 }
1050 }
1051 ret = qemu_strtosz_MiB(p, &end, &val);
1052 if (ret < 0 || val > INT64_MAX) {
1053 monitor_hmp_printf(mon, "invalid size\n");
1054 goto fail;
1055 }
1056 qdict_put_int(qdict, key, val);
1057 p = end;
1058 }
1059 break;
1060 case 'T':
1061 {
1062 double val;
1063
1064 while (qemu_isspace(*p)) {
1065 p++;
1066 }
1067 if (*typestr == '?') {
1068 typestr++;
1069 if (*p == '\0') {
1070 break;
1071 }
1072 }
1073 if (get_double(mon, &val, &p) < 0) {
1074 goto fail;
1075 }
1076 if (p[0] && p[1] == 's') {
1077 switch (*p) {
1078 case 'm':
1079 val /= 1e3; p += 2; break;
1080 case 'u':
1081 val /= 1e6; p += 2; break;
1082 case 'n':
1083 val /= 1e9; p += 2; break;
1084 }
1085 }
1086 if (*p && !qemu_isspace(*p)) {
1087 monitor_hmp_printf(mon, "Unknown unit suffix\n");
1088 goto fail;
1089 }
1090 qdict_put(qdict, key, qnum_from_double(val));
1091 }
1092 break;
1093 case 'b':
1094 {
1095 const char *beg;
1096 bool val;
1097
1098 while (qemu_isspace(*p)) {
1099 p++;
1100 }
1101 beg = p;
1102 while (qemu_isgraph(*p)) {
1103 p++;
1104 }
1105 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
1106 val = true;
1107 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
1108 val = false;
1109 } else {
1110 monitor_hmp_printf(mon, "Expected 'on' or 'off'\n");
1111 goto fail;
1112 }
1113 qdict_put_bool(qdict, key, val);
1114 }
1115 break;
1116 case '-':
1117 {
1118 const char *tmp = p;
1119 int skip_key = 0;
1120 int ret;
1121 /* option */
1122
1123 c = *typestr++;
1124 if (c == '\0') {
1125 goto bad_type;
1126 }
1127 while (qemu_isspace(*p)) {
1128 p++;
1129 }
1130 if (*p == '-') {
1131 p++;
1132 if (c != *p) {
1133 if (!is_valid_option(p, typestr)) {
1134 monitor_hmp_printf(mon, "%s: unsupported option -%c\n",
1135 cmd->name, *p);
1136 goto fail;
1137 } else {
1138 skip_key = 1;
1139 }
1140 }
1141 if (skip_key) {
1142 p = tmp;
1143 } else if (*typestr == 's') {
1144 /* has option with string value */
1145 typestr++;
1146 tmp = p++;
1147 while (qemu_isspace(*p)) {
1148 p++;
1149 }
1150 ret = get_str(buf, sizeof(buf), &p);
1151 if (ret < 0) {
1152 monitor_hmp_printf(mon, "%s: value expected for -%c\n",
1153 cmd->name, *tmp);
1154 goto fail;
1155 }
1156 qdict_put_str(qdict, key, buf);
1157 } else {
1158 /* has boolean option */
1159 p++;
1160 qdict_put_bool(qdict, key, true);
1161 }
1162 } else if (*typestr == 's') {
1163 typestr++;
1164 }
1165 }
1166 break;
1167 case 'S':
1168 {
1169 /* package all remaining string */
1170 int len;
1171
1172 while (qemu_isspace(*p)) {
1173 p++;
1174 }
1175 if (*typestr == '?') {
1176 typestr++;
1177 if (*p == '\0') {
1178 /* no remaining string: NULL argument */
1179 break;
1180 }
1181 }
1182 len = strlen(p);
1183 if (len <= 0) {
1184 monitor_hmp_printf(mon, "%s: string expected\n",
1185 cmd->name);
1186 goto fail;
1187 }
1188 qdict_put_str(qdict, key, p);
1189 p += len;
1190 }
1191 break;
1192 default:
1193 bad_type:
1194 monitor_hmp_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
1195 goto fail;
1196 }
1197 g_free(key);
1198 key = NULL;
1199 }
1200 /* check that all arguments were parsed */
1201 while (qemu_isspace(*p)) {
1202 p++;
1203 }
1204 if (*p != '\0') {
1205 monitor_hmp_printf(mon, "%s: extraneous characters at the end of line\n",
1206 cmd->name);
1207 goto fail;
1208 }
1209
1210 return qdict;
1211
1212 fail:
1213 qobject_unref(qdict);
1214 g_free(key);
1215 return NULL;
1216 }
1217
1218 static void hmp_info_human_readable_text(MonitorHMP *hmp,
1219 HumanReadableText *(*handler)(Error **))
1220 {
1221 Error *err = NULL;
1222 g_autoptr(HumanReadableText) info = handler(&err);
1223
1224 if (hmp_handle_error(hmp, err)) {
1225 return;
1226 }
1227
1228 monitor_puts(MONITOR(hmp), info->human_readable_text);
1229 }
1230
1231 static void handle_hmp_command_exec(MonitorHMP *hmp,
1232 const HMPCommand *cmd,
1233 QDict *qdict)
1234 {
1235 if (cmd->cmd_info_hrt) {
1236 hmp_info_human_readable_text(hmp,
1237 cmd->cmd_info_hrt);
1238 } else {
1239 cmd->cmd(hmp, qdict);
1240 }
1241 }
1242
1243 typedef struct HandleHmpCommandCo {
1244 MonitorHMP *mon;
1245 const HMPCommand *cmd;
1246 QDict *qdict;
1247 bool done;
1248 } HandleHmpCommandCo;
1249
1250 static void handle_hmp_command_co(void *opaque)
1251 {
1252 HandleHmpCommandCo *data = opaque;
1253 handle_hmp_command_exec(data->mon, data->cmd, data->qdict);
1254 monitor_set_cur(qemu_coroutine_self(), NULL);
1255 data->done = true;
1256 }
1257
1258 void handle_hmp_command(MonitorHMP *hmp, const char *cmdline)
1259 {
1260 QDict *qdict;
1261 const HMPCommand *cmd;
1262 const char *cmd_start = cmdline;
1263
1264 trace_handle_hmp_command(hmp, cmdline);
1265
1266 cmd = monitor_parse_command(hmp, cmdline, &cmdline,
1267 hmp_cmds_for_target(false));
1268 if (!cmd) {
1269 return;
1270 }
1271
1272 if (!cmd->cmd && !cmd->cmd_info_hrt) {
1273 /* FIXME: is it useful to try autoload modules here ??? */
1274 monitor_hmp_printf(hmp, "Command \"%.*s\" is not available.\n",
1275 (int)(cmdline - cmd_start), cmd_start);
1276 return;
1277 }
1278
1279 qdict = monitor_parse_arguments(hmp, &cmdline, cmd);
1280 if (!qdict) {
1281 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
1282 cmdline--;
1283 }
1284 monitor_hmp_printf(hmp,
1285 "Try \"help %.*s\" for more information\n",
1286 (int)(cmdline - cmd_start), cmd_start);
1287 return;
1288 }
1289
1290 if (!cmd->coroutine) {
1291 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1292 Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(), MONITOR(hmp));
1293 handle_hmp_command_exec(hmp, cmd, qdict);
1294 monitor_set_cur(qemu_coroutine_self(), old_mon);
1295 } else {
1296 HandleHmpCommandCo data = {
1297 .mon = hmp,
1298 .cmd = cmd,
1299 .qdict = qdict,
1300 .done = false,
1301 };
1302 Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data);
1303 monitor_set_cur(co, MONITOR(hmp));
1304 aio_co_enter(qemu_get_aio_context(), co);
1305 AIO_WAIT_WHILE_UNLOCKED(NULL, !data.done);
1306 }
1307
1308 qobject_unref(qdict);
1309 }
1310
1311 static void cmd_completion(MonitorHMP *hmp,
1312 const char *name, const char *list)
1313 {
1314 const char *p, *pstart;
1315 char cmd[128];
1316 int len;
1317
1318 p = list;
1319 for (;;) {
1320 pstart = p;
1321 p = qemu_strchrnul(p, '|');
1322 len = p - pstart;
1323 if (len > sizeof(cmd) - 2) {
1324 len = sizeof(cmd) - 2;
1325 }
1326 memcpy(cmd, pstart, len);
1327 cmd[len] = '\0';
1328 readline_add_completion_of(hmp->rs, name, cmd);
1329 if (*p == '\0') {
1330 break;
1331 }
1332 p++;
1333 }
1334 }
1335
1336 static void file_completion(MonitorHMP *hmp, const char *input)
1337 {
1338 DIR *ffs;
1339 struct dirent *d;
1340 char path[1024];
1341 char file[1024], file_prefix[1024];
1342 int input_path_len;
1343 const char *p;
1344
1345 p = strrchr(input, '/');
1346 if (!p) {
1347 input_path_len = 0;
1348 pstrcpy(file_prefix, sizeof(file_prefix), input);
1349 pstrcpy(path, sizeof(path), ".");
1350 } else {
1351 input_path_len = p - input + 1;
1352 memcpy(path, input, input_path_len);
1353 if (input_path_len > sizeof(path) - 1) {
1354 input_path_len = sizeof(path) - 1;
1355 }
1356 path[input_path_len] = '\0';
1357 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1358 }
1359
1360 ffs = opendir(path);
1361 if (!ffs) {
1362 return;
1363 }
1364 for (;;) {
1365 struct stat sb;
1366 d = readdir(ffs);
1367 if (!d) {
1368 break;
1369 }
1370
1371 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
1372 continue;
1373 }
1374
1375 if (strstart(d->d_name, file_prefix, NULL)) {
1376 memcpy(file, input, input_path_len);
1377 if (input_path_len < sizeof(file)) {
1378 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
1379 d->d_name);
1380 }
1381 /*
1382 * stat the file to find out if it's a directory.
1383 * In that case add a slash to speed up typing long paths
1384 */
1385 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
1386 pstrcat(file, sizeof(file), "/");
1387 }
1388 readline_add_completion(hmp->rs, file);
1389 }
1390 }
1391 closedir(ffs);
1392 }
1393
1394 static const char *next_arg_type(const char *typestr)
1395 {
1396 const char *p = strchr(typestr, ':');
1397 return (p != NULL ? ++p : typestr);
1398 }
1399
1400 static void monitor_find_completion_by_table(MonitorHMP *hmp,
1401 const HMPCommand *cmd_table,
1402 char **args,
1403 int nb_args)
1404 {
1405 const char *cmdname;
1406 int i;
1407 const char *ptype, *old_ptype, *str;
1408 const HMPCommand *cmd;
1409 BlockBackend *blk = NULL;
1410
1411 if (nb_args <= 1) {
1412 /* command completion */
1413 if (nb_args == 0) {
1414 cmdname = "";
1415 } else {
1416 cmdname = args[0];
1417 }
1418 readline_set_completion_index(hmp->rs, strlen(cmdname));
1419 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
1420 if (cmd_available(cmd)) {
1421 cmd_completion(hmp, cmdname, cmd->name);
1422 }
1423 }
1424 } else {
1425 /* find the command */
1426 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
1427 if (hmp_compare_cmd(args[0], cmd->name) &&
1428 cmd_available(cmd)) {
1429 break;
1430 }
1431 }
1432 if (!cmd->name) {
1433 return;
1434 }
1435
1436 if (cmd->sub_table) {
1437 /* do the job again */
1438 monitor_find_completion_by_table(hmp, cmd->sub_table,
1439 &args[1], nb_args - 1);
1440 return;
1441 }
1442 if (cmd->command_completion) {
1443 cmd->command_completion(hmp->rs, nb_args, args[nb_args - 1]);
1444 return;
1445 }
1446
1447 ptype = next_arg_type(cmd->args_type);
1448 for (i = 0; i < nb_args - 2; i++) {
1449 if (*ptype != '\0') {
1450 ptype = next_arg_type(ptype);
1451 while (*ptype == '?') {
1452 ptype = next_arg_type(ptype);
1453 }
1454 }
1455 }
1456 str = args[nb_args - 1];
1457 old_ptype = NULL;
1458 while (*ptype == '-' && old_ptype != ptype) {
1459 old_ptype = ptype;
1460 ptype = next_arg_type(ptype);
1461 }
1462 switch (*ptype) {
1463 case 'F':
1464 /* file completion */
1465 readline_set_completion_index(hmp->rs, strlen(str));
1466 file_completion(hmp, str);
1467 break;
1468 case 'B':
1469 /* block device name completion */
1470 readline_set_completion_index(hmp->rs, strlen(str));
1471 while ((blk = blk_next(blk)) != NULL) {
1472 readline_add_completion_of(hmp->rs, str, blk_name(blk));
1473 }
1474 break;
1475 case 's':
1476 case 'S':
1477 if (!strcmp(cmd->name, "help|?")) {
1478 monitor_find_completion_by_table(hmp, cmd_table,
1479 &args[1], nb_args - 1);
1480 }
1481 break;
1482 default:
1483 break;
1484 }
1485 }
1486 }
1487
1488 static void monitor_find_completion(void *opaque,
1489 const char *cmdline)
1490 {
1491 MonitorHMP *hmp = opaque;
1492 char *args[MAX_ARGS];
1493 int nb_args, len;
1494
1495 /* 1. parse the cmdline */
1496 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
1497 return;
1498 }
1499
1500 /*
1501 * if the line ends with a space, it means we want to complete the
1502 * next arg
1503 */
1504 len = strlen(cmdline);
1505 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
1506 if (nb_args >= MAX_ARGS) {
1507 goto cleanup;
1508 }
1509 args[nb_args++] = g_strdup("");
1510 }
1511
1512 /* 2. auto complete according to args */
1513 monitor_find_completion_by_table(hmp, hmp_cmds_for_target(false),
1514 args, nb_args);
1515
1516 cleanup:
1517 free_cmdline_args(args, nb_args);
1518 }
1519
1520 static void monitor_read(void *opaque, const uint8_t *buf, int size)
1521 {
1522 Monitor *mon = opaque;
1523 MonitorHMP *hmp = MONITOR_HMP(mon);
1524 int i;
1525
1526 if (hmp->rs) {
1527 for (i = 0; i < size; i++) {
1528 readline_handle_byte(hmp->rs, buf[i]);
1529 }
1530 } else {
1531 if (size == 0 || buf[size - 1] != 0) {
1532 monitor_hmp_printf(hmp, "corrupted command\n");
1533 } else {
1534 handle_hmp_command(hmp, (char *)buf);
1535 }
1536 }
1537 }
1538
1539 static void monitor_event(void *opaque, QEMUChrEvent event)
1540 {
1541 Monitor *mon = opaque;
1542 MonitorHMP *hmp = MONITOR_HMP(mon);
1543
1544 switch (event) {
1545 case CHR_EVENT_MUX_IN:
1546 qemu_mutex_lock(&mon->mon_lock);
1547 if (mon->mux_out) {
1548 mon->mux_out = 0;
1549 if (hmp->use_readline) {
1550 monitor_resume(mon);
1551 }
1552 }
1553 qemu_mutex_unlock(&mon->mon_lock);
1554 break;
1555
1556 case CHR_EVENT_MUX_OUT:
1557 qemu_mutex_lock(&mon->mon_lock);
1558 if (!mon->mux_out) {
1559 if (hmp->reset_seen && !mon->suspend_cnt) {
1560 monitor_puts_locked(mon, "\n");
1561 } else {
1562 monitor_flush_locked(mon);
1563 }
1564 if (hmp->use_readline) {
1565 monitor_suspend(mon);
1566 }
1567 mon->mux_out = 1;
1568 }
1569 qemu_mutex_unlock(&mon->mon_lock);
1570 break;
1571
1572 case CHR_EVENT_OPENED:
1573 monitor_hmp_printf(hmp, "QEMU %s monitor - type 'help' for more "
1574 "information\n", QEMU_VERSION);
1575 qemu_mutex_lock(&mon->mon_lock);
1576 hmp->reset_seen = 1;
1577 if (!mon->mux_out && hmp->use_readline) {
1578 /* Suspend-resume forces the prompt to be printed. */
1579 monitor_suspend(mon);
1580 monitor_resume(mon);
1581 }
1582 qemu_mutex_unlock(&mon->mon_lock);
1583 break;
1584
1585 case CHR_EVENT_CLOSED:
1586 monitor_fdsets_cleanup();
1587 break;
1588
1589 case CHR_EVENT_BREAK:
1590 /* Ignored */
1591 break;
1592 }
1593 }
1594
1595
1596 /*
1597 * These functions just adapt the readline interface in a typesafe way. We
1598 * could cast function pointers but that discards compiler checks.
1599 */
1600 static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque,
1601 const char *fmt, ...)
1602 {
1603 MonitorHMP *hmp = opaque;
1604 va_list ap;
1605 va_start(ap, fmt);
1606 monitor_hmp_vprintf(hmp, fmt, ap);
1607 va_end(ap);
1608 }
1609
1610 static void monitor_readline_flush(void *opaque)
1611 {
1612 MonitorHMP *hmp = opaque;
1613 monitor_flush(&hmp->parent_obj);
1614 }
1615
1616 void monitor_new_hmp(const char *id, const char *chardev_id,
1617 bool use_readline, Error **errp)
1618 {
1619 g_autofree char *autoid = id ? NULL : monitor_compat_id();
1620 object_new_with_props(TYPE_MONITOR_HMP,
1621 object_get_objects_root(),
1622 id ? id : autoid,
1623 errp,
1624 "chardev", chardev_id,
1625 "readline", use_readline ? "yes" : "no",
1626 NULL);
1627 }
1628
1629 static void monitor_hmp_complete(UserCreatable *uc, Error **errp)
1630 {
1631 MonitorHMP *hmp = MONITOR_HMP(uc);
1632 UserCreatableClass *ucc_parent =
1633 USER_CREATABLE_CLASS(
1634 object_class_get_parent(
1635 OBJECT_CLASS(MONITOR_HMP_GET_CLASS(hmp))));
1636 ERRP_GUARD();
1637
1638 ucc_parent->complete(uc, errp);
1639 if (*errp) {
1640 return;
1641 }
1642
1643 if (hmp->parent_obj.chardev_id) {
1644 if (hmp->use_readline) {
1645 hmp->rs = readline_init(monitor_readline_printf,
1646 monitor_readline_flush,
1647 hmp,
1648 monitor_find_completion);
1649 monitor_hmp_read_command(hmp, 0);
1650 }
1651
1652 qemu_chr_fe_set_handlers(&hmp->parent_obj.chr,
1653 monitor_can_read,
1654 monitor_read,
1655 monitor_event, NULL,
1656 &hmp->parent_obj, NULL, true);
1657 monitor_list_append(&hmp->parent_obj);
1658 }
1659 }
1660
1661 static bool monitor_hmp_prepare_delete(UserCreatable *uc, Error **errp)
1662 {
1663 error_setg(errp, "Deleting HMP monitors is not supported");
1664 return false;
1665 }
1666
1667 /**
1668 * Is @name in the '|' separated list of names @list?
1669 */
1670 int hmp_compare_cmd(const char *name, const char *list)
1671 {
1672 const char *p, *pstart;
1673 int len;
1674 len = strlen(name);
1675 p = list;
1676 for (;;) {
1677 pstart = p;
1678 p = qemu_strchrnul(p, '|');
1679 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
1680 return 1;
1681 }
1682 if (*p == '\0') {
1683 break;
1684 }
1685 p++;
1686 }
1687 return 0;
1688 }
1689
1690 void monitor_register_hmp(const char *name, bool info,
1691 void (*cmd)(MonitorHMP *mon, const QDict *qdict))
1692 {
1693 HMPCommand *table = hmp_cmds_for_target(info);
1694
1695 while (table->name != NULL) {
1696 if (strcmp(table->name, name) == 0) {
1697 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1698 table->cmd = cmd;
1699 return;
1700 }
1701 table++;
1702 }
1703 g_assert_not_reached();
1704 }
1705
1706 void monitor_register_hmp_info_hrt(const char *name,
1707 HumanReadableText *(*handler)(Error **errp))
1708 {
1709 HMPCommand *table = hmp_cmds_for_target(true);
1710
1711 while (table->name != NULL) {
1712 if (strcmp(table->name, name) == 0) {
1713 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1714 table->cmd_info_hrt = handler;
1715 return;
1716 }
1717 table++;
1718 }
1719 g_assert_not_reached();
1720 }
1721
1722 /*
1723 * Set @pval to the value in the register identified by @name.
1724 * return 0 if OK, -1 if not found
1725 */
1726 static int get_monitor_def(MonitorHMP *hmp, int64_t *pval, const char *name)
1727 {
1728 CPUState *cs = monitor_hmp_get_cpu(hmp);
1729 const MonitorDef *md = NULL;
1730 void *ptr;
1731
1732 if (cs == NULL) {
1733 return -1;
1734 }
1735 md = cs->cc->sysemu_ops->monitor_defs;
1736 if (md == NULL) {
1737 return -1;
1738 }
1739
1740 for (; md->name != NULL; md++) {
1741 if (hmp_compare_cmd(name, md->name)) {
1742 if (md->get_value) {
1743 *pval = md->get_value(hmp, md, md->offset);
1744 } else {
1745 CPUArchState *env = monitor_hmp_get_cpu_env(hmp);
1746 ptr = (uint8_t *)env + md->offset;
1747 *pval = *(int32_t *)ptr;
1748 }
1749 return 0;
1750 }
1751 }
1752
1753 if (!cs->cc->sysemu_ops->monitor_get_register) {
1754 return -1;
1755 }
1756 return cs->cc->sysemu_ops->monitor_get_register(cs, name, pval);
1757 }
1758
1759 int monitor_hmp_vprintf(MonitorHMP *hmp, const char *fmt, va_list ap)
1760 {
1761 g_autofree char *buf = g_strdup_vprintf(fmt, ap);
1762
1763 if (!hmp) {
1764 return -1;
1765 }
1766
1767 return monitor_puts(MONITOR(hmp), buf);
1768 }
1769
1770 int monitor_hmp_printf(MonitorHMP *hmp, const char *fmt, ...)
1771 {
1772 int ret;
1773
1774 va_list ap;
1775 va_start(ap, fmt);
1776 ret = monitor_hmp_vprintf(hmp, fmt, ap);
1777 va_end(ap);
1778 return ret;
1779 }
1780
1781 void monitor_hmp_printc(MonitorHMP *hmp, int c)
1782 {
1783 monitor_hmp_printf(hmp, "'");
1784 switch (c) {
1785 case '\'':
1786 monitor_hmp_printf(hmp, "\\'");
1787 break;
1788 case '\\':
1789 monitor_hmp_printf(hmp, "\\\\");
1790 break;
1791 case '\n':
1792 monitor_hmp_printf(hmp, "\\n");
1793 break;
1794 case '\r':
1795 monitor_hmp_printf(hmp, "\\r");
1796 break;
1797 default:
1798 if (c >= 32 && c <= 126) {
1799 monitor_hmp_printf(hmp, "%c", c);
1800 } else {
1801 monitor_hmp_printf(hmp, "\\x%02x", c);
1802 }
1803 break;
1804 }
1805 monitor_hmp_printf(hmp, "'");
1806 }