linux下postgresql數據庫debug環境搭建

1.準備

  下載postgresql安裝文件,從http://www.postgresql.org/download/下載需要的版本

安裝readline(非必須)。

如果需要使用zlib,ssl等則需要先安裝zlib,ssl庫,不需要使用則可以不安裝。

2. 創建用戶

groupadd postgres

useradd -g postgres postgres

3.安裝

安裝什麼特別的,就是三板斧 configure,make,make install

我要把pg安裝在/data/pg_debug目錄下。

       ./configure --prefix=/data/pg_debug/ CFLAGS="-O0" --without-readline --without-zlib

        關於參數可以使用./configure -h查看說明,這裏不一一解釋。說明下使用的參數含義:

        --prefix=/data/pg_debug/  是將pg安裝在/data/pg_debug/ 目錄下,如果不指定這個參數的話默認安裝在/usr/local/pgsql目錄下面

  CFLAGS="-O0" 是指gcc編譯時不使用優化

--without-readline --without-zlib 不使用readline和zlib庫(建議大家安裝readline庫,這樣在命令psql命令行下可以自動不齊,我沒有找到對應的版本所以沒有安裝)。

make

make install

安裝就算完成。接下來初始化DB並啓動pg:

localhost:/data/pg_debug # ls
bin  include  lib  share
localhost:/data/pg_debug # mkdir data
localhost:/data/pg_debug # chown postgres.postgres ./data
localhost:/data/pg_debug # su postgres
postgres@localhost:/data/pg_debug> /data/pg_debug/bin/initdb -D /data/pg_debug/data/
postgres@localhost:/data/pg_debug> /data/pg_debug/bin/pg_ctl start -D /data/pg_debug/data/
server starting


4。調試源碼:

調試使用gdb,我使用的vi+gdb。

使用psql登錄pg,並查詢進程號:

postgres@localhost:/data/pg_debug> /data/pg_debug/bin/psql
psql (9.1rc1)
Type "help" for help.

postgres=# select pg_backend_pid();
 pg_backend_pid 
----------------
          21099
(1 row)


在另一終端裏面使用gdb調試,並在be-secure.c的第305行設置斷點:

localhost:/home/vince # gdb /data/pg_debug/bin/postgres 21099
GNU gdb (GDB) SUSE (7.2-3.3)
Copyright (C) 2010 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.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-suse-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data/pg_debug/bin/postgres...done.
Attaching to program: /data/pg_debug/bin/postgres, process 21509
Reading symbols from /lib/libdl.so.2...Missing separate debuginfo for /lib/libdl.so.2
Try: zypper install -C "debuginfo(build-id)=785eb60d6a7a2b4828cd93c6738f00065322f20d"
(no debugging symbols found)...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/libm.so.6...Missing separate debuginfo for /lib/libm.so.6
Try: zypper install -C "debuginfo(build-id)=f8e95f6424bafd41f505d4a5b113c5100ffa03be"
(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libc.so.6...Missing separate debuginfo for /lib/libc.so.6
Try: zypper install -C "debuginfo(build-id)=6478c346f66a284b77eb5ca82ab8f2f4f9561600"
(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...Missing separate debuginfo for /lib/ld-linux.so.2
Try: zypper install -C "debuginfo(build-id)=b6b00f5560b849cf9fac5e6efb9f403c21f508dd"
(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
0xffffe430 in __kernel_vsyscall ()
(gdb) b  be-secure.c:305

在psql中:

postgres=# create table test3(a int);

在gdb中:

(gdb) b  be-secure.c:305
Breakpoint 1 at 0x81ddb81: file be-secure.c, line 305.
(gdb) c
Continuing.

Breakpoint 1, secure_read (port=0x851f680, ptr=0x84c9280, len=8192) at be-secure.c:305
305			client_read_ended();
(gdb) n
309	}
(gdb) n
pq_recvbuf () at pqcomm.c:819
819			if (r < 0)
(gdb) 

要調試其他代碼也採用同樣的方法了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章