common: Move classes to common namespace

This commit is contained in:
GPUCode 2023-11-05 16:56:28 +02:00
parent 996aa9d17a
commit 303f086b14
21 changed files with 190 additions and 199 deletions

View file

@ -1,7 +1,9 @@
#pragma once
#ifdef _MSC_VER
#define BREAKPOINT __debugbreak
#elif defined(__GNUC__)
#define BREAKPOINT __builtin_trap
#else
#error What the fuck is this compiler
#endif
#endif

View file

@ -1,35 +1,33 @@
#include "common/disassembler.h"
#include <fmt/format.h>
Disassembler::Disassembler()
{
namespace Common {
Disassembler::Disassembler() {
ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL);
}
Disassembler::~Disassembler()
{
}
Disassembler::~Disassembler() = default;
void Disassembler::printInstruction(void* code,u64 address)//print a single instruction
{
void Disassembler::printInstruction(void* code, u64 address) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
ZyanStatus status = ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code), &instruction, operands);
if (!ZYAN_SUCCESS(status))
{
ZyanStatus status = ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code),
&instruction, operands);
if (!ZYAN_SUCCESS(status)) {
fmt::print("decode instruction failed at {}\n", fmt::ptr(code));
}
else
{
} else {
printInst(instruction, operands,address);
}
}
void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,u64 address)
{
void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address) {
const int bufLen = 256;
char szBuffer[bufLen];
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands,inst.operand_count_visible, szBuffer, sizeof(szBuffer), address, ZYAN_NULL);
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands,inst.operand_count_visible,
szBuffer, sizeof(szBuffer), address, ZYAN_NULL);
fmt::print("instruction: {}\n", szBuffer);
}
} // namespace Common

View file

@ -3,15 +3,19 @@
#include <Zydis/Zydis.h>
#include "common/types.h"
class Disassembler
{
namespace Common {
class Disassembler {
public:
Disassembler();
~Disassembler();
void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,u64 address);
void printInstruction(void* code,u64 address);
void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address);
void printInstruction(void* code, u64 address);
private:
ZydisDecoder m_decoder;
ZydisDecoder m_decoder;
ZydisFormatter m_formatter;
};
} // namespace Common

View file

@ -3,73 +3,95 @@
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <vector>
#include <Util/config.h>
#include "common/log.h"
#ifdef _WIN64
#include <Windows.h>
#include <windows.h>
#endif
#include "log.h"
namespace Common::Log {
namespace logging {
std::vector<spdlog::sink_ptr> sinks;
constexpr bool log_file_exceptions = true; // disable it to disable logging
constexpr bool log_file_exceptions = true;
void flush() { spdlog::details::registry::instance().flush_all(); }
void Flush() {
spdlog::details::registry::instance().flush_all();
}
#ifdef _WIN64
static LONG WINAPI exception_handler(PEXCEPTION_POINTERS pExp) noexcept {
static LONG WINAPI ExceptionHandler(PEXCEPTION_POINTERS pExp) noexcept {
const u32 ec = pExp->ExceptionRecord->ExceptionCode;
switch (ec) {
case EXCEPTION_ACCESS_VIOLATION:
LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_ACCESS_VIOLATION ({}). ", log_hex(ec));
switch (pExp->ExceptionRecord->ExceptionInformation[0]) {
case 0: LOG_CRITICAL_IF(log_file_exceptions,"Read violation at address {}.", log_hex(pExp->ExceptionRecord->ExceptionInformation[1])); break;
case 1: LOG_CRITICAL_IF(log_file_exceptions,"Write violation at address {}.", log_hex(pExp->ExceptionRecord->ExceptionInformation[1])); break;
case 8:LOG_CRITICAL_IF(log_file_exceptions,"DEP violation at address {}.", log_hex(pExp->ExceptionRecord->ExceptionInformation[1])); break;
default: break;
case EXCEPTION_ACCESS_VIOLATION: {
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ACCESS_VIOLATION ({:#x}). ", ec);
const auto info = pExp->ExceptionRecord->ExceptionInformation;
switch (info[0]) {
case 0:
LOG_CRITICAL_IF(log_file_exceptions, "Read violation at address {:#x}.", info[1]);
break;
case 1:
LOG_CRITICAL_IF(log_file_exceptions, "Write violation at address {:#x}.", info[1]);
break;
case 8:
LOG_CRITICAL_IF(log_file_exceptions, "DEP violation at address {:#x}.", info[1]);
break;
default:
break;
}
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_ARRAY_BOUNDS_EXCEEDED ({:#x}). ", ec); break;
case EXCEPTION_DATATYPE_MISALIGNMENT: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_DATATYPE_MISALIGNMENT ({}). ", log_hex(ec)); break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_FLT_DIVIDE_BY_ZERO ({}). ", log_hex(ec)); break;
case EXCEPTION_ILLEGAL_INSTRUCTION: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_ILLEGAL_INSTRUCTION ({}). ", log_hex(ec)); break;
case EXCEPTION_IN_PAGE_ERROR: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_IN_PAGE_ERROR ({}). ", log_hex(ec)); break;
case EXCEPTION_INT_DIVIDE_BY_ZERO: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_INT_DIVIDE_BY_ZERO ({}). ", log_hex(ec)); break;
case EXCEPTION_PRIV_INSTRUCTION: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_PRIV_INSTRUCTION ({}). ", log_hex(ec)); break;
case EXCEPTION_STACK_OVERFLOW: LOG_CRITICAL_IF(log_file_exceptions,"Exception EXCEPTION_STACK_OVERFLOW ({}). ", log_hex(ec)); break;
default: return EXCEPTION_CONTINUE_SEARCH;
}
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ARRAY_BOUNDS_EXCEEDED ({:#x}). ", ec);
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_DATATYPE_MISALIGNMENT ({:#x}). ", ec);
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_FLT_DIVIDE_BY_ZERO ({:#x}). ", ec);
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ILLEGAL_INSTRUCTION ({:#x}). ", ec);
break;
case EXCEPTION_IN_PAGE_ERROR:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_IN_PAGE_ERROR ({:#x}). ", ec);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_INT_DIVIDE_BY_ZERO ({:#x}). ", ec);
break;
case EXCEPTION_PRIV_INSTRUCTION:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_PRIV_INSTRUCTION ({:#x}). ", ec);
break;
case EXCEPTION_STACK_OVERFLOW:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_STACK_OVERFLOW ({:#x}). ", ec);
break;
default:
return EXCEPTION_CONTINUE_SEARCH;
}
flush();
Flush();
return EXCEPTION_CONTINUE_SEARCH;
}
void register_log_exception_handler() {
if (!AddVectoredExceptionHandler(0, exception_handler)) {
LOG_CRITICAL_IF(log_file_exceptions,"Failed to register an exception handler");
}
}
#endif
int init(bool use_stdout) {
sinks.clear(); // clear existing sinks
if (use_stdout) // if we use stdout window then init it as well
int Init(bool use_stdout) {
sinks.clear();
if (use_stdout) {
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
}
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(L"shadps4.txt", true));
spdlog::set_default_logger(std::make_shared<spdlog::logger>("shadps4 logger", begin(sinks), end(sinks)));
auto f = std::make_unique<spdlog::pattern_formatter>("%^|%L|: %v%$", spdlog::pattern_time_type::local, std::string("")); // disable eol
spdlog::set_formatter(std::move(f));
spdlog::set_level(static_cast<spdlog::level::level_enum>(Config::getLogLevel()));
spdlog::level::level_enum t = spdlog::get_level();
#ifdef _WIN64
register_log_exception_handler();
#endif
static std::terminate_handler old_terminate = nullptr;
#ifdef _WIN64
if (!AddVectoredExceptionHandler(0, ExceptionHandler)) {
LOG_CRITICAL_IF(log_file_exceptions, "Failed to register an exception handler");
}
#endif
static std::terminate_handler old_terminate = nullptr;
old_terminate = std::set_terminate([]() {
try {
std::rethrow_exception(std::current_exception());
@ -78,13 +100,15 @@ int init(bool use_stdout) {
} catch (...) {
LOG_CRITICAL_IF(log_file_exceptions, "Unhandled C++ exception. UNKNOWN");
}
flush();
Flush();
if (old_terminate) old_terminate();
});
return 0; // all ok
return 0;
}
void set_level(spdlog::level::level_enum log_level) { spdlog::set_level(log_level); }
void SetLevel(spdlog::level::level_enum log_level) {
spdlog::set_level(log_level);
}
} // namespace logging
} // namespace Common::Log

View file

@ -4,7 +4,7 @@
#include <spdlog/spdlog.h>
namespace logging {
namespace Common::Log {
#define LOG_TRACE SPDLOG_TRACE
#define LOG_DEBUG SPDLOG_DEBUG
@ -26,48 +26,8 @@ namespace logging {
#define LOG_CRITICAL_IF(flag, ...) \
if (flag) LOG_CRITICAL(__VA_ARGS__)
int init(bool use_stdout);
void set_level(spdlog::level::level_enum log_level);
} // namespace logging
int Init(bool use_stdout);
// copyright vita3k emu https://github.com/Vita3K/Vita3K/blob/master/vita3k/util/include/util/log.h
/*
returns: A string with the input number formatted in hexadecimal
Examples:
* `12` returns: `"0xC"`
* `1337` returns: `"0x539"`
* `72742069` returns: `"0x455F4B5"`
*/
template <typename T>
std::string log_hex(T val) {
using unsigned_type = typename std::make_unsigned<T>::type;
std::stringstream ss;
ss << "0x";
ss << std::hex << static_cast<unsigned_type>(val);
return ss.str();
}
void SetLevel(spdlog::level::level_enum log_level);
/*
returns: A string with the input number formatted in hexadecimal with padding of the inputted type size
Examples:
* `uint8_t 5` returns: `"0x05"`
* `uint8_t 15` returns: `"0x0F"`
* `uint8_t 255` returns: `"0xFF"`
* `uint16_t 15` returns: `"0x000F"`
* `uint16_t 1337` returns: `"0x0539"`
* `uint16_t 65535` returns: `"0xFFFF"`
* `uint32_t 15` returns: `"0x0000000F"`
* `uint32_t 1337` returns: `"0x00000539"`
* `uint32_t 65535` returns: `"0x0000FFFF"`
* `uint32_t 134217728` returns: `"0x08000000"`
*/
template <typename T>
std::string log_hex_full(T val) {
std::stringstream ss;
ss << "0x";
ss << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << val;
return ss.str();
}
} // namespace Common::Log

View file

@ -2,10 +2,12 @@
#include <memory>
namespace Common {
template <class T>
class singleton {
class Singleton {
public:
static T* instance() {
static T* Instance() {
if (!m_instance) {
m_instance = std::make_unique<T>();
}
@ -13,9 +15,11 @@ public:
}
protected:
singleton();
~singleton();
Singleton();
~Singleton();
private:
static inline std::unique_ptr<T> m_instance{};
};
} // namespace Common

View file

@ -3,21 +3,18 @@
#include <string>
#include "common/string_util.h"
namespace StringUtil {
namespace Common {
std::vector<std::string> split_string(const std::string &str, char delimiter) {
std::stringstream str_stream(str);
std::string segment;
std::vector<std::string> seglist;
std::vector<std::string> SplitString(const std::string &str, char delimiter) {
std::istringstream iss(str);
std::vector<std::string> output(1);
const size_t num_segments = std::count_if(str.begin(), str.end(), [&](char c) { return c == delimiter; }) + (str.empty() ? 1 : 0);
seglist.reserve(num_segments);
while (std::getline(str_stream, segment, delimiter)) {
seglist.push_back(segment);
while (std::getline(iss, *output.rbegin(), delimiter)) {
output.emplace_back();
}
return seglist;
output.pop_back();
return output;
}
} // namespace StringUtil
} // namespace Common

View file

@ -1,9 +1,10 @@
#pragma once
#include <vector>
#include <string>
namespace StringUtil {
namespace Common {
std::vector<std::string> split_string(const std::string& str, char delimiter);
std::vector<std::string> SplitString(const std::string& str, char delimiter);
}
} // namespace Common

View file

@ -1,4 +1,5 @@
#pragma once
#include <cstdint>
using s08 = std::int8_t;
@ -16,7 +17,6 @@ using f64 = double;
#define PS4_SYSV_ABI __attribute__((sysv_abi))
// UDLs for memory size values
constexpr u64 operator""_KB(u64 x) { return 1024ULL * x; }
constexpr u64 operator""_MB(u64 x) { return 1024_KB * x; }

View file

@ -1,7 +1,10 @@
#pragma once
#include <string>
#include <string_view>
namespace Emulator {
namespace Common {
constexpr char VERSION[] = "0.0.3 WIP";
}
} // namespace Common