phryniszak / strtt

Segger RTT console using ST-link
GNU General Public License v3.0
43 stars 7 forks source link

Segmentation fault #6

Open dwjbosman opened 1 month ago

dwjbosman commented 1 month ago

I am testing strtt on a STM32U5, it finds the RTT info but then crashes

./strtt -v 4 -ramstart 0x2023af49

I added some debugging code, but I don't know how to fix this:

    strtt.cpp:272

    // 2. Enumerate buffers start and size
    for (size_t i = 0; i < buffersCnt; i++)
    {
        // check only valid channels, eg. with size > 0 AND and something to read
        SEGGER_RTT_BUFFER bufferDesc = this->_rtt_info.pRttDescription->buffDesc[i];
        if ((bufferDesc.SizeOfBuffer) && (bufferDesc.RdOff != bufferDesc.WrOff))
        {
            start = bufferDesc.pBuffer - ramStart;
            size = bufferDesc.SizeOfBuffer;
            LOG_DEBUG("readrtt: pushblock pb=%u, rs=%u start = %u, size = %u",bufferDesc.pBuffer, ramStart,start, size);
            blocks.push_back(std::make_pair(start, size));
        }
    }

Debug: 96 62 strtt.cpp:281 readRtt(): readrtt: pushblock pb=539208521, rs=539209545 start = 4294966272, size = 1024

Somehow bufferDesc.pBuffer < ramStart, which causes start to overflow, which in turn causes _memory[start] to fail.

    LOG_DEBUG("readrtt: read2 %u bytes start=%u memsz=%lu", size,start, this->_memory.size());
    // ret = stlink_usb_layout_api.read_mem(this->_handle, start + RAM_START, -1, size, &this->_memory[start]);
    ret = stlink_usb_layout_api.read_mem(this->_handle, start + ramStart, -1, ((size / 4) * 4) + 4, &this->_memory[start]);
dwjbosman commented 1 month ago

Ok, it seems that if the actual data transfer buffer is before the ramstart parameter, the app crashes.

So I started the app with a lower ramstart and larger ramsize. Now it runs.

It could be a solution to resize the memory cache if it finds bufferDesc.pBuffer < ramStart and then to adjust ramStart

phryniszak commented 1 month ago

Good finding. Can you send some code that let me replicate this and I try to fix it?

dwjbosman commented 1 month ago

Sorry, I can't share the code.

1. If you check SEGGER_RTT.c the _acDownBuffer is allocated, and also _SEGGER_RTT

You could move the allocations to ensure that _acUpBuffer is allocated to an address lower than _SEGGER_RTT 2. And then in the .map file find the exact address of _SEGGER_RTT

Start the rtt viewer with -ramstart setting of the exact _SEGGER_RTT address

This will trigger the fault