Index: trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.cpp	(revision 47548)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.cpp	(revision 47551)
@@ -76,4 +76,5 @@
 static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
 {
+    int rc;
 #ifdef VBOX_WITH_GUEST_PROPS
     /*
@@ -82,5 +83,5 @@
      */
     uint32_t uGuestPropSvcClientID;
-    int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
+    rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
     if (RT_FAILURE(rc))
     {
@@ -91,5 +92,5 @@
         }
         else
-            VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
+            VBoxServiceError("Failed to connect to the guest property service, rc=%Rrc\n", rc);
     }
     else
@@ -100,9 +101,16 @@
     if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
         rc = VINF_SUCCESS;
-    return rc;
 #else
     /* Nothing to do here yet. */
-    return VINF_SUCCESS;
-#endif
+    rc = VINF_SUCCESS;
+#endif
+
+    if (RT_SUCCESS(rc))
+    {
+        /* Init session object. */
+        rc = GstCntlSessionInit(&g_Session, 0 /* Flags */);
+    }
+
+    return rc;
 }
 
@@ -149,6 +157,4 @@
     /* The status code is ignored as this information is not available with VBox < 3.2.10. */
 
-    /* Init session object. */
-    rc = GstCntlSessionInit(&g_Session, 0 /* Flags */);
     if (RT_SUCCESS(rc))
         rc = VbglR3GuestCtrlConnect(&g_uControlSvcClientID);
@@ -329,5 +335,5 @@
 
         rc = GstCntlSessionThreadCreate(&g_lstControlSessionThreads,
-                                        &ssInfo, NULL /* Session */);
+                                        &ssInfo, NULL /* ppSessionThread */);
     }
 
Index: trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.h
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.h	(revision 47548)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.h	(revision 47551)
@@ -143,8 +143,8 @@
  *  as the main executable. */
 #define VBOXSERVICECTRLSESSION_FLAG_ANONYMOUS            RT_BIT(1)
-/** Flag indicating that start guest processes will dump their
+/** Flag indicating that started guest processes will dump their
  *  stdout output to a separate file on disk. For debugging. */
 #define VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT           RT_BIT(2)
-/** Flag indicating that start guest processes will dump their
+/** Flag indicating that started guest processes will dump their
  *  stderr output to a separate file on disk. For debugging. */
 #define VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR           RT_BIT(3)
@@ -174,5 +174,7 @@
     /** The session's critical section. */
     RTCRITSECT                      CritSect;
-    /** Session flags. */
+    /** Internal session flags, not related
+     *  to StartupInfo stuff.
+     *  @sa VBOXSERVICECTRLSESSION_FLAG_* flags. */
     uint32_t                        uFlags;
     /** How many processes do we allow keeping around at a time? */
Index: trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlProcess.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlProcess.cpp	(revision 47548)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlProcess.cpp	(revision 47551)
@@ -1381,4 +1381,38 @@
     return rc;
 }
+
+
+#ifdef DEBUG
+static int gstcntlProcessDumpToFile(const char *pszFileName, void *pvBuf, size_t cbBuf)
+{
+    AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
+    AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
+
+    if (!cbBuf)
+        return VINF_SUCCESS;
+
+    char szFile[RTPATH_MAX];
+
+    int rc = RTPathTemp(szFile, sizeof(szFile));
+    if (RT_SUCCESS(rc))
+        rc = RTPathAppend(szFile, sizeof(szFile), pszFileName);
+
+    if (RT_SUCCESS(rc))
+    {
+        VBoxServiceVerbose(4, "Dumping %ld bytes to \"%s\"\n", cbBuf, szFile);
+
+        RTFILE fh;
+        rc = RTFileOpen(&fh, szFile, RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
+        if (RT_SUCCESS(rc))
+        {
+            rc = RTFileWrite(fh, pvBuf, cbBuf, NULL /* pcbWritten */);
+            RTFileClose(fh);
+        }
+    }
+
+    return rc;
+}
+#endif
+
 
 /**
@@ -1894,20 +1928,23 @@
             rc = VINF_EOF;
 
-#if 0
+#ifdef DEBUG
         if (RT_SUCCESS(rc))
         {
-            if (   (pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT)
-                && (uHandle == OUTPUT_HANDLE_ID_STDERR))
-            {
+            if (   pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT
+                && (   uHandle == OUTPUT_HANDLE_ID_STDOUT
+                    || uHandle == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED)
+               )
+            {
+                VBoxServiceVerbose(3, "[PID %RU32]: dump stdout\n",
+                                   pThis->uPID);
                 char szDumpFile[RTPATH_MAX];
                 if (!RTStrPrintf(szDumpFile, sizeof(szDumpFile), "VBoxService_Session%RU32_PID%RU32_StdOut.txt",
                                  pSession->StartupInfo.uSessionID, pThis->uPID)) rc = VERR_BUFFER_UNDERFLOW;
                 if (RT_SUCCESS(rc))
-                    rc = gstcntlSessionDumpToFile(szDumpFile, pvBuf, cbRead);
+                    rc = gstcntlProcessDumpToFile(szDumpFile, pvBuf, cbRead);
                 AssertRC(rc);
             }
-            else if (   (pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR)
-                     && (   uHandle == OUTPUT_HANDLE_ID_STDOUT
-                         || uHandle == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED))
+            else if (   pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR
+                     && uHandle == OUTPUT_HANDLE_ID_STDERR)
             {
                 char szDumpFile[RTPATH_MAX];
@@ -1916,5 +1953,5 @@
                     rc = VERR_BUFFER_UNDERFLOW;
                 if (RT_SUCCESS(rc))
-                    rc = gstcntlSessionDumpToFile(szDumpFile, pvBuf, cbRead);
+                    rc = gstcntlProcessDumpToFile(szDumpFile, pvBuf, cbRead);
                 AssertRC(rc);
             }
Index: trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlSession.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlSession.cpp	(revision 47548)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlSession.cpp	(revision 47551)
@@ -74,5 +74,10 @@
 enum
 {
-    VBOXSERVICESESSIONOPT_LOG_FILE = 1000,
+    VBOXSERVICESESSIONOPT_FIRST = 1000, /* For initialization. */
+#ifdef DEBUG
+    VBOXSERVICESESSIONOPT_DUMP_STDOUT,
+    VBOXSERVICESESSIONOPT_DUMP_STDERR,
+#endif
+    VBOXSERVICESESSIONOPT_LOG_FILE,
     VBOXSERVICESESSIONOPT_USERNAME,
     VBOXSERVICESESSIONOPT_SESSION_ID,
@@ -114,37 +119,4 @@
     return NULL;
 }
-
-
-#ifdef DEBUG
-static int gstcntlSessionDumpToFile(const char *pszFileName, void *pvBuf, size_t cbBuf)
-{
-    AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
-    AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
-
-    if (!cbBuf)
-        return VINF_SUCCESS;
-
-    char szFile[RTPATH_MAX];
-
-    int rc = RTPathTemp(szFile, sizeof(szFile));
-    if (RT_SUCCESS(rc))
-        rc = RTPathAppend(szFile, sizeof(szFile), pszFileName);
-
-    if (RT_SUCCESS(rc))
-    {
-        VBoxServiceVerbose(4, "Dumping %ld bytes to \"%s\"\n", cbBuf, szFile);
-
-        RTFILE fh;
-        rc = RTFileOpen(&fh, szFile, RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
-        if (RT_SUCCESS(rc))
-        {
-            rc = RTFileWrite(fh, pvBuf, cbBuf, NULL /* pcbWritten */);
-            RTFileClose(fh);
-        }
-    }
-
-    return rc;
-}
-#endif
 
 
@@ -1378,13 +1350,4 @@
 
     pSession->uFlags = uFlags;
-
-    if (pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_FORK)
-    {
-        /* Protocol must be specified explicitly. */
-        pSession->StartupInfo.uProtocol = UINT32_MAX;
-
-        /* Session ID must be specified explicitly. */
-        pSession->StartupInfo.uSessionID = UINT32_MAX;
-    }
 
     /* Init critical section for protecting the thread lists. */
@@ -1707,8 +1670,32 @@
                     if (RT_SUCCESS(rc2))
                         papszArgs[iOptIdx++] = szParmLogFile;
-                    papszArgs[iOptIdx++] = NULL;
+
+                    rc = rc2;
                 }
-                else
-                    papszArgs[iOptIdx++] = NULL;
+                else if (RT_FAILURE(rc2))
+                    rc = rc2;
+#ifdef DEBUG
+                VBoxServiceVerbose(4, "rc=%Rrc, session flags=%x\n",
+                                   rc, g_Session.uFlags);
+                char szParmDumpStdOut[32];
+                if (   RT_SUCCESS(rc)
+                    && g_Session.uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT)
+                {
+                    if (!RTStrPrintf(szParmDumpStdOut, sizeof(szParmDumpStdOut), "--dump-stdout"))
+                        rc = VERR_BUFFER_OVERFLOW;
+                    if (RT_SUCCESS(rc))
+                        papszArgs[iOptIdx++] = szParmDumpStdOut;
+                }
+                char szParmDumpStdErr[32];
+                if (   RT_SUCCESS(rc)
+                    && g_Session.uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR)
+                {
+                    if (!RTStrPrintf(szParmDumpStdErr, sizeof(szParmDumpStdErr), "--dump-stderr"))
+                        rc = VERR_BUFFER_OVERFLOW;
+                    if (RT_SUCCESS(rc))
+                        papszArgs[iOptIdx++] = szParmDumpStdErr;
+                }
+#endif
+                papszArgs[iOptIdx++] = NULL;
 
                 if (g_cVerbosity > 3)
@@ -1777,5 +1764,6 @@
 #else
                 RTHANDLE hStdIn;
-                rc = RTFileOpenBitBucket(&hStdIn.u.hFile, RTFILE_O_READ);
+                if (RT_SUCCESS(rc))
+                    rc = RTFileOpenBitBucket(&hStdIn.u.hFile, RTFILE_O_READ);
                 if (RT_SUCCESS(rc))
                 {
@@ -1973,4 +1961,8 @@
     static const RTGETOPTDEF s_aOptions[] =
     {
+#ifdef DEBUG
+        { "--dump-stdout",     VBOXSERVICESESSIONOPT_DUMP_STDOUT,     RTGETOPT_REQ_NOTHING },
+        { "--dump-stderr",     VBOXSERVICESESSIONOPT_DUMP_STDERR,     RTGETOPT_REQ_NOTHING },
+#endif
         { "--logfile",         VBOXSERVICESESSIONOPT_LOG_FILE,        RTGETOPT_REQ_STRING },
         { "--user",            VBOXSERVICESESSIONOPT_USERNAME,        RTGETOPT_REQ_STRING },
@@ -1992,8 +1984,9 @@
     uint32_t uSessionFlags = VBOXSERVICECTRLSESSION_FLAG_FORK;
 
-    /* Init the session object. */
-    int rc = GstCntlSessionInit(&g_Session, uSessionFlags);
-    if (RT_FAILURE(rc))
-        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize session object, rc=%Rrc\n", rc);
+    /* Protocol and session ID must be specified explicitly. */
+    g_Session.StartupInfo.uProtocol  = UINT32_MAX;
+    g_Session.StartupInfo.uSessionID = UINT32_MAX;
+
+    int rc = VINF_SUCCESS;
 
     while (   (ch = RTGetOpt(&GetState, &ValueUnion))
@@ -2008,7 +2001,15 @@
                                           ValueUnion.psz);
                 break;
-
+#ifdef DEBUG
+            case VBOXSERVICESESSIONOPT_DUMP_STDOUT:
+                uSessionFlags |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT;
+                break;
+
+            case VBOXSERVICESESSIONOPT_DUMP_STDERR:
+                uSessionFlags |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR;
+                break;
+#endif
             case VBOXSERVICESESSIONOPT_USERNAME:
-                /** @todo. */
+                /** @todo Information not needed right now, skip. */
                 break;
 
@@ -2049,4 +2050,9 @@
     if (g_Session.StartupInfo.uSessionID == UINT32_MAX)
         return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No session ID specified");
+
+    /* Init the session object. */
+    rc = GstCntlSessionInit(&g_Session, uSessionFlags);
+    if (RT_FAILURE(rc))
+        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize session object, rc=%Rrc\n", rc);
 
     rc = VBoxServiceLogCreate(strlen(g_szLogFile) ? g_szLogFile : NULL);
