@@ -239,10 +239,13 @@ internal class PidIpcEndpoint : IpcEndpoint
239
239
{
240
240
public static string IpcRootPath { get ; } = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? @"\\.\pipe\" : Path . GetTempPath ( ) ;
241
241
public static string DiagnosticsPortPattern { get ; } = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? @"^dotnet-diagnostic-(\d+)$" : @"^dotnet-diagnostic-(\d+)-(\d+)-socket$" ;
242
-
242
+ // Format strings as private const members
243
+ private const string _defaultAddressFormatWindows = "dotnet-diagnostic-{0}" ;
244
+ private const string _dsrouterAddressFormatWindows = "dotnet-diagnostic-dsrouter-{0}" ;
245
+ private const string _defaultAddressFormatNonWindows = "dotnet-diagnostic-{0}-{1}-socket" ;
246
+ private const string _dsrouterAddressFormatNonWindows = "dotnet-diagnostic-dsrouter-{0}-{1}-socket" ;
243
247
private int _pid ;
244
248
private IpcEndpointConfig _config ;
245
-
246
249
/// <summary>
247
250
/// Creates a reference to a .NET process's IPC Transport
248
251
/// using the default rules for a given pid
@@ -289,11 +292,11 @@ private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
289
292
290
293
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
291
294
{
292
- defaultAddress = $ "dotnet-diagnostic- { pid } " ;
295
+ defaultAddress = string . Format ( _defaultAddressFormatWindows , pid ) ;
293
296
294
297
try
295
298
{
296
- string dsrouterAddress = Directory . GetFiles ( IpcRootPath , $ "dotnet-diagnostic-dsrouter- { pid } " ) . FirstOrDefault ( ) ;
299
+ string dsrouterAddress = Directory . GetFiles ( IpcRootPath , string . Format ( _dsrouterAddressFormatWindows , pid ) ) . FirstOrDefault ( ) ;
297
300
if ( ! string . IsNullOrEmpty ( dsrouterAddress ) )
298
301
{
299
302
defaultAddress = dsrouterAddress ;
@@ -305,11 +308,11 @@ private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
305
308
{
306
309
try
307
310
{
308
- defaultAddress = Directory . GetFiles ( IpcRootPath , $ "dotnet-diagnostic- { pid } -*-socket" ) // Try best match.
311
+ defaultAddress = Directory . GetFiles ( IpcRootPath , string . Format ( _defaultAddressFormatNonWindows , pid , "*" ) ) // Try best match.
309
312
. OrderByDescending ( f => new FileInfo ( f ) . LastWriteTime )
310
313
. FirstOrDefault ( ) ;
311
314
312
- string dsrouterAddress = Directory . GetFiles ( IpcRootPath , $ "dotnet-diagnostic-dsrouter- { pid } -*-socket" ) // Try best match.
315
+ string dsrouterAddress = Directory . GetFiles ( IpcRootPath , string . Format ( _dsrouterAddressFormatNonWindows , pid , "*" ) ) // Try best match.
313
316
. OrderByDescending ( f => new FileInfo ( f ) . LastWriteTime )
314
317
. FirstOrDefault ( ) ;
315
318
@@ -350,8 +353,15 @@ public static string GetDefaultAddress(int pid)
350
353
string msg = $ "Unable to connect to Process { pid } .";
351
354
if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
352
355
{
356
+ int total_length = IpcRootPath . Length + string . Format ( _defaultAddressFormatNonWindows , pid , "##########" ) . Length ;
357
+ if ( total_length > 108 ) // This isn't perfect as we don't know the disambiguation key length. However it should catch most cases.
358
+ {
359
+ msg += "The total length of the diagnostic socket path may exceed 108 characters. " +
360
+ "Try setting the TMPDIR environment variable to a shorter path" ;
361
+ }
353
362
msg += $ " Please verify that { IpcRootPath } is writable by the current user. "
354
363
+ "If the target process has environment variable TMPDIR set, please set TMPDIR to the same directory. "
364
+ + "Please also ensure that the target process has {TMPDIR}/dotnet-diagnostic-{pid}-{disambiguation_key}-socket shorter than 108 characters. "
355
365
+ "Please see https://aka.ms/dotnet-diagnostics-port for more information" ;
356
366
}
357
367
throw new ServerNotAvailableException ( msg ) ;
@@ -367,7 +377,7 @@ public static bool IsDefaultAddressDSRouter(int pid, string address)
367
377
address = address . Substring ( IpcRootPath . Length ) ;
368
378
}
369
379
370
- string dsrouterAddress = $ "dotnet-diagnostic-dsrouter- { pid } " ;
380
+ string dsrouterAddress = string . Format ( _dsrouterAddressFormatWindows , pid ) ;
371
381
return address . StartsWith ( dsrouterAddress , StringComparison . OrdinalIgnoreCase ) ;
372
382
}
373
383
0 commit comments