Visual Studio 2005 : Break when a value appears - visual-studio-2005

I'm trying to simplify my debugging tasks and I had an idea which could increase my debugging speed.
Suppose I have a value, say 2.8651 that appear in the code at a moment I do not know.
I'm wondering if it was possible to create a super breakpoint which would stop at the first time one of the variable takes this value.
I recall that I do not know which variable takes the value.
I could know it by spending some time on the debugger but I'm lazy.
I'm not really familiar with VBA for VS. I guess, a solution would be to create a macro which would loop along local variable at each line of the code execution. It'd stop when the condition localVariable == 2.8651 is verified.
Thank you for your answers!

There's no such thing as a super breakpoint for a block of code. There are two options to achieve what you want:
Place a conditional breakpoint on each line with a variable assignment. Right-click the breakpoint and click Condition... to specify when it should break.
Place a single conditional breakpoint in the code block and check all the required variables in the condition.
As you suggested, you can place a single breakpoint with a macro, by right-clicking the breakpoint and select When Hit.... However, I highly doubt that it's possible to control the breakpoint from the macro, so this wouldn't work.

Related

listing variable contents at certain breakpoint in windbg

I am trying to augment sort of a printf or cout at certain breakpoints. Is there a way I can tell windbg to put a breakpoint and instead of breaking just list variables when execution flows through that breakpoint.
I tried to find about this on Google, but everywhere I get help related to breaking on certain condition.
I am sure there has to be a way of doing what I am trying to do.
Thanks for any help.
-Shobhit
A simple command would be
bp MyFunction "dv;g"
Where dv displays the local variables and then g continue execution.

Duplicated Break Points in VS 2008

I am having a problem which I can't quite understand.
I put a breakpoint in one page, and when I debug, another break points appear from another .cpp.
Does anyone know what could be the source of the problem here?
Please help.
thanks
I can think of 2 possible causes, but it's not really a problem:
The compiler inlines function, so when you set a breakpoint in that function, it automatically sets breakpoints to wherever it is encountered.
The optimizer squeezed more methods in the same location, so all could be marked.
Will add more if I can think of any.

Debugging - skip code with a breakpoint

You know how you can click the yellow arrow on a breakpoint and drag it down to skip execution of lines of code? Well, is it possible to create a "When Hit" macro (or something similar) that skips execution of the line containing the breakpoint?
So instead of writing code like
if(!Debugging)
Response.Redirect("LoginFail.aspx");
I could sit a breakpoint on the Response.Redirect() line and "When Hit" will skip it.
I don't know of baked in way of doing this. You can however set the "When hit" options of a breakpoint to run a macro. It shouldn't be hard to write a macro that gets the current line, and then sets the next debugger line. You'll probably want to look at the Debugger.SetNextStatement method.
A macro like this should do it:
Public Sub SkipNextLine()
ActiveDocument().Selection.LineDown()
DTE.ExecuteCommand("Debug.SetNextStatement")
End Sub
Yes, you can do this directly using a tracepoint.
Find the address of the return statement or final closing curly brace at the bottom of the function by breaking on it once, and then looking at the EIP register either in the Registers window or add a Watch for "#eip".
Add a tracepoint to the line you want to jump from. Remember that the jump will occur before anything on the line is executed. The content of the tracepoint should be {#eip = address} using the address from step 1.
See also https://stackoverflow.com/a/14695736/301729
There's no way I know of to do this with a breakpoint but you can use compiler directives to skip code
#if (DEBUG)
//this code is skipped in debug mode
#endif
Try:
#if DEBUG
Response.Redirect("LoginFail.aspx");
#endif
Make sure you have the DEBUG constant checked in your build properties.
Agree with the use of compiler directives or any other requiring to add logic in code. However, also understand editing the debugged code is not always an option (e.g. not owning the code might imply not having all resources needed to build it).
If that's your case, I'm afraid that writing your own macro or waiting for VS to have one built-in equivalent are your only options.

Can You Use MSVC 6.0's Debugger to 'Step Into' a Macro?

I am using MSVC 6.0 to call a macro in the Win32API and I'm getting an access violation. I know that the pointers I'm passing to the macro contain valid addresses, though they're evidently not pointing to the correct data.
The macro accepts multiple pointers, and I'm not sure which pointer is erroneous, so I'd like to use MSVC's debugger to 'step into' the macro to see exactly where the problem is. When I've tried thus far, the debugger just throws the access violation error.
Is it possible to 'step into' a macro using MSVC 6.0's debugger? If not, is there anyway for me to check what the macro expands to, so I can get a better idea of what I'm not doing correctly?
If you really need to trace the macro code, the only way would be to find the definition of the macro, manually "instantiate" the macro code (substituting the parameters) in place where it is "called", and then trace it in the debugger as ordinary code.
Alternative variant would be to step through the disassembly, if your skill level is sufficient to back-associate the disassembled code with the original macro code.
You cannot step into the macro because at the point compiler does its job, the macro is already expanded. However, you can step through a macro - if you just do "step", you will actually step through all code inside the macro as if it was expanded, line by line. If you to "step into", you will step into every function call made from that macro. If the macro is small enough, and/or you know it very well, you can do a "blind step through" that way.
You can step into functions that are called from the macro but as far as I know can not really step through the macro lines themselves. And yes if you code compiles - you can find the macro definition (use MSVC function/class browser to find where it is defined, some header file probably)
I'd just step into the disassembly - usually, even if you're not an assembly expert, short runs of code (a few lines) the assembly map back to the C/C++ code pretty readily (especially in non-Release builds). Hopefully the macro isn't so hairy that that isn't the case here.
Remember that plenty of debugging occurs even without source code, so having the source and the disassembly together usually isn't too bad. And if it's something you haven't much experience with, it's great experience to get.

Performing expression evaluation/reduction in Visual Studio 2008

Is it possible to get Visual Studio to do mathematical expression evaluation/reduction?
For example if I type '-0.005 + -0.345' how do I get Visual Studio to reduce that (i.e. replace it with the reduction)? Do I have to write a macro? If so, are there any pre-existing macros to do this type of expression reduction?
Just to be clear, I want to be able to highlight an expression and have it replaced with the reduced result. Many are suggesting the immediate window but I fail to see how that will suffice?
Edit I should point out that this is while editing not running or debugging. The immediate window is of little to no use. I also consider this a language neutral question. I would certainly be interested in seeing alternative macros to the one I had posted.
Edit Going Once... Going Twice... (i.e. any other suggestions before I consider accepting my own answer?)
Thank you for the above answers.
There probably are better ways, but here's a quick and dirty macro that does what I need.
References to the System.Data and System.XML namespaces need to be added.
Highlight the expression you want to evaluate and run the macro (it uses the calculated column in the DataTable to evaluate the expression.) It will replace the expression with the reduced result.
Edit - Updated code below. It worked extremely well for reducing a large number of expressions. As pointed out by others there is the immediate window but this will not work for editing purposes. This macro is a language independent solution for basic expressions "(), +, -, *, /".
Sub Eval()
Dim ts As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
Using dt As New DataTable()
dt.Columns.Add("Expression", GetType(Double), ts.Text)
dt.Rows.Add(dt.NewRow)
ts.Text = CDbl(dt.Rows(0).Item("Expression"))
End Using
End Sub
Visual Studio by default will not do any mathematical expression evaluation / reduction. I'm not sure if you can get support for that via items like ReSharper, but if it is available it will be in an add-in.
Also, it would be helpful to know the language you are working in?
Some languages may be helpful in this area. F# for instance makes it easy to evaluate expressions in the IDE via the interactive window and will display out the result. This could easily be added back into your code but it doesn't appear to be exactly what you're looking for.
Here's an answer: Yes, it is possible using the following steps. (While technically performing what you're asking for, I'm not sure it will be extremely useful. :-)
Set a breakpoint in your program that's likely to get hit when you debug the program.
Then, run your program under the Visual Studio debugger.
When the breakpoint is hit, open the Watch window.
In the Watch window, add a new watch by clicking in the Name column.
Enter your expression '-0.005 + -0.345' (without the quotes) then hit [Enter].
... You should see the Value column get populated with -0.35.
Of course, that isn't in the context of the editor window ... which is, presumably, where you'd want to perform the reduction. So again, not very useful, I imagine. An add-in is the likely way to do that in the editor window.
You could just go to the immediate window and type "?<yourExpression>"

Resources