Skip to content

Commit

Permalink
Move GetCurrentProcessId interop for Browser to managed (#39367)
Browse files Browse the repository at this point in the history
* Move GetCurrentProcessId for Browser to managed

It returns constant all the time

* Remove redundant indirection

* Fix EventSource build break

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
marek-safar and jkotas authored Jul 31, 2020
1 parent 5478621 commit 5bd0edf
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ internal static partial class Sys
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPid")]
internal static extern int GetPid();
}

internal static uint GetCurrentProcessId() => (uint)Sys.GetPid();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ internal static partial class Kernel32
[DllImport(Libraries.Kernel32)]
internal static extern uint GetCurrentProcessId();
}

internal static uint GetCurrentProcessId() => Kernel32.GetCurrentProcessId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1715,9 +1715,6 @@
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs">
<Link>Common\Interop\Unix\System.Native\Interop.GetHostName.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetPid.cs">
<Link>Common\Interop\Unix\System.Native\Interop.GetPid.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPwUid.cs">
<Link>Common\Interop\Unix\System.Native\Interop.GetPwUid.cs</Link>
</Compile>
Expand Down Expand Up @@ -1826,6 +1823,9 @@
<Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetPid.cs">
<Link>Common\Interop\Unix\System.Native\Interop.GetPid.cs</Link>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.Unix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.GetDisplayName.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if ES_BUILD_STANDALONE
using System;
using System.Diagnostics;
using Environment = Microsoft.Diagnostics.Tracing.Internal.Environment;
#else
using System.Threading.Tasks;
#endif
Expand Down Expand Up @@ -511,7 +512,7 @@ private static unsafe int AddIdToGuid(Guid* outPtr, int whereToAddId, uint id, b
uint* sumPtr = (uint*)outPtr;
// We set the last DWORD the sum of the first 3 DWORDS in the GUID. This
// This last number is a random number (it identifies us as us) the process ID to make it unique per process.
sumPtr[3] = (sumPtr[0] + sumPtr[1] + sumPtr[2] + 0x599D99AD) ^ EventSource.s_currentPid;
sumPtr[3] = (sumPtr[0] + sumPtr[1] + sumPtr[2] + 0x599D99AD) ^ (uint)Environment.ProcessId;

return (int)(ptr - ((byte*)outPtr));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2843,14 +2843,6 @@ private void EnsureDescriptorsInitialized()
DefineEventPipeEvents();
#endif
}
if (s_currentPid == 0)
{
#if ES_BUILD_STANDALONE
// for non-BCL EventSource we must assert SecurityPermission
new SecurityPermission(PermissionState.Unrestricted).Assert();
#endif
s_currentPid = Interop.GetCurrentProcessId();
}
}

// Send out the ETW manifest XML out to ETW
Expand Down Expand Up @@ -3807,7 +3799,6 @@ private bool SelfDescribingEvents

private string[]? m_traits; // Used to implement GetTraits

internal static uint s_currentPid; // current process id, used in synthesizing quasi-GUIDs
[ThreadStatic]
private static byte m_EventSourceExceptionRecurenceCount; // current recursion count inside ThrowEventSourceException

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
#endif
#if ES_BUILD_AGAINST_DOTNET_V35
using Microsoft.Internal;
Expand All @@ -22,32 +23,18 @@ namespace Microsoft.Diagnostics.Tracing.Internal
namespace System.Diagnostics.Tracing.Internal
#endif
{
#if ES_BUILD_STANDALONE
internal static class Environment
{
public static readonly string NewLine = System.Environment.NewLine;

public static int TickCount => System.Environment.TickCount;

public static string GetResourceString(string key, params object?[] args)
{
string? fmt = rm.GetString(key);
if (fmt != null)
return string.Format(fmt, args);

string sargs = string.Join(", ", args);
public static int ProcessId = GetCurrentProcessId();

return key + " (" + sargs + ")";
}

public static string GetRuntimeResourceString(string key, params object?[] args)
private static int GetCurrentProcessId()
{
return GetResourceString(key, args);
new SecurityPermission(PermissionState.Unrestricted).Assert();
return (int)Interop.Kernel32.GetCurrentProcessId();
}

private static readonly System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Microsoft.Diagnostics.Tracing.Messages", typeof(Environment).Assembly());
}

#if ES_BUILD_STANDALONE
internal static class BitOperations
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -365,7 +352,5 @@ internal static partial class Kernel32
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern uint GetCurrentProcessId();
}

internal static uint GetCurrentProcessId() => Kernel32.GetCurrentProcessId();
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ private static OperatingSystem GetOSVersion()
{
return new OperatingSystem(PlatformID.Other, new Version(1, 0, 0, 0));
}

private static int GetCurrentProcessId() => 42;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,7 @@ private static unsafe bool TryGetUserNameFromPasswd(byte* buf, int bufLen, out s
// Otherwise, fail.
throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
}

private static int GetCurrentProcessId() => Interop.Sys.GetPid();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ private static string ExpandEnvironmentVariablesCore(string name)

private static bool Is64BitOperatingSystemWhen32BitProcess => false;

private static int GetCurrentProcessId() => Interop.Sys.GetPid();

internal const string NewLineConst = "\n";

public static string SystemDirectory => GetFolderPathCore(SpecialFolder.System, SpecialFolderOption.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ internal static Guid CreateGuidForTaskID(int taskID)
// using the taskGuid, the appdomain ID, and 8 bytes of 'randomization' chosen by
// using the last 8 bytes as the provider GUID for this provider.
// These were generated by CreateGuid, and are reasonably random (and thus unlikely to collide
uint pid = EventSource.s_currentPid;
int pid = Environment.ProcessId;
return new Guid(taskID,
(short)DefaultAppDomainID, (short)(DefaultAppDomainID >> 16),
(byte)pid, (byte)(pid >> 8), (byte)(pid >> 16), (byte)(pid >> 24),
Expand Down

0 comments on commit 5bd0edf

Please sign in to comment.