michael@0: // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // This is a cross platform interface for helper functions related to debuggers. michael@0: // You should use this to test if you're running under a debugger, and if you michael@0: // would like to yield (breakpoint) into the debugger. michael@0: michael@0: #ifndef BASE_DEBUG_UTIL_H_ michael@0: #define BASE_DEBUG_UTIL_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: // A stacktrace can be helpful in debugging. For example, you can include a michael@0: // stacktrace member in a object (probably around #ifndef NDEBUG) so that you michael@0: // can later see where the given object was created from. michael@0: class StackTrace { michael@0: public: michael@0: // Create a stacktrace from the current location michael@0: StackTrace(); michael@0: // Get an array of instruction pointer values. michael@0: // count: (output) the number of elements in the returned array michael@0: const void *const *Addresses(size_t* count); michael@0: // Print a backtrace to stderr michael@0: void PrintBacktrace(); michael@0: michael@0: // Resolve backtrace to symbols and write to stream. michael@0: void OutputToStream(std::ostream* os); michael@0: michael@0: private: michael@0: std::vector trace_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(StackTrace); michael@0: }; michael@0: michael@0: class DebugUtil { michael@0: public: michael@0: // Starts the registered system-wide JIT debugger to attach it to specified michael@0: // process. michael@0: static bool SpawnDebuggerOnProcess(unsigned process_id); michael@0: michael@0: // Waits wait_seconds seconds for a debugger to attach to the current process. michael@0: // When silent is false, an exception is thrown when a debugger is detected. michael@0: static bool WaitForDebugger(int wait_seconds, bool silent); michael@0: michael@0: // Are we running under a debugger? michael@0: // On OS X, the underlying mechanism doesn't work when the sandbox is enabled. michael@0: // To get around this, this function caches its value. michael@0: // WARNING: Because of this, on OS X, a call MUST be made to this function michael@0: // BEFORE the sandbox is enabled. michael@0: static bool BeingDebugged(); michael@0: michael@0: // Break into the debugger, assumes a debugger is present. michael@0: static void BreakDebugger(); michael@0: michael@0: #if defined(OS_MACOSX) michael@0: // On OS X, it can take a really long time for the OS Crash handler to michael@0: // process a Chrome crash. This translates into a long wait till the process michael@0: // actually dies. michael@0: // This method disables OS Crash reporting entireley. michael@0: // TODO(playmobil): Remove this when we have Breakpad integration enabled - michael@0: // see http://crbug.com/7652 michael@0: static void DisableOSCrashDumps(); michael@0: #endif // defined(OS_MACOSX) michael@0: }; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class EnvironmentLog michael@0: { michael@0: public: michael@0: EnvironmentLog(const char* varname); michael@0: ~EnvironmentLog(); michael@0: michael@0: void print(const char* format, ...); michael@0: michael@0: private: michael@0: std::string fname_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(EnvironmentLog); michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // BASE_DEBUG_UTIL_H_