Implement IsDebuggerPresent() and DebugBreak() in OS X. This uses an unstable api so it will only be in debug builds.

This commit is contained in:
Steve Checkoway
2006-06-16 07:18:39 +00:00
parent 87e571a528
commit aef9e00016
3 changed files with 44 additions and 1 deletions
+37
View File
@@ -2,6 +2,9 @@
#include "Crash.h" #include "Crash.h"
#include "ProductInfo.h" #include "ProductInfo.h"
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
RString CrashHandler::GetLogsDirectory() RString CrashHandler::GetLogsDirectory()
{ {
@@ -70,6 +73,40 @@ void CrashHandler::InformUserOfCrash( const RString& sPath )
CFRelease( sAlternate ); CFRelease( sAlternate );
} }
/* IMPORTANT: Because the definition of the kinfo_proc structure (in <sys/sysctl.h>)
* is conditionalized by __APPLE_API_UNSTABLE, you should restrict use of the [below]
* code to the debug build of your program.
* http://developer.apple.com/qa/qa2004/qa1361.html */
bool CrashHandler::IsDebuggerPresent()
{
#ifdef DEBUG
int ret;
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
struct kinfo_proc info;
size_t size;
// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.
info.kp_proc.p_flag = 0;
// Call sysctl.
size = sizeof(info);
ret = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
// We're being debugged if the P_TRACED flag is set.
return ret == 0 && (info.kp_proc.p_flag & P_TRACED) != 0;
#else
return false;
#endif
}
void CrashHandler::DebugBreak()
{
DebugStr( "\pDebugBreak()" );
}
/* /*
* (c) 2003-2006 Steve Checkoway * (c) 2003-2006 Steve Checkoway
* All rights reserved. * All rights reserved.
+2
View File
@@ -5,6 +5,8 @@ namespace CrashHandler
{ {
RString GetLogsDirectory(); RString GetLogsDirectory();
void InformUserOfCrash( const RString& sPath ); void InformUserOfCrash( const RString& sPath );
bool IsDebuggerPresent();
void DebugBreak();
} }
#endif /* DARWIN_CRASH_H */ #endif /* DARWIN_CRASH_H */
+5 -1
View File
@@ -6,6 +6,10 @@
# include "windows.h" # include "windows.h"
# include "archutils/Win32/Crash.h" # include "archutils/Win32/Crash.h"
# endif # endif
#elif defined(MACOSX)
# include "archutils/Darwin/Crash.h"
using CrashHandler::IsDebuggerPresent;
using CrashHandler::DebugBreak;
#elif defined(_XBOX) #elif defined(_XBOX)
#else #else
# include <unistd.h> # include <unistd.h>
@@ -17,7 +21,7 @@
void NORETURN sm_crash( const char *reason ) void NORETURN sm_crash( const char *reason )
{ {
#if defined(_WINDOWS) && defined(CRASH_HANDLER) #if ( defined(_WINDOWS) && defined(CRASH_HANDLER) ) || defined(MACOSX)
/* If we're being debugged, throw a debug break so it'll suspend the process. */ /* If we're being debugged, throw a debug break so it'll suspend the process. */
if( IsDebuggerPresent() ) if( IsDebuggerPresent() )
{ {