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

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.

Related

Program does not proceed to console window

I'm using visual studio 2013 express. I have a program that uses recursion, after multiple edits and compiles. The compiler now compiles the program without error but the program does not run in foreground, instead 3 instances of same program run in the background. Task manager cannot stop the programs and i can no longer click and edit on visual studio(but is still responsive according to task manager). This code works on other systems.I have tried repairing visual studio, deleting debug folder, restarting my PC.
image showing build succeeded and 3 instances of program:
You might have missed to use the linker flag SUBSYSTEM:CONSOLE. MSDN link

Debug a mex function in Visual Studio

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.

debugger already attached - cscript

I have a short JScript which creates an active X object and calls a function. That active X object is written in C++. When I run the command cscript scriptName.js //X I start VS2012 in debug mode. Than I try to attach a debugger but as you know one is already attached.
Is there a way to reattach the debugger or connect to it some how?
My current solution is not to use JScript and call the code from C++.
Which debugger do you want to use?Visual Studio or WinDBG?
Do you really need to debug both the JavaScript code AND the C++ code simultanously?
If the latter isn't an issue for you, and you want to focus on the C++ code, in Visual Studio (or WinDBG) just debug cscript.exe, without the /x flag. No need even to attach, you can start debugging with F5 from Visual Studio.
In Visual Studio (2008, 2010, or 2012 - they all work), right click on the ActiveX project (That's the C++ project).
Go to: Configuration Properties -> Debugging
In the Command put the cscript full path: C:\Windows\System32\cscript.exe
In the Command Arguments put the full path of you JS file
Put a break point on your ActiveX code (on dllmain, or the constructor of your COM object)
Hit F5
Visual Studio will complain about lack of symbols of cscript. That's OK. keep going.
You'll hit your breakpoint
Some point to consider:
Setup the symbol path to include the Microsoft Symbols. This way, you'll see the names of the functions that calls your code (oleaut32.dll and friends).
Also, this is the default, but make sure that:
The debugger type in the same property box would be either Native or Auto.

Visual Studio debug ghost cursor - program enters debug but cannot see current line nor variables

Strange situation when I am debuging a C++ application in Visual Studio.
I have set a breakpoint in my file and started debug, and run the scenario.
The application enters debug mode in visual studio at the set breakpoint, but:
I do not see the current line/cursor
there is no call stack displayed
there are no variables in Autos
in my Breakpoints list I see my breakpoint highlighted/bold (which means the debug is on the correct line)
Anyone ?
hope I have been somewhat clear...
Try the Visual Studio command "Debug.ShowNextStatement" (default Alt+Num*)
This can happen if the file you've loaded into the debugger isn't exactly the same version that was compiled.

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.