DEV Community

Mohamed Mokhtar
Mohamed Mokhtar

Posted on • Updated on

How to debug AGE source code on VSCode

Welcome everyone
Today we will set up our environment to debug and work with AGE source code on VSCode Editor.

Prerequisites

  • Postgresql source code installed
  • AGE source code & installed
  • VSCode for sure
  • GDB

Our case here we will debug a function that we have added to the source code if you have missed adding some functions to AGE source code you can find that out on my blog, here is the link of it: https://dev.to/rrrokhtar/guide-to-age-contribution-and-modifying-the-source-code-to-add-new-functions-l7m

Lets set up our AGE git repository to exclude some of our new created directory which is .vscode
TLDR;

echo '/.vscode/*' >> .git/info/exclude
Enter fullscreen mode Exit fullscreen mode

We will need to modify out local git repo to exclude that directory we are going to work with to setup the linkage of our source-codes and header files so that whenever we click on smth it gets navigation to that function/file

cd .git/info
sudo nano exclude
Enter fullscreen mode Exit fullscreen mode

Add whatever you want to exclude just like .gitignore but it works on you local machine only.
REPLACE all /home/rrr/postgresql-14.7/ with corresponding postgresql directory on your machine
AND /usr/local/pgsql/ with your installed postgresl directory

  • incase of you are having the source code and installation on same directory, they will be the same.

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/usr/include/**/**",
                "${workspaceFolder}/**",
                "/usr/include/**",
                "/usr/local/pgsql/include/**",
                "/usr/local/pgsql/include/server/**",
                "/home/rrr/postgresql-14.7/src/**",
                "/home/rrr/postgresql-14.7/**",
                "/home/rrr/postgresql-14.7/src/backend/**",
                "/usr/local/pgsql/lib/**",
                "${workspaceFolder}/src/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}
Enter fullscreen mode Exit fullscreen mode

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "/home/rrr/postgresql-14.7/bin/postgres",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}
Enter fullscreen mode Exit fullscreen mode

settings.json

{
    "files.associations": {
        "postgres.h": "c",
        "genam.h": "c",
        "heapam.h": "c",
        "tableam.h": "c",
        "table.h": "c",
        "htup_details.h": "c",
        "skey.h": "c",
        "pg_operator.h": "c",
        "namespace.h": "c"
    }
}
Enter fullscreen mode Exit fullscreen mode

Now you are supposed to have all of your headers and linkage works properly

LETS dive into the debugging

  1. START postgres server
  2. CONNECT through psql session
  3. GET THE pid of that backend process through

    SELECT pg_backend_pid();
    
  4. SET your breakpoints on your function or anywhere you want

  5. Get start with the debugging on VS code through click on F5 and write the process id you got from the psql session

  6. Write commands to the psql session (call your function to get into that and reach the break points you have set

  7. Have a good debugging day <3

Another debugging blog:
https://dev.to/rrrokhtar/gdb-debugging-session-postgresql-age-2b27

DEMO:
img

References & Resources

Top comments (0)