Git Product home page Git Product logo

debug-py-c-extensions's Introduction

Debug Python's C/C++ extensions

You can easily debug Python's C/C++ extensions using GDB/LLDB. Here's a demo.

You need to install pybind11 to build and run this project.

How to build

mkdir build
cmake -S . -B build -DCMAKE_BUILD_TYPE=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake --build build

This will build a hello extension module in build/, e.g. build/hello.cpython-38-darwin.so in macOS.

How to run/debug

main.py is a simple Python script that loads the hello extension module and calls the hello function:

# main.py
import build.hello as hello

hello.hello(42)

Set a breakpoint using VS Code GUI to hello.cpp's hello function, and click the Debug button of VS Code. You can check the debug config: .vscode/launch.json.

debug.png

You can also use GDB/LLDB CLI to debug it:

lldb python main.py
(lldb) target create "python"
Current executable set to '/Users/guoshuai/mambaforge/envs/mlx-dev-py38/bin/python' (arm64).
(lldb) settings set -- target.run-args  "main.py"
(lldb) b hello.cpp:4
Breakpoint 1: no locations (pending).
WARNING:  Unable to resolve breakpoint to any actual locations.
(lldb) r
Process 24624 launched: '/Users/guoshuai/mambaforge/envs/mlx-dev-py38/bin/python' (arm64)
1 location added to breakpoint 1
Process 24624 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001006d46cc hello.cpython-38-darwin.so`hello(x=42) at hello.cpp:4:6
   1    #include <pybind11/pybind11.h>
   2   
   3    int hello(int x) {
-> 4        x++;
   5        printf("Hello, %d\n", x);
   6        return x;
   7    }
Target 0: (python) stopped.

debug-py-c-extensions's People

Contributors

atomicvar avatar

Watchers

 avatar

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.