Skip to content

Commit 4cb5cd5

Browse files
author
Max Charlamb
committed
implement RuntimeInfo contract
1 parent ef41b06 commit 4cb5cd5

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed

src/coreclr/debug/runtimeinfo/contracts.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"PlatformMetadata": 1,
2020
"PrecodeStubs": 2,
2121
"ReJIT": 1,
22+
"RuntimeInfo": 1,
2223
"RuntimeTypeSystem": 1,
2324
"StackWalk": 1,
2425
"StressLog": 2,

src/coreclr/debug/runtimeinfo/datadescriptor.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,31 @@ CDAC_TYPE_END(CalleeSavedRegisters)
757757
CDAC_TYPES_END()
758758

759759
CDAC_GLOBALS_BEGIN()
760+
761+
// RuntimeInfo OperatingSystem
762+
#ifdef TARGET_UNIX
763+
CDAC_GLOBAL(OperatingSystem, uint32, 2) // RuntimeInfoOperatingSystem.Unix
764+
#else // !TARGET_UNIX
765+
CDAC_GLOBAL(OperatingSystem, uint32, 1) // RuntimeInfoOperatingSystem.Windows
766+
#endif // !TARGET_UNIX
767+
768+
// RuntimeInfo Architecture
769+
#if defined(TARGET_X86)
770+
CDAC_GLOBAL(Architecture, uint32, 1) // RuntimeInfoArchitecture.X86
771+
#elif defined(TARGET_AMD64)
772+
CDAC_GLOBAL(Architecture, uint32, 3) // RuntimeInfoArchitecture.Amd64
773+
#elif defined(TARGET_ARM)
774+
CDAC_GLOBAL(Architecture, uint32, 2) // RuntimeInfoArchitecture.Arm32
775+
#elif defined(TARGET_ARM64)
776+
CDAC_GLOBAL(Architecture, uint32, 4) // RuntimeInfoArchitecture.Arm64
777+
#elif defined(TARGET_LOONGARCH64)
778+
CDAC_GLOBAL(Architecture, uint32, 5) // RuntimeInfoArchitecture.LoongArch64
779+
#elif defined(TARGET_RISCV64)
780+
CDAC_GLOBAL(Architecture, uint32, 6) // RuntimeInfoArchitecture.RISCV
781+
#else
782+
#error Unknown Processor.
783+
#endif
784+
760785
CDAC_GLOBAL_POINTER(AppDomain, &AppDomain::m_pTheAppDomain)
761786
CDAC_GLOBAL_POINTER(ThreadStore, &ThreadStore::s_pThreadStore)
762787
CDAC_GLOBAL_POINTER(FinalizerThread, &::g_pFinalizerThread)

src/native/managed/cdacreader/Microsoft.Diagnostics.DataContractReader.Abstractions/ContractRegistry.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ public abstract class ContractRegistry
6363
/// Gets an instance of the StackWalk contract for the target.
6464
/// </summary>
6565
public abstract IStackWalk StackWalk { get; }
66+
/// <summary>
67+
/// Gets an instance of the RuntimeInfo contract for the target.
68+
/// </summary>
69+
public abstract IRuntimeInfo RuntimeInfo { get; }
6670
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
6+
namespace Microsoft.Diagnostics.DataContractReader.Contracts;
7+
8+
public enum RuntimeInfoArchitecture : uint
9+
{
10+
Unknown = 0,
11+
X86,
12+
Arm32,
13+
Amd64,
14+
Arm64,
15+
LoongArch64,
16+
RISCV,
17+
}
18+
19+
public enum RuntimeInfoOperatingSystem : uint
20+
{
21+
Unknown = 0,
22+
Windows,
23+
Unix,
24+
}
25+
26+
public interface IRuntimeInfo : IContract
27+
{
28+
static string IContract.Name { get; } = nameof(RuntimeInfo);
29+
public virtual RuntimeInfoArchitecture GetTargetArchitecture() => throw new NotImplementedException();
30+
public virtual RuntimeInfoOperatingSystem GetTargetOperatingSystem() => throw new NotImplementedException();
31+
}
32+
33+
public readonly struct RuntimeInfo : IRuntimeInfo
34+
{
35+
RuntimeInfoArchitecture IRuntimeInfo.GetTargetArchitecture() => RuntimeInfoArchitecture.Unknown;
36+
RuntimeInfoOperatingSystem IRuntimeInfo.GetTargetOperatingSystem() => RuntimeInfoOperatingSystem.Unknown;
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.Diagnostics.DataContractReader.Contracts;
5+
6+
public sealed class RuntimeInfoFactory : IContractFactory<IRuntimeInfo>
7+
{
8+
IRuntimeInfo IContractFactory<IRuntimeInfo>.CreateContract(Target target, int version)
9+
{
10+
return version switch
11+
{
12+
1 => new RuntimeInfo_1(target),
13+
_ => default(RuntimeInfo),
14+
};
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Microsoft.Diagnostics.DataContractReader.Data;
7+
8+
namespace Microsoft.Diagnostics.DataContractReader.Contracts;
9+
10+
internal struct RuntimeInfo_1 : IRuntimeInfo
11+
{
12+
private const string ArchitectureGlobalName = "Architecture";
13+
private const string OperatingSystemGlobalName = "OperatingSystem";
14+
15+
internal readonly Target _target;
16+
17+
public RuntimeInfo_1(Target target)
18+
{
19+
_target = target;
20+
}
21+
22+
RuntimeInfoArchitecture IRuntimeInfo.GetTargetArchitecture()
23+
{
24+
if (_target.TryReadGlobal<uint>(ArchitectureGlobalName, out uint? arch))
25+
{
26+
return (RuntimeInfoArchitecture)arch;
27+
}
28+
29+
return RuntimeInfoArchitecture.Unknown;
30+
}
31+
32+
RuntimeInfoOperatingSystem IRuntimeInfo.GetTargetOperatingSystem()
33+
{
34+
if (_target.TryReadGlobal<uint>(OperatingSystemGlobalName, out uint? os))
35+
{
36+
return (RuntimeInfoOperatingSystem)os;
37+
}
38+
39+
return RuntimeInfoOperatingSystem.Unknown;
40+
}
41+
}

src/native/managed/cdacreader/Microsoft.Diagnostics.DataContractReader/CachingContractRegistry.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public CachingContractRegistry(Target target, TryGetContractVersionDelegate tryG
3838
[typeof(IPrecodeStubs)] = new PrecodeStubsFactory(),
3939
[typeof(IReJIT)] = new ReJITFactory(),
4040
[typeof(IStackWalk)] = new StackWalkFactory(),
41+
[typeof(IRuntimeInfo)] = new RuntimeInfoFactory(),
4142
};
4243
configureFactories?.Invoke(_factories);
4344
}
@@ -55,6 +56,7 @@ public CachingContractRegistry(Target target, TryGetContractVersionDelegate tryG
5556
public override IPrecodeStubs PrecodeStubs => GetContract<IPrecodeStubs>();
5657
public override IReJIT ReJIT => GetContract<IReJIT>();
5758
public override IStackWalk StackWalk => GetContract<IStackWalk>();
59+
public override IRuntimeInfo RuntimeInfo => GetContract<IRuntimeInfo>();
5860

5961
private TContract GetContract<TContract>() where TContract : IContract
6062
{

0 commit comments

Comments
 (0)