2017년 9월 21일 목요일

PostgreSQL gdb 디버그

이제 설치도 다 하고, 실행도 다 해봤으니, 휴~ 하고 안심하고 있었다.
그런데 옆에 분이 디버그로 해봤냐고 물어보셨다.
아! 디버그 접속을 안 해봤구나!
나의 임무를 망각하고 있었다.
내 임무는 PostgreSQL 을 디버그 해서 분석하는건데.....
디버그로 PostgreSQL 을 실행해 보도록 한다.

먼저 $PG 디렉토리를 삭제해야 한다.
왜냐하면 그거는 release 로 빌드 되어 있기 때문이다.

혹시라도 release 된 파일로 디버그를 하려고 하면 아래와 같이,
심볼테이블이 없다는 에러가 나올 것이다.
No symbol table is loaded.  Use the "file" command.

암튼 처음부터 다시 진행하자.

기존 파일들을 삭제한다.
% rm -rf $PG

postgresql-9.6.5.tar.gz 압축을 해제한 디렉토리로 이동한다.
특별한 뻘짓을 하지 않았다면,
그 디렉토리에 기존에 빌드되어 있는 파일들이 있다. (오브젝트 파일들)
그러므로 그 파일들을 지워줘야 한다.
% make clean

configure 설정에 추가적인 옵션을 지정한다.
% ./configure --prefix=$PG --enable-debug

그 다음은 동일하다.
% make
% make install

$PG 디렉토리가 새롭게 생긴것을 볼 수 있다.

그리고 DB 도 새롭게 생성해 준다.
% initdb -D $PG/data

자, 이제 디버깅을 해보자.

% gdb postgres

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 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 /home/ck0911.kim/postgresql/bin/postgres...done.

여기까지 나오면 postgres 바이너리를 잘 불러온것이다.
이제 실행을 해보자.

(gdb) run

Starting program: /home/ck0911.kim/postgresql/bin/postgres
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.
[Inferior 1 (process 1233) exited with code 02]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7.x86_64

어....? 아???
아 맞다.
PostgreSQL 은 실행을 할 때, 데이터 위치를 지정해 줘야 한다.
포스팅을 했는데도 불구하고, 금세 또 잊어버렸다.
역시 사람은 치매의 동물이다.

원래는 이런식으로 해줘야 한다.
% postgres -D $PG/data

매번 디버그 할때마다 -D 옵션을 파라미터로 주는건 번거로운 일이다.
위에 에러 메세지를 보면,
환경설정에 추가해주면, 파라미터를 매번 주지 않아도 된다는 말인거 같다.

You must specify the --config-file or -D invocation option or set the PGDATA environment variable.

환경변수를 추가해 준다.
% export PGDATA=$PG/data
※ 이 환경변수도 $HOME/.bashrc 에 추가해 놓으면 편하다.

다시 gdb 를 실행해본다.

% gdb postgres

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 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 /home/ck0911.kim/postgresql/bin/postgres...done.

잘 로딩되었다.
실행해 보자.

(gdb) run

Starting program: /home/ck0911.kim/postgresql/bin/postgres
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Detaching after fork from child process 1504.
LOG:  database system was interrupted; last known up at 2017-09-21 17:46:42 KST
LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  invalid record length at 0/155C140: wanted 24, got 0
LOG:  redo is not required
LOG:  MultiXact member wraparound protections are now enabled
Detaching after fork from child process 1505.
Detaching after fork from child process 1506.
Detaching after fork from child process 1507.
Detaching after fork from child process 1508.
LOG:  autovacuum launcher started
Detaching after fork from child process 1509.
LOG:  database system is ready to accept connections

여기까지 나오면 잘 실행된 것이다.
정말 잘 실행되었는지 psql 으로 접속해 보자.

다른 터미널 세션을 띄운다. (왜냐하면 위 세션은 gdb 가 먹고 있으니 사용할 수 없다.)
% psql postgres

잘 작동하는지 테이블을 만들어 보도록 한다.
postgres=# create table t1 ( i1 char(10) );

만들었으면, 테이블을 조회해 보도록 한다.
\d 명령어를 사용하면 확인해 볼 수 있다.

postgres-# \d

여기까지 잘 된다면, 성공적으로 gdb 에 연결한 것이다.
이제 뭐 알아서 잘 디버깅 하면 된다.
gdb 사용 방법은 구글이 더 잘 알기 때문에 그곳을 참고하면 된다.



출처
나의 삽질

댓글 없음:

댓글 쓰기

화곡 어쩌라고, 굴사냥

석화찜(굴찜)을 먹으러 여의도로 갔다 그런데 재료가 모두 소진되었다고 마감이라고 한다 응? 다들 굴에 미쳤나? 굴을 찾아 헤매다 보니, 화곡까지 가게 되었다. 화곡은 처음 가본다. 첫인상은 "술집 겁네 많네" 피똥쌀때까지 마실 수 있...