mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-24 13:09:16 +01:00
Merge branch 'master' into feat/renderers-cleanup
This commit is contained in:
@@ -201,6 +201,12 @@ struct global_settings {
|
||||
bool EditorOrtho{ false };
|
||||
float EditorOrthoExtent{ 150.f };
|
||||
int EditorOrthoTracks{ 0 }; // the railway in the top view of the editor: 0 whole, 1 without the ballast, 2 the rails alone
|
||||
// the free camera of the editor: its speed set in the View menu, the Ctrl multiplier of it, and in the top view whether it goes as far
|
||||
// as the view reaches, so the map moves across the screen as fast zoomed in as out
|
||||
bool EditorCameraOn{ false }; // the editor is on: its camera goes by the settings below
|
||||
double EditorCameraSpeed{ 1.0 };
|
||||
double EditorCameraFast{ 30.0 };
|
||||
bool EditorCameraZoomed{ false };
|
||||
GLint iMaxTextureSize{ 4096 }; // maksymalny rozmiar tekstury
|
||||
GLint iMaxCabTextureSize{ 4096 }; // largest allowed texture in vehicle cab
|
||||
int iMultisampling{ 2 }; // tryb antyaliasingu: 0=brak,1=2px,2=4px,3=8px,4=16px
|
||||
|
||||
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "utilities/Logs.h"
|
||||
#include "utilities/crashsignals.h"
|
||||
|
||||
#include <format>
|
||||
|
||||
@@ -117,6 +118,8 @@ void LogService()
|
||||
std::string filename = Global.MultipleLogs ? "logs/log (" + filename_scenery() + ") " + filename_date() + ".txt" : "log.txt";
|
||||
output.open(filename, std::ios::trunc);
|
||||
crashdump_set_log_file(crashdump_log_file::log, filename);
|
||||
// the stack of a crash on Linux goes to the end of this file
|
||||
crash_signals::set_log_path(filename);
|
||||
}
|
||||
output << msg << "\n";
|
||||
output.flush();
|
||||
|
||||
234
utilities/crashsignals.cpp
Normal file
234
utilities/crashsignals.cpp
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "utilities/crashsignals.h"
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
#include <execinfo.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace {
|
||||
|
||||
// the handler may only call what is safe in a signal: no allocation, no locks, no streams. so what it needs is
|
||||
// kept in fixed buffers, filled in beforehand, and written with write()
|
||||
|
||||
constexpr std::array crash_signal_list{SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGABRT};
|
||||
// a handler that hangs (a crash inside the allocator or the loader, which backtrace() may need) ends this long after
|
||||
constexpr unsigned int crash_report_timeout_seconds{5};
|
||||
|
||||
// a text of fixed size, filled outside the handler and read in it
|
||||
template <std::size_t Size> struct fixed_text {
|
||||
std::array<char, Size> text{};
|
||||
std::atomic<bool> ready{false};
|
||||
|
||||
void set(std::string_view const Text) {
|
||||
ready = false;
|
||||
auto const length{std::min(Text.size(), text.size() - 1)};
|
||||
std::copy_n(Text.data(), length, text.data());
|
||||
text[length] = '\0';
|
||||
ready = true;
|
||||
}
|
||||
char const *get() const { return ready ? text.data() : nullptr; }
|
||||
};
|
||||
|
||||
fixed_text<1024> &log_path() {
|
||||
static fixed_text<1024> path;
|
||||
return path;
|
||||
}
|
||||
|
||||
fixed_text<256> &version_text() {
|
||||
static fixed_text<256> version;
|
||||
return version;
|
||||
}
|
||||
|
||||
// the stack the handler runs on, so an overflow of the main thread's own stack is reported too
|
||||
std::array<char, 64 * 1024> &alternate_stack() {
|
||||
static std::array<char, 64 * 1024> stack{};
|
||||
return stack;
|
||||
}
|
||||
|
||||
void write_all(int const File, std::string_view Text) {
|
||||
while (false == Text.empty()) {
|
||||
auto const written{::write(File, Text.data(), Text.size())};
|
||||
if (written <= 0)
|
||||
return;
|
||||
Text.remove_prefix(static_cast<std::size_t>(written));
|
||||
}
|
||||
}
|
||||
|
||||
void write_hex(int const File, std::uintptr_t const Value) {
|
||||
constexpr std::size_t digits{sizeof(Value) * 2};
|
||||
constexpr std::string_view hexdigits{"0123456789abcdef"};
|
||||
std::array<char, digits + 2> text{'0', 'x'};
|
||||
for (std::size_t digit = 0; digit < digits; ++digit)
|
||||
text[2 + digit] = hexdigits[(Value >> (4 * (digits - 1 - digit))) & 0xF];
|
||||
write_all(File, {text.data(), text.size()});
|
||||
}
|
||||
|
||||
void write_decimal(int const File, int const Value) {
|
||||
std::array<char, 16> text{};
|
||||
auto position{text.size()};
|
||||
auto rest{static_cast<unsigned int>(Value < 0 ? -Value : Value)};
|
||||
do {
|
||||
text[--position] = static_cast<char>('0' + rest % 10);
|
||||
rest /= 10;
|
||||
} while (rest != 0 && position > 1);
|
||||
if (Value < 0)
|
||||
text[--position] = '-';
|
||||
write_all(File, {text.data() + position, text.size() - position});
|
||||
}
|
||||
|
||||
std::string_view signal_name(int const Signal) {
|
||||
switch (Signal) {
|
||||
case SIGSEGV:
|
||||
return "SIGSEGV";
|
||||
case SIGBUS:
|
||||
return "SIGBUS";
|
||||
case SIGFPE:
|
||||
return "SIGFPE";
|
||||
case SIGILL:
|
||||
return "SIGILL";
|
||||
case SIGABRT:
|
||||
return "SIGABRT";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
// the signal being reported, for a report that hangs
|
||||
std::atomic<int> &crash_signal() {
|
||||
static std::atomic<int> signal{0};
|
||||
return signal;
|
||||
}
|
||||
|
||||
// a report that hangs is given up: the process ends by the signal of the crash, as it would have without the handler
|
||||
void handle_report_timeout(int) {
|
||||
auto const crashsignal{crash_signal().load()};
|
||||
raise(crashsignal);
|
||||
// it is blocked while its own handler runs, which this one interrupted
|
||||
sigset_t unblocked;
|
||||
sigemptyset(&unblocked);
|
||||
sigaddset(&unblocked, crashsignal);
|
||||
sigprocmask(SIG_UNBLOCK, &unblocked, nullptr);
|
||||
}
|
||||
|
||||
struct crash_stack {
|
||||
std::array<void *, 64> frames{};
|
||||
int count{0};
|
||||
};
|
||||
|
||||
void write_report(int const File, int const Signal, siginfo_t const *Info, crash_stack const &Stack) {
|
||||
write_all(File, "\nCrash: signal ");
|
||||
write_decimal(File, Signal);
|
||||
write_all(File, " (");
|
||||
write_all(File, signal_name(Signal));
|
||||
write_all(File, ")");
|
||||
// the address of the fault; an abort has none
|
||||
if (Signal != SIGABRT && Info != nullptr) {
|
||||
write_all(File, " at address ");
|
||||
write_hex(File, reinterpret_cast<std::uintptr_t>(Info->si_addr));
|
||||
}
|
||||
write_all(File, "\n");
|
||||
if (auto const *version{version_text().get()}) {
|
||||
write_all(File, "Version: ");
|
||||
write_all(File, version);
|
||||
write_all(File, "\n");
|
||||
}
|
||||
write_all(File, "Stack (addr2line -f -C -e <the eu07 file of this version> <the offset after the +>):\n");
|
||||
backtrace_symbols_fd(Stack.frames.data(), Stack.count, File);
|
||||
}
|
||||
|
||||
void handle_crash_signal(int const Signal, siginfo_t *Info, void *) {
|
||||
crash_signal() = Signal;
|
||||
struct sigaction timeout{};
|
||||
timeout.sa_handler = handle_report_timeout;
|
||||
sigemptyset(&timeout.sa_mask);
|
||||
sigaction(SIGALRM, &timeout, nullptr);
|
||||
alarm(crash_report_timeout_seconds);
|
||||
|
||||
crash_stack stack;
|
||||
stack.count = backtrace(stack.frames.data(), static_cast<int>(stack.frames.size()));
|
||||
|
||||
if (auto const *path{log_path().get()}) {
|
||||
if (auto const log{::open(path, O_WRONLY | O_APPEND | O_CLOEXEC)}; log >= 0) {
|
||||
write_report(log, Signal, Info, stack);
|
||||
::close(log);
|
||||
}
|
||||
}
|
||||
write_report(STDERR_FILENO, Signal, Info, stack);
|
||||
|
||||
// the handler was taken off as it was called (SA_RESETHAND): the signal does what it always did
|
||||
raise(Signal);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace crash_signals {
|
||||
|
||||
void install(std::string_view const Version) {
|
||||
static std::atomic<bool> installed{false};
|
||||
if (installed.exchange(true))
|
||||
return;
|
||||
|
||||
version_text().set(Version);
|
||||
|
||||
// backtrace() loads what it needs on its first call, which allocates: done here, not in the handler
|
||||
std::array<void *, 1> warmup{};
|
||||
backtrace(warmup.data(), static_cast<int>(warmup.size()));
|
||||
|
||||
stack_t alternate{};
|
||||
alternate.ss_sp = alternate_stack().data();
|
||||
alternate.ss_size = alternate_stack().size();
|
||||
sigaltstack(&alternate, nullptr);
|
||||
|
||||
struct sigaction action{};
|
||||
action.sa_sigaction = handle_crash_signal;
|
||||
action.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESETHAND;
|
||||
// another crash signal of the same thread waits until the report is written, rather than starting a second one in
|
||||
// its middle (a fault that comes while it is blocked still ends the process). a crash of another thread at the
|
||||
// same moment may still write its report alongside
|
||||
sigemptyset(&action.sa_mask);
|
||||
for (auto const crashsignal : crash_signal_list)
|
||||
sigaddset(&action.sa_mask, crashsignal);
|
||||
for (auto const crashsignal : crash_signal_list)
|
||||
sigaction(crashsignal, &action, nullptr);
|
||||
}
|
||||
|
||||
void set_log_path(std::string_view const Path) {
|
||||
// whole, so the report finds the file wherever the simulator stands then
|
||||
std::error_code error;
|
||||
auto const path{std::filesystem::absolute(std::filesystem::path{Path}, error)};
|
||||
log_path().set(error ? std::string{Path} : path.string());
|
||||
}
|
||||
|
||||
} // namespace crash_signals
|
||||
|
||||
#else
|
||||
|
||||
namespace crash_signals {
|
||||
|
||||
void install(std::string_view) {}
|
||||
void set_log_path(std::string_view) {}
|
||||
|
||||
} // namespace crash_signals
|
||||
|
||||
#endif
|
||||
25
utilities/crashsignals.h
Normal file
25
utilities/crashsignals.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
// the stack of a crash on Linux, where no dump is written: a signal of a fault (SIGSEGV, SIGBUS, SIGFPE, SIGILL) or
|
||||
// SIGABRT writes the stack of the thread that took it to the log and to the standard error, which the starter shows,
|
||||
// and then ends the simulator as the signal always did (a core dump where the system makes one). the addresses are
|
||||
// read with addr2line on the eu07 file of the same version, which carries its symbols. elsewhere it does nothing
|
||||
namespace crash_signals {
|
||||
|
||||
// sets the handlers up, once. Version goes into the report, to tell which eu07 file the addresses belong to
|
||||
void install(std::string_view Version);
|
||||
// the log file the report is added to, once the log has opened it
|
||||
void set_log_path(std::string_view Path);
|
||||
|
||||
} // namespace crash_signals
|
||||
@@ -165,11 +165,11 @@ double Random(double min, double max)
|
||||
return dist(random_engine());
|
||||
}
|
||||
|
||||
int Random(int min, int max)
|
||||
int RandomInt(int min, int max)
|
||||
{
|
||||
if (max < min) { std::swap(min, max); } // std::uniform_int_distribution requires min <= max (inverted bounds are UB)
|
||||
std::uniform_int_distribution dist(min, max);
|
||||
return dist(random_engine());
|
||||
return dist(Global.local_random_engine);
|
||||
}
|
||||
|
||||
std::uint64_t true_random_seed()
|
||||
|
||||
@@ -64,7 +64,12 @@ inline long Round(double const f)
|
||||
}
|
||||
|
||||
double Random(double a, double b);
|
||||
int Random(int min, int max);
|
||||
// whole numbers from min to max, both included. a name of its own keeps the calls of Random() with whole-number
|
||||
// bounds (the wind, the wheel flats, the truck hunting, the tachometer) on Random(double, double), which draws the
|
||||
// real number in between that they expect. drawn from the local engine, as up to 26.05.1 from an engine of its own: it
|
||||
// picks what one machine shows (the trivia of the loading screen), and a draw from the engine of the run's seed would
|
||||
// put that engine out of step with the other machines of a network session
|
||||
int RandomInt(int min, int max);
|
||||
// the engine Random() draws from in the calling thread, in place of Global.random_engine, or none for that one again. the threads
|
||||
// of worker_pool have engines of their own: the one of the simulation can't be shared between threads
|
||||
void set_thread_random_engine(std::mt19937 *Engine); // NOSONAR: randomness of the simulation, not of anything security-relevant
|
||||
@@ -285,17 +290,18 @@ template <typename T> bool is_equal(T const &Left, T const &Right, T const Epsil
|
||||
return Left == Right;
|
||||
}
|
||||
|
||||
// Tolerant clamp. Unlike std::clamp, this does NOT invoke undefined behaviour when
|
||||
// the bounds are inverted (Lo > Hi) - it normalizes them instead. This restores the
|
||||
// pre-refactor behaviour of the old custom clamp() for code paths where the bounds are
|
||||
// computed at runtime and can legitimately cross (e.g. AI acceleration/braking limits,
|
||||
// physics jolt calculations), where std::clamp's UB produced wrong results.
|
||||
// Tolerant clamp, the old custom clamp() of the project (up to 26.05.1). Unlike
|
||||
// std::clamp, this does NOT invoke undefined behaviour when the bounds are inverted
|
||||
// (Lo > Hi): the lower bound is applied first and the upper one last, so inverted
|
||||
// bounds give the upper bound, as the old clamp() did. Every call of the old clamp()
|
||||
// whose bounds can cross (they come from the .fiz, the .mmd or the window, or are
|
||||
// computed from such values) uses it, so it gives what it gave before the move to
|
||||
// std::clamp. Where the code keeps the bounds in order, std::clamp gives the same.
|
||||
// New code uses std::clamp.
|
||||
template <typename Type_>
|
||||
constexpr Type_ safe_clamp( Type_ const Value, Type_ const Lo, Type_ const Hi )
|
||||
{
|
||||
return ( Hi < Lo )
|
||||
? std::clamp( Value, Hi, Lo )
|
||||
: std::clamp( Value, Lo, Hi );
|
||||
return std::min( std::max( Value, Lo ), Hi );
|
||||
}
|
||||
|
||||
// keeps the provided value in specified range 0-Range, as if the range was circular buffer
|
||||
|
||||
Reference in New Issue
Block a user