-
Notifications
You must be signed in to change notification settings - Fork 517
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The polling loop calls
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
Comment on lines
+12
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# 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 |
There was a problem hiding this comment.
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.Copilot uses AI. Check for mistakes.