Back to index

php5  5.3.10
fpm_trace_ptrace.c
Go to the documentation of this file.
00001 
00002        /* $Id: fpm_trace_ptrace.c,v 1.7 2008/09/18 23:34:11 anight Exp $ */
00003        /* (c) 2007,2008 Andrei Nigmatulin */
00004 
00005 #include "fpm_config.h"
00006 
00007 #include <sys/wait.h>
00008 #include <sys/ptrace.h>
00009 #include <unistd.h>
00010 #include <errno.h>
00011 
00012 #if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
00013 #define PTRACE_ATTACH PT_ATTACH
00014 #endif
00015 
00016 #if defined(PT_DETACH) && !defined(PTRACE_DETACH)
00017 #define PTRACE_DETACH PT_DETACH
00018 #endif
00019 
00020 #if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
00021 #define PTRACE_PEEKDATA PT_READ_D
00022 #endif
00023 
00024 #include "fpm_trace.h"
00025 #include "zlog.h"
00026 
00027 static pid_t traced_pid;
00028 
00029 int fpm_trace_signal(pid_t pid) /* {{{ */
00030 {
00031        if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
00032               zlog(ZLOG_SYSERROR, "failed to ptrace(ATTACH) child %d", pid);
00033               return -1;
00034        }
00035        return 0;
00036 }
00037 /* }}} */
00038 
00039 int fpm_trace_ready(pid_t pid) /* {{{ */
00040 {
00041        traced_pid = pid;
00042        return 0;
00043 }
00044 /* }}} */
00045 
00046 int fpm_trace_close(pid_t pid) /* {{{ */
00047 {
00048        if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
00049               zlog(ZLOG_SYSERROR, "failed to ptrace(DETACH) child %d", pid);
00050               return -1;
00051        }
00052        traced_pid = 0;
00053        return 0;
00054 }
00055 /* }}} */
00056 
00057 int fpm_trace_get_long(long addr, long *data) /* {{{ */
00058 {
00059 #ifdef PT_IO
00060        struct ptrace_io_desc ptio = {
00061               .piod_op = PIOD_READ_D,
00062               .piod_offs = (void *) addr,
00063               .piod_addr = (void *) data,
00064               .piod_len = sizeof(long)
00065        };
00066 
00067        if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
00068               zlog(ZLOG_SYSERROR, "failed to ptrace(PT_IO) pid %d", traced_pid);
00069               return -1;
00070        }
00071 #else
00072        errno = 0;
00073        *data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
00074        if (errno) {
00075               zlog(ZLOG_SYSERROR, "failed to ptrace(PEEKDATA) pid %d", traced_pid);
00076               return -1;
00077        }
00078 #endif
00079        return 0;
00080 }
00081 /* }}} */
00082