Repository: dotnet/runtime
File: src/coreclr/debug/daccess/enummem.cpp (C++)

Code before:
    for (int i = 0; i < cSubDescriptors; i++)
    {
        int subDescriptorIndex = (pContractDescriptor->pointer_data_count - 1) - i;

        TADDR pSubDescriptorAddr;
        ULONG32 bytesRead;
        if (FAILED(m_pTarget->ReadVirtual(
            (TADDR)pContractDescriptor->pointer_data + subDescriptorIndex * sizeof(void*),
            (PBYTE)&pSubDescriptorAddr, sizeof(TADDR), &bytesRead))
            || bytesRead != sizeof(TADDR)
            || pSubDescriptorAddr == 0)
        {
            continue;
        }

        TADDR subDescriptorAddr;
        if (FAILED(m_pTarget->ReadVirtual(
            pSubDescriptorAddr,
            (PBYTE)&subDescriptorAddr, sizeof(TADDR), &bytesRead))
            || bytesRead != sizeof(TADDR)
            || subDescriptorAddr == 0)
        {
            continue;
        }

        PTR_ContractDescriptor pSubDescriptor = dac_cast<PTR_ContractDescriptor>(subDescriptorAddr);
        // Enumerate the ContractDescriptor structure
        ReportMem(dac_cast<TADDR>(pSubDescriptor), sizeof(ContractDescriptor));
        // Report the pointer data array
        ReportMem((TADDR)pSubDescriptor->pointer_data, pSubDescriptor->pointer_data_count * sizeof(void*));
        // Report the JSON blob
        ReportMem((TADDR)pSubDescriptor->descriptor, pSubDescriptor->descriptor_size);
    }

    return status;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Generating skinny mini-dump. Skinny mini-dump will only support stack trace, module list,
// and Exception list viewing.
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemoryRegionsWorkerSkinny(IN CLRDataEnumMemoryFlags flags)
{
    SUPPORTS_DAC;

    HRESULT status = S_OK;

    // clear all of the previous cached memory
    Flush();

Review comment (refactor):
This and the block above are the same. Do you think it makes sense to have an "EnumContract" helper? I'm ok if not given the small scope of the func if we don't see another use case happening. 

Code after:
            || bytesRead != sizeof(TADDR)
            || pSubDescriptorAddr == 0)
        {
            continue;
        }

        TADDR subDescriptorAddr;
        if (FAILED(m_pTarget->ReadVirtual(
            pSubDescriptorAddr,
            (PBYTE)&subDescriptorAddr, sizeof(TADDR), &bytesRead))
            || bytesRead != sizeof(TADDR)
            || subDescriptorAddr == 0)
        {
            continue;
        }

        // Save the subdescriptor
        EnumDataDescriptorHelper(subDescriptorAddr);
    }

    return S_OK;
}

void ClrDataAccess::EnumDataDescriptorHelper(TADDR dataDescriptorAddr)
{
    PTR_ContractDescriptor pDataDescriptor = dac_cast<PTR_ContractDescriptor>(dataDescriptorAddr);

    EX_TRY
    {
        // Enumerate the ContractDescriptor structure
        ReportMem(dac_cast<TADDR>(pDataDescriptor), sizeof(ContractDescriptor));
        // Report the pointer data array
        ReportMem((TADDR)pDataDescriptor->pointer_data, pDataDescriptor->pointer_data_count * sizeof(void*));
        // Report the JSON blob
        ReportMem((TADDR)pDataDescriptor->descriptor, pDataDescriptor->descriptor_size);
    }
    EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Generating skinny mini-dump. Skinny mini-dump will only support stack trace, module list,
// and Exception list viewing.
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemoryRegionsWorkerSkinny(IN CLRDataEnumMemoryFlags flags)
{
    SUPPORTS_DAC;

    HRESULT status = S_OK;