MATLAB Answers

How can I debug a MEX file on Microsoft Windows Platforms with Microsoft Visual Studio 2017?

215 views (last 30 days)
How can I debug a MEX file with Microsoft® Visual Studio® 2017?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Jul 2020
Edited: MathWorks Support Team on 28 Jul 2020
1. Make sure Visual Studio is your selected C compiler:
cc = mex.getCompilerConfigurations('C','Selected');
cc.Name
ans =
'Microsoft Visual C++ 2017 (C)'
2. Compile the source MEX file with the -g option, which builds the file with debugging symbols included. For example:
copyfile(fullfile(matlabroot,'extern','examples','mex','yprime.c'),'.','f')
mex -g yprime.c
3. Start Visual Studio. Do not exit your MATLAB® session.
4. From the Visual Studio Debug menu, select Attach to Process.
5. In the Attach to Process dialog box, select the MATLAB process, and click Attach.
Ensure that the Attach to: parameter is set to Automatic: Native Code
Visual Studio loads data then displays an empty code pane.
6. Open the source file "yprime.c" by selecting File > Open > File. Locate "yprime.c" in the folder, "c:\work"
7. Set a breakpoint by right-clicking the desired line of code and following Breakpoint > Insert Breakpoint* *the context menu. It is often convenient to set a breakpoint at "mexFunction" to stop at the beginning of the gateway routine.
If you have not yet run the executable file, ignore any “!” icon that appears with the breakpoint next to the line of code.
Once you hit one of your breakpoints, you can make full use of any commands the debugger provides to examine variables, display memory, or inspect registers.
8. Open MATLAB and type:
yprime(1,1:4)
9. If you select Debug > Continue, MATLAB displays:
ans =
2.0000 8.9685 4.0000 -1.0947
For more information on how to debug in the Visual Studio environment, see your Microsoft documentation.
Notes on Debugging
1. To avoid stepping into a MathWorks-provided function, use "Step Over" on lines that contain calls to MathWorks APIs.
2. To avoid stepping out into MathWorks code at the end of your custom function, use the "continue" (play button) when at the end of your function.
3. Do not enable "Access Violation" for handling exceptions when debugging MEX files. To avoid breaking at this exception, clear the check box for Win32 Exceptions. For Visual Studio 2015, look for the check box in Debug > Windows > Exception Settings.... If you are using a different version of Visual Studio, refer to the corresponding Microsoft documentation.
Binary MEX files built with the -g option do not execute on other computers because they rely on files that are not distributed with MATLAB. For more information on isolating problems with MEX files, see Troubleshoot MEX Files.

  2 Comments

Christoph Beeler
Christoph Beeler on 30 Dec 2019
Is there a way to recompile changes without having to detach the Visual Studio Debugger? I otherwise get the error:
LINK : fatal error LNK1201: error writing to program database '...phonebook.mexw64.pdb';
check for insufficient disk space, invalid path, or insufficient privilege
Visual Studio seems to hold the pdb file. Reattaching always takes 5-10 seconds. Is there maybe a "Edit and Continue" Option while compiling?
Sam Cook
Sam Cook on 24 Feb 2020
According to Microsoft, this error can be due to a locked or corrupted PDB file, but my guess is this:
The debugger is active on the program you are trying to link.
This makes me think the Edit and Continue functionality does not work with attached processes at all.
Microsoft help resources will be better suited to help troubleshoot these types of issues.

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!