blob: 80b2e5ff9fdadfcd88b5d11206d931a24884d23b [file] [log] [blame]
Anthony Liguori48a32be2011-09-02 12:34:48 -05001/*
Kevin Wolff1b3ccf2019-06-13 17:33:58 +02002 * Human Monitor Interface commands
Anthony Liguori48a32be2011-09-02 12:34:48 -05003 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Anthony Liguori48a32be2011-09-02 12:34:48 -050014 */
15
Peter Maydelld38ea872016-01-29 17:50:05 +000016#include "qemu/osdep.h"
Markus Armbrustercbf81992023-01-24 13:19:43 +010017#include "exec/address-spaces.h"
Markus Armbrustercbf81992023-01-24 13:19:43 +010018#include "exec/ioport.h"
Alex Bennée5b7d54d2024-06-20 16:22:10 +010019#include "exec/gdbstub.h"
20#include "gdbstub/enums.h"
Markus Armbruster275307a2019-06-19 22:10:36 +020021#include "monitor/hmp.h"
Paolo Bonziniad6f9322020-11-11 05:52:22 -050022#include "qemu/help_option.h"
Markus Armbrustercbf81992023-01-24 13:19:43 +010023#include "monitor/monitor-internal.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010024#include "qapi/error.h"
Kevin Wolffa4dcf52020-01-29 11:22:37 +010025#include "qapi/qapi-commands-control.h"
Paolo Bonzini1935b7e2024-05-07 14:13:46 +020026#include "qapi/qapi-commands-machine.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060027#include "qapi/qapi-commands-misc.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010028#include "qapi/qmp/qdict.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020029#include "qemu/cutils.h"
Markus Armbrustercbf81992023-01-24 13:19:43 +010030#include "qemu/log.h"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010031#include "system/system.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050032
Daniel P. Berrangé0ca117a2021-10-28 16:18:25 +010033bool hmp_handle_error(Monitor *mon, Error *err)
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020034{
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +030035 if (err) {
36 error_reportf_err(err, "Error: ");
Daniel P. Berrangé0ca117a2021-10-28 16:18:25 +010037 return true;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020038 }
Daniel P. Berrangé0ca117a2021-10-28 16:18:25 +010039 return false;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020040}
41
Dr. David Alan Gilbert08528272019-06-20 19:47:03 +010042/*
Markus Armbruster0d792712023-01-24 13:19:28 +010043 * Split @str at comma.
44 * A null @str defaults to "".
Dr. David Alan Gilbert08528272019-06-20 19:47:03 +010045 */
Markus Armbruster0d792712023-01-24 13:19:28 +010046strList *hmp_split_at_comma(const char *str)
Dr. David Alan Gilbert08528272019-06-20 19:47:03 +010047{
Markus Armbruster0d792712023-01-24 13:19:28 +010048 char **split = g_strsplit(str ?: "", ",", -1);
Dr. David Alan Gilbert08528272019-06-20 19:47:03 +010049 strList *res = NULL;
Eric Blakec3033fd2021-01-13 16:10:12 -060050 strList **tail = &res;
Markus Armbruster0d792712023-01-24 13:19:28 +010051 int i;
Dr. David Alan Gilbert08528272019-06-20 19:47:03 +010052
Markus Armbruster0d792712023-01-24 13:19:28 +010053 for (i = 0; split[i]; i++) {
54 QAPI_LIST_APPEND(tail, split[i]);
Dr. David Alan Gilbert08528272019-06-20 19:47:03 +010055 }
56
Markus Armbruster0d792712023-01-24 13:19:28 +010057 g_free(split);
Dr. David Alan Gilbert08528272019-06-20 19:47:03 +010058 return res;
59}
60
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080061void hmp_info_name(Monitor *mon, const QDict *qdict)
Anthony Liguori48a32be2011-09-02 12:34:48 -050062{
63 NameInfo *info;
64
65 info = qmp_query_name(NULL);
Markus Armbruster94927182022-11-04 17:06:59 +010066 if (info->name) {
Anthony Liguori48a32be2011-09-02 12:34:48 -050067 monitor_printf(mon, "%s\n", info->name);
68 }
69 qapi_free_NameInfo(info);
70}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030071
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080072void hmp_info_version(Monitor *mon, const QDict *qdict)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030073{
74 VersionInfo *info;
75
76 info = qmp_query_version(NULL);
77
78 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
Eric Blake4752cdb2015-05-04 09:05:31 -060079 info->qemu->major, info->qemu->minor, info->qemu->micro,
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030080 info->package);
81
82 qapi_free_VersionInfo(info);
83}
Luiz Capitulino292a2602011-09-12 15:10:53 -030084
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030085void hmp_quit(Monitor *mon, const QDict *qdict)
86{
87 monitor_suspend(mon);
88 qmp_quit(NULL);
89}
Luiz Capitulino5f158f22011-09-15 14:34:39 -030090
91void hmp_stop(Monitor *mon, const QDict *qdict)
92{
93 qmp_stop(NULL);
94}
Luiz Capitulino38d22652011-09-15 14:41:46 -030095
Emilio G. Cotadd12e1b2018-08-15 16:00:03 -040096void hmp_sync_profile(Monitor *mon, const QDict *qdict)
97{
98 const char *op = qdict_get_try_str(qdict, "op");
99
100 if (op == NULL) {
101 bool on = qsp_is_enabled();
102
103 monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
104 return;
105 }
106 if (!strcmp(op, "on")) {
107 qsp_enable();
108 } else if (!strcmp(op, "off")) {
109 qsp_disable();
110 } else if (!strcmp(op, "reset")) {
111 qsp_reset();
112 } else {
113 Error *err = NULL;
114
Markus Armbruster7200fb22023-10-31 12:10:54 +0100115 error_setg(&err, "invalid parameter '%s',"
116 " expecting 'on', 'off', or 'reset'", op);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300117 hmp_handle_error(mon, err);
Emilio G. Cotadd12e1b2018-08-15 16:00:03 -0400118 }
119}
120
Dr. David Alan Gilbert8e8581e2018-06-20 16:39:46 +0100121void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
122{
123 Error *err = NULL;
124
Markus Armbruster361ac942018-07-05 11:14:02 +0200125 qmp_x_exit_preconfig(&err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300126 hmp_handle_error(mon, err);
Dr. David Alan Gilbert8e8581e2018-06-20 16:39:46 +0100127}
128
Luiz Capitulino755f1962011-10-06 14:31:39 -0300129void hmp_cpu(Monitor *mon, const QDict *qdict)
130{
131 int64_t cpu_index;
132
133 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
134 use it are converted to the QAPI */
135 cpu_index = qdict_get_int(qdict, "index");
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200136 if (monitor_set_cpu(mon, cpu_index) < 0) {
Luiz Capitulino755f1962011-10-06 14:31:39 -0300137 monitor_printf(mon, "invalid CPU index\n");
138 }
139}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200140
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200141void hmp_cont(Monitor *mon, const QDict *qdict)
142{
Markus Armbrustere940f542014-05-02 13:26:29 +0200143 Error *err = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200144
Markus Armbrustere940f542014-05-02 13:26:29 +0200145 qmp_cont(&err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300146 hmp_handle_error(mon, err);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200147}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200148
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200149void hmp_change(Monitor *mon, const QDict *qdict)
150{
151 const char *device = qdict_get_str(qdict, "device");
152 const char *target = qdict_get_str(qdict, "target");
153 const char *arg = qdict_get_try_str(qdict, "arg");
Max Reitzbaead0a2015-10-26 21:39:18 +0100154 const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
Denis V. Lunev80dd5af2022-04-13 01:18:46 +0300155 bool force = qdict_get_try_bool(qdict, "force", false);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200156 Error *err = NULL;
157
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200158#ifdef CONFIG_VNC
Max Reitz10686742015-10-26 21:39:16 +0100159 if (strcmp(device, "vnc") == 0) {
Markus Armbrusterf916a172023-01-09 20:03:17 +0100160 hmp_change_vnc(mon, device, target, arg, read_only, force, &err);
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200161 } else
162#endif
163 {
Markus Armbrusterfa1d2f82023-01-24 13:19:26 +0100164 hmp_change_medium(mon, device, target, arg, read_only, force, &err);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200165 }
166
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300167 hmp_handle_error(mon, err);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200168}
Luiz Capitulino80047da2011-12-14 16:49:14 -0200169
Marc-André Lureau4bf21c72023-03-02 19:05:41 +0400170#ifdef CONFIG_POSIX
Corey Bryant208c9d12012-06-22 14:36:09 -0400171void hmp_getfd(Monitor *mon, const QDict *qdict)
172{
173 const char *fdname = qdict_get_str(qdict, "fdname");
Markus Armbrustere940f542014-05-02 13:26:29 +0200174 Error *err = NULL;
Corey Bryant208c9d12012-06-22 14:36:09 -0400175
Markus Armbrustere940f542014-05-02 13:26:29 +0200176 qmp_getfd(fdname, &err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300177 hmp_handle_error(mon, err);
Corey Bryant208c9d12012-06-22 14:36:09 -0400178}
Marc-André Lureau4bf21c72023-03-02 19:05:41 +0400179#endif
Corey Bryant208c9d12012-06-22 14:36:09 -0400180
181void hmp_closefd(Monitor *mon, const QDict *qdict)
182{
183 const char *fdname = qdict_get_str(qdict, "fdname");
Markus Armbrustere940f542014-05-02 13:26:29 +0200184 Error *err = NULL;
Corey Bryant208c9d12012-06-22 14:36:09 -0400185
Markus Armbrustere940f542014-05-02 13:26:29 +0200186 qmp_closefd(fdname, &err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300187 hmp_handle_error(mon, err);
Corey Bryant208c9d12012-06-22 14:36:09 -0400188}
Amos Konge4c8f002012-08-31 10:56:26 +0800189
Ting Wang62313162015-06-26 16:07:13 +0800190void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
191{
192 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
193 IOThreadInfoList *info;
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100194 IOThreadInfo *value;
Ting Wang62313162015-06-26 16:07:13 +0800195
196 for (info = info_list; info; info = info->next) {
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100197 value = info->value;
198 monitor_printf(mon, "%s:\n", value->id);
199 monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
200 monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
201 monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
202 monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200203 monitor_printf(mon, " aio-max-batch=%" PRId64 "\n",
204 value->aio_max_batch);
Ting Wang62313162015-06-26 16:07:13 +0800205 }
206
207 qapi_free_IOThreadInfoList(info_list);
208}
Markus Armbrustercbf81992023-01-24 13:19:43 +0100209
210void hmp_help(Monitor *mon, const QDict *qdict)
211{
212 hmp_help_cmd(mon, qdict_get_try_str(qdict, "name"));
213}
214
215void hmp_info_help(Monitor *mon, const QDict *qdict)
216{
217 hmp_help_cmd(mon, "info");
218}
219
220void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
221{
222 int64_t max = qdict_get_try_int(qdict, "max", 10);
223 bool mean = qdict_get_try_bool(qdict, "mean", false);
224 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
225 enum QSPSortBy sort_by;
226
227 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
228 qsp_report(max, sort_by, coalesce);
229}
230
231void hmp_info_history(Monitor *mon, const QDict *qdict)
232{
233 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
234 int i;
235 const char *str;
236
237 if (!hmp_mon->rs) {
238 return;
239 }
240 i = 0;
241 for(;;) {
242 str = readline_get_history(hmp_mon->rs, i);
243 if (!str) {
244 break;
245 }
246 monitor_printf(mon, "%d: '%s'\n", i, str);
247 i++;
248 }
249}
250
251void hmp_logfile(Monitor *mon, const QDict *qdict)
252{
253 Error *err = NULL;
254
255 if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
256 error_report_err(err);
257 }
258}
259
260void hmp_log(Monitor *mon, const QDict *qdict)
261{
262 int mask;
263 const char *items = qdict_get_str(qdict, "items");
264 Error *err = NULL;
265
266 if (!strcmp(items, "none")) {
267 mask = 0;
268 } else {
269 mask = qemu_str_to_log_mask(items);
270 if (!mask) {
271 hmp_help_cmd(mon, "log");
272 return;
273 }
274 }
275
276 if (!qemu_set_log(mask, &err)) {
277 error_report_err(err);
278 }
279}
280
281void hmp_gdbserver(Monitor *mon, const QDict *qdict)
282{
283 const char *device = qdict_get_try_str(qdict, "device");
284 if (!device) {
285 device = "tcp::" DEFAULT_GDBSTUB_PORT;
286 }
287
288 if (gdbserver_start(device) < 0) {
289 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
290 device);
291 } else if (strcmp(device, "none") == 0) {
292 monitor_printf(mon, "Disabled gdbserver\n");
293 } else {
294 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
295 device);
296 }
297}
298
299void hmp_print(Monitor *mon, const QDict *qdict)
300{
301 int format = qdict_get_int(qdict, "format");
302 hwaddr val = qdict_get_int(qdict, "val");
303
304 switch(format) {
305 case 'o':
306 monitor_printf(mon, "%#" HWADDR_PRIo, val);
307 break;
308 case 'x':
309 monitor_printf(mon, "%#" HWADDR_PRIx, val);
310 break;
311 case 'u':
312 monitor_printf(mon, "%" HWADDR_PRIu, val);
313 break;
314 default:
315 case 'd':
316 monitor_printf(mon, "%" HWADDR_PRId, val);
317 break;
318 case 'c':
319 monitor_printc(mon, val);
320 break;
321 }
322 monitor_printf(mon, "\n");
323}
324
325void hmp_sum(Monitor *mon, const QDict *qdict)
326{
327 uint32_t addr;
328 uint16_t sum;
329 uint32_t start = qdict_get_int(qdict, "start");
330 uint32_t size = qdict_get_int(qdict, "size");
331
332 sum = 0;
333 for(addr = start; addr < (start + size); addr++) {
334 uint8_t val = address_space_ldub(&address_space_memory, addr,
335 MEMTXATTRS_UNSPECIFIED, NULL);
336 /* BSD sum algorithm ('sum' Unix command) */
337 sum = (sum >> 1) | (sum << 15);
338 sum += val;
339 }
340 monitor_printf(mon, "%05d\n", sum);
341}
342
343void hmp_ioport_read(Monitor *mon, const QDict *qdict)
344{
345 int size = qdict_get_int(qdict, "size");
346 int addr = qdict_get_int(qdict, "addr");
347 int has_index = qdict_haskey(qdict, "index");
348 uint32_t val;
349 int suffix;
350
351 if (has_index) {
352 int index = qdict_get_int(qdict, "index");
353 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
354 addr++;
355 }
356 addr &= 0xffff;
357
358 switch(size) {
359 default:
360 case 1:
361 val = cpu_inb(addr);
362 suffix = 'b';
363 break;
364 case 2:
365 val = cpu_inw(addr);
366 suffix = 'w';
367 break;
368 case 4:
369 val = cpu_inl(addr);
370 suffix = 'l';
371 break;
372 }
373 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
374 suffix, addr, size * 2, val);
375}
376
377void hmp_ioport_write(Monitor *mon, const QDict *qdict)
378{
379 int size = qdict_get_int(qdict, "size");
380 int addr = qdict_get_int(qdict, "addr");
381 int val = qdict_get_int(qdict, "val");
382
383 addr &= IOPORTS_MASK;
384
385 switch (size) {
386 default:
387 case 1:
388 cpu_outb(addr, val);
389 break;
390 case 2:
391 cpu_outw(addr, val);
392 break;
393 case 4:
394 cpu_outl(addr, val);
395 break;
396 }
397}
398
399void hmp_boot_set(Monitor *mon, const QDict *qdict)
400{
401 Error *local_err = NULL;
402 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
403
404 qemu_boot_set(bootdevice, &local_err);
405 if (local_err) {
406 error_report_err(local_err);
407 } else {
408 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
409 }
410}
411
412void hmp_info_mtree(Monitor *mon, const QDict *qdict)
413{
414 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
415 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
416 bool owner = qdict_get_try_bool(qdict, "owner", false);
417 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
418
419 mtree_info(flatview, dispatch_tree, owner, disabled);
420}
Paolo Bonzini1935b7e2024-05-07 14:13:46 +0200421
422#if defined(CONFIG_FDT)
423void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
424{
425 const char *filename = qdict_get_str(qdict, "filename");
426 Error *local_err = NULL;
427
428 qmp_dumpdtb(filename, &local_err);
429
430 if (hmp_handle_error(mon, local_err)) {
431 return;
432 }
433
434 monitor_printf(mon, "dtb dumped to %s", filename);
435}
436#endif