1. 程式人生 > 其它 >PG-瞭解gdb除錯

PG-瞭解gdb除錯

通過gdb除錯postgresql程序

GDB 除錯

先要在編譯PostgreSQL時開啟enable-debug

1. 1檢視資料庫程序

-- 檢視程序號,在資料庫中執行以下語句
select pg_backend_pid(); -- pid = 3715
select 1;

1.2 gdb 除錯

啟動gdb程序

# gdb attach 程序號
gdb attach 3715

設定相應的斷點進行單步除錯

# 輸入b命令的話,在ExecResult可以設定斷點
(gdb) b ExecResult

再開啟一個psql會話並執行select 1

select 1;

返回GDB視窗,輸入 c 命令,停留在斷點上

(gdb) c

在gdb裡執行"c"命令。執行了以後,就會在ExecResult 處停止

gdb檢視堆疊,輸入bt

(gdb) bt

退出gdb的話可以用quit

(gdb) quit

附錄

GDB常用命令

命令 簡寫形式 說明
list l 檢視原始碼
backtrace bt, where 列印函式棧資訊
next n 執行下一行
step s 一次執行一行,遇到函式會進入
finish 執行到函式結束
continue c 繼續執行
break b 設定斷點
info breakpoints 顯示斷點資訊
delete d 刪除斷點
print p 打印表達式的值
run r 啟動程式
until u 執行到指定行
info i 顯示資訊
help h 幫助資訊

指令碼

#!/bin/sh

# Usage: gdblive [ arguments to grep output of ps ]

cd $HOME

# tee /dev/tty is for user to see the set of procs considered
if [ $# -eq 0 ]
then
    PROCS=`ps auxww | \
        grep postgres: | \
        grep -v -e 'grep postgres:' -e 'postgres: stats' -e 'postgres: writer' -e 'postgres: wal writer' -e 'postgres: checkpointer' -e 'postgres: archiver' -e 'postgres: logger' -e 'postgres: autovacuum' | \
        tee /dev/tty | \
        awk '{print $2}'`
else
    PROCS=`ps auxww | \
        grep postgres: | \
        grep -v -e 'grep postgres:' -e 'postgres: stats' -e 'postgres: writer' -e 'postgres: wal writer' -e 'postgres: checkpointer' -e 'postgres: archiver' -e 'postgres: logger' -e 'postgres: autovacuum' | \
        grep $@ | \
        tee /dev/tty | \
        awk '{print $2}'`
fi

if [ `echo "$PROCS" | wc -w` -eq 1 ]
then
    exec gdb $PGINSTROOT/bin/postgres -silent "$PROCS"
else
    exec gdb $PGINSTROOT/bin/postgres -silent
fi

參考文件

https://wiki.postgresql.org/wiki/Pgsrcstructure