2
0

hmp.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. typedef struct HandleHmpCommandCo {
  979. Monitor *mon;
  980. const HMPCommand *cmd;
  981. QDict *qdict;
  982. bool done;
  983. } HandleHmpCommandCo;
  984. static void handle_hmp_command_co(void *opaque)
  985. {
  986. HandleHmpCommandCo *data = opaque;
  987. data->cmd->cmd(data->mon, data->qdict);
  988. monitor_set_cur(qemu_coroutine_self(), NULL);
  989. data->done = true;
  990. }
  991. void handle_hmp_command(MonitorHMP *mon, const char *cmdline)
  992. {
  993. QDict *qdict;
  994. const HMPCommand *cmd;
  995. const char *cmd_start = cmdline;
  996. trace_handle_hmp_command(mon, cmdline);
  997. cmd = monitor_parse_command(mon, cmdline, &cmdline, hmp_cmds);
  998. if (!cmd) {
  999. return;
  1000. }
  1001. qdict = monitor_parse_arguments(&mon->common, &cmdline, cmd);
  1002. if (!qdict) {
  1003. while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
  1004. cmdline--;
  1005. }
  1006. monitor_printf(&mon->common, "Try \"help %.*s\" for more information\n",
  1007. (int)(cmdline - cmd_start), cmd_start);
  1008. return;
  1009. }
  1010. if (!cmd->coroutine) {
  1011. /* old_mon is non-NULL when called from qmp_human_monitor_command() */
  1012. Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(), &mon->common);
  1013. cmd->cmd(&mon->common, qdict);
  1014. monitor_set_cur(qemu_coroutine_self(), old_mon);
  1015. } else {
  1016. HandleHmpCommandCo data = {
  1017. .mon = &mon->common,
  1018. .cmd = cmd,
  1019. .qdict = qdict,
  1020. .done = false,
  1021. };
  1022. Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data);
  1023. monitor_set_cur(co, &mon->common);
  1024. aio_co_enter(qemu_get_aio_context(), co);
  1025. AIO_WAIT_WHILE(qemu_get_aio_context(), !data.done);
  1026. }
  1027. qobject_unref(qdict);
  1028. }
  1029. static void cmd_completion(MonitorHMP *mon, const char *name, const char *list)
  1030. {
  1031. const char *p, *pstart;
  1032. char cmd[128];
  1033. int len;
  1034. p = list;
  1035. for (;;) {
  1036. pstart = p;
  1037. p = qemu_strchrnul(p, '|');
  1038. len = p - pstart;
  1039. if (len > sizeof(cmd) - 2) {
  1040. len = sizeof(cmd) - 2;
  1041. }
  1042. memcpy(cmd, pstart, len);
  1043. cmd[len] = '\0';
  1044. if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
  1045. readline_add_completion(mon->rs, cmd);
  1046. }
  1047. if (*p == '\0') {
  1048. break;
  1049. }
  1050. p++;
  1051. }
  1052. }
  1053. static void file_completion(MonitorHMP *mon, const char *input)
  1054. {
  1055. DIR *ffs;
  1056. struct dirent *d;
  1057. char path[1024];
  1058. char file[1024], file_prefix[1024];
  1059. int input_path_len;
  1060. const char *p;
  1061. p = strrchr(input, '/');
  1062. if (!p) {
  1063. input_path_len = 0;
  1064. pstrcpy(file_prefix, sizeof(file_prefix), input);
  1065. pstrcpy(path, sizeof(path), ".");
  1066. } else {
  1067. input_path_len = p - input + 1;
  1068. memcpy(path, input, input_path_len);
  1069. if (input_path_len > sizeof(path) - 1) {
  1070. input_path_len = sizeof(path) - 1;
  1071. }
  1072. path[input_path_len] = '\0';
  1073. pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
  1074. }
  1075. ffs = opendir(path);
  1076. if (!ffs) {
  1077. return;
  1078. }
  1079. for (;;) {
  1080. struct stat sb;
  1081. d = readdir(ffs);
  1082. if (!d) {
  1083. break;
  1084. }
  1085. if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
  1086. continue;
  1087. }
  1088. if (strstart(d->d_name, file_prefix, NULL)) {
  1089. memcpy(file, input, input_path_len);
  1090. if (input_path_len < sizeof(file)) {
  1091. pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
  1092. d->d_name);
  1093. }
  1094. /*
  1095. * stat the file to find out if it's a directory.
  1096. * In that case add a slash to speed up typing long paths
  1097. */
  1098. if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
  1099. pstrcat(file, sizeof(file), "/");
  1100. }
  1101. readline_add_completion(mon->rs, file);
  1102. }
  1103. }
  1104. closedir(ffs);
  1105. }
  1106. static const char *next_arg_type(const char *typestr)
  1107. {
  1108. const char *p = strchr(typestr, ':');
  1109. return (p != NULL ? ++p : typestr);
  1110. }
  1111. static void monitor_find_completion_by_table(MonitorHMP *mon,
  1112. const HMPCommand *cmd_table,
  1113. char **args,
  1114. int nb_args)
  1115. {
  1116. const char *cmdname;
  1117. int i;
  1118. const char *ptype, *old_ptype, *str, *name;
  1119. const HMPCommand *cmd;
  1120. BlockBackend *blk = NULL;
  1121. if (nb_args <= 1) {
  1122. /* command completion */
  1123. if (nb_args == 0) {
  1124. cmdname = "";
  1125. } else {
  1126. cmdname = args[0];
  1127. }
  1128. readline_set_completion_index(mon->rs, strlen(cmdname));
  1129. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  1130. if (!runstate_check(RUN_STATE_PRECONFIG) ||
  1131. cmd_can_preconfig(cmd)) {
  1132. cmd_completion(mon, cmdname, cmd->name);
  1133. }
  1134. }
  1135. } else {
  1136. /* find the command */
  1137. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  1138. if (hmp_compare_cmd(args[0], cmd->name) &&
  1139. (!runstate_check(RUN_STATE_PRECONFIG) ||
  1140. cmd_can_preconfig(cmd))) {
  1141. break;
  1142. }
  1143. }
  1144. if (!cmd->name) {
  1145. return;
  1146. }
  1147. if (cmd->sub_table) {
  1148. /* do the job again */
  1149. monitor_find_completion_by_table(mon, cmd->sub_table,
  1150. &args[1], nb_args - 1);
  1151. return;
  1152. }
  1153. if (cmd->command_completion) {
  1154. cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
  1155. return;
  1156. }
  1157. ptype = next_arg_type(cmd->args_type);
  1158. for (i = 0; i < nb_args - 2; i++) {
  1159. if (*ptype != '\0') {
  1160. ptype = next_arg_type(ptype);
  1161. while (*ptype == '?') {
  1162. ptype = next_arg_type(ptype);
  1163. }
  1164. }
  1165. }
  1166. str = args[nb_args - 1];
  1167. old_ptype = NULL;
  1168. while (*ptype == '-' && old_ptype != ptype) {
  1169. old_ptype = ptype;
  1170. ptype = next_arg_type(ptype);
  1171. }
  1172. switch (*ptype) {
  1173. case 'F':
  1174. /* file completion */
  1175. readline_set_completion_index(mon->rs, strlen(str));
  1176. file_completion(mon, str);
  1177. break;
  1178. case 'B':
  1179. /* block device name completion */
  1180. readline_set_completion_index(mon->rs, strlen(str));
  1181. while ((blk = blk_next(blk)) != NULL) {
  1182. name = blk_name(blk);
  1183. if (str[0] == '\0' ||
  1184. !strncmp(name, str, strlen(str))) {
  1185. readline_add_completion(mon->rs, name);
  1186. }
  1187. }
  1188. break;
  1189. case 's':
  1190. case 'S':
  1191. if (!strcmp(cmd->name, "help|?")) {
  1192. monitor_find_completion_by_table(mon, cmd_table,
  1193. &args[1], nb_args - 1);
  1194. }
  1195. break;
  1196. default:
  1197. break;
  1198. }
  1199. }
  1200. }
  1201. static void monitor_find_completion(void *opaque,
  1202. const char *cmdline)
  1203. {
  1204. MonitorHMP *mon = opaque;
  1205. char *args[MAX_ARGS];
  1206. int nb_args, len;
  1207. /* 1. parse the cmdline */
  1208. if (parse_cmdline(cmdline, &nb_args, args) < 0) {
  1209. return;
  1210. }
  1211. /*
  1212. * if the line ends with a space, it means we want to complete the
  1213. * next arg
  1214. */
  1215. len = strlen(cmdline);
  1216. if (len > 0 && qemu_isspace(cmdline[len - 1])) {
  1217. if (nb_args >= MAX_ARGS) {
  1218. goto cleanup;
  1219. }
  1220. args[nb_args++] = g_strdup("");
  1221. }
  1222. /* 2. auto complete according to args */
  1223. monitor_find_completion_by_table(mon, hmp_cmds, args, nb_args);
  1224. cleanup:
  1225. free_cmdline_args(args, nb_args);
  1226. }
  1227. static void monitor_read(void *opaque, const uint8_t *buf, int size)
  1228. {
  1229. MonitorHMP *mon = container_of(opaque, MonitorHMP, common);
  1230. int i;
  1231. if (mon->rs) {
  1232. for (i = 0; i < size; i++) {
  1233. readline_handle_byte(mon->rs, buf[i]);
  1234. }
  1235. } else {
  1236. if (size == 0 || buf[size - 1] != 0) {
  1237. monitor_printf(&mon->common, "corrupted command\n");
  1238. } else {
  1239. handle_hmp_command(mon, (char *)buf);
  1240. }
  1241. }
  1242. }
  1243. static void monitor_event(void *opaque, QEMUChrEvent event)
  1244. {
  1245. Monitor *mon = opaque;
  1246. MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
  1247. switch (event) {
  1248. case CHR_EVENT_MUX_IN:
  1249. qemu_mutex_lock(&mon->mon_lock);
  1250. mon->mux_out = 0;
  1251. qemu_mutex_unlock(&mon->mon_lock);
  1252. if (mon->reset_seen) {
  1253. readline_restart(hmp_mon->rs);
  1254. monitor_resume(mon);
  1255. monitor_flush(mon);
  1256. } else {
  1257. qatomic_mb_set(&mon->suspend_cnt, 0);
  1258. }
  1259. break;
  1260. case CHR_EVENT_MUX_OUT:
  1261. if (mon->reset_seen) {
  1262. if (qatomic_mb_read(&mon->suspend_cnt) == 0) {
  1263. monitor_printf(mon, "\n");
  1264. }
  1265. monitor_flush(mon);
  1266. monitor_suspend(mon);
  1267. } else {
  1268. qatomic_inc(&mon->suspend_cnt);
  1269. }
  1270. qemu_mutex_lock(&mon->mon_lock);
  1271. mon->mux_out = 1;
  1272. qemu_mutex_unlock(&mon->mon_lock);
  1273. break;
  1274. case CHR_EVENT_OPENED:
  1275. monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
  1276. "information\n", QEMU_VERSION);
  1277. if (!mon->mux_out) {
  1278. readline_restart(hmp_mon->rs);
  1279. readline_show_prompt(hmp_mon->rs);
  1280. }
  1281. mon->reset_seen = 1;
  1282. mon_refcount++;
  1283. break;
  1284. case CHR_EVENT_CLOSED:
  1285. mon_refcount--;
  1286. monitor_fdsets_cleanup();
  1287. break;
  1288. case CHR_EVENT_BREAK:
  1289. /* Ignored */
  1290. break;
  1291. }
  1292. }
  1293. /*
  1294. * These functions just adapt the readline interface in a typesafe way. We
  1295. * could cast function pointers but that discards compiler checks.
  1296. */
  1297. static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
  1298. const char *fmt, ...)
  1299. {
  1300. MonitorHMP *mon = opaque;
  1301. va_list ap;
  1302. va_start(ap, fmt);
  1303. monitor_vprintf(&mon->common, fmt, ap);
  1304. va_end(ap);
  1305. }
  1306. static void monitor_readline_flush(void *opaque)
  1307. {
  1308. MonitorHMP *mon = opaque;
  1309. monitor_flush(&mon->common);
  1310. }
  1311. void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
  1312. {
  1313. MonitorHMP *mon = g_new0(MonitorHMP, 1);
  1314. if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
  1315. g_free(mon);
  1316. return;
  1317. }
  1318. monitor_data_init(&mon->common, false, false, false);
  1319. mon->use_readline = use_readline;
  1320. if (mon->use_readline) {
  1321. mon->rs = readline_init(monitor_readline_printf,
  1322. monitor_readline_flush,
  1323. mon,
  1324. monitor_find_completion);
  1325. monitor_read_command(mon, 0);
  1326. }
  1327. qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
  1328. monitor_event, NULL, &mon->common, NULL, true);
  1329. monitor_list_append(&mon->common);
  1330. }