xref: /openbmc/qemu/scripts/analyze-migration.py (revision bcad45de)
1#!/usr/bin/env python
2#
3#  Migration Stream Analyzer
4#
5#  Copyright (c) 2015 Alexander Graf <agraf@suse.de>
6#
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.
11#
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.
16#
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/>.
19
20import numpy as np
21import json
22import os
23import argparse
24import collections
25import pprint
26
27def mkdir_p(path):
28    try:
29        os.makedirs(path)
30    except OSError:
31        pass
32
33class MigrationFile(object):
34    def __init__(self, filename):
35        self.filename = filename
36        self.file = open(self.filename, "rb")
37
38    def read64(self):
39        return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0])
40
41    def read32(self):
42        return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0])
43
44    def read16(self):
45        return np.asscalar(np.fromfile(self.file, count=1, dtype='>i2')[0])
46
47    def read8(self):
48        return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0])
49
50    def readstr(self, len = None):
51        if len is None:
52            len = self.read8()
53        if len == 0:
54            return ""
55        return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0]
56
57    def readvar(self, size = None):
58        if size is None:
59            size = self.read8()
60        if size == 0:
61            return ""
62        value = self.file.read(size)
63        if len(value) != size:
64            raise Exception("Unexpected end of %s at 0x%x" % (self.filename, self.file.tell()))
65        return value
66
67    def tell(self):
68        return self.file.tell()
69
70    # The VMSD description is at the end of the file, after EOF. Look for
71    # the last NULL byte, then for the beginning brace of JSON.
72    def read_migration_debug_json(self):
73        QEMU_VM_VMDESCRIPTION = 0x06
74
75        # Remember the offset in the file when we started
76        entrypos = self.file.tell()
77
78        # Read the last 10MB
79        self.file.seek(0, os.SEEK_END)
80        endpos = self.file.tell()
81        self.file.seek(max(-endpos, -10 * 1024 * 1024), os.SEEK_END)
82        datapos = self.file.tell()
83        data = self.file.read()
84        # The full file read closed the file as well, reopen it
85        self.file = open(self.filename, "rb")
86
87        # Find the last NULL byte, then the first brace after that. This should
88        # be the beginning of our JSON data.
89        nulpos = data.rfind("\0")
90        jsonpos = data.find("{", nulpos)
91
92        # Check backwards from there and see whether we guessed right
93        self.file.seek(datapos + jsonpos - 5, 0)
94        if self.read8() != QEMU_VM_VMDESCRIPTION:
95            raise Exception("No Debug Migration device found")
96
97        jsonlen = self.read32()
98
99        # Seek back to where we were at the beginning
100        self.file.seek(entrypos, 0)
101
102        return data[jsonpos:jsonpos + jsonlen]
103
104    def close(self):
105        self.file.close()
106
107class RamSection(object):
108    RAM_SAVE_FLAG_COMPRESS = 0x02
109    RAM_SAVE_FLAG_MEM_SIZE = 0x04
110    RAM_SAVE_FLAG_PAGE     = 0x08
111    RAM_SAVE_FLAG_EOS      = 0x10
112    RAM_SAVE_FLAG_CONTINUE = 0x20
113    RAM_SAVE_FLAG_XBZRLE   = 0x40
114    RAM_SAVE_FLAG_HOOK     = 0x80
115
116    def __init__(self, file, version_id, ramargs, section_key):
117        if version_id != 4:
118            raise Exception("Unknown RAM version %d" % version_id)
119
120        self.file = file
121        self.section_key = section_key
122        self.TARGET_PAGE_SIZE = ramargs['page_size']
123        self.dump_memory = ramargs['dump_memory']
124        self.write_memory = ramargs['write_memory']
125        self.sizeinfo = collections.OrderedDict()
126        self.data = collections.OrderedDict()
127        self.data['section sizes'] = self.sizeinfo
128        self.name = ''
129        if self.write_memory:
130            self.files = { }
131        if self.dump_memory:
132            self.memory = collections.OrderedDict()
133            self.data['memory'] = self.memory
134
135    def __repr__(self):
136        return self.data.__repr__()
137
138    def __str__(self):
139        return self.data.__str__()
140
141    def getDict(self):
142        return self.data
143
144    def read(self):
145        # Read all RAM sections
146        while True:
147            addr = self.file.read64()
148            flags = addr & (self.TARGET_PAGE_SIZE - 1)
149            addr &= ~(self.TARGET_PAGE_SIZE - 1)
150
151            if flags & self.RAM_SAVE_FLAG_MEM_SIZE:
152                while True:
153                    namelen = self.file.read8()
154                    # We assume that no RAM chunk is big enough to ever
155                    # hit the first byte of the address, so when we see
156                    # a zero here we know it has to be an address, not the
157                    # length of the next block.
158                    if namelen == 0:
159                        self.file.file.seek(-1, 1)
160                        break
161                    self.name = self.file.readstr(len = namelen)
162                    len = self.file.read64()
163                    self.sizeinfo[self.name] = '0x%016x' % len
164                    if self.write_memory:
165                        print self.name
166                        mkdir_p('./' + os.path.dirname(self.name))
167                        f = open('./' + self.name, "wb")
168                        f.truncate(0)
169                        f.truncate(len)
170                        self.files[self.name] = f
171                flags &= ~self.RAM_SAVE_FLAG_MEM_SIZE
172
173            if flags & self.RAM_SAVE_FLAG_COMPRESS:
174                if flags & self.RAM_SAVE_FLAG_CONTINUE:
175                    flags &= ~self.RAM_SAVE_FLAG_CONTINUE
176                else:
177                    self.name = self.file.readstr()
178                fill_char = self.file.read8()
179                # The page in question is filled with fill_char now
180                if self.write_memory and fill_char != 0:
181                    self.files[self.name].seek(addr, os.SEEK_SET)
182                    self.files[self.name].write(chr(fill_char) * self.TARGET_PAGE_SIZE)
183                if self.dump_memory:
184                    self.memory['%s (0x%016x)' % (self.name, addr)] = 'Filled with 0x%02x' % fill_char
185                flags &= ~self.RAM_SAVE_FLAG_COMPRESS
186            elif flags & self.RAM_SAVE_FLAG_PAGE:
187                if flags & self.RAM_SAVE_FLAG_CONTINUE:
188                    flags &= ~self.RAM_SAVE_FLAG_CONTINUE
189                else:
190                    self.name = self.file.readstr()
191
192                if self.write_memory or self.dump_memory:
193                    data = self.file.readvar(size = self.TARGET_PAGE_SIZE)
194                else: # Just skip RAM data
195                    self.file.file.seek(self.TARGET_PAGE_SIZE, 1)
196
197                if self.write_memory:
198                    self.files[self.name].seek(addr, os.SEEK_SET)
199                    self.files[self.name].write(data)
200                if self.dump_memory:
201                    hexdata = " ".join("{0:02x}".format(ord(c)) for c in data)
202                    self.memory['%s (0x%016x)' % (self.name, addr)] = hexdata
203
204                flags &= ~self.RAM_SAVE_FLAG_PAGE
205            elif flags & self.RAM_SAVE_FLAG_XBZRLE:
206                raise Exception("XBZRLE RAM compression is not supported yet")
207            elif flags & self.RAM_SAVE_FLAG_HOOK:
208                raise Exception("RAM hooks don't make sense with files")
209
210            # End of RAM section
211            if flags & self.RAM_SAVE_FLAG_EOS:
212                break
213
214            if flags != 0:
215                raise Exception("Unknown RAM flags: %x" % flags)
216
217    def __del__(self):
218        if self.write_memory:
219            for key in self.files:
220                self.files[key].close()
221
222
223class HTABSection(object):
224    HASH_PTE_SIZE_64       = 16
225
226    def __init__(self, file, version_id, device, section_key):
227        if version_id != 1:
228            raise Exception("Unknown HTAB version %d" % version_id)
229
230        self.file = file
231        self.section_key = section_key
232
233    def read(self):
234
235        header = self.file.read32()
236
237        if (header > 0):
238            # First section, just the hash shift
239            return
240
241        # Read until end marker
242        while True:
243            index = self.file.read32()
244            n_valid = self.file.read16()
245            n_invalid = self.file.read16()
246
247            if index == 0 and n_valid == 0 and n_invalid == 0:
248                break
249
250            self.file.readvar(n_valid * self.HASH_PTE_SIZE_64)
251
252    def getDict(self):
253        return ""
254
255
256class ConfigurationSection(object):
257    def __init__(self, file):
258        self.file = file
259
260    def read(self):
261        name_len = self.file.read32()
262        name = self.file.readstr(len = name_len)
263
264class VMSDFieldGeneric(object):
265    def __init__(self, desc, file):
266        self.file = file
267        self.desc = desc
268        self.data = ""
269
270    def __repr__(self):
271        return str(self.__str__())
272
273    def __str__(self):
274        return " ".join("{0:02x}".format(ord(c)) for c in self.data)
275
276    def getDict(self):
277        return self.__str__()
278
279    def read(self):
280        size = int(self.desc['size'])
281        self.data = self.file.readvar(size)
282        return self.data
283
284class VMSDFieldInt(VMSDFieldGeneric):
285    def __init__(self, desc, file):
286        super(VMSDFieldInt, self).__init__(desc, file)
287        self.size = int(desc['size'])
288        self.format = '0x%%0%dx' % (self.size * 2)
289        self.sdtype = '>i%d' % self.size
290        self.udtype = '>u%d' % self.size
291
292    def __repr__(self):
293        if self.data < 0:
294            return ('%s (%d)' % ((self.format % self.udata), self.data))
295        else:
296            return self.format % self.data
297
298    def __str__(self):
299        return self.__repr__()
300
301    def getDict(self):
302        return self.__str__()
303
304    def read(self):
305        super(VMSDFieldInt, self).read()
306        self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0]
307        self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0]
308        self.data = self.sdata
309        return self.data
310
311class VMSDFieldUInt(VMSDFieldInt):
312    def __init__(self, desc, file):
313        super(VMSDFieldUInt, self).__init__(desc, file)
314
315    def read(self):
316        super(VMSDFieldUInt, self).read()
317        self.data = self.udata
318        return self.data
319
320class VMSDFieldIntLE(VMSDFieldInt):
321    def __init__(self, desc, file):
322        super(VMSDFieldIntLE, self).__init__(desc, file)
323        self.dtype = '<i%d' % self.size
324
325class VMSDFieldBool(VMSDFieldGeneric):
326    def __init__(self, desc, file):
327        super(VMSDFieldBool, self).__init__(desc, file)
328
329    def __repr__(self):
330        return self.data.__repr__()
331
332    def __str__(self):
333        return self.data.__str__()
334
335    def getDict(self):
336        return self.data
337
338    def read(self):
339        super(VMSDFieldBool, self).read()
340        if self.data[0] == 0:
341            self.data = False
342        else:
343            self.data = True
344        return self.data
345
346class VMSDFieldStruct(VMSDFieldGeneric):
347    QEMU_VM_SUBSECTION    = 0x05
348
349    def __init__(self, desc, file):
350        super(VMSDFieldStruct, self).__init__(desc, file)
351        self.data = collections.OrderedDict()
352
353        # When we see compressed array elements, unfold them here
354        new_fields = []
355        for field in self.desc['struct']['fields']:
356            if not 'array_len' in field:
357                new_fields.append(field)
358                continue
359            array_len = field.pop('array_len')
360            field['index'] = 0
361            new_fields.append(field)
362            for i in xrange(1, array_len):
363                c = field.copy()
364                c['index'] = i
365                new_fields.append(c)
366
367        self.desc['struct']['fields'] = new_fields
368
369    def __repr__(self):
370        return self.data.__repr__()
371
372    def __str__(self):
373        return self.data.__str__()
374
375    def read(self):
376        for field in self.desc['struct']['fields']:
377            try:
378                reader = vmsd_field_readers[field['type']]
379            except:
380                reader = VMSDFieldGeneric
381
382            field['data'] = reader(field, self.file)
383            field['data'].read()
384
385            if 'index' in field:
386                if field['name'] not in self.data:
387                    self.data[field['name']] = []
388                a = self.data[field['name']]
389                if len(a) != int(field['index']):
390                    raise Exception("internal index of data field unmatched (%d/%d)" % (len(a), int(field['index'])))
391                a.append(field['data'])
392            else:
393                self.data[field['name']] = field['data']
394
395        if 'subsections' in self.desc['struct']:
396            for subsection in self.desc['struct']['subsections']:
397                if self.file.read8() != self.QEMU_VM_SUBSECTION:
398                    raise Exception("Subsection %s not found at offset %x" % ( subsection['vmsd_name'], self.file.tell()))
399                name = self.file.readstr()
400                version_id = self.file.read32()
401                self.data[name] = VMSDSection(self.file, version_id, subsection, (name, 0))
402                self.data[name].read()
403
404    def getDictItem(self, value):
405       # Strings would fall into the array category, treat
406       # them specially
407       if value.__class__ is ''.__class__:
408           return value
409
410       try:
411           return self.getDictOrderedDict(value)
412       except:
413           try:
414               return self.getDictArray(value)
415           except:
416               try:
417                   return value.getDict()
418               except:
419                   return value
420
421    def getDictArray(self, array):
422        r = []
423        for value in array:
424           r.append(self.getDictItem(value))
425        return r
426
427    def getDictOrderedDict(self, dict):
428        r = collections.OrderedDict()
429        for (key, value) in dict.items():
430            r[key] = self.getDictItem(value)
431        return r
432
433    def getDict(self):
434        return self.getDictOrderedDict(self.data)
435
436vmsd_field_readers = {
437    "bool" : VMSDFieldBool,
438    "int8" : VMSDFieldInt,
439    "int16" : VMSDFieldInt,
440    "int32" : VMSDFieldInt,
441    "int32 equal" : VMSDFieldInt,
442    "int32 le" : VMSDFieldIntLE,
443    "int64" : VMSDFieldInt,
444    "uint8" : VMSDFieldUInt,
445    "uint16" : VMSDFieldUInt,
446    "uint32" : VMSDFieldUInt,
447    "uint32 equal" : VMSDFieldUInt,
448    "uint64" : VMSDFieldUInt,
449    "int64 equal" : VMSDFieldInt,
450    "uint8 equal" : VMSDFieldInt,
451    "uint16 equal" : VMSDFieldInt,
452    "float64" : VMSDFieldGeneric,
453    "timer" : VMSDFieldGeneric,
454    "buffer" : VMSDFieldGeneric,
455    "unused_buffer" : VMSDFieldGeneric,
456    "bitmap" : VMSDFieldGeneric,
457    "struct" : VMSDFieldStruct,
458    "unknown" : VMSDFieldGeneric,
459}
460
461class VMSDSection(VMSDFieldStruct):
462    def __init__(self, file, version_id, device, section_key):
463        self.file = file
464        self.data = ""
465        self.vmsd_name = ""
466        self.section_key = section_key
467        desc = device
468        if 'vmsd_name' in device:
469            self.vmsd_name = device['vmsd_name']
470
471        # A section really is nothing but a FieldStruct :)
472        super(VMSDSection, self).__init__({ 'struct' : desc }, file)
473
474###############################################################################
475
476class MigrationDump(object):
477    QEMU_VM_FILE_MAGIC    = 0x5145564d
478    QEMU_VM_FILE_VERSION  = 0x00000003
479    QEMU_VM_EOF           = 0x00
480    QEMU_VM_SECTION_START = 0x01
481    QEMU_VM_SECTION_PART  = 0x02
482    QEMU_VM_SECTION_END   = 0x03
483    QEMU_VM_SECTION_FULL  = 0x04
484    QEMU_VM_SUBSECTION    = 0x05
485    QEMU_VM_VMDESCRIPTION = 0x06
486    QEMU_VM_CONFIGURATION = 0x07
487    QEMU_VM_SECTION_FOOTER= 0x7e
488
489    def __init__(self, filename):
490        self.section_classes = { ( 'ram', 0 ) : [ RamSection, None ],
491                                 ( 'spapr/htab', 0) : ( HTABSection, None ) }
492        self.filename = filename
493        self.vmsd_desc = None
494
495    def read(self, desc_only = False, dump_memory = False, write_memory = False):
496        # Read in the whole file
497        file = MigrationFile(self.filename)
498
499        # File magic
500        data = file.read32()
501        if data != self.QEMU_VM_FILE_MAGIC:
502            raise Exception("Invalid file magic %x" % data)
503
504        # Version (has to be v3)
505        data = file.read32()
506        if data != self.QEMU_VM_FILE_VERSION:
507            raise Exception("Invalid version number %d" % data)
508
509        self.load_vmsd_json(file)
510
511        # Read sections
512        self.sections = collections.OrderedDict()
513
514        if desc_only:
515            return
516
517        ramargs = {}
518        ramargs['page_size'] = self.vmsd_desc['page_size']
519        ramargs['dump_memory'] = dump_memory
520        ramargs['write_memory'] = write_memory
521        self.section_classes[('ram',0)][1] = ramargs
522
523        while True:
524            section_type = file.read8()
525            if section_type == self.QEMU_VM_EOF:
526                break
527            elif section_type == self.QEMU_VM_CONFIGURATION:
528                section = ConfigurationSection(file)
529                section.read()
530            elif section_type == self.QEMU_VM_SECTION_START or section_type == self.QEMU_VM_SECTION_FULL:
531                section_id = file.read32()
532                name = file.readstr()
533                instance_id = file.read32()
534                version_id = file.read32()
535                section_key = (name, instance_id)
536                classdesc = self.section_classes[section_key]
537                section = classdesc[0](file, version_id, classdesc[1], section_key)
538                self.sections[section_id] = section
539                section.read()
540            elif section_type == self.QEMU_VM_SECTION_PART or section_type == self.QEMU_VM_SECTION_END:
541                section_id = file.read32()
542                self.sections[section_id].read()
543            elif section_type == self.QEMU_VM_SECTION_FOOTER:
544                read_section_id = file.read32()
545                if read_section_id != section_id:
546                    raise Exception("Mismatched section footer: %x vs %x" % (read_section_id, section_id))
547            else:
548                raise Exception("Unknown section type: %d" % section_type)
549        file.close()
550
551    def load_vmsd_json(self, file):
552        vmsd_json = file.read_migration_debug_json()
553        self.vmsd_desc = json.loads(vmsd_json, object_pairs_hook=collections.OrderedDict)
554        for device in self.vmsd_desc['devices']:
555            key = (device['name'], device['instance_id'])
556            value = ( VMSDSection, device )
557            self.section_classes[key] = value
558
559    def getDict(self):
560        r = collections.OrderedDict()
561        for (key, value) in self.sections.items():
562           key = "%s (%d)" % ( value.section_key[0], key )
563           r[key] = value.getDict()
564        return r
565
566###############################################################################
567
568class JSONEncoder(json.JSONEncoder):
569    def default(self, o):
570        if isinstance(o, VMSDFieldGeneric):
571            return str(o)
572        return json.JSONEncoder.default(self, o)
573
574parser = argparse.ArgumentParser()
575parser.add_argument("-f", "--file", help='migration dump to read from', required=True)
576parser.add_argument("-m", "--memory", help='dump RAM contents as well', action='store_true')
577parser.add_argument("-d", "--dump", help='what to dump ("state" or "desc")', default='state')
578parser.add_argument("-x", "--extract", help='extract contents into individual files', action='store_true')
579args = parser.parse_args()
580
581jsonenc = JSONEncoder(indent=4, separators=(',', ': '))
582
583if args.extract:
584    dump = MigrationDump(args.file)
585
586    dump.read(desc_only = True)
587    print "desc.json"
588    f = open("desc.json", "wb")
589    f.truncate()
590    f.write(jsonenc.encode(dump.vmsd_desc))
591    f.close()
592
593    dump.read(write_memory = True)
594    dict = dump.getDict()
595    print "state.json"
596    f = open("state.json", "wb")
597    f.truncate()
598    f.write(jsonenc.encode(dict))
599    f.close()
600elif args.dump == "state":
601    dump = MigrationDump(args.file)
602    dump.read(dump_memory = args.memory)
603    dict = dump.getDict()
604    print jsonenc.encode(dict)
605elif args.dump == "desc":
606    dump = MigrationDump(args.file)
607    dump.read(desc_only = True)
608    print jsonenc.encode(dump.vmsd_desc)
609else:
610    raise Exception("Please specify either -x, -d state or -d dump")
611