#include "_global.h" #include "argument.h" #include "command.h" #include "variable.h" #include "instruction.h" #include "debugger.h" #include "data.h" #include "simplescript.h" #include "console.h" #include "math.h" #include "x64_dbg.h" #include "msgqueue.h" #include "addrinfo.h" #include "threading.h" #include "plugin_loader.h" #include "assemble.h" #include "_dbgfunctions.h" static MESSAGE_STACK* gMsgStack=0; static COMMAND* command_list=0; static HANDLE hCommandLoopThread=0; static char alloctrace[MAX_PATH]=""; //Original code by Aurel from http://www.codeguru.com/cpp/w-p/win32/article.php/c1427/A-Simple-Win32-CommandLine-Parser.htm static void commandlinefree(int argc, char** argv) { for(int i=0; i=deflen) newcmd[deflen-1]=0; strcpy(cmd, newcmd); efree(newcmd, "cbCommandProvider:newcmd"); //free allocated command return true; } extern "C" DLL_EXPORT bool _dbg_dbgcmdexec(const char* cmd) { int len=strlen(cmd); char* newcmd=(char*)emalloc((len+1)*sizeof(char), "_dbg_dbgcmdexec:newcmd"); strcpy(newcmd, cmd); return msgsend(gMsgStack, 0, (uint)newcmd, 0); } static DWORD WINAPI DbgCommandLoopThread(void* a) { cmdloop(command_list, cbBadCmd, cbCommandProvider, cmdfindmain, false); return 0; } static void* emalloc_json(size_t size) { return emalloc(size, "json:ptr"); } static void efree_json(void* ptr) { efree(ptr, "json:ptr"); } extern "C" DLL_EXPORT const char* _dbg_dbginit() { dbginit(); dbgfunctionsinit(); json_set_alloc_funcs(emalloc_json, efree_json); char dir[deflen]=""; if(!GetModuleFileNameA(hInst, dir, deflen)) return "GetModuleFileNameA failed!"; int len=strlen(dir); while(dir[len]!='\\') len--; dir[len]=0; strcpy(alloctrace, dir); PathAppendA(alloctrace, "\\alloctrace.txt"); DeleteFileA(alloctrace); setalloctrace(alloctrace); strcpy(dbbasepath, dir); //debug directory PathAppendA(dbbasepath, "db"); CreateDirectoryA(dbbasepath, 0); //create database directory SetCurrentDirectoryA(dir); gMsgStack=msgallocstack(); if(!gMsgStack) return "Could not allocate message stack!"; varinit(); registercommands(); hCommandLoopThread=CreateThread(0, 0, DbgCommandLoopThread, 0, 0, 0); char plugindir[deflen]=""; strcpy(plugindir, dir); PathAppendA(plugindir, "plugins"); pluginload(plugindir); //handle command line int argc=0; char** argv=commandlineparse(&argc); if(argc>1) //we have an argument { std::string str="init \""; str+=argv[1]; str+="\""; DbgCmdExec(str.c_str()); } commandlinefree(argc, argv); return 0; } extern "C" DLL_EXPORT void _dbg_dbgexitsignal() { cbStopDebug(0, 0); scriptabort(); wait(WAITID_STOP); //after this, debugging stopped pluginunload(); TerminateThread(hCommandLoopThread, 0); CloseHandle(hCommandLoopThread); cmdfree(command_list); varfree(); msgfreestack(gMsgStack); if(memleaks()) { char msg[256]=""; sprintf(msg, "%d memory leak(s) found!\n\nPlease send 'alloctrace.txt' to the authors of x64_dbg.", memleaks()); MessageBoxA(0, msg, "error", MB_ICONERROR|MB_SYSTEMMODAL); } else DeleteFileA(alloctrace); } extern "C" DLL_EXPORT bool _dbg_dbgcmddirectexec(const char* cmd) { if(cmddirectexec(command_list, cmd)==STATUS_ERROR) return false; return true; } COMMAND* dbggetcommandlist() { return command_list; }