Accessing stack pointer and heap pointer

18 Sep 2012

Hello,

I want to access the stack and heap pointers for debugging purposes, and write their values to a terminal:

unsigned int StackPointer = *[address of sp?];
unsigned int HeapPointer = *[address of hp?];
pc.printf("The stack pointer is at: %08x\n", StackPointer);
pc.printf("The heap pointer is at: %08xp\n", HeapPointer);

How can I access these pointers?

Thanks Tim

18 Sep 2012

Hi

Try

__current_sp() 

and

__current_pc()

To dump the heap structure:

void heapinfo()  {
    typedef int (*__heapprt)(void *, char const *, ...);
    __heapstats( (__heapprt)std::fprintf, stdout ) ;
}

int CaptureLine(void* pBuffer, char const* pFormatString, ...) {
    char*   pStringEnd = (char*)pBuffer + strlen((char*)pBuffer);

    va_list valist;
    va_start(valist, pFormatString);

    return vsprintf(pStringEnd, pFormatString, valist);
}

regards Wim van der Vegt

18 Sep 2012

Hi Wim,

Thanks for your help. I've also found your post on http://mbed.org/forum/mbed/topic/2698/?page=1#comment-13850 which suggests using _heapvalid. I've added this to my program along with the _current_sp() and I can now see the start address of the last free block on the heap creeping towards my stack pointer before my program crashes!

Thanks again Tim

18 Sep 2012

Hi Tim,

Beware it's the largest free block on the heap being to small that crashes your program (if it's to small), not the values of the heap/stackpointer themself.

regards Wim van der Vegt