Move DumpProcessWithoutCrash to base, so we can use it from net and content

BUG=none
[email protected]
[email protected]

Review URL: https://codereview.chromium.org/99523009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@241589 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/base.gypi b/base/base.gypi
index 11edab0..60ff4f2 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -137,6 +137,8 @@
           'debug/debugger.h',
           'debug/debugger_posix.cc',
           'debug/debugger_win.cc',
+          'debug/dump_without_crashing.cc',
+          'debug/dump_without_crashing.h',
           # This file depends on files from the 'allocator' target,
           # but this target does not depend on 'allocator' (see
           # allocator.gyp for details).
diff --git a/base/compiler_specific.h b/base/compiler_specific.h
index dc4b233..09b4e703 100644
--- a/base/compiler_specific.h
+++ b/base/compiler_specific.h
@@ -211,4 +211,13 @@
 #define MSAN_UNPOISON(p, s)
 #endif  // MEMORY_SANITIZER
 
+// Macro useful for writing cross-platform function pointers.
+#if !defined(CDECL)
+#if defined(OS_WIN)
+#define CDECL __cdecl
+#else  // defined(OS_WIN)
+#define CDECL
+#endif  // defined(OS_WIN)
+#endif  // !defined(CDECL)
+
 #endif  // BASE_COMPILER_SPECIFIC_H_
diff --git a/base/debug/dump_without_crashing.cc b/base/debug/dump_without_crashing.cc
new file mode 100644
index 0000000..47fd873c
--- /dev/null
+++ b/base/debug/dump_without_crashing.cc
@@ -0,0 +1,32 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/debug/dump_without_crashing.h"
+
+#include "base/logging.h"
+
+namespace {
+
+// Pointer to the function that's called by DumpWithoutCrashing() to dump the
+// process's memory.
+void (CDECL *dump_without_crashing_function_)() = NULL;
+
+}  // namespace
+
+namespace base {
+
+namespace debug {
+
+void DumpWithoutCrashing() {
+  if (dump_without_crashing_function_)
+    (*dump_without_crashing_function_)();
+}
+
+void SetDumpWithoutCrashingFunction(void (CDECL *function)()) {
+  dump_without_crashing_function_ = function;
+}
+
+}  // namespace debug
+
+}  // namespace base
diff --git a/base/debug/dump_without_crashing.h b/base/debug/dump_without_crashing.h
new file mode 100644
index 0000000..c46f446e
--- /dev/null
+++ b/base/debug/dump_without_crashing.h
@@ -0,0 +1,27 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_DEBUG_DUMP_WITHOUT_CRASHING_H_
+#define BASE_DEBUG_DUMP_WITHOUT_CRASHING_H_
+
+#include "base/base_export.h"
+#include "base/compiler_specific.h"
+#include "build/build_config.h"
+
+namespace base {
+
+namespace debug {
+
+// Handler to silently dump the current process without crashing.
+BASE_EXPORT void DumpWithoutCrashing();
+
+// Sets a function that'll be invoked to dump the current process when
+// DumpWithoutCrashing() is called.
+BASE_EXPORT void SetDumpWithoutCrashingFunction(void (CDECL *function)());
+
+}  // namespace debug
+
+}  // namespace base
+
+#endif  // BASE_DEBUG_DUMP_WITHOUT_CRASHING_H_
diff --git a/base/native_library.h b/base/native_library.h
index 891f35b..9353b1ff 100644
--- a/base/native_library.h
+++ b/base/native_library.h
@@ -9,6 +9,7 @@
 // a loadable module.
 
 #include "base/base_export.h"
+#include "base/compiler_specific.h"
 #include "build/build_config.h"
 
 #if defined(OS_WIN)
@@ -19,13 +20,6 @@
 
 #include "base/strings/string16.h"
 
-// Macro useful for writing cross-platform function pointers.
-#if defined(OS_WIN) && !defined(CDECL)
-#define CDECL __cdecl
-#else
-#define CDECL
-#endif
-
 namespace base {
 
 class FilePath;
diff --git a/chrome/app/chrome_breakpad_client.cc b/chrome/app/chrome_breakpad_client.cc
index 84a6977..267584b 100644
--- a/chrome/app/chrome_breakpad_client.cc
+++ b/chrome/app/chrome_breakpad_client.cc
@@ -38,7 +38,7 @@
 #endif
 
 #if defined(OS_POSIX)
-#include "chrome/common/dump_without_crashing.h"
+#include "base/debug/dump_without_crashing.h"
 #endif
 
 #if defined(OS_ANDROID)
@@ -326,12 +326,6 @@
   return PathService::Get(chrome::DIR_CRASH_DUMPS, crash_dir);
 }
 
-#if defined(OS_POSIX)
-void ChromeBreakpadClient::SetDumpWithoutCrashingFunction(void (*function)()) {
-  logging::SetDumpWithoutCrashingFunction(function);
-}
-#endif
-
 size_t ChromeBreakpadClient::RegisterCrashKeys() {
   // Note: This is not called on Windows because Breakpad is initialized in the
   // EXE module, but code that uses crash keys is in the DLL module.
diff --git a/chrome/app/chrome_breakpad_client.h b/chrome/app/chrome_breakpad_client.h
index 7e79a092..489e8fe 100644
--- a/chrome/app/chrome_breakpad_client.h
+++ b/chrome/app/chrome_breakpad_client.h
@@ -46,10 +46,6 @@
 
   virtual bool GetCrashDumpLocation(base::FilePath* crash_dir) OVERRIDE;
 
-#if defined(OS_POSIX)
-  virtual void SetDumpWithoutCrashingFunction(void (*function)()) OVERRIDE;
-#endif
-
   virtual size_t RegisterCrashKeys() OVERRIDE;
 
   virtual bool IsRunningUnattended() OVERRIDE;
diff --git a/chrome/browser/history/thumbnail_database.cc b/chrome/browser/history/thumbnail_database.cc
index ce9cc06..654a5535 100644
--- a/chrome/browser/history/thumbnail_database.cc
+++ b/chrome/browser/history/thumbnail_database.cc
@@ -9,6 +9,7 @@
 
 #include "base/bind.h"
 #include "base/debug/alias.h"
+#include "base/debug/dump_without_crashing.h"
 #include "base/file_util.h"
 #include "base/format_macros.h"
 #include "base/memory/ref_counted_memory.h"
@@ -19,7 +20,6 @@
 #include "base/time/time.h"
 #include "chrome/browser/history/url_database.h"
 #include "chrome/common/chrome_version_info.h"
-#include "chrome/common/dump_without_crashing.h"
 #include "sql/recovery.h"
 #include "sql/statement.h"
 #include "sql/transaction.h"
@@ -121,7 +121,7 @@
   base::strlcpy(debug_buf, debug_info.c_str(), arraysize(debug_buf));
   base::debug::Alias(&debug_buf);
 
-  logging::DumpWithoutCrashing();
+  base::debug::DumpWithoutCrashing();
 }
 
 void ReportCorrupt(sql::Connection* db, size_t startup_kb) {
diff --git a/chrome/browser/metrics/thread_watcher.cc b/chrome/browser/metrics/thread_watcher.cc
index 66dbbd6..74b3df8 100644
--- a/chrome/browser/metrics/thread_watcher.cc
+++ b/chrome/browser/metrics/thread_watcher.cc
@@ -9,6 +9,7 @@
 #include "base/bind.h"
 #include "base/compiler_specific.h"
 #include "base/debug/alias.h"
+#include "base/debug/dump_without_crashing.h"
 #include "base/lazy_instance.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_split.h"
@@ -19,7 +20,6 @@
 #include "chrome/browser/metrics/metrics_service.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/chrome_version_info.h"
-#include "chrome/common/dump_without_crashing.h"
 #include "chrome/common/logging_chrome.h"
 
 #if defined(OS_WIN)
@@ -49,7 +49,7 @@
 #ifndef NDEBUG
   *NullPointer() = line_number;  // Crash.
 #else
-  logging::DumpWithoutCrashing();
+  base::debug::DumpWithoutCrashing();
 #endif
 }
 
@@ -861,7 +861,7 @@
 #ifndef NDEBUG
     DCHECK(false);
 #else
-    logging::DumpWithoutCrashing();
+    base::debug::DumpWithoutCrashing();
 #endif
   }
 
diff --git a/chrome/browser/sync/glue/chrome_report_unrecoverable_error.cc b/chrome/browser/sync/glue/chrome_report_unrecoverable_error.cc
index 72b5c72c..9c7797b 100644
--- a/chrome/browser/sync/glue/chrome_report_unrecoverable_error.cc
+++ b/chrome/browser/sync/glue/chrome_report_unrecoverable_error.cc
@@ -4,10 +4,10 @@
 
 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
 
+#include "base/debug/dump_without_crashing.h"
 #include "base/rand_util.h"
 #include "chrome/common/chrome_constants.h"
 #include "chrome/common/chrome_version_info.h"
-#include "chrome/common/dump_without_crashing.h"
 
 namespace browser_sync {
 
@@ -27,7 +27,7 @@
   if (random_number > kErrorUploadRatio)
     return;
 
-  logging::DumpWithoutCrashing();
+  base::debug::DumpWithoutCrashing();
 }
 
 }  // namespace browser_sync
diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi
index b67a999..cb80a7a 100644
--- a/chrome/chrome_common.gypi
+++ b/chrome/chrome_common.gypi
@@ -112,8 +112,6 @@
         'common/custom_handlers/protocol_handler.cc',
         'common/custom_handlers/protocol_handler.h',
         'common/descriptors_android.h',
-        'common/dump_without_crashing.cc',
-        'common/dump_without_crashing.h',
         'common/encrypted_media_messages_android.h',
         'common/extensions/api/commands/commands_handler.cc',
         'common/extensions/api/commands/commands_handler.h',
diff --git a/chrome/common/dump_without_crashing.cc b/chrome/common/dump_without_crashing.cc
deleted file mode 100644
index 65c6494..0000000
--- a/chrome/common/dump_without_crashing.cc
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "build/build_config.h"
-
-#include "chrome/common/dump_without_crashing.h"
-
-#include "base/logging.h"
-#include "chrome/common/chrome_constants.h"
-
-#if defined(OS_WIN)
-#include <windows.h>
-#endif
-
-namespace {
-
-#if defined(OS_POSIX)
-// Pointer to the function that's called by DumpWithoutCrashing() to dump the
-// process's memory.
-void (*dump_without_crashing_function_)() = NULL;
-#endif
-
-}  // namespace
-
-namespace logging {
-
-void DumpWithoutCrashing() {
-#if defined(OS_WIN)
-  // Get the breakpad pointer from chrome.exe
-  typedef void (__cdecl *DumpProcessFunction)();
-  DumpProcessFunction DumpProcess = reinterpret_cast<DumpProcessFunction>(
-      ::GetProcAddress(::GetModuleHandle(chrome::kBrowserProcessExecutableName),
-                       "DumpProcessWithoutCrash"));
-  if (DumpProcess)
-    DumpProcess();
-#elif defined(OS_POSIX)
-  if (dump_without_crashing_function_)
-    (*dump_without_crashing_function_)();
-#else
-  NOTIMPLEMENTED();
-#endif
-}
-
-#if defined(OS_POSIX)
-void SetDumpWithoutCrashingFunction(void (*function)()) {
-  dump_without_crashing_function_ = function;
-}
-#endif
-
-}  // namespace logging
diff --git a/chrome/common/dump_without_crashing.h b/chrome/common/dump_without_crashing.h
deleted file mode 100644
index dd5299b8..0000000
--- a/chrome/common/dump_without_crashing.h
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_COMMON_DUMP_WITHOUT_CRASHING_H_
-#define CHROME_COMMON_DUMP_WITHOUT_CRASHING_H_
-
-#include "build/build_config.h"
-
-namespace logging {
-
-// Handler to silently dump the current process without crashing.
-void DumpWithoutCrashing();
-
-#if defined(OS_POSIX)
-// Sets a function that'll be invoked to dump the current process when
-// DumpWithoutCrashing() is called.
-void SetDumpWithoutCrashingFunction(void (*function)());
-#endif
-
-}  // namespace logging
-
-#endif  // CHROME_COMMON_DUMP_WITHOUT_CRASHING_H_
diff --git a/chrome/common/logging_chrome.cc b/chrome/common/logging_chrome.cc
index 81d65723..59019c5f 100644
--- a/chrome/common/logging_chrome.cc
+++ b/chrome/common/logging_chrome.cc
@@ -33,6 +33,7 @@
 #include "base/command_line.h"
 #include "base/compiler_specific.h"
 #include "base/debug/debugger.h"
+#include "base/debug/dump_without_crashing.h"
 #include "base/environment.h"
 #include "base/file_util.h"
 #include "base/files/file_path.h"
@@ -47,7 +48,6 @@
 #include "chrome/common/chrome_constants.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/chrome_switches.h"
-#include "chrome/common/dump_without_crashing.h"
 #include "chrome/common/env_vars.h"
 #include "ipc/ipc_logging.h"
 
@@ -96,7 +96,7 @@
 // Handler to silently dump the current process when there is an assert in
 // chrome.
 void DumpProcessAssertHandler(const std::string& str) {
-  logging::DumpWithoutCrashing();
+  base::debug::DumpWithoutCrashing();
 }
 #endif  // OS_WIN
 MSVC_ENABLE_OPTIMIZE();
diff --git a/components/breakpad/app/breakpad_client.cc b/components/breakpad/app/breakpad_client.cc
index e9bbe87..84931eac 100644
--- a/components/breakpad/app/breakpad_client.cc
+++ b/components/breakpad/app/breakpad_client.cc
@@ -90,11 +90,6 @@
   return false;
 }
 
-#if defined(OS_POSIX)
-void BreakpadClient::SetDumpWithoutCrashingFunction(void (*function)()) {
-}
-#endif
-
 size_t BreakpadClient::RegisterCrashKeys() {
   return 0;
 }
diff --git a/components/breakpad/app/breakpad_client.h b/components/breakpad/app/breakpad_client.h
index 09a5047..fb3b205 100644
--- a/components/breakpad/app/breakpad_client.h
+++ b/components/breakpad/app/breakpad_client.h
@@ -106,12 +106,6 @@
   // |crash_dir| was set.
   virtual bool GetCrashDumpLocation(base::FilePath* crash_dir);
 
-#if defined(OS_POSIX)
-  // Sets a function that'll be invoked to dump the current process when
-  // without crashing.
-  virtual void SetDumpWithoutCrashingFunction(void (*function)());
-#endif
-
   // Register all of the potential crash keys that can be sent to the crash
   // reporting server. Returns the size of the union of all keys.
   virtual size_t RegisterCrashKeys();
diff --git a/components/breakpad/app/breakpad_linux.cc b/components/breakpad/app/breakpad_linux.cc
index cc220f1..098c3709 100644
--- a/components/breakpad/app/breakpad_linux.cc
+++ b/components/breakpad/app/breakpad_linux.cc
@@ -25,6 +25,7 @@
 #include "base/base_switches.h"
 #include "base/command_line.h"
 #include "base/debug/crash_logging.h"
+#include "base/debug/dump_without_crashing.h"
 #include "base/files/file_path.h"
 #include "base/linux_util.h"
 #include "base/path_service.h"
@@ -1486,7 +1487,7 @@
   SetProcessStartTime();
   g_pid = getpid();
 
-  GetBreakpadClient()->SetDumpWithoutCrashingFunction(&DumpProcess);
+  base::debug::SetDumpWithoutCrashingFunction(&DumpProcess);
 #if defined(ADDRESS_SANITIZER)
   // Register the callback for AddressSanitizer error reporting.
   __asan_set_error_report_callback(AsanLinuxBreakpadCallback);
diff --git a/components/breakpad/app/breakpad_mac.mm b/components/breakpad/app/breakpad_mac.mm
index b6cac42..d46b0c4a 100644
--- a/components/breakpad/app/breakpad_mac.mm
+++ b/components/breakpad/app/breakpad_mac.mm
@@ -12,6 +12,7 @@
 #import "base/basictypes.h"
 #include "base/command_line.h"
 #include "base/debug/crash_logging.h"
+#include "base/debug/dump_without_crashing.h"
 #include "base/file_util.h"
 #include "base/files/file_path.h"
 #import "base/logging.h"
@@ -243,8 +244,7 @@
   }
 
   logging::SetLogMessageHandler(&FatalMessageHandler);
-  GetBreakpadClient()->SetDumpWithoutCrashingFunction(
-      &DumpHelper::DumpWithoutCrashing);
+  base::debug::SetDumpWithoutCrashingFunction(&DumpHelper::DumpWithoutCrashing);
 
   // abort() sends SIGABRT, which breakpad does not intercept.
   // Register a signal handler to crash in a way breakpad will
diff --git a/components/breakpad/app/breakpad_win.cc b/components/breakpad/app/breakpad_win.cc
index af1f136..02daab6 100644
--- a/components/breakpad/app/breakpad_win.cc
+++ b/components/breakpad/app/breakpad_win.cc
@@ -17,6 +17,7 @@
 #include "base/basictypes.h"
 #include "base/command_line.h"
 #include "base/debug/crash_logging.h"
+#include "base/debug/dump_without_crashing.h"
 #include "base/environment.h"
 #include "base/memory/scoped_ptr.h"
 #include "base/strings/string16.h"
@@ -745,6 +746,8 @@
       google_breakpad::ExceptionHandler::HANDLER_NONE,
       dump_type, pipe_name.c_str(), custom_info);
 
+  base::debug::SetDumpWithoutCrashingFunction(&DumpProcessWithoutCrash);
+
   if (g_breakpad->IsOutOfProcess()) {
     // Tells breakpad to handle breakpoint and single step exceptions.
     // This might break JIT debuggers, but at least it will always
diff --git a/remoting/client/plugin/pepper_plugin_thread_delegate.h b/remoting/client/plugin/pepper_plugin_thread_delegate.h
index 59ea3e4..114a460 100644
--- a/remoting/client/plugin/pepper_plugin_thread_delegate.h
+++ b/remoting/client/plugin/pepper_plugin_thread_delegate.h
@@ -7,13 +7,6 @@
 
 #include "remoting/base/plugin_thread_task_runner.h"
 
-// Macro useful for writing cross-platform function pointers.
-#if defined(OS_WIN) && !defined(CDECL)
-#define CDECL __cdecl
-#else
-#define CDECL
-#endif
-
 namespace pp {
 class Core;
 }  // namespace pp