mirror of
https://github.com/Detanup01/gbe_fork.git
synced 2024-11-27 05:04:01 +08:00
tests for crash printer
This commit is contained in:
parent
fcfda3ddd2
commit
2056698d6e
33
crash_printer/tests/run_tests_linux.sh
Normal file
33
crash_printer/tests/run_tests_linux.sh
Normal file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
my_dir="$(cd "$(dirname "$0")" && pwd)"
|
||||
|
||||
pushd "$my_dir" > /dev/null
|
||||
|
||||
clang++ -x c++ -rdynamic -std=c++17 -fvisibility=hidden -fexceptions -fno-jump-tables -Og -g3 -fPIE -I../ ../linux.cpp ../common.cpp test_linux_sa_handler.cpp -otest_linux_sa_handler && {
|
||||
./test_linux_sa_handler ;
|
||||
echo "exit code = $?" ;
|
||||
rm -f ./test_linux_sa_handler ;
|
||||
}
|
||||
|
||||
clang++ -x c++ -rdynamic -std=c++17 -fvisibility=hidden -fexceptions -fno-jump-tables -Og -g3 -fPIE -I../ ../linux.cpp ../common.cpp test_linux_sa_sigaction.cpp -otest_linux_sa_sigaction && {
|
||||
./test_linux_sa_sigaction ;
|
||||
echo "exit code = $?" ;
|
||||
rm -f ./test_linux_sa_sigaction ;
|
||||
}
|
||||
|
||||
clang++ -m32 -x c++ -rdynamic -std=c++17 -fvisibility=hidden -fexceptions -fno-jump-tables -Og -g3 -fPIE -I../ ../linux.cpp ../common.cpp test_linux_sa_handler.cpp -otest_linux_sa_handler && {
|
||||
./test_linux_sa_handler ;
|
||||
echo "exit code = $?" ;
|
||||
rm -f ./test_linux_sa_handler ;
|
||||
}
|
||||
|
||||
clang++ -m32 -x c++ -rdynamic -std=c++17 -fvisibility=hidden -fexceptions -fno-jump-tables -Og -g3 -fPIE -I../ ../linux.cpp ../common.cpp test_linux_sa_sigaction.cpp -otest_linux_sa_sigaction && {
|
||||
./test_linux_sa_sigaction ;
|
||||
echo "exit code = $?" ;
|
||||
rm -f ./test_linux_sa_sigaction ;
|
||||
}
|
||||
|
||||
rm -f -r ./crash_test
|
||||
|
||||
popd > /dev/null
|
47
crash_printer/tests/run_tests_win.bat
Normal file
47
crash_printer/tests/run_tests_win.bat
Normal file
@ -0,0 +1,47 @@
|
||||
@echo off
|
||||
|
||||
pushd "%~dp0"
|
||||
|
||||
call :cleanup
|
||||
|
||||
setlocal
|
||||
call ..\..\build_win_set_env.bat 64
|
||||
cl.exe /DEBUG:FULL /Z7 /Od /std:c++17 /DYNAMICBASE /errorReport:none /nologo /utf-8 /EHsc /GF /GL- /GS /MT /I../ ../win.cpp ../common.cpp test_win.cpp kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib Ws2_32.lib Iphlpapi.lib Wldap32.lib Winmm.lib Bcrypt.lib Dbghelp.lib /link /DYNAMICBASE /ERRORREPORT:NONE /NOLOGO /OUT:test_win.exe && (
|
||||
call test_win.exe
|
||||
|
||||
setlocal enableDelayedExpansion
|
||||
echo exit code = !errorlevel!
|
||||
endlocal
|
||||
|
||||
call :cleanup
|
||||
)
|
||||
endlocal
|
||||
|
||||
setlocal
|
||||
call ..\..\build_win_set_env.bat 32
|
||||
cl.exe /DEBUG:FULL /Z7 /Od /std:c++17 /DYNAMICBASE /errorReport:none /nologo /utf-8 /EHsc /GF /GL- /GS /MT /I../ ../win.cpp ../common.cpp test_win.cpp kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib Ws2_32.lib Iphlpapi.lib Wldap32.lib Winmm.lib Bcrypt.lib Dbghelp.lib /link /DYNAMICBASE /ERRORREPORT:NONE /NOLOGO /OUT:test_win.exe && (
|
||||
call test_win.exe
|
||||
|
||||
setlocal enableDelayedExpansion
|
||||
echo exit code = !errorlevel!
|
||||
endlocal
|
||||
|
||||
call :cleanup
|
||||
)
|
||||
endlocal
|
||||
|
||||
rmdir /s /q crash_test
|
||||
|
||||
popd
|
||||
|
||||
exit /b 0
|
||||
|
||||
|
||||
:cleanup
|
||||
del /f /q test_win.exe >nul 2>&1
|
||||
del /f /q test_win.ilk >nul 2>&1
|
||||
del /f /q test_win.obj >nul 2>&1
|
||||
del /f /q test_win.pdb >nul 2>&1
|
||||
del /f /q win.obj >nul 2>&1
|
||||
del /f /q common.obj >nul 2>&1
|
||||
exit /b
|
35
crash_printer/tests/test_helper.hpp
Normal file
35
crash_printer/tests/test_helper.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef _TEST_CRASH_PRINTER_HELPER_H
|
||||
#define _TEST_CRASH_PRINTER_HELPER_H
|
||||
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
static inline bool remove_file(const std::string &file)
|
||||
{
|
||||
if (!std::filesystem::exists(file)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (std::filesystem::is_directory(file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return std::filesystem::remove(file);
|
||||
}
|
||||
|
||||
static inline bool remove_file(const std::wstring &file)
|
||||
{
|
||||
if (!std::filesystem::exists(file)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (std::filesystem::is_directory(file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return std::filesystem::remove(file);
|
||||
}
|
||||
|
||||
|
||||
#endif // _TEST_CRASH_PRINTER_HELPER_H
|
60
crash_printer/tests/test_linux_sa_handler.cpp
Normal file
60
crash_printer/tests/test_linux_sa_handler.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
|
||||
#include "crash_printer/linux.hpp"
|
||||
#include "./test_helper.hpp"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <ucontext.h>
|
||||
#include <signal.h> // SIGBUS + SA_SIGINFO ...
|
||||
#include <string.h> // strsignal
|
||||
#include <execinfo.h> // backtrace + backtrace_symbols
|
||||
|
||||
std::string logs_filepath = "./crash_test/asd.txt";
|
||||
|
||||
// sa_sigaction handler for illegal instruction (SIGILL)
|
||||
void exception_handler_SIGILL(int signal)
|
||||
{
|
||||
std::ifstream file(logs_filepath);
|
||||
if (file.is_open()) {
|
||||
std::string line{};
|
||||
std::getline(file, line);
|
||||
file.close();
|
||||
|
||||
if (line.size()) {
|
||||
std::cout << "Success!" << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "Failed!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// simululate the existence of previous handler
|
||||
struct sigaction sa_SIGSEGV_prev{};
|
||||
sa_SIGSEGV_prev.sa_handler = exception_handler_SIGILL;
|
||||
sa_SIGSEGV_prev.sa_flags = SA_RESETHAND;
|
||||
sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked
|
||||
sigaction(SIGSEGV, &sa_SIGSEGV_prev, nullptr);
|
||||
|
||||
if (!remove_file(logs_filepath)) {
|
||||
std::cerr << "failed to remove log" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!crash_printer::init(logs_filepath)) {
|
||||
std::cerr << "failed to init" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// simulate a crash
|
||||
volatile int * volatile ptr = nullptr;
|
||||
*ptr = 42;
|
||||
|
||||
return 0;
|
||||
}
|
60
crash_printer/tests/test_linux_sa_sigaction.cpp
Normal file
60
crash_printer/tests/test_linux_sa_sigaction.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
|
||||
#include "crash_printer/linux.hpp"
|
||||
#include "./test_helper.hpp"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <ucontext.h>
|
||||
#include <signal.h> // SIGBUS + SA_SIGINFO ...
|
||||
#include <string.h> // strsignal
|
||||
#include <execinfo.h> // backtrace + backtrace_symbols
|
||||
|
||||
std::string logs_filepath = "./crash_test/asd.txt";
|
||||
|
||||
// sa_handler handler for illegal instruction (SIGILL)
|
||||
void exception_handler_SIGILL(int signal, siginfo_t *info, void *context)
|
||||
{
|
||||
std::ifstream file(logs_filepath);
|
||||
if (file.is_open()) {
|
||||
std::string line{};
|
||||
std::getline(file, line);
|
||||
file.close();
|
||||
|
||||
if (line.size()) {
|
||||
std::cout << "Success!" << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "Failed!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// simululate the existence of previous handler
|
||||
struct sigaction sa_SIGSEGV_prev{};
|
||||
sa_SIGSEGV_prev.sa_sigaction = exception_handler_SIGILL;
|
||||
sa_SIGSEGV_prev.sa_flags = SA_SIGINFO | SA_RESETHAND;
|
||||
sigemptyset(&sa_SIGSEGV_prev.sa_mask); // all signals unblocked
|
||||
sigaction(SIGSEGV, &sa_SIGSEGV_prev, nullptr);
|
||||
|
||||
if (!remove_file(logs_filepath)) {
|
||||
std::cerr << "failed to remove log" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!crash_printer::init(logs_filepath)) {
|
||||
std::cerr << "failed to init" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// simulate a crash
|
||||
volatile int * volatile ptr = nullptr;
|
||||
*ptr = 42;
|
||||
|
||||
return 0;
|
||||
}
|
53
crash_printer/tests/test_win.cpp
Normal file
53
crash_printer/tests/test_win.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
#include "crash_printer/win.hpp"
|
||||
#include "./test_helper.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
std::wstring logs_filepath = L"./crash_test/zxc.txt";
|
||||
|
||||
|
||||
static LONG WINAPI exception_handler(LPEXCEPTION_POINTERS ex_pointers)
|
||||
{
|
||||
std::ifstream file(logs_filepath);
|
||||
if (file.is_open()) {
|
||||
std::string line{};
|
||||
std::getline(file, line);
|
||||
file.close();
|
||||
|
||||
if (line.size()) {
|
||||
std::cout << "Success!" << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "Failed!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// simululate the existence of previous handler
|
||||
SetUnhandledExceptionFilter(exception_handler);
|
||||
|
||||
if (!remove_file(logs_filepath)) {
|
||||
std::cerr << "failed to remove log" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!crash_printer::init(logs_filepath)) {
|
||||
std::cerr << "failed to init" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// simulate a crash
|
||||
volatile int * volatile ptr = nullptr;
|
||||
*ptr = 42;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user