Main Content

Debug Fortran MEX Files

Notes on Debugging

The examples show how to debug timestwo.F, found in your matlabroot/extern/examples/refbook folder.

Binary MEX files built with the -g option do not execute on other computers because they rely on files that are not distributed with MATLAB® software. For more information on isolating problems with MEX files, see Troubleshooting on Write C Functions Callable from MATLAB (MEX Files).

Debugging on Microsoft Windows Platforms

For MEX files compiled with any version of the Intel® Visual Fortran compiler, you can use the debugging tools found in your version of Microsoft® Visual Studio®.

Debugging on Linux Platforms

The MATLAB supported Fortran compiler g95 has a -g option for building binary MEX files with debug information. Such files can be used with gdb, the GNU® Debugger. This section describes using gdb.

GNU Debugger gdb

In this example, the MATLAB command prompt >> is shown in front of MATLAB commands, and linux> represents a Linux® prompt; your system might show a different prompt. The debugger prompt is <gdb>.

  1. To compile the source MEX file, type:

    linux> mex -g timestwo.F
  2. At the Linux prompt, start the gdb debugger using the matlab -D option:

    linux> matlab -Dgdb
    
  3. Start MATLAB without the Java® Virtual Machine (JVM®) by using the -nojvm startup flag:

    <gdb> run -nojvm
  4. In MATLAB, enable debugging with the dbmex function and run your binary MEX file:

    >> dbmex on
    >> y = timestwo(4)
  5. You are ready to start debugging.

    It is often convenient to set a breakpoint at mexFunction so you stop at the beginning of the gateway routine.

    Note

    The compiler might alter the function name. For example, it might append an underscore. To determine how this symbol appears in a given MEX file, use the Linux command nm. For example:

    linux> nm timestwo.mexa64 | grep -i mexfunction

    The operating system responds with something like:

    0000091c T mexfunction_

    Use mexFunction in the breakpoint statement. Be sure to use the correct case.

    <gdb> break mexfunction_
    <gdb> continue
  6. Once you hit one of your breakpoints, you can make full use of any commands the debugger provides to examine variables, display memory, or inspect registers.

    To proceed from a breakpoint, type continue:

    <gdb> continue
  7. After stopping at the last breakpoint, type:

    <gdb> continue

    timestwo finishes and MATLAB displays:

    y =
    
         8
  8. From the MATLAB prompt you can return control to the debugger by typing:

    >> dbmex stop

    Or, if you are finished running MATLAB, type:

    >> quit
  9. When you are finished with the debugger, type:

    <gdb> quit

    You return to the Linux prompt.

Refer to the documentation provided with your debugger for more information on its use.

Related Topics