From patchwork Thu Mar 1 02:55:52 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wen Congyang X-Patchwork-Id: 143889 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id EF711B6EEE for ; Thu, 1 Mar 2012 13:54:53 +1100 (EST) Received: from localhost ([::1]:34368 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S2wAN-0002mq-Rl for incoming@patchwork.ozlabs.org; Wed, 29 Feb 2012 21:54:51 -0500 Received: from eggs.gnu.org ([208.118.235.92]:58421) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S2wAE-0002mK-VC for qemu-devel@nongnu.org; Wed, 29 Feb 2012 21:54:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S2wAC-0008Gd-B8 for qemu-devel@nongnu.org; Wed, 29 Feb 2012 21:54:42 -0500 Received: from [222.73.24.84] (port=50138 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S2wAA-0008EQ-Sh for qemu-devel@nongnu.org; Wed, 29 Feb 2012 21:54:40 -0500 Received: from tang.cn.fujitsu.com (tang.cn.fujitsu.com [10.167.250.3]) by song.cn.fujitsu.com (Postfix) with ESMTP id 86D0E17011A; Thu, 1 Mar 2012 10:54:36 +0800 (CST) Received: from mailserver.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id q212sZ8F008431; Thu, 1 Mar 2012 10:54:35 +0800 Received: from [10.167.225.226] ([10.167.225.226]) by mailserver.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.1FP4) with ESMTP id 2012030110524767-829748 ; Thu, 1 Mar 2012 10:52:47 +0800 Message-ID: <4F4EE538.5020200@cn.fujitsu.com> Date: Thu, 01 Mar 2012 10:55:52 +0800 From: Wen Congyang User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100413 Fedora/3.0.4-2.fc13 Thunderbird/3.0.4 MIME-Version: 1.0 To: qemu-devel , Jan Kiszka , Dave Anderson , HATAYAMA Daisuke , Luiz Capitulino , Eric Blake References: <4F4EE080.9060307@cn.fujitsu.com> In-Reply-To: <4F4EE080.9060307@cn.fujitsu.com> X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.1FP4|July 25, 2010) at 2012-03-01 10:52:47, Serialize by Router on mailserver/fnst(Release 8.5.1FP4|July 25, 2010) at 2012-03-01 10:52:47, Serialize complete at 2012-03-01 10:52:47 X-detected-operating-system: by eggs.gnu.org: FreeBSD 6.x (1) X-Received-From: 222.73.24.84 Subject: [Qemu-devel] [RFC][PATCH 14/14 v7] allow user to dump a fraction of the memory 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 Signed-off-by: Wen Congyang --- dump.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++------ hmp-commands.hx | 14 ++++-- hmp.c | 13 +++++- memory_mapping.c | 27 ++++++++++++ memory_mapping.h | 2 + qapi-schema.json | 6 ++- qmp-commands.hx | 8 +++- 7 files changed, 172 insertions(+), 22 deletions(-) diff --git a/dump.c b/dump.c index dd3a72c..3aa160e 100644 --- a/dump.c +++ b/dump.c @@ -91,6 +91,9 @@ typedef struct DumpState { void *opaque; RAMBlock *block; ram_addr_t start; + bool has_filter; + int64_t begin; + int64_t length; target_phys_addr_t offset; VMChangeStateEntry *handler; } DumpState; @@ -413,17 +416,47 @@ static int write_memory(DumpState *s, RAMBlock *block, ram_addr_t start, /* get the memory's offset in the vmcore */ static target_phys_addr_t get_offset(target_phys_addr_t phys_addr, - target_phys_addr_t memory_offset) + DumpState *s) { RAMBlock *block; - target_phys_addr_t offset = memory_offset; + target_phys_addr_t offset = s->memory_offset; + int64_t size_in_block, start; + + if (s->has_filter) { + if (phys_addr < s->begin || phys_addr >= s->begin + s->length) { + return -1; + } + } QLIST_FOREACH(block, &ram_list.blocks, next) { - if (phys_addr >= block->offset && - phys_addr < block->offset + block->length) { - return phys_addr - block->offset + offset; + if (s->has_filter) { + if (block->offset >= s->begin + s->length || + block->offset + block->length <= s->begin) { + /* This block is out of the range */ + continue; + } + + if (s->begin <= block->offset) { + start = block->offset; + } else { + start = s->begin; + } + + size_in_block = block->length - (start - block->offset); + if (s->begin + s->length < block->offset + block->length) { + size_in_block -= block->offset + block->length - + (s->begin + s->length); + } + } else { + start = block->offset; + size_in_block = block->length; } - offset += block->length; + + if (phys_addr >= start && phys_addr < start + size_in_block) { + return phys_addr - start + offset; + } + + offset += size_in_block; } return -1; @@ -495,7 +528,7 @@ static int dump_completed(DumpState *s) int phdr_index = 1, ret; QTAILQ_FOREACH(memory_mapping, &s->list.head, next) { - offset = get_offset(memory_mapping->phys_addr, s->memory_offset); + offset = get_offset(memory_mapping->phys_addr, s); if (s->dump_info.d_class == ELFCLASS64) { ret = write_elf64_load(s, memory_mapping, phdr_index++, offset); } else { @@ -522,6 +555,17 @@ static int get_next_block(DumpState *s, RAMBlock *block) s->start = 0; s->block = block; + if (s->has_filter) { + if (block->offset >= s->begin + s->length || + block->offset + block->length <= s->begin) { + /* This block is out of the range */ + continue; + } + + if (s->begin > block->offset) { + s->start = s->begin - block->offset; + } + } return 0; } @@ -600,7 +644,36 @@ static void dump_vm_state_change(void *opaque, int running, RunState state) } } -static DumpState *dump_init(Error **errp) +static ram_addr_t get_start_block(DumpState *s) +{ + RAMBlock *block; + + if (!s->has_filter) { + s->block = QLIST_FIRST(&ram_list.blocks); + return 0; + } + + QLIST_FOREACH(block, &ram_list.blocks, next) { + if (block->offset >= s->begin + s->length || + block->offset + block->length <= s->begin) { + /* This block is out of the range */ + continue; + } + + s->block = block; + if (s->begin > block->offset ) { + s->start = s->begin - block->offset; + } else { + s->start = 0; + } + return s->start; + } + + return -1; +} + +static DumpState *dump_init(bool has_filter, int64_t begin, int64_t length, + Error **errp) { CPUState *env; DumpState *s = dump_get_current(); @@ -617,8 +690,16 @@ static DumpState *dump_init(Error **errp) g_free(s->error); s->error = NULL; } - s->block = QLIST_FIRST(&ram_list.blocks); - s->start = 0; + + s->has_filter = has_filter; + s->begin = begin; + s->length = length; + s->start = get_start_block(s); + if (s->start == -1) { + error_set(errp, QERR_INVALID_PARAMETER, "begin"); + return NULL; + } + s->handler = qemu_add_vm_change_state_handler(dump_vm_state_change, s); /* @@ -643,6 +724,10 @@ static DumpState *dump_init(Error **errp) memory_mapping_list_init(&s->list); qemu_get_guest_memory_mapping(&s->list); + if (s->has_filter) { + memory_mapping_filter(&s->list, s->begin, s->length); + } + /* * calculate phdr_num * @@ -704,9 +789,10 @@ static int fd_dump_begin_iterate(DumpState *s, void *opaque) return qemu_set_fd_handler(fd, NULL, dump_iterate, s); } -static DumpState *dump_init_fd(int fd, Error **errp) +static DumpState *dump_init_fd(int fd, bool has_filter, int64_t begin, + int64_t length, Error **errp) { - DumpState *s = dump_init(errp); + DumpState *s = dump_init(has_filter, begin, length, errp); if (s == NULL) { return NULL; @@ -721,12 +807,22 @@ static DumpState *dump_init_fd(int fd, Error **errp) return s; } -void qmp_dump(bool detach, const char *file, Error **errp) +void qmp_dump(bool detach, const char *file, bool has_begin, int64_t begin, + bool has_length, int64_t length, Error **errp) { const char *p; int fd = -1; DumpState *s; + if (has_begin && !has_length) { + error_set(errp, QERR_MISSING_PARAMETER, "length"); + return; + } + if (!has_begin && has_length) { + error_set(errp, QERR_MISSING_PARAMETER, "begin"); + return; + } + #if !defined(WIN32) if (strstart(file, "fd:", &p)) { fd = qemu_get_fd(p); @@ -750,7 +846,7 @@ void qmp_dump(bool detach, const char *file, Error **errp) return; } - s = dump_init_fd(fd, errp); + s = dump_init_fd(fd, has_begin, begin, length, errp); if (!s) { return; } diff --git a/hmp-commands.hx b/hmp-commands.hx index bd0c95d..8b2dd38 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -883,21 +883,27 @@ ETEXI #if defined(CONFIG_HAVE_CORE_DUMP) { .name = "dump", - .args_type = "detach:-d,file:s", - .params = "[-d] file", - .help = "dump to file (using -d to not wait for completion)", + .args_type = "detach:-d,file:s,begin:i?,length:i?", + .params = "[-d] file [begin] [length]", + .help = "dump to file (using -d to not wait for completion)" + "\n\t\t\t begin(optional): the starting physical address" + "\n\t\t\t length(optional): the memory size, in bytes", .user_print = monitor_user_noop, .mhandler.cmd = hmp_dump, }, STEXI -@item dump [-d] @var{file} +@item dump [-d] @var{file} @var{begin} @var{length} @findex dump Dump to @var{file}. The file can be processed with crash or gdb. file: destination file(started with "file:") or destination file descriptor (started with "fd:") -d: donot wait for completion. + begin: the starting physical address. It's optional, and should be specified + with length together. + length: the memory size, in bytes. It's optional, and should be specified with + begin together. ETEXI #endif diff --git a/hmp.c b/hmp.c index 707701b..326d277 100644 --- a/hmp.c +++ b/hmp.c @@ -885,8 +885,19 @@ void hmp_dump(Monitor *mon, const QDict *qdict) Error *errp = NULL; int detach = qdict_get_try_bool(qdict, "detach", 0); const char *file = qdict_get_str(qdict, "file"); + bool has_begin = qdict_haskey(qdict, "begin"); + bool has_length = qdict_haskey(qdict, "length"); + int64_t begin = 0; + int64_t length = 0; - qmp_dump(!!detach, file, &errp); + if (has_begin) { + begin = qdict_get_int(qdict, "begin"); + } + if (has_length) { + length = qdict_get_int(qdict, "length"); + } + + qmp_dump(!!detach, file, has_begin, begin, has_length, length, &errp); if (errp) { hmp_handle_error(mon, &errp); return; diff --git a/memory_mapping.c b/memory_mapping.c index 3743805..0c6ddbe 100644 --- a/memory_mapping.c +++ b/memory_mapping.c @@ -203,3 +203,30 @@ int qemu_get_guest_memory_mapping(MemoryMappingList *list) return 0; } + +void memory_mapping_filter(MemoryMappingList *list, int64_t begin, + int64_t length) +{ + MemoryMapping *cur, *next; + + QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) { + if (cur->phys_addr >= begin + length || + cur->phys_addr + cur->length <= begin) { + QTAILQ_REMOVE(&list->head, cur, next); + list->num--; + continue; + } + + if (cur->phys_addr < begin) { + cur->length -= begin - cur->phys_addr; + if (cur->virt_addr) { + cur->virt_addr += begin - cur->phys_addr; + } + cur->phys_addr = begin; + } + + if (cur->phys_addr + cur->length > begin + length) { + cur->length -= cur->phys_addr + cur->length - begin - length; + } + } +} diff --git a/memory_mapping.h b/memory_mapping.h index 5760d1d..07270d5 100644 --- a/memory_mapping.h +++ b/memory_mapping.h @@ -49,4 +49,6 @@ void memory_mapping_list_init(MemoryMappingList *list); */ int qemu_get_guest_memory_mapping(MemoryMappingList *list); +void memory_mapping_filter(MemoryMappingList *list, int64_t begin, + int64_t length); #endif diff --git a/qapi-schema.json b/qapi-schema.json index ed157d7..1206d51 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -1601,12 +1601,16 @@ # # @detach: detached dumping. # @file: the filename or file descriptor of the vmcore. +# @begin: if specified, the starting physical address. +# @length: if specified, the memory size, in bytes. # # Returns: nothing on success # # Since: 1.1 ## -{ 'command': 'dump', 'data': { 'detach': 'bool', 'file': 'str' } } +{ 'command': 'dump', + 'data': { 'detach': 'bool', 'file': 'str', '*begin': 'int', + '*length': 'int' } } ## # @dump_cancel diff --git a/qmp-commands.hx b/qmp-commands.hx index c7b9c82..06ff121 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -589,8 +589,8 @@ EQMP #if defined(CONFIG_HAVE_CORE_DUMP) { .name = "dump", - .args_type = "detach:-d,file:s", - .params = "[-d] file", + .args_type = "detach:-d,file:s,begin:i?,end:i?", + .params = "[-d] file [begin] [length]", .help = "dump to file", .user_print = monitor_user_noop, .mhandler.cmd_new = qmp_marshal_input_dump, @@ -606,6 +606,10 @@ Arguments: - "file": destination file(started with "file:") or destination file descriptor (started with "fd:") (json-string) +- "begin": the starting physical address. It's optional, and should be specified + with length together (json-int) +- "length": the memory size, in bytes. It's optional, and should be specified + with begin together (json-int) Example: