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.
18 UINTPTR_T
= gdb
.lookup_type("uintptr_t")
20 TARGET_PAGE_SIZE
= 0x1000
21 TARGET_PAGE_MASK
= 0xFFFFFFFFFFFFF000
23 # Special value for e_phnum. This indicates that the real number of
24 # program headers is too large to fit into e_phnum. Instead the real
25 # value is in the field sh_info of section 0.
49 """Representation of a ELF file."""
51 def __init__(self
, arch
):
56 self
.endianness
= None
57 self
.elfclass
= ELFCLASS64
59 if arch
== 'aarch64-le':
60 self
.endianness
= ELFDATA2LSB
61 self
.elfclass
= ELFCLASS64
62 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
63 self
.ehdr
.e_machine
= EM_AARCH
65 elif arch
== 'aarch64-be':
66 self
.endianness
= ELFDATA2MSB
67 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
68 self
.ehdr
.e_machine
= EM_AARCH
70 elif arch
== 'X86_64':
71 self
.endianness
= ELFDATA2LSB
72 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
73 self
.ehdr
.e_machine
= EM_X86_64
76 self
.endianness
= ELFDATA2LSB
77 self
.elfclass
= ELFCLASS32
78 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
79 self
.ehdr
.e_machine
= EM_386
82 self
.endianness
= ELFDATA2MSB
83 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
84 self
.ehdr
.e_machine
= EM_S390
86 elif arch
== 'ppc64-le':
87 self
.endianness
= ELFDATA2LSB
88 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
89 self
.ehdr
.e_machine
= EM_PPC64
91 elif arch
== 'ppc64-be':
92 self
.endianness
= ELFDATA2MSB
93 self
.ehdr
= get_arch_ehdr(self
.endianness
, self
.elfclass
)
94 self
.ehdr
.e_machine
= EM_PPC64
97 raise gdb
.GdbError("No valid arch type specified.\n"
98 "Currently supported types:\n"
99 "aarch64-be, aarch64-le, X86_64, 386, s390, "
100 "ppc64-be, ppc64-le")
102 self
.add_segment(PT_NOTE
, 0, 0)
104 def add_note(self
, n_name
, n_desc
, n_type
):
105 """Adds a note to the ELF."""
107 note
= get_arch_note(self
.endianness
, len(n_name
), len(n_desc
))
108 note
.n_namesz
= len(n_name
) + 1
109 note
.n_descsz
= len(n_desc
)
110 note
.n_name
= n_name
.encode()
113 # Desc needs to be 4 byte aligned (although the 64bit spec
114 # specifies 8 byte). When defining n_desc as uint32 it will be
115 # automatically aligned but we need the memmove to copy the
117 ctypes
.memmove(note
.n_desc
, n_desc
.encode(), len(n_desc
))
119 self
.notes
.append(note
)
120 self
.segments
[0].p_filesz
+= ctypes
.sizeof(note
)
121 self
.segments
[0].p_memsz
+= ctypes
.sizeof(note
)
123 def add_segment(self
, p_type
, p_paddr
, p_size
):
124 """Adds a segment to the elf."""
126 phdr
= get_arch_phdr(self
.endianness
, self
.elfclass
)
128 phdr
.p_paddr
= p_paddr
129 phdr
.p_filesz
= p_size
130 phdr
.p_memsz
= p_size
131 self
.segments
.append(phdr
)
132 self
.ehdr
.e_phnum
+= 1
134 def to_file(self
, elf_file
):
135 """Writes all ELF structures to the the passed file.
145 elf_file
.write(self
.ehdr
)
146 off
= ctypes
.sizeof(self
.ehdr
) + \
147 len(self
.segments
) * ctypes
.sizeof(self
.segments
[0])
149 for phdr
in self
.segments
:
154 for note
in self
.notes
:
158 def get_arch_note(endianness
, len_name
, len_desc
):
159 """Returns a Note class with the specified endianness."""
161 if endianness
== ELFDATA2LSB
:
162 superclass
= ctypes
.LittleEndianStructure
164 superclass
= ctypes
.BigEndianStructure
166 len_name
= len_name
+ 1
168 class Note(superclass
):
169 """Represents an ELF note, includes the content."""
171 _fields_
= [("n_namesz", ctypes
.c_uint32
),
172 ("n_descsz", ctypes
.c_uint32
),
173 ("n_type", ctypes
.c_uint32
),
174 ("n_name", ctypes
.c_char
* len_name
),
175 ("n_desc", ctypes
.c_uint32
* ((len_desc
+ 3) // 4))]
179 class Ident(ctypes
.Structure
):
180 """Represents the ELF ident array in the ehdr structure."""
182 _fields_
= [('ei_mag0', ctypes
.c_ubyte
),
183 ('ei_mag1', ctypes
.c_ubyte
),
184 ('ei_mag2', ctypes
.c_ubyte
),
185 ('ei_mag3', ctypes
.c_ubyte
),
186 ('ei_class', ctypes
.c_ubyte
),
187 ('ei_data', ctypes
.c_ubyte
),
188 ('ei_version', ctypes
.c_ubyte
),
189 ('ei_osabi', ctypes
.c_ubyte
),
190 ('ei_abiversion', ctypes
.c_ubyte
),
191 ('ei_pad', ctypes
.c_ubyte
* 7)]
193 def __init__(self
, endianness
, elfclass
):
195 self
.ei_mag1
= ord('E')
196 self
.ei_mag2
= ord('L')
197 self
.ei_mag3
= ord('F')
198 self
.ei_class
= elfclass
199 self
.ei_data
= endianness
200 self
.ei_version
= EV_CURRENT
203 def get_arch_ehdr(endianness
, elfclass
):
204 """Returns a EHDR64 class with the specified endianness."""
206 if endianness
== ELFDATA2LSB
:
207 superclass
= ctypes
.LittleEndianStructure
209 superclass
= ctypes
.BigEndianStructure
211 class EHDR64(superclass
):
212 """Represents the 64 bit ELF header struct."""
214 _fields_
= [('e_ident', Ident
),
215 ('e_type', ctypes
.c_uint16
),
216 ('e_machine', ctypes
.c_uint16
),
217 ('e_version', ctypes
.c_uint32
),
218 ('e_entry', ctypes
.c_uint64
),
219 ('e_phoff', ctypes
.c_uint64
),
220 ('e_shoff', ctypes
.c_uint64
),
221 ('e_flags', ctypes
.c_uint32
),
222 ('e_ehsize', ctypes
.c_uint16
),
223 ('e_phentsize', ctypes
.c_uint16
),
224 ('e_phnum', ctypes
.c_uint16
),
225 ('e_shentsize', ctypes
.c_uint16
),
226 ('e_shnum', ctypes
.c_uint16
),
227 ('e_shstrndx', ctypes
.c_uint16
)]
230 super(superclass
, self
).__init
__()
231 self
.e_ident
= Ident(endianness
, elfclass
)
232 self
.e_type
= ET_CORE
233 self
.e_version
= EV_CURRENT
234 self
.e_ehsize
= ctypes
.sizeof(self
)
235 self
.e_phoff
= ctypes
.sizeof(self
)
236 self
.e_phentsize
= ctypes
.sizeof(get_arch_phdr(endianness
, elfclass
))
240 class EHDR32(superclass
):
241 """Represents the 32 bit ELF header struct."""
243 _fields_
= [('e_ident', Ident
),
244 ('e_type', ctypes
.c_uint16
),
245 ('e_machine', ctypes
.c_uint16
),
246 ('e_version', ctypes
.c_uint32
),
247 ('e_entry', ctypes
.c_uint32
),
248 ('e_phoff', ctypes
.c_uint32
),
249 ('e_shoff', ctypes
.c_uint32
),
250 ('e_flags', ctypes
.c_uint32
),
251 ('e_ehsize', ctypes
.c_uint16
),
252 ('e_phentsize', ctypes
.c_uint16
),
253 ('e_phnum', ctypes
.c_uint16
),
254 ('e_shentsize', ctypes
.c_uint16
),
255 ('e_shnum', ctypes
.c_uint16
),
256 ('e_shstrndx', ctypes
.c_uint16
)]
259 super(superclass
, self
).__init
__()
260 self
.e_ident
= Ident(endianness
, elfclass
)
261 self
.e_type
= ET_CORE
262 self
.e_version
= EV_CURRENT
263 self
.e_ehsize
= ctypes
.sizeof(self
)
264 self
.e_phoff
= ctypes
.sizeof(self
)
265 self
.e_phentsize
= ctypes
.sizeof(get_arch_phdr(endianness
, elfclass
))
269 if elfclass
== ELFCLASS64
:
275 def get_arch_phdr(endianness
, elfclass
):
276 """Returns a 32 or 64 bit PHDR class with the specified endianness."""
278 if endianness
== ELFDATA2LSB
:
279 superclass
= ctypes
.LittleEndianStructure
281 superclass
= ctypes
.BigEndianStructure
283 class PHDR64(superclass
):
284 """Represents the 64 bit ELF program header struct."""
286 _fields_
= [('p_type', ctypes
.c_uint32
),
287 ('p_flags', ctypes
.c_uint32
),
288 ('p_offset', ctypes
.c_uint64
),
289 ('p_vaddr', ctypes
.c_uint64
),
290 ('p_paddr', ctypes
.c_uint64
),
291 ('p_filesz', ctypes
.c_uint64
),
292 ('p_memsz', ctypes
.c_uint64
),
293 ('p_align', ctypes
.c_uint64
)]
295 class PHDR32(superclass
):
296 """Represents the 32 bit ELF program header struct."""
298 _fields_
= [('p_type', ctypes
.c_uint32
),
299 ('p_offset', ctypes
.c_uint32
),
300 ('p_vaddr', ctypes
.c_uint32
),
301 ('p_paddr', ctypes
.c_uint32
),
302 ('p_filesz', ctypes
.c_uint32
),
303 ('p_memsz', ctypes
.c_uint32
),
304 ('p_flags', ctypes
.c_uint32
),
305 ('p_align', ctypes
.c_uint32
)]
308 if elfclass
== ELFCLASS64
:
314 def int128_get64(val
):
315 """Returns low 64bit part of Int128 struct."""
317 assert val
["hi"] == 0
321 def qlist_foreach(head
, field_str
):
322 """Generator for qlists."""
324 var_p
= head
["lh_first"]
326 var
= var_p
.dereference()
327 var_p
= var
[field_str
]["le_next"]
331 def qemu_map_ram_ptr(block
, offset
):
332 """Returns qemu vaddr for given guest physical address."""
334 return block
["host"] + offset
337 def memory_region_get_ram_ptr(memory_region
):
338 if memory_region
["alias"] != 0:
339 return (memory_region_get_ram_ptr(memory_region
["alias"].dereference())
340 + memory_region
["alias_offset"])
342 return qemu_map_ram_ptr(memory_region
["ram_block"], 0)
345 def get_guest_phys_blocks():
346 """Returns a list of ram blocks.
348 Each block entry contains:
349 'target_start': guest block phys start address
350 'target_end': guest block phys end address
351 'host_addr': qemu vaddr of the block's start
354 guest_phys_blocks
= []
356 print("guest RAM blocks:")
357 print("target_start target_end host_addr message "
359 print("---------------- ---------------- ---------------- ------- "
362 current_map_p
= gdb
.parse_and_eval("address_space_memory.current_map")
363 current_map
= current_map_p
.dereference()
365 # Conversion to int is needed for python 3
366 # compatibility. Otherwise range doesn't cast the value itself and
368 for cur
in range(int(current_map
["nr"])):
369 flat_range
= (current_map
["ranges"] + cur
).dereference()
370 memory_region
= flat_range
["mr"].dereference()
372 # we only care about RAM
373 if not memory_region
["ram"]:
376 section_size
= int128_get64(flat_range
["addr"]["size"])
377 target_start
= int128_get64(flat_range
["addr"]["start"])
378 target_end
= target_start
+ section_size
379 host_addr
= (memory_region_get_ram_ptr(memory_region
)
380 + flat_range
["offset_in_region"])
383 # find continuity in guest physical address space
384 if len(guest_phys_blocks
) > 0:
385 predecessor
= guest_phys_blocks
[-1]
386 predecessor_size
= (predecessor
["target_end"] -
387 predecessor
["target_start"])
389 # the memory API guarantees monotonically increasing
391 assert predecessor
["target_end"] <= target_start
393 # we want continuity in both guest-physical and
394 # host-virtual memory
395 if (predecessor
["target_end"] < target_start
or
396 predecessor
["host_addr"] + predecessor_size
!= host_addr
):
399 if predecessor
is None:
400 # isolated mapping, add it to the list
401 guest_phys_blocks
.append({"target_start": target_start
,
402 "target_end": target_end
,
403 "host_addr": host_addr
})
406 # expand predecessor until @target_end; predecessor's
407 # start doesn't change
408 predecessor
["target_end"] = target_end
411 print("%016x %016x %016x %-7s %5u" %
412 (target_start
, target_end
, host_addr
.cast(UINTPTR_T
),
413 message
, len(guest_phys_blocks
)))
415 return guest_phys_blocks
418 # The leading docstring doesn't have idiomatic Python formatting. It is
419 # printed by gdb's "help" command (the first line is printed in the
420 # "help data" summary), and it should match how other help texts look in
422 class DumpGuestMemory(gdb
.Command
):
423 """Extract guest vmcore from qemu process coredump.
425 The two required arguments are FILE and ARCH:
426 FILE identifies the target file to write the guest vmcore to.
427 ARCH specifies the architecture for which the core will be generated.
429 This GDB command reimplements the dump-guest-memory QMP command in
430 python, using the representation of guest memory as captured in the qemu
431 coredump. The qemu process that has been dumped must have had the
432 command line option "-machine dump-guest-core=on" which is the default.
434 For simplicity, the "paging", "begin" and "end" parameters of the QMP
435 command are not supported -- no attempt is made to get the guest's
436 internal paging structures (ie. paging=false is hard-wired), and guest
437 memory is always fully dumped.
439 Currently aarch64-be, aarch64-le, X86_64, 386, s390, ppc64-be,
440 ppc64-le guests are supported.
442 The CORE/NT_PRSTATUS and QEMU notes (that is, the VCPUs' statuses) are
443 not written to the vmcore. Preparing these would require context that is
444 only present in the KVM host kernel module when the guest is alive. A
445 fake ELF note is written instead, only to keep the ELF parser of "crash"
448 Dependent on how busted the qemu process was at the time of the
449 coredump, this command might produce unpredictable results. If qemu
450 deliberately called abort(), or it was dumped in response to a signal at
451 a halfway fortunate point, then its coredump should be in reasonable
452 shape and this command should mostly work."""
455 super(DumpGuestMemory
, self
).__init
__("dump-guest-memory",
457 gdb
.COMPLETE_FILENAME
)
459 self
.guest_phys_blocks
= None
461 def dump_init(self
, vmcore
):
462 """Prepares and writes ELF structures to core file."""
464 # Needed to make crash happy, data for more useful notes is
465 # not available in a qemu core.
466 self
.elf
.add_note("NONE", "EMPTY", 0)
468 # We should never reach PN_XNUM for paging=false dumps,
469 # there's just a handful of discontiguous ranges after
471 # The constant is needed to account for the PT_NOTE segment.
472 phdr_num
= len(self
.guest_phys_blocks
) + 1
473 assert phdr_num
< PN_XNUM
475 for block
in self
.guest_phys_blocks
:
476 block_size
= block
["target_end"] - block
["target_start"]
477 self
.elf
.add_segment(PT_LOAD
, block
["target_start"], block_size
)
479 self
.elf
.to_file(vmcore
)
481 def dump_iterate(self
, vmcore
):
482 """Writes guest core to file."""
484 qemu_core
= gdb
.inferiors()[0]
485 for block
in self
.guest_phys_blocks
:
486 cur
= block
["host_addr"]
487 left
= block
["target_end"] - block
["target_start"]
488 print("dumping range at %016x for length %016x" %
489 (cur
.cast(UINTPTR_T
), left
))
492 chunk_size
= min(TARGET_PAGE_SIZE
, left
)
493 chunk
= qemu_core
.read_memory(cur
, chunk_size
)
498 def invoke(self
, args
, from_tty
):
499 """Handles command invocation from gdb."""
501 # Unwittingly pressing the Enter key after the command should
502 # not dump the same multi-gig coredump to the same file.
505 argv
= gdb
.string_to_argv(args
)
507 raise gdb
.GdbError("usage: dump-guest-memory FILE ARCH")
509 self
.elf
= ELF(argv
[1])
510 self
.guest_phys_blocks
= get_guest_phys_blocks()
512 with
open(argv
[0], "wb") as vmcore
:
513 self
.dump_init(vmcore
)
514 self
.dump_iterate(vmcore
)