Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Debugging Fortran Source 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. Refer to the "Calling C/C++ and Fortran Programs from MATLAB" topic Troubleshooting MEX-Files for additional information on isolating problems with 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. Refer to the "Creating C/C++ Language MEX-Files" topic Debugging on the Microsoft Windows Platforms for instructions on using this debugger.

For information on debugging MEX-files compiled with other MATLAB supported compilers, see Technical Note 1605, MEX-files Guide, at http://www.mathworks.com/support/tech-notes/1600/1605.html.

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.

For information on debugging MEX-files compiled with other MATLAB supported compilers, see Technical Note 1605, MEX-files Guide, at http://www.mathworks.com/support/tech-notes/1600/1605.html.

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 may show a different prompt. The debugger prompt is <gdb>.

  1. To compile the source MEX-file, type:

    linux> mex -g timestwo.F

    On a Linux 32–bit platform, this command creates the executable file timestwo.mexglx.

  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. At this point, 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 function name may be slightly altered by the compiler (for example, it may have an underscore appended). To determine how this symbol appears in a given MEX-file, use the Linux command nm. For example:

      linux> nm timestwo.mexglx | 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.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS