Bug 1503538 Part 1/5: Move printf_stderr to a shared header r=aklotz
authorCarl Corcoran <ccorcoran@mozilla.com>
Wed, 06 Mar 2019 00:51:15 +0000
changeset 520364 094cdc0182bc8cc9eb5bcf741a951d147588b77a
parent 520363 4f88eb70887ac247a2daef2c1f0675a6de89d012
child 520365 0fd1ec8af3fd1e2a75f08613414ed590e76ea286
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1503538
milestone67.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 1503538 Part 1/5: Move printf_stderr to a shared header r=aklotz In the tree we have two copies of printf_stderr() with the comment, "Ideally this should be shared". This moves the function to a new exported header which can be the basis for other similar debugging utility functions. To include it, #include "mozilla/glue/Debug.h" A small concern with this is that printf_stderr() is in the global namespace, and could conflict if it's inadvertently included along with, for example, nsDebug.h which also defines this function. The warning in the comment at the top of the file attempts to mitigate this. Differential Revision: https://phabricator.services.mozilla.com/D13196
browser/app/winlauncher/LauncherProcessWin.cpp
mozglue/build/WindowsDllBlocklist.cpp
mozglue/misc/Debug.h
mozglue/misc/moz.build
--- a/browser/app/winlauncher/LauncherProcessWin.cpp
+++ b/browser/app/winlauncher/LauncherProcessWin.cpp
@@ -1,27 +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/glue/Debug.h"
 #include "mozilla/LauncherResult.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>
 
@@ -101,38 +100,16 @@ static mozilla::LauncherFlags ProcessCmd
           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)) {
--- a/mozglue/build/WindowsDllBlocklist.cpp
+++ b/mozglue/build/WindowsDllBlocklist.cpp
@@ -12,41 +12,40 @@
 #  define MALLOC_DECL(name, return_type, ...) \
     MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
 #  include "malloc_decls.h"
 #  include "mozilla/mozalloc.h"
 #endif
 
 #include <windows.h>
 #include <winternl.h>
-#include <io.h>
 
 #pragma warning(push)
 #pragma warning(disable : 4275 4530)  // See msvc-stl-wrapper.template.h
 #include <map>
 #pragma warning(pop)
 
 #include "Authenticode.h"
 #include "CrashAnnotations.h"
 #include "MozglueUtils.h"
 #include "UntrustedDllsHandler.h"
 #include "nsAutoPtr.h"
 #include "nsWindowsDllInterceptor.h"
 #include "mozilla/CmdLineAndEnvUtils.h"
 #include "mozilla/DebugOnly.h"
 #include "mozilla/ScopeExit.h"
-#include "mozilla/Sprintf.h"
 #include "mozilla/StackWalk_windows.h"
 #include "mozilla/TimeStamp.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/Vector.h"
 #include "mozilla/WindowsVersion.h"
 #include "nsWindowsHelpers.h"
 #include "WindowsDllBlocklist.h"
 #include "mozilla/AutoProfilerLabel.h"
+#include "mozilla/glue/Debug.h"
 #include "mozilla/glue/WindowsDllServices.h"
 
 using namespace mozilla;
 
 using CrashReporter::Annotation;
 using CrashReporter::AnnotationToString;
 
 static glue::Win32SRWLock gDllServicesLock;
@@ -59,38 +58,16 @@ static glue::detail::DllServicesBase* gD
 // define this for very verbose dll load debug spew
 #undef DEBUG_very_verbose
 
 static uint32_t sInitFlags;
 static bool sBlocklistInitAttempted;
 static bool sBlocklistInitFailed;
 static bool sUser32BeforeBlocklist;
 
-// Duplicated from xpcom glue. Ideally this should be shared.
-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);
-}
-
 // This feature is enabled only on NIGHTLY, only for the main process.
 inline static bool IsUntrustedDllsHandlerEnabled() {
 #ifdef NIGHTLY_BUILD
   return !(sInitFlags & eDllBlocklistInitFlagIsChildProcess);
 #else
   return false;
 #endif
 }
new file mode 100644
--- /dev/null
+++ b/mozglue/misc/Debug.h
@@ -0,0 +1,63 @@
+/* -*- 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/. */
+
+#ifndef mozilla_glue_Debug_h
+#define mozilla_glue_Debug_h
+
+/* This header file intends to supply debugging utilities for use in code
+ * that cannot use XPCOM debugging facilities like nsDebug.h.
+ * e.g. mozglue, browser/app
+ *
+ * NB: printf_stderr() is in the global namespace, so include this file with
+ * care; avoid including from header files.
+ */
+
+#include <io.h>
+#if defined(XP_WIN)
+#include <windows.h>
+#endif  // defined(XP_WIN)
+#include "mozilla/Attributes.h"
+#include "mozilla/Sprintf.h"
+
+#if defined(MOZILLA_INTERNAL_API)
+#error Do not include this file from XUL sources.
+#endif
+
+// Though this is a separate implementation than nsDebug's, we want to make the
+// declarations compatible to avoid confusing the linker if both headers are
+// included.
+#ifdef __cplusplus
+extern "C" {
+#endif  // __cplusplus
+
+inline void printf_stderr(const char *fmt, ...) MOZ_FORMAT_PRINTF(1, 2) {
+#if defined(XP_WIN)
+  if (IsDebuggerPresent()) {
+    char buf[2048];
+    va_list args;
+    va_start(args, fmt);
+    VsprintfLiteral(buf, fmt, args);
+    va_end(args);
+    OutputDebugStringA(buf);
+  }
+#endif  // defined(XP_WIN)
+
+  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);
+}
+
+#ifdef __cplusplus
+}
+#endif  // __cplusplus
+
+#endif  // mozilla_glue_Debug_h
--- a/mozglue/misc/moz.build
+++ b/mozglue/misc/moz.build
@@ -10,16 +10,20 @@ EXPORTS.mozilla += [
     'AutoProfilerLabel.h',
     'PlatformConditionVariable.h',
     'PlatformMutex.h',
     'Printf.h',
     'StackWalk.h',
     'TimeStamp.h',
 ]
 
+EXPORTS.mozilla.glue += [
+    'Debug.h',
+]
+
 if CONFIG['OS_ARCH'] == 'WINNT':
     EXPORTS.mozilla += [
         'StackWalk_windows.h',
         'TimeStamp_windows.h',
     ]
 
 SOURCES += [
     'AutoProfilerLabel.cpp',