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