Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -282,7 +284,22 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar

#if DEBUG
case "attachdebugger":
Debugger.Launch();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Debugger.Launch() only works on Windows.
_ = Debugger.Launch();
}
else
{
var timeout = TimeSpan.FromMinutes(2);
Console.WriteLine($"Compiler started with process ID {Environment.ProcessId}");
Console.WriteLine($"Waiting {timeout:g} for a debugger to attach");
using var timeoutSource = new CancellationTokenSource(timeout);
while (!Debugger.IsAttached && !timeoutSource.Token.IsCancellationRequested)
{
Thread.Sleep(100);
}
}
continue;
#endif
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.CodeAnalysis.CSharp;

internal static class EnvironmentExtensions
{
extension(Environment)
{
public static int ProcessId
{
get
{
#if NET
return Environment.ProcessId;
#else
return System.Diagnostics.Process.GetCurrentProcess().Id;
#endif
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.Text
Imports System.Threading
Imports Microsoft.CodeAnalysis.Collections
Imports Microsoft.CodeAnalysis.Emit
Imports Microsoft.CodeAnalysis.PooledObjects
Expand Down Expand Up @@ -446,7 +447,24 @@ Namespace Microsoft.CodeAnalysis.VisualBasic

#If DEBUG Then
Case "attachdebugger"
Debugger.Launch()
If RuntimeInformation.IsOSPlatform(OSPlatform.Windows) Then
' Debugger.Launch() only works on Windows
Debugger.Launch()
Else
Dim timeout = TimeSpan.FromMinutes(2)
#If NET Then
Dim processId = Environment.ProcessId
#Else
Dim processId = System.Diagnostics.Process.GetCurrentProcess().Id
#End If
Console.WriteLine($"Compiler started with process ID {processId}")
Console.WriteLine($"Waiting {timeout:g} for a debugger to attach")
Using timeoutSource = New CancellationTokenSource(timeout)
While Not Debugger.IsAttached AndAlso Not timeoutSource.Token.IsCancellationRequested
Thread.Sleep(100)
End While
End Using
End If
Continue For
#End If
End Select
Expand Down