GDB

GDB Debugger

1. Introduction

1
2
3
4
5
6
7

- Allows programmers to see inside and interact/modify with all components of a programs,
including information inside the registers.
- Allows programmers to walk through the program step by step, including down to 
instruction level, to debug the program.

1
2
3
4
5
6
7
8
9
10
11
## 2. tmux


- Our workspace is limited within the scope of a single terminal (a single shell) 
to interact with the operating system. 
- `tmux`: *terminal multiplexer*. 
- `tmux` allows user to open multiple terminals and organize split-views (panes) 
within these terminals within a single original terminal. 
- We can run/keep track off multiple programs within a single terminal. 

1
tmux new -s csc231

:::{image} fig/gdb/01.png :alt: A new tmux windows :class: bg-primary mb-1 :height: 600px :align: center :::

1
tmux ls

:::{image} fig/gdb/02.png :alt: List active tmux sessions :class: bg-primary mb-1 :height: 600px :align: center :::

:::{image} fig/gdb/03.png :alt: Go back to main terminal :class: bg-primary mb-1 :height: 50px :align: center :::

:::{image} fig/gdb/04.png :alt: Go back to the previous tmux session :class: bg-primary mb-1 :height: 600px :align: center :::

:::{image} fig/gdb/05.png :alt: Listing multiple tmux sessions :class: bg-primary mb-1 :height: 600px :align: center :::

1
2
3
4
5
6
7


- Create a new session called `p1`.

```bash
tmux new -s p1

:::{image} fig/gdb/06.png :alt: Tmux session with two vertical panels :class: bg-primary mb-1 :height: 600px :align: center :::

:::{image} fig/gdb/01.png :alt: Tmux sessions with one vertical panel and the other vertical panel splitted into two horizontal panels :class: bg-primary mb-1 :height: 600px :align: center :::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

- Run `tmux ls` to check and `tmux kill-session` to clean up all existing 
tmux sessions. 
- Create a new session called `p1`. 
- Organize `p1` such that: 
  - `p1` has four vertical panes. 
  - The last vertical pane of `p1` has three internal horizontal panes. 
- Kill all panes using `exit`!

:::{image} fig/gdb/08.png
:alt: Tmux with multiple panels
:class: bg-primary mb-1
:height: 600px
:align: center
:::

1
2
3
4
5
6
7
8



- Redo the hands-on activity of slide 8 so that all the panes are aesthetically 
proportional. 
- After complete, finish and exit out of all tmux sessions

3. Running and exiting gdb

1
2
3
4
cd 
git clone https://github.com/longld/peda.git
echo "source $HOME/peda/peda.py" > $HOME/.gdbinit
gdb

:::{image} fig/gdb/09.png :alt: Color-coded gdb :class: bg-primary mb-1 :height: 600px :align: center :::

1
2
3
4
5
6
7
8
9

- To use `gdb` to debug, we need to compile the program with a `-g` flag.
- Split the `gdb` session into two **horizontal** panes.
- In the top pane, run the followings command:

```bash
cd ~/csc231/intro-c
gcc -g -o hello hello.c
1
2
3
cd ~/intro-c
gdb hello
gdb-peda$ run

:::{image} fig/gdb/10.png :alt: Running gdb on hello.c :class: bg-primary mb-1 :height: 800px :align: center :::

1
2
3
4
5
6
7
8
9
10
11

- This is a continue from the previous slide's tmux windows

- We need to set a `breakpoint`:
  - Could be a line number or
  - Could be a function name

```bash
gdb-peda$ b main
gdb-peda$ run

:::{image} fig/gdb/11.png :alt: Set break points and run :class: bg-primary mb-1 :height: 800px :align: center :::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


- Mouse scrolling does not work with tmux. 
- To enable scrolling mode in tmux, type `Ctr-b` then `[`. 
- You can use the `Up`/`Down`/`PgUp`/`PgDn` keys to navigate. 
- To quit scrolling mode, type `q` or `Esc`. 

:::{image} fig/gdb/12.png
:alt: Scrolling in tmux
:class: bg-primary mb-1
:height: 800px
:align: center
:::

- At a glance
  - Registers' contents
  - Code
  - Stack contents
  - Assembly codes
- `gdb` stops at our breakpoint, just before function `main`. 
- The last line (before the `gdb-peda$` prompt) indicates the next line of 
C code to be executed. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14

- This slide should start in a two-horizontal-panel tmux session
- Change to the directory containing your `malloc-1.c` that was 
created from the previous lectures. If you don't have it, create a new `malloc-1.c` 
with the code below

<script src="https://gist.github.com/linhbngo/d1e9336a82632c528ea797210ed0f553.js?file=malloc-1.c"></script>

- In the top pane, compile `malloc-1.c` in debugging mode.

```bash
gcc -g -o malloc-1 malloc-1.c
cat -n malloc-1.c
1
2
3
gdb malloc-1
gdb-peda$ b main
gdb-peda$ run

::::{dropdown} After initial run command :::{image} fig/gdb/malloc-01.png :alt: After initial run command :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running int main(int argc, char *argv[]) { :::{image} fig/gdb/malloc-02.png :alt: After running int main(int argc, char *argv[]) { :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running void *p = malloc(4); :::{image} fig/gdb/malloc-02.png :alt: After running void *p = malloc(4); :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running int *ip = (int *)p; :::{image} fig/gdb/malloc-03.png :alt: After running int *ip = (int *)p; :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running *ip = 98765; :::{image} fig/gdb/malloc-04.png :alt: After running *ip = 98765; :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After finish :::{image} fig/gdb/malloc-05.png :alt: After finish :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

1
2
3
4
5
6
7
8


- In the top pane, compile `array-4.c` in debugging mode. 

```bash
gcc -g -o array-4 array-4.c
cat -n array-4.c
1
2
3
gdb array-4
gdb-peda$ b main
gdb-peda$ run

::::{dropdown} After initial run command :::{image} fig/gdb/array-01.png :alt: After initial run command :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running int main(int argc, char *argv[]) { :::{image} fig/gdb/array-02.png :alt: After running int main(int argc, char *argv[]) { :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running size = atoi(argv[1]); :::{image} fig/gdb/array-03.png :alt: After running size = atoi(argv[1]); :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

1
2
3
gdb array-4
gdb-peda$ b main
gdb-peda$ run 3

::::{dropdown} After initial run command :::{image} fig/gdb/array-04.png :alt: After initial run command :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running int main(int argc, char *argv[]) { :::{image} fig/gdb/array-05.png :alt: After running int main(int argc, char *argv[]) { :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

::::{dropdown} After running size = atoi(argv[1]); :::{image} fig/gdb/array-06.png :alt: After running size = atoi(argv[1]); :class: bg-primary mb-1 :height: 800px :align: center ::: ::::

1
2
3
4
5
6
7
8
9

- Use `n` to run the next two lines (`printf...` and `malloc..`).
- Step through the `for` loop and printing out values of `i`, `p[i]`, `&p[i]`, 
and `p + i` at every iteration. 
- Make sure that you understand the lines of code that cause these variables to 
change value.
- Utilize scrolling as needed.