From 44a2441193aa1b4fffb0687a0d00a71ee14a5a8a Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Mon, 22 Apr 2024 09:07:22 +0200 Subject: [PATCH 1/2] Fix debug Linux --- src/std/debug.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/std/debug.c b/src/std/debug.c index c41cfdf78..a3a46120d 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,21 @@ 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); *thread = ret; + if( ret == -1 && errno != EINTR ) + return 3; + 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; From 20c19bba55d04da4d5ac829ee6e16091fa105602 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Thu, 2 May 2024 09:04:36 +0200 Subject: [PATCH 2/2] Fix detect on exit --- src/std/debug.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/std/debug.c b/src/std/debug.c index a3a46120d..3f8dcf5b4 100644 --- a/src/std/debug.c +++ b/src/std/debug.c @@ -256,9 +256,11 @@ HL_API int hl_debug_wait( int pid, int *thread, int timeout ) { // 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 == -1 && errno != EINTR ) - return 3; if( ret <= 0 ) return -1; if( WIFEXITED(status) )