GDB Integration
Sogen implements the GDB Remote Serial Protocol, enabling you to debug emulated Windows applications using industry-standard debugging tools including GDB, LLDB, IDA Pro, and Visual Studio Code.Overview
The GDB stub allows debuggers to:- Set breakpoints (software and hardware)
- Single-step through code
- Read and write memory
- Inspect and modify registers
- View loaded libraries
- Debug multi-threaded applications
Starting the GDB Server
When running Sogen with the-d flag, it starts a GDB server on port 28960:
GDB Stub Architecture
Debugging Handler Interface
The GDB stub works through thedebugging_handler interface:
Breakpoint Types
Sogen supports multiple breakpoint types:Connection Handling
The stub accepts connections on a TCP socket:Connecting with GDB
1. Start Sogen in Debug Mode
2. Connect GDB
3. Set Breakpoints
4. Inspect State
Connecting with LLDB
LLDB uses the same GDB protocol:LLDB Commands
Connecting with IDA Pro
IDA Pro has excellent GDB stub support:1. Start Remote Debugging
- Start Sogen in debug mode
- In IDA: Debugger → Attach → Remote GDB debugger
- Set hostname:
localhost - Set port:
28960 - Click OK
2. Configure Architecture
IDA should auto-detect x86-64, but verify:- Debugger → Debugger options
- Ensure “x86-64” is selected
3. Debug Session
Once connected:- Set breakpoints by pressing
F2 - Step over with
F8 - Step into with
F7 - Run with
F9 - View registers in Debugger → Registers
- View memory in Debugger → Memory map
Loading Symbols in IDA
The GDB stub reports loaded libraries:Connecting with VS Code
Visual Studio Code can debug through the GDB protocol using the C/C++ extension.1. Install Extension
Install the C/C++ extension by Microsoft.2. Create Launch Configuration
Create.vscode/launch.json:
3. Start Debugging
- Start Sogen:
analyzer.exe -d target.exe - In VS Code: Press
F5or Run → Start Debugging - Use the Debug toolbar to step through code
4. Set Breakpoints
Click in the gutter next to line numbers to set breakpoints. VS Code will translate these to memory addresses.Protocol Details
Supported Commands
The GDB stub implements these GDB Remote Serial Protocol commands:| Command | Description | Source Reference |
|---|---|---|
? | Stop reason | gdb_stub.cpp:634-636 |
c | Continue | gdb_stub.cpp:612-615 |
s | Single step | gdb_stub.cpp:617-619 |
g | Read registers | gdb_stub.cpp:642-644 |
G | Write registers | gdb_stub.cpp:646-648 |
p | Read single register | gdb_stub.cpp:650-652 |
P | Write single register | gdb_stub.cpp:654-656 |
m | Read memory | gdb_stub.cpp:658-660 |
M | Write memory | gdb_stub.cpp:662-664 |
X | Write memory (binary) | gdb_stub.cpp:666-668 |
Z | Set breakpoint | gdb_stub.cpp:629-632 |
z | Remove breakpoint | gdb_stub.cpp:629-632 |
H | Set thread | gdb_stub.cpp:670-672 |
qSupported | Feature negotiation | gdb_stub.cpp:218-220 |
qXfer:features | Target description | gdb_stub.cpp:196-198 |
qXfer:libraries | Library list | gdb_stub.cpp:200-202 |
qC | Current thread | gdb_stub.cpp:234-237 |
qfThreadInfo | Thread list | gdb_stub.cpp:243-254 |
vCont | Extended continue | gdb_stub.cpp:372-387 |
Register Access
Registers are accessed by index:Memory Operations
Memory reads are limited to 4KB per request:Thread Support
Multiple threads are supported:Advanced Features
Asynchronous Interrupts
The debugger can interrupt execution at any time:Ctrl+C in GDB to trigger an interrupt.
Target Description
The stub provides x86-64 register descriptions:Library Notification
Debuggers are notified when libraries are loaded:Debugging Tips
Finding Entry Points
Debugging Syscalls
Set breakpoints on syscall instructions:Watching Memory
Hardware watchpoints track memory changes:Examining Strings
Windows uses UTF-16 strings:Troubleshooting
Connection Refused
Ensure:- Sogen is running with
-dflag - Firewall allows connections to port 28960
- You’re connecting to
localhost:28960
Breakpoints Not Working
Check:- Address is valid and executable
- Code hasn’t been relocated
- Using correct breakpoint type for the operation
Register Values Incorrect
The GDB stub reports registers in little-endian byte order. Most debuggers handle this automatically.Cannot Step Over Instructions
Some complex instructions may require single-stepping (stepi in GDB) instead of stepping over.
Source Code Reference
Key files:src/gdb-stub/gdb_stub.hpp- Main interfacesrc/gdb-stub/gdb_stub.cpp- Protocol implementationsrc/gdb-stub/connection_handler.hpp- Network handlingsrc/gdb-stub/stream_processor.hpp- Packet parsingsrc/gdb-stub/async_handler.hpp- Interrupt handling
Next Steps
- Explore State Management for debugging with snapshots
- Learn about Fuzzing to find bugs to debug
- See Custom Backends for backend-specific debugging considerations