All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wen Congyang <wency@cn.fujitsu.com>
To: qemu-devel <qemu-devel@nongnu.org>,
	Jan Kiszka <jan.kiszka@siemens.com>,
	Dave Anderson <anderson@redhat.com>,
	HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>,
	Luiz Capitulino <lcapitulino@redhat.com>,
	Eric Blake <eblake@redhat.com>
Subject: [Qemu-devel] [RFC][PATCH 16/16 v8] allow user to dump a fraction of the memory
Date: Fri, 02 Mar 2012 18:46:22 +0800	[thread overview]
Message-ID: <4F50A4FE.9020809@cn.fujitsu.com> (raw)
In-Reply-To: <4F509A00.80207@cn.fujitsu.com>

Signed-off-by: Wen Congyang <wency@cn.fujitsu.com>
---
 dump.c           |  124 +++++++++++++++++++++++++++++++++++++++++++++++------
 hmp-commands.hx  |   14 ++++--
 hmp.c            |   14 ++++++-
 memory_mapping.c |   27 ++++++++++++
 memory_mapping.h |    2 +
 qapi-schema.json |    5 ++-
 qmp-commands.hx  |    8 +++-
 7 files changed, 172 insertions(+), 22 deletions(-)

diff --git a/dump.c b/dump.c
index 40eefb9..52df041 100644
--- a/dump.c
+++ b/dump.c
@@ -93,6 +93,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;
@@ -463,17 +466,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;
+        }
+
+        if (phys_addr >= start && phys_addr < start + size_in_block) {
+            return phys_addr - start + offset;
         }
-        offset += block->length;
+
+        offset += size_in_block;
     }
 
     return -1;
@@ -561,7 +594,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 {
@@ -588,6 +621,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;
     }
@@ -666,7 +710,36 @@ static void dump_vm_state_change(void *opaque, int running, RunState state)
     }
 }
 
-static DumpState *dump_init(bool paging, 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 paging, bool has_filter, int64_t begin,
+                            int64_t length, Error **errp)
 {
     CPUState *env;
     DumpState *s = dump_get_current();
@@ -683,8 +756,16 @@ static DumpState *dump_init(bool paging, 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);
 
     /*
@@ -713,6 +794,10 @@ static DumpState *dump_init(bool paging, Error **errp)
         qemu_get_guest_simple_memory_mapping(&s->list);
     }
 
+    if (s->has_filter) {
+        memory_mapping_filter(&s->list, s->begin, s->length);
+    }
+
     /*
      * calculate phdr_num
      *
@@ -783,9 +868,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, bool paging, Error **errp)
+static DumpState *dump_init_fd(int fd, bool paging, bool has_filter,
+                               int64_t begin, int64_t length, Error **errp)
 {
-    DumpState *s = dump_init(paging, errp);
+    DumpState *s = dump_init(paging, has_filter, begin, length, errp);
 
     if (s == NULL) {
         return NULL;
@@ -800,12 +886,22 @@ static DumpState *dump_init_fd(int fd, bool paging, Error **errp)
     return s;
 }
 
-void qmp_dump(bool detach, bool paging, const char *file, Error **errp)
+void qmp_dump(bool detach, bool paging, 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);
@@ -829,7 +925,7 @@ void qmp_dump(bool detach, bool paging, const char *file, Error **errp)
         return;
     }
 
-    s = dump_init_fd(fd, paging, errp);
+    s = dump_init_fd(fd, paging, has_begin, begin, length, errp);
     if (!s) {
         return;
     }
diff --git a/hmp-commands.hx b/hmp-commands.hx
index fd6f9f1..e108780 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -883,22 +883,28 @@ ETEXI
 #if defined(CONFIG_HAVE_CORE_DUMP)
     {
         .name       = "dump",
-        .args_type  = "detach:-d,paging:-p,file:s",
-        .params     = "[-d] [-p] file",
-        .help       = "dump to file (using -d to not wait for completion)",
+        .args_type  = "detach:-d,paging:-p,file:s,begin:i?,length:i?",
+        .params     = "[-d] [-p] 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] [-p] @var{file}
+@item dump [-d] [-p] @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:")
   paging: do paging to get guest's memory mapping
       -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 8652b20..05e30e1 100644
--- a/hmp.c
+++ b/hmp.c
@@ -886,8 +886,20 @@ void hmp_dump(Monitor *mon, const QDict *qdict)
     int paging = qdict_get_try_bool(qdict, "paging", 0);
     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, !!paging, file, &errp);
+    if (has_begin) {
+        begin = qdict_get_int(qdict, "begin");
+    }
+    if (has_length) {
+        length = qdict_get_int(qdict, "length");
+    }
+
+    qmp_dump(!!detach, !!paging, 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 7f4193d..2d17a9a 100644
--- a/memory_mapping.c
+++ b/memory_mapping.c
@@ -261,3 +261,30 @@ void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list)
         create_new_memory_mapping(list, block->offset, 0, block->length);
     }
 }
+
+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 50b1f25..6b454e1 100644
--- a/memory_mapping.h
+++ b/memory_mapping.h
@@ -55,4 +55,6 @@ int qemu_get_guest_memory_mapping(MemoryMappingList *list);
 /* get guest's memory mapping without do paging(virtual address is 0). */
 void qemu_get_guest_simple_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 bd258cb..f981e8d 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -1640,13 +1640,16 @@
 # @detach: detached dumping.
 # @paging: if true, do paging to get guest's memory mapping
 # @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', 'paging': 'bool', 'file': 'str' } }
+  'data': { 'detach': 'bool', 'paging': 'bool', 'file': 'str', '*begin': 'int',
+            '*length': 'int' } }
 
 ##
 # @dump_cancel
diff --git a/qmp-commands.hx b/qmp-commands.hx
index e2accb2..cb2a3f1 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,paging:-p,file:s",
-        .params     = "[-d] [-p] file",
+        .args_type  = "detach:-d,paging:-p,file:s,begin:i?,end:i?",
+        .params     = "[-d] [-p] file [begin] [length]",
         .help       = "dump to file",
         .user_print = monitor_user_noop,
         .mhandler.cmd_new = qmp_marshal_input_dump,
@@ -607,6 +607,10 @@ Arguments:
 - "paging": do paging to get guest's memory mapping (json-bool)
 - "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:
 
-- 
1.7.1

  parent reply	other threads:[~2012-03-02 10:45 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-02  9:59 [Qemu-devel] [RFC][PATCH 00/16 v8] introducing a new, dedicated memory dump mechanism Wen Congyang
2012-03-02 10:02 ` [Qemu-devel] [RFC][PATCH 01/16 v8] Add API to create memory mapping list Wen Congyang
2012-03-02 10:06 ` [Qemu-devel] [RFC][PATCH 02/16 v8] Add API to check whether a physical address is I/O address Wen Congyang
2012-03-02 10:08 ` [Qemu-devel] [RFC][PATCH 03/16 v8] implement cpu_get_memory_mapping() Wen Congyang
2012-03-02 10:12 ` [Qemu-devel] [RFC][PATCH 04/16 v8] Add API to check whether paging mode is enabled Wen Congyang
2012-03-02 10:18 ` [Qemu-devel] [RFC][PATCH 05/16 v8] Add API to get memory mapping Wen Congyang
2012-03-07 15:27   ` HATAYAMA Daisuke
2012-03-08  8:52     ` Wen Congyang
2012-03-09  0:40       ` HATAYAMA Daisuke
2012-03-09  1:46         ` Wen Congyang
2012-03-09  2:05           ` HATAYAMA Daisuke
2012-03-09  2:26             ` Wen Congyang
2012-03-09  2:53               ` HATAYAMA Daisuke
2012-03-09  9:41                 ` Jan Kiszka
2012-03-09  9:57                   ` Wen Congyang
2012-03-09 10:05                     ` Jan Kiszka
2012-03-09 10:06                       ` Jan Kiszka
2012-03-09 12:53                         ` HATAYAMA Daisuke
2012-03-09 13:24                           ` Jan Kiszka
2012-03-12  6:16                             ` HATAYAMA Daisuke
2012-03-12  6:26                               ` HATAYAMA Daisuke
2012-03-12  1:52                       ` Wen Congyang
2012-03-09  9:43                 ` Jan Kiszka
2012-03-02 10:23 ` [Qemu-devel] [RFC][PATCH 06/16 v8] Add API to get memory mapping without doing paging Wen Congyang
2012-03-02 10:27 ` [Qemu-devel] [RFC][PATCH 07/16 v8] target-i386: Add API to write elf notes to core file Wen Congyang
2012-03-02 10:31 ` [Qemu-devel] [RFC][PATCH 08/16 v8] target-i386: Add API to write cpu status " Wen Congyang
2012-03-02 10:33 ` [Qemu-devel] [RFC][PATCH 09/16 v8] target-i386: add API to get dump info Wen Congyang
2012-03-02 10:38 ` [Qemu-devel] [RFC][PATCH 10/16 v8] make gdb_id() generally avialable Wen Congyang
2012-03-02 10:42 ` [Qemu-devel] [RFC][PATCH 11/16 v8] introduce a new monitor command 'dump' to dump guest's memory Wen Congyang
2012-03-02 10:43 ` [Qemu-devel] [RFC][PATCH 12/16 v8] support to cancel the current dumping Wen Congyang
2012-03-02 10:44 ` [Qemu-devel] [RFC][PATCH 13/16 v8] support to query dumping status Wen Congyang
2012-03-02 10:44 ` [Qemu-devel] [RFC][PATCH 14/16 v8] run dump at the background Wen Congyang
2012-03-02 10:45 ` [Qemu-devel] [RFC][PATCH 15/16 v8] support detached dump Wen Congyang
2012-03-02 10:46 ` Wen Congyang [this message]
2012-03-05  9:12 ` [Qemu-devel] [RFC][PATCH 00/16 v8] introducing a new, dedicated memory dump mechanism Wen Congyang
2012-03-06  0:41   ` Luiz Capitulino
2012-03-07 17:38     ` Luiz Capitulino
2012-03-08  8:55       ` Wen Congyang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4F50A4FE.9020809@cn.fujitsu.com \
    --to=wency@cn.fujitsu.com \
    --cc=anderson@redhat.com \
    --cc=d.hatayama@jp.fujitsu.com \
    --cc=eblake@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.