Foreword
Using gdb for command-line debugging still feels inconvenient. I initially wanted to find a simpler way to directly debug the PostgreSQL source code under Windows. After searching for a while, I found that Visual Studio (VS) was the only option available, but it is heavy and the steps are quite complex. Since most real environments run on Linux, it is better to debug the PostgreSQL source code under Linux.
How to build & install PostgreSQL from source code.
I used Ubuntu Linux environment, the first step we might need to install pre-requirement tool for PostgreSQL build.
1 | sudo apt-get update |
download PostgreSQL source code.
1 | wget https://ftp.postgresql.org/pub/source/v14.8/postgresql-14.8.tar.gz |
we would need to make sure the path (--prefix
) exist in your system.
1 | ./configure --prefix=/home/daniel/postgresql-14.8/pgsql --with-icu --with-openssl --with-systemd --with-libxml --enable-debug |
we must build with
--enable-debug
parameter, otherwise we can’t debug with our source code.
Here are commands we would use later.
1 | /home/daniel/postgresql-14.8/pgsql/bin/psql |
setup PostgreSQL environment path
I would recommend setup PostgreSQL environment path, after we build & install PostgreSQL program from source code that would assist with us easier our next steps.
Identify your shell: Determine which shell you are using by running the following command:
1 | echo $SHELL |
It will display the path to your current shell.
Locate the configuration file: The configuration file you need to modify depends on your shell. Here are the common ones:
- Bash: ~/.bashrc or ~/.bash_profile
- Zsh: ~/.zshrc or ~/.zprofile
- Fish: ~/.config/fish/config.fish
Open the configuration file: Use a text editor, such as nano or vim, to open the configuration file. For example, if you are using Bash and the file is ~/.bashrc
, you can
1 | vim ~/.bashrc |
please modify with your environment PGDATA
& PATH
setting align with your PostgreSQL build bin & data path.
because my build parameter used
--prefix=/home/daniel/postgresql-14.8/pgsql
so the setting would be align with that.
1 | export PGDATA="/home/daniel/postgresql-14.8/pgsql/data" |
Update the environment: To apply the changes to your current session, run the following command in your terminal:
1 | source ~/.bashrc # or the appropriate file for your shell |
build pem file to a create user
To create the keys, a preferred command is ssh-keygen, which is available with OpenSSH utilities.
1 | ssh-keygen -m PEM -t rsa -b 4096 |
When you create an Azure VM by specifying the public key, Azure copies the public key (in the .pub format) to the ~/.ssh/authorized_keys
folder on the VM. SSH keys in ~/.ssh/authorized_keys
ensure that connecting clients present the corresponding private key during an SSH connection.
1 | danielss@postgresql-debuger:~/.ssh$ ll |
copy danielkey
(pem file) to your local machine, then you might login via the pem file.
permission problem on pem file
Confirm that the permissions of the .ssh
directory and the authorized_keys file on the server are set correctly. Run the following commands on the server:
1 | chmod 700 ~/.ssh |
These commands ensure that the directory has read, write, and execute permissions only for the user, and the authorized_keys
file has read and write permissions only for the user.
setup vscode
Please follow below config
Modify “program” column with PostgreSQL binary file path from json file
1 | { |
Final used ssh with pem file to login your VM via VsCode.
demo
Here is the demo for vscode debug PostgreSQL running process by sourcecode.
How can solve “ptrace:Operation not permitted” problem?
Regarding to Troubleshoot attaching to processes using GDB
Run the following command as super user: echo 0| sudo tee /proc/sys/kernel/yama/ptrace_scope
This will set the ptrace level to 0, after this just with user permissions you can attach to processes which are not launched by the debugger.
1 | postgresdebuger:~$ echo 0| sudo tee /proc/sys/kernel/yama/ptrace_scope - |
More information
https://github.com/microsoft/MIEngine/wiki/Troubleshoot-attaching-to-processes-using-GDB
https://learn.microsoft.com/en-us/azure/virtual-machines/linux/create-ssh-keys-detailed#overview-of-ssh-and-keys
此文作者:Daniel Shih(石頭)
此文地址: https://isdaniel.github.io/postgresql-vscode-sourcecode-debugger/
版權聲明:本博客所有文章除特別聲明外,均採用 CC BY-NC-SA 3.0 TW 許可協議。轉載請註明出處!