4.4. Running Applications from GDB
Prev | Chapter 4. Using Sourcery G++ from the Command Line | Next

## 4.4. Running Applications from GDB

You can run GDB, the GNU Debugger, on your host system to debug programs running remotely on a target board or system. You can also run and debug programs using the GDB simulator.

While this section explains the alternatives for using GDB to run and debug application programs, explaining the use of the GDB command-line interface is beyond the scope of this document. Please refer to the GDB manual for further instructions.

### 4.4.1. Connecting to the GDB Simulator

GDB includes a simulator that allows you to debug MIPS ELF applications without target hardware. To start and connect to the simulator from within GDB, use this command:

    (gdb) target sim

### 4.4.2. Connecting to the Sourcery G++ Debug Sprite

The Sourcery G++ Debug Sprite is a program that runs on the host system to support hardware debugging devices. You can use the Debug Sprite to run and debug programs on a target board without an operating system, or to debug an operating system kernel. See Chapter 6, “Sourcery G++ Debug Sprite” for detailed information about the supported devices.

You can start the Sprite directly from within GDB:

    (gdb) target remote | mips-sde-elf-sprite arguments

Refer to Section 6.3, “Invoking Sourcery G++ Debug Sprite” for a full description of the Sprite arguments.

### 4.4.3. Connecting to an External GDB Server

From within GDB, you can connect to a running gdbserver or other debugging stub that uses the GDB remote protocol using:

    (gdb) target remote host:port

where `host` is the host name or IP address of the machine the stub is running on, and `port` is the port number it is listening on for TCP connections.

### 4.4.4. Connecting with MDI

Sourcery G++ Lite for MIPS ELF supports debugging with third-party simulators and hardware debug devices that implement the MDI (Microprocessor Debug Interface) API.

### Important

This release of Sourcery G++ Lite for MIPS ELF includes the new Sourcery G++ Debug Sprite for MIPS, which is now the preferred way to connect to an MDI debugging device. MDI support in GDB itself, as documented in this section, will be removed in a future release of Sourcery G++. Refer to Chapter 6, “Sourcery G++ Debug Sprite” for more information.

Before you can connect to a target using the MDI API, you must tell GDB which shared library or DLL to load for your simulator or device, and set up parameters to select your MDI target. This can be done either by means of environment variables or GDB commands. For example, you may want to put the GDB configuration commands in your `.gdbinit` file, which is loaded automatically when you start GDB.

This section describes the basic MDI usage; refer to the documentation for your MDI simulator or debug device for details specific to that target. Note, in particular, that some MDI targets may require you to set up a configuration file and/or license in addition to the steps given here.

In order to tell GDB which MDI library to load, on Linux hosts you should add the directory containing the shared library files to your `LD_LIBRARY_PATH` environment variable. On Windows hosts, add the directory containing the DLLs to your `PATH` environment variable. Then, either set the environment variable `GDBMDILIB` to the base name of the MDI library before starting GDB, or select the library within GDB using the command:

    (gdb) set mdi library name

To verify that your shared library configuration is correct, you can query it from GDB for the devices it supports:

    (gdb) show mdi devices

Each device is identified by a target number and device number. You can select the values you want to use with the following GDB commands:

    (gdb) set mdi target targetnum
    (gdb) set mdi device devicenum

Alternatively, you can set the environment variables `GDBMDITARGET` and `GDBMDIDEVICE`.

At this point, you can establish a connection to the selected MDI device using:

    (gdb) target mdi

Then you can load and run or attach to your program on the target.

### 4.4.5. Loading and Running Applications

Connecting to a bare-metal target or simulator from GDB does not cause your program to be loaded into target memory. You must do this explicitly from GDB after you connect:

    (gdb) load

Alternatively, you can use third-party tools to load your application into flash memory before starting GDB.

To begin execution of your application, you should generally use the continue command:

    (gdb) continue

However, you should use run instead of continue to start your program if you used target mdi or target sim to connect:

    (gdb) run

Prev | Up | Next
4.3. Running Applications in the Simulator | Home | Chapter 5. CS3™: The CodeSourcery Common Startup Code Sequence