- 我写过一篇如何在Ubuntu里搭建hack环境:postgresql8.4+postgis1.5+eclipse CDT3.6 调试环境搭建,http://blog.chinaunix.net/u2/81513/showart_2168880.html
- http://wiki.postgresql.org/wiki/Working_with_Eclipse
官方: http://www.gnu.org/software/ddd/ 上讲,GNU DDD is a graphical front-end for command-line debuggers such as GDB , DBX Ladebug the Perl debugger 下文都是在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的调试结果和前二者一样,但是更容易看文件结构呀。