Skip to contentSkip to conversation

ptrace problem in 2.6.9

DISCUSSION Stephane Eranian 3 participants 5 messages Oct 22, 2004 – Oct 25, 2004 ptrace support

lore
Share & tools
lore
#0 lore raw

Hi Roland,

I have some problems with the recent modifications to the Ptrace infrastructure in 2.6.9 release. Basically I have the smiple test program attached to this E-mail and it hangs in 2.6.9 but not in 2.6.9-rc4. Somehow the first waitpid() does not return. It is not clear to me why. Is there something Ineed to change in the call itself? I am running all of this on a 2-way IA-64 machine with 2.6.9. I have not tried on x86.

I would appreciate your thoughts on this.

Thanks.

Signature · 1 line
-Stephane

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/ptrace.h>

static int p[2];

int
child(void)

{
	int ret;
	char c;

	printf("child %d started\n", getpid());
	ret = read(p[0], &c, 1);
	if (ret != 1) {
		perror("read");
		return -1;
	}
	return 0;
}

int
do_test(int argc, char **argv)

{
	int ret, status;
	pid_t pid;
	char c;

	ret = pipe(p);
	if (ret) {
		perror("pipe");
		return -1;
	}

	pid = fork();
	switch(pid) {
		case 0: close(p[1]); exit(child());

		case -1: perror("fork"); return -1;

		default: close(p[0]);
			 sleep(5);
			 /*
			  * stop child
			  */
			 ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
			 if (ret) {
				 perror("ptrace_attach");
				 goto abort;
			 }
			 printf("issued ptrace attach ret=%d\n", ret);

			 /*
			  * wait for child stop
			  */
			 ret = waitpid(pid, &status, WUNTRACED);
			 if (ret != pid) {
				 perror("waitpid");
				 goto abort;
			 }
			 printf("got process stopped\n");

			 if (WIFEXITED(status)) {
				printf("child has exited\n");
				goto abort;
			 }

			 /*
			  * detach and resume execution
			  */
			 ptrace(PTRACE_DETACH, pid, NULL, NULL);

			 /*
			  * signal child can proceed
			  */
			 ret = write(p[1], &c, 1);
			 if (ret != 1) {
				 perror("write");
				 kill(pid, SIGKILL);
				 return -1;
			 }

			/* wait for child exit */
			 ret = waitpid(pid, &status, 0);
			 if (ret == -1) {
				perror("final waitpid");
			 }
	}
	return ret == -1 || WEXITSTATUS(status) != 0? -1: 0;
abort:
	 kill(pid, SIGKILL);
	 return -1;
}

int
main(int argc, char **argv)

{
	return do_test(argc, argv);
}
#1 lore raw

This is indeed a new bug, and it is not architecture-specific. In my recent changes to close some race conditions, I overlooked the case of a process using PTRACE_ATTACH on its own children. The new PT_ATTACHED flag does not really mean "PTRACE_ATTACH was used", it means "PTRACE_ATTACH is changing the ->parent link". This patch fixes the problem that your test program demonstrates.

Thanks,
Roland

kernel/ptrace.c 23 Oct 2004 04:43:20 -0000 renamed from kernel/ptrace.c 19 Oct 2004 06:12:06 -0000 1.38 +2 -1
@@ -132,7 +132,8 @@ int ptrace_attach(struct task_struct *ta
 		goto bad;
 
 	/* Go */
-	task->ptrace |= PT_PTRACED | PT_ATTACHED;
+	task->ptrace |= PT_PTRACED | ((task->real_parent != current)
+				      ? PT_ATTACHED : 0);
 	if (capable(CAP_SYS_PTRACE))
 		task->ptrace |= PT_PTRACE_CAP;
 	task_unlock(task);
#2 lore raw
In reply to Stephane Eranian
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Stephane Eranian

Stephane> Hi Roland, I have some problems with the recent Stephane> modifications to the Ptrace infrastructure in 2.6.9 Stephane> release. Basically I have the smiple test program attached Stephane> to this E-mail and it hangs in 2.6.9 but not in Stephane> 2.6.9-rc4. Somehow the first waitpid() does not return. It Stephane> is not clear to me why. Is there something Ineed to Stephane> change in the call itself? I am running all of this on a Stephane> 2-way IA-64 machine with 2.6.9. I have not tried on x86.

Shoot, I think I ran into the same problem yesterday when running the GCC testsuite. In my case, the problem seemed to disappear (mostly) after downgrading "expect" from the "unstable" to the "stable" version, but the symptoms where exactly like in your case: waitpid() didn't return, so I suspect it really was the kernel's fault. I'll try Roland's patch on Monday.

	--david
#3 lore raw
In reply to Stephane Eranian
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines · David Mosberger

On Fri, 22 Oct 2004 10:05:57 -0700, Stephane Eranian <[email protected]> said:

Stephane> Hi Roland, I have some problems with the recent Stephane> modifications to the Ptrace infrastructure in 2.6.9 Stephane> release. Basically I have the smiple test program attached Stephane> to this E-mail and it hangs in 2.6.9 but not in Stephane> 2.6.9-rc4. Somehow the first waitpid() does not return. It Stephane> is not clear to me why. Is there something Ineed to Stephane> change in the call itself? I am running all of this on a Stephane> 2-way IA-64 machine with 2.6.9. I have not tried on x86.

David> Shoot, I think I ran into the same problem yesterday when David> running the GCC testsuite. In my case, the problem seemed to David> disappear (mostly) after downgrading "expect" from the David> "unstable" to the "stable" version, but the symptoms where David> exactly like in your case: waitpid() didn't return, so I David> suspect it really was the kernel's fault. I'll try Roland's David> patch on Monday.

Then again, may be not: it appears that the bug only relates to PTRACE'd processes, which "expect" is unlikely to do. Oh, well, would have been too easy.

	--david
#4 lore raw
In reply to Stephane Eranian
Quoted 17 lines

Stephane> Hi Roland, I have some problems with the recent Stephane> modifications to the Ptrace infrastructure in 2.6.9 Stephane> release. Basically I have the smiple test program attached Stephane> to this E-mail and it hangs in 2.6.9 but not in Stephane> 2.6.9-rc4. Somehow the first waitpid() does not return. It Stephane> is not clear to me why. Is there something Ineed to Stephane> change in the call itself? I am running all of this on a Stephane> 2-way IA-64 machine with 2.6.9. I have not tried on x86.

David> Shoot, I think I ran into the same problem yesterday when David> running the GCC testsuite. In my case, the problem seemed to David> disappear (mostly) after downgrading "expect" from the David> "unstable" to the "stable" version, but the symptoms where David> exactly like in your case: waitpid() didn't return, so I David> suspect it really was the kernel's fault. I'll try Roland's David> patch on Monday.

Quoted 4 lines

Then again, may be not: it appears that the bug only relates to PTRACE'd processes, which "expect" is unlikely to do. Oh, well, would have been too easy.

In any case, that fixed the problem for me.

Signature · 1 line
-Stephane