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

Code before:
        return hr;
    }

    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)
    {
        if (buffer is null && pNeeded is null)
            return HResults.E_POINTER;

        int hr = HResults.S_OK;
        try
        {
            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);

Review comment (refactor):
@copilot, Move this inside the catch block and throw an exception with the specific HResult.

Code after:
        return hr;
    }

    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);