Debugging C Language MEX-Files

Notes on Debugging

The examples show how to debug yprime.c, found in your matlabroot/extern/examples/mex/ directory.

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 and Fortran Programs from MATLAB" topic Troubleshooting for additional information on isolating problems with MEX-files.

Debugging on the Microsoft® Windows® Platforms

The Microsoft® Visual Studio® development environment provides complete source code debugging, including the ability to set breakpoints, examine variables, and step through the source code line-by-line.

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.

Visual Studio® 2005

This section describes how to debug using the default compiler, that is, the compiler used to build MATLAB.

  1. Select the Microsoft® Visual C++® 2005 compiler. At the MATLAB prompt, type:

    mex -setup

    Type y to locate installed compilers, and then type the number corresponding to this compiler.

  2. Next, compile the source MEX-file with the -g option, which builds the file with debugging symbols included. For example:

    mex -g yprime.c

    On a 32–bit platform, this command creates the executable file yprime.mexw32.

  3. Start Visual Studio®. Do not exit your MATLAB session.

  4. From the Visual Studio Tools menu, select Attach to Process...[1]

  5. In the Attach to Process dialog box, select the MATLAB process and click Attach.

    Visual Studio loads data then displays an empty code pane.

  6. Open the source file yprime.c by selecting File > Open > File. yprime.c is found in the matlabroot/extern/examples/mex/ directory.

  7. Set a breakpoint by right-clicking the desired line of code and following Breakpoint > Insert Breakpoint on the context menu. It is often convenient to set a breakpoint at mexFunction to stop at the beginning of the gateway routine.

    If you have not yet run the executable file, ignore any "!" icon that appears with the breakpoint next to the line of code.

    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.

  8. Run the binary MEX-file in MATLAB. After typing:

    yprime(1,1:4)

    yprime.c is opened in the Visual Studio debugger at the first breakpoint.

  9. If you select Debug > Continue, MATLAB displays:

    ans =
    
        2.0000    8.9685    4.0000   -1.0947
    

    For more information on how to debug in the Visual Studio environment, see your Microsoft® documentation.

Debugging on Linux® Platforms

The GNU Debugger gdb, available on Linux®[2] systems, provides complete source code debugging, including the ability to set breakpoints, examine variables, and step through the source code line-by-line.

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 procedure, 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>.

To debug with gdb:

  1. Compile the source MEX-file with the -g option, which builds the file with debugging symbols included. For this example, at the Linux prompt, type:

    linux> mex -g yprime.c

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

  2. At the Linux prompt, start the gdb debugger using the matlab function -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
    >> yprime(1,1: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.

    <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:

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

    <gdb> continue

    yprime finishes and MATLAB displays:

    ans =
    
        2.0000    8.9685    4.0000   -1.0947
    
  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.


[1] used by permission

[2] Linux is a registered trademark of Linus Torvalds.

  


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