Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
340 changes: 340 additions & 0 deletions COPYING

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
include AUTHORS
include COPYING
include ChangeLog
include INSTALL
include MANIFEST.in
include README.cptrace
include TODO
include tox.ini
include SYSCALL_PROTOTYPES.codegen.py

include cptrace/Makefile
include cptrace/cptrace.c
include cptrace/version.py

include doc/*.rst doc/conf.py doc/make.bat doc/Makefile

include examples/itrace.py
include examples/simple_dbg.py
include pyflakes.sh
include python3.0.patch
include setup_cptrace.py

# Tests
include runtests.py
include test_doc.py
include tests/test_*.py
include tests/crash/*.c
include tests/crash/BSDmakefile
include tests/crash/Makefile
42 changes: 42 additions & 0 deletions PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Metadata-Version: 2.1
Name: python-ptrace
Version: 0.9.9
Summary: python binding of ptrace
Home-page: http://python-ptrace.readthedocs.io/
Download-URL: http://python-ptrace.readthedocs.io/
Author: Victor Stinner
License: GNU GPL v2
Classifier: Intended Audience :: Developers
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
Classifier: Operating System :: OS Independent
Classifier: Natural Language :: English
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
License-File: COPYING

=============
python-ptrace
=============

.. image:: https://img.shields.io/pypi/v/python-ptrace.svg
:alt: Latest release on the Python Cheeseshop (PyPI)
:target: https://pypi.python.org/pypi/python-ptrace

.. image:: https://github.com/vstinner/python-ptrace/actions/workflows/build.yml/badge.svg
:alt: Build status of python-ptrace on GitHub Actions
:target: https://github.com/vstinner/python-ptrace/actions

python-ptrace is a debugger using ptrace (Linux, BSD and Darwin system call to
trace processes) written in Python.

* `python-ptrace documentation
<http://python-ptrace.readthedocs.io/>`_
* `python-ptrace at GitHub
<https://github.com/vstinner/python-ptrace>`_
* `python-ptrace at the Python Cheeseshop (PyPI)
<https://pypi.python.org/pypi/python-ptrace>`_

python-ptrace is an opensource project written in Python under GNU GPLv2
license. It supports Python 3.6 and newer.
1 change: 0 additions & 1 deletion README.md

This file was deleted.

24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=============
python-ptrace
=============

.. image:: https://img.shields.io/pypi/v/python-ptrace.svg
:alt: Latest release on the Python Cheeseshop (PyPI)
:target: https://pypi.python.org/pypi/python-ptrace

.. image:: https://github.com/vstinner/python-ptrace/actions/workflows/build.yml/badge.svg
:alt: Build status of python-ptrace on GitHub Actions
:target: https://github.com/vstinner/python-ptrace/actions

python-ptrace is a debugger using ptrace (Linux, BSD and Darwin system call to
trace processes) written in Python.

* `python-ptrace documentation
<http://python-ptrace.readthedocs.io/>`_
* `python-ptrace at GitHub
<https://github.com/vstinner/python-ptrace>`_
* `python-ptrace at the Python Cheeseshop (PyPI)
<https://pypi.python.org/pypi/python-ptrace>`_

python-ptrace is an opensource project written in Python under GNU GPLv2
license. It supports Python 3.6 and newer.
47 changes: 47 additions & 0 deletions SYSCALL_PROTOTYPES.codegen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

"""
Generates the SYSCALL_PROTOTYPES dictionary from the Linux kernel source
and prints out Python code representing it.
"""

import urllib2
import re

url = "https://raw.githubusercontent.com/torvalds/linux/master/include/linux/syscalls.h"
source = urllib2.urlopen(url).read()

p1 = re.compile(r"^asmlinkage long sys(?:32)?_(.*?)\((.*?)\)",
re.MULTILINE | re.DOTALL)
p2 = re.compile(r"^(.*?)([^ *]+)$")

SYSCALL_PROTOTYPES = {}

for m1 in p1.finditer(source):
call_name = m1.group(1)
args = m1.group(2)
args = args.replace("__user", "")
args = " ".join(args.split())
args_tuple = ()
if args != "void":
for arg in args.split(","):
if arg.endswith(("*", "long", "int", "size_t")):
arg_type = arg.strip()
arg_name = ""
else:
m2 = p2.match(arg)
arg_type = m2.group(1).strip()
arg_name = m2.group(2).strip()
# Workaround for pipe system call
if (call_name == 'pipe' or call_name == 'pipe2') and arg_type == "int *":
arg_type = "int[2]"
args_tuple += ((arg_type, arg_name),)
SYSCALL_PROTOTYPES[call_name] = ("long", args_tuple)

for call_name in sorted(SYSCALL_PROTOTYPES):
signature = SYSCALL_PROTOTYPES[call_name]
args_tuple = signature[1]
print('"%s": ("%s", (' % (call_name, signature[0]))
for arg in args_tuple:
print((' ("%s", "%s"),' % (arg[0], arg[1])))
print(')),')
10 changes: 10 additions & 0 deletions cptrace/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CC=gcc
CFLAGS=-fPIC -shared -Wall -Wextra -Wextra $(shell python-config --cflags)
LIBS=$(shell python-config --libs)
LIBRARY=cptrace.so

$(LIBRARY): cptrace.c
$(CC) -o $@ $< $(CFLAGS) $(LIBS)

clean:
rm -f $(LIBRARY)
107 changes: 107 additions & 0 deletions cptrace/cptrace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <Python.h>
#include <stdbool.h>
#if __APPLE__
#include <sys/types.h>
#endif
#include <sys/ptrace.h>

#define UNUSED(arg) arg __attribute__((unused))

char python_ptrace_DOCSTR[] =
"ptrace(command: int, pid: int, arg1=0, arg2=0, check_errno=False): call ptrace syscall.\r\n"
"Raise a ValueError on error.\r\n"
"Returns an unsigned integer.\r\n";

static bool cpython_cptrace(
unsigned int request,
pid_t pid,
void *arg1,
void *arg2,
bool check_errno,
unsigned long *result)
{
unsigned long ret;
errno = 0;
ret = ptrace(request, pid, arg1, arg2);
if ((long)ret == -1) {
/**
* peek operations may returns -1 with errno=0: it's not an error.
* For other operations, -1 is always an error
*/
if (!check_errno || errno) {
PyErr_Format(
PyExc_ValueError,
"ptrace(request=%u, pid=%i, %p, %p) "
"error #%i: %s",
request, pid, arg1, arg2,
errno, strerror(errno));
return false;
}
}
if (result)
*result = ret;
return true;
}

static PyObject* cpython_ptrace(PyObject* UNUSED(self), PyObject *args, PyObject *keywds)
{
unsigned long result;
unsigned int request;
pid_t pid;
unsigned long arg1 = 0;
unsigned long arg2 = 0;
bool check_errno = false;
PyObject* check_errno_p = NULL;
static char *kwlist[] = {"request", "pid", "arg1", "arg2", "check_errno", NULL};

if (!PyArg_ParseTupleAndKeywords(args, keywds,
"Ii|LLO", kwlist,
&request, &pid, &arg1, &arg2, &check_errno_p
))
{
return NULL;
}

if (check_errno_p) {
check_errno = PyObject_IsTrue(check_errno_p);
}

if (cpython_cptrace(request, pid, (void*)arg1, (void*)arg2, check_errno, &result))
return PyLong_FromUnsignedLong(result);
else
return NULL;
}

static PyMethodDef module_methods[] = {
{"ptrace", (PyCFunction)cpython_ptrace, METH_VARARGS | METH_KEYWORDS, python_ptrace_DOCSTR},
{NULL, NULL, 0, NULL}
};

PyDoc_STRVAR(module_doc,
"ptrace module written in C");

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"cptrace",
module_doc,
0,
module_methods,
NULL
};
#endif

PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_cptrace(void)
#else
initcptrace(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
return PyModule_Create(&module_def);
#else
(void)Py_InitModule3("cptrace", module_methods, module_doc);
#endif
}

4 changes: 4 additions & 0 deletions cptrace/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PACKAGE = "cptrace"
VERSION = "0.6.1"
WEBSITE = "http://python-ptrace.readthedocs.io/"
LICENSE = "GNU GPL v2"
Loading
Loading