Skip to content

Commit

Permalink
[cdac] Add basic cdacreader project (#100623)
Browse files Browse the repository at this point in the history
This change adds a basic `cdacreader` project under src/native/managed.
- Built as part of the clr subset (via runtime-prereqs referencing compile-native.proj)
- Not yet integrated into anything in the product (that is, the dac doesn't try to use it yet)
- Can return a class that can implement the ISOSDacInterface* interfaces - currently does ISOSDacInterface9
  • Loading branch information
elinor-fung authored Apr 5, 2024
1 parent 4c20b21 commit b973027
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/native/managed/cdacreader/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(cdacreader_api INTERFACE)
target_include_directories(cdacreader_api INTERFACE ${CLR_SRC_NATIVE_DIR}/managed/cdacreader/inc)
20 changes: 20 additions & 0 deletions src/native/managed/cdacreader/inc/cdac_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#ifndef CDAC_READER_H
#define CDAC_READER_H

#ifdef __cplusplus
extern "C"
{
#endif

int cdac_reader_init(intptr_t descriptor, intptr_t* handle);
int cdac_reader_free(intptr_t handle);
int cdac_reader_get_sos_interface(intptr_t handle, IUnknown** obj);

#ifdef __cplusplus
}
#endif

#endif // CDAC_READER_H
50 changes: 50 additions & 0 deletions src/native/managed/cdacreader/src/Entrypoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace Microsoft.Diagnostics.DataContractReader;

internal static class Entrypoints
{
private const string CDAC = "cdac_reader_";

[UnmanagedCallersOnly(EntryPoint = $"{CDAC}init")]
private static unsafe int Init(nint descriptor, IntPtr* handle)
{
Target target = new(descriptor);
GCHandle gcHandle = GCHandle.Alloc(target);
*handle = GCHandle.ToIntPtr(gcHandle);
return 0;
}

[UnmanagedCallersOnly(EntryPoint = $"{CDAC}free")]
private static unsafe int Free(IntPtr handle)
{
GCHandle h = GCHandle.FromIntPtr(handle);
h.Free();
return 0;
}

/// <summary>
/// Get the SOS-DAC interface implementation.
/// </summary>
/// <param name="handle">Handle crated via cdac initialization</param>
/// <param name="obj"><c>IUnknown</c> pointer that can be queried for ISOSDacInterface*</param>
/// <returns></returns>
[UnmanagedCallersOnly(EntryPoint = $"{CDAC}get_sos_interface")]
private static unsafe int GetSOSInterface(IntPtr handle, nint* obj)
{
ComWrappers cw = new StrategyBasedComWrappers();
Target? target = GCHandle.FromIntPtr(handle).Target as Target;
if (target == null)
return -1;

SOSDacImpl impl = new(target);
nint ptr = cw.GetOrCreateComInterfaceForObject(impl, CreateComInterfaceFlags.None);
*obj = ptr;
return 0;
}
}
36 changes: 36 additions & 0 deletions src/native/managed/cdacreader/src/SOSDacImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace Microsoft.Diagnostics.DataContractReader;

[GeneratedComInterface]
[Guid("4eca42d8-7e7b-4c8a-a116-7bfbf6929267")]
internal partial interface ISOSDacInterface9
{
int GetBreakingChangeVersion();
}

/// <summary>
/// Implementation of ISOSDacInterface* interfaces intended to be passed out to consumers
/// interacting with the DAC via those COM interfaces.
/// </summary>
[GeneratedComClass]
internal sealed partial class SOSDacImpl : ISOSDacInterface9
{
private readonly Target _target;

public SOSDacImpl(Target target)
{
_target = target;
}

public int GetBreakingChangeVersion()
{
// TODO: Return non-hard-coded version
return 4;
}
}
11 changes: 11 additions & 0 deletions src/native/managed/cdacreader/src/Target.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Diagnostics.DataContractReader;

internal sealed class Target
{
public Target(nint _)
{
}
}
17 changes: 17 additions & 0 deletions src/native/managed/cdacreader/src/cdacreader.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- Do not produce a public package. This ships as part of the runtime -->
<IsShippingPackage>false</IsShippingPackage>
</PropertyGroup>

<ItemGroup>
<InstallRuntimeComponentDestination Include="." />
<!-- TODO: [cdac] Output to sharedFramework and add PlatformManifestFileEntry for Microsoft.NETCore.App once ready to include in shipping package -->
<!-- <InstallRuntimeComponentDestination Include="sharedFramework" Condition="'$(RuntimeFlavor)' == 'coreclr'"/> -->
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/native/managed/compile-native.proj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<!-- add new projects here -->
<!-- NativeLibsProjectsToBuild Include="$(MSBuildThisFileDirectory)libhellomanaged/src/libhellomanaged.csproj" -->
<NativeLibsProjectsToBuild Include="$(MSBuildThisFileDirectory)cdacreader/src/cdacreader.csproj" />
</ItemGroup>

<!-- Decide if we're going to do the NativeAOT builds -->
Expand Down

0 comments on commit b973027

Please sign in to comment.