public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonny Grant <jg@jguk.org>
To: Xi Ruoyao <xry111@mengyan1223.wang>
Cc: Jonathan Wakely <jwakely.gcc@gmail.com>,
	Florian Weimer <fw@deneb.enyo.de>, Andrew Haley <aph@redhat.com>,
	gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Recursive SIGSEGV question
Date: Sat, 30 Mar 2019 17:32:00 -0000	[thread overview]
Message-ID: <979bb085-1f08-1c40-df77-5d9e5512f184@jguk.org> (raw)
In-Reply-To: <3f191a23-97c3-3f40-3760-8f41074901e0@jguk.org>

[-- Attachment #1: Type: text/plain, Size: 944 bytes --]

Hi!
Just sharing the little backtrace sample I made using addr2line and libc 
backtrace_symbols().

The backtrace doesn't appear to be completely accurate, but it is something.

The file goes from 22KB (as you found Xi!) to 87KB on my 64bit ubuntu 
machine when I add -g.

So I'm pleased with this, better than Go's 1,997,502 byte backtrace then?



$ ./exception4
Unhandled C++ exception: [vector::_M_range_check: __n (which is 0) >= 
this->size() (which is 0)]

Backtrace:
0x00000000000014de: test() at exception4.cpp:75
[1]: ./exception4(+0x14de) [0x563bd5b804de]
0x0000000000001550: main at exception4.cpp:87
[2]: ./exception4(+0x1550) [0x563bd5b80550]
0x0000000000000000: ?? ??:0
[3]: /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) 
[0x7f1d0c52bb97]
0x0000000000000fea: _start at ??:?
[4]: ./exception4(+0xfea) [0x563bd5b7ffea]


Could GCC insert something similar using libc backtrace_symbols() with 
an -fcrash-handler ?

Jonny

[-- Attachment #2: exception4.cpp --]
[-- Type: text/x-c++src, Size: 2001 bytes --]

// g++-8 -Wall -g -o exception4 exception4.cpp
#include <vector>
#include <iostream>
#include <string>

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

int main2();

/* 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];
    const size_t size = backtrace (array, 10);
    char **strings = backtrace_symbols (array, size);

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

    // skip first, as it is this handler
    for (size_t i = 1; i < size; i++)
    {
        // extract the exe name
        std::string exe(strings[i]);
        {
            const size_t s = exe.find("(");
            if(std::string::npos != s)
            {
                exe.erase(s, exe.length());
            }
        }

        // extract the address
        std::string addr(strings[i]);
        {
            size_t s = addr.find("(");
            if(std::string::npos != s)
            {
                ++s;
                addr.erase(0, s);

                s = addr.find(")");
                if(std::string::npos != s)
                {
                    addr.erase(s, addr.length());
                }
            }
        }

        //printf("exe '%s' addr '%s'\n", exe.c_str(), addr.c_str());

        char syscom[256];
        sprintf(syscom,"addr2line -s -a -p -f -C -e %s %s", exe.c_str(), addr.c_str());
        //printf("%s\n", syscom);
        system(syscom);

        printf ("[%zu]: %s\n", i, strings[i]);
    }

    free (strings);
}

void test()
{
    try
    {
        main2();
    }
    catch( const std::exception &e)
    {
        const std::string what(e.what());
        std::cout << "Unhandled C++ exception: [" << e.what() << "]\n";
        print_trace();
        //char * p = NULL;
        //*p = 1;
    }
}


int main(int argc, const char * argv[])
{
    test();
}


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


  reply	other threads:[~2019-03-30  0:35 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-19 22:05 Jonny Grant
2019-03-20  4:02 ` Florian Weimer
2019-03-20  8:11   ` Jonny Grant
2019-03-25 13:23   ` Jonny Grant
2019-03-25 13:27     ` Jonathan Wakely
2019-03-25 13:56     ` Florian Weimer
2019-03-25 14:01     ` Xi Ruoyao
2019-03-25 15:47       ` Florian Weimer
2019-03-25 16:10         ` Andrew Haley
2019-03-25 16:13           ` Jonny Grant
2019-03-25 16:23             ` Jonathan Wakely
2019-03-25 18:51           ` Florian Weimer
2019-03-25 20:39             ` Jonny Grant
2019-03-26  6:50               ` Xi Ruoyao
2019-03-27  0:29                 ` Jonathan Wakely
2019-03-27 21:34             ` Jonny Grant
2019-03-27 23:43               ` Jonathan Wakely
2019-03-27 23:51                 ` Jonny Grant
2019-03-28  8:26                   ` Xi Ruoyao
2019-03-28 11:52                     ` Jonathan Wakely
2019-03-29  2:24                     ` Jonny Grant
2019-03-30 17:32                       ` Jonny Grant [this message]
2023-02-19 21:21                       ` Jonny Grant
2023-02-19 21:34                         ` Jonny Grant
2019-03-28 13:55                   ` Jonathan Wakely
2019-03-28 14:39                     ` Jonny Grant
2019-03-28 14:39                       ` Jonathan Wakely
2019-03-25 20:28         ` Segher Boessenkool
2019-03-25 18:56       ` Segher Boessenkool
2019-03-25 22:05       ` Jonny Grant
2019-03-26 10:20         ` Xi Ruoyao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=979bb085-1f08-1c40-df77-5d9e5512f184@jguk.org \
    --to=jg@jguk.org \
    --cc=aph@redhat.com \
    --cc=fw@deneb.enyo.de \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    --cc=xry111@mengyan1223.wang \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).