5d360b
From 231682fd6a7ca6fc5eecbec3df2f96133b9f3729 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:04 +0100
5d360b
Subject: [PATCH 33/41] scripts/dump-guest-memory.py: Move constants to the top
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-34-marcandre.lureau@redhat.com>
5d360b
Patchwork-id: 78378
5d360b
O-Subject: [RHEL-7.5 qemu-kvm PATCH v3 33/41] scripts/dump-guest-memory.py: Move constants to the top
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
The constants bloated the class definition and were therefore moved to
5d360b
the top.
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-2-git-send-email-frankja@linux.vnet.ibm.com>
5d360b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
5d360b
5d360b
(cherry picked from commit ca81ce72b4d12494424d1813c6437035c1f89a8c)
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 | 126 +++++++++++++++++++++----------------------
5d360b
 1 file changed, 63 insertions(+), 63 deletions(-)
5d360b
5d360b
diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py
5d360b
index 1ed8b67..29f7c5b 100644
5d360b
--- a/scripts/dump-guest-memory.py
5d360b
+++ b/scripts/dump-guest-memory.py
5d360b
@@ -17,6 +17,55 @@
5d360b
 
5d360b
 import struct
5d360b
 
5d360b
+TARGET_PAGE_SIZE = 0x1000
5d360b
+TARGET_PAGE_MASK = 0xFFFFFFFFFFFFF000
5d360b
+
5d360b
+# Various ELF constants
5d360b
+EM_X86_64   = 62        # AMD x86-64 target machine
5d360b
+ELFDATA2LSB = 1         # little endian
5d360b
+ELFCLASS64  = 2
5d360b
+ELFMAG      = "\x7FELF"
5d360b
+EV_CURRENT  = 1
5d360b
+ET_CORE     = 4
5d360b
+PT_LOAD     = 1
5d360b
+PT_NOTE     = 4
5d360b
+
5d360b
+# Special value for e_phnum. This indicates that the real number of
5d360b
+# program headers is too large to fit into e_phnum. Instead the real
5d360b
+# value is in the field sh_info of section 0.
5d360b
+PN_XNUM = 0xFFFF
5d360b
+
5d360b
+# Format strings for packing and header size calculation.
5d360b
+ELF64_EHDR = ("4s" # e_ident/magic
5d360b
+              "B"  # e_ident/class
5d360b
+              "B"  # e_ident/data
5d360b
+              "B"  # e_ident/version
5d360b
+              "B"  # e_ident/osabi
5d360b
+              "8s" # e_ident/pad
5d360b
+              "H"  # e_type
5d360b
+              "H"  # e_machine
5d360b
+              "I"  # e_version
5d360b
+              "Q"  # e_entry
5d360b
+              "Q"  # e_phoff
5d360b
+              "Q"  # e_shoff
5d360b
+              "I"  # e_flags
5d360b
+              "H"  # e_ehsize
5d360b
+              "H"  # e_phentsize
5d360b
+              "H"  # e_phnum
5d360b
+              "H"  # e_shentsize
5d360b
+              "H"  # e_shnum
5d360b
+              "H"  # e_shstrndx
5d360b
+          )
5d360b
+ELF64_PHDR = ("I"  # p_type
5d360b
+              "I"  # p_flags
5d360b
+              "Q"  # p_offset
5d360b
+              "Q"  # p_vaddr
5d360b
+              "Q"  # p_paddr
5d360b
+              "Q"  # p_filesz
5d360b
+              "Q"  # p_memsz
5d360b
+              "Q"  # p_align
5d360b
+          )
5d360b
+
5d360b
 class DumpGuestMemory(gdb.Command):
5d360b
     """Extract guest vmcore from qemu process coredump.
5d360b
 
5d360b
@@ -47,62 +96,13 @@ deliberately called abort(), or it was dumped in response to a signal at
5d360b
 a halfway fortunate point, then its coredump should be in reasonable
5d360b
 shape and this command should mostly work."""
5d360b
 
5d360b
-    TARGET_PAGE_SIZE = 0x1000
5d360b
-    TARGET_PAGE_MASK = 0xFFFFFFFFFFFFF000
5d360b
-
5d360b
-    # Various ELF constants
5d360b
-    EM_X86_64   = 62        # AMD x86-64 target machine
5d360b
-    ELFDATA2LSB = 1         # little endian
5d360b
-    ELFCLASS64  = 2
5d360b
-    ELFMAG      = "\x7FELF"
5d360b
-    EV_CURRENT  = 1
5d360b
-    ET_CORE     = 4
5d360b
-    PT_LOAD     = 1
5d360b
-    PT_NOTE     = 4
5d360b
-
5d360b
-    # Special value for e_phnum. This indicates that the real number of
5d360b
-    # program headers is too large to fit into e_phnum. Instead the real
5d360b
-    # value is in the field sh_info of section 0.
5d360b
-    PN_XNUM = 0xFFFF
5d360b
-
5d360b
-    # Format strings for packing and header size calculation.
5d360b
-    ELF64_EHDR = ("4s" # e_ident/magic
5d360b
-                  "B"  # e_ident/class
5d360b
-                  "B"  # e_ident/data
5d360b
-                  "B"  # e_ident/version
5d360b
-                  "B"  # e_ident/osabi
5d360b
-                  "8s" # e_ident/pad
5d360b
-                  "H"  # e_type
5d360b
-                  "H"  # e_machine
5d360b
-                  "I"  # e_version
5d360b
-                  "Q"  # e_entry
5d360b
-                  "Q"  # e_phoff
5d360b
-                  "Q"  # e_shoff
5d360b
-                  "I"  # e_flags
5d360b
-                  "H"  # e_ehsize
5d360b
-                  "H"  # e_phentsize
5d360b
-                  "H"  # e_phnum
5d360b
-                  "H"  # e_shentsize
5d360b
-                  "H"  # e_shnum
5d360b
-                  "H"  # e_shstrndx
5d360b
-                 )
5d360b
-    ELF64_PHDR = ("I"  # p_type
5d360b
-                  "I"  # p_flags
5d360b
-                  "Q"  # p_offset
5d360b
-                  "Q"  # p_vaddr
5d360b
-                  "Q"  # p_paddr
5d360b
-                  "Q"  # p_filesz
5d360b
-                  "Q"  # p_memsz
5d360b
-                  "Q"  # p_align
5d360b
-                 )
5d360b
-
5d360b
     def __init__(self):
5d360b
         super(DumpGuestMemory, self).__init__("dump-guest-memory",
5d360b
                                               gdb.COMMAND_DATA,
5d360b
                                               gdb.COMPLETE_FILENAME)
5d360b
         self.uintptr_t     = gdb.lookup_type("uintptr_t")
5d360b
-        self.elf64_ehdr_le = struct.Struct("<%s" % self.ELF64_EHDR)
5d360b
-        self.elf64_phdr_le = struct.Struct("<%s" % self.ELF64_PHDR)
5d360b
+        self.elf64_ehdr_le = struct.Struct("<%s" % ELF64_EHDR)
5d360b
+        self.elf64_phdr_le = struct.Struct("<%s" % ELF64_PHDR)
5d360b
 
5d360b
     def int128_get64(self, val):
5d360b
         assert (val["hi"] == 0)
5d360b
@@ -130,7 +130,7 @@ shape and this command should mostly work."""
5d360b
         if (mr["alias"] != 0):
5d360b
             return (self.memory_region_get_ram_ptr(mr["alias"].dereference()) +
5d360b
                     mr["alias_offset"])
5d360b
-        return self.qemu_get_ram_ptr(mr["ram_addr"] & self.TARGET_PAGE_MASK)
5d360b
+        return self.qemu_get_ram_ptr(mr["ram_addr"] & TARGET_PAGE_MASK)
5d360b
 
5d360b
     def guest_phys_blocks_init(self):
5d360b
         self.guest_phys_blocks = []
5d360b
@@ -198,21 +198,21 @@ shape and this command should mostly work."""
5d360b
         # most common values. This also means that instruction pointer
5d360b
         # etc. will be bogus in the dump, but at least the RAM contents
5d360b
         # should be valid.
5d360b
-        self.dump_info = {"d_machine": self.EM_X86_64,
5d360b
-                          "d_endian" : self.ELFDATA2LSB,
5d360b
-                          "d_class"  : self.ELFCLASS64}
5d360b
+        self.dump_info = {"d_machine": EM_X86_64,
5d360b
+                          "d_endian" : ELFDATA2LSB,
5d360b
+                          "d_class"  : ELFCLASS64}
5d360b
 
5d360b
     def encode_elf64_ehdr_le(self):
5d360b
         return self.elf64_ehdr_le.pack(
5d360b
-                                 self.ELFMAG,                 # e_ident/magic
5d360b
+                                 ELFMAG,                      # e_ident/magic
5d360b
                                  self.dump_info["d_class"],   # e_ident/class
5d360b
                                  self.dump_info["d_endian"],  # e_ident/data
5d360b
-                                 self.EV_CURRENT,             # e_ident/version
5d360b
+                                 EV_CURRENT,                  # e_ident/version
5d360b
                                  0,                           # e_ident/osabi
5d360b
                                  "",                          # e_ident/pad
5d360b
-                                 self.ET_CORE,                # e_type
5d360b
+                                 ET_CORE,                     # e_type
5d360b
                                  self.dump_info["d_machine"], # e_machine
5d360b
-                                 self.EV_CURRENT,             # e_version
5d360b
+                                 EV_CURRENT,                  # e_version
5d360b
                                  0,                           # e_entry
5d360b
                                  self.elf64_ehdr_le.size,     # e_phoff
5d360b
                                  0,                           # e_shoff
5d360b
@@ -226,7 +226,7 @@ shape and this command should mostly work."""
5d360b
                                 )
5d360b
 
5d360b
     def encode_elf64_note_le(self):
5d360b
-        return self.elf64_phdr_le.pack(self.PT_NOTE,         # p_type
5d360b
+        return self.elf64_phdr_le.pack(PT_NOTE,              # p_type
5d360b
                                        0,                    # p_flags
5d360b
                                        (self.memory_offset -
5d360b
                                         len(self.note)),     # p_offset
5d360b
@@ -238,7 +238,7 @@ shape and this command should mostly work."""
5d360b
                                       )
5d360b
 
5d360b
     def encode_elf64_load_le(self, offset, start_hwaddr, range_size):
5d360b
-        return self.elf64_phdr_le.pack(self.PT_LOAD, # p_type
5d360b
+        return self.elf64_phdr_le.pack(PT_LOAD,      # p_type
5d360b
                                        0,            # p_flags
5d360b
                                        offset,       # p_offset
5d360b
                                        0,            # p_vaddr
5d360b
@@ -276,7 +276,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 < self.PN_XNUM)
5d360b
+        assert (self.phdr_num < PN_XNUM)
5d360b
 
5d360b
         # Calculate the ELF file offset where the memory dump commences:
5d360b
         #
5d360b
@@ -312,7 +312,7 @@ shape and this command should mostly work."""
5d360b
             print ("dumping range at %016x for length %016x" %
5d360b
                    (cur.cast(self.uintptr_t), left))
5d360b
             while (left > 0):
5d360b
-                chunk_size = min(self.TARGET_PAGE_SIZE, left)
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
-- 
5d360b
1.8.3.1
5d360b