-
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
Open
jborean93
wants to merge
1
commit into
PowerShell:main
Choose a base branch
from
jborean93:start-debugging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
JustinGrote marked this conversation as resolved.
Show resolved
Hide resolved
JustinGrote marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
# 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.