2
0

hmp.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * QEMU monitor
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include <dirent.h>
  26. #include "monitor-internal.h"
  27. #include "qapi/error.h"
  28. #include "qapi/qmp/qdict.h"
  29. #include "qapi/qmp/qnum.h"
  30. #include "qemu/config-file.h"
  31. #include "qemu/ctype.h"
  32. #include "qemu/cutils.h"
  33. #include "qemu/log.h"
  34. #include "qemu/option.h"
  35. #include "qemu/units.h"
  36. #include "sysemu/block-backend.h"
  37. #include "sysemu/runstate.h"
  38. #include "trace.h"
  39. static void monitor_command_cb(void *opaque, const char *cmdline,
  40. void *readline_opaque)
  41. {
  42. MonitorHMP *mon = opaque;
  43. monitor_suspend(&mon->common);
  44. handle_hmp_command(mon, cmdline);
  45. monitor_resume(&mon->common);
  46. }
  47. void monitor_read_command(MonitorHMP *mon, int show_prompt)
  48. {
  49. if (!mon->rs) {
  50. return;
  51. }
  52. readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
  53. if (show_prompt) {
  54. readline_show_prompt(mon->rs);
  55. }
  56. }
  57. int monitor_read_password(MonitorHMP *mon, ReadLineFunc *readline_func,
  58. void *opaque)
  59. {
  60. if (mon->rs) {
  61. readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
  62. /* prompt is printed on return from the command handler */
  63. return 0;
  64. } else {
  65. monitor_printf(&mon->common,
  66. "terminal does not support password prompting\n");
  67. return -ENOTTY;
  68. }
  69. }
  70. static int get_str(char *buf, int buf_size, const char **pp)
  71. {
  72. const char *p;
  73. char *q;
  74. int c;
  75. q = buf;
  76. p = *pp;
  77. while (qemu_isspace(*p)) {
  78. p++;
  79. }
  80. if (*p == '\0') {
  81. fail:
  82. *q = '\0';
  83. *pp = p;
  84. return -1;
  85. }
  86. if (*p == '\"') {
  87. p++;
  88. while (*p != '\0' && *p != '\"') {
  89. if (*p == '\\') {
  90. p++;
  91. c = *p++;
  92. switch (c) {
  93. case 'n':
  94. c = '\n';
  95. break;
  96. case 'r':
  97. c = '\r';
  98. break;
  99. case '\\':
  100. case '\'':
  101. case '\"':
  102. break;
  103. default:
  104. printf("unsupported escape code: '\\%c'\n", c);
  105. goto fail;
  106. }
  107. if ((q - buf) < buf_size - 1) {
  108. *q++ = c;
  109. }
  110. } else {
  111. if ((q - buf) < buf_size - 1) {
  112. *q++ = *p;
  113. }
  114. p++;
  115. }
  116. }
  117. if (*p != '\"') {
  118. printf("unterminated string\n");
  119. goto fail;
  120. }
  121. p++;
  122. } else {
  123. while (*p != '\0' && !qemu_isspace(*p)) {
  124. if ((q - buf) < buf_size - 1) {
  125. *q++ = *p;
  126. }
  127. p++;
  128. }
  129. }
  130. *q = '\0';
  131. *pp = p;
  132. return 0;
  133. }
  134. #define MAX_ARGS 16
  135. static void free_cmdline_args(char **args, int nb_args)
  136. {
  137. int i;
  138. assert(nb_args <= MAX_ARGS);
  139. for (i = 0; i < nb_args; i++) {
  140. g_free(args[i]);
  141. }
  142. }
  143. /*
  144. * Parse the command line to get valid args.
  145. * @cmdline: command line to be parsed.
  146. * @pnb_args: location to store the number of args, must NOT be NULL.
  147. * @args: location to store the args, which should be freed by caller, must
  148. * NOT be NULL.
  149. *
  150. * Returns 0 on success, negative on failure.
  151. *
  152. * NOTE: this parser is an approximate form of the real command parser. Number
  153. * of args have a limit of MAX_ARGS. If cmdline contains more, it will
  154. * return with failure.
  155. */
  156. static int parse_cmdline(const char *cmdline,
  157. int *pnb_args, char **args)
  158. {
  159. const char *p;
  160. int nb_args, ret;
  161. char buf[1024];
  162. p = cmdline;
  163. nb_args = 0;
  164. for (;;) {
  165. while (qemu_isspace(*p)) {
  166. p++;
  167. }
  168. if (*p == '\0') {
  169. break;
  170. }
  171. if (nb_args >= MAX_ARGS) {
  172. goto fail;
  173. }
  174. ret = get_str(buf, sizeof(buf), &p);
  175. if (ret < 0) {
  176. goto fail;
  177. }
  178. args[nb_args] = g_strdup(buf);
  179. nb_args++;
  180. }
  181. *pnb_args = nb_args;
  182. return 0;
  183. fail:
  184. free_cmdline_args(args, nb_args);
  185. return -1;
  186. }
  187. /*
  188. * Can command @cmd be executed in preconfig state?
  189. */
  190. static bool cmd_can_preconfig(const HMPCommand *cmd)
  191. {
  192. if (!cmd->flags) {
  193. return false;
  194. }
  195. return strchr(cmd->flags, 'p');
  196. }
  197. static void help_cmd_dump_one(Monitor *mon,
  198. const HMPCommand *cmd,
  199. char **prefix_args,
  200. int prefix_args_nb)
  201. {
  202. int i;
  203. if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
  204. return;
  205. }
  206. for (i = 0; i < prefix_args_nb; i++) {
  207. monitor_printf(mon, "%s ", prefix_args[i]);
  208. }
  209. monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
  210. }
  211. /* @args[@arg_index] is the valid command need to find in @cmds */
  212. static void help_cmd_dump(Monitor *mon, const HMPCommand *cmds,
  213. char **args, int nb_args, int arg_index)
  214. {
  215. const HMPCommand *cmd;
  216. size_t i;
  217. /* No valid arg need to compare with, dump all in *cmds */
  218. if (arg_index >= nb_args) {
  219. for (cmd = cmds; cmd->name != NULL; cmd++) {
  220. help_cmd_dump_one(mon, cmd, args, arg_index);
  221. }
  222. return;
  223. }
  224. /* Find one entry to dump */
  225. for (cmd = cmds; cmd->name != NULL; cmd++) {
  226. if (hmp_compare_cmd(args[arg_index], cmd->name) &&
  227. ((!runstate_check(RUN_STATE_PRECONFIG) ||
  228. cmd_can_preconfig(cmd)))) {
  229. if (cmd->sub_table) {
  230. /* continue with next arg */
  231. help_cmd_dump(mon, cmd->sub_table,
  232. args, nb_args, arg_index + 1);
  233. } else {
  234. help_cmd_dump_one(mon, cmd, args, arg_index);
  235. }
  236. return;
  237. }
  238. }
  239. /* Command not found */
  240. monitor_printf(mon, "unknown command: '");
  241. for (i = 0; i <= arg_index; i++) {
  242. monitor_printf(mon, "%s%s", args[i], i == arg_index ? "'\n" : " ");
  243. }
  244. }
  245. void help_cmd(Monitor *mon, const char *name)
  246. {
  247. char *args[MAX_ARGS];
  248. int nb_args = 0;
  249. /* 1. parse user input */
  250. if (name) {
  251. /* special case for log, directly dump and return */
  252. if (!strcmp(name, "log")) {
  253. const QEMULogItem *item;
  254. monitor_printf(mon, "Log items (comma separated):\n");
  255. monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
  256. for (item = qemu_log_items; item->mask != 0; item++) {
  257. monitor_printf(mon, "%-10s %s\n", item->name, item->help);
  258. }
  259. return;
  260. }
  261. if (parse_cmdline(name, &nb_args, args) < 0) {
  262. return;
  263. }
  264. }
  265. /* 2. dump the contents according to parsed args */
  266. help_cmd_dump(mon, hmp_cmds, args, nb_args, 0);
  267. free_cmdline_args(args, nb_args);
  268. }
  269. /*******************************************************************/
  270. static const char *pch;
  271. static sigjmp_buf expr_env;
  272. static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
  273. expr_error(Monitor *mon, const char *fmt, ...)
  274. {
  275. va_list ap;
  276. va_start(ap, fmt);
  277. monitor_vprintf(mon, fmt, ap);
  278. monitor_printf(mon, "\n");
  279. va_end(ap);
  280. siglongjmp(expr_env, 1);
  281. }
  282. static void next(void)
  283. {
  284. if (*pch != '\0') {
  285. pch++;
  286. while (qemu_isspace(*pch)) {
  287. pch++;
  288. }
  289. }
  290. }
  291. static int64_t expr_sum(Monitor *mon);
  292. static int64_t expr_unary(Monitor *mon)
  293. {
  294. int64_t n;
  295. char *p;
  296. int ret;
  297. switch (*pch) {
  298. case '+':
  299. next();
  300. n = expr_unary(mon);
  301. break;
  302. case '-':
  303. next();
  304. n = -expr_unary(mon);
  305. break;
  306. case '~':
  307. next();
  308. n = ~expr_unary(mon);
  309. break;
  310. case '(':
  311. next();
  312. n = expr_sum(mon);
  313. if (*pch != ')') {
  314. expr_error(mon, "')' expected");
  315. }
  316. next();
  317. break;
  318. case '\'':
  319. pch++;
  320. if (*pch == '\0') {
  321. expr_error(mon, "character constant expected");
  322. }
  323. n = *pch;
  324. pch++;
  325. if (*pch != '\'') {
  326. expr_error(mon, "missing terminating \' character");
  327. }
  328. next();
  329. break;
  330. case '$':
  331. {
  332. char buf[128], *q;
  333. int64_t reg = 0;
  334. pch++;
  335. q = buf;
  336. while ((*pch >= 'a' && *pch <= 'z') ||
  337. (*pch >= 'A' && *pch <= 'Z') ||
  338. (*pch >= '0' && *pch <= '9') ||
  339. *pch == '_' || *pch == '.') {
  340. if ((q - buf) < sizeof(buf) - 1) {
  341. *q++ = *pch;
  342. }
  343. pch++;
  344. }
  345. while (qemu_isspace(*pch)) {
  346. pch++;
  347. }
  348. *q = 0;
  349. ret = get_monitor_def(&reg, buf);
  350. if (ret < 0) {
  351. expr_error(mon, "unknown register");
  352. }
  353. n = reg;
  354. }
  355. break;
  356. case '\0':
  357. expr_error(mon, "unexpected end of expression");
  358. n = 0;
  359. break;
  360. default:
  361. errno = 0;
  362. n = strtoull(pch, &p, 0);
  363. if (errno == ERANGE) {
  364. expr_error(mon, "number too large");
  365. }
  366. if (pch == p) {
  367. expr_error(mon, "invalid char '%c' in expression", *p);
  368. }
  369. pch = p;
  370. while (qemu_isspace(*pch)) {
  371. pch++;
  372. }
  373. break;
  374. }
  375. return n;
  376. }
  377. static int64_t expr_prod(Monitor *mon)
  378. {
  379. int64_t val, val2;
  380. int op;
  381. val = expr_unary(mon);
  382. for (;;) {
  383. op = *pch;
  384. if (op != '*' && op != '/' && op != '%') {
  385. break;
  386. }
  387. next();
  388. val2 = expr_unary(mon);
  389. switch (op) {
  390. default:
  391. case '*':
  392. val *= val2;
  393. break;
  394. case '/':
  395. case '%':
  396. if (val2 == 0) {
  397. expr_error(mon, "division by zero");
  398. }
  399. if (op == '/') {
  400. val /= val2;
  401. } else {
  402. val %= val2;
  403. }
  404. break;
  405. }
  406. }
  407. return val;
  408. }
  409. static int64_t expr_logic(Monitor *mon)
  410. {
  411. int64_t val, val2;
  412. int op;
  413. val = expr_prod(mon);
  414. for (;;) {
  415. op = *pch;
  416. if (op != '&' && op != '|' && op != '^') {
  417. break;
  418. }
  419. next();
  420. val2 = expr_prod(mon);
  421. switch (op) {
  422. default:
  423. case '&':
  424. val &= val2;
  425. break;
  426. case '|':
  427. val |= val2;
  428. break;
  429. case '^':
  430. val ^= val2;
  431. break;
  432. }
  433. }
  434. return val;
  435. }
  436. static int64_t expr_sum(Monitor *mon)
  437. {
  438. int64_t val, val2;
  439. int op;
  440. val = expr_logic(mon);
  441. for (;;) {
  442. op = *pch;
  443. if (op != '+' && op != '-') {
  444. break;
  445. }
  446. next();
  447. val2 = expr_logic(mon);
  448. if (op == '+') {
  449. val += val2;
  450. } else {
  451. val -= val2;
  452. }
  453. }
  454. return val;
  455. }
  456. static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
  457. {
  458. pch = *pp;
  459. if (sigsetjmp(expr_env, 0)) {
  460. *pp = pch;
  461. return -1;
  462. }
  463. while (qemu_isspace(*pch)) {
  464. pch++;
  465. }
  466. *pval = expr_sum(mon);
  467. *pp = pch;
  468. return 0;
  469. }
  470. static int get_double(Monitor *mon, double *pval, const char **pp)
  471. {
  472. const char *p = *pp;
  473. char *tailp;
  474. double d;
  475. d = strtod(p, &tailp);
  476. if (tailp == p) {
  477. monitor_printf(mon, "Number expected\n");
  478. return -1;
  479. }
  480. if (d != d || d - d != 0) {
  481. /* NaN or infinity */
  482. monitor_printf(mon, "Bad number\n");
  483. return -1;
  484. }
  485. *pval = d;
  486. *pp = tailp;
  487. return 0;
  488. }
  489. /*
  490. * Store the command-name in cmdname, and return a pointer to
  491. * the remaining of the command string.
  492. */
  493. static const char *get_command_name(const char *cmdline,
  494. char *cmdname, size_t nlen)
  495. {
  496. size_t len;
  497. const char *p, *pstart;
  498. p = cmdline;
  499. while (qemu_isspace(*p)) {
  500. p++;
  501. }
  502. if (*p == '\0') {
  503. return NULL;
  504. }
  505. pstart = p;
  506. while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) {
  507. p++;
  508. }
  509. len = p - pstart;
  510. if (len > nlen - 1) {
  511. len = nlen - 1;
  512. }
  513. memcpy(cmdname, pstart, len);
  514. cmdname[len] = '\0';
  515. return p;
  516. }
  517. /**
  518. * Read key of 'type' into 'key' and return the current
  519. * 'type' pointer.
  520. */
  521. static char *key_get_info(const char *type, char **key)
  522. {
  523. size_t len;
  524. char *p, *str;
  525. if (*type == ',') {
  526. type++;
  527. }
  528. p = strchr(type, ':');
  529. if (!p) {
  530. *key = NULL;
  531. return NULL;
  532. }
  533. len = p - type;
  534. str = g_malloc(len + 1);
  535. memcpy(str, type, len);
  536. str[len] = '\0';
  537. *key = str;
  538. return ++p;
  539. }
  540. static int default_fmt_format = 'x';
  541. static int default_fmt_size = 4;
  542. static int is_valid_option(const char *c, const char *typestr)
  543. {
  544. char option[3];
  545. option[0] = '-';
  546. option[1] = *c;
  547. option[2] = '\0';
  548. typestr = strstr(typestr, option);
  549. return (typestr != NULL);
  550. }
  551. static const HMPCommand *search_dispatch_table(const HMPCommand *disp_table,
  552. const char *cmdname)
  553. {
  554. const HMPCommand *cmd;
  555. for (cmd = disp_table; cmd->name != NULL; cmd++) {
  556. if (hmp_compare_cmd(cmdname, cmd->name)) {
  557. return cmd;
  558. }
  559. }
  560. return NULL;
  561. }
  562. /*
  563. * Parse command name from @cmdp according to command table @table.
  564. * If blank, return NULL.
  565. * Else, if no valid command can be found, report to @mon, and return
  566. * NULL.
  567. * Else, change @cmdp to point right behind the name, and return its
  568. * command table entry.
  569. * Do not assume the return value points into @table! It doesn't when
  570. * the command is found in a sub-command table.
  571. */
  572. static const HMPCommand *monitor_parse_command(MonitorHMP *hmp_mon,
  573. const char *cmdp_start,
  574. const char **cmdp,
  575. HMPCommand *table)
  576. {
  577. Monitor *mon = &hmp_mon->common;
  578. const char *p;
  579. const HMPCommand *cmd;
  580. char cmdname[256];
  581. /* extract the command name */
  582. p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
  583. if (!p) {
  584. return NULL;
  585. }
  586. cmd = search_dispatch_table(table, cmdname);
  587. if (!cmd) {
  588. monitor_printf(mon, "unknown command: '%.*s'\n",
  589. (int)(p - cmdp_start), cmdp_start);
  590. return NULL;
  591. }
  592. if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
  593. monitor_printf(mon, "Command '%.*s' not available with -preconfig "
  594. "until after exit_preconfig.\n",
  595. (int)(p - cmdp_start), cmdp_start);
  596. return NULL;
  597. }
  598. /* filter out following useless space */
  599. while (qemu_isspace(*p)) {
  600. p++;
  601. }
  602. *cmdp = p;
  603. /* search sub command */
  604. if (cmd->sub_table != NULL && *p != '\0') {
  605. return monitor_parse_command(hmp_mon, cmdp_start, cmdp, cmd->sub_table);
  606. }
  607. return cmd;
  608. }
  609. /*
  610. * Parse arguments for @cmd.
  611. * If it can't be parsed, report to @mon, and return NULL.
  612. * Else, insert command arguments into a QDict, and return it.
  613. * Note: On success, caller has to free the QDict structure.
  614. */
  615. static QDict *monitor_parse_arguments(Monitor *mon,
  616. const char **endp,
  617. const HMPCommand *cmd)
  618. {
  619. const char *typestr;
  620. char *key;
  621. int c;
  622. const char *p = *endp;
  623. char buf[1024];
  624. QDict *qdict = qdict_new();
  625. /* parse the parameters */
  626. typestr = cmd->args_type;
  627. for (;;) {
  628. typestr = key_get_info(typestr, &key);
  629. if (!typestr) {
  630. break;
  631. }
  632. c = *typestr;
  633. typestr++;
  634. switch (c) {
  635. case 'F':
  636. case 'B':
  637. case 's':
  638. {
  639. int ret;
  640. while (qemu_isspace(*p)) {
  641. p++;
  642. }
  643. if (*typestr == '?') {
  644. typestr++;
  645. if (*p == '\0') {
  646. /* no optional string: NULL argument */
  647. break;
  648. }
  649. }
  650. ret = get_str(buf, sizeof(buf), &p);
  651. if (ret < 0) {
  652. switch (c) {
  653. case 'F':
  654. monitor_printf(mon, "%s: filename expected\n",
  655. cmd->name);
  656. break;
  657. case 'B':
  658. monitor_printf(mon, "%s: block device name expected\n",
  659. cmd->name);
  660. break;
  661. default:
  662. monitor_printf(mon, "%s: string expected\n", cmd->name);
  663. break;
  664. }
  665. goto fail;
  666. }
  667. qdict_put_str(qdict, key, buf);
  668. }
  669. break;
  670. case 'O':
  671. {
  672. QemuOptsList *opts_list;
  673. QemuOpts *opts;
  674. opts_list = qemu_find_opts(key);
  675. if (!opts_list || opts_list->desc->name) {
  676. goto bad_type;
  677. }
  678. while (qemu_isspace(*p)) {
  679. p++;
  680. }
  681. if (!*p) {
  682. break;
  683. }
  684. if (get_str(buf, sizeof(buf), &p) < 0) {
  685. goto fail;
  686. }
  687. opts = qemu_opts_parse_noisily(opts_list, buf, true);
  688. if (!opts) {
  689. goto fail;
  690. }
  691. qemu_opts_to_qdict(opts, qdict);
  692. qemu_opts_del(opts);
  693. }
  694. break;
  695. case '/':
  696. {
  697. int count, format, size;
  698. while (qemu_isspace(*p)) {
  699. p++;
  700. }
  701. if (*p == '/') {
  702. /* format found */
  703. p++;
  704. count = 1;
  705. if (qemu_isdigit(*p)) {
  706. count = 0;
  707. while (qemu_isdigit(*p)) {
  708. count = count * 10 + (*p - '0');
  709. p++;
  710. }
  711. }
  712. size = -1;
  713. format = -1;
  714. for (;;) {
  715. switch (*p) {
  716. case 'o':
  717. case 'd':
  718. case 'u':
  719. case 'x':
  720. case 'i':
  721. case 'c':
  722. format = *p++;
  723. break;
  724. case 'b':
  725. size = 1;
  726. p++;
  727. break;
  728. case 'h':
  729. size = 2;
  730. p++;
  731. break;
  732. case 'w':
  733. size = 4;
  734. p++;
  735. break;
  736. case 'g':
  737. case 'L':
  738. size = 8;
  739. p++;
  740. break;
  741. default:
  742. goto next;
  743. }
  744. }
  745. next:
  746. if (*p != '\0' && !qemu_isspace(*p)) {
  747. monitor_printf(mon, "invalid char in format: '%c'\n",
  748. *p);
  749. goto fail;
  750. }
  751. if (format < 0) {
  752. format = default_fmt_format;
  753. }
  754. if (format != 'i') {
  755. /* for 'i', not specifying a size gives -1 as size */
  756. if (size < 0) {
  757. size = default_fmt_size;
  758. }
  759. default_fmt_size = size;
  760. }
  761. default_fmt_format = format;
  762. } else {
  763. count = 1;
  764. format = default_fmt_format;
  765. if (format != 'i') {
  766. size = default_fmt_size;
  767. } else {
  768. size = -1;
  769. }
  770. }
  771. qdict_put_int(qdict, "count", count);
  772. qdict_put_int(qdict, "format", format);
  773. qdict_put_int(qdict, "size", size);
  774. }
  775. break;
  776. case 'i':
  777. case 'l':
  778. case 'M':
  779. {
  780. int64_t val;
  781. while (qemu_isspace(*p)) {
  782. p++;
  783. }
  784. if (*typestr == '?' || *typestr == '.') {
  785. if (*typestr == '?') {
  786. if (*p == '\0') {
  787. typestr++;
  788. break;
  789. }
  790. } else {
  791. if (*p == '.') {
  792. p++;
  793. while (qemu_isspace(*p)) {
  794. p++;
  795. }
  796. } else {
  797. typestr++;
  798. break;
  799. }
  800. }
  801. typestr++;
  802. }
  803. if (get_expr(mon, &val, &p)) {
  804. goto fail;
  805. }
  806. /* Check if 'i' is greater than 32-bit */
  807. if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
  808. monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
  809. monitor_printf(mon, "integer is for 32-bit values\n");
  810. goto fail;
  811. } else if (c == 'M') {
  812. if (val < 0) {
  813. monitor_printf(mon, "enter a positive value\n");
  814. goto fail;
  815. }
  816. val *= MiB;
  817. }
  818. qdict_put_int(qdict, key, val);
  819. }
  820. break;
  821. case 'o':
  822. {
  823. int ret;
  824. uint64_t val;
  825. const char *end;
  826. while (qemu_isspace(*p)) {
  827. p++;
  828. }
  829. if (*typestr == '?') {
  830. typestr++;
  831. if (*p == '\0') {
  832. break;
  833. }
  834. }
  835. ret = qemu_strtosz_MiB(p, &end, &val);
  836. if (ret < 0 || val > INT64_MAX) {
  837. monitor_printf(mon, "invalid size\n");
  838. goto fail;
  839. }
  840. qdict_put_int(qdict, key, val);
  841. p = end;
  842. }
  843. break;
  844. case 'T':
  845. {
  846. double val;
  847. while (qemu_isspace(*p)) {
  848. p++;
  849. }
  850. if (*typestr == '?') {
  851. typestr++;
  852. if (*p == '\0') {
  853. break;
  854. }
  855. }
  856. if (get_double(mon, &val, &p) < 0) {
  857. goto fail;
  858. }
  859. if (p[0] && p[1] == 's') {
  860. switch (*p) {
  861. case 'm':
  862. val /= 1e3; p += 2; break;
  863. case 'u':
  864. val /= 1e6; p += 2; break;
  865. case 'n':
  866. val /= 1e9; p += 2; break;
  867. }
  868. }
  869. if (*p && !qemu_isspace(*p)) {
  870. monitor_printf(mon, "Unknown unit suffix\n");
  871. goto fail;
  872. }
  873. qdict_put(qdict, key, qnum_from_double(val));
  874. }
  875. break;
  876. case 'b':
  877. {
  878. const char *beg;
  879. bool val;
  880. while (qemu_isspace(*p)) {
  881. p++;
  882. }
  883. beg = p;
  884. while (qemu_isgraph(*p)) {
  885. p++;
  886. }
  887. if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
  888. val = true;
  889. } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
  890. val = false;
  891. } else {
  892. monitor_printf(mon, "Expected 'on' or 'off'\n");
  893. goto fail;
  894. }
  895. qdict_put_bool(qdict, key, val);
  896. }
  897. break;
  898. case '-':
  899. {
  900. const char *tmp = p;
  901. int skip_key = 0;
  902. /* option */
  903. c = *typestr++;
  904. if (c == '\0') {
  905. goto bad_type;
  906. }
  907. while (qemu_isspace(*p)) {
  908. p++;
  909. }
  910. if (*p == '-') {
  911. p++;
  912. if (c != *p) {
  913. if (!is_valid_option(p, typestr)) {
  914. monitor_printf(mon, "%s: unsupported option -%c\n",
  915. cmd->name, *p);
  916. goto fail;
  917. } else {
  918. skip_key = 1;
  919. }
  920. }
  921. if (skip_key) {
  922. p = tmp;
  923. } else {
  924. /* has option */
  925. p++;
  926. qdict_put_bool(qdict, key, true);
  927. }
  928. }
  929. }
  930. break;
  931. case 'S':
  932. {
  933. /* package all remaining string */
  934. int len;
  935. while (qemu_isspace(*p)) {
  936. p++;
  937. }
  938. if (*typestr == '?') {
  939. typestr++;
  940. if (*p == '\0') {
  941. /* no remaining string: NULL argument */
  942. break;
  943. }
  944. }
  945. len = strlen(p);
  946. if (len <= 0) {
  947. monitor_printf(mon, "%s: string expected\n",
  948. cmd->name);
  949. goto fail;
  950. }
  951. qdict_put_str(qdict, key, p);
  952. p += len;
  953. }
  954. break;
  955. default:
  956. bad_type:
  957. monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
  958. goto fail;
  959. }
  960. g_free(key);
  961. key = NULL;
  962. }
  963. /* check that all arguments were parsed */
  964. while (qemu_isspace(*p)) {
  965. p++;
  966. }
  967. if (*p != '\0') {
  968. monitor_printf(mon, "%s: extraneous characters at the end of line\n",
  969. cmd->name);
  970. goto fail;
  971. }
  972. return qdict;
  973. fail:
  974. qobject_unref(qdict);
  975. g_free(key);
  976. return NULL;
  977. }
  978. void handle_hmp_command(MonitorHMP *mon, const char *cmdline)
  979. {
  980. QDict *qdict;
  981. const HMPCommand *cmd;
  982. const char *cmd_start = cmdline;
  983. trace_handle_hmp_command(mon, cmdline);
  984. cmd = monitor_parse_command(mon, cmdline, &cmdline, hmp_cmds);
  985. if (!cmd) {
  986. return;
  987. }
  988. qdict = monitor_parse_arguments(&mon->common, &cmdline, cmd);
  989. if (!qdict) {
  990. while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
  991. cmdline--;
  992. }
  993. monitor_printf(&mon->common, "Try \"help %.*s\" for more information\n",
  994. (int)(cmdline - cmd_start), cmd_start);
  995. return;
  996. }
  997. cmd->cmd(&mon->common, qdict);
  998. qobject_unref(qdict);
  999. }
  1000. static void cmd_completion(MonitorHMP *mon, const char *name, const char *list)
  1001. {
  1002. const char *p, *pstart;
  1003. char cmd[128];
  1004. int len;
  1005. p = list;
  1006. for (;;) {
  1007. pstart = p;
  1008. p = qemu_strchrnul(p, '|');
  1009. len = p - pstart;
  1010. if (len > sizeof(cmd) - 2) {
  1011. len = sizeof(cmd) - 2;
  1012. }
  1013. memcpy(cmd, pstart, len);
  1014. cmd[len] = '\0';
  1015. if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
  1016. readline_add_completion(mon->rs, cmd);
  1017. }
  1018. if (*p == '\0') {
  1019. break;
  1020. }
  1021. p++;
  1022. }
  1023. }
  1024. static void file_completion(MonitorHMP *mon, const char *input)
  1025. {
  1026. DIR *ffs;
  1027. struct dirent *d;
  1028. char path[1024];
  1029. char file[1024], file_prefix[1024];
  1030. int input_path_len;
  1031. const char *p;
  1032. p = strrchr(input, '/');
  1033. if (!p) {
  1034. input_path_len = 0;
  1035. pstrcpy(file_prefix, sizeof(file_prefix), input);
  1036. pstrcpy(path, sizeof(path), ".");
  1037. } else {
  1038. input_path_len = p - input + 1;
  1039. memcpy(path, input, input_path_len);
  1040. if (input_path_len > sizeof(path) - 1) {
  1041. input_path_len = sizeof(path) - 1;
  1042. }
  1043. path[input_path_len] = '\0';
  1044. pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
  1045. }
  1046. ffs = opendir(path);
  1047. if (!ffs) {
  1048. return;
  1049. }
  1050. for (;;) {
  1051. struct stat sb;
  1052. d = readdir(ffs);
  1053. if (!d) {
  1054. break;
  1055. }
  1056. if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
  1057. continue;
  1058. }
  1059. if (strstart(d->d_name, file_prefix, NULL)) {
  1060. memcpy(file, input, input_path_len);
  1061. if (input_path_len < sizeof(file)) {
  1062. pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
  1063. d->d_name);
  1064. }
  1065. /*
  1066. * stat the file to find out if it's a directory.
  1067. * In that case add a slash to speed up typing long paths
  1068. */
  1069. if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
  1070. pstrcat(file, sizeof(file), "/");
  1071. }
  1072. readline_add_completion(mon->rs, file);
  1073. }
  1074. }
  1075. closedir(ffs);
  1076. }
  1077. static const char *next_arg_type(const char *typestr)
  1078. {
  1079. const char *p = strchr(typestr, ':');
  1080. return (p != NULL ? ++p : typestr);
  1081. }
  1082. static void monitor_find_completion_by_table(MonitorHMP *mon,
  1083. const HMPCommand *cmd_table,
  1084. char **args,
  1085. int nb_args)
  1086. {
  1087. const char *cmdname;
  1088. int i;
  1089. const char *ptype, *old_ptype, *str, *name;
  1090. const HMPCommand *cmd;
  1091. BlockBackend *blk = NULL;
  1092. if (nb_args <= 1) {
  1093. /* command completion */
  1094. if (nb_args == 0) {
  1095. cmdname = "";
  1096. } else {
  1097. cmdname = args[0];
  1098. }
  1099. readline_set_completion_index(mon->rs, strlen(cmdname));
  1100. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  1101. if (!runstate_check(RUN_STATE_PRECONFIG) ||
  1102. cmd_can_preconfig(cmd)) {
  1103. cmd_completion(mon, cmdname, cmd->name);
  1104. }
  1105. }
  1106. } else {
  1107. /* find the command */
  1108. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  1109. if (hmp_compare_cmd(args[0], cmd->name) &&
  1110. (!runstate_check(RUN_STATE_PRECONFIG) ||
  1111. cmd_can_preconfig(cmd))) {
  1112. break;
  1113. }
  1114. }
  1115. if (!cmd->name) {
  1116. return;
  1117. }
  1118. if (cmd->sub_table) {
  1119. /* do the job again */
  1120. monitor_find_completion_by_table(mon, cmd->sub_table,
  1121. &args[1], nb_args - 1);
  1122. return;
  1123. }
  1124. if (cmd->command_completion) {
  1125. cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
  1126. return;
  1127. }
  1128. ptype = next_arg_type(cmd->args_type);
  1129. for (i = 0; i < nb_args - 2; i++) {
  1130. if (*ptype != '\0') {
  1131. ptype = next_arg_type(ptype);
  1132. while (*ptype == '?') {
  1133. ptype = next_arg_type(ptype);
  1134. }
  1135. }
  1136. }
  1137. str = args[nb_args - 1];
  1138. old_ptype = NULL;
  1139. while (*ptype == '-' && old_ptype != ptype) {
  1140. old_ptype = ptype;
  1141. ptype = next_arg_type(ptype);
  1142. }
  1143. switch (*ptype) {
  1144. case 'F':
  1145. /* file completion */
  1146. readline_set_completion_index(mon->rs, strlen(str));
  1147. file_completion(mon, str);
  1148. break;
  1149. case 'B':
  1150. /* block device name completion */
  1151. readline_set_completion_index(mon->rs, strlen(str));
  1152. while ((blk = blk_next(blk)) != NULL) {
  1153. name = blk_name(blk);
  1154. if (str[0] == '\0' ||
  1155. !strncmp(name, str, strlen(str))) {
  1156. readline_add_completion(mon->rs, name);
  1157. }
  1158. }
  1159. break;
  1160. case 's':
  1161. case 'S':
  1162. if (!strcmp(cmd->name, "help|?")) {
  1163. monitor_find_completion_by_table(mon, cmd_table,
  1164. &args[1], nb_args - 1);
  1165. }
  1166. break;
  1167. default:
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. static void monitor_find_completion(void *opaque,
  1173. const char *cmdline)
  1174. {
  1175. MonitorHMP *mon = opaque;
  1176. char *args[MAX_ARGS];
  1177. int nb_args, len;
  1178. /* 1. parse the cmdline */
  1179. if (parse_cmdline(cmdline, &nb_args, args) < 0) {
  1180. return;
  1181. }
  1182. /*
  1183. * if the line ends with a space, it means we want to complete the
  1184. * next arg
  1185. */
  1186. len = strlen(cmdline);
  1187. if (len > 0 && qemu_isspace(cmdline[len - 1])) {
  1188. if (nb_args >= MAX_ARGS) {
  1189. goto cleanup;
  1190. }
  1191. args[nb_args++] = g_strdup("");
  1192. }
  1193. /* 2. auto complete according to args */
  1194. monitor_find_completion_by_table(mon, hmp_cmds, args, nb_args);
  1195. cleanup:
  1196. free_cmdline_args(args, nb_args);
  1197. }
  1198. static void monitor_read(void *opaque, const uint8_t *buf, int size)
  1199. {
  1200. MonitorHMP *mon;
  1201. Monitor *old_mon = cur_mon;
  1202. int i;
  1203. cur_mon = opaque;
  1204. mon = container_of(cur_mon, MonitorHMP, common);
  1205. if (mon->rs) {
  1206. for (i = 0; i < size; i++) {
  1207. readline_handle_byte(mon->rs, buf[i]);
  1208. }
  1209. } else {
  1210. if (size == 0 || buf[size - 1] != 0) {
  1211. monitor_printf(cur_mon, "corrupted command\n");
  1212. } else {
  1213. handle_hmp_command(mon, (char *)buf);
  1214. }
  1215. }
  1216. cur_mon = old_mon;
  1217. }
  1218. static void monitor_event(void *opaque, QEMUChrEvent event)
  1219. {
  1220. Monitor *mon = opaque;
  1221. MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
  1222. switch (event) {
  1223. case CHR_EVENT_MUX_IN:
  1224. qemu_mutex_lock(&mon->mon_lock);
  1225. mon->mux_out = 0;
  1226. qemu_mutex_unlock(&mon->mon_lock);
  1227. if (mon->reset_seen) {
  1228. readline_restart(hmp_mon->rs);
  1229. monitor_resume(mon);
  1230. monitor_flush(mon);
  1231. } else {
  1232. atomic_mb_set(&mon->suspend_cnt, 0);
  1233. }
  1234. break;
  1235. case CHR_EVENT_MUX_OUT:
  1236. if (mon->reset_seen) {
  1237. if (atomic_mb_read(&mon->suspend_cnt) == 0) {
  1238. monitor_printf(mon, "\n");
  1239. }
  1240. monitor_flush(mon);
  1241. monitor_suspend(mon);
  1242. } else {
  1243. atomic_inc(&mon->suspend_cnt);
  1244. }
  1245. qemu_mutex_lock(&mon->mon_lock);
  1246. mon->mux_out = 1;
  1247. qemu_mutex_unlock(&mon->mon_lock);
  1248. break;
  1249. case CHR_EVENT_OPENED:
  1250. monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
  1251. "information\n", QEMU_VERSION);
  1252. if (!mon->mux_out) {
  1253. readline_restart(hmp_mon->rs);
  1254. readline_show_prompt(hmp_mon->rs);
  1255. }
  1256. mon->reset_seen = 1;
  1257. mon_refcount++;
  1258. break;
  1259. case CHR_EVENT_CLOSED:
  1260. mon_refcount--;
  1261. monitor_fdsets_cleanup();
  1262. break;
  1263. case CHR_EVENT_BREAK:
  1264. /* Ignored */
  1265. break;
  1266. }
  1267. }
  1268. /*
  1269. * These functions just adapt the readline interface in a typesafe way. We
  1270. * could cast function pointers but that discards compiler checks.
  1271. */
  1272. static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
  1273. const char *fmt, ...)
  1274. {
  1275. MonitorHMP *mon = opaque;
  1276. va_list ap;
  1277. va_start(ap, fmt);
  1278. monitor_vprintf(&mon->common, fmt, ap);
  1279. va_end(ap);
  1280. }
  1281. static void monitor_readline_flush(void *opaque)
  1282. {
  1283. MonitorHMP *mon = opaque;
  1284. monitor_flush(&mon->common);
  1285. }
  1286. void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
  1287. {
  1288. MonitorHMP *mon = g_new0(MonitorHMP, 1);
  1289. if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
  1290. g_free(mon);
  1291. return;
  1292. }
  1293. monitor_data_init(&mon->common, false, false, false);
  1294. mon->use_readline = use_readline;
  1295. if (mon->use_readline) {
  1296. mon->rs = readline_init(monitor_readline_printf,
  1297. monitor_readline_flush,
  1298. mon,
  1299. monitor_find_completion);
  1300. monitor_read_command(mon, 0);
  1301. }
  1302. qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
  1303. monitor_event, NULL, &mon->common, NULL, true);
  1304. monitor_list_append(&mon->common);
  1305. }