Visual Studio 2015 Debug: How to stay within my own program only - c++

I'm a beginner at c++, and whenever I write a trivial program in VS2015, and try to debug it, the debugger takes me through every single detail of every standard library function that I used, with 20-30 break points between the statements that I actually wrote.
I can't debug at all this way, and I'd really like to know if there's a way to turn this feature off.
I want to debug MY code, not the library code.

I suggest you to read the manual. Consider options like the breakpoints or the "step over".

One workaround is that you could think about using the "Step Into Specific" or not step into Functions using the registry key like this case:
Is there a way to automatically avoiding stepping into certain functions in Visual Studio?

Related

How to trace a C program by hiding header files in visual C++?

I am using visual studio for last few months. When I trace a C program
it will show line by line execution including the lines in the header files. It will take lot of time to trace it.
In addition to #NathanOliver's comment about breakpoints, make sure you're also using "Step Over" (F10) instead of "Step Into" (F11) (here's more info on that).
If you truly want to change what functions are stepped into, you can do that on a per-function basis by editing default.natstepfilter (see this link), but I don't think you can do it on a "skip all headers" kind of thing.
Basic method of all debugging:
1)Know what your program is supposed to do.
2)Detect when it doesn't. Print some values with printf()
3)Fix it.
This link might be useful.
Use the last version Visual Studio. It has the Just My Code option enabled by default. This makes the debugger skip "not that interesting" standard library code, including inline functions in system headers.

Extending functionalitty (aka Modifying) visual studio C/C++ compiler

I want to extend the features of C/C++ compiler used in Visual studio. Basically, I want to write a tool which parses the c/c++ code and prints out where all branching (if check, break statement, for/while loops, etc) happens in the code. Then I would like to use this information while executing code to grey out areas of code that have not been executed, for a given testcase.
Is it possible? Does Microsoft provide any way to add features to its compiler/debugger?
--Thanks
Microsoft's compilers always were a black box. (Taken from their own site from the upcoming link). So you have a chance that it is not possible right now. But with project Roslyn that's about to be changed.
Anyway, it seems to me you shouldn't look into the compiler but the debugging part of Visual Studio. There are APIs that allow you to interact with the debugger and that's probably the road you want to take (and others did take).
You would not normally do that by modifying the compiler, and with Microsoft's compiler you cannot in any case. Rather you would write a preproccessor that instruments the code (inserts additional code at the conditional nodes to trace the control flow), and then a tool that process the trace data to determine what ran.
Visual Studio itself has an add-in architecture that would allow you to render this data in the editor in the manner you describe.
The instrumentation itself is not trivial - it will need to be able to parse all valid C and C++ code and be able to retain the original line number information so that the uninstrumented code can be presented. For completeness it would have to be able to re-factor code using the ternary ?: operator so that its flow could be instrumented. The instrumented code would also need to be the code output by the standard pre-processor rather than the original source code - making line number tracking a little more difficult (although the preprocesor already manages that for use with the debugger).
Use preprocessor to instrument code. Code should spit out a file while running. At breakpoints, process this information and "grey out" code using debugger APIs + visual studio addin mechanism.

What's the reason to use DEBUG macro in C++?

I'm working on C++ program built by other people, and saw a lot of uses of DEBUG like this
#ifdef DEBUG
cout << "Value is "<< value << endl;
#endif
I myself am still in the learning process to become an affluent C++ programmer, and I majorly use Visual Studio and breakpoints to debug. So I'm wondering, if I'm able to step through the code to debug values, is there any other reason to use these kind of macros?
Tried to google but didn't find much useful page.
Thanks.
Sometimes you don't want to step through the whole code, but just inspect the output in the terminal.
If the code is compiled with DEBUG defined, probably in a debug build, you see the output. For a release build, you don't. If you go to project settings -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, you'll see that DEBUG is defined for the debug build, but it's not for release. (I actually have _DEBUG)
Imagine you have a huge function, and what you're interested in is at the 1000th line (it's old code, can't change it). Would you rather step through all of that messy legacy code or have helpful debug statements at key points? Would you rather the console tell you where something went wrong, or set breakpoints at each of the 237 return statements at fail locations?
While you're debugging, it is a common practice to dump some intermediate values on the screen. The visual debugger is not always helping, because you waste a lot of time manipulating with mouse.
The need for "text-mode debugging" and logging also frequently comes from the embedded systems experience, where you don't have much visual aid and all you can do is to dump a byte or two to serial port or something like that. When you get used to quickly finding critical debug points, you just insert some printing code there whose value checks the program correctness.
The "DEBUG" macro is defined by MSVC++ compiler while your project is compiled in Debug mode. When you're making the Release version, all the code which is actually useless for the end-users gets "stripped away" by the preprocessor.
Typical snippet
#ifdef DEBUG
Some code
#endif
is removed by the preprocessor if the DEBUG is not defined.
It can be handy to use console output rather than a debugger for identifying multithreading bugs. Interrupting the flow of the program via a breakpoint often stops the bug from occurring because it stops the threads stepping on each other's toes. The same is true of other timing-based bugs.
"I majorly use Visual Studio and breakpoints to debug"
Debugging your code by watching its behaviour step by step is quite hard or even impossible in some situations. Sometimes it's just easier to create this kind of "debug output" so that you see what's going on from these logs instead of trying to step through it in real time.
Checking whether DEBUG symbol is defined is to make sure that your release version doesn't make this kind of output. Note that Visual Studio defines _DEBUG for debug configuration. More specifically: "The compiler defines _DEBUG when you specify the /MTd or /MDd option. These options specify debug versions of the C run-time library."
There is also NDEBUG that disables C-style assertions when defined. For more information check _DEBUG vs NDEBUG.
It's for debugging, by wrapping the code in preprocessor commands you can turn that code on or off.
Take a look here: C++ Notes: Preprocessor
Easy, when you want to get some messages that may help you to make a "soft" debug you just define DEBUG and the sentences between #ifdef DEBUG and #endif will make effect and in your case get some useful messages.
This way, when you finished developing and you want to make a release you just undefine debug and the messages will appear no more.
You may think yes, it is a good idea, but is more code for the app but the good point is that those are macros and are evaluated at compile time, therefore, the app will be the same that if you delete all of them :)
I'd recommend not using a DEBUG macro. Instead, use the standard NDEBUG macro, which is defined when debugging code is not wanted, rather than defined when debugging code is wanted. That is, have debugging code active by default. You will find that only a small core of performance critical code needs to have debugging checks switched off to get adequate performance.

Tell visual studios to skip over?

While debugging i press F11 often to step into a function. For this project i use properties everwhere which is simply a RAII wrapper that checks if i have set the variable and gives me an assert if i have not. Its been useful.
However now debugging is annoying since F11 will step into the property. Can i skip it somehow? by writing attributes, keywords or anything?
I am using VS11beta
This is quite easy to set up when you're dealing with managed code. You can manually mark the function with the DebuggerHiddenAttribute class, and even turn on built-in debugger settings like "Step over properties and operators".
Unfortunately, automatically stepping over a particular function is not supported by Visual Studio for native C++ code. (At least, it wasn't supported up until VS 2010—I haven't had enough time to play with VS 11 to see if this is something they gave us to make up for the fact that they stole all our colors.)
There is a workaround, though, documented a long while ago on Andy Pennell's blog:
How to Not Step Into Functions using the Visual C++ Debugger
Essentially, you edit the following registry key (for VS 2010):
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\NativeDE\StepOver
or for 64-bit applications:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\NativeDE\StepOver
to specify a regular expression that will be matched against functions by the debugger.
For example, if you don't want the debugger to step into overloaded operators, you can use the following expression:
\scope:operator\oper:=NoStepInto
As the disclaimer in the blog post says:
This is not a documented feature. Well obviously you are reading this “documentation” right here, but what I mean is that is not guaranteed to work as it was never officially tested, not is it supported by Microsoft. Its existence in future versions or update to current versions is not guaranteed.

how to set break point on all function start in c++ file using visual studio 2008

I have more than 100 files as source. Each of these files are atleast 1000 lines. In order to debug the sequence of calls, I want to put a break point on all the functions for all the files. There are plenty of COM Pointers in all the files.
Is it even possible with VS 2008 to set multiple break points?
I know this is a crazy idea, any optimization suggestion?
You might be able to use a custom prologue to add a machine debug break to every function in the compilation unit.
http://msdn.microsoft.com/en-us/library/6xy06s51(v=vs.80).aspx
maybe I'm not understanding you correctly, but have you tried using the debug features "Step Into (F11)", "Step Over (F10)", and "Step Out (Shift+F11)"? That would effectively do the same thing I would imagine since you want to stop at each function call anyway.