示例#1
0
static void LogStacktrace(const int logLevel, StackTrace& stacktrace)
{
	int colFileline = 0;
	const std::string& exe_path = Platform::GetProcessExecutablePath();
	const std::string& cwd_path = Platform::GetOrigCWD();
	for (auto fit = stacktrace.begin(); fit != stacktrace.end(); fit++) {
        for (auto eit = fit->entries.begin(); eit != fit->entries.end(); eit++) {
			eit->abbrev_funcname = eit->funcname;
			std::string fileline = eit->fileline;
			if (fileline[1] == '?') {  // case "??:?", ":?"
				fileline = fit->symbol;  // print raw backtrace_symbol
			}
			eit->abbrev_fileline = fileline;
			int abbrev_start = 0;
			if (fileline[0] == '/') { // See if we can shorten the file/line bit by removing the common path
				if (CommonStringLength(fileline, exe_path, &abbrev_start) > 1) { // i.e. one char for first '/'
					eit->abbrev_fileline = std::string(".../") + fileline.substr(abbrev_start, std::string::npos);
				} else if (CommonStringLength(fileline, cwd_path, &abbrev_start) > 1) {
					eit->abbrev_fileline = std::string("./") + fileline.substr(abbrev_start, std::string::npos);
				}
			}

			if (eit->abbrev_funcname.size() > 100) {
				eit->abbrev_funcname.resize(94);
				eit->abbrev_funcname.append(" [...]");
			}

			colFileline = std::max(colFileline, (int)eit->abbrev_fileline.length());
        }
    }

    bool hideSignalHandler = true;

    // Print out the translated StackTrace
    unsigned numLine = 0;
    unsigned hiddenLines = 0;
    while (numLine == 0) { // outer loop at most twice -- tries to find the signal handler once and if that doesn't work, then just print every frame
        for (auto fit = stacktrace.cbegin(); fit != stacktrace.cend(); fit++) {
            for (auto eit = fit->entries.begin(); eit != fit->entries.end(); eit++) {

                if (hideSignalHandler) {
                    hiddenLines++;
					if (eit->fileline.find("sigaction.c:?") == 0) {
                        hideSignalHandler = false;
                        LOG_I(logLevel, "(Signal handler calls suppressed [%d]. Inlined calls denoted by < > brackets.)", hiddenLines);
                    }
                    continue;
                }

                if (eit->inLine) {
					LOG_I(logLevel, "  <%02u> %*s  %s", fit->level, colFileline, eit->abbrev_fileline.c_str(), eit->abbrev_funcname.c_str());
                } else {
					LOG_I(logLevel, "[%02u]   %*s  %s", fit->level, colFileline, eit->abbrev_fileline.c_str(), eit->abbrev_funcname.c_str());
                }
                numLine++;
            }
        }
        hideSignalHandler = false;
    }
}
示例#2
0
/**
 * @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;
}