gdi32: Add a structure to store all the extra information needed for a pattern brush.
[wine.git] / server / mach.c
blob03c88ec6561d83589c953ea98595453422228ed7
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"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #ifdef HAVE_SYS_SYSCALL_H
33 #include <sys/syscall.h>
34 #endif
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "winternl.h"
40 #include "file.h"
41 #include "process.h"
42 #include "thread.h"
43 #include "request.h"
44 #include "wine/library.h"
46 #ifdef USE_MACH
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)
62 switch (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)
81 mach_port_t bp;
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(),
88 server_mach_port,
89 server_mach_port,
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 )
100 int pid, ret;
101 struct
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 */
107 } msg;
109 for (;;)
111 ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
112 server_mach_port, 0, 0 );
113 if (ret)
115 if (ret != MACH_RCV_TIMED_OUT && debug_level)
116 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
117 return;
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;
134 else
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 )
153 #ifdef __i386__
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 );
167 return;
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;
180 #else
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;
187 #endif
188 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
190 mach_port_deallocate( mach_task_self(), port );
191 #endif
194 /* set the thread x86 registers */
195 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
197 #ifdef __i386__
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 );
202 unsigned int dr7;
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 );
212 return;
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;
223 state.__dr4 = 0;
224 state.__dr5 = 0;
225 state.__dr6 = context->debug.i386_regs.dr6;
226 state.__dr7 = dr7;
227 #else
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;
232 state.dr4 = 0;
233 state.dr5 = 0;
234 state.dr6 = context->debug.i386_regs.dr6;
235 state.dr7 = dr7;
236 #endif
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 );
243 #endif
246 int send_thread_signal( struct thread *thread, int sig )
248 int ret = -1;
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;
254 mach_port_t port;
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 );
262 else errno = ESRCH;
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 );
272 return (ret != -1);
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 )
278 kern_return_t ret;
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 );
286 if (!process_port)
288 set_error( STATUS_ACCESS_DENIED );
289 return 0;
291 if ((vm_address_t)ptr != ptr)
293 set_error( STATUS_ACCESS_DENIED );
294 return 0;
297 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
299 mach_set_error( ret );
300 return 0;
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 );
309 else
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 )
321 kern_return_t ret;
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;
327 mach_port_t dummy;
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 ((vm_address_t)ptr != ptr)
338 set_error( STATUS_ACCESS_DENIED );
339 return 0;
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 );
349 return 0;
352 ret = vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
353 if (ret != KERN_SUCCESS)
355 mach_set_error( ret );
356 goto failed;
358 region_address = aligned_address;
359 info_size = sizeof(info);
360 ret = vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO,
361 (vm_region_info_t)&info, &info_size, &dummy );
362 if (ret != KERN_SUCCESS)
364 mach_set_error( ret );
365 goto failed;
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 );
372 goto failed;
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 );
378 goto failed;
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 );
390 else
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 );
396 return 1;
399 failed:
400 if (task_mem) vm_deallocate( mach_task_self(), task_mem, bytes_read );
401 task_resume( process_port );
402 return 0;
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();
412 vm_offset_t data;
413 kern_return_t ret;
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 );
420 return;
422 if (entry >= 8192)
424 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
425 return;
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 );
436 else
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 */