E D R , A S I H C RSS

GDB Usage

Difference between r1.1 and the current

@@ -25,18 +25,18 @@

state = pipe(fd);
if (state == -1) {
puts(\"pipe() error\");
puts("pipe() error");
exit(1);
}

pid = fork();
if(pid == -1) {
puts(\"fork() error\");
puts("fork() error");
exit(1);
}

else if (pid == 0) {
write(fd[1], \"Good\\n\", 6);
write(fd[1], "Good\n", 6);
}

else {
@@ -47,20 +47,20 @@
return 0;
}
}}}
gdb 를 이용하기 위해서는 gcc 옵션에 \'\'\'-g\'\'\'를 주고 컴파일 해야함.
gdb 를 이용하기 위해서는 gcc 옵션에 '''-g'''를 주고 컴파일 해야함.
{{{
gcc pipe1.c -o pipe1 -g
}}}
== gdb 실행 ==
\'\'\'gdb pipe1\'\'\'
'''gdb pipe1'''
{{{
GNU gdb 6.4-debian
Copyright 2005 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type \"show copying\" to see the conditions.
There is absolutely no warranty for GDB. Type \"show warranty\" for details.
This GDB was configured as \"i486-linux-gnu\"...Using host libthread_db library \"/lib/tls/libthread_db.so.1\".
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb)
}}}
@@ -94,13 +94,13 @@
Program exited normally.
(gdb) list
16 if (state == -1) {
17 puts(\"pipe() error\");
17 puts("pipe() error");
18 exit(1);
19 }
20
21 pid = fork();
22 if(pid == -1) {
23 puts(\"fork() error\");
23 puts("fork() error");
24 exit(1);
25 }
(gdb)
@@ -128,7 +128,7 @@
(gdb) print pid
$2 = 0
(gdb) print buffer
$3 = \"涌E\\203\\004\\b\\001\\000\\000\\000\\224\\203涌\\030\\203涌o\\205\\004\\b?217湯?205\\004\\b\"
$3 = "涌E\203\004\b\001\000\000\000\224\203涌\030\203涌o\205\004\b?217湯?205\004\b"
(gdb)
}}}



1. GDB

GNU Debugger.
The GNU Debugger, usually called just GDB, is the standard debugger for the GNU software system. It is a portable debugger that runs on many Unix-like systems and works for many programming languages, including C, C++, and FORTRAN.

2. Usage

2.1. 디버그 모드 컴파일

example source.
~cpp
/**
pipe를 통한 간단한 프로세스 통신의 일예
**/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define BUFSIZE 30

int main(int argc, char** argv)
{
        int fd[2];
        char buffer[BUFSIZE];
        pid_t pid;
        int state;

        state = pipe(fd);
        if (state == -1) {
                puts("pipe() error");
                exit(1);
        }

        pid = fork();
        if(pid == -1) {
                puts("fork() error");
                exit(1);
        }

        else if (pid == 0) {
                write(fd[1], "Good\n", 6);
        }

        else {
                read(fd[0], buffer, BUFSIZE);
                puts(buffer);
        }

        return 0;
}
gdb 를 이용하기 위해서는 gcc 옵션에 -g를 주고 컴파일 해야함.
gcc pipe1.c -o pipe1 -g

2.2. gdb 실행

gdb pipe1
GNU gdb 6.4-debian
Copyright 2005 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) 

2.3. list #

해당 라인을 전후로 하여 소스 코드를 보여준다.
(gdb) list
6       #define BUFSIZE 30
7
8       int main(int argc, char** argv)
9       {
10              int fd[2];
11              char buffer[BUFSIZE];
12              pid_t pid;
13              int state;
14
15              state = pipe(fd);
(gdb) 

2.4. run #

해당 인자값을 구조 프로그램이 실행된다. 브레이크 포인터 설정을 통해서 진행을 중지하는 것이 가능하다.
(VS에서는 프로젝트 설정으로 조정가능)
(gdb) run 15
Starting program: /home/staff/sapius/source/ipc_test/pipe1 15
Good


Program exited normally.
(gdb) list
16              if (state == -1) {
17                      puts("pipe() error");
18                      exit(1);
19              }
20
21              pid = fork();
22              if(pid == -1) {
23                      puts("fork() error");
24                      exit(1);
25              }
(gdb) 

2.5. break #

해당 라인에 브레이크 포인트 설정
(gdb) break 21
Breakpoint 1 at 0x80484cc: file pipe1.c, line 21.

2.6. print #

해당 변수의 내용을 출력해준다.
(gdb) break 21
Breakpoint 1 at 0x80484cc: file pipe1.c, line 21.
(gdb) run
Starting program: /home/staff/sapius/source/ipc_test/pipe1 

Breakpoint 1, main (argc=1, argv=0xbfe98394) at pipe1.c:21
21              pid = fork();
(gdb) print state
$1 = 0
(gdb) print pid
$2 = 0
(gdb) print buffer
$3 = "涌E\203\004\b\001\000\000\000\224\203涌\030\203涌o\205\004\b?217湯?205\004\b"
(gdb) 

2.7. step

VS step in 동일
(gdb) break 21
Breakpoint 1 at 0x80484cc: file pipe1.c, line 21.
(gdb) run
Starting program: /home/staff/sapius/source/ipc_test/pipe1 

Breakpoint 1, main (argc=1, argv=0xbfda24b4) at pipe1.c:21
21              pid = fork();
(gdb) step 
22              if(pid == -1) {

2.8. next

VS step over 동일

2.9. continue

VS go 동일

2.10. shell #

쉘로 탈출해 해당 명령어를 수행후 복귀한다.
(gdb) shell gcc pipe3.c -o pipe3 -g
(gdb) exec-file pipe3
(gdb) list
1       #include <stdio.h>
2       #include <unistd.h>
3       //#include <sys/types.h>
4       #include <stdlib.h>
5
6       #define BUFSIZE 30
7
8       int main(int argc, char** argv)
9       {
10              int fd[2];

2.11. help #

해당 명령어에 대한 설명을 출력한다.
(gdb) help edit
Edit specified file or function.
With no argument, edits file containing most recent line listed.
Editing targets can be specified in these ways:
  FILE:LINENUM, to edit at that line in that file,
  FUNCTION, to edit at the beginning of that function,
  FILE:FUNCTION, to distinguish among like-named static functions.
  *ADDRESS, to edit at the line containing that address.
Uses EDITOR environment variable contents as editor (or ex as default).
(gdb) help load
Dynamically load FILE into the running program, and record its symbols
for access from GDB.
(gdb) help shell
Execute the rest of the line as a shell command.
With no arguments, run an inferior shell.


3. front-end

ddd : 일반적인 gdb 윈도우 프론트엔드
kdbg : kdevelop에서 제공하는 기본 프론트엔드

4. documentation

gdb documentation
gdb internals
gdb tutorial
KLDP:GDB 잘 쓰기라는 글 에도 사용법이 쉽게 정리 되어 있습니다.

다른 유용한 기능들도 적어주333 - eternalbleu
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:17
Processing time 0.0644 sec