From patchwork Fri Jan 22 12:08:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janosch Frank X-Patchwork-Id: 8089361 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 4E148BEEE5 for ; Fri, 22 Jan 2016 12:11:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8265C20457 for ; Fri, 22 Jan 2016 12:11:58 +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 7A38E2042A for ; Fri, 22 Jan 2016 12:11:57 +0000 (UTC) Received: from localhost ([::1]:52931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMaZI-0000eD-QM for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Jan 2016 07:11:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53266) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMaYv-0000bm-CU 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 1aMaYl-0007PE-63 for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:11:27 -0500 Received: from e06smtp17.uk.ibm.com ([195.75.94.113]:51641) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMaYk-0007OS-PN for qemu-devel@nongnu.org; Fri, 22 Jan 2016 07:11:23 -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:20 -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 9E7D117D8063 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 u0MCBIsP4981034 for ; Fri, 22 Jan 2016 12:11:18 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 u0MCBHwj002135 for ; Fri, 22 Jan 2016 05:11:18 -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 u0MCBGUH002067; Fri, 22 Jan 2016 05:11:17 -0700 From: Janosch Frank To: qemu-devel@nongnu.org Date: Fri, 22 Jan 2016 13:08:38 +0100 Message-Id: <1453464520-3882-5-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-00000A173C2E 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 4/6] scripts/dump-guest-memory.py: Cleanup 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 Increase readability by adding newlines and comments, as well as removing wrong whitespaces and C style braces around conditionals and loops. Reviewed-by: Laszlo Ersek Signed-off-by: Janosch Frank --- scripts/dump-guest-memory.py | 75 +++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py index bb4ca8e..2cf7365 100644 --- a/scripts/dump-guest-memory.py +++ b/scripts/dump-guest-memory.py @@ -69,35 +69,60 @@ ELF64_PHDR = ("I" # p_type ) def int128_get64(val): - assert (val["hi"] == 0) + """Returns low 64bit part of Int128 struct.""" + + assert val["hi"] == 0 return val["lo"] + def qlist_foreach(head, field_str): + """Generator for qlists.""" + var_p = head["lh_first"] - while (var_p != 0): + while var_p != 0: var = var_p.dereference() - yield var var_p = var[field_str]["le_next"] + yield var + def qemu_get_ram_block(ram_addr): + """Returns the RAMBlock struct to which the given address belongs.""" + 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"]): + 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): + """Returns qemu vaddr for given guest physical address.""" + 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 memory_region_get_ram_ptr(memory_region): + if memory_region["alias"] != 0: + return (memory_region_get_ram_ptr(memory_region["alias"].dereference()) + + memory_region["alias_offset"]) + + return qemu_get_ram_ptr(memory_region["ram_addr"] & TARGET_PAGE_MASK) + def get_guest_phys_blocks(): + """Returns a list of ram blocks. + + Each block entry contains: + 'target_start': guest block phys start address + 'target_end': guest block phys end address + 'host_addr': qemu vaddr of the block's start + """ + guest_phys_blocks = [] + print("guest RAM blocks:") print("target_start target_end host_addr message " "count") @@ -111,29 +136,29 @@ def get_guest_phys_blocks(): # compatibility. Otherwise range doesn't cast the value itself and # breaks. for cur in range(int(current_map["nr"])): - flat_range = (current_map["ranges"] + cur).dereference() - mr = flat_range["mr"].dereference() + flat_range = (current_map["ranges"] + cur).dereference() + memory_region = flat_range["mr"].dereference() # we only care about RAM - if (not mr["ram"]): + if not memory_region["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"]) + target_end = target_start + section_size + host_addr = (memory_region_get_ram_ptr(memory_region) + + flat_range["offset_in_region"]) predecessor = None # find continuity in guest physical address space - if (len(guest_phys_blocks) > 0): + 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) + assert predecessor["target_end"] <= target_start # we want continuity in both guest-physical and # host-virtual memory @@ -141,11 +166,11 @@ def get_guest_phys_blocks(): predecessor["host_addr"] + predecessor_size != host_addr): predecessor = None - if (predecessor is 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}) + "target_end": target_end, + "host_addr": host_addr}) message = "added" else: # expand predecessor until @target_end; predecessor's @@ -282,7 +307,7 @@ shape and this command should mostly work.""" # We should never reach PN_XNUM for paging=false dumps: there's # just a handful of discontiguous ranges after merging. self.phdr_num += len(self.guest_phys_blocks) - assert (self.phdr_num < PN_XNUM) + assert self.phdr_num < PN_XNUM # Calculate the ELF file offset where the memory dump commences: # @@ -313,15 +338,15 @@ shape and this command should mostly work.""" def dump_iterate(self, vmcore): qemu_core = gdb.inferiors()[0] for block in self.guest_phys_blocks: - cur = block["host_addr"] + cur = block["host_addr"] left = block["target_end"] - block["target_start"] print("dumping range at %016x for length %016x" % (cur.cast(UINTPTR_T), left)) - while (left > 0): + while left > 0: chunk_size = min(TARGET_PAGE_SIZE, left) chunk = qemu_core.read_memory(cur, chunk_size) vmcore.write(chunk) - cur += chunk_size + cur += chunk_size left -= chunk_size def create_vmcore(self, filename): @@ -336,7 +361,7 @@ shape and this command should mostly work.""" self.dont_repeat() argv = gdb.string_to_argv(args) - if (len(argv) != 1): + if len(argv) != 1: raise gdb.GdbError("usage: dump-guest-memory FILE") self.dump_init()