Skip to content

Commit 8436ceb

Browse files
committed
Merge branch 'master' of github.com:EmmyLua/EmmyLuaDebugger
2 parents b772b3e + 335adc8 commit 8436ceb

File tree

5 files changed

+164
-80
lines changed

5 files changed

+164
-80
lines changed

emmy_debugger/src/api/lua_api_loader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ IMP_LUA_API(lua_type);
6868
IMP_LUA_API(lua_typename);
6969
IMP_LUA_API(lua_tolstring);
7070
IMP_LUA_API(lua_toboolean);
71+
IMP_LUA_API(lua_tothread);
7172
IMP_LUA_API(lua_pushnil);
7273
IMP_LUA_API(lua_pushnumber);
7374
IMP_LUA_API(lua_pushlstring);
@@ -420,6 +421,7 @@ extern "C" bool SetupLuaAPI()
420421
LOAD_LUA_API(lua_typename);
421422
LOAD_LUA_API(lua_tolstring);
422423
LOAD_LUA_API(lua_toboolean);
424+
LOAD_LUA_API(lua_tothread);
423425
LOAD_LUA_API(lua_pushnil);
424426
LOAD_LUA_API(lua_pushnumber);
425427
LOAD_LUA_API(lua_pushlstring);

emmy_debugger/src/emmy_debugger.cpp

Lines changed: 155 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void Debugger::Hook(lua_Debug* ar, lua_State* L)
105105
return;
106106
}
107107
// 设置当前协程
108-
currentL = L;
108+
SetCurrentState(L);
109109

110110
if (getDebugEvent(ar) == LUA_HOOKLINE)
111111
{
@@ -180,81 +180,122 @@ bool Debugger::IsMainCoroutine(lua_State* L) const
180180
return L == mainL;
181181
}
182182

183+
lua_State* Debugger::queryParentThread(lua_State* L)
184+
{
185+
lua_State* PL;
186+
const int t = lua_gettop(L);
187+
lua_getglobal(L, "emmyHelper");
188+
if (lua_istable(L, -1))
189+
{
190+
lua_getfield(L, -1, "queryParentThread");
191+
if (lua_isfunction(L, -1))
192+
{
193+
const auto r = lua_pcall(L, 0, 1, 0);
194+
if (r == LUA_OK)
195+
{
196+
PL = lua_tothread(L, -1);
197+
}
198+
}
199+
}
200+
lua_settop(L, t);
201+
202+
return PL;
203+
}
204+
183205
bool Debugger::GetStacks(std::vector<std::shared_ptr<Stack>>& stacks, StackAllocatorCB alloc)
184206
{
185207
if (!currentL)
186208
{
187209
return false;
188210
}
189211

212+
auto prevCurrentL = currentL;
190213
auto L = currentL;
191214

192-
int level = 0;
193-
while (true)
194-
{
195-
lua_Debug ar{};
196-
if (!lua_getstack(L, level, &ar))
215+
int totalLevel = 0;
216+
while(true)
217+
{
218+
int level = 0;
219+
while (true)
220+
{
221+
lua_Debug ar{};
222+
if (!lua_getstack(L, level, &ar))
223+
{
224+
break;
225+
}
226+
if (!lua_getinfo(L, "nSlu", &ar))
227+
{
228+
continue;
229+
}
230+
auto stack = alloc();
231+
stack->file = GetFile(&ar);
232+
stack->functionName = getDebugName(&ar) == nullptr ? "" : getDebugName(&ar);
233+
stack->level = totalLevel++;
234+
stack->line = getDebugCurrentLine(&ar);
235+
stacks.push_back(stack);
236+
// get variables
237+
{
238+
for (int i = 1;; i++)
239+
{
240+
const char* name = lua_getlocal(L, &ar, i);
241+
if (name == nullptr)
242+
{
243+
break;
244+
}
245+
if (name[0] == '(')
246+
{
247+
lua_pop(L, 1);
248+
continue;
249+
}
250+
251+
// add local variable
252+
auto var = stack->CreateVariable();
253+
var->name = name;
254+
GetVariable(L, var, -1, 1);
255+
lua_pop(L, 1);
256+
stack->localVariables.push_back(var);
257+
}
258+
259+
if (lua_getinfo(L, "f", &ar))
260+
{
261+
const int fIdx = lua_gettop(L);
262+
for (int i = 1;; i++)
263+
{
264+
const char* name = lua_getupvalue(L, fIdx, i);
265+
if (!name)
266+
{
267+
break;
268+
}
269+
270+
// add up variable
271+
auto var = stack->CreateVariable();
272+
var->name = name;
273+
GetVariable(L, var, -1, 1);
274+
lua_pop(L, 1);
275+
stack->upvalueVariables.push_back(var);
276+
}
277+
// pop function
278+
lua_pop(L, 1);
279+
}
280+
}
281+
282+
level++;
283+
}
284+
285+
lua_State* PL = queryParentThread(L);
286+
287+
if (PL != nullptr)
197288
{
198-
break;
289+
L = PL;
199290
}
200-
if (!lua_getinfo(L, "nSlu", &ar))
291+
else
201292
{
202-
continue;
293+
break;
203294
}
204-
auto stack = alloc();
205-
stack->file = GetFile(&ar);
206-
stack->functionName = getDebugName(&ar) == nullptr ? "" : getDebugName(&ar);
207-
stack->level = level;
208-
stack->line = getDebugCurrentLine(&ar);
209-
stacks.push_back(stack);
210-
// get variables
211-
{
212-
for (int i = 1;; i++)
213-
{
214-
const char* name = lua_getlocal(L, &ar, i);
215-
if (name == nullptr)
216-
{
217-
break;
218-
}
219-
if (name[0] == '(')
220-
{
221-
lua_pop(L, 1);
222-
continue;
223-
}
224-
225-
// add local variable
226-
auto var = stack->CreateVariable();
227-
var->name = name;
228-
GetVariable(var, -1, 1);
229-
lua_pop(L, 1);
230-
stack->localVariables.push_back(var);
231-
}
232-
233-
if (lua_getinfo(L, "f", &ar))
234-
{
235-
const int fIdx = lua_gettop(L);
236-
for (int i = 1;; i++)
237-
{
238-
const char* name = lua_getupvalue(L, fIdx, i);
239-
if (!name)
240-
{
241-
break;
242-
}
295+
}
243296

244-
// add up variable
245-
auto var = stack->CreateVariable();
246-
var->name = name;
247-
GetVariable(var, -1, 1);
248-
lua_pop(L, 1);
249-
stack->upvalueVariables.push_back(var);
250-
}
251-
// pop function
252-
lua_pop(L, 1);
253-
}
254-
}
297+
SetCurrentState(prevCurrentL);
255298

256-
level++;
257-
}
258299
return false;
259300
}
260301

@@ -380,15 +421,21 @@ void DisplayFunction(std::shared_ptr<Variable> variable, lua_State* L, int index
380421
}
381422

382423
// algorithm optimization
383-
void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int depth, bool queryHelper)
424+
void Debugger::GetVariable(lua_State* L, std::shared_ptr<Variable> variable, int index, int depth, bool queryHelper)
384425
{
385-
if (!currentL)
426+
//if (!currentL)
427+
//{
428+
// return;
429+
//}
430+
431+
//auto L = currentL;
432+
if (!L) L = currentL;
433+
434+
if (!L)
386435
{
387436
return;
388437
}
389438

390-
auto L = currentL;
391-
392439
// 如果没有计算深度则不予计算
393440
if (depth <= 0)
394441
{
@@ -458,7 +505,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
458505
{
459506
if (lua_getmetatable(L, index))
460507
{
461-
GetVariable(variable, -1, depth);
508+
GetVariable(L, variable, -1, depth);
462509
lua_pop(L, 1); //pop meta
463510
}
464511
}
@@ -499,7 +546,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
499546
{
500547
v->name = ToPointer(L, -2);
501548
}
502-
GetVariable(v, -1, depth - 1);
549+
GetVariable(L, v, -1, depth - 1);
503550
variable->children.push_back(v);
504551
}
505552
lua_pop(L, 1);
@@ -514,7 +561,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
514561
metatable->name = "(metatable)";
515562
metatable->nameType = lua_type(L, -1);
516563

517-
GetVariable(metatable, -1, depth - 1);
564+
GetVariable(L, metatable, -1, depth - 1);
518565
variable->children.push_back(metatable);
519566

520567
//__index
@@ -528,7 +575,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
528575
auto v = std::make_shared<Variable>();
529576
v->name = "(metatable.__index)";
530577
v->nameType = lua_type(L, -1);
531-
GetVariable(v, -1, depth - 1);
578+
GetVariable(L, v, -1, depth - 1);
532579
variable->children.push_back(v);
533580
}
534581
lua_pop(L, 1);
@@ -803,15 +850,13 @@ int EnvIndexFunction(lua_State* L)
803850
return 0;
804851
}
805852

806-
bool Debugger::CreateEnv(int stackLevel)
853+
bool Debugger::CreateEnv(lua_State* L, int stackLevel)
807854
{
808-
if (!currentL)
855+
if (!L)
809856
{
810857
return false;
811858
}
812859

813-
auto L = currentL;
814-
815860

816861
//assert(L);
817862
//const auto L = L;
@@ -1000,6 +1045,19 @@ bool Debugger::Eval(std::shared_ptr<EvalContext> evalContext, bool force)
10001045
return true;
10011046
}
10021047

1048+
int lastlevel(lua_State* L)
1049+
{
1050+
int level = 0;
1051+
1052+
lua_Debug ar;
1053+
while(lua_getstack(L, level, &ar))
1054+
{
1055+
level++;
1056+
}
1057+
1058+
return level;
1059+
}
1060+
10031061
// host thread
10041062
bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10051063
{
@@ -1010,6 +1068,27 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10101068

10111069
auto L = currentL;
10121070

1071+
int innerLevel = evalContext->stackLevel;
1072+
1073+
while (L != nullptr)
1074+
{
1075+
int level = lastlevel(L);
1076+
if (innerLevel > level)
1077+
{
1078+
innerLevel -= level;
1079+
L = queryParentThread(L);
1080+
}
1081+
else
1082+
{
1083+
break;
1084+
}
1085+
}
1086+
1087+
if (L == nullptr)
1088+
{
1089+
return false;
1090+
}
1091+
10131092
//auto* const L = L;
10141093
// From "cacheId"
10151094
if (evalContext->cacheId > 0)
@@ -1018,7 +1097,7 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10181097
if (lua_type(L, -1) == LUA_TTABLE)
10191098
{
10201099
lua_getfield(L, -1, std::to_string(evalContext->cacheId).c_str()); // 1: cacheTable, 2: value
1021-
GetVariable(evalContext->result, -1, evalContext->depth);
1100+
GetVariable(L, evalContext->result, -1, evalContext->depth);
10221101
lua_pop(L, 2);
10231102
return true;
10241103
}
@@ -1040,7 +1119,7 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10401119
// call
10411120
const int fIdx = lua_gettop(L);
10421121
// create env
1043-
if (!CreateEnv(evalContext->stackLevel))
1122+
if (!CreateEnv(L, innerLevel))
10441123
return false;
10451124
// setup env
10461125
#ifndef EMMY_USE_LUA_SOURCE
@@ -1056,7 +1135,7 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10561135
if (r == LUA_OK)
10571136
{
10581137
evalContext->result->name = evalContext->expr;
1059-
GetVariable(evalContext->result, -1, evalContext->depth);
1138+
GetVariable(L, evalContext->result, -1, evalContext->depth);
10601139
lua_pop(L, 1);
10611140
return true;
10621141
}

emmy_debugger/src/emmy_helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int metaQuery(lua_State* L)
3939

4040
if (debugger && variable)
4141
{
42-
debugger->GetVariable(variable, index, static_cast<int>(depth), queryHelper);
42+
debugger->GetVariable(L, variable, index, static_cast<int>(depth), queryHelper);
4343
}
4444
return 0;
4545
}

include/emmy_debugger/api/lua_api_loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ typedef const char*(*dll_lua_tolstring)(lua_State* L, int idx, size_t* len);
257257
DEF_LUA_API(lua_tolstring);
258258
typedef int (*dll_lua_toboolean)(lua_State* L, int idx);
259259
DEF_LUA_API(lua_toboolean);
260+
typedef lua_State* (*dll_lua_tothread)(lua_State* L, int idx);
261+
DEF_LUA_API(lua_tothread);
260262
typedef void (*dll_lua_pushnil)(lua_State* L);
261263
DEF_LUA_API(lua_pushnil);
262264
typedef void (*dll_lua_pushnumber)(lua_State* L, lua_Number n);

0 commit comments

Comments
 (0)