Skip to content
Merged
58 changes: 37 additions & 21 deletions tracer/test/Datadog.Trace.TestHelpers/MemoryDumpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,58 @@ public static async Task InitializeAsync(IProgress<string> progress)
_path = Path.Combine(unpackedDirectory, "procdump.exe");
}

public static void MonitorCrashes(int pid)
public static Task MonitorCrashes(int pid)
{
if (!EnvironmentTools.IsWindows() || !IsAvailable)
{
return;
return Task.CompletedTask;
}

if (_path == null)
{
return;
return Task.CompletedTask;
}

_ = Task.Run(() =>
{
var args = $"-ma -accepteula -e {pid} {Path.GetTempPath()}";
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

using var dumpToolProcess = Process.Start(new ProcessStartInfo(_path, args)
_ = Task.Factory.StartNew(
() =>
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
});
var args = $"-ma -accepteula -e {pid} {Path.GetTempPath()}";

using var helper = new ProcessHelper(dumpToolProcess);
using var dumpToolProcess = Process.Start(new ProcessStartInfo(_path, args)
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
});

helper.Drain();
void OnDataReceived(string output)
{
if (output == "Press Ctrl-C to end monitoring without terminating the process.")
{
tcs.TrySetResult(true);
}
}

if (helper.StandardOutput.Contains("Dump count reached") || !helper.StandardOutput.Contains("Dump count not reached"))
{
_output.Report($"procdump for process {pid} exited with code {helper.Process.ExitCode}");
using var helper = new ProcessHelper(dumpToolProcess, OnDataReceived);

_output.Report($"[dump][stdout] {helper.StandardOutput}");
_output.Report($"[dump][stderr] {helper.ErrorOutput}");
}
});
helper.Drain();

if (helper.StandardOutput.Contains("Dump count reached") || !helper.StandardOutput.Contains("Dump count not reached"))
{
_output.Report($"procdump for process {pid} exited with code {helper.Process.ExitCode}");

_output.Report($"[dump][stdout] {helper.StandardOutput}");
_output.Report($"[dump][stderr] {helper.ErrorOutput}");
}

tcs.TrySetCanceled();
},
TaskCreationOptions.LongRunning);

return tcs.Task;
}

public static bool CaptureMemoryDump(Process process, IProgress<string> output = null)
Expand Down
Loading