New stack trace functions

Parsing the backtrace output to demangle names:

    String msg;
    String bt = SystemStats::getStackBacktrace();

    // The lines returned look like this on linux:
    //
    //      test/test.out(_ZN4juce11SystemStats17getStackBacktraceEv+0x2a) [0x53fdea]
    //
    // At the very least, attempt to parse out the names so we can properly demangle them.

    while( bt.isNotEmpty() )
    {
        // parse out the next line from the backtrace string and remove it from bt
        String line;
        const int eol = bt.indexOfChar( '\n' );
        if ( eol > 0 )
        {
            line = bt.substring( 0, eol - 1 );
            bt   = bt.substring( eol + 1 );
        }
        else
        {
            // no "\n" left, so consume the rest of the string
            line = bt;
            bt   = String::empty;
        }

        const int startParen = line.indexOfChar( '(' );
        const int closeParen = line.indexOfChar( ')' );
        const int plusOffset = line.lastIndexOfChar( '+' );
        if (    startParen  > 0             &&
                plusOffset  > startParen    &&
                closeParen  > plusOffset    )
        {
            // look like we have a string we can try and demangle
            String name( line.substring( startParen + 1, plusOffset ) );
            int status = 0;
            char *demangled = abi::__cxa_demangle( name.toStdString().c_str(), nullptr, nullptr, &status );
            if ( status == 0 )
            {
                // we have a better demangled name we can use -- replace it within the line
                name = " " + String(demangled) + " ";
                line = line.replaceSection( startParen + 1, plusOffset - startParen - 1, name );
            }
            free( demangled );
        }

        msg += line + '\n';
    }
1 Like