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