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 |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 5 | import collections |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 6 | import io |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 7 | |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 8 | from opcode import * |
| 9 | from opcode import __all__ as _opcodes_all |
| 10 | |
Nick Coghlan | 7646f7e | 2010-09-10 12:24:24 +0000 | [diff] [blame] | 11 | __all__ = ["code_info", "dis", "disassemble", "distb", "disco", |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 12 | "findlinestarts", "findlabels", "show_code", |
| 13 | "get_instructions", "Instruction", "Bytecode"] + _opcodes_all |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 14 | del _opcodes_all |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 15 | |
Benjamin Peterson | 6ef9a84 | 2010-04-04 23:26:50 +0000 | [diff] [blame] | 16 | _have_code = (types.MethodType, types.FunctionType, types.CodeType, type) |
| 17 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 18 | def _try_compile(source, name): |
| 19 | """Attempts to compile the given source, first as an expression and |
| 20 | then as a statement if the first approach fails. |
| 21 | |
| 22 | Utility function to accept strings in functions that otherwise |
| 23 | expect code objects |
| 24 | """ |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 25 | try: |
| 26 | c = compile(source, name, 'eval') |
| 27 | except SyntaxError: |
| 28 | c = compile(source, name, 'exec') |
| 29 | return c |
| 30 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 31 | def dis(x=None, *, file=None): |
Nick Coghlan | efd5df9 | 2014-07-25 23:02:56 +1000 | [diff] [blame] | 32 | """Disassemble classes, methods, functions, generators, or code. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 33 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 34 | With no argument, disassemble the last traceback. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 35 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 36 | """ |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 37 | if x is None: |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 38 | distb(file=file) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 39 | return |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 40 | if hasattr(x, '__func__'): # Method |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 41 | x = x.__func__ |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 42 | if hasattr(x, '__code__'): # Function |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 43 | x = x.__code__ |
Nick Coghlan | efd5df9 | 2014-07-25 23:02:56 +1000 | [diff] [blame] | 44 | if hasattr(x, 'gi_code'): # Generator |
| 45 | x = x.gi_code |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 46 | if hasattr(x, '__dict__'): # Class or module |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 47 | items = sorted(x.__dict__.items()) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 48 | for name, x1 in items: |
Benjamin Peterson | 6ef9a84 | 2010-04-04 23:26:50 +0000 | [diff] [blame] | 49 | if isinstance(x1, _have_code): |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 50 | print("Disassembly of %s:" % name, file=file) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 51 | try: |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 52 | dis(x1, file=file) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 53 | except TypeError as msg: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 54 | print("Sorry:", msg, file=file) |
| 55 | print(file=file) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 56 | elif hasattr(x, 'co_code'): # Code object |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 57 | disassemble(x, file=file) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 58 | elif isinstance(x, (bytes, bytearray)): # Raw bytecode |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 59 | _disassemble_bytes(x, file=file) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 60 | elif isinstance(x, str): # Source code |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 61 | _disassemble_str(x, file=file) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 62 | else: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 63 | raise TypeError("don't know how to disassemble %s objects" % |
| 64 | type(x).__name__) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 65 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 66 | def distb(tb=None, *, file=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 67 | """Disassemble a traceback (default: last traceback).""" |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 68 | if tb is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 69 | try: |
| 70 | tb = sys.last_traceback |
| 71 | except AttributeError: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 72 | raise RuntimeError("no last traceback to disassemble") |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 73 | while tb.tb_next: tb = tb.tb_next |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 74 | disassemble(tb.tb_frame.f_code, tb.tb_lasti, file=file) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 75 | |
Nick Coghlan | 09c8123 | 2010-08-17 10:18:16 +0000 | [diff] [blame] | 76 | # The inspect module interrogates this dictionary to build its |
| 77 | # list of CO_* constants. It is also used by pretty_flags to |
| 78 | # turn the co_flags field into a human readable list. |
| 79 | COMPILER_FLAG_NAMES = { |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 80 | 1: "OPTIMIZED", |
| 81 | 2: "NEWLOCALS", |
| 82 | 4: "VARARGS", |
| 83 | 8: "VARKEYWORDS", |
| 84 | 16: "NESTED", |
| 85 | 32: "GENERATOR", |
| 86 | 64: "NOFREE", |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 87 | 128: "COROUTINE", |
| 88 | 256: "ITERABLE_COROUTINE", |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | def pretty_flags(flags): |
| 92 | """Return pretty representation of code flags.""" |
| 93 | names = [] |
| 94 | for i in range(32): |
| 95 | flag = 1<<i |
| 96 | if flags & flag: |
Nick Coghlan | 09c8123 | 2010-08-17 10:18:16 +0000 | [diff] [blame] | 97 | names.append(COMPILER_FLAG_NAMES.get(flag, hex(flag))) |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 98 | flags ^= flag |
| 99 | if not flags: |
| 100 | break |
| 101 | else: |
| 102 | names.append(hex(flags)) |
| 103 | return ", ".join(names) |
| 104 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 105 | def _get_code_object(x): |
Nick Coghlan | efd5df9 | 2014-07-25 23:02:56 +1000 | [diff] [blame] | 106 | """Helper to handle methods, functions, generators, strings and raw code objects""" |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 107 | if hasattr(x, '__func__'): # Method |
| 108 | x = x.__func__ |
| 109 | if hasattr(x, '__code__'): # Function |
| 110 | x = x.__code__ |
Nick Coghlan | efd5df9 | 2014-07-25 23:02:56 +1000 | [diff] [blame] | 111 | if hasattr(x, 'gi_code'): # Generator |
| 112 | x = x.gi_code |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 113 | if isinstance(x, str): # Source code |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 114 | x = _try_compile(x, "<disassembly>") |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 115 | if hasattr(x, 'co_code'): # Code object |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 116 | return x |
| 117 | raise TypeError("don't know how to disassemble %s objects" % |
| 118 | type(x).__name__) |
| 119 | |
| 120 | def code_info(x): |
| 121 | """Formatted details of methods, functions, or code.""" |
| 122 | return _format_code_info(_get_code_object(x)) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 123 | |
| 124 | def _format_code_info(co): |
| 125 | lines = [] |
| 126 | lines.append("Name: %s" % co.co_name) |
| 127 | lines.append("Filename: %s" % co.co_filename) |
| 128 | lines.append("Argument count: %s" % co.co_argcount) |
| 129 | lines.append("Kw-only arguments: %s" % co.co_kwonlyargcount) |
| 130 | lines.append("Number of locals: %s" % co.co_nlocals) |
| 131 | lines.append("Stack size: %s" % co.co_stacksize) |
| 132 | lines.append("Flags: %s" % pretty_flags(co.co_flags)) |
| 133 | if co.co_consts: |
| 134 | lines.append("Constants:") |
| 135 | for i_c in enumerate(co.co_consts): |
| 136 | lines.append("%4d: %r" % i_c) |
| 137 | if co.co_names: |
| 138 | lines.append("Names:") |
| 139 | for i_n in enumerate(co.co_names): |
| 140 | lines.append("%4d: %s" % i_n) |
| 141 | if co.co_varnames: |
| 142 | lines.append("Variable names:") |
| 143 | for i_n in enumerate(co.co_varnames): |
| 144 | lines.append("%4d: %s" % i_n) |
| 145 | if co.co_freevars: |
| 146 | lines.append("Free variables:") |
| 147 | for i_n in enumerate(co.co_freevars): |
| 148 | lines.append("%4d: %s" % i_n) |
| 149 | if co.co_cellvars: |
| 150 | lines.append("Cell variables:") |
| 151 | for i_n in enumerate(co.co_cellvars): |
| 152 | lines.append("%4d: %s" % i_n) |
| 153 | return "\n".join(lines) |
| 154 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 155 | def show_code(co, *, file=None): |
Ezio Melotti | 6e6c6ac | 2013-08-23 22:41:39 +0300 | [diff] [blame] | 156 | """Print details of methods, functions, or code to *file*. |
| 157 | |
| 158 | If *file* is not provided, the output is printed on stdout. |
| 159 | """ |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 160 | print(code_info(co), file=file) |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 161 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 162 | _Instruction = collections.namedtuple("_Instruction", |
| 163 | "opname opcode arg argval argrepr offset starts_line is_jump_target") |
| 164 | |
| 165 | class Instruction(_Instruction): |
| 166 | """Details for a bytecode operation |
| 167 | |
| 168 | Defined fields: |
| 169 | opname - human readable name for operation |
| 170 | opcode - numeric code for operation |
| 171 | arg - numeric argument to operation (if any), otherwise None |
| 172 | argval - resolved arg value (if known), otherwise same as arg |
| 173 | argrepr - human readable description of operation argument |
| 174 | offset - start index of operation within bytecode sequence |
| 175 | starts_line - line started by this opcode (if any), otherwise None |
| 176 | is_jump_target - True if other code jumps to here, otherwise False |
| 177 | """ |
| 178 | |
| 179 | def _disassemble(self, lineno_width=3, mark_as_current=False): |
| 180 | """Format instruction details for inclusion in disassembly output |
| 181 | |
| 182 | *lineno_width* sets the width of the line number field (0 omits it) |
| 183 | *mark_as_current* inserts a '-->' marker arrow as part of the line |
| 184 | """ |
| 185 | fields = [] |
| 186 | # Column: Source code line number |
| 187 | if lineno_width: |
| 188 | if self.starts_line is not None: |
| 189 | lineno_fmt = "%%%dd" % lineno_width |
| 190 | fields.append(lineno_fmt % self.starts_line) |
| 191 | else: |
| 192 | fields.append(' ' * lineno_width) |
| 193 | # Column: Current instruction indicator |
| 194 | if mark_as_current: |
| 195 | fields.append('-->') |
| 196 | else: |
| 197 | fields.append(' ') |
| 198 | # Column: Jump target marker |
| 199 | if self.is_jump_target: |
| 200 | fields.append('>>') |
| 201 | else: |
| 202 | fields.append(' ') |
| 203 | # Column: Instruction offset from start of code sequence |
| 204 | fields.append(repr(self.offset).rjust(4)) |
| 205 | # Column: Opcode name |
| 206 | fields.append(self.opname.ljust(20)) |
| 207 | # Column: Opcode argument |
| 208 | if self.arg is not None: |
| 209 | fields.append(repr(self.arg).rjust(5)) |
| 210 | # Column: Opcode argument details |
| 211 | if self.argrepr: |
| 212 | fields.append('(' + self.argrepr + ')') |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 213 | return ' '.join(fields).rstrip() |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 214 | |
| 215 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 216 | def get_instructions(x, *, first_line=None): |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 217 | """Iterator for the opcodes in methods, functions or code |
| 218 | |
| 219 | Generates a series of Instruction named tuples giving the details of |
| 220 | each operations in the supplied code. |
| 221 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 222 | If *first_line* is not None, it indicates the line number that should |
| 223 | be reported for the first source line in the disassembled code. |
| 224 | Otherwise, the source line information (if any) is taken directly from |
| 225 | the disassembled code object. |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 226 | """ |
| 227 | co = _get_code_object(x) |
| 228 | cell_names = co.co_cellvars + co.co_freevars |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 229 | linestarts = dict(findlinestarts(co)) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 230 | if first_line is not None: |
| 231 | line_offset = first_line - co.co_firstlineno |
| 232 | else: |
| 233 | line_offset = 0 |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 234 | return _get_instructions_bytes(co.co_code, co.co_varnames, co.co_names, |
| 235 | co.co_consts, cell_names, linestarts, |
| 236 | line_offset) |
| 237 | |
| 238 | def _get_const_info(const_index, const_list): |
| 239 | """Helper to get optional details about const references |
| 240 | |
| 241 | Returns the dereferenced constant and its repr if the constant |
| 242 | list is defined. |
| 243 | Otherwise returns the constant index and its repr(). |
| 244 | """ |
| 245 | argval = const_index |
| 246 | if const_list is not None: |
| 247 | argval = const_list[const_index] |
| 248 | return argval, repr(argval) |
| 249 | |
| 250 | def _get_name_info(name_index, name_list): |
| 251 | """Helper to get optional details about named references |
| 252 | |
| 253 | Returns the dereferenced name as both value and repr if the name |
| 254 | list is defined. |
| 255 | Otherwise returns the name index and its repr(). |
| 256 | """ |
| 257 | argval = name_index |
| 258 | if name_list is not None: |
| 259 | argval = name_list[name_index] |
| 260 | argrepr = argval |
| 261 | else: |
| 262 | argrepr = repr(argval) |
| 263 | return argval, argrepr |
| 264 | |
| 265 | |
| 266 | def _get_instructions_bytes(code, varnames=None, names=None, constants=None, |
| 267 | cells=None, linestarts=None, line_offset=0): |
| 268 | """Iterate over the instructions in a bytecode string. |
| 269 | |
| 270 | Generates a sequence of Instruction namedtuples giving the details of each |
| 271 | opcode. Additional information about the code's runtime environment |
| 272 | (e.g. variable names, constants) can be specified using optional |
| 273 | arguments. |
| 274 | |
| 275 | """ |
| 276 | labels = findlabels(code) |
| 277 | extended_arg = 0 |
| 278 | starts_line = None |
| 279 | free = None |
| 280 | # enumerate() is not an option, since we sometimes process |
| 281 | # multiple elements on a single pass through the loop |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 282 | n = len(code) |
| 283 | i = 0 |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 284 | while i < n: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 285 | op = code[i] |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 286 | offset = i |
| 287 | if linestarts is not None: |
| 288 | starts_line = linestarts.get(i, None) |
| 289 | if starts_line is not None: |
| 290 | starts_line += line_offset |
| 291 | is_jump_target = i in labels |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 292 | i = i+1 |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 293 | arg = None |
| 294 | argval = None |
| 295 | argrepr = '' |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 296 | if op >= HAVE_ARGUMENT: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 297 | arg = code[i] + code[i+1]*256 + extended_arg |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 298 | extended_arg = 0 |
| 299 | i = i+2 |
| 300 | if op == EXTENDED_ARG: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 301 | extended_arg = arg*65536 |
| 302 | # Set argval to the dereferenced value of the argument when |
| 303 | # availabe, and argrepr to the string representation of argval. |
| 304 | # _disassemble_bytes needs the string repr of the |
| 305 | # raw name index for LOAD_GLOBAL, LOAD_CONST, etc. |
| 306 | argval = arg |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 307 | if op in hasconst: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 308 | argval, argrepr = _get_const_info(arg, constants) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 309 | elif op in hasname: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 310 | argval, argrepr = _get_name_info(arg, names) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 311 | elif op in hasjrel: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 312 | argval = i + arg |
| 313 | argrepr = "to " + repr(argval) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 314 | elif op in haslocal: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 315 | argval, argrepr = _get_name_info(arg, varnames) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 316 | elif op in hascompare: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 317 | argval = cmp_op[arg] |
| 318 | argrepr = argval |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 319 | elif op in hasfree: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 320 | argval, argrepr = _get_name_info(arg, cells) |
Alexander Belopolsky | 7448220 | 2012-06-07 14:28:14 -0400 | [diff] [blame] | 321 | elif op in hasnargs: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 322 | argrepr = "%d positional, %d keyword pair" % (code[i-2], code[i-1]) |
| 323 | yield Instruction(opname[op], op, |
| 324 | arg, argval, argrepr, |
| 325 | offset, starts_line, is_jump_target) |
| 326 | |
| 327 | def disassemble(co, lasti=-1, *, file=None): |
| 328 | """Disassemble a code object.""" |
| 329 | cell_names = co.co_cellvars + co.co_freevars |
| 330 | linestarts = dict(findlinestarts(co)) |
| 331 | _disassemble_bytes(co.co_code, lasti, co.co_varnames, co.co_names, |
| 332 | co.co_consts, cell_names, linestarts, file=file) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 333 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 334 | def _disassemble_bytes(code, lasti=-1, varnames=None, names=None, |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 335 | constants=None, cells=None, linestarts=None, |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 336 | *, file=None, line_offset=0): |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 337 | # Omit the line number column entirely if we have no line number info |
| 338 | show_lineno = linestarts is not None |
| 339 | # TODO?: Adjust width upwards if max(linestarts.values()) >= 1000? |
| 340 | lineno_width = 3 if show_lineno else 0 |
| 341 | for instr in _get_instructions_bytes(code, varnames, names, |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 342 | constants, cells, linestarts, |
| 343 | line_offset=line_offset): |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 344 | new_source_line = (show_lineno and |
| 345 | instr.starts_line is not None and |
| 346 | instr.offset > 0) |
| 347 | if new_source_line: |
| 348 | print(file=file) |
| 349 | is_current_instr = instr.offset == lasti |
| 350 | print(instr._disassemble(lineno_width, is_current_instr), file=file) |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 351 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 352 | def _disassemble_str(source, *, file=None): |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 353 | """Compile the source string, then disassemble the code object.""" |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 354 | disassemble(_try_compile(source, '<dis>'), file=file) |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 355 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 356 | disco = disassemble # XXX For backwards compatibility |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 358 | def findlabels(code): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 359 | """Detect all offsets in a byte code which are jump targets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 360 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 361 | Return the list of offsets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 362 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 363 | """ |
| 364 | labels = [] |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 365 | # enumerate() is not an option, since we sometimes process |
| 366 | # multiple elements on a single pass through the loop |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 367 | n = len(code) |
| 368 | i = 0 |
| 369 | while i < n: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 370 | op = code[i] |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 371 | i = i+1 |
| 372 | if op >= HAVE_ARGUMENT: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 373 | arg = code[i] + code[i+1]*256 |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 374 | i = i+2 |
| 375 | label = -1 |
| 376 | if op in hasjrel: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 377 | label = i+arg |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 378 | elif op in hasjabs: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 379 | label = arg |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 380 | if label >= 0: |
| 381 | if label not in labels: |
| 382 | labels.append(label) |
| 383 | return labels |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 384 | |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 385 | def findlinestarts(code): |
| 386 | """Find the offsets in a byte code which are start of lines in the source. |
| 387 | |
| 388 | Generate pairs (offset, lineno) as described in Python/compile.c. |
| 389 | |
| 390 | """ |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 391 | byte_increments = list(code.co_lnotab[0::2]) |
| 392 | line_increments = list(code.co_lnotab[1::2]) |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 393 | |
| 394 | lastlineno = None |
| 395 | lineno = code.co_firstlineno |
| 396 | addr = 0 |
| 397 | for byte_incr, line_incr in zip(byte_increments, line_increments): |
| 398 | if byte_incr: |
| 399 | if lineno != lastlineno: |
| 400 | yield (addr, lineno) |
| 401 | lastlineno = lineno |
| 402 | addr += byte_incr |
| 403 | lineno += line_incr |
| 404 | if lineno != lastlineno: |
| 405 | yield (addr, lineno) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 406 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 407 | class Bytecode: |
| 408 | """The bytecode operations of a piece of code |
| 409 | |
| 410 | Instantiate this with a function, method, string of code, or a code object |
| 411 | (as returned by compile()). |
| 412 | |
| 413 | Iterating over this yields the bytecode operations as Instruction instances. |
| 414 | """ |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 415 | def __init__(self, x, *, first_line=None, current_offset=None): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 416 | self.codeobj = co = _get_code_object(x) |
| 417 | if first_line is None: |
| 418 | self.first_line = co.co_firstlineno |
| 419 | self._line_offset = 0 |
| 420 | else: |
| 421 | self.first_line = first_line |
| 422 | self._line_offset = first_line - co.co_firstlineno |
| 423 | self._cell_names = co.co_cellvars + co.co_freevars |
| 424 | self._linestarts = dict(findlinestarts(co)) |
| 425 | self._original_object = x |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 426 | self.current_offset = current_offset |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 427 | |
| 428 | def __iter__(self): |
| 429 | co = self.codeobj |
| 430 | return _get_instructions_bytes(co.co_code, co.co_varnames, co.co_names, |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 431 | co.co_consts, self._cell_names, |
| 432 | self._linestarts, |
| 433 | line_offset=self._line_offset) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 434 | |
| 435 | def __repr__(self): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 436 | return "{}({!r})".format(self.__class__.__name__, |
| 437 | self._original_object) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 438 | |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 439 | @classmethod |
| 440 | def from_traceback(cls, tb): |
| 441 | """ Construct a Bytecode from the given traceback """ |
| 442 | while tb.tb_next: |
| 443 | tb = tb.tb_next |
| 444 | return cls(tb.tb_frame.f_code, current_offset=tb.tb_lasti) |
| 445 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 446 | def info(self): |
| 447 | """Return formatted information about the code object.""" |
| 448 | return _format_code_info(self.codeobj) |
| 449 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 450 | def dis(self): |
| 451 | """Return a formatted view of the bytecode operations.""" |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 452 | co = self.codeobj |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 453 | if self.current_offset is not None: |
| 454 | offset = self.current_offset |
| 455 | else: |
| 456 | offset = -1 |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 457 | with io.StringIO() as output: |
| 458 | _disassemble_bytes(co.co_code, varnames=co.co_varnames, |
| 459 | names=co.co_names, constants=co.co_consts, |
| 460 | cells=self._cell_names, |
| 461 | linestarts=self._linestarts, |
| 462 | line_offset=self._line_offset, |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 463 | file=output, |
| 464 | lasti=offset) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 465 | return output.getvalue() |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 466 | |
| 467 | |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 468 | def _test(): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 469 | """Simple test program to disassemble a file.""" |
Nick Coghlan | 0956689 | 2013-08-25 00:48:17 +1000 | [diff] [blame] | 470 | import argparse |
| 471 | |
| 472 | parser = argparse.ArgumentParser() |
| 473 | parser.add_argument('infile', type=argparse.FileType(), nargs='?', default='-') |
| 474 | args = parser.parse_args() |
| 475 | with args.infile as infile: |
| 476 | source = infile.read() |
| 477 | code = compile(source, args.infile.name, "exec") |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 478 | dis(code) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 479 | |
| 480 | if __name__ == "__main__": |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 481 | _test() |