How to debug AGE source code on VSCode
Tags: vscode, postgres, database, apacheage
Published: 2023-05-06 11:19:46

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;
```sh
echo '/.vscode/*' >> .git/info/exclude
```
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
```sh
cd .git/info
sudo nano exclude
```
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

```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
}
```
launch.json
```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
                }
            ]
        }

    ]
}
```
settings.json
```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"
    }
}
```

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 ```SQL
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](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZThhZTg0MzJiMmIwNjNmYjFjMWM4NmI5NGJlOWUxODcwYWIxMDJhYSZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/OOYV1q0PcjuVhkf5cB/giphy.gif)


References & Resources

-     https://github.com/apache/age
-     https://age.apache.org/
-     https://blog.devgenius.io/stepping-through-postgresql-f40990685a56
-     https://www.highgo.ca/2019/10/03/trace-query-processing-internals-with-debugger/