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