目录
一、编译安装Postgre
二、使用GDB调试
三、修改源码并且编译运行
四、在源文件中添加头文件编译运行
[root@bogon ~]# yum install -y zlib-devel
[root@bogon ~]# yum install -y readline-devel
[root@bogon ~]# yum install -y gcc gdb
[root@bogon ~]# useradd postgres
[root@bogon ~]# passwd postgres
[root@bogon ~]# su postgres
[postgres@bogon ~]$ tar -zxvf postgresql-12.0
[postgres@bogon ~]$ cd postgresql-12.0
[postgres@bogon postgresql-12.0]$ ./configure --prefix=/home/postgres/install --with-pgport=5432 --enable-debug
注意:这里一一定要把 --enable-debug 参数加上
在编译过程中可以把gcc的参数从O2 改为O0 ,关闭所有优化选项 防止编译器优化,优化后有些代码在调试过程中可能就看不到了
[postgres@bogon postgresql-12.0]$ make
[postgres@bogon postgresql-12.0]$ make clean
[postgres@bogon postgresql-12.0]$ vi src/Makefile.global
[postgres@bogon postgresql-12.0]$ make
[postgres@bogon postgresql-12.0]$ make install
[postgres@bogon bin]$ cd /home/postgres/install/bin/
[postgres@bogon bin]$ ./initdb -D /home/postgres/install/data/
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ -l logfile start
[postgres@bogon bin]$ ./psql
postgres=# create table test(id varchar(10),name varchar(100) );
CREATE TABLE
postgres=# insert into test(id,name) values('1','aaa');
INSERT 0 1
postgres=# select * from test;
id | name
----+------
1 | aaa
(1 row)
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
26239
(1 row)
[postgres@bogon ~]$ gdb -p 26239
(gdb)b GetNewTransactionId
注意:出现函数的地址才算成功,如果下断点后不显示函数地址显示pending,可能是普通用户对该进程没有权限导致,可以换超级用户试试。(如果是在Ubuntu上,可以修改/etc/sysctl.d/10-ptrace.conf 将最后一行改为 kernel.yama.ptrace_scope = 0)
(gdb) c
[postgres@bogon ~]$ cd /home/postgres/postgresql-12.0/src/backend/access/transam/
[postgres@bogon transam]$ vi varsup.c
注意:这里我们使用make的方式进行编译,而不是直接用gcc对源文件varsup.c进行编译,如果直接用gcc编译 会报链接库有问题,链接库找了很久编译才过去,但是执行的时候还是有问题,所以还是直接用make,方便好用不容易出问题
[postgres@bogon transam]$ make
注意:这里make install 一定要指定目录,不然文件复制到哪里去了我也没找到~
[postgres@bogon postgresql-12.0]$ cd /home/postgres/postgresql-12.0
[postgres@bogon postgresql-12.0]$ make install DESTDIR=/
可执行文件的修改时间变了,说明是刚复制过来的
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ stop
waiting for server to shut down.... done
server stopped
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ start
waiting for server to start....2022-03-20 02:21:57.017 CST [27163] LOG: starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2022-03-20 02:21:57.019 CST [27163] LOG: listening on IPv6 address "::1", port 5432
2022-03-20 02:21:57.019 CST [27163] LOG: listening on IPv4 address "127.0.0.1", port 5432
2022-03-20 02:21:57.020 CST [27163] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-03-20 02:21:57.036 CST [27164] LOG: database system was shut down at 2022-03-20 02:21:52 CST
2022-03-20 02:21:57.038 CST [27163] LOG: database system is ready to accept connections
done
server started
[postgres@bogon bin]$ ./psql
psql (12.0)
Type "help" for help.
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
27172
(1 row)
上面这种方式是把函数直接写在源文件中的,实际上我们会有自己的源文件和头文件,下面就是自己写一个源文件和头文件,然后在源码中引用我们的头文件,验证是否可行
[postgres@bogon include]$ cd /home/postgres/postgresql-12.0/src/include
[postgres@bogon include]$ mkdir mycode
[postgres@bogon include]$ cd mycode/
[postgres@bogon mycode]$ vim mycode.h
[postgres@bogon mycode]$ vim mycode.c
C文件:
头文件:
[postgres@bogon mycode]$ vi /home/postgres/postgresql-12.0/src/backend/access/transam/varsup.c
[postgres@bogon mycode]$ cd /home/postgres/postgresql-12.0/src/backend/access/transam/
[postgres@bogon transam]$ make clean
[postgres@bogon transam]$ make
[postgres@bogon transam]$ cd /home/postgres/postgresql-12.0
[postgres@bogon transam]$ make install DESTDIR=/
[postgres@bogon bin]$ cd /home/postgres/install/bin/
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ stop
waiting for server to shut down.... done
server stopped
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ start
waiting for server to start....2022-03-20 03:24:30.138 CST [28438] LOG: starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2022-03-20 03:24:30.139 CST [28438] LOG: listening on IPv6 address "::1", port 5432
2022-03-20 03:24:30.139 CST [28438] LOG: listening on IPv4 address "127.0.0.1", port 5432
2022-03-20 03:24:30.140 CST [28438] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-03-20 03:24:30.157 CST [28439] LOG: database system was shut down at 2022-03-20 03:24:23 CST
2022-03-20 03:24:30.158 CST [28438] LOG: database system is ready to accept connections
done
server started
[postgres@bogon bin]$ ./psql
psql (12.0)
Type "help" for help.
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
28447
(1 row)
postgres=# begin;
BEGIN
postgres=# update test set name='mycode' where id ='1';
gnaixgnahz
test c:3
mycode test d=6
UPDATE 1
postgres=# commit;
COMMIT
postgres=# quit;