Forum Discussion
About exceptions: Is try, catch and thow supported?
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?15 Replies
- Altera_Forum
Honored Contributor
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.
- Altera_Forum
Honored Contributor
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) - Altera_Forum
Honored Contributor
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 - Altera_Forum
Honored Contributor
more current info on bringing up gcc exceptions
http://gcc.gnu.org/wiki/dwarf2ehnewbieshowto - Altera_Forum
Honored Contributor
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); } - Altera_Forum
Honored Contributor
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' - Altera_Forum
Honored Contributor
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! - Altera_Forum
Honored Contributor
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.
- Altera_Forum
Honored Contributor
Hi Jeffrey,
Great explanations! Though I cannot find the aforementioned gcc/config/nios2/rtems.h file under my Altera Niod EDS folder. I'm currently using the Nios II EDS 11.1sp1. Can you help me find that file, so I can try your solution for the try-catch problem? I didn't want to start changing random files in my gcc folder. I only have the following rtems.h files: c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\rtems.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\arm\rtems-elf.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\avr\rtems.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\c4x\rtems.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\h8300\rtems.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\i386\rtemself.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\m68k\rtemself.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\mips\rtems.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\rs6000\rtems.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\sh\rtems.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\sh\rtemself.h c:\altera\11.1\nios2eds\bin\gnu\src\gcc-4.1\gcc\config\sparc\rtemself.h There is no nios2 under the config folder. Thanks, Tibor - Altera_Forum
Honored Contributor