thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-scripts-dump-guest-memory.py-Cleanup-functions.patch

9ae3a8
From e40871f4bf8ca981075eb7baa5a8ff0f3f8f4a67 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:07 +0100
9ae3a8
Subject: [PATCH 36/41] scripts/dump-guest-memory.py: Cleanup 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-37-marcandre.lureau@redhat.com>
9ae3a8
Patchwork-id: 78387
9ae3a8
O-Subject: [RHEL-7.5 qemu-kvm PATCH v3 36/41] scripts/dump-guest-memory.py: Cleanup 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
Increase readability by adding newlines and comments, as well as
9ae3a8
removing wrong whitespaces and C style braces around conditionals and
9ae3a8
loops.
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-5-git-send-email-frankja@linux.vnet.ibm.com>
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
9ae3a8
(cherry picked from commit 6782c0e785a0ba48cd96d99f2402cb87af027d26)
9ae3a8
9ae3a8
RHEL: conflicts due to qtailq usage
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 | 69 +++++++++++++++++++++++++++++---------------
9ae3a8
 1 file changed, 46 insertions(+), 23 deletions(-)
9ae3a8
9ae3a8
diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py
9ae3a8
index d44de99..3d54d05 100644
9ae3a8
--- a/scripts/dump-guest-memory.py
9ae3a8
+++ b/scripts/dump-guest-memory.py
9ae3a8
@@ -69,35 +69,58 @@ ELF64_PHDR = ("I"  # p_type
9ae3a8
           )
9ae3a8
 
9ae3a8
 def int128_get64(val):
9ae3a8
-    assert (val["hi"] == 0)
9ae3a8
+    """Returns low 64bit part of Int128 struct."""
9ae3a8
+
9ae3a8
+    assert val["hi"] == 0
9ae3a8
     return val["lo"]
9ae3a8
 
9ae3a8
 def qtailq_foreach(head, field_str):
9ae3a8
+    """Generator for qtails."""
9ae3a8
+
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
+        yield var
9ae3a8
 
9ae3a8
 def qemu_get_ram_block(ram_addr):
9ae3a8
+    """Returns the RAMBlock struct to which the given address belongs."""
9ae3a8
+
9ae3a8
     ram_blocks = gdb.parse_and_eval("ram_list.blocks")
9ae3a8
+
9ae3a8
     for block in qtailq_foreach(ram_blocks, "next"):
9ae3a8
         if (ram_addr - block["offset"] < block["length"]):
9ae3a8
             return block
9ae3a8
+
9ae3a8
     raise gdb.GdbError("Bad ram offset %x" % ram_addr)
9ae3a8
 
9ae3a8
+
9ae3a8
 def qemu_get_ram_ptr(ram_addr):
9ae3a8
+    """Returns qemu vaddr for given guest physical address."""
9ae3a8
+
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 memory_region_get_ram_ptr(memory_region):
9ae3a8
+    if memory_region["alias"] != 0:
9ae3a8
+        return (memory_region_get_ram_ptr(memory_region["alias"].dereference())
9ae3a8
+                + memory_region["alias_offset"])
9ae3a8
+
9ae3a8
+    return qemu_get_ram_ptr(memory_region["ram_addr"] & TARGET_PAGE_MASK)
9ae3a8
+
9ae3a8
 
9ae3a8
 def get_guest_phys_blocks():
9ae3a8
+    """Returns a list of ram blocks.
9ae3a8
+
9ae3a8
+    Each block entry contains:
9ae3a8
+    'target_start': guest block phys start address
9ae3a8
+    'target_end':   guest block phys end address
9ae3a8
+    'host_addr':    qemu vaddr of the block's start
9ae3a8
+    """
9ae3a8
+
9ae3a8
     guest_phys_blocks = []
9ae3a8
+
9ae3a8
     print("guest RAM blocks:")
9ae3a8
     print("target_start     target_end       host_addr        message "
9ae3a8
           "count")
9ae3a8
@@ -111,29 +134,29 @@ def get_guest_phys_blocks():
9ae3a8
     # compatibility. Otherwise range doesn't cast the value itself and
9ae3a8
     # breaks.
9ae3a8
     for cur in range(int(current_map["nr"])):
9ae3a8
-        flat_range   = (current_map["ranges"] + cur).dereference()
9ae3a8
-        mr           = flat_range["mr"].dereference()
9ae3a8
+        flat_range = (current_map["ranges"] + cur).dereference()
9ae3a8
+        memory_region = flat_range["mr"].dereference()
9ae3a8
 
9ae3a8
         # we only care about RAM
9ae3a8
-        if (not mr["ram"]):
9ae3a8
+        if not memory_region["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
+        target_end = target_start + section_size
9ae3a8
+        host_addr = (memory_region_get_ram_ptr(memory_region)
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
+        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
+            assert predecessor["target_end"] <= target_start
9ae3a8
 
9ae3a8
             # we want continuity in both guest-physical and
9ae3a8
             # host-virtual memory
9ae3a8
@@ -141,11 +164,11 @@ def get_guest_phys_blocks():
9ae3a8
                 predecessor["host_addr"] + predecessor_size != host_addr):
9ae3a8
                 predecessor = None
9ae3a8
 
9ae3a8
-        if (predecessor is None):
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
+                                      "target_end":   target_end,
9ae3a8
+                                      "host_addr":    host_addr})
9ae3a8
             message = "added"
9ae3a8
         else:
9ae3a8
             # expand predecessor until @target_end; predecessor's
9ae3a8
@@ -282,7 +305,7 @@ shape and this command should mostly work."""
9ae3a8
         # We should never reach PN_XNUM for paging=false dumps: there's
9ae3a8
         # just a handful of discontiguous ranges after merging.
9ae3a8
         self.phdr_num += len(self.guest_phys_blocks)
9ae3a8
-        assert (self.phdr_num < PN_XNUM)
9ae3a8
+        assert self.phdr_num < PN_XNUM
9ae3a8
 
9ae3a8
         # Calculate the ELF file offset where the memory dump commences:
9ae3a8
         #
9ae3a8
@@ -313,15 +336,15 @@ shape and this command should mostly work."""
9ae3a8
     def dump_iterate(self, vmcore):
9ae3a8
         qemu_core = gdb.inferiors()[0]
9ae3a8
         for block in self.guest_phys_blocks:
9ae3a8
-            cur  = block["host_addr"]
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(UINTPTR_T), left))
9ae3a8
-            while (left > 0):
9ae3a8
+            while left > 0:
9ae3a8
                 chunk_size = min(TARGET_PAGE_SIZE, left)
9ae3a8
                 chunk = qemu_core.read_memory(cur, chunk_size)
9ae3a8
                 vmcore.write(chunk)
9ae3a8
-                cur  += chunk_size
9ae3a8
+                cur += chunk_size
9ae3a8
                 left -= chunk_size
9ae3a8
 
9ae3a8
     def create_vmcore(self, filename):
9ae3a8
@@ -336,7 +359,7 @@ shape and this command should mostly work."""
9ae3a8
         self.dont_repeat()
9ae3a8
 
9ae3a8
         argv = gdb.string_to_argv(args)
9ae3a8
-        if (len(argv) != 1):
9ae3a8
+        if len(argv) != 1:
9ae3a8
             raise gdb.GdbError("usage: dump-guest-memory FILE")
9ae3a8
 
9ae3a8
         self.dump_init()
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8