Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

Technical Solutions

How do I debug Fortran MEX-files under UNIX with MATLAB 7.3 (R2006b)?


Date Last Modified: Friday, June 26, 2009
Solution ID:   1-35LZ0B
Product:   MATLAB
Reported in Release:   R2006b
Platform:   UNIX
Operating System:   UNIX Any
 

Subject:

How do I debug Fortran MEX-files under UNIX with MATLAB 7.3 (R2006b)?

Problem Description:

I would like an example of how to debug a Fortran MEX-file under UNIX.

Solution:

This Solution is specific to the GDB debugger on Linux/UNIX/Mac. For other configurations, see the Debugging section of Tech Note 1605, MEX-files Guide:
http://www.mathworks.com/support/tech-notes/1600/1605.html

These instructions assume familiarity with the GDB debugger. For instructions on how to use your debugger once the MEX-file is loaded, refer to your debugger documentation.

This is a step-by-step version of the debugging overview presented here:
Debugging Fortran Language MEX-Files :: Creating Fortran MEX-Files (External Interfaces)
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f28378.html

These instructions start at the UNIX prompt, before launching MATLAB. This process involves several environments, and lists the prompts provided by each: ">>" for MATLAB, "(gdb)" for GDB, and "unix>" for the UNIX command prompt (which may be different on your machine). Ensure that you do not type these prompts; they are included to demonstrate which program receives each command.

1. First, select your compiler:
unix> mex -setup
2. Copy the YPRIMEF.F and YPRIMEFG.F files to a working directory. These files can be found in $MATLABROOT/extern/examples/mex (where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.)

Alternatively, use your own MEX-function.

3. Compile the MEX-file using -g to include debugging symbols:
unix> mex -g yprimef.F yprimefg.F
4. Load MATLAB into the debugger:
unix> matlab -Dgdb
5. Use the debugger to start the MATLAB process:
(gdb) run -nojvm
6. Enable MEX-file debugging:
>> dbmex on
7. Execute your MEX-file:
>> yprimef(1,1:4)
8. Set a breakpoint at the start of the gateway function:
(gdb) break mexfunction_
Many Fortran compilers change the name of the MEXFUNCTION subroutine; g77 changes it to "mexfunction_". For more information on how to find the equivalent name for other Fortran compilers, see here:
Debugging on UNIX :: Creating Fortran MEX-Files (External Interfaces)
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f21321.html

9. Execute until you reach the breakpoint:
(gdb) continue

This will show the next line to be executed:


Continuing.

Breakpoint 1, mexfunction_ (nlhs=0x7fffffffa628, plhs=0x7fffffffafe0,
nrhs=0x7fffffffa62c, prhs=0x7fffffffb0a0) at yprimefg.F:65
65 IF (NRHS .NE. 2) THEN
Current language: auto; currently fortran

10. Step 1 statement:
(gdb) step

11. Set another breakpoint at line 82 in YPRIMEFG.F, and continue to that point:
(gdb) break 82
(gdb) continue

12. Print the value of some variables:
(gdb) print m
(gdb) print n

These are displayed by GDB in order, but without their names:

$1 = 1
$2 = 4

13. Finish executing the MEX-file and return to MATLAB:
(gdb) continue

14. Use DBMEX to change to the debugger:
>> dbmex stop

15. End the MATLAB and GDB session:
(gdb) quit

GDB will display the following:

The program is running. Quit anyway (and kill it)? (y or n)
Answer "y". You are now back at the UNIX prompt.

Please provide feedback to help us improve this Solution
Contact support
E-mail this page
Print this page