MATLAB: How to debug a C MEX S-function on a Linux system

cdebuglinuxmexs-functionsimulink

I would like to debug a C MEX S-function on a Linux system.

Best Answer

The following is an example of how to debug a Simulink C MEX S-function on Debian Linux, kernel 2.2.9. This example uses the example S-function timestwo.c. This file can be found in the $MATLAB/simulink/src directory (where $MATLAB is your root MATLAB directory). Before beginning this example, copy this file into a working directory.
1. To debug a MEX-file from within MATLAB, you must first compile the MEX-file with the -g option to MEX:
mex -g timestwo.c
2. You will need to start MATLAB from within a debugger. To do this, specify the name of the debugger you want to use with the -D option when starting MATLAB. For example, to use dbx, the UNIX debugger, type:
[linux:/home/matlab/simulink/src] % matlab -Dgdb
GNU gdb 4.17.m68k.objc.threads.hwwp.fpu.gnat
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i486-pc-linux-gnu"...
3. Once the debugger loads MATLAB into memory, you can start it by issuing a "run" command:
(gdb) run
Starting program: /home/matlab/bin/glnx86/matlab
In some releases of MATLAB the command above will not run MATLAB, so you would need to use the command:
(gdb) run -nojvm
Starting program: /home/matlab/bin/glnx86/matlab
4. From within MATLAB, enable MEX-file debugging by typing:
dbmex on
5. Load the model that calls your S-function by typing the name of the model at the MATLAB command prompt. For example:
sfcndemo_vdpmex
6. Update the diagram by clicking the Update Diagram option from the Edit menu. This will produce output similar to the following:
MEX FILE: /home/matlab/simulink/src/sfun_bitop.mexglx entry point located at address 0x40dc1a80
Load the MEX-file symbol table by issuing the following command:
share /home/matlab/simulink/src/sfun_bitop.mexglx
Add breakpoints at the debugger prompt and issue a "continue" to resume execution of MATLAB.
7. Type Ctrl-C at the MATLAB prompt. You will see the following:
Program received signal SIGINT, Interrupt.
0x406d2bd1 in HandleCtrlC ()
1046 {
Current language: auto; currently c
8. Set a breakpoint in the S-function.
(gdb) share /home/matlab/simulink/src/sfun_bitop.mexglx
Symbols already loaded for /home/matlab/simulink/src/sfun_bitop.mexglx
(gdb) break sfun_bitop.c:325
Breakpoint 1 at 0x40dbe035: file sfun_bitop.c, line 325.
(gdb) break sfun_bitop.c:mdlCheckParameters
Breakpoint 2 at 0x40dbdae3: file sfun_bitop.c, line 85.
(gdb) c
Continuing.
9. Start the Simulink model by selecting Simulation -> Start to run the model and the S-function. The simulation should stop at the breakpoints you set.
Breakpoint 2, mdlCheckParameters (S=0x81683c8) at sfun_bitop.c:85
85 operator = mxGetScalar( OPERATOR(S) );
(gdb)