2 * Server-side ptrace support
4 * Copyright (C) 1999 Alexandre Julliard
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.
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.
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
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_PTRACE_H
30 # include <sys/ptrace.h>
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
35 #ifdef HAVE_SYS_WAIT_H
36 # include <sys/wait.h>
41 #define WIN32_NO_STATUS
49 #define PTRACE_CONT PT_CONTINUE
51 #ifndef PTRACE_SINGLESTEP
52 #define PTRACE_SINGLESTEP PT_STEP
55 #define PTRACE_ATTACH PT_ATTACH
58 #define PTRACE_DETACH PT_DETACH
60 #ifndef PTRACE_PEEKDATA
61 #define PTRACE_PEEKDATA PT_READ_D
63 #ifndef PTRACE_POKEDATA
64 #define PTRACE_POKEDATA PT_WRITE_D
66 #ifndef PTRACE_PEEKUSER
67 #define PTRACE_PEEKUSER PT_READ_U
69 #ifndef PTRACE_POKEUSER
70 #define PTRACE_POKEUSER PT_WRITE_U
74 #define PTRACE_GETDBREGS PT_GETDBREGS
77 #define PTRACE_SETDBREGS PT_SETDBREGS
80 #ifndef HAVE_SYS_PTRACE_H
87 inline static int ptrace(int req
, ...) { errno
= EPERM
; return -1; /*FAIL*/ }
88 #endif /* HAVE_SYS_PTRACE_H */
90 /* handle a status returned by wait4 */
91 static int handle_child_status( struct thread
*thread
, int pid
, int status
, int want_sig
)
93 if (WIFSTOPPED(status
))
95 int sig
= WSTOPSIG(status
);
96 if (debug_level
&& thread
)
97 fprintf( stderr
, "%04x: *signal* signal=%d\n", thread
->id
, sig
);
100 /* ignore other signals for now */
101 ptrace( PTRACE_CONT
, pid
, (caddr_t
)1, sig
);
105 if (thread
&& (WIFSIGNALED(status
) || WIFEXITED(status
)))
107 thread
->unix_pid
= -1;
108 thread
->unix_tid
= -1;
111 if (WIFSIGNALED(status
))
112 fprintf( stderr
, "%04x: *exited* signal=%d\n",
113 thread
->id
, WTERMSIG(status
) );
115 fprintf( stderr
, "%04x: *exited* status=%d\n",
116 thread
->id
, WEXITSTATUS(status
) );
122 /* wait4 wrapper to handle missing __WALL flag in older kernels */
123 static inline pid_t
wait4_wrapper( pid_t pid
, int *status
, int options
, struct rusage
*usage
)
126 static int wall_flag
= __WALL
;
130 pid_t ret
= wait4( pid
, status
, options
| wall_flag
, usage
);
131 if (ret
!= -1 || !wall_flag
|| errno
!= EINVAL
) return ret
;
135 return wait4( pid
, status
, options
, usage
);
139 /* handle a SIGCHLD signal */
140 void sigchld_callback(void)
146 if (!(pid
= wait4_wrapper( -1, &status
, WUNTRACED
| WNOHANG
, NULL
))) break;
147 if (pid
!= -1) handle_child_status( get_thread_from_pid(pid
), pid
, status
, -1 );
152 /* return the Unix pid to use in ptrace calls for a given thread */
153 static int get_ptrace_pid( struct thread
*thread
)
155 if (thread
->unix_tid
!= -1) return thread
->unix_tid
;
156 return thread
->unix_pid
;
159 /* wait for a ptraced child to get a certain signal */
160 static int wait4_thread( struct thread
*thread
, int signal
)
167 if ((res
= wait4_wrapper( get_ptrace_pid(thread
), &status
, WUNTRACED
, NULL
)) == -1)
171 if (!watchdog_triggered()) continue;
172 if (debug_level
) fprintf( stderr
, "%04x: *watchdog* wait4 aborted\n", thread
->id
);
174 else if (errno
== ECHILD
) /* must have died */
176 thread
->unix_pid
= -1;
177 thread
->unix_tid
= -1;
179 else perror( "wait4" );
183 res
= handle_child_status( thread
, res
, status
, signal
);
184 if (!res
|| res
== signal
) break;
187 return (thread
->unix_pid
!= -1);
190 /* send a signal to a specific thread */
191 static inline int tkill( int tgid
, int pid
, int sig
)
197 __asm__( "pushl %%ebx\n\t"
202 : "0" (270) /*SYS_tgkill*/, "r" (tgid
), "c" (pid
), "d" (sig
) );
204 __asm__( "pushl %%ebx\n\t"
209 : "0" (238) /*SYS_tkill*/, "r" (pid
), "c" (sig
) );
210 # elif defined(__x86_64__)
211 __asm__( "syscall" : "=a" (ret
)
212 : "0" (200) /*SYS_tkill*/, "D" (pid
), "S" (sig
) );
214 #endif /* __linux__ */
216 if (ret
>= 0) return ret
;
221 /* send a Unix signal to a specific thread */
222 int send_thread_signal( struct thread
*thread
, int sig
)
226 if (thread
->unix_pid
!= -1)
228 if (thread
->unix_tid
!= -1)
230 ret
= tkill( thread
->unix_pid
, thread
->unix_tid
, sig
);
231 if (ret
== -1 && errno
== ENOSYS
) ret
= kill( thread
->unix_pid
, sig
);
233 else ret
= kill( thread
->unix_pid
, sig
);
235 if (ret
== -1 && errno
== ESRCH
) /* thread got killed */
237 thread
->unix_pid
= -1;
238 thread
->unix_tid
= -1;
244 /* resume a thread after we have used ptrace on it */
245 static void resume_after_ptrace( struct thread
*thread
)
247 if (thread
->unix_pid
== -1) return;
248 if (ptrace( PTRACE_DETACH
, get_ptrace_pid(thread
), (caddr_t
)1, 0 ) == -1)
250 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
254 /* suspend a thread to allow using ptrace on it */
255 /* you must do a resume_after_ptrace when finished with the thread */
256 static int suspend_for_ptrace( struct thread
*thread
)
258 /* can't stop a thread while initialisation is in progress */
259 if (thread
->unix_pid
== -1 || !is_process_init_done(thread
->process
)) goto error
;
261 /* this may fail if the client is already being debugged */
262 if (ptrace( PTRACE_ATTACH
, get_ptrace_pid(thread
), 0, 0 ) == -1)
264 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
267 if (wait4_thread( thread
, SIGSTOP
)) return 1;
268 resume_after_ptrace( thread
);
270 set_error( STATUS_ACCESS_DENIED
);
274 /* read an int from a thread address space */
275 static int read_thread_int( struct thread
*thread
, const int *addr
, int *data
)
278 *data
= ptrace( PTRACE_PEEKDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, 0 );
279 if ( *data
== -1 && errno
)
287 /* write an int to a thread address space */
288 static int write_thread_int( struct thread
*thread
, int *addr
, int data
, unsigned int mask
)
293 if (read_thread_int( thread
, addr
, &res
) == -1) return -1;
294 data
= (data
& mask
) | (res
& ~mask
);
296 if ((res
= ptrace( PTRACE_POKEDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, data
)) == -1)
301 /* read data from a process memory space */
302 int read_process_memory( struct process
*process
, const void *ptr
, size_t size
, char *dest
)
304 struct thread
*thread
= get_process_first_thread( process
);
305 unsigned int first_offset
, last_offset
, len
;
308 if (!thread
) /* process is dead */
310 set_error( STATUS_ACCESS_DENIED
);
314 first_offset
= (unsigned long)ptr
% sizeof(int);
315 last_offset
= (size
+ first_offset
) % sizeof(int);
316 if (!last_offset
) last_offset
= sizeof(int);
318 addr
= (int *)((char *)ptr
- first_offset
);
319 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
321 if (suspend_for_ptrace( thread
))
325 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
326 memcpy( dest
, (char *)&data
+ first_offset
, sizeof(int) - first_offset
);
327 dest
+= sizeof(int) - first_offset
;
334 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
335 memcpy( dest
, &data
, sizeof(int) );
340 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
341 memcpy( dest
, (char *)&data
+ first_offset
, last_offset
- first_offset
);
345 resume_after_ptrace( thread
);
350 /* make sure we can write to the whole address range */
351 /* len is the total size (in ints) */
352 static int check_process_write_access( struct thread
*thread
, int *addr
, size_t len
)
354 int page
= get_page_size() / sizeof(int);
358 if (write_thread_int( thread
, addr
, 0, 0 ) == -1) return 0;
359 if (len
<= page
) break;
363 return (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) != -1);
366 /* write data to a process memory space */
367 int write_process_memory( struct process
*process
, void *ptr
, size_t size
, const char *src
)
369 struct thread
*thread
= get_process_first_thread( process
);
370 int ret
= 0, data
= 0;
373 unsigned int first_mask
, first_offset
, last_mask
, last_offset
;
375 if (!thread
) /* process is dead */
377 set_error( STATUS_ACCESS_DENIED
);
381 /* compute the mask for the first int */
383 first_offset
= (unsigned long)ptr
% sizeof(int);
384 memset( &first_mask
, 0, first_offset
);
386 /* compute the mask for the last int */
387 last_offset
= (size
+ first_offset
) % sizeof(int);
388 if (!last_offset
) last_offset
= sizeof(int);
390 memset( &last_mask
, 0xff, last_offset
);
392 addr
= (int *)((char *)ptr
- first_offset
);
393 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
395 if (suspend_for_ptrace( thread
))
397 if (!check_process_write_access( thread
, addr
, len
))
399 set_error( STATUS_ACCESS_DENIED
);
402 /* first word is special */
405 memcpy( (char *)&data
+ first_offset
, src
, sizeof(int) - first_offset
);
406 src
+= sizeof(int) - first_offset
;
407 if (write_thread_int( thread
, addr
++, data
, first_mask
) == -1) goto done
;
411 else last_mask
&= first_mask
;
415 memcpy( &data
, src
, sizeof(int) );
417 if (write_thread_int( thread
, addr
++, data
, ~0 ) == -1) goto done
;
421 /* last word is special too */
422 memcpy( (char *)&data
+ first_offset
, src
, last_offset
- first_offset
);
423 if (write_thread_int( thread
, addr
, data
, last_mask
) == -1) goto done
;
427 resume_after_ptrace( thread
);
432 /* retrieve an LDT selector entry */
433 void get_selector_entry( struct thread
*thread
, int entry
, unsigned int *base
,
434 unsigned int *limit
, unsigned char *flags
)
436 if (!thread
->process
->ldt_copy
)
438 set_error( STATUS_ACCESS_DENIED
);
443 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
446 if (suspend_for_ptrace( thread
))
448 unsigned char flags_buf
[4];
449 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
450 if (read_thread_int( thread
, addr
, (int *)base
) == -1) goto done
;
451 if (read_thread_int( thread
, addr
+ 8192, (int *)limit
) == -1) goto done
;
452 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
453 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
454 *flags
= flags_buf
[entry
& 3];
456 resume_after_ptrace( thread
);
461 #if defined(linux) && (defined(__i386__) || defined(__x86_64__))
463 #ifdef HAVE_SYS_USER_H
464 # include <sys/user.h>
467 /* debug register offset in struct user */
468 #define DR_OFFSET(dr) ((((struct user *)0)->u_debugreg) + (dr))
470 /* retrieve the thread x86 registers */
471 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
473 int i
, pid
= get_ptrace_pid(thread
);
476 /* all other regs are handled on the client side */
477 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
479 if (!suspend_for_ptrace( thread
)) return;
481 for (i
= 0; i
< 8; i
++)
483 if (i
== 4 || i
== 5) continue;
485 data
[i
] = ptrace( PTRACE_PEEKUSER
, pid
, DR_OFFSET(i
), 0 );
486 if ((data
[i
] == -1) && errno
)
492 context
->Dr0
= data
[0];
493 context
->Dr1
= data
[1];
494 context
->Dr2
= data
[2];
495 context
->Dr3
= data
[3];
496 context
->Dr6
= data
[6];
497 context
->Dr7
= data
[7];
498 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
500 resume_after_ptrace( thread
);
503 /* set the thread x86 registers */
504 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
506 int pid
= get_ptrace_pid( thread
);
508 /* all other regs are handled on the client side */
509 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
511 if (!suspend_for_ptrace( thread
)) return;
513 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(0), context
->Dr0
) == -1) goto error
;
514 if (thread
->context
) thread
->context
->Dr0
= context
->Dr0
;
515 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(1), context
->Dr1
) == -1) goto error
;
516 if (thread
->context
) thread
->context
->Dr1
= context
->Dr1
;
517 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(2), context
->Dr2
) == -1) goto error
;
518 if (thread
->context
) thread
->context
->Dr2
= context
->Dr2
;
519 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(3), context
->Dr3
) == -1) goto error
;
520 if (thread
->context
) thread
->context
->Dr3
= context
->Dr3
;
521 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(6), context
->Dr6
) == -1) goto error
;
522 if (thread
->context
) thread
->context
->Dr6
= context
->Dr6
;
523 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(7), context
->Dr7
) == -1) goto error
;
524 if (thread
->context
) thread
->context
->Dr7
= context
->Dr7
;
525 resume_after_ptrace( thread
);
529 resume_after_ptrace( thread
);
532 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
533 (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__))
535 #include <machine/reg.h>
537 /* retrieve the thread x86 registers */
538 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
540 int pid
= get_ptrace_pid(thread
);
543 /* all other regs are handled on the client side */
544 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
546 if (!suspend_for_ptrace( thread
)) return;
548 if (ptrace( PTRACE_GETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1) file_set_error();
552 /* needed for FreeBSD, the structure fields have changed under 5.x */
553 context
->Dr0
= DBREG_DRX((&dbregs
), 0);
554 context
->Dr1
= DBREG_DRX((&dbregs
), 1);
555 context
->Dr2
= DBREG_DRX((&dbregs
), 2);
556 context
->Dr3
= DBREG_DRX((&dbregs
), 3);
557 context
->Dr6
= DBREG_DRX((&dbregs
), 6);
558 context
->Dr7
= DBREG_DRX((&dbregs
), 7);
560 context
->Dr0
= dbregs
.dr0
;
561 context
->Dr1
= dbregs
.dr1
;
562 context
->Dr2
= dbregs
.dr2
;
563 context
->Dr3
= dbregs
.dr3
;
564 context
->Dr6
= dbregs
.dr6
;
565 context
->Dr7
= dbregs
.dr7
;
567 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
569 resume_after_ptrace( thread
);
572 /* set the thread x86 registers */
573 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
575 int pid
= get_ptrace_pid(thread
);
578 /* all other regs are handled on the client side */
579 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
581 if (!suspend_for_ptrace( thread
)) return;
584 /* needed for FreeBSD, the structure fields have changed under 5.x */
585 DBREG_DRX((&dbregs
), 0) = context
->Dr0
;
586 DBREG_DRX((&dbregs
), 1) = context
->Dr1
;
587 DBREG_DRX((&dbregs
), 2) = context
->Dr2
;
588 DBREG_DRX((&dbregs
), 3) = context
->Dr3
;
589 DBREG_DRX((&dbregs
), 4) = 0;
590 DBREG_DRX((&dbregs
), 5) = 0;
591 DBREG_DRX((&dbregs
), 6) = context
->Dr6
;
592 DBREG_DRX((&dbregs
), 7) = context
->Dr7
;
594 dbregs
.dr0
= context
->Dr0
;
595 dbregs
.dr1
= context
->Dr1
;
596 dbregs
.dr2
= context
->Dr2
;
597 dbregs
.dr3
= context
->Dr3
;
600 dbregs
.dr6
= context
->Dr6
;
601 dbregs
.dr7
= context
->Dr7
;
603 if (ptrace( PTRACE_SETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1) file_set_error();
604 else if (thread
->context
) /* update the cached values */
606 thread
->context
->Dr0
= context
->Dr0
;
607 thread
->context
->Dr1
= context
->Dr1
;
608 thread
->context
->Dr2
= context
->Dr2
;
609 thread
->context
->Dr3
= context
->Dr3
;
610 thread
->context
->Dr6
= context
->Dr6
;
611 thread
->context
->Dr7
= context
->Dr7
;
613 resume_after_ptrace( thread
);
616 #else /* linux || __FreeBSD__ */
618 /* retrieve the thread x86 registers */
619 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
623 /* set the thread x86 debug registers */
624 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
628 #endif /* linux || __FreeBSD__ */