Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix debug Linux #679

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/std/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# include <sys/wait.h>
# include <sys/user.h>
# include <signal.h>
# include <errno.h>
# define USE_PTRACE
#endif

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think SIGSTOP would be more appropriate here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But when I test with SIGSTOP, the disconnect does not works properly (maybe SIGSTOP is not actually stopping - the signal is forward to our thread? or has some delay / different behavior than SIGTRAP). :(

return ptrace(PTRACE_DETACH,pid,0,0) >= 0;
# else
return false;
Expand Down Expand Up @@ -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);
Comment on lines +253 to +258
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that waitpid with WNOHANG is also vulnerable to a race condition with libuv.

I'm not sure how to fix this other than by doing the same thread & semaphore dance as the Mac debugger.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also suspect the waitpid hack was for libuv (or vscode) waiting for all childs.

I tested your implementation with thread (it helps me a lot!), but it failed when we are actually waiting (by default the vscode extension call wait with a timeout = 0) and has a timeout. I didn't managed to get it works yet :'(
You said @ncannasse refused your PR due to complexity, so I tried this hack. I will do the thread & semaphore if hack is not a good option either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was another PR, I never actually submitted the thread & semaphore implementation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I misunderstood :(

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;
Expand Down