diff --git a/src/std/debug.c b/src/std/debug.c index c41cfdf78..3f8dcf5b4 100644 --- a/src/std/debug.c +++ b/src/std/debug.c @@ -25,6 +25,7 @@ # include # include # include +# include # define USE_PTRACE #endif @@ -84,6 +85,7 @@ HL_API bool hl_debug_stop( int pid ) { # elif defined(MAC_DEBUG) return mdbg_session_detach(pid); # elif defined(USE_PTRACE) + kill(pid, SIGTRAP); // DETACH needs ptrace-stop return ptrace(PTRACE_DETACH,pid,0,0) >= 0; # else return false; @@ -248,14 +250,23 @@ HL_API int hl_debug_wait( int pid, int *thread, int timeout ) { return mdbg_session_wait(pid, thread, timeout); # elif defined(USE_PTRACE) int status; - int ret = waitpid(pid,&status,0); - //printf("WAITPID=%X %X\n",ret,status); + // *** HACK *** + // usleep here is needed for a good result. + // Without it, waitpid can miss many stop event; + // With it, and more we wait, less we miss stop event. + usleep(100 * 1000); + int ret = waitpid(pid, &status, WNOHANG); + if( ret == -1 && errno == ECHILD ) { + *thread = pid; + return 0; + } *thread = ret; + if( ret <= 0 ) + return -1; if( WIFEXITED(status) ) return 0; if( WIFSTOPPED(status) ) { int sig = WSTOPSIG(status); - //printf(" STOPSIG=%d\n",sig); if( sig == SIGSTOP || sig == SIGTRAP ) return 1; return 3;