Changeset 20774

Show
Ignore:
Timestamp:
09/06/07 15:27:10 (1 year ago)
Author:
patrick
Message:

Demangle function names when getting the stack trace on Mac OS X.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/trunk/modules/vapor/vpr/SystemBase.cpp

    r20773 r20774  
    3636#include <vpr/vprConfig.h> 
    3737 
     38#include <cstdlib> 
     39#include <sstream> 
     40#include <string> 
     41 
    3842#if defined(HAVE_BACKTRACE) 
    3943#  include <sys/types.h> 
    4044#  include <unistd.h> 
    4145#  include <execinfo.h> 
    42 #  include <sstream> 
    4346#elif defined(VPR_OS_Darwin) 
    4447#  include <iomanip> 
    45 #  include <sstream> 
    4648#  include <vector> 
     49#  include <boost/algorithm/string/predicate.hpp> 
    4750 
    4851extern "C" 
     
    6164#  endif 
    6265 
    63 #  include <stdlib.h> 
    6466#  include <dbghelp.h> 
    6567#  include <iomanip> 
    66 #  include <sstream> 
    6768#  include <boost/format.hpp> 
    6869#endif 
    69  
    70 #include <string> 
    7170 
    7271#if (! defined(__INTEL_COMPILER) && defined(__GNUC__) && \ 
     
    208207#endif   /* ifdef VPR_OS_Windows */ 
    209208 
    210 std::string demangleTraceString(char* traceLine) 
     209std::string demangleTraceString(const std::string& traceLine) 
    211210{ 
    212211#ifdef USE_CXA_DEMANGLE 
     
    214213   // and replace it with a demangled version of the name. 
    215214   // Example: build.linux/stuff/classfile(_ZN4vpr11Someing33methodEv+0xd3) [0x80cfa29] 
     215   // For Mac OS X, things are a little different. A symbol may be of the 
     216   // form _ZN3vpr9ExceptionC2ERKSsS2_:F(0,1), or it may be a simple function 
     217   // name, probably with a leading underscore. 
    216218 
    217219   std::string trace_line(traceLine); 
     
    219221 
    220222   std::string::size_type start(std::string::npos), end(std::string::npos); 
     223 
     224#if defined(VPR_OS_Darwin) 
     225   end = trace_line.find(":F("); 
     226 
     227   // If trace_line does not contain ":F(", then we set the start to be either 
     228   // 0 or 1 depending on whether trace_line starts with "_", and we set end 
     229   // to be the end of the string. 
     230   if ( std::string::npos == end ) 
     231   { 
     232      start = boost::algorithm::starts_with(trace_line, "_") ? 1 : 0; 
     233      end   = trace_line.size(); 
     234   } 
     235   else 
     236   { 
     237      start = 0; 
     238   } 
     239#else 
    221240   start = trace_line.find("(_"); 
    222241   if(std::string::npos != start) 
     
    224243      end = trace_line.find_first_of("+)",start); 
    225244   } 
     245#endif 
    226246 
    227247   if(std::string::npos != end) 
    228248   { 
    229       mangled_name.assign(trace_line, start+1, end-start-1); 
     249      std::string::size_type assign_start, assign_end; 
     250#if defined(VPR_OS_Darwin) 
     251      assign_start = start; 
     252      assign_end   = end; 
     253#else 
     254      assign_start = start + 1; 
     255      assign_end   = end - start - 1; 
     256#endif 
     257      mangled_name.assign(trace_line, assign_start, assign_end); 
     258 
    230259      int status; 
    231260      char* demangled_buf = abi::__cxa_demangle(mangled_name.c_str(), NULL, 
     
    233262      if(0==status) 
    234263      { 
    235          demangled_name = std::string(demangled_buf); 
    236          free(demangled_buf); 
    237  
    238          trace_line.replace(start+1, (end-start-1), demangled_name); 
     264#if defined(VPR_OS_Darwin) 
     265         trace_line = demangled_buf; 
     266#else 
     267         trace_line.replace(start + 1, end - start - 1, demangled_buf); 
     268#endif 
     269         std::free(demangled_buf); 
    239270      } 
    240271      else if(-1==status) 
     
    501532      cur_frame.pc    = frame->savedLR; 
    502533      cur_frame.frame = reinterpret_cast<size_t>(frame); 
    503       cur_frame.name  = getFunctionName(frame->savedLR, &cur_frame.offset); 
     534 
     535      const std::string func_name = getFunctionName(frame->savedLR, 
     536                                                    &cur_frame.offset); 
     537 
     538      if ( ! func_name.empty() ) 
     539      { 
     540         cur_frame.name = demangleTraceString(func_name); 
     541      } 
    504542 
    505543      if ( cur_frame.pc != 0 )