Skip to content

Commit

Permalink
fix: avoid call debug step when evaluating variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sssooonnnggg committed Dec 5, 2024
1 parent 8a2ebaf commit bd95007
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
9 changes: 4 additions & 5 deletions debugger/src/internal/debug_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ StackTraceResponse DebugBridge::getStackTrace() {

StackTraceResponse response;
lua_State* L = break_vm_;
lua_utils::DisableDebugStep _(break_vm_);

updateVariables();
auto frames = updateStackFrames();
Expand Down Expand Up @@ -337,11 +338,9 @@ void DebugBridge::onConnect(dap::Session* session) {

void DebugBridge::onDisconnect() {
task_pool_.post([this] { clearBreakPoints(); });
std::scoped_lock lock(break_mutex_);
if (!resume_) {
resume_ = true;
resume_cv_.notify_one();
}

processSingleStep(nullptr);
resumeInternal();
session_ = nullptr;
}

Expand Down
10 changes: 7 additions & 3 deletions debugger/src/internal/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum class ScopeType {
Local,
UpValue,
Table,
UserData,
Unknown,
};

Expand Down Expand Up @@ -59,13 +60,17 @@ class Scope final {
static Scope createTable(lua_State* L, int index = -1) {
const TValue* t = luaA_toobject(L, index);
auto* address = hvalue(t);
return createFromAddress<Table>(L, index, address);
auto scope = createFromAddress<Table>(L, index, address);
scope.type_ = ScopeType::Table;
return scope;
}

static Scope createUserData(lua_State* L, int index = -1) {
const TValue* u = luaA_toobject(L, index);
auto* address = uvalue(u);
return createFromAddress<Udata>(L, index, address);
auto scope = createFromAddress<Udata>(L, index, address);
scope.type_ = ScopeType::UserData;
return scope;
}

explicit Scope(int key) : key_(key) { type_ = ScopeType::Unknown; }
Expand Down Expand Up @@ -102,7 +107,6 @@ class Scope final {
Scope scope;
scope.L_ = L;
scope.createKey(std::hash<const T*>{}(address));
scope.type_ = ScopeType::Table;
scope.ref_ = lua_ref(L, index);
return scope;
}
Expand Down
9 changes: 1 addition & 8 deletions debugger/src/internal/utils/lua_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
namespace luau::debugger::lua_utils {

std::optional<int> eval(lua_State* L, const std::string& code, int env) {
auto main_vm = lua_mainthread(L);
auto callbacks = lua_callbacks(main_vm);
auto old_step = callbacks->debugstep;

// Avoid break by debugstep when executing `eval`
auto _ = utils::makeRAII(
[callbacks]() { callbacks->debugstep = nullptr; },
[callbacks, old_step]() { callbacks->debugstep = old_step; });
DisableDebugStep _(L);

lua_setsafeenv(L, LUA_ENVIRONINDEX, false);
auto env_idx = lua_absindex(L, env);
Expand Down
17 changes: 17 additions & 0 deletions debugger/src/internal/utils/lua_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,21 @@ class StackGuard {
int top_;
};

class DisableDebugStep {
public:
DisableDebugStep(lua_State* L) {
main_vm_ = lua_mainthread(L);
callbacks_ = lua_callbacks(main_vm_);
old_debug_step_ = callbacks_->debugstep;
callbacks_->debugstep = nullptr;
}
~DisableDebugStep() { callbacks_->debugstep = old_debug_step_; }

private:
lua_State* main_vm_;
lua_Callbacks* callbacks_;
using DebugStep = void (*)(lua_State*, lua_Debug*);
DebugStep old_debug_step_;
};

} // namespace luau::debugger::lua_utils

0 comments on commit bd95007

Please sign in to comment.