Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:47

0001 #pragma once
0002 
0003 /**
0004 SStackFrame
0005 =============
0006 
0007 Used for stack frame introspection based on *cxxabi.h*
0008 
0009 **/
0010 
0011 
0012 #include "SYSRAP_API_EXPORT.hh"
0013 #include <ostream>
0014 
0015 struct SYSRAP_API SStackFrame
0016 {
0017     static char* TrimArgs(const char* signature); 
0018 
0019     SStackFrame( char* line ) ;
0020     ~SStackFrame();
0021 
0022     void parse();
0023     char* demangle(); // fails for non C++ symbols
0024     void dump();
0025     void dump(std::ostream& out);
0026 
0027     char* line ; 
0028     char* name ; 
0029     char* offset ;
0030     char* end_offset ;
0031  
0032     char* func ;    // only func and smry are "owned"
0033     char* smry ; 
0034 };
0035 
0036 
0037 #include <iostream>
0038 #include <iomanip>
0039 #include <cxxabi.h>
0040 
0041 
0042 inline SStackFrame::SStackFrame(char* line_)
0043     :
0044     line(line_),
0045     name(nullptr),
0046     offset(nullptr),
0047     end_offset(nullptr),
0048     func(nullptr),
0049     smry(nullptr)
0050 {
0051     parse(); 
0052     if(func) smry = TrimArgs(func); 
0053 } 
0054 
0055 inline SStackFrame::~SStackFrame()
0056 {
0057     free(func);  
0058     free(smry);  
0059 }
0060 
0061 inline void SStackFrame::dump()
0062 {
0063     std::ostream& out = std::cout ;
0064     dump(out);    
0065 }
0066 
0067 /**
0068 SStackFrame::TrimArgs
0069 -----------------------
0070 
0071 G4VEmProcess::PostStepGetPhysicalInteractionLength(G4Track const&, double, G4ForceCondition*)
0072 -> 
0073 G4VEmProcess::PostStepGetPhysicalInteractionLength
0074 
0075 **/
0076 
0077 inline char* SStackFrame::TrimArgs(const char* signature)
0078 {
0079     char* smry = strdup(signature) ; 
0080     for( char* p = smry ; *p ; ++p ) if( *p == '(' ) *p = '\0' ; 
0081     return smry ; 
0082 }
0083 
0084 
0085 #ifdef __APPLE__
0086 inline void SStackFrame::parse()
0087 {
0088     /**
0089     4   libG4processes.dylib                0x00000001090baee5 _ZN12G4VEmProcess36PostStepGetPhysicalInteractionLengthERK7G4TrackdP16G4ForceCondition + 661 
0090     **/ 
0091     for( char *p = line ; *p ; ++p )
0092     {   
0093         if (( *p == '_' ) && ( *(p-1) == ' ' )) name = p-1;  // starting from first underscore after space 
0094         else if ( *p == '+' ) offset = p-1;
0095     }   
0096 
0097     if( name && offset && ( name < offset ))
0098     {   
0099         *name++ = '\0';    // plant terminator into line
0100         *offset++ = '\0';  // plant terminator into name  
0101         func = demangle(); 
0102     }  
0103 
0104 }
0105 
0106 
0107 inline void SStackFrame::dump(std::ostream& out)
0108 {
0109      if( func )
0110      {
0111          out 
0112              << std::setw(30) << std::left << line  
0113              << " "
0114              << std::setw(100) << std::left << func
0115              << " "
0116              << std::setw(10) << std::left << offset 
0117              << " " 
0118              << std::endl 
0119              ;
0120      }
0121      else
0122      {
0123          out << line << std::endl ; 
0124 
0125      }
0126 }
0127 
0128 #else
0129 inline void SStackFrame::parse()
0130 {
0131     /**
0132     /home/blyth/local/opticks/externals/lib64/libG4tracking.so(_ZN10G4VProcess12PostStepGPILERK7G4TrackdP16G4ForceCondition+0x42) [0x7ffff36ff9b2] 
0133     **/
0134     for( char *p = line ; *p ; ++p )
0135     {   
0136         if ( *p == '(' ) name = p;
0137         else if ( *p == '+' ) offset = p;
0138         else if ( *p == ')' && ( offset || name )) end_offset = p;
0139     }   
0140 
0141     if( name && end_offset && ( name < end_offset ))
0142     {   
0143         *name++ = '\0';    // plant terminator into line
0144         *end_offset++ = '\0';  // plant terminator into name  
0145         if(offset) *offset++ = '\0' ; 
0146         func = demangle(); 
0147     }  
0148 }
0149 inline void SStackFrame::dump(std::ostream& out)
0150 {
0151      if( func )
0152      {
0153          out 
0154                << std::setw(25) << std::left << ( end_offset ? end_offset : "" )  // addr
0155                << " " << std::setw(10) << std::left << offset 
0156                << " " << std::setw(60) << line  
0157                << " " << std::setw(60) << std::left << func
0158                << std::endl 
0159                ;
0160      }
0161      else
0162      {
0163          out  << line << std::endl ; 
0164 
0165      }
0166 }
0167 #endif
0168 
0169 inline char* SStackFrame::demangle() // demangling fails for non C++ symbols
0170 {
0171     int status;
0172     char* ret = abi::__cxa_demangle( name, NULL, NULL, &status );
0173     return status == 0 ? ret : NULL ; 
0174 }
0175 
0176