Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 960637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:12:50+00:00 2026-05-16T01:12:50+00:00

This question has been asked before and there have been windows-specific answers but no

  • 0

This question has been asked before and there have been windows-specific answers but no satisfactory gcc answer. I can use set_terminate() to set a function that will be called (in place of terminate()) when an unhandled exception is thrown. I know how to use the backtrace library to generate a stack trace from a given point in the program. However, this won’t help when my terminate-replacement is called since at that point the stack has been unwound.

Yet if I simply allow the program to abort(), it will produce a core-dump which contains the full stack information from the point at which the exception was thrown. So the information is there — but is there a programmatic way to get it, for example so it can be logged, rather than having to examine a core file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:12:50+00:00Added an answer on May 16, 2026 at 1:12 am

    Edited Answer:

    You can use std::set_terminate

    #include <cstdlib>
    #include <iostream>
    #include <stdexcept>
    
    #include <execinfo.h>
    
    void
    handler()
    {
        void *trace_elems[20];
        int trace_elem_count(backtrace( trace_elems, 20 ));
        char **stack_syms(backtrace_symbols( trace_elems, trace_elem_count ));
        for ( int i = 0 ; i < trace_elem_count ; ++i )
        {
            std::cout << stack_syms[i] << "\n";
        }
        free( stack_syms );
    
        exit(1);
    }   
    
    int foo()
    {
        throw std::runtime_error( "hello" );
    }   
    
    void bar()
    {
        foo();
    }
    
    void baz()
    {
        bar();
    }
    
    int
    main()
    {
        std::set_terminate( handler );
        baz();
    }
    

    giving this output:

    samm@macmini ~> ./a.out 
    ./a.out [0x10000d20]
    /usr/lib/libstdc++.so.6 [0xf9bb8c8]
    /usr/lib/libstdc++.so.6 [0xf9bb90c]
    /usr/lib/libstdc++.so.6 [0xf9bbaa0]
    ./a.out [0x10000c18]
    ./a.out [0x10000c70]
    ./a.out [0x10000ca0]
    ./a.out [0x10000cdc]
    /lib/libc.so.6 [0xfe4dd80]
    /lib/libc.so.6 [0xfe4dfc0]
    samjmill@bgqfen4 ~> 
    

    assuming you have debug symbols in your binary, you can then use addr2line to construct a prettier stack trace postmortem

    samm@macmini ~> addr2line 0x10000c18
    /home/samm/foo.cc:23
    samm@macmini ~> 
    

    original answer is below


    I’ve done this in the past using boost::error_info to inject the stack trace using backtrace from execinfo.h into an exception that is thrown.

    typedef boost::error_info<struct tag_stack_str,std::string> stack_info;
    

    Then when catching the exceptions, you can do

    } catch ( const std::exception& e ) {                                                                                                            
        if ( std::string const *stack boost::get_error_info<stack_error_info>(e) ) {                    
            std::cout << stack << std::endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked before ( link ) but I have slightly different
I know this specific question has been asked before , but I am not
I know this question has been asked before, but I ran into a problem.
I know this question has been asked a bit before. But looking around I
It's quite possible a question like this has been asked before, but I can't
This has been asked before (question no. 308581) , but that particular question and
If this question has already been asked I appologies, please point me in the
Preamble So, this question has already been answered, but as it was my first
This question has been discussed in two blog posts ( http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/ , http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/ ),
Being new to test based development, this question has been bugging me. How much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.