Skip to main content

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:
The emulator will pause and wait for a debugger to connect before starting execution.

GDB Stub Architecture

Debugging Handler Interface

The GDB stub works through the debugging_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

Output:

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

  1. Start Sogen in debug mode
  2. In IDA: Debugger → Attach → Remote GDB debugger
  3. Set hostname: localhost
  4. Set port: 28960
  5. 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:
IDA will automatically load symbols for recognized DLLs.

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

  1. Start Sogen: analyzer.exe -d target.exe
  2. In VS Code: Press F5 or Run → Start Debugging
  3. 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:

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:
Press 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 -d flag
  • 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 interface
  • src/gdb-stub/gdb_stub.cpp - Protocol implementation
  • src/gdb-stub/connection_handler.hpp - Network handling
  • src/gdb-stub/stream_processor.hpp - Packet parsing
  • src/gdb-stub/async_handler.hpp - Interrupt handling

Next Steps