使用GDB和DDD調試POSTGRESQL

之前一直用Eclipse CDT調試postgresql,有時候斷點不許,很faint,心想是否是GDB和DDD斷點就準了呢。因此決定鼓搗鼓搗GDB和DDD。關於如何搭建CDT的調試環境,請參考:
  1. 我寫過一篇如何在Ubuntu裏搭建hack環境:postgresql8.4+postgis1.5+eclipse CDT3.6 調試環境搭建,http://blog.chinaunix.net/u2/81513/showart_2168880.html
  2. http://wiki.postgresql.org/wiki/Working_with_Eclipse
先說說GDB和DDD的區別:
官方: http://www.gnu.org/software/ddd/  上講,GNU DDD is a graphical front-end for command-line debuggers such as  GDB DBX , WDB,  Ladebug , JDB, XDB,  the Perl debugger ,..       So,不用俺解釋了。
下文都是在Ubuntu中調試,先說好,沒耍賴的。

因此咱就先從如何用GDB調試PostgreSQL開始談起:
首先,咱們得定義一下,咱們知道Postmaster啓動做爲後端damon進程,等待前端如PSQL的請求,而後Fork()一個Postgres進程來處理該請求,因此咱們在服務器上就有Postmaster和處理該請求的Postgres (一個請求一個該進程)了,那麼咱們該調試哪一個呢?是Postmaster仍是Postgre呢?因此有以下定義(轉載於  http://www-inst.eecs.berkeley.edu/~cs186/fa04/usingddd.pdf  ):
There are two ways to debug postgres (a) in the interactive mode and (b) in the bare backend mode. The
interactive mode is where you start up the postmaster process (with pg ctl), use psql to connect to it, which
spawns off a separate postgres process that is dedicated to serving requests from the psql client. This is the
mode that the scripts in the Hw1/exec directory use, and the mode you used in Hw0. In the second mode, you
don’t create a separate postmaster process and don’t use psql at all. Instead, you start up postgres from the
command line and directly interact with it. While the latter is a very user-unfriendly way of using postgres it has
the advantage that it is very easy to use a debugger (like ddd) with it.
原文講的很清楚了,爲了再清楚一點,我稍做解釋,調試分紅兩類,一類是 interactive mode 二類是bare backend mode。第一類是先啓動Postmaster,再啓動Postgres,而後調試Postgres。第二類是隻啓動Postgres,直接調試Postgres。
我發現第一類比較符合CDT的狀況,並且上面所引的文章中主要講了第二類,因此第二類咱們就無論了。因此結論是咱們調試第一類的Postgres,而若是你對Postmaster的調試也感興趣,其實更簡單了,拔高一籌理論上講豈不是和第二類同樣了。

1.在Ubuntu中先添加postgres帳號,之後所有用該帳號登錄:關於如何添加postgres帳號,請參考: http://blog.chinaunix.net/u2/81513/showart.php?id=2168880  第3條.而後:
sudo mkdir /usr/local/pgsql
sudo chown postgres /usr/local/pgsql

2.在Ubuntu中先下載必要的依賴文件,並安裝:
先安裝:libreadline6-dev
sudo apt-get install libreadline6-dev
而後安裝zlib
tar zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3/
make
sudo make install

3.下載PostgreSQL源碼,例如放在~/develop目錄下面:
tar zxvf postgresql-8.4.3.tar.gz
cd postgresql-snapshot/
./configure --enable-depend --enable-cassert --enable-debug
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data -- 初始化系統數據庫
而後建立本身的數據庫(mydb是數據庫名)
/usr/local/pgsql/bin/createdb mydb 
而後啓動postmaster
/usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data


4.新開一個Terminal,開啓前端psql連mydb
psql mydb
select pg_backend_pid();
是否是出現:
 pg_backend_pid 
----------------
           2184
把該進程id記下來。

5.啓動GDB調試進程progres 2184(記得GDB在UBUNTU裏是系統自帶的,因此不用再安裝了)
gdb progres 2184;

O(∩_∩)O哈哈哈~,至此是否是GDB界面出來了:

GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 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 "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
progres: No such file or directory.
Attaching to process 2184
ptrace: Operation not permitted.
/home/postgres/2184: No such file or directory.
(gdb) 

關於如何使用GDB,我就不介紹了,看官方文檔: http://www.gnu.org/software/gdb/documentation/


接下來寫一下如何用DDD調試postgresql。
前4步和GDB的調試相似。
第5步,安裝DDD
sudo apt-get install ddd
第6步,啓動DDD調試
ddd postgres 2184;
至此,DDD圖形化的界面是否是出來了,上張圖,慶祝一下:

第7步,在第4步的psql窗口裏敲入sql命令:
create table weather(tmp_lo int);

看看在DDD窗口中有什麼變化,你按一下F6看看有什麼效果,是否是和ECLIPSE的F6同樣,F5也同樣滴...接下來如何設置斷點,如何跟蹤,就參考官方文檔吧: http://www.gnu.org/manual/ddd/html_mono/ddd.html 

在文章最後給出個人結論: 我不推薦用GDB和DDD調試PostgreSQL,推薦用ECLIPSE CDT調試。緣由就是ECLIPSE的調試結果和前兩者同樣,可是更容易看文件結構呀。 
相關文章
相關標籤/搜索