Note: This was written for DVF6.0-CVF6.6. I do not own IVF, so I have no idea if this is the same or not. If you have IVF and can comment on debugging MEX files, pleaseĀ drop me a line.

Here is how to use the CVF debugger, it is a bit weird, but once you get used to it, its really nice.

(1) create a new directory using the name of your project. (we’ll say foo)
(1a) if a FORTRAN file already exists, copy it into the directory
(2) open DVF. On the toolbar use File>New, choose the ‘Project’ tab. Choose a Fortran Dynamic Link Library type. On the right, enter the name of your project (foo) and below it, choose the directory ABOVE the one you created (i.e. if foo is actually c:\mexcode\foo, then in the browse box, choose c:\mexcode) Hit ok
(3) Add your source file to the project.
(4) Start MATLAB. cd to creavect. Use the command mex -g creavect.f90
(5) In DVF, go to Project>Settings, use the ‘Debug’ tab. Where it says Executable, enter the path to Matlab
(6) Now, you are ready to debug. go to Build>Start Debug>Go. You will get a dialog box that says such and such hasn’t been built do you want to build them, choose no. You will get a dialog box that says that the application Matlab doesn’t have debugging information, press OK to continue.
(7) DVF will start a new instance of Matlab. Go back to DVF & set a break point. Go into the new instance of ML and execute your code, it will pop into ML when it hits your breakpoint.

Instead of giving me a new instance, all I see is disassembler code. What’s up?

A common problem that occurs when debugging is that instead of launching ML, you go into a window with the disassembly code. Just close the window (Ctrl+F4) and hit debug again (F5), and you will be off & running.

When I start debugging, I get the error “Some Breakpoints are invalid and have been disabled” and then MATLAB launches, but I can’t set any breakpoints to use the debugger. What is wrong?

I don’t know why it does this, but I can tell you how to work around it. For some reason, if you can call the function and have it return normally (i.e. no segmentation fault) then you will be able to set breakpoints. To do this, I just make it easy to get the function to return an error, as follows:
(1) Always put in error checking code in the gateway function. This is important of its own right, and is essential for dealing with this problem. I usually have a statement that checks to make sure that the number of input arguments is correct. An example is:
if (nrhs .ne. 2) then
call mexErrMsgTxt(‘boo’)
end if

(2) Now, when you launch the debugger and get the error message above, go to MATLAB and call the function using the incorrect number of arguments. Your call will break with an error (not a segmentation fault), and you can now go to DVF and set breakpoints & debug normally.