feron_jb Posted March 10, 2014 Report Posted March 10, 2014 Hi, I have some problems with exceptions thrown inside systemC modules and catched in a classical C++ class, parent of the systemC one. The codes shown here only illustrate the structure of my program and are not intended to be compiled. I defined my own exception class, extending the standard exception class : #include "systemc.h" class customException : public std::exception{ // Constructor customException{ // Instance variables with information about the error std::string message; std::string fileInfo; sc_time time; }; // Get information about the error std::string getMessage(); std::string getFileInfo(); sc_time getTime(); }; In a systemC module, one of my customException is thrown. If it is catched inside the modules, it is correctely interpreted. #include "systemc.h" #include "customException.h" SC_MODULE (funcModule){ // Constructor SC_HAS_PROCESS(funcModule); funcModule(sc_module_name instanceName) : sc_module(instanceName){ SC_THREAD(funcThread); } // SystemC thread, throwing an exception void funcThread(){ while(true){ ... // Throw one of my custom exception customException e("Error message", __FILE__, sc_time_stamp()); throw e; ... } // If 'while block' in try-catch block, exception is caught normally, // as a custom exception } }; A classical C++ class instanciates this systemC module and defines a function running it, able to catch its exception: #include "systemc.h" #include "customException.h" #include "funcModule.h" class testFunc{ funcModule myFunc; // Consructor testFunc : myFunc("myFunc"){ }; // Run the SystemC module void funcRun(){ try{ sc_start(20, SC_NS); } catch (customException& e){ // Not catched } catch (const std::exception &exc){ // Catched } catch (...){ // Catched } } }; In a testbench, if I call the funcRun function testFunc myTestFunc; myTestFunc.funcRun(); The exception will be catched, but as a standard exception, not as one of my custom exception. Do you have any idea about why this happens? Do you have a solution? Thank you, Regards, J-B Quote
dakupoto Posted March 11, 2014 Report Posted March 11, 2014 Hi, I have some problems with exceptions thrown inside systemC modules and catched in a classical C++ class, parent of the systemC one. The codes shown here only illustrate the structure of my program and are not intended to be compiled. I defined my own exception class, extending the standard exception class : #include "systemc.h" class customException : public std::exception{ // Constructor customException{ // Instance variables with information about the error std::string message; std::string fileInfo; sc_time time; }; // Get information about the error std::string getMessage(); std::string getFileInfo(); sc_time getTime(); }; In a systemC module, one of my customException is thrown. If it is catched inside the modules, it is correctely interpreted. #include "systemc.h" #include "customException.h" SC_MODULE (funcModule){ // Constructor SC_HAS_PROCESS(funcModule); funcModule(sc_module_name instanceName) : sc_module(instanceName){ SC_THREAD(funcThread); } // SystemC thread, throwing an exception void funcThread(){ while(true){ ... // Throw one of my custom exception customException e("Error message", __FILE__, sc_time_stamp()); throw e; ... } // If 'while block' in try-catch block, exception is caught normally, // as a custom exception } }; A classical C++ class instanciates this systemC module and defines a function running it, able to catch its exception: #include "systemc.h" #include "customException.h" #include "funcModule.h" class testFunc{ funcModule myFunc; // Consructor testFunc : myFunc("myFunc"){ }; // Run the SystemC module void funcRun(){ try{ sc_start(20, SC_NS); } catch (customException& e){ // Not catched } catch (const std::exception &exc){ // Catched } catch (...){ // Catched } } }; In a testbench, if I call the funcRun function testFunc myTestFunc; myTestFunc.funcRun(); The exception will be catched, but as a standard exception, not as one of my custom exception. Do you have any idea about why this happens? Do you have a solution? Thank you, Regards, J-B Dear Sir, It is perfectly understandable that for proprietary research or industrial purposes it is impossible to release the full code, so please provide a minimal working module set that illustrates the problem. Has the code been tested with e.g., GDB to trace the control flow ? Quote
Philipp A Hartmann Posted March 11, 2014 Report Posted March 11, 2014 The exception will be catched, but as a standard exception, not as one of my custom exception. Do you have any idea about why this happens? Do you have a solution? An SC_THREAD runs within its own stack. When an exception is thrown, this stack is unwound until it arrives at a matching exception handler. That's why you can catch your custom exception within the thread itself. If you don't provide your own exception handler, the SystemC kernel's generic exception handler for the thread jumps in and catches the exception on your behalf. Since the SystemC kernel can't know anything about your customException class, it is caught as std::exception (and translated to a new sc_report instance). The only available solution is to wrap your thread implementation (not the sc_start call) with a catch black and to translate and forward it appropriately to your test class (e.g. via a member of the module and then calling sc_stop() to stop the simulation). You can find the translation and forwarding as it is done in the SystemC kernel itself in the (Accellera) kernel's function sc_handle_exception() and its uses. hth, Philipp David Black 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.