Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 98604)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 98605)
@@ -619,4 +619,31 @@
     return uisession()->acquireEffectiveCPULoad(uLoad);
 }
+
+#ifdef VBOX_WITH_DEBUGGER_GUI
+bool UIMachine::dbgCreated(void *pActionDebug)
+{
+    return uisession()->dbgCreated(pActionDebug);
+}
+
+void UIMachine::dbgDestroy()
+{
+    return uisession()->dbgDestroy();
+}
+
+void UIMachine::dbgShowStatistics()
+{
+    return uisession()->dbgShowStatistics();
+}
+
+void UIMachine::dbgShowCommandLine()
+{
+    return uisession()->dbgShowCommandLine();
+}
+
+void UIMachine::dbgAdjustRelativePos()
+{
+    return uisession()->dbgAdjustRelativePos();
+}
+#endif /* VBOX_WITH_DEBUGGER_GUI */
 
 void UIMachine::detachUi()
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h	(revision 98604)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h	(revision 98605)
@@ -459,4 +459,19 @@
         /** Acquires effective CPU @a uLoad. */
         bool acquireEffectiveCPULoad(ulong &uLoad);
+
+#ifdef VBOX_WITH_DEBUGGER_GUI
+        /** Makes sure debugger GUI is created. */
+        bool dbgCreated(void *pActionDebug);
+        /** Makes sure debugger GUI is destroyed. */
+        void dbgDestroy();
+
+        /** Shows debugger UI statistics window. */
+        void dbgShowStatistics();
+        /** Shows debugger UI command line window. */
+        void dbgShowCommandLine();
+
+        /** Adjusts relative position for debugger window. */
+        void dbgAdjustRelativePos();
+#endif /* VBOX_WITH_DEBUGGER_GUI */
     /** @} */
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(revision 98604)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(revision 98605)
@@ -107,8 +107,4 @@
 #include <iprt/path.h>
 #include <iprt/thread.h>
-#ifdef VBOX_WITH_DEBUGGER_GUI
-# include <VBox/dbggui.h>
-# include <iprt/ldr.h>
-#endif
 
 /* VirtualBox interface declarations: */
@@ -217,11 +213,4 @@
 #endif /* VBOX_WS_MAC */
 
-#if 0 /* To early! The debugger needs a VM handle to work. So, must be done after power on.  Moved to initializePostPowerUp. */
-#ifdef VBOX_WITH_DEBUGGER_GUI
-    /* Prepare debugger: */
-    prepareDebugger();
-#endif /* VBOX_WITH_DEBUGGER_GUI */
-#endif
-
     /* Load settings: */
     loadSettings();
@@ -265,5 +254,5 @@
 #ifdef VBOX_WITH_DEBUGGER_GUI
     prepareDebugger();
-#endif
+#endif /* VBOX_WITH_DEBUGGER_GUI */
     sltMachineStateChanged();
     sltAdditionsStateChanged();
@@ -279,9 +268,4 @@
 {
     return uimachine()->actionPool();
-}
-
-CSession& UIMachineLogic::session() const
-{
-    return uisession()->session();
 }
 
@@ -758,8 +742,4 @@
     , m_pDragAndDropActions(0)
     , m_fIsWindowsCreated(false)
-#ifdef VBOX_WITH_DEBUGGER_GUI
-    , m_pDbgGui(0)
-    , m_pDbgGuiVT(0)
-#endif /* VBOX_WITH_DEBUGGER_GUI */
 #ifdef VBOX_WS_MAC
     , m_fIsDockIconEnabled(true)
@@ -1388,5 +1368,5 @@
 {
     /* Close debugger: */
-    dbgDestroy();
+    uimachine()->dbgDestroy();
 }
 #endif /* VBOX_WITH_DEBUGGER_GUI */
@@ -2471,10 +2451,8 @@
 void UIMachineLogic::sltShowDebugStatistics()
 {
-    if (dbgCreated())
+    if (uimachine()->dbgCreated(actionPool()->action(UIActionIndexRT_M_Debug)))
     {
         keyboardHandler()->setDebuggerActive();
-        const QByteArray &expandBytes = uiCommon().getDebuggerStatisticsExpand().toUtf8();
-        const QByteArray &filterBytes = uiCommon().getDebuggerStatisticsFilter().toUtf8();
-        m_pDbgGuiVT->pfnShowStatistics(m_pDbgGui, filterBytes.constData(), expandBytes.constData());
+        uimachine()->dbgShowStatistics();
     }
 }
@@ -2482,8 +2460,8 @@
 void UIMachineLogic::sltShowDebugCommandLine()
 {
-    if (dbgCreated())
+    if (uimachine()->dbgCreated(actionPool()->action(UIActionIndexRT_M_Debug)))
     {
         keyboardHandler()->setDebuggerActive();
-        m_pDbgGuiVT->pfnShowCommandLine(m_pDbgGui);
+        uimachine()->dbgShowCommandLine();
     }
 }
@@ -2711,9 +2689,8 @@
 {
 #ifdef VBOX_WITH_DEBUGGER_GUI
-    /* Cleanup debugger before VBoxDbg module handle cleaned up: */
     cleanupDebugger();
+    sltCloseGuestControlConsoleDialog();
+#endif /* VBOX_WITH_DEBUGGER_GUI */
     sltCloseLogDialog();
-    sltCloseGuestControlConsoleDialog();
-#endif
     activateScreenSaver();
     sltCloseFileManagerDialog();
@@ -2735,15 +2712,4 @@
     pHostKeyAction->toggle();
 }
-
-#ifdef VBOX_WITH_DEBUGGER_GUI
-void UIMachineLogic::dbgAdjustRelativePos()
-{
-    if (m_pDbgGui)
-    {
-        const QRect rct = activeMachineWindow()->frameGeometry();
-        m_pDbgGuiVT->pfnAdjustRelativePos(m_pDbgGui, rct.x(), rct.y(), rct.width(), rct.height());
-    }
-}
-#endif /* VBOX_WITH_DEBUGGER_GUI */
 
 void UIMachineLogic::updateMenuDevicesStorage(QMenu *pMenu)
@@ -3265,57 +3231,4 @@
 }
 
-#ifdef VBOX_WITH_DEBUGGER_GUI
-
-bool UIMachineLogic::dbgCreated()
-{
-    if (m_pDbgGui)
-        return true;
-
-    RTLDRMOD hLdrMod = uiCommon().getDebuggerModule();
-    if (hLdrMod == NIL_RTLDRMOD)
-        return false;
-
-    PFNDBGGUICREATE pfnGuiCreate;
-    int rc = RTLdrGetSymbol(hLdrMod, "DBGGuiCreate", (void**)&pfnGuiCreate);
-    if (RT_SUCCESS(rc))
-    {
-        ISession *pISession = session().raw();
-        rc = pfnGuiCreate(pISession, &m_pDbgGui, &m_pDbgGuiVT);
-        if (RT_SUCCESS(rc))
-        {
-            if (   DBGGUIVT_ARE_VERSIONS_COMPATIBLE(m_pDbgGuiVT->u32Version, DBGGUIVT_VERSION)
-                || m_pDbgGuiVT->u32EndVersion == m_pDbgGuiVT->u32Version)
-            {
-                m_pDbgGuiVT->pfnSetParent(m_pDbgGui, activeMachineWindow());
-                m_pDbgGuiVT->pfnSetMenu(m_pDbgGui, actionPool()->action(UIActionIndexRT_M_Debug));
-                dbgAdjustRelativePos();
-                return true;
-            }
-
-            LogRel(("GUI: DBGGuiCreate failed, incompatible versions (loaded %#x/%#x, expected %#x)\n",
-                    m_pDbgGuiVT->u32Version, m_pDbgGuiVT->u32EndVersion, DBGGUIVT_VERSION));
-        }
-        else
-            LogRel(("GUI: DBGGuiCreate failed, rc=%Rrc\n", rc));
-    }
-    else
-        LogRel(("GUI: RTLdrGetSymbol(,\"DBGGuiCreate\",) -> %Rrc\n", rc));
-
-    m_pDbgGui = 0;
-    m_pDbgGuiVT = 0;
-    return false;
-}
-
-void UIMachineLogic::dbgDestroy()
-{
-    if (m_pDbgGui)
-    {
-        m_pDbgGuiVT->pfnDestroy(m_pDbgGui);
-        m_pDbgGui = 0;
-        m_pDbgGuiVT = 0;
-    }
-}
-#endif /* VBOX_WITH_DEBUGGER_GUI */
-
 void UIMachineLogic::reset(bool fShowConfirmation)
 {
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.h	(revision 98604)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.h	(revision 98605)
@@ -54,5 +54,4 @@
 class UISoftKeyboard;
 class UIVMInformationDialog;
-class CSession;
 class CMachine;
 class CConsole;
@@ -65,9 +64,4 @@
 #endif
 
-#ifdef VBOX_WITH_DEBUGGER_GUI
-typedef struct DBGGUIVT const *PCDBGGUIVT;
-typedef struct DBGGUI *PDBGGUI;
-#endif /* VBOX_WITH_DEBUGGER_GUI */
-
 /* Machine logic interface: */
 class UIMachineLogic : public QIWithRetranslateUI3<QObject>
@@ -115,6 +109,4 @@
     UIActionPool *actionPool() const;
 
-    /** Returns the session reference. */
-    CSession& session() const;
     /** Returns the session's machine reference. */
     CMachine& machine() const;
@@ -148,9 +140,4 @@
     /** An public interface to sltTypeHostKeyComboPressRelease. */
     void typeHostKeyComboPressRelease(bool fToggleSequence);
-
-#ifdef VBOX_WITH_DEBUGGER_GUI
-    /** Adjusts relative position for debugger window. */
-    void dbgAdjustRelativePos();
-#endif /* VBOX_WITH_DEBUGGER_GUI */
 
 protected slots:
@@ -415,14 +402,4 @@
     bool m_fIsWindowsCreated : 1;
 
-#ifdef VBOX_WITH_DEBUGGER_GUI
-    /* Debugger functionality: */
-    bool dbgCreated();
-    void dbgDestroy();
-    /* The handle to the debugger GUI: */
-    PDBGGUI m_pDbgGui;
-    /* The virtual method table for the debugger GUI: */
-    PCDBGGUIVT m_pDbgGuiVT;
-#endif /* VBOX_WITH_DEBUGGER_GUI */
-
 #ifdef VBOX_WS_MAC
     bool m_fIsDockIconEnabled;
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp	(revision 98604)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp	(revision 98605)
@@ -464,7 +464,7 @@
                         /* We are going to show close-dialog only
                          * if headless frontend stopped/killed already: */
-                        CMachine machine = uisession()->machine();
-                        KMachineState machineState = machine.GetState();
-                        fShowCloseDialog = !machine.isOk() || machineState == KMachineState_Null;
+                        CMachine comMachine = machine();
+                        KMachineState enmMachineState = comMachine.GetState();
+                        fShowCloseDialog = !comMachine.isOk() || enmMachineState == KMachineState_Null;
                     }
                 }
@@ -646,5 +646,5 @@
     /* The debugger windows are bind to the main VM window. */
     if (m_uScreenId == 0)
-        machineLogic()->dbgAdjustRelativePos();
+        uimachine()->dbgAdjustRelativePos();
 }
 #endif /* VBOX_WITH_DEBUGGER_GUI */
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp	(revision 98604)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp	(revision 98605)
@@ -72,4 +72,10 @@
 #endif
 
+/* Other VBox includes: */
+#ifdef VBOX_WITH_DEBUGGER_GUI
+# include <VBox/dbggui.h>
+# include <iprt/ldr.h>
+#endif
+
 /* External includes: */
 #ifdef VBOX_WS_X11
@@ -712,4 +718,77 @@
     return fSuccess;
 }
+
+#ifdef VBOX_WITH_DEBUGGER_GUI
+bool UISession::dbgCreated(void *pActionDebug)
+{
+    if (m_pDbgGui)
+        return true;
+
+    RTLDRMOD hLdrMod = uiCommon().getDebuggerModule();
+    if (hLdrMod == NIL_RTLDRMOD)
+        return false;
+
+    PFNDBGGUICREATE pfnGuiCreate;
+    int rc = RTLdrGetSymbol(hLdrMod, "DBGGuiCreate", (void**)&pfnGuiCreate);
+    if (RT_SUCCESS(rc))
+    {
+        ISession *pISession = session().raw();
+        rc = pfnGuiCreate(pISession, &m_pDbgGui, &m_pDbgGuiVT);
+        if (RT_SUCCESS(rc))
+        {
+            if (   DBGGUIVT_ARE_VERSIONS_COMPATIBLE(m_pDbgGuiVT->u32Version, DBGGUIVT_VERSION)
+                || m_pDbgGuiVT->u32EndVersion == m_pDbgGuiVT->u32Version)
+            {
+                m_pDbgGuiVT->pfnSetParent(m_pDbgGui, activeMachineWindow());
+                m_pDbgGuiVT->pfnSetMenu(m_pDbgGui, pActionDebug);
+                dbgAdjustRelativePos();
+                return true;
+            }
+
+            LogRel(("GUI: DBGGuiCreate failed, incompatible versions (loaded %#x/%#x, expected %#x)\n",
+                    m_pDbgGuiVT->u32Version, m_pDbgGuiVT->u32EndVersion, DBGGUIVT_VERSION));
+        }
+        else
+            LogRel(("GUI: DBGGuiCreate failed, rc=%Rrc\n", rc));
+    }
+    else
+        LogRel(("GUI: RTLdrGetSymbol(,\"DBGGuiCreate\",) -> %Rrc\n", rc));
+
+    m_pDbgGui = 0;
+    m_pDbgGuiVT = 0;
+    return false;
+}
+
+void UISession::dbgDestroy()
+{
+    if (m_pDbgGui)
+    {
+        m_pDbgGuiVT->pfnDestroy(m_pDbgGui);
+        m_pDbgGui = 0;
+        m_pDbgGuiVT = 0;
+    }
+}
+
+void UISession::dbgShowStatistics()
+{
+    const QByteArray &expandBytes = uiCommon().getDebuggerStatisticsExpand().toUtf8();
+    const QByteArray &filterBytes = uiCommon().getDebuggerStatisticsFilter().toUtf8();
+    m_pDbgGuiVT->pfnShowStatistics(m_pDbgGui, filterBytes.constData(), expandBytes.constData());
+}
+
+void UISession::dbgShowCommandLine()
+{
+    m_pDbgGuiVT->pfnShowCommandLine(m_pDbgGui);
+}
+
+void UISession::dbgAdjustRelativePos()
+{
+    if (m_pDbgGui)
+    {
+        const QRect rct = activeMachineWindow()->frameGeometry();
+        m_pDbgGuiVT->pfnAdjustRelativePos(m_pDbgGui, rct.x(), rct.y(), rct.width(), rct.height());
+    }
+}
+#endif /* VBOX_WITH_DEBUGGER_GUI */
 
 bool UISession::prepareToBeSaved()
@@ -811,4 +890,9 @@
     , m_fIsGuestSupportsGraphics(false)
     , m_fIsGuestSupportsSeamless(false)
+#ifdef VBOX_WITH_DEBUGGER_GUI
+    /* Debug UI stuff: */
+    , m_pDbgGui(0)
+    , m_pDbgGuiVT(0)
+#endif /* VBOX_WITH_DEBUGGER_GUI */
 {
 }
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h	(revision 98604)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h	(revision 98605)
@@ -65,4 +65,9 @@
 class CNetworkAdapter;
 class CMediumAttachment;
+
+#ifdef VBOX_WITH_DEBUGGER_GUI
+typedef struct DBGGUIVT const *PCDBGGUIVT;
+typedef struct DBGGUI *PDBGGUI;
+#endif /* VBOX_WITH_DEBUGGER_GUI */
 
 /** QObject subclass implementing
@@ -368,4 +373,19 @@
         /** Acquires effective CPU @a uLoad. */
         bool acquireEffectiveCPULoad(ulong &uLoad);
+
+#ifdef VBOX_WITH_DEBUGGER_GUI
+        /** Makes sure debugger GUI is created. */
+        bool dbgCreated(void *pActionDebug);
+        /** Makes sure debugger GUI is destroyed. */
+        void dbgDestroy();
+
+        /** Shows debugger UI statistics window. */
+        void dbgShowStatistics();
+        /** Shows debugger UI command line window. */
+        void dbgShowCommandLine();
+
+        /** Adjusts relative position for debugger window. */
+        void dbgAdjustRelativePos();
+#endif /* VBOX_WITH_DEBUGGER_GUI */
     /** @} */
 
@@ -524,4 +544,14 @@
         QVector<UIFrameBuffer*>  m_frameBufferVector;
     /** @} */
+
+    /** @name Debugger stuff.
+     ** @{ */
+#ifdef VBOX_WITH_DEBUGGER_GUI
+        /** Holds the handle to the debugger GUI. */
+        PDBGGUI     m_pDbgGui;
+        /** Holds the virtual method table for the debugger GUI. */
+        PCDBGGUIVT  m_pDbgGuiVT;
+#endif /* VBOX_WITH_DEBUGGER_GUI */
+    /** @} */
 };
 
