Blame SOURCES/kvm-scripts-dump-guest-memory.py-add-vmcoreinfo.patch

4a2fec
From 7d60c93dbdf3568c9b96cf6bc61af49966e1a06f Mon Sep 17 00:00:00 2001
4a2fec
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
4a2fec
Date: Mon, 27 Nov 2017 22:51:09 +0100
4a2fec
Subject: [PATCH 11/21] scripts/dump-guest-memory.py: add vmcoreinfo
4a2fec
MIME-Version: 1.0
4a2fec
Content-Type: text/plain; charset=UTF-8
4a2fec
Content-Transfer-Encoding: 8bit
4a2fec
4a2fec
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
4a2fec
Message-id: <20171127225111.24518-8-marcandre.lureau@redhat.com>
4a2fec
Patchwork-id: 77929
4a2fec
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 7/9] scripts/dump-guest-memory.py: add vmcoreinfo
4a2fec
Bugzilla: 1398633
4a2fec
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
4a2fec
RH-Acked-by: Andrew Jones <drjones@redhat.com>
4a2fec
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
Add a vmcoreinfo ELF note in the dump if vmcoreinfo device has the
4a2fec
memory location details.
4a2fec
4a2fec
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
4a2fec
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
4a2fec
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
4a2fec
4a2fec
(cherry picked from commit d23bfa91b7789534d16ede6cb7d925bfac3f3c4c)
4a2fec
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 scripts/dump-guest-memory.py | 61 ++++++++++++++++++++++++++++++++++++++++++++
4a2fec
 1 file changed, 61 insertions(+)
4a2fec
4a2fec
diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py
4a2fec
index f7c6635..69dd5ef 100644
4a2fec
--- a/scripts/dump-guest-memory.py
4a2fec
+++ b/scripts/dump-guest-memory.py
4a2fec
@@ -14,6 +14,7 @@ the COPYING file in the top-level directory.
4a2fec
 """
4a2fec
 
4a2fec
 import ctypes
4a2fec
+import struct
4a2fec
 
4a2fec
 UINTPTR_T = gdb.lookup_type("uintptr_t")
4a2fec
 
4a2fec
@@ -45,6 +46,17 @@ EM_S390 = 22
4a2fec
 EM_AARCH = 183
4a2fec
 EM_X86_64 = 62
4a2fec
 
4a2fec
+VMCOREINFO_FORMAT_ELF = 1
4a2fec
+
4a2fec
+def le16_to_cpu(val):
4a2fec
+    return struct.unpack("
4a2fec
+
4a2fec
+def le32_to_cpu(val):
4a2fec
+    return struct.unpack("
4a2fec
+
4a2fec
+def le64_to_cpu(val):
4a2fec
+    return struct.unpack("
4a2fec
+
4a2fec
 class ELF(object):
4a2fec
     """Representation of a ELF file."""
4a2fec
 
4a2fec
@@ -120,6 +132,25 @@ class ELF(object):
4a2fec
         self.segments[0].p_filesz += ctypes.sizeof(note)
4a2fec
         self.segments[0].p_memsz += ctypes.sizeof(note)
4a2fec
 
4a2fec
+
4a2fec
+    def add_vmcoreinfo_note(self, vmcoreinfo):
4a2fec
+        """Adds a vmcoreinfo note to the ELF dump."""
4a2fec
+        # compute the header size, and copy that many bytes from the note
4a2fec
+        header = get_arch_note(self.endianness, 0, 0)
4a2fec
+        ctypes.memmove(ctypes.pointer(header),
4a2fec
+                       vmcoreinfo, ctypes.sizeof(header))
4a2fec
+        if header.n_descsz > 1 << 20:
4a2fec
+            print('warning: invalid vmcoreinfo size')
4a2fec
+            return
4a2fec
+        # now get the full note
4a2fec
+        note = get_arch_note(self.endianness,
4a2fec
+                             header.n_namesz - 1, header.n_descsz)
4a2fec
+        ctypes.memmove(ctypes.pointer(note), vmcoreinfo, ctypes.sizeof(note))
4a2fec
+
4a2fec
+        self.notes.append(note)
4a2fec
+        self.segments[0].p_filesz += ctypes.sizeof(note)
4a2fec
+        self.segments[0].p_memsz += ctypes.sizeof(note)
4a2fec
+
4a2fec
     def add_segment(self, p_type, p_paddr, p_size):
4a2fec
         """Adds a segment to the elf."""
4a2fec
 
4a2fec
@@ -505,6 +536,35 @@ shape and this command should mostly work."""
4a2fec
                 cur += chunk_size
4a2fec
                 left -= chunk_size
4a2fec
 
4a2fec
+    def phys_memory_read(self, addr, size):
4a2fec
+        qemu_core = gdb.inferiors()[0]
4a2fec
+        for block in self.guest_phys_blocks:
4a2fec
+            if block["target_start"] <= addr \
4a2fec
+               and addr + size <= block["target_end"]:
4a2fec
+                haddr = block["host_addr"] + (addr - block["target_start"])
4a2fec
+                return qemu_core.read_memory(haddr, size)
4a2fec
+        return None
4a2fec
+
4a2fec
+    def add_vmcoreinfo(self):
4a2fec
+        if not gdb.parse_and_eval("vmcoreinfo_find()") \
4a2fec
+           or not gdb.parse_and_eval("vmcoreinfo_find()->has_vmcoreinfo"):
4a2fec
+            return
4a2fec
+
4a2fec
+        fmt = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.guest_format")
4a2fec
+        addr = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.paddr")
4a2fec
+        size = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.size")
4a2fec
+
4a2fec
+        fmt = le16_to_cpu(fmt)
4a2fec
+        addr = le64_to_cpu(addr)
4a2fec
+        size = le32_to_cpu(size)
4a2fec
+
4a2fec
+        if fmt != VMCOREINFO_FORMAT_ELF:
4a2fec
+            return
4a2fec
+
4a2fec
+        vmcoreinfo = self.phys_memory_read(addr, size)
4a2fec
+        if vmcoreinfo:
4a2fec
+            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
4a2fec
+
4a2fec
     def invoke(self, args, from_tty):
4a2fec
         """Handles command invocation from gdb."""
4a2fec
 
4a2fec
@@ -518,6 +578,7 @@ shape and this command should mostly work."""
4a2fec
 
4a2fec
         self.elf = ELF(argv[1])
4a2fec
         self.guest_phys_blocks = get_guest_phys_blocks()
4a2fec
+        self.add_vmcoreinfo()
4a2fec
 
4a2fec
         with open(argv[0], "wb") as vmcore:
4a2fec
             self.dump_init(vmcore)
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec