Giter VIP home page Giter VIP logo

bytecode_tracer's Introduction

bytecode_tracer

Python script to trace execution of bytecode for python 2.7

This works by modifying the CPython virtual machine such that it calls our trace function for each executed instruction rather than line by line of source code.

Visit http://0xec.blogspot.com/2017/03/hacking-cpython-virtual-machine-to.html to know more.

Known limitations

Tracing of SETUP_EXTENDED opcode is not fully supported. However this does not mean tracing will fail or stop. In such case only the SETUP_EXTENDED instruction will be written to the trace file AND NOT the following instructions on which this opcode is actually operating on.

For example, if the bytecode looks like

SETUP_EXTENDED 0x0001
JUMP_ABSOLUTE 0x1234

0x11234:
POP_TOP

The target of the JUMP_ABSOLUTE instruction is 0x11234 due to the SETUP_EXTENDED before ((0x0001 << 16) | 0x1234). However the trace file will NOT contain the JUMP_ABSOLUTE instruction and will contain the following listing.

SETUP_EXTENDED 0x0001
POP_TOP

This feature is easy to implement. We need to check if the current instruction is SETUP_EXTENDED. If so, we need to decode the following instructions on which this is operating as well.

Command line arguments

$ python tracer.py -h
usage: tracer.py [-h] [-t {all,only}] [-n NAME] [-r] pycfile tracefile

positional arguments:
  pycfile               The pyc file to trace.
  tracefile             The file where to write the trace.

optional arguments:
  -h, --help            show this help message and exit
  -t {all,only}, --trace {all,only}
                        Trace all or only those codeobjects with co_name
                        specified by -name.
  -n NAME, --name NAME  co_name of the codeobject that will be traced.
  -r, --resolve         Resolve instruction operand values.

Usage

  1. Trace execution of all code objects in the file factorial.pyc; do not resolve arguments

    $ python tracer.py factorial.pyc trace.txt
    
  2. Trace code objects having a name of recur_factorial; resolve arguments as well

    $ python tracer.py -t=only -n=recur_factorial -r factorial.pyc trace.txt
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.