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++
The Intel sign-in experience has changed to support enhanced security controls. If you sign in, click here for more information.
12519 Discussions

How backtrace when an instruction related exception is caught

Honored Contributor II

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
Honored Contributor II

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).
Honored Contributor II

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