All of lore.kernel.org
 help / color / mirror / Atom feed
* Trouble with ptrace self-attach rule since kernel > 2.6.14
@ 2006-08-31 21:05 Andreas Hobein
  2006-09-01  7:36 ` Andreas Hobein
  0 siblings, 1 reply; 12+ messages in thread
From: Andreas Hobein @ 2006-08-31 21:05 UTC (permalink / raw)
  To: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1295 bytes --]

Hi !

I have some trouble with the restriction of the ptrace functionality assumably 
introduced into the linux kernel  with the patch from 9. 11.2006 
1105_2_ptrace-self-attach.patch.

My multithreaded application tries to write callstacks of all threads (some 
sort of built-in mini debugger) in case of abnormal situations or failure. 
With the newer linux kernel (> 2.6.14) self-attaching to processes of the 
same thread group does not work any longer. Any call to ptrace results in a 
EPERM result.

I have worked around this problem by first forking the process, than creating 
the callstack output in the forked child process - which works without the 
above mentioned problem - and terminating the child process just after this 
operation.

Anyway this solution is somehow dirty and I would prefer the way it was 
implemented before. My question is: Why may a sibling thread not 
ptrace_attach another process of the same thread group, while at the same 
time a forked child process of the same thread is allowed to do this 
operation? Is there any replacement like pthread_suspend, which is available 
on other Unixes?

(A short program for the demonstration of this effect is attached. Use Option 
-f to enable forking)

Best regards,

        Andreas


[-- Attachment #2: trace.c --]
[-- Type: text/x-csrc, Size: 1811 bytes --]

// Build with:    gcc trace.c -o trace -lpthread
// Usage trace [-f ]    Option -f forks the tracing process before attaching to child thread

#include <stdio.h>
#include <errno.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <unistd.h>

pthread_t threadPid=0;

void *threadFunc(void* dummy)
{

    threadPid=syscall(__NR_gettid);
    
    while(1)
    {
        printf("Thread is running with pid %d\n",threadPid);
	sleep(1);
    }
}

int main (int argc,char** argv)
{
    printf("Parent pid: %d\n",getpid());
    
    pthread_t thread;
    if (pthread_create(&thread, NULL, &threadFunc, NULL) == -1)
    {
	perror("pthread_create:");
	return 10;
    }

    sleep(1);
    
    pid_t childPid;
    
    if(argc==2 && strcmp(argv[1],"-f")==0 &&( childPid=fork()) > 0)
    {
        printf("Forking process for PTRACE_ATTACH, waitig for\n");
        int status;
        
        waitpid(childPid,&status,0);
        
        if( WIFEXITED(status) )
        {
            printf("Child terminated normally\n");
        }
        return 0;
    }
        
    printf("Tracing threadPid %d.\n",threadPid);

    if(ptrace(PTRACE_ATTACH,threadPid,NULL,NULL)!=-1)
    {
        int status;

        if(waitpid(threadPid, &status, WUNTRACED|__WALL) == threadPid)
        {
            if(ptrace(PTRACE_DETACH,threadPid,NULL,NULL)!=-1)
            {
                printf("Process %d attaching/detaching was sucessful!\n");
            }
            else
            {
                perror("PTRACE_ATTACH:");
            }
        }
        else
        {
            perror("waitthreadPid:");
            printf("status:%d errno:%d\n",status,errno);
        }

    }
    else
    {
        perror("PTRACE_ATTACH: ");
    }
    return 0;
}

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2006-09-04 22:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-31 21:05 Trouble with ptrace self-attach rule since kernel > 2.6.14 Andreas Hobein
2006-09-01  7:36 ` Andreas Hobein
2006-09-01  7:49   ` Andrew Morton
2006-09-01 18:28     ` Linus Torvalds
2006-09-02 17:03       ` Oleg Nesterov
2006-09-04 12:16         ` Andreas Hobein
2006-09-04 15:23           ` Oleg Nesterov
2006-09-04 15:56             ` Andreas Hobein
2006-09-04 21:42               ` Andreas Hobein
2006-09-04 22:00                 ` Linus Torvalds
2006-09-04 20:07             ` Markus Gutschke
2006-09-02 17:22       ` [PATCH] eligible_child: remove an obsolete ->tgid check Oleg Nesterov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.