~skeeto/public-inbox

Re: Assertions should be more debugger-oriented

Alexander Shpilkin <ashpilkin@gmail.com>
Details
Message ID
<CAAiuFs8dO2yUzfvKtftrQF5ZsZZ79RnPJekAYnroVCnnMgvmRQ@mail.gmail.com>
DKIM signature
missing
Download raw message
How bad of an idea would it be to modify assert() to behave differently
depending on whether a debugger is present?  The Unix equivalent of
IsDebuggerPresent() seems to be unpleasantly involved, so I'm not sure.

Demo for linux-gnu:

---8<--- cut here ---8<---
#define _XOPEN_SOURCE 500

#include <assert.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>

int __notrace(void) {
	/* FIXME EINTR? */
	pid_t pid; int st;
	/* FIXME will invoke pthread_atfork() handlers */
	if ((pid = fork()) == -1)
		return -1; /* dunno, assume not */
	if (!pid) {
		int ppid = getppid();
		if (ptrace(PTRACE_ATTACH, ppid, 0, 0) < 0)
			_exit(0);
		waitpid(ppid, 0, 0);
		ptrace(PTRACE_CONT, ppid, 0, 0);
		ptrace(PTRACE_DETACH, ppid, 0, 0);
		_exit(1);
	}
	waitpid(pid, &st, 0);
	return WEXITSTATUS(st);
}

#ifndef NDEBUG
#undef assert
#define assert(E) \
	((E) ? (void)0 : \
	 /* FIXME GCC doesn't have a builtin for SIGTRAP */ \
	 (__notrace() ? (void)0 : __builtin_trap()), \
	 /* LSB 5.0 section 14.4.3 */ \
	 __assert_fail(#E, __FILE__, __LINE__, __func__))
#endif

int main(void) {
	assert(!"will break when debugging"); return 0;
}
---8<--- cut here ---8<---

-- 
Cheers,
Alex
Reply to thread Export thread (mbox)