-
Notifications
You must be signed in to change notification settings - Fork 4
/
notes-debugging.page
147 lines (103 loc) · 3.34 KB
/
notes-debugging.page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
title: Introduction to Debugging
format: Markdown
...
# GNU Debugger - gdb
- [Greg Law "Give me 15 minutes & I'll change your view of GDB"](https://www.youtube.com/watch?v=PorfLSr3DDI)
## GDB Graphical Interfaces
- [gdbgui](https://gdbgui.com), starts fabulous GDB interface in local webserver
- [gdb dashboard](https://github.com/cyrus-and/gdb-dashboard), Python-based,
gorgeous replacement of GDB command-line interface
## Installation
Install gdb on the VM:
~~~bash
$ sudo apt-get update && sudo apt-get install gdb
# Confirm with <ENTER>
~~~
## Example
Using test suite as an example for debugging with gdb:
~~~bash
$ gdb ./testsuite.bin
~~~
This starts program `testsuite.bin` and halts execution at its entry
point (usually the `main` function entry point).
First, let's switch to a more useful view. In gdb, enter `<CTRL-X> 2`.
This switches to a TUI (terminal user interface, a GUI in the terminal)
consisting of three view sections: the source code at the top with
assembly below, and the gdb command line at the bottom.
Or, write the content of
[this file](https://raw.githubusercontent.com/gdbinit/Gdbinit/master/gdbinit)
to `~/.gdbinit` for colored output and overall prettier gdb
(incompatible with TUI apparently).
Searching for gdb dotfiles on github gives lots of other coder's gdb
tweaks like [this one](https://github.com/RAttab/dotfiles/blob/master).
Now, enable pretty printing for displaying values:
~~~gdb
(gdb) set print pretty on
~~~
You will find the integrated help system of gdb very useful:
~~~gdb
(gdb) help
# display help for a command category like "running":
(gdb) help running
# display help for a specific command:
(gdb) help next
~~~
To debug a specific function like `vector__push_back`, add a break
point at the function's entry point using the gdb command `break`.
You can use auto-completion to choose from all functions named
'vector__' like:
~~~gdb
(gdb) break vector__<TAB><TAB> (select a function name) <ENTER>
~~~
To run the program, use the gdb command `run`. Execution will halt
at the first break point.
~~~gdb
(gdb) run
~~~
To continue execution in single operations step-by-step, use `next`
or `n`:
~~~gdb
(gdb) n
~~~
To repeat the last command, just hit `<ENTER>`.
Show the value of a variable with `print` (ensure that
pretty-printing is enabled as described above):
~~~gdb
(gdb) print *data
~~~
# Memory Debugging: valgrind
Install valgrind on the VM:
~~~bash
$ sudo apt-get update && sudo apt-get install valgrind
# Confirm with <ENTER>
~~~
From the [valgrind quick start tutorial](http://valgrind.org/docs/manual/quick-start.html):
Example program:
~~~c
#include <stdlib.h>
void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; // problem 1: heap block overrun
} // problem 2: memory leak -- x not freed
int main(void)
{
f();
return 0;
}
~~~
Using valgrind to check for memory leaks:
~~~bash
$ valgrind --leak-check=yes myprog [arg1 arg2 ...]
~~~
Output from memory check:
~~~
==19182== Invalid write of size 4
==19182== at 0x804838F: f (example.c:6)
==19182== by 0x80483AB: main (example.c:11)
==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd
==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
==19182== by 0x8048385: f (example.c:5)
==19182== by 0x80483AB: main (example.c:11)
~~~