Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 1 | """Disassembler of Python byte code into mnemonics.""" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 2 | |
| 3 | import sys |
Guido van Rossum | 18aef3c | 1997-03-14 04:15:43 +0000 | [diff] [blame] | 4 | import types |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 5 | |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 6 | from opcode import * |
| 7 | from opcode import __all__ as _opcodes_all |
| 8 | |
| 9 | __all__ = ["dis","disassemble","distb","disco"] + _opcodes_all |
| 10 | del _opcodes_all |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 12 | def dis(x=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 13 | """Disassemble classes, methods, functions, or code. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 14 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 15 | With no argument, disassemble the last traceback. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 16 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 17 | """ |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 18 | if x is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 19 | distb() |
| 20 | return |
| 21 | if type(x) is types.InstanceType: |
| 22 | x = x.__class__ |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 23 | if hasattr(x, 'im_func'): |
| 24 | x = x.im_func |
| 25 | if hasattr(x, 'func_code'): |
| 26 | x = x.func_code |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 27 | if hasattr(x, '__dict__'): |
| 28 | items = x.__dict__.items() |
| 29 | items.sort() |
| 30 | for name, x1 in items: |
| 31 | if type(x1) in (types.MethodType, |
| 32 | types.FunctionType, |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 33 | types.CodeType, |
| 34 | types.ClassType): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 35 | print "Disassembly of %s:" % name |
| 36 | try: |
| 37 | dis(x1) |
| 38 | except TypeError, msg: |
| 39 | print "Sorry:", msg |
| 40 | print |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 41 | elif hasattr(x, 'co_code'): |
| 42 | disassemble(x) |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 43 | elif isinstance(x, str): |
| 44 | disassemble_string(x) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 45 | else: |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 46 | raise TypeError, \ |
| 47 | "don't know how to disassemble %s objects" % \ |
| 48 | type(x).__name__ |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 50 | def distb(tb=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 51 | """Disassemble a traceback (default: last traceback).""" |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 52 | if tb is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 53 | try: |
| 54 | tb = sys.last_traceback |
| 55 | except AttributeError: |
| 56 | raise RuntimeError, "no last traceback to disassemble" |
| 57 | while tb.tb_next: tb = tb.tb_next |
| 58 | disassemble(tb.tb_frame.f_code, tb.tb_lasti) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 60 | def disassemble(co, lasti=-1): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 61 | """Disassemble a code object.""" |
| 62 | code = co.co_code |
| 63 | labels = findlabels(code) |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 64 | linestarts = dict(findlinestarts(co)) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 65 | n = len(code) |
| 66 | i = 0 |
| 67 | extended_arg = 0 |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 68 | free = None |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 69 | while i < n: |
| 70 | c = code[i] |
| 71 | op = ord(c) |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 72 | if i in linestarts: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 73 | if i > 0: |
| 74 | print |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 75 | print "%3d" % linestarts[i], |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 76 | else: |
| 77 | print ' ', |
| 78 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 79 | if i == lasti: print '-->', |
| 80 | else: print ' ', |
| 81 | if i in labels: print '>>', |
| 82 | else: print ' ', |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 83 | print repr(i).rjust(4), |
Eric S. Raymond | 373c55e | 2001-02-09 08:25:29 +0000 | [diff] [blame] | 84 | print opname[op].ljust(20), |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 85 | i = i+1 |
| 86 | if op >= HAVE_ARGUMENT: |
| 87 | oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg |
| 88 | extended_arg = 0 |
| 89 | i = i+2 |
| 90 | if op == EXTENDED_ARG: |
| 91 | extended_arg = oparg*65536L |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 92 | print repr(oparg).rjust(5), |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 93 | if op in hasconst: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 94 | print '(' + repr(co.co_consts[oparg]) + ')', |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 95 | elif op in hasname: |
| 96 | print '(' + co.co_names[oparg] + ')', |
| 97 | elif op in hasjrel: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 98 | print '(to ' + repr(i + oparg) + ')', |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 99 | elif op in haslocal: |
| 100 | print '(' + co.co_varnames[oparg] + ')', |
| 101 | elif op in hascompare: |
| 102 | print '(' + cmp_op[oparg] + ')', |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 103 | elif op in hasfree: |
| 104 | if free is None: |
| 105 | free = co.co_cellvars + co.co_freevars |
| 106 | print '(' + free[oparg] + ')', |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 107 | print |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 108 | |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 109 | def disassemble_string(code, lasti=-1, varnames=None, names=None, |
| 110 | constants=None): |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 111 | labels = findlabels(code) |
| 112 | n = len(code) |
| 113 | i = 0 |
| 114 | while i < n: |
| 115 | c = code[i] |
| 116 | op = ord(c) |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 117 | if i == lasti: print '-->', |
| 118 | else: print ' ', |
| 119 | if i in labels: print '>>', |
| 120 | else: print ' ', |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 121 | print repr(i).rjust(4), |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 122 | print opname[op].ljust(15), |
| 123 | i = i+1 |
| 124 | if op >= HAVE_ARGUMENT: |
| 125 | oparg = ord(code[i]) + ord(code[i+1])*256 |
| 126 | i = i+2 |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 127 | print repr(oparg).rjust(5), |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 128 | if op in hasconst: |
| 129 | if constants: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 130 | print '(' + repr(constants[oparg]) + ')', |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 131 | else: |
| 132 | print '(%d)'%oparg, |
| 133 | elif op in hasname: |
| 134 | if names is not None: |
| 135 | print '(' + names[oparg] + ')', |
| 136 | else: |
| 137 | print '(%d)'%oparg, |
| 138 | elif op in hasjrel: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 139 | print '(to ' + repr(i + oparg) + ')', |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 140 | elif op in haslocal: |
| 141 | if varnames: |
| 142 | print '(' + varnames[oparg] + ')', |
| 143 | else: |
| 144 | print '(%d)' % oparg, |
| 145 | elif op in hascompare: |
| 146 | print '(' + cmp_op[oparg] + ')', |
| 147 | print |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 148 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 149 | disco = disassemble # XXX For backwards compatibility |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 151 | def findlabels(code): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 152 | """Detect all offsets in a byte code which are jump targets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 153 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 154 | Return the list of offsets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 155 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 156 | """ |
| 157 | labels = [] |
| 158 | n = len(code) |
| 159 | i = 0 |
| 160 | while i < n: |
| 161 | c = code[i] |
| 162 | op = ord(c) |
| 163 | i = i+1 |
| 164 | if op >= HAVE_ARGUMENT: |
| 165 | oparg = ord(code[i]) + ord(code[i+1])*256 |
| 166 | i = i+2 |
| 167 | label = -1 |
| 168 | if op in hasjrel: |
| 169 | label = i+oparg |
| 170 | elif op in hasjabs: |
| 171 | label = oparg |
| 172 | if label >= 0: |
| 173 | if label not in labels: |
| 174 | labels.append(label) |
| 175 | return labels |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 176 | |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 177 | def findlinestarts(code): |
| 178 | """Find the offsets in a byte code which are start of lines in the source. |
| 179 | |
| 180 | Generate pairs (offset, lineno) as described in Python/compile.c. |
| 181 | |
| 182 | """ |
| 183 | byte_increments = [ord(c) for c in code.co_lnotab[0::2]] |
| 184 | line_increments = [ord(c) for c in code.co_lnotab[1::2]] |
| 185 | |
| 186 | lastlineno = None |
| 187 | lineno = code.co_firstlineno |
| 188 | addr = 0 |
| 189 | for byte_incr, line_incr in zip(byte_increments, line_increments): |
| 190 | if byte_incr: |
| 191 | if lineno != lastlineno: |
| 192 | yield (addr, lineno) |
| 193 | lastlineno = lineno |
| 194 | addr += byte_incr |
| 195 | lineno += line_incr |
| 196 | if lineno != lastlineno: |
| 197 | yield (addr, lineno) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 198 | |
| 199 | def _test(): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 200 | """Simple test program to disassemble a file.""" |
| 201 | if sys.argv[1:]: |
| 202 | if sys.argv[2:]: |
| 203 | sys.stderr.write("usage: python dis.py [-|file]\n") |
| 204 | sys.exit(2) |
| 205 | fn = sys.argv[1] |
| 206 | if not fn or fn == "-": |
| 207 | fn = None |
| 208 | else: |
| 209 | fn = None |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 210 | if fn is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 211 | f = sys.stdin |
| 212 | else: |
| 213 | f = open(fn) |
| 214 | source = f.read() |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 215 | if fn is not None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 216 | f.close() |
| 217 | else: |
| 218 | fn = "<stdin>" |
| 219 | code = compile(source, fn, "exec") |
| 220 | dis(code) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 221 | |
| 222 | if __name__ == "__main__": |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 223 | _test() |