当前位置:网站首页>postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
2022-07-01 07:54:00 【Hehuyi_In】
本篇纪念小白第一次用开发工具调试源码成功~
gdb使用命令行调试感觉还是有诸多不便,本来想简单点找找怎么直接调试Windows下的pg源码,找了半天只有用vs,太重而且步骤比较复杂。大多数实际环境还是运行在Linux的,所以最好还是调试Linux下的pg源码。
- 目标端:centos 7+postgresql 14
- 源端:Windows 10 + vscode
一、 postgresql 14源码编译与安装
- 之前写过pg用于调试的源码安装方法,这里是一样的(所以如果之前有装好可以就用这个环境)。特别要注意的就是编译的时候要加--enable-debug,不然是调试不了的。
postgresql源码学习(一)—— 源码编译安装与gdb调试入门_Hehuyi_In的博客-CSDN博客_postgresql 源码调试
安装完之后试一下里面gdb调试的例子,因为后面的工具也会依赖gdb。如果执行没问题,就可以继续往下走了。
二、 安装vscode
下载链接:Visual Studio Code - Code Editing. Redefined
如果速度太慢,国内也有大量网站提供下载,安装过程下一步下一步即可。
1. 本地安装vscode插件

装下面3个:
- C/C++
- C/C++ Runner:编译、运行、debug
- Remote Development:远程开发。它安装了三个插件:Remote-SSH,Remote-Container,Remote-WSL,如果只需要ssh连接,只装Remote-SSH就可以。

2. 测试远程连接
在Remote Development插件装完后,左下角会多一个小电脑图标,点它,然后新建ssh连接。按照提示在框里填 ssh [email protected]

配好后点旁边的小加号,会弹出新界面让输密码

第一次进去的时候它会在远端服务器装vscode,会比较慢,它在/root下面创建一个隐藏目录vscode。

如果安装遇到报错 Resolver error: Error: XHR failed,有可能是网络问题,把linux上的.vscode目录删掉,重试几次即可。
连接成功之后可以看到Linux的命令行

三、 配置远端调试pg
打开源码src目录,创建.vscode目录,以及launch.json文件。直接用Linux命令行也可以,通过vscode建也可以

launch.json文件内容如下
{
"version": "0.2.0",
"configurations": [
{
"name": "postgresql",
"type": "cppdbg",
"request": "attach",
"program": "/data/postgres/base/14.4/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
}
]
}
]
}唯一要改的地方是"program": "/data/postgres/base/14.4/bin/postgres",换成实际的路径。
另外远端vscode需要安装json插件,否则会报错不能解析该文件。
顺便看了下远端都装了哪些插件,除了json和C/C++ Extension Pack,其他貌似都是自动装的。

如果你遇到这个报错,说不能识别cppdbg,说明没有装C/C++ Runner插件,装上就可以。

四、远程调试测试
我们以最简单的begin操作为例,跟踪StartTransactionCommand函数执行情况。这个函数在 /root/postgresql-14.4/src/backend/access/transam/xact.c 文件中
vscode端:
找到xact.c 文件中的StartTransactionCommand函数,打上断点

linux端:
psql创建连接,查询进程号
select pg_backend_pid();

vscode端:
开始debug,快捷键F5

attach对应进程

会看到右下角有这样的输出

linux端:
执行一个begin命令,发现它会卡住
vscode端:
光标跳到了对应函数,说明已经开始调试了,可以点上面的小箭头,一步步看。

调试界面主要区域如下:
- variable:每一步执行时对应的变量值,这个非常有用
- call stack:调用栈,从下往上看
- 代码中发光的那行是当前执行到的行

通过代码和变量值的对比,很容易看出下一步应该进哪个case,然后再执行什么。

至此,我们的调试测试就成功啦,迈出了pg源码学习万里长征的第一步~
参考
https://blog.csdn.net/loveoobaby/article/details/123765071?spm=1001.2014.3001.5501
https://blog.csdn.net/loveoobaby/article/details/123766468
https://blog.csdn.net/Hehuyi_In/article/details/110729822
https://blog.csdn.net/Hehuyi_In/article/details/124524307
https://blog.csdn.net/wangyjfrecky/article/details/124681577
边栏推荐
- [batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
- 【mysql学习笔记27】存储过程
- 【mysql学习笔记28】存储函数
- 2022茶艺师(初级)操作证考试题库及模拟考试
- How to get a SharePoint online site created using the office365 group template
- Android screen adaptation (using constraintlayout), kotlin array sorting
- 2022 electrician (intermediate) recurrent training question bank and answers
- AUTOSAR learning record (1) – ECUM_ Init
- Lm08 mesh series mesh inversion (fine)
- Do securities account opening affect the security of account opening
猜你喜欢

SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
![[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion](/img/ce/6c9e4f2c54710610e8b1f68d6d8088.png)
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
![Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list](/img/9a/44976b5df5567a7aff315e63569f6a.png)
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list

Significance and measures of source code encryption

2022茶艺师(初级)操作证考试题库及模拟考试

Download xshell and xftp

奥迪AUDI EDI 项目中供应商需要了解哪些信息?
![[programming training 2] sorting subsequence + inverted string](/img/96/87750c5d3954ef6c39cce073e8b9ae.png)
[programming training 2] sorting subsequence + inverted string

kubernetes资源对象介绍及常用命令(二)

Teach you how to apply for domestic trademark online step by step
随机推荐
Vhost kick & call principle
【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
How to create an exclusive vs Code theme
Kickback -- find the first palindrome character in a group of characters
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
[programming training 2] sorting subsequence + inverted string
How outlook puts together messages with the same discussion
十大劵商如何开户?另外,手机开户安全么?
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
The database is locked. Is there a solution
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
Teach you how to apply for domestic trademark online step by step
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
Software testing methods and techniques - overview of basic knowledge
华泰证券开户是安全可靠的么?怎么开华泰证券账户
Significance and measures of source code encryption
华为ModelArts训练Alexnet模型
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?