Skip to content

Commit

Permalink
DebugStackTracker class in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
bburdette committed Dec 22, 2021
1 parent f317019 commit b4a59a5
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ ref<EvalState> EvalCommand::getEvalState()

printStaticEnvBindings(expr);

std::cout << evalState->vCallFlake << std::endl;

std::cout << "expr: " << std::endl;
expr.show(std::cout);
std::cout << std::endl;
Expand Down
1 change: 1 addition & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ bool NixRepl::processLine(string line)
}
else if (arg == "error") {
if (this->debugError.has_value()) {
// TODO user --show-trace setting?
showErrorInfo(std::cout, (*debugError)->info(), true);
}
else
Expand Down
65 changes: 63 additions & 2 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sys/resource.h>
#include <iostream>
#include <fstream>
#include <functional>

#include <sys/resource.h>

Expand Down Expand Up @@ -854,13 +855,20 @@ LocalNoInlineNoReturn(void throwAssertionError(const Pos & pos, const char * s,

LocalNoInlineNoReturn(void throwUndefinedVarError(const Pos & pos, const char * s, const string & s1, Env & env, Expr *expr))
{
std::cout << "throwUndefinedVarError" << std::endl;

std::cout << "loggerSettings.showTrace: " << loggerSettings.showTrace << std::endl;

auto error = UndefinedVarError({
.msg = hintfmt(s, s1),
.errPos = pos
});

if (debuggerHook && expr)
if (debuggerHook && expr) {

std::cout << "throwUndefinedVarError debuggerHook" << std::endl;
debuggerHook(error, env, *expr);
}

throw error;
}
Expand Down Expand Up @@ -888,6 +896,16 @@ LocalNoInline(void addErrorTrace(Error & e, const Pos & pos, const char * s, con
e.addTrace(pos, s, s2);
}

// LocalNoInline(void makeErrorTrace(Error & e, const char * s, const string & s2))
// {
// Trace { .pos = e, .hint = hint }
// }

// LocalNoInline(void makeErrorTrace(Error & e, const Pos & pos, const char * s, const string & s2))
// {
// return Trace { .pos = e, .hint = hintfmt(s, s2); };
// }

void mkString(Value & v, const char * s)
{
v.mkString(dupString(s));
Expand Down Expand Up @@ -934,7 +952,7 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
return j->value;
}
if (!env->prevWith) {
throwUndefinedVarError(var.pos, "undefined variable '%1%'", var.name, *env, 0);
throwUndefinedVarError(var.pos, "undefined variable '%1%'", var.name, *env, (Expr*)&var);
}
for (size_t l = env->prevWith; l; --l, env = env->up) ;
}
Expand Down Expand Up @@ -1092,6 +1110,39 @@ void EvalState::resetFileCache()
fileParseCache.clear();
}

class DebugTraceStacker {
public:
DebugTraceStacker(EvalState &evalState, Trace t)
:evalState(evalState), trace(t)
{
evalState.debugTraces.push_front(t);
}
~DebugTraceStacker() {
// assert(evalState.debugTraces.front() == trace);
evalState.debugTraces.pop_front();
}

EvalState &evalState;
Trace trace;

};

// class DebugTraceStacker {
// DebugTraceStacker(std::ref<EvalState> evalState, std::ref<Trace> t)
// :evalState(evalState), trace(t)
// {
// evalState->debugTraces.push_front(t);
// }
// ~DebugTraceStacker() {
// assert(evalState->debugTraces.pop_front() == trace);
// }

// std::ref<EvalState> evalState;
// std::ref<Trace> trace;

// };



void EvalState::cacheFile(
const Path & path,
Expand All @@ -1103,6 +1154,15 @@ void EvalState::cacheFile(
fileParseCache[resolvedPath] = e;

try {
std::unique_ptr<DebugTraceStacker> dts = debuggerHook ?
std::unique_ptr<DebugTraceStacker>(new DebugTraceStacker(*this,
Trace { .pos = (e->getPos() ? std::optional(ErrPos(*e->getPos())) : std::nullopt),
.hint = hintfmt("while evaluating the file '%1%':", resolvedPath)
}
)) : std::unique_ptr<DebugTraceStacker>();

// Trace( .pos = (e->getPos() ? std::optional(ErrPos(*e->getPos())):
// std::nullopt), hintfmt("while evaluating the file '%1%':", resolvedPath));
// Enforce that 'flake.nix' is a direct attrset, not a
// computation.
if (mustBeTrivial &&
Expand Down Expand Up @@ -1472,6 +1532,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
try {
lambda.body->eval(*this, env2, vCur);
} catch (Error & e) {
std::cout << "eval showErrorInfo showTrace: " << loggerSettings.showTrace.get() << std::endl;
if (loggerSettings.showTrace.get()) {
addErrorTrace(e, lambda.pos, "while evaluating %s",
(lambda.name.set()
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public:
RootValue vCallFlake = nullptr;
RootValue vImportedDrvToDerivation = nullptr;

std::list<Trace> debugTraces;

private:
SrcToStore srcToStore;

Expand Down
25 changes: 25 additions & 0 deletions src/libexpr/nixexpr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct Expr
virtual void setName(Symbol & name);

std::shared_ptr<const StaticEnv> staticenv;
virtual Pos* getPos() = 0;
};

std::ostream & operator << (std::ostream & str, const Expr & e);
Expand All @@ -100,6 +101,8 @@ struct ExprInt : Expr
ExprInt(NixInt n) : n(n) { mkInt(v, n); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);

Pos* getPos() { return 0; }
};

struct ExprFloat : Expr
Expand All @@ -109,6 +112,8 @@ struct ExprFloat : Expr
ExprFloat(NixFloat nf) : nf(nf) { mkFloat(v, nf); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);

Pos* getPos() { return 0; }
};

struct ExprString : Expr
Expand All @@ -118,13 +123,17 @@ struct ExprString : Expr
ExprString(const Symbol & s) : s(s) { mkString(v, s); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);

Pos* getPos() { return 0; }
};

/* Temporary class used during parsing of indented strings. */
struct ExprIndStr : Expr
{
string s;
ExprIndStr(const string & s) : s(s) { };

Pos* getPos() { return 0; }
};

struct ExprPath : Expr
Expand All @@ -134,6 +143,7 @@ struct ExprPath : Expr
ExprPath(const string & s) : s(s) { v.mkPath(this->s.c_str()); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
Pos* getPos() { return 0; }
};

typedef uint32_t Level;
Expand Down Expand Up @@ -161,6 +171,7 @@ struct ExprVar : Expr
ExprVar(const Pos & pos, const Symbol & name) : pos(pos), name(name) { };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
Pos* getPos() { return &pos; }
};

struct ExprSelect : Expr
Expand All @@ -171,6 +182,7 @@ struct ExprSelect : Expr
ExprSelect(const Pos & pos, Expr * e, const AttrPath & attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(attrPath) { };
ExprSelect(const Pos & pos, Expr * e, const Symbol & name) : pos(pos), e(e), def(0) { attrPath.push_back(AttrName(name)); };
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprOpHasAttr : Expr
Expand All @@ -179,6 +191,7 @@ struct ExprOpHasAttr : Expr
AttrPath attrPath;
ExprOpHasAttr(Expr * e, const AttrPath & attrPath) : e(e), attrPath(attrPath) { };
COMMON_METHODS
Pos* getPos() { return e->getPos(); }
};

struct ExprAttrs : Expr
Expand Down Expand Up @@ -207,13 +220,15 @@ struct ExprAttrs : Expr
ExprAttrs(const Pos &pos) : recursive(false), pos(pos) { };
ExprAttrs() : recursive(false), pos(noPos) { };
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprList : Expr
{
std::vector<Expr *> elems;
ExprList() { };
COMMON_METHODS
Pos* getPos() { return 0; }
};

struct Formal
Expand Down Expand Up @@ -252,6 +267,7 @@ struct ExprLambda : Expr
string showNamePos() const;
inline bool hasFormals() const { return formals != nullptr; }
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprCall : Expr
Expand All @@ -263,6 +279,7 @@ struct ExprCall : Expr
: fun(fun), args(args), pos(pos)
{ }
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprLet : Expr
Expand All @@ -271,6 +288,7 @@ struct ExprLet : Expr
Expr * body;
ExprLet(ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { };
COMMON_METHODS
Pos* getPos() { return 0; }
};

struct ExprWith : Expr
Expand All @@ -280,6 +298,7 @@ struct ExprWith : Expr
size_t prevWith;
ExprWith(const Pos & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprIf : Expr
Expand All @@ -288,6 +307,7 @@ struct ExprIf : Expr
Expr * cond, * then, * else_;
ExprIf(const Pos & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { };
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprAssert : Expr
Expand All @@ -296,13 +316,15 @@ struct ExprAssert : Expr
Expr * cond, * body;
ExprAssert(const Pos & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprOpNot : Expr
{
Expr * e;
ExprOpNot(Expr * e) : e(e) { };
COMMON_METHODS
Pos* getPos() { return 0; }
};

#define MakeBinOp(name, s) \
Expand All @@ -321,6 +343,7 @@ struct ExprOpNot : Expr
e1->bindVars(env); e2->bindVars(env); \
} \
void eval(EvalState & state, Env & env, Value & v); \
Pos* getPos() { return &pos; } \
};

MakeBinOp(ExprOpEq, "==")
Expand All @@ -339,13 +362,15 @@ struct ExprConcatStrings : Expr
ExprConcatStrings(const Pos & pos, bool forceString, vector<Expr *> * es)
: pos(pos), forceString(forceString), es(es) { };
COMMON_METHODS
Pos* getPos() { return &pos; }
};

struct ExprPos : Expr
{
Pos pos;
ExprPos(const Pos & pos) : pos(pos) { };
COMMON_METHODS
Pos* getPos() { return &pos; }
};


Expand Down
2 changes: 2 additions & 0 deletions src/libutil/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ static std::string indent(std::string_view indentFirst, std::string_view indentR

std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace)
{
std::cout << "showErrorInfo showTrace: " << showTrace << std::endl;

std::string prefix;
switch (einfo.level) {
case Verbosity::lvlError: {
Expand Down
1 change: 1 addition & 0 deletions src/libutil/logging.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef uint64_t ActivityId;
struct LoggerSettings : Config
{
Setting<bool> showTrace{
// this, false, "show-trace",
this, false, "show-trace",
R"(
Where Nix should print out a stack trace in case of Nix
Expand Down

0 comments on commit b4a59a5

Please sign in to comment.