2009年6月17日

GNU GDB

GDB: The GNU Project Debugger
GDB QUICK REFERENCE CARD
基本gdb

// -- $ mkdir src; vim src/hello.c --
#include <stdio.h>
#include <stdlib.h>

static void function2(int i) {
printf("%d\n", i);
}

static int function1(int x) {
int i;
for (i = 0; i < x; i++) {
function2(x);
}
return x + 1;
}

int main(int argc, char* argv[]) {
int i, total, count;

total = 0;
if (argc > 1) {
count = atoi(argv[1]);
} else {
count = 5000;
}

for (i = 0; i < count; i++) {
total = function1(i);
function2(total);
}
return 0;
}

// -- $ mkdir script; vim script/hello.gdb.script --
set args 50
show args
# list source line 1-32
list 1, 32
disassemble function1
break 28 if (i == 30)
run
echo -- total value --\n
print total
print/x $1
print &total
delete break 1
info local
info registers
ptype argv
whatis i
next
break function1
break 11
info break
continue
disable 3
continue
enable 3
next 2
step 2
up
down
backtrace
finish
nexti
disassemble $pc ($pc+1)
clear function1
continue
until
disable
interpreter-exec mi "continue"
quit

// -- $ vim makefile --
all: hello.gdb.out
cat hello.gdb.out

hello.gdb.out: hello
gdb -q -batch -x ./script/hello.gdb.script hello > hello.gdb.out

hello: src/hello.c
gcc -g -o hello ./src/hello.c

clean:
rm -f hello*

// -- make --
$ make all
cat hello.gdb.out
Argument list to give program being debugged when it is started is "50".
1 #include
2 #include
3
4 static void function2(int i) {
5 printf("%d\n", i);
...

沒有留言:

網誌存檔