This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: f_trace_opcodes setting and accessing opcodes
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: anthony shaw, ncoghlan
Priority: normal Keywords:

Created on 2019-03-25 00:00 by anthony shaw, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg338772 - (view) Author: anthony shaw (anthony shaw) Date: 2019-03-25 00:00
The f_trace_opcodes flag for sys.settrace in 3.7 are proving tricky. 

I must be missing something but it's not clear how it helps in tracing the opcode about to be executed because it runs before opcode and oparg variables are set by NEXTOPARG(), so the only way to establish the opcode is to look at the frame code and work out the next instruction in the stack.

The documentation references dis, but if you call that for a traceback or using the frame code, you only have the last instruction, not the next one?

def trace(frame, event, args):
  frame.f_trace_opcodes = True
  if event == 'opcode':
    disassemble(frame.f_code, frame.f_lasti)
  return frame

It looks like the emitting of the opcode event needs to come after NEXTOPARG(), but that means if the tracing function were to add any instructions to the stack, that would no longer work. 
Alternatively, the opcode could be calculated and added as an argument.
msg338777 - (view) Author: anthony shaw (anthony shaw) Date: 2019-03-25 02:09
Took a while, but I worked out a solution:

import sys
import dis
import traceback
import io

def t(frame, event, args):
   frame.f_trace_opcodes=True
   stack = traceback.extract_stack(frame)
   pad = "   "*len(stack) + "|"
   if event == 'opcode':
      with io.StringIO() as out:
         dis.disco(frame.f_code, frame.f_lasti, file=out)
         lines = out.getvalue().split('\n')
         [print(f"{pad}{l}") for l in lines]
   elif event == 'call':
      print(f"{pad}Calling {frame.f_code}")
   elif event == 'return':
      print(f"{pad}Returning {args}")
   elif event == 'line':
      print(f"{pad}Changing line to {frame.f_lineno}")
   else:
      print(f"{pad}{frame} ({event} - {args})")
   print(f"{pad}----------------------------------")
   return t
sys.settrace(t)
eval('"-".join([letter for letter in "hello"])')
History
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80601
2019-03-25 02:09:33anthony shawsetstatus: open -> closed
resolution: not a bug
stage: resolved
2019-03-25 02:09:18anthony shawsetmessages: + msg338777
2019-03-25 00:00:56anthony shawcreate