gdbserver Command in Linux



gdbserver is a command-line tool that is used in Linux to debug programs running on a different machine (the target) from where you are running the debugger (the host). This tool works by connecting the GNU debugger on your host machine to the program on the target machine over a network or a serial connection. Once connected, you then can debug embedded systems or remote servers with it.

To use this command, you start it on the target machine with the program you want to debug. Additionally, you should also specify how the program will communicate with GDB either through network or serial communication. This setup allows GDB on your host machine to control and debug the program running on the target machine remotely.

Table of Contents

Here is a comprehensive guide to the options available with the gdbserver command in linux −

How to Install gdbserver in Linux?

The gdbserver is a third-party command line utility that doesnt come preinstalled on your Linux system. However, manual installation is possible by using the systems default package manager.

For Debian-based distributions like Ubuntu and Kali Linux, you can install it with the following command −

sudo apt install gdbserver
How to Install gdbserver Command

For Red-Hat based systems like Fedora and CentOS, the tool can be installed via the below-given command −

sudo yum install gdbserver

On Arch Linux system, you can install it using −

sudo pacman -S gdb

The OpenSUSE users can install gdbserver on their systems from the following command −

sudo zypper install gdbserver

Syntax of gdbserver Command

The basic syntax to use the gdbserver in Linux is provided below −

gdbserver [options] comm program [args ...]

Where,

  • [options] are optional flags or parameters that modify the behavior of gdbserver.
  • comm specifies the communication method between gdbserver and GDB.
  • program is the executable program you want to debug. This is the path to the binary file that gdbserver will run and debug.
  • args are optional arguments that you want to pass to the program being debugged.

gdbserver Command Options

With gdbserver, you can use multiple options, these are discussed in the table provided below −

Option Description
--attach Attach to a running process.
--multi Enable multi-process mode.
--debug Enable debugging output for gdbserver itself.
--once Exit after the first connection is closed.
--wrapper WRAPPER Specify a wrapper program to run the debugged program.
disabled-randomization Disable address space layout randomization (ASLR) for the debugged program.
no-disable-randomization Do not disable address space layout randomization (ASLR).
--startup-with-shell Start the debugged program with a shell.
--no-startup-with-shell Do not start the debugged program with a shell.
--help Display version information and exit.
--version Display help information and exit.

For more details about options, you can open the manual on terminal using −

man gdbserver

Examples of gdbserver in Linux

Lets go through a few examples of gdbserver in Linux system −

  • Basic Usage
  • Attaching to a Running Process
  • Using a Serial Port
  • Running in Multi-Process Mode
  • Remote Debugging via SSH

Basic Usage

The basic use of gdbserver is to start it with the specified program and optional arguments. This is possible using the below-given example −

gdbserver localhost:2345 /path/to/my_program arg1 arg2

The above command will start the gdbserver to debug my_program with arguments arg1 and arg2, and communicate over TCP/IP on port 2345.

Attaching to a Running Process

If you want to attach gdbserver to an already running process identified by its PID, you can use the following syntax −

gdbserver --attach localhost:2345 1234

This command will attach gdbserver to the running process with PID 1234 and allow GDB to connect via TCP/IP on port 2345.

Using a Serial Port

You can also use gdbserver with a serial port by using the following syntax −

gdbserver /dev/ttyS0 /path/to/my_program arg1 arg2

The above command will start gdbserver to debug my_program with arguments arg1 and arg2 and communicate via the serial port /dev/ttyS0.

Running in Multi-Process Mode

If you want to start gdbserver in multi-process mode and handle multiple debugging sessions, you can consider the following syntax −

gdbserver --multi localhost:2345

This command will start gdbserver in multi-process mode and allow it to handle multiple debugging sessions over TCP/IP on port 2345.

Remote Debugging via SSH

To set up remote debugging via SSH, you can use the following syntax −

ssh user@remotehost -L 12345:localhost:2345 gdbserver localhost:2345 /path/to/my_program

This command sets up an SSH tunnel to the remote host and starts gdbserver to debug my_program and communicate over TCP/IP on port 2345.

Conclusion

The gdbserver command is a powerful utility used for debugging programs running on a different machine from where you are running GDB. You can use various options and communication methods with it and adjust the process according to your needs.

In this tutorial, we have provided the way of installing gdbserver on different Linux systems. Apart from that, we have provided the syntax, options and a few examples of the gdbserver on the Linux system to ensure you will get a better understanding.

After getting adequate information from these methods, you will be able to perform effective remote debugging on your system.

Advertisements