A question about ptrace

Ali Polatel alip at exherbo.org
Sat Jan 16 11:32:52 UTC 2010

I'm trying to port a program using ptrace from Linux to FreeBSD.
For this reason I'm trying to understand how ptrace on FreeBSD works.
Below is a sample program I've written which fork()'s and executes
"true" after calling PT_TRACE_ME. Having read the manual page of ptrace
I assume the printf() in parent should print SIGTRAP but it gives:
"Segmentation fault: 11"
Can someone help me figure out the problem?

#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
    int status;
    pid_t pid;
    char *const myargv[] = { "true", NULL };

    pid = fork();
    if (0 > pid)
    else if (!pid) {
        ptrace(PT_TRACE_ME, 0, 0, 0);
        execvp(myargv[0], myargv);
    else {
        assert(0 < waitpid(pid, &status, 0));

        assert(0 == ptrace(PT_TO_SCE, pid, 0, 0));
        assert(0 < waitpid(pid, &status, 0));

        printf("%s\n", strsignal(WSTOPSIG(status)));
        ptrace(PT_KILL, pid, 0, 0);
        return 0;

Ali Polatel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20100116/757ad1aa/attachment.pgp

More information about the freebsd-questions mailing list