Skip to content

Commit

Permalink
Fix memory dumps on artifact tests (#4906)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingosse authored Dec 1, 2023
1 parent b38c6b7 commit 924dc94
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 16 deletions.
16 changes: 16 additions & 0 deletions .azure-pipelines/ultimate-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3396,6 +3396,12 @@ stages:
DD_LOGGER_DD_API_KEY: $(ddApiKey)
enable_crash_dumps: true

- publish: tracer/build_data
displayName: Uploading tool_artifacts_tests_windows logs
artifact: _$(System.StageName)_$(Agent.JobName)_logs_$(System.JobAttempt)
condition: succeededOrFailed()
continueOnError: true

- task: PublishTestResults@2
displayName: publish test results
inputs:
Expand Down Expand Up @@ -3500,6 +3506,16 @@ stages:
baseImage: $(baseImage)
command: "BuildAndRunDdDotnetArtifactTests --framework net7.0"

- script: |
sudo chmod -R 644 tracer/build_data/dumps/* || true
displayName: Make dumps uploadable to AzDo
condition: succeededOrFailed()
- publish: tracer/build_data
artifact: _$(System.StageName)_$(Agent.JobName)_logs_$(System.JobAttempt)
condition: succeededOrFailed()
continueOnError: true

- task: PublishTestResults@2
displayName: publish test results
inputs:
Expand Down
14 changes: 12 additions & 2 deletions tracer/build/_build/Build.Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2706,17 +2706,27 @@ private void CopyDumpsToBuildData()

private void CopyDumpsTo(AbsolutePath root)
{
var dumpFolder = root / "dumps";

if (Directory.Exists(TempDirectory))
{
foreach (var dump in GlobFiles(TempDirectory, "coredump*", "*.dmp"))
{
MoveFileToDirectory(dump, root / "dumps", FileExistsPolicy.Overwrite);
Logger.Information("Moving file '{Dump}' to '{Root}'", dump, dumpFolder);

MoveFileToDirectory(dump, dumpFolder, FileExistsPolicy.Overwrite);
}
}

foreach (var file in Directory.EnumerateFiles(TracerDirectory, "*.dmp", SearchOption.AllDirectories))
{
CopyFileToDirectory(file, root, FileExistsPolicy.OverwriteIfNewer);
if (Path.GetDirectoryName(file) == dumpFolder)
{
// The dump is already in the right location
continue;
}

CopyFileToDirectory(file, dumpFolder, FileExistsPolicy.OverwriteIfNewer);
}
}

Expand Down
14 changes: 14 additions & 0 deletions tracer/test/Datadog.Trace.TestHelpers/CustomTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public CustomTestFramework(IMessageSink messageSink)
: base(messageSink)
{
FluentAssertions.Formatting.Formatter.AddFormatter(new DiffPaneModelFormatter());

if (bool.Parse(Environment.GetEnvironmentVariable("enable_crash_dumps") ?? "false"))
{
var progress = new Progress<string>(message => messageSink.OnMessage(new DiagnosticMessage(message)));

try
{
MemoryDumpHelper.InitializeAsync(progress).GetAwaiter().GetResult();
}
catch (Exception ex)
{
messageSink.OnMessage(new DiagnosticMessage($"MemoryDumpHelper initialization failed: {ex}"));
}
}
}

public CustomTestFramework(IMessageSink messageSink, Type typeTestedAssembly)
Expand Down
18 changes: 9 additions & 9 deletions tracer/test/Datadog.Trace.TestHelpers/MemoryDumpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static void MonitorCrashes(int pid)
});
}

public static bool CaptureMemoryDump(Process process)
public static bool CaptureMemoryDump(Process process, IProgress<string> output = null)
{
if (!IsAvailable)
{
Expand All @@ -96,8 +96,8 @@ public static bool CaptureMemoryDump(Process process)

try
{
var args = EnvironmentTools.IsWindows() ? $"-ma {process.Id} -accepteula" : process.Id.ToString();
return CaptureMemoryDump(args);
var args = EnvironmentTools.IsWindows() ? $"-ma -accepteula {process.Id} {Path.GetTempPath()}" : process.Id.ToString();
return CaptureMemoryDump(args, output ?? _output);
}
catch (Exception ex)
{
Expand All @@ -106,9 +106,9 @@ public static bool CaptureMemoryDump(Process process)
}
}

private static bool CaptureMemoryDump(string args)
private static bool CaptureMemoryDump(string args, IProgress<string> output)
{
_output?.Report($"Capturing memory dump using '{_path} {args}'");
output?.Report($"Capturing memory dump using '{_path} {args}'");

using var dumpToolProcess = Process.Start(new ProcessStartInfo(_path, args)
{
Expand All @@ -121,16 +121,16 @@ private static bool CaptureMemoryDump(string args)
using var helper = new ProcessHelper(dumpToolProcess);
dumpToolProcess.WaitForExit(30_000);
helper.Drain();
_output?.Report($"[dump][stdout] {helper.StandardOutput}");
_output?.Report($"[dump][stderr] {helper.ErrorOutput}");
output?.Report($"[dump][stdout] {helper.StandardOutput}");
output?.Report($"[dump][stderr] {helper.ErrorOutput}");

if (dumpToolProcess.ExitCode == 0)
{
_output?.Report($"Memory dump successfully captured using '{_path} {args}'.");
output?.Report($"Memory dump successfully captured using '{_path} {args}'.");
}
else
{
_output?.Report($"Failed to capture memory dump using '{_path} {args}'. Exit code was {dumpToolProcess.ExitCode}.");
output?.Report($"Failed to capture memory dump using '{_path} {args}'. Exit code was {dumpToolProcess.ExitCode}.");
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ protected async Task<ProcessHelper> StartConsole(EnvironmentHelper environmentHe
return helper;
}

helper.Dispose();

if (completed == helper.Task)
{
helper.Dispose();
throw new Exception("The target process unexpectedly exited");
}

// Try to capture a memory dump before giving up
if (MemoryDumpHelper.CaptureMemoryDump(helper.Process))
if (MemoryDumpHelper.CaptureMemoryDump(helper.Process, new Progress<string>(Output.WriteLine)))
{
Output.WriteLine("Successfully captured a memory dump");
}
Expand All @@ -104,6 +103,8 @@ protected async Task<ProcessHelper> StartConsole(EnvironmentHelper environmentHe
Output.WriteLine("Failed to capture a memory dump");
}

helper.Dispose();

throw new TimeoutException("Timeout when waiting for the target process to start");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ProcessBasicChecksTests(ITestOutputHelper output)
public async Task DetectRuntime()
{
SkipOn.Platform(SkipOn.PlatformValue.MacOs);
using var helper = await StartConsole(enableProfiler: false);
using var helper = await StartConsole(enableProfiler: false, ("crash", "1"));

var (standardOutput, errorOutput, _) = await RunTool($"check process {helper.Process.Id}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Xunit;
using Xunit.Abstractions;

[assembly: TestFramework("Datadog.Trace.Tools.Runner.ArtifactTests.CustomTestFramework", "Datadog.Trace.Tools.Runner.ArtifactTests")]
[assembly: TestFramework("Datadog.Trace.Tools.dd_dotnet.ArtifactTests.CustomTestFramework", "Datadog.Trace.Tools.dd_dotnet.ArtifactTests")]

namespace Datadog.Trace.Tools.dd_dotnet.ArtifactTests;

Expand Down

0 comments on commit 924dc94

Please sign in to comment.