{"id":812449,"url":"http://patchwork.ozlabs.org/api/patches/812449/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170911165929.2791-7-marcandre.lureau@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170911165929.2791-7-marcandre.lureau@redhat.com>","list_archive_url":null,"date":"2017-09-11T16:59:28","name":"[v6,6/7] scripts/dump-guest-memory.py: add vmcoreinfo","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"22be3bcd2f4b3038e08cdecfcad4cc0713613800","submitter":{"id":66774,"url":"http://patchwork.ozlabs.org/api/people/66774/?format=json","name":"Marc-André Lureau","email":"marcandre.lureau@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170911165929.2791-7-marcandre.lureau@redhat.com/mbox/","series":[{"id":2525,"url":"http://patchwork.ozlabs.org/api/series/2525/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=2525","date":"2017-09-11T16:59:22","name":"KASLR kernel dump support","version":6,"mbox":"http://patchwork.ozlabs.org/series/2525/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/812449/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/812449/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx06.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=marcandre.lureau@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xrZCp6bMbz9s81\n\tfor <incoming@patchwork.ozlabs.org>;\n\tTue, 12 Sep 2017 03:09:26 +1000 (AEST)","from localhost ([::1]:59212 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1drSD6-0005RM-RP\n\tfor incoming@patchwork.ozlabs.org; Mon, 11 Sep 2017 13:09:25 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:57877)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <marcandre.lureau@redhat.com>) id 1drS4M-0006Ui-Gg\n\tfor qemu-devel@nongnu.org; Mon, 11 Sep 2017 13:00:28 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <marcandre.lureau@redhat.com>) id 1drS4I-0007Je-Ka\n\tfor qemu-devel@nongnu.org; Mon, 11 Sep 2017 13:00:22 -0400","from mx1.redhat.com ([209.132.183.28]:54982)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <marcandre.lureau@redhat.com>)\n\tid 1drS4I-0007J7-CK\n\tfor qemu-devel@nongnu.org; Mon, 11 Sep 2017 13:00:18 -0400","from smtp.corp.redhat.com\n\t(int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 5E4991B5C53\n\tfor <qemu-devel@nongnu.org>; Mon, 11 Sep 2017 17:00:17 +0000 (UTC)","from localhost (ovpn-112-51.ams2.redhat.com [10.36.112.51])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 131FA5C2F4;\n\tMon, 11 Sep 2017 17:00:02 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 5E4991B5C53","From":"=?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>","To":"qemu-devel@nongnu.org","Date":"Mon, 11 Sep 2017 18:59:28 +0200","Message-Id":"<20170911165929.2791-7-marcandre.lureau@redhat.com>","In-Reply-To":"<20170911165929.2791-1-marcandre.lureau@redhat.com>","References":"<20170911165929.2791-1-marcandre.lureau@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.12","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.30]);\n\tMon, 11 Sep 2017 17:00:17 +0000 (UTC)","Content-Transfer-Encoding":"quoted-printable","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [PATCH v6 6/7] scripts/dump-guest-memory.py: add\n\tvmcoreinfo","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"ehabkost@redhat.com, mst@redhat.com, anderson@redhat.com, =?utf-8?q?Ma?=\n\t=?utf-8?q?rc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>,\n\timammedo@redhat.com, lersek@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"Add a vmcoreinfo ELF note in the dump if vmcoreinfo device has the\nmemory location details.\n\nSigned-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>\n---\n scripts/dump-guest-memory.py | 61 ++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 61 insertions(+)","diff":"diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py\nindex f7c6635f15..69dd5efadf 100644\n--- a/scripts/dump-guest-memory.py\n+++ b/scripts/dump-guest-memory.py\n@@ -14,6 +14,7 @@ the COPYING file in the top-level directory.\n \"\"\"\n \n import ctypes\n+import struct\n \n UINTPTR_T = gdb.lookup_type(\"uintptr_t\")\n \n@@ -45,6 +46,17 @@ EM_S390 = 22\n EM_AARCH = 183\n EM_X86_64 = 62\n \n+VMCOREINFO_FORMAT_ELF = 1\n+\n+def le16_to_cpu(val):\n+    return struct.unpack(\"<H\", struct.pack(\"=H\", val))[0]\n+\n+def le32_to_cpu(val):\n+    return struct.unpack(\"<I\", struct.pack(\"=I\", val))[0]\n+\n+def le64_to_cpu(val):\n+    return struct.unpack(\"<Q\", struct.pack(\"=Q\", val))[0]\n+\n class ELF(object):\n     \"\"\"Representation of a ELF file.\"\"\"\n \n@@ -120,6 +132,25 @@ class ELF(object):\n         self.segments[0].p_filesz += ctypes.sizeof(note)\n         self.segments[0].p_memsz += ctypes.sizeof(note)\n \n+\n+    def add_vmcoreinfo_note(self, vmcoreinfo):\n+        \"\"\"Adds a vmcoreinfo note to the ELF dump.\"\"\"\n+        # compute the header size, and copy that many bytes from the note\n+        header = get_arch_note(self.endianness, 0, 0)\n+        ctypes.memmove(ctypes.pointer(header),\n+                       vmcoreinfo, ctypes.sizeof(header))\n+        if header.n_descsz > 1 << 20:\n+            print('warning: invalid vmcoreinfo size')\n+            return\n+        # now get the full note\n+        note = get_arch_note(self.endianness,\n+                             header.n_namesz - 1, header.n_descsz)\n+        ctypes.memmove(ctypes.pointer(note), vmcoreinfo, ctypes.sizeof(note))\n+\n+        self.notes.append(note)\n+        self.segments[0].p_filesz += ctypes.sizeof(note)\n+        self.segments[0].p_memsz += ctypes.sizeof(note)\n+\n     def add_segment(self, p_type, p_paddr, p_size):\n         \"\"\"Adds a segment to the elf.\"\"\"\n \n@@ -505,6 +536,35 @@ shape and this command should mostly work.\"\"\"\n                 cur += chunk_size\n                 left -= chunk_size\n \n+    def phys_memory_read(self, addr, size):\n+        qemu_core = gdb.inferiors()[0]\n+        for block in self.guest_phys_blocks:\n+            if block[\"target_start\"] <= addr \\\n+               and addr + size <= block[\"target_end\"]:\n+                haddr = block[\"host_addr\"] + (addr - block[\"target_start\"])\n+                return qemu_core.read_memory(haddr, size)\n+        return None\n+\n+    def add_vmcoreinfo(self):\n+        if not gdb.parse_and_eval(\"vmcoreinfo_find()\") \\\n+           or not gdb.parse_and_eval(\"vmcoreinfo_find()->has_vmcoreinfo\"):\n+            return\n+\n+        fmt = gdb.parse_and_eval(\"vmcoreinfo_find()->vmcoreinfo.guest_format\")\n+        addr = gdb.parse_and_eval(\"vmcoreinfo_find()->vmcoreinfo.paddr\")\n+        size = gdb.parse_and_eval(\"vmcoreinfo_find()->vmcoreinfo.size\")\n+\n+        fmt = le16_to_cpu(fmt)\n+        addr = le64_to_cpu(addr)\n+        size = le32_to_cpu(size)\n+\n+        if fmt != VMCOREINFO_FORMAT_ELF:\n+            return\n+\n+        vmcoreinfo = self.phys_memory_read(addr, size)\n+        if vmcoreinfo:\n+            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())\n+\n     def invoke(self, args, from_tty):\n         \"\"\"Handles command invocation from gdb.\"\"\"\n \n@@ -518,6 +578,7 @@ shape and this command should mostly work.\"\"\"\n \n         self.elf = ELF(argv[1])\n         self.guest_phys_blocks = get_guest_phys_blocks()\n+        self.add_vmcoreinfo()\n \n         with open(argv[0], \"wb\") as vmcore:\n             self.dump_init(vmcore)\n","prefixes":["v6","6/7"]}