Forum Discussion
Altera_Forum
Honored Contributor
13 years ago --- Quote Start --- No, I never got that working, but tklauser's kernel fix did fix debugging using a core dump. --- Quote End --- And I fixed the backtrace() function... or bypass it and wrote my own! stackframe.c
# define _GNU_SOURCE
# include <stdio.h># include <inttypes.h># include <stdint.h># include <ucontext.h># include <dlfcn.h># include <string.h>
struct frame {
void *fp; // frame pointer
void *ra; // return address
};
void print_symbol(void *address) {
Dl_info info;
char library;
char function;
void *symbol_address;
if (dladdr(address, &info) == 0) {
strncpy(function, "???", sizeof(function));
strncpy(library, "???", sizeof(library));
symbol_address = 0;
} else {
strncpy(function, info.dli_sname, sizeof(function));
strncpy(library, info.dli_fname, sizeof(library));
symbol_address = info.dli_saddr;
}
printf("0x%08X %s: %s\n", (uint32_t) symbol_address, library, function);
}
void backtrace() {
struct frame *frame;
// frame = CURRENT_FRAME_POINTER
__asm("mov %0,r28" : "=r"(frame));
// Last frames
while (frame && frame->fp) {
printf("fp = 0x%08X ra = 0x%08X\n", (uint32_t) frame->fp, (uint32_t) frame->ra);
print_symbol((void *) frame->ra);
frame = (struct frame *) frame->fp;
}
}
void test3() {
backtrace();
}
void test2() {
test3();
}
void test1() {
test2();
}
int main(int argc, char *argv) {
test1();
return 0;
}Makefile
CC = nios2-linux-gnu-gcc
CFLAGS = -Wall -fno-omit-frame-pointer
LDFLAGS = -rdynamic -ldl
SOURCES = stackframe.c
OBJECTS = $(SOURCES:%.c=%.o)
PROGRAM = stackframe
.PHONY: all clean
all: $(PROGRAM)
clean:
rm -rf $(OBJECTS) $(PROGRAM)
build: clean all
$(PROGRAM): $(OBJECTS)
$(CC) -o $@ $(OBJECTS) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $<
Build: $ make
nios2-linux-gnu-gcc -Wall -fno-omit-frame-pointer -c stackframe.c
nios2-linux-gnu-gcc -o stackframe stackframe.o -rdynamic -ldl
Execute: # stackframe
fp = 0x7FE81D24 ra = 0x00001838
0x00001824 stackframe: test3
fp = 0x7FE81D2C ra = 0x00001860
0x0000184C stackframe: test2
fp = 0x7FE81D34 ra = 0x00001888
0x00001874 stackframe: test1
fp = 0x7FE81D44 ra = 0x000018B8
0x0000189C stackframe: main