/** * @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; }
static void TranslateStackTrace(std::vector<std::string>* lines, const std::vector< std::pair<std::string,uintptr_t> >& paths) { // 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; } // Detect BaseMemoryAddresses of all Lib's found in the stacktrace std::map<std::string,uintptr_t> binPath_baseMemAddr; for (std::vector< std::pair<std::string,uintptr_t> >::const_iterator it = paths.begin(); it != paths.end(); ++it) { binPath_baseMemAddr[it->first] = 0; } FindBaseMemoryAddresses(binPath_baseMemAddr); // Finally translate it for (std::map<std::string,uintptr_t>::const_iterator it = binPath_baseMemAddr.begin(); it != binPath_baseMemAddr.end(); ++it) { const std::string& libName = it->first; const uintptr_t& libAddr = it->second; const std::string symbolFile = LocateSymbolFile(libName); std::ostringstream buf; buf << ADDR2LINE << " --exe=\"" << symbolFile << "\""; // insert requested addresses that should be translated by addr2line std::queue<size_t> indices; for (size_t i = 0; i < paths.size(); ++i) { const std::pair<std::string,uintptr_t>& pt = paths[i]; if (pt.first == libName) { // Put it twice in the queue. // Depending on sys, compilation & lobby settings the libaddr doesn't need to be cut! // The detection of these situations is more complexe than just dropping the line that fails // (likely only one of the addresses will give something unequal to "??:0"). buf << " " << std::hex << pt.second; indices.push(i); buf << " " << std::hex << (pt.second - libAddr); indices.push(i); } } // execute command addr2line, read stdout and write to log-file buf << " 2>/dev/null"; // hide error output from spring's pipe FILE* cmdOut = popen(buf.str().c_str(), "r"); if (cmdOut != NULL) { const size_t line_sizeMax = 2048; char line[line_sizeMax]; while (fgets(line, line_sizeMax, cmdOut) != NULL) { const size_t i = indices.front(); indices.pop(); if (strcmp(line,"??:0\n") != 0) { (*lines)[i] = std::string(line, strlen(line) - 1); // exclude the line-ending } } pclose(cmdOut); } } }
static void TranslateStackTrace(std::vector<std::string>* lines, const std::vector< std::pair<std::string,uintptr_t> >& paths) { //! 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!"); logOutput.Flush(); return; } //! Detect BaseMemoryAddresses of all Lib's found in the stacktrace std::map<std::string,uintptr_t> binPath_baseMemAddr; for (std::vector< std::pair<std::string,uintptr_t> >::const_iterator it = paths.begin(); it != paths.end(); ++it) { binPath_baseMemAddr[it->first] = 0; } FindBaseMemoryAddresses(binPath_baseMemAddr); //! Finally translate it for (std::map<std::string,uintptr_t>::const_iterator it = binPath_baseMemAddr.begin(); it != binPath_baseMemAddr.end(); ++it) { const std::string symbolFile = LocateSymbolFile(it->first); std::ostringstream buf; buf << "addr2line " << "--exe=\"" << symbolFile << "\""; const uintptr_t& baseLibAddr = it->second; //! insert requested addresses that should be translated by addr2line std::queue<size_t> indices; for (size_t i = 0; i < paths.size(); ++i) { const std::pair<std::string,uintptr_t>& pt = paths[i]; if (pt.first == it->first) { buf << " " << std::hex << (pt.second - baseLibAddr); indices.push(i); } } //! execute command addr2line, read stdout and write to log-file buf << " 2>/dev/null"; //! hide error output from spring's pipe FILE* cmdOut = popen(buf.str().c_str(), "r"); if (cmdOut != NULL) { const size_t line_sizeMax = 2048; char line[line_sizeMax]; while (fgets(line, line_sizeMax, cmdOut) != NULL) { const size_t i = indices.front(); indices.pop(); if (strcmp(line,"??:0\n") != 0) { (*lines)[i] = std::string(line); } } pclose(cmdOut); } } }