Windows下使用VSCode远程debug PostgreSQL

Windows下使用VSCode远程debug PostgreSQL,第1张

基本思路
  1. 远程主机先完成PostgreSQL的编译
  2. 用psql连接PG,获取到fork出来的子进程PID
  3. 在psql中执行sql,触发PG中的代码
  4. 远程用VSCode调试子进程
环境版本说明

PostgreSQL版本:PostgreSQL12.10
远程目标机:CentOS7
本地主机:Windows10

具体步骤 1. 远程主机准备,编译并启动PostgreSQL

以下 *** 作均在root用户下执行。



1. 创建用户postgres

useradd -s /bin/bash -d /home/postgres -m  postgres

2. 从PostgreSQL官网下载其源码,并解压要到本地目录

wget https://ftp.postgresql.org/pub/source/v12.10/postgresql-12.10.tar.gz
tar -xzvf ./postgresql-12.10.tar.gz
cd ./postgresql-12.10

3. 在环境中安装以下软件
yum install readline-devel zlib-devel

4. 运行配置
./configure --enable-debug --enable-cassert
–enable-debug表示开启Debug
–enable-cassert表示打开断言检查
其他选项可使用./configure --help查看
5. 将src/Makefile.global文件中的编译优化级别去掉
改之前:
CFLAGS = -Wall (…) -fexcess-precision=standard -g -O2
CXXFLAGS = -Wall (…) -g -O2
改之后:
CFLAGS = -Wall (…) -fexcess-precision=standard -g
CXXFLAGS = -Wall (…) -g

6. 执行构建并安装
make && sudo make install
PG将安装安装到/usr/local/pgsql目录下。


6. 为了使用方便将PostgreSQL加入PATH环境变量。


将以下字符数写入~/.bashrc或/etc/profile文件中,并刷新环境变量。


export PATH=/usr/local/pgsql/bin:$PATH

7. 创建数据库数据目录,如/home/postgres/pgdata, 并将其拥有者改为postgres

mkdir /home/postgres/pgdata
chown postgres:postgres /home/postgres/pgdata

8. 初始化数据库
sudo -u postgres -s /bin/bash -c "/usr/local/pgsql/bin/initdb -D /home/postgres/pgdata"
初始化完之后,目录中会出现大量的文件。


9. 启动postgresql服务
sudo -u postgres -s /bin/bash -c "/usr/local/pgsql/bin/pg_ctl -D /home/postgres/pgdata start"

10. 连接PostgreSQL
psql -U postgres

11. 在psql终端中,创建简单的表,并插入简单的数据

create table t1(id integer, name text);
insert into t1 values(1, 'name1');
insert into t1 values(2, 'name2');

12. 在psql终端中,查询PostgreSQL子进程的pid
select pg_backend_pid();
请记住子进程的pid,调试的时候会用到。


3. 本地主机准备工作
  1. 安装Visual Studio Code,设置远程开发需要的插件,Remote-SSH,C/C++等插件;具体可参考:Windows + VSCode SSH实现远程C/C++开发
  2. 使用VSCode打开远程主机上Postgre源码,新增.vscode目录,新建launch.json文件,内容如下:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "postgresql",
                "type": "cppdbg",
                "request": "attach",
                "program": "/usr/local/pgsql/bin/postgres",
                "processId": "${command:pickProcess}",
                "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
                    }
              ]
            }
        ]
    }
    
4. 启动调试
  1. 在本地机器上,进入VSCode的调试面板,启动调试,并选择2.(12)查到的进程号。


    3. 在本地机器上,查看[Breakpoints],新增断点在任意函数上,如planner4. 在2.12打开的psql终端中,进行简单查询,触发代码的执行,如“select * from t1 where id = 1”。


    如果你看到PostgreSQL进程停在planner.c中的planner()函数上,那么Debug成功…

5. 参考

[1]. How to do linux debugging PostgreSQL remotely using Visual Studio Code

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/564436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-06
下一篇 2022-04-06

发表评论

登录后才能评论

评论列表(0条)

保存