dinput/tests: Remove unmatched winetest_pop_context().
[wine.git] / server / mach.c
blob3852bb3ee5bba9bb1a3a4df5a9c124824f10ccb8
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
34 #ifdef HAVE_SYS_SYSCTL_H
35 #include <sys/sysctl.h>
36 #endif
38 #include "ntstatus.h"
39 #define WIN32_NO_STATUS
40 #include "winternl.h"
42 #include "file.h"
43 #include "process.h"
44 #include "thread.h"
45 #include "request.h"
47 #ifdef USE_MACH
49 #include <mach/mach.h>
50 #include <mach/mach_error.h>
51 #include <mach/thread_act.h>
52 #include <mach/mach_vm.h>
53 #include <servers/bootstrap.h>
55 static mach_port_t server_mach_port;
57 void sigchld_callback(void)
59 assert(0); /* should never be called on MacOS */
62 static void mach_set_error(kern_return_t mach_error)
64 switch (mach_error)
66 case KERN_SUCCESS: break;
67 case KERN_INVALID_ARGUMENT: set_error(STATUS_INVALID_PARAMETER); break;
68 case KERN_NO_SPACE: set_error(STATUS_NO_MEMORY); break;
69 case KERN_PROTECTION_FAILURE: set_error(STATUS_ACCESS_DENIED); break;
70 case KERN_INVALID_ADDRESS: set_error(STATUS_ACCESS_VIOLATION); break;
71 default: set_error(STATUS_UNSUCCESSFUL); break;
75 static mach_port_t get_process_port( struct process *process )
77 return process->trace_data;
80 static int is_rosetta( void )
82 static int rosetta_status, did_check = 0;
83 if (!did_check)
85 /* returns 0 for native process or on error, 1 for translated */
86 int ret = 0;
87 size_t size = sizeof(ret);
88 if (sysctlbyname( "sysctl.proc_translated", &ret, &size, NULL, 0 ) == -1)
89 rosetta_status = 0;
90 else
91 rosetta_status = ret;
93 did_check = 1;
96 return rosetta_status;
99 /* initialize the process control mechanism */
100 void init_tracing_mechanism(void)
102 mach_port_t bp;
104 if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
105 fatal_error("Can't find bootstrap port\n");
106 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
107 fatal_error("Can't allocate port\n");
108 if (mach_port_insert_right( mach_task_self(),
109 server_mach_port,
110 server_mach_port,
111 MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
112 fatal_error("Error inserting rights\n");
113 if (bootstrap_register(bp, server_dir, server_mach_port) != KERN_SUCCESS)
114 fatal_error("Can't check in server_mach_port\n");
115 mach_port_deallocate(mach_task_self(), bp);
118 /* initialize the per-process tracing mechanism */
119 void init_process_tracing( struct process *process )
121 int pid, ret;
122 struct
124 mach_msg_header_t header;
125 mach_msg_body_t body;
126 mach_msg_port_descriptor_t task_port;
127 mach_msg_trailer_t trailer; /* only present on receive */
128 } msg;
130 for (;;)
132 ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
133 server_mach_port, 0, 0 );
134 if (ret)
136 if (ret != MACH_RCV_TIMED_OUT && debug_level)
137 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
138 return;
141 /* if anything in the message is invalid, ignore it */
142 if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
143 if (msg.body.msgh_descriptor_count != 1) continue;
144 if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
145 if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
146 if (msg.task_port.name == MACH_PORT_NULL) continue;
147 if (msg.task_port.name == MACH_PORT_DEAD) continue;
149 if (!pid_for_task( msg.task_port.name, &pid ))
151 struct thread *thread = get_thread_from_pid( pid );
153 if (thread && !thread->process->trace_data)
154 thread->process->trace_data = msg.task_port.name;
155 else
156 mach_port_deallocate( mach_task_self(), msg.task_port.name );
161 /* terminate the per-process tracing mechanism */
162 void finish_process_tracing( struct process *process )
164 if (process->trace_data)
166 mach_port_deallocate( mach_task_self(), process->trace_data );
167 process->trace_data = 0;
171 /* initialize registers in new thread if necessary */
172 void init_thread_context( struct thread *thread )
176 /* retrieve the thread x86 registers */
177 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
179 #if defined(__i386__) || defined(__x86_64__)
180 x86_debug_state_t state;
181 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
182 mach_msg_type_name_t type;
183 mach_port_t port, process_port = get_process_port( thread->process );
184 kern_return_t ret;
185 unsigned long dr[8];
187 /* all other regs are handled on the client side */
188 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
190 if (is_rosetta())
192 /* getting debug registers of a translated process is not supported cross-process, return all zeroes */
193 memset( &context->debug, 0, sizeof(context->debug) );
194 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
195 return;
198 if (thread->unix_pid == -1 || !process_port ||
199 mach_port_extract_right( process_port, thread->unix_tid,
200 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
202 set_error( STATUS_ACCESS_DENIED );
203 return;
206 ret = thread_get_state( port, x86_DEBUG_STATE, (thread_state_t)&state, &count );
207 if (!ret)
209 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
210 state.dsh.flavor == x86_DEBUG_STATE64 );
212 if (state.dsh.flavor == x86_DEBUG_STATE64)
214 dr[0] = state.uds.ds64.__dr0;
215 dr[1] = state.uds.ds64.__dr1;
216 dr[2] = state.uds.ds64.__dr2;
217 dr[3] = state.uds.ds64.__dr3;
218 dr[6] = state.uds.ds64.__dr6;
219 dr[7] = state.uds.ds64.__dr7;
221 else
223 dr[0] = state.uds.ds32.__dr0;
224 dr[1] = state.uds.ds32.__dr1;
225 dr[2] = state.uds.ds32.__dr2;
226 dr[3] = state.uds.ds32.__dr3;
227 dr[6] = state.uds.ds32.__dr6;
228 dr[7] = state.uds.ds32.__dr7;
231 switch (context->machine)
233 case IMAGE_FILE_MACHINE_I386:
234 context->debug.i386_regs.dr0 = dr[0];
235 context->debug.i386_regs.dr1 = dr[1];
236 context->debug.i386_regs.dr2 = dr[2];
237 context->debug.i386_regs.dr3 = dr[3];
238 context->debug.i386_regs.dr6 = dr[6];
239 context->debug.i386_regs.dr7 = dr[7];
240 break;
241 case IMAGE_FILE_MACHINE_AMD64:
242 context->debug.x86_64_regs.dr0 = dr[0];
243 context->debug.x86_64_regs.dr1 = dr[1];
244 context->debug.x86_64_regs.dr2 = dr[2];
245 context->debug.x86_64_regs.dr3 = dr[3];
246 context->debug.x86_64_regs.dr6 = dr[6];
247 context->debug.x86_64_regs.dr7 = dr[7];
248 break;
249 default:
250 set_error( STATUS_INVALID_PARAMETER );
251 goto done;
253 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
255 else
256 mach_set_error( ret );
257 done:
258 mach_port_deallocate( mach_task_self(), port );
259 #endif
262 /* set the thread x86 registers */
263 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
265 #if defined(__i386__) || defined(__x86_64__)
266 x86_debug_state_t state;
267 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
268 mach_msg_type_name_t type;
269 mach_port_t port, process_port = get_process_port( thread->process );
270 unsigned long dr[8];
271 kern_return_t ret;
273 /* all other regs are handled on the client side */
274 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
276 if (thread->unix_pid == -1 || !process_port ||
277 mach_port_extract_right( process_port, thread->unix_tid,
278 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
280 set_error( STATUS_ACCESS_DENIED );
281 return;
284 if (is_rosetta())
286 /* Setting debug registers of a translated process is not supported cross-process
287 * (and even in-process, setting debug registers never has the desired effect).
289 set_error( STATUS_UNSUCCESSFUL );
290 return;
293 /* get the debug state to determine which flavor to use */
294 ret = thread_get_state(port, x86_DEBUG_STATE, (thread_state_t)&state, &count);
295 if (ret)
297 mach_set_error( ret );
298 goto done;
300 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
301 state.dsh.flavor == x86_DEBUG_STATE64 );
303 switch (context->machine)
305 case IMAGE_FILE_MACHINE_I386:
306 dr[0] = context->debug.i386_regs.dr0;
307 dr[1] = context->debug.i386_regs.dr1;
308 dr[2] = context->debug.i386_regs.dr2;
309 dr[3] = context->debug.i386_regs.dr3;
310 dr[6] = context->debug.i386_regs.dr6;
311 dr[7] = context->debug.i386_regs.dr7;
312 break;
313 case IMAGE_FILE_MACHINE_AMD64:
314 dr[0] = context->debug.x86_64_regs.dr0;
315 dr[1] = context->debug.x86_64_regs.dr1;
316 dr[2] = context->debug.x86_64_regs.dr2;
317 dr[3] = context->debug.x86_64_regs.dr3;
318 dr[6] = context->debug.x86_64_regs.dr6;
319 dr[7] = context->debug.x86_64_regs.dr7;
320 break;
321 default:
322 set_error( STATUS_INVALID_PARAMETER );
323 goto done;
326 /* Mac OS doesn't allow setting the global breakpoint flags */
327 dr[7] = (dr[7] & ~0xaa) | ((dr[7] & 0xaa) >> 1);
329 if (state.dsh.flavor == x86_DEBUG_STATE64)
331 state.dsh.count = sizeof(state.uds.ds64) / sizeof(int);
332 state.uds.ds64.__dr0 = dr[0];
333 state.uds.ds64.__dr1 = dr[1];
334 state.uds.ds64.__dr2 = dr[2];
335 state.uds.ds64.__dr3 = dr[3];
336 state.uds.ds64.__dr4 = 0;
337 state.uds.ds64.__dr5 = 0;
338 state.uds.ds64.__dr6 = dr[6];
339 state.uds.ds64.__dr7 = dr[7];
341 else
343 state.dsh.count = sizeof(state.uds.ds32) / sizeof(int);
344 state.uds.ds32.__dr0 = dr[0];
345 state.uds.ds32.__dr1 = dr[1];
346 state.uds.ds32.__dr2 = dr[2];
347 state.uds.ds32.__dr3 = dr[3];
348 state.uds.ds32.__dr4 = 0;
349 state.uds.ds32.__dr5 = 0;
350 state.uds.ds32.__dr6 = dr[6];
351 state.uds.ds32.__dr7 = dr[7];
353 ret = thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count );
354 if (ret)
355 mach_set_error( ret );
356 done:
357 mach_port_deallocate( mach_task_self(), port );
358 #endif
361 int send_thread_signal( struct thread *thread, int sig )
363 int ret = -1;
364 mach_port_t process_port = get_process_port( thread->process );
366 if (thread->unix_pid != -1 && process_port)
368 mach_msg_type_name_t type;
369 mach_port_t port;
371 if (!mach_port_extract_right( process_port, thread->unix_tid,
372 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
374 ret = syscall( SYS___pthread_kill, port, sig );
375 mach_port_deallocate( mach_task_self(), port );
377 else errno = ESRCH;
379 if (ret == -1 && errno == ESRCH) /* thread got killed */
381 thread->unix_pid = -1;
382 thread->unix_tid = -1;
385 if (debug_level && ret != -1)
386 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
387 return (ret != -1);
390 /* read data from a process memory space */
391 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
393 kern_return_t ret;
394 mach_msg_type_number_t bytes_read;
395 mach_vm_offset_t offset;
396 vm_offset_t data;
397 mach_vm_address_t aligned_address;
398 mach_vm_size_t aligned_size;
399 unsigned int page_size = get_page_size();
400 mach_port_t process_port = get_process_port( process );
402 if (!process_port)
404 set_error( STATUS_ACCESS_DENIED );
405 return 0;
407 if ((mach_vm_address_t)ptr != ptr)
409 set_error( STATUS_ACCESS_DENIED );
410 return 0;
413 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
415 mach_set_error( ret );
416 return 0;
419 offset = ptr % page_size;
420 aligned_address = (mach_vm_address_t)(ptr - offset);
421 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
423 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
424 if (ret != KERN_SUCCESS) mach_set_error( ret );
425 else
427 memcpy( dest, (char *)data + offset, size );
428 mach_vm_deallocate( mach_task_self(), data, bytes_read );
430 task_resume( process_port );
431 return (ret == KERN_SUCCESS);
434 /* write data to a process memory space */
435 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
437 kern_return_t ret;
438 mach_vm_address_t aligned_address, region_address;
439 mach_vm_size_t aligned_size, region_size;
440 mach_msg_type_number_t info_size, bytes_read;
441 mach_vm_offset_t offset;
442 vm_offset_t task_mem = 0;
443 struct vm_region_basic_info_64 info;
444 mach_port_t dummy;
445 unsigned int page_size = get_page_size();
446 mach_port_t process_port = get_process_port( process );
448 if (!process_port)
450 set_error( STATUS_ACCESS_DENIED );
451 return 0;
453 if ((mach_vm_address_t)ptr != ptr)
455 set_error( STATUS_ACCESS_DENIED );
456 return 0;
459 offset = ptr % page_size;
460 aligned_address = (mach_vm_address_t)(ptr - offset);
461 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
463 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
465 mach_set_error( ret );
466 return 0;
469 ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
470 if (ret != KERN_SUCCESS)
472 mach_set_error( ret );
473 goto failed;
475 region_address = aligned_address;
476 info_size = sizeof(info);
477 ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
478 (vm_region_info_t)&info, &info_size, &dummy );
479 if (ret != KERN_SUCCESS)
481 mach_set_error( ret );
482 goto failed;
484 if (region_address > aligned_address ||
485 region_address + region_size < aligned_address + aligned_size)
487 /* FIXME: should support multiple regions */
488 set_error( ERROR_ACCESS_DENIED );
489 goto failed;
491 ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
492 if (ret != KERN_SUCCESS)
494 mach_set_error( ret );
495 goto failed;
498 /* FIXME: there's an optimization that can be made: check first and last */
499 /* pages for writability; read first and last pages; write interior */
500 /* pages to task without ever reading&modifying them; if that succeeds, */
501 /* modify first and last pages and write them. */
503 memcpy( (char*)task_mem + offset, src, size );
505 ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
506 if (ret != KERN_SUCCESS) mach_set_error( ret );
507 else
509 mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
510 /* restore protection */
511 mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
512 task_resume( process_port );
513 return 1;
516 failed:
517 if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
518 task_resume( process_port );
519 return 0;
522 /* retrieve an LDT selector entry */
523 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
524 unsigned int *limit, unsigned char *flags )
526 const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
527 struct process *process = thread->process;
528 unsigned int page_size = get_page_size();
529 vm_offset_t data;
530 kern_return_t ret;
531 mach_msg_type_number_t bytes_read;
532 mach_port_t process_port = get_process_port( thread->process );
534 if (!process->ldt_copy || !process_port)
536 set_error( STATUS_ACCESS_DENIED );
537 return;
539 if (entry >= 8192)
541 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
542 return;
545 if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
547 mach_vm_offset_t offset = process->ldt_copy % page_size;
548 mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
549 mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
551 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
552 if (ret != KERN_SUCCESS) mach_set_error( ret );
553 else
555 const int *ldt = (const int *)((char *)data + offset);
556 memcpy( base, ldt + entry, sizeof(int) );
557 memcpy( limit, ldt + entry + 8192, sizeof(int) );
558 memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
559 mach_vm_deallocate( mach_task_self(), data, bytes_read );
561 task_resume( process_port );
563 else mach_set_error( ret );
566 #endif /* USE_MACH */