Skip to content

Commit c442e44

Browse files
committed
Revert PR. Since many bugs were reported
1 parent 03ad197 commit c442e44

File tree

8 files changed

+195
-390
lines changed

8 files changed

+195
-390
lines changed

src/EasyHookDll/LocalHook/debug.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,17 @@ typedef LONG ZwQueryObject_PROC(
6969
ULONG InInfoSize,
7070
PULONG OutRequiredSize);
7171

72-
typedef struct _DBG_CLIENT_ID
72+
typedef struct _CLIENT_ID
7373
{
7474
DWORD UniqueProcess;
7575
DWORD UniqueThread;
76-
}DBG_CLIENT_ID, * PDBG_CLIENT_ID;
76+
}CLIENT_ID, * PCLIENT_ID;
7777

7878
typedef struct _THREAD_BASIC_INFORMATION
7979
{
8080
LONG ExitStatus;
8181
PNT_TIB TebBaseAddress;
82-
DBG_CLIENT_ID ClientId;
82+
CLIENT_ID ClientId;
8383
DWORD AffinityMask;
8484
LONG Priority;
8585
LONG BasePriority;

src/Shared/WindowUtility.cpp

Lines changed: 13 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <tlhelp32.h>
33
#include <psapi.h>
44
#include <shlobj.h>
5-
#include <algorithm>
65

76
HWND GetProcessWindow(DWORD processId)
87
{
@@ -34,34 +33,6 @@ HWND GetProcessWindow(DWORD processId)
3433

3534
}
3635

37-
inline char char_tolower(char c) {
38-
return (char)tolower(c);
39-
}
40-
41-
inline char char_toupper(char c) {
42-
return (char)toupper(c);
43-
}
44-
45-
46-
static void EmmyToLowerCase(std::string& str)
47-
{
48-
std::transform(
49-
str.begin(),
50-
str.end(),
51-
str.begin(),
52-
char_tolower);
53-
}
54-
55-
//-----------------------------------------------------------------------
56-
static void EmmyToUpperCase(std::string& str)
57-
{
58-
std::transform(
59-
str.begin(),
60-
str.end(),
61-
str.begin(),
62-
char_toupper);
63-
}
64-
6536
void GetProcesses(std::vector<Process>& processes)
6637
{
6738
static char fileName[_MAX_PATH];
@@ -79,21 +50,11 @@ void GetProcesses(std::vector<Process>& processes)
7950

8051
if (Process32First(snapshot, &processEntry))
8152
{
82-
char windowsPath[MAX_PATH] = { 0 };
83-
bool isGetWindowsPath = false;
84-
std::string strWinPath;
85-
if (SHGetFolderPath(nullptr, CSIDL_WINDOWS, nullptr, SHGFP_TYPE_CURRENT, windowsPath) == 0)
86-
{
87-
strWinPath = windowsPath;
88-
EmmyToLowerCase(strWinPath);
89-
isGetWindowsPath = true;
90-
}
91-
92-
9353
do
9454
{
9555
if (processEntry.th32ProcessID != currentProcessId && processEntry.th32ProcessID != 0)
9656
{
57+
9758
Process process;
9859

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

109-
EmmyToLowerCase(process.path);
70+
if (!process.path.empty()) {
71+
char windowsPath[MAX_PATH];
72+
if (SHGetFolderPath(nullptr, CSIDL_WINDOWS, nullptr, SHGFP_TYPE_CURRENT, windowsPath) == 0) {
73+
if (process.path.find(windowsPath) == std::string::npos) {
11074

111-
if (!process.path.empty())
112-
{
113-
if (isGetWindowsPath && process.path.find(strWinPath.c_str()) != std::string::npos)
114-
{
115-
//on windows path exe
116-
continue;
117-
}
75+
HWND hWnd = GetProcessWindow(processEntry.th32ProcessID);
11876

119-
std::string skipExeNameList[] = {
120-
"360se",
121-
"MSBuild",
122-
"vcpkgsrv",
123-
"ServiceHub",
124-
"VcxprojReader",
125-
"mspdbsrv",
126-
"TGitCache",
127-
"TortoiseGitProc",
128-
"devenv.exe",
129-
"PerfWatson2",
130-
"TSVNCache",
131-
"steamwebhelper",
132-
"UnrealCEFSubProcess",
133-
"Microsoft.",
134-
"VaCodeInspectionsServer",
135-
"Steam.exe",
136-
};
137-
138-
139-
size_t totalSkip = sizeof(skipExeNameList) / sizeof(skipExeNameList[0]);
140-
bool needSkip = false;
141-
for (int i = 0; i < totalSkip; i++)
142-
{
143-
EmmyToLowerCase(skipExeNameList[i]);
144-
needSkip = process.path.find(skipExeNameList[i].c_str()) != std::string::npos;
145-
if (needSkip)
146-
{
147-
break;
77+
if (hWnd != nullptr)
78+
{
79+
char buffer[1024];
80+
GetWindowText(hWnd, buffer, 1024);
81+
process.title = buffer;
82+
}
83+
processes.push_back(process);
14884
}
14985
}
150-
151-
if (needSkip)
152-
{
153-
continue;
154-
}
155-
156-
HWND hWnd = GetProcessWindow(processEntry.th32ProcessID);
157-
158-
if (hWnd != nullptr)
159-
{
160-
char buffer[1024];
161-
GetWindowText(hWnd, buffer, 1024);
162-
process.title = buffer;
163-
}
164-
processes.push_back(process);
16586
}
16687
}
16788
}

src/emmy.arch/main.cpp

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
#include <stdio.h>
33
#include <string>
44
#include <psapi.h>
5-
#include <algorithm>
65
#include "Utility.h"
76
#include "WindowUtility.h"
87

98
using namespace std;
109

11-
12-
1310
int main(int argc, char** argv)
1411
{
1512
string cmd = argv[1];
@@ -32,45 +29,10 @@ int main(int argc, char** argv)
3229
HANDLE m_process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
3330
GetModuleFileNameEx(m_process, nullptr, fileName, _MAX_PATH);
3431

35-
USHORT processMachine = 0, nativeMachine = 0;
36-
37-
typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
38-
LPFN_ISWOW64PROCESS fnIsWow64Process = nullptr;
39-
40-
typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS2) (HANDLE, USHORT*, USHORT*);
41-
LPFN_ISWOW64PROCESS2 fnIsWow64Process2 = nullptr;
42-
43-
fnIsWow64Process2 = (LPFN_ISWOW64PROCESS2)GetProcAddress(
44-
GetModuleHandle(TEXT("kernel32")), "IsWow64Process2");
45-
46-
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
47-
GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
48-
49-
////fnIsWow64Process2 = nullptr;
50-
5132
ExeInfo info;
5233
if (GetExeInfo(fileName, info)) {
53-
if (!info.managed)
54-
{
55-
printf("%d", info.i386);
56-
return info.i386;
57-
}
58-
else
59-
{
60-
BOOL is64bit = FALSE;
61-
if (fnIsWow64Process2)
62-
{
63-
is64bit = fnIsWow64Process2(m_process, &processMachine, &nativeMachine);
64-
}
65-
else if (fnIsWow64Process)
66-
{
67-
is64bit = fnIsWow64Process(m_process, &is64bit);
68-
}
69-
70-
printf("%d", !is64bit);
71-
return !is64bit;
72-
}
73-
34+
printf("%d", info.i386);
35+
return info.i386;
7436
}
7537
}
7638
}

src/emmy.backend/DebugBackend.cpp

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -818,10 +818,6 @@ void DebugBackend::Message(MessageType type, const char *fmt, ...) const
818818

819819
void DebugBackend::HookCallback(LAPI api, lua_State* L, lua_Debug* ar)
820820
{
821-
{
822-
CriticalSectionLock tmpInstallHookLock(m_debugHookInstallLock);
823-
}
824-
825821

826822
m_criticalSection.Enter();
827823

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

854-
//TODO: Same L call by not same lua
850+
//TODO: 同一个L在不同的lua代码里被调用?
855851
//assert(vm->api == api);
856852
if (vm->api != api) {
857853
api = vm->api;
@@ -1000,11 +996,8 @@ void DebugBackend::HookCallback(LAPI api, lua_State* L, lua_Debug* ar)
1000996
if (m_mode == Mode_StepInto) {
1001997
stop = true;
1002998
}
1003-
else if (m_mode == Mode_StepOver)
1004-
{
1005-
int stackDepth = GetStackDepth(api, L);
1006-
stop = stop || (vm->callStackDepth >= stackDepth);
1007-
////stop = stop || (vm->callCount == 0);
999+
else if (m_mode == Mode_StepOver) {
1000+
stop = vm->callCount == 0;
10081001
}
10091002
else if (m_mode == Mode_StepOut) {
10101003
int stackDepth = GetStackDepth(api, L);
@@ -1245,12 +1238,8 @@ void DebugBackend::HandleMessage(DebugMessage* message)
12451238
{
12461239
case DebugMessageId::ReqInitialize:
12471240
{
1248-
CriticalSectionLock tmpInstallLock(m_debugHookInstallLock);
1249-
12501241
DMReqInitialize* init_emmy = dynamic_cast<DMReqInitialize*>(message);
12511242
if (!m_hooked) {
1252-
1253-
12541243
m_hooked = InstallLuaHooker(g_hInstance, init_emmy->emmyLuaFile.c_str());
12551244
if (m_hooked && init_emmy->captureOutputDebugString)
12561245
HookOuputDebugString();
@@ -1851,14 +1840,7 @@ int DebugBackend::ErrorHandler(LAPI api, lua_State* L)
18511840

18521841
if (!lua_isnil_dll(api, L, -1))
18531842
{
1854-
////lua_pushvalue_dll(api, L, -2);
1855-
1856-
////for (int i = -1; i>=-3; i--)
1857-
////{
1858-
//// int type = lua_type_dll(api, L, i);
1859-
//// const char* typeName = lua_typename_dll(api, L, type);
1860-
////}
1861-
lua_pushstring_dll(api, L, message);
1843+
lua_pushvalue_dll(api, L, -2);
18621844
lua_pcall_dll(api, L, 1, 1, 0);
18631845
}
18641846
else
@@ -2023,18 +2005,17 @@ bool DebugBackend::CreateEnvironment(LAPI api, lua_State* L, int stackLevel, int
20232005

20242006
int IndexChained_worker(LAPI api, lua_State* L)
20252007
{
2008+
20262009
LUA_CHECK_STACK(api, L, 1)
20272010

2028-
int key = 2;
2011+
int key = 2;
20292012

20302013
int nilSentinel = lua_upvalueindex_dll(api, 1);
20312014

2032-
int table[2];
2015+
int table[3];
20332016
table[0] = lua_upvalueindex_dll(api, 2); // Locals
20342017
table[1] = lua_upvalueindex_dll(api, 3); // Up values
2035-
2036-
2037-
////table[2] = lua_upvalueindex_dll(api, 4); // Globals
2018+
table[2] = lua_upvalueindex_dll(api, 4); // Globals
20382019

20392020
// Get from the local table.
20402021
lua_pushvalue_dll(api, L, key);
@@ -2052,14 +2033,8 @@ int IndexChained_worker(LAPI api, lua_State* L)
20522033
if (lua_isnil_dll(api, L, -1))
20532034
{
20542035
lua_pop_dll(api, L, 1);
2055-
2056-
//Modify here to use push global help function(serve 5.1 & 5.2 & 5.3)
2057-
lua_pushglobaltable_dll(api, L);
20582036
lua_pushvalue_dll(api, L, key);
2059-
2060-
lua_gettable_dll(api, L, -2);
2061-
lua_remove_dll(api, L, -2); //remove global table
2062-
2037+
lua_gettable_dll(api, L, table[2]);
20632038
}
20642039

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

20902065
int NewIndexChained_worker(LAPI api, lua_State* L)
20912066
{
2067+
20922068
LUA_CHECK_STACK(api, L, 0)
20932069

2094-
int key = 2;
2070+
int key = 2;
20952071
int value = 3;
20962072

20972073
int nilSentinel = lua_upvalueindex_dll(api, 1);
20982074

2099-
int table[2];
2075+
int table[3];
21002076
table[0] = lua_upvalueindex_dll(api, 2); // Locals
21012077
table[1] = lua_upvalueindex_dll(api, 3); // Up values
2102-
////table[2] = lua_upvalueindex_dll(api, 4); // Globals
2078+
table[2] = lua_upvalueindex_dll(api, 4); // Globals
21032079

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

@@ -2135,13 +2111,9 @@ int NewIndexChained_worker(LAPI api, lua_State* L)
21352111
}
21362112

21372113
// Set on the global table.
2138-
lua_pushglobaltable_dll(api, L);
2139-
21402114
lua_pushvalue_dll(api, L, key);
21412115
lua_pushvalue_dll(api, L, value);
2142-
lua_settable_dll(api, L, -2);
2143-
2144-
lua_pop_dll(api, L, 1); //pop global table
2116+
lua_settable_dll(api, L, table[2]);
21452117

21462118
return 0;
21472119

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

24202392
}
24212393

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

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

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

26052577
if (askEmmy)
26062578
{
2607-
//save value index
2579+
//存value index
26082580
lua_pushvalue_dll(api, L, n);
26092581
int valueIndex = lua_gettop_dll(api, L);
26102582

0 commit comments

Comments
 (0)