dis.py 20 KB

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