《PG源码学习--1.容器中gdb debug PostgreSQL》

容器中gdb debug PostgreSql

一.背景

工作环境为 Mac,编译打包后的PostgreSQL运行起来,通过gdb调试,出各种错。因而通过容器方式来调试,进一步学习源码。本文介绍如何进行容器制作和gdb调试。

二.操作步骤

介绍在 Mac 上 centos基础镜像拉取,软件包安装,源码下载和编译,安装,调试等:

  1. Docker基础镜像
    #docker pull centos
    会拉取最新版本镜像。
  2. 下载postgres
    这里下载的版本为postgres-REL_10_10.zip,
    展开到对应本地目录,如 :/Users/sinwaj/software/gp/postgres
  3. 启动容器
    #docker run --cap-add=SYS_PTRACE -tid --name postgres -p 5432:5432 -v /Users/sinwaj/software/gp:/gp centos /bin/bash
    参数说明:
    –cap-add=SYS_PTRACE ,如果不加gdb attach process报ptrace: Operation not permitted
    -tid ,后台启动
    -v /Users/sinwaj/software/gp:/gp,把容器内目录/gp外挂
    -p 5432:5432,端口暴露到host机器
  4. 容器中安装依赖包
    1)查找容器id
    #docker ps
    2)进入容器
    #docker exec -it xxxxx /bin/bash
    3)安装依赖
#yum install flex
#yum install bison
#yum install zlib zlib-devel
#yum install readline readline-devel
#yum install perl-ExtUtils-Embed
#yum install zlib zlib-devel
#yum install openssl openssl-devel
#yum install pam pam-devel
#yum install libxml2 libxml2-devel
#yum install libxslt libxslt-devel
#yum install tcl tcl-devel
#yum install openldap openldap-devel
#yum install python python-devel
#yum install make
#yum install net-tools

后续过程中如有缺失可以继续安装

  1. 编译postgres
    1)设置编译参数
    #export CFLAGS="-g -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wformat-security -fno-strict-aliasing -fwrapv"
    2)配置configure
    #cd /gp/postgres
    #./configure --prefix=/gp/postgres --enable-depend --enable-cassert --enable-debug
    3)编译
    #make world -j 2
    2表示可以2核并行编译,加快速度。
    4)安装
    #make install
    6.启动postgres
    1)设置环境变量
export PGPORT=5432 
export PGDATA=/gp/postgres/data   
export LANG=en_US.utf8  
export PGHOME=/gp/postgres 
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH  export PATH=$PGHOME/bin:$PATH:.  
export MANPATH=$PGHOME/share/man:$MANPATH  
export PGUSER=postgres  
export PGDATABASE=postgres

2)用户和权限
数据库不能在root账号下启动,因而创建数据库用户postgres
#useradd postgres
#chown postgres /gp/postgres
#chgrp postgres /gp/postgres
#sudo postgres
3)初始化数据库
#initdb -D /gp/postgres/data -E UTF8 --locale=C -U postgres
4)启动数据库
#pg_ctl -D /gp/postgres/data -l logfile start

三.gdb debug

  1. 启动连接进程
    #psql
    postgres=# create table user(id int);
    postgres=#select * from user;
  2. 调试 ,root用户下,12046为数据库连接进程
[root@ab50dcf74584 /]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-6.el8
Copyright (C) 2018 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) attach 12046
Attaching to process 12046
Reading symbols from /gp/postgres/bin/postgres...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Reading symbols from /lib64/librt.so.1...(no debugging symbols found)...done.
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Reading symbols from /lib64/libnss_files.so.2...(no debugging symbols found)...done.
0x00007fc8f4f837ab in epoll_wait () from /lib64/libc.so.6
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-72.el8.x86_64
(gdb) 

  1. 断点
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-72.el8.x86_64
(gdb) break analyze.c:104
Breakpoint 1 at 0x554a1c: file analyze.c, line 105.
(gdb) print
The history is empty.
(gdb) c
Continuing.

Breakpoint 1, parse_analyze (parseTree=0x255b600, sourceText=0x255a740 "select * from user;", paramTypes=0x0, numParams=0, 
    queryEnv=0x0) at analyze.c:105
105		pstate->p_sourcetext = sourceText;
(gdb) print sourceText
$1 = 0x255a740 "select * from user;"
(gdb) 

四.后续

环境准备好了,后面可以边看代码边调试,加深对系统的理解。

发布了6 篇原创文章 · 获赞 1 · 访问量 223

猜你喜欢

转载自blog.csdn.net/weixin_39939108/article/details/104106342