| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "na64util/exception.hh" | ||
| 2 | |||
| 3 | #if defined(NA64DP_ENABLE_EXC_STACKTRACE) && NA64DP_ENABLE_EXC_STACKTRACE | ||
| 4 | # include <cxxabi.h> | ||
| 5 | # include <execinfo.h> | ||
| 6 | # include <unistd.h> | ||
| 7 | #endif | ||
| 8 | |||
| 9 | #include <iostream> | ||
| 10 | #include <iomanip> | ||
| 11 | |||
| 12 | namespace na64dp { | ||
| 13 | namespace errors { | ||
| 14 | |||
| 15 | #if defined(NA64DP_ENABLE_EXC_STACKTRACE) && NA64DP_ENABLE_EXC_STACKTRACE | ||
| 16 | |||
| 17 | // shall be a platform-dependant value? anyway, stack trace is usually | ||
| 18 | // pointless at the level of recursion... | ||
| 19 | #define MAX_STACK_DEPTH_N 255 | ||
| 20 | |||
| 21 | #if defined(BFD_FOUND) && BFD_FOUND | ||
| 22 | // RD: Binutils version 2.33 -> 2.34 made `*_get_*()` functions deprecated. | ||
| 23 | // However I failed to find out proper version macro to make a conditional | ||
| 24 | // block, so if your glibc is of version <2.34, you have to uncomment this line | ||
| 25 | // manually :(. | ||
| 26 | //#define OLD_BFD_API | ||
| 27 | static void | ||
| 28 | _seek_entry_in_section( bfd * abfd | ||
| 29 | , asection * section | ||
| 30 | , void * entry_) { | ||
| 31 | auto & entry = *reinterpret_cast<StackRecord*>(entry_); | ||
| 32 | if( entry.lineno ) { /* line found */ return; } | ||
| 33 | |||
| 34 | if(!( | ||
| 35 | #ifdef OLD_BFD_API | ||
| 36 | bfd_get_section_flags(abfd, section) & SEC_ALLOC | ||
| 37 | #else | ||
| 38 | bfd_section_flags(section) & SEC_ALLOC | ||
| 39 | #endif | ||
| 40 | )) { | ||
| 41 | /*no debug info here*/ return; } | ||
| 42 | bfd_vma section_vma = | ||
| 43 | #ifdef OLD_BFD_API | ||
| 44 | bfd_get_section_vma(abfd, section) | ||
| 45 | #else | ||
| 46 | bfd_section_vma(section) | ||
| 47 | #endif | ||
| 48 | ; | ||
| 49 | if(entry.soLibAddr < section_vma) { | ||
| 50 | /* the addr lies above the section */ return;} | ||
| 51 | bfd_size_type section_size = | ||
| 52 | #ifdef OLD_BFD_API | ||
| 53 | bfd_section_size(abfd, section) | ||
| 54 | #else | ||
| 55 | bfd_section_size(section) | ||
| 56 | #endif | ||
| 57 | ; | ||
| 58 | if(entry.soLibAddr >= section_vma + section_size) { | ||
| 59 | /* the addr lies below the section */ return; } | ||
| 60 | // Calculate the correct offset of our line in the section | ||
| 61 | bfd_vma offset = entry.soLibAddr - section_vma - 1; | ||
| 62 | // Locate the line by offset | ||
| 63 | const char * filename=NULL, | ||
| 64 | * functionName=NULL; | ||
| 65 | entry.lFound = bfd_find_nearest_line( | ||
| 66 | abfd, section, entry.sTable, | ||
| 67 | offset, &filename, &functionName, | ||
| 68 | &entry.lineno ); | ||
| 69 | |||
| 70 | if( !entry.lFound ) { | ||
| 71 | entry.failure = "Source lookup failed."; | ||
| 72 | } | ||
| 73 | |||
| 74 | entry.srcFilename = filename ? filename : ""; | ||
| 75 | entry.function = functionName ? functionName : ""; | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | static int | ||
| 80 | _sup_positional_info( const SafeString & path | ||
| 81 | , bfd_vma addr | ||
| 82 | , StackRecord & entry ) { | ||
| 83 | // see: std::string addr2str(std::string file_name, bfd_vma addr) | ||
| 84 | #if defined(BFD_FOUND) && BFD_FOUND | ||
| 85 | bfd * abfd; | ||
| 86 | abfd = bfd_openr(path.c_str(), NULL); { | ||
| 87 | char ** matching; // TODO: clear? | ||
| 88 | if( !abfd ) { | ||
| 89 | entry.failure = "Couln't open " + path + "."; return -1; | ||
| 90 | } if( bfd_check_format(abfd, bfd_archive) ) { | ||
| 91 | entry.failure = "File " + path + " is not a BFD-compliant archive."; return -1; | ||
| 92 | } if(!bfd_check_format_matches(abfd, bfd_object, &matching)) { | ||
| 93 | entry.failure = "File " + path + " has not a BFD-compliant archive format."; return -1; | ||
| 94 | } if((bfd_get_file_flags(abfd) & HAS_SYMS) != 0) { | ||
| 95 | unsigned int symbolSize; | ||
| 96 | void ** sTablePtr = (void**) &(entry.sTable); | ||
| 97 | long nSymbols = bfd_read_minisymbols( abfd, false, | ||
| 98 | sTablePtr, &symbolSize ); | ||
| 99 | |||
| 100 | if( !nSymbols ) { | ||
| 101 | // If the bfd_read_minisymbols() already allocated the table, we need | ||
| 102 | // to free it first: | ||
| 103 | if( entry.sTable != NULL ) | ||
| 104 | free( entry.sTable ); | ||
| 105 | // dynamic | ||
| 106 | nSymbols = bfd_read_minisymbols( abfd, true, | ||
| 107 | sTablePtr, &symbolSize ); | ||
| 108 | } else if( nSymbols < 0 ) { | ||
| 109 | entry.failure = "bfd_read_minisymbols() failed."; | ||
| 110 | return -1; | ||
| 111 | } | ||
| 112 | } else { | ||
| 113 | entry.failure = "BFD archive " + path + " has no symbols."; | ||
| 114 | } | ||
| 115 | bfd_map_over_sections(abfd, _seek_entry_in_section, &entry); | ||
| 116 | } bfd_close(abfd); | ||
| 117 | if( entry.sTable ) { free(entry.sTable); } | ||
| 118 | # endif | ||
| 119 | |||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | #endif // defined(BFD_FOUND) && BFD_FOUND | ||
| 123 | |||
| 124 | static int | ||
| 125 | _so_lib_callback( struct dl_phdr_info *info | ||
| 126 | , size_t size | ||
| 127 | , void * _data | ||
| 128 | ) { | ||
| 129 | struct StackRecord * data = (struct StackRecord *) _data; | ||
| 130 | for( int i=0; i < info->dlpi_phnum; i++ ) { | ||
| 131 | if( info->dlpi_phdr[i].p_type == PT_LOAD ) { | ||
| 132 | ElfW(Addr) min_addr = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr; | ||
| 133 | ElfW(Addr) max_addr = min_addr + info->dlpi_phdr[i].p_memsz; | ||
| 134 | if ((data->addr >= min_addr) && (data->addr < max_addr)) { | ||
| 135 | data->elfFilename = info->dlpi_name; | ||
| 136 | data->soLibAddr = data->addr - info->dlpi_addr; | ||
| 137 | // We found a match, return a non-zero value | ||
| 138 | return 1; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | // We didn't find a match, return a zero value | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | static int | ||
| 147 | _supplement_stacktrace( StackRecords & target, size_t n ) { | ||
| 148 | #if defined(BFD_FOUND) && BFD_FOUND | ||
| 149 | bfd_init(); | ||
| 150 | #endif | ||
| 151 | for( auto it = target.begin(); it != target.end(); ++it ) { | ||
| 152 | if( dl_iterate_phdr( _so_lib_callback, &(*it) ) == 0) { | ||
| 153 | it->failure = "dl_iterate_phdr() failed."; | ||
| 154 | return -1; | ||
| 155 | } | ||
| 156 | if( it->elfFilename.length() > 0 ) { | ||
| 157 | // This happens for shared libraries. | ||
| 158 | // 'it->elfFilename' then contains the full path to the .so library. | ||
| 159 | _sup_positional_info(it->elfFilename, it->soLibAddr, *it); | ||
| 160 | } else { | ||
| 161 | // The 'addr_in_file' is from the current executable binary. | ||
| 162 | // It can be debugger (gdb, valgrind, etc.), so: | ||
| 163 | //pid_t pid = getpid(); | ||
| 164 | //char bf[32]; | ||
| 165 | //snprintf( bf, 32, "/proc/%d/exe", pid ); (TODO: not work) | ||
| 166 | _sup_positional_info( "/proc/self/exe", it->soLibAddr, *it); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | return 0; | ||
| 170 | } | ||
| 171 | |||
| 172 | /**@brief Obtains stacktrace info using binary file descriptor library library. | ||
| 173 | * 0 -- ok | ||
| 174 | */ | ||
| 175 | int obtain_stacktrace( StackRecords & target ) { | ||
| 176 | target.clear(); | ||
| 177 | void * stackPointers[MAX_STACK_DEPTH_N]; | ||
| 178 | const size_t size = backtrace( | ||
| 179 | stackPointers, | ||
| 180 | MAX_STACK_DEPTH_N ); | ||
| 181 | for( size_t i = 1; i < size; ++i ) { // Note: hides own entries | ||
| 182 | StackRecord entry { | ||
| 183 | # if 0 /* unsupported by GCC */ | ||
| 184 | .addr = (bfd_vma) stackPointers[i], | ||
| 185 | .soLibAddr = 0, .lFound = 0, .lineno = 0, | ||
| 186 | "", "", "", "", | ||
| 187 | # else | ||
| 188 | 0, 0, | ||
| 189 | "", "", "", "", | ||
| 190 | # endif | ||
| 191 | #if defined(BFD_FOUND) && BFD_FOUND | ||
| 192 | (bfd_vma) stackPointers[i], | ||
| 193 | 0, | ||
| 194 | 0, | ||
| 195 | # endif | ||
| 196 | }; | ||
| 197 | target.push_front( entry ); | ||
| 198 | } | ||
| 199 | return _supplement_stacktrace( target, size ); | ||
| 200 | } | ||
| 201 | |||
| 202 | SafeString demangle_class( const char * classname ) { | ||
| 203 | if( (!classname) || (*classname == '\0') ) { | ||
| 204 | return "<null>"; | ||
| 205 | } else { | ||
| 206 | int status = -4; | ||
| 207 | char *d = 0; | ||
| 208 | d = abi::__cxa_demangle(classname, 0, 0, &status); | ||
| 209 | if( d ) { | ||
| 210 | SafeString s(d); | ||
| 211 | free(d); | ||
| 212 | if( s.size() > 0 ) { | ||
| 213 | return s; | ||
| 214 | } else { | ||
| 215 | return classname; | ||
| 216 | } | ||
| 217 | } else { | ||
| 218 | return classname; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | SafeString demangle_function( const SafeString & name ) { | ||
| 224 | SafeString s; | ||
| 225 | if( name.length() == 0 ) { | ||
| 226 | s = "??"; | ||
| 227 | } else { | ||
| 228 | int status = 0; | ||
| 229 | char *d = 0; | ||
| 230 | d = abi::__cxa_demangle(name.c_str(), 0, 0, &status); | ||
| 231 | if (d) { | ||
| 232 | s = d; | ||
| 233 | free(d); | ||
| 234 | if (s.size() > 0) { | ||
| 235 | if (s[s.size()-1] != ')') { | ||
| 236 | // This means that the C++ demangler failed for some | ||
| 237 | // reason, as each symbol needs to end with ")". We | ||
| 238 | // just return the original version: | ||
| 239 | s = name + "()"; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } else { | ||
| 243 | s = name + "()"; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | return s; | ||
| 248 | } | ||
| 249 | |||
| 250 | static void | ||
| 251 | _fancy_print_single(std::ostream& os, const StackRecord & t) { | ||
| 252 | const static char * ESC_BLDRED = "\033[1;31m" | ||
| 253 | , * ESC_CLRCLEAR = "\033[0m" | ||
| 254 | , * ESC_CLRGREEN = "\033[0;33m" | ||
| 255 | , * ESC_BLDGREEN = "\033[1;33m" | ||
| 256 | , * ESC_BLDWHITE = "\033[1m" | ||
| 257 | ; | ||
| 258 | if( !t.failure.empty() ) os << " / " | ||
| 259 | << ESC_BLDRED << t.failure | ||
| 260 | << ESC_CLRCLEAR << " / "; | ||
| 261 | if( !t.elfFilename.empty() ) os << t.elfFilename; | ||
| 262 | else os << "??"; | ||
| 263 | os << " / "; | ||
| 264 | os << "0x" << std::hex << std::setw(8) << std::setfill('0') << t.addr << " " | ||
| 265 | << "0x" << std::hex << std::setw(8) << std::setfill('0') << t.soLibAddr << " " | ||
| 266 | << std::dec; | ||
| 267 | if( !t.srcFilename.empty() ) { | ||
| 268 | os << "\n\t\tat " | ||
| 269 | << ESC_CLRGREEN << t.srcFilename << ESC_CLRCLEAR << ":" | ||
| 270 | << ESC_BLDGREEN << t.lineno << ESC_CLRCLEAR << ","; | ||
| 271 | } | ||
| 272 | os << "\n\t\tfunction " << ESC_BLDWHITE << demangle_function( t.function ) << ESC_CLRCLEAR | ||
| 273 | ; | ||
| 274 | } | ||
| 275 | |||
| 276 | void | ||
| 277 | fancy_print( std::ostream & os, const StackRecords & recs) { | ||
| 278 | for( const auto & rec : recs ) { | ||
| 279 | _fancy_print_single(os, rec); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | #endif // defined(NA64DP_ENABLE_EXC_STACKTRACE) && NA64DP_ENABLE_EXC_STACKTRACE | ||
| 284 | |||
| 285 | 47 | GenericRuntimeError::GenericRuntimeError( const char * s ) throw() | |
| 286 | 47 | : std::runtime_error( s ) { | |
| 287 | #if defined(NA64DP_ENABLE_EXC_STACKTRACE) && NA64DP_ENABLE_EXC_STACKTRACE | ||
| 288 | obtain_stacktrace(_stackRecords); | ||
| 289 | #endif | ||
| 290 | 47 | } | |
| 291 | |||
| 292 | } // namespace ::na64dp::errors | ||
| 293 | } // namespace na64dp | ||
| 294 | |||
| 295 |