debug.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python2
  2. import struct
  3. import binascii
  4. TYPES = {
  5. (0x1ff, 0x001): 'reg',
  6. (0x1ff, 0x002): 'dir',
  7. (0x1ff, 0x011): 'superblock',
  8. (0x1ff, 0x010): 'root',
  9. (0x1ff, 0x030): 'delete',
  10. (0x1f0, 0x080): 'globals',
  11. (0x1ff, 0x0c0): 'tail soft',
  12. (0x1ff, 0x0c1): 'tail hard',
  13. (0x1f0, 0x0f0): 'crc',
  14. (0x1ff, 0x040): 'struct dir',
  15. (0x1ff, 0x041): 'struct inline',
  16. (0x1ff, 0x042): 'struct ctz',
  17. (0x100, 0x100): 'attr',
  18. }
  19. def typeof(type):
  20. for prefix in range(9):
  21. mask = 0x1ff & ~((1 << prefix)-1)
  22. if (mask, type & mask) in TYPES:
  23. return TYPES[mask, type & mask] + (
  24. ' [%0*x]' % (prefix/4, type & ((1 << prefix)-1))
  25. if prefix else '')
  26. else:
  27. return '[%02x]' % type
  28. def main(*blocks):
  29. # find most recent block
  30. file = None
  31. rev = None
  32. crc = None
  33. versions = []
  34. for block in blocks:
  35. try:
  36. nfile = open(block, 'rb')
  37. ndata = nfile.read(4)
  38. ncrc = binascii.crc32(ndata)
  39. nrev, = struct.unpack('<I', ndata)
  40. assert rev != nrev
  41. if not file or ((rev - nrev) & 0x80000000):
  42. file = nfile
  43. rev = nrev
  44. crc = ncrc
  45. versions.append((nrev, '%s (rev %d)' % (block, nrev)))
  46. except (IOError, struct.error):
  47. pass
  48. if not file:
  49. print 'Bad metadata pair {%s}' % ', '.join(blocks)
  50. return 1
  51. print "--- %s ---" % ', '.join(v for _,v in sorted(versions, reverse=True))
  52. # go through each tag, print useful information
  53. print "%-4s %-8s %-14s %3s %3s %s" % (
  54. 'off', 'tag', 'type', 'id', 'len', 'dump')
  55. tag = 0xffffffff
  56. off = 4
  57. while True:
  58. try:
  59. data = file.read(4)
  60. crc = binascii.crc32(data, crc)
  61. ntag, = struct.unpack('<I', data)
  62. except struct.error:
  63. break
  64. tag ^= ntag
  65. off += 4
  66. type = (tag & 0x7fc00000) >> 22
  67. id = (tag & 0x003ff000) >> 12
  68. size = (tag & 0x00000fff) >> 0
  69. iscrc = (type & 0x1f0) == 0x0f0
  70. data = file.read(size)
  71. if iscrc:
  72. crc = binascii.crc32(data[:4], crc)
  73. else:
  74. crc = binascii.crc32(data, crc)
  75. print '%04x: %08x %-14s %3s %3d %-23s %-8s' % (
  76. off, tag,
  77. typeof(type) + (' bad!' if iscrc and ~crc else ''),
  78. id if id != 0x3ff else '.', size,
  79. ' '.join('%02x' % ord(c) for c in data[:8]),
  80. ''.join(c if c >= ' ' and c <= '~' else '.' for c in data[:8]))
  81. off += tag & 0xfff
  82. if iscrc:
  83. crc = 0
  84. tag ^= (type & 1) << 31
  85. return 0
  86. if __name__ == "__main__":
  87. import sys
  88. sys.exit(main(*sys.argv[1:]))