hmp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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, int 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. }
  1264. }
  1265. /*
  1266. * These functions just adapt the readline interface in a typesafe way. We
  1267. * could cast function pointers but that discards compiler checks.
  1268. */
  1269. static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
  1270. const char *fmt, ...)
  1271. {
  1272. MonitorHMP *mon = opaque;
  1273. va_list ap;
  1274. va_start(ap, fmt);
  1275. monitor_vprintf(&mon->common, fmt, ap);
  1276. va_end(ap);
  1277. }
  1278. static void monitor_readline_flush(void *opaque)
  1279. {
  1280. MonitorHMP *mon = opaque;
  1281. monitor_flush(&mon->common);
  1282. }
  1283. void monitor_init_hmp(Chardev *chr, bool use_readline)
  1284. {
  1285. MonitorHMP *mon = g_new0(MonitorHMP, 1);
  1286. monitor_data_init(&mon->common, false, false, false);
  1287. qemu_chr_fe_init(&mon->common.chr, chr, &error_abort);
  1288. mon->use_readline = use_readline;
  1289. if (mon->use_readline) {
  1290. mon->rs = readline_init(monitor_readline_printf,
  1291. monitor_readline_flush,
  1292. mon,
  1293. monitor_find_completion);
  1294. monitor_read_command(mon, 0);
  1295. }
  1296. qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
  1297. monitor_event, NULL, &mon->common, NULL, true);
  1298. monitor_list_append(&mon->common);
  1299. }