hillpig的个人博客分享 http://blog.sciencenet.cn/u/hillpig 畅想ing,思考ing,前行ing Email:bluevaley@gmail.com

博文

使用GDB和DDD调试POSTGRESQL

已有 12904 次阅读 2010-3-28 22:23 |个人分类:postgresql|系统分类:科研笔记| 调试, GDB, DDD

决定今后在科博上写postgresql的文章,cu上以后主要写一些lfs的东西。好听点就是不把鸡蛋们放到一个篮子里,我发现为了一个目的,我们总能找到理由来发扬该目的的长处。

以前一直用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的调试结果和前二者一样,但是更容易看文件结构呀。


加我私人微信,交流技术。




https://blog.sciencenet.cn/blog-419883-306975.html

上一篇:database course
下一篇:Postmaster的Shared Memory中的shmem index table 内存结构
收藏 IP: 223.72.72.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-14 07:27

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部