yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

9ae3a8
From 8a0fb382404cba1a4d88dd489a14d585fc95f3cf 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:10 +0100
9ae3a8
Subject: [PATCH 39/41] scripts/dump-guest-memory.py: add vmcoreinfo
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-40-marcandre.lureau@redhat.com>
9ae3a8
Patchwork-id: 78389
9ae3a8
O-Subject: [RHEL-7.5 qemu-kvm PATCH v3 39/41] scripts/dump-guest-memory.py: add vmcoreinfo
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
Add a vmcoreinfo ELF note in the dump if vmcoreinfo device has the
9ae3a8
memory location details.
9ae3a8
9ae3a8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
9ae3a8
(cherry picked from commit d23bfa91b7789534d16ede6cb7d925bfac3f3c4c)
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 | 61 ++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 1 file changed, 61 insertions(+)
9ae3a8
9ae3a8
diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py
9ae3a8
index 03d692d..471aa73 100644
9ae3a8
--- a/scripts/dump-guest-memory.py
9ae3a8
+++ b/scripts/dump-guest-memory.py
9ae3a8
@@ -17,6 +17,7 @@
9ae3a8
 # gdb.
9ae3a8
 
9ae3a8
 import ctypes
9ae3a8
+import struct
9ae3a8
 
9ae3a8
 UINTPTR_T = gdb.lookup_type("uintptr_t")
9ae3a8
 
9ae3a8
@@ -48,6 +49,17 @@ EM_S390 = 22
9ae3a8
 EM_AARCH = 183
9ae3a8
 EM_X86_64 = 62
9ae3a8
 
9ae3a8
+VMCOREINFO_FORMAT_ELF = 1
9ae3a8
+
9ae3a8
+def le16_to_cpu(val):
9ae3a8
+    return struct.unpack("
9ae3a8
+
9ae3a8
+def le32_to_cpu(val):
9ae3a8
+    return struct.unpack("
9ae3a8
+
9ae3a8
+def le64_to_cpu(val):
9ae3a8
+    return struct.unpack("
9ae3a8
+
9ae3a8
 class ELF(object):
9ae3a8
     """Representation of a ELF file."""
9ae3a8
 
9ae3a8
@@ -123,6 +135,25 @@ class ELF(object):
9ae3a8
         self.segments[0].p_filesz += ctypes.sizeof(note)
9ae3a8
         self.segments[0].p_memsz += ctypes.sizeof(note)
9ae3a8
 
9ae3a8
+
9ae3a8
+    def add_vmcoreinfo_note(self, vmcoreinfo):
9ae3a8
+        """Adds a vmcoreinfo note to the ELF dump."""
9ae3a8
+        # compute the header size, and copy that many bytes from the note
9ae3a8
+        header = get_arch_note(self.endianness, 0, 0)
9ae3a8
+        ctypes.memmove(ctypes.pointer(header),
9ae3a8
+                       vmcoreinfo, ctypes.sizeof(header))
9ae3a8
+        if header.n_descsz > 1 << 20:
9ae3a8
+            print('warning: invalid vmcoreinfo size')
9ae3a8
+            return
9ae3a8
+        # now get the full note
9ae3a8
+        note = get_arch_note(self.endianness,
9ae3a8
+                             header.n_namesz - 1, header.n_descsz)
9ae3a8
+        ctypes.memmove(ctypes.pointer(note), vmcoreinfo, ctypes.sizeof(note))
9ae3a8
+
9ae3a8
+        self.notes.append(note)
9ae3a8
+        self.segments[0].p_filesz += ctypes.sizeof(note)
9ae3a8
+        self.segments[0].p_memsz += ctypes.sizeof(note)
9ae3a8
+
9ae3a8
     def add_segment(self, p_type, p_paddr, p_size):
9ae3a8
         """Adds a segment to the elf."""
9ae3a8
 
9ae3a8
@@ -505,6 +536,35 @@ shape and this command should mostly work."""
9ae3a8
                 cur += chunk_size
9ae3a8
                 left -= chunk_size
9ae3a8
 
9ae3a8
+    def phys_memory_read(self, addr, size):
9ae3a8
+        qemu_core = gdb.inferiors()[0]
9ae3a8
+        for block in self.guest_phys_blocks:
9ae3a8
+            if block["target_start"] <= addr \
9ae3a8
+               and addr + size <= block["target_end"]:
9ae3a8
+                haddr = block["host_addr"] + (addr - block["target_start"])
9ae3a8
+                return qemu_core.read_memory(haddr, size)
9ae3a8
+        return None
9ae3a8
+
9ae3a8
+    def add_vmcoreinfo(self):
9ae3a8
+        if not gdb.parse_and_eval("vmcoreinfo_find()") \
9ae3a8
+           or not gdb.parse_and_eval("vmcoreinfo_find()->has_vmcoreinfo"):
9ae3a8
+            return
9ae3a8
+
9ae3a8
+        fmt = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.guest_format")
9ae3a8
+        addr = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.paddr")
9ae3a8
+        size = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.size")
9ae3a8
+
9ae3a8
+        fmt = le16_to_cpu(fmt)
9ae3a8
+        addr = le64_to_cpu(addr)
9ae3a8
+        size = le32_to_cpu(size)
9ae3a8
+
9ae3a8
+        if fmt != VMCOREINFO_FORMAT_ELF:
9ae3a8
+            return
9ae3a8
+
9ae3a8
+        vmcoreinfo = self.phys_memory_read(addr, size)
9ae3a8
+        if vmcoreinfo:
9ae3a8
+            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
9ae3a8
+
9ae3a8
     def invoke(self, args, from_tty):
9ae3a8
         """Handles command invocation from gdb."""
9ae3a8
 
9ae3a8
@@ -518,6 +578,7 @@ shape and this command should mostly work."""
9ae3a8
 
9ae3a8
         self.elf = ELF("X86_64")
9ae3a8
         self.guest_phys_blocks = get_guest_phys_blocks()
9ae3a8
+        self.add_vmcoreinfo()
9ae3a8
 
9ae3a8
         with open(argv[0], "wb") as vmcore:
9ae3a8
             self.dump_init(vmcore)
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8