# Debug a Coarray Application

To debug a coarray application, you need an application with shared variables or coarrays whose storage spans all the images in a program.

Linux

Follow these steps to debug your coarray application:

1. Add a stall loop to your application before the area of code you wish to debug:

       LOGICAL VOLATILE :: WAIT_FOR_DEBUGGER
       LOGICAL, VOLATILE :: TICK
       !
       ! Other code may be here
       !
       DO WHILE(WAIT_FOR_DEBUGGER)
       TICK = .NOT. TICK
       END DO
       !
       ! Code you want to debug is here

   Specify VOLATILE to ensure that the loop will not be removed by the compiler.

   If the problem is only found on one image, you can wrap the loop in code such as the following:

       IF (THIS_IMAGE() .EQ. 4) THEN

2. Specify compiler option -g tp compile and link with debug enabled.

3. Create at least N+1 terminal windows on the machine where the application will be running, where N is the number of images your application will have.

4. In a terminal window, start the application:

       linuxprompt> ./my_app

5. In each of the other terminal windows, set your default directory to be the same as the location of the application executable. Use the `ps` command in one of the windows to find out which processes are running your application:

       linuxprompt> ps –ef | grep 'whoami' | grep my_app

   There will be several processes.

   The oldest is the one you started in step 4 – it has run the Message Passing Interface (MPI) launcher and is now waiting for the others to terminate. Do not debug it.

   The others will look like this:

       <your-user-name> 25653 25650 98 15:06 ? 00:00:49 my_app
       <your-user-name> 25654 25651 97 15:06 ? 00:00:48 my_app
       <your-user-name> 25655 25649 98 15:06 ? 00:00:49 my_app

   The first number is the PID of the process (for example, `25653` in the first line). In the steps below, the PIDs of these N processes are referred to as `P1`, `P2`, and so on.

   When you type the commands, replace the text `<P1>` with the actual value of the PID for process 1, and so on.

6. In each window other than the first, start your debugger and set it to stop processes when attached:

       linuxprompt> gdb

7. Attach to one of the processes. For example, attach to `P1` in window 1, attach to `P2` in window 2, and so on:

       (gdb) attach <P1>

8. Get execution out of the stall loop:

       (gdb) set WAIT_FOR_DEBUGGER = .false.

To debug your coarray application, examine the data and code paths in the various images.

Windows

For information about debugging coarray applications started in the Visual Studio* debugger, see article How to Gain Control of Debugging a Fortran Coarray Application.

Parent topic: Coarrays