Thursday, January 18, 2018

gdb

1. Getting PID / Attach gdb to current process with PID
$ ps l | grep [program]

$ sudo gdb [program]
$ set target-asycn 1
$ attach [pid] &
 ~~~~
$ detach
$ quit

2. Bash command line에서 batch로 gdb  command 입력 

$gdb --ex="set breakpoint pending on" --ex="b some.cpp:13" --ex="r" --ex="plot mat" -args ./bin/ex arg1 arg2

--ex  순서대로 gdb command 자동입력 (배치파일로 입력하는건 다음에 정리)
-args : 프로그래명 arguments 순서대로 입력.

3. Core file 로부터 symbol 불러오기 
Core dump를 bash 에서 명령하는 방법을 주로 이야기하지만, 나는 gdb에서 디버깅하다가 중간에 종료하는 경우가 더 많으므로, gdb 에서 core dump를 하는 방법부터 대신 다룸.

gdb> generate-core-file [core_filename]
$ gdb [program] [core_filename]

Core dump도 debug info 가 필요하므로 bash 의 core dump 명령이든, gdb 의 core dump 명령이든 프로그램은 debug mode나 최소한 RelWithDebInfo 로 빌드되있어야함. 

4. 그외 기본적인 gdb command 
gdb> set breakpoint pending on : 아직 load 되지 않은 shared-lib의 코드에 break point 허용
gdb> b : break point
n : Next
f  [stack number] : 지정된 frame 으로 이동
bt : back trace. stack 의 frame을 나열
info b : break point를 보여줌
generate-core-file : 중단점에 걸린 현재 상태를 core dump. 이후

5. Watching

gdb> rwatch *0xfeedface



References>
https://stackoverflow.com/questions/100444/how-to-set-breakpoints-on-future-shared-libraries-with-a-command-flag