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 |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 23 | if hasattr(x, '__code__'): |
| 24 | x = x.__code__ |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 25 | if hasattr(x, '__dict__'): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 26 | items = sorted(x.__dict__.items()) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 27 | for name, x1 in items: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 28 | if isinstance(x1, (types.MethodType, types.FunctionType, |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 29 | types.CodeType, type)): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 30 | print("Disassembly of %s:" % name) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 31 | try: |
| 32 | dis(x1) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 33 | except TypeError as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 34 | print("Sorry:", msg) |
| 35 | print() |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 36 | elif hasattr(x, 'co_code'): |
| 37 | disassemble(x) |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 38 | elif isinstance(x, str): |
| 39 | disassemble_string(x) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 40 | else: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 41 | raise TypeError("don't know how to disassemble %s objects" % |
| 42 | type(x).__name__) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 44 | def distb(tb=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 45 | """Disassemble a traceback (default: last traceback).""" |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 46 | if tb is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 47 | try: |
| 48 | tb = sys.last_traceback |
| 49 | except AttributeError: |
| 50 | raise RuntimeError, "no last traceback to disassemble" |
| 51 | while tb.tb_next: tb = tb.tb_next |
| 52 | disassemble(tb.tb_frame.f_code, tb.tb_lasti) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 54 | # XXX This duplicates information from code.h, also duplicated in inspect.py. |
| 55 | # XXX Maybe this ought to be put in a central location, like opcode.py? |
| 56 | flag2name = { |
| 57 | 1: "OPTIMIZED", |
| 58 | 2: "NEWLOCALS", |
| 59 | 4: "VARARGS", |
| 60 | 8: "VARKEYWORDS", |
| 61 | 16: "NESTED", |
| 62 | 32: "GENERATOR", |
| 63 | 64: "NOFREE", |
| 64 | } |
| 65 | |
| 66 | def pretty_flags(flags): |
| 67 | """Return pretty representation of code flags.""" |
| 68 | names = [] |
| 69 | for i in range(32): |
| 70 | flag = 1<<i |
| 71 | if flags & flag: |
| 72 | names.append(flag2name.get(flag, hex(flag))) |
| 73 | flags ^= flag |
| 74 | if not flags: |
| 75 | break |
| 76 | else: |
| 77 | names.append(hex(flags)) |
| 78 | return ", ".join(names) |
| 79 | |
| 80 | def show_code(co): |
| 81 | """Show details about a code object.""" |
| 82 | print("Name: ", co.co_name) |
| 83 | print("Filename: ", co.co_filename) |
| 84 | print("Argument count: ", co.co_argcount) |
| 85 | print("Kw-only arguments:", co.co_kwonlyargcount) |
| 86 | print("Number of locals: ", co.co_nlocals) |
| 87 | print("Stack size: ", co.co_stacksize) |
| 88 | print("Flags: ", pretty_flags(co.co_flags)) |
| 89 | if co.co_consts: |
| 90 | print("Constants:") |
| 91 | for i_c in enumerate(co.co_consts): |
| 92 | print("%4d: %r" % i_c) |
| 93 | if co.co_names: |
| 94 | print("Names:") |
| 95 | for i_n in enumerate(co.co_names): |
| 96 | print("%4d: %s" % i_n) |
| 97 | if co.co_varnames: |
| 98 | print("Variable names:") |
| 99 | for i_n in enumerate(co.co_varnames): |
| 100 | print("%4d: %s" % i_n) |
| 101 | if co.co_freevars: |
| 102 | print("Free variables:") |
| 103 | for i_n in enumerate(co.co_freevars): |
| 104 | print("%4d: %s" % i_n) |
| 105 | if co.co_cellvars: |
| 106 | print("Cell variables:") |
| 107 | for i_n in enumerate(co.co_cellvars): |
| 108 | print("%4d: %s" % i_n) |
| 109 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 110 | def disassemble(co, lasti=-1): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 111 | """Disassemble a code object.""" |
| 112 | code = co.co_code |
| 113 | labels = findlabels(code) |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 114 | linestarts = dict(findlinestarts(co)) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 115 | n = len(code) |
| 116 | i = 0 |
| 117 | extended_arg = 0 |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 118 | free = None |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 119 | while i < n: |
| 120 | c = code[i] |
| 121 | op = ord(c) |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 122 | if i in linestarts: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 123 | if i > 0: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 124 | print() |
| 125 | print("%3d" % linestarts[i], end=' ') |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 126 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 127 | print(' ', end=' ') |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 129 | if i == lasti: print('-->', end=' ') |
| 130 | else: print(' ', end=' ') |
| 131 | if i in labels: print('>>', end=' ') |
| 132 | else: print(' ', end=' ') |
| 133 | print(repr(i).rjust(4), end=' ') |
| 134 | print(opname[op].ljust(20), end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 135 | i = i+1 |
| 136 | if op >= HAVE_ARGUMENT: |
| 137 | oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg |
| 138 | extended_arg = 0 |
| 139 | i = i+2 |
| 140 | if op == EXTENDED_ARG: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 141 | extended_arg = oparg*65536 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 142 | print(repr(oparg).rjust(5), end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 143 | if op in hasconst: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 144 | print('(' + repr(co.co_consts[oparg]) + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 145 | elif op in hasname: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 146 | print('(' + co.co_names[oparg] + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 147 | elif op in hasjrel: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 148 | print('(to ' + repr(i + oparg) + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 149 | elif op in haslocal: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 150 | print('(' + co.co_varnames[oparg] + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 151 | elif op in hascompare: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 152 | print('(' + cmp_op[oparg] + ')', end=' ') |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 153 | elif op in hasfree: |
| 154 | if free is None: |
| 155 | free = co.co_cellvars + co.co_freevars |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 156 | print('(' + free[oparg] + ')', end=' ') |
| 157 | print() |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 158 | |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 159 | def disassemble_string(code, lasti=-1, varnames=None, names=None, |
| 160 | constants=None): |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 161 | labels = findlabels(code) |
| 162 | n = len(code) |
| 163 | i = 0 |
| 164 | while i < n: |
| 165 | c = code[i] |
| 166 | op = ord(c) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 167 | if i == lasti: print('-->', end=' ') |
| 168 | else: print(' ', end=' ') |
| 169 | if i in labels: print('>>', end=' ') |
| 170 | else: print(' ', end=' ') |
| 171 | print(repr(i).rjust(4), end=' ') |
| 172 | print(opname[op].ljust(15), end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 173 | i = i+1 |
| 174 | if op >= HAVE_ARGUMENT: |
| 175 | oparg = ord(code[i]) + ord(code[i+1])*256 |
| 176 | i = i+2 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 177 | print(repr(oparg).rjust(5), end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 178 | if op in hasconst: |
| 179 | if constants: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 180 | print('(' + repr(constants[oparg]) + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 181 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 182 | print('(%d)'%oparg, end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 183 | elif op in hasname: |
| 184 | if names is not None: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 185 | print('(' + names[oparg] + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 186 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 187 | print('(%d)'%oparg, end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 188 | elif op in hasjrel: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 189 | print('(to ' + repr(i + oparg) + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 190 | elif op in haslocal: |
| 191 | if varnames: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 192 | print('(' + varnames[oparg] + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 193 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 194 | print('(%d)' % oparg, end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 195 | elif op in hascompare: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 196 | print('(' + cmp_op[oparg] + ')', end=' ') |
| 197 | print() |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 198 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 199 | disco = disassemble # XXX For backwards compatibility |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 201 | def findlabels(code): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 202 | """Detect all offsets in a byte code which are jump targets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 203 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 204 | Return the list of offsets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 205 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 206 | """ |
| 207 | labels = [] |
| 208 | n = len(code) |
| 209 | i = 0 |
| 210 | while i < n: |
| 211 | c = code[i] |
| 212 | op = ord(c) |
| 213 | i = i+1 |
| 214 | if op >= HAVE_ARGUMENT: |
| 215 | oparg = ord(code[i]) + ord(code[i+1])*256 |
| 216 | i = i+2 |
| 217 | label = -1 |
| 218 | if op in hasjrel: |
| 219 | label = i+oparg |
| 220 | elif op in hasjabs: |
| 221 | label = oparg |
| 222 | if label >= 0: |
| 223 | if label not in labels: |
| 224 | labels.append(label) |
| 225 | return labels |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 226 | |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 227 | def findlinestarts(code): |
| 228 | """Find the offsets in a byte code which are start of lines in the source. |
| 229 | |
| 230 | Generate pairs (offset, lineno) as described in Python/compile.c. |
| 231 | |
| 232 | """ |
| 233 | byte_increments = [ord(c) for c in code.co_lnotab[0::2]] |
| 234 | line_increments = [ord(c) for c in code.co_lnotab[1::2]] |
| 235 | |
| 236 | lastlineno = None |
| 237 | lineno = code.co_firstlineno |
| 238 | addr = 0 |
| 239 | for byte_incr, line_incr in zip(byte_increments, line_increments): |
| 240 | if byte_incr: |
| 241 | if lineno != lastlineno: |
| 242 | yield (addr, lineno) |
| 243 | lastlineno = lineno |
| 244 | addr += byte_incr |
| 245 | lineno += line_incr |
| 246 | if lineno != lastlineno: |
| 247 | yield (addr, lineno) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 248 | |
| 249 | def _test(): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 250 | """Simple test program to disassemble a file.""" |
| 251 | if sys.argv[1:]: |
| 252 | if sys.argv[2:]: |
| 253 | sys.stderr.write("usage: python dis.py [-|file]\n") |
| 254 | sys.exit(2) |
| 255 | fn = sys.argv[1] |
| 256 | if not fn or fn == "-": |
| 257 | fn = None |
| 258 | else: |
| 259 | fn = None |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 260 | if fn is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 261 | f = sys.stdin |
| 262 | else: |
| 263 | f = open(fn) |
| 264 | source = f.read() |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 265 | if fn is not None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 266 | f.close() |
| 267 | else: |
| 268 | fn = "<stdin>" |
| 269 | code = compile(source, fn, "exec") |
| 270 | dis(code) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 271 | |
| 272 | if __name__ == "__main__": |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 273 | _test() |