hmp.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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 "qobject/qdict.h"
  30. #include "qobject/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 "system/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_UNLOCKED(NULL, !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. switch (event) {
  1291. case CHR_EVENT_MUX_IN:
  1292. qemu_mutex_lock(&mon->mon_lock);
  1293. if (mon->mux_out) {
  1294. mon->mux_out = 0;
  1295. monitor_resume(mon);
  1296. }
  1297. qemu_mutex_unlock(&mon->mon_lock);
  1298. break;
  1299. case CHR_EVENT_MUX_OUT:
  1300. qemu_mutex_lock(&mon->mon_lock);
  1301. if (!mon->mux_out) {
  1302. if (mon->reset_seen && !mon->suspend_cnt) {
  1303. monitor_puts_locked(mon, "\n");
  1304. } else {
  1305. monitor_flush_locked(mon);
  1306. }
  1307. monitor_suspend(mon);
  1308. mon->mux_out = 1;
  1309. }
  1310. qemu_mutex_unlock(&mon->mon_lock);
  1311. break;
  1312. case CHR_EVENT_OPENED:
  1313. monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
  1314. "information\n", QEMU_VERSION);
  1315. qemu_mutex_lock(&mon->mon_lock);
  1316. mon->reset_seen = 1;
  1317. if (!mon->mux_out) {
  1318. /* Suspend-resume forces the prompt to be printed. */
  1319. monitor_suspend(mon);
  1320. monitor_resume(mon);
  1321. }
  1322. qemu_mutex_unlock(&mon->mon_lock);
  1323. break;
  1324. case CHR_EVENT_CLOSED:
  1325. monitor_fdsets_cleanup();
  1326. break;
  1327. case CHR_EVENT_BREAK:
  1328. /* Ignored */
  1329. break;
  1330. }
  1331. }
  1332. /*
  1333. * These functions just adapt the readline interface in a typesafe way. We
  1334. * could cast function pointers but that discards compiler checks.
  1335. */
  1336. static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque,
  1337. const char *fmt, ...)
  1338. {
  1339. MonitorHMP *mon = opaque;
  1340. va_list ap;
  1341. va_start(ap, fmt);
  1342. monitor_vprintf(&mon->common, fmt, ap);
  1343. va_end(ap);
  1344. }
  1345. static void monitor_readline_flush(void *opaque)
  1346. {
  1347. MonitorHMP *mon = opaque;
  1348. monitor_flush(&mon->common);
  1349. }
  1350. void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
  1351. {
  1352. MonitorHMP *mon = g_new0(MonitorHMP, 1);
  1353. if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
  1354. g_free(mon);
  1355. return;
  1356. }
  1357. monitor_data_init(&mon->common, false, false, false);
  1358. mon->use_readline = use_readline;
  1359. if (mon->use_readline) {
  1360. mon->rs = readline_init(monitor_readline_printf,
  1361. monitor_readline_flush,
  1362. mon,
  1363. monitor_find_completion);
  1364. monitor_read_command(mon, 0);
  1365. }
  1366. qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
  1367. monitor_event, NULL, &mon->common, NULL, true);
  1368. monitor_list_append(&mon->common);
  1369. }