Bug 1495039: Add support for MOZ_DEBUG_BROWSER_PROCESS and MOZ_DEBUG_BROWSER_PAUSE env vars to launcher process; r=mhowell
authorAaron Klotz <aklotz@mozilla.com>
Mon, 01 Oct 2018 19:47:14 +0000
changeset 494777 0535f1d5c860ac2bd55209fd6964d8b4c00b875d
parent 494776 05df95ba3a989632898ca6d379cf7cce580f9a76
child 494778 9a685b824b8862df011d96b6a246f7ff83fe5e67
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhowell
bugs1495039
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1495039: Add support for MOZ_DEBUG_BROWSER_PROCESS and MOZ_DEBUG_BROWSER_PAUSE env vars to launcher process; r=mhowell Differential Revision: https://phabricator.services.mozilla.com/D7311
browser/app/winlauncher/LauncherProcessWin.cpp
--- a/browser/app/winlauncher/LauncherProcessWin.cpp
+++ b/browser/app/winlauncher/LauncherProcessWin.cpp
@@ -1,24 +1,26 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
 
 #include "LauncherProcessWin.h"
 
+#include <io.h> // For printf_stderr
 #include <string.h>
 
 #include "mozilla/Attributes.h"
 #include "mozilla/CmdLineAndEnvUtils.h"
 #include "mozilla/DebugOnly.h"
 #include "mozilla/DynamicallyLinkedFunctionPtr.h"
 #include "mozilla/Maybe.h"
 #include "mozilla/SafeMode.h"
+#include "mozilla/Sprintf.h" // For printf_stderr
 #include "mozilla/UniquePtr.h"
 #include "mozilla/WindowsVersion.h"
 #include "mozilla/WinHeaderOnlyUtils.h"
 #include "nsWindowsHelpers.h"
 
 #include <windows.h>
 #include <processthreadsapi.h>
 
@@ -112,16 +114,60 @@ ProcessCmdLine(int& aArgc, wchar_t* aArg
                         mozilla::CheckArgFlag::CheckOSInt |
                         mozilla::CheckArgFlag::RemoveArg) == mozilla::ARG_FOUND) {
     result |= mozilla::LauncherFlags::eNoDeelevate;
   }
 
   return result;
 }
 
+// Duplicated from xpcom glue. Ideally this should be shared.
+static void
+printf_stderr(const char *fmt, ...)
+{
+  if (IsDebuggerPresent()) {
+    char buf[2048];
+    va_list args;
+    va_start(args, fmt);
+    VsprintfLiteral(buf, fmt, args);
+    va_end(args);
+    OutputDebugStringA(buf);
+  }
+
+  FILE *fp = _fdopen(_dup(2), "a");
+  if (!fp)
+      return;
+
+  va_list args;
+  va_start(args, fmt);
+  vfprintf(fp, fmt, args);
+  va_end(args);
+
+  fclose(fp);
+}
+
+static void
+MaybeBreakForBrowserDebugging()
+{
+  if (mozilla::EnvHasValue("MOZ_DEBUG_BROWSER_PROCESS")) {
+    ::DebugBreak();
+    return;
+  }
+
+  const wchar_t* pauseLenS = _wgetenv(L"MOZ_DEBUG_BROWSER_PAUSE");
+  if (!pauseLenS || !(*pauseLenS)) {
+    return;
+  }
+
+  DWORD pauseLenMs = wcstoul(pauseLenS, nullptr, 10) * 1000;
+  printf_stderr("\n\nBROWSERBROWSERBROWSERBROWSER\n  debug me @ %lu\n\n",
+                ::GetCurrentProcessId());
+  ::Sleep(pauseLenMs);
+}
+
 #if defined(MOZ_LAUNCHER_PROCESS)
 
 static mozilla::Maybe<bool>
 IsSameBinaryAsParentProcess()
 {
   mozilla::Maybe<DWORD> parentPid = mozilla::nt::GetParentProcessId();
   if (!parentPid) {
     return mozilla::Nothing();
@@ -175,16 +221,22 @@ RunAsLauncherProcess(int& argc, wchar_t*
     mozilla::SaveToEnv("MOZ_LAUNCHER_PROCESS=");
     result = true;
   }
 
   result |= CheckArg(argc, argv, L"launcher",
                      static_cast<const wchar_t**>(nullptr),
                      CheckArgFlag::RemoveArg) == ARG_FOUND;
 
+  if (!result) {
+    // In this case, we will be proceeding to run as the browser.
+    // We should check MOZ_DEBUG_BROWSER_* env vars.
+    MaybeBreakForBrowserDebugging();
+  }
+
   return result;
 }
 
 int
 LauncherMain(int argc, wchar_t* argv[])
 {
   // Make sure that the launcher process itself has image load policies set
   if (IsWin10AnniversaryUpdateOrLater()) {