How do I debug and set breakpoints in custom C code used in a MATLAB Function block, Stateflow, or a C Caller block?

23 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Jun 2022
Edited: MathWorks Support Team on 8 Sep 2022
For MATLAB R2022a:
You can debug and set breakpoints in customer code from within Simulink. 
1. Follow this documentation to generate debug symbols for custom code
2. Then follow this documentation to launch an external debugger 
For MATLAB R2013b to R2021b:
1. Add the following code to a MATLAB Function block in your model:
coder.updateBuildInfo('addCompileFlags', '/Zi');
coder.updateBuildInfo('addLinkFlags', '/DEBUG');
2. Change the compile and link flags to be the appropriate flags for your compiler and linker to produce debugging symbols. The flags listed in the previous step are for Microsoft Visual Studio.
3. Upon model update, a dynamic library of the custom code will be created in the current directory as a MEX:
On Mac and Linux, the debugging symbols are stored in the dynamic library itself.
On Windows, debugging symbols (a PDB file) will be produced in the following directory (where <model> is the name of the model):
<currentFolder>\slprj\_sfprj\<model>\_self\sfun\src\
4. Use a debugger to attach to the MATLAB process. Enter the file path to the symbols file in the debugger so it can find the symbols file.
Note: If the symbols file is not loaded in the debugger, enter:
>> clear mex
in MATLAB Command Window, then update the model again. This will reload the MEX dynamic linked library.
5. Set breakpoints in the custom code in the debugger and check that the symbols for that file are loaded.
6. Simulate and hit the breakpoints.
For MATLAB R2013a and earlier:
While similar to the instructions above, coder.updateBuildInfo was added in R2013b, so the compile and link flags must be added manually.
1. Update the model.
2. Navigate to:
<currentFolder>\slprj\_sfprj\<model>\_self\sfun\src\
3. Delete all object files in this folder (ending in .o or .obj), which will later force the compiler to recreate these files.
4. Open the makefile (on Windows, <model>_sfun.mak) with a text editor and find the lines defining compiler flags (CFLAGS) and linker flags (LDFLAGS)
5. Add the appropriate compiler flag to produce debugging symbols to the CFLAGS declaration
6. Add the appropriate linker flag to produce debugging symbols to the LDFLAGS declaration
7. In MATLAB, enter
>> clear mex
in order to unload the already-loaded MEX file.
8. Run the build script (on Windows, <model>_sfun.bat) using the ! operator in MATLAB, which will regenerate the object files and MEX file:
>> !<model>_sfun.bat
9. Navigate back to <currentFolder> (outside of slprj). If the MEX file is in the previous inner folder, copy it here.
10. Use a debugger to attach to the MATLAB process. Enter the file path to the symbols file in the debugger so it can find the symbols file.
11. Set breakpoints in the custom code in the debugger and check that the symbols for that file are loaded.
12. Simulate and hit the breakpoints.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!