签到成功

知道了

CNDBA社区CNDBA社区

在PG中使用 GDB 工具调试

2021-10-09 22:17 1884 0 原创 PostgreSQL
作者: Kubee

GDB 调试

先要在编译PostgreSQL时打开enable-debughttps://www.cndba.cn/biubiu/article/4911https://www.cndba.cn/biubiu/article/4911https://www.cndba.cn/biubiu/article/4911

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 处停止https://www.cndba.cn/biubiu/article/4911https://www.cndba.cn/biubiu/article/4911https://www.cndba.cn/biubiu/article/4911

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

https://www.cndba.cn/biubiu/article/4911
https://www.cndba.cn/biubiu/article/4911
https://www.cndba.cn/biubiu/article/4911
https://www.cndba.cn/biubiu/article/4911

版权声明:本文为博主原创文章,未经博主允许不得转载。

PostgreSQL

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
Kubee

Kubee

关注
  • 4
    原创
  • 0
    翻译
  • 0
    转载
  • 0
    评论
  • 访问:14282次
  • 积分:37
  • 等级:注册会员
  • 排名:第54名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ