Debugging PowerShell in Visual Studio Code – Part 02

In this blog post, we’ll continue from where we left earlier in the previous post on debugging PowerShell using Visual Studio code. You may be beginning to notice that the debugging features offered in Visual Studio code are far ahead in terms of usability.

Not only that, we can set line breakpoints (which can be specified by pressing F9 on code line in reference), we can also set conditional breakpoints, functional breakpoints and trace breakpoints.

A functional breakpoint can be defined as breakpoint which is invoked on calling of a particular function. You can set a function breakpoint to break into the debugger not only on a particular function invocation but also on an alias, a built-in command, or application invocation. 

If you are continuing from last blog post, first remove all line breakpoints from DEBUG view, so that we can understand it working. To set a function breakpoint, hover over the BREAKPOINTS section title bar, click the + button, type function name, and then press Enter as shown in the following screenshot:

Add Function BreakPoint in the Visual Studio Code
Add Function BreakPoint in the Visual Studio Code

Press F5 to start debugging. It will start processing code and will break exactly when the function in reference is called. We can tell when the debugger is stopped on a function breakpoint by looking at the CALL STACK section of the Debug view. It will indicate that it is paused on a function breakpoint. The Debug Console will also indicate that a breakpoint has paused the debugger as shown in the following screenshot. If the Debug Console is not visible, select Debug Console from the View menu, or press Ctrl + Shift + Y.

Following are couple of screenshots for same:

Observe functional breakpoint in call stack
Observe functional breakpoint in call stack
Observe functional breakpoint in debug console
Observe functional breakpoint in debug console

We’ll now remove the function breakpoint by right-clicking it in the BREAKPOINTS section, and selecting Remove breakpoint.

A conditional breakpoint is a line breakpoint that breaks into the debugger only when the line is executed and a user-supplied expression evaluates to $true. Conditional breakpoints are handy in scenarios where a line is executed many times, but you’re interested in breaking into the debugger only when a certain condition is true.

Let’s set a conditional breakpoint on one of our code lines. First we need to place cursor at the line in reference. Then we go to Debug options and select conditional breakpoint:

Select conditional breakpoint from Debug Options
Select conditional breakpoint from Debug Options

We’ll enter the expression as shown in the following screenshot, and then press Enter. As in the case with setting the value of a variable in the VARIABLES section, you have to use PowerShell syntax in the condition expression.

Define Conditional BreakPoint in Visual Studio Code
Define Conditional BreakPoint in Visual Studio Code

After you set the breakpoint, you will see that conditional breakpoints are displayed with an “=” sign in the glyph. When this expression evaluates to $true, the debugger will pause execution. When this expression evaluates to $true, the debugger will pause execution. Now press F5 to start debugging.

Tracepoints allow you to emit information to the Debug Console (or change state in your script) without ever pausing the debugger. These are effectively the same as using Set-PSBreakpoint -Action {scriptblock} where the scriptblock tests for a certain condition, and if met, executes some script and then uses Continue to resume execution.

Let’s convert our previous conditional breakpoint to a tracepoint. Right-click the conditional breakpoint in the left editor margin, select Edit Breakpoint…, and modify the condition to:

Define tracepoint in Visual Studio Code
Define tracepoint in Visual Studio Code

Press F5 to start debugging. You will notice that the script runs to completion without ever breaking into the debugger. In the Debug Console (Ctrl + Shift + Y), you will see the following output from the tracepoint defined.

Line breakpoints support not only condition expressions but hit counts as well. When you specify a hit count, the PowerShell debugger notes the number of times that the breakpoint has been encountered and only breaks into the debugger after the specified hit count has been reached.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s