最近有使用到,总结有些许简单
安装
root@localhost ~]# yum install gdb
配置 PostgreSQL
./configure -prefix=/usr/local/pgsql --enable-debug
[postgres@localhost ~]$ vi /opt/postgresql-9.5.3/src/Makefile.global
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -O2
上面的行的"-O2"选项删除,然后加上"-g"
"-O2"是编译器的优化选项,如果打开了,代码的执行顺序会改变,使得追踪起代码来比较困难,所以要去除。
gdb 调试程序的时候打印变量值出现<value optimized out>就是由于没有去掉 “-O2”
[postgres@localhost ~]$ ps x
PID TTY STAT TIME COMMAND
2819 pts/1 S 0:00 -bash
2845 pts/1 S 0:00 /usr/local/pgsql9.5.3/bin/postgres -D /usr/local/pgsql/data
2847 ? Ss 0:00 postgres: checkpointer process
2848 ? Ss 0:00 postgres: writer process
2849 ? Ss 0:00 postgres: wal writer process
2850 ? Ss 0:00 postgres: autovacuum launcher process
2851 ? Ss 0:00 postgres: stats collector process
2853 pts/1 S+ 0:00 psql
2854 ? Ss 0:00 postgres: postgres postgres [local] idle
2871 pts/0 S 0:00 -bash
2890 pts/0 R+ 0:00 ps x
[postgres@localhost ~]$ gdb postgres 2854
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-92.el6)
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/>...
Reading symbols from /usr/local/pgsql9.5.3/bin/postgres...done.
Attaching to program: /usr/local/pgsql9.5.3/bin/postgres, process 2854
Reading symbols from /usr/lib64/libxml2.so.2...(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/libxml2.so.2
Reading symbols from /lib64/libpam.so.0...(no debugging symbols found)...done.
Loaded symbols for /lib64/libpam.so.0
Reading symbols from /usr/lib64/libssl.so.10...(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/libssl.so.10
。。。
Loaded symbols for /lib64/libfreebl3.so
Reading symbols from /lib64/libselinux.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libselinux.so.1
Reading symbols from /lib64/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libnss_files.so.2
0x0000003ffc2df248 in poll () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64
(gdb)
解决
1、需要先修改“/etc/yum.repos.d/CentOS-Debuginfo.repo”文件的enable=1;
2、使用 yum install glibc 安装;
3、使用 debuginfo-install glibc-2.12-1.192.e16.x86_64 安装。
上面是调试进程的。
另一种调试方式
core文件
[postgres@localhost ~]$ pg_ctl restart -c
[postgres@localhost ~]$ cd /usr/local/hgdb-core/data
[postgres@localhost data]$ pwd
/usr/local/hgdb-core/data
[postgres@localhost data]$ ls
base core.16142 pg_commit_ts pg_logical pg_serial pg_subtrans pg_xlog postmaster.pid
core.11229 core.17967 pg_dynshmem pg_multixact pg_snapshots pg_tblspc postgresql.auto.conf
core.14912 global pg_hba.conf pg_notify pg_stat pg_twophase postgresql.conf
core.14924 pg_clog pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postmaster.opts
[postgres@localhost data]$ gdb postgres core.16142
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-92.el6)
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/>...
Reading symbols from /usr/local/hgdb-core/bin/postgres...done.
warning: exec file is newer than core file.
[New Thread 16142]
Reading symbols from /lib64/librt.so.1...Reading symbols from /usr/lib/debug/lib64/librt-2.12.so.debug...done.
done.
Loaded symbols for /lib64/librt.so.1
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/libpthread.so.0...Reading symbols from /usr/lib/debug/lib64/libpthread-2.12.so.debug...done.
[Thread debugging using libthread_db enabled]
done.
Loaded symbols for /lib64/libpthread.so.0
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
Core was generated by `postgres: postgres postgres [local] SELECT '.
Program terminated with signal 11, Segmentation fault.
#0 0x000000000087f1ab in regexp_match_str (fcinfo=0x0) at myfuncs.c:524
524 orig_len = VARSIZE_ANY_EXHDR(orig_str);
(gdb)
这个core文件只有在内存错误时才产生
也就是数据库崩溃时
其他错误时 不会生成core文件
根据core文件 就能跟踪到内存错误的地方 比如这里的524行