2 * Server-side debugger support using Mach primitives
4 * Copyright (C) 1999, 2006 Alexandre Julliard
5 * Copyright (C) 2006 Ken Thomases for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #include <sys/types.h>
34 #define WIN32_NO_STATUS
41 #include "wine/library.h"
45 #include <mach/mach.h>
46 #include <mach/mach_error.h>
47 #include <mach/thread_act.h>
48 #include <servers/bootstrap.h>
50 #if defined(__APPLE__) && defined(__i386__)
51 extern int pthread_kill_syscall( mach_port_t
, int );
52 __ASM_GLOBAL_FUNC( pthread_kill_syscall
,
53 "movl $328,%eax\n\t" /* SYS___pthread_kill */
59 static inline int pthread_kill_syscall( mach_port_t
, int )
65 static mach_port_t server_mach_port
;
67 void sigchld_callback(void)
69 assert(0); /* should never be called on MacOS */
72 static void mach_set_error(kern_return_t mach_error
)
76 case KERN_SUCCESS
: break;
77 case KERN_INVALID_ARGUMENT
: set_error(STATUS_INVALID_PARAMETER
); break;
78 case KERN_NO_SPACE
: set_error(STATUS_NO_MEMORY
); break;
79 case KERN_PROTECTION_FAILURE
: set_error(STATUS_ACCESS_DENIED
); break;
80 case KERN_INVALID_ADDRESS
: set_error(STATUS_ACCESS_VIOLATION
); break;
81 default: set_error(STATUS_UNSUCCESSFUL
); break;
85 static mach_port_t
get_process_port( struct process
*process
)
87 return process
->trace_data
;
90 /* initialize the process control mechanism */
91 void init_tracing_mechanism(void)
95 if (task_get_bootstrap_port(mach_task_self(), &bp
) != KERN_SUCCESS
)
96 fatal_error("Can't find bootstrap port\n");
97 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &server_mach_port
) != KERN_SUCCESS
)
98 fatal_error("Can't allocate port\n");
99 if (mach_port_insert_right( mach_task_self(),
102 MACH_MSG_TYPE_MAKE_SEND
) != KERN_SUCCESS
)
103 fatal_error("Error inserting rights\n");
104 if (bootstrap_register(bp
, (char*)wine_get_server_dir(), server_mach_port
) != KERN_SUCCESS
)
105 fatal_error("Can't check in server_mach_port\n");
106 mach_port_deallocate(mach_task_self(), bp
);
109 /* initialize the per-process tracing mechanism */
110 void init_process_tracing( struct process
*process
)
115 mach_msg_header_t header
;
116 mach_msg_body_t body
;
117 mach_msg_port_descriptor_t task_port
;
118 mach_msg_trailer_t trailer
; /* only present on receive */
123 ret
= mach_msg( &msg
.header
, MACH_RCV_MSG
|MACH_RCV_TIMEOUT
, 0, sizeof(msg
),
124 server_mach_port
, 0, 0 );
127 if (ret
!= MACH_RCV_TIMED_OUT
&& debug_level
)
128 fprintf( stderr
, "warning: mach port receive failed with %x\n", ret
);
132 /* if anything in the message is invalid, ignore it */
133 if (msg
.header
.msgh_size
!= offsetof(typeof(msg
), trailer
)) continue;
134 if (msg
.body
.msgh_descriptor_count
!= 1) continue;
135 if (msg
.task_port
.type
!= MACH_MSG_PORT_DESCRIPTOR
) continue;
136 if (msg
.task_port
.disposition
!= MACH_MSG_TYPE_PORT_SEND
) continue;
137 if (msg
.task_port
.name
== MACH_PORT_NULL
) continue;
138 if (msg
.task_port
.name
== MACH_PORT_DEAD
) continue;
140 if (!pid_for_task( msg
.task_port
.name
, &pid
))
142 struct thread
*thread
= get_thread_from_pid( pid
);
144 if (thread
&& !thread
->process
->trace_data
)
145 thread
->process
->trace_data
= msg
.task_port
.name
;
147 mach_port_deallocate( mach_task_self(), msg
.task_port
.name
);
152 /* terminate the per-process tracing mechanism */
153 void finish_process_tracing( struct process
*process
)
155 if (process
->trace_data
)
157 mach_port_deallocate( mach_task_self(), process
->trace_data
);
158 process
->trace_data
= 0;
162 /* retrieve the thread x86 registers */
163 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
165 struct x86_debug_state32 state
;
166 mach_msg_type_number_t count
= sizeof(state
) / sizeof(int);
167 mach_msg_type_name_t type
;
168 mach_port_t port
, process_port
= get_process_port( thread
->process
);
170 /* all other regs are handled on the client side */
171 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
173 if (thread
->unix_pid
== -1 || !process_port
||
174 mach_port_extract_right( process_port
, thread
->unix_tid
,
175 MACH_MSG_TYPE_COPY_SEND
, &port
, &type
))
177 set_error( STATUS_ACCESS_DENIED
);
181 if (!thread_get_state( port
, x86_DEBUG_STATE32
, (thread_state_t
)&state
, &count
))
183 context
->Dr0
= state
.dr0
;
184 context
->Dr1
= state
.dr1
;
185 context
->Dr2
= state
.dr2
;
186 context
->Dr3
= state
.dr3
;
187 context
->Dr6
= state
.dr6
;
188 context
->Dr7
= state
.dr7
;
189 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
191 mach_port_deallocate( mach_task_self(), port
);
194 /* set the thread x86 registers */
195 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
197 struct x86_debug_state32 state
;
198 mach_msg_type_number_t count
= sizeof(state
) / sizeof(int);
199 mach_msg_type_name_t type
;
200 mach_port_t port
, process_port
= get_process_port( thread
->process
);
202 /* all other regs are handled on the client side */
203 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
205 if (thread
->unix_pid
== -1 || !process_port
||
206 mach_port_extract_right( process_port
, thread
->unix_tid
,
207 MACH_MSG_TYPE_COPY_SEND
, &port
, &type
))
209 set_error( STATUS_ACCESS_DENIED
);
213 state
.dr0
= context
->Dr0
;
214 state
.dr1
= context
->Dr1
;
215 state
.dr2
= context
->Dr2
;
216 state
.dr3
= context
->Dr3
;
219 state
.dr6
= context
->Dr6
;
220 state
.dr7
= context
->Dr7
;
221 if (!thread_set_state( port
, x86_DEBUG_STATE32
, (thread_state_t
)&state
, count
))
223 if (thread
->context
) /* update the cached values */
225 thread
->context
->Dr0
= context
->Dr0
;
226 thread
->context
->Dr1
= context
->Dr1
;
227 thread
->context
->Dr2
= context
->Dr2
;
228 thread
->context
->Dr3
= context
->Dr3
;
229 thread
->context
->Dr6
= context
->Dr6
;
230 thread
->context
->Dr7
= context
->Dr7
;
233 mach_port_deallocate( mach_task_self(), port
);
236 int send_thread_signal( struct thread
*thread
, int sig
)
239 mach_port_t process_port
= get_process_port( thread
->process
);
241 if (thread
->unix_pid
!= -1 && process_port
)
243 mach_msg_type_name_t type
;
246 if (!mach_port_extract_right( process_port
, thread
->unix_tid
,
247 MACH_MSG_TYPE_COPY_SEND
, &port
, &type
))
249 if ((ret
= pthread_kill_syscall( port
, sig
)) < 0)
254 mach_port_deallocate( mach_task_self(), port
);
258 if (ret
== -1 && errno
== ESRCH
) /* thread got killed */
260 thread
->unix_pid
= -1;
261 thread
->unix_tid
= -1;
264 if (debug_level
&& ret
!= -1)
265 fprintf( stderr
, "%04x: *sent signal* signal=%d\n", thread
->id
, sig
);
269 /* read data from a process memory space */
270 int read_process_memory( struct process
*process
, const void *ptr
, data_size_t size
, char *dest
)
273 mach_msg_type_number_t bytes_read
;
274 vm_offset_t offset
, data
;
275 vm_address_t aligned_address
;
276 vm_size_t aligned_size
;
277 unsigned int page_size
= get_page_size();
278 mach_port_t process_port
= get_process_port( process
);
282 set_error( STATUS_ACCESS_DENIED
);
286 if ((ret
= task_suspend( process_port
)) != KERN_SUCCESS
)
288 mach_set_error( ret
);
292 offset
= (unsigned long)ptr
% page_size
;
293 aligned_address
= (vm_address_t
)((char *)ptr
- offset
);
294 aligned_size
= (size
+ offset
+ page_size
- 1) / page_size
* page_size
;
296 ret
= vm_read( process_port
, aligned_address
, aligned_size
, &data
, &bytes_read
);
297 if (ret
!= KERN_SUCCESS
) mach_set_error( ret
);
300 memcpy( dest
, (char *)data
+ offset
, size
);
301 vm_deallocate( mach_task_self(), data
, bytes_read
);
303 task_resume( process_port
);
304 return (ret
== KERN_SUCCESS
);
307 /* write data to a process memory space */
308 int write_process_memory( struct process
*process
, void *ptr
, data_size_t size
, const char *src
)
311 vm_address_t aligned_address
, region_address
;
312 vm_size_t aligned_size
, region_size
;
313 mach_msg_type_number_t info_size
, bytes_read
;
314 vm_offset_t offset
, task_mem
= 0;
315 struct vm_region_basic_info info
;
317 unsigned int page_size
= get_page_size();
318 mach_port_t process_port
= get_process_port( process
);
322 set_error( STATUS_ACCESS_DENIED
);
326 offset
= (unsigned long)ptr
% page_size
;
327 aligned_address
= (vm_address_t
)((char *)ptr
- offset
);
328 aligned_size
= (size
+ offset
+ page_size
- 1) / page_size
* page_size
;
330 if ((ret
= task_suspend( process_port
)) != KERN_SUCCESS
)
332 mach_set_error( ret
);
336 ret
= vm_read( process_port
, aligned_address
, aligned_size
, &task_mem
, &bytes_read
);
337 if (ret
!= KERN_SUCCESS
)
339 mach_set_error( ret
);
342 region_address
= aligned_address
;
343 info_size
= sizeof(info
);
344 ret
= vm_region( process_port
, ®ion_address
, ®ion_size
, VM_REGION_BASIC_INFO
,
345 (vm_region_info_t
)&info
, &info_size
, &dummy
);
346 if (ret
!= KERN_SUCCESS
)
348 mach_set_error( ret
);
351 if (region_address
> aligned_address
||
352 region_address
+ region_size
< aligned_address
+ aligned_size
)
354 /* FIXME: should support multiple regions */
355 set_error( ERROR_ACCESS_DENIED
);
358 ret
= vm_protect( process_port
, aligned_address
, aligned_size
, 0, VM_PROT_READ
| VM_PROT_WRITE
);
359 if (ret
!= KERN_SUCCESS
)
361 mach_set_error( ret
);
365 /* FIXME: there's an optimization that can be made: check first and last */
366 /* pages for writability; read first and last pages; write interior */
367 /* pages to task without ever reading&modifying them; if that succeeds, */
368 /* modify first and last pages and write them. */
370 memcpy( (char*)task_mem
+ offset
, src
, size
);
372 ret
= vm_write( process_port
, aligned_address
, task_mem
, bytes_read
);
373 if (ret
!= KERN_SUCCESS
) mach_set_error( ret
);
376 vm_deallocate( mach_task_self(), task_mem
, bytes_read
);
377 /* restore protection */
378 vm_protect( process_port
, aligned_address
, aligned_size
, 0, info
.protection
);
379 task_resume( process_port
);
384 if (task_mem
) vm_deallocate( mach_task_self(), task_mem
, bytes_read
);
385 task_resume( process_port
);
389 /* retrieve an LDT selector entry */
390 void get_selector_entry( struct thread
*thread
, int entry
, unsigned int *base
,
391 unsigned int *limit
, unsigned char *flags
)
393 const unsigned int total_size
= (2 * sizeof(int) + 1) * 8192;
394 struct process
*process
= thread
->process
;
395 unsigned int page_size
= get_page_size();
398 mach_msg_type_number_t bytes_read
;
399 mach_port_t process_port
= get_process_port( thread
->process
);
401 if (!process
->ldt_copy
|| !process_port
)
403 set_error( STATUS_ACCESS_DENIED
);
408 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
412 if ((ret
= task_suspend( process_port
)) == KERN_SUCCESS
)
414 void *ptr
= process
->ldt_copy
;
415 vm_offset_t offset
= (unsigned long)ptr
% page_size
;
416 vm_address_t aligned_address
= (vm_address_t
)((char *)ptr
- offset
);
417 vm_size_t aligned_size
= (total_size
+ offset
+ page_size
- 1) / page_size
* page_size
;
419 ret
= vm_read( process_port
, aligned_address
, aligned_size
, &data
, &bytes_read
);
420 if (ret
!= KERN_SUCCESS
) mach_set_error( ret
);
423 const int *ldt
= (const int *)((char *)data
+ offset
);
424 memcpy( base
, ldt
+ entry
, sizeof(int) );
425 memcpy( limit
, ldt
+ entry
+ 8192, sizeof(int) );
426 memcpy( flags
, (char *)(ldt
+ 2 * 8192) + entry
, 1 );
427 vm_deallocate( mach_task_self(), data
, bytes_read
);
429 task_resume( process_port
);
431 else mach_set_error( ret
);
434 #endif /* USE_MACH */