├── .vscode ├── settings.json ├── c_cpp_properties.json ├── tasks.json └── launch.json ├── .devcontainer ├── devcontainer.json └── Dockerfile └── README.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.pythonPath": "${workspaceFolder}/python.exe", 3 | "python.testing.unittestArgs": [ 4 | "-v", 5 | "-s", 6 | "${workspaceFolder}/Lib/test", 7 | "-p", 8 | "test_*.py" 9 | ], 10 | "python.testing.pytestEnabled": false, 11 | "python.testing.nosetestsEnabled": false, 12 | "python.testing.unittestEnabled": true 13 | } 14 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "compilerPath": "/usr/bin/gcc", 10 | "cStandard": "c11", 11 | "cppStandard": "c++17", 12 | "intelliSenseMode": "gcc-x64" 13 | } 14 | ], 15 | "version": 4 16 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "shell", 5 | "label": "clang build active file", 6 | "command": "/usr/bin/clang", 7 | "args": [ 8 | "-g", 9 | "${file}", 10 | "-o", 11 | "${fileDirname}/${fileBasenameNoExtension}" 12 | ], 13 | "options": { 14 | "cwd": "/usr/bin" 15 | } 16 | } 17 | ], 18 | "version": "2.0.0" 19 | } -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/vscode-remote/devcontainer.json or the definition README at 2 | // https://github.com/microsoft/vscode-dev-containers/tree/master/containers/cpp 3 | { 4 | "name": "CPython", 5 | "dockerFile": "Dockerfile", 6 | "runArgs": [ 7 | // Uncomment the next line to use a non-root user. On Linux, this will prevent 8 | // new files getting created as root, but you may need to update the USER_UID 9 | // and USER_GID in .devcontainer/Dockerfile to match your user if not 1000. 10 | // "-u", "vscode", 11 | 12 | "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" 13 | ], 14 | // Use 'settings' to set *default* container specific settings.json values on container create. 15 | // You can edit these settings after create using File > Preferences > Settings > Remote. 16 | "settings": { 17 | "terminal.integrated.shell.linux": "/bin/bash", 18 | "python.pythonPath": "${workspaceFolder}/python.exe", 19 | "python.testing.cwd": "${workspaceFolder}/Lib/test", 20 | "python.testing.unittestEnabled": true 21 | 22 | }, 23 | 24 | // Uncomment the next line if you want to publish any ports. 25 | // "appPort": [], 26 | 27 | // Uncomment the next line to run commands after the container is created. 28 | "postCreateCommand": "echo \"add-auto-load-safe-path ${workspaceFolder}\" >> /root/.gdbinit &&./configure --with-pydebug && make && ./python.exe -m test -v test_list", 29 | 30 | // Add the IDs of extensions you want installed when the container is created in the array below. 31 | "extensions": [ 32 | //cpp debug plugin 33 | "ms-vscode.cpptools", 34 | //python debug plugin 35 | "ms-python.python", 36 | //git blame plugin 37 | "waderyan.gitblame" 38 | ] 39 | } -------------------------------------------------------------------------------- /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------------------------------------- 2 | # Copyright (c) Microsoft Corporation. All rights reserved. 3 | # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. 4 | #------------------------------------------------------------------------------------------------------------- 5 | 6 | FROM ubuntu:latest 7 | 8 | # Avoid warnings by switching to noninteractive 9 | ENV DEBIAN_FRONTEND=noninteractive 10 | 11 | # This Dockerfile adds a non-root 'vscode' user with sudo access. However, for Linux, 12 | # this user's GID/UID must match your local user UID/GID to avoid permission issues 13 | # with bind mounts. Update USER_UID / USER_GID if yours is not 1000. See 14 | # https://aka.ms/vscode-remote/containers/non-root-user for details. 15 | ARG USERNAME=vscode 16 | ARG USER_UID=1000 17 | ARG USER_GID=$USER_UID 18 | 19 | # Configure apt and install packages 20 | RUN apt-get update \ 21 | && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \ 22 | # 23 | # Verify git, process tools, lsb-release (useful for CLI installs) installed 24 | && apt-get -y install git iproute2 procps lsb-release \ 25 | && apt-get -y install libffi-dev openssl \ 26 | # 27 | # Install C++ tools 28 | && apt-get -y install build-essential cmake cppcheck valgrind \ 29 | # 30 | # Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user. 31 | && groupadd --gid $USER_GID $USERNAME \ 32 | && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \ 33 | # [Optional] Add sudo support for the non-root user 34 | && apt-get install -y sudo \ 35 | && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\ 36 | && chmod 0440 /etc/sudoers.d/$USERNAME \ 37 | # 38 | # Clean up 39 | && apt-get autoremove -y \ 40 | && apt-get clean -y \ 41 | && rm -rf /var/lib/apt/lists/* \ 42 | && echo "set auto-load safe-path /" > /root/.gdbinit 43 | 44 | # Switch back to dialog for any ad-hoc use of apt-get 45 | ENV DEBIAN_FRONTEND= 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vscode-CPython-debug 2 | 3 | Vscode configuration for debugging CPython. 4 | 5 | 6 | ## Prerequisites: 7 | 1. Install [Docker](https://www.docker.com/products/docker-desktop) 8 | 2. Install [vscode](https://code.visualstudio.com/download) with [remote-development](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) plugin 9 | 10 | 11 | ## Setup: 12 | 1. Clone [Cpython](https://github.com/python/cpython/tree/v3.8.0). 13 | 2. Clone/Download this repo in another location apart from where you cloned Cpython. 14 | 3. Copy and Paste the `.devcontainer` and `.vscode` directories into the directory where you cloned CPython. 15 | 4. Open Cpython in vscode. 16 | 5. Click on the green *Remote action button* " `><` " at the lower leftmost corner in vscode(as shown in the image) and select **Remote-Containers: Open Folder in Container...** from the commands popup that appears, vscode will now start building the container and then build Cpython with relevant flags. 17 | 18 | ![](https://code.visualstudio.com/assets/docs/remote/common/remote-dev-status-bar.png) 19 | 20 | 21 | ## Debug Configurations: 22 | Inside the `.vscode/launch.json` resides the debug configurations, there are three types of debugging configured: 23 | 24 | 1. `Cpython test` 25 | 26 | Debug a CPython test case. 27 | 28 | The best way to read the code of any open source project is to debug using the test cases. CPython's test cases are inside `Lib/tests`. Open any of the test case, for eg `Lib/tests/test_list.py`, these are the test cases for good ol python list, each object in Python has its respective implementations inside the directory `Objects` and you can find the list implementation in `Objects/listobject.c`, open the same and set a debug point in `PyList_GetItem` at line 199. 29 | Now go back to the `test_list.py` and start the debugger, Voila, the debugger breaks exactly at `PyList_GetItem`. 30 | 31 | 2. `python -c` 32 | 33 | Debug the python process by executing using the `python -c` option. 34 | 35 | Change the statement that you want the Python interpreter to run in the `args` option; currently it is `"args": ["-c", "print(\"hello\")"]`; i.e `python -c "print('hello')"`. 36 | 37 | 3. `python` 38 | 39 | Launches a Python interpreter and from there you can provide a statement for the interpreter to execute and you can follow along. 40 | 41 | ## Further reading 42 | Head to [Exploring Cpython](https://devguide.python.org/exploring/), the *Additional References* section has all the exhaustive resources for you to get started with CPython walkthrough! Happy Hacking. 43 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | 8 | // Open any Cpython test case in cpython/Lib/test 9 | { 10 | "name": "Cpython test", 11 | "type": "cppdbg", 12 | "request": "launch", 13 | "program": "${workspaceFolder}/python.exe", 14 | "args": ["-m", "test", "-v", "${file}"], 15 | "stopAtEntry": false, 16 | "cwd": "${workspaceFolder}/Lib/test", 17 | "environment": [], 18 | "externalConsole": false, 19 | "MIMode": "gdb", 20 | "setupCommands": [ 21 | { 22 | "description": "Enable pretty-printing for gdb", 23 | "text": "-enable-pretty-printing", 24 | "ignoreFailures": true 25 | } 26 | ], 27 | // "preLaunchTask": "Build Main" 28 | }, 29 | // run python with -c option 30 | { 31 | "name": "python -c", 32 | "type": "cppdbg", 33 | "request": "launch", 34 | "program": "${workspaceFolder}/python.exe", 35 | // change the second argument accordinly 36 | "args": ["-c", "print(\"hello\")"], 37 | "stopAtEntry": false, 38 | "cwd": "${workspaceFolder}", 39 | "environment": [], 40 | "externalConsole": false, 41 | "MIMode": "gdb", 42 | "setupCommands": [ 43 | { 44 | "description": "Enable pretty-printing for gdb", 45 | "text": "-enable-pretty-printing", 46 | "ignoreFailures": true 47 | } 48 | ], 49 | // "preLaunchTask": "Build Main" 50 | }, 51 | { 52 | "name": "python interpretter", 53 | "type": "cppdbg", 54 | "request": "launch", 55 | "program": "${workspaceFolder}/python.exe", 56 | // change the second argument accordinly 57 | "args": [], 58 | "stopAtEntry": false, 59 | "cwd": "${workspaceFolder}", 60 | "environment": [], 61 | "externalConsole": false, 62 | "MIMode": "gdb", 63 | "setupCommands": [ 64 | { 65 | "description": "Enable pretty-printing for gdb", 66 | "text": "-enable-pretty-printing", 67 | "ignoreFailures": true 68 | } 69 | ], 70 | // "preLaunchTask": "Build Main" 71 | } 72 | ] 73 | } --------------------------------------------------------------------------------