Repository: dotnet/runtime
File: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/SOSDacImpl.cs (C#)

Code before:
            data->RemoteStackTraceString = exceptionData.RemoteStackTraceString.ToClrDataAddress(_target);
            data->HResult = exceptionData.HResult;
            data->XCode = exceptionData.XCode;
        }
        catch (System.Exception ex)
        {
            return ex.HResult;
        }

        return HResults.S_OK;
    }

    int ISOSDacInterface2.IsRCWDCOMProxy(ClrDataAddress rcwAddress, int* inDCOMProxy)
    {
        int hr = HResults.S_OK;
        try
        {
            if (inDCOMProxy == null)
                throw new NullReferenceException(); // HResults.E_POINTER;

            *inDCOMProxy = (int)Interop.BOOL.FALSE;

            if (_target.ReadGlobal<byte>(Constants.Globals.FeatureCOMInterop) == 0)
            {
                hr = HResults.E_NOTIMPL;
            }
        }
        catch (System.Exception ex)
        {
            hr = ex.HResult;
        }

#if DEBUG
        if (_legacyImpl2 is not null)
        {
            int inDCOMProxyLocal;
            int hrLocal = _legacyImpl2.IsRCWDCOMProxy(rcwAddress, &inDCOMProxyLocal);
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
            if (hr == HResults.S_OK)
            {
                Debug.Assert(*inDCOMProxy == inDCOMProxyLocal);
            }
        }
#endif
        return hr;
    }
    #endregion ISOSDacInterface2

    #region ISOSDacInterface3
    int ISOSDacInterface3.GetGCInterestingInfoData(ClrDataAddress interestingInfoAddr, DacpGCInterestingInfoData* data)
    {

Review comment (suggestion):
nit: Add comment that `ulong` should be `ClrDataAddress`, but these parameters are not automatically marshalled so we defer to `ulong`.

Code after:

            int hrLocal = _legacyImpl.TraverseModuleMap(mmt, moduleAddr, callbackDebugPtr, tokenDebug);
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
            Debug.Assert(expectedElements[default] == elements.Count(), $"cDAC: {elements.Count()} elements, DAC: {expectedElements[default]} elements");
            GCHandle.FromIntPtr((nint)tokenDebug).Free();
        }
#endif
        return hr;
    }
#if DEBUG
    [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
    private static Interop.BOOL TraverseRCWCleanupListCallback(ulong rcwAddr, ulong ctx, ulong staThread, Interop.BOOL isFreeThreaded, void* expectedElements)
    {
        var expectedElementsDict = (Dictionary<ulong, ulong>)GCHandle.FromIntPtr((nint)expectedElements).Target!;
        if (expectedElementsDict.TryGetValue(rcwAddr, out ulong expectedCtx) && expectedCtx == ctx)
        {
            expectedElementsDict[default]++; // Increment the count for verification
        }
        else
        {
            Debug.Fail($"Unexpected RCW address {rcwAddr:x} or context {ctx:x}");
        }
        return Interop.BOOL.TRUE;
    }
#endif
    int ISOSDacInterface.TraverseRCWCleanupList(ClrDataAddress cleanupListPtr, delegate* unmanaged[Stdcall]<ulong, ulong, ulong, Interop.BOOL, void*, Interop.BOOL> pCallback, void* token)
    {
        int hr = HResults.S_OK;
        IEnumerable<Contracts.RCWCleanupInfo> cleanupInfos = Enumerable.Empty<Contracts.RCWCleanupInfo>();
        try
        {
            if (pCallback is null)
                throw new ArgumentException();

            Contracts.IBuiltInCOM contract = _target.Contracts.BuiltInCOM;
            TargetPointer listPtr = cleanupListPtr.ToTargetPointer(_target);

            cleanupInfos = contract.GetRCWCleanupList(listPtr);
            foreach (Contracts.RCWCleanupInfo info in cleanupInfos)
            {
                pCallback(
                    info.RCW.ToClrDataAddress(_target).Value,
                    info.Context.ToClrDataAddress(_target).Value,
                    info.STAThread.ToClrDataAddress(_target).Value,
                    info.IsFreeThreaded ? Interop.BOOL.TRUE : Interop.BOOL.FALSE,
                    token);
            }
        }
        catch (System.Exception ex)
        {
            hr = ex.HResult;