Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12603 Discussions

How backtrace when an instruction related exception is caught

Altera_Forum
Honored Contributor II
1,107 Views

Hello, I have followed the instructions to enabled the "instruction related exception" and is able to catch the exception. The exception handler provides the cause and address which can be used to determine what instruction caused the exception. However, I also would like to get the backtrace when this exception happened. Could anyone help me to figure out how to get the backtrace? Thanks

0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
419 Views

The easiest method is that used by the linux kernel: 

Just scan through the stack and report every value that is inside the code area. 

Stale values (caused by unwritten locations in the current call sequence) are a slight problem. 

 

If the code is compiled to use a frame pointer, then it should be possible to follow the fp chain (fp should point to the previous value and the return address should be in the adjacent location). But IIRC someone has reported that gcc doesn't always get this right. 

 

Or you can use the debug info generated by the compiler that contains the stack offset at every instruction offset - not for the faint hearted. 

 

Or you can follow the instruction stream forwards while monitoring the value of sp (and fp) until you hit a return instruction. You need to remember the instructions processed and be willing to restart assuming that a branch is taken. Works quite well provided you are in a function that can return. 

Probably not to bad to implement for nios either (a pain for x86).
0 Kudos
Altera_Forum
Honored Contributor II
419 Views

Thanks, I will try to do what you have suggested.

0 Kudos
Reply