# gdb
## Running
### Compile for debugging
>g++ -Wall -g <source.cpp>
### start GDB
>gdb <program>
>gdb --args <program> <arguments>
### set arguments
``` gdb
set args <arguments>
```
### Run and Kill
``` gdb
run
kill
```
## Breakpoint / Watchpoint
### set
``` gdb
break <where>
watch <lvalue>
```
### show
``` gdb
info breakpoints
info watchpoints
```
### delete
``` gdb
delete <breakpoint id>
delete <watch id>
```
### clear all breakpoints
``` gdb
clear
```
### enable
``` gdb
enable <breakpoint id>
enable <watchpoint id>
```
### disable
``` gdb
disable <breakpoint id>
disable <watchpoint id>
```
### condition
``` gdb
condition <breakpoint id> [condition]
```
## Where
``` gdb
function_name
line_number
file: line_number
```
## Examining the stack
### Show call stack
``` gdb
backtrace
backtrace full
```
### Select stack
``` gdb
frame <frame id>
```
## Stepping
### Go to next instruction, diving into function
``` gdb
step
```
### Go to next instruction, not diving into function
``` gdb
next
```
### Continue until the current function returns
``` gdb
finish
```
### Continue to the next breakpoint
``` gdb
continue
```
## Variables
### Print value of variable
``` gdb
print/format <what>
```
### Print value of variable after each stepping instruction
``` gdb
display/format <what>
```
### What
``` gdb
expression
file_name::variable_name
function::variable_name
```
### Format
- a - Pointer
- c - Character
- d - Integer (signed decimal)
- f - Floating point
- o - Integer (octal)
- s - C string
- t - binary
- u - Undigned integer (decimal)
- x - Integer (hexadecimal)
### Remove display
``` gdb
undisplay <display id>
```
### Enable and disable display
```
enable display <display id>
disable display <display id>
```
### Print local variables
``` gdb
info locals
```
## Manipulate the program
### Change the value of a variable
``` gdb
set var <variable_name>=<value>
```
``` gdb
return <expression>
```
## Show the source code
``` gdb
refresh
list
list <filename>:<function>
list <filename>:<line_number>
list <begin>:<end>
set listsize <size>
```
## Shortuct
- b - breakpoint
- w - watchpoint
- d - delete
- r - run
- c - continue
- n - next
- s - step
- p - print
- bt - backtrack
- f - frame
- i - info
- k - kill
- q - quit