All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Hobein <ah2@delair.de>
To: linux-kernel@vger.kernel.org
Subject: Trouble with ptrace self-attach rule since kernel > 2.6.14
Date: Thu, 31 Aug 2006 23:05:47 +0200	[thread overview]
Message-ID: <200608312305.47515.ah2@delair.de> (raw)

[-- 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;
}

             reply	other threads:[~2006-08-31 21:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-31 21:05 Andreas Hobein [this message]
2006-09-01  7:36 ` Trouble with ptrace self-attach rule since kernel > 2.6.14 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200608312305.47515.ah2@delair.de \
    --to=ah2@delair.de \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.