Giter VIP home page Giter VIP logo

ptracer's Introduction

ptracer -- a library for ptrace-based tracing of Python programs

Ptracer is a library providing on-demand system call tracing in Python programs.

Basic Usage

Filtering

Ptracer allows elaborate syscall filtering via the filter argument:

In the above example, ptracer will invoke the callback only for successful attempts to open files in the "/tmp" directory for writing.

Documentation

The documentation is available on ptracer.readthedocs.io.

ptracer's People

Contributors

0xflotus avatar elprans avatar jparise avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ptracer's Issues

ImportError: .../_lltraceback.cpython-38-x86_64-linux-gnu.so: undefined symbol: _PyThreadState_GetFrame

Hello,

I am not sure, if this repository is actively maintained, because the last changes are some time ago.

I tried to use ptracer and installed it in a Python 3.8 environment.
The import failed in Python 3.8:

>>> import ptracer
...
ImportError: .../ptracer/_lltraceback.cpython-38-x86_64-linux-gnu.so: undefined symbol: _PyThreadState_GetFrame

Then I tried it in an older environment with Python 3.6 on another computer and ptracer worked.

I found https://bugs.python.org/issue39946 and from the comments there, it looks like _PyThreadState_GetFrame was removed in Python 3.7.

Fix thread-safety (or document its absence)

Because ptracer uses a context manager, it's easy to use it in a multi-threaded context where the context can be entered and exited from multiple threads concurrently. However it seems to deal rather badly with doing that: because ptracer.context works with an unprotected global (_context), that object can be in a completely inconsistent state because there's a TOCTOU bug on self.enabled.

An easy fix would be to replace enabled with a regular lock (and acquire it in non-blocking, raising if it was not acquired).

It would be nice if ptracer could handle the situation better by having each context "subscribe" to the tracing process, but the asynchronous nature of the reporting likely makes that complicated.

TypeError: cannot use a bytes pattern on a string-like object

The following snippet crashes with the above error when it comes across a system call that passes the filter.

import traceback
import ptracer
import re
import os

def callback(syscall):
    print(syscall.name, syscall.result.raw_value)

flt = [
    ptracer.SysCallPattern(
        name='openat',
        args=[
            re.compile(b'/tmp/.*'),
            lambda arg: arg.value & os.O_WRONLY
        ],
        result=lambda res: res.value > 0
    )
]

with ptracer.context(callback, filter=flt):
    open('/tmp/domf', mode='w')

Here's the error stack.

$ py a.py 
Traceback (most recent call last):
  File "/home/shubham/workspace/tracer/a.py", line 21, in <module>
    open('/tmp/domf', mode='w')
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/__init__.py", line 106, in __exit__
    _context.disable()
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/__init__.py", line 92, in disable
    raise error
ptracer._ptracer.PtracerError: Unhandled exception in ptrace process
Traceback (most recent call last):
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 276, in _debugger_thread
    _debugger_thread_inner(main_pid, dbgproc_started, dbgthread_stop,
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 405, in _debugger_thread_inner
    elif filter_ is None or filter_(syscall):
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 294, in <lambda>
    filter_ = lambda sc: any(m.match(sc) for m in syscall_filter)
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 294, in <genexpr>
    filter_ = lambda sc: any(m.match(sc) for m in syscall_filter)
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_syscall.py", line 52, in match
    return all(m[1](m[0](syscall)) for m in self.matcher)
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_syscall.py", line 52, in <genexpr>
    return all(m[1](m[0](syscall)) for m in self.matcher)
TypeError: cannot use a bytes pattern on a string-like object

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.