Skip to content

Commit

Permalink
Generate backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarBLG committed Apr 28, 2021
1 parent 63ab4f4 commit d22db2e
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ ETCS.kdev4
.directory
Thumbs.db
EVC/orts
error.log
2 changes: 1 addition & 1 deletion DMI/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CFLAGS = -Wall -g
LDFLAGS = -lSDL2 -lSDL2_ttf -lSDL2_net -pthread
ifeq ($(TARGET),WIN32)
CPP = x86_64-w64-mingw32-g++ -m64 -mwindows
LDFLAGS = -mwindows -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic -lmingw32 -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_net -lwsock32
LDFLAGS = -mwindows -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic -lmingw32 -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_net -lwsock32 -limagehlp
else
CFLAGS += -rdynamic
endif
Expand Down
2 changes: 1 addition & 1 deletion DMI/graphics/sdl/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void init_video()
running = false;
return;
}
startDisplay(true);
startDisplay(false);
int timer = SDL_AddTimer(250, flash, nullptr);
//SDL_AddTimer(100, [](Uint32 interval, void *) {repaint(); return interval;}, nullptr);
if(timer == 0)
Expand Down
110 changes: 103 additions & 7 deletions DMI/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,125 @@ void quit()
printf("quit\n");
}
#ifdef __ANDROID__
#else
#ifdef __unix__
#elif defined(_WIN32)
#include <windows.h>
#include <imagehlp.h>
LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS * ExceptionInfo)
{
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();

CONTEXT context = *(ExceptionInfo->ContextRecord);
/*memset(&context, 0, sizeof(CONTEXT));
context.ContextFlags = CONTEXT_FULL;
RtlCaptureContext(&context);*/

SymInitialize(process, NULL, TRUE);

DWORD image;
STACKFRAME64 stackframe;
ZeroMemory(&stackframe, sizeof(STACKFRAME64));

/* #ifdef _M_IX86
image = IMAGE_FILE_MACHINE_I386;
stackframe.AddrPC.Offset = context.Eip;
stackframe.AddrPC.Mode = AddrModeFlat;
stackframe.AddrFrame.Offset = context.Ebp;
stackframe.AddrFrame.Mode = AddrModeFlat;
stackframe.AddrStack.Offset = context.Esp;
stackframe.AddrStack.Mode = AddrModeFlat;
#elif _M_X64*/
image = IMAGE_FILE_MACHINE_AMD64;
stackframe.AddrPC.Offset = context.Rip;
stackframe.AddrPC.Mode = AddrModeFlat;
stackframe.AddrFrame.Offset = context.Rbp;
stackframe.AddrFrame.Mode = AddrModeFlat;
stackframe.AddrStack.Offset = context.Rsp;
stackframe.AddrStack.Mode = AddrModeFlat;
/*#elif _M_IA64
image = IMAGE_FILE_MACHINE_IA64;
stackframe.AddrPC.Offset = context.StIIP;
stackframe.AddrPC.Mode = AddrModeFlat;
stackframe.AddrFrame.Offset = context.IntSp;
stackframe.AddrFrame.Mode = AddrModeFlat;
stackframe.AddrBStore.Offset = context.RsBSP;
stackframe.AddrBStore.Mode = AddrModeFlat;
stackframe.AddrStack.Offset = context.IntSp;
stackframe.AddrStack.Mode = AddrModeFlat;
#endif*/

FILE *file = fopen("error.log", "w+");
for (size_t i = 0; i < 25; i++) {

BOOL result = StackWalk64(
image, process, thread,
&stackframe, &context, NULL,
SymFunctionTableAccess64, SymGetModuleBase64, NULL);

if (!result) { break; }

char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;

DWORD64 displacement = 0;
if (SymFromAddr(process, stackframe.AddrPC.Offset, &displacement, symbol)) {
fprintf(file, "[%i] %s\n", i, symbol->Name);
} else {
fprintf(file, "[%i] %p\n", i, (void*)stackframe.AddrPC.Offset);
}

}
fclose(file);

SymCleanup(process);
return EXCEPTION_EXECUTE_HANDLER;
}
#elif defined(__unix__)
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <cxxabi.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cxxabi.h>
#include <time.h>
void sighandler(int sig)
{
quit();
}
int addr2line(char const * const program_name, void const * const addr)
{
char addr2line_cmd[512] = {0};

sprintf(addr2line_cmd,"addr2line -f -p -e %.256s %p", program_name, addr);

return system(addr2line_cmd);
}
#define MAX_STACK_FRAMES 64
static void *stack_traces[MAX_STACK_FRAMES];
void crash_handler(int sig)
{
void *bt[20];
size_t size = backtrace(bt, 20);
time_t now = time(nullptr);
fprintf(stderr, "DMI received signal %d, date: %s\n", sig, ctime(&now));
backtrace_symbols_fd(bt, size, 2);
running = false;
exit(1);
FILE *file = fopen("error.log", "w+");
fprintf(file, "DMI received signal %d, date: %s\n", sig, ctime(&now));
backtrace_symbols_fd(bt, size, fileno(file));
fclose(file);

signal(sig, SIG_DFL);
return;
}
#endif
#ifdef __ANDROID__
#include <jni.h>
extern "C" void Java_com_etcs_dmi_DMI_DMIstop(JNIEnv *env, jobject thiz)
{
running = false;
}
#endif
int main(int argc, char** argv)
{
Expand All @@ -66,6 +159,9 @@ int main(int argc, char** argv)
signal(SIGSEGV, crash_handler);
signal(SIGABRT, crash_handler);
#endif
#endif
#ifdef _WIN32
SetUnhandledExceptionFilter(windows_exception_handler);
#endif
setSpeeds(0, 0, 0, 0, 0, 0);
setMonitor(CSM);
Expand Down
1 change: 0 additions & 1 deletion DMI/state/gps_pos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gps_pos.h"
#include "../time.h"
Component gps_pos(120,50,display_gps);
static bool show_pos = false;
static bool f = false;
Expand Down
2 changes: 1 addition & 1 deletion DMI/tcp/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "../monitor.h"
#include "../sound/sound.h"
#include "../messages/messages.h"
#include "../time.h"
#include "../time_etcs.h"
#include "../planning/planning.h"
#include "../graphics/drawing.h"
#include "../state/gps_pos.h"
Expand Down
2 changes: 1 addition & 1 deletion DMI/window/menu_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <algorithm>
#include "../monitor.h"
#include "../messages/messages.h"
#include "../time.h"
#include "../time_etcs.h"
#include "../control/control.h"
#include "../tcp/server.h"

Expand Down
12 changes: 6 additions & 6 deletions EVC/DMI/dmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ void start_dmi()
{
printf("Starting Driver Machine Interface...\n");
#ifndef _WIN32
/*dmi_pid = fork();
dmi_pid = fork();
if(dmi_pid == 0)
{
chdir("../DMI");*/
chdir("../DMI");
/*int fd = open("dmi.log.o", O_RDWR);
dup2(fd, 2);*/
/*execl("dmi", "dmi", nullptr);
}*/
execl("dmi", "dmi", nullptr);
}
sleep(1);
#else
/*STARTUPINFO si;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
TCHAR cmd[] = TEXT("../DMI/dmi.exe");
CreateProcess(nullptr, cmd, nullptr, nullptr, false, 0, nullptr, "../DMI", &si, &pi);*/
CreateProcess(nullptr, cmd, nullptr, nullptr, false, 0, nullptr, "../DMI", &si, &pi);
Sleep(1000);
WSADATA wsa;
WSAStartup(MAKEWORD(2,2), &wsa);
Expand Down
4 changes: 2 additions & 2 deletions EVC/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LIB = evc.dll
CXX = x86_64-w64-mingw32-g++ -m64
CC = x86_64-w64-mingw32-gcc -m64
CFLAGS = -I.
LDFLAGS = -lstdc++ -lwsock32 -L. -Bdynamic -lorts
LDFLAGS = -lstdc++ -lwsock32 -L. -Bdynamic -lorts -limagehlp
DEBUG=-g
endif
all: evc
Expand All @@ -31,4 +31,4 @@ evc: $(OBJECTS)
$(LIB): $(OBJECTS)
$(CXX) -shared -o evc.dll $(OBJECTS) $(LDFLAGS) -g
$(ARCHIVE): $(OBJECTS)
ar rcs
ar rcs
115 changes: 114 additions & 1 deletion EVC/evc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,121 @@
#include "TrackConditions/track_condition.h"
#include "TrainSubsystems/subsystems.h"

#ifdef __ANDROID__
#elif defined(_WIN32)
#include <windows.h>
#include <imagehlp.h>
LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS * ExceptionInfo)
{
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();

CONTEXT context = *(ExceptionInfo->ContextRecord);
/*memset(&context, 0, sizeof(CONTEXT));
context.ContextFlags = CONTEXT_FULL;
RtlCaptureContext(&context);*/

SymInitialize(process, NULL, TRUE);

DWORD image;
STACKFRAME64 stackframe;
ZeroMemory(&stackframe, sizeof(STACKFRAME64));

/* #ifdef _M_IX86
image = IMAGE_FILE_MACHINE_I386;
stackframe.AddrPC.Offset = context.Eip;
stackframe.AddrPC.Mode = AddrModeFlat;
stackframe.AddrFrame.Offset = context.Ebp;
stackframe.AddrFrame.Mode = AddrModeFlat;
stackframe.AddrStack.Offset = context.Esp;
stackframe.AddrStack.Mode = AddrModeFlat;
#elif _M_X64*/
image = IMAGE_FILE_MACHINE_AMD64;
stackframe.AddrPC.Offset = context.Rip;
stackframe.AddrPC.Mode = AddrModeFlat;
stackframe.AddrFrame.Offset = context.Rbp;
stackframe.AddrFrame.Mode = AddrModeFlat;
stackframe.AddrStack.Offset = context.Rsp;
stackframe.AddrStack.Mode = AddrModeFlat;
/*#elif _M_IA64
image = IMAGE_FILE_MACHINE_IA64;
stackframe.AddrPC.Offset = context.StIIP;
stackframe.AddrPC.Mode = AddrModeFlat;
stackframe.AddrFrame.Offset = context.IntSp;
stackframe.AddrFrame.Mode = AddrModeFlat;
stackframe.AddrBStore.Offset = context.RsBSP;
stackframe.AddrBStore.Mode = AddrModeFlat;
stackframe.AddrStack.Offset = context.IntSp;
stackframe.AddrStack.Mode = AddrModeFlat;
#endif*/

FILE *file = fopen("error.log", "w+");
for (size_t i = 0; i < 25; i++) {

BOOL result = StackWalk64(
image, process, thread,
&stackframe, &context, NULL,
SymFunctionTableAccess64, SymGetModuleBase64, NULL);

if (!result) { break; }

char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;

DWORD64 displacement = 0;
if (SymFromAddr(process, stackframe.AddrPC.Offset, &displacement, symbol)) {
fprintf(file, "[%i] %s\n", i, symbol->Name);
} else {
fprintf(file, "[%i] %p\n", i, (void*)stackframe.AddrPC.Offset);
}

}
fclose(file);

SymCleanup(process);
return EXCEPTION_EXECUTE_HANDLER;
}
#else
#include <execinfo.h>
#include <cxxabi.h>
#include <unistd.h>
#include <signal.h>
void crash_handler(int sig)
{
void *bt[20];
size_t size = backtrace(bt, 20);
time_t now = time(nullptr);
FILE *file = fopen("error.log", "w+");
fprintf(file, "DMI received signal %d, date: %s\n", sig, ctime(&now));
backtrace_symbols_fd(bt, size, fileno(file));
fclose(file);

signal(sig, SIG_DFL);
return;
}
#endif


std::mutex loop_mtx;
std::condition_variable loop_notifier;
void loop();
void start();
bool run;
int main()
{
run = true;
#ifdef __ANDROID__
#else
#ifdef __unix__
signal(SIGSEGV, crash_handler);
signal(SIGABRT, crash_handler);
#endif
#endif
#ifdef _WIN32
SetUnhandledExceptionFilter(windows_exception_handler);
#endif
std::printf("Starting European Train Control System...\n");
start();
loop();
Expand All @@ -56,6 +165,10 @@ extern "C" void Java_com_etcs_dmi_EVC_evcMain(JNIEnv *env, jobject thiz)
{
main();
}
extern "C" void Java_com_etcs_dmi_EVC_evcStop(JNIEnv *env, jobject thiz)
{
run = false;
}
#endif
bool started=false;
void start()
Expand Down Expand Up @@ -85,7 +198,7 @@ void update()
}
void loop()
{
while(1)
while(run)
{
auto prev = std::chrono::system_clock::now();
update();
Expand Down

0 comments on commit d22db2e

Please sign in to comment.