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>
32 #ifdef HAVE_SYS_SYSCALL_H
33 #include <sys/syscall.h>
37 #define WIN32_NO_STATUS
44 #include "wine/library.h"
48 #include <mach/mach.h>
49 #include <mach/mach_error.h>
50 #include <mach/thread_act.h>
51 #include <servers/bootstrap.h>
53 static mach_port_t server_mach_port
;
55 void sigchld_callback(void)
57 assert(0); /* should never be called on MacOS */
60 static void mach_set_error(kern_return_t mach_error
)
64 case KERN_SUCCESS
: break;
65 case KERN_INVALID_ARGUMENT
: set_error(STATUS_INVALID_PARAMETER
); break;
66 case KERN_NO_SPACE
: set_error(STATUS_NO_MEMORY
); break;
67 case KERN_PROTECTION_FAILURE
: set_error(STATUS_ACCESS_DENIED
); break;
68 case KERN_INVALID_ADDRESS
: set_error(STATUS_ACCESS_VIOLATION
); break;
69 default: set_error(STATUS_UNSUCCESSFUL
); break;
73 static mach_port_t
get_process_port( struct process
*process
)
75 return process
->trace_data
;
78 /* initialize the process control mechanism */
79 void init_tracing_mechanism(void)
83 if (task_get_bootstrap_port(mach_task_self(), &bp
) != KERN_SUCCESS
)
84 fatal_error("Can't find bootstrap port\n");
85 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &server_mach_port
) != KERN_SUCCESS
)
86 fatal_error("Can't allocate port\n");
87 if (mach_port_insert_right( mach_task_self(),
90 MACH_MSG_TYPE_MAKE_SEND
) != KERN_SUCCESS
)
91 fatal_error("Error inserting rights\n");
92 if (bootstrap_register(bp
, (char*)wine_get_server_dir(), server_mach_port
) != KERN_SUCCESS
)
93 fatal_error("Can't check in server_mach_port\n");
94 mach_port_deallocate(mach_task_self(), bp
);
97 /* initialize the per-process tracing mechanism */
98 void init_process_tracing( struct process
*process
)
103 mach_msg_header_t header
;
104 mach_msg_body_t body
;
105 mach_msg_port_descriptor_t task_port
;
106 mach_msg_trailer_t trailer
; /* only present on receive */
111 ret
= mach_msg( &msg
.header
, MACH_RCV_MSG
|MACH_RCV_TIMEOUT
, 0, sizeof(msg
),
112 server_mach_port
, 0, 0 );
115 if (ret
!= MACH_RCV_TIMED_OUT
&& debug_level
)
116 fprintf( stderr
, "warning: mach port receive failed with %x\n", ret
);
120 /* if anything in the message is invalid, ignore it */
121 if (msg
.header
.msgh_size
!= offsetof(typeof(msg
), trailer
)) continue;
122 if (msg
.body
.msgh_descriptor_count
!= 1) continue;
123 if (msg
.task_port
.type
!= MACH_MSG_PORT_DESCRIPTOR
) continue;
124 if (msg
.task_port
.disposition
!= MACH_MSG_TYPE_PORT_SEND
) continue;
125 if (msg
.task_port
.name
== MACH_PORT_NULL
) continue;
126 if (msg
.task_port
.name
== MACH_PORT_DEAD
) continue;
128 if (!pid_for_task( msg
.task_port
.name
, &pid
))
130 struct thread
*thread
= get_thread_from_pid( pid
);
132 if (thread
&& !thread
->process
->trace_data
)
133 thread
->process
->trace_data
= msg
.task_port
.name
;
135 mach_port_deallocate( mach_task_self(), msg
.task_port
.name
);
140 /* terminate the per-process tracing mechanism */
141 void finish_process_tracing( struct process
*process
)
143 if (process
->trace_data
)
145 mach_port_deallocate( mach_task_self(), process
->trace_data
);
146 process
->trace_data
= 0;
150 /* retrieve the thread x86 registers */
151 void get_thread_context( struct thread
*thread
, context_t
*context
, unsigned int flags
)
154 x86_debug_state32_t state
;
155 mach_msg_type_number_t count
= sizeof(state
) / sizeof(int);
156 mach_msg_type_name_t type
;
157 mach_port_t port
, process_port
= get_process_port( thread
->process
);
159 /* all other regs are handled on the client side */
160 assert( flags
== SERVER_CTX_DEBUG_REGISTERS
);
162 if (thread
->unix_pid
== -1 || !process_port
||
163 mach_port_extract_right( process_port
, thread
->unix_tid
,
164 MACH_MSG_TYPE_COPY_SEND
, &port
, &type
))
166 set_error( STATUS_ACCESS_DENIED
);
170 if (!thread_get_state( port
, x86_DEBUG_STATE32
, (thread_state_t
)&state
, &count
))
172 /* work around silly renaming of struct members in OS X 10.5 */
173 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
174 context
->debug
.i386_regs
.dr0
= state
.__dr0
;
175 context
->debug
.i386_regs
.dr1
= state
.__dr1
;
176 context
->debug
.i386_regs
.dr2
= state
.__dr2
;
177 context
->debug
.i386_regs
.dr3
= state
.__dr3
;
178 context
->debug
.i386_regs
.dr6
= state
.__dr6
;
179 context
->debug
.i386_regs
.dr7
= state
.__dr7
;
181 context
->debug
.i386_regs
.dr0
= state
.dr0
;
182 context
->debug
.i386_regs
.dr1
= state
.dr1
;
183 context
->debug
.i386_regs
.dr2
= state
.dr2
;
184 context
->debug
.i386_regs
.dr3
= state
.dr3
;
185 context
->debug
.i386_regs
.dr6
= state
.dr6
;
186 context
->debug
.i386_regs
.dr7
= state
.dr7
;
188 context
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
190 mach_port_deallocate( mach_task_self(), port
);
194 /* set the thread x86 registers */
195 void set_thread_context( struct thread
*thread
, const context_t
*context
, unsigned int flags
)
198 x86_debug_state32_t state
;
199 mach_msg_type_number_t count
= sizeof(state
) / sizeof(int);
200 mach_msg_type_name_t type
;
201 mach_port_t port
, process_port
= get_process_port( thread
->process
);
204 /* all other regs are handled on the client side */
205 assert( flags
== SERVER_CTX_DEBUG_REGISTERS
);
207 if (thread
->unix_pid
== -1 || !process_port
||
208 mach_port_extract_right( process_port
, thread
->unix_tid
,
209 MACH_MSG_TYPE_COPY_SEND
, &port
, &type
))
211 set_error( STATUS_ACCESS_DENIED
);
215 /* Mac OS doesn't allow setting the global breakpoint flags */
216 dr7
= (context
->debug
.i386_regs
.dr7
& ~0xaa) | ((context
->debug
.i386_regs
.dr7
& 0xaa) >> 1);
218 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
219 state
.__dr0
= context
->debug
.i386_regs
.dr0
;
220 state
.__dr1
= context
->debug
.i386_regs
.dr1
;
221 state
.__dr2
= context
->debug
.i386_regs
.dr2
;
222 state
.__dr3
= context
->debug
.i386_regs
.dr3
;
225 state
.__dr6
= context
->debug
.i386_regs
.dr6
;
228 state
.dr0
= context
->debug
.i386_regs
.dr0
;
229 state
.dr1
= context
->debug
.i386_regs
.dr1
;
230 state
.dr2
= context
->debug
.i386_regs
.dr2
;
231 state
.dr3
= context
->debug
.i386_regs
.dr3
;
234 state
.dr6
= context
->debug
.i386_regs
.dr6
;
237 if (!thread_set_state( port
, x86_DEBUG_STATE32
, (thread_state_t
)&state
, count
))
239 if (thread
->context
) /* update the cached values */
240 thread
->context
->debug
.i386_regs
= context
->debug
.i386_regs
;
242 mach_port_deallocate( mach_task_self(), port
);
246 int send_thread_signal( struct thread
*thread
, int sig
)
249 mach_port_t process_port
= get_process_port( thread
->process
);
251 if (thread
->unix_pid
!= -1 && process_port
)
253 mach_msg_type_name_t type
;
256 if (!mach_port_extract_right( process_port
, thread
->unix_tid
,
257 MACH_MSG_TYPE_COPY_SEND
, &port
, &type
))
259 ret
= syscall( SYS___pthread_kill
, port
, sig
);
260 mach_port_deallocate( mach_task_self(), port
);
264 if (ret
== -1 && errno
== ESRCH
) /* thread got killed */
266 thread
->unix_pid
= -1;
267 thread
->unix_tid
= -1;
270 if (debug_level
&& ret
!= -1)
271 fprintf( stderr
, "%04x: *sent signal* signal=%d\n", thread
->id
, sig
);
275 /* read data from a process memory space */
276 int read_process_memory( struct process
*process
, client_ptr_t ptr
, data_size_t size
, char *dest
)
279 mach_msg_type_number_t bytes_read
;
280 vm_offset_t offset
, data
;
281 vm_address_t aligned_address
;
282 vm_size_t aligned_size
;
283 unsigned int page_size
= get_page_size();
284 mach_port_t process_port
= get_process_port( process
);
288 set_error( STATUS_ACCESS_DENIED
);
291 if ((vm_address_t
)ptr
!= ptr
)
293 set_error( STATUS_ACCESS_DENIED
);
297 if ((ret
= task_suspend( process_port
)) != KERN_SUCCESS
)
299 mach_set_error( ret
);
303 offset
= ptr
% page_size
;
304 aligned_address
= (vm_address_t
)(ptr
- offset
);
305 aligned_size
= (size
+ offset
+ page_size
- 1) / page_size
* page_size
;
307 ret
= vm_read( process_port
, aligned_address
, aligned_size
, &data
, &bytes_read
);
308 if (ret
!= KERN_SUCCESS
) mach_set_error( ret
);
311 memcpy( dest
, (char *)data
+ offset
, size
);
312 vm_deallocate( mach_task_self(), data
, bytes_read
);
314 task_resume( process_port
);
315 return (ret
== KERN_SUCCESS
);
318 /* write data to a process memory space */
319 int write_process_memory( struct process
*process
, client_ptr_t ptr
, data_size_t size
, const char *src
)
322 vm_address_t aligned_address
, region_address
;
323 vm_size_t aligned_size
, region_size
;
324 mach_msg_type_number_t info_size
, bytes_read
;
325 vm_offset_t offset
, task_mem
= 0;
326 struct vm_region_basic_info info
;
328 unsigned int page_size
= get_page_size();
329 mach_port_t process_port
= get_process_port( process
);
333 set_error( STATUS_ACCESS_DENIED
);
336 if ((vm_address_t
)ptr
!= ptr
)
338 set_error( STATUS_ACCESS_DENIED
);
342 offset
= ptr
% page_size
;
343 aligned_address
= (vm_address_t
)(ptr
- offset
);
344 aligned_size
= (size
+ offset
+ page_size
- 1) / page_size
* page_size
;
346 if ((ret
= task_suspend( process_port
)) != KERN_SUCCESS
)
348 mach_set_error( ret
);
352 ret
= vm_read( process_port
, aligned_address
, aligned_size
, &task_mem
, &bytes_read
);
353 if (ret
!= KERN_SUCCESS
)
355 mach_set_error( ret
);
358 region_address
= aligned_address
;
359 info_size
= sizeof(info
);
360 ret
= vm_region( process_port
, ®ion_address
, ®ion_size
, VM_REGION_BASIC_INFO
,
361 (vm_region_info_t
)&info
, &info_size
, &dummy
);
362 if (ret
!= KERN_SUCCESS
)
364 mach_set_error( ret
);
367 if (region_address
> aligned_address
||
368 region_address
+ region_size
< aligned_address
+ aligned_size
)
370 /* FIXME: should support multiple regions */
371 set_error( ERROR_ACCESS_DENIED
);
374 ret
= vm_protect( process_port
, aligned_address
, aligned_size
, 0, VM_PROT_READ
| VM_PROT_WRITE
);
375 if (ret
!= KERN_SUCCESS
)
377 mach_set_error( ret
);
381 /* FIXME: there's an optimization that can be made: check first and last */
382 /* pages for writability; read first and last pages; write interior */
383 /* pages to task without ever reading&modifying them; if that succeeds, */
384 /* modify first and last pages and write them. */
386 memcpy( (char*)task_mem
+ offset
, src
, size
);
388 ret
= vm_write( process_port
, aligned_address
, task_mem
, bytes_read
);
389 if (ret
!= KERN_SUCCESS
) mach_set_error( ret
);
392 vm_deallocate( mach_task_self(), task_mem
, bytes_read
);
393 /* restore protection */
394 vm_protect( process_port
, aligned_address
, aligned_size
, 0, info
.protection
);
395 task_resume( process_port
);
400 if (task_mem
) vm_deallocate( mach_task_self(), task_mem
, bytes_read
);
401 task_resume( process_port
);
405 /* retrieve an LDT selector entry */
406 void get_selector_entry( struct thread
*thread
, int entry
, unsigned int *base
,
407 unsigned int *limit
, unsigned char *flags
)
409 const unsigned int total_size
= (2 * sizeof(int) + 1) * 8192;
410 struct process
*process
= thread
->process
;
411 unsigned int page_size
= get_page_size();
414 mach_msg_type_number_t bytes_read
;
415 mach_port_t process_port
= get_process_port( thread
->process
);
417 if (!process
->ldt_copy
|| !process_port
)
419 set_error( STATUS_ACCESS_DENIED
);
424 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
428 if ((ret
= task_suspend( process_port
)) == KERN_SUCCESS
)
430 vm_offset_t offset
= process
->ldt_copy
% page_size
;
431 vm_address_t aligned_address
= (vm_address_t
)(process
->ldt_copy
- offset
);
432 vm_size_t aligned_size
= (total_size
+ offset
+ page_size
- 1) / page_size
* page_size
;
434 ret
= vm_read( process_port
, aligned_address
, aligned_size
, &data
, &bytes_read
);
435 if (ret
!= KERN_SUCCESS
) mach_set_error( ret
);
438 const int *ldt
= (const int *)((char *)data
+ offset
);
439 memcpy( base
, ldt
+ entry
, sizeof(int) );
440 memcpy( limit
, ldt
+ entry
+ 8192, sizeof(int) );
441 memcpy( flags
, (char *)(ldt
+ 2 * 8192) + entry
, 1 );
442 vm_deallocate( mach_task_self(), data
, bytes_read
);
444 task_resume( process_port
);
446 else mach_set_error( ret
);
449 #endif /* USE_MACH */