Skip to content

Commit

Permalink
Revert PR. Since many bugs were reported
Browse files Browse the repository at this point in the history
  • Loading branch information
tangzx committed Aug 14, 2019
1 parent 03ad197 commit c442e44
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 390 deletions.
6 changes: 3 additions & 3 deletions src/EasyHookDll/LocalHook/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ typedef LONG ZwQueryObject_PROC(
ULONG InInfoSize,
PULONG OutRequiredSize);

typedef struct _DBG_CLIENT_ID
typedef struct _CLIENT_ID
{
DWORD UniqueProcess;
DWORD UniqueThread;
}DBG_CLIENT_ID, * PDBG_CLIENT_ID;
}CLIENT_ID, * PCLIENT_ID;

typedef struct _THREAD_BASIC_INFORMATION
{
LONG ExitStatus;
PNT_TIB TebBaseAddress;
DBG_CLIENT_ID ClientId;
CLIENT_ID ClientId;
DWORD AffinityMask;
LONG Priority;
LONG BasePriority;
Expand Down
105 changes: 13 additions & 92 deletions src/Shared/WindowUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <tlhelp32.h>
#include <psapi.h>
#include <shlobj.h>
#include <algorithm>

HWND GetProcessWindow(DWORD processId)
{
Expand Down Expand Up @@ -34,34 +33,6 @@ HWND GetProcessWindow(DWORD processId)

}

inline char char_tolower(char c) {
return (char)tolower(c);
}

inline char char_toupper(char c) {
return (char)toupper(c);
}


static void EmmyToLowerCase(std::string& str)
{
std::transform(
str.begin(),
str.end(),
str.begin(),
char_tolower);
}

//-----------------------------------------------------------------------
static void EmmyToUpperCase(std::string& str)
{
std::transform(
str.begin(),
str.end(),
str.begin(),
char_toupper);
}

void GetProcesses(std::vector<Process>& processes)
{
static char fileName[_MAX_PATH];
Expand All @@ -79,21 +50,11 @@ void GetProcesses(std::vector<Process>& processes)

if (Process32First(snapshot, &processEntry))
{
char windowsPath[MAX_PATH] = { 0 };
bool isGetWindowsPath = false;
std::string strWinPath;
if (SHGetFolderPath(nullptr, CSIDL_WINDOWS, nullptr, SHGFP_TYPE_CURRENT, windowsPath) == 0)
{
strWinPath = windowsPath;
EmmyToLowerCase(strWinPath);
isGetWindowsPath = true;
}


do
{
if (processEntry.th32ProcessID != currentProcessId && processEntry.th32ProcessID != 0)
{

Process process;

process.id = processEntry.th32ProcessID;
Expand All @@ -106,62 +67,22 @@ void GetProcesses(std::vector<Process>& processes)
process.path = "error";
else process.path = fileName;

EmmyToLowerCase(process.path);
if (!process.path.empty()) {
char windowsPath[MAX_PATH];
if (SHGetFolderPath(nullptr, CSIDL_WINDOWS, nullptr, SHGFP_TYPE_CURRENT, windowsPath) == 0) {
if (process.path.find(windowsPath) == std::string::npos) {

if (!process.path.empty())
{
if (isGetWindowsPath && process.path.find(strWinPath.c_str()) != std::string::npos)
{
//on windows path exe
continue;
}
HWND hWnd = GetProcessWindow(processEntry.th32ProcessID);

std::string skipExeNameList[] = {
"360se",
"MSBuild",
"vcpkgsrv",
"ServiceHub",
"VcxprojReader",
"mspdbsrv",
"TGitCache",
"TortoiseGitProc",
"devenv.exe",
"PerfWatson2",
"TSVNCache",
"steamwebhelper",
"UnrealCEFSubProcess",
"Microsoft.",
"VaCodeInspectionsServer",
"Steam.exe",
};


size_t totalSkip = sizeof(skipExeNameList) / sizeof(skipExeNameList[0]);
bool needSkip = false;
for (int i = 0; i < totalSkip; i++)
{
EmmyToLowerCase(skipExeNameList[i]);
needSkip = process.path.find(skipExeNameList[i].c_str()) != std::string::npos;
if (needSkip)
{
break;
if (hWnd != nullptr)
{
char buffer[1024];
GetWindowText(hWnd, buffer, 1024);
process.title = buffer;
}
processes.push_back(process);
}
}

if (needSkip)
{
continue;
}

HWND hWnd = GetProcessWindow(processEntry.th32ProcessID);

if (hWnd != nullptr)
{
char buffer[1024];
GetWindowText(hWnd, buffer, 1024);
process.title = buffer;
}
processes.push_back(process);
}
}
}
Expand Down
42 changes: 2 additions & 40 deletions src/emmy.arch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
#include <stdio.h>
#include <string>
#include <psapi.h>
#include <algorithm>
#include "Utility.h"
#include "WindowUtility.h"

using namespace std;



int main(int argc, char** argv)
{
string cmd = argv[1];
Expand All @@ -32,45 +29,10 @@ int main(int argc, char** argv)
HANDLE m_process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
GetModuleFileNameEx(m_process, nullptr, fileName, _MAX_PATH);

USHORT processMachine = 0, nativeMachine = 0;

typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process = nullptr;

typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS2) (HANDLE, USHORT*, USHORT*);
LPFN_ISWOW64PROCESS2 fnIsWow64Process2 = nullptr;

fnIsWow64Process2 = (LPFN_ISWOW64PROCESS2)GetProcAddress(
GetModuleHandle(TEXT("kernel32")), "IsWow64Process2");

fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
GetModuleHandle(TEXT("kernel32")), "IsWow64Process");

////fnIsWow64Process2 = nullptr;

ExeInfo info;
if (GetExeInfo(fileName, info)) {
if (!info.managed)
{
printf("%d", info.i386);
return info.i386;
}
else
{
BOOL is64bit = FALSE;
if (fnIsWow64Process2)
{
is64bit = fnIsWow64Process2(m_process, &processMachine, &nativeMachine);
}
else if (fnIsWow64Process)
{
is64bit = fnIsWow64Process(m_process, &is64bit);
}

printf("%d", !is64bit);
return !is64bit;
}

printf("%d", info.i386);
return info.i386;
}
}
}
Expand Down
62 changes: 17 additions & 45 deletions src/emmy.backend/DebugBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,6 @@ void DebugBackend::Message(MessageType type, const char *fmt, ...) const

void DebugBackend::HookCallback(LAPI api, lua_State* L, lua_Debug* ar)
{
{
CriticalSectionLock tmpInstallHookLock(m_debugHookInstallLock);
}


m_criticalSection.Enter();

Expand Down Expand Up @@ -851,7 +847,7 @@ void DebugBackend::HookCallback(LAPI api, lua_State* L, lua_Debug* ar)
vm = iterator->second;
}

//TODO: Same L call by not same lua
//TODO: 同一个L在不同的lua代码里被调用?
//assert(vm->api == api);
if (vm->api != api) {
api = vm->api;
Expand Down Expand Up @@ -1000,11 +996,8 @@ void DebugBackend::HookCallback(LAPI api, lua_State* L, lua_Debug* ar)
if (m_mode == Mode_StepInto) {
stop = true;
}
else if (m_mode == Mode_StepOver)
{
int stackDepth = GetStackDepth(api, L);
stop = stop || (vm->callStackDepth >= stackDepth);
////stop = stop || (vm->callCount == 0);
else if (m_mode == Mode_StepOver) {
stop = vm->callCount == 0;
}
else if (m_mode == Mode_StepOut) {
int stackDepth = GetStackDepth(api, L);
Expand Down Expand Up @@ -1245,12 +1238,8 @@ void DebugBackend::HandleMessage(DebugMessage* message)
{
case DebugMessageId::ReqInitialize:
{
CriticalSectionLock tmpInstallLock(m_debugHookInstallLock);

DMReqInitialize* init_emmy = dynamic_cast<DMReqInitialize*>(message);
if (!m_hooked) {


m_hooked = InstallLuaHooker(g_hInstance, init_emmy->emmyLuaFile.c_str());
if (m_hooked && init_emmy->captureOutputDebugString)
HookOuputDebugString();
Expand Down Expand Up @@ -1851,14 +1840,7 @@ int DebugBackend::ErrorHandler(LAPI api, lua_State* L)

if (!lua_isnil_dll(api, L, -1))
{
////lua_pushvalue_dll(api, L, -2);

////for (int i = -1; i>=-3; i--)
////{
//// int type = lua_type_dll(api, L, i);
//// const char* typeName = lua_typename_dll(api, L, type);
////}
lua_pushstring_dll(api, L, message);
lua_pushvalue_dll(api, L, -2);
lua_pcall_dll(api, L, 1, 1, 0);
}
else
Expand Down Expand Up @@ -2023,18 +2005,17 @@ bool DebugBackend::CreateEnvironment(LAPI api, lua_State* L, int stackLevel, int

int IndexChained_worker(LAPI api, lua_State* L)
{

LUA_CHECK_STACK(api, L, 1)

int key = 2;
int key = 2;

int nilSentinel = lua_upvalueindex_dll(api, 1);

int table[2];
int table[3];
table[0] = lua_upvalueindex_dll(api, 2); // Locals
table[1] = lua_upvalueindex_dll(api, 3); // Up values


////table[2] = lua_upvalueindex_dll(api, 4); // Globals
table[2] = lua_upvalueindex_dll(api, 4); // Globals

// Get from the local table.
lua_pushvalue_dll(api, L, key);
Expand All @@ -2052,14 +2033,8 @@ int IndexChained_worker(LAPI api, lua_State* L)
if (lua_isnil_dll(api, L, -1))
{
lua_pop_dll(api, L, 1);

//Modify here to use push global help function(serve 5.1 & 5.2 & 5.3)
lua_pushglobaltable_dll(api, L);
lua_pushvalue_dll(api, L, key);

lua_gettable_dll(api, L, -2);
lua_remove_dll(api, L, -2); //remove global table

lua_gettable_dll(api, L, table[2]);
}

// If the value is our nil sentinel, convert it to an actual nil.
Expand Down Expand Up @@ -2089,17 +2064,18 @@ int DebugBackend::IndexChained_intercept(lua_State* L)

int NewIndexChained_worker(LAPI api, lua_State* L)
{

LUA_CHECK_STACK(api, L, 0)

int key = 2;
int key = 2;
int value = 3;

int nilSentinel = lua_upvalueindex_dll(api, 1);

int table[2];
int table[3];
table[0] = lua_upvalueindex_dll(api, 2); // Locals
table[1] = lua_upvalueindex_dll(api, 3); // Up values
////table[2] = lua_upvalueindex_dll(api, 4); // Globals
table[2] = lua_upvalueindex_dll(api, 4); // Globals

// Try to set the value in the local table.

Expand Down Expand Up @@ -2135,13 +2111,9 @@ int NewIndexChained_worker(LAPI api, lua_State* L)
}

// Set on the global table.
lua_pushglobaltable_dll(api, L);

lua_pushvalue_dll(api, L, key);
lua_pushvalue_dll(api, L, value);
lua_settable_dll(api, L, -2);

lua_pop_dll(api, L, 1); //pop global table
lua_settable_dll(api, L, table[2]);

return 0;

Expand Down Expand Up @@ -2419,7 +2391,7 @@ EvalResultNode* DebugBackend::Evaluate(LAPI api, lua_State* L, const std::string

}

//restore run enviroment, not need in MS?
//恢复现场,MS不恢复也没问题?
// Copy any changes to the up values due to evaluating the watch back.
SetLocals(api, L, stackLevel, localTable, nilSentinel);
SetUpValues(api, L, stackLevel, upValueTable, nilSentinel);
Expand Down Expand Up @@ -2459,7 +2431,7 @@ bool DebugBackend::CallMetaMethod(LAPI api, lua_State* L, int valueIndex, const
{

lua_pushstring_dll(api, L, method);
//lua_gettable_dll(api, L, metaTableIndex); //get table in to lua will failed
//lua_gettable_dll(api, L, metaTableIndex); //在tolua中直接获取table字段会挂的
lua_rawget_dll(api, L, metaTableIndex);

if (lua_isnil_dll(api, L, -1))
Expand Down Expand Up @@ -2604,7 +2576,7 @@ StackLuaObjectNode* DebugBackend::GetValueAsText(LAPI api, lua_State* L, int n,

if (askEmmy)
{
//save value index
//存value index
lua_pushvalue_dll(api, L, n);
int valueIndex = lua_gettop_dll(api, L);

Expand Down

0 comments on commit c442e44

Please sign in to comment.