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++
12589 Discussions

About exceptions: Is try, catch and thow supported?

Altera_Forum
Honored Contributor II
2,481 Views

I would like to use try-catch statements, but it does not seems to work for me. I work with Quartus II 11.0sp1 and nios II (the fpga includes a vic). 

 

When I debug what happens in my throw statement it seems as if I get another exception during the call of "_Unwind_RaiseException" in _cxxabiv1::__cxa_throw in the gnu file eh_throw.cc. 

 

My code setup is very simple: static int catched = 0; try { throw 20; } catch (...) { catched++; } Any one have a hint for me?
0 Kudos
15 Replies
Altera_Forum
Honored Contributor II
588 Views

Some basic c++ exception tests for rtems rt os also fail using 11.0sp1 nios eds. Apprarently gcc uses either setjmp/longjmp or DWARF based mechanisms to unwind the stack, but I haven't yet determined which of these is used for nios2. We are building the nios2 cross compiler configuring for multi-threaded execution but I am starting to suspect that using the current state of the nios2 gcc 4 compiler with threads is sketchy as the Altera built compiler is configured for single-threaded use. We need C++ so we can port community software to the nios2.

0 Kudos
Altera_Forum
Honored Contributor II
588 Views

So it appears that the DWARF mechanism is used after seeing this type of code in gcc/config/nios2/nios2.h 

 

/* Describe __builtin_eh_return */# define EH_RETURN_STACKADJ_RTX gen_rtx_REG (Pmode, LAST_RETVAL_REGNO)# define EH_RETURN_DATA_REGNO(N) ((N) <= (LAST_ARG_REGNO - FIRST_ARG_REGNO)  

? (N) + FIRST_ARG_REGNO : INVALID_REGNUM)# define ASM_PREFERRED_EH_DATA_FORMAT(CODE, GLOBAL)  

(!flag_pic ? DW_EH_PE_sdata4  

/* FIXME: These get expanded to dynamic relocs, which is wrong */  

/* : !(GLOBAL) ? DW_EH_PE_pcrel | DW_EH_PE_sdata4 */  

: DW_EH_PE_aligned)
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

I am having a look at this link to understand gcc's exception handling mechanisms, BTW 

 

http://gcc.gnu.org/ml/gcc/2002-07/msg00352.html
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

more current info on bringing up gcc exceptions  

 

http://gcc.gnu.org/wiki/dwarf2ehnewbieshowto
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

FWIW, I can see that the exception stack unwind fails at the gcc_assert in this function. 

 

static inline void 

_Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa, 

_Unwind_SpTmp *tmp_sp) 

int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()]; 

 

if (size == sizeof(_Unwind_Ptr)) 

tmp_sp->ptr = (_Unwind_Ptr) cfa; 

else 

gcc_assert (size == sizeof(_Unwind_Word)); 

tmp_sp->word = (_Unwind_Ptr) cfa; 

_Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp); 

}
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

Starting to understand what isn't working... 

 

(gdb) print dwarf_reg_size_table[0] 

$6 = 0 '\000' 

(gdb) call init_dwarf_reg_size_table() 

(gdb) Quit 

(gdb) print dwarf_reg_size_table[0] 

$8 = 4 '\004'
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

Success! FWIW, I resolved this issue and now the basic RTEMS C++ exceptions tests pass w/o issues. It turns out that there were no issues so far with the Altera extensions to gcc for nios2, but instead with the RTEMS specific configurations of the nios2 cross gcc. I needed only to add "builtin_define ("__USE_INIT_FINI__");" to "<gcc>/gcc/config/nios2/rtems.h" to make the C++ exception unwinding work correctly. 

 

I should add that the combination of ddd with nios2-elf-gdb works very nicely for debugging rtems startup issues. Initially I was confused about what location in the source was failing because the optimized gcc startup code was integrating/optimizing away inline functions. With ddd I can see both the source window and the machine instruction window simultaneously and then its easy to see what is happening!
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

BTW, I do see in the debugger that the nios2-rtems-g++ configured gcc run-time support code does administer the proper mutual exclusion locks when a thread throws a c++ exception.

0 Kudos
Altera_Forum
Honored Contributor II
588 Views

The RTEMS related files from gcc/config/nios2/ are attached.

0 Kudos
Altera_Forum
Honored Contributor II
588 Views

And the source code for nios2 gcc is of course available here. I have been using gcc 4.1. 

 

http://www.altera.com/support/ip/processors/nios2/ide/ips-nios2-ide-tutorial.html
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

Hi Jeffry, 

 

Thanks for the code, though I have a question: 

What am I suppose to do with the new packages? 

 

I downloaded the rtems.h file and the nios2 gcc. I unpacked the nios2 gcc package, but there wasn't any manual or help document in it, and I already have a NiosII EDS installed on my computer. Is there any tutorial that I can use to make this package work? 

I guess I have to make my NiosII EDS use this compiler insted of the one under the original Altera installation. Can you refer my to any document that has a description how to do that? I haven't tried to hack my EDS installation before. 

 

Thanks for your help again. 

Tibor
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

There is some info on the wiki about building gcc from the Altera tarballs.

0 Kudos
Altera_Forum
Honored Contributor II
588 Views

gytibor:  

 

I obtained the source code so I could build a nios2 cross compiler which specifically targets RTEMS RT OS. The rtems.h is used by the cross compiler build to configure stack unwinding and other properties of the cross compiler specifically for RTEMS. 

 

The link below has some of the details, and you might also search for "building configuring cross gcc" on google. 

 

http://www.alterawiki.com/wiki/crossgcc
0 Kudos
Altera_Forum
Honored Contributor II
588 Views

Hi Jeffrey, 

 

I've just realized that the RTEMS is a RTOS. Unfortunately, I'm tied to the MicroC OS-II, so I cannot just switch to a new RTOS. 

 

Thank you very much for your help. I really appreciated it. 

 

Tibor
0 Kudos
Reply