2023-02-19 14:45:56 +01:00
|
|
|
#include "CrashReporter.hpp"
|
|
|
|
#include <random>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <fstream>
|
2023-03-01 16:08:44 +01:00
|
|
|
#include <signal.h>
|
2024-02-07 10:50:23 +01:00
|
|
|
#include <link.h>
|
2023-02-19 14:45:56 +01:00
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
#include "../plugins/PluginSystem.hpp"
|
|
|
|
|
2023-02-20 15:15:15 +01:00
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2023-02-19 14:45:56 +01:00
|
|
|
std::string getRandomMessage() {
|
|
|
|
|
|
|
|
const std::vector<std::string> MESSAGES = {"Sorry, didn't mean to...",
|
2023-12-06 23:54:56 +01:00
|
|
|
"This was an accident, I swear!",
|
|
|
|
"Calm down, it was a misinput! MISINPUT!",
|
|
|
|
"Oops",
|
|
|
|
"Vaxry is going to be upset.",
|
|
|
|
"Who tried dividing by zero?!",
|
|
|
|
"Maybe you should try dusting your PC in the meantime?",
|
|
|
|
"I tried so hard, and got so far...",
|
|
|
|
"I don't feel so good...",
|
|
|
|
"*thud*",
|
|
|
|
"Well this is awkward.",
|
|
|
|
"\"stable\"",
|
|
|
|
"I hope you didn't have any unsaved progress.",
|
|
|
|
"All these computers..."};
|
2023-02-19 14:45:56 +01:00
|
|
|
|
|
|
|
std::random_device dev;
|
|
|
|
std::mt19937 engine(dev());
|
|
|
|
std::uniform_int_distribution<> distribution(0, MESSAGES.size() - 1);
|
|
|
|
|
|
|
|
return MESSAGES[distribution(engine)];
|
|
|
|
}
|
|
|
|
|
2023-03-01 16:08:44 +01:00
|
|
|
void CrashReporter::createAndSaveCrash(int sig) {
|
2023-02-19 14:45:56 +01:00
|
|
|
|
|
|
|
// get the backtrace
|
|
|
|
const int PID = getpid();
|
|
|
|
|
|
|
|
std::string finalCrashReport = "";
|
|
|
|
|
2023-02-19 14:51:40 +01:00
|
|
|
finalCrashReport += "--------------------------------------------\n Hyprland Crash Report\n--------------------------------------------\n";
|
2023-02-19 14:45:56 +01:00
|
|
|
finalCrashReport += getRandomMessage() + "\n\n";
|
|
|
|
|
2023-09-20 09:26:20 +02:00
|
|
|
finalCrashReport += std::format("Hyprland received signal {} ({})\n\n", sig, (const char*)strsignal(sig));
|
2023-02-19 14:45:56 +01:00
|
|
|
|
2023-09-20 09:26:20 +02:00
|
|
|
finalCrashReport += std::format("Version: {}\nTag: {}\n\n", GIT_COMMIT_HASH, GIT_TAG);
|
2023-03-17 12:50:31 +01:00
|
|
|
|
2023-04-15 11:58:46 +02:00
|
|
|
if (g_pPluginSystem && !g_pPluginSystem->getAllPlugins().empty()) {
|
2023-02-27 13:32:38 +01:00
|
|
|
finalCrashReport += "Hyprland seems to be running with plugins. This crash might not be Hyprland's fault.\nPlugins:\n";
|
|
|
|
|
|
|
|
for (auto& p : g_pPluginSystem->getAllPlugins()) {
|
2023-09-20 09:26:20 +02:00
|
|
|
finalCrashReport += std::format("\t{} ({}) {}\n", p->name, p->author, p->version);
|
2023-02-27 13:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
finalCrashReport += "\n\n";
|
|
|
|
}
|
|
|
|
|
2023-02-19 14:45:56 +01:00
|
|
|
finalCrashReport += "System info:\n";
|
|
|
|
|
|
|
|
struct utsname unameInfo;
|
|
|
|
uname(&unameInfo);
|
|
|
|
|
2023-12-19 00:33:10 +01:00
|
|
|
finalCrashReport += std::format("\tSystem name: {}\n\tNode name: {}\n\tRelease: {}\n\tVersion: {}\n\n", std::string{unameInfo.sysname}, std::string{unameInfo.nodename},
|
|
|
|
std::string{unameInfo.release}, std::string{unameInfo.version});
|
2023-02-19 14:45:56 +01:00
|
|
|
|
2023-02-20 15:15:15 +01:00
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__)
|
|
|
|
const std::string GPUINFO = execAndGet("pciconf -lv | fgrep -A4 vga");
|
|
|
|
#else
|
2023-02-19 14:45:56 +01:00
|
|
|
const std::string GPUINFO = execAndGet("lspci -vnn | grep VGA");
|
2023-02-20 15:15:15 +01:00
|
|
|
#endif
|
2023-02-19 14:45:56 +01:00
|
|
|
|
|
|
|
finalCrashReport += "GPU:\n\t" + GPUINFO;
|
|
|
|
|
2023-09-20 09:26:20 +02:00
|
|
|
finalCrashReport += std::format("\n\nos-release:\n\t{}\n\n\n", replaceInString(execAndGet("cat /etc/os-release"), "\n", "\n\t"));
|
2023-02-19 14:45:56 +01:00
|
|
|
|
|
|
|
finalCrashReport += "Backtrace:\n";
|
|
|
|
|
2023-08-11 17:22:16 +02:00
|
|
|
const auto CALLSTACK = getBacktrace();
|
2023-02-20 15:15:15 +01:00
|
|
|
|
|
|
|
#if defined(KERN_PROC_PATHNAME)
|
|
|
|
int mib[] = {
|
2023-03-01 10:32:31 +01:00
|
|
|
CTL_KERN,
|
2023-02-20 15:15:15 +01:00
|
|
|
#if defined(__NetBSD__)
|
2023-03-01 10:32:31 +01:00
|
|
|
KERN_PROC_ARGS,
|
|
|
|
-1,
|
|
|
|
KERN_PROC_PATHNAME,
|
2023-02-20 15:15:15 +01:00
|
|
|
#else
|
2023-03-01 10:32:31 +01:00
|
|
|
KERN_PROC,
|
|
|
|
KERN_PROC_PATHNAME,
|
|
|
|
-1,
|
2023-02-20 15:15:15 +01:00
|
|
|
#endif
|
|
|
|
};
|
2023-03-01 10:32:31 +01:00
|
|
|
u_int miblen = sizeof(mib) / sizeof(mib[0]);
|
|
|
|
char exe[PATH_MAX] = "";
|
|
|
|
size_t sz = sizeof(exe);
|
2023-02-20 15:15:15 +01:00
|
|
|
sysctl(mib, miblen, &exe, &sz, NULL, 0);
|
|
|
|
const auto FPATH = std::filesystem::canonical(exe);
|
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
// Neither KERN_PROC_PATHNAME nor /proc are supported
|
|
|
|
const auto FPATH = std::filesystem::canonical("/usr/local/bin/Hyprland");
|
|
|
|
#else
|
2023-02-20 11:28:16 +01:00
|
|
|
const auto FPATH = std::filesystem::canonical("/proc/self/exe");
|
2023-02-20 15:15:15 +01:00
|
|
|
#endif
|
2023-02-19 14:45:56 +01:00
|
|
|
|
2024-02-07 10:50:23 +01:00
|
|
|
std::string addrs = "";
|
2023-08-11 17:22:16 +02:00
|
|
|
for (size_t i = 0; i < CALLSTACK.size(); ++i) {
|
2024-03-05 23:51:34 +01:00
|
|
|
#ifdef __GLIBC__
|
2024-02-07 10:50:23 +01:00
|
|
|
// convert in memory address to VMA address
|
|
|
|
Dl_info info;
|
|
|
|
struct link_map* linkMap;
|
|
|
|
dladdr1((void*)CALLSTACK[i].adr, &info, (void**)&linkMap, RTLD_DL_LINKMAP);
|
|
|
|
size_t vmaAddr = (size_t)CALLSTACK[i].adr - linkMap->l_addr;
|
2024-03-05 23:51:34 +01:00
|
|
|
#else
|
|
|
|
// musl doesn't define dladdr1
|
|
|
|
size_t vmaAddr = (size_t)CALLSTACK[i].adr;
|
|
|
|
#endif
|
2023-02-19 14:45:56 +01:00
|
|
|
|
2024-02-07 10:50:23 +01:00
|
|
|
addrs += std::format("0x{:x} ", vmaAddr);
|
|
|
|
}
|
2023-02-20 15:15:15 +01:00
|
|
|
#ifdef __clang__
|
2024-02-07 10:50:23 +01:00
|
|
|
const auto CMD = std::format("llvm-addr2line -e {} -Cf {}", FPATH.c_str(), addrs);
|
2023-02-20 15:15:15 +01:00
|
|
|
#else
|
2024-03-05 23:51:34 +01:00
|
|
|
const auto CMD = std::format("addr2line -e {} -Cf {}", FPATH.c_str(), addrs);
|
2023-02-20 15:15:15 +01:00
|
|
|
#endif
|
2024-02-07 10:50:23 +01:00
|
|
|
|
|
|
|
const auto ADDR2LINE = execAndGet(CMD.c_str());
|
|
|
|
|
|
|
|
std::stringstream ssin(ADDR2LINE);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < CALLSTACK.size(); ++i) {
|
|
|
|
finalCrashReport += std::format("\t#{} | {}", i, CALLSTACK[i].desc);
|
|
|
|
std::string functionInfo;
|
|
|
|
std::string fileLineInfo;
|
|
|
|
std::getline(ssin, functionInfo);
|
|
|
|
std::getline(ssin, fileLineInfo);
|
|
|
|
finalCrashReport += std::format("\n\t\t{}\n\t\t{}\n", functionInfo, fileLineInfo);
|
2023-02-19 14:45:56 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 16:14:35 +01:00
|
|
|
finalCrashReport += "\n\nLog tail:\n";
|
|
|
|
|
2024-01-20 09:16:27 +01:00
|
|
|
finalCrashReport += Debug::rollingLog.substr(Debug::rollingLog.find("\n") + 1);
|
2023-03-01 16:14:35 +01:00
|
|
|
|
2023-04-15 11:58:46 +02:00
|
|
|
const auto HOME = getenv("HOME");
|
2023-03-31 14:15:24 +02:00
|
|
|
const auto CACHE_HOME = getenv("XDG_CACHE_HOME");
|
2023-02-19 14:45:56 +01:00
|
|
|
|
|
|
|
if (!HOME)
|
|
|
|
return;
|
|
|
|
|
2023-03-31 14:15:24 +02:00
|
|
|
std::ofstream ofs;
|
2024-02-20 01:55:04 +01:00
|
|
|
std::string reportDir;
|
2023-03-31 14:15:24 +02:00
|
|
|
|
2024-02-20 01:55:04 +01:00
|
|
|
if (!CACHE_HOME || std::string(CACHE_HOME).empty())
|
|
|
|
reportDir = std::string(HOME) + "/.cache/hyprland";
|
|
|
|
else
|
|
|
|
reportDir = std::string(CACHE_HOME) + "/hyprland";
|
2023-03-31 14:15:24 +02:00
|
|
|
|
2024-02-20 01:55:04 +01:00
|
|
|
if (!std::filesystem::exists(reportDir))
|
|
|
|
std::filesystem::create_directory(reportDir);
|
|
|
|
const auto path = reportDir + "/hyprlandCrashReport" + std::to_string(PID) + ".txt";
|
2023-02-19 14:51:40 +01:00
|
|
|
|
2024-02-20 01:55:04 +01:00
|
|
|
ofs.open(path, std::ios::trunc);
|
2023-02-19 14:45:56 +01:00
|
|
|
|
|
|
|
ofs << finalCrashReport;
|
|
|
|
|
|
|
|
ofs.close();
|
2023-04-21 17:48:30 +02:00
|
|
|
|
|
|
|
Debug::disableStdout = false;
|
2023-09-06 21:45:37 +02:00
|
|
|
Debug::log(CRIT, "Hyprland has crashed :( Consult the crash report at {} for more information.", path);
|
2023-03-31 14:15:24 +02:00
|
|
|
}
|