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/qdev-core.h" 28 #include "monitor-internal.h" 29 #include "monitor/hmp.h" 30 #include "qobject/qdict.h" 31 #include "qobject/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 "system/block-backend.h" 39 #include "trace.h" 40 41 static void monitor_command_cb(void *opaque, const char *cmdline, 42 void *readline_opaque) 43 { 44 MonitorHMP *mon = opaque; 45 46 monitor_suspend(&mon->common); 47 handle_hmp_command(mon, cmdline); 48 monitor_resume(&mon->common); 49 } 50 51 void monitor_read_command(MonitorHMP *mon, int show_prompt) 52 { 53 if (!mon->rs) { 54 return; 55 } 56 57 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); 58 if (show_prompt) { 59 readline_show_prompt(mon->rs); 60 } 61 } 62 63 int monitor_read_password(MonitorHMP *mon, ReadLineFunc *readline_func, 64 void *opaque) 65 { 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; 74 } 75 } 76 77 static int get_str(char *buf, int buf_size, const char **pp) 78 { 79 const char *p; 80 char *q; 81 int c; 82 83 q = buf; 84 p = *pp; 85 while (qemu_isspace(*p)) { 86 p++; 87 } 88 if (*p == '\0') { 89 fail: 90 *q = '\0'; 91 *pp = p; 92 return -1; 93 } 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; 114 } 115 if ((q - buf) < buf_size - 1) { 116 *q++ = c; 117 } 118 } else { 119 if ((q - buf) < buf_size - 1) { 120 *q++ = *p; 121 } 122 p++; 123 } 124 } 125 if (*p != '\"') { 126 printf("unterminated string\n"); 127 goto fail; 128 } 129 p++; 130 } else { 131 while (*p != '\0' && !qemu_isspace(*p)) { 132 if ((q - buf) < buf_size - 1) { 133 *q++ = *p; 134 } 135 p++; 136 } 137 } 138 *q = '\0'; 139 *pp = p; 140 return 0; 141 } 142 143 #define MAX_ARGS 16 144 145 static void free_cmdline_args(char **args, int nb_args) 146 { 147 int i; 148 149 assert(nb_args <= MAX_ARGS); 150 151 for (i = 0; i < nb_args; i++) { 152 g_free(args[i]); 153 } 154 155 } 156 157 /* 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. 163 * 164 * Returns 0 on success, negative on failure. 165 * 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. 169 */ 170 static int parse_cmdline(const char *cmdline, 171 int *pnb_args, char **args) 172 { 173 const char *p; 174 int nb_args, ret; 175 char buf[1024]; 176 177 p = cmdline; 178 nb_args = 0; 179 for (;;) { 180 while (qemu_isspace(*p)) { 181 p++; 182 } 183 if (*p == '\0') { 184 break; 185 } 186 if (nb_args >= MAX_ARGS) { 187 goto fail; 188 } 189 ret = get_str(buf, sizeof(buf), &p); 190 if (ret < 0) { 191 goto fail; 192 } 193 args[nb_args] = g_strdup(buf); 194 nb_args++; 195 } 196 *pnb_args = nb_args; 197 return 0; 198 199 fail: 200 free_cmdline_args(args, nb_args); 201 return -1; 202 } 203 204 /* 205 * Can command @cmd be executed in preconfig state? 206 */ 207 static bool cmd_can_preconfig(const HMPCommand *cmd) 208 { 209 if (!cmd->flags) { 210 return false; 211 } 212 213 return strchr(cmd->flags, 'p'); 214 } 215 216 static bool cmd_available(const HMPCommand *cmd) 217 { 218 return phase_check(PHASE_MACHINE_READY) || cmd_can_preconfig(cmd); 219 } 220 221 static void help_cmd_dump_one(Monitor *mon, 222 const HMPCommand *cmd, 223 char **prefix_args, 224 int prefix_args_nb) 225 { 226 int i; 227 228 if (!cmd_available(cmd)) { 229 return; 230 } 231 232 for (i = 0; i < prefix_args_nb; i++) { 233 monitor_printf(mon, "%s ", prefix_args[i]); 234 } 235 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help); 236 } 237 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) 241 { 242 const HMPCommand *cmd; 243 size_t i; 244 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); 249 } 250 return; 251 } 252 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); 263 } 264 return; 265 } 266 } 267 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" : " "); 272 } 273 } 274 275 void hmp_help_cmd(Monitor *mon, const char *name) 276 { 277 char *args[MAX_ARGS]; 278 int nb_args = 0; 279 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); 289 } 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; 296 } 297 298 if (parse_cmdline(name, &nb_args, args) < 0) { 299 return; 300 } 301 } 302 303 /* 2. dump the contents according to parsed args */ 304 help_cmd_dump(mon, hmp_cmds, args, nb_args, 0); 305 306 free_cmdline_args(args, nb_args); 307 } 308 309 /*******************************************************************/ 310 311 static const char *pch; 312 static sigjmp_buf expr_env; 313 314 static G_NORETURN G_GNUC_PRINTF(2, 3) 315 void expr_error(Monitor *mon, const char *fmt, ...) 316 { 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); 323 } 324 325 static void next(void) 326 { 327 if (*pch != '\0') { 328 pch++; 329 while (qemu_isspace(*pch)) { 330 pch++; 331 } 332 } 333 } 334 335 static int64_t expr_sum(Monitor *mon); 336 337 static int64_t expr_unary(Monitor *mon) 338 { 339 int64_t n; 340 char *p; 341 int ret; 342 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"); 361 } 362 next(); 363 break; 364 case '\'': 365 pch++; 366 if (*pch == '\0') { 367 expr_error(mon, "character constant expected"); 368 } 369 n = *pch; 370 pch++; 371 if (*pch != '\'') { 372 expr_error(mon, "missing terminating \' character"); 373 } 374 next(); 375 break; 376 case '$': 377 { 378 char buf[128], *q; 379 int64_t reg = 0; 380 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; 389 } 390 pch++; 391 } 392 while (qemu_isspace(*pch)) { 393 pch++; 394 } 395 *q = 0; 396 ret = get_monitor_def(mon, ®, buf); 397 if (ret < 0) { 398 expr_error(mon, "unknown register"); 399 } 400 n = reg; 401 } 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"); 412 } 413 if (pch == p) { 414 expr_error(mon, "invalid char '%c' in expression", *p); 415 } 416 pch = p; 417 while (qemu_isspace(*pch)) { 418 pch++; 419 } 420 break; 421 } 422 return n; 423 } 424 425 static int64_t expr_prod(Monitor *mon) 426 { 427 int64_t val, val2; 428 int op; 429 430 val = expr_unary(mon); 431 for (;;) { 432 op = *pch; 433 if (op != '*' && op != '/' && op != '%') { 434 break; 435 } 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"); 447 } 448 if (op == '/') { 449 val /= val2; 450 } else { 451 val %= val2; 452 } 453 break; 454 } 455 } 456 return val; 457 } 458 459 static int64_t expr_logic(Monitor *mon) 460 { 461 int64_t val, val2; 462 int op; 463 464 val = expr_prod(mon); 465 for (;;) { 466 op = *pch; 467 if (op != '&' && op != '|' && op != '^') { 468 break; 469 } 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; 483 } 484 } 485 return val; 486 } 487 488 static int64_t expr_sum(Monitor *mon) 489 { 490 int64_t val, val2; 491 int op; 492 493 val = expr_logic(mon); 494 for (;;) { 495 op = *pch; 496 if (op != '+' && op != '-') { 497 break; 498 } 499 next(); 500 val2 = expr_logic(mon); 501 if (op == '+') { 502 val += val2; 503 } else { 504 val -= val2; 505 } 506 } 507 return val; 508 } 509 510 static int get_expr(Monitor *mon, int64_t *pval, const char **pp) 511 { 512 pch = *pp; 513 if (sigsetjmp(expr_env, 0)) { 514 *pp = pch; 515 return -1; 516 } 517 while (qemu_isspace(*pch)) { 518 pch++; 519 } 520 *pval = expr_sum(mon); 521 *pp = pch; 522 return 0; 523 } 524 525 static int get_double(Monitor *mon, double *pval, const char **pp) 526 { 527 const char *p = *pp; 528 char *tailp; 529 double d; 530 531 d = strtod(p, &tailp); 532 if (tailp == p) { 533 monitor_printf(mon, "Number expected\n"); 534 return -1; 535 } 536 if (d != d || d - d != 0) { 537 /* NaN or infinity */ 538 monitor_printf(mon, "Bad number\n"); 539 return -1; 540 } 541 *pval = d; 542 *pp = tailp; 543 return 0; 544 } 545 546 /* 547 * Store the command-name in cmdname, and return a pointer to 548 * the remaining of the command string. 549 */ 550 static const char *get_command_name(const char *cmdline, 551 char *cmdname, size_t nlen) 552 { 553 size_t len; 554 const char *p, *pstart; 555 556 p = cmdline; 557 while (qemu_isspace(*p)) { 558 p++; 559 } 560 if (*p == '\0') { 561 return NULL; 562 } 563 pstart = p; 564 while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) { 565 p++; 566 } 567 len = p - pstart; 568 if (len > nlen - 1) { 569 len = nlen - 1; 570 } 571 memcpy(cmdname, pstart, len); 572 cmdname[len] = '\0'; 573 return p; 574 } 575 576 /** 577 * Read key of 'type' into 'key' and return the current 578 * 'type' pointer. 579 */ 580 static const char *key_get_info(const char *type, char **key) 581 { 582 size_t len; 583 const char *p; 584 char *str; 585 586 if (*type == ',') { 587 type++; 588 } 589 590 p = strchr(type, ':'); 591 if (!p) { 592 *key = NULL; 593 return NULL; 594 } 595 len = p - type; 596 597 str = g_malloc(len + 1); 598 memcpy(str, type, len); 599 str[len] = '\0'; 600 601 *key = str; 602 return ++p; 603 } 604 605 static int default_fmt_format = 'x'; 606 static int default_fmt_size = 4; 607 608 static int is_valid_option(const char *c, const char *typestr) 609 { 610 char option[3]; 611 612 option[0] = '-'; 613 option[1] = *c; 614 option[2] = '\0'; 615 616 typestr = strstr(typestr, option); 617 return (typestr != NULL); 618 } 619 620 static const HMPCommand *search_dispatch_table(const HMPCommand *disp_table, 621 const char *cmdname) 622 { 623 const HMPCommand *cmd; 624 625 for (cmd = disp_table; cmd->name != NULL; cmd++) { 626 if (hmp_compare_cmd(cmdname, cmd->name)) { 627 return cmd; 628 } 629 } 630 631 return NULL; 632 } 633 634 /* 635 * Parse command name from @cmdp according to command table @table. 636 * If blank, return NULL. 637 * Else, if no valid command can be found, report to @mon, and return 638 * NULL. 639 * Else, change @cmdp to point right behind the name, and return its 640 * command table entry. 641 * Do not assume the return value points into @table! It doesn't when 642 * the command is found in a sub-command table. 643 */ 644 static const HMPCommand *monitor_parse_command(MonitorHMP *hmp_mon, 645 const char *cmdp_start, 646 const char **cmdp, 647 HMPCommand *table) 648 { 649 Monitor *mon = &hmp_mon->common; 650 const char *p; 651 const HMPCommand *cmd; 652 char cmdname[256]; 653 654 /* extract the command name */ 655 p = get_command_name(*cmdp, cmdname, sizeof(cmdname)); 656 if (!p) { 657 return NULL; 658 } 659 660 cmd = search_dispatch_table(table, cmdname); 661 if (!cmd) { 662 monitor_printf(mon, "unknown command: '%.*s'\n", 663 (int)(p - cmdp_start), cmdp_start); 664 return NULL; 665 } 666 if (!cmd_available(cmd)) { 667 monitor_printf(mon, "Command '%.*s' not available " 668 "until machine initialization has completed.\n", 669 (int)(p - cmdp_start), cmdp_start); 670 return NULL; 671 } 672 673 /* filter out following useless space */ 674 while (qemu_isspace(*p)) { 675 p++; 676 } 677 678 *cmdp = p; 679 /* search sub command */ 680 if (cmd->sub_table != NULL && *p != '\0') { 681 return monitor_parse_command(hmp_mon, cmdp_start, cmdp, cmd->sub_table); 682 } 683 684 return cmd; 685 } 686 687 /* 688 * Parse arguments for @cmd. 689 * If it can't be parsed, report to @mon, and return NULL. 690 * Else, insert command arguments into a QDict, and return it. 691 * Note: On success, caller has to free the QDict structure. 692 */ 693 static QDict *monitor_parse_arguments(Monitor *mon, 694 const char **endp, 695 const HMPCommand *cmd) 696 { 697 const char *typestr; 698 char *key; 699 int c; 700 const char *p = *endp; 701 char buf[1024]; 702 QDict *qdict = qdict_new(); 703 704 /* parse the parameters */ 705 typestr = cmd->args_type; 706 for (;;) { 707 typestr = key_get_info(typestr, &key); 708 if (!typestr) { 709 break; 710 } 711 c = *typestr; 712 typestr++; 713 switch (c) { 714 case 'F': 715 case 'B': 716 case 's': 717 { 718 int ret; 719 720 while (qemu_isspace(*p)) { 721 p++; 722 } 723 if (*typestr == '?') { 724 typestr++; 725 if (*p == '\0') { 726 /* no optional string: NULL argument */ 727 break; 728 } 729 } 730 ret = get_str(buf, sizeof(buf), &p); 731 if (ret < 0) { 732 switch (c) { 733 case 'F': 734 monitor_printf(mon, "%s: filename expected\n", 735 cmd->name); 736 break; 737 case 'B': 738 monitor_printf(mon, "%s: block device name expected\n", 739 cmd->name); 740 break; 741 default: 742 monitor_printf(mon, "%s: string expected\n", cmd->name); 743 break; 744 } 745 goto fail; 746 } 747 qdict_put_str(qdict, key, buf); 748 } 749 break; 750 case 'O': 751 { 752 QemuOptsList *opts_list; 753 QemuOpts *opts; 754 755 opts_list = qemu_find_opts(key); 756 if (!opts_list || opts_list->desc->name) { 757 goto bad_type; 758 } 759 while (qemu_isspace(*p)) { 760 p++; 761 } 762 if (!*p) { 763 break; 764 } 765 if (get_str(buf, sizeof(buf), &p) < 0) { 766 goto fail; 767 } 768 opts = qemu_opts_parse_noisily(opts_list, buf, true); 769 if (!opts) { 770 goto fail; 771 } 772 qemu_opts_to_qdict(opts, qdict); 773 qemu_opts_del(opts); 774 } 775 break; 776 case '/': 777 { 778 int count, format, size; 779 780 while (qemu_isspace(*p)) { 781 p++; 782 } 783 if (*p == '/') { 784 /* format found */ 785 p++; 786 count = 1; 787 if (qemu_isdigit(*p)) { 788 count = 0; 789 while (qemu_isdigit(*p)) { 790 count = count * 10 + (*p - '0'); 791 p++; 792 } 793 } 794 size = -1; 795 format = -1; 796 for (;;) { 797 switch (*p) { 798 case 'o': 799 case 'd': 800 case 'u': 801 case 'x': 802 case 'i': 803 case 'c': 804 format = *p++; 805 break; 806 case 'b': 807 size = 1; 808 p++; 809 break; 810 case 'h': 811 size = 2; 812 p++; 813 break; 814 case 'w': 815 size = 4; 816 p++; 817 break; 818 case 'g': 819 case 'L': 820 size = 8; 821 p++; 822 break; 823 default: 824 goto next; 825 } 826 } 827 next: 828 if (*p != '\0' && !qemu_isspace(*p)) { 829 monitor_printf(mon, "invalid char in format: '%c'\n", 830 *p); 831 goto fail; 832 } 833 if (format < 0) { 834 format = default_fmt_format; 835 } 836 if (format != 'i') { 837 /* for 'i', not specifying a size gives -1 as size */ 838 if (size < 0) { 839 size = default_fmt_size; 840 } 841 default_fmt_size = size; 842 } 843 default_fmt_format = format; 844 } else { 845 count = 1; 846 format = default_fmt_format; 847 if (format != 'i') { 848 size = default_fmt_size; 849 } else { 850 size = -1; 851 } 852 } 853 qdict_put_int(qdict, "count", count); 854 qdict_put_int(qdict, "format", format); 855 qdict_put_int(qdict, "size", size); 856 } 857 break; 858 case 'i': 859 case 'l': 860 case 'M': 861 { 862 int64_t val; 863 864 while (qemu_isspace(*p)) { 865 p++; 866 } 867 if (*typestr == '?' || *typestr == '.') { 868 if (*typestr == '?') { 869 if (*p == '\0') { 870 typestr++; 871 break; 872 } 873 } else { 874 if (*p == '.') { 875 p++; 876 while (qemu_isspace(*p)) { 877 p++; 878 } 879 } else { 880 typestr++; 881 break; 882 } 883 } 884 typestr++; 885 } 886 if (get_expr(mon, &val, &p)) { 887 goto fail; 888 } 889 /* Check if 'i' is greater than 32-bit */ 890 if ((c == 'i') && ((val >> 32) & 0xffffffff)) { 891 monitor_printf(mon, "\'%s\' has failed: ", cmd->name); 892 monitor_printf(mon, "integer is for 32-bit values\n"); 893 goto fail; 894 } else if (c == 'M') { 895 if (val < 0) { 896 monitor_printf(mon, "enter a positive value\n"); 897 goto fail; 898 } 899 val *= MiB; 900 } 901 qdict_put_int(qdict, key, val); 902 } 903 break; 904 case 'o': 905 { 906 int ret; 907 uint64_t val; 908 const char *end; 909 910 while (qemu_isspace(*p)) { 911 p++; 912 } 913 if (*typestr == '?') { 914 typestr++; 915 if (*p == '\0') { 916 break; 917 } 918 } 919 ret = qemu_strtosz_MiB(p, &end, &val); 920 if (ret < 0 || val > INT64_MAX) { 921 monitor_printf(mon, "invalid size\n"); 922 goto fail; 923 } 924 qdict_put_int(qdict, key, val); 925 p = end; 926 } 927 break; 928 case 'T': 929 { 930 double val; 931 932 while (qemu_isspace(*p)) { 933 p++; 934 } 935 if (*typestr == '?') { 936 typestr++; 937 if (*p == '\0') { 938 break; 939 } 940 } 941 if (get_double(mon, &val, &p) < 0) { 942 goto fail; 943 } 944 if (p[0] && p[1] == 's') { 945 switch (*p) { 946 case 'm': 947 val /= 1e3; p += 2; break; 948 case 'u': 949 val /= 1e6; p += 2; break; 950 case 'n': 951 val /= 1e9; p += 2; break; 952 } 953 } 954 if (*p && !qemu_isspace(*p)) { 955 monitor_printf(mon, "Unknown unit suffix\n"); 956 goto fail; 957 } 958 qdict_put(qdict, key, qnum_from_double(val)); 959 } 960 break; 961 case 'b': 962 { 963 const char *beg; 964 bool val; 965 966 while (qemu_isspace(*p)) { 967 p++; 968 } 969 beg = p; 970 while (qemu_isgraph(*p)) { 971 p++; 972 } 973 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) { 974 val = true; 975 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) { 976 val = false; 977 } else { 978 monitor_printf(mon, "Expected 'on' or 'off'\n"); 979 goto fail; 980 } 981 qdict_put_bool(qdict, key, val); 982 } 983 break; 984 case '-': 985 { 986 const char *tmp = p; 987 int skip_key = 0; 988 int ret; 989 /* option */ 990 991 c = *typestr++; 992 if (c == '\0') { 993 goto bad_type; 994 } 995 while (qemu_isspace(*p)) { 996 p++; 997 } 998 if (*p == '-') { 999 p++; 1000 if (c != *p) { 1001 if (!is_valid_option(p, typestr)) { 1002 monitor_printf(mon, "%s: unsupported option -%c\n", 1003 cmd->name, *p); 1004 goto fail; 1005 } else { 1006 skip_key = 1; 1007 } 1008 } 1009 if (skip_key) { 1010 p = tmp; 1011 } else if (*typestr == 's') { 1012 /* has option with string value */ 1013 typestr++; 1014 tmp = p++; 1015 while (qemu_isspace(*p)) { 1016 p++; 1017 } 1018 ret = get_str(buf, sizeof(buf), &p); 1019 if (ret < 0) { 1020 monitor_printf(mon, "%s: value expected for -%c\n", 1021 cmd->name, *tmp); 1022 goto fail; 1023 } 1024 qdict_put_str(qdict, key, buf); 1025 } else { 1026 /* has boolean option */ 1027 p++; 1028 qdict_put_bool(qdict, key, true); 1029 } 1030 } else if (*typestr == 's') { 1031 typestr++; 1032 } 1033 } 1034 break; 1035 case 'S': 1036 { 1037 /* package all remaining string */ 1038 int len; 1039 1040 while (qemu_isspace(*p)) { 1041 p++; 1042 } 1043 if (*typestr == '?') { 1044 typestr++; 1045 if (*p == '\0') { 1046 /* no remaining string: NULL argument */ 1047 break; 1048 } 1049 } 1050 len = strlen(p); 1051 if (len <= 0) { 1052 monitor_printf(mon, "%s: string expected\n", 1053 cmd->name); 1054 goto fail; 1055 } 1056 qdict_put_str(qdict, key, p); 1057 p += len; 1058 } 1059 break; 1060 default: 1061 bad_type: 1062 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c); 1063 goto fail; 1064 } 1065 g_free(key); 1066 key = NULL; 1067 } 1068 /* check that all arguments were parsed */ 1069 while (qemu_isspace(*p)) { 1070 p++; 1071 } 1072 if (*p != '\0') { 1073 monitor_printf(mon, "%s: extraneous characters at the end of line\n", 1074 cmd->name); 1075 goto fail; 1076 } 1077 1078 return qdict; 1079 1080 fail: 1081 qobject_unref(qdict); 1082 g_free(key); 1083 return NULL; 1084 } 1085 1086 static void hmp_info_human_readable_text(Monitor *mon, 1087 HumanReadableText *(*handler)(Error **)) 1088 { 1089 Error *err = NULL; 1090 g_autoptr(HumanReadableText) info = handler(&err); 1091 1092 if (hmp_handle_error(mon, err)) { 1093 return; 1094 } 1095 1096 monitor_puts(mon, info->human_readable_text); 1097 } 1098 1099 static void handle_hmp_command_exec(Monitor *mon, 1100 const HMPCommand *cmd, 1101 QDict *qdict) 1102 { 1103 if (cmd->cmd_info_hrt) { 1104 hmp_info_human_readable_text(mon, 1105 cmd->cmd_info_hrt); 1106 } else { 1107 cmd->cmd(mon, qdict); 1108 } 1109 } 1110 1111 typedef struct HandleHmpCommandCo { 1112 Monitor *mon; 1113 const HMPCommand *cmd; 1114 QDict *qdict; 1115 bool done; 1116 } HandleHmpCommandCo; 1117 1118 static void handle_hmp_command_co(void *opaque) 1119 { 1120 HandleHmpCommandCo *data = opaque; 1121 handle_hmp_command_exec(data->mon, data->cmd, data->qdict); 1122 monitor_set_cur(qemu_coroutine_self(), NULL); 1123 data->done = true; 1124 } 1125 1126 void handle_hmp_command(MonitorHMP *mon, const char *cmdline) 1127 { 1128 QDict *qdict; 1129 const HMPCommand *cmd; 1130 const char *cmd_start = cmdline; 1131 1132 trace_handle_hmp_command(mon, cmdline); 1133 1134 cmd = monitor_parse_command(mon, cmdline, &cmdline, hmp_cmds); 1135 if (!cmd) { 1136 return; 1137 } 1138 1139 if (!cmd->cmd && !cmd->cmd_info_hrt) { 1140 /* FIXME: is it useful to try autoload modules here ??? */ 1141 monitor_printf(&mon->common, "Command \"%.*s\" is not available.\n", 1142 (int)(cmdline - cmd_start), cmd_start); 1143 return; 1144 } 1145 1146 qdict = monitor_parse_arguments(&mon->common, &cmdline, cmd); 1147 if (!qdict) { 1148 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) { 1149 cmdline--; 1150 } 1151 monitor_printf(&mon->common, "Try \"help %.*s\" for more information\n", 1152 (int)(cmdline - cmd_start), cmd_start); 1153 return; 1154 } 1155 1156 if (!cmd->coroutine) { 1157 /* old_mon is non-NULL when called from qmp_human_monitor_command() */ 1158 Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(), &mon->common); 1159 handle_hmp_command_exec(&mon->common, cmd, qdict); 1160 monitor_set_cur(qemu_coroutine_self(), old_mon); 1161 } else { 1162 HandleHmpCommandCo data = { 1163 .mon = &mon->common, 1164 .cmd = cmd, 1165 .qdict = qdict, 1166 .done = false, 1167 }; 1168 Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data); 1169 monitor_set_cur(co, &mon->common); 1170 aio_co_enter(qemu_get_aio_context(), co); 1171 AIO_WAIT_WHILE_UNLOCKED(NULL, !data.done); 1172 } 1173 1174 qobject_unref(qdict); 1175 } 1176 1177 static void cmd_completion(MonitorHMP *mon, const char *name, const char *list) 1178 { 1179 const char *p, *pstart; 1180 char cmd[128]; 1181 int len; 1182 1183 p = list; 1184 for (;;) { 1185 pstart = p; 1186 p = qemu_strchrnul(p, '|'); 1187 len = p - pstart; 1188 if (len > sizeof(cmd) - 2) { 1189 len = sizeof(cmd) - 2; 1190 } 1191 memcpy(cmd, pstart, len); 1192 cmd[len] = '\0'; 1193 readline_add_completion_of(mon->rs, name, cmd); 1194 if (*p == '\0') { 1195 break; 1196 } 1197 p++; 1198 } 1199 } 1200 1201 static void file_completion(MonitorHMP *mon, const char *input) 1202 { 1203 DIR *ffs; 1204 struct dirent *d; 1205 char path[1024]; 1206 char file[1024], file_prefix[1024]; 1207 int input_path_len; 1208 const char *p; 1209 1210 p = strrchr(input, '/'); 1211 if (!p) { 1212 input_path_len = 0; 1213 pstrcpy(file_prefix, sizeof(file_prefix), input); 1214 pstrcpy(path, sizeof(path), "."); 1215 } else { 1216 input_path_len = p - input + 1; 1217 memcpy(path, input, input_path_len); 1218 if (input_path_len > sizeof(path) - 1) { 1219 input_path_len = sizeof(path) - 1; 1220 } 1221 path[input_path_len] = '\0'; 1222 pstrcpy(file_prefix, sizeof(file_prefix), p + 1); 1223 } 1224 1225 ffs = opendir(path); 1226 if (!ffs) { 1227 return; 1228 } 1229 for (;;) { 1230 struct stat sb; 1231 d = readdir(ffs); 1232 if (!d) { 1233 break; 1234 } 1235 1236 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) { 1237 continue; 1238 } 1239 1240 if (strstart(d->d_name, file_prefix, NULL)) { 1241 memcpy(file, input, input_path_len); 1242 if (input_path_len < sizeof(file)) { 1243 pstrcpy(file + input_path_len, sizeof(file) - input_path_len, 1244 d->d_name); 1245 } 1246 /* 1247 * stat the file to find out if it's a directory. 1248 * In that case add a slash to speed up typing long paths 1249 */ 1250 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) { 1251 pstrcat(file, sizeof(file), "/"); 1252 } 1253 readline_add_completion(mon->rs, file); 1254 } 1255 } 1256 closedir(ffs); 1257 } 1258 1259 static const char *next_arg_type(const char *typestr) 1260 { 1261 const char *p = strchr(typestr, ':'); 1262 return (p != NULL ? ++p : typestr); 1263 } 1264 1265 static void monitor_find_completion_by_table(MonitorHMP *mon, 1266 const HMPCommand *cmd_table, 1267 char **args, 1268 int nb_args) 1269 { 1270 const char *cmdname; 1271 int i; 1272 const char *ptype, *old_ptype, *str; 1273 const HMPCommand *cmd; 1274 BlockBackend *blk = NULL; 1275 1276 if (nb_args <= 1) { 1277 /* command completion */ 1278 if (nb_args == 0) { 1279 cmdname = ""; 1280 } else { 1281 cmdname = args[0]; 1282 } 1283 readline_set_completion_index(mon->rs, strlen(cmdname)); 1284 for (cmd = cmd_table; cmd->name != NULL; cmd++) { 1285 if (cmd_available(cmd)) { 1286 cmd_completion(mon, cmdname, cmd->name); 1287 } 1288 } 1289 } else { 1290 /* find the command */ 1291 for (cmd = cmd_table; cmd->name != NULL; cmd++) { 1292 if (hmp_compare_cmd(args[0], cmd->name) && 1293 cmd_available(cmd)) { 1294 break; 1295 } 1296 } 1297 if (!cmd->name) { 1298 return; 1299 } 1300 1301 if (cmd->sub_table) { 1302 /* do the job again */ 1303 monitor_find_completion_by_table(mon, cmd->sub_table, 1304 &args[1], nb_args - 1); 1305 return; 1306 } 1307 if (cmd->command_completion) { 1308 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]); 1309 return; 1310 } 1311 1312 ptype = next_arg_type(cmd->args_type); 1313 for (i = 0; i < nb_args - 2; i++) { 1314 if (*ptype != '\0') { 1315 ptype = next_arg_type(ptype); 1316 while (*ptype == '?') { 1317 ptype = next_arg_type(ptype); 1318 } 1319 } 1320 } 1321 str = args[nb_args - 1]; 1322 old_ptype = NULL; 1323 while (*ptype == '-' && old_ptype != ptype) { 1324 old_ptype = ptype; 1325 ptype = next_arg_type(ptype); 1326 } 1327 switch (*ptype) { 1328 case 'F': 1329 /* file completion */ 1330 readline_set_completion_index(mon->rs, strlen(str)); 1331 file_completion(mon, str); 1332 break; 1333 case 'B': 1334 /* block device name completion */ 1335 readline_set_completion_index(mon->rs, strlen(str)); 1336 while ((blk = blk_next(blk)) != NULL) { 1337 readline_add_completion_of(mon->rs, str, blk_name(blk)); 1338 } 1339 break; 1340 case 's': 1341 case 'S': 1342 if (!strcmp(cmd->name, "help|?")) { 1343 monitor_find_completion_by_table(mon, cmd_table, 1344 &args[1], nb_args - 1); 1345 } 1346 break; 1347 default: 1348 break; 1349 } 1350 } 1351 } 1352 1353 static void monitor_find_completion(void *opaque, 1354 const char *cmdline) 1355 { 1356 MonitorHMP *mon = opaque; 1357 char *args[MAX_ARGS]; 1358 int nb_args, len; 1359 1360 /* 1. parse the cmdline */ 1361 if (parse_cmdline(cmdline, &nb_args, args) < 0) { 1362 return; 1363 } 1364 1365 /* 1366 * if the line ends with a space, it means we want to complete the 1367 * next arg 1368 */ 1369 len = strlen(cmdline); 1370 if (len > 0 && qemu_isspace(cmdline[len - 1])) { 1371 if (nb_args >= MAX_ARGS) { 1372 goto cleanup; 1373 } 1374 args[nb_args++] = g_strdup(""); 1375 } 1376 1377 /* 2. auto complete according to args */ 1378 monitor_find_completion_by_table(mon, hmp_cmds, args, nb_args); 1379 1380 cleanup: 1381 free_cmdline_args(args, nb_args); 1382 } 1383 1384 static void monitor_read(void *opaque, const uint8_t *buf, int size) 1385 { 1386 MonitorHMP *mon = container_of(opaque, MonitorHMP, common); 1387 int i; 1388 1389 if (mon->rs) { 1390 for (i = 0; i < size; i++) { 1391 readline_handle_byte(mon->rs, buf[i]); 1392 } 1393 } else { 1394 if (size == 0 || buf[size - 1] != 0) { 1395 monitor_printf(&mon->common, "corrupted command\n"); 1396 } else { 1397 handle_hmp_command(mon, (char *)buf); 1398 } 1399 } 1400 } 1401 1402 static void monitor_event(void *opaque, QEMUChrEvent event) 1403 { 1404 Monitor *mon = opaque; 1405 1406 switch (event) { 1407 case CHR_EVENT_MUX_IN: 1408 qemu_mutex_lock(&mon->mon_lock); 1409 if (mon->mux_out) { 1410 mon->mux_out = 0; 1411 monitor_resume(mon); 1412 } 1413 qemu_mutex_unlock(&mon->mon_lock); 1414 break; 1415 1416 case CHR_EVENT_MUX_OUT: 1417 qemu_mutex_lock(&mon->mon_lock); 1418 if (!mon->mux_out) { 1419 if (mon->reset_seen && !mon->suspend_cnt) { 1420 monitor_puts_locked(mon, "\n"); 1421 } else { 1422 monitor_flush_locked(mon); 1423 } 1424 monitor_suspend(mon); 1425 mon->mux_out = 1; 1426 } 1427 qemu_mutex_unlock(&mon->mon_lock); 1428 break; 1429 1430 case CHR_EVENT_OPENED: 1431 monitor_printf(mon, "QEMU %s monitor - type 'help' for more " 1432 "information\n", QEMU_VERSION); 1433 qemu_mutex_lock(&mon->mon_lock); 1434 mon->reset_seen = 1; 1435 if (!mon->mux_out) { 1436 /* Suspend-resume forces the prompt to be printed. */ 1437 monitor_suspend(mon); 1438 monitor_resume(mon); 1439 } 1440 qemu_mutex_unlock(&mon->mon_lock); 1441 break; 1442 1443 case CHR_EVENT_CLOSED: 1444 monitor_fdsets_cleanup(); 1445 break; 1446 1447 case CHR_EVENT_BREAK: 1448 /* Ignored */ 1449 break; 1450 } 1451 } 1452 1453 1454 /* 1455 * These functions just adapt the readline interface in a typesafe way. We 1456 * could cast function pointers but that discards compiler checks. 1457 */ 1458 static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque, 1459 const char *fmt, ...) 1460 { 1461 MonitorHMP *mon = opaque; 1462 va_list ap; 1463 va_start(ap, fmt); 1464 monitor_vprintf(&mon->common, fmt, ap); 1465 va_end(ap); 1466 } 1467 1468 static void monitor_readline_flush(void *opaque) 1469 { 1470 MonitorHMP *mon = opaque; 1471 monitor_flush(&mon->common); 1472 } 1473 1474 void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) 1475 { 1476 MonitorHMP *mon = g_new0(MonitorHMP, 1); 1477 1478 if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) { 1479 g_free(mon); 1480 return; 1481 } 1482 1483 monitor_data_init(&mon->common, false, false, false); 1484 1485 mon->use_readline = use_readline; 1486 if (mon->use_readline) { 1487 mon->rs = readline_init(monitor_readline_printf, 1488 monitor_readline_flush, 1489 mon, 1490 monitor_find_completion); 1491 monitor_read_command(mon, 0); 1492 } 1493 1494 qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read, 1495 monitor_event, NULL, &mon->common, NULL, true); 1496 monitor_list_append(&mon->common); 1497 } 1498