# GDB

## 1. Bắt đầu GDB

`gdb [program]`

`gdb [program] [argments]`

## 2. Disassembly (hiện thị function/frame disassembly

`(gdb) disassemble [address/symbol]`

`(gdb) disass main`

## 3. View Disassembly During Exection

`(gdb) display/[# of instructions]i $pc [± offset]`

* `display` hiển thị data with each step
* `/[#]i` - hiện thị bao nhiêu dòng instruction
* `$pc` - program counter - bộ đếm chương trình (đại ý là chỉ địa chỉ hiện tại chương trình)
* `[± offset]` - cho bạn muốn xem các câu lệnh ở vị bù từ địa chỉ hiện tại

#### Xóa chế xem:

* `(gdb) info display` - để hiện thị xem có bao nhiêu chế độ được set
* `(gdb) delete display 1` - để xóa chế độ 1

## Registers

* `info registers` - display the state of all the registers (`info r`)
* x/x $\[register] - xem nội dung của thanh ghi duy nhất (x/x hiển thị dưới dạng hex)

## Breakpoints

`(gdb) b*[Address/Symbol]` - Setting Breakpoints

`(gdb) info breakpoints` - hiện thị các breakpoints hiện có

`(gdb) delete 1` - xóa breakpoints Num = 1

`(gdb) continue [# of breakpoints]` - tiếp tục chương trình cho đến khi kết thúc hoặc gặp 1 breakpoint tiếp theo.

`(gdb) step[# of instructions]` - step into default =1

`(gdb) next instruction [# of instructions]` - step over (shorthand `ni`)

`(gdb) finish` - kết thúc 1 hàm hoặc dừng lại sau khi gặp return (shorthand `fin`)

## Kiểm tra giá trị, data

`(gdb) x/[#][size][format] [Address/Symbol/Register][± offset]`

* `x/` - câu lệnh xem giá trị, kiểm tra
* `[#]` bao nhiêu
* `[size]` means what size the data should be such as a word *w* (2 bytes), double word *d* (4 bytes), or giant word *g* (8 bytes)
* `[format]` means how the data should be interpreted such as an instruction *i*, a string *s*, hex bytes *x*
* `[Address/Symbol][± offset]` means where to start interpreting the data

## Setting Data

`set [Address/Register]=[Hex Data]`

## Process Mapping

`info proc map` - dùng để hiện thị không gian địa chỉ được ánh xạ của quy trình

## Attaching Processes <a href="#attaching-processes" id="attaching-processes"></a>

`attch [pid]`

## Bảng câu lệnh

<table><thead><tr><th width="144">Câu lệnh</th><th>Cú pháp</th></tr></thead><tbody><tr><td>gcc</td><td><strong>gcc &#x3C;file.c or file.cpp> -o namefile_bin</strong></td></tr><tr><td>gdb</td><td><strong>gdb --quiet &#x3C;name_file_bin></strong> || <strong>gdb -q</strong> and <strong>file &#x3C;name_file_bin></strong></td></tr><tr><td>b</td><td><strong>b main</strong> or <strong>break &#x3C;name_funtion or *address></strong></td></tr><tr><td>disassemble</td><td>Dump of assembler code for function current</td></tr><tr><td>info b</td><td><strong>info b (info breakpoints)</strong> – <em>show information breakpoints</em></td></tr><tr><td>clear</td><td><strong>clear &#x3C;*address></strong> – <em>can be remove a breakpoint at the address</em></td></tr><tr><td>del</td><td><strong>del &#x3C;Num_breackpoint></strong> – <em>delete breakpoint at the Num in list breakpoints</em></td></tr><tr><td>disable</td><td><strong>disable &#x3C;Num_breakpoint></strong> – <em>disable a breakpoint</em></td></tr><tr><td>enable</td><td><strong>enable &#x3C;Num_breakpoint></strong> – <em>enable a breakpoint</em></td></tr><tr><td>p ...</td><td><strong>[print,p]/[x,d,u,o,t,a,c,f,s,z,r] [$register,*address]</strong> – <em>print value</em></td></tr><tr><td>info r</td><td><strong>info [r,registers]</strong> – <em>print information registers</em></td></tr><tr><td>info r ...</td><td><strong>info r [rax,rbx, ...] [] [] ...</strong> – <em>print infor specific register</em></td></tr><tr><td>set</td><td><strong>set $register = value</strong> <em>– modifying registers</em></td></tr><tr><td>x</td><td><strong>x/nfu [$register, *address]</strong> – <em>viewing memory, for examine memory</em><br><em>/> n – the repeat count</em><br><em>/> f – the display format :x,d,u,o,t,a,c,f,s,i,m</em><br><em>/> u – the unit size is any of: b,h,w,g</em></td></tr><tr><td>set</td><td><strong>set {C-style type}$register = value</strong> – <em>modifying memory</em><br><em>> char, short, long long</em></td></tr><tr><td>bt</td><td><strong>bt</strong> – <em>provides a call stack <strong>b</strong>ack<strong>t</strong>race.</em></td></tr><tr><td>n</td><td><strong>[next, n, nexti, ni]</strong> – <em>step over</em></td></tr><tr><td>si</td><td><strong>[step, s, stepi, si]</strong> – <em>step into</em></td></tr><tr><td>fin</td><td><strong>[finish, fin]</strong> – <em>step out</em></td></tr><tr><td>u</td><td><strong>[until, u] *address</strong> – <em>run until address</em></td></tr><tr><td>set intel</td><td><strong>set disassembly-flavor intel</strong> <em>– changing disassembly to Intel</em></td></tr><tr><td>set att</td><td><strong>set disassembly-flavor att</strong> – <em>changing disassembly sytax to ATT&#x26;T</em></td></tr><tr><td>p $flag</td><td><strong>p $eflags –</strong> <em>print flags is open 1</em></td></tr><tr><td>kill</td><td>kill the running program</td></tr></tbody></table>

## Chú thích

* *x – hexadecimal*
* *d – decimal*
* *u – decimal, as if it were unsigned*
* *o – octal*
* *t – binary*
* *a – address*
* *c – for ASCII characters*
* *s – for a full (presumed-null-terminated) ASCII string*
* *…*[*link*](https://sourceware.org/gdb/onlinedocs/gdb/Output-Formats.html)
* *The unit sizes for u are*
  * *b for bytes*
  * *h for half-words (2 bytes)*
  * *w for words (4 bytes)*
  * *g for giant-words (8 bytes)*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/education/tools/gdb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
