天天看點

gdb調試postgresql(待續)

http://blog.csdn.net/bhq2010/article/details/12200533

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

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

http://blog.163.com/[email protected]/blog/static/163877040201252605924116/

http://blog.163.com/[email protected]/blog/static/16387704020121125113226736/

示範:

1:建立linux使用者

[[email protected] postgresql-9.3.4]# adduser digoal
           

2:配置postgresql

修改調試項:

[[email protected] src]# pwd
/usr/local/rj/postgresql-9.3.4/src
[[email protected] 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調試:

[[email protected] postgresql-9.3.4]# pwd
/usr/local/rj/postgresql-9.3.4
           
[[email protected] 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:編譯

[[email protected] postgresql-9.3.4]# gmake world
           

4:安裝

[[email protected] postgresql-9.3.4]# gmake install-world
           

5:建立資料目錄

[[email protected] ~]# mkdir -p /home/digoal/data
[[email protected] ~]# chown -R digoal /opt/pgsql_934
[[email protected] ~]# 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

[d[email protected] ~]$ psql -h localhost -p 5432 postgres postgres
           

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

[digo[email protected] ~]$ pg_ctl start -D $PGDATA
[[email protected] ~]$ 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
[[email protected] ~]$ 
           

其以程序id為34446進行debug調試。

9:開始gdb

[[email protected] ~]$ 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>, buf=0xb110a0, n=8192, 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",則修改

[[email protected] ~]# 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:

參見gdb的指令 http://blog.csdn.net/bielidefeng/article/details/28896651

繼續閱讀