3 # Migration Stream Analyzer
5 # Copyright (c) 2015 Alexander Graf <agraf@suse.de>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 if sys
.version_info
< MIN_PYTHON
:
30 sys
.exit("Python %s.%s or later is required.\n" % MIN_PYTHON
)
40 class MigrationFile(object):
41 def __init__(self
, filename
):
42 self
.filename
= filename
43 self
.file = open(self
.filename
, "rb")
46 return int.from_bytes(self
.file.read(8), byteorder
='big', signed
=True)
49 return int.from_bytes(self
.file.read(4), byteorder
='big', signed
=True)
52 return int.from_bytes(self
.file.read(2), byteorder
='big', signed
=True)
55 return int.from_bytes(self
.file.read(1), byteorder
='big', signed
=True)
57 def readstr(self
, len = None):
58 return self
.readvar(len).decode('utf-8')
60 def readvar(self
, size
= None):
65 value
= self
.file.read(size
)
66 if len(value
) != size
:
67 raise Exception("Unexpected end of %s at 0x%x" % (self
.filename
, self
.file.tell()))
71 return self
.file.tell()
73 # The VMSD description is at the end of the file, after EOF. Look for
74 # the last NULL byte, then for the beginning brace of JSON.
75 def read_migration_debug_json(self
):
76 QEMU_VM_VMDESCRIPTION
= 0x06
78 # Remember the offset in the file when we started
79 entrypos
= self
.file.tell()
82 self
.file.seek(0, os
.SEEK_END
)
83 endpos
= self
.file.tell()
84 self
.file.seek(max(-endpos
, -10 * 1024 * 1024), os
.SEEK_END
)
85 datapos
= self
.file.tell()
86 data
= self
.file.read()
87 # The full file read closed the file as well, reopen it
88 self
.file = open(self
.filename
, "rb")
90 # Find the last NULL byte, then the first brace after that. This should
91 # be the beginning of our JSON data.
92 nulpos
= data
.rfind(b
'\0')
93 jsonpos
= data
.find(b
'{', nulpos
)
95 # Check backwards from there and see whether we guessed right
96 self
.file.seek(datapos
+ jsonpos
- 5, 0)
97 if self
.read8() != QEMU_VM_VMDESCRIPTION
:
98 raise Exception("No Debug Migration device found")
100 jsonlen
= self
.read32()
102 # Seek back to where we were at the beginning
103 self
.file.seek(entrypos
, 0)
105 return data
[jsonpos
:jsonpos
+ jsonlen
]
110 class RamSection(object):
111 RAM_SAVE_FLAG_COMPRESS
= 0x02
112 RAM_SAVE_FLAG_MEM_SIZE
= 0x04
113 RAM_SAVE_FLAG_PAGE
= 0x08
114 RAM_SAVE_FLAG_EOS
= 0x10
115 RAM_SAVE_FLAG_CONTINUE
= 0x20
116 RAM_SAVE_FLAG_XBZRLE
= 0x40
117 RAM_SAVE_FLAG_HOOK
= 0x80
119 def __init__(self
, file, version_id
, ramargs
, section_key
):
121 raise Exception("Unknown RAM version %d" % version_id
)
124 self
.section_key
= section_key
125 self
.TARGET_PAGE_SIZE
= ramargs
['page_size']
126 self
.dump_memory
= ramargs
['dump_memory']
127 self
.write_memory
= ramargs
['write_memory']
128 self
.sizeinfo
= collections
.OrderedDict()
129 self
.data
= collections
.OrderedDict()
130 self
.data
['section sizes'] = self
.sizeinfo
132 if self
.write_memory
:
135 self
.memory
= collections
.OrderedDict()
136 self
.data
['memory'] = self
.memory
139 return self
.data
.__repr
__()
142 return self
.data
.__str
__()
148 # Read all RAM sections
150 addr
= self
.file.read64()
151 flags
= addr
& (self
.TARGET_PAGE_SIZE
- 1)
152 addr
&= ~
(self
.TARGET_PAGE_SIZE
- 1)
154 if flags
& self
.RAM_SAVE_FLAG_MEM_SIZE
:
156 namelen
= self
.file.read8()
157 # We assume that no RAM chunk is big enough to ever
158 # hit the first byte of the address, so when we see
159 # a zero here we know it has to be an address, not the
160 # length of the next block.
162 self
.file.file.seek(-1, 1)
164 self
.name
= self
.file.readstr(len = namelen
)
165 len = self
.file.read64()
166 self
.sizeinfo
[self
.name
] = '0x%016x' % len
167 if self
.write_memory
:
169 mkdir_p('./' + os
.path
.dirname(self
.name
))
170 f
= open('./' + self
.name
, "wb")
173 self
.files
[self
.name
] = f
174 flags
&= ~self
.RAM_SAVE_FLAG_MEM_SIZE
176 if flags
& self
.RAM_SAVE_FLAG_COMPRESS
:
177 if flags
& self
.RAM_SAVE_FLAG_CONTINUE
:
178 flags
&= ~self
.RAM_SAVE_FLAG_CONTINUE
180 self
.name
= self
.file.readstr()
181 fill_char
= self
.file.read8()
182 # The page in question is filled with fill_char now
183 if self
.write_memory
and fill_char
!= 0:
184 self
.files
[self
.name
].seek(addr
, os
.SEEK_SET
)
185 self
.files
[self
.name
].write(chr(fill_char
) * self
.TARGET_PAGE_SIZE
)
187 self
.memory
['%s (0x%016x)' % (self
.name
, addr
)] = 'Filled with 0x%02x' % fill_char
188 flags
&= ~self
.RAM_SAVE_FLAG_COMPRESS
189 elif flags
& self
.RAM_SAVE_FLAG_PAGE
:
190 if flags
& self
.RAM_SAVE_FLAG_CONTINUE
:
191 flags
&= ~self
.RAM_SAVE_FLAG_CONTINUE
193 self
.name
= self
.file.readstr()
195 if self
.write_memory
or self
.dump_memory
:
196 data
= self
.file.readvar(size
= self
.TARGET_PAGE_SIZE
)
197 else: # Just skip RAM data
198 self
.file.file.seek(self
.TARGET_PAGE_SIZE
, 1)
200 if self
.write_memory
:
201 self
.files
[self
.name
].seek(addr
, os
.SEEK_SET
)
202 self
.files
[self
.name
].write(data
)
204 hexdata
= " ".join("{0:02x}".format(ord(c
)) for c
in data
)
205 self
.memory
['%s (0x%016x)' % (self
.name
, addr
)] = hexdata
207 flags
&= ~self
.RAM_SAVE_FLAG_PAGE
208 elif flags
& self
.RAM_SAVE_FLAG_XBZRLE
:
209 raise Exception("XBZRLE RAM compression is not supported yet")
210 elif flags
& self
.RAM_SAVE_FLAG_HOOK
:
211 raise Exception("RAM hooks don't make sense with files")
214 if flags
& self
.RAM_SAVE_FLAG_EOS
:
218 raise Exception("Unknown RAM flags: %x" % flags
)
221 if self
.write_memory
:
222 for key
in self
.files
:
223 self
.files
[key
].close()
226 class HTABSection(object):
227 HASH_PTE_SIZE_64
= 16
229 def __init__(self
, file, version_id
, device
, section_key
):
231 raise Exception("Unknown HTAB version %d" % version_id
)
234 self
.section_key
= section_key
238 header
= self
.file.read32()
245 # First section, just the hash shift
248 # Read until end marker
250 index
= self
.file.read32()
251 n_valid
= self
.file.read16()
252 n_invalid
= self
.file.read16()
254 if index
== 0 and n_valid
== 0 and n_invalid
== 0:
257 self
.file.readvar(n_valid
* self
.HASH_PTE_SIZE_64
)
263 class ConfigurationSection(object):
264 def __init__(self
, file):
268 name_len
= self
.file.read32()
269 name
= self
.file.readstr(len = name_len
)
271 class VMSDFieldGeneric(object):
272 def __init__(self
, desc
, file):
278 return str(self
.__str
__())
281 return " ".join("{0:02x}".format(c
) for c
in self
.data
)
284 return self
.__str
__()
287 size
= int(self
.desc
['size'])
288 self
.data
= self
.file.readvar(size
)
291 class VMSDFieldInt(VMSDFieldGeneric
):
292 def __init__(self
, desc
, file):
293 super(VMSDFieldInt
, self
).__init
__(desc
, file)
294 self
.size
= int(desc
['size'])
295 self
.format
= '0x%%0%dx' % (self
.size
* 2)
296 self
.sdtype
= '>i%d' % self
.size
297 self
.udtype
= '>u%d' % self
.size
301 return ('%s (%d)' % ((self
.format
% self
.udata
), self
.data
))
303 return self
.format
% self
.data
306 return self
.__repr
__()
309 return self
.__str
__()
312 super(VMSDFieldInt
, self
).read()
313 self
.sdata
= int.from_bytes(self
.data
, byteorder
='big', signed
=True)
314 self
.udata
= int.from_bytes(self
.data
, byteorder
='big', signed
=False)
315 self
.data
= self
.sdata
318 class VMSDFieldUInt(VMSDFieldInt
):
319 def __init__(self
, desc
, file):
320 super(VMSDFieldUInt
, self
).__init
__(desc
, file)
323 super(VMSDFieldUInt
, self
).read()
324 self
.data
= self
.udata
327 class VMSDFieldIntLE(VMSDFieldInt
):
328 def __init__(self
, desc
, file):
329 super(VMSDFieldIntLE
, self
).__init
__(desc
, file)
330 self
.dtype
= '<i%d' % self
.size
332 class VMSDFieldBool(VMSDFieldGeneric
):
333 def __init__(self
, desc
, file):
334 super(VMSDFieldBool
, self
).__init
__(desc
, file)
337 return self
.data
.__repr
__()
340 return self
.data
.__str
__()
346 super(VMSDFieldBool
, self
).read()
347 if self
.data
[0] == 0:
353 class VMSDFieldStruct(VMSDFieldGeneric
):
354 QEMU_VM_SUBSECTION
= 0x05
356 def __init__(self
, desc
, file):
357 super(VMSDFieldStruct
, self
).__init
__(desc
, file)
358 self
.data
= collections
.OrderedDict()
360 # When we see compressed array elements, unfold them here
362 for field
in self
.desc
['struct']['fields']:
363 if not 'array_len' in field
:
364 new_fields
.append(field
)
366 array_len
= field
.pop('array_len')
368 new_fields
.append(field
)
369 for i
in range(1, array_len
):
374 self
.desc
['struct']['fields'] = new_fields
377 return self
.data
.__repr
__()
380 return self
.data
.__str
__()
383 for field
in self
.desc
['struct']['fields']:
385 reader
= vmsd_field_readers
[field
['type']]
387 reader
= VMSDFieldGeneric
389 field
['data'] = reader(field
, self
.file)
393 if field
['name'] not in self
.data
:
394 self
.data
[field
['name']] = []
395 a
= self
.data
[field
['name']]
396 if len(a
) != int(field
['index']):
397 raise Exception("internal index of data field unmatched (%d/%d)" % (len(a
), int(field
['index'])))
398 a
.append(field
['data'])
400 self
.data
[field
['name']] = field
['data']
402 if 'subsections' in self
.desc
['struct']:
403 for subsection
in self
.desc
['struct']['subsections']:
404 if self
.file.read8() != self
.QEMU_VM_SUBSECTION
:
405 raise Exception("Subsection %s not found at offset %x" % ( subsection
['vmsd_name'], self
.file.tell()))
406 name
= self
.file.readstr()
407 version_id
= self
.file.read32()
408 self
.data
[name
] = VMSDSection(self
.file, version_id
, subsection
, (name
, 0))
409 self
.data
[name
].read()
411 def getDictItem(self
, value
):
412 # Strings would fall into the array category, treat
414 if value
.__class
__ is ''.__class
__:
418 return self
.getDictOrderedDict(value
)
421 return self
.getDictArray(value
)
424 return value
.getDict()
428 def getDictArray(self
, array
):
431 r
.append(self
.getDictItem(value
))
434 def getDictOrderedDict(self
, dict):
435 r
= collections
.OrderedDict()
436 for (key
, value
) in dict.items():
437 r
[key
] = self
.getDictItem(value
)
441 return self
.getDictOrderedDict(self
.data
)
443 vmsd_field_readers
= {
444 "bool" : VMSDFieldBool
,
445 "int8" : VMSDFieldInt
,
446 "int16" : VMSDFieldInt
,
447 "int32" : VMSDFieldInt
,
448 "int32 equal" : VMSDFieldInt
,
449 "int32 le" : VMSDFieldIntLE
,
450 "int64" : VMSDFieldInt
,
451 "uint8" : VMSDFieldUInt
,
452 "uint16" : VMSDFieldUInt
,
453 "uint32" : VMSDFieldUInt
,
454 "uint32 equal" : VMSDFieldUInt
,
455 "uint64" : VMSDFieldUInt
,
456 "int64 equal" : VMSDFieldInt
,
457 "uint8 equal" : VMSDFieldInt
,
458 "uint16 equal" : VMSDFieldInt
,
459 "float64" : VMSDFieldGeneric
,
460 "timer" : VMSDFieldGeneric
,
461 "buffer" : VMSDFieldGeneric
,
462 "unused_buffer" : VMSDFieldGeneric
,
463 "bitmap" : VMSDFieldGeneric
,
464 "struct" : VMSDFieldStruct
,
465 "unknown" : VMSDFieldGeneric
,
468 class VMSDSection(VMSDFieldStruct
):
469 def __init__(self
, file, version_id
, device
, section_key
):
473 self
.section_key
= section_key
475 if 'vmsd_name' in device
:
476 self
.vmsd_name
= device
['vmsd_name']
478 # A section really is nothing but a FieldStruct :)
479 super(VMSDSection
, self
).__init
__({ 'struct' : desc
}, file)
481 ###############################################################################
483 class MigrationDump(object):
484 QEMU_VM_FILE_MAGIC
= 0x5145564d
485 QEMU_VM_FILE_VERSION
= 0x00000003
487 QEMU_VM_SECTION_START
= 0x01
488 QEMU_VM_SECTION_PART
= 0x02
489 QEMU_VM_SECTION_END
= 0x03
490 QEMU_VM_SECTION_FULL
= 0x04
491 QEMU_VM_SUBSECTION
= 0x05
492 QEMU_VM_VMDESCRIPTION
= 0x06
493 QEMU_VM_CONFIGURATION
= 0x07
494 QEMU_VM_SECTION_FOOTER
= 0x7e
496 def __init__(self
, filename
):
497 self
.section_classes
= { ( 'ram', 0 ) : [ RamSection
, None ],
498 ( 'spapr/htab', 0) : ( HTABSection
, None ) }
499 self
.filename
= filename
500 self
.vmsd_desc
= None
502 def read(self
, desc_only
= False, dump_memory
= False, write_memory
= False):
503 # Read in the whole file
504 file = MigrationFile(self
.filename
)
508 if data
!= self
.QEMU_VM_FILE_MAGIC
:
509 raise Exception("Invalid file magic %x" % data
)
511 # Version (has to be v3)
513 if data
!= self
.QEMU_VM_FILE_VERSION
:
514 raise Exception("Invalid version number %d" % data
)
516 self
.load_vmsd_json(file)
519 self
.sections
= collections
.OrderedDict()
525 ramargs
['page_size'] = self
.vmsd_desc
['page_size']
526 ramargs
['dump_memory'] = dump_memory
527 ramargs
['write_memory'] = write_memory
528 self
.section_classes
[('ram',0)][1] = ramargs
531 section_type
= file.read8()
532 if section_type
== self
.QEMU_VM_EOF
:
534 elif section_type
== self
.QEMU_VM_CONFIGURATION
:
535 section
= ConfigurationSection(file)
537 elif section_type
== self
.QEMU_VM_SECTION_START
or section_type
== self
.QEMU_VM_SECTION_FULL
:
538 section_id
= file.read32()
539 name
= file.readstr()
540 instance_id
= file.read32()
541 version_id
= file.read32()
542 section_key
= (name
, instance_id
)
543 classdesc
= self
.section_classes
[section_key
]
544 section
= classdesc
[0](file, version_id
, classdesc
[1], section_key
)
545 self
.sections
[section_id
] = section
547 elif section_type
== self
.QEMU_VM_SECTION_PART
or section_type
== self
.QEMU_VM_SECTION_END
:
548 section_id
= file.read32()
549 self
.sections
[section_id
].read()
550 elif section_type
== self
.QEMU_VM_SECTION_FOOTER
:
551 read_section_id
= file.read32()
552 if read_section_id
!= section_id
:
553 raise Exception("Mismatched section footer: %x vs %x" % (read_section_id
, section_id
))
555 raise Exception("Unknown section type: %d" % section_type
)
558 def load_vmsd_json(self
, file):
559 vmsd_json
= file.read_migration_debug_json()
560 self
.vmsd_desc
= json
.loads(vmsd_json
, object_pairs_hook
=collections
.OrderedDict
)
561 for device
in self
.vmsd_desc
['devices']:
562 key
= (device
['name'], device
['instance_id'])
563 value
= ( VMSDSection
, device
)
564 self
.section_classes
[key
] = value
567 r
= collections
.OrderedDict()
568 for (key
, value
) in self
.sections
.items():
569 key
= "%s (%d)" % ( value
.section_key
[0], key
)
570 r
[key
] = value
.getDict()
573 ###############################################################################
575 class JSONEncoder(json
.JSONEncoder
):
576 def default(self
, o
):
577 if isinstance(o
, VMSDFieldGeneric
):
579 return json
.JSONEncoder
.default(self
, o
)
581 parser
= argparse
.ArgumentParser()
582 parser
.add_argument("-f", "--file", help='migration dump to read from', required
=True)
583 parser
.add_argument("-m", "--memory", help='dump RAM contents as well', action
='store_true')
584 parser
.add_argument("-d", "--dump", help='what to dump ("state" or "desc")', default
='state')
585 parser
.add_argument("-x", "--extract", help='extract contents into individual files', action
='store_true')
586 args
= parser
.parse_args()
588 jsonenc
= JSONEncoder(indent
=4, separators
=(',', ': '))
591 dump
= MigrationDump(args
.file)
593 dump
.read(desc_only
= True)
595 f
= open("desc.json", "wb")
597 f
.write(jsonenc
.encode(dump
.vmsd_desc
))
600 dump
.read(write_memory
= True)
601 dict = dump
.getDict()
603 f
= open("state.json", "wb")
605 f
.write(jsonenc
.encode(dict))
607 elif args
.dump
== "state":
608 dump
= MigrationDump(args
.file)
609 dump
.read(dump_memory
= args
.memory
)
610 dict = dump
.getDict()
611 print(jsonenc
.encode(dict))
612 elif args
.dump
== "desc":
613 dump
= MigrationDump(args
.file)
614 dump
.read(desc_only
= True)
615 print(jsonenc
.encode(dump
.vmsd_desc
))
617 raise Exception("Please specify either -x, -d state or -d dump")