Debug Custom C/C++ Code
When you integrate custom C/C++ code into your model, you can debug your code from within Simulink® by launching an external debugger and setting breakpoints in your custom code. This option applies to custom code specified in the Simulation Target pane of the Model Configuration Parameters dialog box. You can integrate this custom code into your model using any of these blocks:
To debug custom code, the Import custom code check box in the Simulation Target pane of the Model Configuration Parameters dialog must be selected. This parameter is selected by default.
This table gives information about debugger support.
|Operating System||Supported Debuggers|
Microsoft® Visual Studio® IDE.
Visual Studio Code with MinGW® GDB. Requires installation of MATLAB® Coder™ Interface for Visual Studio Code Debugging support package, which you can download from MATLAB Central™ File Exchange.
Visual Studio Code with LLDB. Requires installation of MATLAB Coder Interface for Visual Studio Code Debugging support package, which you can download from MATLAB Central File Exchange.
GNU® Data Display Debugger (DDD).
Visual Studio Code with GDB. Requires installation of MATLAB Coder Interface for Visual Studio Code Debugging support package, which you can download from MATLAB Central File Exchange.
To select a compiler to use with C or C++ code, execute the command
mex -setup C++.
To launch the external debugger, on the Debug tab of the Simulink toolstrip, in the Breakpoints section, click the arrow to
the right of the Add Breakpoint button. From the menu, select
Set Breakpoints in Custom Code.
Debugging is supported only when simulating in normal or accelerated mode and only in normal mode for referenced models. If debugging is not supported, the button to launch the debugger is disabled.
Once the debugger is launched, you can set breakpoints in your custom code.
When you simulate your model, the simulation pauses when it reaches a breakpoint in the custom code. Note that when the simulation is paused during debugging, the simulation time step display at the bottom of the Simulink window may not be up-to-date.
If you use the Microsoft
Visual Studio debugger, you should set the value of the Windows registry key
DebuggerMaxModuleMsgs to 4096 or greater. To
obtain the current value of
DebuggerMaxModuleMsgs, execute this command in
To end a debugging session, first detach the active process from the debugger, then close the debugger window.
Do not kill the process attached to the debugger. Issuing a
kill command from the process in which the debugger is running
can cause MATLAB to stop running, and you can lose work.
On Linux or Mac OS platforms, do not close the debugger before issuing a
detach command from the process in which the debugger is
In-Process and Out-of-Process Simulation
When simulating a model containing custom C or C++ code, you have the option to run the custom code in a separate process outside of MATLAB. This process is called out-of-process simulation. This option may be useful when debugging your custom code. By running in a separate process, problems with the custom code do not cause MATLAB to crash, and you can more easily debug and resolve such problems. Problems can arise due to unexpected exceptions in the custom code or errors in the interface between Simulink and the custom code.
To enable out-of-process simulation, in the Model Configuration Parameters dialog box, in the Simulation Target pane, on the Import settings tab, select Simulate custom code in a separate process.
If Simulate custom code in a separate process is not selected, that is, if you are using in-process simulation, then when you launch an external debugger, the entire MATLAB process attaches to the debugger. This action could cause the debugger to take more time to launch. Debugging is generally faster with out-of-process simulation.
Debugging with out-of-process simulation is not supported on Mac OS, and not all custom code functions are supported in out-of-process simulation. Note that simulation time is generally faster with in-process simulation.
When you simulate a model out-of-process, if you encounter a run-time exception, Simulink gives you the option to launch the external debugger. For example, this model contains a C Caller block that calls the function
adder(), which accesses an object called
adderObj. Before calling the function, the object must be created, which you can do by calling
initAdder() from the Initialize code specified on the Additional source code tab of the Simulation Target pane of the Model Configuration Parameters dialog box.
initAdder() is not called before
adder() attempts to access an uninitialized pointer, which causes a run-time exception. During in-process simulation, this exception can cause MATLAB to crash when you simulate the model. However, with out-of-process simulation, simulating the model produces an error message within Simulink.
You can then click Open to launch your external debugger and resolve the issue that caused the error.
After the debugger launches, it restarts the simulation and stops at the breakpoints of custom function entries automatically.