mach.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Server-side debugger support using Mach primitives
  3. *
  4. * Copyright (C) 1999, 2006 Alexandre Julliard
  5. * Copyright (C) 2006 Ken Thomases for CodeWeavers
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include "config.h"
  22. #include "wine/port.h"
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <signal.h>
  27. #include <stdarg.h>
  28. #include <sys/types.h>
  29. #include <unistd.h>
  30. #ifdef HAVE_SYS_SYSCALL_H
  31. #include <sys/syscall.h>
  32. #endif
  33. #include "ntstatus.h"
  34. #define WIN32_NO_STATUS
  35. #include "winternl.h"
  36. #include "file.h"
  37. #include "process.h"
  38. #include "thread.h"
  39. #include "request.h"
  40. #ifdef USE_MACH
  41. #include <mach/mach.h>
  42. #include <mach/mach_error.h>
  43. #include <mach/thread_act.h>
  44. #include <mach/mach_vm.h>
  45. #include <servers/bootstrap.h>
  46. static mach_port_t server_mach_port;
  47. void sigchld_callback(void)
  48. {
  49. assert(0); /* should never be called on MacOS */
  50. }
  51. static void mach_set_error(kern_return_t mach_error)
  52. {
  53. switch (mach_error)
  54. {
  55. case KERN_SUCCESS: break;
  56. case KERN_INVALID_ARGUMENT: set_error(STATUS_INVALID_PARAMETER); break;
  57. case KERN_NO_SPACE: set_error(STATUS_NO_MEMORY); break;
  58. case KERN_PROTECTION_FAILURE: set_error(STATUS_ACCESS_DENIED); break;
  59. case KERN_INVALID_ADDRESS: set_error(STATUS_ACCESS_VIOLATION); break;
  60. default: set_error(STATUS_UNSUCCESSFUL); break;
  61. }
  62. }
  63. static mach_port_t get_process_port( struct process *process )
  64. {
  65. return process->trace_data;
  66. }
  67. /* initialize the process control mechanism */
  68. void init_tracing_mechanism(void)
  69. {
  70. mach_port_t bp;
  71. if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
  72. fatal_error("Can't find bootstrap port\n");
  73. if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
  74. fatal_error("Can't allocate port\n");
  75. if (mach_port_insert_right( mach_task_self(),
  76. server_mach_port,
  77. server_mach_port,
  78. MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
  79. fatal_error("Error inserting rights\n");
  80. if (bootstrap_register(bp, server_dir, server_mach_port) != KERN_SUCCESS)
  81. fatal_error("Can't check in server_mach_port\n");
  82. mach_port_deallocate(mach_task_self(), bp);
  83. }
  84. /* initialize the per-process tracing mechanism */
  85. void init_process_tracing( struct process *process )
  86. {
  87. int pid, ret;
  88. struct
  89. {
  90. mach_msg_header_t header;
  91. mach_msg_body_t body;
  92. mach_msg_port_descriptor_t task_port;
  93. mach_msg_trailer_t trailer; /* only present on receive */
  94. } msg;
  95. for (;;)
  96. {
  97. ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
  98. server_mach_port, 0, 0 );
  99. if (ret)
  100. {
  101. if (ret != MACH_RCV_TIMED_OUT && debug_level)
  102. fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
  103. return;
  104. }
  105. /* if anything in the message is invalid, ignore it */
  106. if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
  107. if (msg.body.msgh_descriptor_count != 1) continue;
  108. if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
  109. if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
  110. if (msg.task_port.name == MACH_PORT_NULL) continue;
  111. if (msg.task_port.name == MACH_PORT_DEAD) continue;
  112. if (!pid_for_task( msg.task_port.name, &pid ))
  113. {
  114. struct thread *thread = get_thread_from_pid( pid );
  115. if (thread && !thread->process->trace_data)
  116. thread->process->trace_data = msg.task_port.name;
  117. else
  118. mach_port_deallocate( mach_task_self(), msg.task_port.name );
  119. }
  120. }
  121. }
  122. /* terminate the per-process tracing mechanism */
  123. void finish_process_tracing( struct process *process )
  124. {
  125. if (process->trace_data)
  126. {
  127. mach_port_deallocate( mach_task_self(), process->trace_data );
  128. process->trace_data = 0;
  129. }
  130. }
  131. /* initialize registers in new thread if necessary */
  132. void init_thread_context( struct thread *thread )
  133. {
  134. }
  135. /* retrieve the thread x86 registers */
  136. void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
  137. {
  138. #if defined(__i386__) || defined(__x86_64__)
  139. x86_debug_state_t state;
  140. mach_msg_type_number_t count = sizeof(state) / sizeof(int);
  141. mach_msg_type_name_t type;
  142. mach_port_t port, process_port = get_process_port( thread->process );
  143. /* all other regs are handled on the client side */
  144. assert( flags == SERVER_CTX_DEBUG_REGISTERS );
  145. if (thread->unix_pid == -1 || !process_port ||
  146. mach_port_extract_right( process_port, thread->unix_tid,
  147. MACH_MSG_TYPE_COPY_SEND, &port, &type ))
  148. {
  149. set_error( STATUS_ACCESS_DENIED );
  150. return;
  151. }
  152. if (!thread_get_state( port, x86_DEBUG_STATE, (thread_state_t)&state, &count ))
  153. {
  154. #ifdef __x86_64__
  155. assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
  156. state.dsh.flavor == x86_DEBUG_STATE64 );
  157. #else
  158. assert( state.dsh.flavor == x86_DEBUG_STATE32 );
  159. #endif
  160. #ifdef __x86_64__
  161. if (state.dsh.flavor == x86_DEBUG_STATE64)
  162. {
  163. context->debug.x86_64_regs.dr0 = state.uds.ds64.__dr0;
  164. context->debug.x86_64_regs.dr1 = state.uds.ds64.__dr1;
  165. context->debug.x86_64_regs.dr2 = state.uds.ds64.__dr2;
  166. context->debug.x86_64_regs.dr3 = state.uds.ds64.__dr3;
  167. context->debug.x86_64_regs.dr6 = state.uds.ds64.__dr6;
  168. context->debug.x86_64_regs.dr7 = state.uds.ds64.__dr7;
  169. }
  170. else
  171. #endif
  172. {
  173. /* work around silly renaming of struct members in OS X 10.5 */
  174. #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
  175. context->debug.i386_regs.dr0 = state.uds.ds32.__dr0;
  176. context->debug.i386_regs.dr1 = state.uds.ds32.__dr1;
  177. context->debug.i386_regs.dr2 = state.uds.ds32.__dr2;
  178. context->debug.i386_regs.dr3 = state.uds.ds32.__dr3;
  179. context->debug.i386_regs.dr6 = state.uds.ds32.__dr6;
  180. context->debug.i386_regs.dr7 = state.uds.ds32.__dr7;
  181. #else
  182. context->debug.i386_regs.dr0 = state.uds.ds32.dr0;
  183. context->debug.i386_regs.dr1 = state.uds.ds32.dr1;
  184. context->debug.i386_regs.dr2 = state.uds.ds32.dr2;
  185. context->debug.i386_regs.dr3 = state.uds.ds32.dr3;
  186. context->debug.i386_regs.dr6 = state.uds.ds32.dr6;
  187. context->debug.i386_regs.dr7 = state.uds.ds32.dr7;
  188. #endif
  189. }
  190. context->flags |= SERVER_CTX_DEBUG_REGISTERS;
  191. }
  192. mach_port_deallocate( mach_task_self(), port );
  193. #endif
  194. }
  195. /* set the thread x86 registers */
  196. void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
  197. {
  198. #if defined(__i386__) || defined(__x86_64__)
  199. x86_debug_state_t state;
  200. mach_msg_type_number_t count = sizeof(state) / sizeof(int);
  201. mach_msg_type_name_t type;
  202. mach_port_t port, process_port = get_process_port( thread->process );
  203. unsigned int dr7;
  204. /* all other regs are handled on the client side */
  205. assert( flags == SERVER_CTX_DEBUG_REGISTERS );
  206. if (thread->unix_pid == -1 || !process_port ||
  207. mach_port_extract_right( process_port, thread->unix_tid,
  208. MACH_MSG_TYPE_COPY_SEND, &port, &type ))
  209. {
  210. set_error( STATUS_ACCESS_DENIED );
  211. return;
  212. }
  213. #ifdef __x86_64__
  214. if (thread->process->cpu == CPU_x86_64)
  215. {
  216. /* Mac OS doesn't allow setting the global breakpoint flags */
  217. dr7 = (context->debug.x86_64_regs.dr7 & ~0xaa) | ((context->debug.x86_64_regs.dr7 & 0xaa) >> 1);
  218. state.dsh.flavor = x86_DEBUG_STATE64;
  219. state.dsh.count = sizeof(state.uds.ds64) / sizeof(int);
  220. state.uds.ds64.__dr0 = context->debug.x86_64_regs.dr0;
  221. state.uds.ds64.__dr1 = context->debug.x86_64_regs.dr1;
  222. state.uds.ds64.__dr2 = context->debug.x86_64_regs.dr2;
  223. state.uds.ds64.__dr3 = context->debug.x86_64_regs.dr3;
  224. state.uds.ds64.__dr4 = 0;
  225. state.uds.ds64.__dr5 = 0;
  226. state.uds.ds64.__dr6 = context->debug.x86_64_regs.dr6;
  227. state.uds.ds64.__dr7 = dr7;
  228. }
  229. else
  230. #endif
  231. {
  232. dr7 = (context->debug.i386_regs.dr7 & ~0xaa) | ((context->debug.i386_regs.dr7 & 0xaa) >> 1);
  233. state.dsh.flavor = x86_DEBUG_STATE32;
  234. state.dsh.count = sizeof(state.uds.ds32) / sizeof(int);
  235. #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
  236. state.uds.ds32.__dr0 = context->debug.i386_regs.dr0;
  237. state.uds.ds32.__dr1 = context->debug.i386_regs.dr1;
  238. state.uds.ds32.__dr2 = context->debug.i386_regs.dr2;
  239. state.uds.ds32.__dr3 = context->debug.i386_regs.dr3;
  240. state.uds.ds32.__dr4 = 0;
  241. state.uds.ds32.__dr5 = 0;
  242. state.uds.ds32.__dr6 = context->debug.i386_regs.dr6;
  243. state.uds.ds32.__dr7 = dr7;
  244. #else
  245. state.uds.ds32.dr0 = context->debug.i386_regs.dr0;
  246. state.uds.ds32.dr1 = context->debug.i386_regs.dr1;
  247. state.uds.ds32.dr2 = context->debug.i386_regs.dr2;
  248. state.uds.ds32.dr3 = context->debug.i386_regs.dr3;
  249. state.uds.ds32.dr4 = 0;
  250. state.uds.ds32.dr5 = 0;
  251. state.uds.ds32.dr6 = context->debug.i386_regs.dr6;
  252. state.uds.ds32.dr7 = dr7;
  253. #endif
  254. }
  255. thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count );
  256. mach_port_deallocate( mach_task_self(), port );
  257. #endif
  258. }
  259. int send_thread_signal( struct thread *thread, int sig )
  260. {
  261. int ret = -1;
  262. mach_port_t process_port = get_process_port( thread->process );
  263. if (thread->unix_pid != -1 && process_port)
  264. {
  265. mach_msg_type_name_t type;
  266. mach_port_t port;
  267. if (!mach_port_extract_right( process_port, thread->unix_tid,
  268. MACH_MSG_TYPE_COPY_SEND, &port, &type ))
  269. {
  270. ret = syscall( SYS___pthread_kill, port, sig );
  271. mach_port_deallocate( mach_task_self(), port );
  272. }
  273. else errno = ESRCH;
  274. if (ret == -1 && errno == ESRCH) /* thread got killed */
  275. {
  276. thread->unix_pid = -1;
  277. thread->unix_tid = -1;
  278. }
  279. }
  280. if (debug_level && ret != -1)
  281. fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
  282. return (ret != -1);
  283. }
  284. /* read data from a process memory space */
  285. int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
  286. {
  287. kern_return_t ret;
  288. mach_msg_type_number_t bytes_read;
  289. mach_vm_offset_t offset;
  290. vm_offset_t data;
  291. mach_vm_address_t aligned_address;
  292. mach_vm_size_t aligned_size;
  293. unsigned int page_size = get_page_size();
  294. mach_port_t process_port = get_process_port( process );
  295. if (!process_port)
  296. {
  297. set_error( STATUS_ACCESS_DENIED );
  298. return 0;
  299. }
  300. if ((mach_vm_address_t)ptr != ptr)
  301. {
  302. set_error( STATUS_ACCESS_DENIED );
  303. return 0;
  304. }
  305. if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
  306. {
  307. mach_set_error( ret );
  308. return 0;
  309. }
  310. offset = ptr % page_size;
  311. aligned_address = (mach_vm_address_t)(ptr - offset);
  312. aligned_size = (size + offset + page_size - 1) / page_size * page_size;
  313. ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
  314. if (ret != KERN_SUCCESS) mach_set_error( ret );
  315. else
  316. {
  317. memcpy( dest, (char *)data + offset, size );
  318. mach_vm_deallocate( mach_task_self(), data, bytes_read );
  319. }
  320. task_resume( process_port );
  321. return (ret == KERN_SUCCESS);
  322. }
  323. /* write data to a process memory space */
  324. int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
  325. {
  326. kern_return_t ret;
  327. mach_vm_address_t aligned_address, region_address;
  328. mach_vm_size_t aligned_size, region_size;
  329. mach_msg_type_number_t info_size, bytes_read;
  330. mach_vm_offset_t offset;
  331. vm_offset_t task_mem = 0;
  332. struct vm_region_basic_info_64 info;
  333. mach_port_t dummy;
  334. unsigned int page_size = get_page_size();
  335. mach_port_t process_port = get_process_port( process );
  336. if (!process_port)
  337. {
  338. set_error( STATUS_ACCESS_DENIED );
  339. return 0;
  340. }
  341. if ((mach_vm_address_t)ptr != ptr)
  342. {
  343. set_error( STATUS_ACCESS_DENIED );
  344. return 0;
  345. }
  346. offset = ptr % page_size;
  347. aligned_address = (mach_vm_address_t)(ptr - offset);
  348. aligned_size = (size + offset + page_size - 1) / page_size * page_size;
  349. if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
  350. {
  351. mach_set_error( ret );
  352. return 0;
  353. }
  354. ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
  355. if (ret != KERN_SUCCESS)
  356. {
  357. mach_set_error( ret );
  358. goto failed;
  359. }
  360. region_address = aligned_address;
  361. info_size = sizeof(info);
  362. ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
  363. (vm_region_info_t)&info, &info_size, &dummy );
  364. if (ret != KERN_SUCCESS)
  365. {
  366. mach_set_error( ret );
  367. goto failed;
  368. }
  369. if (region_address > aligned_address ||
  370. region_address + region_size < aligned_address + aligned_size)
  371. {
  372. /* FIXME: should support multiple regions */
  373. set_error( ERROR_ACCESS_DENIED );
  374. goto failed;
  375. }
  376. ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
  377. if (ret != KERN_SUCCESS)
  378. {
  379. mach_set_error( ret );
  380. goto failed;
  381. }
  382. /* FIXME: there's an optimization that can be made: check first and last */
  383. /* pages for writability; read first and last pages; write interior */
  384. /* pages to task without ever reading&modifying them; if that succeeds, */
  385. /* modify first and last pages and write them. */
  386. memcpy( (char*)task_mem + offset, src, size );
  387. ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
  388. if (ret != KERN_SUCCESS) mach_set_error( ret );
  389. else
  390. {
  391. mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
  392. /* restore protection */
  393. mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
  394. task_resume( process_port );
  395. return 1;
  396. }
  397. failed:
  398. if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
  399. task_resume( process_port );
  400. return 0;
  401. }
  402. /* retrieve an LDT selector entry */
  403. void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
  404. unsigned int *limit, unsigned char *flags )
  405. {
  406. const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
  407. struct process *process = thread->process;
  408. unsigned int page_size = get_page_size();
  409. vm_offset_t data;
  410. kern_return_t ret;
  411. mach_msg_type_number_t bytes_read;
  412. mach_port_t process_port = get_process_port( thread->process );
  413. if (!process->ldt_copy || !process_port)
  414. {
  415. set_error( STATUS_ACCESS_DENIED );
  416. return;
  417. }
  418. if (entry >= 8192)
  419. {
  420. set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
  421. return;
  422. }
  423. if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
  424. {
  425. mach_vm_offset_t offset = process->ldt_copy % page_size;
  426. mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
  427. mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
  428. ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
  429. if (ret != KERN_SUCCESS) mach_set_error( ret );
  430. else
  431. {
  432. const int *ldt = (const int *)((char *)data + offset);
  433. memcpy( base, ldt + entry, sizeof(int) );
  434. memcpy( limit, ldt + entry + 8192, sizeof(int) );
  435. memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
  436. mach_vm_deallocate( mach_task_self(), data, bytes_read );
  437. }
  438. task_resume( process_port );
  439. }
  440. else mach_set_error( ret );
  441. }
  442. #endif /* USE_MACH */