1#!/usr/bin/python 2 3# GDB debugging support 4# 5# Copyright 2012 Red Hat, Inc. and/or its affiliates 6# 7# Authors: 8# Avi Kivity <avi@redhat.com> 9# 10# This work is licensed under the terms of the GNU GPL, version 2 or 11# later. See the COPYING file in the top-level directory. 12 13# 'qemu mtree' -- display the memory hierarchy 14 15import gdb 16 17def isnull(ptr): 18 return ptr == gdb.Value(0).cast(ptr.type) 19 20def int128(p): 21 '''Read an Int128 type to a python integer. 22 23 QEMU can be built with native Int128 support so we need to detect 24 if the value is a structure or the native type. 25 ''' 26 if p.type.code == gdb.TYPE_CODE_STRUCT: 27 return int(p['lo']) + (int(p['hi']) << 64) 28 else: 29 return int(("%s" % p), 16) 30 31class MtreeCommand(gdb.Command): 32 '''Display the memory tree hierarchy''' 33 def __init__(self): 34 gdb.Command.__init__(self, 'qemu mtree', gdb.COMMAND_DATA, 35 gdb.COMPLETE_NONE) 36 self.queue = [] 37 def invoke(self, arg, from_tty): 38 self.seen = set() 39 self.queue_root('address_space_memory') 40 self.queue_root('address_space_io') 41 self.process_queue() 42 def queue_root(self, varname): 43 ptr = gdb.parse_and_eval(varname)['root'] 44 self.queue.append(ptr) 45 def process_queue(self): 46 while self.queue: 47 ptr = self.queue.pop(0) 48 if int(ptr) in self.seen: 49 continue 50 self.print_item(ptr) 51 def print_item(self, ptr, offset = gdb.Value(0), level = 0): 52 self.seen.add(int(ptr)) 53 addr = ptr['addr'] 54 addr += offset 55 size = int128(ptr['size']) 56 alias = ptr['alias'] 57 klass = '' 58 if not isnull(alias): 59 klass = ' (alias)' 60 elif not isnull(ptr['ops']): 61 klass = ' (I/O)' 62 elif bool(ptr['ram']): 63 klass = ' (RAM)' 64 gdb.write('%s%016x-%016x %s%s (@ %s)\n' 65 % (' ' * level, 66 int(addr), 67 int(addr + (size - 1)), 68 ptr['name'].string(), 69 klass, 70 ptr, 71 ), 72 gdb.STDOUT) 73 if not isnull(alias): 74 gdb.write('%s alias: %s@%016x (@ %s)\n' % 75 (' ' * level, 76 alias['name'].string(), 77 int(ptr['alias_offset']), 78 alias, 79 ), 80 gdb.STDOUT) 81 self.queue.append(alias) 82 subregion = ptr['subregions']['tqh_first'] 83 level += 1 84 while not isnull(subregion): 85 self.print_item(subregion, addr, level) 86 subregion = subregion['subregions_link']['tqe_next'] 87 88