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'
Comment on lines +11 to +12
Copy link
Preview

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding 'pwsh' assumes PowerShell Core is available. Consider using $PSVersionTable.PSEdition to detect the current PowerShell edition and use 'powershell' for Windows PowerShell or 'pwsh' for PowerShell Core, or provide a parameter to specify the executable.

Suggested change
$procParams = @{
FilePath = 'pwsh'
$psExecutable = if ($PSVersionTable.PSEdition -eq 'Core') { 'pwsh' } else { 'powershell' }
$procParams = @{
FilePath = $psExecutable

Copilot uses AI. Check for mistakes.

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
Comment on lines +13 to +17
Copy link
Preview

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The polling loop calls Get-Runspace repeatedly without any delay optimization. Consider adding a small delay before each check or implementing exponential backoff to reduce unnecessary CPU usage during the wait period.

Suggested change
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
$currentDelay = 1 # Initial delay duration in seconds
$maxDelay = 16 # Maximum delay duration in seconds
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds $currentDelay
$currentDelay = [math]::Min($currentDelay * 2, $maxDelay) # Exponential backoff

Copilot uses AI. Check for mistakes.

Comment on lines +12 to +17
Copy link
Preview

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The infinite loop lacks a timeout mechanism. Consider adding a maximum wait time to prevent the script from hanging indefinitely if the debugger never attaches.

Suggested change
$runspaces = Get-Runspace
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
$runspaces = Get-Runspace
$maxWaitTime = 60 # Maximum wait time in seconds
$elapsedTime = 0 # Initialize elapsed time
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
$elapsedTime += 1
if ($elapsedTime -ge $maxWaitTime) {
Write-Host "Timeout reached while waiting for debugger to attach."
break
}

Copilot uses AI. Check for mistakes.

}

# 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