GDB
===
###### tags: `Debug` `Linux Tools`
## Basic Commands
### Quick Start
- [Introduction to GDB a Tutorial - Harvard CS50](https://www.youtube.com/watch?v=sCtY--xRUyI)
### Compile & Debug
```
gcc -g -Werror -o hello hello.c
gdb -tui hello
```
:::info
-Werror make all warnings into errors.
:::
### TUI commands
* Display the named window: ```layout src```, ```layout asm```, ```layout regs```
* Set the focus to the named window: ```focus src```, ```focus asm```, ```focus regs```
* Change the height of the window name by count lines: ```winheight name +count```, ```winheight name -count```
### Save command history
```
set history filename ./gdb_cmd_history
set history save on
SOME COMMAND
SOME COMMAND
SOME COMMAND
quit
```
重開GDB
```
source ./gdb_cmd_history
```
## Glossary
* Endianness
位元組順序(Endianness)是指資料在記憶體中的放置順序,不同的 CPU架構可能會採用不同的放置規則,若遇到需要在不同機器或是網路之間交換低階的二進位資料時,就必須注意這個問題。
* Memory Pointer Register
* Program Pointer
A register that points to where the data immediately need to be accessed by processor.
* Frame Pointer(=Local Base Pointer) vs. Stack Pointer
SP指向stack的最頂端(最低位址)。
FP指向stack的固定位址,以FP計算local variable和parameter較方便。

:::info
Stack: reserved RAM memory for temporary data storage.
:::
* Symbol Table
There are two sets of symbols that gdb uses:
* Debugging symbols
The gcc **-g** option set allows to see source code and variables while debugging.
* Linking symbols
This set lives in the ELF (Executable Linkable Format) symbol table that contain addresses of the things in your executable, library, or object file.
## Tutorials
- [RMS's gdb Debugger Tutorial](http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html)
- [基本gdb](http://www.study-area.org/cyril/opentools/opentools/x1253.html)
- [進階gdb](http://www.study-area.org/cyril/opentools/opentools/x1265.html)