gdb调试postgresql(待续)

前端之家收集整理的这篇文章主要介绍了gdb调试postgresql(待续)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.jb51.cc/article/p-euufifjc-mv.html

http://wiki.postgresql.org/wiki/Pgsrcstructure

http://wiki.postgresql.org/wiki/Working_with_Eclipse

http://blog.163.com/digoal@126/blog/static/163877040201252605924116/

http://blog.163.com/digoal@126/blog/static/16387704020121125113226736/



演示:

1:建立linux用户

[root@localhost postgresql-9.3.4]# adduser digoal

2:配置postgresql

修改调试项:
[root@localhost src]# pwd
/usr/local/rj/postgresql-9.3.4/src
[root@localhost src]# vi Makefile.global
CFLAGS = -O2 -Wall -Wmissing-prototypes -Wpointer-arith \
-Wdeclaration-after-statement -Wendif-labels -Wformat-security \
-fno-strict-aliasing -fwrapv

上面的行的"-O2"选项删除,然后加上"-g"

CFLAGS = -g -Wall -Wmissing-prototypes -Wpointer-arith \
-Wdeclaration-after-statement -Wendif-labels -Wformat-security \
-fno-strict-aliasing -fwrapv

"-O2"是编译器的优化选项,如果打开了,代码的执行顺序会改变,使得追踪起代码来比较困难,所以要去除。当然这样的话,编译后的可执行文件会比较大,而且会比较慢,生产环境不太合适。大家需要理解这个操作仅仅是在学习的时候而设置的。

需要含有“--enable-debug”,才可以对postgresql进行debug调试:
[root@localhost postgresql-9.3.4]# pwd
/usr/local/rj/postgresql-9.3.4
[root@localhost postgresql-9.3.4]# ./configure --prefix=/home/digoal/pgsql91 --with-pgport=1999 --with-perl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --enable-thread-safety --with-wal-blocksize=16 --enable-debug
如果报错“configure: error: library 'pam' is required for PAM”则安装
yum install pam pam-devel

如果报错“configure: error: library 'xslt' is required for XSLT support”则安装
yum install libxslt-devel

如果报错“configure: error: header file <Python.h> is required for Python”,则安装
yum install python-devel

在使用此配置“./configure --......”配置进行安装后,在使用psql的时候报错会出现“Segmentation fault (core dumped)”的现象,然后将其改为了“./configure --prefix=/opt/pgsql_934 --enable-debug”,总之去掉其中的“--with-openssl --with-pam”这部分配置即可,之后就可以正常使用psql -h localhost -p 5432 postgres postgres进行访问了。

3:编译

[root@localhost postgresql-9.3.4]# gmake world

4:安装

[root@localhost postgresql-9.3.4]# gmake install-world

5:建立数据目录

[root@localhost ~]# mkdir -p /home/digoal/data
[root@localhost ~]# chown -R digoal /opt/pgsql_934
[root@localhost ~]# chown -R digoal /home/digoal/data

6:初始化环境变量

<pre name="code" class="sql">export PGPORT=5432
export PGDATA=/home/digoal/data
export PGPHOME=/opt/pgsql_934
export LANG=en_US.utf8
export PGHOME=/opt/pgsql_934
export LD_LIBRARY_PATH=$PGHOME/lib:/opt/pgpool/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.:/usr/local/rj/postgresql-9.3.4/src
export PATH=/opt/pgpool/bin:$PATH:.:/usr/local/rj/postgresql-9.3.4/src
export PATH=/opt/pgsql_934/bin:$PATH
export MANPATH=$PGHOME/share/man:$MANPATH
export MANPATH=/opt/pgpool/share/man:$MANPATH
export PGUSER=postgres
export PGHOST=$PGDATA
alias rm='rm -i'
alias ll='ls -lh'
export PGDATABASE=digoal


 
  
 
 

7:初始化数据库

initdb -D $PGDATA -E UTF8 --locale=C -W -U postgres

8:启动数据库获取进程id

[digoal@localhost ~]$ psql -h localhost -p 5432 postgres postgres
postgres=# select pg_backend_pid(); pg_backend_pid ---------------- 34446(1 row)
 
[digoal@localhost ~]$ pg_ctl start -D $PGDATA
[digoal@localhost ~]$ netstat -anp|grep post
tcp    0   0 127.0.0.1:5432       0.0.0.0:*          LISTEN   34353/postgres   
tcp    0   0 ::1:5432          :::*            LISTEN   34353/postgres   
tcp    0   0 ::1:5432          ::1:57310          ESTABLISHED 34446/postgres: pos
udp    0   0 ::1:58044          ::1:58044          ESTABLISHED 34353/postgres   
unix 2   [ ACC ]   STREAM   LISTENING   143676 34353/postgres   /tmp/.s.PGsql.5432
[digoal@localhost ~]$ 

其以进程id为34446进行debug调试。

9:开始gdb

[digoal@localhost ~]$ gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
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 "x86_64-redhat-linux-gnu".
For bug reporting instructions,please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) attach 34446
Attaching to process 34446
Reading symbols from /opt/pgsql_934/bin/postgres...done.
Reading symbols from /lib64/libdl.so.2...Reading symbols from /usr/lib/debug/lib64/libdl-2.12.so.debug...done.
done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/libm.so.6...Reading symbols from /usr/lib/debug/lib64/libm-2.12.so.debug...done.
done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libc.so.6...Reading symbols from /usr/lib/debug/lib64/libc-2.12.so.debug...done.
done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug/lib64/ld-2.12.so.debug...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /lib64/libnss_files.so.2...Reading symbols from /usr/lib/debug/lib64/libnss_files-2.12.so.debug...done.
done.
Loaded symbols for /lib64/libnss_files.so.2
0x0000003ebb6e98d2 in __libc_recv (fd=<value optimized out>,buf=0xb110a0,n=8192,flags=0) at ../sysdeps/unix/sysv/linux/x86_64/recv.c:30
30     return INLINE_SYSCALL (recvfrom,6,fd,buf,n,flags,NULL,NULL);
(gdb) b ExecResult
Breakpoint 1 at 0x594c10
(gdb) bt
#0 0x0000003ebb6e98d2 in __libc_recv (fd=<value optimized out>,flags=0) at ../sysdeps/unix/sysv/linux/x86_64/recv.c:30
#1 0x00000000005a1420 in secure_read ()
#2 0x00000000005a99ab in pq_recvbuf ()
#3 0x00000000005a9dab in pq_getbyte ()
#4 0x0000000000650f11 in PostgresMain ()
#5 0x000000000060ee55 in PostmasterMain ()
#6 0x00000000005aba20 in main ()
(gdb) b main
Breakpoint 2 at 0x5ab6b0
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /opt/pgsql_934/bin/postgres


Breakpoint 2,0x00000000005ab6b0 in main ()
(gdb) l
1    /* Run time dynamic linker.
2     Copyright (C) 1995-2006,2007,2008,2009,2010 Free Software Foundation,Inc.
3     This file is part of the GNU C Library.
4
5     The GNU C Library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License,or (at your option) any later version.
9
10     The GNU C Library is distributed in the hope that it will be useful,(gdb)
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with the GNU C Library; if not,write to the Free
17     Software Foundation,Inc.,59 Temple Place,Suite 330,Boston,MA
18     02111-1307 USA. */
19
20   #include <errno.h>
(gdb)
21   #include <dlfcn.h>
22   #include <fcntl.h>
23   #include <stdbool.h>
24   #include <stdlib.h>
25   #include <string.h>
26   #include <unistd.h>
27   #include <sys/mman.h>
28   #include <sys/param.h>
29   #include <sys/stat.h>
30   #include <ldsodefs.h>
(gdb)
31   #include <stdio-common/_itoa.h>
32   #include <entry.h>
33   #include <fpu_control.h>
34   #include <hp-timing.h>
35   #include <bits/libc-lock.h>
36   #include "dynamic-link.h"
37   #include <dl-librecon.h>
38   #include <unsecvars.h>
39   #include <dl-cache.h>
40   #include <dl-osinfo.h>
(gdb)
41   #include <dl-procinfo.h>
42   #include <tls.h>
43   #include <stap-probe.h>
44
45   #include <assert.h>
46
47   /* Avoid PLT use for our local calls at startup. */
48   extern __typeof (__mempcpy) __mempcpy attribute_hidden;
49
50   /* GCC has mental blocks about _exit. */
(gdb)
51   extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
52   #define _exit exit_internal
53
54   /* Helper function to handle errors while resolving symbols. */
55   static void print_unresolved (int errcode,const char *objname,


如果报错"Missing separate debuginfos,use: debuginfo-install glibc-2.12-1.132.el6.x86_64",则修改
[root@localhost ~]# vi /etc/yum.repos.d/CentOS-Debuginfo.repo
修改内容enable=1。
并安装nss-softokn-debuginfo
yum install nss-softokn-debuginfo
然后再运行如下安装glibc-2.12-1.132.el6.x86_64
debuginfo-install glibc-2.12-1.132.el6.x86_64

然后进行运行gdb:

猜你在找的Postgre SQL相关文章