yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
9ae3a8
From 0e125906f0e8bd7015569e7c76b687e4aacb3cca Mon Sep 17 00:00:00 2001
9ae3a8
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
9ae3a8
Date: Wed, 13 Dec 2017 13:39:05 +0100
9ae3a8
Subject: [PATCH 34/41] scripts/dump-guest-memory.py: Make methods functions
9ae3a8
MIME-Version: 1.0
9ae3a8
Content-Type: text/plain; charset=UTF-8
9ae3a8
Content-Transfer-Encoding: 8bit
9ae3a8
9ae3a8
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
Message-id: <20171213133912.26176-35-marcandre.lureau@redhat.com>
9ae3a8
Patchwork-id: 78384
9ae3a8
O-Subject: [RHEL-7.5 qemu-kvm PATCH v3 34/41] scripts/dump-guest-memory.py: Make methods functions
9ae3a8
Bugzilla: 1411490
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
From: Janosch Frank <frankja@linux.vnet.ibm.com>
9ae3a8
9ae3a8
The functions dealing with qemu components rarely used parts of the
9ae3a8
class, so they were moved out of the class.
9ae3a8
9ae3a8
As the uintptr_t variable is needed both within and outside the class,
9ae3a8
it was made a constant and moved to the top.
9ae3a8
9ae3a8
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Signed-off-by: Janosch Frank <frankja@linux.vnet.ibm.com>
9ae3a8
Message-Id: <1453464520-3882-3-git-send-email-frankja@linux.vnet.ibm.com>
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
9ae3a8
(cherry picked from commit 47890203842de8b29716bdffb406ca851e70829d)
9ae3a8
9ae3a8
RHEL: conflicts due to qtailq->qlist, used_length->length
9ae3a8
9ae3a8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 scripts/dump-guest-memory.py | 184 ++++++++++++++++++++++---------------------
9ae3a8
 1 file changed, 93 insertions(+), 91 deletions(-)
9ae3a8
9ae3a8
diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py
9ae3a8
index 29f7c5b..7d93d86 100644
9ae3a8
--- a/scripts/dump-guest-memory.py
9ae3a8
+++ b/scripts/dump-guest-memory.py
9ae3a8
@@ -17,6 +17,8 @@
9ae3a8
 
9ae3a8
 import struct
9ae3a8
 
9ae3a8
+UINTPTR_T = gdb.lookup_type("uintptr_t")
9ae3a8
+
9ae3a8
 TARGET_PAGE_SIZE = 0x1000
9ae3a8
 TARGET_PAGE_MASK = 0xFFFFFFFFFFFFF000
9ae3a8
 
9ae3a8
@@ -66,6 +68,94 @@ ELF64_PHDR = ("I"  # p_type
9ae3a8
               "Q"  # p_align
9ae3a8
           )
9ae3a8
 
9ae3a8
+def int128_get64(val):
9ae3a8
+    assert (val["hi"] == 0)
9ae3a8
+    return val["lo"]
9ae3a8
+
9ae3a8
+def qtailq_foreach(head, field_str):
9ae3a8
+    var_p = head["tqh_first"]
9ae3a8
+    while (var_p != 0):
9ae3a8
+        var = var_p.dereference()
9ae3a8
+        yield var
9ae3a8
+        var_p = var[field_str]["tqe_next"]
9ae3a8
+
9ae3a8
+def qemu_get_ram_block(ram_addr):
9ae3a8
+    ram_blocks = gdb.parse_and_eval("ram_list.blocks")
9ae3a8
+    for block in qtailq_foreach(ram_blocks, "next"):
9ae3a8
+        if (ram_addr - block["offset"] < block["length"]):
9ae3a8
+            return block
9ae3a8
+    raise gdb.GdbError("Bad ram offset %x" % ram_addr)
9ae3a8
+
9ae3a8
+def qemu_get_ram_ptr(ram_addr):
9ae3a8
+    block = qemu_get_ram_block(ram_addr)
9ae3a8
+    return block["host"] + (ram_addr - block["offset"])
9ae3a8
+
9ae3a8
+def memory_region_get_ram_ptr(mr):
9ae3a8
+    if (mr["alias"] != 0):
9ae3a8
+        return (memory_region_get_ram_ptr(mr["alias"].dereference()) +
9ae3a8
+                mr["alias_offset"])
9ae3a8
+    return qemu_get_ram_ptr(mr["ram_addr"] & TARGET_PAGE_MASK)
9ae3a8
+
9ae3a8
+def get_guest_phys_blocks():
9ae3a8
+    guest_phys_blocks = []
9ae3a8
+    print "guest RAM blocks:"
9ae3a8
+    print ("target_start     target_end       host_addr        message "
9ae3a8
+           "count")
9ae3a8
+    print ("---------------- ---------------- ---------------- ------- "
9ae3a8
+           "-----")
9ae3a8
+
9ae3a8
+    current_map_p = gdb.parse_and_eval("address_space_memory.current_map")
9ae3a8
+    current_map = current_map_p.dereference()
9ae3a8
+    for cur in range(current_map["nr"]):
9ae3a8
+        flat_range   = (current_map["ranges"] + cur).dereference()
9ae3a8
+        mr           = flat_range["mr"].dereference()
9ae3a8
+
9ae3a8
+        # we only care about RAM
9ae3a8
+        if (not mr["ram"]):
9ae3a8
+            continue
9ae3a8
+
9ae3a8
+        section_size = int128_get64(flat_range["addr"]["size"])
9ae3a8
+        target_start = int128_get64(flat_range["addr"]["start"])
9ae3a8
+        target_end   = target_start + section_size
9ae3a8
+        host_addr    = (memory_region_get_ram_ptr(mr) +
9ae3a8
+                        flat_range["offset_in_region"])
9ae3a8
+        predecessor = None
9ae3a8
+
9ae3a8
+        # find continuity in guest physical address space
9ae3a8
+        if (len(guest_phys_blocks) > 0):
9ae3a8
+            predecessor = guest_phys_blocks[-1]
9ae3a8
+            predecessor_size = (predecessor["target_end"] -
9ae3a8
+                                predecessor["target_start"])
9ae3a8
+
9ae3a8
+            # the memory API guarantees monotonically increasing
9ae3a8
+            # traversal
9ae3a8
+            assert (predecessor["target_end"] <= target_start)
9ae3a8
+
9ae3a8
+            # we want continuity in both guest-physical and
9ae3a8
+            # host-virtual memory
9ae3a8
+            if (predecessor["target_end"] < target_start or
9ae3a8
+                predecessor["host_addr"] + predecessor_size != host_addr):
9ae3a8
+                predecessor = None
9ae3a8
+
9ae3a8
+        if (predecessor is None):
9ae3a8
+            # isolated mapping, add it to the list
9ae3a8
+            guest_phys_blocks.append({"target_start": target_start,
9ae3a8
+                                      "target_end"  : target_end,
9ae3a8
+                                      "host_addr"   : host_addr})
9ae3a8
+            message = "added"
9ae3a8
+        else:
9ae3a8
+            # expand predecessor until @target_end; predecessor's
9ae3a8
+            # start doesn't change
9ae3a8
+            predecessor["target_end"] = target_end
9ae3a8
+            message = "joined"
9ae3a8
+
9ae3a8
+        print ("%016x %016x %016x %-7s %5u" %
9ae3a8
+               (target_start, target_end, host_addr.cast(UINTPTR_T),
9ae3a8
+                message, len(guest_phys_blocks)))
9ae3a8
+
9ae3a8
+    return guest_phys_blocks
9ae3a8
+
9ae3a8
+
9ae3a8
 class DumpGuestMemory(gdb.Command):
9ae3a8
     """Extract guest vmcore from qemu process coredump.
9ae3a8
 
9ae3a8
@@ -100,96 +190,9 @@ shape and this command should mostly work."""
9ae3a8
         super(DumpGuestMemory, self).__init__("dump-guest-memory",
9ae3a8
                                               gdb.COMMAND_DATA,
9ae3a8
                                               gdb.COMPLETE_FILENAME)
9ae3a8
-        self.uintptr_t     = gdb.lookup_type("uintptr_t")
9ae3a8
         self.elf64_ehdr_le = struct.Struct("<%s" % ELF64_EHDR)
9ae3a8
         self.elf64_phdr_le = struct.Struct("<%s" % ELF64_PHDR)
9ae3a8
-
9ae3a8
-    def int128_get64(self, val):
9ae3a8
-        assert (val["hi"] == 0)
9ae3a8
-        return val["lo"]
9ae3a8
-
9ae3a8
-    def qtailq_foreach(self, head, field_str):
9ae3a8
-        var_p = head["tqh_first"]
9ae3a8
-        while (var_p != 0):
9ae3a8
-            var = var_p.dereference()
9ae3a8
-            yield var
9ae3a8
-            var_p = var[field_str]["tqe_next"]
9ae3a8
-
9ae3a8
-    def qemu_get_ram_block(self, ram_addr):
9ae3a8
-        ram_blocks = gdb.parse_and_eval("ram_list.blocks")
9ae3a8
-        for block in self.qtailq_foreach(ram_blocks, "next"):
9ae3a8
-            if (ram_addr - block["offset"] < block["length"]):
9ae3a8
-                return block
9ae3a8
-        raise gdb.GdbError("Bad ram offset %x" % ram_addr)
9ae3a8
-
9ae3a8
-    def qemu_get_ram_ptr(self, ram_addr):
9ae3a8
-        block = self.qemu_get_ram_block(ram_addr)
9ae3a8
-        return block["host"] + (ram_addr - block["offset"])
9ae3a8
-
9ae3a8
-    def memory_region_get_ram_ptr(self, mr):
9ae3a8
-        if (mr["alias"] != 0):
9ae3a8
-            return (self.memory_region_get_ram_ptr(mr["alias"].dereference()) +
9ae3a8
-                    mr["alias_offset"])
9ae3a8
-        return self.qemu_get_ram_ptr(mr["ram_addr"] & TARGET_PAGE_MASK)
9ae3a8
-
9ae3a8
-    def guest_phys_blocks_init(self):
9ae3a8
-        self.guest_phys_blocks = []
9ae3a8
-
9ae3a8
-    def guest_phys_blocks_append(self):
9ae3a8
-        print "guest RAM blocks:"
9ae3a8
-        print ("target_start     target_end       host_addr        message "
9ae3a8
-               "count")
9ae3a8
-        print ("---------------- ---------------- ---------------- ------- "
9ae3a8
-               "-----")
9ae3a8
-
9ae3a8
-        current_map_p = gdb.parse_and_eval("address_space_memory.current_map")
9ae3a8
-        current_map = current_map_p.dereference()
9ae3a8
-        for cur in range(current_map["nr"]):
9ae3a8
-            flat_range   = (current_map["ranges"] + cur).dereference()
9ae3a8
-            mr           = flat_range["mr"].dereference()
9ae3a8
-
9ae3a8
-            # we only care about RAM
9ae3a8
-            if (not mr["ram"]):
9ae3a8
-                continue
9ae3a8
-
9ae3a8
-            section_size = self.int128_get64(flat_range["addr"]["size"])
9ae3a8
-            target_start = self.int128_get64(flat_range["addr"]["start"])
9ae3a8
-            target_end   = target_start + section_size
9ae3a8
-            host_addr    = (self.memory_region_get_ram_ptr(mr) +
9ae3a8
-                            flat_range["offset_in_region"])
9ae3a8
-            predecessor = None
9ae3a8
-
9ae3a8
-            # find continuity in guest physical address space
9ae3a8
-            if (len(self.guest_phys_blocks) > 0):
9ae3a8
-                predecessor = self.guest_phys_blocks[-1]
9ae3a8
-                predecessor_size = (predecessor["target_end"] -
9ae3a8
-                                    predecessor["target_start"])
9ae3a8
-
9ae3a8
-                # the memory API guarantees monotonically increasing
9ae3a8
-                # traversal
9ae3a8
-                assert (predecessor["target_end"] <= target_start)
9ae3a8
-
9ae3a8
-                # we want continuity in both guest-physical and
9ae3a8
-                # host-virtual memory
9ae3a8
-                if (predecessor["target_end"] < target_start or
9ae3a8
-                    predecessor["host_addr"] + predecessor_size != host_addr):
9ae3a8
-                    predecessor = None
9ae3a8
-
9ae3a8
-            if (predecessor is None):
9ae3a8
-                # isolated mapping, add it to the list
9ae3a8
-                self.guest_phys_blocks.append({"target_start": target_start,
9ae3a8
-                                               "target_end"  : target_end,
9ae3a8
-                                               "host_addr"   : host_addr})
9ae3a8
-                message = "added"
9ae3a8
-            else:
9ae3a8
-                # expand predecessor until @target_end; predecessor's
9ae3a8
-                # start doesn't change
9ae3a8
-                predecessor["target_end"] = target_end
9ae3a8
-                message = "joined"
9ae3a8
-
9ae3a8
-            print ("%016x %016x %016x %-7s %5u" %
9ae3a8
-                   (target_start, target_end, host_addr.cast(self.uintptr_t),
9ae3a8
-                    message, len(self.guest_phys_blocks)))
9ae3a8
+        self.guest_phys_blocks = None
9ae3a8
 
9ae3a8
     def cpu_get_dump_info(self):
9ae3a8
         # We can't synchronize the registers with KVM post-mortem, and
9ae3a8
@@ -263,8 +266,7 @@ shape and this command should mostly work."""
9ae3a8
                                 len(name) + 1, len(desc), type, name, desc)
9ae3a8
 
9ae3a8
     def dump_init(self):
9ae3a8
-        self.guest_phys_blocks_init()
9ae3a8
-        self.guest_phys_blocks_append()
9ae3a8
+        self.guest_phys_blocks = get_guest_phys_blocks()
9ae3a8
         self.cpu_get_dump_info()
9ae3a8
         # we have no way to retrieve the VCPU status from KVM
9ae3a8
         # post-mortem
9ae3a8
@@ -310,7 +312,7 @@ shape and this command should mostly work."""
9ae3a8
             cur  = block["host_addr"]
9ae3a8
             left = block["target_end"] - block["target_start"]
9ae3a8
             print ("dumping range at %016x for length %016x" %
9ae3a8
-                   (cur.cast(self.uintptr_t), left))
9ae3a8
+                   (cur.cast(UINTPTR_T), left))
9ae3a8
             while (left > 0):
9ae3a8
                 chunk_size = min(TARGET_PAGE_SIZE, left)
9ae3a8
                 chunk = qemu_core.read_memory(cur, chunk_size)
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8