Skip to content

Commit be16291

Browse files
committed
重新构思对luajit 的调试兼容
1 parent 21dc835 commit be16291

File tree

13 files changed

+110
-66
lines changed

13 files changed

+110
-66
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ elseif(${EMMY_LUA_VERSION} STREQUAL "51")
5959
add_definitions(-DEMMY_LUA_51)
6060
elseif(${EMMY_LUA_VERSION} STREQUAL "jit")
6161
set(EMMY_LUA_DIR "luajit")
62-
add_definitions(-DEMMY_LUA_51)
62+
add_definitions(-DEMMY_LUA_JIT)
6363
# if LUAJIT support lua_setfuncs use this macro define
6464
#add_definitions(-DEMMY_LUA_JIT_SUPPORT_LUA_SETFUNCS)
6565
endif()

emmy_debugger/src/api/lua_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919

20-
#ifdef EMMY_LUA_51
20+
#if defined(EMMY_LUA_51) || defined(EMMY_LUA_JIT)
2121
#include "emmy_debugger/api/lua_api.h"
2222
int lua_absindex(lua_State *L, int idx) {
2323
if (idx > 0) {

emmy_debugger/src/api/lua_state.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ lua_State* GetMainState(lua_State* L)
5656
std::vector<lua_State*> FindAllCoroutine(lua_State* L)
5757
{
5858
LuaSwitchDo(
59-
FindAllCoroutine_luaJIT(L),
59+
std::vector<lua_State*>(),
6060
FindAllCoroutine_lua51(L),
6161
FindAllCoroutine_lua52(L),
6262
FindAllCoroutine_lua53(L),
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#include "emmy_debugger/api/lua_state.h"
2+
23
// luajit不包含luajit头文件,所以本质上getmainstate 结果是错的
34
// 但是luajit有global hook,所以不影响调试
45

56
lua_State* GetMainState_luaJIT(lua_State* L)
67
{
78
return L;
89
}
9-
10-
std::vector<lua_State*> FindAllCoroutine_luaJIT(lua_State* L)
11-
{
12-
return std::vector<lua_State*>();
13-
}

emmy_debugger/src/emmy_debugger.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,8 @@ void Debugger::Attach(bool isMainThread)
8484
});
8585
}
8686

87-
if (EmmyFacade::Get().GetWorkMode() == WorkMode::EmmyCore)
87+
if (EmmyFacade::Get().GetWorkMode() == WorkMode::EmmyCore && mainL)
8888
{
89-
// 对于luajit来讲,没有办法获得他的mainstate(因为luajit没有lua5.2以上的mainstate伪索引,而且头文件在不同平台上要重新生成)
90-
// 很难正确的简单知道mainstate,所以mainL 这个变量对luajit并不指代mainState
91-
92-
// if(luaVersion == LuaVersion::LUA_JIT)
93-
// {
94-
// int ret = lua_pushthread(mainL);
95-
// lua_pop(mainL, 1);
96-
// if(ret != 1)
97-
// {
98-
// return;
99-
// }
100-
// }
10189
if (isMainThread)
10290
{
10391
auto states = FindAllCoroutine(mainL);

emmy_debugger/src/emmy_debugger_manager.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ EmmyDebuggerManager::~EmmyDebuggerManager()
1919
std::shared_ptr<Debugger> EmmyDebuggerManager::GetDebugger(lua_State* L)
2020
{
2121
std::lock_guard<std::mutex> lock(debuggerMtx);
22-
auto mainState = GetMainState(L);
23-
auto it = debuggers.find(mainState);
22+
auto identify = GetUniqueIdentify(L);
23+
auto it = debuggers.find(identify);
2424
if (it != debuggers.end())
2525
{
2626
return it->second;
@@ -35,14 +35,23 @@ std::shared_ptr<Debugger> EmmyDebuggerManager::AddDebugger(lua_State* L)
3535
{
3636
std::lock_guard<std::mutex> lock(debuggerMtx);
3737

38-
auto mainState = GetMainState(L);
38+
auto identify = GetUniqueIdentify(L);
39+
3940
std::shared_ptr<Debugger> debugger = nullptr;
40-
auto it = debuggers.find(mainState);
41+
42+
auto it = debuggers.find(identify);
4143

4244
if (it == debuggers.end())
4345
{
44-
debugger = std::make_shared<Debugger>(mainState, shared_from_this());
45-
debuggers.insert({ mainState, debugger });
46+
if (luaVersion != LuaVersion::LUA_JIT)
47+
{
48+
debugger = std::make_shared<Debugger>(reinterpret_cast<lua_State*>(identify), shared_from_this());
49+
}
50+
else
51+
{
52+
debugger = std::make_shared<Debugger>(nullptr, shared_from_this());
53+
}
54+
debuggers.insert({identify, debugger});
4655
}
4756
else
4857
{
@@ -53,11 +62,12 @@ std::shared_ptr<Debugger> EmmyDebuggerManager::AddDebugger(lua_State* L)
5362
return debugger;
5463
}
5564

65+
5666
std::shared_ptr<Debugger> EmmyDebuggerManager::RemoveDebugger(lua_State* L)
5767
{
5868
std::lock_guard<std::mutex> lock(debuggerMtx);
59-
auto mainL = GetMainState(L);
60-
auto it = debuggers.find(mainL);
69+
auto identify = GetUniqueIdentify(L);
70+
auto it = debuggers.find(identify);
6171
if (it != debuggers.end())
6272
{
6373
auto debugger = it->second;
@@ -106,16 +116,16 @@ void EmmyDebuggerManager::AddBreakpoint(std::shared_ptr<BreakPoint> breakpoint)
106116
{
107117
std::lock_guard<std::mutex> lock(breakpointsMtx);
108118
bool isAdd = false;
109-
for(std::shared_ptr<BreakPoint>& bp: breakpoints)
119+
for (std::shared_ptr<BreakPoint>& bp : breakpoints)
110120
{
111-
if(bp->line == breakpoint->line && CompareIgnoreCase(bp->file,breakpoint->file) == 0)
121+
if (bp->line == breakpoint->line && CompareIgnoreCase(bp->file, breakpoint->file) == 0)
112122
{
113123
bp = breakpoint;
114124
isAdd = true;
115125
}
116126
}
117-
118-
if(!isAdd)
127+
128+
if (!isAdd)
119129
{
120130
breakpoints.push_back(breakpoint);
121131
}
@@ -135,7 +145,6 @@ void EmmyDebuggerManager::RemoveBreakpoint(const std::string& file, int line)
135145
auto it = breakpoints.begin();
136146
while (it != breakpoints.end())
137147
{
138-
139148
const auto bp = *it;
140149
if (bp->line == line && CompareIgnoreCase(bp->file, file) == 0)
141150
{
@@ -212,8 +221,21 @@ void EmmyDebuggerManager::OnDisconnect()
212221
{
213222
it.second->Stop();
214223
}
215-
if(luaVersion == LuaVersion::LUA_JIT)
224+
if (luaVersion == LuaVersion::LUA_JIT)
216225
{
217226
debuggers.clear();
218227
}
219228
}
229+
230+
EmmyDebuggerManager::UniqueIdentifyType EmmyDebuggerManager::GetUniqueIdentify(lua_State* L)
231+
{
232+
if (luaVersion != LuaVersion::LUA_JIT)
233+
{
234+
// 我们认为luajit只会有一个debugger
235+
return 0;
236+
}
237+
else
238+
{
239+
return reinterpret_cast<UniqueIdentifyType>(GetMainState(L));
240+
}
241+
}

emmy_debugger/src/emmy_facade.cpp

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "emmy_debugger/emmy_facade.h"
1818
#include <cstdarg>
19+
#include <cstdint>
1920
#include "emmy_debugger/proto/socket_server_transporter.h"
2021
#include "emmy_debugger/proto/socket_client_transporter.h"
2122
#include "emmy_debugger/proto/pipeline_server_transporter.h"
@@ -58,7 +59,7 @@ EmmyFacade::EmmyFacade()
5859
isAPIReady(false),
5960
isWaitingForIDE(false),
6061
StartHook(nullptr),
61-
workMode(WorkMode::EmmyCore),
62+
workMode(WorkMode::EmmyCore),
6263
emmyDebuggerManager(std::make_shared<EmmyDebuggerManager>())
6364
{
6465
}
@@ -102,6 +103,9 @@ bool EmmyFacade::TcpListen(lua_State* L, const std::string& host, int port, std:
102103
{
103104
Destroy();
104105

106+
// 仅仅luajit需要
107+
mainStates.insert(L);
108+
105109
emmyDebuggerManager->AddDebugger(L);
106110

107111
const auto s = std::make_shared<SocketServerTransporter>();
@@ -130,6 +134,9 @@ bool EmmyFacade::TcpConnect(lua_State* L, const std::string& host, int port, std
130134
{
131135
Destroy();
132136

137+
// 仅仅luajit需要
138+
mainStates.insert(L);
139+
133140
emmyDebuggerManager->AddDebugger(L);
134141

135142
const auto c = std::make_shared<SocketClientTransporter>();
@@ -153,6 +160,9 @@ bool EmmyFacade::PipeListen(lua_State* L, const std::string& name, std::string&
153160
{
154161
Destroy();
155162

163+
// 仅仅luajit需要
164+
mainStates.insert(L);
165+
156166
emmyDebuggerManager->AddDebugger(L);
157167

158168
const auto p = std::make_shared<PipelineServerTransporter>();
@@ -166,6 +176,9 @@ bool EmmyFacade::PipeConnect(lua_State* L, const std::string& name, std::string&
166176
{
167177
Destroy();
168178

179+
// 仅仅luajit需要
180+
mainStates.insert(L);
181+
169182
emmyDebuggerManager->AddDebugger(L);
170183

171184
const auto p = std::make_shared<PipelineClientTransporter>();
@@ -229,6 +242,7 @@ int EmmyFacade::OnDisconnect()
229242
void EmmyFacade::Destroy()
230243
{
231244
OnDisconnect();
245+
232246
if (transporter)
233247
{
234248
transporter->Stop();
@@ -315,8 +329,10 @@ void EmmyFacade::OnInitReq(const rapidjson::Document& document)
315329
for (auto debugger : debuggers)
316330
{
317331
debugger->Start();
318-
319-
debugger->Attach(false);
332+
if (luaVersion != LuaVersion::LUA_JIT)
333+
{
334+
debugger->Attach(false);
335+
}
320336
}
321337
}
322338

@@ -591,21 +607,25 @@ void EmmyFacade::Hook(lua_State* L, lua_Debug* ar)
591607
}
592608
else
593609
{
594-
debugger = emmyDebuggerManager->AddDebugger(L);
595-
install_emmy_core(L);
596-
debugger->Start();
597-
debugger->Attach();
598-
599-
// send attached notify
600-
rapidjson::Document rspDoc;
601-
rspDoc.SetObject();
602-
// fix macosx compiler error,
603-
// repidjson 应该有重载决议的错误
604-
int64_t state = reinterpret_cast<int64_t>(L);
605-
rspDoc.AddMember("state", state, rspDoc.GetAllocator());
606-
this->transporter->Send(int(MessageCMD::AttachedNotify), rspDoc);
607-
608-
debugger->Hook(ar, L);
610+
if (workMode == WorkMode::Attach)
611+
{
612+
debugger = emmyDebuggerManager->AddDebugger(L);
613+
install_emmy_core(L);
614+
// attach的时候能进入这里必然是 connected,所以可以执行start
615+
debugger->Start();
616+
debugger->Attach();
617+
618+
// send attached notify
619+
rapidjson::Document rspDoc;
620+
rspDoc.SetObject();
621+
// fix macosx compiler error,
622+
// repidjson 应该有重载决议的错误
623+
int64_t state = reinterpret_cast<int64_t>(L);
624+
rspDoc.AddMember("state", state, rspDoc.GetAllocator());
625+
this->transporter->Send(int(MessageCMD::AttachedNotify), rspDoc);
626+
627+
debugger->Hook(ar, L);
628+
}
609629
}
610630
}
611631

emmy_debugger/src/stack.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@
1818
#include "emmy_debugger/types.h"
1919
#include "emmy_debugger/api/lua_api.h"
2020

21-
Variable::Variable(): nameType(LUA_TSTRING), valueType(0), cacheId(0){
21+
Variable::Variable()
22+
: nameType(LUA_TSTRING),
23+
valueType(0),
24+
cacheId(0)
25+
{
2226
}
2327

24-
Variable::~Variable() {
28+
Variable::~Variable()
29+
{
2530
}
2631

27-
std::shared_ptr<Variable> Variable::Clone() {
32+
std::shared_ptr<Variable> Variable::Clone()
33+
{
2834
auto to = std::make_shared<Variable>();
2935
to->name = name;
3036
to->nameType = nameType;
3137
to->value = value;
3238
to->valueType = valueType;
3339
to->valueTypeName = valueTypeName;
34-
for (auto child : children) {
40+
for (auto child : children)
41+
{
3542
auto c = std::make_shared<Variable>();
3643
to->children.push_back(child->Clone());
3744
}
@@ -45,12 +52,15 @@ std::shared_ptr<Variable> Variable::CreateChildNode()
4552
return child;
4653
}
4754

48-
Stack::Stack(): level(0), line(0) {
55+
Stack::Stack(): level(0), line(0)
56+
{
4957
}
5058

51-
Stack::~Stack() {
59+
Stack::~Stack()
60+
{
5261
}
5362

54-
std::shared_ptr<Variable> Stack::CreateVariable() {
63+
std::shared_ptr<Variable> Stack::CreateVariable()
64+
{
5565
return std::make_shared<Variable>();
5666
}

include/emmy_debugger/api/lua_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef ptrdiff_t lua_KContext;
3131
typedef int(*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
3232
#endif
3333

34-
#ifdef EMMY_LUA_51
34+
#if defined(EMMY_LUA_51) || defined(EMMY_LUA_JIT)
3535
#define LUA_OK 0
3636

3737
int lua_absindex(lua_State *L, int idx);

include/emmy_debugger/api/lua_state.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ std::vector<lua_State*> FindAllCoroutine_lua53(lua_State* L);
1212

1313
std::vector<lua_State*> FindAllCoroutine_lua54(lua_State* L);
1414

15-
std::vector<lua_State*> FindAllCoroutine_luaJIT(lua_State* L);
15+
// std::vector<lua_State*> FindAllCoroutine_luaJIT(lua_State* L);
1616

1717
lua_State* GetMainState(lua_State* L);
1818

0 commit comments

Comments
 (0)