E D R , A S I H C RSS

GDB Usage

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.0224 sec