mirror of https://github.com/xqemu/xqemu.git
scripts/dump-guest-memory.py: Cleanup functions
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 <lersek@redhat.com> Signed-off-by: Janosch Frank <frankja@linux.vnet.ibm.com> Message-Id: <1453464520-3882-5-git-send-email-frankja@linux.vnet.ibm.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
7cb1089d5f
commit
6782c0e785
|
@ -69,35 +69,60 @@ ELF64_PHDR = ("I" # p_type
|
||||||
)
|
)
|
||||||
|
|
||||||
def int128_get64(val):
|
def int128_get64(val):
|
||||||
assert (val["hi"] == 0)
|
"""Returns low 64bit part of Int128 struct."""
|
||||||
|
|
||||||
|
assert val["hi"] == 0
|
||||||
return val["lo"]
|
return val["lo"]
|
||||||
|
|
||||||
|
|
||||||
def qlist_foreach(head, field_str):
|
def qlist_foreach(head, field_str):
|
||||||
|
"""Generator for qlists."""
|
||||||
|
|
||||||
var_p = head["lh_first"]
|
var_p = head["lh_first"]
|
||||||
while (var_p != 0):
|
while var_p != 0:
|
||||||
var = var_p.dereference()
|
var = var_p.dereference()
|
||||||
yield var
|
|
||||||
var_p = var[field_str]["le_next"]
|
var_p = var[field_str]["le_next"]
|
||||||
|
yield var
|
||||||
|
|
||||||
|
|
||||||
def qemu_get_ram_block(ram_addr):
|
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")
|
ram_blocks = gdb.parse_and_eval("ram_list.blocks")
|
||||||
|
|
||||||
for block in qlist_foreach(ram_blocks, "next"):
|
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
|
return block
|
||||||
|
|
||||||
raise gdb.GdbError("Bad ram offset %x" % ram_addr)
|
raise gdb.GdbError("Bad ram offset %x" % ram_addr)
|
||||||
|
|
||||||
|
|
||||||
def qemu_get_ram_ptr(ram_addr):
|
def qemu_get_ram_ptr(ram_addr):
|
||||||
|
"""Returns qemu vaddr for given guest physical address."""
|
||||||
|
|
||||||
block = qemu_get_ram_block(ram_addr)
|
block = qemu_get_ram_block(ram_addr)
|
||||||
return block["host"] + (ram_addr - block["offset"])
|
return block["host"] + (ram_addr - block["offset"])
|
||||||
|
|
||||||
def memory_region_get_ram_ptr(mr):
|
|
||||||
if (mr["alias"] != 0):
|
def memory_region_get_ram_ptr(memory_region):
|
||||||
return (memory_region_get_ram_ptr(mr["alias"].dereference()) +
|
if memory_region["alias"] != 0:
|
||||||
mr["alias_offset"])
|
return (memory_region_get_ram_ptr(memory_region["alias"].dereference())
|
||||||
return qemu_get_ram_ptr(mr["ram_addr"] & TARGET_PAGE_MASK)
|
+ memory_region["alias_offset"])
|
||||||
|
|
||||||
|
return qemu_get_ram_ptr(memory_region["ram_addr"] & TARGET_PAGE_MASK)
|
||||||
|
|
||||||
|
|
||||||
def get_guest_phys_blocks():
|
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 = []
|
guest_phys_blocks = []
|
||||||
|
|
||||||
print("guest RAM blocks:")
|
print("guest RAM blocks:")
|
||||||
print("target_start target_end host_addr message "
|
print("target_start target_end host_addr message "
|
||||||
"count")
|
"count")
|
||||||
|
@ -111,29 +136,29 @@ def get_guest_phys_blocks():
|
||||||
# compatibility. Otherwise range doesn't cast the value itself and
|
# compatibility. Otherwise range doesn't cast the value itself and
|
||||||
# breaks.
|
# breaks.
|
||||||
for cur in range(int(current_map["nr"])):
|
for cur in range(int(current_map["nr"])):
|
||||||
flat_range = (current_map["ranges"] + cur).dereference()
|
flat_range = (current_map["ranges"] + cur).dereference()
|
||||||
mr = flat_range["mr"].dereference()
|
memory_region = flat_range["mr"].dereference()
|
||||||
|
|
||||||
# we only care about RAM
|
# we only care about RAM
|
||||||
if (not mr["ram"]):
|
if not memory_region["ram"]:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
section_size = int128_get64(flat_range["addr"]["size"])
|
section_size = int128_get64(flat_range["addr"]["size"])
|
||||||
target_start = int128_get64(flat_range["addr"]["start"])
|
target_start = int128_get64(flat_range["addr"]["start"])
|
||||||
target_end = target_start + section_size
|
target_end = target_start + section_size
|
||||||
host_addr = (memory_region_get_ram_ptr(mr) +
|
host_addr = (memory_region_get_ram_ptr(memory_region)
|
||||||
flat_range["offset_in_region"])
|
+ flat_range["offset_in_region"])
|
||||||
predecessor = None
|
predecessor = None
|
||||||
|
|
||||||
# find continuity in guest physical address space
|
# 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 = guest_phys_blocks[-1]
|
||||||
predecessor_size = (predecessor["target_end"] -
|
predecessor_size = (predecessor["target_end"] -
|
||||||
predecessor["target_start"])
|
predecessor["target_start"])
|
||||||
|
|
||||||
# the memory API guarantees monotonically increasing
|
# the memory API guarantees monotonically increasing
|
||||||
# traversal
|
# traversal
|
||||||
assert (predecessor["target_end"] <= target_start)
|
assert predecessor["target_end"] <= target_start
|
||||||
|
|
||||||
# we want continuity in both guest-physical and
|
# we want continuity in both guest-physical and
|
||||||
# host-virtual memory
|
# host-virtual memory
|
||||||
|
@ -141,11 +166,11 @@ def get_guest_phys_blocks():
|
||||||
predecessor["host_addr"] + predecessor_size != host_addr):
|
predecessor["host_addr"] + predecessor_size != host_addr):
|
||||||
predecessor = None
|
predecessor = None
|
||||||
|
|
||||||
if (predecessor is None):
|
if predecessor is None:
|
||||||
# isolated mapping, add it to the list
|
# isolated mapping, add it to the list
|
||||||
guest_phys_blocks.append({"target_start": target_start,
|
guest_phys_blocks.append({"target_start": target_start,
|
||||||
"target_end" : target_end,
|
"target_end": target_end,
|
||||||
"host_addr" : host_addr})
|
"host_addr": host_addr})
|
||||||
message = "added"
|
message = "added"
|
||||||
else:
|
else:
|
||||||
# expand predecessor until @target_end; predecessor's
|
# 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
|
# We should never reach PN_XNUM for paging=false dumps: there's
|
||||||
# just a handful of discontiguous ranges after merging.
|
# just a handful of discontiguous ranges after merging.
|
||||||
self.phdr_num += len(self.guest_phys_blocks)
|
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:
|
# 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):
|
def dump_iterate(self, vmcore):
|
||||||
qemu_core = gdb.inferiors()[0]
|
qemu_core = gdb.inferiors()[0]
|
||||||
for block in self.guest_phys_blocks:
|
for block in self.guest_phys_blocks:
|
||||||
cur = block["host_addr"]
|
cur = block["host_addr"]
|
||||||
left = block["target_end"] - block["target_start"]
|
left = block["target_end"] - block["target_start"]
|
||||||
print("dumping range at %016x for length %016x" %
|
print("dumping range at %016x for length %016x" %
|
||||||
(cur.cast(UINTPTR_T), left))
|
(cur.cast(UINTPTR_T), left))
|
||||||
while (left > 0):
|
while left > 0:
|
||||||
chunk_size = min(TARGET_PAGE_SIZE, left)
|
chunk_size = min(TARGET_PAGE_SIZE, left)
|
||||||
chunk = qemu_core.read_memory(cur, chunk_size)
|
chunk = qemu_core.read_memory(cur, chunk_size)
|
||||||
vmcore.write(chunk)
|
vmcore.write(chunk)
|
||||||
cur += chunk_size
|
cur += chunk_size
|
||||||
left -= chunk_size
|
left -= chunk_size
|
||||||
|
|
||||||
def create_vmcore(self, filename):
|
def create_vmcore(self, filename):
|
||||||
|
@ -336,7 +361,7 @@ shape and this command should mostly work."""
|
||||||
self.dont_repeat()
|
self.dont_repeat()
|
||||||
|
|
||||||
argv = gdb.string_to_argv(args)
|
argv = gdb.string_to_argv(args)
|
||||||
if (len(argv) != 1):
|
if len(argv) != 1:
|
||||||
raise gdb.GdbError("usage: dump-guest-memory FILE")
|
raise gdb.GdbError("usage: dump-guest-memory FILE")
|
||||||
|
|
||||||
self.dump_init()
|
self.dump_init()
|
||||||
|
|
Loading…
Reference in New Issue