mfreadwrite/reader: Keep the output subtypes when propagating media types.
[wine.git] / server / mach.c
blob86ff7eac38226dcc5ec54f835e5c8f838aa7489b
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_SYSCTL_H
32 #include <sys/sysctl.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 static int is_rosetta( void )
79 static int rosetta_status, did_check = 0;
80 if (!did_check)
82 /* returns 0 for native process or on error, 1 for translated */
83 int ret = 0;
84 size_t size = sizeof(ret);
85 if (sysctlbyname( "sysctl.proc_translated", &ret, &size, NULL, 0 ) == -1)
86 rosetta_status = 0;
87 else
88 rosetta_status = ret;
90 did_check = 1;
93 return rosetta_status;
96 extern kern_return_t bootstrap_register2( mach_port_t bp, name_t service_name, mach_port_t sp, uint64_t flags );
98 /* initialize the process control mechanism */
99 void init_tracing_mechanism(void)
101 mach_port_t bp;
103 if (task_get_bootstrap_port( mach_task_self(), &bp ) != KERN_SUCCESS)
104 fatal_error( "Can't find bootstrap port\n" );
105 if (mach_port_allocate( mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port ) != KERN_SUCCESS)
106 fatal_error( "Can't allocate port\n" );
107 if (mach_port_insert_right( mach_task_self(),
108 server_mach_port,
109 server_mach_port,
110 MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
111 fatal_error( "Error inserting rights\n" );
112 if (bootstrap_register2( bp, server_dir, server_mach_port, 0 ) != KERN_SUCCESS)
113 fatal_error( "Can't check in server_mach_port\n" );
114 mach_port_deallocate( mach_task_self(), bp );
117 /* initialize the per-process tracing mechanism */
118 void init_process_tracing( struct process *process )
120 int pid, ret;
121 struct
123 mach_msg_header_t header;
124 mach_msg_body_t body;
125 mach_msg_port_descriptor_t task_port;
126 mach_msg_trailer_t trailer; /* only present on receive */
127 } msg;
129 for (;;)
131 ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
132 server_mach_port, 0, 0 );
133 if (ret)
135 if (ret != MACH_RCV_TIMED_OUT && debug_level)
136 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
137 return;
140 /* if anything in the message is invalid, ignore it */
141 if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
142 if (msg.body.msgh_descriptor_count != 1) continue;
143 if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
144 if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
145 if (msg.task_port.name == MACH_PORT_NULL) continue;
146 if (msg.task_port.name == MACH_PORT_DEAD) continue;
148 if (!pid_for_task( msg.task_port.name, &pid ))
150 struct thread *thread = get_thread_from_pid( pid );
152 if (thread && !thread->process->trace_data)
153 thread->process->trace_data = msg.task_port.name;
154 else
155 mach_port_deallocate( mach_task_self(), msg.task_port.name );
160 /* terminate the per-process tracing mechanism */
161 void finish_process_tracing( struct process *process )
163 if (process->trace_data)
165 mach_port_deallocate( mach_task_self(), process->trace_data );
166 process->trace_data = 0;
170 /* initialize registers in new thread if necessary */
171 void init_thread_context( struct thread *thread )
175 /* retrieve the thread x86 registers */
176 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
178 #if defined(__i386__) || defined(__x86_64__)
179 x86_debug_state_t state;
180 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
181 mach_msg_type_name_t type;
182 mach_port_t port, process_port = get_process_port( thread->process );
183 kern_return_t ret;
184 unsigned long dr[8];
186 /* all other regs are handled on the client side */
187 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
189 if (is_rosetta())
191 /* getting debug registers of a translated process is not supported cross-process, return all zeroes */
192 memset( &context->debug, 0, sizeof(context->debug) );
193 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
194 return;
197 if (thread->unix_pid == -1 || !process_port ||
198 mach_port_extract_right( process_port, thread->unix_tid,
199 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
201 set_error( STATUS_ACCESS_DENIED );
202 return;
205 ret = thread_get_state( port, x86_DEBUG_STATE, (thread_state_t)&state, &count );
206 if (!ret)
208 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
209 state.dsh.flavor == x86_DEBUG_STATE64 );
211 if (state.dsh.flavor == x86_DEBUG_STATE64)
213 dr[0] = state.uds.ds64.__dr0;
214 dr[1] = state.uds.ds64.__dr1;
215 dr[2] = state.uds.ds64.__dr2;
216 dr[3] = state.uds.ds64.__dr3;
217 dr[6] = state.uds.ds64.__dr6;
218 dr[7] = state.uds.ds64.__dr7;
220 else
222 dr[0] = state.uds.ds32.__dr0;
223 dr[1] = state.uds.ds32.__dr1;
224 dr[2] = state.uds.ds32.__dr2;
225 dr[3] = state.uds.ds32.__dr3;
226 dr[6] = state.uds.ds32.__dr6;
227 dr[7] = state.uds.ds32.__dr7;
230 switch (context->machine)
232 case IMAGE_FILE_MACHINE_I386:
233 context->debug.i386_regs.dr0 = dr[0];
234 context->debug.i386_regs.dr1 = dr[1];
235 context->debug.i386_regs.dr2 = dr[2];
236 context->debug.i386_regs.dr3 = dr[3];
237 context->debug.i386_regs.dr6 = dr[6];
238 context->debug.i386_regs.dr7 = dr[7];
239 break;
240 case IMAGE_FILE_MACHINE_AMD64:
241 context->debug.x86_64_regs.dr0 = dr[0];
242 context->debug.x86_64_regs.dr1 = dr[1];
243 context->debug.x86_64_regs.dr2 = dr[2];
244 context->debug.x86_64_regs.dr3 = dr[3];
245 context->debug.x86_64_regs.dr6 = dr[6];
246 context->debug.x86_64_regs.dr7 = dr[7];
247 break;
248 default:
249 set_error( STATUS_INVALID_PARAMETER );
250 goto done;
252 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
254 else
255 mach_set_error( ret );
256 done:
257 mach_port_deallocate( mach_task_self(), port );
258 #endif
261 /* set the thread x86 registers */
262 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
264 #if defined(__i386__) || defined(__x86_64__)
265 x86_debug_state_t state;
266 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
267 mach_msg_type_name_t type;
268 mach_port_t port, process_port = get_process_port( thread->process );
269 unsigned long dr[8];
270 kern_return_t ret;
272 /* all other regs are handled on the client side */
273 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
275 if (is_rosetta())
277 /* Setting debug registers of a translated process is not supported cross-process
278 * (and even in-process, setting debug registers never has the desired effect).
280 set_error( STATUS_UNSUCCESSFUL );
281 return;
284 if (thread->unix_pid == -1 || !process_port ||
285 mach_port_extract_right( process_port, thread->unix_tid,
286 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
288 set_error( STATUS_ACCESS_DENIED );
289 return;
292 /* get the debug state to determine which flavor to use */
293 ret = thread_get_state(port, x86_DEBUG_STATE, (thread_state_t)&state, &count);
294 if (ret)
296 mach_set_error( ret );
297 goto done;
299 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
300 state.dsh.flavor == x86_DEBUG_STATE64 );
302 switch (context->machine)
304 case IMAGE_FILE_MACHINE_I386:
305 dr[0] = context->debug.i386_regs.dr0;
306 dr[1] = context->debug.i386_regs.dr1;
307 dr[2] = context->debug.i386_regs.dr2;
308 dr[3] = context->debug.i386_regs.dr3;
309 dr[6] = context->debug.i386_regs.dr6;
310 dr[7] = context->debug.i386_regs.dr7;
311 break;
312 case IMAGE_FILE_MACHINE_AMD64:
313 dr[0] = context->debug.x86_64_regs.dr0;
314 dr[1] = context->debug.x86_64_regs.dr1;
315 dr[2] = context->debug.x86_64_regs.dr2;
316 dr[3] = context->debug.x86_64_regs.dr3;
317 dr[6] = context->debug.x86_64_regs.dr6;
318 dr[7] = context->debug.x86_64_regs.dr7;
319 break;
320 default:
321 set_error( STATUS_INVALID_PARAMETER );
322 goto done;
325 /* Mac OS doesn't allow setting the global breakpoint flags */
326 dr[7] = (dr[7] & ~0xaa) | ((dr[7] & 0xaa) >> 1);
328 if (state.dsh.flavor == x86_DEBUG_STATE64)
330 state.dsh.count = sizeof(state.uds.ds64) / sizeof(int);
331 state.uds.ds64.__dr0 = dr[0];
332 state.uds.ds64.__dr1 = dr[1];
333 state.uds.ds64.__dr2 = dr[2];
334 state.uds.ds64.__dr3 = dr[3];
335 state.uds.ds64.__dr4 = 0;
336 state.uds.ds64.__dr5 = 0;
337 state.uds.ds64.__dr6 = dr[6];
338 state.uds.ds64.__dr7 = dr[7];
340 else
342 state.dsh.count = sizeof(state.uds.ds32) / sizeof(int);
343 state.uds.ds32.__dr0 = dr[0];
344 state.uds.ds32.__dr1 = dr[1];
345 state.uds.ds32.__dr2 = dr[2];
346 state.uds.ds32.__dr3 = dr[3];
347 state.uds.ds32.__dr4 = 0;
348 state.uds.ds32.__dr5 = 0;
349 state.uds.ds32.__dr6 = dr[6];
350 state.uds.ds32.__dr7 = dr[7];
352 ret = thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count );
353 if (ret)
354 mach_set_error( ret );
355 done:
356 mach_port_deallocate( mach_task_self(), port );
357 #endif
360 extern int __pthread_kill( mach_port_t, int );
362 int send_thread_signal( struct thread *thread, int sig )
364 int ret = -1;
365 mach_port_t process_port = get_process_port( thread->process );
367 if (thread->unix_pid != -1 && process_port)
369 mach_msg_type_name_t type;
370 mach_port_t port;
372 if (!mach_port_extract_right( process_port, thread->unix_tid,
373 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
375 ret = __pthread_kill( port, sig );
376 mach_port_deallocate( mach_task_self(), port );
378 else errno = ESRCH;
380 if (ret == -1 && errno == ESRCH) /* thread got killed */
382 thread->unix_pid = -1;
383 thread->unix_tid = -1;
386 if (debug_level && ret != -1)
387 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
388 return (ret != -1);
391 /* read data from a process memory space */
392 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
394 kern_return_t ret;
395 mach_msg_type_number_t bytes_read;
396 mach_vm_offset_t offset;
397 vm_offset_t data;
398 mach_vm_address_t aligned_address;
399 mach_vm_size_t aligned_size;
400 unsigned int page_size = get_page_size();
401 mach_port_t process_port = get_process_port( process );
403 if (!process_port)
405 set_error( STATUS_ACCESS_DENIED );
406 return 0;
408 if ((mach_vm_address_t)ptr != ptr)
410 set_error( STATUS_ACCESS_DENIED );
411 return 0;
414 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
416 mach_set_error( ret );
417 return 0;
420 offset = ptr % page_size;
421 aligned_address = (mach_vm_address_t)(ptr - offset);
422 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
424 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
425 if (ret != KERN_SUCCESS) mach_set_error( ret );
426 else
428 memcpy( dest, (char *)data + offset, size );
429 mach_vm_deallocate( mach_task_self(), data, bytes_read );
431 task_resume( process_port );
432 return (ret == KERN_SUCCESS);
435 /* write data to a process memory space */
436 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
438 kern_return_t ret;
439 mach_vm_address_t aligned_address, region_address;
440 mach_vm_size_t aligned_size, region_size;
441 mach_msg_type_number_t info_size, bytes_read;
442 mach_vm_offset_t offset;
443 vm_offset_t task_mem = 0;
444 struct vm_region_basic_info_64 info;
445 mach_port_t dummy;
446 unsigned int page_size = get_page_size();
447 mach_port_t process_port = get_process_port( process );
449 if (!process_port)
451 set_error( STATUS_ACCESS_DENIED );
452 return 0;
454 if ((mach_vm_address_t)ptr != ptr)
456 set_error( STATUS_ACCESS_DENIED );
457 return 0;
460 offset = ptr % page_size;
461 aligned_address = (mach_vm_address_t)(ptr - offset);
462 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
464 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
466 mach_set_error( ret );
467 return 0;
470 ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
471 if (ret != KERN_SUCCESS)
473 mach_set_error( ret );
474 goto failed;
476 region_address = aligned_address;
477 info_size = sizeof(info);
478 ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
479 (vm_region_info_t)&info, &info_size, &dummy );
480 if (ret != KERN_SUCCESS)
482 mach_set_error( ret );
483 goto failed;
485 if (region_address > aligned_address ||
486 region_address + region_size < aligned_address + aligned_size)
488 /* FIXME: should support multiple regions */
489 set_error( ERROR_ACCESS_DENIED );
490 goto failed;
492 ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
493 if (ret != KERN_SUCCESS)
495 mach_set_error( ret );
496 goto failed;
499 /* FIXME: there's an optimization that can be made: check first and last */
500 /* pages for writability; read first and last pages; write interior */
501 /* pages to task without ever reading&modifying them; if that succeeds, */
502 /* modify first and last pages and write them. */
504 memcpy( (char*)task_mem + offset, src, size );
506 ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
507 if (ret != KERN_SUCCESS) mach_set_error( ret );
508 else
510 mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
511 /* restore protection */
512 mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
513 task_resume( process_port );
514 return 1;
517 failed:
518 if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
519 task_resume( process_port );
520 return 0;
523 /* retrieve an LDT selector entry */
524 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
525 unsigned int *limit, unsigned char *flags )
527 const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
528 struct process *process = thread->process;
529 unsigned int page_size = get_page_size();
530 vm_offset_t data;
531 kern_return_t ret;
532 mach_msg_type_number_t bytes_read;
533 mach_port_t process_port = get_process_port( thread->process );
535 if (!process->ldt_copy || !process_port)
537 set_error( STATUS_ACCESS_DENIED );
538 return;
540 if (entry >= 8192)
542 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
543 return;
546 if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
548 mach_vm_offset_t offset = process->ldt_copy % page_size;
549 mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
550 mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
552 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
553 if (ret != KERN_SUCCESS) mach_set_error( ret );
554 else
556 const int *ldt = (const int *)((char *)data + offset);
557 memcpy( base, ldt + entry, sizeof(int) );
558 memcpy( limit, ldt + entry + 8192, sizeof(int) );
559 memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
560 mach_vm_deallocate( mach_task_self(), data, bytes_read );
562 task_resume( process_port );
564 else mach_set_error( ret );
567 #endif /* USE_MACH */