user32: Introduce get_class_info.
[wine.git] / server / mach.c
blobd31e4025776d723e29d2a3c4c6353970a372a734
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 context->debug.i386_regs.dr0 = state.uds.ds32.__dr0;
197 context->debug.i386_regs.dr1 = state.uds.ds32.__dr1;
198 context->debug.i386_regs.dr2 = state.uds.ds32.__dr2;
199 context->debug.i386_regs.dr3 = state.uds.ds32.__dr3;
200 context->debug.i386_regs.dr6 = state.uds.ds32.__dr6;
201 context->debug.i386_regs.dr7 = state.uds.ds32.__dr7;
203 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
205 mach_port_deallocate( mach_task_self(), port );
206 #endif
209 /* set the thread x86 registers */
210 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
212 #if defined(__i386__) || defined(__x86_64__)
213 x86_debug_state_t state;
214 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
215 mach_msg_type_name_t type;
216 mach_port_t port, process_port = get_process_port( thread->process );
217 unsigned int dr7;
219 /* all other regs are handled on the client side */
220 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
222 if (thread->unix_pid == -1 || !process_port ||
223 mach_port_extract_right( process_port, thread->unix_tid,
224 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
226 set_error( STATUS_ACCESS_DENIED );
227 return;
231 #ifdef __x86_64__
232 if (thread->process->machine == IMAGE_FILE_MACHINE_AMD64)
234 /* Mac OS doesn't allow setting the global breakpoint flags */
235 dr7 = (context->debug.x86_64_regs.dr7 & ~0xaa) | ((context->debug.x86_64_regs.dr7 & 0xaa) >> 1);
237 state.dsh.flavor = x86_DEBUG_STATE64;
238 state.dsh.count = sizeof(state.uds.ds64) / sizeof(int);
239 state.uds.ds64.__dr0 = context->debug.x86_64_regs.dr0;
240 state.uds.ds64.__dr1 = context->debug.x86_64_regs.dr1;
241 state.uds.ds64.__dr2 = context->debug.x86_64_regs.dr2;
242 state.uds.ds64.__dr3 = context->debug.x86_64_regs.dr3;
243 state.uds.ds64.__dr4 = 0;
244 state.uds.ds64.__dr5 = 0;
245 state.uds.ds64.__dr6 = context->debug.x86_64_regs.dr6;
246 state.uds.ds64.__dr7 = dr7;
248 else
249 #endif
251 dr7 = (context->debug.i386_regs.dr7 & ~0xaa) | ((context->debug.i386_regs.dr7 & 0xaa) >> 1);
253 state.dsh.flavor = x86_DEBUG_STATE32;
254 state.dsh.count = sizeof(state.uds.ds32) / sizeof(int);
255 state.uds.ds32.__dr0 = context->debug.i386_regs.dr0;
256 state.uds.ds32.__dr1 = context->debug.i386_regs.dr1;
257 state.uds.ds32.__dr2 = context->debug.i386_regs.dr2;
258 state.uds.ds32.__dr3 = context->debug.i386_regs.dr3;
259 state.uds.ds32.__dr4 = 0;
260 state.uds.ds32.__dr5 = 0;
261 state.uds.ds32.__dr6 = context->debug.i386_regs.dr6;
262 state.uds.ds32.__dr7 = dr7;
264 thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count );
265 mach_port_deallocate( mach_task_self(), port );
266 #endif
269 int send_thread_signal( struct thread *thread, int sig )
271 int ret = -1;
272 mach_port_t process_port = get_process_port( thread->process );
274 if (thread->unix_pid != -1 && process_port)
276 mach_msg_type_name_t type;
277 mach_port_t port;
279 if (!mach_port_extract_right( process_port, thread->unix_tid,
280 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
282 ret = syscall( SYS___pthread_kill, port, sig );
283 mach_port_deallocate( mach_task_self(), port );
285 else errno = ESRCH;
287 if (ret == -1 && errno == ESRCH) /* thread got killed */
289 thread->unix_pid = -1;
290 thread->unix_tid = -1;
293 if (debug_level && ret != -1)
294 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
295 return (ret != -1);
298 /* read data from a process memory space */
299 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
301 kern_return_t ret;
302 mach_msg_type_number_t bytes_read;
303 mach_vm_offset_t offset;
304 vm_offset_t data;
305 mach_vm_address_t aligned_address;
306 mach_vm_size_t aligned_size;
307 unsigned int page_size = get_page_size();
308 mach_port_t process_port = get_process_port( process );
310 if (!process_port)
312 set_error( STATUS_ACCESS_DENIED );
313 return 0;
315 if ((mach_vm_address_t)ptr != ptr)
317 set_error( STATUS_ACCESS_DENIED );
318 return 0;
321 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
323 mach_set_error( ret );
324 return 0;
327 offset = ptr % page_size;
328 aligned_address = (mach_vm_address_t)(ptr - offset);
329 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
331 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
332 if (ret != KERN_SUCCESS) mach_set_error( ret );
333 else
335 memcpy( dest, (char *)data + offset, size );
336 mach_vm_deallocate( mach_task_self(), data, bytes_read );
338 task_resume( process_port );
339 return (ret == KERN_SUCCESS);
342 /* write data to a process memory space */
343 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
345 kern_return_t ret;
346 mach_vm_address_t aligned_address, region_address;
347 mach_vm_size_t aligned_size, region_size;
348 mach_msg_type_number_t info_size, bytes_read;
349 mach_vm_offset_t offset;
350 vm_offset_t task_mem = 0;
351 struct vm_region_basic_info_64 info;
352 mach_port_t dummy;
353 unsigned int page_size = get_page_size();
354 mach_port_t process_port = get_process_port( process );
356 if (!process_port)
358 set_error( STATUS_ACCESS_DENIED );
359 return 0;
361 if ((mach_vm_address_t)ptr != ptr)
363 set_error( STATUS_ACCESS_DENIED );
364 return 0;
367 offset = ptr % page_size;
368 aligned_address = (mach_vm_address_t)(ptr - offset);
369 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
371 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
373 mach_set_error( ret );
374 return 0;
377 ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
378 if (ret != KERN_SUCCESS)
380 mach_set_error( ret );
381 goto failed;
383 region_address = aligned_address;
384 info_size = sizeof(info);
385 ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
386 (vm_region_info_t)&info, &info_size, &dummy );
387 if (ret != KERN_SUCCESS)
389 mach_set_error( ret );
390 goto failed;
392 if (region_address > aligned_address ||
393 region_address + region_size < aligned_address + aligned_size)
395 /* FIXME: should support multiple regions */
396 set_error( ERROR_ACCESS_DENIED );
397 goto failed;
399 ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
400 if (ret != KERN_SUCCESS)
402 mach_set_error( ret );
403 goto failed;
406 /* FIXME: there's an optimization that can be made: check first and last */
407 /* pages for writability; read first and last pages; write interior */
408 /* pages to task without ever reading&modifying them; if that succeeds, */
409 /* modify first and last pages and write them. */
411 memcpy( (char*)task_mem + offset, src, size );
413 ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
414 if (ret != KERN_SUCCESS) mach_set_error( ret );
415 else
417 mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
418 /* restore protection */
419 mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
420 task_resume( process_port );
421 return 1;
424 failed:
425 if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
426 task_resume( process_port );
427 return 0;
430 /* retrieve an LDT selector entry */
431 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
432 unsigned int *limit, unsigned char *flags )
434 const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
435 struct process *process = thread->process;
436 unsigned int page_size = get_page_size();
437 vm_offset_t data;
438 kern_return_t ret;
439 mach_msg_type_number_t bytes_read;
440 mach_port_t process_port = get_process_port( thread->process );
442 if (!process->ldt_copy || !process_port)
444 set_error( STATUS_ACCESS_DENIED );
445 return;
447 if (entry >= 8192)
449 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
450 return;
453 if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
455 mach_vm_offset_t offset = process->ldt_copy % page_size;
456 mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
457 mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
459 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
460 if (ret != KERN_SUCCESS) mach_set_error( ret );
461 else
463 const int *ldt = (const int *)((char *)data + offset);
464 memcpy( base, ldt + entry, sizeof(int) );
465 memcpy( limit, ldt + entry + 8192, sizeof(int) );
466 memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
467 mach_vm_deallocate( mach_task_self(), data, bytes_read );
469 task_resume( process_port );
471 else mach_set_error( ret );
474 #endif /* USE_MACH */