From: Sebastian Bachmann Date: Thu, 7 Aug 2014 19:59:05 +0000 (+0200) Subject: adding notes for gdb X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=6cbaeed351109f8e63b548579e93842def71f7f9;hp=4dd84bcbf540253e6a98ffb777183b8741b3cff0;p=debienna.git adding notes for gdb --- diff --git a/gdb/index.mdwn b/gdb/index.mdwn new file mode 100644 index 0000000..741668c --- /dev/null +++ b/gdb/index.mdwn @@ -0,0 +1,110 @@ +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 +