Return from function using breakpoint

Is it possible to automatically return from a function using a breakpoint / trace point?
I don't want to drag the execution point or set it with CTRL + SHIFT + F10 every time I break the breakpoint.
I tried to "print" the following "messages" when "Hit", but execution continues unchanged.

{return;} {return null;} 

Note that I need to return from the function without actually changing the code.

To clarify what a trace point is: “A breakpoint is a breakpoint with a custom action associated with it. When a trace point hits, the debugger performs the specified trace action instead of or in addition to disrupting program execution." From MSDN .

If you don’t know what I mean by “message printing”, you can read this AltDevBlogADay article on Tracepoints. It's good.

+6
source share
4 answers

Well, after a little digging, you can do this, but it will not work in all cases.

Beware, this uses macros and cannot be guaranteed to work with built-in delegates; or with methods that really 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 located.

First you need to add this macro to the macro environment (open with ALT + F11 in VS). This code is probably not as good as it can be, since I just selected it :)

 Sub ExitStack() 'get the last-hit breakpoint Dim breakPoint As EnvDTE.Breakpoint breakPoint = DTE.Debugger.BreakpointLastHit() 'if the currently open file is the same as where the breakpoint is set '(could search and open it, but the debugger *should* already have done that) If (DTE.ActiveDocument.FullName = breakPoint.File) Then Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection Dim editPoint As EnvDTE.EditPoint 'move the cursor to where the breakpoint is actually defined selection.MoveToLineAndOffset(breakPoint.FileLine, breakPoint.FileColumn) Dim codeElement As EnvDTE.CodeElement codeElement = DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElementFromPoint(selection.ActivePoint, vsCMElement.vsCMElementFunction) 'if a function is found, move the cursor to the last character of it If Not (codeElement Is Nothing) Then Dim lastLine As EnvDTE.TextPoint lastLine = codeElement.GetEndPoint() selection.MoveToPoint(lastLine) selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText) 'execute the SetNextStatement command. 'Has to be done via ExecuteCommand DTE.ExecuteCommand("Debug.SetNextStatement") End If End If End Sub 

Now you can set your breakpoint - right-click on it and select the menu item When hit... (this only works in VS2010). ScottGu describes this in this blog post.

In the dialog box, locate the ExitStack macro that you just pasted.

Run the code with the debugger attached, and when the breakpoint is deleted, the rest of the function code should be skipped. This should obey other debugger tricks - for example, conditions, etc.

Note. I used this SO to solve the problem I encountered; I initially called the debugger's SetNextStatement method directly, and it did not work

I have no idea how the methods that should return will behave - in theory they should return that the local value is returning at that time, but in some cases the fact simply will not work!

Similarly, if the breakpoint is in the try / catch block, it will not work, because try / catch must be deleted before you can set the next statement somewhere outside of it.

+3
source

In Visual Studio, you can simply drag the arrow that indicates the current line of code during debugging to the end of the function.

+7
source

Two options:

  • If you want the function to complete its execution and break after returning to the caller. Click Exit (Shift-F11)
  • If you want to skip the execution of several lines, drag the yellow marker to the next line that you want to execute. Remember that dragging a marker to a location can cause a run order that can never happen when working without intervention, so the result may be completely wrong.
+3
source

Yes, you can do this directly using a trace point.

  • Find the address of the return statement at the bottom of the function, breaking it once, and then look at the EIP register either in the register window or add a Watch for "@eip".
  • Add a trace point to the line you want to jump from. Remember that a transition will occur before anything on the line is completed. The contents of the trace point should be {@eip = address} using the address from step 1.
  • Profit!

See also fooobar.com/questions/327579 / ...

+1
source

Source: https://habr.com/ru/post/906373/


All Articles