How do I view code in Visual Studio in Release Mode? - c++

When I change from debug to release mode in Visual Studio and the code is being optimized after debugging in Release Mode, can I see what the optimized code looks like? Or it is already a binary code that is optimized so Visual Studio itself doesn't optimize the code by rewriting it in C++, so there is no any C++ code that is my code but already rewritten for optimalization.

The following works for both Debug and Release builds:
Set a breakpoint on the line(s) of interest.
Launch your program under the debugger (F5).
When your program breaks, right-click and select 'Go To Disassembly'.
But just to say, the code generated by the compiler in Release mode is not always easy to follow so be prepared for some surprises.
[Edit] As per #paddy's comment above, make sure that you compile your project with debugging information included, otherwise none of this will work properly:
Project properties -> C/C++ -> General -> Debug Information Format = Program Database (/Zi)

Related

Code working only when debugging

I'm using Visual Studio 2012 Express for Desktop and I have a code based on Winsock client-server. The problem comes when running the program. One functionality of the program works ONLY when debugging. Why could it happen? Any help is appreciated.
For Visual Studio, if you compiled using the usual debugging defaults, there are things that are done at runtime that are not done in release mode.
One is that variables are initialized to 0 (or their default), while release mode they are left uninitialized. So it could be that uninitialized variable(s) are being used, and you don't see the issue when running the debug version.
Your best bet is to debug the release version. Then you can use the integrated debugger in the release build of your application.

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.

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.

WDK C++ Project Needs to change debug compiler strictness

I'm relatively new at drivers with WDK and Visual Studio.
When I compile project in debug mode I get no warnings or errors and project compiles and runs fine. However, when I compile in release mode, compilation stops and I get an error stating that a warning was found and is being treated as an error. The details of this are dumped into a log file found at the project root dir.
What I would like to do is have the compiler in debug mode be as strict as the release mode compiler. Currently they are both at default. The release mode seems stricter. I am using VS 2010 and WDK. I'm not sure how to do this. It would be ideal if this setting was at the VS level and not at a per project level.
Also, It would be great if the warnings would show up in VS IDE instead of a log file.
Visual Studio has different configuration setting for debug and release mode.
Check whether do you have relevant configuration in project > project properties -> linker etc......
If there is mismatch between debug and release mode configuration then change it. This should work
--Ali Chachar
--Pakistan
There are code differences in the debug and release compilation that may lead to warnings unrelated to strictness of the compiler. Most noticeable is in the logs; in the release version KdPrint/KdPrintEx calls will be discarded. There is a good chance that some of your function input parameters are used for printing only and in case it's being omitted you'll end up with unused parameter warnings - this is the most frequent difference in debug vs. release compilations.
Even if you have a VS2010 solution, your driver isn't being compiled with the VS compiler but rather with the WDK compiler so VS settings are irrelevant here. You can migrate your solution to VS2012 which have driver support integrated.

Can Visual Studio show me which assertion failed?

I normally work with Qt Creator for my C++ needs. When an assertion in my program fails, it can show me which assertion failed. In Visual Studio, I only see this:
I can click Retry to jump into the application, but it doesn't tell me which assertion failed. Even for a simple assert(false) Visual Studio tries to show me the source code for msvcr100d.dll, which isn't available.
How can I find out which assertion in my program failed? I really don't want to do a cumbersome manual search using a combination of breakpoints and std::couts for something that can be tracked down automatically.
Edit: Visual Studio did in fact generate a .PDB file for me, but it still isn't working. Although the debugger won't highlight the line with the failed assertion, I do see Assertion failed: false, file main.cpp, line 8 on the command line. Why can't it just show me the line and let me inspect the variables? I mean, all the information appears to be available...
All you should have to do is click "Retry" to break into the application's source code with the debugger. That will highlight the line containing the assertion that failed, so it's easy to see the culprit unless you like to cram multiple nested assertions onto a single line. And if you do, then you deserve all the pain that this might cause you.
The reason it's not working for you now is more than likely because you don't have the debugging symbols available from your application's current build. When you build an app with the compiler bundled with Visual Studio, depending on your project settings, it will generate a .PDB file, which contains the debug symbols. These are required to get useful information while debugging, such as what line produced the last failure.
Make sure that when you compile your application, you have it set to generate debug symbols. You should see a .PDB file in your /bin directory. As discussed here, the generation of debug symbols is orthogonal to whether optimizations are enabled (the configuration typically known as "Release").
Update: I just realized you're probably compiling/building the app in Qt Creator, and then trying to debug the binaries from Visual Studio. That's not going to work out well—if you don't compile using VS's tools, it's not going to generate debug symbols that the VS debugger can read.
I assume the problem would be the same in reverse: if you built with Visual Studio, then tried to debug with Qt Creator, it probably wouldn't be able to interpret the debugging symbols, either.
Therefore, I'd recommend sticking with a single toolset for building. It doesn't matter which IDE you use, but you'll need to compile/build with the same tools. You can configure either Qt Creator or Visual Studio to use the compiler and linker bundled with the other. Typically Qt Creator comes with a Win32 port of GCC, but it's trivial to get it to build with Microsoft's toolset instead, which would allow you to use VS to debug your code.