Skip to content

Commit dfc30a6

Browse files
committed
Add build-id checks in test
1 parent bbe4162 commit dfc30a6

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/CreatedumpTests.cs

+40-17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
using Datadog.Trace.Telemetry;
1515
using Datadog.Trace.Telemetry.DTOs;
1616
using Datadog.Trace.TestHelpers;
17+
using Datadog.Trace.Util;
18+
using ELFSharp.ELF;
19+
using ELFSharp.ELF.Sections;
1720
using FluentAssertions;
1821
using FluentAssertions.Execution;
1922
using Newtonsoft.Json.Linq;
@@ -549,28 +552,27 @@ void ValidateStacktrace(JToken callstack)
549552
frame.Should().NotBeNull($"couldn't find expected frame {expectedFrame}");
550553
}
551554

552-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
555+
var validatedModules = new HashSet<string>();
556+
557+
foreach (var frame in frames)
553558
{
554-
var validatedModules = new HashSet<string>();
559+
var moduleName = frame["names"][0]["name"].Value<string>().Split('!').First();
555560

556-
// Validate PDBs
557-
foreach (var frame in frames)
561+
if (moduleName.Length > 0 && !moduleName.StartsWith("<") && Path.IsPathRooted(moduleName))
558562
{
559-
// Open the PE file
560-
var moduleName = frame["names"][0]["name"].Value<string>().Split('!').First();
561-
562-
if (moduleName.Length > 0 && !moduleName.StartsWith("<") && Path.IsPathRooted(moduleName))
563+
if (!validatedModules.Add(moduleName))
563564
{
564-
if (!validatedModules.Add(moduleName))
565-
{
566-
continue;
567-
}
565+
continue;
566+
}
568567

568+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
569+
{
570+
// Validate PDBs
569571
var pdbNode = frame["normalized_ip"]["meta"]["Pdb"];
570-
571572
var hash = ((JArray)pdbNode["guid"]).Select(g => g.Value<byte>()).ToArray();
572573
var age = pdbNode["age"].Value<uint>();
573574

575+
// Open the PE file
574576
using var file = File.OpenRead(moduleName);
575577
using var peReader = new PEReader(file);
576578

@@ -581,17 +583,38 @@ void ValidateStacktrace(JToken callstack)
581583
age.Should().Be(unchecked((uint)pdbInfo.Age));
582584
hash.Should().Equal(pdbInfo.Guid.ToByteArray());
583585
}
586+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
587+
{
588+
// Validate sofile
589+
if (frame["normalized_ip"] == null)
590+
{
591+
// On linux we can face cases where the build_id is not available:
592+
// - specifically on alpine, /lib/ld-musl-XX do not have a build_id.
593+
// - We are looking at a frame for which the library was unloaded /memfd:doublemapper (deleted)
594+
continue;
595+
}
596+
597+
var elfNode = frame["normalized_ip"]["meta"]["Elf"];
598+
var buildId = ((JArray)elfNode["build_id"]).Select(g => g.Value<byte>()).ToArray();
599+
600+
using var elf = ELFReader.Load(moduleName);
601+
var buildIdNote = elf.GetSection(".note.gnu.build-id") as INoteSection;
602+
buildId.Should().Equal(buildIdNote.Description);
603+
}
584604
}
605+
}
585606

586-
validatedModules.Should().NotBeEmpty();
607+
validatedModules.Should().NotBeEmpty();
587608

588609
#if NETFRAMEWORK
589-
var clrModuleName = "clr.dll";
610+
var clrModuleName = "clr.dll";
590611
#else
591-
var clrModuleName = "coreclr.dll";
612+
var clrModuleName = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "libcoreclr.so" : "coreclr.dll";
592613
#endif
593614

594-
validatedModules.Should().ContainMatch($@"*\{clrModuleName}");
615+
if (!Utils.IsAlpine())
616+
{
617+
validatedModules.Should().ContainMatch($@"*{Path.DirectorySeparatorChar}{clrModuleName}");
595618
}
596619
}
597620
}

tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/Datadog.Trace.Tools.dd_dotnet.ArtifactTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
</ItemGroup>
3131

3232
<ItemGroup>
33+
<PackageReference Include="ELFSharp" Version="2.17.3" />
3334
<PackageReference Include="Verify.Xunit" Version="14.13.1" />
3435
</ItemGroup>
3536

0 commit comments

Comments
 (0)