Jump to content

Why vector use at function can't caught exception in SC?


Recommended Posts

Nowadays, I use at function to access vector and it out of range. However, it didn't pause and the program shutdown only.

#include <systemc>

int sc_main(int args, char* argv[])
{
    std::vector<int> vec = {1, 2, 3};

    int num1 = vec.at(1);
    int num2 = vec.at(10); 

    return 0;
  }

Here's the result:

root@xxx:/home#
        SystemC 2.3.3-Accellera --- Jan 25 2024 15:41:45
        Copyright (c) 1996-2018 by all Contributors,
        ALL RIGHTS RESERVED
Error: (E549) uncaught exception: vector::_M_range_check: __n (which is 10) >= this->size() (which is 3)
In file: ../../../src/sysc/kernel/sc_except.cpp:101
root@xxx:/home#

 

I expected it will be like below, and the program will be crash and output coredump file.

terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 10) >= this->size() (which is 3)

 

 

Link to comment
Share on other sites

Hello @Allen yang,

It is because sc_main is called within a context of a try catch block.

See this:

https://github.com/accellera-official/systemc/blob/bec101067d808f93bf215031dff6fa9ab7035995/src/sysc/kernel/sc_main_main.cpp#L66-L108\

Captured the snippet from the implementation and reproduced below.

try
    {
        pln();

        // Perform initialization here
        sc_in_action = true;

        // copy array of pointers to keep allocated pointers for later release
        std::vector<char*> argv_call = argv_copy;
        status = sc_main( argc, &argv_call[0] );

        // Perform cleanup here
        sc_in_action = false;
    }
    catch( const sc_report& x )
    {
        sc_report_handler::get_handler()
            ( x, sc_report_handler::get_catch_actions() );
    }
    catch( ... )
    {
        // translate other escaping exceptions
        sc_report*  err_p = sc_handle_exception();
        if( err_p )
            sc_report_handler::get_handler()
                ( *err_p, sc_report_handler::get_catch_actions() );
        delete err_p;
    }

You can see there is a broader catch( ... ) statement which is causing the exception to be handled instead of being thrown.

You can possibly try writing you own main function but will need to take care of additional SystemC kernel initialization sequences.

Hope this helps.

Regards,

Ameya Vikram Singh

Link to comment
Share on other sites

2 hours ago, Allen yang said:

I try to rewrite `sc_except.cpp::sc_handle_exception`, it didn't work. Can you help me figure out this?

Hello @Allen yang,

It is possible to get a stack trace, but then again what is your use-case?

Regards,

Ameya Vikram Singh

Link to comment
Share on other sites

12 hours ago, AmeyaVS said:

Hello @Allen yang,

It is possible to get a stack trace, but then again what is your use-case?

Regards,

Ameya Vikram Singh

My use-case is in `SC_METHOD` or `SC_(C)THREAD` if exception caught I can see the ori stack trace with c++ not rethrow or caught by `sc_report`.

The stack trace isn't my exception. I don't want to use `catch throw` to get stack trace which I really need.

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...