Skip to content

Commit 335adc8

Browse files
authored
Merge pull request #41 from Jayatubi/master
show local variables of parent threads
2 parents 94c4997 + 9512441 commit 335adc8

File tree

2 files changed

+70
-25
lines changed

2 files changed

+70
-25
lines changed

emmy_debugger/src/emmy_debugger.cpp

Lines changed: 67 additions & 23 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,15 +180,39 @@ 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

215+
int totalLevel = 0;
192216
while(true)
193217
{
194218
int level = 0;
@@ -206,7 +230,7 @@ bool Debugger::GetStacks(std::vector<std::shared_ptr<Stack>>& stacks, StackAlloc
206230
auto stack = alloc();
207231
stack->file = GetFile(&ar);
208232
stack->functionName = getDebugName(&ar) == nullptr ? "" : getDebugName(&ar);
209-
stack->level = level;
233+
stack->level = totalLevel++;
210234
stack->line = getDebugCurrentLine(&ar);
211235
stacks.push_back(stack);
212236
// get variables
@@ -258,22 +282,7 @@ bool Debugger::GetStacks(std::vector<std::shared_ptr<Stack>>& stacks, StackAlloc
258282
level++;
259283
}
260284

261-
lua_State* PL = nullptr;
262-
const int t = lua_gettop(L);
263-
lua_getglobal(L, "emmyHelper");
264-
if (lua_istable(L, -1))
265-
{
266-
lua_getfield(L, -1, "queryParentThread");
267-
if (lua_isfunction(L, -1))
268-
{
269-
const auto r = lua_pcall(L, 0, 1, 0);
270-
if (r == LUA_OK)
271-
{
272-
PL = lua_tothread(L, -1);
273-
}
274-
}
275-
}
276-
lua_settop(L, t);
285+
lua_State* PL = queryParentThread(L);
277286

278287
if (PL != nullptr)
279288
{
@@ -284,6 +293,9 @@ bool Debugger::GetStacks(std::vector<std::shared_ptr<Stack>>& stacks, StackAlloc
284293
break;
285294
}
286295
}
296+
297+
SetCurrentState(prevCurrentL);
298+
287299
return false;
288300
}
289301

@@ -838,15 +850,13 @@ int EnvIndexFunction(lua_State* L)
838850
return 0;
839851
}
840852

841-
bool Debugger::CreateEnv(int stackLevel)
853+
bool Debugger::CreateEnv(lua_State* L, int stackLevel)
842854
{
843-
if (!currentL)
855+
if (!L)
844856
{
845857
return false;
846858
}
847859

848-
auto L = currentL;
849-
850860

851861
//assert(L);
852862
//const auto L = L;
@@ -1035,6 +1045,19 @@ bool Debugger::Eval(std::shared_ptr<EvalContext> evalContext, bool force)
10351045
return true;
10361046
}
10371047

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+
10381061
// host thread
10391062
bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10401063
{
@@ -1045,6 +1068,27 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10451068

10461069
auto L = currentL;
10471070

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+
10481092
//auto* const L = L;
10491093
// From "cacheId"
10501094
if (evalContext->cacheId > 0)
@@ -1075,7 +1119,7 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10751119
// call
10761120
const int fIdx = lua_gettop(L);
10771121
// create env
1078-
if (!CreateEnv(evalContext->stackLevel))
1122+
if (!CreateEnv(L, innerLevel))
10791123
return false;
10801124
// setup env
10811125
#ifndef EMMY_USE_LUA_SOURCE

include/emmy_debugger/emmy_debugger.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class Debugger : public std::enable_shared_from_this<Debugger>
6363
* 判断当前使用的lua_state 是否是main state
6464
*/
6565
bool IsMainCoroutine(lua_State* L) const;
66-
/*
66+
lua_State* queryParentThread(lua_State* L);
67+
/*
6768
* 推迟到lua线程执行
6869
*/
6970
void AsyncDoString(const std::string& code);
@@ -94,7 +95,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>
9495
std::string GetFile(lua_Debug* ar) const;
9596

9697
void CheckDoString();
97-
bool CreateEnv(int stackLevel);
98+
bool CreateEnv(lua_State* L, int stackLevel);
9899
bool ProcessBreakPoint(std::shared_ptr<BreakPoint> bp);
99100
bool DoEval(std::shared_ptr<EvalContext> evalContext);
100101
void DoLogMessage(std::shared_ptr<BreakPoint> bp);

0 commit comments

Comments
 (0)