ptrace.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Server-side ptrace support
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <signal.h>
  27. #include <stdarg.h>
  28. #include <sys/types.h>
  29. #ifdef HAVE_SYS_PTRACE_H
  30. # include <sys/ptrace.h>
  31. #endif
  32. #ifdef HAVE_SYS_PARAM_H
  33. # include <sys/param.h>
  34. #endif
  35. #ifdef HAVE_SYS_WAIT_H
  36. # include <sys/wait.h>
  37. #endif
  38. #ifdef HAVE_SYS_SYSCALL_H
  39. # include <sys/syscall.h>
  40. #endif
  41. #ifdef HAVE_SYS_UCONTEXT_H
  42. # include <sys/ucontext.h>
  43. #endif
  44. #ifdef HAVE_SYS_THR_H
  45. # include <sys/thr.h>
  46. #endif
  47. #include <unistd.h>
  48. #include "ntstatus.h"
  49. #define WIN32_NO_STATUS
  50. #include "winternl.h"
  51. #include "file.h"
  52. #include "process.h"
  53. #include "thread.h"
  54. #ifdef USE_PTRACE
  55. #ifndef PTRACE_CONT
  56. #define PTRACE_CONT PT_CONTINUE
  57. #endif
  58. #ifndef PTRACE_SINGLESTEP
  59. #define PTRACE_SINGLESTEP PT_STEP
  60. #endif
  61. #ifndef PTRACE_ATTACH
  62. #define PTRACE_ATTACH PT_ATTACH
  63. #endif
  64. #ifndef PTRACE_DETACH
  65. #define PTRACE_DETACH PT_DETACH
  66. #endif
  67. #ifndef PTRACE_PEEKDATA
  68. #define PTRACE_PEEKDATA PT_READ_D
  69. #endif
  70. #ifndef PTRACE_POKEDATA
  71. #define PTRACE_POKEDATA PT_WRITE_D
  72. #endif
  73. #ifndef PTRACE_PEEKUSER
  74. #define PTRACE_PEEKUSER PT_READ_U
  75. #endif
  76. #ifndef PTRACE_POKEUSER
  77. #define PTRACE_POKEUSER PT_WRITE_U
  78. #endif
  79. #ifdef PT_GETDBREGS
  80. #define PTRACE_GETDBREGS PT_GETDBREGS
  81. #endif
  82. #ifdef PT_SETDBREGS
  83. #define PTRACE_SETDBREGS PT_SETDBREGS
  84. #endif
  85. #ifndef HAVE_SYS_PTRACE_H
  86. #define PT_CONTINUE 0
  87. #define PT_ATTACH 1
  88. #define PT_DETACH 2
  89. #define PT_READ_D 3
  90. #define PT_WRITE_D 4
  91. #define PT_STEP 5
  92. static inline int ptrace(int req, ...) { errno = EPERM; return -1; /*FAIL*/ }
  93. #endif /* HAVE_SYS_PTRACE_H */
  94. #ifndef __WALL
  95. #define __WALL 0
  96. #endif
  97. /* handle a status returned by waitpid */
  98. static int handle_child_status( struct thread *thread, int pid, int status, int want_sig )
  99. {
  100. if (WIFSTOPPED(status))
  101. {
  102. int sig = WSTOPSIG(status);
  103. if (debug_level && thread)
  104. fprintf( stderr, "%04x: *signal* signal=%d\n", thread->id, sig );
  105. if (sig != want_sig)
  106. {
  107. /* ignore other signals for now */
  108. ptrace( PTRACE_CONT, pid, (caddr_t)1, sig );
  109. }
  110. return sig;
  111. }
  112. if (thread && (WIFSIGNALED(status) || WIFEXITED(status)))
  113. {
  114. thread->unix_pid = -1;
  115. thread->unix_tid = -1;
  116. if (debug_level)
  117. {
  118. if (WIFSIGNALED(status))
  119. fprintf( stderr, "%04x: *exited* signal=%d\n",
  120. thread->id, WTERMSIG(status) );
  121. else
  122. fprintf( stderr, "%04x: *exited* status=%d\n",
  123. thread->id, WEXITSTATUS(status) );
  124. }
  125. }
  126. return 0;
  127. }
  128. /* handle a SIGCHLD signal */
  129. void sigchld_callback(void)
  130. {
  131. int pid, status;
  132. for (;;)
  133. {
  134. if (!(pid = waitpid( -1, &status, WUNTRACED | WNOHANG | __WALL ))) break;
  135. if (pid != -1)
  136. {
  137. struct thread *thread = get_thread_from_tid( pid );
  138. if (!thread) thread = get_thread_from_pid( pid );
  139. handle_child_status( thread, pid, status, -1 );
  140. }
  141. else break;
  142. }
  143. }
  144. /* return the Unix pid to use in ptrace calls for a given process */
  145. static int get_ptrace_pid( struct thread *thread )
  146. {
  147. #ifdef linux /* linux always uses thread id */
  148. if (thread->unix_tid != -1) return thread->unix_tid;
  149. #endif
  150. return thread->unix_pid;
  151. }
  152. /* return the Unix tid to use in ptrace calls for a given thread */
  153. static int get_ptrace_tid( struct thread *thread )
  154. {
  155. if (thread->unix_tid != -1) return thread->unix_tid;
  156. return thread->unix_pid;
  157. }
  158. /* wait for a ptraced child to get a certain signal */
  159. static int waitpid_thread( struct thread *thread, int signal )
  160. {
  161. int res, status;
  162. start_watchdog();
  163. for (;;)
  164. {
  165. if ((res = waitpid( get_ptrace_pid(thread), &status, WUNTRACED | __WALL )) == -1)
  166. {
  167. if (errno == EINTR)
  168. {
  169. if (!watchdog_triggered()) continue;
  170. if (debug_level) fprintf( stderr, "%04x: *watchdog* waitpid aborted\n", thread->id );
  171. }
  172. else if (errno == ECHILD) /* must have died */
  173. {
  174. thread->unix_pid = -1;
  175. thread->unix_tid = -1;
  176. }
  177. else perror( "waitpid" );
  178. stop_watchdog();
  179. return 0;
  180. }
  181. res = handle_child_status( thread, res, status, signal );
  182. if (!res || res == signal) break;
  183. }
  184. stop_watchdog();
  185. return (thread->unix_pid != -1);
  186. }
  187. /* send a signal to a specific thread */
  188. static inline int tkill( int tgid, int pid, int sig )
  189. {
  190. #ifdef __linux__
  191. int ret = syscall( __NR_tgkill, tgid, pid, sig );
  192. if (ret < 0 && errno == ENOSYS) ret = syscall( __NR_tkill, pid, sig );
  193. return ret;
  194. #elif (defined(__FreeBSD__) || defined (__FreeBSD_kernel__)) && defined(HAVE_THR_KILL2)
  195. return thr_kill2( tgid, pid, sig );
  196. #else
  197. errno = ENOSYS;
  198. return -1;
  199. #endif
  200. }
  201. /* initialize the process tracing mechanism */
  202. void init_tracing_mechanism(void)
  203. {
  204. /* no initialization needed for ptrace */
  205. }
  206. /* initialize the per-process tracing mechanism */
  207. void init_process_tracing( struct process *process )
  208. {
  209. /* ptrace setup is done on-demand */
  210. }
  211. /* terminate the per-process tracing mechanism */
  212. void finish_process_tracing( struct process *process )
  213. {
  214. }
  215. /* send a Unix signal to a specific thread */
  216. int send_thread_signal( struct thread *thread, int sig )
  217. {
  218. int ret = -1;
  219. if (thread->unix_pid != -1)
  220. {
  221. if (thread->unix_tid != -1)
  222. {
  223. ret = tkill( thread->unix_pid, thread->unix_tid, sig );
  224. if (ret == -1 && errno == ENOSYS) ret = kill( thread->unix_pid, sig );
  225. }
  226. else ret = kill( thread->unix_pid, sig );
  227. if (ret == -1 && errno == ESRCH) /* thread got killed */
  228. {
  229. thread->unix_pid = -1;
  230. thread->unix_tid = -1;
  231. }
  232. }
  233. if (debug_level && ret != -1)
  234. fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
  235. return (ret != -1);
  236. }
  237. /* resume a thread after we have used ptrace on it */
  238. static void resume_after_ptrace( struct thread *thread )
  239. {
  240. if (thread->unix_pid == -1) return;
  241. if (ptrace( PTRACE_DETACH, get_ptrace_pid(thread), (caddr_t)1, 0 ) == -1)
  242. {
  243. if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
  244. }
  245. }
  246. /* suspend a thread to allow using ptrace on it */
  247. /* you must do a resume_after_ptrace when finished with the thread */
  248. static int suspend_for_ptrace( struct thread *thread )
  249. {
  250. /* can't stop a thread while initialisation is in progress */
  251. if (thread->unix_pid == -1 || !is_process_init_done(thread->process)) goto error;
  252. /* this may fail if the client is already being debugged */
  253. if (ptrace( PTRACE_ATTACH, get_ptrace_pid(thread), 0, 0 ) == -1)
  254. {
  255. if (errno == ESRCH) thread->unix_pid = thread->unix_tid = -1; /* thread got killed */
  256. goto error;
  257. }
  258. if (waitpid_thread( thread, SIGSTOP )) return 1;
  259. resume_after_ptrace( thread );
  260. error:
  261. set_error( STATUS_ACCESS_DENIED );
  262. return 0;
  263. }
  264. /* read a long from a thread address space */
  265. static int read_thread_long( struct thread *thread, void *addr, unsigned long *data )
  266. {
  267. errno = 0;
  268. *data = ptrace( PTRACE_PEEKDATA, get_ptrace_pid(thread), (caddr_t)addr, 0 );
  269. if ( *data == -1 && errno)
  270. {
  271. file_set_error();
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. static int read_thread_int( struct thread *thread, void *addr, unsigned int *data )
  277. {
  278. unsigned long long_data;
  279. int ret;
  280. ret = read_thread_long( thread, addr, &long_data );
  281. *data = long_data;
  282. return ret;
  283. }
  284. /* write a long to a thread address space */
  285. static long write_thread_long( struct thread *thread, void *addr, unsigned long data, unsigned long mask )
  286. {
  287. unsigned long old_data;
  288. int res;
  289. if (mask != ~0ul)
  290. {
  291. if (read_thread_long( thread, addr, &old_data ) == -1) return -1;
  292. data = (data & mask) | (old_data & ~mask);
  293. }
  294. if ((res = ptrace( PTRACE_POKEDATA, get_ptrace_pid(thread), (caddr_t)addr, data )) == -1)
  295. file_set_error();
  296. return res;
  297. }
  298. /* return a thread of the process suitable for ptracing */
  299. static struct thread *get_ptrace_thread( struct process *process )
  300. {
  301. struct thread *thread;
  302. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  303. {
  304. if (thread->unix_pid != -1) return thread;
  305. }
  306. set_error( STATUS_PROCESS_IS_TERMINATING ); /* process is dead */
  307. return NULL;
  308. }
  309. /* read data from a process memory space */
  310. int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
  311. {
  312. struct thread *thread = get_ptrace_thread( process );
  313. unsigned int first_offset, last_offset, len;
  314. unsigned long data, *addr;
  315. if (!thread) return 0;
  316. if ((unsigned long)ptr != ptr)
  317. {
  318. set_error( STATUS_ACCESS_DENIED );
  319. return 0;
  320. }
  321. first_offset = ptr % sizeof(long);
  322. last_offset = (size + first_offset) % sizeof(long);
  323. if (!last_offset) last_offset = sizeof(long);
  324. addr = (unsigned long *)(unsigned long)(ptr - first_offset);
  325. len = (size + first_offset + sizeof(long) - 1) / sizeof(long);
  326. if (suspend_for_ptrace( thread ))
  327. {
  328. if (len > 3) /* /proc/pid/mem should be faster for large sizes */
  329. {
  330. char procmem[24];
  331. int fd;
  332. sprintf( procmem, "/proc/%u/mem", process->unix_pid );
  333. if ((fd = open( procmem, O_RDONLY )) != -1)
  334. {
  335. ssize_t ret = pread( fd, dest, size, ptr );
  336. close( fd );
  337. if (ret == size)
  338. {
  339. len = 0;
  340. goto done;
  341. }
  342. }
  343. }
  344. if (len > 1)
  345. {
  346. if (read_thread_long( thread, addr++, &data ) == -1) goto done;
  347. memcpy( dest, (char *)&data + first_offset, sizeof(long) - first_offset );
  348. dest += sizeof(long) - first_offset;
  349. first_offset = 0;
  350. len--;
  351. }
  352. while (len > 1)
  353. {
  354. if (read_thread_long( thread, addr++, &data ) == -1) goto done;
  355. memcpy( dest, &data, sizeof(long) );
  356. dest += sizeof(long);
  357. len--;
  358. }
  359. if (read_thread_long( thread, addr++, &data ) == -1) goto done;
  360. memcpy( dest, (char *)&data + first_offset, last_offset - first_offset );
  361. len--;
  362. done:
  363. resume_after_ptrace( thread );
  364. }
  365. return !len;
  366. }
  367. /* make sure we can write to the whole address range */
  368. /* len is the total size (in ints) */
  369. static int check_process_write_access( struct thread *thread, long *addr, data_size_t len )
  370. {
  371. int page = get_page_size() / sizeof(int);
  372. for (;;)
  373. {
  374. if (write_thread_long( thread, addr, 0, 0 ) == -1) return 0;
  375. if (len <= page) break;
  376. addr += page;
  377. len -= page;
  378. }
  379. return (write_thread_long( thread, addr + len - 1, 0, 0 ) != -1);
  380. }
  381. /* write data to a process memory space */
  382. int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
  383. {
  384. struct thread *thread = get_ptrace_thread( process );
  385. int ret = 0;
  386. long data = 0;
  387. data_size_t len;
  388. long *addr;
  389. unsigned long first_mask, first_offset, last_mask, last_offset;
  390. if (!thread) return 0;
  391. if ((unsigned long)ptr != ptr)
  392. {
  393. set_error( STATUS_ACCESS_DENIED );
  394. return 0;
  395. }
  396. /* compute the mask for the first long */
  397. first_mask = ~0;
  398. first_offset = ptr % sizeof(long);
  399. memset( &first_mask, 0, first_offset );
  400. /* compute the mask for the last long */
  401. last_offset = (size + first_offset) % sizeof(long);
  402. if (!last_offset) last_offset = sizeof(long);
  403. last_mask = 0;
  404. memset( &last_mask, 0xff, last_offset );
  405. addr = (long *)(unsigned long)(ptr - first_offset);
  406. len = (size + first_offset + sizeof(long) - 1) / sizeof(long);
  407. if (suspend_for_ptrace( thread ))
  408. {
  409. if (!check_process_write_access( thread, addr, len ))
  410. {
  411. set_error( STATUS_ACCESS_DENIED );
  412. goto done;
  413. }
  414. if (len > 3)
  415. {
  416. char procmem[24];
  417. int fd;
  418. sprintf( procmem, "/proc/%u/mem", process->unix_pid );
  419. if ((fd = open( procmem, O_WRONLY )) != -1)
  420. {
  421. ssize_t r = pwrite( fd, src, size, ptr );
  422. close( fd );
  423. if (r == size)
  424. {
  425. ret = 1;
  426. goto done;
  427. }
  428. }
  429. }
  430. /* first word is special */
  431. if (len > 1)
  432. {
  433. memcpy( (char *)&data + first_offset, src, sizeof(long) - first_offset );
  434. src += sizeof(long) - first_offset;
  435. if (write_thread_long( thread, addr++, data, first_mask ) == -1) goto done;
  436. first_offset = 0;
  437. len--;
  438. }
  439. else last_mask &= first_mask;
  440. while (len > 1)
  441. {
  442. memcpy( &data, src, sizeof(long) );
  443. src += sizeof(long);
  444. if (write_thread_long( thread, addr++, data, ~0ul ) == -1) goto done;
  445. len--;
  446. }
  447. /* last word is special too */
  448. memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
  449. if (write_thread_long( thread, addr, data, last_mask ) == -1) goto done;
  450. ret = 1;
  451. done:
  452. resume_after_ptrace( thread );
  453. }
  454. return ret;
  455. }
  456. /* retrieve an LDT selector entry */
  457. void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
  458. unsigned int *limit, unsigned char *flags )
  459. {
  460. if (!thread->process->ldt_copy)
  461. {
  462. set_error( STATUS_ACCESS_DENIED );
  463. return;
  464. }
  465. if (entry >= 8192)
  466. {
  467. set_error( STATUS_ACCESS_VIOLATION );
  468. return;
  469. }
  470. if (suspend_for_ptrace( thread ))
  471. {
  472. unsigned int flags_buf;
  473. unsigned long addr = (unsigned long)thread->process->ldt_copy + (entry * 4);
  474. if (read_thread_int( thread, (void *)addr, base ) == -1) goto done;
  475. if (read_thread_int( thread, (void *)(addr + (8192 * 4)), limit ) == -1) goto done;
  476. addr = (unsigned long)thread->process->ldt_copy + (2 * 8192 * 4) + (entry & ~3);
  477. if (read_thread_int( thread, (void *)addr, &flags_buf ) == -1) goto done;
  478. *flags = flags_buf >> (entry & 3) * 8;
  479. done:
  480. resume_after_ptrace( thread );
  481. }
  482. }
  483. #if defined(linux) && (defined(HAVE_SYS_USER_H) || defined(HAVE_ASM_USER_H)) \
  484. && (defined(__i386__) || defined(__x86_64__))
  485. #ifdef HAVE_SYS_USER_H
  486. #include <sys/user.h>
  487. #elif defined(HAVE_ASM_USER_H)
  488. #include <asm/user.h>
  489. #endif
  490. /* debug register offset in struct user */
  491. #define DR_OFFSET(dr) ((((struct user *)0)->u_debugreg) + (dr))
  492. /* initialize registers in new thread if necessary */
  493. void init_thread_context( struct thread *thread )
  494. {
  495. /* Linux doesn't clear all registers, but hopefully enough to avoid spurious breakpoints */
  496. thread->system_regs = 0;
  497. }
  498. /* retrieve the thread x86 registers */
  499. void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
  500. {
  501. int i, pid = get_ptrace_tid(thread);
  502. long data[8];
  503. /* all other regs are handled on the client side */
  504. assert( flags == SERVER_CTX_DEBUG_REGISTERS );
  505. if (!suspend_for_ptrace( thread )) return;
  506. if (!(thread->system_regs & SERVER_CTX_DEBUG_REGISTERS))
  507. {
  508. /* caller has initialized everything to 0 already, just return */
  509. context->flags |= SERVER_CTX_DEBUG_REGISTERS;
  510. goto done;
  511. }
  512. for (i = 0; i < 8; i++)
  513. {
  514. if (i == 4 || i == 5) continue;
  515. errno = 0;
  516. data[i] = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(i), 0 );
  517. if ((data[i] == -1) && errno)
  518. {
  519. file_set_error();
  520. goto done;
  521. }
  522. }
  523. switch (context->cpu)
  524. {
  525. case CPU_x86:
  526. context->debug.i386_regs.dr0 = data[0];
  527. context->debug.i386_regs.dr1 = data[1];
  528. context->debug.i386_regs.dr2 = data[2];
  529. context->debug.i386_regs.dr3 = data[3];
  530. context->debug.i386_regs.dr6 = data[6];
  531. context->debug.i386_regs.dr7 = data[7];
  532. break;
  533. case CPU_x86_64:
  534. context->debug.x86_64_regs.dr0 = data[0];
  535. context->debug.x86_64_regs.dr1 = data[1];
  536. context->debug.x86_64_regs.dr2 = data[2];
  537. context->debug.x86_64_regs.dr3 = data[3];
  538. context->debug.x86_64_regs.dr6 = data[6];
  539. context->debug.x86_64_regs.dr7 = data[7];
  540. break;
  541. default:
  542. set_error( STATUS_INVALID_PARAMETER );
  543. goto done;
  544. }
  545. context->flags |= SERVER_CTX_DEBUG_REGISTERS;
  546. done:
  547. resume_after_ptrace( thread );
  548. }
  549. /* set the thread x86 registers */
  550. void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
  551. {
  552. int pid = get_ptrace_tid( thread );
  553. /* all other regs are handled on the client side */
  554. assert( flags == SERVER_CTX_DEBUG_REGISTERS );
  555. if (!suspend_for_ptrace( thread )) return;
  556. /* force all breakpoint lengths to 1, workaround for kernel bug 200965 */
  557. ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), 0x11110055 );
  558. switch (context->cpu)
  559. {
  560. case CPU_x86:
  561. /* Linux 2.6.33+ does DR0-DR3 alignment validation, so it has to know LEN bits first */
  562. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.i386_regs.dr7 & 0xffff0000 ) == -1) goto error;
  563. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->debug.i386_regs.dr0 ) == -1) goto error;
  564. if (thread->context) thread->context->debug.i386_regs.dr0 = context->debug.i386_regs.dr0;
  565. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->debug.i386_regs.dr1 ) == -1) goto error;
  566. if (thread->context) thread->context->debug.i386_regs.dr1 = context->debug.i386_regs.dr1;
  567. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->debug.i386_regs.dr2 ) == -1) goto error;
  568. if (thread->context) thread->context->debug.i386_regs.dr2 = context->debug.i386_regs.dr2;
  569. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->debug.i386_regs.dr3 ) == -1) goto error;
  570. if (thread->context) thread->context->debug.i386_regs.dr3 = context->debug.i386_regs.dr3;
  571. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->debug.i386_regs.dr6 ) == -1) goto error;
  572. if (thread->context) thread->context->debug.i386_regs.dr6 = context->debug.i386_regs.dr6;
  573. /* Linux 2.6.33+ needs enable bits set briefly to update value returned by PEEKUSER later */
  574. ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.i386_regs.dr7 | 0x55 );
  575. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.i386_regs.dr7 ) == -1) goto error;
  576. if (thread->context) thread->context->debug.i386_regs.dr7 = context->debug.i386_regs.dr7;
  577. thread->system_regs |= SERVER_CTX_DEBUG_REGISTERS;
  578. break;
  579. case CPU_x86_64:
  580. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.x86_64_regs.dr7 & 0xffff0000 ) == -1) goto error;
  581. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->debug.x86_64_regs.dr0 ) == -1) goto error;
  582. if (thread->context) thread->context->debug.x86_64_regs.dr0 = context->debug.x86_64_regs.dr0;
  583. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->debug.x86_64_regs.dr1 ) == -1) goto error;
  584. if (thread->context) thread->context->debug.x86_64_regs.dr1 = context->debug.x86_64_regs.dr1;
  585. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->debug.x86_64_regs.dr2 ) == -1) goto error;
  586. if (thread->context) thread->context->debug.x86_64_regs.dr2 = context->debug.x86_64_regs.dr2;
  587. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->debug.x86_64_regs.dr3 ) == -1) goto error;
  588. if (thread->context) thread->context->debug.x86_64_regs.dr3 = context->debug.x86_64_regs.dr3;
  589. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->debug.x86_64_regs.dr6 ) == -1) goto error;
  590. if (thread->context) thread->context->debug.x86_64_regs.dr6 = context->debug.x86_64_regs.dr6;
  591. ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.x86_64_regs.dr7 | 0x55 );
  592. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->debug.x86_64_regs.dr7 ) == -1) goto error;
  593. if (thread->context) thread->context->debug.x86_64_regs.dr7 = context->debug.x86_64_regs.dr7;
  594. thread->system_regs |= SERVER_CTX_DEBUG_REGISTERS;
  595. break;
  596. default:
  597. set_error( STATUS_INVALID_PARAMETER );
  598. }
  599. resume_after_ptrace( thread );
  600. return;
  601. error:
  602. file_set_error();
  603. resume_after_ptrace( thread );
  604. }
  605. #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
  606. (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__))
  607. #include <machine/reg.h>
  608. /* initialize registers in new thread if necessary */
  609. void init_thread_context( struct thread *thread )
  610. {
  611. if (!(thread->system_regs & SERVER_CTX_DEBUG_REGISTERS)) return;
  612. /* FreeBSD doesn't clear the debug registers in new threads */
  613. if (suspend_for_ptrace( thread ))
  614. {
  615. struct dbreg dbregs;
  616. memset( &dbregs, 0, sizeof(dbregs) );
  617. ptrace( PTRACE_SETDBREGS, get_ptrace_tid( thread ), (caddr_t)&dbregs, 0 );
  618. resume_after_ptrace( thread );
  619. }
  620. thread->system_regs = 0;
  621. }
  622. /* retrieve the thread x86 registers */
  623. void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
  624. {
  625. int pid = get_ptrace_tid(thread);
  626. struct dbreg dbregs;
  627. /* all other regs are handled on the client side */
  628. assert( flags == SERVER_CTX_DEBUG_REGISTERS );
  629. if (!suspend_for_ptrace( thread )) return;
  630. if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) file_set_error();
  631. else
  632. {
  633. #ifdef DBREG_DRX
  634. /* needed for FreeBSD, the structure fields have changed under 5.x */
  635. context->debug.i386_regs.dr0 = DBREG_DRX((&dbregs), 0);
  636. context->debug.i386_regs.dr1 = DBREG_DRX((&dbregs), 1);
  637. context->debug.i386_regs.dr2 = DBREG_DRX((&dbregs), 2);
  638. context->debug.i386_regs.dr3 = DBREG_DRX((&dbregs), 3);
  639. context->debug.i386_regs.dr6 = DBREG_DRX((&dbregs), 6);
  640. context->debug.i386_regs.dr7 = DBREG_DRX((&dbregs), 7);
  641. #elif defined(__NetBSD__)
  642. context->debug.i386_regs.dr0 = dbregs.dr[0];
  643. context->debug.i386_regs.dr1 = dbregs.dr[1];
  644. context->debug.i386_regs.dr2 = dbregs.dr[2];
  645. context->debug.i386_regs.dr3 = dbregs.dr[3];
  646. context->debug.i386_regs.dr6 = dbregs.dr[6];
  647. context->debug.i386_regs.dr7 = dbregs.dr[7];
  648. #else
  649. context->debug.i386_regs.dr0 = dbregs.dr0;
  650. context->debug.i386_regs.dr1 = dbregs.dr1;
  651. context->debug.i386_regs.dr2 = dbregs.dr2;
  652. context->debug.i386_regs.dr3 = dbregs.dr3;
  653. context->debug.i386_regs.dr6 = dbregs.dr6;
  654. context->debug.i386_regs.dr7 = dbregs.dr7;
  655. #endif
  656. context->flags |= SERVER_CTX_DEBUG_REGISTERS;
  657. }
  658. resume_after_ptrace( thread );
  659. }
  660. /* set the thread x86 registers */
  661. void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
  662. {
  663. int pid = get_ptrace_tid(thread);
  664. struct dbreg dbregs;
  665. /* all other regs are handled on the client side */
  666. assert( flags == SERVER_CTX_DEBUG_REGISTERS );
  667. if (!suspend_for_ptrace( thread )) return;
  668. #ifdef DBREG_DRX
  669. /* needed for FreeBSD, the structure fields have changed under 5.x */
  670. DBREG_DRX((&dbregs), 0) = context->debug.i386_regs.dr0;
  671. DBREG_DRX((&dbregs), 1) = context->debug.i386_regs.dr1;
  672. DBREG_DRX((&dbregs), 2) = context->debug.i386_regs.dr2;
  673. DBREG_DRX((&dbregs), 3) = context->debug.i386_regs.dr3;
  674. DBREG_DRX((&dbregs), 4) = 0;
  675. DBREG_DRX((&dbregs), 5) = 0;
  676. DBREG_DRX((&dbregs), 6) = context->debug.i386_regs.dr6;
  677. DBREG_DRX((&dbregs), 7) = context->debug.i386_regs.dr7;
  678. #elif defined(__NetBSD__)
  679. dbregs.dr[0] = context->debug.i386_regs.dr0;
  680. dbregs.dr[1] = context->debug.i386_regs.dr1;
  681. dbregs.dr[2] = context->debug.i386_regs.dr2;
  682. dbregs.dr[3] = context->debug.i386_regs.dr3;
  683. dbregs.dr[4] = 0;
  684. dbregs.dr[5] = 0;
  685. dbregs.dr[6] = context->debug.i386_regs.dr6;
  686. dbregs.dr[7] = context->debug.i386_regs.dr7;
  687. #else
  688. dbregs.dr0 = context->debug.i386_regs.dr0;
  689. dbregs.dr1 = context->debug.i386_regs.dr1;
  690. dbregs.dr2 = context->debug.i386_regs.dr2;
  691. dbregs.dr3 = context->debug.i386_regs.dr3;
  692. dbregs.dr4 = 0;
  693. dbregs.dr5 = 0;
  694. dbregs.dr6 = context->debug.i386_regs.dr6;
  695. dbregs.dr7 = context->debug.i386_regs.dr7;
  696. #endif
  697. if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t)&dbregs, 0 ) != -1)
  698. {
  699. if (thread->context)
  700. thread->context->debug.i386_regs = context->debug.i386_regs; /* update the cached values */
  701. thread->system_regs |= SERVER_CTX_DEBUG_REGISTERS;
  702. }
  703. else file_set_error();
  704. resume_after_ptrace( thread );
  705. }
  706. #else /* linux || __FreeBSD__ */
  707. /* initialize registers in new thread if necessary */
  708. void init_thread_context( struct thread *thread )
  709. {
  710. }
  711. /* retrieve the thread x86 registers */
  712. void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
  713. {
  714. }
  715. /* set the thread x86 debug registers */
  716. void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
  717. {
  718. }
  719. #endif /* linux || __FreeBSD__ */
  720. #endif /* USE_PTRACE */