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