Debugging with gdb
Our Demo Code:
#include <stdio.h>
#include <stdint.h>
int g_myvar = 42;
#define MYCONST 99
void sideeffect(void){
g_myvar += MYCONST;
}
uint8_t myfunction(uint8_t x){
return x+42;
}
int main(void){
uint8_t foo = 23;
uint8_t bla = 23;
uint8_t x = foo + bla;
uint8_t i;
uint8_t y = myfunction(bla);
uint8_t * verybad = 0;
printf("%d\n", foo);
printf("%d\n", y);
// use this printf to cause a segfault
// printf("%d\n", *verybad);
for(i = 0; i < 10; ++i){
y = myfunction(y);
sideeffect();
}
printf("%d\n", y);
return 0;
}
Resources
Basics
- gcc -g test.c -o test
- gdb test
- gdb test core
- gdb --args
Commands in GDB
- break
- continue
- next
- info breakpoints
- watch
- list
- bt full
- catch signal
- run
- break .. if ...
- delete
- disable
- enable
- step
- stepi
- target record-full
- reverse-next
- set
- quit
Show preprocessor macros
See Thread on SO
gcc -g3 test.c
gdb a.out
(gdb) info macro MYCONST
(gdb) macro expand MYCONST
Fun with print
- print myfunction(y+23/7)/23.0
- print printf("%d\n", 23 > 42 ? 111 : 222)
Enable history
create a file .gdbinit in ~ and put this line in:
set history save on