Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/engine/deploy/launch/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ static LaunchResult do_launch(const LaunchParams &params) {
// while the OverlayFS path above honored it.
auto proton = std::make_unique<ProtonRuntime>(params.platform);
proton->set_runner_override(params.proton_runner);
// Capture the child's stderr (Proton failures) into the instance cache
// so a failed launch leaves a trace the watchdog can surface.
{
std::filesystem::path cache = params.cache_dir;
if (cache.empty())
cache = capture_dir.parent_path() / "cache";
proton->set_stderr_log_path(cache / ".gmm_proton_stderr.log");
}
runtime = std::move(proton);
}
if (!runtime)
Expand Down
4 changes: 4 additions & 0 deletions src/engine/deploy/launch/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ struct LaunchParams {
// after the game exits. Empty = default Overwrite capture.
std::filesystem::path output_capture_dir;

// Instance cache directory (for stderr capture files etc.). Empty = derive
// from capture_dir/overwrite_dir.
std::filesystem::path cache_dir;

// OverlayFS mod layer directories (priority order, first = highest).
// When non-empty, the OverlayFsLauncher layers these over game_dir as
// additional read-only overlay lowerdirs, enabling mod deployment without
Expand Down
13 changes: 10 additions & 3 deletions src/platform/linux/linux_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,9 +1498,10 @@ const char *dlerror_message() {
}

std::int64_t spawn_detached_process(const std::filesystem::path &executable,
const std::vector<std::string> &argv,
const std::filesystem::path &work_dir,
bool shell_fallback) {
const std::vector<std::string> &argv,
const std::filesystem::path &work_dir,
bool shell_fallback,
const std::filesystem::path &stderr_log_path) {
if (argv.empty() || executable.empty())
return -1;

Expand All @@ -1525,6 +1526,12 @@ std::int64_t spawn_detached_process(const std::filesystem::path &executable,
::chdir(work_dir.c_str());
// stdin from the null device so the child never inherits our terminal.
std::freopen("/dev/null", "r", stdin);
// Capture stderr to a file when requested (Proton launch path), so a
// failed launch leaves a trace instead of vanishing with the detached
// process.
if (!stderr_log_path.empty()) {
std::freopen(stderr_log_path.c_str(), "w", stderr);
}
// execvp, not execv: a bare name is resolved through PATH the way the
// native launcher always has, and a path containing a separator is exec'd
// directly, which is what the Proton path needs.
Expand Down
10 changes: 7 additions & 3 deletions src/platform/macos/macos_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,10 @@ const char *dlerror_message() {
}

std::int64_t spawn_detached_process(const std::filesystem::path &executable,
const std::vector<std::string> &argv,
const std::filesystem::path &work_dir,
bool shell_fallback) {
const std::vector<std::string> &argv,
const std::filesystem::path &work_dir,
bool shell_fallback,
const std::filesystem::path &stderr_log_path) {
if (argv.empty() || executable.empty())
return -1;

Expand All @@ -631,6 +632,9 @@ std::int64_t spawn_detached_process(const std::filesystem::path &executable,
if (!work_dir.empty())
::chdir(work_dir.c_str());
std::freopen("/dev/null", "r", stdin);
if (!stderr_log_path.empty()) {
std::freopen(stderr_log_path.c_str(), "w", stderr);
}
// execvp, not execv: a bare name resolves through PATH, a path containing
// a separator is exec'd directly, which is what the Proton path needs.
::execvp(exe.c_str(), args.data());
Expand Down
3 changes: 2 additions & 1 deletion src/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ void set_thread_low_priority();
[[nodiscard]] std::int64_t
spawn_detached_process(const std::filesystem::path &executable,
const std::vector<std::string> &argv,
const std::filesystem::path &work_dir, bool shell_fallback);
const std::filesystem::path &work_dir, bool shell_fallback,
const std::filesystem::path &stderr_log_path = {});

// Set one variable in THIS process's environment, replacing whatever was there
// (setenv(key, value, 1) / SetEnvironmentVariableW). Set per variable on
Expand Down
26 changes: 20 additions & 6 deletions src/platform/windows/windows_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,10 @@ void unload_shared_library(void *handle) noexcept {
}

std::int64_t spawn_detached_process(const std::filesystem::path &executable,
const std::vector<std::string> &argv,
const std::filesystem::path &work_dir,
bool /*shell_fallback*/) {
const std::vector<std::string> &argv,
const std::filesystem::path &work_dir,
bool /*shell_fallback*/,
const std::filesystem::path &stderr_log_path) {
if (argv.empty() || executable.empty())
return -1;

Expand All @@ -799,17 +800,30 @@ std::int64_t spawn_detached_process(const std::filesystem::path &executable,
si.cb = sizeof(si);
PROCESS_INFORMATION pi{};

HANDLE stderr_handle = nullptr;
if (!stderr_log_path.empty()) {
stderr_handle = CreateFileW(stderr_log_path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (stderr_handle != INVALID_HANDLE_VALUE) {
si.hStdError = stderr_handle;
si.dwFlags |= STARTF_USESTDHANDLES;
}
}

const std::wstring wdir(work_dir.wstring());
const wchar_t *cwd = wdir.empty() ? nullptr : wdir.c_str();

// DETACHED_PROCESS so the game outlives us and does not inherit our console;
// CREATE_NEW_PROCESS_GROUP so Ctrl-C in our terminal does not reach it. The
// command line is mutable, so cmd.data() is handed over as-is.
if (!CreateProcessW(exe.c_str(), cmd.data(), nullptr, nullptr, FALSE,
DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, nullptr, cwd, &si,
&pi))
if (!CreateProcessW(exe.c_str(), cmd.data(), nullptr, nullptr, TRUE,
DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, nullptr, cwd, &si,
&pi))
return -1;

if (stderr_handle && stderr_handle != INVALID_HANDLE_VALUE)
CloseHandle(stderr_handle);

CloseHandle(pi.hThread);
const auto pid = static_cast<std::int64_t>(pi.dwProcessId);
// Closed rather than kept: nothing here waits on the child, and holding it
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ bool ProtonRuntime::launch(const std::filesystem::path &executable,
// No shell fallback: a missing or broken proton runner is a real failure the
// caller must see as one, not something to paper over by re-running the
// runner script through /bin/sh.
last_pid_ = spawn_detached_process(proton, argv, work_dir, /*shell_fallback=*/false);
last_pid_ = spawn_detached_process(proton, argv, work_dir, /*shell_fallback=*/false,
stderr_log_path_);
return last_pid_ > 0;
}

Expand Down
7 changes: 7 additions & 0 deletions src/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class ProtonRuntime : public Runtime {
void set_runner_override(const std::string &runner) { runner_override_ = runner; }
[[nodiscard]] const std::string &runner_override() const { return runner_override_; }

// Optional path for the child's stderr capture (Proton only). Empty = inherit.
void set_stderr_log_path(const std::filesystem::path &p) { stderr_log_path_ = p; }
[[nodiscard]] const std::filesystem::path &stderr_log_path() const {
return stderr_log_path_;
}

// Find the Proton binary for a game (respects per-game compat tool
// override). Returns empty path if none found. All discovery goes through
// `platform`. `runner_override`, when non-empty, wins over every
Expand All @@ -85,6 +91,7 @@ class ProtonRuntime : public Runtime {
private:
const Platform *platform_;
std::string runner_override_;
std::filesystem::path stderr_log_path_;
int64_t last_pid_ = -1;
};

Expand Down
116 changes: 74 additions & 42 deletions src/ui/controllers/launch_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ void LaunchController::on_launch_params_prepared(engine::LaunchParams lparams) {

lparams.platform = w_->platform_;
lparams.overwrite_dir = w_->overwrite_dir_path();
lparams.cache_dir = w_->cache_dir_path();

// MO2-equivalent plugin order: build + write the game's Plugins.txt (and
// the instance profile) right before launch. No-op for games without
Expand Down Expand Up @@ -869,6 +870,7 @@ void LaunchController::on_launch_params_prepared(engine::LaunchParams lparams) {
trace.end_stage("launch", true, "Overlay/staging paths ready");

trace.begin_stage("launch", "Launch executable");
w_->launch_time_ = std::filesystem::file_time_type::clock::now();
auto lresult = engine::launch_game(lparams);

if (lresult.pid <= 0) {
Expand Down Expand Up @@ -896,7 +898,6 @@ void LaunchController::on_launch_params_prepared(engine::LaunchParams lparams) {
w_->overlay_launched_ = lresult.overlay_launched;
w_->running_process_pid_ = lresult.pid;
w_->cgroup_path_ = lresult.cgroup_path;
w_->launch_time_ = std::filesystem::file_time_type::clock::now();

// P1.3 event bus: mirror MO2 onAboutToRun - emitted only once the launch
// actually succeeded (a PID exists) so a failed launch is not reported.
Expand Down Expand Up @@ -1011,26 +1012,43 @@ void LaunchController::check_running_process() {
engine::Logger::instance().debug("Watchdog: cgroup empty, game fully exited");
int supervisor_exit =
reap_supervisor(static_cast<pid_t>(w_->running_process_pid_));
// Surface the real failure to the user-visible log.
{
std::string tail;
auto log_path = w_->cache_dir_path() / ".gmm_overlay_stderr.log";
std::error_code lec;
if (std::filesystem::exists(log_path, lec)) {
std::ifstream f(log_path);
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
if (content.size() > 500)
tail = content.substr(content.size() - 500);
else
tail = content;
for (char &c : tail)
if (c == '\n' || c == '\r')
c = ' ';
}
engine::Logger::instance().error("game exited code " +
std::to_string(supervisor_exit) + ": " + tail);
}
// Surface the real failure to the user-visible log. Read both the
// overlay and proton stderr captures; only files written this run
// (mtime >= launch start) are surfaced, so a stale traceback from a
// previous run can never masquerade as this run's error. Newest wins.
{
std::string tail;
std::filesystem::path chosen;
std::filesystem::file_time_type chosen_mtime{};
for (const char *name :
{".gmm_overlay_stderr.log", ".gmm_proton_stderr.log"}) {
auto p = w_->cache_dir_path() / name;
std::error_code lec;
if (!std::filesystem::exists(p, lec))
continue;
auto mt = std::filesystem::last_write_time(p, lec);
if (lec || mt < w_->launch_time_)
continue;
if (chosen.empty() || mt > chosen_mtime) {
chosen = p;
chosen_mtime = mt;
}
}
if (!chosen.empty()) {
std::ifstream f(chosen);
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
if (content.size() > 500)
tail = content.substr(content.size() - 500);
else
tail = content;
for (char &c : tail)
if (c == '\n' || c == '\r')
c = ' ';
}
engine::Logger::instance().error("game exited code " +
std::to_string(supervisor_exit) + ": " + tail);
}
w_->queue_->flush_pending_changes();
hide_game_lock_overlay();
trace.end_stage("launch", true, "Game exited");
Expand Down Expand Up @@ -1125,27 +1143,41 @@ void LaunchController::check_running_process() {
w_->cgroup_path_.clear();
if (w_->process_watch_timer_)
w_->process_watch_timer_->stop();
// Surface the real failure to the user-visible log.
{
int code = WIFEXITED(reap_status) ? WEXITSTATUS(reap_status) : -1;
std::string tail;
auto log_path = w_->cache_dir_path() / ".gmm_overlay_stderr.log";
std::error_code lec;
if (std::filesystem::exists(log_path, lec)) {
std::ifstream f(log_path);
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
if (content.size() > 500)
tail = content.substr(content.size() - 500);
else
tail = content;
for (char &c : tail)
if (c == '\n' || c == '\r')
c = ' ';
}
engine::Logger::instance().error("game exited code " + std::to_string(code) + ": " +
tail);
}
// Surface the real failure to the user-visible log.
{
int code = WIFEXITED(reap_status) ? WEXITSTATUS(reap_status) : -1;
std::string tail;
std::filesystem::path chosen;
std::filesystem::file_time_type chosen_mtime{};
for (const char *name :
{".gmm_overlay_stderr.log", ".gmm_proton_stderr.log"}) {
auto p = w_->cache_dir_path() / name;
std::error_code lec;
if (!std::filesystem::exists(p, lec))
continue;
auto mt = std::filesystem::last_write_time(p, lec);
if (lec || mt < w_->launch_time_)
continue;
if (chosen.empty() || mt > chosen_mtime) {
chosen = p;
chosen_mtime = mt;
}
}
if (!chosen.empty()) {
std::ifstream f(chosen);
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
if (content.size() > 500)
tail = content.substr(content.size() - 500);
else
tail = content;
for (char &c : tail)
if (c == '\n' || c == '\r')
c = ' ';
}
engine::Logger::instance().error("game exited code " + std::to_string(code) + ": " +
tail);
}
// P1.3 event bus: mirror MO2 onFinishedRun (fallback PGID path).
engine::EventBus::instance().dispatch(
engine::events::kGameFinished,
Expand Down