yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
5d360b
From e40871f4bf8ca981075eb7baa5a8ff0f3f8f4a67 Mon Sep 17 00:00:00 2001
5d360b
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
5d360b
Date: Wed, 13 Dec 2017 13:39:07 +0100
5d360b
Subject: [PATCH 36/41] scripts/dump-guest-memory.py: Cleanup functions
5d360b
MIME-Version: 1.0
5d360b
Content-Type: text/plain; charset=UTF-8
5d360b
Content-Transfer-Encoding: 8bit
5d360b
5d360b
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
5d360b
Message-id: <20171213133912.26176-37-marcandre.lureau@redhat.com>
5d360b
Patchwork-id: 78387
5d360b
O-Subject: [RHEL-7.5 qemu-kvm PATCH v3 36/41] scripts/dump-guest-memory.py: Cleanup functions
5d360b
Bugzilla: 1411490
5d360b
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
5d360b
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
5d360b
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
5d360b
From: Janosch Frank <frankja@linux.vnet.ibm.com>
5d360b
5d360b
Increase readability by adding newlines and comments, as well as
5d360b
removing wrong whitespaces and C style braces around conditionals and
5d360b
loops.
5d360b
5d360b
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
5d360b
Signed-off-by: Janosch Frank <frankja@linux.vnet.ibm.com>
5d360b
Message-Id: <1453464520-3882-5-git-send-email-frankja@linux.vnet.ibm.com>
5d360b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
5d360b
5d360b
(cherry picked from commit 6782c0e785a0ba48cd96d99f2402cb87af027d26)
5d360b
5d360b
RHEL: conflicts due to qtailq usage
5d360b
5d360b
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
5d360b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
---
5d360b
 scripts/dump-guest-memory.py | 69 +++++++++++++++++++++++++++++---------------
5d360b
 1 file changed, 46 insertions(+), 23 deletions(-)
5d360b
5d360b
diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py
5d360b
index d44de99..3d54d05 100644
5d360b
--- a/scripts/dump-guest-memory.py
5d360b
+++ b/scripts/dump-guest-memory.py
5d360b
@@ -69,35 +69,58 @@ ELF64_PHDR = ("I"  # p_type
5d360b
           )
5d360b
 
5d360b
 def int128_get64(val):
5d360b
-    assert (val["hi"] == 0)
5d360b
+    """Returns low 64bit part of Int128 struct."""
5d360b
+
5d360b
+    assert val["hi"] == 0
5d360b
     return val["lo"]
5d360b
 
5d360b
 def qtailq_foreach(head, field_str):
5d360b
+    """Generator for qtails."""
5d360b
+
5d360b
     var_p = head["tqh_first"]
5d360b
     while (var_p != 0):
5d360b
         var = var_p.dereference()
5d360b
-        yield var
5d360b
         var_p = var[field_str]["tqe_next"]
5d360b
+        yield var
5d360b
 
5d360b
 def qemu_get_ram_block(ram_addr):
5d360b
+    """Returns the RAMBlock struct to which the given address belongs."""
5d360b
+
5d360b
     ram_blocks = gdb.parse_and_eval("ram_list.blocks")
5d360b
+
5d360b
     for block in qtailq_foreach(ram_blocks, "next"):
5d360b
         if (ram_addr - block["offset"] < block["length"]):
5d360b
             return block
5d360b
+
5d360b
     raise gdb.GdbError("Bad ram offset %x" % ram_addr)
5d360b
 
5d360b
+
5d360b
 def qemu_get_ram_ptr(ram_addr):
5d360b
+    """Returns qemu vaddr for given guest physical address."""
5d360b
+
5d360b
     block = qemu_get_ram_block(ram_addr)
5d360b
     return block["host"] + (ram_addr - block["offset"])
5d360b
 
5d360b
-def memory_region_get_ram_ptr(mr):
5d360b
-    if (mr["alias"] != 0):
5d360b
-        return (memory_region_get_ram_ptr(mr["alias"].dereference()) +
5d360b
-                mr["alias_offset"])
5d360b
-    return qemu_get_ram_ptr(mr["ram_addr"] & TARGET_PAGE_MASK)
5d360b
+
5d360b
+def memory_region_get_ram_ptr(memory_region):
5d360b
+    if memory_region["alias"] != 0:
5d360b
+        return (memory_region_get_ram_ptr(memory_region["alias"].dereference())
5d360b
+                + memory_region["alias_offset"])
5d360b
+
5d360b
+    return qemu_get_ram_ptr(memory_region["ram_addr"] & TARGET_PAGE_MASK)
5d360b
+
5d360b
 
5d360b
 def get_guest_phys_blocks():
5d360b
+    """Returns a list of ram blocks.
5d360b
+
5d360b
+    Each block entry contains:
5d360b
+    'target_start': guest block phys start address
5d360b
+    'target_end':   guest block phys end address
5d360b
+    'host_addr':    qemu vaddr of the block's start
5d360b
+    """
5d360b
+
5d360b
     guest_phys_blocks = []
5d360b
+
5d360b
     print("guest RAM blocks:")
5d360b
     print("target_start     target_end       host_addr        message "
5d360b
           "count")
5d360b
@@ -111,29 +134,29 @@ def get_guest_phys_blocks():
5d360b
     # compatibility. Otherwise range doesn't cast the value itself and
5d360b
     # breaks.
5d360b
     for cur in range(int(current_map["nr"])):
5d360b
-        flat_range   = (current_map["ranges"] + cur).dereference()
5d360b
-        mr           = flat_range["mr"].dereference()
5d360b
+        flat_range = (current_map["ranges"] + cur).dereference()
5d360b
+        memory_region = flat_range["mr"].dereference()
5d360b
 
5d360b
         # we only care about RAM
5d360b
-        if (not mr["ram"]):
5d360b
+        if not memory_region["ram"]:
5d360b
             continue
5d360b
 
5d360b
         section_size = int128_get64(flat_range["addr"]["size"])
5d360b
         target_start = int128_get64(flat_range["addr"]["start"])
5d360b
-        target_end   = target_start + section_size
5d360b
-        host_addr    = (memory_region_get_ram_ptr(mr) +
5d360b
-                        flat_range["offset_in_region"])
5d360b
+        target_end = target_start + section_size
5d360b
+        host_addr = (memory_region_get_ram_ptr(memory_region)
5d360b
+                     + flat_range["offset_in_region"])
5d360b
         predecessor = None
5d360b
 
5d360b
         # find continuity in guest physical address space
5d360b
-        if (len(guest_phys_blocks) > 0):
5d360b
+        if len(guest_phys_blocks) > 0:
5d360b
             predecessor = guest_phys_blocks[-1]
5d360b
             predecessor_size = (predecessor["target_end"] -
5d360b
                                 predecessor["target_start"])
5d360b
 
5d360b
             # the memory API guarantees monotonically increasing
5d360b
             # traversal
5d360b
-            assert (predecessor["target_end"] <= target_start)
5d360b
+            assert predecessor["target_end"] <= target_start
5d360b
 
5d360b
             # we want continuity in both guest-physical and
5d360b
             # host-virtual memory
5d360b
@@ -141,11 +164,11 @@ def get_guest_phys_blocks():
5d360b
                 predecessor["host_addr"] + predecessor_size != host_addr):
5d360b
                 predecessor = None
5d360b
 
5d360b
-        if (predecessor is None):
5d360b
+        if predecessor is None:
5d360b
             # isolated mapping, add it to the list
5d360b
             guest_phys_blocks.append({"target_start": target_start,
5d360b
-                                      "target_end"  : target_end,
5d360b
-                                      "host_addr"   : host_addr})
5d360b
+                                      "target_end":   target_end,
5d360b
+                                      "host_addr":    host_addr})
5d360b
             message = "added"
5d360b
         else:
5d360b
             # expand predecessor until @target_end; predecessor's
5d360b
@@ -282,7 +305,7 @@ shape and this command should mostly work."""
5d360b
         # We should never reach PN_XNUM for paging=false dumps: there's
5d360b
         # just a handful of discontiguous ranges after merging.
5d360b
         self.phdr_num += len(self.guest_phys_blocks)
5d360b
-        assert (self.phdr_num < PN_XNUM)
5d360b
+        assert self.phdr_num < PN_XNUM
5d360b
 
5d360b
         # Calculate the ELF file offset where the memory dump commences:
5d360b
         #
5d360b
@@ -313,15 +336,15 @@ shape and this command should mostly work."""
5d360b
     def dump_iterate(self, vmcore):
5d360b
         qemu_core = gdb.inferiors()[0]
5d360b
         for block in self.guest_phys_blocks:
5d360b
-            cur  = block["host_addr"]
5d360b
+            cur = block["host_addr"]
5d360b
             left = block["target_end"] - block["target_start"]
5d360b
             print("dumping range at %016x for length %016x" %
5d360b
                   (cur.cast(UINTPTR_T), left))
5d360b
-            while (left > 0):
5d360b
+            while left > 0:
5d360b
                 chunk_size = min(TARGET_PAGE_SIZE, left)
5d360b
                 chunk = qemu_core.read_memory(cur, chunk_size)
5d360b
                 vmcore.write(chunk)
5d360b
-                cur  += chunk_size
5d360b
+                cur += chunk_size
5d360b
                 left -= chunk_size
5d360b
 
5d360b
     def create_vmcore(self, filename):
5d360b
@@ -336,7 +359,7 @@ shape and this command should mostly work."""
5d360b
         self.dont_repeat()
5d360b
 
5d360b
         argv = gdb.string_to_argv(args)
5d360b
-        if (len(argv) != 1):
5d360b
+        if len(argv) != 1:
5d360b
             raise gdb.GdbError("usage: dump-guest-memory FILE")
5d360b
 
5d360b
         self.dump_init()
5d360b
-- 
5d360b
1.8.3.1
5d360b