Ecco un tutorial di avvio rapido per gdb:
/* test.c */
/* Sample program to debug. */
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char **argv)
{
if (argc != 3)
return 1;
int a = atoi (argv[1]);
int b = atoi (argv[2]);
int c = a + b;
printf ("%d\n", c);
return 0;
}
Co mpile con l'opzione -g:
gcc -g -o test test.c
Caricare il file eseguibile, che ora contiene i simboli di debug, in gdb:
gdb --annotate=3 test.exe
Ora si dovrebbe trovare te stesso al prompt di gdb. Qui puoi impartire comandi a gdb. Dire che ti piace di mettere un punto di interruzione alla linea 11 e passo attraverso l'esecuzione, la stampa i valori delle variabili locali - le sequenze di comandi seguenti vi aiuteranno a fare questo:
(gdb) break test.c:11
Breakpoint 1 at 0x401329: file test.c, line 11.
(gdb) set args 10 20
(gdb) run
Starting program: c:\Documents and Settings\VMathew\Desktop/test.exe 10 20
[New thread 3824.0x8e8]
Breakpoint 1, main (argc=3, argv=0x3d5a90) at test.c:11
(gdb) n
(gdb) print a
$1 = 10
(gdb) n
(gdb) print b
$2 = 20
(gdb) n
(gdb) print c
$3 = 30
(gdb) c
Continuing.
30
Program exited normally.
(gdb)
In breve, i seguenti comandi sono tutti è necessario iniziare a utilizzare gdb:
break file:lineno - sets a breakpoint in the file at lineno.
set args - sets the command line arguments.
run - executes the debugged program with the given command line arguments.
next (n) and step (s) - step program and step program until it
reaches a different source line, respectively.
print - prints a local variable
bt - print backtrace of all stack frames
c - continue execution.
tipo di aiuto alla (gdb) per ottenere un elenco e la descrizione di tutti i comandi validi.
fonte
2010-01-15 04:05:14
http://www.yolinux.com/TUTORIALS/GDB-Commands.html ecco un buon foglio di comando di gdb. Troverai tutto ciò che devi sapere su gdb. – Phong