mach.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. if (!thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count ))
  256. {
  257. if (thread->context) /* update the cached values */
  258. {
  259. #ifdef __x86_64__
  260. if (thread->process->cpu == CPU_x86_64)
  261. thread->context->debug.x86_64_regs = context->debug.x86_64_regs;
  262. else
  263. #endif
  264. thread->context->debug.i386_regs = context->debug.i386_regs;
  265. }
  266. }
  267. mach_port_deallocate( mach_task_self(), port );
  268. #endif
  269. }
  270. int send_thread_signal( struct thread *thread, int sig )
  271. {
  272. int ret = -1;
  273. mach_port_t process_port = get_process_port( thread->process );
  274. if (thread->unix_pid != -1 && process_port)
  275. {
  276. mach_msg_type_name_t type;
  277. mach_port_t port;
  278. if (!mach_port_extract_right( process_port, thread->unix_tid,
  279. MACH_MSG_TYPE_COPY_SEND, &port, &type ))
  280. {
  281. ret = syscall( SYS___pthread_kill, port, sig );
  282. mach_port_deallocate( mach_task_self(), port );
  283. }
  284. else errno = ESRCH;
  285. if (ret == -1 && errno == ESRCH) /* thread got killed */
  286. {
  287. thread->unix_pid = -1;
  288. thread->unix_tid = -1;
  289. }
  290. }
  291. if (debug_level && ret != -1)
  292. fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
  293. return (ret != -1);
  294. }
  295. /* read data from a process memory space */
  296. int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
  297. {
  298. kern_return_t ret;
  299. mach_msg_type_number_t bytes_read;
  300. mach_vm_offset_t offset;
  301. vm_offset_t data;
  302. mach_vm_address_t aligned_address;
  303. mach_vm_size_t aligned_size;
  304. unsigned int page_size = get_page_size();
  305. mach_port_t process_port = get_process_port( process );
  306. if (!process_port)
  307. {
  308. set_error( STATUS_ACCESS_DENIED );
  309. return 0;
  310. }
  311. if ((mach_vm_address_t)ptr != ptr)
  312. {
  313. set_error( STATUS_ACCESS_DENIED );
  314. return 0;
  315. }
  316. if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
  317. {
  318. mach_set_error( ret );
  319. return 0;
  320. }
  321. offset = ptr % page_size;
  322. aligned_address = (mach_vm_address_t)(ptr - offset);
  323. aligned_size = (size + offset + page_size - 1) / page_size * page_size;
  324. ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
  325. if (ret != KERN_SUCCESS) mach_set_error( ret );
  326. else
  327. {
  328. memcpy( dest, (char *)data + offset, size );
  329. mach_vm_deallocate( mach_task_self(), data, bytes_read );
  330. }
  331. task_resume( process_port );
  332. return (ret == KERN_SUCCESS);
  333. }
  334. /* write data to a process memory space */
  335. int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
  336. {
  337. kern_return_t ret;
  338. mach_vm_address_t aligned_address, region_address;
  339. mach_vm_size_t aligned_size, region_size;
  340. mach_msg_type_number_t info_size, bytes_read;
  341. mach_vm_offset_t offset;
  342. vm_offset_t task_mem = 0;
  343. struct vm_region_basic_info_64 info;
  344. mach_port_t dummy;
  345. unsigned int page_size = get_page_size();
  346. mach_port_t process_port = get_process_port( process );
  347. if (!process_port)
  348. {
  349. set_error( STATUS_ACCESS_DENIED );
  350. return 0;
  351. }
  352. if ((mach_vm_address_t)ptr != ptr)
  353. {
  354. set_error( STATUS_ACCESS_DENIED );
  355. return 0;
  356. }
  357. offset = ptr % page_size;
  358. aligned_address = (mach_vm_address_t)(ptr - offset);
  359. aligned_size = (size + offset + page_size - 1) / page_size * page_size;
  360. if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
  361. {
  362. mach_set_error( ret );
  363. return 0;
  364. }
  365. ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
  366. if (ret != KERN_SUCCESS)
  367. {
  368. mach_set_error( ret );
  369. goto failed;
  370. }
  371. region_address = aligned_address;
  372. info_size = sizeof(info);
  373. ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
  374. (vm_region_info_t)&info, &info_size, &dummy );
  375. if (ret != KERN_SUCCESS)
  376. {
  377. mach_set_error( ret );
  378. goto failed;
  379. }
  380. if (region_address > aligned_address ||
  381. region_address + region_size < aligned_address + aligned_size)
  382. {
  383. /* FIXME: should support multiple regions */
  384. set_error( ERROR_ACCESS_DENIED );
  385. goto failed;
  386. }
  387. ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
  388. if (ret != KERN_SUCCESS)
  389. {
  390. mach_set_error( ret );
  391. goto failed;
  392. }
  393. /* FIXME: there's an optimization that can be made: check first and last */
  394. /* pages for writability; read first and last pages; write interior */
  395. /* pages to task without ever reading&modifying them; if that succeeds, */
  396. /* modify first and last pages and write them. */
  397. memcpy( (char*)task_mem + offset, src, size );
  398. ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
  399. if (ret != KERN_SUCCESS) mach_set_error( ret );
  400. else
  401. {
  402. mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
  403. /* restore protection */
  404. mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
  405. task_resume( process_port );
  406. return 1;
  407. }
  408. failed:
  409. if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
  410. task_resume( process_port );
  411. return 0;
  412. }
  413. /* retrieve an LDT selector entry */
  414. void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
  415. unsigned int *limit, unsigned char *flags )
  416. {
  417. const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
  418. struct process *process = thread->process;
  419. unsigned int page_size = get_page_size();
  420. vm_offset_t data;
  421. kern_return_t ret;
  422. mach_msg_type_number_t bytes_read;
  423. mach_port_t process_port = get_process_port( thread->process );
  424. if (!process->ldt_copy || !process_port)
  425. {
  426. set_error( STATUS_ACCESS_DENIED );
  427. return;
  428. }
  429. if (entry >= 8192)
  430. {
  431. set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
  432. return;
  433. }
  434. if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
  435. {
  436. mach_vm_offset_t offset = process->ldt_copy % page_size;
  437. mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
  438. mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
  439. ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
  440. if (ret != KERN_SUCCESS) mach_set_error( ret );
  441. else
  442. {
  443. const int *ldt = (const int *)((char *)data + offset);
  444. memcpy( base, ldt + entry, sizeof(int) );
  445. memcpy( limit, ldt + entry + 8192, sizeof(int) );
  446. memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
  447. mach_vm_deallocate( mach_task_self(), data, bytes_read );
  448. }
  449. task_resume( process_port );
  450. }
  451. else mach_set_error( ret );
  452. }
  453. #endif /* USE_MACH */