monitor: add memsave -i to save unreadable pages as zeros

This commit is contained in:
Kirill Zhumarin 2026-11-24 13:12:24 +03:00
parent fc30921547
commit 827e5a7e62
4 changed files with 48 additions and 10 deletions

View file

@ -820,15 +820,18 @@ ERST
{
.name = "memsave",
.args_type = "val:l,size:i,filename:s",
.params = "addr size file",
.help = "save to disk virtual memory dump starting at 'addr' of size 'size'",
.args_type = "ignore:-i,val:l,size:i,filename:s",
.params = "[-i] addr size file",
.help = "save to disk virtual memory dump starting at 'addr' of size 'size'"
" (-i: save unreadable pages as zeros with a warning)",
.cmd = hmp_memsave,
},
SRST
``memsave`` *addr* *size* *file*
``memsave [-i]`` *addr* *size* *file*
save to disk virtual memory dump starting at *addr* of size *size*.
With ``-i``, pages with read errors are saved as zeros and each such
range is reported as a warning.
ERST
{

View file

@ -220,6 +220,7 @@ void hmp_memsave(Monitor *mon, const QDict *qdict)
uint32_t size = qdict_get_int(qdict, "size");
const char *filename = qdict_get_str(qdict, "filename");
uint64_t addr = qdict_get_int(qdict, "val");
bool ignore_errors = qdict_get_try_bool(qdict, "ignore", false);
Error *err = NULL;
int cpu_index = monitor_get_cpu_index(mon);
@ -228,7 +229,8 @@ void hmp_memsave(Monitor *mon, const QDict *qdict)
return;
}
qmp_memsave(addr, size, filename, true, cpu_index, &err);
qmp_memsave(addr, size, filename, true, cpu_index, true, ignore_errors,
&err);
hmp_handle_error(mon, err);
}

View file

@ -806,6 +806,10 @@
# @cpu-index: the index of the virtual CPU to use for translating the
# virtual address (defaults to CPU 0)
#
# @ignore-errors: save pages with read errors as zeros and report each
# such range as a warning instead of failing (default false)
# (since 11.2)
#
# Since: 0.14
#
# .. caution:: Errors were not reliably returned until 1.1.
@ -823,7 +827,8 @@
'val': 'uint64',
'size': 'size',
'filename': 'str',
'*cpu-index': 'int' } }
'*cpu-index': 'int',
'*ignore-errors': 'bool' } }
##
# @pmemsave:

View file

@ -26,6 +26,8 @@
#include "monitor/monitor.h"
#include "qemu/coroutine-tls.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "exec/target_page.h"
#include "qapi/qapi-commands-machine.h"
#include "qapi/qapi-commands-misc.h"
#include "qapi/qapi-events-run-state.h"
@ -864,14 +866,24 @@ int vm_stop_force_state(RunState state)
}
}
static void memsave_warn_unreadable(uint64_t addr, uint64_t size)
{
warn_report("memsave: 0x%016" PRIx64 "..0x%016" PRIx64
" is unreadable, saved as zeros",
addr, addr + size - 1);
}
void qmp_memsave(uint64_t addr, uint64_t size, const char *filename,
bool has_cpu, int64_t cpu_index, Error **errp)
bool has_cpu, int64_t cpu_index,
bool has_ignore_errors, bool ignore_errors, Error **errp)
{
FILE *f;
uint64_t l;
CPUState *cpu;
uint8_t buf[1024];
uint64_t orig_addr = addr, orig_size = size;
uint64_t page_size = qemu_target_page_size();
uint64_t unreadable_addr = 0, unreadable_size = 0;
if (migration_guest_ram_loading()) {
error_setg(errp, "Guest memory access not allowed during migration");
@ -899,10 +911,23 @@ void qmp_memsave(uint64_t addr, uint64_t size, const char *filename,
l = sizeof(buf);
if (l > size)
l = size;
if (ignore_errors) {
l = MIN(l, page_size - (addr & (page_size - 1)));
}
if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRIu64
" specified", orig_addr, orig_size);
goto exit;
if (!ignore_errors) {
error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRIu64
" specified", orig_addr, orig_size);
goto exit;
}
memset(buf, 0, l);
if (unreadable_size == 0) {
unreadable_addr = addr;
}
unreadable_size += l;
} else if (unreadable_size != 0) {
memsave_warn_unreadable(unreadable_addr, unreadable_size);
unreadable_size = 0;
}
if (fwrite(buf, 1, l, f) != l) {
error_setg(errp, "writing memory to '%s' failed",
@ -912,6 +937,9 @@ void qmp_memsave(uint64_t addr, uint64_t size, const char *filename,
addr += l;
size -= l;
}
if (unreadable_size != 0) {
memsave_warn_unreadable(unreadable_addr, unreadable_size);
}
exit:
fclose(f);