Skip to content

Commit

Permalink
Fix error autodetection (#69)
Browse files Browse the repository at this point in the history
* Fix error autodetection

* Fix error autodetection
  • Loading branch information
nohwnd authored May 27, 2024
1 parent 8007706 commit 0066ca7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Profiler/Trace-Script.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ function Trace-Script {
$ticks = if (0 -ne $total.Ticks) { $total.Ticks } else { 1 }
$line.Percent = [Math]::Round($line.Duration.Ticks / $ticks, 5, [System.MidpointRounding]::AwayFromZero) * 100
$line.SelfPercent = [Math]::Round($line.SelfDuration.Ticks / $ticks, 5, [System.MidpointRounding]::AwayFromZero) * 100
$line.MemoryPercent = [Math]::Round($line.Memory / $totalMem, 5, [System.MidpointRounding]::AwayFromZero) * 100
$line.SelfMemoryPercent = [Math]::Round($line.SelfMemory / $totalMem, 5, [System.MidpointRounding]::AwayFromZero) * 100
$line.MemoryPercent = if (0 -ne $totalMem) { [Math]::Round($line.Memory / $totalMem, 5, [System.MidpointRounding]::AwayFromZero) * 100 } else { 100 }
$line.SelfMemoryPercent = if (0 -ne $totalMem) { [Math]::Round($line.SelfMemory / $totalMem, 5, [System.MidpointRounding]::AwayFromZero) * 100 } else { 100 }
$line
}
Write-TimeAndRestart $sw
Expand Down
5 changes: 3 additions & 2 deletions csharp/Profiler/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ private static bool HasDifferentTracer(ITracer currentTracer, object newTracer)
private static void EnsureTracingWasNotCorrupted()
{
// Code like Set-PSDebug -Trace 0, or Get-PSBreakPoint | Remove-PSBreakPoint will break the tracing, and we will lose data,
// the only line that is allowed to do that is $corruptionAutodetectionVariable = Set-PsDebug -Trace 0
// the only line that is allowed to do that is $corruptionAutodetectionVariable = Set-PSDebug -Trace 0
// This line should be the second to last line that was executed. We re-enable the trace in Profiler, so the last line (::Unregister or ::Unpatch) is
// captured even when tracing is broken by user.
var corrupted = LastTraceItem.Extent?.Text != null && LastTraceItem.Extent.Text != "$corruptionAutodetectionVariable = Set-PsDebug -Trace 0";
const string autodetectionText = "$corruptionAutodetectionVariable = Set-PSDebug -Trace 0";
var corrupted = LastTraceItem.Extent?.Text != null && !LastTraceItem.Extent.Text.Equals(autodetectionText, StringComparison.OrdinalIgnoreCase);
if (corrupted)
{
throw new InvalidOperationException($"Trace was broken by: {LastTraceItem.Extent.Text} from {LastTraceItem.Extent.File}:{LastTraceItem.Extent.StartLineNumber}");
Expand Down

0 comments on commit 0066ca7

Please sign in to comment.