Skip to content

Commit add23bd

Browse files
committed
breakpoint condition
1 parent d18da64 commit add23bd

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

emmy_core/emmy_debugger.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ void Debugger::Hook(lua_State* L, lua_Debug* ar) {
118118
}
119119
luaThreadExecutors.clear();
120120
}
121-
const auto* bp = FindBreakPoint(L, ar);
122-
if (bp) {
121+
auto* bp = FindBreakPoint(L, ar);
122+
if (bp && ProcessBreakPoint(bp)) {
123123
HandleBreak(L);
124124
return;
125125
}
@@ -159,7 +159,7 @@ bool Debugger::GetStacks(lua_State* L, std::vector<Stack*>& stacks, StackAllocat
159159
if (!lua_getinfo(L, "nSlu", &ar)) {
160160
continue;
161161
}
162-
auto stack = alloc();
162+
auto* stack = alloc();
163163
stack->file = GetFile(L, &ar);
164164
stack->functionName = getDebugName(&ar) == nullptr ? "" : getDebugName(&ar);
165165
stack->level = level;
@@ -510,11 +510,14 @@ void ParsePathParts(const std::string& file, std::vector<std::string>& paths) {
510510

511511
void Debugger::AddBreakPoint(const BreakPoint& breakPoint) {
512512
std::lock_guard <std::mutex> lock(mutexBP);
513-
const auto bp = new BreakPoint();
513+
auto* const bp = new BreakPoint();
514514
bp->file = breakPoint.file;
515515
std::transform(bp->file.begin(), bp->file.end(), bp->file.begin(), tolower);
516516
bp->condition = breakPoint.condition;
517+
bp->hitCondition = breakPoint.hitCondition;
518+
bp->logMessage = breakPoint.logMessage;
517519
bp->line = breakPoint.line;
520+
bp->hitCount = 0;
518521
ParsePathParts(bp->file, bp->pathParts);
519522
breakPoints.push_back(bp);
520523
RefreshLineSet();
@@ -639,6 +642,25 @@ bool Debugger::CreateEnv(int stackLevel) {
639642
return true;
640643
}
641644

645+
bool Debugger::ProcessBreakPoint(BreakPoint* bp) {
646+
if (!bp->condition.empty()) {
647+
EvalContext ctx{};
648+
ctx.expr = bp->condition;
649+
bool suc = DoEval(&ctx);
650+
return suc && ctx.result.valueType == LUA_TBOOLEAN && ctx.result.value == "true";
651+
}
652+
if (!bp->logMessage.empty()) {
653+
EmmyFacade::Get()->SendLog(LogType::Info, bp->logMessage.c_str());
654+
return false;
655+
}
656+
if (!bp->hitCondition.empty()) {
657+
bp->hitCount++;
658+
//TODO check hit condition
659+
return false;
660+
}
661+
return true;
662+
}
663+
642664
void Debugger::SetHookState(lua_State* L, HookState* newState) {
643665
hookState = nullptr;
644666
if (newState->Start(this, L)) {
@@ -765,7 +787,7 @@ BreakPoint* Debugger::FindBreakPoint(const std::string& file, int line) {
765787
ParsePathParts(lowerCaseFile, pathParts);
766788
auto it = breakPoints.begin();
767789
while (it != breakPoints.end()) {
768-
const auto bp = *it;
790+
auto* const bp = *it;
769791
if (bp->line == line) {
770792
// full match: bp(a/b/c), file(a/b/c)
771793
if (bp->file == lowerCaseFile) {

emmy_core/emmy_debugger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class Debugger {
102102
void SetHookState(lua_State* L, HookState* newState);
103103
void CheckDoString(lua_State* L);
104104
bool CreateEnv(int stackLevel);
105+
bool ProcessBreakPoint(BreakPoint* bp);
105106
bool DoEval(EvalContext* evalContext);
106107
bool MatchFileName(const std::string& chunkName, const std::string& fileName) const;
107108
void CacheValue(lua_State* L, int valueIndex, Variable* variable) const;

emmy_core/emmy_facade.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ void ReadBreakPoint(const rapidjson::Value& value, BreakPoint* bp) {
297297
if (value.HasMember("line")) {
298298
bp->line = value["line"].GetInt();
299299
}
300+
if (value.HasMember("condition")) {
301+
bp->condition = value["condition"].GetString();
302+
}
303+
if (value.HasMember("hitCondition")) {
304+
bp->hitCondition = value["hitCondition"].GetString();
305+
}
306+
if (value.HasMember("logMessage")) {
307+
bp->hitCondition = value["logMessage"].GetString();
308+
}
300309
}
301310

302311
void EmmyFacade::OnAddBreakPointReq(const rapidjson::Document& document) {

emmy_core/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class BreakPoint {
3131
public:
3232
std::string file;
3333
std::string condition;
34+
std::string hitCondition;
35+
std::string logMessage;
3436
std::vector<std::string> pathParts;
37+
int hitCount;
3538
int line;
3639
};
3740

0 commit comments

Comments
 (0)