Debugging with gdb ================== Our Demo Code: #include #include 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 --------- * Vorlesungsfolien Betriebssysteme UE auf der TU Wien: [Debugging](http://ti.tuwien.ac.at/cps/teaching/courses/osue/slides/ss14_entwicklung_in_c.pdf) * Online Tutorial auf [beej.us](http://beej.us/guide/bggdb/) Basics ------ * gcc -g test.c -o test * gdb test * gdb test core * gdb --args Commands in GDB --------------- * break * continue * next * info breakpoints * watch * list * print * 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](http://stackoverflow.com/questions/2934006/how-do-i-print-a-defined-constant-in-gdb) 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