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