Debug a mex function in Visual Studio - c++

I'm trying to wrap existing C++ code into a MATLAB callable function. I'm using Visual Studio 2013 to generate the MEX file . The MEX file is created properly, I can call it from MATLAB and pass arguments back and forth without any issues.
Now I want to debug my C++ logic, and I can't seem to get it to work. I've created an m script that calls my function, and had Visual Studio run MATLAB when debugging - as explained here .
When I hit F5 to debug my MEX file, Visual Studio runs MATLAB, and then exits debug mode very quickly, as if the MATLAB process terminated. A few seconds after that, MATLAB starts running the code. It is as if the MATLAB instance I'm running starts another instance and terminates, confusing Visual Studio.
How can I debug my MEX function?
UPDATE: Apparantly MATLAB is doing exactly that, as described here. Adding the -wait argument makes Visual Studio wait until the script is done running, but the breakpoints I set don't work - because the process being debugged is not the process loading the DLL.

Turns out <MATLABROOT>\bin\matlab.exe actually runs <MATLABROOT>\bin\w64\matlab.exe. So if I ask Visual Studio to run that, breakpoints are triggered as expected.
Running MATLAB this way under the debugger is a lot slower than any other way, but at least now I can debug my code.

You could also run a MATLAB session as usual, and then attach Visual Studio to the running process. This is explained in more details in the documentation. Here is a quick summary:
compile the source MEX-file with debugging symbols enabled.
open the source C/C++ file in Visual Studio, and place a breakpoint.
start a normal MATLAB session. Then from Visual Studio, attach to the running matlab.exe process.
finally from MATLAB, run the MEX-function. You should hit the breakpoint with execution paused.

Related

Is there a method of having the output of a local windows debugger to be integrated within the interface of visual studio?

I have been coding in visual studio for a long time but I don't know how to integrate the command prompt window for the local windows debugger, with the visual studio interface itself.
I have seen there are other programs where the output is displayed within the interface of the program itself.
Programs such as NetBeans.
Is this a possible thing in c++?, as I am getting tired of going back and forth in the output executioner file for the local windows debugger.

Unable to attach matlab.exe process to visual studio 2013 for debugging mex files?

I am writing some mex files to run in my matlab program using visual studio 2013 compiler.
In order to be able to debug your mex files, you should follow these steps
Everything was right just some minutes ago and I was doing my project without any problem.
Today I have typed the code
mex -g mx_minimum_power.cpp cvm_em64t_debug.lib
on command prompt many times and after getting the success message, I've attached matlab.exe to visual studio and through setting a break point, I've
debugged my code.
But this time I suddenly ran into the following error and I don't know how to solve it.
When I right-clicked on the third option and clicked run as administrator, I encountered the following message:
Then if I choose configure remote debugging, I'll encounter:
Now I have the following processes that are shown to be running.
and again:
When I click on permissions or options for remote debugger:
After running visual studio remote debugging monitor and getting the following message:
One should click the Find button in this window in order to find the msvsmon.exe that is being run on the subnet
and then choose MATLAB.exe, in the Available Processes list.

C++ source code line that stops execution and starts Visual Studio debugger

Is there a line of source code in C++ that will pause execution and start the debugger? Basically, I'm looking for the Matlab keyboard functionality.
I'm using Visual Studio 2010 and compiling in Debug mode.
I know I can set a breakpoint in the source code editor, insert a desired list of command arguments in the project properties, and use the Debug > Start Debugging (F5) option. But I'd like to be able to run the program from the command line and still get to the Visual Studio debugger.
Use __debugbreak(). It sets a breakpoint in your code (something, that was achieved on x86 using __asm int 3; instruction).
When such breakpoint is encountered in application executed without debugger, you will be prompted to run it. You will see window similar to this one:
Then, you can run new instance of Visual Studio or attach already running one.
EDIT
Oh, one more thing: you can also attach debugger to any running process in your system.
In Visual Studio, click: Debug -> Attach to Process and then select desired process.

cannot find .exe file in Visual C++ Express 2010

So, I installed Visual C++ 2010 for the fifth time, I believe, and every time I debug a program, even the simplest c++ program fails to compile, and I get the following error:
Unable to start program 'C:\Users\Ruth\sid\game\Debug\game.exe'.
The system cannot find the file specified.
I don't know why this is happening or if I need to create an exe file. myself. I need help fast. Thanks.
Do the setup program of Visual C++ 2010 includes a repair program? If it does, then run it and try to repair your MSVC by clicking the "Repair" button therein and following the next procedure, but it takes time to complete.

Debug C++ code in visual studio from python code running in eclipse

Does any one know how we can do this?
I have python code in eclipse and whenever it calls c++ functions, i want the break point to go to the visual studio c++ project.
You can use a __debugbreak in visual studio so that every time the code is invoked it triggers the debugger (you may want to search the function in MSDN).
Insert the instruction in the C++ function (or class method) you want to debug, e.g.
void foo()
{
__debugbreak();
[...]
}
at this point compile the library and run the python script, when library loads and the code is executed a messagebox appears telling if you want to attach the visual studio debugger.
It is the replacement of the old __asm { int 3 }.
If the C++ app runs as a separate process then its pretty easy. You can run the process yourself or attach visual studio to existing running process and put break points.
If C++ code is an embedded DLL/LIB then you can use python as debug/launch process. As soon as python will load the DLL/LIB into your python code visual studio will activate your break points.
Alternatively you can also add windows debugger launcher calls to your code. As soon as your code gets executed, you will see a dialog box asking if you want to attach a debugger.