When using custom code in Simulink, how do I look at stack trace information from a custom code crash?
MATLAB: When using custom code in Simulink, how to look at stack trace information from a custom code crash
ccodecrashcustomdebugsimulinkstacksymbolstrace
Related Solutions
For MATLAB R2013b and later:
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.
To create a Visual Studio solution (.sln) file that will build a ".mex" file for use in MATLAB/Simulink based on existing source code, please do the following:
1) Open Visual Studio, and create a new project by selecting "File"->"New"->"Project from existing code"
2) Choose "Visual C++" as the type of project.
3) You may choose any project location that you like, but the project name should match the name of the resulting S-function. That means that the project is intended to produce <project name>.mexw<32|64>, and that the source code includes the preprocessor directive: "#define S_FUNCTION_NAME <project name>"
4) Using the "Add" button, select all of the user-defined source files and add them to the project.
5) In the next pane, select "Use Visual Studio" as the method for building the project, and for the "Project Type" select "Dynamically Linked Library (DLL) Project". Click 'Next'.
6) For the "Preprocessor definitions" field add
MATLAB_MEX_FILE
7) For the "Include Search Paths" field, add
"<MATLABROOT>\simulink\include"
and
"<MATLABROOT>\extern\include"
(including quotes) where <MATLABROOT> is the absolute path to the MATLAB installation.
8) Finish creating the project
9) Right-click on the newly-created project in the Solution Explorer and select "Properties" and make the following changes in the dialog that appears:
10) Click the "Configuration Manager" button, and in the dialog that appears open the "Active solution platform" drop-down menu. If you want to create an S-Function for 64-bit MATLAB, make sure "x64" is selected. If it is available, select it. If not, select "<new...>" and select "x64" from the dialog that appears. If the "Platform" for your project in the subwindow below is "Win32" change it to "x64" as well. If "x64" is not an option, create it by selecting "<new>". When the new window opens, select "x64" under "New platform" and unclick "Create new solution platform". Click "OK" and "Close" to apply these changes to the Property window.
11) In the "Linker" pane, select the "General" section, and edit the file extension in the "Output File" section, changing it from ".dll" to ".mexw32" or ".mexw64". (This is why the project name has to match the S-function name, ensuring that the generated ".mex" file name matches the "S_FUNCTION_NAME" defined in the source code.)
12) In the "Linker" pane, select the "Input" section and add to "Additional dependencies" section, and add the following(including quotes, and expanding <MATLABROOT> to be the MATLAB installation directory):
"<MATLABROOT>\extern\lib\win64\microsoft\libmx.lib" "<MATLABROOT>\extern\lib\win64\microsoft\libmat.lib" "<MATLABROOT>\extern\lib\win64\microsoft\libmex.lib"
(If you are creating an S-Function for 32-bit MATLAB, please replace win64 by win32)
13) In the "General" pane, set "Platform Toolset" to "Windows7.1SDK".
14) Click "Apply" and close the dialog box.
15) Select "Project"->"Add"->"New Item".
16) If you have a "Module-Definition file" option, select "Module-Definition file" from the GUI, give the file a name, and then click "Add".
16a) Edit the newly-created module-definition file to be the following, where <s-function name> is the project/S-function name, and <extension> is the appropriate extension for the target architecture (e.g. "mexw32" or "mexw64"):
LIBRARY"<s-function name>.<mex extension>" EXPORTS mexFunction
17) If you do not have a "Module-Definition file" option, you would need to create this file outside of Visual Studio and import it. Create a text file with the following information inside it:
LIBRARY"<s-function name>.<mex extension>" EXPORTS mexFunction
Save the file with a name to the folder that contains the Visual Studio Solution.
17a) Change the extension from ".txt" to ".def". Inside Visual Studio, select "Project"->"Add"->"Existing Item" and select the .DEF file.
At this point, you should be able to select the "build" option from the menus to create a ".mex" file that will work with Simulink.
Please note: For distributing S-functions created this way, it is necessary to use the "release" output from Visual Studio, not the "debug" output. For each mode selected you would need to make sure the steps starting from step 9 are set in that mode. In some cases, the "debug" output will require debuggable versions of standard DLLs, which could clash with normal versions already loaded by Simulink.
Attached is a ZIP file that contains the an example Visual Studio Solution and files configured for building S-function MEX according to the instructions above. The ZIP file contains the following:
1) "subsystem_sfcn_rtw" folder containing the C and H files for the S-function
2) "Project" folder containing the Visual Studio files.
Related Question
- Sl_customization
- How to programmatically replace a referenced model with a Subsystem in Simulink
- Do I receive a link error when I try to use the lcc compiler to build the C MEX-file or MATLAB Compiler-generated shared library that includes a DLL compiled with MSVC
- Customize “implay”
- Refresh function and sytem path cache
- How to debug MATLAB Support Package for Arduino with custom libraries
- How to call Cygwin command pipeline with the MATLAB ‘system’ function
Best Answer