Skip to content

Commit

Permalink
分离出proto, 整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jun 29, 2023
1 parent 2d65488 commit ce653b9
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 135 deletions.
3 changes: 2 additions & 1 deletion emmy_debugger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ target_sources(emmy_debugger
src/debugger/extension_point.cpp

#src/proto
src/proto/proto.cpp
src/proto/proto.cpp
src/proto/proto_handler.cpp

#src/arena
src/arena/arena.cpp
Expand Down
8 changes: 4 additions & 4 deletions emmy_debugger/include/emmy_debugger/debugger/emmy_debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
using Executor = std::function<void(lua_State* L)>;
class EmmyDebuggerManager;

class Debugger : public std::enable_shared_from_this<Debugger>
class Debugger: public std::enable_shared_from_this<Debugger>
{
public:
Debugger(lua_State* L, std::shared_ptr<EmmyDebuggerManager> manager);
Debugger(lua_State* L, EmmyDebuggerManager* manager);
~Debugger();

void Start();
Expand Down Expand Up @@ -86,7 +86,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>
* 设置当前状态机,他的锁由doAction负责
*/
void SetHookState(std::shared_ptr<HookState> newState);
std::shared_ptr<EmmyDebuggerManager> GetEmmyDebuggerManager();
EmmyDebuggerManager* GetEmmyDebuggerManager();

private:
std::shared_ptr<BreakPoint> FindBreakPoint(lua_Debug* ar);
Expand All @@ -109,7 +109,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>
lua_State* currentL;
lua_State* mainL;

std::shared_ptr<EmmyDebuggerManager> manager;
EmmyDebuggerManager* manager;

// 取消递归锁的使用
std::mutex hookStateMtx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "emmy_debugger/api/lua_api.h"


class EmmyDebuggerManager : public std::enable_shared_from_this<EmmyDebuggerManager>
class EmmyDebuggerManager
{
public:
using UniqueIdentifyType = unsigned long long;
Expand Down
26 changes: 15 additions & 11 deletions emmy_debugger/include/emmy_debugger/emmy_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "emmy_debugger/transporter/transporter.h"
#include "emmy_debugger/api/lua_api.h"
#include "emmy_debugger/debugger/emmy_debugger_manager.h"
#include "proto/proto_handler.h"

enum class LogType
{
Expand Down Expand Up @@ -61,14 +62,13 @@ class EmmyFacade
int OnConnect(bool suc);
int OnDisconnect();
void WaitIDE(bool force = false, int timeout = 0);
void OnReceiveMessage(const nlohmann::json document);
bool OnBreak(std::shared_ptr<Debugger> debugger);
void Destroy();
void OnEvalResult(std::shared_ptr<EvalContext> context);
void SendLog(LogType type, const char* fmt, ...);
void OnLuaStateGC(lua_State* L);
void Hook(lua_State* L, lua_Debug* ar);
std::shared_ptr<EmmyDebuggerManager> GetDebugManager() const;
EmmyDebuggerManager& GetDebugManager();

std::shared_ptr<Debugger> GetDebugger(lua_State* L);

Expand All @@ -82,17 +82,19 @@ class EmmyFacade

void SetWorkMode(WorkMode mode);
WorkMode GetWorkMode();

void InitReq(InitParams &params);

void ReadyReq();

void OnReceiveMessage(nlohmann::json document);

// void Remove

// Start hook 作为成员存在
std::function<void()> StartHook;

private:
void OnInitReq(const nlohmann::json document);
void OnReadyReq(const nlohmann::json document);
void OnAddBreakPointReq(const nlohmann::json document);
void OnRemoveBreakPointReq(const nlohmann::json document);
void OnActionReq(const nlohmann::json document);
void OnEvalReq(const nlohmann::json document);

std::mutex waitIDEMutex;
std::condition_variable waitIDECV;

Expand All @@ -103,12 +105,14 @@ class EmmyFacade
bool isWaitingForIDE;
WorkMode workMode;

std::shared_ptr<EmmyDebuggerManager> emmyDebuggerManager;

// 表示使用了tcplisten tcpConnect 的states
std::set<lua_State*> mainStates;

std::atomic<bool> readyHook;

ProtoHandler _protoHandler;

EmmyDebuggerManager _emmyDebuggerManager;
};


Expand Down
53 changes: 52 additions & 1 deletion emmy_debugger/include/emmy_debugger/proto/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@


enum class DebugAction {
Break,
None = -1,
Break = 0,
Continue,
StepOver,
StepIn,
Expand All @@ -49,6 +50,16 @@ class JsonProtocol {
virtual void Deserialize(nlohmann::json json);
};

class InitParams : public JsonProtocol {
public:
std::string emmyHelper;
std::vector<std::string> ext;

virtual nlohmann::json Serialize();

virtual void Deserialize(nlohmann::json json);
};

class BreakPoint : public JsonProtocol {
public:
std::string file;
Expand All @@ -63,6 +74,35 @@ class BreakPoint : public JsonProtocol {
void Deserialize(nlohmann::json json) override;
};

class AddBreakpointParams : public JsonProtocol {
public:
bool clear = false;
std::vector<std::shared_ptr<BreakPoint>> breakPoints;

nlohmann::json Serialize() override;

void Deserialize(nlohmann::json json) override;
};

class RemoveBreakpointParams : public JsonProtocol {
public:
std::vector<std::shared_ptr<BreakPoint>> breakPoints;

nlohmann::json Serialize() override;

void Deserialize(nlohmann::json json) override;
};

class ActionParams : public JsonProtocol {
public:
DebugAction action = DebugAction::None;

nlohmann::json Serialize() override;

void Deserialize(nlohmann::json json) override;
};


class Variable : public JsonProtocol {
public:
Variable();
Expand Down Expand Up @@ -103,16 +143,27 @@ class EvalContext : public JsonProtocol {
EvalContext();

std::string expr;
std::string value;
std::string error;
int seq = 0;
int stackLevel = 0;
int depth = 0;
int cacheId = 0;
Idx<Variable> result;
bool success = false;
bool setValue = false;
std::shared_ptr<Arena<Variable>> variableArena;

nlohmann::json Serialize() override;

void Deserialize(nlohmann::json json) override;
};

class EvalParams : public JsonProtocol {
public:
std::shared_ptr<EvalContext> ctx;

nlohmann::json Serialize() override;

void Deserialize(nlohmann::json json) override;
};
27 changes: 27 additions & 0 deletions emmy_debugger/include/emmy_debugger/proto/proto_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "proto.h"
#include "nlohmann/json_fwd.hpp"

class EmmyFacade;

class ProtoHandler {
public:
explicit ProtoHandler(EmmyFacade *owner);

void OnDispatch(nlohmann::json params);

private:
void OnInitReq(InitParams &params);

void OnReadyReq();

void OnAddBreakPointReq(AddBreakpointParams &params);

void OnRemoveBreakPointReq(RemoveBreakpointParams &params);

void OnActionReq(ActionParams& params);

void OnEvalReq(EvalParams& params);

EmmyFacade *_owner;
};
15 changes: 9 additions & 6 deletions emmy_debugger/src/debugger/emmy_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void WaitConnectedHook(lua_State *L, lua_Debug *ar) {
// std::lock_guard<std::mutex> lock()
}

Debugger::Debugger(lua_State *L, std::shared_ptr<EmmyDebuggerManager> manager)
Debugger::Debugger(lua_State *L, EmmyDebuggerManager *manager)
: currentL(L),
mainL(L),
manager(manager),
Expand Down Expand Up @@ -179,7 +179,7 @@ bool Debugger::GetStacks(std::vector<Stack> &stacks) {
}
// C++ 17 only return T&
stacks.emplace_back();
auto& stack = stacks.back();
auto &stack = stacks.back();
stack.file = GetFile(&ar);
stack.functionName = getDebugName(&ar) == nullptr ? "" : getDebugName(&ar);
stack.level = totalLevel++;
Expand Down Expand Up @@ -789,7 +789,7 @@ void Debugger::SetHookState(std::shared_ptr<HookState> newState) {
}
}

std::shared_ptr<EmmyDebuggerManager> Debugger::GetEmmyDebuggerManager() {
EmmyDebuggerManager* Debugger::GetEmmyDebuggerManager() {
return manager;
}

Expand Down Expand Up @@ -906,10 +906,13 @@ bool Debugger::DoEval(std::shared_ptr<EvalContext> evalContext) {
}
// LOAD AS "return expr"
std::string statement = "return ";
// 如果是 aaa:bbbb 则纠正为aaa.bbbb

if (evalContext->setValue) {
statement = evalContext->expr + " = " + evalContext->value + " return " + evalContext->expr;
} else {
statement.append(evalContext->expr);
}

statement.append(evalContext->expr);
// 如果是 aaa:bbbb 则纠正为aaa.bbbb
int r = luaL_loadstring(L, statement.c_str());
if (r == LUA_ERRSYNTAX) {
evalContext->error = "syntax err: ";
Expand Down
2 changes: 1 addition & 1 deletion emmy_debugger/src/debugger/emmy_debugger_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ bool install_emmy_debugger(struct lua_State* L)
}
#endif
// register helper lib
EmmyFacade::Get().GetDebugManager()->extension.Initialize(L);
EmmyFacade::Get().GetDebugManager().extension.Initialize(L);
handleStateClose(L);
return true;
}
Expand Down
11 changes: 6 additions & 5 deletions emmy_debugger/src/debugger/emmy_debugger_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

EmmyDebuggerManager::EmmyDebuggerManager()
: stateBreak(std::make_shared<HookStateBreak>()),
stateStepOver(std::make_shared<HookStateStepOver>()),
stateStepIn(std::make_shared<HookStateStepIn>()),
stateStepOut(std::make_shared<HookStateStepOut>()),
stateContinue(std::make_shared<HookStateContinue>()),
stateStepOver(std::make_shared<HookStateStepOver>()),
stateStop(std::make_shared<HookStateStop>()),
stateStepIn(std::make_shared<HookStateStepIn>()),
stateStepOut(std::make_shared<HookStateStepOut>()),

isRunning(false)
{
}
Expand Down Expand Up @@ -46,7 +47,7 @@ std::shared_ptr<Debugger> EmmyDebuggerManager::AddDebugger(lua_State* L)
{
if (luaVersion != LuaVersion::LUA_JIT)
{
debugger = std::make_shared<Debugger>(reinterpret_cast<lua_State*>(identify), shared_from_this());
debugger = std::make_shared<Debugger>(reinterpret_cast<lua_State*>(identify), this);
}
else
{
Expand All @@ -61,7 +62,7 @@ std::shared_ptr<Debugger> EmmyDebuggerManager::AddDebugger(lua_State* L)
mainState = L;
}

debugger = std::make_shared<Debugger>(mainState, shared_from_this());
debugger = std::make_shared<Debugger>(mainState, this);
}
debuggers.insert({identify, debugger});
}
Expand Down
Loading

0 comments on commit ce653b9

Please sign in to comment.