SimpleCharBuffer GdbSrvControllerImpl::PrintRegistersGroup()

in Exdi/exdigdbsrv/GdbSrvControllerLib/GdbSrvControllerLib.cpp [2596:2682]


    SimpleCharBuffer GdbSrvControllerImpl::PrintRegistersGroup(_In_ RegisterGroupType groupType, _In_ bool verbose = false)
    {
        //  Get the current system register values
        int alignValue;
        std::map<std::string, std::string> registers = QueryRegistersByGroup(GetLastKnownActiveCpu(),
            groupType, alignValue);

        size_t bufferCapacity = (registers.size() > 100) ? (2 * C_MAX_MONITOR_CMD_BUFFER) : 
            C_MAX_MONITOR_CMD_BUFFER;
        SimpleCharBuffer monitorResult;
        if (!monitorResult.TryEnsureCapacity(bufferCapacity))
        {
            throw _com_error(E_OUTOFMEMORY);
        }

        char regPrtBuffer[256] = { 0 };
        int index = 0;
        int operationResult  = sprintf_s(&regPrtBuffer[index], ARRAYSIZE(regPrtBuffer),
            "\nNumberOfRegisters: %zd\n", registers.size());

        if (verbose)
        {
            index += operationResult;
            operationResult = sprintf_s(&regPrtBuffer[index], ARRAYSIZE(regPrtBuffer) - index,
                "\n%*s | %*s | %*s\n", alignValue, g_headerRegisterVerbose[0], -16,
                g_headerRegisterVerbose[1], -6, g_headerRegisterVerbose[2]);
        }

        index += operationResult;
        int numberOfRegs = 0;
        for (const_regIterator it = RegistersBegin(groupType);
             it != RegistersEnd(groupType); ++it, numberOfRegs++)
        {
            if (verbose)
            {
                char buffer[128] = { 0 };
                AddressType accessCode = GetAccessCodeByRegisterNumber(it->nameOrder);
                sprintf_s(buffer, ARRAYSIZE(buffer), "0x%llx", accessCode);
                const char * pAccessCode = (accessCode == c_InvalidAddress) ?
                    "n/a" : buffer;
                operationResult = sprintf_s(&regPrtBuffer[index], ARRAYSIZE(regPrtBuffer) - index,
                    "%*s | %016I64x | %*s", alignValue, it->name.c_str(),
                    ParseRegisterValue(registers[it->name]),
                    -7, pAccessCode);
            }
            else
            {
                operationResult = sprintf_s(&regPrtBuffer[index], ARRAYSIZE(regPrtBuffer) - index,
                    "%*s = %016I64x ", alignValue, it->name.c_str(), 
                    ParseRegisterValue(registers[it->name]));
            }
            if (operationResult == -1)
            {
                throw _com_error(E_FAIL);
            }
            index += operationResult;
            bool isNeededPrint = (verbose) ? true : (((numberOfRegs + 1) % 3) == 0);
            if (isNeededPrint)
            {
                sprintf_s(&regPrtBuffer[index], ARRAYSIZE(regPrtBuffer)- index, "\n");
                size_t currentMsgLength = strlen(regPrtBuffer);
                if ((currentMsgLength + monitorResult.GetLength()) >= monitorResult.GetCapacity()) 
                {
                    if (!monitorResult.TryEnsureCapacity((monitorResult.GetLength() + 
                        ((2 * C_MAX_MONITOR_CMD_BUFFER) + 1))))
                    {
                        throw _com_error(E_OUTOFMEMORY);
                    } 
                }

                monitorResult.SetLength(monitorResult.GetLength() + currentMsgLength);
                memcpy(&monitorResult[monitorResult.GetLength() - currentMsgLength], 
                    regPrtBuffer, currentMsgLength);
                memset(regPrtBuffer, 0x00, sizeof(regPrtBuffer));
                index = 0;
            }
        }

        if (index != 0)
        {
            sprintf_s(&regPrtBuffer[index], ARRAYSIZE(regPrtBuffer) - index, "\n");
            monitorResult.SetLength(monitorResult.GetLength() + strlen(regPrtBuffer));
            memcpy(&monitorResult[monitorResult.GetLength() - strlen(regPrtBuffer)], regPrtBuffer, strlen(regPrtBuffer));
        }

        return monitorResult;
    }