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
51 #define PTRACE_CONT PT_CONTINUE
53 #ifndef PTRACE_SINGLESTEP
54 #define PTRACE_SINGLESTEP PT_STEP
57 #define PTRACE_ATTACH PT_ATTACH
60 #define PTRACE_DETACH PT_DETACH
62 #ifndef PTRACE_PEEKDATA
63 #define PTRACE_PEEKDATA PT_READ_D
65 #ifndef PTRACE_POKEDATA
66 #define PTRACE_POKEDATA PT_WRITE_D
68 #ifndef PTRACE_PEEKUSER
69 #define PTRACE_PEEKUSER PT_READ_U
71 #ifndef PTRACE_POKEUSER
72 #define PTRACE_POKEUSER PT_WRITE_U
76 #define PTRACE_GETDBREGS PT_GETDBREGS
79 #define PTRACE_SETDBREGS PT_SETDBREGS
82 #ifndef HAVE_SYS_PTRACE_H
89 inline static int ptrace(int req
, ...) { errno
= EPERM
; return -1; /*FAIL*/ }
90 #endif /* HAVE_SYS_PTRACE_H */
92 /* handle a status returned by wait4 */
93 static int handle_child_status( struct thread
*thread
, int pid
, int status
, int want_sig
)
95 if (WIFSTOPPED(status
))
97 int sig
= WSTOPSIG(status
);
98 if (debug_level
&& thread
)
99 fprintf( stderr
, "%04x: *signal* signal=%d\n", thread
->id
, sig
);
102 /* ignore other signals for now */
103 ptrace( PTRACE_CONT
, pid
, (caddr_t
)1, sig
);
107 if (thread
&& (WIFSIGNALED(status
) || WIFEXITED(status
)))
109 thread
->unix_pid
= -1;
110 thread
->unix_tid
= -1;
113 if (WIFSIGNALED(status
))
114 fprintf( stderr
, "%04x: *exited* signal=%d\n",
115 thread
->id
, WTERMSIG(status
) );
117 fprintf( stderr
, "%04x: *exited* status=%d\n",
118 thread
->id
, WEXITSTATUS(status
) );
124 /* wait4 wrapper to handle missing __WALL flag in older kernels */
125 static inline pid_t
wait4_wrapper( pid_t pid
, int *status
, int options
, struct rusage
*usage
)
128 static int wall_flag
= __WALL
;
132 pid_t ret
= wait4( pid
, status
, options
| wall_flag
, usage
);
133 if (ret
!= -1 || !wall_flag
|| errno
!= EINVAL
) return ret
;
137 return wait4( pid
, status
, options
, usage
);
141 /* handle a SIGCHLD signal */
142 void sigchld_callback(void)
148 if (!(pid
= wait4_wrapper( -1, &status
, WUNTRACED
| WNOHANG
, NULL
))) break;
151 struct thread
*thread
= get_thread_from_tid( pid
);
152 if (!thread
) thread
= get_thread_from_pid( pid
);
153 handle_child_status( thread
, pid
, status
, -1 );
159 /* return the Unix pid to use in ptrace calls for a given thread */
160 static int get_ptrace_pid( struct thread
*thread
)
162 if (thread
->unix_tid
!= -1) return thread
->unix_tid
;
163 return thread
->unix_pid
;
166 /* wait for a ptraced child to get a certain signal */
167 static int wait4_thread( struct thread
*thread
, int signal
)
174 if ((res
= wait4_wrapper( get_ptrace_pid(thread
), &status
, WUNTRACED
, NULL
)) == -1)
178 if (!watchdog_triggered()) continue;
179 if (debug_level
) fprintf( stderr
, "%04x: *watchdog* wait4 aborted\n", thread
->id
);
181 else if (errno
== ECHILD
) /* must have died */
183 thread
->unix_pid
= -1;
184 thread
->unix_tid
= -1;
186 else perror( "wait4" );
190 res
= handle_child_status( thread
, res
, status
, signal
);
191 if (!res
|| res
== signal
) break;
194 return (thread
->unix_pid
!= -1);
197 /* send a signal to a specific thread */
198 static inline int tkill( int tgid
, int pid
, int sig
)
204 __asm__( "pushl %%ebx\n\t"
209 : "0" (270) /*SYS_tgkill*/, "r" (tgid
), "c" (pid
), "d" (sig
) );
211 __asm__( "pushl %%ebx\n\t"
216 : "0" (238) /*SYS_tkill*/, "r" (pid
), "c" (sig
) );
217 # elif defined(__x86_64__)
218 __asm__( "syscall" : "=a" (ret
)
219 : "0" (200) /*SYS_tkill*/, "D" (pid
), "S" (sig
) );
221 #endif /* __linux__ */
223 if (ret
>= 0) return ret
;
228 /* initialize the process tracing mechanism */
229 void init_tracing_mechanism(void)
231 /* no initialization needed for ptrace */
234 /* initialize the per-process tracing mechanism */
235 void init_process_tracing( struct process
*process
)
237 /* ptrace setup is done on-demand */
240 /* terminate the per-process tracing mechanism */
241 void finish_process_tracing( struct process
*process
)
245 /* send a Unix signal to a specific thread */
246 int send_thread_signal( struct thread
*thread
, int sig
)
250 if (thread
->unix_pid
!= -1)
252 if (thread
->unix_tid
!= -1)
254 ret
= tkill( thread
->unix_pid
, thread
->unix_tid
, sig
);
255 if (ret
== -1 && errno
== ENOSYS
) ret
= kill( thread
->unix_pid
, sig
);
257 else ret
= kill( thread
->unix_pid
, sig
);
259 if (ret
== -1 && errno
== ESRCH
) /* thread got killed */
261 thread
->unix_pid
= -1;
262 thread
->unix_tid
= -1;
268 /* resume a thread after we have used ptrace on it */
269 static void resume_after_ptrace( struct thread
*thread
)
271 if (thread
->unix_pid
== -1) return;
272 if (ptrace( PTRACE_DETACH
, get_ptrace_pid(thread
), (caddr_t
)1, 0 ) == -1)
274 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
278 /* suspend a thread to allow using ptrace on it */
279 /* you must do a resume_after_ptrace when finished with the thread */
280 static int suspend_for_ptrace( struct thread
*thread
)
282 /* can't stop a thread while initialisation is in progress */
283 if (thread
->unix_pid
== -1 || !is_process_init_done(thread
->process
)) goto error
;
285 /* this may fail if the client is already being debugged */
286 if (ptrace( PTRACE_ATTACH
, get_ptrace_pid(thread
), 0, 0 ) == -1)
288 if (errno
== ESRCH
) thread
->unix_pid
= thread
->unix_tid
= -1; /* thread got killed */
291 if (wait4_thread( thread
, SIGSTOP
)) return 1;
292 resume_after_ptrace( thread
);
294 set_error( STATUS_ACCESS_DENIED
);
298 /* read an int from a thread address space */
299 static int read_thread_int( struct thread
*thread
, int *addr
, int *data
)
302 *data
= ptrace( PTRACE_PEEKDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, 0 );
303 if ( *data
== -1 && errno
)
311 /* write an int to a thread address space */
312 static int write_thread_int( struct thread
*thread
, int *addr
, int data
, unsigned int mask
)
317 if (read_thread_int( thread
, addr
, &res
) == -1) return -1;
318 data
= (data
& mask
) | (res
& ~mask
);
320 if ((res
= ptrace( PTRACE_POKEDATA
, get_ptrace_pid(thread
), (caddr_t
)addr
, data
)) == -1)
325 /* return a thread of the process suitable for ptracing */
326 static struct thread
*get_ptrace_thread( struct process
*process
)
328 struct thread
*thread
;
330 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
332 if (thread
->unix_pid
!= -1) return thread
;
334 set_error( STATUS_ACCESS_DENIED
); /* process is dead */
338 /* read data from a process memory space */
339 int read_process_memory( struct process
*process
, const void *ptr
, data_size_t size
, char *dest
)
341 struct thread
*thread
= get_ptrace_thread( process
);
342 unsigned int first_offset
, last_offset
, len
;
345 if (!thread
) return 0;
347 first_offset
= (unsigned long)ptr
% sizeof(int);
348 last_offset
= (size
+ first_offset
) % sizeof(int);
349 if (!last_offset
) last_offset
= sizeof(int);
351 addr
= (int *)((char *)ptr
- first_offset
);
352 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
354 if (suspend_for_ptrace( thread
))
358 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
359 memcpy( dest
, (char *)&data
+ first_offset
, sizeof(int) - first_offset
);
360 dest
+= sizeof(int) - first_offset
;
367 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
368 memcpy( dest
, &data
, sizeof(int) );
373 if (read_thread_int( thread
, addr
++, &data
) == -1) goto done
;
374 memcpy( dest
, (char *)&data
+ first_offset
, last_offset
- first_offset
);
378 resume_after_ptrace( thread
);
383 /* make sure we can write to the whole address range */
384 /* len is the total size (in ints) */
385 static int check_process_write_access( struct thread
*thread
, int *addr
, data_size_t len
)
387 int page
= get_page_size() / sizeof(int);
391 if (write_thread_int( thread
, addr
, 0, 0 ) == -1) return 0;
392 if (len
<= page
) break;
396 return (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) != -1);
399 /* write data to a process memory space */
400 int write_process_memory( struct process
*process
, void *ptr
, data_size_t size
, const char *src
)
402 struct thread
*thread
= get_ptrace_thread( process
);
403 int ret
= 0, data
= 0;
406 unsigned int first_mask
, first_offset
, last_mask
, last_offset
;
408 if (!thread
) return 0;
410 /* compute the mask for the first int */
412 first_offset
= (unsigned long)ptr
% sizeof(int);
413 memset( &first_mask
, 0, first_offset
);
415 /* compute the mask for the last int */
416 last_offset
= (size
+ first_offset
) % sizeof(int);
417 if (!last_offset
) last_offset
= sizeof(int);
419 memset( &last_mask
, 0xff, last_offset
);
421 addr
= (int *)((char *)ptr
- first_offset
);
422 len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
424 if (suspend_for_ptrace( thread
))
426 if (!check_process_write_access( thread
, addr
, len
))
428 set_error( STATUS_ACCESS_DENIED
);
431 /* first word is special */
434 memcpy( (char *)&data
+ first_offset
, src
, sizeof(int) - first_offset
);
435 src
+= sizeof(int) - first_offset
;
436 if (write_thread_int( thread
, addr
++, data
, first_mask
) == -1) goto done
;
440 else last_mask
&= first_mask
;
444 memcpy( &data
, src
, sizeof(int) );
446 if (write_thread_int( thread
, addr
++, data
, ~0 ) == -1) goto done
;
450 /* last word is special too */
451 memcpy( (char *)&data
+ first_offset
, src
, last_offset
- first_offset
);
452 if (write_thread_int( thread
, addr
, data
, last_mask
) == -1) goto done
;
456 resume_after_ptrace( thread
);
461 /* retrieve an LDT selector entry */
462 void get_selector_entry( struct thread
*thread
, int entry
, unsigned int *base
,
463 unsigned int *limit
, unsigned char *flags
)
465 if (!thread
->process
->ldt_copy
)
467 set_error( STATUS_ACCESS_DENIED
);
472 set_error( STATUS_ACCESS_VIOLATION
);
475 if (suspend_for_ptrace( thread
))
477 unsigned char flags_buf
[4];
478 int *addr
= (int *)thread
->process
->ldt_copy
+ entry
;
479 if (read_thread_int( thread
, addr
, (int *)base
) == -1) goto done
;
480 if (read_thread_int( thread
, addr
+ 8192, (int *)limit
) == -1) goto done
;
481 addr
= (int *)thread
->process
->ldt_copy
+ 2*8192 + (entry
>> 2);
482 if (read_thread_int( thread
, addr
, (int *)flags_buf
) == -1) goto done
;
483 *flags
= flags_buf
[entry
& 3];
485 resume_after_ptrace( thread
);
490 #if defined(linux) && (defined(__i386__) || defined(__x86_64__))
492 #ifdef HAVE_SYS_USER_H
493 # include <sys/user.h>
496 /* debug register offset in struct user */
497 #define DR_OFFSET(dr) ((((struct user *)0)->u_debugreg) + (dr))
499 /* retrieve the thread x86 registers */
500 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
502 int i
, pid
= get_ptrace_pid(thread
);
505 /* all other regs are handled on the client side */
506 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
508 if (!suspend_for_ptrace( thread
)) return;
510 for (i
= 0; i
< 8; i
++)
512 if (i
== 4 || i
== 5) continue;
514 data
[i
] = ptrace( PTRACE_PEEKUSER
, pid
, DR_OFFSET(i
), 0 );
515 if ((data
[i
] == -1) && errno
)
521 context
->Dr0
= data
[0];
522 context
->Dr1
= data
[1];
523 context
->Dr2
= data
[2];
524 context
->Dr3
= data
[3];
525 context
->Dr6
= data
[6];
526 context
->Dr7
= data
[7];
527 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
529 resume_after_ptrace( thread
);
532 /* set the thread x86 registers */
533 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
535 int pid
= get_ptrace_pid( thread
);
537 /* all other regs are handled on the client side */
538 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
540 if (!suspend_for_ptrace( thread
)) return;
542 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(0), context
->Dr0
) == -1) goto error
;
543 if (thread
->context
) thread
->context
->Dr0
= context
->Dr0
;
544 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(1), context
->Dr1
) == -1) goto error
;
545 if (thread
->context
) thread
->context
->Dr1
= context
->Dr1
;
546 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(2), context
->Dr2
) == -1) goto error
;
547 if (thread
->context
) thread
->context
->Dr2
= context
->Dr2
;
548 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(3), context
->Dr3
) == -1) goto error
;
549 if (thread
->context
) thread
->context
->Dr3
= context
->Dr3
;
550 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(6), context
->Dr6
) == -1) goto error
;
551 if (thread
->context
) thread
->context
->Dr6
= context
->Dr6
;
552 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(7), context
->Dr7
) == -1) goto error
;
553 if (thread
->context
) thread
->context
->Dr7
= context
->Dr7
;
554 resume_after_ptrace( thread
);
558 resume_after_ptrace( thread
);
561 #elif defined(__i386__) && defined(PTRACE_GETDBREGS) && defined(PTRACE_SETDBREGS) && \
562 (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__))
564 #include <machine/reg.h>
566 /* retrieve the thread x86 registers */
567 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
569 int pid
= get_ptrace_pid(thread
);
572 /* all other regs are handled on the client side */
573 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
575 if (!suspend_for_ptrace( thread
)) return;
577 if (ptrace( PTRACE_GETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1) file_set_error();
581 /* needed for FreeBSD, the structure fields have changed under 5.x */
582 context
->Dr0
= DBREG_DRX((&dbregs
), 0);
583 context
->Dr1
= DBREG_DRX((&dbregs
), 1);
584 context
->Dr2
= DBREG_DRX((&dbregs
), 2);
585 context
->Dr3
= DBREG_DRX((&dbregs
), 3);
586 context
->Dr6
= DBREG_DRX((&dbregs
), 6);
587 context
->Dr7
= DBREG_DRX((&dbregs
), 7);
589 context
->Dr0
= dbregs
.dr0
;
590 context
->Dr1
= dbregs
.dr1
;
591 context
->Dr2
= dbregs
.dr2
;
592 context
->Dr3
= dbregs
.dr3
;
593 context
->Dr6
= dbregs
.dr6
;
594 context
->Dr7
= dbregs
.dr7
;
596 context
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
598 resume_after_ptrace( thread
);
601 /* set the thread x86 registers */
602 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
604 int pid
= get_ptrace_pid(thread
);
607 /* all other regs are handled on the client side */
608 assert( (flags
| CONTEXT_i386
) == CONTEXT_DEBUG_REGISTERS
);
610 if (!suspend_for_ptrace( thread
)) return;
613 /* needed for FreeBSD, the structure fields have changed under 5.x */
614 DBREG_DRX((&dbregs
), 0) = context
->Dr0
;
615 DBREG_DRX((&dbregs
), 1) = context
->Dr1
;
616 DBREG_DRX((&dbregs
), 2) = context
->Dr2
;
617 DBREG_DRX((&dbregs
), 3) = context
->Dr3
;
618 DBREG_DRX((&dbregs
), 4) = 0;
619 DBREG_DRX((&dbregs
), 5) = 0;
620 DBREG_DRX((&dbregs
), 6) = context
->Dr6
;
621 DBREG_DRX((&dbregs
), 7) = context
->Dr7
;
623 dbregs
.dr0
= context
->Dr0
;
624 dbregs
.dr1
= context
->Dr1
;
625 dbregs
.dr2
= context
->Dr2
;
626 dbregs
.dr3
= context
->Dr3
;
629 dbregs
.dr6
= context
->Dr6
;
630 dbregs
.dr7
= context
->Dr7
;
632 if (ptrace( PTRACE_SETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1) file_set_error();
633 else if (thread
->context
) /* update the cached values */
635 thread
->context
->Dr0
= context
->Dr0
;
636 thread
->context
->Dr1
= context
->Dr1
;
637 thread
->context
->Dr2
= context
->Dr2
;
638 thread
->context
->Dr3
= context
->Dr3
;
639 thread
->context
->Dr6
= context
->Dr6
;
640 thread
->context
->Dr7
= context
->Dr7
;
642 resume_after_ptrace( thread
);
645 #else /* linux || __FreeBSD__ */
647 /* retrieve the thread x86 registers */
648 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
652 /* set the thread x86 debug registers */
653 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
657 #endif /* linux || __FreeBSD__ */
659 #endif /* USE_PTRACE */