程序员的资源宝库

网站首页 > gitee 正文

数据库内核:VScode远程调试 vscode远程debug

sanyeah 2024-03-29 17:24:14 gitee 13 ℃ 0 评论

准备虚拟机

这里我使用 Docker 的 Ubuntu 的镜像去创建容器。

下载 Ubuntu 镜像

docker pull ubuntu:20.04

创建子网

创建容器时需要设置固定 IP,所以先要在 docker 中创建固定 IP 的子网。

(base) quanjunyi@Tys-MacBook-Pro ~ % docker network create --subnet=172.30.0.0/16 PostgreSQL
b1432a4717f33aac1c617295049257c4ad954f38af49863c79401bbe62a04f2b

创建容器

docker run -d --privileged -ti --cap-add=SYS_PTRACE --platform linux/amd64 --name postgresqlunbuntu -h postgresqlunbuntu -p 18090:18090 -p 9872:9872 --net PostgreSQL --ip 172.30.0.3 d5ca7a4456053674d490803005766890dd19e3f7e789a48737c0d462da531f5d  /usr/sbin/init

进入容器

docker exec -it postgresqlunbuntu /bin/bash

配置 apt 源

我用的镜像是 arm64 架构的,可能是因为我本机就是 arm64 架构,所以在拉镜像的时候就拉了 arm64 架构。

用网上的 apt 源大部分都不能 apt-get update 成功。以下是我找到适合本架构的源。

deb http://mirrors.ustc.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse

执行更新命令

root@postgresqlunbuntu:~# apt-get update
root@postgresqlunbuntu:~# apt-get upgrade

安装 GDB

root@postgresqlunbuntu:~# apt install gdb

# 查看版本
root@postgresqlunbuntu:~# gdb --version
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

# 查看路径
root@postgresqlunbuntu:~# which gdb
/usr/bin/gdb

远程调试

在本机安装 VScode,安装过程比较简单,不赘述了。

安装插件

首先需要在 VScode 中安装插件 doker(用于识别 docker 的容器),remote-ssh(用于连接容器)。

上面两个插件是在安装在本地。安装了上面两个插件之后,我们就可以在 VScode 中看到 Docker 的图标,还有 Docker 里的容器。

右键对应的容器,选择 Attach Visual Studio Code 就会打开容器的工作间,如下所示:

刚打开的时候,左侧是没有容器里相应的文件目录的,但是有一个 Open Folder 的按钮,点击之后输入容器(Ubuntu)你想要的文件目录就可以将容器里该目录下的文件展示到左侧框中。

但是要调试容器里 C 程序还要安装 C/C++ 插件,这个插件是要安装在容器里的。如果你只在本地安装了,在打开容器工作间之后,再搜索这个插件他会有如下提示:

直接安装,我一直没安装上,所以采用离线安装,先从 https://marketplace.visualstudio.com/ 中下载插件。

Download Extension 选择 Linux Arm64 版本。然后将安装包传到 Ubuntu 的任意位置。

docker cp ms-vscode.cpptools-1.14.4@linux-arm64.vsix postgresqlunbuntu:/root/

点击上方三个点的按钮,选择 Install from VSIX,再选择容器里的安装包即可。

然后点击最左侧图标(调试)生成 luanch.json 文件,用于设置调试参数。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: Debugging in Visual Studio Code
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) start", //配置名称,显示在配置下拉菜单中
            "type": "cppdbg", //配置类型
            //"targetArchitecture": "x86_64",
            "targetArchitecture": "arm64",
            "request": "launch", //请求配置类型,可以是启动或者是附加
            "program": "${workspaceFolder}/test", // linux上程序可执行文件的完整路径         
            //"program": "/home/postgres/helloworld",
            "args": ["--help"], //传递给程序的命令行参数
            "stopAtEntry": false,//可选参数,如果为true,调试程序应该在入口(main)处停止
            "cwd": "${workspaceFolder}", //目标的工作目录
            "environment": [], //表示要预设的环境变量
            "externalConsole": false,//如果为true,则为调试对象启动控制台
            "MIMode": "gdb",//要连接到的控制台启动程序
            "setupCommands": [ //为了安装基础调试程序而执行的一个或多个GDB/LLDB命令
                {
                    "description": "for gdb print",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "miDebuggerPath": "/usr/bin/gdb", // linux上适合目标板的arm格式

            // gdb程序路径,作为与目标板gdbserver通信的gdb client运行于linux服务器上
            //"miDebuggerServerAddress": "192.168.225.1:9001"  //目标板上gdbserver监听的IP地址和 // 端口号      
        }
    ]
}

安装 PostgreSQL

创建用户及用户组

# 创建postgres用户组
root@postgresqlunbuntu:~# groupadd postgres

# 创建postgre用户,用户位于postgres组内,需要加参数-m,否则不会像Centos一样创建用户的家目录
root@postgresqlunbuntu:~# useradd -m -g postgres postgre

# 为root@postgresqlunbuntu:~# su postgre用户设置密码,密码为12345678
root@postgresqlunbuntu:~# passwd postgre
New password:
Retype new password:
passwd: password updated successfully

# 给root也设置一下密码,密码为root
root@postgresqlunbuntu:~# passwd
New password:
Retype new password:
passwd: password updated successfully

# 切换到postgre用户
root@postgresqlunbuntu:~# su postgre

使用 postgres 用户管理 PostgreSQL

# 新建的用户未指定shell。只需将其指定为/bin/bash即可
root@postgresqlunbuntu:~# usermod -s /bin/bash postgre

# 切换用户
root@postgresqlunbuntu:~# su postgre
postgres@postgresqlunbuntu:/root$

# 回到用户目录
postgres@postgresqlunbuntu:/root$ cd ~

上传安装包

(base) quanjunyi@Tys-MacBook-Pro ~ % docker cp PGDev.zip postgresqlunbuntu:/home/postgre/

安装依赖

postgre@postgresqlunbuntu:~$ su root

# 安装解压zip格式的压缩包所需的依赖
root@postgresqlunbuntu:/home/postgre# apt install unzip zip

# 安装libreadline-dev,否则在配置过程中会报configure: error: readline library not found错误
root@postgresqlunbuntu:/home/postgre# apt install libreadline-dev

# 安装zlib1g-dev,否则在配置过程中会报configure: error: zlib library not found错误
root@postgresqlunbuntu:/home/postgre# apt install zlib1g-dev

# 安装以下依赖,否则在构建过程中会报-bash: make: command not found
root@postgresqlunbuntu:/home/postgre# apt-get install  gcc automake autoconf libtool make

解压安装包

root@postgresqlunbuntu:/# su postgre
postgre@postgresqlunbuntu:/$ cd ~
postgre@postgresqlunbuntu:~$ ls
PGDev.zip
postgre@postgresqlunbuntu:~$ pwd
/home/postgre

# 解压
postgre@postgresqlunbuntu:~$ unzip PGDev.zip

postgre@postgresqlunbuntu:~$ ls
PGDev  PGDev.zip

创建所需文件目录

# 进入PGDev文件夹
postgre@postgresqlunbuntu:~$ cd PGDev/

# 创建pghome文件夹,用于存储PostgreSQL本身
postgre@postgresqlunbuntu:~/PGDev$ mkdir pghome

# 创建data文件夹,用于存储PostgreSQL所产生的数据
postgre@postgresqlunbuntu:~/PGDev$ mkdir data

配置 env-debug 文件

# 修改配置文件
postgre@postgresqlunbuntu:~/PGDev$ vim env-debug
PGHOME=/home/postgre/PGDev/pghome
export PGDATA=$PGHOME/../data
export PGHOST=127.0.0.1
export PGPORT=5432
export LD_LIBRARY_PATH=$PGHOME/lib
export PATH=$PGHOME/bin:$PATH

# 将前面配置好的路径导入当前shell的环境变量中,没有任何输出则表示执行成功
postgre@postgresqlunbuntu:~/PGDev$ source env-debug

# 可以将上面的配置文件内容写到 ~/.bashrc 中,不然重启服务器之后,需要再手动 source env-debug
postgre@postgresqlunbuntu:~/PGDev$ vim ~/.bashrc
postgre@postgresqlunbuntu:~/PGDev$ source ~/.bashrc

配置

postgre@postgresqlunbuntu:~/PGDev$ cd postgresql-12.5/

# 配置:确保接下来的构建和安装过程所需要的依赖准备好,并且搞清楚使用这些依赖需要的东西
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ ./configure --prefix=$PGHOME --enable-debug

构建

postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ make

安装

# 需要加sudo,否则权限会受限制,报错/usr/bin/install: cannot create regular file '/lib/libpgcommon.a': Permission denied
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ sudo make install

# 但是再次执行还是报错
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ sudo make install
[sudo] password for postgre:
postgre is not in the sudoers file.  This incident will be reported.

# 切换到root用户下,/etc/sudoers文件默认是只读的,对root来说也是,因此需先添加sudoers文件的写权限
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ su root
root@postgresqlunbuntu:/home/postgre/PGDev/postgresql-12.5# chmod u+w /etc/sudoers

# 然后就可以编辑sudoers文件,找到这行 root ALL=(ALL:ALL) ALL  ,在他下面添加 xxx ALL=(ALL) ALL  (这里的xxx是你的用户名).
root@postgresqlunbuntu:/home/postgre/PGDev/postgresql-12.5# vim /etc/sudoers
root    ALL=(ALL:ALL) ALL
postgre ALL=(ALL) ALL

# 撤销sudoers文件写权限
root@postgresqlunbuntu:/home/postgre/PGDev/postgresql-12.5# chmod u-w /etc/sudoers

# 然后可以成功执行安装操作
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ sudo make install

运行

# 初始化数据库
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ initdb

# 启动PostgreSQL服务
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$  pg_ctl -D /home/postgre/PGDev/data/ -l logfile start

# 停止PostgreSQL服务,如果要关闭服务器的话,一定要关闭PostgreSQL服务,否则下次启动会出错。
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ pg_ctl -D /home/postgre/PGDev/data/ -l logfile stop

# 重启PostgreSQL服务
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ pg_ctl -D /home/postgre/PGDev/data/ -l logfile restart

# 查看PostgreSQL状态
postgre@postgresqlunbuntu:~/PGDev/postgresql-12.5$ pg_ctl -D /home/postgre/PGDev/data/ -l logfile status

# 创建数据库uni2
postgre@postgresqlunbuntu:~/PGDev$ createdb uni2

# 进入数据库
postgre@postgresqlunbuntu:~/PGDev$ psql uni2

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表