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

Code before:

    int ISOSDacInterface.GetStressLogAddress(ClrDataAddress* stressLog)
    {
        ulong stressLogAddress = _target.ReadGlobalPointer(Constants.Globals.StressLog);

#if DEBUG
        if (_legacyImpl is not null)
        {
            ClrDataAddress legacyStressLog;
            Debug.Assert(HResults.S_OK == _legacyImpl.GetStressLogAddress(&legacyStressLog));
            Debug.Assert(legacyStressLog == stressLogAddress);
        }
#endif
        *stressLog = stressLogAddress;
        return HResults.S_OK;
    }

    int ISOSDacInterface.GetSyncBlockCleanupData(ClrDataAddress addr, void* data)
        => _legacyImpl is not null ? _legacyImpl.GetSyncBlockCleanupData(addr, data) : HResults.E_NOTIMPL;
    int ISOSDacInterface.GetSyncBlockData(uint number, void* data)
        => _legacyImpl is not null ? _legacyImpl.GetSyncBlockData(number, data) : HResults.E_NOTIMPL;

    int ISOSDacInterface.GetThreadAllocData(ClrDataAddress thread, DacpAllocData* data)
    {
        if (thread == 0)
            return HResults.E_INVALIDARG;

        int hr = HResults.S_OK;
        try
        {
            if (data is null)
                throw new ArgumentException();

            Contracts.IThread contract = _target.Contracts.Thread;
            Contracts.ThreadAllocData allocData = contract.GetThreadAllocData(thread.ToTargetPointer(_target));
            data->allocBytes = (ClrDataAddress)(ulong)allocData.AllocBytes;
            data->allocBytesLoh = (ClrDataAddress)(ulong)allocData.AllocBytesLoh;
        }
        catch (global::System.Exception ex)
        {
            hr = ex.HResult;
        }

#if DEBUG
        if (_legacyImpl is not null)
        {
            DacpAllocData dataLocal = default;
            int hrLocal = _legacyImpl.GetThreadAllocData(thread, &dataLocal);
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
            if (hr == HResults.S_OK)
            {

Review comment (suggestion):
Convert this to a throw ArgumentException inside the try/catch block.

Code after:
    }

    int ISOSDacInterface.GetPrivateBinPaths(ClrDataAddress appDomain, int count, char* paths, uint* pNeeded)
    {
        // Method is not supported on CoreCLR
        int hr = HResults.E_NOTIMPL;

#if DEBUG
        if (_legacyImpl is not null)
        {
            int hrLocal = _legacyImpl.GetPrivateBinPaths(appDomain, count, paths, pNeeded);
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
        }
#endif

        return hr;
    }
    int ISOSDacInterface.GetRCWData(ClrDataAddress addr, void* data)
        => _legacyImpl is not null ? _legacyImpl.GetRCWData(addr, data) : HResults.E_NOTIMPL;
    int ISOSDacInterface.GetRCWInterfaces(ClrDataAddress rcw, uint count, void* interfaces, uint* pNeeded)
        => _legacyImpl is not null ? _legacyImpl.GetRCWInterfaces(rcw, count, interfaces, pNeeded) : HResults.E_NOTIMPL;
    int ISOSDacInterface.GetRegisterName(int regName, uint count, char* buffer, uint* pNeeded)
    {
        int hr = HResults.S_OK;
        try
        {
            if (buffer is null && pNeeded is null)
                throw new NullReferenceException();

            string[] regs = _target.Contracts.RuntimeInfo.GetTargetArchitecture() switch
            {
                RuntimeInfoArchitecture.X64 => s_amd64Registers,
                RuntimeInfoArchitecture.Arm => s_armRegisters,
                RuntimeInfoArchitecture.Arm64 => s_arm64Registers,
                RuntimeInfoArchitecture.X86 => s_x86Registers,
                RuntimeInfoArchitecture.LoongArch64 => s_loongArch64Registers,
                RuntimeInfoArchitecture.RiscV64 => s_riscV64Registers,
                _ => throw new InvalidOperationException(),
            };

            // Caller frame registers are encoded as "-(reg+1)".
            bool callerFrame = regName < 0;
            int regIndex = callerFrame ? -regName - 1 : regName;

            if ((uint)regIndex >= (uint)regs.Length)
                return unchecked((int)0x8000FFFF); // E_UNEXPECTED

            string name = callerFrame ? $"caller.{regs[regIndex]}" : regs[regIndex];

            uint needed = (uint)(name.Length + 1);
            if (pNeeded is not null)