Skip to content

Commit

Permalink
Forbid nested debuggers
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Feb 8, 2024
1 parent 1ba9780 commit aab0116
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
8 changes: 1 addition & 7 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,7 @@ void NixRepl::mainLoop()
printMsg(lvlError, e.msg());
}
} catch (EvalError & e) {
// in debugger mode, an EvalError should trigger another repl session.
// when that session returns the exception will land here. No need to show it again;
// show the error for this repl session instead.
if (state->debugRepl && !state->debugTraces.empty())
showDebugTrace(std::cout, state->positions, state->debugTraces.front());
else
printMsg(lvlError, e.msg());
printMsg(lvlError, e.msg());
} catch (Error & e) {
printMsg(lvlError, e.msg());
} catch (Interrupted & e) {
Expand Down
5 changes: 3 additions & 2 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,8 @@ std::unique_ptr<ValMap> mapStaticEnvBindings(const SymbolTable & st, const Stati

void EvalState::runDebugRepl(const Error * error, const Env & env, const Expr & expr)
{
// double check we've got the debugRepl function pointer.
if (!debugRepl)
// Make sure we have a debugger to run and we're not already in a debugger.
if (!debugRepl || inDebugger)
return;

auto dts =
Expand All @@ -792,6 +792,7 @@ void EvalState::runDebugRepl(const Error * error, const Env & env, const Expr &
auto se = getStaticEnv(expr);
if (se) {
auto vm = mapStaticEnvBindings(symbols, *se.get(), env);
DebuggerGuard _guard(inDebugger);
(debugRepl)(ref<EvalState>(shared_from_this()), *vm);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ struct DebugTrace {
bool isError;
};


namespace {
/**
* Sets `inDebugger` to true on construction and false on destruction.
*/
class DebuggerGuard {
bool & inDebugger;
public:
DebuggerGuard(bool & inDebugger) : inDebugger(inDebugger) {
inDebugger = true;
}
~DebuggerGuard() {
inDebugger = false;
}
};
}


class EvalState : public std::enable_shared_from_this<EvalState>
{
public:
Expand Down Expand Up @@ -222,6 +240,7 @@ public:
void (* debugRepl)(ref<EvalState> es, const ValMap & extraEnv);
bool debugStop;
bool debugQuit;
bool inDebugger = false;
int trylevel;
std::list<DebugTrace> debugTraces;
std::map<const Expr*, const std::shared_ptr<const StaticEnv>> exprEnvs;
Expand Down

0 comments on commit aab0116

Please sign in to comment.