Skip to content

Commit 94c4997

Browse files
authored
Merge pull request #40 from Jayatubi/master
include parent threads into callstack, if privodes
2 parents e338b5c + 2d3aa7b commit 94c4997

File tree

5 files changed

+109
-70
lines changed

5 files changed

+109
-70
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: 103 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -189,71 +189,100 @@ bool Debugger::GetStacks(std::vector<std::shared_ptr<Stack>>& stacks, StackAlloc
189189

190190
auto L = currentL;
191191

192-
int level = 0;
193-
while (true)
194-
{
195-
lua_Debug ar{};
196-
if (!lua_getstack(L, level, &ar))
197-
{
198-
break;
199-
}
200-
if (!lua_getinfo(L, "nSlu", &ar))
201-
{
202-
continue;
203-
}
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
192+
while(true)
193+
{
194+
int level = 0;
195+
while (true)
196+
{
197+
lua_Debug ar{};
198+
if (!lua_getstack(L, level, &ar))
199+
{
200+
break;
201+
}
202+
if (!lua_getinfo(L, "nSlu", &ar))
203+
{
204+
continue;
205+
}
206+
auto stack = alloc();
207+
stack->file = GetFile(&ar);
208+
stack->functionName = getDebugName(&ar) == nullptr ? "" : getDebugName(&ar);
209+
stack->level = level;
210+
stack->line = getDebugCurrentLine(&ar);
211+
stacks.push_back(stack);
212+
// get variables
213+
{
214+
for (int i = 1;; i++)
215+
{
216+
const char* name = lua_getlocal(L, &ar, i);
217+
if (name == nullptr)
218+
{
219+
break;
220+
}
221+
if (name[0] == '(')
222+
{
223+
lua_pop(L, 1);
224+
continue;
225+
}
226+
227+
// add local variable
228+
auto var = stack->CreateVariable();
229+
var->name = name;
230+
GetVariable(L, var, -1, 1);
231+
lua_pop(L, 1);
232+
stack->localVariables.push_back(var);
233+
}
234+
235+
if (lua_getinfo(L, "f", &ar))
236+
{
237+
const int fIdx = lua_gettop(L);
238+
for (int i = 1;; i++)
239+
{
240+
const char* name = lua_getupvalue(L, fIdx, i);
241+
if (!name)
242+
{
243+
break;
244+
}
245+
246+
// add up variable
247+
auto var = stack->CreateVariable();
248+
var->name = name;
249+
GetVariable(L, var, -1, 1);
250+
lua_pop(L, 1);
251+
stack->upvalueVariables.push_back(var);
252+
}
253+
// pop function
254+
lua_pop(L, 1);
255+
}
256+
}
257+
258+
level++;
259+
}
260+
261+
lua_State* PL = nullptr;
262+
const int t = lua_gettop(L);
263+
lua_getglobal(L, "emmyHelper");
264+
if (lua_istable(L, -1))
211265
{
212-
for (int i = 1;; i++)
266+
lua_getfield(L, -1, "queryParentThread");
267+
if (lua_isfunction(L, -1))
213268
{
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++)
269+
const auto r = lua_pcall(L, 0, 1, 0);
270+
if (r == LUA_OK)
237271
{
238-
const char* name = lua_getupvalue(L, fIdx, i);
239-
if (!name)
240-
{
241-
break;
242-
}
243-
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);
272+
PL = lua_tothread(L, -1);
250273
}
251-
// pop function
252-
lua_pop(L, 1);
253274
}
254275
}
276+
lua_settop(L, t);
255277

256-
level++;
278+
if (PL != nullptr)
279+
{
280+
L = PL;
281+
}
282+
else
283+
{
284+
break;
285+
}
257286
}
258287
return false;
259288
}
@@ -380,15 +409,21 @@ void DisplayFunction(std::shared_ptr<Variable> variable, lua_State* L, int index
380409
}
381410

382411
// algorithm optimization
383-
void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int depth, bool queryHelper)
412+
void Debugger::GetVariable(lua_State* L, std::shared_ptr<Variable> variable, int index, int depth, bool queryHelper)
384413
{
385-
if (!currentL)
414+
//if (!currentL)
415+
//{
416+
// return;
417+
//}
418+
419+
//auto L = currentL;
420+
if (!L) L = currentL;
421+
422+
if (!L)
386423
{
387424
return;
388425
}
389426

390-
auto L = currentL;
391-
392427
// 如果没有计算深度则不予计算
393428
if (depth <= 0)
394429
{
@@ -458,7 +493,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
458493
{
459494
if (lua_getmetatable(L, index))
460495
{
461-
GetVariable(variable, -1, depth);
496+
GetVariable(L, variable, -1, depth);
462497
lua_pop(L, 1); //pop meta
463498
}
464499
}
@@ -499,7 +534,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
499534
{
500535
v->name = ToPointer(L, -2);
501536
}
502-
GetVariable(v, -1, depth - 1);
537+
GetVariable(L, v, -1, depth - 1);
503538
variable->children.push_back(v);
504539
}
505540
lua_pop(L, 1);
@@ -514,7 +549,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
514549
metatable->name = "(metatable)";
515550
metatable->nameType = lua_type(L, -1);
516551

517-
GetVariable(metatable, -1, depth - 1);
552+
GetVariable(L, metatable, -1, depth - 1);
518553
variable->children.push_back(metatable);
519554

520555
//__index
@@ -528,7 +563,7 @@ void Debugger::GetVariable(std::shared_ptr<Variable> variable, int index, int de
528563
auto v = std::make_shared<Variable>();
529564
v->name = "(metatable.__index)";
530565
v->nameType = lua_type(L, -1);
531-
GetVariable(v, -1, depth - 1);
566+
GetVariable(L, v, -1, depth - 1);
532567
variable->children.push_back(v);
533568
}
534569
lua_pop(L, 1);
@@ -1018,7 +1053,7 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10181053
if (lua_type(L, -1) == LUA_TTABLE)
10191054
{
10201055
lua_getfield(L, -1, std::to_string(evalContext->cacheId).c_str()); // 1: cacheTable, 2: value
1021-
GetVariable(evalContext->result, -1, evalContext->depth);
1056+
GetVariable(L, evalContext->result, -1, evalContext->depth);
10221057
lua_pop(L, 2);
10231058
return true;
10241059
}
@@ -1056,7 +1091,7 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext)
10561091
if (r == LUA_OK)
10571092
{
10581093
evalContext->result->name = evalContext->expr;
1059-
GetVariable(evalContext->result, -1, evalContext->depth);
1094+
GetVariable(L, evalContext->result, -1, evalContext->depth);
10601095
lua_pop(L, 1);
10611096
return true;
10621097
}

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);

include/emmy_debugger/emmy_debugger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>
6969
void AsyncDoString(const std::string& code);
7070
bool Eval(std::shared_ptr<EvalContext> evalContext, bool force = false);
7171
bool GetStacks(std::vector<std::shared_ptr<Stack>>& stacks, StackAllocatorCB alloc);
72-
void GetVariable(std::shared_ptr<Variable> variable, int index, int depth, bool queryHelper = true);
72+
void GetVariable(lua_State* L, std::shared_ptr<Variable> variable, int index, int depth, bool queryHelper = true);
7373
void DoAction(DebugAction action);
7474
void EnterDebugMode();
7575
void ExitDebugMode();

0 commit comments

Comments
 (0)