2
0

analyze-migration.py 20 KB

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