public TracerContext LaunchTracerSuspended(string targetAssembly) { if (!File.Exists(targetAssembly)) { throw new FileNotFoundException(); } var hostCommServer = new HostCommServer(); hostCommServer.Initialize(); var procInfo = new Win32.ProcessInfo(); try { Win32.StartupInfo startupInfo; PrepareEnvironment(targetAssembly, hostCommServer.HostPipeName, out procInfo, out startupInfo); } catch (Exception e) { } TracedProcessInfo = procInfo; return(new TracerContext { Bootstrapper = this, CommServer = hostCommServer }); }
private void PrepareEnvironment(string targetFile, string pipeName, out Win32.ProcessInfo procInfo, out Win32.StartupInfo startupInfo) { var tracerPath = GetTracerEnginePath(); if (tracerPath == null) { throw new Exception("Tracer engine could not be found."); } var envBlock = new StringBuilder(); envBlock.Append("COR_ENABLE_PROFILING=1\0"); envBlock.Append($"COR_PROFILER={ProfilerClsId}\0"); envBlock.Append($"COR_PROFILER_PATH={tracerPath}\0"); // We need to tell the tracer what our pipename is envBlock.Append($"NETTrace_pipe_name=\\\\.\\pipe\\{pipeName}\0"); // We also need to tell the tracer where to find the settings file envBlock.Append( // ReSharper disable once AssignNullToNotNullAttribute $"NETTrace_settings_file=\"{Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "settings.json")}\"\0"); var creationFlag = Win32.CreateProcess(targetFile, null, IntPtr.Zero, IntPtr.Zero, true, 0x00000004, envBlock, null, out startupInfo, out procInfo); if (!creationFlag) { throw new Exception("Could not create process"); } }