How do I debug C MEX-files under UNIX?
9 views (last 30 days)
Show older comments
I need examples of how to debug C MEX-files.
Accepted Answer
MathWorks Support Team
on 31 May 2017
This solution covers several debuggers on various Linux/UNIX/Mac plaforms. For other configurations, see the Debugging section of Tech Note 1605, MEX-files Guide:
<http://www.mathworks.com/support/tech-notes/1600/1605.html>
This solution discusses the general use of a UNIX debugger followed by step-by-step examples on how to use the various UNIX debuggers.
NOTE: These examples are for illustration purposes only. For complete instructions on how to use a specific debugger, refer to the debugger's Reference Guide or help files.
The following section gives a generic description of how to debug C MEX-files. Complete instructions for each debugger follows this discussion.
1.Note: You must build your MEX-File with symbols. In order to do so, issue the following command at the MATLAB Command Prompt:
mex -g filename.c
or at the UNIX prompt:
unix> mex -g filename.c
2.At the UNIX prompt, start up MATLAB with a particular debugger
unix> matlab -D<name_of_debugger>
3.Once the debugger loads MATLAB into memory, continue running with the run command:
<debugger> run -nojvm
Note: you may have to execute the following command to load the symbols for the MEX-file:
add-sym <filename>
If you are using MATLAB older than R12 use the following instead:
<debugger> run
4.MATLAB will now start up. You can enable MEX-File debugging with
dbmex on
% call the MEX-File as you normally would
(i.e. through a MATLAB file or by its name.)
5.MATLAB will now tell you how to load the MEX-File. You can set breakpoints at this point.
<debugger> stop in mexFunction
<debugger> continue
6.Once you hit a breakpoint, you can use the debugger as you would normally.
<debugger> step
<debugger> step
7.If you are at the MATLAB prompt and want to return control to the debugger, issue the command
dbmex stop
As a rule of thumb, if you use the GCC C compiler, use the GCC debugger. If you use the native C compiler, use the native debugger.
The following is a list of available debuggers with the appropriate debugging commands noted. For a complete example on how to use these debuggers, see below:
Alpha
G-gdb run, break, step, continue, quit, print, share, file
N-dbx run, stop, step, cont, quit, print, file
HP 700
G-gdb run, break, step, continue, quit, print, share, file
N-dde DDE does not allow breakpoints to be set.
IBM RS/6000
G-gdb run, break, step, continue, quit, print, share, file
N-dbx run, stop, step, cont, quit, print, file
Linux
G-gdb run, break, step, continue, quit, print, share, file
SGI
G-gdb run, break, step, continue, quit, print, share, file
N-dbx run, stop, step, cont, quit, print, file
N-cvd CVD is not supported on the SGI at this time.
SGI64
G-gdb GCC is not supported on the SGI64 at this time.
N-dbx run, stop, step, cont, quit, print, file
N-cvd CVD is not supported on the SGI64 at this time.
Sol2
G-gdb run, break, step, continue, quit, print, file
N-dbx run, stop, step, cont, quit, print, file
Sun4
G-gdb run, break, step, continue, quit, print, add-sym, file
N-dbx run, stop, step, cont, quit, print, file
Alpha
GDB:
The following are the steps you should take to debug using GDB on the Dec Alpha:
unix> mex -g -f $MATLABROOT/bin/gccopts.sh yprime.c
($MATLABROOT is your MATLAB root directory)
unix> matlab -Dgdb
<gdb> run
dbmex on
yprime(1,1:4)
<gdb> share <full path to yprime.mexap>
i.e. /home/name/mex/yprime.mexap
<gdb>break mexFunction
<gdb> continue
<gdb> step
<gdb> break 54
<gdb> step
<gdb> continue
(This should bring you to line 54 in yprime.c)
<gdb> step
<gdb> print r2
(You should see the value of r2 printed to screen.)
<gdb> continue
(Should finish running yprime and bring you back to MATLAB.)
dbmex stop
<gdb> quit
This will display the following:
The program is running.
Quit anyway (and kill it)? (y or n)
answer y
DBX:
The following are the steps you should take to debug using DBX on the Dec Alpha:
unix> mex -g yprime.c
unix> matlab -Ddbx
<dbx> run
dbmex on
yprime(1,1:4)
<dbx> stop in mexFunction
<dbx> cont
<dbx> step
<dbx> stop at 54
<dbx> step
<dbx> cont
(This should bring you to line 54 in yprime.c)
<dbx> step
<dbx> print r2
(You should see the value of r2 printed to screen.)
<dbx> step
<dbx> cont
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<dbx> quit
HP700
GDB:
The GDB debugger is not supported.
DDE:
It is not possible to set breakpoints when using the DDE debugger.
IBM RS 6000
GDB:
The following are the steps you should take to debug using GDB on the IBM RS 6000:
unix> mex -g -f $MATLABRROT/bin/gccopts.sh yprime.c
($MATLABROOT is your MATLAB root directory)
unix> matlab -Dgdb
<gdb> run
dbmex on
yprime(1,1:4)
<gdb> share <full path to yprime.mexrs6>
i.e. /home/name/mex/yprime.mexrs6
<gdb> break mexFunction
<gdb> continue
<gdb> step
<gdb> break 54
<gdb> step
<gdb> continue
(This should bring you to line 54 in yprime.c)
<gdb> step
<gdb> print r2
(You should see the value of r2 printed to screen.)
<gdb> continue
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<gdb> quit
It will display the following:
The program is running.
Quit anyway (and kill it)? (y or n)
answer y
DBX:
The following are the steps you should take to debug using DBX on the IBM RS 6000:
unix> mex -g yprime.c
unix> matlab -Ddbx
<dbx> run
If you are using MATLAB 6.0 (R12) or R12.1, do the following instead:
<dbx> run -nojvm
dbmex on
yprime(1,1:4)
<dbx> stop in mexFunction
<dbx> cont
<dbx> step
<dbx> stop at 54
<dbx> step
<dbx> cont
(This should bring you to line 54 in yprime.c)
<dbx> step
<dbx> print r2
(You should see the value of r2 printed to screen.)
<dbx> step
<dbx> cont
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<dbx>quit
Linux
GDB:
The following are the steps you should take to debug using GDB on Linux:
unix> mex -g -f $MATLABROOT/bin/gccopts.sh yprime.c
($MATLABROOT is your MATLAB root directory)
unix> matlab -Dgdb
<gdb> run -nojvm
dbmex on
yprime(1,1:4)
<gdb> share <full path to yprime.mexlx>
i.e. /home/name/mex/yprime.mexlx
<gdb> break mexFunction
<gdb> continue
<gdb> step
<gdb> break 54
<gdb> step
<gdb> continue
(This should bring you to line 54 in yprime.c)
<gdb> step
<gdb> print r2
(You should see the value of r2 printed to screen.)
<gdb> continue
(Should finish running yprime and bring you back to MATLAB.)
dbmex stop
<gdb> quit
It will display the following:
The program is running.
Quit anyway (and kill it)? (y or n)
answer y
SGI
GDB:
The following are the steps you should take to debug using GDB on the SGI:
unix> mex -g -f $MATLABROOT/bin/gccopts.sh yprime.c
($MATLABROOT is your MATLAB root directory)
unix> matlab -Dgdb
<gdb> run
dbmex on
yprime(1,1:4)
<gdb> share <full path to yprime.mexsg>
i.e. /home/name/mex/yprime.mexsg
<gdb> break mexFunction
<gdb> continue
<gdb> step
<gdb> break 56
<gdb> step
<gdb> continue
(This should bring you to line 56 in yprime.c)
<gdb> step
<gdb> print r2
(You should see the value of r2 printed to screen.)
<gdb> continue
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<gdb> quit
It will display the following:
The program is running.
Quit anyway (and kill it)? (y or n)
answer y
DBX:
The following are the steps you should take to debug using DBX on the SGI:
unix> mex -g yprime.c
unix> matlab -Ddbx
<dbx> run
dbmex on
yprime(1,1:4)
<dbx> stop in mexFunction
<dbx> cont
<dbx> step
<dbx> stop at 54
<dbx> step
<dbx> cont
(This should bring you to line 54 in yprime.c)
<dbx> step
<dbx> print r2
(You should see the value of r2 printed to screen.)
<dbx> step
<dbx> cont
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<dbx>quit
CVD:
Building of MEX-files with CVD on the SGI is not supported.
SGI64
GDB:
Building of MEX-files with GDB on the SGI64 is not supported.
DBX:
The following are the steps you should take to debug using DBX on the SGI64:
unix> mex -g yprime.c
unix> matlab -Ddbx
<dbx> run
dbmex on
yprime(1,1:4)
<dbx> stop in mexFunction
<dbx> cont
<dbx> step
<dbx> stop at 54
<dbx> step
<dbx> cont
(This should bring you to line 54 in yprime.c)
<dbx> step
<dbx> print r2
(You should see the value of r2 printed to screen.)
<dbx> step
<dbx> cont
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<dbx>quit
CVD:
Building of MEX-files with CVD on the SGI64 is not supported.
Sol2
GDB:
The following are the steps you should take to debug uisng GDB on Solaris2:
unix> mex -g -f $MATLABROOT/bin/gccopts.sh yprime.c
($MATLABROOT is your MATLAB root directory)
unix>matlab -Dgdb
<gdb> run
dbmex on
yprime(1,1:4)
<gdb> break mexFunction
<gdb> continue
<gdb> step
<gdb> break 54
<gdb> step
<gdb> continue
(This should bring you to line 54 in yprime.c)
<gdb> step
<gdb> print r2
(You should see the value of r2 printed to screen.)
<gdb> continue
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<gdb> quit
It will display the following:
The program is running.
Quit anyway (and kill it)? (y or n)
answer y
DBX:
The following are the steps you should take to debug using DBX on Solaris2:
unix> mex -g yprime.c
unix> matlab -Ddbx
<dbx> run
dbmex on
yprime(1,1:4)
<dbx> stop in mexFunction
<dbx> cont
<dbx> step
<dbx> stop at 54
<dbx> step
<dbx> cont
(This should bring you to line 54 in yprime.c)
<dbx> step
<dbx> print r2
(You should see the value of r2 printed to screen.)
<dbx> step
<dbx> cont
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<dbx> quit
Sun4
GDB:
The following are the steps you should take to debug uisng GDB on the Sun4:
unix> mex -g -f $MATLABROOT/bin/gccopts.sh yprime.c
($MATLABROOT is your MATLAB root directory)
unix> matlab -Dgdb
<gdb> run
dbmex on
yprime(1,1:4)
<gdb> add-sym <full path to yprime.mex4> address number
i.e. (/home/name/mex/yprime.mex4)
NOTE: The debugger will tell you what the address number
is and what exactly you should type at this point.
<gdb> break mexFunction
<gdb> continue
<gdb> step
<gdb> break 54
<gdb> step
<gdb> continue
(This should bring you to line 54 in yprime.c)
<gdb> step
<gdb> print r2
(You should see the value of r2 printed to screen.)
<gdb> continue
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<gdb> quit
It will display the following:
The program is running.
Exit anyway? (y or n)
answer y
DBX:
The following are the steps you should take to debug using DBX on Sun4:
unix> mex -g yprime.c
unix> matlab -Ddbx
<dbx> run
dbmex on
yprime(1,1:4)
<dbx> stop in mexFunction
<dbx> cont
<dbx> step
<dbx> stop at 54
<dbx> step
<dbx> cont
(This should bring you to line 54 in yprime.c)
<dbx> step
<dbx> print r2
(You should see the value of r2 printed to screen.)
<dbx> step
<dbx> cont
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<dbx> quit
Mac
GDB:
The following are the steps you should take to debug using GDB on Mac:
unix> mex -g -f $MATLABROOT/bin/gccopts.sh yprime.c
unix> matlab -Dgdb
<gdb> run
dbmex on
yprime(1,1:4)
<gdb> share <full path to yprime.mexmac>
(i.e. /home/name/mex/yprime.mexmac)
<gdb> break yprime.c:mexFunction
<gdb> cont
<gdb> step
<gdb> break 54
<gdb> step
<gdb> cont
(This should bring you to line 54 in yprime.c)
<gdb> print r2
(You should see the value of r2 printed to screen.)
<gdb> cont
(Should finish running yprime and
bring you back to MATLAB.)
dbmex stop
<gdb> quit
It will display the following:
The program is running.
Exit anyway? (y or n)
answer y
0 Comments
More Answers (0)
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!