From patchwork Mon Dec 5 20:00:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 129405 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4908C1007D4 for ; Tue, 6 Dec 2011 07:01:05 +1100 (EST) Received: from localhost ([::1]:36284 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXeij-0002Tf-9M for incoming@patchwork.ozlabs.org; Mon, 05 Dec 2011 15:01:01 -0500 Received: from eggs.gnu.org ([140.186.70.92]:41659) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXeiR-0002ID-L1 for qemu-devel@nongnu.org; Mon, 05 Dec 2011 15:00:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RXeiP-0005ya-Tj for qemu-devel@nongnu.org; Mon, 05 Dec 2011 15:00:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:35230) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXeiP-0005yV-Mq for qemu-devel@nongnu.org; Mon, 05 Dec 2011 15:00:41 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id pB5K0dxH011365 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 5 Dec 2011 15:00:39 -0500 Received: from localhost (ovpn-113-50.phx2.redhat.com [10.3.113.50]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id pB5K0cGF012104; Mon, 5 Dec 2011 15:00:39 -0500 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Mon, 5 Dec 2011 18:00:14 -0200 Message-Id: <1323115226-16817-5-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1323115226-16817-1-git-send-email-lcapitulino@redhat.com> References: <1323115226-16817-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 209.132.183.28 Cc: aliguori@us.ibm.com, mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 04/16] qapi: Convert memsave X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Please, note that the QMP command has a new 'cpu-index' parameter. Signed-off-by: Anthony Liguori Signed-off-by: Luiz Capitulino --- cpus.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ hmp-commands.hx | 3 +-- hmp.c | 19 +++++++++++++++++++ hmp.h | 1 + monitor.c | 38 -------------------------------------- qapi-schema.json | 26 ++++++++++++++++++++++++++ qmp-commands.hx | 10 +++------- 7 files changed, 97 insertions(+), 47 deletions(-) diff --git a/cpus.c b/cpus.c index ca46ec6..0f2ce60 100644 --- a/cpus.c +++ b/cpus.c @@ -1136,3 +1136,50 @@ CpuInfoList *qmp_query_cpus(Error **errp) return head; } + +void qmp_memsave(int64_t addr, int64_t size, const char *filename, + bool has_cpu, int64_t cpu_index, Error **errp) +{ + FILE *f; + uint32_t l; + CPUState *env; + uint8_t buf[1024]; + + if (!has_cpu) { + cpu_index = 0; + } + + for (env = first_cpu; env; env = env->next_cpu) { + if (cpu_index == env->cpu_index) { + break; + } + } + + if (env == NULL) { + error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", + "a CPU number"); + return; + } + + f = fopen(filename, "wb"); + if (!f) { + error_set(errp, QERR_OPEN_FILE_FAILED, filename); + return; + } + + while (size != 0) { + l = sizeof(buf); + if (l > size) + l = size; + cpu_memory_rw_debug(env, addr, buf, l, 0); + if (fwrite(buf, 1, l, f) != l) { + error_set(errp, QERR_IO_ERROR); + goto exit; + } + addr += l; + size -= l; + } + +exit: + fclose(f); +} diff --git a/hmp-commands.hx b/hmp-commands.hx index 79a9195..dac0b47 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -689,8 +689,7 @@ ETEXI .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'", - .user_print = monitor_user_noop, - .mhandler.cmd_new = do_memory_save, + .mhandler.cmd = hmp_memsave, }, STEXI diff --git a/hmp.c b/hmp.c index dfab7ad..67b3eb3 100644 --- a/hmp.c +++ b/hmp.c @@ -14,6 +14,14 @@ #include "hmp.h" #include "qmp-commands.h" +static void hmp_handle_error(Monitor *mon, Error **errp) +{ + if (error_is_set(errp)) { + monitor_printf(mon, "%s\n", error_get_pretty(*errp)); + error_free(*errp); + } +} + void hmp_info_name(Monitor *mon) { NameInfo *info; @@ -531,3 +539,14 @@ void hmp_cpu(Monitor *mon, const QDict *qdict) monitor_printf(mon, "invalid CPU index\n"); } } + +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"); + Error *errp = NULL; + + qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp); + hmp_handle_error(mon, &errp); +} diff --git a/hmp.h b/hmp.h index 4422578..dd8ad0c 100644 --- a/hmp.h +++ b/hmp.h @@ -37,5 +37,6 @@ void hmp_stop(Monitor *mon, const QDict *qdict); void hmp_system_reset(Monitor *mon, const QDict *qdict); void hmp_system_powerdown(Monitor *mon, const QDict *qdict); void hmp_cpu(Monitor *mon, const QDict *qdict); +void hmp_memsave(Monitor *mon, const QDict *qdict); #endif diff --git a/monitor.c b/monitor.c index 344b196..7272014 100644 --- a/monitor.c +++ b/monitor.c @@ -1370,44 +1370,6 @@ static void do_print(Monitor *mon, const QDict *qdict) monitor_printf(mon, "\n"); } -static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data) -{ - FILE *f; - uint32_t size = qdict_get_int(qdict, "size"); - const char *filename = qdict_get_str(qdict, "filename"); - target_long addr = qdict_get_int(qdict, "val"); - uint32_t l; - CPUState *env; - uint8_t buf[1024]; - int ret = -1; - - env = mon_get_cpu(); - - f = fopen(filename, "wb"); - if (!f) { - qerror_report(QERR_OPEN_FILE_FAILED, filename); - return -1; - } - while (size != 0) { - l = sizeof(buf); - if (l > size) - l = size; - cpu_memory_rw_debug(env, addr, buf, l, 0); - if (fwrite(buf, 1, l, f) != l) { - monitor_printf(mon, "fwrite() error in do_memory_save\n"); - goto exit; - } - addr += l; - size -= l; - } - - ret = 0; - -exit: - fclose(f); - return ret; -} - static int do_physical_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data) { diff --git a/qapi-schema.json b/qapi-schema.json index fbbdbe0..dbf6170 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -901,3 +901,29 @@ # Notes: Do not use this command. ## { 'command': 'cpu', 'data': {'index': 'int'} } + +## +# @memsave: +# +# Save a portion of guest memory to a file. +# +# @val: the virtual address of the guest to start from +# +# @size: the size of memory region to save +# +# @filename: the file to save the memory to as binary data +# +# @cpu-index: #optional the index of the virtual CPU to use for translating the +# virtual address (defaults to CPU 0) +# +# Returns: Nothing on success +# If @cpu is not a valid VCPU, InvalidParameterValue +# If @filename cannot be opened, OpenFileFailed +# If an I/O error occurs while writing the file, IOError +# +# Since: 0.14.0 +# +# Notes: Errors were not reliably returned until 1.1 +## +{ 'command': 'memsave', + 'data': {'val': 'int', 'size': 'int', 'filename': 'str', '*cpu-index': 'int'} } diff --git a/qmp-commands.hx b/qmp-commands.hx index 4fcb92c..0e2f392 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -352,11 +352,8 @@ EQMP { .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'", - .user_print = monitor_user_noop, - .mhandler.cmd_new = do_memory_save, + .args_type = "val:l,size:i,filename:s,cpu:i?", + .mhandler.cmd_new = qmp_marshal_input_memsave, }, SQMP @@ -370,6 +367,7 @@ Arguments: - "val": the starting address (json-int) - "size": the memory size, in bytes (json-int) - "filename": file path (json-string) +- "cpu": virtual CPU index (json-int, optional) Example: @@ -379,8 +377,6 @@ Example: "filename": "/tmp/virtual-mem-dump" } } <- { "return": {} } -Note: Depends on the current CPU. - EQMP {