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>
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
38 #ifdef HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
44 #define WIN32_NO_STATUS
52 #define PTRACE_CONT PT_CONTINUE
54 #ifndef PTRACE_SINGLESTEP
55 #define PTRACE_SINGLESTEP PT_STEP
58 #define PTRACE_ATTACH PT_ATTACH
61 #define PTRACE_DETACH PT_DETACH
63 #ifndef PTRACE_PEEKDATA
64 #define PTRACE_PEEKDATA PT_READ_D
66 #ifndef PTRACE_POKEDATA
67 #define PTRACE_POKEDATA PT_WRITE_D
69 #ifndef PTRACE_PEEKUSER
70 #define PTRACE_PEEKUSER PT_READ_U
72 #ifndef PTRACE_POKEUSER
73 #define PTRACE_POKEUSER PT_WRITE_U
77 #define PTRACE_GETDBREGS PT_GETDBREGS
80 #define PTRACE_SETDBREGS PT_SETDBREGS
83 #ifndef HAVE_SYS_PTRACE_H
90 inline static int ptrace(int req
, ...) { errno
= EPERM
; return -1; /*FAIL*/ }
91 #endif /* HAVE_SYS_PTRACE_H */
93 /* handle a status returned by wait4 */
94 static int handle_child_status( struct thread
*thread
, int pid
, int status
, int want_sig
)
96 if (WIFSTOPPED(status
))
98 int sig
= WSTOPSIG(status
);
99 if (debug_level
&& thread
)
100 fprintf( stderr
, "%04x: *signal* signal=%d\n", thread
->id
, sig
);
103 /* ignore other signals for now */
104 ptrace( PTRACE_CONT
, pid
, (caddr_t
)1, sig
);
108 if (thread
&& (WIFSIGNALED(status
) || WIFEXITED(status
)))
110 thread
->unix_pid
= -1;
111 thread
->unix_tid
= -1;
114 if (WIFSIGNALED(status
))
115 fprintf( stderr
, "%04x: *exited* signal=%d\n",
116 thread
->id
, WTERMSIG(status
) );
118 fprintf( stderr
, "%04x: *exited* status=%d\n",
119 thread
->id
, WEXITSTATUS(status
) );
125 /* wait4 wrapper to handle missing __WALL flag in older kernels */
126 static inline pid_t
wait4_wrapper( pid_t pid
, int *status
, int options
, struct rusage
*usage
)
129 static int wall_flag
= __WALL
;
133 pid_t ret
= wait4( pid
, status
, options
| wall_flag
, usage
);
134 if (ret
!= -1 || !wall_flag
|| errno
!= EINVAL
) return ret
;
138 return wait4( pid
, status
, options
, usage
);
142 /* handle a SIGCHLD signal */
143 void sigchld_callback(void)
149 if (!(pid
= wait4_wrapper( -1, &status
, WUNTRACED
| WNOHANG
, NULL
))) break;
150 if (pid
!= -1) handle_child_status( get_thread_from_pid(pid
), pid
, status
, -1 );
155 /* return the Unix pid to use in ptrace calls for a given thread */
156 static int get_ptrace_pid( struct thread
*thread
)
158 if (thread
->unix_tid
!= -1) return thread
->unix_tid
;
159 return thread
->unix_pid
;
162 /* wait for a ptraced child to get a certain signal */
163 static int wait4_thread( struct thread
*thread
, int signal
)
170 if ((res
= wait4_wrapper( get_ptrace_pid(thread
), &status
, WUNTRACED
, NULL
)) == -1)
174 if (!watchdog_triggered()) continue;
175 if (debug_level
) fprintf( stderr
, "%04x: *watchdog* wait4 aborted\n", thread
->id
);
177 else if (errno
== ECHILD
) /* must have died */
179 thread
->unix_pid
= -1;
180 thread
->unix_tid
= -1;
182 else perror( "wait4" );
186 res
= handle_child_status( thread
, res
, status
, signal
);
187 if (!res
|| res
== signal
) break;
190 return (thread
->unix_pid
!= -1);
193 /* send a signal to a specific thread */
194 static inline int tkill( int tgid
, int pid
, int sig
)
200 __asm__( "pushl %%ebx\n\t"
205 : "0" (270) /*SYS_tgkill*/, "r" (tgid
), "c" (pid
), "d" (sig
) );
207 __asm__( "pushl %%ebx\n\t"
212 : "0" (238) /*SYS_tkill*/, "r" (pid
), "c" (sig
) );
213 # elif defined(__x86_64__)
214 __asm__( "syscall" : "=a" (ret
)
215 : "0" (200) /*SYS_tkill*/, "D" (pid
), "S" (sig
) );
217 #endif /* __linux__ */
219 if (ret
>= 0) return ret
;
224 /* send a Unix signal to a specific thread */
225 int send_thread_signal( struct thread
*thread
, int sig
)
229 if (thread
->unix_pid
!= -1)
231 if (thread
->unix_tid
!= -1)
233 ret
= tkill( thread
->unix_pid
, thread
->unix_tid
, sig
);
234 if (ret
== -1 && errno
== ENOSYS
) ret
= kill( thread
->unix_pid
, sig
);
236 else ret
= kill( thread
->unix_pid
, sig
);
238 if (ret
== -1 && errno
== ESRCH
) /* thread got killed */
240 thread
->unix_pid
= -1;
241 thread
->unix_tid
= -1;
247 /* resume a thread after we have used ptrace on it */
248 static void resume_after_ptrace( struct thread
*thread
)
250 if (thread
->unix_pid
== -1) return;
251 if (ptrace( PTRACE_DETACH
, get_ptrace_pid(thread
), (caddr_t
)1, 0 ) == -1)
253 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
257 /* suspend a thread to allow using ptrace on it */
258 /* you must do a resume_after_ptrace when finished with the thread */
259 static int suspend_for_ptrace( struct thread
*thread
)
261 /* can't stop a thread while initialisation is in progress */
262 if (thread
->unix_pid
== -1 || !is_process_init_done(thread
->process
)) goto error
;
264 /* this may fail if the client is already being debugged */
265 if (ptrace( PTRACE_ATTACH
, get_ptrace_pid(thread
), 0, 0 ) == -1)
267 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
270 if (wait4_thread( thread
, SIGSTOP
)) return 1;
271 resume_after_ptrace( thread
);
273 set_error( STATUS_ACCESS_DENIED
);
277 /* read an int from a thread address space */
278 static int read_thread_int( struct thread
*thread
, const int *addr
, int *data
)
281 *data
= ptrace( PTRACE_PEEKDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, 0 );
282 if ( *data
== -1 && errno
)
290 /* write an int to a thread address space */
291 static int write_thread_int( struct thread
*thread
, int *addr
, int data
, unsigned int mask
)
296 if (read_thread_int( thread
, addr
, &res
) == -1) return -1;
297 data
= (data
& mask
) | (res
& ~mask
);
299 if ((res
= ptrace( PTRACE_POKEDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, data
)) == -1)
304 /* read data from a process memory space */
305 int read_process_memory( struct process
*process
, const void *ptr
, size_t size
, char *dest
)
307 struct thread
*thread
= get_process_first_thread( process
);
308 unsigned int first_offset
, last_offset
, len
;
311 if (!thread
) /* process is dead */
313 set_error( STATUS_ACCESS_DENIED
);
317 first_offset
= (unsigned long)ptr
% sizeof(int);
318 last_offset
= (size
+ first_offset
) % sizeof(int);
319 if (!last_offset
) last_offset
= sizeof(int);
321 addr
= (int *)((char *)ptr
- first_offset
);
322 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
324 if (suspend_for_ptrace( thread
))
328 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
329 memcpy( dest
, (char *)&data
+ first_offset
, sizeof(int) - first_offset
);
330 dest
+= sizeof(int) - first_offset
;
337 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
338 memcpy( dest
, &data
, sizeof(int) );
343 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
344 memcpy( dest
, (char *)&data
+ first_offset
, last_offset
- first_offset
);
348 resume_after_ptrace( thread
);
353 /* make sure we can write to the whole address range */
354 /* len is the total size (in ints) */
355 static int check_process_write_access( struct thread
*thread
, int *addr
, size_t len
)
357 int page
= get_page_size() / sizeof(int);
361 if (write_thread_int( thread
, addr
, 0, 0 ) == -1) return 0;
362 if (len
<= page
) break;
366 return (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) != -1);
369 /* write data to a process memory space */
370 int write_process_memory( struct process
*process
, void *ptr
, size_t size
, const char *src
)
372 struct thread
*thread
= get_process_first_thread( process
);
373 int ret
= 0, data
= 0;
376 unsigned int first_mask
, first_offset
, last_mask
, last_offset
;
378 if (!thread
) /* process is dead */
380 set_error( STATUS_ACCESS_DENIED
);
384 /* compute the mask for the first int */
386 first_offset
= (unsigned long)ptr
% sizeof(int);
387 memset( &first_mask
, 0, first_offset
);
389 /* compute the mask for the last int */
390 last_offset
= (size
+ first_offset
) % sizeof(int);
391 if (!last_offset
) last_offset
= sizeof(int);
393 memset( &last_mask
, 0xff, last_offset
);
395 addr
= (int *)((char *)ptr
- first_offset
);
396 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
398 if (suspend_for_ptrace( thread
))
400 if (!check_process_write_access( thread
, addr
, len
))
402 set_error( STATUS_ACCESS_DENIED
);
405 /* first word is special */
408 memcpy( (char *)&data
+ first_offset
, src
, sizeof(int) - first_offset
);
409 src
+= sizeof(int) - first_offset
;
410 if (write_thread_int( thread
, addr
++, data
, first_mask
) == -1) goto done
;
414 else last_mask
&= first_mask
;
418 memcpy( &data
, src
, sizeof(int) );
420 if (write_thread_int( thread
, addr
++, data
, ~0 ) == -1) goto done
;
424 /* last word is special too */
425 memcpy( (char *)&data
+ first_offset
, src
, last_offset
- first_offset
);
426 if (write_thread_int( thread
, addr
, data
, last_mask
) == -1) goto done
;
430 resume_after_ptrace( thread
);
435 /* retrieve an LDT selector entry */
436 void get_selector_entry( struct thread
*thread
, int entry
, unsigned int *base
,
437 unsigned int *limit
, unsigned char *flags
)
439 if (!thread
->process
->ldt_copy
)
441 set_error( STATUS_ACCESS_DENIED
);
446 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
449 if (suspend_for_ptrace( thread
))
451 unsigned char flags_buf
[4];
452 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
453 if (read_thread_int( thread
, addr
, (int *)base
) == -1) goto done
;
454 if (read_thread_int( thread
, addr
+ 8192, (int *)limit
) == -1) goto done
;
455 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
456 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
457 *flags
= flags_buf
[entry
& 3];
459 resume_after_ptrace( thread
);
464 #if defined(linux) && (defined(__i386__) || defined(__x86_64__))
466 #ifdef HAVE_SYS_USER_H
467 # include <sys/user.h>
470 /* debug register offset in struct user */
471 #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
473 /* retrieve a debug register */
474 static inline int get_debug_reg( int pid
, int num
, DWORD
*data
)
478 res
= ptrace( PTRACE_PEEKUSER
, pid
, DR_OFFSET(num
), 0 );
479 if ((res
== -1) && errno
)
488 /* retrieve the thread x86 registers */
489 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
491 int pid
= get_ptrace_pid(thread
);
493 /* all other regs are handled on the client side */
494 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
496 if (!suspend_for_ptrace( thread
)) return;
498 if (get_debug_reg( pid
, 0, &context
->Dr0
) == -1) goto error
;
499 if (get_debug_reg( pid
, 1, &context
->Dr1
) == -1) goto error
;
500 if (get_debug_reg( pid
, 2, &context
->Dr2
) == -1) goto error
;
501 if (get_debug_reg( pid
, 3, &context
->Dr3
) == -1) goto error
;
502 if (get_debug_reg( pid
, 6, &context
->Dr6
) == -1) goto error
;
503 if (get_debug_reg( pid
, 7, &context
->Dr7
) == -1) goto error
;
504 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
505 resume_after_ptrace( thread
);
509 resume_after_ptrace( thread
);
512 /* set the thread x86 registers */
513 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
515 int pid
= get_ptrace_pid( thread
);
517 /* all other regs are handled on the client side */
518 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
520 if (!suspend_for_ptrace( thread
)) return;
522 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(0), context
->Dr0
) == -1) goto error
;
523 if (thread
->context
) thread
->context
->Dr0
= context
->Dr0
;
524 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(1), context
->Dr1
) == -1) goto error
;
525 if (thread
->context
) thread
->context
->Dr1
= context
->Dr1
;
526 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(2), context
->Dr2
) == -1) goto error
;
527 if (thread
->context
) thread
->context
->Dr2
= context
->Dr2
;
528 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(3), context
->Dr3
) == -1) goto error
;
529 if (thread
->context
) thread
->context
->Dr3
= context
->Dr3
;
530 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(6), context
->Dr6
) == -1) goto error
;
531 if (thread
->context
) thread
->context
->Dr6
= context
->Dr6
;
532 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(7), context
->Dr7
) == -1) goto error
;
533 if (thread
->context
) thread
->context
->Dr7
= context
->Dr7
;
534 resume_after_ptrace( thread
);
538 resume_after_ptrace( thread
);
541 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
542 (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__))
544 #include <machine/reg.h>
546 /* retrieve the thread x86 registers */
547 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
549 int pid
= get_ptrace_pid(thread
);
552 /* all other regs are handled on the client side */
553 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
555 if (!suspend_for_ptrace( thread
)) return;
557 if (ptrace( PTRACE_GETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1) file_set_error();
561 /* needed for FreeBSD, the structure fields have changed under 5.x */
562 context
->Dr0
= DBREG_DRX((&dbregs
), 0);
563 context
->Dr1
= DBREG_DRX((&dbregs
), 1);
564 context
->Dr2
= DBREG_DRX((&dbregs
), 2);
565 context
->Dr3
= DBREG_DRX((&dbregs
), 3);
566 context
->Dr6
= DBREG_DRX((&dbregs
), 6);
567 context
->Dr7
= DBREG_DRX((&dbregs
), 7);
569 context
->Dr0
= dbregs
.dr0
;
570 context
->Dr1
= dbregs
.dr1
;
571 context
->Dr2
= dbregs
.dr2
;
572 context
->Dr3
= dbregs
.dr3
;
573 context
->Dr6
= dbregs
.dr6
;
574 context
->Dr7
= dbregs
.dr7
;
576 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
578 resume_after_ptrace( thread
);
581 /* set the thread x86 registers */
582 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
584 int pid
= get_ptrace_pid(thread
);
587 /* all other regs are handled on the client side */
588 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
590 if (!suspend_for_ptrace( thread
)) return;
593 /* needed for FreeBSD, the structure fields have changed under 5.x */
594 DBREG_DRX((&dbregs
), 0) = context
->Dr0
;
595 DBREG_DRX((&dbregs
), 1) = context
->Dr1
;
596 DBREG_DRX((&dbregs
), 2) = context
->Dr2
;
597 DBREG_DRX((&dbregs
), 3) = context
->Dr3
;
598 DBREG_DRX((&dbregs
), 4) = 0;
599 DBREG_DRX((&dbregs
), 5) = 0;
600 DBREG_DRX((&dbregs
), 6) = context
->Dr6
;
601 DBREG_DRX((&dbregs
), 7) = context
->Dr7
;
603 dbregs
.dr0
= context
->Dr0
;
604 dbregs
.dr1
= context
->Dr1
;
605 dbregs
.dr2
= context
->Dr2
;
606 dbregs
.dr3
= context
->Dr3
;
609 dbregs
.dr6
= context
->Dr6
;
610 dbregs
.dr7
= context
->Dr7
;
612 if (ptrace( PTRACE_SETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1) file_set_error();
613 else if (thread
->context
) /* update the cached values */
615 thread
->context
->Dr0
= context
->Dr0
;
616 thread
->context
->Dr1
= context
->Dr1
;
617 thread
->context
->Dr2
= context
->Dr2
;
618 thread
->context
->Dr3
= context
->Dr3
;
619 thread
->context
->Dr6
= context
->Dr6
;
620 thread
->context
->Dr7
= context
->Dr7
;
622 resume_after_ptrace( thread
);
625 #else /* linux || __FreeBSD__ */
627 /* retrieve the thread x86 registers */
628 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
632 /* set the thread x86 debug registers */
633 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
637 #endif /* linux || __FreeBSD__ */