]> Git Repo - qemu.git/blame - monitor/misc.c
qapi: introduce x-query-roms QMP command
[qemu.git] / monitor / misc.c
CommitLineData
9dc39cba
FB
1/*
2 * QEMU monitor
5fafdf24 3 *
9dc39cba 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
9dc39cba
FB
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 */
e688df6b 24
d38ea87a 25#include "qemu/osdep.h"
5bce308a 26#include "monitor-internal.h"
b4a42f81 27#include "monitor/qdev.h"
87ecb68b 28#include "hw/usb.h"
a2cb15b0 29#include "hw/pci/pci.h"
0d09e41a 30#include "sysemu/watchdog.h"
45a50b16 31#include "hw/loader.h"
022c62cb 32#include "exec/gdbstub.h"
1422e32d 33#include "net/net.h"
68ac40d2 34#include "net/slirp.h"
7572150c 35#include "ui/qemu-spice.h"
213dcb06 36#include "qemu/config-file.h"
856dfd8a 37#include "qemu/ctype.h"
28ecbaee 38#include "ui/console.h"
c751a74a 39#include "ui/input.h"
87ecb68b 40#include "audio/audio.h"
76cad711 41#include "disas/disas.h"
9c17d615 42#include "sysemu/balloon.h"
1de7afc9 43#include "qemu/timer.h"
b3946626 44#include "sysemu/hw_accel.h"
54d31236 45#include "sysemu/runstate.h"
b76806d4
DB
46#include "authz/list.h"
47#include "qapi/util.h"
55225c85
MA
48#include "sysemu/blockdev.h"
49#include "sysemu/sysemu.h"
14a48c1d 50#include "sysemu/tcg.h"
bdee56f5 51#include "sysemu/tpm.h"
452fcdbc 52#include "qapi/qmp/qdict.h"
cc7a8ea7 53#include "qapi/qmp/qerror.h"
fc81fa1e 54#include "qapi/qmp/qstring.h"
a9c94277 55#include "qom/object_interfaces.h"
31965ae2 56#include "trace/control.h"
bf957284 57#include "monitor/hmp-target.h"
275307aa 58#include "monitor/hmp.h"
6d8a764e 59#ifdef CONFIG_TRACE_SIMPLE
31965ae2 60#include "trace/simple.h"
22890ab5 61#endif
022c62cb 62#include "exec/memory.h"
63c91552 63#include "exec/exec-all.h"
922a01a0 64#include "qemu/option.h"
1de7afc9 65#include "qemu/thread.h"
b21631f3 66#include "block/qapi.h"
a2dde2f2 67#include "block/block-hmp-cmds.h"
00ca24ff 68#include "qapi/qapi-commands-char.h"
fa4dcf57 69#include "qapi/qapi-commands-control.h"
00ca24ff
MA
70#include "qapi/qapi-commands-migration.h"
71#include "qapi/qapi-commands-misc.h"
72#include "qapi/qapi-commands-qom.h"
73#include "qapi/qapi-commands-trace.h"
dd98234c 74#include "qapi/qapi-commands-machine.h"
00ca24ff 75#include "qapi/qapi-init-commands.h"
e688df6b 76#include "qapi/error.h"
43a14cfc 77#include "qapi/qmp-event.h"
d2528bdc 78#include "sysemu/cpus.h"
f348b6d1 79#include "qemu/cutils.h"
6a5bd307 80
a4538a5c
JH
81#if defined(TARGET_S390X)
82#include "hw/s390x/storage-keys.h"
f860d497 83#include "hw/s390x/storage-attributes.h"
a4538a5c
JH
84#endif
85
f07918fd 86/* file descriptors passed via SCM_RIGHTS */
c227f099
AL
87typedef struct mon_fd_t mon_fd_t;
88struct mon_fd_t {
f07918fd
MM
89 char *name;
90 int fd;
c227f099 91 QLIST_ENTRY(mon_fd_t) next;
f07918fd
MM
92};
93
ba1c048a
CB
94/* file descriptor associated with a file descriptor set */
95typedef struct MonFdsetFd MonFdsetFd;
96struct MonFdsetFd {
97 int fd;
98 bool removed;
99 char *opaque;
100 QLIST_ENTRY(MonFdsetFd) next;
101};
102
103/* file descriptor set containing fds passed via SCM_RIGHTS */
104typedef struct MonFdset MonFdset;
105struct MonFdset {
106 int64_t id;
107 QLIST_HEAD(, MonFdsetFd) fds;
adb696f3 108 QLIST_HEAD(, MonFdsetFd) dup_fds;
ba1c048a
CB
109 QLIST_ENTRY(MonFdset) next;
110};
111
47451466
PX
112/* Protects mon_fdsets */
113static QemuMutex mon_fdsets_lock;
b58deb34 114static QLIST_HEAD(, MonFdset) mon_fdsets;
47451466 115
a0cd5e1c 116static HMPCommand hmp_info_cmds[];
9dc39cba 117
d51a67b4
LC
118char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
119 int64_t cpu_index, Error **errp)
0268d97c 120{
d51a67b4 121 char *output = NULL;
5f9dba16 122 MonitorHMP hmp = {};
0268d97c 123
92082416 124 monitor_data_init(&hmp.common, false, true, false);
0268d97c 125
d51a67b4 126 if (has_cpu_index) {
dcba65f8 127 int ret = monitor_set_cpu(&hmp.common, cpu_index);
0268d97c 128 if (ret < 0) {
c6bd8c70
MA
129 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
130 "a CPU number");
0268d97c
LC
131 goto out;
132 }
133 }
134
7ef6cf63 135 handle_hmp_command(&hmp, command_line);
0268d97c 136
0210c3b3 137 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
20076f4a 138 output = g_strdup(hmp.common.outbuf->str);
0268d97c
LC
139 }
140
141out:
5f9dba16 142 monitor_data_destroy(&hmp.common);
d51a67b4 143 return output;
0268d97c
LC
144}
145
ed7bda5d
KW
146/**
147 * Is @name in the '|' separated list of names @list?
148 */
149int hmp_compare_cmd(const char *name, const char *list)
9dc39cba
FB
150{
151 const char *p, *pstart;
152 int len;
153 len = strlen(name);
154 p = list;
ed7bda5d 155 for (;;) {
9dc39cba 156 pstart = p;
5c99fa37 157 p = qemu_strchrnul(p, '|');
ed7bda5d 158 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
9dc39cba 159 return 1;
f5438c05
WX
160 }
161 if (*p == '\0') {
162 break;
163 }
ed7bda5d 164 p++;
f5438c05 165 }
dcc70cdf 166 return 0;
9dc39cba
FB
167}
168
d54908a5 169static void do_help_cmd(Monitor *mon, const QDict *qdict)
38183186 170{
d54908a5 171 help_cmd(mon, qdict_get_try_str(qdict, "name"));
38183186
LC
172}
173
3e5a50d6 174static void hmp_trace_event(Monitor *mon, const QDict *qdict)
22890ab5
PS
175{
176 const char *tp_name = qdict_get_str(qdict, "name");
177 bool new_state = qdict_get_bool(qdict, "option");
77e2b172
LV
178 bool has_vcpu = qdict_haskey(qdict, "vcpu");
179 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
14101d02 180 Error *local_err = NULL;
f871d689 181
77e2b172
LV
182 if (vcpu < 0) {
183 monitor_printf(mon, "argument vcpu must be positive");
184 return;
185 }
186
187 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
14101d02 188 if (local_err) {
091e38b7 189 error_report_err(local_err);
f871d689 190 }
22890ab5 191}
c5ceb523 192
c45a8168 193#ifdef CONFIG_TRACE_SIMPLE
3e5a50d6 194static void hmp_trace_file(Monitor *mon, const QDict *qdict)
c5ceb523
SH
195{
196 const char *op = qdict_get_try_str(qdict, "op");
197 const char *arg = qdict_get_try_str(qdict, "arg");
198
199 if (!op) {
ba4912cb 200 st_print_trace_file_status();
c5ceb523
SH
201 } else if (!strcmp(op, "on")) {
202 st_set_trace_file_enabled(true);
203 } else if (!strcmp(op, "off")) {
204 st_set_trace_file_enabled(false);
205 } else if (!strcmp(op, "flush")) {
206 st_flush_trace_buffer();
207 } else if (!strcmp(op, "set")) {
208 if (arg) {
209 st_set_trace_file(arg);
210 }
211 } else {
212 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
213 help_cmd(mon, "trace-file");
214 }
215}
22890ab5
PS
216#endif
217
3e5a50d6 218static void hmp_info_help(Monitor *mon, const QDict *qdict)
9dc39cba 219{
13c7425e 220 help_cmd(mon, "info");
9dc39cba
FB
221}
222
6adf08dd 223static void monitor_init_qmp_commands(void)
edcfaefe 224{
635db18f
MA
225 /*
226 * Two command lists:
227 * - qmp_commands contains all QMP commands
228 * - qmp_cap_negotiation_commands contains just
229 * "qmp_capabilities", to enforce capability negotiation
230 */
231
1527badb 232 qmp_init_marshal(&qmp_commands);
05875687 233
6604e475
MA
234 qmp_register_command(&qmp_commands, "device_add",
235 qmp_device_add, 0, 0);
5032a16d 236
635db18f
MA
237 QTAILQ_INIT(&qmp_cap_negotiation_commands);
238 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
6604e475
MA
239 qmp_marshal_qmp_capabilities,
240 QCO_ALLOW_PRECONFIG, 0);
635db18f
MA
241}
242
d9f25280 243/* Set the current CPU defined by the user. Callers must hold BQL. */
dcba65f8 244int monitor_set_cpu(Monitor *mon, int cpu_index)
6a00d601 245{
55e5c285 246 CPUState *cpu;
6a00d601 247
1c8bb3cc
AF
248 cpu = qemu_get_cpu(cpu_index);
249 if (cpu == NULL) {
250 return -1;
6a00d601 251 }
dcba65f8
KW
252 g_free(mon->mon_cpu_path);
253 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
1c8bb3cc 254 return 0;
6a00d601
FB
255}
256
d9f25280 257/* Callers must hold BQL. */
87e6f4a4 258static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
6a00d601 259{
a85d0bf3 260 CPUState *cpu = NULL;
751f8cfe 261
87e6f4a4
KW
262 if (mon->mon_cpu_path) {
263 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
751f8cfe
GK
264 TYPE_CPU, NULL);
265 if (!cpu) {
87e6f4a4
KW
266 g_free(mon->mon_cpu_path);
267 mon->mon_cpu_path = NULL;
751f8cfe
GK
268 }
269 }
87e6f4a4 270 if (!mon->mon_cpu_path) {
854e67fe
TH
271 if (!first_cpu) {
272 return NULL;
273 }
87e6f4a4 274 monitor_set_cpu(mon, first_cpu->cpu_index);
751f8cfe 275 cpu = first_cpu;
6a00d601 276 }
a85d0bf3 277 assert(cpu != NULL);
137b5cb6
VM
278 if (synchronize) {
279 cpu_synchronize_state(cpu);
280 }
751f8cfe 281 return cpu;
5bcda5f7
PC
282}
283
2fc5d01b 284CPUState *mon_get_cpu(Monitor *mon)
137b5cb6 285{
2fc5d01b 286 return mon_get_cpu_sync(mon, true);
137b5cb6
VM
287}
288
e7cff9c6 289CPUArchState *mon_get_cpu_env(Monitor *mon)
5bcda5f7 290{
e7cff9c6 291 CPUState *cs = mon_get_cpu(mon);
854e67fe
TH
292
293 return cs ? cs->env_ptr : NULL;
6a00d601
FB
294}
295
87e6f4a4 296int monitor_get_cpu_index(Monitor *mon)
99b7796f 297{
87e6f4a4 298 CPUState *cs = mon_get_cpu_sync(mon, false);
854e67fe
TH
299
300 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
99b7796f
LC
301}
302
1ce6be24 303static void hmp_info_registers(Monitor *mon, const QDict *qdict)
9307c4c1 304{
18f08282
SJS
305 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
306 CPUState *cs;
854e67fe 307
18f08282
SJS
308 if (all_cpus) {
309 CPU_FOREACH(cs) {
310 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
90c84c56 311 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
18f08282
SJS
312 }
313 } else {
2fc5d01b 314 cs = mon_get_cpu(mon);
18f08282
SJS
315
316 if (!cs) {
317 monitor_printf(mon, "No CPU available\n");
318 return;
319 }
320
90c84c56 321 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
854e67fe 322 }
9307c4c1
FB
323}
324
97bfafe2
EC
325static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
326{
327 int64_t max = qdict_get_try_int(qdict, "max", 10);
328 bool mean = qdict_get_try_bool(qdict, "mean", false);
329 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
330 enum QSPSortBy sort_by;
331
332 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
ac7ff4cf 333 qsp_report(max, sort_by, coalesce);
97bfafe2
EC
334}
335
1ce6be24 336static void hmp_info_history(Monitor *mon, const QDict *qdict)
aa455485 337{
5f9dba16 338 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
aa455485 339 int i;
7e2515e8 340 const char *str;
3b46e624 341
5f9dba16 342 if (!hmp_mon->rs) {
cde76ee1 343 return;
5f9dba16 344 }
7e2515e8
FB
345 i = 0;
346 for(;;) {
5f9dba16
KW
347 str = readline_get_history(hmp_mon->rs, i);
348 if (!str) {
7e2515e8 349 break;
5f9dba16 350 }
376253ec 351 monitor_printf(mon, "%d: '%s'\n", i, str);
8e3a9fd2 352 i++;
aa455485
FB
353 }
354}
355
1ce6be24 356static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
22890ab5 357{
bd71211d 358 const char *name = qdict_get_try_str(qdict, "name");
77e2b172
LV
359 bool has_vcpu = qdict_haskey(qdict, "vcpu");
360 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
bd71211d 361 TraceEventInfoList *events;
14101d02 362 TraceEventInfoList *elem;
bd71211d
LV
363 Error *local_err = NULL;
364
365 if (name == NULL) {
366 name = "*";
367 }
77e2b172
LV
368 if (vcpu < 0) {
369 monitor_printf(mon, "argument vcpu must be positive");
370 return;
371 }
bd71211d 372
77e2b172 373 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
bd71211d
LV
374 if (local_err) {
375 error_report_err(local_err);
376 return;
377 }
14101d02
LV
378
379 for (elem = events; elem != NULL; elem = elem->next) {
380 monitor_printf(mon, "%s : state %u\n",
381 elem->value->name,
382 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
383 }
384 qapi_free_TraceEventInfoList(events);
22890ab5 385}
22890ab5 386
b8a185bc
MA
387void qmp_client_migrate_info(const char *protocol, const char *hostname,
388 bool has_port, int64_t port,
389 bool has_tls_port, int64_t tls_port,
390 bool has_cert_subject, const char *cert_subject,
391 Error **errp)
e866e239 392{
e866e239 393 if (strcmp(protocol, "spice") == 0) {
b8a185bc
MA
394 if (!qemu_using_spice(errp)) {
395 return;
e866e239
GH
396 }
397
b8a185bc 398 if (!has_port && !has_tls_port) {
c6bd8c70 399 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
b8a185bc 400 return;
6ec5dae5
YH
401 }
402
7477477c 403 if (qemu_spice.migrate_info(hostname,
b8a185bc
MA
404 has_port ? port : -1,
405 has_tls_port ? tls_port : -1,
406 cert_subject)) {
9e1b9c6c 407 error_setg(errp, "Could not set up display for migration");
b8a185bc 408 return;
e866e239 409 }
b8a185bc 410 return;
e866e239
GH
411 }
412
99750d82 413 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
e866e239
GH
414}
415
3e5a50d6 416static void hmp_logfile(Monitor *mon, const QDict *qdict)
e735b91c 417{
daa76aa4
MA
418 Error *err = NULL;
419
420 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
421 if (err) {
422 error_report_err(err);
423 }
e735b91c
PB
424}
425
3e5a50d6 426static void hmp_log(Monitor *mon, const QDict *qdict)
f193c797
FB
427{
428 int mask;
d54908a5 429 const char *items = qdict_get_str(qdict, "items");
3b46e624 430
9307c4c1 431 if (!strcmp(items, "none")) {
f193c797
FB
432 mask = 0;
433 } else {
4fde1eba 434 mask = qemu_str_to_log_mask(items);
f193c797 435 if (!mask) {
376253ec 436 help_cmd(mon, "log");
f193c797
FB
437 return;
438 }
439 }
24537a01 440 qemu_set_log(mask);
f193c797
FB
441}
442
3e5a50d6 443static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1b530a6d 444{
d54908a5 445 const char *option = qdict_get_try_str(qdict, "option");
1b530a6d
AJ
446 if (!option || !strcmp(option, "on")) {
447 singlestep = 1;
448 } else if (!strcmp(option, "off")) {
449 singlestep = 0;
450 } else {
451 monitor_printf(mon, "unexpected option %s\n", option);
452 }
453}
454
3e5a50d6 455static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
59030a8c 456{
d54908a5 457 const char *device = qdict_get_try_str(qdict, "device");
89854b95 458 if (!device) {
59030a8c 459 device = "tcp::" DEFAULT_GDBSTUB_PORT;
89854b95
YA
460 }
461
59030a8c
AL
462 if (gdbserver_start(device) < 0) {
463 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
464 device);
465 } else if (strcmp(device, "none") == 0) {
36556b20 466 monitor_printf(mon, "Disabled gdbserver\n");
8a7ddc38 467 } else {
59030a8c
AL
468 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
469 device);
8a7ddc38
FB
470 }
471}
472
3e5a50d6 473static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
9dd986cc 474{
d54908a5 475 const char *action = qdict_get_str(qdict, "action");
9dd986cc
RJ
476 if (select_watchdog_action(action) == -1) {
477 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
478 }
479}
480
376253ec 481static void monitor_printc(Monitor *mon, int c)
9307c4c1 482{
376253ec 483 monitor_printf(mon, "'");
9307c4c1
FB
484 switch(c) {
485 case '\'':
376253ec 486 monitor_printf(mon, "\\'");
9307c4c1
FB
487 break;
488 case '\\':
376253ec 489 monitor_printf(mon, "\\\\");
9307c4c1
FB
490 break;
491 case '\n':
376253ec 492 monitor_printf(mon, "\\n");
9307c4c1
FB
493 break;
494 case '\r':
376253ec 495 monitor_printf(mon, "\\r");
9307c4c1
FB
496 break;
497 default:
498 if (c >= 32 && c <= 126) {
376253ec 499 monitor_printf(mon, "%c", c);
9307c4c1 500 } else {
376253ec 501 monitor_printf(mon, "\\x%02x", c);
9307c4c1
FB
502 }
503 break;
504 }
376253ec 505 monitor_printf(mon, "'");
9307c4c1
FB
506}
507
376253ec 508static void memory_dump(Monitor *mon, int count, int format, int wsize,
a8170e5e 509 hwaddr addr, int is_physical)
9307c4c1 510{
23842aab 511 int l, line_size, i, max_digits, len;
9307c4c1
FB
512 uint8_t buf[16];
513 uint64_t v;
2fc5d01b 514 CPUState *cs = mon_get_cpu(mon);
854e67fe
TH
515
516 if (!cs && (format == 'i' || !is_physical)) {
517 monitor_printf(mon, "Can not dump without CPU\n");
518 return;
519 }
9307c4c1
FB
520
521 if (format == 'i') {
1d48474d 522 monitor_disas(mon, cs, addr, count, is_physical);
9307c4c1
FB
523 return;
524 }
525
526 len = wsize * count;
89854b95 527 if (wsize == 1) {
9307c4c1 528 line_size = 8;
89854b95 529 } else {
9307c4c1 530 line_size = 16;
89854b95 531 }
9307c4c1
FB
532 max_digits = 0;
533
534 switch(format) {
535 case 'o':
69db8dfc 536 max_digits = DIV_ROUND_UP(wsize * 8, 3);
9307c4c1
FB
537 break;
538 default:
539 case 'x':
540 max_digits = (wsize * 8) / 4;
541 break;
542 case 'u':
543 case 'd':
69db8dfc 544 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
9307c4c1
FB
545 break;
546 case 'c':
547 wsize = 1;
548 break;
549 }
550
551 while (len > 0) {
89854b95 552 if (is_physical) {
376253ec 553 monitor_printf(mon, TARGET_FMT_plx ":", addr);
89854b95 554 } else {
376253ec 555 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
89854b95 556 }
9307c4c1
FB
557 l = len;
558 if (l > line_size)
559 l = line_size;
560 if (is_physical) {
6f89ae58
PM
561 AddressSpace *as = cs ? cs->as : &address_space_memory;
562 MemTxResult r = address_space_read(as, addr,
563 MEMTXATTRS_UNSPECIFIED, buf, l);
564 if (r != MEMTX_OK) {
565 monitor_printf(mon, " Cannot access memory\n");
566 break;
567 }
9307c4c1 568 } else {
854e67fe 569 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
376253ec 570 monitor_printf(mon, " Cannot access memory\n");
c8f79b67
AL
571 break;
572 }
9307c4c1 573 }
5fafdf24 574 i = 0;
9307c4c1
FB
575 while (i < l) {
576 switch(wsize) {
577 default:
578 case 1:
24e60305 579 v = ldub_p(buf + i);
9307c4c1
FB
580 break;
581 case 2:
24e60305 582 v = lduw_p(buf + i);
9307c4c1
FB
583 break;
584 case 4:
24e60305 585 v = (uint32_t)ldl_p(buf + i);
9307c4c1
FB
586 break;
587 case 8:
24e60305 588 v = ldq_p(buf + i);
9307c4c1
FB
589 break;
590 }
376253ec 591 monitor_printf(mon, " ");
9307c4c1
FB
592 switch(format) {
593 case 'o':
376253ec 594 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
9307c4c1
FB
595 break;
596 case 'x':
376253ec 597 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
9307c4c1
FB
598 break;
599 case 'u':
376253ec 600 monitor_printf(mon, "%*" PRIu64, max_digits, v);
9307c4c1
FB
601 break;
602 case 'd':
376253ec 603 monitor_printf(mon, "%*" PRId64, max_digits, v);
9307c4c1
FB
604 break;
605 case 'c':
376253ec 606 monitor_printc(mon, v);
9307c4c1
FB
607 break;
608 }
609 i += wsize;
610 }
376253ec 611 monitor_printf(mon, "\n");
9307c4c1
FB
612 addr += l;
613 len -= l;
614 }
615}
616
3e5a50d6 617static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 618{
1bd1442e
LC
619 int count = qdict_get_int(qdict, "count");
620 int format = qdict_get_int(qdict, "format");
621 int size = qdict_get_int(qdict, "size");
622 target_long addr = qdict_get_int(qdict, "addr");
623
376253ec 624 memory_dump(mon, count, format, size, addr, 0);
9307c4c1
FB
625}
626
3e5a50d6 627static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 628{
1bd1442e
LC
629 int count = qdict_get_int(qdict, "count");
630 int format = qdict_get_int(qdict, "format");
631 int size = qdict_get_int(qdict, "size");
a8170e5e 632 hwaddr addr = qdict_get_int(qdict, "addr");
1bd1442e 633
376253ec 634 memory_dump(mon, count, format, size, addr, 1);
9307c4c1
FB
635}
636
c7f7e697 637void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
e9628441 638{
c7f7e697 639 Int128 gpa_region_size;
e9628441 640 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
c7f7e697 641 addr, size);
e9628441
PB
642
643 if (!mrs.mr) {
644 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
645 return NULL;
646 }
647
648 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
649 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
650 memory_region_unref(mrs.mr);
651 return NULL;
652 }
653
c7f7e697
TFF
654 gpa_region_size = int128_make64(size);
655 if (int128_lt(mrs.size, gpa_region_size)) {
656 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
657 " exceeded.", addr);
658 memory_region_unref(mrs.mr);
659 return NULL;
660 }
661
e9628441
PB
662 *p_mr = mrs.mr;
663 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
664}
665
666static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
667{
668 hwaddr addr = qdict_get_int(qdict, "addr");
669 Error *local_err = NULL;
670 MemoryRegion *mr = NULL;
671 void *ptr;
672
c7f7e697 673 ptr = gpa2hva(&mr, addr, 1, &local_err);
e9628441
PB
674 if (local_err) {
675 error_report_err(local_err);
676 return;
677 }
678
679 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
680 " (%s) is %p\n",
681 addr, mr->name, ptr);
682
683 memory_region_unref(mr);
684}
685
574d9693
DDAG
686static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
687{
688 target_ulong addr = qdict_get_int(qdict, "addr");
689 MemTxAttrs attrs;
2fc5d01b 690 CPUState *cs = mon_get_cpu(mon);
574d9693
DDAG
691 hwaddr gpa;
692
693 if (!cs) {
694 monitor_printf(mon, "No cpu\n");
695 return;
696 }
697
9d3250d5 698 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
574d9693
DDAG
699 if (gpa == -1) {
700 monitor_printf(mon, "Unmapped\n");
701 } else {
702 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
703 gpa + (addr & ~TARGET_PAGE_MASK));
704 }
705}
706
e9628441
PB
707#ifdef CONFIG_LINUX
708static uint64_t vtop(void *ptr, Error **errp)
709{
710 uint64_t pinfo;
711 uint64_t ret = -1;
712 uintptr_t addr = (uintptr_t) ptr;
038adc2f 713 uintptr_t pagesize = qemu_real_host_page_size;
e9628441
PB
714 off_t offset = addr / pagesize * sizeof(pinfo);
715 int fd;
716
717 fd = open("/proc/self/pagemap", O_RDONLY);
718 if (fd == -1) {
719 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
720 return -1;
721 }
722
723 /* Force copy-on-write if necessary. */
d73415a3 724 qatomic_add((uint8_t *)ptr, 0);
e9628441
PB
725
726 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
727 error_setg_errno(errp, errno, "Cannot read pagemap");
728 goto out;
729 }
730 if ((pinfo & (1ull << 63)) == 0) {
731 error_setg(errp, "Page not present");
732 goto out;
733 }
734 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
735
736out:
737 close(fd);
738 return ret;
739}
740
741static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
742{
743 hwaddr addr = qdict_get_int(qdict, "addr");
744 Error *local_err = NULL;
745 MemoryRegion *mr = NULL;
746 void *ptr;
747 uint64_t physaddr;
748
c7f7e697 749 ptr = gpa2hva(&mr, addr, 1, &local_err);
e9628441
PB
750 if (local_err) {
751 error_report_err(local_err);
752 return;
753 }
754
755 physaddr = vtop(ptr, &local_err);
756 if (local_err) {
757 error_report_err(local_err);
758 } else {
759 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
760 " (%s) is 0x%" PRIx64 "\n",
761 addr, mr->name, (uint64_t) physaddr);
762 }
763
764 memory_region_unref(mr);
765}
766#endif
767
1bd1442e 768static void do_print(Monitor *mon, const QDict *qdict)
9307c4c1 769{
1bd1442e 770 int format = qdict_get_int(qdict, "format");
a8170e5e 771 hwaddr val = qdict_get_int(qdict, "val");
1bd1442e 772
9307c4c1
FB
773 switch(format) {
774 case 'o':
a8170e5e 775 monitor_printf(mon, "%#" HWADDR_PRIo, val);
9307c4c1
FB
776 break;
777 case 'x':
a8170e5e 778 monitor_printf(mon, "%#" HWADDR_PRIx, val);
9307c4c1
FB
779 break;
780 case 'u':
a8170e5e 781 monitor_printf(mon, "%" HWADDR_PRIu, val);
9307c4c1
FB
782 break;
783 default:
784 case 'd':
a8170e5e 785 monitor_printf(mon, "%" HWADDR_PRId, val);
9307c4c1
FB
786 break;
787 case 'c':
376253ec 788 monitor_printc(mon, val);
9307c4c1
FB
789 break;
790 }
376253ec 791 monitor_printf(mon, "\n");
9307c4c1
FB
792}
793
3e5a50d6 794static void hmp_sum(Monitor *mon, const QDict *qdict)
e4cf1adc
FB
795{
796 uint32_t addr;
e4cf1adc 797 uint16_t sum;
f18c16de
LC
798 uint32_t start = qdict_get_int(qdict, "start");
799 uint32_t size = qdict_get_int(qdict, "size");
e4cf1adc
FB
800
801 sum = 0;
802 for(addr = start; addr < (start + size); addr++) {
42874d3a
PM
803 uint8_t val = address_space_ldub(&address_space_memory, addr,
804 MEMTXATTRS_UNSPECIFIED, NULL);
e4cf1adc
FB
805 /* BSD sum algorithm ('sum' Unix command) */
806 sum = (sum >> 1) | (sum << 15);
54f7b4a3 807 sum += val;
e4cf1adc 808 }
376253ec 809 monitor_printf(mon, "%05d\n", sum);
e4cf1adc
FB
810}
811
13224a87
FB
812static int mouse_button_state;
813
3e5a50d6 814static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
13224a87 815{
c751a74a 816 int dx, dy, dz, button;
1d4daa91
LC
817 const char *dx_str = qdict_get_str(qdict, "dx_str");
818 const char *dy_str = qdict_get_str(qdict, "dy_str");
819 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
c751a74a 820
13224a87
FB
821 dx = strtol(dx_str, NULL, 0);
822 dy = strtol(dy_str, NULL, 0);
c751a74a
GH
823 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
824 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
825
826 if (dz_str) {
13224a87 827 dz = strtol(dz_str, NULL, 0);
c751a74a 828 if (dz != 0) {
f22d0af0 829 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
c751a74a
GH
830 qemu_input_queue_btn(NULL, button, true);
831 qemu_input_event_sync();
832 qemu_input_queue_btn(NULL, button, false);
833 }
834 }
835 qemu_input_event_sync();
13224a87
FB
836}
837
3e5a50d6 838static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
13224a87 839{
7fb1cf16 840 static uint32_t bmap[INPUT_BUTTON__MAX] = {
c751a74a
GH
841 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
842 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
843 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
844 };
d54908a5 845 int button_state = qdict_get_int(qdict, "button_state");
c751a74a
GH
846
847 if (mouse_button_state == button_state) {
848 return;
849 }
850 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
851 qemu_input_event_sync();
13224a87 852 mouse_button_state = button_state;
13224a87
FB
853}
854
3e5a50d6 855static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
3440557b 856{
aa93e39c
LC
857 int size = qdict_get_int(qdict, "size");
858 int addr = qdict_get_int(qdict, "addr");
859 int has_index = qdict_haskey(qdict, "index");
3440557b
FB
860 uint32_t val;
861 int suffix;
862
863 if (has_index) {
aa93e39c 864 int index = qdict_get_int(qdict, "index");
afcea8cb 865 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
3440557b
FB
866 addr++;
867 }
868 addr &= 0xffff;
869
870 switch(size) {
871 default:
872 case 1:
afcea8cb 873 val = cpu_inb(addr);
3440557b
FB
874 suffix = 'b';
875 break;
876 case 2:
afcea8cb 877 val = cpu_inw(addr);
3440557b
FB
878 suffix = 'w';
879 break;
880 case 4:
afcea8cb 881 val = cpu_inl(addr);
3440557b
FB
882 suffix = 'l';
883 break;
884 }
33b1fa94 885 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
376253ec 886 suffix, addr, size * 2, val);
3440557b 887}
a3a91a35 888
3e5a50d6 889static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
f114784f 890{
1bd1442e
LC
891 int size = qdict_get_int(qdict, "size");
892 int addr = qdict_get_int(qdict, "addr");
893 int val = qdict_get_int(qdict, "val");
894
f114784f
JK
895 addr &= IOPORTS_MASK;
896
897 switch (size) {
898 default:
899 case 1:
afcea8cb 900 cpu_outb(addr, val);
f114784f
JK
901 break;
902 case 2:
afcea8cb 903 cpu_outw(addr, val);
f114784f
JK
904 break;
905 case 4:
afcea8cb 906 cpu_outl(addr, val);
f114784f
JK
907 break;
908 }
909}
910
3e5a50d6 911static void hmp_boot_set(Monitor *mon, const QDict *qdict)
0ecdffbb 912{
f1839938 913 Error *local_err = NULL;
d54908a5 914 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
0ecdffbb 915
f1839938
GA
916 qemu_boot_set(bootdevice, &local_err);
917 if (local_err) {
193227f9 918 error_report_err(local_err);
0ecdffbb 919 } else {
f1839938 920 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
0ecdffbb
AJ
921 }
922}
923
1ce6be24 924static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
314e2987 925{
57bb40c9 926 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
5e8fd947 927 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
fc051ae6 928 bool owner = qdict_get_try_bool(qdict, "owner", false);
2261d393 929 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
57bb40c9 930
2261d393 931 mtree_info(flatview, dispatch_tree, owner, disabled);
314e2987
BS
932}
933
5f1ce948
FB
934#ifdef CONFIG_PROFILER
935
e9a6625e
AJ
936int64_t dev_time;
937
1ce6be24 938static void hmp_info_profile(Monitor *mon, const QDict *qdict)
5f1ce948 939{
72fd2efb
EC
940 static int64_t last_cpu_exec_time;
941 int64_t cpu_exec_time;
942 int64_t delta;
943
944 cpu_exec_time = tcg_cpu_exec_time();
945 delta = cpu_exec_time - last_cpu_exec_time;
946
376253ec 947 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
73bcb24d 948 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
376253ec 949 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
72fd2efb
EC
950 delta, delta / (double)NANOSECONDS_PER_SECOND);
951 last_cpu_exec_time = cpu_exec_time;
5f1ce948 952 dev_time = 0;
5f1ce948
FB
953}
954#else
1ce6be24 955static void hmp_info_profile(Monitor *mon, const QDict *qdict)
5f1ce948 956{
376253ec 957 monitor_printf(mon, "Internal profiler not compiled\n");
5f1ce948
FB
958}
959#endif
960
ec36b695 961/* Capture support */
72cf2d4f 962static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
ec36b695 963
1ce6be24 964static void hmp_info_capture(Monitor *mon, const QDict *qdict)
ec36b695
FB
965{
966 int i;
967 CaptureState *s;
968
969 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
376253ec 970 monitor_printf(mon, "[%d]: ", i);
ec36b695
FB
971 s->ops.info (s->opaque);
972 }
973}
974
3e5a50d6 975static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
ec36b695
FB
976{
977 int i;
d54908a5 978 int n = qdict_get_int(qdict, "n");
ec36b695
FB
979 CaptureState *s;
980
981 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
982 if (i == n) {
983 s->ops.destroy (s->opaque);
72cf2d4f 984 QLIST_REMOVE (s, entries);
7267c094 985 g_free (s);
ec36b695
FB
986 return;
987 }
988 }
989}
990
3e5a50d6 991static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
c1925484
LC
992{
993 const char *path = qdict_get_str(qdict, "path");
f0b9f36d
KZ
994 int freq = qdict_get_try_int(qdict, "freq", 44100);
995 int bits = qdict_get_try_int(qdict, "bits", 16);
996 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
997 const char *audiodev = qdict_get_str(qdict, "audiodev");
ec36b695 998 CaptureState *s;
f0b9f36d 999 AudioState *as = audio_state_by_name(audiodev);
ec36b695 1000
f0b9f36d
KZ
1001 if (!as) {
1002 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1003 return;
1004 }
ec36b695 1005
f0b9f36d 1006 s = g_malloc0 (sizeof (*s));
ec36b695 1007
f0b9f36d 1008 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
d00b2618 1009 monitor_printf(mon, "Failed to add wave capture\n");
7267c094 1010 g_free (s);
d00b2618 1011 return;
ec36b695 1012 }
72cf2d4f 1013 QLIST_INSERT_HEAD (&capture_head, s, entries);
ec36b695 1014}
ec36b695 1015
208c9d1b 1016void qmp_getfd(const char *fdname, Error **errp)
f07918fd 1017{
947e4744 1018 Monitor *cur_mon = monitor_cur();
c227f099 1019 mon_fd_t *monfd;
9409fc05 1020 int fd, tmp_fd;
f07918fd 1021
5345fdb4 1022 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
f07918fd 1023 if (fd == -1) {
f820af87 1024 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
208c9d1b 1025 return;
f07918fd
MM
1026 }
1027
1028 if (qemu_isdigit(fdname[0])) {
0b9f0e2f 1029 close(fd);
c6bd8c70
MA
1030 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1031 "a name not starting with a digit");
208c9d1b 1032 return;
f07918fd
MM
1033 }
1034
0210c3b3 1035 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
208c9d1b 1036 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
1037 if (strcmp(monfd->name, fdname) != 0) {
1038 continue;
1039 }
1040
9409fc05 1041 tmp_fd = monfd->fd;
f07918fd 1042 monfd->fd = fd;
774a6b67 1043 /* Make sure close() is outside critical section */
9409fc05 1044 close(tmp_fd);
208c9d1b 1045 return;
f07918fd
MM
1046 }
1047
7267c094
AL
1048 monfd = g_malloc0(sizeof(mon_fd_t));
1049 monfd->name = g_strdup(fdname);
f07918fd
MM
1050 monfd->fd = fd;
1051
208c9d1b 1052 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
f07918fd
MM
1053}
1054
208c9d1b 1055void qmp_closefd(const char *fdname, Error **errp)
f07918fd 1056{
947e4744 1057 Monitor *cur_mon = monitor_cur();
c227f099 1058 mon_fd_t *monfd;
9409fc05 1059 int tmp_fd;
f07918fd 1060
9409fc05 1061 qemu_mutex_lock(&cur_mon->mon_lock);
208c9d1b 1062 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
1063 if (strcmp(monfd->name, fdname) != 0) {
1064 continue;
1065 }
1066
72cf2d4f 1067 QLIST_REMOVE(monfd, next);
9409fc05 1068 tmp_fd = monfd->fd;
7267c094
AL
1069 g_free(monfd->name);
1070 g_free(monfd);
9409fc05 1071 qemu_mutex_unlock(&cur_mon->mon_lock);
774a6b67 1072 /* Make sure close() is outside critical section */
9409fc05 1073 close(tmp_fd);
208c9d1b 1074 return;
f07918fd
MM
1075 }
1076
9409fc05 1077 qemu_mutex_unlock(&cur_mon->mon_lock);
f820af87 1078 error_setg(errp, "File descriptor named '%s' not found", fdname);
f07918fd
MM
1079}
1080
a9940fc4 1081int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
7768e04c 1082{
c227f099 1083 mon_fd_t *monfd;
7768e04c 1084
0210c3b3 1085 QEMU_LOCK_GUARD(&mon->mon_lock);
72cf2d4f 1086 QLIST_FOREACH(monfd, &mon->fds, next) {
7768e04c
MM
1087 int fd;
1088
1089 if (strcmp(monfd->name, fdname) != 0) {
1090 continue;
1091 }
1092
1093 fd = monfd->fd;
1094
1095 /* caller takes ownership of fd */
72cf2d4f 1096 QLIST_REMOVE(monfd, next);
7267c094
AL
1097 g_free(monfd->name);
1098 g_free(monfd);
7768e04c
MM
1099
1100 return fd;
1101 }
1102
a9940fc4 1103 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
7768e04c
MM
1104 return -1;
1105}
1106
ba1c048a
CB
1107static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1108{
1109 MonFdsetFd *mon_fdset_fd;
1110 MonFdsetFd *mon_fdset_fd_next;
1111
1112 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
ebe52b59
CB
1113 if ((mon_fdset_fd->removed ||
1114 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1115 runstate_is_running()) {
ba1c048a
CB
1116 close(mon_fdset_fd->fd);
1117 g_free(mon_fdset_fd->opaque);
1118 QLIST_REMOVE(mon_fdset_fd, next);
1119 g_free(mon_fdset_fd);
1120 }
1121 }
1122
adb696f3 1123 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
ba1c048a
CB
1124 QLIST_REMOVE(mon_fdset, next);
1125 g_free(mon_fdset);
1126 }
1127}
1128
7e3c0dea 1129void monitor_fdsets_cleanup(void)
efb87c16
CB
1130{
1131 MonFdset *mon_fdset;
1132 MonFdset *mon_fdset_next;
1133
0210c3b3 1134 QEMU_LOCK_GUARD(&mon_fdsets_lock);
efb87c16
CB
1135 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1136 monitor_fdset_cleanup(mon_fdset);
1137 }
1138}
1139
ba1c048a
CB
1140AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1141 const char *opaque, Error **errp)
1142{
1143 int fd;
947e4744 1144 Monitor *mon = monitor_cur();
ba1c048a
CB
1145 AddfdInfo *fdinfo;
1146
5345fdb4 1147 fd = qemu_chr_fe_get_msgfd(&mon->chr);
ba1c048a 1148 if (fd == -1) {
f820af87 1149 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
ba1c048a
CB
1150 goto error;
1151 }
1152
e446f70d
CB
1153 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1154 has_opaque, opaque, errp);
1155 if (fdinfo) {
1156 return fdinfo;
ba1c048a 1157 }
ba1c048a
CB
1158
1159error:
1160 if (fd != -1) {
1161 close(fd);
1162 }
1163 return NULL;
1164}
1165
1166void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1167{
1168 MonFdset *mon_fdset;
1169 MonFdsetFd *mon_fdset_fd;
1170 char fd_str[60];
1171
0210c3b3 1172 QEMU_LOCK_GUARD(&mon_fdsets_lock);
ba1c048a
CB
1173 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1174 if (mon_fdset->id != fdset_id) {
1175 continue;
1176 }
1177 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1178 if (has_fd) {
1179 if (mon_fdset_fd->fd != fd) {
1180 continue;
1181 }
1182 mon_fdset_fd->removed = true;
1183 break;
1184 } else {
1185 mon_fdset_fd->removed = true;
1186 }
1187 }
1188 if (has_fd && !mon_fdset_fd) {
1189 goto error;
1190 }
1191 monitor_fdset_cleanup(mon_fdset);
1192 return;
1193 }
1194
1195error:
1196 if (has_fd) {
1197 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1198 fdset_id, fd);
1199 } else {
1200 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1201 }
f820af87 1202 error_setg(errp, "File descriptor named '%s' not found", fd_str);
ba1c048a
CB
1203}
1204
1205FdsetInfoList *qmp_query_fdsets(Error **errp)
1206{
1207 MonFdset *mon_fdset;
1208 MonFdsetFd *mon_fdset_fd;
1209 FdsetInfoList *fdset_list = NULL;
1210
0210c3b3 1211 QEMU_LOCK_GUARD(&mon_fdsets_lock);
ba1c048a 1212 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
54aa3de7 1213 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
ba1c048a 1214
54aa3de7 1215 fdset_info->fdset_id = mon_fdset->id;
ba1c048a
CB
1216
1217 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
54aa3de7 1218 FdsetFdInfo *fdsetfd_info;
ba1c048a
CB
1219
1220 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
54aa3de7 1221 fdsetfd_info->fd = mon_fdset_fd->fd;
ba1c048a 1222 if (mon_fdset_fd->opaque) {
54aa3de7
EB
1223 fdsetfd_info->has_opaque = true;
1224 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
ba1c048a 1225 } else {
54aa3de7 1226 fdsetfd_info->has_opaque = false;
ba1c048a
CB
1227 }
1228
54aa3de7 1229 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
ba1c048a
CB
1230 }
1231
54aa3de7 1232 QAPI_LIST_PREPEND(fdset_list, fdset_info);
ba1c048a
CB
1233 }
1234
1235 return fdset_list;
1236}
1237
e446f70d
CB
1238AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1239 bool has_opaque, const char *opaque,
1240 Error **errp)
1241{
1242 MonFdset *mon_fdset = NULL;
1243 MonFdsetFd *mon_fdset_fd;
1244 AddfdInfo *fdinfo;
1245
6e8a355d 1246 QEMU_LOCK_GUARD(&mon_fdsets_lock);
e446f70d
CB
1247 if (has_fdset_id) {
1248 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1249 /* Break if match found or match impossible due to ordering by ID */
1250 if (fdset_id <= mon_fdset->id) {
1251 if (fdset_id < mon_fdset->id) {
1252 mon_fdset = NULL;
1253 }
1254 break;
1255 }
1256 }
1257 }
1258
1259 if (mon_fdset == NULL) {
1260 int64_t fdset_id_prev = -1;
1261 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1262
1263 if (has_fdset_id) {
1264 if (fdset_id < 0) {
c6bd8c70
MA
1265 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1266 "a non-negative value");
e446f70d
CB
1267 return NULL;
1268 }
1269 /* Use specified fdset ID */
1270 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1271 mon_fdset_cur = mon_fdset;
1272 if (fdset_id < mon_fdset_cur->id) {
1273 break;
1274 }
1275 }
1276 } else {
1277 /* Use first available fdset ID */
1278 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1279 mon_fdset_cur = mon_fdset;
1280 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1281 fdset_id_prev = mon_fdset_cur->id;
1282 continue;
1283 }
1284 break;
1285 }
1286 }
1287
1288 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1289 if (has_fdset_id) {
1290 mon_fdset->id = fdset_id;
1291 } else {
1292 mon_fdset->id = fdset_id_prev + 1;
1293 }
1294
1295 /* The fdset list is ordered by fdset ID */
1296 if (!mon_fdset_cur) {
1297 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1298 } else if (mon_fdset->id < mon_fdset_cur->id) {
1299 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1300 } else {
1301 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1302 }
1303 }
1304
1305 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1306 mon_fdset_fd->fd = fd;
1307 mon_fdset_fd->removed = false;
1308 if (has_opaque) {
1309 mon_fdset_fd->opaque = g_strdup(opaque);
1310 }
1311 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1312
1313 fdinfo = g_malloc0(sizeof(*fdinfo));
1314 fdinfo->fdset_id = mon_fdset->id;
1315 fdinfo->fd = mon_fdset_fd->fd;
1316
1317 return fdinfo;
1318}
1319
60efffa4 1320int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
adb696f3 1321{
47451466
PX
1322#ifdef _WIN32
1323 return -ENOENT;
1324#else
adb696f3 1325 MonFdset *mon_fdset;
adb696f3 1326
0210c3b3 1327 QEMU_LOCK_GUARD(&mon_fdsets_lock);
adb696f3 1328 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
60efffa4
DB
1329 MonFdsetFd *mon_fdset_fd;
1330 MonFdsetFd *mon_fdset_fd_dup;
1331 int fd = -1;
1332 int dup_fd;
1333 int mon_fd_flags;
1334
adb696f3
CB
1335 if (mon_fdset->id != fdset_id) {
1336 continue;
1337 }
60efffa4 1338
adb696f3
CB
1339 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1340 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1341 if (mon_fd_flags == -1) {
60efffa4 1342 return -1;
adb696f3
CB
1343 }
1344
1345 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
60efffa4
DB
1346 fd = mon_fdset_fd->fd;
1347 break;
adb696f3
CB
1348 }
1349 }
adb696f3 1350
60efffa4 1351 if (fd == -1) {
60efffa4
DB
1352 errno = EACCES;
1353 return -1;
adb696f3 1354 }
60efffa4
DB
1355
1356 dup_fd = qemu_dup_flags(fd, flags);
1357 if (dup_fd == -1) {
60efffa4 1358 return -1;
adb696f3 1359 }
60efffa4 1360
adb696f3
CB
1361 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1362 mon_fdset_fd_dup->fd = dup_fd;
1363 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
60efffa4 1364 return dup_fd;
adb696f3 1365 }
47451466 1366
60efffa4 1367 errno = ENOENT;
adb696f3 1368 return -1;
60efffa4 1369#endif
adb696f3
CB
1370}
1371
854f63d4 1372static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
adb696f3
CB
1373{
1374 MonFdset *mon_fdset;
1375 MonFdsetFd *mon_fdset_fd_dup;
1376
0210c3b3 1377 QEMU_LOCK_GUARD(&mon_fdsets_lock);
adb696f3
CB
1378 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1379 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1380 if (mon_fdset_fd_dup->fd == dup_fd) {
1381 if (remove) {
1382 QLIST_REMOVE(mon_fdset_fd_dup, next);
a661614d 1383 g_free(mon_fdset_fd_dup);
adb696f3
CB
1384 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1385 monitor_fdset_cleanup(mon_fdset);
1386 }
0210c3b3 1387 return -1;
b3dd1b8c
MT
1388 } else {
1389 return mon_fdset->id;
adb696f3 1390 }
adb696f3
CB
1391 }
1392 }
1393 }
47451466 1394
adb696f3
CB
1395 return -1;
1396}
1397
854f63d4 1398int64_t monitor_fdset_dup_fd_find(int dup_fd)
adb696f3
CB
1399{
1400 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1401}
1402
b3dd1b8c 1403void monitor_fdset_dup_fd_remove(int dup_fd)
adb696f3 1404{
b3dd1b8c 1405 monitor_fdset_dup_fd_find_remove(dup_fd, true);
adb696f3
CB
1406}
1407
1677f4c6 1408int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
5906366e
LE
1409{
1410 int fd;
1411 Error *local_err = NULL;
a96ed02f 1412
5906366e 1413 if (!qemu_isdigit(fdname[0]) && mon) {
a9940fc4 1414 fd = monitor_get_fd(mon, fdname, &local_err);
5906366e
LE
1415 } else {
1416 fd = qemu_parse_fd(fdname);
a96ed02f 1417 if (fd == -1) {
5906366e
LE
1418 error_setg(&local_err, "Invalid file descriptor number '%s'",
1419 fdname);
a96ed02f 1420 }
5906366e
LE
1421 }
1422 if (local_err) {
1423 error_propagate(errp, local_err);
1424 assert(fd == -1);
a96ed02f 1425 } else {
5906366e 1426 assert(fd != -1);
a96ed02f
NB
1427 }
1428
1429 return fd;
1430}
1431
acd0a093 1432/* Please update hmp-commands.hx when adding or changing commands */
a0cd5e1c 1433static HMPCommand hmp_info_cmds[] = {
da76ee76
PB
1434#include "hmp-commands-info.h"
1435 { NULL, NULL, },
9dc39cba
FB
1436};
1437
a0cd5e1c 1438/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
ed7bda5d 1439HMPCommand hmp_cmds[] = {
a13ced59
WX
1440#include "hmp-commands.h"
1441 { NULL, NULL, },
1442};
1443
ed7bda5d
KW
1444/*
1445 * Set @pval to the value in the register identified by @name.
1446 * return 0 if OK, -1 if not found
1447 */
2fc5d01b 1448int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
9307c4c1 1449{
bf957284 1450 const MonitorDef *md = target_monitor_defs();
2fc5d01b 1451 CPUState *cs = mon_get_cpu(mon);
92a31b1f 1452 void *ptr;
0a9516c2
AK
1453 uint64_t tmp = 0;
1454 int ret;
92a31b1f 1455
854e67fe 1456 if (cs == NULL || md == NULL) {
bf957284
PB
1457 return -1;
1458 }
1459
1460 for(; md->name != NULL; md++) {
ed7bda5d 1461 if (hmp_compare_cmd(name, md->name)) {
9307c4c1 1462 if (md->get_value) {
43cf067f 1463 *pval = md->get_value(mon, md, md->offset);
9307c4c1 1464 } else {
e7cff9c6 1465 CPUArchState *env = mon_get_cpu_env(mon);
6a00d601 1466 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
1467 switch(md->type) {
1468 case MD_I32:
1469 *pval = *(int32_t *)ptr;
1470 break;
1471 case MD_TLONG:
1472 *pval = *(target_long *)ptr;
1473 break;
1474 default:
1475 *pval = 0;
1476 break;
1477 }
9307c4c1
FB
1478 }
1479 return 0;
1480 }
1481 }
0a9516c2 1482
854e67fe 1483 ret = target_get_monitor_def(cs, name, &tmp);
0a9516c2
AK
1484 if (!ret) {
1485 *pval = (target_long) tmp;
1486 }
1487
1488 return ret;
9307c4c1
FB
1489}
1490
40d19394
HB
1491static void add_completion_option(ReadLineState *rs, const char *str,
1492 const char *option)
1493{
1494 if (!str || !option) {
1495 return;
1496 }
1497 if (!strncmp(option, str, strlen(str))) {
1498 readline_add_completion(rs, option);
1499 }
1500}
1501
13e315da
HB
1502void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1503{
1504 size_t len;
1505 ChardevBackendInfoList *list, *start;
1506
1507 if (nb_args != 2) {
1508 return;
1509 }
1510 len = strlen(str);
1511 readline_set_completion_index(rs, len);
1512
1513 start = list = qmp_query_chardev_backends(NULL);
1514 while (list) {
1515 const char *chr_name = list->value->name;
1516
1517 if (!strncmp(chr_name, str, len)) {
1518 readline_add_completion(rs, chr_name);
1519 }
1520 list = list->next;
1521 }
1522 qapi_free_ChardevBackendInfoList(start);
1523}
1524
b162b49a
HB
1525void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1526{
1527 size_t len;
1528 int i;
1529
1530 if (nb_args != 2) {
1531 return;
1532 }
1533 len = strlen(str);
1534 readline_set_completion_index(rs, len);
1c236ba5 1535 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
977c736f 1536 add_completion_option(rs, str, NetClientDriver_str(i));
b162b49a
HB
1537 }
1538}
1539
2da1b3ab 1540void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
992d3e64
HB
1541{
1542 GSList *list, *elt;
1543 size_t len;
1544
2da1b3ab
HB
1545 if (nb_args != 2) {
1546 return;
1547 }
1548
992d3e64
HB
1549 len = strlen(str);
1550 readline_set_completion_index(rs, len);
1551 list = elt = object_class_get_list(TYPE_DEVICE, false);
1552 while (elt) {
1553 const char *name;
1554 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1555 TYPE_DEVICE);
1556 name = object_class_get_name(OBJECT_CLASS(dc));
2da1b3ab 1557
e90f2a8c 1558 if (dc->user_creatable
2da1b3ab 1559 && !strncmp(name, str, len)) {
992d3e64
HB
1560 readline_add_completion(rs, name);
1561 }
1562 elt = elt->next;
1563 }
1564 g_slist_free(list);
1565}
1566
bfa40f77 1567void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1094fd3a
HB
1568{
1569 GSList *list, *elt;
1570 size_t len;
1571
bfa40f77
HB
1572 if (nb_args != 2) {
1573 return;
1574 }
1575
1094fd3a
HB
1576 len = strlen(str);
1577 readline_set_completion_index(rs, len);
1578 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1579 while (elt) {
1580 const char *name;
1581
1582 name = object_class_get_name(OBJECT_CLASS(elt->data));
1583 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1584 readline_add_completion(rs, name);
1585 }
1586 elt = elt->next;
1587 }
1588 g_slist_free(list);
1589}
1590
91590159
MAL
1591static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1592{
1593 GSList **list = opaque;
688ffbb4 1594 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
91590159
MAL
1595
1596 if (dev == NULL) {
1597 return 0;
1598 }
1599
1600 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1601 *list = g_slist_append(*list, dev);
1602 }
1603
1604 return 0;
1605}
1606
1607static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1608{
1609 GSList *list = NULL;
1610
1611 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1612
1613 return list;
1614}
1615
6a1fa9f5
ZG
1616static void peripheral_device_del_completion(ReadLineState *rs,
1617 const char *str, size_t len)
1618{
4cae4d5a
MA
1619 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1620 GSList *list, *item;
6a1fa9f5 1621
4cae4d5a
MA
1622 list = qdev_build_hotpluggable_device_list(peripheral);
1623 if (!list) {
6a1fa9f5
ZG
1624 return;
1625 }
1626
6a1fa9f5
ZG
1627 for (item = list; item; item = g_slist_next(item)) {
1628 DeviceState *dev = item->data;
1629
1630 if (dev->id && !strncmp(str, dev->id, len)) {
1631 readline_add_completion(rs, dev->id);
1632 }
1633 }
1634
1635 g_slist_free(list);
1636}
1637
6297d9a2
HB
1638void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1639{
1640 size_t len;
1641 ChardevInfoList *list, *start;
1642
1643 if (nb_args != 2) {
1644 return;
1645 }
1646 len = strlen(str);
1647 readline_set_completion_index(rs, len);
1648
1649 start = list = qmp_query_chardev(NULL);
1650 while (list) {
1651 ChardevInfo *chr = list->value;
1652
1653 if (!strncmp(chr->label, str, len)) {
1654 readline_add_completion(rs, chr->label);
1655 }
1656 list = list->next;
1657 }
1658 qapi_free_ChardevInfoList(start);
1659}
1660
8e597779
HB
1661static void ringbuf_completion(ReadLineState *rs, const char *str)
1662{
1663 size_t len;
1664 ChardevInfoList *list, *start;
1665
1666 len = strlen(str);
1667 readline_set_completion_index(rs, len);
1668
1669 start = list = qmp_query_chardev(NULL);
1670 while (list) {
1671 ChardevInfo *chr_info = list->value;
1672
1673 if (!strncmp(chr_info->label, str, len)) {
0ec7b3e7 1674 Chardev *chr = qemu_chr_find(chr_info->label);
777357d7 1675 if (chr && CHARDEV_IS_RINGBUF(chr)) {
8e597779
HB
1676 readline_add_completion(rs, chr_info->label);
1677 }
1678 }
1679 list = list->next;
1680 }
1681 qapi_free_ChardevInfoList(start);
1682}
1683
8e597779
HB
1684void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1685{
1686 if (nb_args != 2) {
1687 return;
1688 }
1689 ringbuf_completion(rs, str);
1690}
1691
2da1b3ab
HB
1692void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1693{
1694 size_t len;
1695
1696 if (nb_args != 2) {
1697 return;
1698 }
1699
1700 len = strlen(str);
1701 readline_set_completion_index(rs, len);
6a1fa9f5 1702 peripheral_device_del_completion(rs, str, len);
2da1b3ab
HB
1703}
1704
bfa40f77 1705void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
b48fa074
HB
1706{
1707 ObjectPropertyInfoList *list, *start;
1708 size_t len;
1709
bfa40f77
HB
1710 if (nb_args != 2) {
1711 return;
1712 }
b48fa074
HB
1713 len = strlen(str);
1714 readline_set_completion_index(rs, len);
1715
1716 start = list = qmp_qom_list("/objects", NULL);
1717 while (list) {
1718 ObjectPropertyInfo *info = list->value;
1719
1720 if (!strncmp(info->type, "child<", 5)
1721 && !strncmp(info->name, str, len)) {
1722 readline_add_completion(rs, info->name);
1723 }
1724 list = list->next;
1725 }
1726 qapi_free_ObjectPropertyInfoList(start);
1727}
1728
29136cd8
HB
1729void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1730{
1731 int i;
1732 char *sep;
1733 size_t len;
1734
1735 if (nb_args != 2) {
1736 return;
1737 }
1738 sep = strrchr(str, '-');
1739 if (sep) {
1740 str = sep + 1;
1741 }
1742 len = strlen(str);
1743 readline_set_completion_index(rs, len);
7fb1cf16 1744 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
977c736f
MA
1745 if (!strncmp(str, QKeyCode_str(i), len)) {
1746 readline_add_completion(rs, QKeyCode_str(i));
29136cd8
HB
1747 }
1748 }
1749}
1750
40d19394
HB
1751void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1752{
1753 size_t len;
1754
1755 len = strlen(str);
1756 readline_set_completion_index(rs, len);
1757 if (nb_args == 2) {
eaed483c 1758 NetClientState *ncs[MAX_QUEUE_NUM];
40d19394
HB
1759 int count, i;
1760 count = qemu_find_net_clients_except(NULL, ncs,
f394b2e2 1761 NET_CLIENT_DRIVER_NONE,
eaed483c 1762 MAX_QUEUE_NUM);
bcfa4d60 1763 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
40d19394
HB
1764 const char *name = ncs[i]->name;
1765 if (!strncmp(str, name, len)) {
1766 readline_add_completion(rs, name);
1767 }
1768 }
1769 } else if (nb_args == 3) {
1770 add_completion_option(rs, str, "on");
1771 add_completion_option(rs, str, "off");
1772 }
1773}
1774
11b389f2
HB
1775void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1776{
1777 int len, count, i;
eaed483c 1778 NetClientState *ncs[MAX_QUEUE_NUM];
11b389f2
HB
1779
1780 if (nb_args != 2) {
1781 return;
1782 }
1783
1784 len = strlen(str);
1785 readline_set_completion_index(rs, len);
f394b2e2 1786 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
eaed483c 1787 MAX_QUEUE_NUM);
bcfa4d60 1788 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
11b389f2
HB
1789 const char *name = ncs[i]->name;
1790 if (strncmp(str, name, len)) {
1791 continue;
1792 }
08712fcb 1793 if (ncs[i]->is_netdev) {
11b389f2
HB
1794 readline_add_completion(rs, name);
1795 }
1796 }
1797}
1798
bd71211d
LV
1799void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1800{
1801 size_t len;
1802
1803 len = strlen(str);
1804 readline_set_completion_index(rs, len);
1805 if (nb_args == 2) {
0d4e995c
DB
1806 TraceEventIter iter;
1807 TraceEvent *ev;
1808 char *pattern = g_strdup_printf("%s*", str);
117856c3 1809 trace_event_iter_init_pattern(&iter, pattern);
0d4e995c
DB
1810 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1811 readline_add_completion(rs, trace_event_get_name(ev));
bd71211d 1812 }
0d4e995c 1813 g_free(pattern);
bd71211d
LV
1814 }
1815}
1816
987bd270
DDAG
1817void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1818{
1819 size_t len;
1820
1821 len = strlen(str);
1822 readline_set_completion_index(rs, len);
1823 if (nb_args == 2) {
0d4e995c
DB
1824 TraceEventIter iter;
1825 TraceEvent *ev;
1826 char *pattern = g_strdup_printf("%s*", str);
117856c3 1827 trace_event_iter_init_pattern(&iter, pattern);
0d4e995c
DB
1828 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1829 readline_add_completion(rs, trace_event_get_name(ev));
1830 }
1831 g_free(pattern);
987bd270
DDAG
1832 } else if (nb_args == 3) {
1833 add_completion_option(rs, str, "on");
1834 add_completion_option(rs, str, "off");
1835 }
1836}
1837
d0ece345
HB
1838void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1839{
4bb08af3
HB
1840 int i;
1841
d0ece345
HB
1842 if (nb_args != 2) {
1843 return;
1844 }
1845 readline_set_completion_index(rs, strlen(str));
14d53b4f
MP
1846 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1847 add_completion_option(rs, str, WatchdogAction_str(i));
4bb08af3 1848 }
d0ece345
HB
1849}
1850
c68a0409
HB
1851void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1852 const char *str)
1853{
1854 size_t len;
1855
1856 len = strlen(str);
1857 readline_set_completion_index(rs, len);
1858 if (nb_args == 2) {
1859 int i;
7fb1cf16 1860 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
977c736f 1861 const char *name = MigrationCapability_str(i);
c68a0409
HB
1862 if (!strncmp(str, name, len)) {
1863 readline_add_completion(rs, name);
1864 }
1865 }
1866 } else if (nb_args == 3) {
1867 add_completion_option(rs, str, "on");
1868 add_completion_option(rs, str, "off");
1869 }
1870}
1871
50e9a629
LL
1872void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1873 const char *str)
1874{
1875 size_t len;
1876
1877 len = strlen(str);
1878 readline_set_completion_index(rs, len);
1879 if (nb_args == 2) {
1880 int i;
7fb1cf16 1881 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
977c736f 1882 const char *name = MigrationParameter_str(i);
50e9a629
LL
1883 if (!strncmp(str, name, len)) {
1884 readline_add_completion(rs, name);
1885 }
1886 }
1887 }
1888}
1889
b21631f3
HB
1890static void vm_completion(ReadLineState *rs, const char *str)
1891{
1892 size_t len;
7c8eece4 1893 BlockDriverState *bs;
88be7b4b 1894 BdrvNextIterator it;
b21631f3
HB
1895
1896 len = strlen(str);
1897 readline_set_completion_index(rs, len);
7c8eece4 1898
88be7b4b 1899 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
b21631f3 1900 SnapshotInfoList *snapshots, *snapshot;
6bf1faa8
DL
1901 AioContext *ctx = bdrv_get_aio_context(bs);
1902 bool ok = false;
b21631f3 1903
6bf1faa8
DL
1904 aio_context_acquire(ctx);
1905 if (bdrv_can_snapshot(bs)) {
1906 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
b21631f3 1907 }
6bf1faa8
DL
1908 aio_context_release(ctx);
1909 if (!ok) {
b21631f3
HB
1910 continue;
1911 }
6bf1faa8 1912
b21631f3
HB
1913 snapshot = snapshots;
1914 while (snapshot) {
1915 char *completion = snapshot->value->name;
1916 if (!strncmp(str, completion, len)) {
1917 readline_add_completion(rs, completion);
1918 }
1919 completion = snapshot->value->id;
1920 if (!strncmp(str, completion, len)) {
1921 readline_add_completion(rs, completion);
1922 }
1923 snapshot = snapshot->next;
1924 }
1925 qapi_free_SnapshotInfoList(snapshots);
1926 }
1927
1928}
1929
1930void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1931{
1932 if (nb_args == 2) {
1933 vm_completion(rs, str);
1934 }
1935}
1936
1937void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1938{
1939 if (nb_args == 2) {
1940 vm_completion(rs, str);
1941 }
1942}
1943
816f8925
WX
1944static int
1945compare_mon_cmd(const void *a, const void *b)
1946{
a0cd5e1c
KW
1947 return strcmp(((const HMPCommand *)a)->name,
1948 ((const HMPCommand *)b)->name);
816f8925
WX
1949}
1950
1951static void sortcmdlist(void)
1952{
a0cd5e1c
KW
1953 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1954 sizeof(*hmp_cmds),
1955 compare_mon_cmd);
1956 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1957 sizeof(*hmp_info_cmds),
1958 compare_mon_cmd);
816f8925
WX
1959}
1960
f0e48cbd
GH
1961void monitor_register_hmp(const char *name, bool info,
1962 void (*cmd)(Monitor *mon, const QDict *qdict))
1963{
1964 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1965
1966 while (table->name != NULL) {
1967 if (strcmp(table->name, name) == 0) {
f9429c67 1968 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
f0e48cbd
GH
1969 table->cmd = cmd;
1970 return;
1971 }
1972 table++;
1973 }
1974 g_assert_not_reached();
1975}
1976
f9429c67
DB
1977void monitor_register_hmp_info_hrt(const char *name,
1978 HumanReadableText *(*handler)(Error **errp))
1979{
1980 HMPCommand *table = hmp_info_cmds;
1981
1982 while (table->name != NULL) {
1983 if (strcmp(table->name, name) == 0) {
1984 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1985 table->cmd_info_hrt = handler;
1986 return;
1987 }
1988 table++;
1989 }
1990 g_assert_not_reached();
1991}
1992
6adf08dd
PX
1993void monitor_init_globals(void)
1994{
1d95db74 1995 monitor_init_globals_core();
6adf08dd 1996 monitor_init_qmp_commands();
6adf08dd 1997 sortcmdlist();
47451466 1998 qemu_mutex_init(&mon_fdsets_lock);
aa455485 1999}
This page took 1.764807 seconds and 4 git commands to generate.