Is it possible to automatically return from a function using a Breakpoint/Tracepoint?
I don’t want to drag the execution point or set it with CTRL+SHIFT+F10 every time the breakpoint is hit.
I tried “printing” the following “messages” When Hit, but the executions continue without change.
{return;}
{return null;}
Note that I need to return from the function without actually changing code.
To clarify what a Tracepoint is: “A tracepoint is a breakpoint with a custom action associated with it. When a tracepoint is hit, the debugger performs the specified tracepoint action instead of, or in addition to, breaking program execution.” From MSDN.
If you don’t know what I mean with “printing messages”, you might want to read this AltDevBlogADay post about Tracepoints. It’s good.
Okay, after a bit of digging around you can do this – but it’s not going to work in all cases.
Beware, this uses macros and can’t be guaranteed to work with inline delegates; or with methods that actually need to return something. It automates the process described by @juergen d and @Erno when a breakpoint is hit; using very simple heuristics to find where the end of the current function is.
You first need to add this macro to your macros environment (open with ALT+F11 in VS). This code is probably not as good as it could be as I’ve just rushed it out 🙂
With that in place, now you can set your breakpoint – right click on it and hit the
When hit...menu option (this only works in VS2010 I believe). ScottGu describes this in this blog post.From the dialog, find the
ExitStackmacro that you’ve just pasted in.Run the code with the debugger attached and when the breakpoint is hit the rest of the function’s code should be skipped. This should obey other debugger tricks – like conditions etc.
Note – I used this SO to solve a problem I was having; originally I was invoking the debugger’s SetNextStatement method directly and it didn’t work
I have no idea how methods that should return will behave – in theory they should return whatever the return value local is at the time, but in some cases the fact is this simply won’t work!
Equally if the breakpoint is in a try/catch block then it won’t work – because the try/catch has to be exited before you can set the next statement to somewhere outside of it.