Program Built with Foreign Tools and DLL Built with GCC/GNAT (GNAT User’s Guide for Native Platforms)

#### 7.4.8.27 Program Built with Foreign Tools and DLL Built with GCC/GNAT ¶

In this case, things are slightly more complex because you can’t start the main program and then break at the beginning to load the DLL and the associated DLL debugging information. It’s not possible to break at the beginning of the program because there’s no `GDB` debugging information, and therefore there’s no direct way of getting initial control. This section addresses this issue by describing some methods that you can use to break somewhere in the DLL to debug it.

First, suppose that the main procedure is named `main` (this is the case, for example, for some C code built with Microsoft Visual C) and that there’s a DLL named `test.dll` containing an Ada entry point named `ada_dll`.

The DLL (see Introduction to Dynamic Link Libraries (DLLs)) must have been built with debugging information (see the GNAT `-g` switch).

#### Debugging the DLL Directly ¶

- Determine the executable’s starting address

      $ objdump --file-header main.exe

  The starting address is reported on the last line. For example:

      main.exe:     file format pei-i386
      architecture: i386, flags 0x0000010a:
      EXEC_P, HAS_DEBUG, D_PAGED
      start address 0x00401010

- Launch the debugger on the executable.

      $ gdb main.exe

- Set a breakpoint at the starting address and launch the program.

      $ (gdb) break *0x00401010
      $ (gdb) run

  The program will stop at the specified address.

- Set a breakpoint on a DLL subroutine.

      (gdb) break ada_dll.adb:45

  Or if you want to break using a symbol on the DLL, you need first to select the Ada language (language used by the DLL).

      (gdb) set language ada
      (gdb) break ada_dll

- Continue the program.

      (gdb) cont

  This runs the program until it reaches the breakpoint that you’ve set. From that point, you can use standard `GDB` commands to debug a program as described in (Running and Debugging Ada Programs).

You can also debug the DLL by attaching `GDB` to a running process.

#### Attaching to a Running Process ¶

With `GDB`, you can always debug a running process by attaching to it. It’s possible to debug a DLL this way. The limitation of this approach is that the DLL must run long enough to perform the attach operation. To ensure this, you may want, for example, to insert a time-wasting loop in the code of the DLL to allow this to happen.

- Launch the main program `main.exe`.

      $ main

- Use the Windows ‘Task Manager’ to find the process ID. Let’s say that the process PID for `main.exe` is 208.
- Launch gdb.

      $ gdb

- Attach to the running process to be debugged.

      (gdb) attach 208

- Load the process debugging information.

      (gdb) symbol-file main.exe

- Break somewhere in the DLL.

      (gdb) break ada_dll

- Continue process execution.

      (gdb) cont

This last step will resume the process execution and stop at the breakpoint we have set. From there you can use standard `GDB` commands to debug a program, as described in Running and Debugging Ada Programs.