ppc-eabi, get stack backtrace

support for the powerpc toolchain
Post Reply
DRS
Posts: 11
Joined: Fri Nov 06, 2009 1:50 pm

ppc-eabi, get stack backtrace

Post by DRS » Wed Oct 31, 2012 1:42 pm

Hi,

I was wondering, if an exception occurs the stack backtrace is shown, and code locations can be looked up with addr2line. According to GNU C++, the same can be achieved using the backtrace function that is defined in execinfo.h.

This file doesn't seem to be available in devkitPPC. My question is if devkitPPC has alternatives to request the backtrace. I figure that if the exception handler can do it, there must be code to do so in libc (or whatever other lib) and we might be able to use it.

My other question is if it is possible to fetch the contents of GPR01 (using asm or so), which, if I'm not mistaken, should contain the stackpointer. From there on I could browse the stackframes and check the method pointers to implement backtrace like behaviour myself.

Thanks,

Danny

DRS
Posts: 11
Joined: Fri Nov 06, 2009 1:50 pm

Re: ppc-eabi, get stack backtrace

Post by DRS » Sat Nov 03, 2012 1:51 am

I unit tested it a bit so it may not work every time, but I created something to start with:

Code: Select all

int backtrace (void **buffer, int size) {
	int depth;
	u32 stackptr, lr, *addr;
	// get link register
	asm volatile ("mflr %0" : "=r"(lr));
	// link register is assigned to depth[0]
	buffer[0] = (void *) (lr - 4);
	// get stackpointer
	asm volatile ("stw %%sp, 0(%0)" : : "b"((u32) &stackptr));
	// assign stack ptr to address
	addr = (u32 *) stackptr;
	// get the frames
	if (*addr) {
		// skip first two frames because this function
		// does create a stackframe but doesn't set lr on
		// the previous one.
		addr = (u32 *) *addr;
		if (*addr) {
			addr = (u32 *) *addr;
		}
	}
	for (depth=1; (depth<size && *addr); ++depth) {
		u32 * next = (u32 *) *addr;
		buffer[depth] = (void *) (*(addr+1) - 4);
		addr = next;
	}

	return depth;
}

Thanks to Dolphin's wonderful debug mode:)

Have to test it in my port in progress now.

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests