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