[LLVMbugs] [Bug 2787] New: Add demangling to PrintStackTrace

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Wed Sep 10 22:57:49 PDT 2008


http://llvm.org/bugs/show_bug.cgi?id=2787

           Summary: Add demangling to PrintStackTrace
           Product: new-bugs
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: viridia at gmail.com
                CC: llvmbugs at cs.uiuc.edu


Here's the code snippet which does demangling on OS X. To make this work for
other unix-like platforms, you'll need to change the code which extracts the
symbol name from the line of text returned by backtrace symbols.

So essentially, you will need to:

1) Add a test for <cxxabi.h> in the configure script.

2) Optionally, you can add a test for __cxa_demangle. There are autoconfig
tests for this out on the net that can be used, but personally I think this
step is not necessary.

3) Write a platform-specific function to extract the name of the mangled symbol
from the line of text produced by backtrace_symbols.

----------------------------------------------------------------
#if HAVE_CXXABI_H
   #include <cxxabi.h>
#endif

   /// ....

   static void PrintStackTrace(int skipFrames) {
   #ifdef HAVE_BACKTRACE
     // Use backtrace() to output a backtrace on Linux systems with glibc.
     int depth = backtrace(stackTrace,
         static_cast<int>(arrayLengthOf(StackTrace)));

   #ifdef HAVE_CXXABI_H
     if (char ** symbols = backtrace_symbols(StackTrace, depth)) {
             // Name buffer used to contain demangling result.
       size_t sz = 256;
       char * buffer = (char *)malloc(sz);

       for (int i = 0; i < depth; ++i) {
         // Skip this frame, and possibly the assert machinery as well.
         if (i >= skipFrames) {
           char * symbol = symbols[i];
           // TODO: This is a very cheesy way to extract the symbol name,
           // need to come up with something that will work on various
   platforms.
           // fprintf(outstream, "%s\n", symbol);
           char * begin = strchr(symbol, '_');
           char * demangled_name = NULL;
           if (begin) {
             char * end = strchr(begin, ' ');
             if (end) {
               *end = 0;
               int status;
               demangled_name = abi::__cxa_demangle(begin, buffer, &sz,
   &status);
             }
           }
                   if (demangled_name != NULL) {
             fprintf(outstream, "%s\n", demangled_name);
                       // Result may be a realloc of input buffer.
             buffer = demangled_name;
           }
         }
       }

       free(symbols);
       free(buffer);
     }
   #else
     backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
   #endif
   #endif
   } 
----------------------------------------------------------------


-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list