Debugging AGE source code using VSCode.
Published: 2023-07-21 15:50:37

Hi, In today's blog we will be exploring how we can setup our working environment to attach a debugger and debug AGE source code. 

We will be adding some breakpoints at random files in the code just to make sure that debugger has been  attached. In further blogs we will see how we can debug custom functions. 


## Prerequisites

- AGE and PostgreSQL Source code/installed
- VSCode
- GDB

You can checkout this blog to [install AGE and PostgreSQL](https://dev.to/muhammadzeeshan03/install-postgresql-16beta1-and-age-along-with-possible-error-solutions-3ma8) on you machine.

## STEP 1

First of all go to the directory where postgres is installed. And start postgres. In my case its the folder named,  **postgresql-16beta1** So,

```
cd postgresql-16beta1
```
Then initialize a database cluster. 

```
bin/initdb demo
```
Start the server using the command.

```
bin/pg_ctl -D demo -l logfile start
```
Now create a database named demodb.

```
bin/createdb demodb
```

Start the PostgreSQL server.

```
bin/psql demodb
```

After running above commands you will see a terminal like this. 

![DemoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3ws2hgrle40ndvw1ok4.png)

Now run the command to check the backendId where postgres is running.

```
SELECT pg_backend_pid();
```
For now we are done with PostgreSQL directory.

## STEP 2
**Open AGE directory in the VS-Code.**
Install these two VS-Code extensions if you don't have.

![VS code Extensions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rq1j0kg6nq4uq7luehb.png)
After installing these extensions press F5. You will be directed to Configure Debugger for the code. It will open 

`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/zeeshan/age16/pg/postgresql-16beta1/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
                }
            ]
        }


    ]
}
```

Edit this line in the Launch.json file.

`"program": "/home/zeeshan/age16/pg/postgresql-16beta1/bin/postgres",`

In my case postgres16-beta is installed in age16 and then in pg directory. Change it with path where you have installed PostgreSQL.
Save changes, 

## STEP 3

Again press F5, this time you will be asked to add process Id. Paste the backend-id that you copied from PostgreSQL after running command `SELECT pg_backend_pid();`.
If it successful you will be asked to enter your device password. Enter the password.

Now add some random breakpoints in cypher_clause.c and cypher_analyze.c file(These files are in AGE's folder under src->backend->parser).

## Step 4

Head back to PostgreSQL terminal where you are in demodb database. 

Load Age extension. 

```
CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
```


Try some cypher queries.

```
SELECT create_graph('demo_graph');
SELECT * FROM cypher('demo_graph', $$ CREATE (n:Person {name : "james", bornIn : "US"}) $$) AS (a agtype);
SELECT * FROM cypher('demo_graph', $$ MATCH (v) RETURN v $$) as (v agtype);
```

During each command debugger will start and ask you to move forward you can move into specific function and move forward using the debugger.


![Debug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1xogu8q8a4xk1mier0s.png)

You have successfully attached the debugger. Happy debugging :)

## REFERENCES
[https://dev.to/rrrokhtar/how-to-debug-age-source-code-on-vscode-3op7](https://dev.to/rrrokhtar/how-to-debug-age-source-code-on-vscode-3op7)

[https://imranzaheer.hashnode.dev/install-age-psql-from-source](https://imranzaheer.hashnode.dev/install-age-psql-from-source)