From patchwork Fri Jan 22 12:08:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janosch Frank X-Patchwork-Id: 8089511 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 7CFC69F859 for ; Fri, 22 Jan 2016 12:15:13 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BFD45202DD for ; Fri, 22 Jan 2016 12:15:07 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A6758202B8 for ; Fri, 22 Jan 2016 12:15:06 +0000 (UTC) Received: from localhost ([::1]:52959 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMacL-0005mA-VH for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Jan 2016 07:15:05 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53267) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMaYv-0000bn-CR for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:11:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aMaYk-0007Oq-Nd for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:11:27 -0500 Received: from e06smtp17.uk.ibm.com ([195.75.94.113]:51637) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMaYk-0007OR-9r for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:11:22 -0500 Received: from localhost by e06smtp17.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 22 Jan 2016 12:11:19 -0000 Received: from d06dlp01.portsmouth.uk.ibm.com (9.149.20.13) by e06smtp17.uk.ibm.com (192.168.101.147) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 22 Jan 2016 12:11:18 -0000 X-IBM-Helo: d06dlp01.portsmouth.uk.ibm.com X-IBM-MailFrom: frankja@linux.vnet.ibm.com X-IBM-RcptTo: qemu-devel@nongnu.org Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id 2ACDB17D8056 for ; Fri, 22 Jan 2016 12:11:24 +0000 (GMT) Received: from d06av02.portsmouth.uk.ibm.com (d06av02.portsmouth.uk.ibm.com [9.149.37.228]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u0MCBHRL4981030 for ; Fri, 22 Jan 2016 12:11:17 GMT Received: from d06av02.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av02.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u0MCBHtH002110 for ; Fri, 22 Jan 2016 05:11:17 -0700 Received: from s38lp20.boeblingen.de.ibm.com (dyn-9-152-224-160.boeblingen.de.ibm.com [9.152.224.160]) by d06av02.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u0MCBGUF002067; Fri, 22 Jan 2016 05:11:17 -0700 From: Janosch Frank To: qemu-devel@nongnu.org Date: Fri, 22 Jan 2016 13:08:36 +0100 Message-Id: <1453464520-3882-3-git-send-email-frankja@linux.vnet.ibm.com> X-Mailer: git-send-email 2.3.0 In-Reply-To: <1453464520-3882-1-git-send-email-frankja@linux.vnet.ibm.com> References: <1453464520-3882-1-git-send-email-frankja@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16012212-0005-0000-0000-00000A173C2D X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 195.75.94.113 Cc: pbonzini@redhat.com, drjones@redhat.com, lersek@redhat.com, frankja@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 2/6] scripts/dump-guest-memory.py: Make methods functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The functions dealing with qemu components rarely used parts of the class, so they were moved out of the class. As the uintptr_t variable is needed both within and outside the class, it was made a constant and moved to the top. Reviewed-by: Laszlo Ersek Signed-off-by: Janosch Frank --- scripts/dump-guest-memory.py | 184 ++++++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 91 deletions(-) diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py index e49c835..d0b927a 100644 --- a/scripts/dump-guest-memory.py +++ b/scripts/dump-guest-memory.py @@ -17,6 +17,8 @@ import struct +UINTPTR_T = gdb.lookup_type("uintptr_t") + TARGET_PAGE_SIZE = 0x1000 TARGET_PAGE_MASK = 0xFFFFFFFFFFFFF000 @@ -66,6 +68,94 @@ ELF64_PHDR = ("I" # p_type "Q" # p_align ) +def int128_get64(val): + assert (val["hi"] == 0) + return val["lo"] + +def qlist_foreach(head, field_str): + var_p = head["lh_first"] + while (var_p != 0): + var = var_p.dereference() + yield var + var_p = var[field_str]["le_next"] + +def qemu_get_ram_block(ram_addr): + ram_blocks = gdb.parse_and_eval("ram_list.blocks") + for block in qlist_foreach(ram_blocks, "next"): + if (ram_addr - block["offset"] < block["used_length"]): + return block + raise gdb.GdbError("Bad ram offset %x" % ram_addr) + +def qemu_get_ram_ptr(ram_addr): + block = qemu_get_ram_block(ram_addr) + return block["host"] + (ram_addr - block["offset"]) + +def memory_region_get_ram_ptr(mr): + if (mr["alias"] != 0): + return (memory_region_get_ram_ptr(mr["alias"].dereference()) + + mr["alias_offset"]) + return qemu_get_ram_ptr(mr["ram_addr"] & TARGET_PAGE_MASK) + +def get_guest_phys_blocks(): + guest_phys_blocks = [] + print "guest RAM blocks:" + print ("target_start target_end host_addr message " + "count") + print ("---------------- ---------------- ---------------- ------- " + "-----") + + current_map_p = gdb.parse_and_eval("address_space_memory.current_map") + current_map = current_map_p.dereference() + for cur in range(current_map["nr"]): + flat_range = (current_map["ranges"] + cur).dereference() + mr = flat_range["mr"].dereference() + + # we only care about RAM + if (not mr["ram"]): + continue + + section_size = int128_get64(flat_range["addr"]["size"]) + target_start = int128_get64(flat_range["addr"]["start"]) + target_end = target_start + section_size + host_addr = (memory_region_get_ram_ptr(mr) + + flat_range["offset_in_region"]) + predecessor = None + + # find continuity in guest physical address space + if (len(guest_phys_blocks) > 0): + predecessor = guest_phys_blocks[-1] + predecessor_size = (predecessor["target_end"] - + predecessor["target_start"]) + + # the memory API guarantees monotonically increasing + # traversal + assert (predecessor["target_end"] <= target_start) + + # we want continuity in both guest-physical and + # host-virtual memory + if (predecessor["target_end"] < target_start or + predecessor["host_addr"] + predecessor_size != host_addr): + predecessor = None + + if (predecessor is None): + # isolated mapping, add it to the list + guest_phys_blocks.append({"target_start": target_start, + "target_end" : target_end, + "host_addr" : host_addr}) + message = "added" + else: + # expand predecessor until @target_end; predecessor's + # start doesn't change + predecessor["target_end"] = target_end + message = "joined" + + print ("%016x %016x %016x %-7s %5u" % + (target_start, target_end, host_addr.cast(UINTPTR_T), + message, len(guest_phys_blocks))) + + return guest_phys_blocks + + class DumpGuestMemory(gdb.Command): """Extract guest vmcore from qemu process coredump. @@ -100,96 +190,9 @@ shape and this command should mostly work.""" super(DumpGuestMemory, self).__init__("dump-guest-memory", gdb.COMMAND_DATA, gdb.COMPLETE_FILENAME) - self.uintptr_t = gdb.lookup_type("uintptr_t") self.elf64_ehdr_le = struct.Struct("<%s" % ELF64_EHDR) self.elf64_phdr_le = struct.Struct("<%s" % ELF64_PHDR) - - def int128_get64(self, val): - assert (val["hi"] == 0) - return val["lo"] - - def qlist_foreach(self, head, field_str): - var_p = head["lh_first"] - while (var_p != 0): - var = var_p.dereference() - yield var - var_p = var[field_str]["le_next"] - - def qemu_get_ram_block(self, ram_addr): - ram_blocks = gdb.parse_and_eval("ram_list.blocks") - for block in self.qlist_foreach(ram_blocks, "next"): - if (ram_addr - block["offset"] < block["used_length"]): - return block - raise gdb.GdbError("Bad ram offset %x" % ram_addr) - - def qemu_get_ram_ptr(self, ram_addr): - block = self.qemu_get_ram_block(ram_addr) - return block["host"] + (ram_addr - block["offset"]) - - def memory_region_get_ram_ptr(self, mr): - if (mr["alias"] != 0): - return (self.memory_region_get_ram_ptr(mr["alias"].dereference()) + - mr["alias_offset"]) - return self.qemu_get_ram_ptr(mr["ram_addr"] & TARGET_PAGE_MASK) - - def guest_phys_blocks_init(self): - self.guest_phys_blocks = [] - - def guest_phys_blocks_append(self): - print "guest RAM blocks:" - print ("target_start target_end host_addr message " - "count") - print ("---------------- ---------------- ---------------- ------- " - "-----") - - current_map_p = gdb.parse_and_eval("address_space_memory.current_map") - current_map = current_map_p.dereference() - for cur in range(current_map["nr"]): - flat_range = (current_map["ranges"] + cur).dereference() - mr = flat_range["mr"].dereference() - - # we only care about RAM - if (not mr["ram"]): - continue - - section_size = self.int128_get64(flat_range["addr"]["size"]) - target_start = self.int128_get64(flat_range["addr"]["start"]) - target_end = target_start + section_size - host_addr = (self.memory_region_get_ram_ptr(mr) + - flat_range["offset_in_region"]) - predecessor = None - - # find continuity in guest physical address space - if (len(self.guest_phys_blocks) > 0): - predecessor = self.guest_phys_blocks[-1] - predecessor_size = (predecessor["target_end"] - - predecessor["target_start"]) - - # the memory API guarantees monotonically increasing - # traversal - assert (predecessor["target_end"] <= target_start) - - # we want continuity in both guest-physical and - # host-virtual memory - if (predecessor["target_end"] < target_start or - predecessor["host_addr"] + predecessor_size != host_addr): - predecessor = None - - if (predecessor is None): - # isolated mapping, add it to the list - self.guest_phys_blocks.append({"target_start": target_start, - "target_end" : target_end, - "host_addr" : host_addr}) - message = "added" - else: - # expand predecessor until @target_end; predecessor's - # start doesn't change - predecessor["target_end"] = target_end - message = "joined" - - print ("%016x %016x %016x %-7s %5u" % - (target_start, target_end, host_addr.cast(self.uintptr_t), - message, len(self.guest_phys_blocks))) + self.guest_phys_blocks = None def cpu_get_dump_info(self): # We can't synchronize the registers with KVM post-mortem, and @@ -263,8 +266,7 @@ shape and this command should mostly work.""" len(name) + 1, len(desc), type, name, desc) def dump_init(self): - self.guest_phys_blocks_init() - self.guest_phys_blocks_append() + self.guest_phys_blocks = get_guest_phys_blocks() self.cpu_get_dump_info() # we have no way to retrieve the VCPU status from KVM # post-mortem @@ -310,7 +312,7 @@ shape and this command should mostly work.""" cur = block["host_addr"] left = block["target_end"] - block["target_start"] print ("dumping range at %016x for length %016x" % - (cur.cast(self.uintptr_t), left)) + (cur.cast(UINTPTR_T), left)) while (left > 0): chunk_size = min(TARGET_PAGE_SIZE, left) chunk = qemu_core.read_memory(cur, chunk_size)