1 /* (c) 2007,2008 Andrei Nigmatulin */
2
3 #include "fpm_config.h"
4
5 #include <mach/mach.h>
6 #include <mach/mach_vm.h>
7
8 #include <unistd.h>
9
10 #include "fpm_trace.h"
11 #include "fpm_process_ctl.h"
12 #include "fpm_unix.h"
13 #include "zlog.h"
14
15
16 static mach_port_name_t target;
17 static vm_offset_t target_page_base;
18 static vm_offset_t local_page;
19 static mach_msg_type_number_t local_size;
20
fpm_mach_vm_deallocate()21 static void fpm_mach_vm_deallocate() /* {{{ */
22 {
23 if (local_page) {
24 mach_vm_deallocate(mach_task_self(), local_page, local_size);
25 target_page_base = 0;
26 local_page = 0;
27 local_size = 0;
28 }
29 }
30 /* }}} */
31
fpm_mach_vm_read_page(vm_offset_t page)32 static int fpm_mach_vm_read_page(vm_offset_t page) /* {{{ */
33 {
34 kern_return_t kr;
35
36 kr = mach_vm_read(target, page, fpm_pagesize, &local_page, &local_size);
37 if (kr != KERN_SUCCESS) {
38 zlog(ZLOG_ERROR, "failed to read vm page: mach_vm_read(): %s (%d)", mach_error_string(kr), kr);
39 return -1;
40 }
41 return 0;
42 }
43 /* }}} */
44
fpm_trace_signal(pid_t pid)45 int fpm_trace_signal(pid_t pid) /* {{{ */
46 {
47 if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
48 zlog(ZLOG_SYSERROR, "failed to send SIGSTOP to %d", pid);
49 return -1;
50 }
51 return 0;
52 }
53 /* }}} */
54
fpm_trace_ready(pid_t pid)55 int fpm_trace_ready(pid_t pid) /* {{{ */
56 {
57 kern_return_t kr;
58
59 kr = task_for_pid(mach_task_self(), pid, &target);
60 if (kr != KERN_SUCCESS) {
61 char *msg = "";
62
63 if (kr == KERN_FAILURE) {
64 msg = " It seems that master process does not have enough privileges to trace processes.";
65 }
66 zlog(ZLOG_ERROR, "task_for_pid() failed: %s (%d)%s", mach_error_string(kr), kr, msg);
67 return -1;
68 }
69 return 0;
70 }
71 /* }}} */
72
fpm_trace_close(pid_t pid)73 int fpm_trace_close(pid_t pid) /* {{{ */
74 {
75 fpm_mach_vm_deallocate();
76 target = 0;
77 return 0;
78 }
79 /* }}} */
80
fpm_trace_get_long(long addr,long * data)81 int fpm_trace_get_long(long addr, long *data) /* {{{ */
82 {
83 size_t offset = ((uintptr_t) (addr) % fpm_pagesize);
84 vm_offset_t base = (uintptr_t) (addr) - offset;
85
86 if (base != target_page_base) {
87 fpm_mach_vm_deallocate();
88 if (0 > fpm_mach_vm_read_page(base)) {
89 return -1;
90 }
91 }
92 *data = * (long *) (local_page + offset);
93 return 0;
94 }
95 /* }}} */
96