Re: Recursive SIGSEGV question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 27/03/2019 21:34, Jonathan Wakely wrote:
On Wed, 27 Mar 2019 at 21:27, Jonny Grant wrote:
Hi!

Thank you for your reply and input.

Maybe GCC's "libbacktrace" module could be used?

I was wondering if -fsanitize=address would output a backtrace for the
C++ exception, but it doesn't seem to. Also it actually prevents the
core being dumped - that's probably not intended?

Compile without Sanitizer, and it does dump the core to a file at least!

$ export UBSAN_OPTIONS=print_stacktrace=1

This is a UBsan option.

// g++-8 -fsanitize=address -Wall -o exception exception.cpp

But you're not using UBsan.

#include <vector>
int main()
{
      std::vector<int> v;
      return v.at(0);
}


$ ./exception
terminate called after throwing an instance of 'std::out_of_range'
    what():  vector::_M_range_check: __n (which is 0) >= this->size()
(which is 0)
Aborted

There's no undefined behaviour or memory corruption here, so it's not
surprising that UBsan and Asan don't print anything.

Ok I see, thank you for pointing this out.

I did wonder, as -fsanitize=address seems to inhibit the core dump that is otherwise created by the abort() that appears to be called - is that a known issue?

$ g++-8 -Wall -o exception exception.cpp
jonny@asus:~/code/crash$ ./exception
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (core dumped)
$

Usually I just load the core dump into GDB to take a look at it.


If you want a stack trace for exceptions that terminate the process
then you could install a custom terminate handler which does that.
Libstdc++'s default terminate handler just prints the message above,
which includes the type of the exception and if it's a type derived
from std::exception, the what() string stored in it.

Yes, I'd love to have a stack trace for exceptions that terminate the process. Do you know a simple example you can refer me to? I've looked and there are people using boost::stacktrace::stacktrace() but I'd rather not link to boost as a dependency.

It would be great if there was a glibc option to do this, or GCC could insert it.

Otherwise we each need to insert our own stack tracers...

Found this:
https://www.gnu.org/software/libc/manual/html_node/Backtraces.html

I added this (attached) to a C++ exception handler, but there's no file and line numbers. Other examples resort to calling addr2line! Seems a bit over the top for us each to code our own stack tracer... Or reading symbols etc. Am I asking too much for a general print_backtrace() in libc or elsewhere ?

Cheers, Jonny
// g++-8 -Wall -g -o exception exception.cpp
#include <vector>
#include <iostream>
int main2();

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

/* Obtain a backtrace and print it to stdout.
 https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
 */
void
print_trace (void)
{
  void *array[10];
  size_t size;
  char **strings;
  size_t i;

  size = backtrace (array, 10);
  strings = backtrace_symbols (array, size);

  printf ("Obtained %zd stack frames.\n", size);

  for (i = 0; i < size; i++)
     printf ("symbols %s\n", strings[i]);

  free (strings);
}

int main()
{
    try
    {
        main2();
    }
    catch( const std::exception &e)
    {
        const std::string what(e.what());
        std::cout << "Unhandled exception: [" << what << "]\n";
        print_trace();
        exit(0);
    }
}

int main2()
{
    std::vector<int> v;
    return v.at(0);
}


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux