Skip to contentSkip to conversation

ptrace on stopped processes (2.4)

PATCH Rajesh Rajamani 3 participants 7 messages Mar 17, 2003 – Mar 25, 2003 ptrace support

lore
Share & tools
lore
#0 lore raw

Hi All,
I'm working on adding a function

void debugbreak(void);

to glibc. I got the inspiration for this from Windows. Windows has a DebugBreak() function, which when invoked from a process, spawns a debugger, which then attaches to the process. I think this would be invaluable to linux developers in situations where they currently have to put an infinite loop and attach to a running process (say, to stop in a library that has been LD_PRELOADed).

To this end, I've been experimenting and found out that gdb can't attach to a process that has been stopped. I'd like to send a SIGSTOP as soon as debugbreak() is invoked, so that all threads are stopped in a state close to the one they were in, when the debugbreak() was invoked.

I spoke to Vic Zandy about this and he informed me that he had submitted a patch
that would allow ptrace to attach to stopped processes also (the thread of
discussion is pasted below). I believe the patch was not accepted at that time.
I was wondering what the official line on this is? If there are no serious
objections, will the community consider accepting the patch? It would go a long way in helping me accomplish my goal.

Thank you,
Raj

PS - I am not subscribed to the kernel mailing list. Would appreciate your cc-ing your reply to me.
-------------------------------------------------------------------------------
Vic Zandy wrote --

This is to respond to feedback for the ptrace patch I sent toward the end of december. The original message is below.

From: Alan Cox <[email protected]>

Quoted 2 lines
Quoted 1 line

+ if (signr == SIGSTOP && current->ptrace & PT_PTRACED)

This does not I suspect do what you think - surely you want brackets ?

I agree the second term should be wrapped in parens (it is now in the patch below); but isn't that logically equivalent to what I had?

From: Mike Coleman <[email protected]>:

Quoted 2 lines

Also, is this something that used to work? Or would this be a change in the semantics of ptrace?

This is a change of semantics at least going back to 2.2.

Quoted 10 lines
Quoted 3 lines

Another bug is that it is not possible to use PTRACE_DETACH to leave a process stopped, because ptrace ignores SIGSTOPs sent by the tracing process.

Unless I'm missing something (frequently the case), there are two cases here:
(1) the tracer wants to leave the tracee stopped, and (2) the tracer wants the process to continue running in as natural a way as possible, meaning without sending it a SIGCONT (which can cause the SIGCONT signal handler to execute). As things currently stand, we have behavior (2), and (1) is not possible. With your change, we'd have behavior (1), and (2) would not be possible.

I agree that the ability to do (2) should be preserved, but I don't see how this patch breaks it; do you have an example?

From: OGAWA Hirofumi <[email protected]>:

Quoted 14 lines
Quoted 14 lines
kernel/ptrace.c Fri Dec 21 10:42:44 2001 renamed from kernel/ptrace.c Wed Nov 21 16:43:01 2001 +4 -2
@@ -89,8 +89,10 @@
   SET_LINKS(task);
  }
  write_unlock_irq(&tasklist_lock);
-
- send_sig(SIGSTOP, task, 1);
+ if (task->state != TASK_STOPPED)
+  send_sig(SIGSTOP, task, 1);
+ else
+  task->exit_code = SIGSTOP;
  return 0;
 
Quoted 4 lines

kernel/ptrace.c Fri Dec 21 10:42:44 2001 line 98

Quoted 1 line
kernel/ptrace.c Fri Dec 21 10:42:44 2001 renamed from kernel/ptrace.c Wed Nov 21 16:43:01 2001 +0 -0
 bad:

It seems that trace is started in the place different from usual. Then, I think PTRACE_KILL doesn't work.

I don't agree, it seems to work for me.

I'd still want to check uml and subterfuge, which I'll do after these points are cleared up.

Thanks,
Vic

From: vic <[email protected]>
Subject: [PATCH] ptrace on stopped processes (2.4)
To: [email protected]
Cc: [email protected], [email protected],
[email protected]
Date: Fri, 21 Dec 2001 13:53:32 -0600

This patch fixes a couple problems with ptrace's interaction with stopped processes on Linux 2.4.

The most significant bug is that gdb cannot attach to a stopped process. Specifically, the wait that follows the PTRACE_ATTACH will block indefinitely.

Another bug is that it is not possible to use PTRACE_DETACH to leave a process stopped, because ptrace ignores SIGSTOPs sent by the tracing process.

This patch is against 2.4.16 on x86. I have tested gdb and strace. After this patch is reviewed, I would be happy to submit an analogous patch for the other platforms, although I cannot test it.

Vic Zandy

arch/i386/kernel/signal.c Wed Jan 16 22:19:16 2002 renamed from arch/i386/kernel/signal.c Fri Sep 14 16:15:40 2001 +3 -3
@@ -620,9 +620,9 @@
                                continue;
                        current->exit_code = 0;
 
-                       /* The debugger continued.  Ignore SIGSTOP.  */
-                       if (signr == SIGSTOP)
-                               continue;
+                       /* The debugger continued. */
+                       if (signr == SIGSTOP && (current->ptrace & PT_PTRACED))
+                               continue; /* ignore SIGSTOP */
 
                        /* Update the siginfo structure.  Is this good?  */
                        if (signr != info.si_signo) {
kernel/ptrace.c Fri Dec 21 10:42:44 2001 renamed from kernel/ptrace.c Wed Nov 21 16:43:01 2001 +4 -2
@@ -89,8 +89,10 @@
                SET_LINKS(task);
        }
        write_unlock_irq(&tasklist_lock);
-
-       send_sig(SIGSTOP, task, 1);
+       if (task->state != TASK_STOPPED)
+               send_sig(SIGSTOP, task, 1);
+       else
+               task->exit_code = SIGSTOP;
        return 0;
 
 bad:

-

#1 lore raw
Quoted 17 lines · Rajesh Rajamani

Hi All,
I'm working on adding a function

void debugbreak(void);

to glibc. I got the inspiration for this from Windows. Windows has a DebugBreak() function, which when invoked from a process, spawns a debugger, which then attaches to the process. I think this would be invaluable to linux developers in situations where they currently have to put an infinite loop and attach to a running process (say, to stop in a library that has been LD_PRELOADed).

To this end, I've been experimenting and found out that gdb can't attach to a process that has been stopped. I'd like to send a SIGSTOP as soon as debugbreak() is invoked, so that all threads are stopped in a state close to the one they were in, when the debugbreak() was invoked.

Quoted 6 lines

I spoke to Vic Zandy about this and he informed me that he had submitted a patch
that would allow ptrace to attach to stopped processes also (the thread of
discussion is pasted below). I believe the patch was not accepted at that time.
I was wondering what the official line on this is? If there are no serious
objections, will the community consider accepting the patch? It would go a long way in helping me accomplish my goal.

The question is, what _should_ happen when yu attach to a stopped process? If the tracer receives the same one SIGSTOP that it normally would, then it will just resume the program as if it weren't stopped. Does that make sense or not?

As for Vic's patch, the SIGSTOP leaving processes stopped bit is already fixed in 2.5, I believe. We need to decide what should happen when attaching to a stopped process before that half can be considered.

(And the patch itself is wrong; send_sig is too broad a hammer for this, it should probably be something more like force_sig_specific; but I'm not sure that's the right approach. I'll think about it.)

Signature · 2 lines
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer
author
#2 lore raw
Quoted 29 lines · Daniel Jacobowitz
Quoted 17 lines · Rajesh Rajamani

Hi All,
I'm working on adding a function

void debugbreak(void);

to glibc. I got the inspiration for this from Windows. Windows has a DebugBreak() function, which when invoked from a process, spawns a debugger, which then attaches to the process. I think this would be invaluable to linux developers in situations where they currently have to put an infinite loop and attach to a running process (say, to stop in a library that has been LD_PRELOADed).

To this end, I've been experimenting and found out that gdb can't attach to a process that has been stopped. I'd like to send a SIGSTOP as soon as debugbreak() is invoked, so that all threads are stopped in a state close to the one they were in, when the debugbreak() was invoked.

Quoted 8 lines

I spoke to Vic Zandy about this and he informed me that he had submitted a patch that would allow ptrace to attach to stopped processes also (the thread of discussion is pasted below). I believe the patch was not accepted at that time.
I was wondering what the official line on this is? If there are no serious
objections, will the community consider accepting the patch? It would go a long way in helping me accomplish my goal.

Quoted 4 lines

The question is, what _should_ happen when yu attach to a stopped
process? If the tracer receives the same one SIGSTOP that it normally
would, then it will just resume the program as if it weren't stopped. Does that make sense or not?

Ideally, a stopped process should not be resumed, until it receives a SIGCONT. I ran two tests to understand the effect of this patch.

1. Attaching to a stopped process using gdb. This did not cause the tracee to continue.

2. stracing a stopped process. This caused the stopped process to continue. On further investigation, I found out that strace calls ptrace(PTRACE_SYSCALL), which restarts the child, but arranges for the child to be stopped at the next entry to or exit from a system call.

IMHO, the new ptrace semantics adds a feature to strace. If someone tries to run strace a stopped process without the patch, it will just hang, whereas with the patch, it'll let the process continue. Even without the patch, if a process that sends itself a SIGCONT is straced, the process will not stop. I do not think this is an issue, considering that ppl will not get any info by running strace on a stopped process, until they send a SIGCONT.

In conclusion, the action taken by the tracer depends on the mechanism it uses for monitoring the system calls. That said, it'll definitely be useful if the tracer could find out the state of the tracee at the time of attachment and take appropriate action. Given below, is a port of Vic's patch to 2.5.xx series -
--------------------------------------------------------------------------------
-- kernel/ptrace.c 2003-03-23 22:15:33.000000000 -0800
+++ kernel/ptrace.c 2003-03-23 22:19:27.000000000 -0800
@@ -115,7 +115,10 @@

       __ptrace_link(task, current);
       write_unlock_irq(&tasklist_lock);

- force_sig_specific(SIGSTOP, task);
+ if (task->state != TASK_STOPPED)
+ force_sig_specific(SIGSTOP, task);
+ else

+               task->exit_code = SIGSTOP;
       return 0;

bad:
--------------------------------------------------------------------
Thanks,
Raj

#3 lore raw
Quoted 40 lines · raj
Quoted 33 lines · Daniel Jacobowitz
Quoted 17 lines · Rajesh Rajamani

Hi All,
I'm working on adding a function

void debugbreak(void);

to glibc. I got the inspiration for this from Windows. Windows has a DebugBreak() function, which when invoked from a process, spawns a debugger, which then attaches to the process. I think this would be invaluable to linux developers in situations where they currently have to put an infinite loop and attach to a running process (say, to stop in a library that has been LD_PRELOADed).

To this end, I've been experimenting and found out that gdb can't attach to a process that has been stopped. I'd like to send a SIGSTOP as soon as debugbreak() is invoked, so that all threads are stopped in a state close to the one they were in, when the debugbreak() was invoked.

Quoted 12 lines

I spoke to Vic Zandy about this and he informed me that he had submitted a patch
that would allow ptrace to attach to stopped processes also (the thread of discussion is pasted below). I believe the patch was not accepted at that time.
I was wondering what the official line on this is? If there are no serious
objections, will the community consider accepting the patch? It would go a long
way in helping me accomplish my goal.

Quoted 5 lines

The question is, what _should_ happen when yu attach to a stopped process? If the tracer receives the same one SIGSTOP that it normally would, then it will just resume the program as if it weren't stopped. Does that make sense or not?

Quoted 5 lines

Ideally, a stopped process should not be resumed, until it receives a SIGCONT. I ran two tests to understand the effect of this patch.

1. Attaching to a stopped process using gdb. This did not cause the tracee to continue.

No, that's not what I meant. When you attach using GDB, there is no way for GDB to determine if the process was previously stopped or running.

Quoted 32 lines

2. stracing a stopped process. This caused the stopped process to continue. On further investigation, I found out that strace calls ptrace(PTRACE_SYSCALL), which restarts the child, but arranges for the child to be stopped at the next entry to or exit from a system call.

IMHO, the new ptrace semantics adds a feature to strace. If someone tries to run strace a stopped process without the patch, it will just hang, whereas with the patch, it'll let the process continue. Even without the patch, if a process that sends itself a SIGCONT is straced, the process will not stop. I do not think this is an issue, considering that ppl will not get any info by running strace on a stopped process, until they send a SIGCONT.

In conclusion, the action taken by the tracer depends on the mechanism it uses for monitoring the system calls. That said, it'll definitely be useful if the tracer could find out the state of the tracee at the time of attachment and take appropriate action. Given below, is a port of Vic's patch to 2.5.xx series -
--------------------------------------------------------------------------------
-- kernel/ptrace.c 2003-03-23 22:15:33.000000000 -0800
+++ kernel/ptrace.c 2003-03-23 22:19:27.000000000 -0800

kernel/ptrace.c Fri Dec 21 10:42:44 2001 renamed from kernel/ptrace.c Wed Nov 21 16:43:01 2001 +4 -1
@@ -115,7 +115,10 @@
       __ptrace_link(task, current);
       write_unlock_irq(&tasklist_lock);
 
-       force_sig_specific(SIGSTOP, task);
+       if (task->state != TASK_STOPPED)
+               force_sig_specific(SIGSTOP, task);
+       else
+               task->exit_code = SIGSTOP;
       return 0;
 
Quoted 1 line

bad:

I'll test this here, give me a day or two.

Signature · 2 lines
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer
#4 lore raw
Quoted 3 lines · Daniel Jacobowitz

No, that's not what I meant. When you attach using GDB, there is no way for GDB to determine if the process was previously stopped or running.

Likewise, there's a race condition with any other concurrent use of SIGSTOP.

Perhaps one could introduce a PTRACE_ATTACH2 that uses "addr" to indicate the signal that should be used to sychronize attaching. That way, programs that use STOP/CONT for their own purposes could be attached to with ptrace(PTRACE_ATTACH2,pid,SIGTRAP,0), or such.

If the process is already stopped, the debugger would be notified with WSTOPSIG set to that signal instead of SIGTRAP.

- Werner

Signature · 3 lines
  _________________________________________________________________________
 / Werner Almesberger, Buenos Aires, Argentina         [email protected] /
/_http://www.almesberger.net/____________________________________________/
#5 lore raw
Quoted 8 lines · Werner Almesberger
Quoted 3 lines · Daniel Jacobowitz

No, that's not what I meant. When you attach using GDB, there is no way for GDB to determine if the process was previously stopped or running.

Likewise, there's a race condition with any other concurrent use of SIGSTOP.

Quoted 7 lines

Perhaps one could introduce a PTRACE_ATTACH2 that uses "addr" to indicate the signal that should be used to sychronize attaching. That way, programs that use STOP/CONT for their own purposes could be attached to with ptrace(PTRACE_ATTACH2,pid,SIGTRAP,0), or such.

If the process is already stopped, the debugger would be notified with WSTOPSIG set to that signal instead of SIGTRAP.

Have you got an example that needs this? I'm not terribly concerned; GDB's handling of SIGSTOP has always been pretty bad. Strace is a bit better.

Signature · 2 lines
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer
#6 lore raw
Quoted 1 line · Daniel Jacobowitz

Have you got an example that needs this?

No, I was just suggesting an approach that may work, in case somebody wants to fix this. SIGSTOP/CONT doesn't seem like a very popular communication mechanism anyway.

I did notice that two processes trying to ptrace each other end up being unkillably deadlocked on 2.4.18, which a fix for the SIGSTOP problem may resolve, but I didn't check if this still happens in more recent kernels. ("Don't do it" is a satisfactory work-around.)

I'll worry about such subtleties once I've made heads and tails of how UML's ptrace-relay interacts with process termination :-)

- Werner

Signature · 3 lines
  _________________________________________________________________________
 / Werner Almesberger, Buenos Aires, Argentina         [email protected] /
/_http://www.almesberger.net/____________________________________________/