blob: b2931267f921adbb5d75f2ab50d75e59634bbfbd [file] [log] [blame]
Alexandre Julliard578c1001999-11-14 21:23:21 +00001/*
2 * Server-side ptrace support
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 */
6
7#include "config.h"
8
9#include <assert.h>
10#include <errno.h>
11#include <stdio.h>
12#include <signal.h>
13#include <sys/ptrace.h>
14#ifdef HAVE_SYS_WAIT_H
15#include <sys/wait.h>
16#endif
17#include <unistd.h>
18
19#include "process.h"
20#include "thread.h"
21
22
23#ifndef PTRACE_CONT
24#define PTRACE_CONT PT_CONTINUE
25#endif
26#ifndef PTRACE_ATTACH
27#define PTRACE_ATTACH PT_ATTACH
28#endif
29#ifndef PTRACE_DETACH
30#define PTRACE_DETACH PT_DETACH
31#endif
32#ifndef PTRACE_PEEKDATA
33#define PTRACE_PEEKDATA PT_READ_D
34#endif
35#ifndef PTRACE_POKEDATA
36#define PTRACE_POKEDATA PT_WRITE_D
37#endif
38
39static const int use_ptrace = 1; /* set to 0 to disable ptrace */
40
41
42/* wait for a ptraced child to get a certain signal */
43/* if the signal is 0, we simply check if anything is pending and return at once */
44void wait4_thread( struct thread *thread, int signal )
45{
46 int status;
47 int pid;
48
49 restart:
50 pid = thread ? thread->unix_pid : -1;
51 if ((pid = wait4( pid, &status, WUNTRACED | (signal ? 0 : WNOHANG), NULL )) == -1)
52 {
53 perror( "wait4" );
54 return;
55 }
56 if (WIFSTOPPED(status))
57 {
58 int sig = WSTOPSIG(status);
59 if (debug_level) fprintf( stderr, "ptrace: pid %d got sig %d\n", pid, sig );
60 switch(sig)
61 {
62 case SIGSTOP: /* continue at once if not suspended */
63 if (!thread)
64 if (!(thread = get_thread_from_pid( pid ))) break;
65 if (!(thread->process->suspend + thread->suspend))
66 ptrace( PTRACE_CONT, pid, 0, sig );
67 break;
68 default: /* ignore other signals for now */
69 ptrace( PTRACE_CONT, pid, 0, sig );
70 break;
71 }
72 if (signal && sig != signal) goto restart;
73 }
74 else if (WIFSIGNALED(status))
75 {
76 int exit_code = WTERMSIG(status);
77 if (debug_level)
78 fprintf( stderr, "ptrace: pid %d killed by sig %d\n", pid, exit_code );
79 if (!thread)
80 if (!(thread = get_thread_from_pid( pid ))) return;
81 if (thread->client) remove_client( thread->client, exit_code );
82 }
83 else if (WIFEXITED(status))
84 {
85 int exit_code = WEXITSTATUS(status);
86 if (debug_level)
87 fprintf( stderr, "ptrace: pid %d exited with status %d\n", pid, exit_code );
88 if (!thread)
89 if (!(thread = get_thread_from_pid( pid ))) return;
90 if (thread->client) remove_client( thread->client, exit_code );
91 }
92 else fprintf( stderr, "wait4: pid %d unknown status %x\n", pid, status );
93}
94
95/* attach to a Unix thread */
96static int attach_thread( struct thread *thread )
97{
98 /* this may fail if the client is already being debugged */
99 if (!use_ptrace || (ptrace( PTRACE_ATTACH, thread->unix_pid, 0, 0 ) == -1)) return 0;
100 if (debug_level) fprintf( stderr, "ptrace: attached to pid %d\n", thread->unix_pid );
101 thread->attached = 1;
102 wait4_thread( thread, SIGSTOP );
103 return 1;
104}
105
106/* detach from a Unix thread and kill it */
107void detach_thread( struct thread *thread )
108{
109 if (!thread->unix_pid) return;
110 kill( thread->unix_pid, SIGTERM );
111 if (thread->suspend + thread->process->suspend) continue_thread( thread );
112 if (thread->attached)
113 {
114 wait4_thread( thread, SIGTERM );
115 if (debug_level) fprintf( stderr, "ptrace: detaching from %d\n", thread->unix_pid );
116 ptrace( PTRACE_DETACH, thread->unix_pid, 0, SIGTERM );
117 thread->attached = 0;
118 }
119}
120
121/* stop a thread (at the Unix level) */
122void stop_thread( struct thread *thread )
123{
124 /* can't stop a thread while initialisation is in progress */
125 if (!thread->unix_pid || thread->process->init_event) return;
126 /* first try to attach to it */
127 if (!thread->attached)
128 if (attach_thread( thread )) return; /* this will have stopped it */
129 /* attached already, or attach failed -> send a signal */
130 kill( thread->unix_pid, SIGSTOP );
131 if (thread->attached) wait4_thread( thread, SIGSTOP );
132}
133
134/* make a thread continue (at the Unix level) */
135void continue_thread( struct thread *thread )
136{
137 if (!thread->unix_pid) return;
138 if (!thread->attached) kill( thread->unix_pid, SIGCONT );
139 else ptrace( PTRACE_CONT, thread->unix_pid, 0, SIGSTOP );
140}
141
142/* read an int from a thread address space */
143int read_thread_int( struct thread *thread, const int *addr, int *data )
144{
145 if (((*data = ptrace( PTRACE_PEEKDATA, thread->unix_pid, addr )) == -1) && errno)
146 {
147 file_set_error();
148 return -1;
149 }
150 return 0;
151}
152
153/* write an int to a thread address space */
154int write_thread_int( struct thread *thread, int *addr, int data, unsigned int mask )
155{
156 int res;
157 if (mask != ~0)
158 {
159 if (read_thread_int( thread, addr, &res ) == -1) return -1;
160 data = (data & mask) | (res & ~mask);
161 }
162 if ((res = ptrace( PTRACE_POKEDATA, thread->unix_pid, addr, data )) == -1) file_set_error();
163 return res;
164}