qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"Cleber Rosa" <crosa@redhat.com>
Subject: [Qemu-devel] [PULL 08/26] scripts/dump-guest-memory.py: add vmcoreinfo
Date: Sun, 15 Oct 2017 06:23:02 +0300	[thread overview]
Message-ID: <1508036858-13479-9-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1508036858-13479-1-git-send-email-mst@redhat.com>

From: Marc-André Lureau <marcandre.lureau@redhat.com>

Add a vmcoreinfo ELF note in the dump if vmcoreinfo device has the
memory location details.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 scripts/dump-guest-memory.py | 61 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py
index f7c6635..69dd5ef 100644
--- a/scripts/dump-guest-memory.py
+++ b/scripts/dump-guest-memory.py
@@ -14,6 +14,7 @@ the COPYING file in the top-level directory.
 """
 
 import ctypes
+import struct
 
 UINTPTR_T = gdb.lookup_type("uintptr_t")
 
@@ -45,6 +46,17 @@ EM_S390 = 22
 EM_AARCH = 183
 EM_X86_64 = 62
 
+VMCOREINFO_FORMAT_ELF = 1
+
+def le16_to_cpu(val):
+    return struct.unpack("<H", struct.pack("=H", val))[0]
+
+def le32_to_cpu(val):
+    return struct.unpack("<I", struct.pack("=I", val))[0]
+
+def le64_to_cpu(val):
+    return struct.unpack("<Q", struct.pack("=Q", val))[0]
+
 class ELF(object):
     """Representation of a ELF file."""
 
@@ -120,6 +132,25 @@ class ELF(object):
         self.segments[0].p_filesz += ctypes.sizeof(note)
         self.segments[0].p_memsz += ctypes.sizeof(note)
 
+
+    def add_vmcoreinfo_note(self, vmcoreinfo):
+        """Adds a vmcoreinfo note to the ELF dump."""
+        # compute the header size, and copy that many bytes from the note
+        header = get_arch_note(self.endianness, 0, 0)
+        ctypes.memmove(ctypes.pointer(header),
+                       vmcoreinfo, ctypes.sizeof(header))
+        if header.n_descsz > 1 << 20:
+            print('warning: invalid vmcoreinfo size')
+            return
+        # now get the full note
+        note = get_arch_note(self.endianness,
+                             header.n_namesz - 1, header.n_descsz)
+        ctypes.memmove(ctypes.pointer(note), vmcoreinfo, ctypes.sizeof(note))
+
+        self.notes.append(note)
+        self.segments[0].p_filesz += ctypes.sizeof(note)
+        self.segments[0].p_memsz += ctypes.sizeof(note)
+
     def add_segment(self, p_type, p_paddr, p_size):
         """Adds a segment to the elf."""
 
@@ -505,6 +536,35 @@ shape and this command should mostly work."""
                 cur += chunk_size
                 left -= chunk_size
 
+    def phys_memory_read(self, addr, size):
+        qemu_core = gdb.inferiors()[0]
+        for block in self.guest_phys_blocks:
+            if block["target_start"] <= addr \
+               and addr + size <= block["target_end"]:
+                haddr = block["host_addr"] + (addr - block["target_start"])
+                return qemu_core.read_memory(haddr, size)
+        return None
+
+    def add_vmcoreinfo(self):
+        if not gdb.parse_and_eval("vmcoreinfo_find()") \
+           or not gdb.parse_and_eval("vmcoreinfo_find()->has_vmcoreinfo"):
+            return
+
+        fmt = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.guest_format")
+        addr = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.paddr")
+        size = gdb.parse_and_eval("vmcoreinfo_find()->vmcoreinfo.size")
+
+        fmt = le16_to_cpu(fmt)
+        addr = le64_to_cpu(addr)
+        size = le32_to_cpu(size)
+
+        if fmt != VMCOREINFO_FORMAT_ELF:
+            return
+
+        vmcoreinfo = self.phys_memory_read(addr, size)
+        if vmcoreinfo:
+            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
+
     def invoke(self, args, from_tty):
         """Handles command invocation from gdb."""
 
@@ -518,6 +578,7 @@ shape and this command should mostly work."""
 
         self.elf = ELF(argv[1])
         self.guest_phys_blocks = get_guest_phys_blocks()
+        self.add_vmcoreinfo()
 
         with open(argv[0], "wb") as vmcore:
             self.dump_init(vmcore)
-- 
MST

  parent reply	other threads:[~2017-10-15  3:23 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-15  3:22 [Qemu-devel] [PULL 00/26] pc, pci, virtio: fixes, features Michael S. Tsirkin
2017-10-15  3:22 ` [Qemu-devel] [PULL 01/26] xio3130_downstream: Report error if pcie_chassis_add_slot() failed Michael S. Tsirkin
2017-10-15  3:22 ` [Qemu-devel] [PULL 02/26] pci: Set err to errp directly rather than through error_propagate() Michael S. Tsirkin
2017-10-15  3:22 ` [Qemu-devel] [PULL 03/26] fw_cfg: add write callback Michael S. Tsirkin
2017-10-15  3:22 ` [Qemu-devel] [PULL 04/26] hw/misc: add vmcoreinfo device Michael S. Tsirkin
2017-10-15  3:22 ` [Qemu-devel] [PULL 05/26] dump: add guest ELF note Michael S. Tsirkin
2017-10-15  3:22 ` [Qemu-devel] [PULL 06/26] dump: update phys_base header field based on VMCOREINFO content Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 07/26] kdump: set vmcoreinfo location Michael S. Tsirkin
2017-10-15  3:23 ` Michael S. Tsirkin [this message]
2017-10-15  3:23 ` [Qemu-devel] [PULL 09/26] MAINTAINERS: add Dump maintainers Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 10/26] virtio/vhost: reset dev->log after syncing Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 11/26] pci: allow 32-bit PCI IO accesses to pass through the PCI bridge Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 12/26] hw/pci-bridge/pcie_pci_bridge: properly handle MSI unavailability case Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 13/26] virtio/pci/migration: Convert to VMState Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 14/26] PCI: PCIe access should always be little endian Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 15/26] pci: conventional-pci-device and pci-express-device interfaces Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 16/26] pci: Add interface names to hybrid PCI devices Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 17/26] pci: Add INTERFACE_PCIE_DEVICE to all PCIe devices Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 18/26] pci: Add INTERFACE_CONVENTIONAL_PCI_DEVICE to Conventional PCI devices Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 19/26] xen/pt: Mark TYPE_XEN_PT_DEVICE as hybrid Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 20/26] pci: Validate interfaces on base_class_init Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 21/26] hw/gen_pcie_root_port: make IO RO 0 on IO disabled Michael S. Tsirkin
2017-10-15  3:23 ` [Qemu-devel] [PULL 22/26] virtio: fix descriptor counting in virtqueue_pop Michael S. Tsirkin
2017-10-15  3:24 ` [Qemu-devel] [PULL 23/26] virtio-pci: Replace modern_as with direct access to modern_bar Michael S. Tsirkin
2017-10-15  3:24 ` [Qemu-devel] [PULL 24/26] isapc: Remove unnecessary migration compatibility code Michael S. Tsirkin
2017-10-15  3:24 ` [Qemu-devel] [PULL 25/26] pc: remove useless hot_add_cpu initialisation Michael S. Tsirkin
2017-10-15  3:24 ` [Qemu-devel] [PULL 26/26] tests/pxe: Test more NICs when running in SPEED=slow mode Michael S. Tsirkin
2017-10-16 17:29 ` [Qemu-devel] [PULL 00/26] pc, pci, virtio: fixes, features Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1508036858-13479-9-git-send-email-mst@redhat.com \
    --to=mst@redhat.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).