mach.c 16 KB

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