Skip to content

Commit

Permalink
Fix bugs in Daemon process
Browse files Browse the repository at this point in the history
  • Loading branch information
yqs112358 committed Jul 16, 2021
1 parent e9736d5 commit 2f4d4ba
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 69 deletions.
2 changes: 1 addition & 1 deletion CrashLogger.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.30907.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrashLogger", "CrashLogger\CrashLogger.vcxproj", "{B9492785-E9A2-42BA-A6C0-5AFE14FA3614}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrashLogger_Deamon", "CrashLogger_Deamon\CrashLogger_Deamon.vcxproj", "{336BD48D-37CD-41CC-BC61-F5090AF162BE}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrashLogger_Daemon", "CrashLogger_Daemon\CrashLogger_Daemon.vcxproj", "{336BD48D-37CD-41CC-BC61-F5090AF162BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
5 changes: 4 additions & 1 deletion CrashLogger/CrashLogger.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerCommand>C:\Users\yqs11\Desktop\Minecraft\BDS\BDS-1.17\bedrock_server.exe</LocalDebuggerCommand>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
13 changes: 7 additions & 6 deletions CrashLogger/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "pch.h"
#include <string>
#include <cstdio>
#include <cstring>
#include "../include/LoggerShareData.h"

bool InitDeamonProcess()
bool InitDaemonProcess()
{
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
Expand All @@ -15,12 +16,12 @@ bool InitDeamonProcess()
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);

wchar_t deamonPath[MAX_PATH];
wchar_t daemonPath[MAX_PATH];

wsprintf(deamonPath, L"%ls %u", DEAMON_PROCESS_PATH, GetCurrentProcessId());
if (!CreateProcess(NULL, deamonPath, &sa, &sa, TRUE, 0, NULL, NULL, &si, &pi))
wsprintf(daemonPath, L"%ls %u", DAEMON_PROCESS_PATH, GetCurrentProcessId());
if (!CreateProcess(NULL, daemonPath, &sa, &sa, TRUE, 0, NULL, NULL, &si, &pi))
{
printf("[CrashLogger][ERROR] Could not Create Deamon Process! Error Code: %d\n", GetLastError());
printf("[CrashLogger][ERROR] Could not Create Daemon Process! Error Code: %d\n", GetLastError());
return false;
}

Expand All @@ -42,7 +43,7 @@ BOOL APIENTRY DllMain( HMODULE hModule,
printf("[CrashLogger][Warning] Existing debugger detected. CrashLogger will not work.\n");
return TRUE;
}
if (!InitDeamonProcess())
if (!InitDaemonProcess())
return FALSE;
printf("[CrashLogger] CrashLogger loaded.\n");
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ProjectGuid>{336bd48d-37cd-41cc-bc61-f5090af162be}</ProjectGuid>
<RootNamespace>CrashLoggerD</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>CrashLogger_Deamon</ProjectName>
<ProjectName>CrashLogger_Daemon</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down Expand Up @@ -138,6 +138,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
File renamed without changes.
32 changes: 18 additions & 14 deletions CrashLogger_Deamon/Debugger.cpp → CrashLogger_Daemon/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

using namespace std;

HANDLE hProcess;
HANDLE hThread;
HANDLE hProcess_Debug;
bool waitEvent = true;
DEBUG_EVENT debugEvent;

Expand Down Expand Up @@ -44,7 +43,7 @@ bool LoadSymbolFiles()
FindSymbols(symbolPath,".",false);
FindSymbols(symbolPath,".\\plugins", true);

if (!SymInitializeW(hProcess, symbolPath.c_str(), TRUE))
if (!SymInitializeW(hProcess_Debug, symbolPath.c_str(), TRUE))
{
printf("[CrashLogger][ERROR] Fail to load Symbol Files! Error Code: %d\n", GetLastError());
return false;
Expand All @@ -54,7 +53,7 @@ bool LoadSymbolFiles()

bool InitDebugger()
{
if (!DebugActiveProcess(GetProcessId(hProcess)))
if (!DebugActiveProcess(GetProcessId(hProcess_Debug)))
{
printf("[CrashLogger][ERROR] Fail to attach Debugger! Error Code: %d\n", GetLastError());
return false;
Expand All @@ -76,46 +75,51 @@ DWORD inline OnThreadCreated(const CREATE_THREAD_DEBUG_INFO* e)
}
DWORD inline OnDllLoaded(const LOAD_DLL_DEBUG_INFO* e)
{
SymLoadModule64(hProcess, e->hFile, NULL, NULL , (DWORD64)(e->lpBaseOfDll), 0);
SymLoadModule64(hProcess_Debug, e->hFile, NULL, NULL , (DWORD64)(e->lpBaseOfDll), 0);
return DBG_CONTINUE;
}
DWORD inline OnDllUnloaded(const UNLOAD_DLL_DEBUG_INFO* e)
{
SymUnloadModule64(hProcess, (DWORD64)(e->lpBaseOfDll));
SymUnloadModule64(hProcess_Debug, (DWORD64)(e->lpBaseOfDll));
return DBG_CONTINUE;
}

extern void LogCrash(PEXCEPTION_POINTERS e);
DWORD OnException(const EXCEPTION_DEBUG_INFO* e, DWORD threadId)
extern void LogCrash(PEXCEPTION_POINTERS e, HANDLE hPro, HANDLE hThr, DWORD dProId, DWORD dThrId);
DWORD OnException(const EXCEPTION_DEBUG_INFO* e, DWORD processId, DWORD threadId)
{
if (e->dwFirstChance)
return DBG_EXCEPTION_NOT_HANDLED;

EXCEPTION_POINTERS exception = { 0 };
CONTEXT context;

hThread = OpenThread(THREAD_ALL_ACCESS, TRUE, threadId);
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, TRUE, threadId);
if (hThread == NULL)
{
printf("[CrashLogger][ERROR] Fail to Open Thread! Error Code: %d\n", GetLastError());
return DBG_EXCEPTION_NOT_HANDLED;
}
context.ContextFlags = CONTEXT_FULL;
GetThreadContext(hThread, &context);
if (!GetThreadContext(hThread, &context))
{
printf("[CrashLogger][ERROR] Fail to Get Context! Error Code: %d\n", GetLastError());
return DBG_EXCEPTION_NOT_HANDLED;
}

exception.ContextRecord = &context;
exception.ExceptionRecord = (PEXCEPTION_RECORD)&(e->ExceptionRecord);

LogCrash(&exception);
LogCrash(&exception, hProcess_Debug, hThread, processId, threadId);
//Do other operations

SymCleanup(hProcess);
SymCleanup(hProcess_Debug);
Sleep(SLEEP_BEFORE_ABORT * 1000);
return DBG_EXCEPTION_NOT_HANDLED;
}

void DebuggerMain()
void DebuggerMain(HANDLE hPro)
{
hProcess_Debug = hPro;
if (!InitDebugger() || !LoadSymbolFiles())
return;

Expand All @@ -131,7 +135,7 @@ void DebuggerMain()
continueStatus = OnThreadCreated(&debugEvent.u.CreateThread);
break;
case EXCEPTION_DEBUG_EVENT:
continueStatus = OnException(&debugEvent.u.Exception, debugEvent.dwThreadId);
continueStatus = OnException(&debugEvent.u.Exception, debugEvent.dwProcessId, debugEvent.dwThreadId);
break;
case EXIT_PROCESS_DEBUG_EVENT:
waitEvent = false;
Expand Down
24 changes: 17 additions & 7 deletions CrashLogger_Deamon/Logger.cpp → CrashLogger_Daemon/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

using namespace std;

extern HANDLE hProcess;
extern HANDLE hThread;
HANDLE hProcess;
HANDLE hThread;
DWORD dProcessId;
DWORD dThreadId;
FILE* fLog;
HANDLE hDumpFile;

Expand Down Expand Up @@ -39,7 +41,7 @@ bool CreateLogFiles()
string dateTimeStr = GetDateTime();

string dumpPath = DUMP_OUTPUT_PATH + dateTimeStr + ".dmp";
HANDLE hDumpFile = CreateFileA(dumpPath.c_str(), GENERIC_WRITE, 0, NULL,
hDumpFile = CreateFileA(dumpPath.c_str(), GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDumpFile == INVALID_HANDLE_VALUE || hDumpFile == NULL)
{
Expand All @@ -62,10 +64,13 @@ void CoreDump(PEXCEPTION_POINTERS e)
if (hDumpFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
memset(&dumpInfo, 0, sizeof(MINIDUMP_EXCEPTION_INFORMATION));

dumpInfo.ExceptionPointers = e;
dumpInfo.ThreadId = GetThreadId(hThread);
dumpInfo.ThreadId = dThreadId;
dumpInfo.ClientPointers = FALSE;
if (!MiniDumpWriteDump(hProcess, GetProcessId(hProcess), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL))

if (!MiniDumpWriteDump(hProcess, dProcessId, hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL))
{
log("[CrashLogger][ERROR] Fail to Generate Minidump! Error Code: %u\n", GetLastError());
}
Expand Down Expand Up @@ -108,8 +113,13 @@ void TrackBack(PEXCEPTION_POINTERS e)
}
}

void LogCrash(PEXCEPTION_POINTERS e)
void LogCrash(PEXCEPTION_POINTERS e, HANDLE hPro, HANDLE hThr, DWORD dProId, DWORD dThrId)
{
hProcess = hPro;
hThread = hThr;
dProcessId = dProId;
dThreadId = dThrId;

if (!CreateLogFiles() || !CreateModuleMap(hProcess))
return;

Expand All @@ -118,7 +128,7 @@ void LogCrash(PEXCEPTION_POINTERS e)
log("-- Unhandled Exception in -> %ls\n", MapModuleFromAddr(hProcess, e->ExceptionRecord->ExceptionAddress).c_str());
log("-- Exception Code: %u\n", e->ExceptionRecord->ExceptionCode);
if(e->ExceptionRecord->ExceptionCode == CRT_EXCEPTION_CODE)
log("-- C++ STL Exception detected! CrashLogger may not give correct information about this exception.\n")
log("-- C++ STL Exception detected!\n")

CoreDump(e);
CloseHandle(hDumpFile);
Expand Down
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions CrashLogger_Daemon/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <windows.h>
#include <cstdio>
#include <string>
#include "../include/LoggerShareData.h"
#include <iostream>

using namespace std;

extern void DebuggerMain(HANDLE hPro);

int main(int argc,char **argv)
{
if (argc <= 1)
{
printf("[CrashLogger] Don't execute this process independently.\n"
"[CrashLogger] You should pass a PID as a command line argument which is of the process to be daemoned.\n");
MessageBox(NULL, L"Don't execute this process independently.\r\nYou should pass a PID as a command line argument which is of the process to be daemoned.",L"Error",MB_OK);
return 1;
}

HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, stoul(argv[1]));
if (hProcess == NULL)
{
printf("[CrashLogger][ERROR] Fail to Open the process daemoned! Error Code: %d\n", GetLastError());
return -1;
}

printf("[CrashLogger] CrashLogger Daemon Process attached.\n");
DebuggerMain(hProcess);
return 0;
}
33 changes: 0 additions & 33 deletions CrashLogger_Deamon/main.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion MakeRelease.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
mkdir RELEASE
mkdir RELEASE\CrashLogger
copy /Y x64\Release\CrashLogger.dll RELEASE\
copy /Y x64\Release\CrashLogger_Deamon.exe RELEASE\CrashLogger\
copy /Y x64\Release\CrashLogger_Daemon.exe RELEASE\CrashLogger\
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Support me at https://www.minebbs.com/resources/crashlogger.2287/
- Result of stack walk will output both at console and file `.\logs\Crash\TrackBack_xxxxxxxx_xx-xx-xx.log`
- Dumpbin database will generate at file `.\logs\Crash\CrashDump_xxxxxxxx_xx-xx-xx.dmp`

![image](https://user-images.githubusercontent.com/37969157/115204373-fd948700-a12a-11eb-9352-7ffb7182fea1.png)
![image-20210716124312591](asserts/image-20210716124312591.png)
- Stack walk can give you a general view of possible problems.

Using CrashDump, WinDbg and .pdb files or Visual Studio, you can get more infomation about the crash.
Using CrashDump, Dump files and WinDbg or Visual Studio, you can get more infomation about the crash.
Binary file added asserts/image-20210716124312591.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions include/LoggerShareData.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#define MACHINE_TYPE IMAGE_FILE_MACHINE_AMD64
#define DEAMON_PROCESS_PATH L"./plugins/CrashLogger/CrashLogger_Deamon.exe"
#define TRACKBACK_OUTPUT_PATH "./logs/Crash/TrackBack_"
#define DUMP_OUTPUT_PATH "./logs/Crash/CrashDump_"
#define DAEMON_PROCESS_PATH L"plugins\\CrashLogger\\CrashLogger_Daemon.exe"
#define TRACKBACK_OUTPUT_PATH "logs\\Crash\\TrackBack_"
#define DUMP_OUTPUT_PATH "logs\\Crash\\CrashDump_"

#define CRT_EXCEPTION_CODE 0xE06D7363
#define CRT_RAISE_CODE CRT_EXCEPTION_CODE
Expand Down

0 comments on commit 2f4d4ba

Please sign in to comment.