From 3b3f3bebcd24073455de9f4abf2f0c7712a1c1ee Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Tue, 4 Dec 2012 10:31:31 +0100 Subject: [PATCH] Add utility function to print stack traces from lldb/gdb. Change-Id: I81315a1cd6900dbecfc9a39d9dc4256461163921 Reviewed-by: Simon Hausmann --- debugging.cpp | 35 ++++++++++++++++++++++++++++++++--- debugging.h | 1 + moth/qv4vme_moth.cpp | 7 ++----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/debugging.cpp b/debugging.cpp index 207eab7..62e38d0 100644 --- a/debugging.cpp +++ b/debugging.cpp @@ -32,6 +32,8 @@ #include +#define LOW_LEVEL_DEBUGGING_HELPERS + using namespace QQmlJS; using namespace QQmlJS::Debugging; @@ -63,13 +65,32 @@ VM::Value *FunctionState::local(unsigned idx) return 0; } +#ifdef LOW_LEVEL_DEBUGGING_HELPERS +Debugger *globalInstance = 0; + +void printStackTrace() +{ + if (globalInstance) + globalInstance->printStackTrace(); + else + std::cerr << "No debugger." << std::endl; +} +#endif // DO_TRACE_INSTR + Debugger::Debugger(VM::ExecutionEngine *engine) : _engine(engine) { +#ifdef LOW_LEVEL_DEBUGGING_HELPERS + globalInstance = this; +#endif // DO_TRACE_INSTR } Debugger::~Debugger() { +#ifdef LOW_LEVEL_DEBUGGING_HELPERS + globalInstance = 0; +#endif // DO_TRACE_INSTR + qDeleteAll(_functionInfo.values()); } @@ -129,9 +150,9 @@ void Debugger::enterFunction(FunctionState *state) #ifdef DO_TRACE_INSTR QString n = name(_callStack[callIndex(state->context())].function); - std::cerr << "*** Entering \"" << qPrintable(n) << "\" with" << state->context()->variableEnvironment->argumentCount << "args" << std::endl; - for (unsigned i = 0; i < state->context()->variableEnvironment->argumentCount; ++i) - std::cerr << " " << i << ": " << currentArg(i) << std::endl; + std::cerr << "*** Entering \"" << qPrintable(n) << "\" with " << state->context()->variableEnvironment->argumentCount << " args" << std::endl; +// for (unsigned i = 0; i < state->context()->variableEnvironment->argumentCount; ++i) +// std::cerr << " " << i << ": " << currentArg(i) << std::endl; #endif // DO_TRACE_INSTR } @@ -171,6 +192,14 @@ const char *Debugger::currentTemp(unsigned idx) const return qPrintable(state->temp(idx)->toString(state->context())->toQString()); } +void Debugger::printStackTrace() const +{ + for (int i = _callStack.size() - 1; i >=0; --i) { + QString n = name(_callStack[i].function); + std::cerr << "\tframe #" << i << ": " << qPrintable(n) << std::endl; + } +} + int Debugger::callIndex(VM::ExecutionContext *context) { for (int idx = _callStack.size() - 1; idx >= 0; --idx) { diff --git a/debugging.h b/debugging.h index 7a0a27d..7c4c273 100644 --- a/debugging.h +++ b/debugging.h @@ -121,6 +121,7 @@ public: // debugging hooks const char *currentArg(unsigned idx) const; const char *currentLocal(unsigned idx) const; const char *currentTemp(unsigned idx) const; + void printStackTrace() const; private: int callIndex(VM::ExecutionContext *context); diff --git a/moth/qv4vme_moth.cpp b/moth/qv4vme_moth.cpp index c83e35f..d38d7d7 100644 --- a/moth/qv4vme_moth.cpp +++ b/moth/qv4vme_moth.cpp @@ -7,7 +7,7 @@ #ifdef DO_TRACE_INSTR # define TRACE_INSTR(I) fprintf(stderr, "executing a %s\n", #I); -# define TRACE(n, str, ...) { fprintf(stderr, " %s : ", #n); fprintf(stderr, str, __VA_ARGS__); fprintf(stderr, "\n"); } +# define TRACE(n, str, ...) { char buf[4096]; snprintf(buf, 4096, str, __VA_ARGS__); fprintf(stderr, " %s : %s\n", #n, buf); } #else # define TRACE_INSTR(I) # define TRACE(n, str, ...) @@ -154,9 +154,6 @@ VM::Value VME::operator()(QQmlJS::VM::ExecutionContext *context, const uchar *co MOTH_BEGIN_INSTR(LoadClosure) VM::Value c = __qmljs_init_closure(instr.value, context); TEMP(instr.targetTempIndex) = c; -#ifdef DO_TRACE_INSTR - qDebug() << "loaded:" << c.toString(context)->toQString(); -#endif MOTH_END_INSTR(LoadClosure) MOTH_BEGIN_INSTR(LoadName) @@ -341,7 +338,7 @@ VM::Value VME::operator()(QQmlJS::VM::ExecutionContext *context, const uchar *co MOTH_BEGIN_INSTR(Ret) VM::Value result = TEMP(instr.tempIndex); - TRACE(Ret, "returning value %s", result.toString(context)->toQString().toUtf8().constData()); +// TRACE(Ret, "returning value %s", result.toString(context)->toQString().toUtf8().constData()); return result; MOTH_END_INSTR(Ret) -- 2.7.4