From 17f9204bbadb5a145883f6ff84cf61fa2f7bfca2 Mon Sep 17 00:00:00 2001
From: hjk <qtc-committer@nokia.com>
Date: Fri, 11 Sep 2009 12:30:53 +0200
Subject: [PATCH] debugger: refactoring in the gdbengine

---
 src/plugins/debugger/debuggermanager.h        |  1 +
 src/plugins/debugger/gdb/gdbengine.cpp        | 72 +++++++++++--------
 src/plugins/debugger/gdb/gdbengine.h          |  7 ++
 src/plugins/debugger/gdb/gdbprocessbase.h     |  4 ++
 .../debugger/symbian/symbianadapter.cpp       | 18 ++++-
 src/plugins/debugger/symbian/symbianadapter.h |  1 +
 tests/manual/trk/runner.pro                   |  2 +
 7 files changed, 72 insertions(+), 33 deletions(-)

diff --git a/src/plugins/debugger/debuggermanager.h b/src/plugins/debugger/debuggermanager.h
index 3b9622e7f4c..e857f7bdb82 100644
--- a/src/plugins/debugger/debuggermanager.h
+++ b/src/plugins/debugger/debuggermanager.h
@@ -430,6 +430,7 @@ private:
     BreakpointData *findBreakpoint(const QString &fileName, int lineNumber);
     void setToolTipExpression(const QPoint &mousePos, TextEditor::ITextEditor *editor, int cursorPos);
 
+    // FIXME: Remove engine-specific state
     QSharedPointer<DebuggerStartParameters> m_startParameters;
     DebuggerRunControl *m_runControl;
     QString m_dumperLib;
diff --git a/src/plugins/debugger/gdb/gdbengine.cpp b/src/plugins/debugger/gdb/gdbengine.cpp
index 84380f20548..02e84fd6a94 100644
--- a/src/plugins/debugger/gdb/gdbengine.cpp
+++ b/src/plugins/debugger/gdb/gdbengine.cpp
@@ -138,6 +138,20 @@ static QByteArray parsePlainConsoleStream(const GdbResultRecord &record)
     return out.mid(pos + 3);
 }
 
+///////////////////////////////////////////////////////////////////////
+//
+// GdbProcess
+//
+///////////////////////////////////////////////////////////////////////
+
+void GdbProcess::attach(GdbEngine *engine) const
+{
+    QFileInfo fi(engine->startParameters().executable);
+    QString fileName = fi.absoluteFilePath();
+    engine->postCommand(_("-file-exec-and-symbols ") + fileName,
+        &GdbEngine::handleFileExecAndSymbols, "handleFileExecAndSymbols");
+}
+
 ///////////////////////////////////////////////////////////////////////
 //
 // GdbEngine
@@ -1678,26 +1692,28 @@ void GdbEngine::startDebugger2()
     } else if (m_startParameters.useTerminal) {
         qq->breakHandler()->setAllPending();
     } else if (q->startMode() == StartInternal || q->startMode() == StartExternal) {
-        QFileInfo fi(m_startParameters.executable);
-        QString fileName = _c('"') + fi.absoluteFilePath() + _c('"');
-        postCommand(_("-file-exec-and-symbols ") + fileName, CB(handleFileExecAndSymbols));
-        //postCommand(_("file ") + fileName, handleFileExecAndSymbols);
-        #ifdef Q_OS_MAC
-        postCommand(_("sharedlibrary apply-load-rules all"));
-        #endif
-        if (!m_startParameters.processArgs.isEmpty())
-            postCommand(_("-exec-arguments ") + m_startParameters.processArgs.join(_(" ")));
-        #ifndef Q_OS_MAC        
-        if (!m_dumperInjectionLoad)
-            postCommand(_("set auto-solib-add off"));
-        postCommand(_("info target"), CB(handleStart));
-        #else
-        // On MacOS, breaking in at the entry point wreaks havoc.
-        postCommand(_("tbreak main"));
-        m_waitingForFirstBreakpointToBeHit = true;
-        qq->notifyInferiorRunningRequested();
-        postCommand(_("-exec-run"), CB(handleExecRun));
-        #endif
+        m_gdbProc->attach(this);
+        if (m_gdbProc->isAdapter()) {
+            qq->notifyInferiorRunningRequested();
+            postCommand(_("-exec-continue"), CB(handleExecContinue));
+        } else {
+            #ifdef Q_OS_MAC
+            postCommand(_("sharedlibrary apply-load-rules all"));
+            #endif
+            if (!m_startParameters.processArgs.isEmpty())
+                postCommand(_("-exec-arguments ") + m_startParameters.processArgs.join(_(" ")));
+            #ifdef Q_OS_MAC        
+            // On MacOS, breaking in at the entry point wreaks havoc.
+            postCommand(_("tbreak main"));
+            m_waitingForFirstBreakpointToBeHit = true;
+            qq->notifyInferiorRunningRequested();
+            postCommand(_("-exec-run"), CB(handleExecRun));
+            #else
+            if (!m_dumperInjectionLoad)
+                postCommand(_("set auto-solib-add off"));
+            postCommand(_("info target"), CB(handleStart));
+            #endif
+        }
         qq->breakHandler()->setAllPending();
     }
 
@@ -1724,16 +1740,11 @@ void GdbEngine::handleStart(const GdbResultRecord &response, const QVariant &)
         QString msg = _(response.data.findChild("consolestreamoutput").data());
         QRegExp needle(_("\\bEntry point: (0x[0-9a-f]+)\\b"));
         if (needle.indexIn(msg) != -1) {
-            if (m_gdbProc->isAdapter()) {
-                postCommand(_("-exec-continue"), CB(handleExecRun));
-                qq->notifyInferiorRunningRequested();
-            } else {
-                //debugMessage(_("STREAM: ") + msg + " " + needle.cap(1));
-                postCommand(_("tbreak *") + needle.cap(1));
-                m_waitingForFirstBreakpointToBeHit = true;
-                qq->notifyInferiorRunningRequested();
-                postCommand(_("-exec-run"), CB(handleExecRun));
-            }
+            //debugMessage(_("STREAM: ") + msg + " " + needle.cap(1));
+            postCommand(_("tbreak *") + needle.cap(1));
+            m_waitingForFirstBreakpointToBeHit = true;
+            qq->notifyInferiorRunningRequested();
+            postCommand(_("-exec-run"), CB(handleExecRun));
         } else {
             debugMessage(_("PARSING START ADDRESS FAILED: ") + msg);
         }
@@ -1771,7 +1782,6 @@ void GdbEngine::handleAttach(const GdbResultRecord &, const QVariant &)
 void GdbEngine::handleSetTargetAsync(const GdbResultRecord &record, const QVariant &)
 {
     if (record.resultClass == GdbResultDone) {
-        //postCommand(_("info target"), handleStart);
         qq->notifyInferiorRunningRequested();
         postCommand(_("target remote %1").arg(q->startParameters()->remoteChannel),
             CB(handleTargetRemote));
diff --git a/src/plugins/debugger/gdb/gdbengine.h b/src/plugins/debugger/gdb/gdbengine.h
index e202ec987a5..53ae548e277 100644
--- a/src/plugins/debugger/gdb/gdbengine.h
+++ b/src/plugins/debugger/gdb/gdbengine.h
@@ -107,6 +107,7 @@ public:
     void setWorkingDirectory(const QString &dir) { m_proc.setWorkingDirectory(dir); }
     void setEnvironment(const QStringList &env) { m_proc.setEnvironment(env); }
     bool isAdapter() const { return false; }
+    void attach(GdbEngine *engine) const;
 
 private:
     QProcess m_proc;
@@ -126,6 +127,11 @@ signals:
     void applicationOutputAvailable(const QString &output);
 
 private:
+    friend class GdbProcess;
+    friend class SymbianAdapter;
+
+    const DebuggerStartParameters &startParameters() const
+        { return m_startParameters; }
     //
     // IDebuggerEngine implementation
     //
@@ -206,6 +212,7 @@ public: // otherwise the Qt flag macros are unhappy
     };
     Q_DECLARE_FLAGS(GdbCommandFlags, GdbCommandFlag)
 
+
 private:
     typedef void (GdbEngine::*GdbCommandCallback)(const GdbResultRecord &record, const QVariant &cookie);
 
diff --git a/src/plugins/debugger/gdb/gdbprocessbase.h b/src/plugins/debugger/gdb/gdbprocessbase.h
index 6c5825e6f66..71483cd2ee5 100644
--- a/src/plugins/debugger/gdb/gdbprocessbase.h
+++ b/src/plugins/debugger/gdb/gdbprocessbase.h
@@ -36,6 +36,8 @@
 namespace Debugger {
 namespace Internal {
 
+class GdbEngine;
+
 // GdbProcessBase is inherited by GdbProcess and the gdb/trk Adapter.
 // In the GdbProcess case it's just a wrapper around a QProcess running
 // gdb, in the Adapter case it's the interface to the gdb process in
@@ -62,6 +64,8 @@ public:
     virtual void setEnvironment(const QStringList &env) = 0;
     virtual bool isAdapter() const = 0;
 
+    virtual void attach(GdbEngine *engine) const = 0;
+
 signals:
     void error(QProcess::ProcessError);
     void started();
diff --git a/src/plugins/debugger/symbian/symbianadapter.cpp b/src/plugins/debugger/symbian/symbianadapter.cpp
index 667bc6bf574..c5ff3dc2b24 100644
--- a/src/plugins/debugger/symbian/symbianadapter.cpp
+++ b/src/plugins/debugger/symbian/symbianadapter.cpp
@@ -28,9 +28,11 @@
 **************************************************************************/
 
 #include "symbianadapter.h"
+#ifndef STANDALONE_RUNNER
+#include "gdb/gdbengine.h"
+#endif
 
 #define TrkCB(s) TrkCallback(this, &SymbianAdapter::s)
-#define GdbCB(s) GdbCallback(this, &SymbianAdapter::s)
 
 
 using namespace trk;
@@ -1307,7 +1309,7 @@ void SymbianAdapter::sendGdbMessage(const QString &msg, GdbCallback callback,
     data.command = msg;
     data.callback = callback;
     data.cookie = cookie;
-    logMessage(QString("<- GDB: %2").arg(msg));
+    logMessage(QString("<- ADAPTER TO GDB: %2").arg(msg));
     m_gdbProc.write(msg.toLatin1() + "\n");
 }
 
@@ -1410,5 +1412,17 @@ void SymbianAdapter::setEnvironment(const QStringList &env)
     m_gdbProc.setEnvironment(env);
 }
 
+void SymbianAdapter::attach(GdbEngine *engine) const
+{
+#ifdef STANDALONE_RUNNER
+#else
+    QString fileName = engine->startParameters().executable; 
+    engine->postCommand(_("add-symbol-file \"%1\" %2").arg(fileName)
+        .arg(m_session.codeseg));
+    engine->postCommand(_("symbol-file \"%1\"").arg(fileName));
+    engine->postCommand(_("target remote ") + gdbServerName());
+#endif
+}
+
 } // namespace Internal
 } // namespace Debugger
diff --git a/src/plugins/debugger/symbian/symbianadapter.h b/src/plugins/debugger/symbian/symbianadapter.h
index ed508d91289..713692d44fa 100644
--- a/src/plugins/debugger/symbian/symbianadapter.h
+++ b/src/plugins/debugger/symbian/symbianadapter.h
@@ -132,6 +132,7 @@ public:
     void setWorkingDirectory(const QString &dir);
     void setEnvironment(const QStringList &env);
     bool isAdapter() const { return true; }
+    void attach(GdbEngine *engine) const;
 
     //
     // TRK
diff --git a/tests/manual/trk/runner.pro b/tests/manual/trk/runner.pro
index 21461da2d94..20240eb1f73 100644
--- a/tests/manual/trk/runner.pro
+++ b/tests/manual/trk/runner.pro
@@ -4,6 +4,8 @@ TEMPLATE = app
 DEBUGGERHOME = ../../../src/plugins/debugger/symbian
 INCLUDEPATH *= $$DEBUGGERHOME
 
+DEFINES += STANDALONE_RUNNER
+
 QT += network
 
 win32:CONFIG+=console
-- 
GitLab