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 |
| 4 | import string |
Guido van Rossum | 18aef3c | 1997-03-14 04:15:43 +0000 | [diff] [blame] | 5 | import types |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 7 | def dis(x=None): |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 8 | """Disassemble classes, methods, functions, or code. |
| 9 | |
| 10 | With no argument, disassemble the last traceback. |
| 11 | |
| 12 | """ |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 13 | if not x: |
| 14 | distb() |
Guido van Rossum | 18aef3c | 1997-03-14 04:15:43 +0000 | [diff] [blame] | 15 | return |
| 16 | if type(x) is types.InstanceType: |
| 17 | x = x.__class__ |
| 18 | if hasattr(x, '__dict__'): |
| 19 | items = x.__dict__.items() |
| 20 | items.sort() |
| 21 | for name, x1 in items: |
| 22 | if type(x1) in (types.MethodType, |
| 23 | types.FunctionType, |
| 24 | types.CodeType): |
| 25 | print "Disassembly of %s:" % name |
| 26 | try: |
| 27 | dis(x1) |
| 28 | except TypeError, msg: |
| 29 | print "Sorry:", msg |
| 30 | print |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 31 | else: |
| 32 | if hasattr(x, 'im_func'): |
| 33 | x = x.im_func |
| 34 | if hasattr(x, 'func_code'): |
| 35 | x = x.func_code |
| 36 | if hasattr(x, 'co_code'): |
| 37 | disassemble(x) |
| 38 | else: |
Guido van Rossum | 00f86e6 | 1997-01-17 20:08:18 +0000 | [diff] [blame] | 39 | raise TypeError, \ |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 40 | "don't know how to disassemble %s objects" % \ |
| 41 | type(x).__name__ |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 43 | def distb(tb=None): |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 44 | """Disassemble a traceback (default: last traceback).""" |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 45 | if not tb: |
| 46 | try: |
| 47 | tb = sys.last_traceback |
| 48 | except AttributeError: |
| 49 | raise RuntimeError, "no last traceback to disassemble" |
| 50 | while tb.tb_next: tb = tb.tb_next |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 51 | disassemble(tb.tb_frame.f_code, tb.tb_lasti) |
| 52 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 53 | def disassemble(co, lasti=-1): |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 54 | """Disassemble a code object.""" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 55 | code = co.co_code |
| 56 | labels = findlabels(code) |
| 57 | n = len(code) |
| 58 | i = 0 |
| 59 | while i < n: |
| 60 | c = code[i] |
| 61 | op = ord(c) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 62 | if op == SET_LINENO and i > 0: print # Extra blank line |
| 63 | if i == lasti: print '-->', |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 64 | else: print ' ', |
| 65 | if i in labels: print '>>', |
| 66 | else: print ' ', |
| 67 | print string.rjust(`i`, 4), |
Guido van Rossum | d30dedc | 2000-03-30 15:02:11 +0000 | [diff] [blame] | 68 | print string.ljust(opname[op], 20), |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 69 | i = i+1 |
| 70 | if op >= HAVE_ARGUMENT: |
| 71 | oparg = ord(code[i]) + ord(code[i+1])*256 |
| 72 | i = i+2 |
| 73 | print string.rjust(`oparg`, 5), |
| 74 | if op in hasconst: |
| 75 | print '(' + `co.co_consts[oparg]` + ')', |
| 76 | elif op in hasname: |
| 77 | print '(' + co.co_names[oparg] + ')', |
| 78 | elif op in hasjrel: |
| 79 | print '(to ' + `i + oparg` + ')', |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 80 | elif op in haslocal: |
| 81 | print '(' + co.co_varnames[oparg] + ')', |
Guido van Rossum | d0bc9cb | 1997-01-16 18:52:24 +0000 | [diff] [blame] | 82 | elif op in hascompare: |
| 83 | print '(' + cmp_op[oparg] + ')', |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 84 | print |
| 85 | |
Guido van Rossum | 1cc2b9d | 1999-05-03 18:09:53 +0000 | [diff] [blame] | 86 | disco = disassemble # XXX For backwards compatibility |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 88 | def findlabels(code): |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 89 | """Detect all offsets in a byte code which are jump targets. |
| 90 | |
| 91 | Return the list of offsets. |
| 92 | |
| 93 | """ |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 94 | labels = [] |
| 95 | n = len(code) |
| 96 | i = 0 |
| 97 | while i < n: |
| 98 | c = code[i] |
| 99 | op = ord(c) |
| 100 | i = i+1 |
| 101 | if op >= HAVE_ARGUMENT: |
| 102 | oparg = ord(code[i]) + ord(code[i+1])*256 |
| 103 | i = i+2 |
| 104 | label = -1 |
| 105 | if op in hasjrel: |
| 106 | label = i+oparg |
| 107 | elif op in hasjabs: |
| 108 | label = oparg |
| 109 | if label >= 0: |
| 110 | if label not in labels: |
| 111 | labels.append(label) |
| 112 | return labels |
| 113 | |
Guido van Rossum | d0bc9cb | 1997-01-16 18:52:24 +0000 | [diff] [blame] | 114 | cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', |
| 115 | 'is not', 'exception match', 'BAD') |
| 116 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 117 | hasconst = [] |
| 118 | hasname = [] |
| 119 | hasjrel = [] |
| 120 | hasjabs = [] |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 121 | haslocal = [] |
Guido van Rossum | d0bc9cb | 1997-01-16 18:52:24 +0000 | [diff] [blame] | 122 | hascompare = [] |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 124 | opname = [''] * 256 |
| 125 | for op in range(256): opname[op] = '<' + `op` + '>' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 126 | |
| 127 | def def_op(name, op): |
| 128 | opname[op] = name |
| 129 | |
| 130 | def name_op(name, op): |
| 131 | opname[op] = name |
| 132 | hasname.append(op) |
| 133 | |
| 134 | def jrel_op(name, op): |
| 135 | opname[op] = name |
| 136 | hasjrel.append(op) |
| 137 | |
| 138 | def jabs_op(name, op): |
| 139 | opname[op] = name |
| 140 | hasjabs.append(op) |
| 141 | |
| 142 | # Instruction opcodes for compiled code |
| 143 | |
| 144 | def_op('STOP_CODE', 0) |
| 145 | def_op('POP_TOP', 1) |
| 146 | def_op('ROT_TWO', 2) |
| 147 | def_op('ROT_THREE', 3) |
| 148 | def_op('DUP_TOP', 4) |
| 149 | |
| 150 | def_op('UNARY_POSITIVE', 10) |
| 151 | def_op('UNARY_NEGATIVE', 11) |
| 152 | def_op('UNARY_NOT', 12) |
| 153 | def_op('UNARY_CONVERT', 13) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | 8379ed5 | 1993-03-30 19:13:03 +0000 | [diff] [blame] | 155 | def_op('UNARY_INVERT', 15) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 6e21ceb | 1996-07-21 02:16:53 +0000 | [diff] [blame] | 157 | def_op('BINARY_POWER', 19) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 159 | def_op('BINARY_MULTIPLY', 20) |
| 160 | def_op('BINARY_DIVIDE', 21) |
| 161 | def_op('BINARY_MODULO', 22) |
| 162 | def_op('BINARY_ADD', 23) |
| 163 | def_op('BINARY_SUBTRACT', 24) |
| 164 | def_op('BINARY_SUBSCR', 25) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 165 | |
| 166 | def_op('SLICE+0', 30) |
| 167 | def_op('SLICE+1', 31) |
| 168 | def_op('SLICE+2', 32) |
| 169 | def_op('SLICE+3', 33) |
| 170 | |
| 171 | def_op('STORE_SLICE+0', 40) |
| 172 | def_op('STORE_SLICE+1', 41) |
| 173 | def_op('STORE_SLICE+2', 42) |
| 174 | def_op('STORE_SLICE+3', 43) |
| 175 | |
| 176 | def_op('DELETE_SLICE+0', 50) |
| 177 | def_op('DELETE_SLICE+1', 51) |
| 178 | def_op('DELETE_SLICE+2', 52) |
| 179 | def_op('DELETE_SLICE+3', 53) |
| 180 | |
| 181 | def_op('STORE_SUBSCR', 60) |
| 182 | def_op('DELETE_SUBSCR', 61) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | d0bc9cb | 1997-01-16 18:52:24 +0000 | [diff] [blame] | 184 | def_op('BINARY_LSHIFT', 62) |
| 185 | def_op('BINARY_RSHIFT', 63) |
| 186 | def_op('BINARY_AND', 64) |
| 187 | def_op('BINARY_XOR', 65) |
| 188 | def_op('BINARY_OR', 66) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 189 | |
| 190 | def_op('PRINT_EXPR', 70) |
| 191 | def_op('PRINT_ITEM', 71) |
| 192 | def_op('PRINT_NEWLINE', 72) |
| 193 | |
| 194 | def_op('BREAK_LOOP', 80) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 196 | def_op('LOAD_LOCALS', 82) |
| 197 | def_op('RETURN_VALUE', 83) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | def_op('EXEC_STMT', 85) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 201 | def_op('POP_BLOCK', 87) |
| 202 | def_op('END_FINALLY', 88) |
| 203 | def_op('BUILD_CLASS', 89) |
| 204 | |
| 205 | HAVE_ARGUMENT = 90 # Opcodes from here have an argument: |
| 206 | |
| 207 | name_op('STORE_NAME', 90) # Index in name list |
| 208 | name_op('DELETE_NAME', 91) # "" |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 209 | def_op('UNPACK_SEQUENCE', 92) # Number of tuple items |
| 210 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 211 | name_op('STORE_ATTR', 95) # Index in name list |
Guido van Rossum | a594fab | 1991-12-16 13:09:28 +0000 | [diff] [blame] | 212 | name_op('DELETE_ATTR', 96) # "" |
| 213 | name_op('STORE_GLOBAL', 97) # "" |
| 214 | name_op('DELETE_GLOBAL', 98) # "" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 215 | |
| 216 | def_op('LOAD_CONST', 100) # Index in const list |
| 217 | hasconst.append(100) |
| 218 | name_op('LOAD_NAME', 101) # Index in name list |
| 219 | def_op('BUILD_TUPLE', 102) # Number of tuple items |
| 220 | def_op('BUILD_LIST', 103) # Number of list items |
| 221 | def_op('BUILD_MAP', 104) # Always zero for now |
| 222 | name_op('LOAD_ATTR', 105) # Index in name list |
| 223 | def_op('COMPARE_OP', 106) # Comparison operator |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 224 | hascompare.append(106) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 225 | name_op('IMPORT_NAME', 107) # Index in name list |
| 226 | name_op('IMPORT_FROM', 108) # Index in name list |
| 227 | |
| 228 | jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip |
| 229 | jrel_op('JUMP_IF_FALSE', 111) # "" |
| 230 | jrel_op('JUMP_IF_TRUE', 112) # "" |
| 231 | jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code |
| 232 | jrel_op('FOR_LOOP', 114) # Number of bytes to skip |
| 233 | |
Guido van Rossum | a594fab | 1991-12-16 13:09:28 +0000 | [diff] [blame] | 234 | name_op('LOAD_GLOBAL', 116) # Index in name list |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 236 | jrel_op('SETUP_LOOP', 120) # Distance to target address |
| 237 | jrel_op('SETUP_EXCEPT', 121) # "" |
| 238 | jrel_op('SETUP_FINALLY', 122) # "" |
| 239 | |
Guido van Rossum | 8379ed5 | 1993-03-30 19:13:03 +0000 | [diff] [blame] | 240 | def_op('LOAD_FAST', 124) # Local variable number |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 241 | haslocal.append(124) |
Guido van Rossum | 8379ed5 | 1993-03-30 19:13:03 +0000 | [diff] [blame] | 242 | def_op('STORE_FAST', 125) # Local variable number |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 243 | haslocal.append(125) |
Guido van Rossum | 8379ed5 | 1993-03-30 19:13:03 +0000 | [diff] [blame] | 244 | def_op('DELETE_FAST', 126) # Local variable number |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 245 | haslocal.append(126) |
Guido van Rossum | 8379ed5 | 1993-03-30 19:13:03 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 247 | def_op('SET_LINENO', 127) # Current line number |
| 248 | SET_LINENO = 127 |
Guido van Rossum | 6e21ceb | 1996-07-21 02:16:53 +0000 | [diff] [blame] | 249 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 250 | def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3) |
| 251 | def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8) |
| 252 | def_op('MAKE_FUNCTION', 132) # Number of args with default values |
| 253 | def_op('BUILD_SLICE', 133) # Number of items |
| 254 | |
| 255 | def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8) |
| 256 | def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8) |
| 257 | def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 258 | |
| 259 | |
| 260 | def _test(): |
| 261 | """Simple test program to disassemble a file.""" |
| 262 | if sys.argv[1:]: |
| 263 | if sys.argv[2:]: |
| 264 | sys.stderr.write("usage: python dis.py [-|file]\n") |
| 265 | sys.exit(2) |
| 266 | fn = sys.argv[1] |
| 267 | if not fn or fn == "-": |
| 268 | fn = None |
| 269 | else: |
| 270 | fn = None |
| 271 | if not fn: |
| 272 | f = sys.stdin |
| 273 | else: |
| 274 | f = open(fn) |
| 275 | source = f.read() |
| 276 | if fn: |
| 277 | f.close() |
| 278 | else: |
| 279 | fn = "<stdin>" |
| 280 | code = compile(source, fn, "exec") |
| 281 | dis(code) |
| 282 | |
| 283 | if __name__ == "__main__": |
| 284 | _test() |