hmp.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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 "hw/qdev-core.h"
  27. #include "monitor-internal.h"
  28. #include "monitor/hmp.h"
  29. #include "qapi/qmp/qdict.h"
  30. #include "qapi/qmp/qnum.h"
  31. #include "qemu/config-file.h"
  32. #include "qemu/ctype.h"
  33. #include "qemu/cutils.h"
  34. #include "qemu/log.h"
  35. #include "qemu/option.h"
  36. #include "qemu/units.h"
  37. #include "sysemu/block-backend.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 bool cmd_available(const HMPCommand *cmd)
  198. {
  199. return phase_check(PHASE_MACHINE_READY) || cmd_can_preconfig(cmd);
  200. }
  201. static void help_cmd_dump_one(Monitor *mon,
  202. const HMPCommand *cmd,
  203. char **prefix_args,
  204. int prefix_args_nb)
  205. {
  206. int i;
  207. if (!cmd_available(cmd)) {
  208. return;
  209. }
  210. for (i = 0; i < prefix_args_nb; i++) {
  211. monitor_printf(mon, "%s ", prefix_args[i]);
  212. }
  213. monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
  214. }
  215. /* @args[@arg_index] is the valid command need to find in @cmds */
  216. static void help_cmd_dump(Monitor *mon, const HMPCommand *cmds,
  217. char **args, int nb_args, int arg_index)
  218. {
  219. const HMPCommand *cmd;
  220. size_t i;
  221. /* No valid arg need to compare with, dump all in *cmds */
  222. if (arg_index >= nb_args) {
  223. for (cmd = cmds; cmd->name != NULL; cmd++) {
  224. help_cmd_dump_one(mon, cmd, args, arg_index);
  225. }
  226. return;
  227. }
  228. /* Find one entry to dump */
  229. for (cmd = cmds; cmd->name != NULL; cmd++) {
  230. if (hmp_compare_cmd(args[arg_index], cmd->name) &&
  231. cmd_available(cmd)) {
  232. if (cmd->sub_table) {
  233. /* continue with next arg */
  234. help_cmd_dump(mon, cmd->sub_table,
  235. args, nb_args, arg_index + 1);
  236. } else {
  237. help_cmd_dump_one(mon, cmd, args, arg_index);
  238. }
  239. return;
  240. }
  241. }
  242. /* Command not found */
  243. monitor_printf(mon, "unknown command: '");
  244. for (i = 0; i <= arg_index; i++) {
  245. monitor_printf(mon, "%s%s", args[i], i == arg_index ? "'\n" : " ");
  246. }
  247. }
  248. void hmp_help_cmd(Monitor *mon, const char *name)
  249. {
  250. char *args[MAX_ARGS];
  251. int nb_args = 0;
  252. /* 1. parse user input */
  253. if (name) {
  254. /* special case for log, directly dump and return */
  255. if (!strcmp(name, "log")) {
  256. const QEMULogItem *item;
  257. monitor_printf(mon, "Log items (comma separated):\n");
  258. monitor_printf(mon, "%-15s %s\n", "none", "remove all logs");
  259. for (item = qemu_log_items; item->mask != 0; item++) {
  260. monitor_printf(mon, "%-15s %s\n", item->name, item->help);
  261. }
  262. #ifdef CONFIG_TRACE_LOG
  263. monitor_printf(mon, "trace:PATTERN enable trace events\n");
  264. monitor_printf(mon, "\nUse \"log trace:help\" to get a list of "
  265. "trace events.\n\n");
  266. #endif
  267. return;
  268. }
  269. if (parse_cmdline(name, &nb_args, args) < 0) {
  270. return;
  271. }
  272. }
  273. /* 2. dump the contents according to parsed args */
  274. help_cmd_dump(mon, hmp_cmds, args, nb_args, 0);
  275. free_cmdline_args(args, nb_args);
  276. }
  277. /*******************************************************************/
  278. static const char *pch;
  279. static sigjmp_buf expr_env;
  280. static G_NORETURN G_GNUC_PRINTF(2, 3)
  281. void expr_error(Monitor *mon, const char *fmt, ...)
  282. {
  283. va_list ap;
  284. va_start(ap, fmt);
  285. monitor_vprintf(mon, fmt, ap);
  286. monitor_printf(mon, "\n");
  287. va_end(ap);
  288. siglongjmp(expr_env, 1);
  289. }
  290. static void next(void)
  291. {
  292. if (*pch != '\0') {
  293. pch++;
  294. while (qemu_isspace(*pch)) {
  295. pch++;
  296. }
  297. }
  298. }
  299. static int64_t expr_sum(Monitor *mon);
  300. static int64_t expr_unary(Monitor *mon)
  301. {
  302. int64_t n;
  303. char *p;
  304. int ret;
  305. switch (*pch) {
  306. case '+':
  307. next();
  308. n = expr_unary(mon);
  309. break;
  310. case '-':
  311. next();
  312. n = -expr_unary(mon);
  313. break;
  314. case '~':
  315. next();
  316. n = ~expr_unary(mon);
  317. break;
  318. case '(':
  319. next();
  320. n = expr_sum(mon);
  321. if (*pch != ')') {
  322. expr_error(mon, "')' expected");
  323. }
  324. next();
  325. break;
  326. case '\'':
  327. pch++;
  328. if (*pch == '\0') {
  329. expr_error(mon, "character constant expected");
  330. }
  331. n = *pch;
  332. pch++;
  333. if (*pch != '\'') {
  334. expr_error(mon, "missing terminating \' character");
  335. }
  336. next();
  337. break;
  338. case '$':
  339. {
  340. char buf[128], *q;
  341. int64_t reg = 0;
  342. pch++;
  343. q = buf;
  344. while ((*pch >= 'a' && *pch <= 'z') ||
  345. (*pch >= 'A' && *pch <= 'Z') ||
  346. (*pch >= '0' && *pch <= '9') ||
  347. *pch == '_' || *pch == '.') {
  348. if ((q - buf) < sizeof(buf) - 1) {
  349. *q++ = *pch;
  350. }
  351. pch++;
  352. }
  353. while (qemu_isspace(*pch)) {
  354. pch++;
  355. }
  356. *q = 0;
  357. ret = get_monitor_def(mon, &reg, buf);
  358. if (ret < 0) {
  359. expr_error(mon, "unknown register");
  360. }
  361. n = reg;
  362. }
  363. break;
  364. case '\0':
  365. expr_error(mon, "unexpected end of expression");
  366. n = 0;
  367. break;
  368. default:
  369. errno = 0;
  370. n = strtoull(pch, &p, 0);
  371. if (errno == ERANGE) {
  372. expr_error(mon, "number too large");
  373. }
  374. if (pch == p) {
  375. expr_error(mon, "invalid char '%c' in expression", *p);
  376. }
  377. pch = p;
  378. while (qemu_isspace(*pch)) {
  379. pch++;
  380. }
  381. break;
  382. }
  383. return n;
  384. }
  385. static int64_t expr_prod(Monitor *mon)
  386. {
  387. int64_t val, val2;
  388. int op;
  389. val = expr_unary(mon);
  390. for (;;) {
  391. op = *pch;
  392. if (op != '*' && op != '/' && op != '%') {
  393. break;
  394. }
  395. next();
  396. val2 = expr_unary(mon);
  397. switch (op) {
  398. default:
  399. case '*':
  400. val *= val2;
  401. break;
  402. case '/':
  403. case '%':
  404. if (val2 == 0) {
  405. expr_error(mon, "division by zero");
  406. }
  407. if (op == '/') {
  408. val /= val2;
  409. } else {
  410. val %= val2;
  411. }
  412. break;
  413. }
  414. }
  415. return val;
  416. }
  417. static int64_t expr_logic(Monitor *mon)
  418. {
  419. int64_t val, val2;
  420. int op;
  421. val = expr_prod(mon);
  422. for (;;) {
  423. op = *pch;
  424. if (op != '&' && op != '|' && op != '^') {
  425. break;
  426. }
  427. next();
  428. val2 = expr_prod(mon);
  429. switch (op) {
  430. default:
  431. case '&':
  432. val &= val2;
  433. break;
  434. case '|':
  435. val |= val2;
  436. break;
  437. case '^':
  438. val ^= val2;
  439. break;
  440. }
  441. }
  442. return val;
  443. }
  444. static int64_t expr_sum(Monitor *mon)
  445. {
  446. int64_t val, val2;
  447. int op;
  448. val = expr_logic(mon);
  449. for (;;) {
  450. op = *pch;
  451. if (op != '+' && op != '-') {
  452. break;
  453. }
  454. next();
  455. val2 = expr_logic(mon);
  456. if (op == '+') {
  457. val += val2;
  458. } else {
  459. val -= val2;
  460. }
  461. }
  462. return val;
  463. }
  464. static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
  465. {
  466. pch = *pp;
  467. if (sigsetjmp(expr_env, 0)) {
  468. *pp = pch;
  469. return -1;
  470. }
  471. while (qemu_isspace(*pch)) {
  472. pch++;
  473. }
  474. *pval = expr_sum(mon);
  475. *pp = pch;
  476. return 0;
  477. }
  478. static int get_double(Monitor *mon, double *pval, const char **pp)
  479. {
  480. const char *p = *pp;
  481. char *tailp;
  482. double d;
  483. d = strtod(p, &tailp);
  484. if (tailp == p) {
  485. monitor_printf(mon, "Number expected\n");
  486. return -1;
  487. }
  488. if (d != d || d - d != 0) {
  489. /* NaN or infinity */
  490. monitor_printf(mon, "Bad number\n");
  491. return -1;
  492. }
  493. *pval = d;
  494. *pp = tailp;
  495. return 0;
  496. }
  497. /*
  498. * Store the command-name in cmdname, and return a pointer to
  499. * the remaining of the command string.
  500. */
  501. static const char *get_command_name(const char *cmdline,
  502. char *cmdname, size_t nlen)
  503. {
  504. size_t len;
  505. const char *p, *pstart;
  506. p = cmdline;
  507. while (qemu_isspace(*p)) {
  508. p++;
  509. }
  510. if (*p == '\0') {
  511. return NULL;
  512. }
  513. pstart = p;
  514. while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) {
  515. p++;
  516. }
  517. len = p - pstart;
  518. if (len > nlen - 1) {
  519. len = nlen - 1;
  520. }
  521. memcpy(cmdname, pstart, len);
  522. cmdname[len] = '\0';
  523. return p;
  524. }
  525. /**
  526. * Read key of 'type' into 'key' and return the current
  527. * 'type' pointer.
  528. */
  529. static char *key_get_info(const char *type, char **key)
  530. {
  531. size_t len;
  532. char *p, *str;
  533. if (*type == ',') {
  534. type++;
  535. }
  536. p = strchr(type, ':');
  537. if (!p) {
  538. *key = NULL;
  539. return NULL;
  540. }
  541. len = p - type;
  542. str = g_malloc(len + 1);
  543. memcpy(str, type, len);
  544. str[len] = '\0';
  545. *key = str;
  546. return ++p;
  547. }
  548. static int default_fmt_format = 'x';
  549. static int default_fmt_size = 4;
  550. static int is_valid_option(const char *c, const char *typestr)
  551. {
  552. char option[3];
  553. option[0] = '-';
  554. option[1] = *c;
  555. option[2] = '\0';
  556. typestr = strstr(typestr, option);
  557. return (typestr != NULL);
  558. }
  559. static const HMPCommand *search_dispatch_table(const HMPCommand *disp_table,
  560. const char *cmdname)
  561. {
  562. const HMPCommand *cmd;
  563. for (cmd = disp_table; cmd->name != NULL; cmd++) {
  564. if (hmp_compare_cmd(cmdname, cmd->name)) {
  565. return cmd;
  566. }
  567. }
  568. return NULL;
  569. }
  570. /*
  571. * Parse command name from @cmdp according to command table @table.
  572. * If blank, return NULL.
  573. * Else, if no valid command can be found, report to @mon, and return
  574. * NULL.
  575. * Else, change @cmdp to point right behind the name, and return its
  576. * command table entry.
  577. * Do not assume the return value points into @table! It doesn't when
  578. * the command is found in a sub-command table.
  579. */
  580. static const HMPCommand *monitor_parse_command(MonitorHMP *hmp_mon,
  581. const char *cmdp_start,
  582. const char **cmdp,
  583. HMPCommand *table)
  584. {
  585. Monitor *mon = &hmp_mon->common;
  586. const char *p;
  587. const HMPCommand *cmd;
  588. char cmdname[256];
  589. /* extract the command name */
  590. p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
  591. if (!p) {
  592. return NULL;
  593. }
  594. cmd = search_dispatch_table(table, cmdname);
  595. if (!cmd) {
  596. monitor_printf(mon, "unknown command: '%.*s'\n",
  597. (int)(p - cmdp_start), cmdp_start);
  598. return NULL;
  599. }
  600. if (!cmd_available(cmd)) {
  601. monitor_printf(mon, "Command '%.*s' not available "
  602. "until machine initialization has completed.\n",
  603. (int)(p - cmdp_start), cmdp_start);
  604. return NULL;
  605. }
  606. /* filter out following useless space */
  607. while (qemu_isspace(*p)) {
  608. p++;
  609. }
  610. *cmdp = p;
  611. /* search sub command */
  612. if (cmd->sub_table != NULL && *p != '\0') {
  613. return monitor_parse_command(hmp_mon, cmdp_start, cmdp, cmd->sub_table);
  614. }
  615. return cmd;
  616. }
  617. /*
  618. * Parse arguments for @cmd.
  619. * If it can't be parsed, report to @mon, and return NULL.
  620. * Else, insert command arguments into a QDict, and return it.
  621. * Note: On success, caller has to free the QDict structure.
  622. */
  623. static QDict *monitor_parse_arguments(Monitor *mon,
  624. const char **endp,
  625. const HMPCommand *cmd)
  626. {
  627. const char *typestr;
  628. char *key;
  629. int c;
  630. const char *p = *endp;
  631. char buf[1024];
  632. QDict *qdict = qdict_new();
  633. /* parse the parameters */
  634. typestr = cmd->args_type;
  635. for (;;) {
  636. typestr = key_get_info(typestr, &key);
  637. if (!typestr) {
  638. break;
  639. }
  640. c = *typestr;
  641. typestr++;
  642. switch (c) {
  643. case 'F':
  644. case 'B':
  645. case 's':
  646. {
  647. int ret;
  648. while (qemu_isspace(*p)) {
  649. p++;
  650. }
  651. if (*typestr == '?') {
  652. typestr++;
  653. if (*p == '\0') {
  654. /* no optional string: NULL argument */
  655. break;
  656. }
  657. }
  658. ret = get_str(buf, sizeof(buf), &p);
  659. if (ret < 0) {
  660. switch (c) {
  661. case 'F':
  662. monitor_printf(mon, "%s: filename expected\n",
  663. cmd->name);
  664. break;
  665. case 'B':
  666. monitor_printf(mon, "%s: block device name expected\n",
  667. cmd->name);
  668. break;
  669. default:
  670. monitor_printf(mon, "%s: string expected\n", cmd->name);
  671. break;
  672. }
  673. goto fail;
  674. }
  675. qdict_put_str(qdict, key, buf);
  676. }
  677. break;
  678. case 'O':
  679. {
  680. QemuOptsList *opts_list;
  681. QemuOpts *opts;
  682. opts_list = qemu_find_opts(key);
  683. if (!opts_list || opts_list->desc->name) {
  684. goto bad_type;
  685. }
  686. while (qemu_isspace(*p)) {
  687. p++;
  688. }
  689. if (!*p) {
  690. break;
  691. }
  692. if (get_str(buf, sizeof(buf), &p) < 0) {
  693. goto fail;
  694. }
  695. opts = qemu_opts_parse_noisily(opts_list, buf, true);
  696. if (!opts) {
  697. goto fail;
  698. }
  699. qemu_opts_to_qdict(opts, qdict);
  700. qemu_opts_del(opts);
  701. }
  702. break;
  703. case '/':
  704. {
  705. int count, format, size;
  706. while (qemu_isspace(*p)) {
  707. p++;
  708. }
  709. if (*p == '/') {
  710. /* format found */
  711. p++;
  712. count = 1;
  713. if (qemu_isdigit(*p)) {
  714. count = 0;
  715. while (qemu_isdigit(*p)) {
  716. count = count * 10 + (*p - '0');
  717. p++;
  718. }
  719. }
  720. size = -1;
  721. format = -1;
  722. for (;;) {
  723. switch (*p) {
  724. case 'o':
  725. case 'd':
  726. case 'u':
  727. case 'x':
  728. case 'i':
  729. case 'c':
  730. format = *p++;
  731. break;
  732. case 'b':
  733. size = 1;
  734. p++;
  735. break;
  736. case 'h':
  737. size = 2;
  738. p++;
  739. break;
  740. case 'w':
  741. size = 4;
  742. p++;
  743. break;
  744. case 'g':
  745. case 'L':
  746. size = 8;
  747. p++;
  748. break;
  749. default:
  750. goto next;
  751. }
  752. }
  753. next:
  754. if (*p != '\0' && !qemu_isspace(*p)) {
  755. monitor_printf(mon, "invalid char in format: '%c'\n",
  756. *p);
  757. goto fail;
  758. }
  759. if (format < 0) {
  760. format = default_fmt_format;
  761. }
  762. if (format != 'i') {
  763. /* for 'i', not specifying a size gives -1 as size */
  764. if (size < 0) {
  765. size = default_fmt_size;
  766. }
  767. default_fmt_size = size;
  768. }
  769. default_fmt_format = format;
  770. } else {
  771. count = 1;
  772. format = default_fmt_format;
  773. if (format != 'i') {
  774. size = default_fmt_size;
  775. } else {
  776. size = -1;
  777. }
  778. }
  779. qdict_put_int(qdict, "count", count);
  780. qdict_put_int(qdict, "format", format);
  781. qdict_put_int(qdict, "size", size);
  782. }
  783. break;
  784. case 'i':
  785. case 'l':
  786. case 'M':
  787. {
  788. int64_t val;
  789. while (qemu_isspace(*p)) {
  790. p++;
  791. }
  792. if (*typestr == '?' || *typestr == '.') {
  793. if (*typestr == '?') {
  794. if (*p == '\0') {
  795. typestr++;
  796. break;
  797. }
  798. } else {
  799. if (*p == '.') {
  800. p++;
  801. while (qemu_isspace(*p)) {
  802. p++;
  803. }
  804. } else {
  805. typestr++;
  806. break;
  807. }
  808. }
  809. typestr++;
  810. }
  811. if (get_expr(mon, &val, &p)) {
  812. goto fail;
  813. }
  814. /* Check if 'i' is greater than 32-bit */
  815. if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
  816. monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
  817. monitor_printf(mon, "integer is for 32-bit values\n");
  818. goto fail;
  819. } else if (c == 'M') {
  820. if (val < 0) {
  821. monitor_printf(mon, "enter a positive value\n");
  822. goto fail;
  823. }
  824. val *= MiB;
  825. }
  826. qdict_put_int(qdict, key, val);
  827. }
  828. break;
  829. case 'o':
  830. {
  831. int ret;
  832. uint64_t val;
  833. const char *end;
  834. while (qemu_isspace(*p)) {
  835. p++;
  836. }
  837. if (*typestr == '?') {
  838. typestr++;
  839. if (*p == '\0') {
  840. break;
  841. }
  842. }
  843. ret = qemu_strtosz_MiB(p, &end, &val);
  844. if (ret < 0 || val > INT64_MAX) {
  845. monitor_printf(mon, "invalid size\n");
  846. goto fail;
  847. }
  848. qdict_put_int(qdict, key, val);
  849. p = end;
  850. }
  851. break;
  852. case 'T':
  853. {
  854. double val;
  855. while (qemu_isspace(*p)) {
  856. p++;
  857. }
  858. if (*typestr == '?') {
  859. typestr++;
  860. if (*p == '\0') {
  861. break;
  862. }
  863. }
  864. if (get_double(mon, &val, &p) < 0) {
  865. goto fail;
  866. }
  867. if (p[0] && p[1] == 's') {
  868. switch (*p) {
  869. case 'm':
  870. val /= 1e3; p += 2; break;
  871. case 'u':
  872. val /= 1e6; p += 2; break;
  873. case 'n':
  874. val /= 1e9; p += 2; break;
  875. }
  876. }
  877. if (*p && !qemu_isspace(*p)) {
  878. monitor_printf(mon, "Unknown unit suffix\n");
  879. goto fail;
  880. }
  881. qdict_put(qdict, key, qnum_from_double(val));
  882. }
  883. break;
  884. case 'b':
  885. {
  886. const char *beg;
  887. bool val;
  888. while (qemu_isspace(*p)) {
  889. p++;
  890. }
  891. beg = p;
  892. while (qemu_isgraph(*p)) {
  893. p++;
  894. }
  895. if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
  896. val = true;
  897. } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
  898. val = false;
  899. } else {
  900. monitor_printf(mon, "Expected 'on' or 'off'\n");
  901. goto fail;
  902. }
  903. qdict_put_bool(qdict, key, val);
  904. }
  905. break;
  906. case '-':
  907. {
  908. const char *tmp = p;
  909. int skip_key = 0;
  910. int ret;
  911. /* option */
  912. c = *typestr++;
  913. if (c == '\0') {
  914. goto bad_type;
  915. }
  916. while (qemu_isspace(*p)) {
  917. p++;
  918. }
  919. if (*p == '-') {
  920. p++;
  921. if (c != *p) {
  922. if (!is_valid_option(p, typestr)) {
  923. monitor_printf(mon, "%s: unsupported option -%c\n",
  924. cmd->name, *p);
  925. goto fail;
  926. } else {
  927. skip_key = 1;
  928. }
  929. }
  930. if (skip_key) {
  931. p = tmp;
  932. } else if (*typestr == 's') {
  933. /* has option with string value */
  934. typestr++;
  935. tmp = p++;
  936. while (qemu_isspace(*p)) {
  937. p++;
  938. }
  939. ret = get_str(buf, sizeof(buf), &p);
  940. if (ret < 0) {
  941. monitor_printf(mon, "%s: value expected for -%c\n",
  942. cmd->name, *tmp);
  943. goto fail;
  944. }
  945. qdict_put_str(qdict, key, buf);
  946. } else {
  947. /* has boolean option */
  948. p++;
  949. qdict_put_bool(qdict, key, true);
  950. }
  951. } else if (*typestr == 's') {
  952. typestr++;
  953. }
  954. }
  955. break;
  956. case 'S':
  957. {
  958. /* package all remaining string */
  959. int len;
  960. while (qemu_isspace(*p)) {
  961. p++;
  962. }
  963. if (*typestr == '?') {
  964. typestr++;
  965. if (*p == '\0') {
  966. /* no remaining string: NULL argument */
  967. break;
  968. }
  969. }
  970. len = strlen(p);
  971. if (len <= 0) {
  972. monitor_printf(mon, "%s: string expected\n",
  973. cmd->name);
  974. goto fail;
  975. }
  976. qdict_put_str(qdict, key, p);
  977. p += len;
  978. }
  979. break;
  980. default:
  981. bad_type:
  982. monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
  983. goto fail;
  984. }
  985. g_free(key);
  986. key = NULL;
  987. }
  988. /* check that all arguments were parsed */
  989. while (qemu_isspace(*p)) {
  990. p++;
  991. }
  992. if (*p != '\0') {
  993. monitor_printf(mon, "%s: extraneous characters at the end of line\n",
  994. cmd->name);
  995. goto fail;
  996. }
  997. return qdict;
  998. fail:
  999. qobject_unref(qdict);
  1000. g_free(key);
  1001. return NULL;
  1002. }
  1003. static void hmp_info_human_readable_text(Monitor *mon,
  1004. HumanReadableText *(*handler)(Error **))
  1005. {
  1006. Error *err = NULL;
  1007. g_autoptr(HumanReadableText) info = handler(&err);
  1008. if (hmp_handle_error(mon, err)) {
  1009. return;
  1010. }
  1011. monitor_puts(mon, info->human_readable_text);
  1012. }
  1013. static void handle_hmp_command_exec(Monitor *mon,
  1014. const HMPCommand *cmd,
  1015. QDict *qdict)
  1016. {
  1017. if (cmd->cmd_info_hrt) {
  1018. hmp_info_human_readable_text(mon,
  1019. cmd->cmd_info_hrt);
  1020. } else {
  1021. cmd->cmd(mon, qdict);
  1022. }
  1023. }
  1024. typedef struct HandleHmpCommandCo {
  1025. Monitor *mon;
  1026. const HMPCommand *cmd;
  1027. QDict *qdict;
  1028. bool done;
  1029. } HandleHmpCommandCo;
  1030. static void handle_hmp_command_co(void *opaque)
  1031. {
  1032. HandleHmpCommandCo *data = opaque;
  1033. handle_hmp_command_exec(data->mon, data->cmd, data->qdict);
  1034. monitor_set_cur(qemu_coroutine_self(), NULL);
  1035. data->done = true;
  1036. }
  1037. void handle_hmp_command(MonitorHMP *mon, const char *cmdline)
  1038. {
  1039. QDict *qdict;
  1040. const HMPCommand *cmd;
  1041. const char *cmd_start = cmdline;
  1042. trace_handle_hmp_command(mon, cmdline);
  1043. cmd = monitor_parse_command(mon, cmdline, &cmdline, hmp_cmds);
  1044. if (!cmd) {
  1045. return;
  1046. }
  1047. if (!cmd->cmd && !cmd->cmd_info_hrt) {
  1048. /* FIXME: is it useful to try autoload modules here ??? */
  1049. monitor_printf(&mon->common, "Command \"%.*s\" is not available.\n",
  1050. (int)(cmdline - cmd_start), cmd_start);
  1051. return;
  1052. }
  1053. qdict = monitor_parse_arguments(&mon->common, &cmdline, cmd);
  1054. if (!qdict) {
  1055. while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
  1056. cmdline--;
  1057. }
  1058. monitor_printf(&mon->common, "Try \"help %.*s\" for more information\n",
  1059. (int)(cmdline - cmd_start), cmd_start);
  1060. return;
  1061. }
  1062. if (!cmd->coroutine) {
  1063. /* old_mon is non-NULL when called from qmp_human_monitor_command() */
  1064. Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(), &mon->common);
  1065. handle_hmp_command_exec(&mon->common, cmd, qdict);
  1066. monitor_set_cur(qemu_coroutine_self(), old_mon);
  1067. } else {
  1068. HandleHmpCommandCo data = {
  1069. .mon = &mon->common,
  1070. .cmd = cmd,
  1071. .qdict = qdict,
  1072. .done = false,
  1073. };
  1074. Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data);
  1075. monitor_set_cur(co, &mon->common);
  1076. aio_co_enter(qemu_get_aio_context(), co);
  1077. AIO_WAIT_WHILE(qemu_get_aio_context(), !data.done);
  1078. }
  1079. qobject_unref(qdict);
  1080. }
  1081. static void cmd_completion(MonitorHMP *mon, const char *name, const char *list)
  1082. {
  1083. const char *p, *pstart;
  1084. char cmd[128];
  1085. int len;
  1086. p = list;
  1087. for (;;) {
  1088. pstart = p;
  1089. p = qemu_strchrnul(p, '|');
  1090. len = p - pstart;
  1091. if (len > sizeof(cmd) - 2) {
  1092. len = sizeof(cmd) - 2;
  1093. }
  1094. memcpy(cmd, pstart, len);
  1095. cmd[len] = '\0';
  1096. readline_add_completion_of(mon->rs, name, cmd);
  1097. if (*p == '\0') {
  1098. break;
  1099. }
  1100. p++;
  1101. }
  1102. }
  1103. static void file_completion(MonitorHMP *mon, const char *input)
  1104. {
  1105. DIR *ffs;
  1106. struct dirent *d;
  1107. char path[1024];
  1108. char file[1024], file_prefix[1024];
  1109. int input_path_len;
  1110. const char *p;
  1111. p = strrchr(input, '/');
  1112. if (!p) {
  1113. input_path_len = 0;
  1114. pstrcpy(file_prefix, sizeof(file_prefix), input);
  1115. pstrcpy(path, sizeof(path), ".");
  1116. } else {
  1117. input_path_len = p - input + 1;
  1118. memcpy(path, input, input_path_len);
  1119. if (input_path_len > sizeof(path) - 1) {
  1120. input_path_len = sizeof(path) - 1;
  1121. }
  1122. path[input_path_len] = '\0';
  1123. pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
  1124. }
  1125. ffs = opendir(path);
  1126. if (!ffs) {
  1127. return;
  1128. }
  1129. for (;;) {
  1130. struct stat sb;
  1131. d = readdir(ffs);
  1132. if (!d) {
  1133. break;
  1134. }
  1135. if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
  1136. continue;
  1137. }
  1138. if (strstart(d->d_name, file_prefix, NULL)) {
  1139. memcpy(file, input, input_path_len);
  1140. if (input_path_len < sizeof(file)) {
  1141. pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
  1142. d->d_name);
  1143. }
  1144. /*
  1145. * stat the file to find out if it's a directory.
  1146. * In that case add a slash to speed up typing long paths
  1147. */
  1148. if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
  1149. pstrcat(file, sizeof(file), "/");
  1150. }
  1151. readline_add_completion(mon->rs, file);
  1152. }
  1153. }
  1154. closedir(ffs);
  1155. }
  1156. static const char *next_arg_type(const char *typestr)
  1157. {
  1158. const char *p = strchr(typestr, ':');
  1159. return (p != NULL ? ++p : typestr);
  1160. }
  1161. static void monitor_find_completion_by_table(MonitorHMP *mon,
  1162. const HMPCommand *cmd_table,
  1163. char **args,
  1164. int nb_args)
  1165. {
  1166. const char *cmdname;
  1167. int i;
  1168. const char *ptype, *old_ptype, *str;
  1169. const HMPCommand *cmd;
  1170. BlockBackend *blk = NULL;
  1171. if (nb_args <= 1) {
  1172. /* command completion */
  1173. if (nb_args == 0) {
  1174. cmdname = "";
  1175. } else {
  1176. cmdname = args[0];
  1177. }
  1178. readline_set_completion_index(mon->rs, strlen(cmdname));
  1179. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  1180. if (cmd_available(cmd)) {
  1181. cmd_completion(mon, cmdname, cmd->name);
  1182. }
  1183. }
  1184. } else {
  1185. /* find the command */
  1186. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  1187. if (hmp_compare_cmd(args[0], cmd->name) &&
  1188. cmd_available(cmd)) {
  1189. break;
  1190. }
  1191. }
  1192. if (!cmd->name) {
  1193. return;
  1194. }
  1195. if (cmd->sub_table) {
  1196. /* do the job again */
  1197. monitor_find_completion_by_table(mon, cmd->sub_table,
  1198. &args[1], nb_args - 1);
  1199. return;
  1200. }
  1201. if (cmd->command_completion) {
  1202. cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
  1203. return;
  1204. }
  1205. ptype = next_arg_type(cmd->args_type);
  1206. for (i = 0; i < nb_args - 2; i++) {
  1207. if (*ptype != '\0') {
  1208. ptype = next_arg_type(ptype);
  1209. while (*ptype == '?') {
  1210. ptype = next_arg_type(ptype);
  1211. }
  1212. }
  1213. }
  1214. str = args[nb_args - 1];
  1215. old_ptype = NULL;
  1216. while (*ptype == '-' && old_ptype != ptype) {
  1217. old_ptype = ptype;
  1218. ptype = next_arg_type(ptype);
  1219. }
  1220. switch (*ptype) {
  1221. case 'F':
  1222. /* file completion */
  1223. readline_set_completion_index(mon->rs, strlen(str));
  1224. file_completion(mon, str);
  1225. break;
  1226. case 'B':
  1227. /* block device name completion */
  1228. readline_set_completion_index(mon->rs, strlen(str));
  1229. while ((blk = blk_next(blk)) != NULL) {
  1230. readline_add_completion_of(mon->rs, str, blk_name(blk));
  1231. }
  1232. break;
  1233. case 's':
  1234. case 'S':
  1235. if (!strcmp(cmd->name, "help|?")) {
  1236. monitor_find_completion_by_table(mon, cmd_table,
  1237. &args[1], nb_args - 1);
  1238. }
  1239. break;
  1240. default:
  1241. break;
  1242. }
  1243. }
  1244. }
  1245. static void monitor_find_completion(void *opaque,
  1246. const char *cmdline)
  1247. {
  1248. MonitorHMP *mon = opaque;
  1249. char *args[MAX_ARGS];
  1250. int nb_args, len;
  1251. /* 1. parse the cmdline */
  1252. if (parse_cmdline(cmdline, &nb_args, args) < 0) {
  1253. return;
  1254. }
  1255. /*
  1256. * if the line ends with a space, it means we want to complete the
  1257. * next arg
  1258. */
  1259. len = strlen(cmdline);
  1260. if (len > 0 && qemu_isspace(cmdline[len - 1])) {
  1261. if (nb_args >= MAX_ARGS) {
  1262. goto cleanup;
  1263. }
  1264. args[nb_args++] = g_strdup("");
  1265. }
  1266. /* 2. auto complete according to args */
  1267. monitor_find_completion_by_table(mon, hmp_cmds, args, nb_args);
  1268. cleanup:
  1269. free_cmdline_args(args, nb_args);
  1270. }
  1271. static void monitor_read(void *opaque, const uint8_t *buf, int size)
  1272. {
  1273. MonitorHMP *mon = container_of(opaque, MonitorHMP, common);
  1274. int i;
  1275. if (mon->rs) {
  1276. for (i = 0; i < size; i++) {
  1277. readline_handle_byte(mon->rs, buf[i]);
  1278. }
  1279. } else {
  1280. if (size == 0 || buf[size - 1] != 0) {
  1281. monitor_printf(&mon->common, "corrupted command\n");
  1282. } else {
  1283. handle_hmp_command(mon, (char *)buf);
  1284. }
  1285. }
  1286. }
  1287. static void monitor_event(void *opaque, QEMUChrEvent event)
  1288. {
  1289. Monitor *mon = opaque;
  1290. MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
  1291. switch (event) {
  1292. case CHR_EVENT_MUX_IN:
  1293. qemu_mutex_lock(&mon->mon_lock);
  1294. mon->mux_out = 0;
  1295. qemu_mutex_unlock(&mon->mon_lock);
  1296. if (mon->reset_seen) {
  1297. readline_restart(hmp_mon->rs);
  1298. monitor_resume(mon);
  1299. monitor_flush(mon);
  1300. } else {
  1301. qatomic_mb_set(&mon->suspend_cnt, 0);
  1302. }
  1303. break;
  1304. case CHR_EVENT_MUX_OUT:
  1305. if (mon->reset_seen) {
  1306. if (qatomic_mb_read(&mon->suspend_cnt) == 0) {
  1307. monitor_printf(mon, "\n");
  1308. }
  1309. monitor_flush(mon);
  1310. monitor_suspend(mon);
  1311. } else {
  1312. qatomic_inc(&mon->suspend_cnt);
  1313. }
  1314. qemu_mutex_lock(&mon->mon_lock);
  1315. mon->mux_out = 1;
  1316. qemu_mutex_unlock(&mon->mon_lock);
  1317. break;
  1318. case CHR_EVENT_OPENED:
  1319. monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
  1320. "information\n", QEMU_VERSION);
  1321. if (!mon->mux_out) {
  1322. readline_restart(hmp_mon->rs);
  1323. readline_show_prompt(hmp_mon->rs);
  1324. }
  1325. mon->reset_seen = 1;
  1326. mon_refcount++;
  1327. break;
  1328. case CHR_EVENT_CLOSED:
  1329. mon_refcount--;
  1330. monitor_fdsets_cleanup();
  1331. break;
  1332. case CHR_EVENT_BREAK:
  1333. /* Ignored */
  1334. break;
  1335. }
  1336. }
  1337. /*
  1338. * These functions just adapt the readline interface in a typesafe way. We
  1339. * could cast function pointers but that discards compiler checks.
  1340. */
  1341. static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque,
  1342. const char *fmt, ...)
  1343. {
  1344. MonitorHMP *mon = opaque;
  1345. va_list ap;
  1346. va_start(ap, fmt);
  1347. monitor_vprintf(&mon->common, fmt, ap);
  1348. va_end(ap);
  1349. }
  1350. static void monitor_readline_flush(void *opaque)
  1351. {
  1352. MonitorHMP *mon = opaque;
  1353. monitor_flush(&mon->common);
  1354. }
  1355. void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
  1356. {
  1357. MonitorHMP *mon = g_new0(MonitorHMP, 1);
  1358. if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
  1359. g_free(mon);
  1360. return;
  1361. }
  1362. monitor_data_init(&mon->common, false, false, false);
  1363. mon->use_readline = use_readline;
  1364. if (mon->use_readline) {
  1365. mon->rs = readline_init(monitor_readline_printf,
  1366. monitor_readline_flush,
  1367. mon,
  1368. monitor_find_completion);
  1369. monitor_read_command(mon, 0);
  1370. }
  1371. qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
  1372. monitor_event, NULL, &mon->common, NULL, true);
  1373. monitor_list_append(&mon->common);
  1374. }