Skip to content

Commit

Permalink
fix(device): Report Device architecture or Null if it cannot reliably (
Browse files Browse the repository at this point in the history
…getsentry#247)

Report Device CPU architecture
  • Loading branch information
bruno-garcia authored May 2, 2018
1 parent 66e4b9a commit 309a536
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions src/app/SharpRaven/Data/Context/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ internal static Device Create()
}
}


/// <summary>
/// Retrieves the architecture of this device.
/// </summary>
Expand All @@ -212,17 +213,46 @@ internal static string GetArchitecture() =>
#if HAS_RUNTIME_INFORMATION
// x-plat: Known results: X86, X64, Arm, Arm64,
RuntimeInformation.ProcessArchitecture.ToString();
#else
#elif NET35
// Windows: Known results: AMD64, IA64, x86
Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine)
// Unix: Known results: i686, i386, x86_64, aarch64 (Target Machine is unsupported on Mono outside Windows)
?? Environment.GetEnvironmentVariable("HOSTTYPE", EnvironmentVariableTarget.Process)
#if !NET35
// https://github.com/mono/mono/blob/cdea795c0e4706abee0841174c35799690f63ccb/mcs/class/corlib/System.Runtime.InteropServices.RuntimeInformation/RuntimeInformation.cs
?? (Environment.Is64BitProcess ? "X64" : "X86");
#else
?? (IntPtr.Size == 4 ? "X86" : "X64");
#endif
?? ProcessorArchitectureNet35();
#else
// https://github.com/mono/mono/blob/cdea795c0e4706abee0841174c35799690f63ccb/mcs/class/corlib/System.Runtime.InteropServices.RuntimeInformation/RuntimeInformation.cs#L79
Environment.Is64BitOperatingSystem ? "X64" : "X86";
#endif

#if NET35

// Will attempt to detect the architecture of the OS. Returns null if it cannot.
private static string ProcessorArchitectureNet35()
{
bool? is64Bit = null;
if (IntPtr.Size == 8)
{
is64Bit = true;
}
// Only XP SP2+ support this API
else if (Environment.OSVersion.Version.Major == 5
&& Environment.OSVersion.Version.Minor >= 1
|| Environment.OSVersion.Version.Major >= 6)
{
// 32 bit process on Windows could be WoW64, make P/Invoke call to verify
using (var p = Process.GetCurrentProcess())
{
if (IsWow64Process(p.Handle, out var retVal))
{
is64Bit = retVal;
}
}
}

return !is64Bit.HasValue ? null : is64Bit.Value ? "X64" : "X86";
}

[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool wow64Process);
#endif
}
}

0 comments on commit 309a536

Please sign in to comment.