test-runner: Implement is_debugger_attached() for FreeBSD

FreeBSD provides a PROC_TRACE_STATUS procctl(2) to detect this directly.

Signed-off-by: Alex Richardson <Alexander.Richardson@cl.cam.ac.uk>
This commit is contained in:
Alex Richardson 2021-03-15 22:37:49 +00:00 committed by Alexander Richardson
parent 644efe9517
commit bb92828807
2 changed files with 23 additions and 1 deletions

View file

@ -22,6 +22,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "../config.h"
#define _GNU_SOURCE
@ -36,11 +37,16 @@
#include <dlfcn.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <sys/ptrace.h>
#ifdef HAVE_SYS_PROCCTL_H
#include <sys/procctl.h>
#elif defined(HAVE_SYS_PRCTL_H)
#include <sys/prctl.h>
#ifndef PR_SET_PTRACER
# define PR_SET_PTRACER 0x59616d61
#endif
#endif
#include "test-runner.h"
@ -226,6 +232,21 @@ stderr_reset_color(void)
* Returns: 1 if a debugger is confirmed present; 0 if no debugger is
* present or if it can't be determined.
*/
#if defined(HAVE_SYS_PROCCTL_H) && defined(PROC_TRACE_STATUS)
static int
is_debugger_attached(void)
{
int rc;
int status;
rc = procctl(P_PID, getpid(), PROC_TRACE_STATUS, &status);
if (rc == -1) {
perror("procctl");
return 0;
}
/* -1=tracing disabled, 0=no debugger attached, >0=pid of debugger. */
return status > 0;
}
#elif defined(HAVE_SYS_PRCTL_H)
static int
is_debugger_attached(void)
{
@ -287,6 +308,7 @@ is_debugger_attached(void)
return rc;
}
#endif
int main(int argc, char *argv[])
{