47 return IsDebuggerPresent ();
66#include <sys/sysctl.h>
78 struct kinfo_proc
info;
84 info.kp_proc.p_flag = 0;
91 mib[2] = KERN_PROC_PID;
97 sysctl (mib,
sizeof (mib) /
sizeof (*mib), &
info, &
size,
NULL, 0);
100 return ((
info.kp_proc.p_flag & P_TRACED) != 0);
113#define _WIN32_WINNT 0x0400
118#define vsnprintf _vsnprintf
119#define snprintf _snprintf
123#include <mach/mach_init.h>
124#include <mach/mach_time.h>
128#define THREAD_ALLOC_WATCH 0
130#if THREAD_ALLOC_WATCH
131mach_port_t watchThreadID = 0;
136AssertionHandler gAssertionHandler =
nullptr;
137AssertionHandler gPreAssertionHook =
nullptr;
138DebugPrintLogger gDebugPrintLogger =
nullptr;
141static const int kDebugPrintfBufferSize = 10000;
142static bool neverDebugger =
false;
145static void printDebugString (
const char*
string)
150 if (gDebugPrintLogger)
152 gDebugPrintLogger (
string);
156#if SMTG_OS_MACOS || defined(__MINGW32__)
157 fprintf (stderr,
"%s",
string);
159 OutputDebugStringA (
string);
167void FDebugPrint (
const char*
format, ...)
169 char string[kDebugPrintfBufferSize];
172 vsnprintf (
string, kDebugPrintfBufferSize,
format,
marker);
174 printDebugString (
string);
180void FDebugBreak (
const char*
format, ...)
182 char string[kDebugPrintfBufferSize];
185 vsnprintf (
string, kDebugPrintfBufferSize,
format,
marker);
187 printDebugString (
string);
191 if (gPreAssertionHook)
193 gPreAssertionHook (
string);
203 bool breakIntoDebugger =
true;
204 if (gAssertionHandler && gAssertionHandler (
string) ==
false)
206 breakIntoDebugger =
false;
209 if (breakIntoDebugger)
211#if SMTG_OS_WINDOWS && _MSC_VER
213#elif SMTG_OS_MACOS && __arm64__
216#elif __ppc64__ || __ppc__ || __arm__
217 kill (getpid (), SIGINT);
218#elif __i386__ || __x86_64__
220 __asm__
volatile (
"int3");
228void FPrintLastError (
const char*
file,
int line)
232 FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
233 GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
234 (
LPSTR)&lpMessageBuffer, 0,
nullptr);
235 FDebugPrint (
"%s(%d) : %s\n",
file, line, lpMessageBuffer);
236 LocalFree (lpMessageBuffer);
243 FDebugPrint (
"%s(%d) : Errno %d\n",
file, line, errno);
250void*
operator new (
size_t size,
int,
const char*
file,
int line)
252#if THREAD_ALLOC_WATCH
253 mach_port_t threadID = mach_thread_self ();
254 if (watchThreadID == threadID)
256 FDebugPrint (
"Watched Thread Allocation : %s (Line:%d)\n",
file ?
file :
"Unknown", line);
261 return ::operator
new (
size);
263 catch (std::bad_alloc exception)
265 FDebugPrint (
"bad_alloc exception : %s (Line:%d)",
file ?
file :
"Unknown", line);
271void*
operator new[] (
size_t size,
int,
const char*
file,
int line)
273#if THREAD_ALLOC_WATCH
274 mach_port_t threadID = mach_thread_self ();
275 if (watchThreadID == threadID)
277 FDebugPrint (
"Watched Thread Allocation : %s (Line:%d)\n",
file ?
file :
"Unknown", line);
282 return ::operator
new[] (
size);
284 catch (std::bad_alloc exception)
286 FDebugPrint (
"bad_alloc exception : %s (Line:%d)",
file ?
file :
"Unknown", line);
292void operator delete (
void*
p,
int,
const char*
file,
int line)
294 ::operator
delete (
p);
298void operator delete[] (
void*
p,
int,
const char*
file,
int line)
300 ::operator
delete[] (
p);
void setSmtgUnitTesting()
Definition fdebug.cpp:316
bool isSmtgUnitTesting()
Definition fdebug.cpp:310
static bool smtg_unit_testing_active
Definition fdebug.cpp:307
struct backing_store_struct * info
Definition jmemsys.h:183
int marker
Definition jpeglib.h:950
char * LPSTR
Definition swell-types.h:189
void * LPVOID
Definition swell-types.h:174
uch * p
Definition crypt.c:594
typedef int(UZ_EXP MsgFn)()
struct zdirent * file
Definition win32.c:1500
_WDL_CSTRING_PREFIX void INT_PTR const char * format
Definition wdlcstring.h:263