msvcrt: Reimplement _mbsspnp using _mbsspn.
[wine.git] / server / mach.c
blob9d8d911cb7cbb8a99245b93f2f148e286ef694a6
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 <mach/mach_vm.h>
52 #include <servers/bootstrap.h>
54 static mach_port_t server_mach_port;
56 void sigchld_callback(void)
58 assert(0); /* should never be called on MacOS */
61 static void mach_set_error(kern_return_t mach_error)
63 switch (mach_error)
65 case KERN_SUCCESS: break;
66 case KERN_INVALID_ARGUMENT: set_error(STATUS_INVALID_PARAMETER); break;
67 case KERN_NO_SPACE: set_error(STATUS_NO_MEMORY); break;
68 case KERN_PROTECTION_FAILURE: set_error(STATUS_ACCESS_DENIED); break;
69 case KERN_INVALID_ADDRESS: set_error(STATUS_ACCESS_VIOLATION); break;
70 default: set_error(STATUS_UNSUCCESSFUL); break;
74 static mach_port_t get_process_port( struct process *process )
76 return process->trace_data;
79 /* initialize the process control mechanism */
80 void init_tracing_mechanism(void)
82 mach_port_t bp;
84 if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
85 fatal_error("Can't find bootstrap port\n");
86 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
87 fatal_error("Can't allocate port\n");
88 if (mach_port_insert_right( mach_task_self(),
89 server_mach_port,
90 server_mach_port,
91 MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
92 fatal_error("Error inserting rights\n");
93 if (bootstrap_register(bp, (char*)wine_get_server_dir(), server_mach_port) != KERN_SUCCESS)
94 fatal_error("Can't check in server_mach_port\n");
95 mach_port_deallocate(mach_task_self(), bp);
98 /* initialize the per-process tracing mechanism */
99 void init_process_tracing( struct process *process )
101 int pid, ret;
102 struct
104 mach_msg_header_t header;
105 mach_msg_body_t body;
106 mach_msg_port_descriptor_t task_port;
107 mach_msg_trailer_t trailer; /* only present on receive */
108 } msg;
110 for (;;)
112 ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
113 server_mach_port, 0, 0 );
114 if (ret)
116 if (ret != MACH_RCV_TIMED_OUT && debug_level)
117 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
118 return;
121 /* if anything in the message is invalid, ignore it */
122 if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
123 if (msg.body.msgh_descriptor_count != 1) continue;
124 if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
125 if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
126 if (msg.task_port.name == MACH_PORT_NULL) continue;
127 if (msg.task_port.name == MACH_PORT_DEAD) continue;
129 if (!pid_for_task( msg.task_port.name, &pid ))
131 struct thread *thread = get_thread_from_pid( pid );
133 if (thread && !thread->process->trace_data)
134 thread->process->trace_data = msg.task_port.name;
135 else
136 mach_port_deallocate( mach_task_self(), msg.task_port.name );
141 /* terminate the per-process tracing mechanism */
142 void finish_process_tracing( struct process *process )
144 if (process->trace_data)
146 mach_port_deallocate( mach_task_self(), process->trace_data );
147 process->trace_data = 0;
151 /* initialize registers in new thread if necessary */
152 void init_thread_context( struct thread *thread )
156 /* retrieve the thread x86 registers */
157 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
159 #if defined(__i386__) || defined(__x86_64__)
160 x86_debug_state_t state;
161 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
162 mach_msg_type_name_t type;
163 mach_port_t port, process_port = get_process_port( thread->process );
165 /* all other regs are handled on the client side */
166 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
168 if (thread->unix_pid == -1 || !process_port ||
169 mach_port_extract_right( process_port, thread->unix_tid,
170 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
172 set_error( STATUS_ACCESS_DENIED );
173 return;
176 if (!thread_get_state( port, x86_DEBUG_STATE, (thread_state_t)&state, &count ))
178 #ifdef __x86_64__
179 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
180 state.dsh.flavor == x86_DEBUG_STATE64 );
181 #else
182 assert( state.dsh.flavor == x86_DEBUG_STATE32 );
183 #endif
185 #ifdef __x86_64__
186 if (state.dsh.flavor == x86_DEBUG_STATE64)
188 context->debug.x86_64_regs.dr0 = state.uds.ds64.__dr0;
189 context->debug.x86_64_regs.dr1 = state.uds.ds64.__dr1;
190 context->debug.x86_64_regs.dr2 = state.uds.ds64.__dr2;
191 context->debug.x86_64_regs.dr3 = state.uds.ds64.__dr3;
192 context->debug.x86_64_regs.dr6 = state.uds.ds64.__dr6;
193 context->debug.x86_64_regs.dr7 = state.uds.ds64.__dr7;
195 else
196 #endif
198 /* work around silly renaming of struct members in OS X 10.5 */
199 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
200 context->debug.i386_regs.dr0 = state.uds.ds32.__dr0;
201 context->debug.i386_regs.dr1 = state.uds.ds32.__dr1;
202 context->debug.i386_regs.dr2 = state.uds.ds32.__dr2;
203 context->debug.i386_regs.dr3 = state.uds.ds32.__dr3;
204 context->debug.i386_regs.dr6 = state.uds.ds32.__dr6;
205 context->debug.i386_regs.dr7 = state.uds.ds32.__dr7;
206 #else
207 context->debug.i386_regs.dr0 = state.uds.ds32.dr0;
208 context->debug.i386_regs.dr1 = state.uds.ds32.dr1;
209 context->debug.i386_regs.dr2 = state.uds.ds32.dr2;
210 context->debug.i386_regs.dr3 = state.uds.ds32.dr3;
211 context->debug.i386_regs.dr6 = state.uds.ds32.dr6;
212 context->debug.i386_regs.dr7 = state.uds.ds32.dr7;
213 #endif
215 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
217 mach_port_deallocate( mach_task_self(), port );
218 #endif
221 /* set the thread x86 registers */
222 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
224 #if defined(__i386__) || defined(__x86_64__)
225 x86_debug_state_t state;
226 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
227 mach_msg_type_name_t type;
228 mach_port_t port, process_port = get_process_port( thread->process );
229 unsigned int dr7;
231 /* all other regs are handled on the client side */
232 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
234 if (thread->unix_pid == -1 || !process_port ||
235 mach_port_extract_right( process_port, thread->unix_tid,
236 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
238 set_error( STATUS_ACCESS_DENIED );
239 return;
243 #ifdef __x86_64__
244 if (thread->process->cpu == CPU_x86_64)
246 /* Mac OS doesn't allow setting the global breakpoint flags */
247 dr7 = (context->debug.x86_64_regs.dr7 & ~0xaa) | ((context->debug.x86_64_regs.dr7 & 0xaa) >> 1);
249 state.dsh.flavor = x86_DEBUG_STATE64;
250 state.dsh.count = sizeof(state.uds.ds64) / sizeof(int);
251 state.uds.ds64.__dr0 = context->debug.x86_64_regs.dr0;
252 state.uds.ds64.__dr1 = context->debug.x86_64_regs.dr1;
253 state.uds.ds64.__dr2 = context->debug.x86_64_regs.dr2;
254 state.uds.ds64.__dr3 = context->debug.x86_64_regs.dr3;
255 state.uds.ds64.__dr4 = 0;
256 state.uds.ds64.__dr5 = 0;
257 state.uds.ds64.__dr6 = context->debug.x86_64_regs.dr6;
258 state.uds.ds64.__dr7 = dr7;
260 else
261 #endif
263 dr7 = (context->debug.i386_regs.dr7 & ~0xaa) | ((context->debug.i386_regs.dr7 & 0xaa) >> 1);
265 state.dsh.flavor = x86_DEBUG_STATE32;
266 state.dsh.count = sizeof(state.uds.ds32) / sizeof(int);
267 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
268 state.uds.ds32.__dr0 = context->debug.i386_regs.dr0;
269 state.uds.ds32.__dr1 = context->debug.i386_regs.dr1;
270 state.uds.ds32.__dr2 = context->debug.i386_regs.dr2;
271 state.uds.ds32.__dr3 = context->debug.i386_regs.dr3;
272 state.uds.ds32.__dr4 = 0;
273 state.uds.ds32.__dr5 = 0;
274 state.uds.ds32.__dr6 = context->debug.i386_regs.dr6;
275 state.uds.ds32.__dr7 = dr7;
276 #else
277 state.uds.ds32.dr0 = context->debug.i386_regs.dr0;
278 state.uds.ds32.dr1 = context->debug.i386_regs.dr1;
279 state.uds.ds32.dr2 = context->debug.i386_regs.dr2;
280 state.uds.ds32.dr3 = context->debug.i386_regs.dr3;
281 state.uds.ds32.dr4 = 0;
282 state.uds.ds32.dr5 = 0;
283 state.uds.ds32.dr6 = context->debug.i386_regs.dr6;
284 state.uds.ds32.dr7 = dr7;
285 #endif
287 if (!thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count ))
289 if (thread->context) /* update the cached values */
291 #ifdef __x86_64__
292 if (thread->process->cpu == CPU_x86_64)
293 thread->context->debug.x86_64_regs = context->debug.x86_64_regs;
294 else
295 #endif
296 thread->context->debug.i386_regs = context->debug.i386_regs;
299 mach_port_deallocate( mach_task_self(), port );
300 #endif
303 int send_thread_signal( struct thread *thread, int sig )
305 int ret = -1;
306 mach_port_t process_port = get_process_port( thread->process );
308 if (thread->unix_pid != -1 && process_port)
310 mach_msg_type_name_t type;
311 mach_port_t port;
313 if (!mach_port_extract_right( process_port, thread->unix_tid,
314 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
316 ret = syscall( SYS___pthread_kill, port, sig );
317 mach_port_deallocate( mach_task_self(), port );
319 else errno = ESRCH;
321 if (ret == -1 && errno == ESRCH) /* thread got killed */
323 thread->unix_pid = -1;
324 thread->unix_tid = -1;
327 if (debug_level && ret != -1)
328 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
329 return (ret != -1);
332 /* read data from a process memory space */
333 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
335 kern_return_t ret;
336 mach_msg_type_number_t bytes_read;
337 mach_vm_offset_t offset;
338 vm_offset_t data;
339 mach_vm_address_t aligned_address;
340 mach_vm_size_t aligned_size;
341 unsigned int page_size = get_page_size();
342 mach_port_t process_port = get_process_port( process );
344 if (!process_port)
346 set_error( STATUS_ACCESS_DENIED );
347 return 0;
349 if ((mach_vm_address_t)ptr != ptr)
351 set_error( STATUS_ACCESS_DENIED );
352 return 0;
355 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
357 mach_set_error( ret );
358 return 0;
361 offset = ptr % page_size;
362 aligned_address = (mach_vm_address_t)(ptr - offset);
363 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
365 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
366 if (ret != KERN_SUCCESS) mach_set_error( ret );
367 else
369 memcpy( dest, (char *)data + offset, size );
370 mach_vm_deallocate( mach_task_self(), data, bytes_read );
372 task_resume( process_port );
373 return (ret == KERN_SUCCESS);
376 /* write data to a process memory space */
377 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
379 kern_return_t ret;
380 mach_vm_address_t aligned_address, region_address;
381 mach_vm_size_t aligned_size, region_size;
382 mach_msg_type_number_t info_size, bytes_read;
383 mach_vm_offset_t offset;
384 vm_offset_t task_mem = 0;
385 struct vm_region_basic_info_64 info;
386 mach_port_t dummy;
387 unsigned int page_size = get_page_size();
388 mach_port_t process_port = get_process_port( process );
390 if (!process_port)
392 set_error( STATUS_ACCESS_DENIED );
393 return 0;
395 if ((mach_vm_address_t)ptr != ptr)
397 set_error( STATUS_ACCESS_DENIED );
398 return 0;
401 offset = ptr % page_size;
402 aligned_address = (mach_vm_address_t)(ptr - offset);
403 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
405 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
407 mach_set_error( ret );
408 return 0;
411 ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
412 if (ret != KERN_SUCCESS)
414 mach_set_error( ret );
415 goto failed;
417 region_address = aligned_address;
418 info_size = sizeof(info);
419 ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
420 (vm_region_info_t)&info, &info_size, &dummy );
421 if (ret != KERN_SUCCESS)
423 mach_set_error( ret );
424 goto failed;
426 if (region_address > aligned_address ||
427 region_address + region_size < aligned_address + aligned_size)
429 /* FIXME: should support multiple regions */
430 set_error( ERROR_ACCESS_DENIED );
431 goto failed;
433 ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
434 if (ret != KERN_SUCCESS)
436 mach_set_error( ret );
437 goto failed;
440 /* FIXME: there's an optimization that can be made: check first and last */
441 /* pages for writability; read first and last pages; write interior */
442 /* pages to task without ever reading&modifying them; if that succeeds, */
443 /* modify first and last pages and write them. */
445 memcpy( (char*)task_mem + offset, src, size );
447 ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
448 if (ret != KERN_SUCCESS) mach_set_error( ret );
449 else
451 mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
452 /* restore protection */
453 mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
454 task_resume( process_port );
455 return 1;
458 failed:
459 if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
460 task_resume( process_port );
461 return 0;
464 /* retrieve an LDT selector entry */
465 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
466 unsigned int *limit, unsigned char *flags )
468 const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
469 struct process *process = thread->process;
470 unsigned int page_size = get_page_size();
471 vm_offset_t data;
472 kern_return_t ret;
473 mach_msg_type_number_t bytes_read;
474 mach_port_t process_port = get_process_port( thread->process );
476 if (!process->ldt_copy || !process_port)
478 set_error( STATUS_ACCESS_DENIED );
479 return;
481 if (entry >= 8192)
483 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
484 return;
487 if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
489 mach_vm_offset_t offset = process->ldt_copy % page_size;
490 mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
491 mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
493 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
494 if (ret != KERN_SUCCESS) mach_set_error( ret );
495 else
497 const int *ldt = (const int *)((char *)data + offset);
498 memcpy( base, ldt + entry, sizeof(int) );
499 memcpy( limit, ldt + entry + 8192, sizeof(int) );
500 memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
501 mach_vm_deallocate( mach_task_self(), data, bytes_read );
503 task_resume( process_port );
505 else mach_set_error( ret );
508 #endif /* USE_MACH */