Add utility function to print stack traces from lldb/gdb.
authorErik Verbruggen <erik.verbruggen@digia.com>
Tue, 4 Dec 2012 09:31:31 +0000 (10:31 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Fri, 7 Dec 2012 15:21:49 +0000 (16:21 +0100)
Change-Id: I81315a1cd6900dbecfc9a39d9dc4256461163921
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
debugging.cpp
debugging.h
moth/qv4vme_moth.cpp

index 207eab7..62e38d0 100644 (file)
@@ -32,6 +32,8 @@
 
 #include <iostream>
 
+#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) {
index 7a0a27d..7c4c273 100644 (file)
@@ -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);
index c83e35f..d38d7d7 100644 (file)
@@ -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)