gdi32/tests: Test substituted face family name.
[wine.git] / server / mach.c
blob77f1fdcdb86f3332b04c3b9e74ed2819d60c695e
1 /*
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
22 #include "config.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #ifdef HAVE_SYS_SYSCALL_H
32 #include <sys/syscall.h>
33 #endif
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "winternl.h"
39 #include "file.h"
40 #include "process.h"
41 #include "thread.h"
42 #include "request.h"
44 #ifdef USE_MACH
46 #include <mach/mach.h>
47 #include <mach/mach_error.h>
48 #include <mach/thread_act.h>
49 #include <mach/mach_vm.h>
50 #include <servers/bootstrap.h>
52 static mach_port_t server_mach_port;
54 void sigchld_callback(void)
56 assert(0); /* should never be called on MacOS */
59 static void mach_set_error(kern_return_t mach_error)
61 switch (mach_error)
63 case KERN_SUCCESS: break;
64 case KERN_INVALID_ARGUMENT: set_error(STATUS_INVALID_PARAMETER); break;
65 case KERN_NO_SPACE: set_error(STATUS_NO_MEMORY); break;
66 case KERN_PROTECTION_FAILURE: set_error(STATUS_ACCESS_DENIED); break;
67 case KERN_INVALID_ADDRESS: set_error(STATUS_ACCESS_VIOLATION); break;
68 default: set_error(STATUS_UNSUCCESSFUL); break;
72 static mach_port_t get_process_port( struct process *process )
74 return process->trace_data;
77 /* initialize the process control mechanism */
78 void init_tracing_mechanism(void)
80 mach_port_t bp;
82 if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
83 fatal_error("Can't find bootstrap port\n");
84 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
85 fatal_error("Can't allocate port\n");
86 if (mach_port_insert_right( mach_task_self(),
87 server_mach_port,
88 server_mach_port,
89 MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
90 fatal_error("Error inserting rights\n");
91 if (bootstrap_register(bp, server_dir, server_mach_port) != KERN_SUCCESS)
92 fatal_error("Can't check in server_mach_port\n");
93 mach_port_deallocate(mach_task_self(), bp);
96 /* initialize the per-process tracing mechanism */
97 void init_process_tracing( struct process *process )
99 int pid, ret;
100 struct
102 mach_msg_header_t header;
103 mach_msg_body_t body;
104 mach_msg_port_descriptor_t task_port;
105 mach_msg_trailer_t trailer; /* only present on receive */
106 } msg;
108 for (;;)
110 ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
111 server_mach_port, 0, 0 );
112 if (ret)
114 if (ret != MACH_RCV_TIMED_OUT && debug_level)
115 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
116 return;
119 /* if anything in the message is invalid, ignore it */
120 if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
121 if (msg.body.msgh_descriptor_count != 1) continue;
122 if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
123 if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
124 if (msg.task_port.name == MACH_PORT_NULL) continue;
125 if (msg.task_port.name == MACH_PORT_DEAD) continue;
127 if (!pid_for_task( msg.task_port.name, &pid ))
129 struct thread *thread = get_thread_from_pid( pid );
131 if (thread && !thread->process->trace_data)
132 thread->process->trace_data = msg.task_port.name;
133 else
134 mach_port_deallocate( mach_task_self(), msg.task_port.name );
139 /* terminate the per-process tracing mechanism */
140 void finish_process_tracing( struct process *process )
142 if (process->trace_data)
144 mach_port_deallocate( mach_task_self(), process->trace_data );
145 process->trace_data = 0;
149 /* initialize registers in new thread if necessary */
150 void init_thread_context( struct thread *thread )
154 /* retrieve the thread x86 registers */
155 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
157 #if defined(__i386__) || defined(__x86_64__)
158 x86_debug_state_t state;
159 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
160 mach_msg_type_name_t type;
161 mach_port_t port, process_port = get_process_port( thread->process );
163 /* all other regs are handled on the client side */
164 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
166 if (thread->unix_pid == -1 || !process_port ||
167 mach_port_extract_right( process_port, thread->unix_tid,
168 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
170 set_error( STATUS_ACCESS_DENIED );
171 return;
174 if (!thread_get_state( port, x86_DEBUG_STATE, (thread_state_t)&state, &count ))
176 #ifdef __x86_64__
177 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
178 state.dsh.flavor == x86_DEBUG_STATE64 );
179 #else
180 assert( state.dsh.flavor == x86_DEBUG_STATE32 );
181 #endif
183 #ifdef __x86_64__
184 if (state.dsh.flavor == x86_DEBUG_STATE64)
186 context->debug.x86_64_regs.dr0 = state.uds.ds64.__dr0;
187 context->debug.x86_64_regs.dr1 = state.uds.ds64.__dr1;
188 context->debug.x86_64_regs.dr2 = state.uds.ds64.__dr2;
189 context->debug.x86_64_regs.dr3 = state.uds.ds64.__dr3;
190 context->debug.x86_64_regs.dr6 = state.uds.ds64.__dr6;
191 context->debug.x86_64_regs.dr7 = state.uds.ds64.__dr7;
193 else
194 #endif
196 /* work around silly renaming of struct members in OS X 10.5 */
197 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
198 context->debug.i386_regs.dr0 = state.uds.ds32.__dr0;
199 context->debug.i386_regs.dr1 = state.uds.ds32.__dr1;
200 context->debug.i386_regs.dr2 = state.uds.ds32.__dr2;
201 context->debug.i386_regs.dr3 = state.uds.ds32.__dr3;
202 context->debug.i386_regs.dr6 = state.uds.ds32.__dr6;
203 context->debug.i386_regs.dr7 = state.uds.ds32.__dr7;
204 #else
205 context->debug.i386_regs.dr0 = state.uds.ds32.dr0;
206 context->debug.i386_regs.dr1 = state.uds.ds32.dr1;
207 context->debug.i386_regs.dr2 = state.uds.ds32.dr2;
208 context->debug.i386_regs.dr3 = state.uds.ds32.dr3;
209 context->debug.i386_regs.dr6 = state.uds.ds32.dr6;
210 context->debug.i386_regs.dr7 = state.uds.ds32.dr7;
211 #endif
213 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
215 mach_port_deallocate( mach_task_self(), port );
216 #endif
219 /* set the thread x86 registers */
220 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
222 #if defined(__i386__) || defined(__x86_64__)
223 x86_debug_state_t state;
224 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
225 mach_msg_type_name_t type;
226 mach_port_t port, process_port = get_process_port( thread->process );
227 unsigned int dr7;
229 /* all other regs are handled on the client side */
230 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
232 if (thread->unix_pid == -1 || !process_port ||
233 mach_port_extract_right( process_port, thread->unix_tid,
234 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
236 set_error( STATUS_ACCESS_DENIED );
237 return;
241 #ifdef __x86_64__
242 if (thread->process->machine == IMAGE_FILE_MACHINE_AMD64)
244 /* Mac OS doesn't allow setting the global breakpoint flags */
245 dr7 = (context->debug.x86_64_regs.dr7 & ~0xaa) | ((context->debug.x86_64_regs.dr7 & 0xaa) >> 1);
247 state.dsh.flavor = x86_DEBUG_STATE64;
248 state.dsh.count = sizeof(state.uds.ds64) / sizeof(int);
249 state.uds.ds64.__dr0 = context->debug.x86_64_regs.dr0;
250 state.uds.ds64.__dr1 = context->debug.x86_64_regs.dr1;
251 state.uds.ds64.__dr2 = context->debug.x86_64_regs.dr2;
252 state.uds.ds64.__dr3 = context->debug.x86_64_regs.dr3;
253 state.uds.ds64.__dr4 = 0;
254 state.uds.ds64.__dr5 = 0;
255 state.uds.ds64.__dr6 = context->debug.x86_64_regs.dr6;
256 state.uds.ds64.__dr7 = dr7;
258 else
259 #endif
261 dr7 = (context->debug.i386_regs.dr7 & ~0xaa) | ((context->debug.i386_regs.dr7 & 0xaa) >> 1);
263 state.dsh.flavor = x86_DEBUG_STATE32;
264 state.dsh.count = sizeof(state.uds.ds32) / sizeof(int);
265 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
266 state.uds.ds32.__dr0 = context->debug.i386_regs.dr0;
267 state.uds.ds32.__dr1 = context->debug.i386_regs.dr1;
268 state.uds.ds32.__dr2 = context->debug.i386_regs.dr2;
269 state.uds.ds32.__dr3 = context->debug.i386_regs.dr3;
270 state.uds.ds32.__dr4 = 0;
271 state.uds.ds32.__dr5 = 0;
272 state.uds.ds32.__dr6 = context->debug.i386_regs.dr6;
273 state.uds.ds32.__dr7 = dr7;
274 #else
275 state.uds.ds32.dr0 = context->debug.i386_regs.dr0;
276 state.uds.ds32.dr1 = context->debug.i386_regs.dr1;
277 state.uds.ds32.dr2 = context->debug.i386_regs.dr2;
278 state.uds.ds32.dr3 = context->debug.i386_regs.dr3;
279 state.uds.ds32.dr4 = 0;
280 state.uds.ds32.dr5 = 0;
281 state.uds.ds32.dr6 = context->debug.i386_regs.dr6;
282 state.uds.ds32.dr7 = dr7;
283 #endif
285 thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count );
286 mach_port_deallocate( mach_task_self(), port );
287 #endif
290 int send_thread_signal( struct thread *thread, int sig )
292 int ret = -1;
293 mach_port_t process_port = get_process_port( thread->process );
295 if (thread->unix_pid != -1 && process_port)
297 mach_msg_type_name_t type;
298 mach_port_t port;
300 if (!mach_port_extract_right( process_port, thread->unix_tid,
301 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
303 ret = syscall( SYS___pthread_kill, port, sig );
304 mach_port_deallocate( mach_task_self(), port );
306 else errno = ESRCH;
308 if (ret == -1 && errno == ESRCH) /* thread got killed */
310 thread->unix_pid = -1;
311 thread->unix_tid = -1;
314 if (debug_level && ret != -1)
315 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
316 return (ret != -1);
319 /* read data from a process memory space */
320 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
322 kern_return_t ret;
323 mach_msg_type_number_t bytes_read;
324 mach_vm_offset_t offset;
325 vm_offset_t data;
326 mach_vm_address_t aligned_address;
327 mach_vm_size_t aligned_size;
328 unsigned int page_size = get_page_size();
329 mach_port_t process_port = get_process_port( process );
331 if (!process_port)
333 set_error( STATUS_ACCESS_DENIED );
334 return 0;
336 if ((mach_vm_address_t)ptr != ptr)
338 set_error( STATUS_ACCESS_DENIED );
339 return 0;
342 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
344 mach_set_error( ret );
345 return 0;
348 offset = ptr % page_size;
349 aligned_address = (mach_vm_address_t)(ptr - offset);
350 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
352 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
353 if (ret != KERN_SUCCESS) mach_set_error( ret );
354 else
356 memcpy( dest, (char *)data + offset, size );
357 mach_vm_deallocate( mach_task_self(), data, bytes_read );
359 task_resume( process_port );
360 return (ret == KERN_SUCCESS);
363 /* write data to a process memory space */
364 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
366 kern_return_t ret;
367 mach_vm_address_t aligned_address, region_address;
368 mach_vm_size_t aligned_size, region_size;
369 mach_msg_type_number_t info_size, bytes_read;
370 mach_vm_offset_t offset;
371 vm_offset_t task_mem = 0;
372 struct vm_region_basic_info_64 info;
373 mach_port_t dummy;
374 unsigned int page_size = get_page_size();
375 mach_port_t process_port = get_process_port( process );
377 if (!process_port)
379 set_error( STATUS_ACCESS_DENIED );
380 return 0;
382 if ((mach_vm_address_t)ptr != ptr)
384 set_error( STATUS_ACCESS_DENIED );
385 return 0;
388 offset = ptr % page_size;
389 aligned_address = (mach_vm_address_t)(ptr - offset);
390 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
392 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
394 mach_set_error( ret );
395 return 0;
398 ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
399 if (ret != KERN_SUCCESS)
401 mach_set_error( ret );
402 goto failed;
404 region_address = aligned_address;
405 info_size = sizeof(info);
406 ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
407 (vm_region_info_t)&info, &info_size, &dummy );
408 if (ret != KERN_SUCCESS)
410 mach_set_error( ret );
411 goto failed;
413 if (region_address > aligned_address ||
414 region_address + region_size < aligned_address + aligned_size)
416 /* FIXME: should support multiple regions */
417 set_error( ERROR_ACCESS_DENIED );
418 goto failed;
420 ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
421 if (ret != KERN_SUCCESS)
423 mach_set_error( ret );
424 goto failed;
427 /* FIXME: there's an optimization that can be made: check first and last */
428 /* pages for writability; read first and last pages; write interior */
429 /* pages to task without ever reading&modifying them; if that succeeds, */
430 /* modify first and last pages and write them. */
432 memcpy( (char*)task_mem + offset, src, size );
434 ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
435 if (ret != KERN_SUCCESS) mach_set_error( ret );
436 else
438 mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
439 /* restore protection */
440 mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
441 task_resume( process_port );
442 return 1;
445 failed:
446 if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
447 task_resume( process_port );
448 return 0;
451 /* retrieve an LDT selector entry */
452 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
453 unsigned int *limit, unsigned char *flags )
455 const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
456 struct process *process = thread->process;
457 unsigned int page_size = get_page_size();
458 vm_offset_t data;
459 kern_return_t ret;
460 mach_msg_type_number_t bytes_read;
461 mach_port_t process_port = get_process_port( thread->process );
463 if (!process->ldt_copy || !process_port)
465 set_error( STATUS_ACCESS_DENIED );
466 return;
468 if (entry >= 8192)
470 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
471 return;
474 if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
476 mach_vm_offset_t offset = process->ldt_copy % page_size;
477 mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
478 mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
480 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
481 if (ret != KERN_SUCCESS) mach_set_error( ret );
482 else
484 const int *ldt = (const int *)((char *)data + offset);
485 memcpy( base, ldt + entry, sizeof(int) );
486 memcpy( limit, ldt + entry + 8192, sizeof(int) );
487 memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
488 mach_vm_deallocate( mach_task_self(), data, bytes_read );
490 task_resume( process_port );
492 else mach_set_error( ret );
495 #endif /* USE_MACH */