static void Stacktrace(bool* aiCrash, pthread_t* hThread = NULL, const char* threadName = NULL, const int logLevel = LOG_LEVEL_ERROR) { #ifndef DEDICATED Watchdog::ClearTimer(); #endif if (threadName != NULL) { LOG_I(logLevel, "Stacktrace (%s) for Spring %s:", threadName, (SpringVersion::GetFull()).c_str()); } else { LOG_I(logLevel, "Stacktrace for Spring %s:", (SpringVersion::GetFull()).c_str()); } bool containsDriverSo = false; // OpenGL lib -> graphic problem bool containedAIInterfaceSo = false; bool containedSkirmishAISo = false; std::vector<std::string> stacktrace; std::vector< std::pair<std::string,uintptr_t> > symbols; // Get untranslated stacktrace symbols { // process and analyse the raw stack trace std::vector<void*> buffer(MAX_STACKTRACE_DEPTH + 2); int numLines; if (hThread && Threading::GetCurrentThread() != *hThread) { LOG_I(logLevel, " (Note: This stacktrace is not 100%% accurate! It just gives an impression.)"); LOG_CLEANUP(); numLines = thread_backtrace(*hThread, &buffer[0], buffer.size()); // stack pointers } else { numLines = backtrace(&buffer[0], buffer.size()); // stack pointers buffer.erase(buffer.begin()); // pop Stacktrace() buffer.erase(buffer.begin()); // pop HandleSignal() numLines -= 2; } numLines = (numLines > MAX_STACKTRACE_DEPTH) ? MAX_STACKTRACE_DEPTH : numLines; char** lines = backtrace_symbols(&buffer[0], numLines); // give them meaningfull names stacktrace.reserve(numLines); for (int l = 0; l < numLines; l++) { stacktrace.push_back(lines[l]); } free(lines); } if (stacktrace.empty()) { LOG_I(logLevel, " Unable to create stacktrace"); return; } // Extract important data from backtrace_symbols' output { for (std::vector<std::string>::iterator it = stacktrace.begin(); it != stacktrace.end(); ++it) { std::pair<std::string,uintptr_t> data; // prepare for TranslateStackTrace() const std::string path = ExtractPath(*it); const std::string absPath = CreateAbsolutePath(path); data.first = absPath; data.second = ExtractAddr(*it); symbols.push_back(data); // check if there are known sources of fail on the stack containsDriverSo = (containsDriverSo || (path.find("libGLcore.so") != std::string::npos)); containsDriverSo = (containsDriverSo || (path.find("psb_dri.so") != std::string::npos)); containsDriverSo = (containsDriverSo || (path.find("i965_dri.so") != std::string::npos)); containsDriverSo = (containsDriverSo || (path.find("fglrx_dri.so") != std::string::npos)); if (!containedAIInterfaceSo && (absPath.find("Interfaces") != std::string::npos)) { containedAIInterfaceSo = true; } if (!containedSkirmishAISo && (absPath.find("Skirmish") != std::string::npos)) { containedSkirmishAISo = true; } } // Linux Graphic drivers are known to fail with moderate OpenGL usage if (containsDriverSo) { LOG_I(logLevel, "This stack trace indicates a problem with your graphic card driver. " "Please try upgrading or downgrading it. " "Specifically recommended is the latest driver, and one that is as old as your graphic card. " "Also try lower graphic details and disabling Lua widgets in spring-settings.\n"); } // if stack trace contains AI and AI Interface frames, // it is very likely that the problem lies in the AI only if (containedSkirmishAISo) { containedAIInterfaceSo = false; } if (containedAIInterfaceSo) { LOG_I(logLevel, "This stack trace indicates a problem with an AI Interface library."); if (aiCrash) *aiCrash = true; } if (containedSkirmishAISo) { LOG_I(logLevel, "This stack trace indicates a problem with a Skirmish AI library."); if (aiCrash) *aiCrash = true; } LOG_CLEANUP(); } // Translate it TranslateStackTrace(&stacktrace, symbols); // Print out the translated StackTrace unsigned numLine = 0; for (std::vector<std::string>::iterator it = stacktrace.begin(); it != stacktrace.end(); ++it) { LOG_I(logLevel, " <%u> %s", numLine++, it->c_str()); } }
/** * @brief TranslateStackTrace * @param stacktrace These are the lines and addresses produced by backtrace_symbols() * Translates the module and address information from backtrace symbols into a vector of StackFrame objects, * each with its own set of entries representing the function call and any inlined functions for that call. */ static void TranslateStackTrace(bool* aiCrash, StackTrace& stacktrace, const int logLevel) { // Extract important data from backtrace_symbols' output bool containsDriverSo = false; // OpenGL lib -> graphic problem bool containsAIInterfaceSo = false; bool containsSkirmishAISo = false; LOG_L(L_DEBUG, "TranslateStackTrace[1]"); for (auto it = stacktrace.begin(); it != stacktrace.end(); ++it) { // prepare for addr2line() const std::string path = ExtractPath(it->symbol); const std::string absPath = CreateAbsolutePath(path); it->path = absPath; it->addr = ExtractAddr(*it); LOG_L(L_DEBUG, "symbol = \"%s\", path = \"%s\", absPath = \"%s\", addr = 0x%lx", it->symbol.c_str(), path.c_str(), absPath.c_str(), it->addr); // check if there are known sources of fail on the stack containsDriverSo = (containsDriverSo || (path.find("libGLcore.so") != std::string::npos)); containsDriverSo = (containsDriverSo || (path.find("psb_dri.so") != std::string::npos)); containsDriverSo = (containsDriverSo || (path.find("i965_dri.so") != std::string::npos)); containsDriverSo = (containsDriverSo || (path.find("fglrx_dri.so") != std::string::npos)); if (!containsAIInterfaceSo && (absPath.find("Interfaces") != std::string::npos)) { containsAIInterfaceSo = true; } if (!containsSkirmishAISo && (absPath.find("Skirmish") != std::string::npos)) { containsSkirmishAISo = true; } } LOG_L(L_DEBUG, "TranslateStackTrace[2]"); // Linux Graphic drivers are known to fail with moderate OpenGL usage if (containsDriverSo) { LOG_I(logLevel, "This stack trace indicates a problem with your graphic card driver. " "Please try upgrading or downgrading it. " "Specifically recommended is the latest driver, and one that is as old as your graphic card. " "Also try lower graphic details and disabling Lua widgets in spring-settings.\n"); } // if stack trace contains AI and AI Interface frames, // it is very likely that the problem lies in the AI only if (containsSkirmishAISo) { containsAIInterfaceSo = false; } if (containsAIInterfaceSo) { LOG_I(logLevel, "This stack trace indicates a problem with an AI Interface library."); if (aiCrash) *aiCrash = true; } if (containsSkirmishAISo) { LOG_I(logLevel, "This stack trace indicates a problem with a Skirmish AI library."); if (aiCrash) *aiCrash = true; } LOG_L(L_DEBUG, "TranslateStackTrace[3]"); // Check if addr2line is available static int addr2line_found = -1; if (addr2line_found < 0) { FILE* cmdOut = popen(ADDR2LINE " --help", "r"); if (cmdOut == NULL) { addr2line_found = false; } else { addr2line_found = true; pclose(cmdOut); } } if (!addr2line_found) { LOG_L(L_WARNING, " addr2line not found!"); return; } LOG_L(L_DEBUG, "TranslateStackTrace[4]"); // Detect BaseMemoryAddresses of all Lib's found in the stacktrace std::map<std::string,uintptr_t> binPath_baseMemAddr; for (auto it = stacktrace.cbegin(); it != stacktrace.cend(); it++) { binPath_baseMemAddr[it->path] = 0; } FindBaseMemoryAddresses(binPath_baseMemAddr); LOG_L(L_DEBUG, "TranslateStackTrace[5]"); // Finally translate it: // This is nested so that the outer loop covers all the entries for one library -- this means fewer addr2line calls. for (auto it = binPath_baseMemAddr.cbegin(); it != binPath_baseMemAddr.cend(); it++) { const std::string& modulePath = it->first; //const uintptr_t& moduleAddr = it->second; const std::string symbolFile = LocateSymbolFile(modulePath); LOG_L(L_DEBUG, "modulePath: %s, symbolFile: %s", modulePath.c_str(), symbolFile.c_str()); std::ostringstream buf; buf << ADDR2LINE << " -i -a -f -C --exe=\"" << symbolFile << "\""; // insert requested addresses that should be translated by addr2line std::queue<size_t> indices; int i=0; for (auto fit = stacktrace.cbegin(); fit != stacktrace.cend(); fit++) { if (fit->path == modulePath) { buf << " " << std::hex << fit->addr; indices.push(i); } i++; } // execute command addr2line, read stdout and write to log-file buf << " 2>/dev/null"; // hide error output from spring's pipe LOG_L(L_DEBUG, "> %s", buf.str().c_str()); FILE* cmdOut = popen(buf.str().c_str(), "r"); if (cmdOut != NULL) { const size_t maxLength = 2048; char line[maxLength]; char* res = fgets_addr2line(line, maxLength, cmdOut); while (res != NULL) { i = indices.front(); indices.pop(); // First scan the address and ensure that it matches the one we were expecting uintptr_t parsedAddr = 0; int matched = sscanf(line, "0x%lx", &parsedAddr); if (matched != 1 || parsedAddr != stacktrace[i].addr) { LOG_L(L_WARNING, "Mismatched address received from addr2line: 0x%lx != 0x%lx", parsedAddr, stacktrace[i].addr); break; } res = fgets_addr2line(line, maxLength, cmdOut); while (res != NULL) { if (line[0] == '0' && line[1] == 'x') { break; // This is the start of a new address } StackFunction entry; entry.inLine = true; // marked false later if the last entry std::string funcname = std::string(line); entry.funcname = funcname.substr(0, funcname.rfind(" (discriminator")); if (fgets_addr2line(line, maxLength, cmdOut) == nullptr) { break; } entry.fileline = std::string(line); stacktrace[i].entries.push_back(entry); res = fgets_addr2line(line, maxLength, cmdOut); } if (!stacktrace[i].entries.empty()) { // Ensure that the last entry in a sequence of results is marked as "not inlined" stacktrace[i].entries.rbegin()->inLine = false; } } pclose(cmdOut); } } LOG_L(L_DEBUG, "TranslateStackTrace[6]"); return; }