ptrace.c 24 KB

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