private void WriteProcessList(IList <IProcessInfo> processes, ConsoleLog console) { if (!processes.Any()) { return; } var tabulator = new Tabulator( new Column("Pid") { Width = 5, RightAlign = true }, new Column("Name") { Width = 20 }, new Column("Memory") { Width = 12, RightAlign = true }, new Column("CLR Versions") { Width = 12 }, new Column("App Pool")) { Defaults = { Padding = 2 } }; console.WriteLine($"{processes.Count} matching {ProcessArchitecture.FromCurrentProcess().Describe()} processes were found:"); console.WriteLine(tabulator.GetHeader()); foreach (var candidate in processes.OrderBy(c => c.Pid)) { console.WriteLine(tabulator.Tabulate(candidate.Pid, candidate.Name, $"{candidate.WorkingSetSizeBytes.InKilobytes()} KB", DescribeCLRVersions(candidate), candidate.IISAppPoolName)); } }
public void DescribeCandidateProcesses(IList <IProcessInfo> candidates, ConsoleLog console) { var byArchitecture = candidates.ToLookup(c => c.Architecture); var self = ProcessArchitecture.FromCurrentProcess(); if (self is ProcessArchitecture.x86) { if (!x86Thunk.Bootstrap.WasUsed) { // Native x86. Write out everything. WriteProcessList(candidates, console); } else { // Write out only matching processes. WriteProcessList(byArchitecture[self].ToList(), console); } } else { WriteProcessList(byArchitecture[self].ToList(), console); if (byArchitecture[new ProcessArchitecture.x86()].Any()) { // Go to 32-bit and render the rest of the process information. Bootstrap.RecurseInto32BitProcess(); } } }
/// <summary> /// Creates a DebugSession against a memory dump file. /// </summary> /// <remarks> /// First verifies that the target dump's architecture matches this process, throwing a Requires32/64BitEnvironmentException as necessary. /// </remarks> /// <param name="dumpFilePath"></param> /// <returns></returns> public static DebugSession Load(string dumpFilePath) { // Create the data target. This tells us the versions of CLR loaded in the target process. var dataTarget = DataTarget.LoadCrashDump(dumpFilePath); ProcessArchitecture.FromClrArchitecture(dataTarget.Architecture).AssertMatchesCurrent(); return(new DebugSession(dataTarget)); }
public static IProcessInfo FromProcess(Process process, string appPoolName = null) { return(new ProcessInfo { Pid = process.Id, Name = process.ProcessName, VirtualMemorySizeBytes = process.VirtualMemorySize64, WorkingSetSizeBytes = process.WorkingSet64, Architecture = ProcessArchitecture.FromProcess(process), IISAppPoolName = appPoolName, }); }
public ProcessArchitecture GetArchitecture() { if (_architecture != ProcessArchitecture.Unknown) { return(_architecture); } try { using var process = Process.GetProcessById((int)Pid); var modules = NativeHelper.GetModules(Pid); bool HasModule(string s) => modules.Any(x => x.moduleName.Equals(s, StringComparison.InvariantCultureIgnoreCase)); // .NET 2 has mscoree and mscorwks // .NET 4 has mscoree and clr // .NET Core 3.1 has coreclr // Some unity games have mono-2.0-bdwgc.dll if (HasModule("mscoree.dll")) { if (HasModule("clr.dll")) { return(_architecture = ProcessArchitecture.NetFrameworkV4); } if (HasModule("mscorwks.dll")) { return(_architecture = ProcessArchitecture.NetFrameworkV2); } } if (HasModule("coreclr.dll")) { return(_architecture = ProcessArchitecture.NetCore); } // TODO: also check non-bleeding mono dll if (HasModule("mono-2.0-bdwgc.dll")) { return(_architecture = ProcessArchitecture.Mono); } return(ProcessArchitecture.Unknown); } catch (Exception) { return(ProcessArchitecture.Unknown); } }
public override int GetHashCode() { unchecked { var hashCode = FrameworkDescription != null?FrameworkDescription.GetHashCode() : 0; hashCode = (hashCode * 397) ^ (RunningEnvironment != null ? RunningEnvironment.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (EntryAssemblyName != null ? EntryAssemblyName.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (EntryAssemblyVersion != null ? EntryAssemblyVersion.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (LocalTimeString != null ? LocalTimeString.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (MachineName != null ? MachineName.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (OperatingSystemPlatform != null ? OperatingSystemPlatform.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (OperatingSystemArchitecture != null ? OperatingSystemArchitecture.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (OperatingSystemVersion != null ? OperatingSystemVersion.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (ProcessArchitecture != null ? ProcessArchitecture.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (ProcessorCount != null ? ProcessorCount.GetHashCode() : 0); return(hashCode); } }
public static int Main(string[] args) { if (!AssertSufficientDotNetVersion()) { return(255); } var options = new OptionSet(); var mainArguments = options.AddCollector(new Arguments()); try { var jobFactory = ParseArguments(options, mainArguments, args); var console = new ConsoleLog(Console.Error, mainArguments.Verbose); jobFactory.Validate(); var job = jobFactory.CreateJob(console); console.WriteLineVerbose($"Running as a {ProcessArchitecture.FromCurrentProcess().Describe()} process."); ExecuteJob(console, job); return(0); } catch (Requires32BitEnvironmentException) { return(Bootstrap.RecurseInto32BitProcess()); } catch (Requires64BitEnvironmentException ex) { Console.Error.WriteLine(ex.Message); return(64); } catch (ErrorWithExitCodeException ex) { if (!String.IsNullOrEmpty(ex.Message)) { Console.Error.WriteLine(ex.Message); } if (ex.ShowUsage) { ShowUsage(mainArguments.JobType, options); } return(ex.ExitCode); } catch (FeatureUnavailableException ex) { Console.Error.WriteLine(ex.Message); if (mainArguments.Verbose) { Console.Error.WriteLine(ex.InnerException); } return(254); } catch (Exception ex) { // Otherwise-unhandled exception. Console.Error.WriteLine(ex); return(255); } }
/// <summary> /// Opens a registry key. /// </summary> /// <param name="path"> /// The path to the registry key. The root of the /// path should begin with one of the registry root /// acronyms, such as HKLM, HKCU, HKCC, etc. /// </param> /// <param name="writeable"> /// Set to true to open the key as writeable. /// </param> /// <param name="hive"> /// The process architecture of the hive to use. /// </param> /// <returns> /// A registry key for the given path or null if /// the path does not exist. /// </returns> public static RegistryKey OpenKey( string path, bool writeable, ProcessArchitecture hive) { UIntPtr regRootKey; path = ProcessRegPath(path, out regRootKey); if (regRootKey == UIntPtr.Zero) { return null; } var flags = KeyRead; if (writeable) { flags |= KeyWrite; } if (hive == ProcessArchitecture.X64) { flags |= KeyWow6464Key; } else { flags |= KeyWow6432Key; } IntPtr handle; var hresult = RegOpenKeyEx(regRootKey, path, 0, flags, out handle); if (hresult != 0) { return null; } var sh = CreateSafeHandle(handle); var key = (RegistryKey) Activator.CreateInstance( RegistryKeyType, PrivateInstanceFlags, null, new object[] { sh, true }, null); return key; }
public static PythonEnvironment EnsureDeployed(DirectoryInfo target) { if (target is null) { throw new ArgumentNullException(nameof(target)); } if (PathEx.IsNested(root: target.FullName, item: AssemblyPath)) { throw new ArgumentException("Can not deploy over this assembly"); } if (ProcessArchitecture != Architecture.X64) { throw new PlatformNotSupportedException(ProcessArchitecture.ToString()); } string platform = IsOSPlatform(Windows) ? "win" : IsOSPlatform(Linux) ? "linux" : IsOSPlatform(OSX) ? "osx" : throw new PlatformNotSupportedException(); var runtimeLibrary = DependencyContext.Default.RuntimeLibraries .Single(lib => lib.Type == "package" && lib.Name == $"LostTech.TensorFlow.Python.runtime.{platform}-x64"); string?packagePath = PackageHelper.TryLocatePackage(runtimeLibrary.Path); if (packagePath is null) { throw new PlatformNotSupportedException($"Unable to find runtime package in {nameof(DependencyContext)}"); } string archivePath = runtimeLibrary.NativeLibraryGroups .SelectMany(group => group.AssetPaths) .Single(path => path.EndsWith("/TensorFlow.tar.xz", StringComparison.Ordinal)) .Replace('/', Path.DirectorySeparatorChar); archivePath = Path.Combine(packagePath, archivePath); if (!File.Exists(archivePath)) { throw new FileNotFoundException( message: "Packaged TensorFlow was not found. Perhaps the platform is not supported.", fileName: archivePath); } string relativeInterpreterPath = IsOSPlatform(Windows) ? "python.exe" : IsOSPlatform(Linux) || IsOSPlatform(OSX) ? Path.Combine("bin", "python") : throw new PlatformNotSupportedException(); string interpreterPath = Path.Combine(target.FullName, relativeInterpreterPath); // TODO: more robust detection if (!File.Exists(interpreterPath)) { Archive.Extract(archivePath, target.FullName); } // TODO: detect Python version var version = new Version(3, 7); string dllName = IsOSPlatform(Windows) ? Invariant($"python{version.Major}{version.Minor}.dll") : Path.Combine("lib", Invariant($"libpython{version.Major}.{version.Minor}m{DynamicLibraryExtension}")); target.Refresh(); return(new PythonEnvironment( // TODO: support non-Windows interpreterPath: new FileInfo(interpreterPath), home: target, // TODO: support non-Windows dll: new FileInfo(Path.Combine(target.FullName, dllName)), languageVersion: version, Architecture.X64)); }