Skip to content

Commit d3b51d0

Browse files
author
Max Charlamb
committed
convert to use IRuntimeInfo to fetch platform
1 parent 4cb5cd5 commit d3b51d0

File tree

7 files changed

+26
-106
lines changed

7 files changed

+26
-106
lines changed

src/coreclr/debug/daccess/cdac.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ namespace
5252

5353
return S_OK;
5454
}
55-
56-
int GetPlatform(uint32_t* platform, void* context)
57-
{
58-
ICorDebugDataTarget* target = reinterpret_cast<ICorDebugDataTarget*>(context);
59-
HRESULT hr = target->GetPlatform((CorDebugPlatform*)platform);
60-
if (FAILED(hr))
61-
return hr;
62-
63-
return S_OK;
64-
}
6555
}
6656

6757
CDAC CDAC::Create(uint64_t descriptorAddr, ICorDebugDataTarget* target, IUnknown* legacyImpl)
@@ -74,7 +64,7 @@ CDAC CDAC::Create(uint64_t descriptorAddr, ICorDebugDataTarget* target, IUnknown
7464
_ASSERTE(init != nullptr);
7565

7666
intptr_t handle;
77-
if (init(descriptorAddr, &ReadFromTargetCallback, &ReadThreadContext, &GetPlatform, target, &handle) != 0)
67+
if (init(descriptorAddr, &ReadFromTargetCallback, &ReadThreadContext, target, &handle) != 0)
7868
{
7969
::FreeLibrary(cdacLib);
8070
return {};

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@ namespace Microsoft.Diagnostics.DataContractReader;
1818
/// </remarks>
1919
public abstract class Target
2020
{
21-
/// <summary>
22-
/// CorDebugPlatform represents the platform of the target.
23-
/// </summary>
24-
public enum CorDebugPlatform : int
25-
{
26-
CORDB_PLATFORM_WINDOWS_X86 = 0,
27-
CORDB_PLATFORM_WINDOWS_AMD64 = 1,
28-
CORDB_PLATFORM_WINDOWS_IA64 = 2,
29-
CORDB_PLATFORM_MAC_PPC = 3,
30-
CORDB_PLATFORM_MAC_X86 = 4,
31-
CORDB_PLATFORM_WINDOWS_ARM = 5,
32-
CORDB_PLATFORM_MAC_AMD64 = 6,
33-
CORDB_PLATFORM_WINDOWS_ARM64 = 7,
34-
CORDB_PLATFORM_POSIX_AMD64 = 8,
35-
CORDB_PLATFORM_POSIX_X86 = 9,
36-
CORDB_PLATFORM_POSIX_ARM = 10,
37-
CORDB_PLATFORM_POSIX_ARM64 = 11,
38-
CORDB_PLATFORM_POSIX_LOONGARCH64 = 12,
39-
CORDB_PLATFORM_POSIX_RISCV64 = 13,
40-
}
41-
4221
/// <summary>
4322
/// Pointer size of the target
4423
/// </summary>
@@ -48,11 +27,6 @@ public enum CorDebugPlatform : int
4827
/// </summary>
4928
public abstract bool IsLittleEndian { get; }
5029

51-
/// <summary>
52-
/// Platform of the target
53-
/// </summary>
54-
public abstract CorDebugPlatform Platform { get; }
55-
5630
/// <summary>
5731
/// Fills a buffer with the context of the given thread
5832
/// </summary>

src/native/managed/cdacreader/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/Context/IPlatformAgnosticContext.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@ public interface IPlatformAgnosticContext
2525

2626
public static IPlatformAgnosticContext GetContextForPlatform(Target target)
2727
{
28-
switch (target.Platform)
28+
IRuntimeInfo runtimeInfo = target.Contracts.RuntimeInfo;
29+
return runtimeInfo.GetTargetArchitecture() switch
2930
{
30-
case Target.CorDebugPlatform.CORDB_PLATFORM_WINDOWS_AMD64:
31-
case Target.CorDebugPlatform.CORDB_PLATFORM_POSIX_AMD64:
32-
case Target.CorDebugPlatform.CORDB_PLATFORM_MAC_AMD64:
33-
return new ContextHolder<AMD64Context>();
34-
case Target.CorDebugPlatform.CORDB_PLATFORM_POSIX_ARM64:
35-
case Target.CorDebugPlatform.CORDB_PLATFORM_WINDOWS_ARM64:
36-
return new ContextHolder<ARM64Context>();
37-
default:
38-
throw new InvalidOperationException($"Unsupported platform {target.Platform}");
39-
}
31+
RuntimeInfoArchitecture.Amd64 => new ContextHolder<AMD64Context>(),
32+
RuntimeInfoArchitecture.Arm64 => new ContextHolder<ARM64Context>(),
33+
RuntimeInfoArchitecture.Unknown => throw new InvalidOperationException($"Processor architecture is required for creating a platform specific context and is not provided by the target"),
34+
_ => throw new InvalidOperationException($"Unsupported architecture {runtimeInfo.GetTargetArchitecture()}"),
35+
};
4036
}
4137
}

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,22 @@ private readonly struct Configuration
4444

4545
public delegate int ReadFromTargetDelegate(ulong address, Span<byte> bufferToFill);
4646
public delegate int GetTargetThreadContextDelegate(uint threadId, uint contextFlags, uint contextSize, Span<byte> bufferToFill);
47-
public delegate int GetTargetPlatformDelegate(out int platform);
4847

4948
/// <summary>
5049
/// Create a new target instance from a contract descriptor embedded in the target memory.
5150
/// </summary>
5251
/// <param name="contractDescriptor">The offset of the contract descriptor in the target memory</param>
5352
/// <param name="readFromTarget">A callback to read memory blocks at a given address from the target</param>
5453
/// <param name="getThreadContext">A callback to fetch a thread's context</param>
55-
/// <param name="getTargetPlatform">A callback to fetch the target's platform</param>
5654
/// <param name="target">The target object.</param>
5755
/// <returns>If a target instance could be created, <c>true</c>; otherwise, <c>false</c>.</returns>
5856
public static bool TryCreate(
5957
ulong contractDescriptor,
6058
ReadFromTargetDelegate readFromTarget,
6159
GetTargetThreadContextDelegate getThreadContext,
62-
GetTargetPlatformDelegate getTargetPlatform,
6360
[NotNullWhen(true)] out ContractDescriptorTarget? target)
6461
{
65-
Reader reader = new Reader(readFromTarget, getThreadContext, getTargetPlatform);
62+
Reader reader = new Reader(readFromTarget, getThreadContext);
6663
if (TryReadContractDescriptor(
6764
contractDescriptor,
6865
reader,
@@ -85,7 +82,6 @@ public static bool TryCreate(
8582
/// <param name="globalPointerValues">The values for any global pointers specified in the contract descriptor.</param>
8683
/// <param name="readFromTarget">A callback to read memory blocks at a given address from the target</param>
8784
/// <param name="getThreadContext">A callback to fetch a thread's context</param>
88-
/// <param name="getTargetPlatform">A callback to fetch the target's platform</param>
8985
/// <param name="isLittleEndian">Whether the target is little-endian</param>
9086
/// <param name="pointerSize">The size of a pointer in bytes in the target process.</param>
9187
/// <returns>The target object.</returns>
@@ -94,15 +90,14 @@ public static ContractDescriptorTarget Create(
9490
TargetPointer[] globalPointerValues,
9591
ReadFromTargetDelegate readFromTarget,
9692
GetTargetThreadContextDelegate getThreadContext,
97-
GetTargetPlatformDelegate getTargetPlatform,
9893
bool isLittleEndian,
9994
int pointerSize)
10095
{
10196
return new ContractDescriptorTarget(
10297
new Configuration { IsLittleEndian = isLittleEndian, PointerSize = pointerSize },
10398
contractDescriptor,
10499
globalPointerValues,
105-
new Reader(readFromTarget, getThreadContext, getTargetPlatform));
100+
new Reader(readFromTarget, getThreadContext));
106101
}
107102

108103
private ContractDescriptorTarget(Configuration config, ContractDescriptorParser.ContractDescriptor descriptor, TargetPointer[] pointerData, Reader reader)
@@ -263,14 +258,6 @@ private static DataType GetDataType(string type)
263258

264259
public override int PointerSize => _config.PointerSize;
265260
public override bool IsLittleEndian => _config.IsLittleEndian;
266-
public override CorDebugPlatform Platform
267-
{
268-
get
269-
{
270-
_reader.GetTargetPlatform(out int platform);
271-
return (CorDebugPlatform)platform;
272-
}
273-
}
274261

275262
public override bool TryGetThreadContext(ulong threadId, uint contextFlags, Span<byte> buffer)
276263
{
@@ -610,8 +597,7 @@ public void Clear()
610597

611598
private readonly struct Reader(
612599
ReadFromTargetDelegate readFromTarget,
613-
GetTargetThreadContextDelegate getThreadContext,
614-
GetTargetPlatformDelegate getTargetPlatform)
600+
GetTargetThreadContextDelegate getThreadContext)
615601
{
616602
public int ReadFromTarget(ulong address, Span<byte> buffer)
617603
{
@@ -621,11 +607,6 @@ public int ReadFromTarget(ulong address, Span<byte> buffer)
621607
public int ReadFromTarget(ulong address, byte* buffer, uint bytesToRead)
622608
=> readFromTarget(address, new Span<byte>(buffer, checked((int)bytesToRead)));
623609

624-
public int GetTargetPlatform(out int platform)
625-
{
626-
return getTargetPlatform(out platform);
627-
}
628-
629610
public int GetThreadContext(uint threadId, uint contextFlags, uint contextSize, Span<byte> buffer)
630611
{
631612
return getThreadContext(threadId, contextFlags, contextSize, buffer);

src/native/managed/cdacreader/inc/cdac_reader.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ extern "C"
1313
// descriptor: the address of the descriptor in the target process
1414
// read_from_target: a callback that reads memory from the target process
1515
// read_thread_context: a callback that reads the context of a thread in the target process
16-
// get_platform: a callback that reads the platform of the target process
1716
// read_context: a context pointer that will be passed to callbacks
1817
// handle: returned opaque the handle to the reader. This should be passed to other functions in this API.
1918
int cdac_reader_init(
2019
uint64_t descriptor,
2120
int(*read_from_target)(uint64_t, uint8_t*, uint32_t, void*),
2221
int(*read_thread_context)(uint32_t, uint32_t, uint32_t, uint8_t*, void*),
23-
int(*get_platform)(uint32_t*, void*),
2422
void* read_context,
2523
/*out*/ intptr_t* handle);
2624

src/native/managed/cdacreader/src/Entrypoints.cs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ private static unsafe int Init(
1717
ulong descriptor,
1818
delegate* unmanaged<ulong, byte*, uint, void*, int> readFromTarget,
1919
delegate* unmanaged<uint, uint, uint, byte*, void*, int> readThreadContext,
20-
delegate* unmanaged<int*, void*, int> getPlatform,
2120
void* readContext,
2221
IntPtr* handle)
2322
{
@@ -38,13 +37,6 @@ private static unsafe int Init(
3837
return readThreadContext(threadId, contextFlags, contextSize, bufferPtr, readContext);
3938
}
4039
},
41-
(out int platform) =>
42-
{
43-
fixed (int* platformPtr = &platform)
44-
{
45-
return getPlatform(platformPtr, readContext);
46-
}
47-
},
4840
out ContractDescriptorTarget? target))
4941
return -1;
5042

@@ -95,6 +87,7 @@ private static unsafe int CLRDataCreateInstance(Guid* pIID, IntPtr /*ICLRDataTar
9587

9688
ComWrappers cw = new StrategyBasedComWrappers();
9789
object obj = cw.GetOrCreateObjectForComInstance(pLegacyTarget, CreateObjectFlags.None);
90+
9891
ICLRDataTarget dataTarget = obj as ICLRDataTarget ?? throw new ArgumentException($"pLegacyTarget does not implement ${nameof(ICLRDataTarget)}", nameof(pLegacyTarget));
9992
ICLRContractLocator contractLocator = obj as ICLRContractLocator ?? throw new ArgumentException($"pLegacyTarget does not implement ${nameof(ICLRContractLocator)}", nameof(pLegacyTarget));
10093

@@ -121,30 +114,6 @@ private static unsafe int CLRDataCreateInstance(Guid* pIID, IntPtr /*ICLRDataTar
121114
return dataTarget.GetThreadContext(threadId, contextFlags, contextSize, bufferPtr);
122115
}
123116
},
124-
(out platform) =>
125-
{
126-
platform = 0;
127-
uint machineType;
128-
int hr = dataTarget.GetMachineType(&machineType);
129-
switch (machineType)
130-
{
131-
// ICLRDataTarget can not be used to find OS. For now labeling all platforms as Windows
132-
//
133-
case 0x014c: // IMAGE_FILE_MACHINE_I386
134-
platform = (int)Target.CorDebugPlatform.CORDB_PLATFORM_WINDOWS_X86;
135-
break;
136-
case 0x8664: // IMAGE_FILE_MACHINE_AMD64
137-
platform = (int)Target.CorDebugPlatform.CORDB_PLATFORM_WINDOWS_AMD64;
138-
break;
139-
case 0x01c4: // IMAGE_FILE_MACHINE_ARMNT
140-
platform = (int)Target.CorDebugPlatform.CORDB_PLATFORM_WINDOWS_ARM;
141-
break;
142-
case 0xAA64: // IMAGE_FILE_MACHINE_ARM64
143-
platform = (int)Target.CorDebugPlatform.CORDB_PLATFORM_WINDOWS_ARM64;
144-
break;
145-
}
146-
return hr;
147-
},
148117
out ContractDescriptorTarget? target))
149118
{
150119
return -1;

src/native/managed/cdacreader/tests/TestPlaceholderTarget.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public TestPlaceholderTarget(MockTarget.Architecture arch, ReadFromTargetDelegat
2929
{
3030
IsLittleEndian = arch.IsLittleEndian;
3131
PointerSize = arch.Is64Bit ? 8 : 4;
32-
Platform = Target.CorDebugPlatform.CORDB_PLATFORM_MAC_AMD64;
3332
_contractRegistry = new Mock<ContractRegistry>().Object;
3433
_dataCache = new DefaultDataCache(this);
3534
_typeInfoCache = types ?? [];
@@ -44,7 +43,6 @@ internal void SetContracts(ContractRegistry contracts)
4443

4544
public override int PointerSize { get; }
4645
public override bool IsLittleEndian { get; }
47-
public override CorDebugPlatform Platform { get; }
4846

4947
public override bool IsAlignedToPointerSize(TargetPointer pointer)
5048
{
@@ -93,6 +91,20 @@ public override string ReadUtf16String(ulong address)
9391
}
9492

9593
public override TargetNUInt ReadNUInt(ulong address) => DefaultReadNUInt(address);
94+
95+
public override bool TryReadGlobal<T>(string name, [NotNullWhen(true)] out T? value)
96+
{
97+
value = default;
98+
foreach (var global in _globals)
99+
{
100+
if (global.Name == name)
101+
{
102+
value = T.CreateChecked(global.Value);
103+
return true;
104+
}
105+
}
106+
return false;
107+
}
96108
public override T ReadGlobal<T>(string name)
97109
{
98110
foreach (var global in _globals)

0 commit comments

Comments
 (0)