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