Reattaching Visual Studio debugger fails to detect mex file modules
4 views (last 30 days)
Show older comments
I am attempting to debug a Matlab "mex" file using Visual Studio.
I can attach Visual Studio to Matlab and set a breakpoint in my mex file source code just fine in the first go-around. When calling the mex file, visual studio stops at the breakpoint, and I note that the mex file is listed in the loaded modules:
However, if I de-attach visual studio, then re-attach, seems that visual studio no longer detects the loaded modules. See screenshot below, there are far fewer modules listed:
This is quite frustrating, as the only way I seem to be able to work around is to quit Matlab and re-start each time in need to re-attach the debugger (i.e. after tweaking code and rebuilding the mex file).
I used to do this all the time in the past (i.e. with VS 2010-2012 / Matlab R2012B-R2017B), but I'm stumped on this now using VS 2015 w/ Matlab R2019B.
0 Comments
Answers (1)
Sachin Lodhi
on 6 Jun 2024
Hello Jesse,
I understand that you are facing issues after reattaching the Visual Studio Debugger. To address this issue, please follow this workaround :
1. Deattach Visual Studio Debugger from Matlab
2. Clear mex or clear [Sfcn-Name].mexw32
3. Attach Visual Studio Debugger to Matlab
If the issue persists, then it is advisable to check the Visual Studio debugger setup. If the Visual Studio debugger does indeed not stop at breakpoints, then there are some quirks with attaching Visual Studio (2015, 2017, 2019, or 2022) as a debugger for external code in Simulink. Typically things need to be done in a certain sequence for it to work right as listed below.
Kindly, see also the "Debugging C MEX S-Functions Using the Microsoft Visual C++ .NET Environment" example for details:
1. PDB files are required for Visual Studio to debug an S-function since they contain the symbols related to that source file. It should be ensured that the MEX files for the S-function are generated using the right compiler i.e., Visual Studio in this case. The compiler being used can be checked by entering the following command:
>> mex -setup C
2. Next, ensure you have disabled Simulation Target > Enable Custom Code Analysis in the configuration parameters of your Simulink model.3. In MATLAB R2022a and earlier, make sure that the execution of the below command returns 1 (true):
>> Simulink.CustomCode.debugSymbols('on')
In MATLAB R2022b and newer, this option is always turned on.4. Next, issue the following command at the MATLAB Command Window to delete any existing MEX files and cache(s):
>> clear mex
5. Attach the Visual Studio debugger to the MATLAB.exe process
6. Add a breakpoint to the source code in Visual Studio. At this stage, Visual Studio will still say the breakpoint will not be hit. This is expected.
7. Return to Simulink and update the model (Ctrl+D)
8. Now, the breakpoint will light up in Visual Studio and function as normal. Breakpoints you place will be hit as you simulate the model.
0 Comments
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!