gdbserver - Linux
Overview
gdbserver is a command-line tool that allows remote debugging of programs using the GNU Debugger (GDB). It enables developers to connect to a running program and control its execution, inspect memory, and modify variables remotely from a GDB client.
Syntax
gdbserver [options] port [prog [arguments ...]]
Options/Flags
- -a address: Specify the address to listen on. Default: localhost
- -c cmdfile: Specify a file containing GDB commands to execute at connect time.
- -d: Run in debug mode, printing debug messages.
- -F: Run the program in full mode (set a full breakpoint on the entry function).
- -g: Enable core file generation.
- -h: Print usage information.
- -i pid: Attach to a running process with the specified PID.
- -p: Print output in a format compatible with ps.
- -q: Quiet mode, suppress informational messages.
- -u: Allow untrusted clients to connect.
Examples
Simple usage:
gdbserver :12345 program
Remote debugging via SSH:
ssh user@remotehost -L 12345:localhost:12345
gdbserver :12345 program
Attach to a running process:
gdbserver -i 12345
Capture a core file:
gdbserver -g :12345 program
Common Issues
- Ensure that port forwarding is set up correctly if debugging remotely.
- Check that the program is compiled with debugging symbols for remote debugging.
- Address mismatched endianness between the client and server machines.
Integration
- Use gdbserver in conjunction with gdb for remote debugging.
- Combine with tools like valgrind for memory profiling and debugging.
- Integrate into automated testing frameworks for debugging failures.
Related Commands
- gdb: GNU Debugger
- lldb: LLVM Debugger
- dtrace: System tracing and debugging tool
- strace: Trace system calls
- memcheck: Valgrind memory leak detector and profiler