xref: /PHP-5.5/sapi/fpm/fpm/fpm_trace_ptrace.c (revision 4e396d73)
1 
2 	/* $Id: fpm_trace_ptrace.c,v 1.7 2008/09/18 23:34:11 anight Exp $ */
3 	/* (c) 2007,2008 Andrei Nigmatulin */
4 
5 #include "fpm_config.h"
6 
7 #include <sys/wait.h>
8 #include <sys/ptrace.h>
9 #include <unistd.h>
10 #include <errno.h>
11 
12 #if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
13 #define PTRACE_ATTACH PT_ATTACH
14 #endif
15 
16 #if defined(PT_DETACH) && !defined(PTRACE_DETACH)
17 #define PTRACE_DETACH PT_DETACH
18 #endif
19 
20 #if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
21 #define PTRACE_PEEKDATA PT_READ_D
22 #endif
23 
24 #include "fpm_trace.h"
25 #include "zlog.h"
26 
27 static pid_t traced_pid;
28 
fpm_trace_signal(pid_t pid)29 int fpm_trace_signal(pid_t pid) /* {{{ */
30 {
31 	if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
32 		zlog(ZLOG_SYSERROR, "failed to ptrace(ATTACH) child %d", pid);
33 		return -1;
34 	}
35 	return 0;
36 }
37 /* }}} */
38 
fpm_trace_ready(pid_t pid)39 int fpm_trace_ready(pid_t pid) /* {{{ */
40 {
41 	traced_pid = pid;
42 	return 0;
43 }
44 /* }}} */
45 
fpm_trace_close(pid_t pid)46 int fpm_trace_close(pid_t pid) /* {{{ */
47 {
48 	if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
49 		zlog(ZLOG_SYSERROR, "failed to ptrace(DETACH) child %d", pid);
50 		return -1;
51 	}
52 	traced_pid = 0;
53 	return 0;
54 }
55 /* }}} */
56 
fpm_trace_get_long(long addr,long * data)57 int fpm_trace_get_long(long addr, long *data) /* {{{ */
58 {
59 #ifdef PT_IO
60 	struct ptrace_io_desc ptio = {
61 		.piod_op = PIOD_READ_D,
62 		.piod_offs = (void *) addr,
63 		.piod_addr = (void *) data,
64 		.piod_len = sizeof(long)
65 	};
66 
67 	if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
68 		zlog(ZLOG_SYSERROR, "failed to ptrace(PT_IO) pid %d", traced_pid);
69 		return -1;
70 	}
71 #else
72 	errno = 0;
73 	*data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
74 	if (errno) {
75 		zlog(ZLOG_SYSERROR, "failed to ptrace(PEEKDATA) pid %d", traced_pid);
76 		return -1;
77 	}
78 #endif
79 	return 0;
80 }
81 /* }}} */
82 
83