]> git.deb.at Git - debienna.git/blob - gdb/index.mdwn
adding keyboard besprochen
[debienna.git] / gdb / index.mdwn
1 Debugging with gdb
2 ==================
3
4 Our Demo Code:
5
6     #include <stdio.h>
7     #include <stdint.h>
8
9     int g_myvar = 42;
10     #define MYCONST 99
11
12     void sideeffect(void){
13         g_myvar += MYCONST;
14     }
15
16     uint8_t myfunction(uint8_t x){
17         return x+42;
18     }
19
20     int main(void){
21
22         uint8_t foo = 23;
23         uint8_t bla = 23;
24
25         uint8_t x = foo + bla;
26         uint8_t i;
27
28         uint8_t y = myfunction(bla);
29
30         uint8_t * verybad = 0;
31         
32         printf("%d\n", foo);
33         printf("%d\n", y);
34         // use this printf to cause a segfault
35         // printf("%d\n", *verybad);
36
37         for(i = 0; i < 10; ++i){
38             y = myfunction(y);
39             sideeffect();
40         }
41         printf("%d\n", y);
42
43
44         return 0;
45     }
46
47 Resources
48 ---------
49
50 * Vorlesungsfolien Betriebssysteme UE auf der TU Wien:
51 [Debugging](http://ti.tuwien.ac.at/cps/teaching/courses/osue/slides/ss14_entwicklung_in_c.pdf)
52 * Online Tutorial auf [beej.us](http://beej.us/guide/bggdb/)
53
54 Basics
55 ------
56
57 * gcc -g test.c -o test
58 * gdb test
59 * gdb test core
60 * gdb --args
61
62 Commands in GDB
63 ---------------
64
65 * break
66 * continue
67 * next
68 * info breakpoints
69 * watch
70 * list
71 * print
72 * bt full
73 * catch signal
74 * run
75 * break .. if ...
76 * delete
77 * disable
78 * enable
79 * step
80 * stepi
81 * target record-full
82 * reverse-next
83 * set
84 * quit
85
86 Show preprocessor macros
87 ------------------------
88
89 See [Thread on
90 SO](http://stackoverflow.com/questions/2934006/how-do-i-print-a-defined-constant-in-gdb)
91
92     gcc -g3 test.c
93     gdb a.out
94     (gdb) info macro MYCONST
95     (gdb) macro expand MYCONST
96
97 Fun with print
98 --------------
99
100 * print myfunction(y+23/7)/23.0
101 * print printf("%d\n", 23 > 42 ? 111 : 222) 
102
103
104 Enable history
105 --------------
106
107 create a file .gdbinit in ~ and put this line in:
108
109     set history save on
110