Search code examples
pythonassemblyreverse-engineering

How to log CPU instructions executed by a Python program?


I understand that Python source code is compiled into bytecode which is then interpreted by the Python VM (let's say CPython). If I understand correctly, this mean that the VM parses the bytecode instructions and decides (at runtime) what CPU instructions should be executed accordingly.

My questions:

  • Is it possible to log the actual CPU instructions executed on your machine as a result of the interpretation of a particular Python file (.py)? I understand it might not be simple (or even feasible) to get a 1-1 correspondence between a .py file and CPU instructions, but what is the closest you can get?
  • Going a step further: Is it even possible to log the instructions executed that correspond to a particular process?

Solution

  • use strace on linux, it will show you every system call made by any program (including python). On windows you have to use something like wt or maybe Logger.exe which traces all library calls (not just system).

    You can use a debugger like gdb to look at the machine code in realtime, and since you have CPython source code, a better alternative is to just compile it with debugging symbols then run it in a C debugger, that can give you a high-level call stack, which will be a lot easier to understand.