Skip to content

Commit

Permalink
Add support for debugger detection on Mac OS X
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
hovsater authored Jun 21, 2021
1 parent aadfc55 commit 1849c02
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ and its core depends only on few standard C library functions.
* Acutest installs a SEH filter to print out uncaught SEH exceptions.
* User can measure test execution times with `--time`.

**macOS specific features:**
* If a debugger is detected, the default execution of tests as child processes
is suppressed in order to make the debugging easier.

Any C/C++ module implementing one or more unit tests and including `acutest.h`,
can be built as a standalone program. We call the resulted binary as a "test
suite" for purposes of this document. The suite is then executed to run the
Expand Down
43 changes: 43 additions & 0 deletions include/acutest.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@
#include <io.h>
#endif

#if defined(__APPLE__)
#define ACUTEST_MACOS_
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
#endif

#ifdef __cplusplus
#include <exception>
#endif
Expand Down Expand Up @@ -1629,6 +1638,36 @@ acutest_is_tracer_present_(void)
}
#endif

#ifdef ACUTEST_MACOS_
static bool
acutest_AmIBeingDebugged(void)
{
int junk;
int mib[4];
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;

// Initialize mib, which tells sysctl the info we want, in this case
// we're looking for information about a specific process ID.
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();

// Call sysctl.
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);

// We're being debugged if the P_TRACED flag is set.
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}
#endif

int
main(int argc, char** argv)
{
Expand Down Expand Up @@ -1693,6 +1732,10 @@ main(int argc, char** argv)
if(acutest_is_tracer_present_())
acutest_no_exec_ = 1;
#endif
#ifdef ACUTEST_MACOS_
if(acutest_AmIBeingDebugged())
acutest_no_exec_ = 1;
#endif
#ifdef RUNNING_ON_VALGRIND
/* RUNNING_ON_VALGRIND is provided by optionally included <valgrind.h> */
if(RUNNING_ON_VALGRIND)
Expand Down

0 comments on commit 1849c02

Please sign in to comment.