Re: Recursive SIGSEGV question

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

 



Just to note, -O1 seems to break this, output shows basic_string.h instead of exception4.cpp:81


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

Backtrace:
0x0000000000001573: test() at basic_string.h:176
[1]: ./exception4(+0x1573) [0x56297f26b573]
0x00000000000015d9: main at exception4.cpp:93
[2]: ./exception4(+0x15d9) [0x56297f26b5d9]
0x0000000000000000: ?? ??:0
[3]: /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x7ffa1b36db97]
0x000000000000108a: _start at ??:?
[4]: ./exception4(+0x108a) [0x56297f26b08a]

Jonny
// g++-8 -Wall -g -o exception4 exception4.cpp
// g++-8 -g -pipe -pthread -O1 -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 int 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 (int 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);
        int result = system(syscom);
        if(0 != result)
        {
            printf("system [%s] result: %d\n", syscom, result);
        }

        printf ("[%d]: %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()
{
    test();
}


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