public void PrintDebugString(OUTPUT_DEBUG_STRING_INFO outputDbgStrInfo) { using (SafeProcessHandle hProcess = Process.OpenProcess(ProcessAccessFlags.VmRead, false, pid)) { if (hProcess.IsInvalid) { throw new ArgumentException(String.Format("Unable to open process {0}, error {x:8}", pid, Marshal.GetLastWin32Error())); } var dbgString = new byte[outputDbgStrInfo.nDebugStringLength]; uint numberOfBytesRead; var result = Process.ReadProcessMemory(hProcess, outputDbgStrInfo.lpDebugStringData, dbgString, outputDbgStrInfo.nDebugStringLength, out numberOfBytesRead); if (result) { if (outputDbgStrInfo.fUnicode == 0) { Console.WriteLine("Debug String: {0}", Encoding.ASCII.GetString(dbgString)); } else { Console.WriteLine("Debug String: {0}", Encoding.Unicode.GetString(dbgString)); } } } }
private UInt32 GetProcessCommit() { PROCESS_MEMORY_COUNTERS_EX counters; var result = ProcessNativeMethod.GetProcessMemoryInfo(hProcess, out counters, (uint)Marshal.SizeOf <PROCESS_MEMORY_COUNTERS_EX>()); Debug.Assert(result); if (!result) { var lastWin32Error = Marshal.GetLastWin32Error(); throw new Exception($"Unable to query process memory info {pid}, error {lastWin32Error:x8}"); } return(counters.PrivateUsage.ToUInt32()); }
public void PrintDebugString(OUTPUT_DEBUG_STRING_INFO outputDbgStrInfo) { var dbgString = new byte[outputDbgStrInfo.nDebugStringLength]; uint numberOfBytesRead; var result = ProcessNativeMethod.ReadProcessMemory(hProcess, outputDbgStrInfo.lpDebugStringData, dbgString, outputDbgStrInfo.nDebugStringLength, out numberOfBytesRead); if (result) { if (outputDbgStrInfo.fUnicode == 0) { Console.WriteLine("Debug String: {0}", Encoding.ASCII.GetString(dbgString)); } else { Console.WriteLine("Debug String: {0}", Encoding.Unicode.GetString(dbgString)); } } }
public MiniDumper(string dumpFolder, int pid, string processName, TextWriter logger, DumpType dumpType, bool writeAsync, string filter, int numberOfDumps) { this.dumpFolder = dumpFolder; this.pid = pid; this.processName = processName; this.logger = logger; this.dumpType = dumpType; this.writeAsync = writeAsync; rgxFilter = new Regex((filter ?? "*").Replace("*", ".*").Replace('?', '.'), RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase); hProcess = ProcessNativeMethod.OpenProcess(ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VmRead, false, pid); if (hProcess.IsInvalid) { throw new ArgumentException(String.Format("Unable to open process {0}, error {1:x8}", pid, Marshal.GetLastWin32Error())); } this.numberOfDumps = numberOfDumps; }
void CreateProcess() { bool spawnNew = !string.IsNullOrEmpty(_options.DumpFolderForNewlyStartedProcess); _processName = null; int pid; if (int.TryParse(_options.ProcessInfo, out pid)) { _pid = pid; } else { // not numeric - let's try to find it by name var procs = Process.GetProcesses(); foreach (var proc in procs) { try { if (_options.ProcessInfo.Equals(proc.MainModule.ModuleName, StringComparison.OrdinalIgnoreCase)) { _pid = proc.Id; break; } } catch { // just ignore it } } } if (_pid > 0) { // process found - let's attach to it if (!DebuggingNativeMethods.DebugActiveProcess(_pid)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } _processName = GetProcessName(_pid); return; } if (spawnNew) { // final try - let's try creating it (but only if -x option is set) var commandLine = _options.ProcessInfo + " " + string.Join(" ", _options.Args ?? new string[0]); var startupInfo = new STARTUPINFO(); var processInformation = new PROCESS_INFORMATION(); var processCreationFlags = ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS; if (_options.StartProcessInNewConsoleWindow) { processCreationFlags |= ProcessCreationFlags.CREATE_NEW_CONSOLE; } bool res = ProcessNativeMethods.CreateProcess(null, new StringBuilder(commandLine), null, null, false, processCreationFlags, IntPtr.Zero, null, startupInfo, processInformation); if (!res) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } if (!DebuggingNativeMethods.DebugSetProcessKillOnExit(false)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } _pid = processInformation.dwProcessId; _processName = GetProcessName(_pid); return; } throw new ArgumentException("Something is wrong with the arguments - couldn't find or create a requested process."); }