P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

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.

Upvotes: 5

Views: 5912

Answers (5)

Eduardo Lemus
Eduardo Lemus

Reputation: 21

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.

Upvotes: 0

Kurt Hutchinson
Kurt Hutchinson

Reputation: 3069

Yes, you can do this directly using a tracepoint.

  1. 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".
  2. 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

Upvotes: 1

heavyd
heavyd

Reputation: 17691

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

Upvotes: 6

Alex Moore
Alex Moore

Reputation: 3455

Try:

#if DEBUG
Response.Redirect("LoginFail.aspx");
#endif

Make sure you have the DEBUG constant checked in your build properties.

Upvotes: 0

Michael
Michael

Reputation: 1879

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

Upvotes: 0

Related Questions