2 This python script adds a new gdb command, "dump-guest-memory". It
3 should be loaded with "source dump-guest-memory.py" at the (gdb)
6 Copyright (C) 2013, Red Hat, Inc.
9 Laszlo Ersek <lersek@redhat.com>
10 Janosch Frank <frankja@linux.vnet.ibm.com>
12 This work is licensed under the terms of the GNU GPL, version 2 or later. See
13 the COPYING file in the top-level directory.
15 from __future__
import print_function
21 UINTPTR_T
= gdb
.lookup_type("uintptr_t")
22 except Exception as inst
:
23 raise gdb
.GdbError("Symbols must be loaded prior to sourcing dump-guest-memory.\n"
24 "Symbols may be loaded by 'attach'ing a QEMU process id or by "
25 "'load'ing a QEMU binary.")
27 TARGET_PAGE_SIZE
= 0x1000
28 TARGET_PAGE_MASK
= 0xFFFFFFFFFFFFF000
30 # Special value for e_phnum. This indicates that the real number of
31 # program headers is too large to fit into e_phnum. Instead the real
32 # value is in the field sh_info of section 0.
55 VMCOREINFO_FORMAT_ELF
= 1
58 return struct
.unpack("<H", struct
.pack("=H", val
))[0]
61 return struct
.unpack("<I", struct
.pack("=I", val
))[0]
64 return struct
.unpack("<Q", struct
.pack("=Q", val
))[0]
67 """Representation of a ELF file."""
69 def __init__(self
, arch
):
74 self
.endianness
= None
75 self
.elfclass
= ELFCLASS64
77 if arch
== 'aarch64-le':
78 self
.endianness
= ELFDATA2LSB
79 self
.elfclass
= ELFCLASS64
80 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
81 self
.ehdr
.e_machine
= EM_AARCH
83 elif arch
== 'aarch64-be':
84 self
.endianness
= ELFDATA2MSB
85 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
86 self
.ehdr
.e_machine
= EM_AARCH
88 elif arch
== 'X86_64':
89 self
.endianness
= ELFDATA2LSB
90 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
91 self
.ehdr
.e_machine
= EM_X86_64
94 self
.endianness
= ELFDATA2LSB
95 self
.elfclass
= ELFCLASS32
96 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
97 self
.ehdr
.e_machine
= EM_386
100 self
.endianness
= ELFDATA2MSB
101 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
102 self
.ehdr
.e_machine
= EM_S390
104 elif arch
== 'ppc64-le':
105 self
.endianness
= ELFDATA2LSB
106 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
107 self
.ehdr
.e_machine
= EM_PPC64
109 elif arch
== 'ppc64-be':
110 self
.endianness
= ELFDATA2MSB
111 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
112 self
.ehdr
.e_machine
= EM_PPC64
115 raise gdb
.GdbError("No valid arch type specified.\n"
116 "Currently supported types:\n"
117 "aarch64-be, aarch64-le, X86_64, 386, s390, "
118 "ppc64-be, ppc64-le")
120 self
.add_segment(PT_NOTE
, 0, 0)
122 def add_note(self
, n_name
, n_desc
, n_type
):
123 """Adds a note to the ELF."""
125 note
= get_arch_note(self
.endianness
, len(n_name
), len(n_desc
))
126 note
.n_namesz
= len(n_name
) + 1
127 note
.n_descsz
= len(n_desc
)
128 note
.n_name
= n_name
.encode()
131 # Desc needs to be 4 byte aligned (although the 64bit spec
132 # specifies 8 byte). When defining n_desc as uint32 it will be
133 # automatically aligned but we need the memmove to copy the
135 ctypes
.memmove(note
.n_desc
, n_desc
.encode(), len(n_desc
))
137 self
.notes
.append(note
)
138 self
.segments
[0].p_filesz
+= ctypes
.sizeof(note
)
139 self
.segments
[0].p_memsz
+= ctypes
.sizeof(note
)
142 def add_vmcoreinfo_note(self
, vmcoreinfo
):
143 """Adds a vmcoreinfo note to the ELF dump."""
144 # compute the header size, and copy that many bytes from the note
145 header
= get_arch_note(self
.endianness
, 0, 0)
146 ctypes
.memmove(ctypes
.pointer(header
),
147 vmcoreinfo
, ctypes
.sizeof(header
))
148 if header
.n_descsz
> 1 << 20:
149 print('warning: invalid vmcoreinfo size')
151 # now get the full note
152 note
= get_arch_note(self
.endianness
,
153 header
.n_namesz
- 1, header
.n_descsz
)
154 ctypes
.memmove(ctypes
.pointer(note
), vmcoreinfo
, ctypes
.sizeof(note
))
156 self
.notes
.append(note
)
157 self
.segments
[0].p_filesz
+= ctypes
.sizeof(note
)
158 self
.segments
[0].p_memsz
+= ctypes
.sizeof(note
)
160 def add_segment(self
, p_type
, p_paddr
, p_size
):
161 """Adds a segment to the elf."""
163 phdr
= get_arch_phdr(self
.endianness
, self
.elfclass
)
165 phdr
.p_paddr
= p_paddr
166 phdr
.p_filesz
= p_size
167 phdr
.p_memsz
= p_size
168 self
.segments
.append(phdr
)
169 self
.ehdr
.e_phnum
+= 1
171 def to_file(self
, elf_file
):
172 """Writes all ELF structures to the the passed file.
182 elf_file
.write(self
.ehdr
)
183 off
= ctypes
.sizeof(self
.ehdr
) + \
184 len(self
.segments
) * ctypes
.sizeof(self
.segments
[0])
186 for phdr
in self
.segments
:
191 for note
in self
.notes
:
195 def get_arch_note(endianness
, len_name
, len_desc
):
196 """Returns a Note class with the specified endianness."""
198 if endianness
== ELFDATA2LSB
:
199 superclass
= ctypes
.LittleEndianStructure
201 superclass
= ctypes
.BigEndianStructure
203 len_name
= len_name
+ 1
205 class Note(superclass
):
206 """Represents an ELF note, includes the content."""
208 _fields_
= [("n_namesz", ctypes
.c_uint32
),
209 ("n_descsz", ctypes
.c_uint32
),
210 ("n_type", ctypes
.c_uint32
),
211 ("n_name", ctypes
.c_char
* len_name
),
212 ("n_desc", ctypes
.c_uint32
* ((len_desc
+ 3) // 4))]
216 class Ident(ctypes
.Structure
):
217 """Represents the ELF ident array in the ehdr structure."""
219 _fields_
= [('ei_mag0', ctypes
.c_ubyte
),
220 ('ei_mag1', ctypes
.c_ubyte
),
221 ('ei_mag2', ctypes
.c_ubyte
),
222 ('ei_mag3', ctypes
.c_ubyte
),
223 ('ei_class', ctypes
.c_ubyte
),
224 ('ei_data', ctypes
.c_ubyte
),
225 ('ei_version', ctypes
.c_ubyte
),
226 ('ei_osabi', ctypes
.c_ubyte
),
227 ('ei_abiversion', ctypes
.c_ubyte
),
228 ('ei_pad', ctypes
.c_ubyte
* 7)]
230 def __init__(self
, endianness
, elfclass
):
232 self
.ei_mag1
= ord('E')
233 self
.ei_mag2
= ord('L')
234 self
.ei_mag3
= ord('F')
235 self
.ei_class
= elfclass
236 self
.ei_data
= endianness
237 self
.ei_version
= EV_CURRENT
240 def get_arch_ehdr(endianness
, elfclass
):
241 """Returns a EHDR64 class with the specified endianness."""
243 if endianness
== ELFDATA2LSB
:
244 superclass
= ctypes
.LittleEndianStructure
246 superclass
= ctypes
.BigEndianStructure
248 class EHDR64(superclass
):
249 """Represents the 64 bit ELF header struct."""
251 _fields_
= [('e_ident', Ident
),
252 ('e_type', ctypes
.c_uint16
),
253 ('e_machine', ctypes
.c_uint16
),
254 ('e_version', ctypes
.c_uint32
),
255 ('e_entry', ctypes
.c_uint64
),
256 ('e_phoff', ctypes
.c_uint64
),
257 ('e_shoff', ctypes
.c_uint64
),
258 ('e_flags', ctypes
.c_uint32
),
259 ('e_ehsize', ctypes
.c_uint16
),
260 ('e_phentsize', ctypes
.c_uint16
),
261 ('e_phnum', ctypes
.c_uint16
),
262 ('e_shentsize', ctypes
.c_uint16
),
263 ('e_shnum', ctypes
.c_uint16
),
264 ('e_shstrndx', ctypes
.c_uint16
)]
267 super(superclass
, self
).__init
__()
268 self
.e_ident
= Ident(endianness
, elfclass
)
269 self
.e_type
= ET_CORE
270 self
.e_version
= EV_CURRENT
271 self
.e_ehsize
= ctypes
.sizeof(self
)
272 self
.e_phoff
= ctypes
.sizeof(self
)
273 self
.e_phentsize
= ctypes
.sizeof(get_arch_phdr(endianness
, elfclass
))
277 class EHDR32(superclass
):
278 """Represents the 32 bit ELF header struct."""
280 _fields_
= [('e_ident', Ident
),
281 ('e_type', ctypes
.c_uint16
),
282 ('e_machine', ctypes
.c_uint16
),
283 ('e_version', ctypes
.c_uint32
),
284 ('e_entry', ctypes
.c_uint32
),
285 ('e_phoff', ctypes
.c_uint32
),
286 ('e_shoff', ctypes
.c_uint32
),
287 ('e_flags', ctypes
.c_uint32
),
288 ('e_ehsize', ctypes
.c_uint16
),
289 ('e_phentsize', ctypes
.c_uint16
),
290 ('e_phnum', ctypes
.c_uint16
),
291 ('e_shentsize', ctypes
.c_uint16
),
292 ('e_shnum', ctypes
.c_uint16
),
293 ('e_shstrndx', ctypes
.c_uint16
)]
296 super(superclass
, self
).__init
__()
297 self
.e_ident
= Ident(endianness
, elfclass
)
298 self
.e_type
= ET_CORE
299 self
.e_version
= EV_CURRENT
300 self
.e_ehsize
= ctypes
.sizeof(self
)
301 self
.e_phoff
= ctypes
.sizeof(self
)
302 self
.e_phentsize
= ctypes
.sizeof(get_arch_phdr(endianness
, elfclass
))
306 if elfclass
== ELFCLASS64
:
312 def get_arch_phdr(endianness
, elfclass
):
313 """Returns a 32 or 64 bit PHDR class with the specified endianness."""
315 if endianness
== ELFDATA2LSB
:
316 superclass
= ctypes
.LittleEndianStructure
318 superclass
= ctypes
.BigEndianStructure
320 class PHDR64(superclass
):
321 """Represents the 64 bit ELF program header struct."""
323 _fields_
= [('p_type', ctypes
.c_uint32
),
324 ('p_flags', ctypes
.c_uint32
),
325 ('p_offset', ctypes
.c_uint64
),
326 ('p_vaddr', ctypes
.c_uint64
),
327 ('p_paddr', ctypes
.c_uint64
),
328 ('p_filesz', ctypes
.c_uint64
),
329 ('p_memsz', ctypes
.c_uint64
),
330 ('p_align', ctypes
.c_uint64
)]
332 class PHDR32(superclass
):
333 """Represents the 32 bit ELF program header struct."""
335 _fields_
= [('p_type', ctypes
.c_uint32
),
336 ('p_offset', ctypes
.c_uint32
),
337 ('p_vaddr', ctypes
.c_uint32
),
338 ('p_paddr', ctypes
.c_uint32
),
339 ('p_filesz', ctypes
.c_uint32
),
340 ('p_memsz', ctypes
.c_uint32
),
341 ('p_flags', ctypes
.c_uint32
),
342 ('p_align', ctypes
.c_uint32
)]
345 if elfclass
== ELFCLASS64
:
351 def int128_get64(val
):
352 """Returns low 64bit part of Int128 struct."""
355 assert val
["hi"] == 0
358 u64t
= gdb
.lookup_type('uint64_t').array(2)
360 if sys
.byteorder
== 'little':
368 def qlist_foreach(head
, field_str
):
369 """Generator for qlists."""
371 var_p
= head
["lh_first"]
373 var
= var_p
.dereference()
374 var_p
= var
[field_str
]["le_next"]
378 def qemu_map_ram_ptr(block
, offset
):
379 """Returns qemu vaddr for given guest physical address."""
381 return block
["host"] + offset
384 def memory_region_get_ram_ptr(memory_region
):
385 if memory_region
["alias"] != 0:
386 return (memory_region_get_ram_ptr(memory_region
["alias"].dereference())
387 + memory_region
["alias_offset"])
389 return qemu_map_ram_ptr(memory_region
["ram_block"], 0)
392 def get_guest_phys_blocks():
393 """Returns a list of ram blocks.
395 Each block entry contains:
396 'target_start': guest block phys start address
397 'target_end': guest block phys end address
398 'host_addr': qemu vaddr of the block's start
401 guest_phys_blocks
= []
403 print("guest RAM blocks:")
404 print("target_start target_end host_addr message "
406 print("---------------- ---------------- ---------------- ------- "
409 current_map_p
= gdb
.parse_and_eval("address_space_memory.current_map")
410 current_map
= current_map_p
.dereference()
412 # Conversion to int is needed for python 3
413 # compatibility. Otherwise range doesn't cast the value itself and
415 for cur
in range(int(current_map
["nr"])):
416 flat_range
= (current_map
["ranges"] + cur
).dereference()
417 memory_region
= flat_range
["mr"].dereference()
419 # we only care about RAM
420 if not memory_region
["ram"]:
423 section_size
= int128_get64(flat_range
["addr"]["size"])
424 target_start
= int128_get64(flat_range
["addr"]["start"])
425 target_end
= target_start
+ section_size
426 host_addr
= (memory_region_get_ram_ptr(memory_region
)
427 + flat_range
["offset_in_region"])
430 # find continuity in guest physical address space
431 if len(guest_phys_blocks
) > 0:
432 predecessor
= guest_phys_blocks
[-1]
433 predecessor_size
= (predecessor
["target_end"] -
434 predecessor
["target_start"])
436 # the memory API guarantees monotonically increasing
438 assert predecessor
["target_end"] <= target_start
440 # we want continuity in both guest-physical and
441 # host-virtual memory
442 if (predecessor
["target_end"] < target_start
or
443 predecessor
["host_addr"] + predecessor_size
!= host_addr
):
446 if predecessor
is None:
447 # isolated mapping, add it to the list
448 guest_phys_blocks
.append({"target_start": target_start
,
449 "target_end": target_end
,
450 "host_addr": host_addr
})
453 # expand predecessor until @target_end; predecessor's
454 # start doesn't change
455 predecessor
["target_end"] = target_end
458 print("%016x %016x %016x %-7s %5u" %
459 (target_start
, target_end
, host_addr
.cast(UINTPTR_T
),
460 message
, len(guest_phys_blocks
)))
462 return guest_phys_blocks
465 # The leading docstring doesn't have idiomatic Python formatting. It is
466 # printed by gdb's "help" command (the first line is printed in the
467 # "help data" summary), and it should match how other help texts look in
469 class DumpGuestMemory(gdb
.Command
):
470 """Extract guest vmcore from qemu process coredump.
472 The two required arguments are FILE and ARCH:
473 FILE identifies the target file to write the guest vmcore to.
474 ARCH specifies the architecture for which the core will be generated.
476 This GDB command reimplements the dump-guest-memory QMP command in
477 python, using the representation of guest memory as captured in the qemu
478 coredump. The qemu process that has been dumped must have had the
479 command line option "-machine dump-guest-core=on" which is the default.
481 For simplicity, the "paging", "begin" and "end" parameters of the QMP
482 command are not supported -- no attempt is made to get the guest's
483 internal paging structures (ie. paging=false is hard-wired), and guest
484 memory is always fully dumped.
486 Currently aarch64-be, aarch64-le, X86_64, 386, s390, ppc64-be,
487 ppc64-le guests are supported.
489 The CORE/NT_PRSTATUS and QEMU notes (that is, the VCPUs' statuses) are
490 not written to the vmcore. Preparing these would require context that is
491 only present in the KVM host kernel module when the guest is alive. A
492 fake ELF note is written instead, only to keep the ELF parser of "crash"
495 Dependent on how busted the qemu process was at the time of the
496 coredump, this command might produce unpredictable results. If qemu
497 deliberately called abort(), or it was dumped in response to a signal at
498 a halfway fortunate point, then its coredump should be in reasonable
499 shape and this command should mostly work."""
502 super(DumpGuestMemory
, self
).__init
__("dump-guest-memory",
504 gdb
.COMPLETE_FILENAME
)
506 self
.guest_phys_blocks
= None
508 def dump_init(self
, vmcore
):
509 """Prepares and writes ELF structures to core file."""
511 # Needed to make crash happy, data for more useful notes is
512 # not available in a qemu core.
513 self
.elf
.add_note("NONE", "EMPTY", 0)
515 # We should never reach PN_XNUM for paging=false dumps,
516 # there's just a handful of discontiguous ranges after
518 # The constant is needed to account for the PT_NOTE segment.
519 phdr_num
= len(self
.guest_phys_blocks
) + 1
520 assert phdr_num
< PN_XNUM
522 for block
in self
.guest_phys_blocks
:
523 block_size
= block
["target_end"] - block
["target_start"]
524 self
.elf
.add_segment(PT_LOAD
, block
["target_start"], block_size
)
526 self
.elf
.to_file(vmcore
)
528 def dump_iterate(self
, vmcore
):
529 """Writes guest core to file."""
531 qemu_core
= gdb
.inferiors()[0]
532 for block
in self
.guest_phys_blocks
:
533 cur
= block
["host_addr"]
534 left
= block
["target_end"] - block
["target_start"]
535 print("dumping range at %016x for length %016x" %
536 (cur
.cast(UINTPTR_T
), left
))
539 chunk_size
= min(TARGET_PAGE_SIZE
, left
)
540 chunk
= qemu_core
.read_memory(cur
, chunk_size
)
545 def phys_memory_read(self
, addr
, size
):
546 qemu_core
= gdb
.inferiors()[0]
547 for block
in self
.guest_phys_blocks
:
548 if block
["target_start"] <= addr \
549 and addr
+ size
<= block
["target_end"]:
550 haddr
= block
["host_addr"] + (addr
- block
["target_start"])
551 return qemu_core
.read_memory(haddr
, size
)
554 def add_vmcoreinfo(self
):
555 if gdb
.lookup_symbol("vmcoreinfo_realize")[0] is None:
557 vmci
= 'vmcoreinfo_realize::vmcoreinfo_state'
558 if not gdb
.parse_and_eval("%s" % vmci
) \
559 or not gdb
.parse_and_eval("(%s)->has_vmcoreinfo" % vmci
):
562 fmt
= gdb
.parse_and_eval("(%s)->vmcoreinfo.guest_format" % vmci
)
563 addr
= gdb
.parse_and_eval("(%s)->vmcoreinfo.paddr" % vmci
)
564 size
= gdb
.parse_and_eval("(%s)->vmcoreinfo.size" % vmci
)
566 fmt
= le16_to_cpu(fmt
)
567 addr
= le64_to_cpu(addr
)
568 size
= le32_to_cpu(size
)
570 if fmt
!= VMCOREINFO_FORMAT_ELF
:
573 vmcoreinfo
= self
.phys_memory_read(addr
, size
)
575 self
.elf
.add_vmcoreinfo_note(bytes(vmcoreinfo
))
577 def invoke(self
, args
, from_tty
):
578 """Handles command invocation from gdb."""
580 # Unwittingly pressing the Enter key after the command should
581 # not dump the same multi-gig coredump to the same file.
584 argv
= gdb
.string_to_argv(args
)
586 raise gdb
.GdbError("usage: dump-guest-memory FILE ARCH")
588 self
.elf
= ELF(argv
[1])
589 self
.guest_phys_blocks
= get_guest_phys_blocks()
590 self
.add_vmcoreinfo()
592 with
open(argv
[0], "wb") as vmcore
:
593 self
.dump_init(vmcore
)
594 self
.dump_iterate(vmcore
)