Skip to content

WIP: Add examples for Start-DebugAttachSession #5246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/ChildDebugSessionAttach.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Example on how Start-DebugAttachSession can be used to attach to another
# process. This launches a child process that runs ChildDebugSessionTarget.ps1
# but it can be adapted to attach to any other PowerShell process that is
# either already running or started like this example. To test this example,
# add a breakpoint to ChildDebugSessionTarget.ps1, select the
# 'PowerShell Launch Current File' configuration and press F5.

$pipeName = "TestPipe-$(New-Guid)"
$scriptPath = Join-Path -Path $PSScriptRoot -ChildPath 'ChildDebugSessionTarget.ps1'

$procParams = @{
FilePath = 'pwsh'
ArgumentList = ('-CustomPipeName {0} -File "{1}" -WaitForAttach' -f $pipeName, $scriptPath)
PassThru = $true
}
$proc = Start-Process @procParams

Start-DebugAttachSession -CustomPipeName $pipeName -RunspaceId 1

# We need to ensure this debug session stays alive until the process exits. If
# we exit early then the child debug session will also exit.
$proc | Wait-Process
31 changes: 31 additions & 0 deletions examples/ChildDebugSessionTarget.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[CmdletBinding()]
param (
[Parameter()]
[switch]
$WaitForAttach
)

if ($WaitForAttach) {
# For an attach request we need to wait for the debug pipe runspace to be
# opened before continuing. There is no builtin way to do this so we
# poll the runspace list until a new one is created.
$runspaces = Get-Runspace
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
}

# Windows PowerShell 5.1 will not sync breakpoints until the debugger has
# stopped at least once. We use Wait-Debugger to make this happen.
if ($PSVersionTable.PSVersion -lt '6.0') {
Wait-Debugger
}
else {
Start-Sleep -Seconds 1 # Give the debugger time to sync breakpoints
}
}

$processInfo = "This process is running with PID $PID and has runspace ID $([Runspace]::DefaultRunspace.Id)"
Write-Host $processInfo # Place breakpoint here