1 /* This file handles the process manager's part of debugging, using the
2 * ptrace system call. Most of the commands are passed on to the system
5 * The debugging commands available are:
6 * T_STOP stop the process
7 * T_OK enable tracing by parent for this process
8 * T_GETINS return value from instruction space
9 * T_GETDATA return value from data space
10 * T_GETUSER return value from user process table
11 * T_SETINS set value in instruction space
12 * T_SETDATA set value in data space
13 * T_SETUSER set value in user process table
14 * T_RESUME resume execution
16 * T_STEP set trace bit
17 * T_SYSCALL trace system call
18 * T_ATTACH attach to an existing process
19 * T_DETACH detach from a traced process
20 * T_SETOPT set trace options
21 * T_GETRANGE get range of values
22 * T_SETRANGE set range of values
24 * The T_OK, T_ATTACH, T_EXIT, and T_SETOPT commands are handled here, and the
25 * T_RESUME, T_STEP, T_SYSCALL, and T_DETACH commands are partially handled
26 * here and completed by the system task. The rest are handled entirely by the
31 #include <minix/com.h>
32 #include <sys/ptrace.h>
37 /*===========================================================================*
39 *===========================================================================*/
42 register struct mproc
*child
;
43 struct ptrace_range pr
;
48 /* The T_OK call is made by the child fork of the debugger before it execs
49 * the process to be traced. The T_ATTACH call is made by the debugger itself
50 * to attach to an existing process.
53 case T_OK
: /* enable tracing by parent for this proc */
54 if (mp
->mp_tracer
!= NO_TRACER
) return(EBUSY
);
56 mp
->mp_tracer
= mp
->mp_parent
;
57 mp
->mp_reply
.reply_trace
= 0;
60 case T_ATTACH
: /* attach to an existing process */
61 if ((child
= find_proc(m_in
.pid
)) == NIL_MPROC
) return(ESRCH
);
62 if (child
->mp_flags
& EXITING
) return(ESRCH
);
64 /* For non-root processes, user and group ID must match. */
65 if (mp
->mp_effuid
!= SUPER_USER
&&
66 (mp
->mp_effuid
!= child
->mp_effuid
||
67 mp
->mp_effgid
!= child
->mp_effgid
||
68 child
->mp_effuid
!= child
->mp_realuid
||
69 child
->mp_effgid
!= child
->mp_realgid
)) return(EPERM
);
71 /* Only root may trace system servers. */
72 if (mp
->mp_effuid
!= SUPER_USER
&& (child
->mp_flags
& PRIV_PROC
))
75 /* System servers may not trace anyone. They can use sys_trace(). */
76 if (mp
->mp_flags
& PRIV_PROC
) return(EPERM
);
78 /* Can't trace self, PM or VM. */
79 if (child
== mp
|| child
->mp_endpoint
== PM_PROC_NR
||
80 child
->mp_endpoint
== VM_PROC_NR
) return(EPERM
);
82 /* Can't trace a process that is already being traced. */
83 if (child
->mp_tracer
!= NO_TRACER
) return(EBUSY
);
85 child
->mp_tracer
= who_p
;
86 child
->mp_trace_flags
= TO_NOEXEC
;
88 sig_proc(child
, SIGSTOP
, TRUE
/*trace*/);
90 mp
->mp_reply
.reply_trace
= 0;
93 case T_STOP
: /* stop the process */
94 /* This call is not exposed to user programs, because its effect can be
95 * achieved better by sending the traced process a signal with kill(2).
99 case T_READB_INS
: /* special hack for reading text segments */
100 if (mp
->mp_effuid
!= SUPER_USER
) return(EPERM
);
101 if ((child
= find_proc(m_in
.pid
)) == NIL_MPROC
) return(ESRCH
);
102 if (child
->mp_flags
& EXITING
) return(ESRCH
);
104 r
= sys_trace(req
, child
->mp_endpoint
, m_in
.PMTRACE_ADDR
, &m_in
.data
);
105 if (r
!= OK
) return(r
);
107 mp
->mp_reply
.reply_trace
= m_in
.data
;
110 case T_WRITEB_INS
: /* special hack for patching text segments */
111 if (mp
->mp_effuid
!= SUPER_USER
) return(EPERM
);
112 if ((child
= find_proc(m_in
.pid
)) == NIL_MPROC
) return(ESRCH
);
113 if (child
->mp_flags
& EXITING
) return(ESRCH
);
116 /* Should check for shared text */
118 /* Make sure the text segment is not used as a source for shared
126 r
= sys_trace(req
, child
->mp_endpoint
, m_in
.PMTRACE_ADDR
, &m_in
.data
);
127 if (r
!= OK
) return(r
);
129 mp
->mp_reply
.reply_trace
= m_in
.data
;
133 /* All the other calls are made by the tracing process to control execution
134 * of the child. For all these calls, the child must be stopped.
136 if ((child
= find_proc(m_in
.pid
)) == NIL_MPROC
) return(ESRCH
);
137 if (child
->mp_flags
& EXITING
) return(ESRCH
);
138 if (child
->mp_tracer
!= who_p
) return(ESRCH
);
139 if (!(child
->mp_flags
& STOPPED
)) return(EBUSY
);
142 case T_EXIT
: /* exit */
143 child
->mp_flags
|= TRACE_EXIT
;
145 /* Defer the exit if the traced process has an FS call pending. */
146 if (child
->mp_flags
& FS_CALL
)
147 child
->mp_exitstatus
= (int) m_in
.data
; /* save for later */
149 exit_proc(child
, (int) m_in
.data
, FALSE
/*dump_core*/);
151 /* Do not reply to the caller until FS has processed the exit
156 case T_SETOPT
: /* set trace options */
157 child
->mp_trace_flags
= m_in
.data
;
159 mp
->mp_reply
.reply_trace
= 0;
163 case T_SETRANGE
: /* get/set range of values */
164 r
= sys_datacopy(who_e
, (vir_bytes
) m_in
.PMTRACE_ADDR
,
165 SELF
, (vir_bytes
) &pr
, (phys_bytes
) sizeof(pr
));
166 if (r
!= OK
) return(r
);
168 if (pr
.pr_space
!= TS_INS
&& pr
.pr_space
!= TS_DATA
) return(EINVAL
);
169 if (pr
.pr_size
== 0 || pr
.pr_size
> LONG_MAX
) return(EINVAL
);
171 seg
= (pr
.pr_space
== TS_INS
) ? T
: D
;
172 if (req
== T_GETRANGE
)
173 r
= sys_vircopy(child
->mp_endpoint
, seg
, (vir_bytes
) pr
.pr_addr
,
174 who_e
, D
, (vir_bytes
) pr
.pr_ptr
,
175 (phys_bytes
) pr
.pr_size
);
177 r
= sys_vircopy(who_e
, D
, (vir_bytes
) pr
.pr_ptr
,
178 child
->mp_endpoint
, seg
, (vir_bytes
) pr
.pr_addr
,
179 (phys_bytes
) pr
.pr_size
);
181 if (r
!= OK
) return(r
);
183 mp
->mp_reply
.reply_trace
= 0;
186 case T_DETACH
: /* detach from traced process */
187 if (m_in
.data
< 0 || m_in
.data
>= _NSIG
) return(EINVAL
);
189 child
->mp_tracer
= NO_TRACER
;
191 /* Let all tracer-pending signals through the filter. */
192 for (i
= 1; i
< _NSIG
; i
++) {
193 if (sigismember(&child
->mp_sigtrace
, i
)) {
194 sigdelset(&child
->mp_sigtrace
, i
);
195 check_sig(child
->mp_pid
, i
);
199 if (m_in
.data
> 0) { /* issue signal */
200 sig_proc(child
, (int) m_in
.data
, TRUE
/*trace*/);
203 /* Resume the child as if nothing ever happened. */
204 child
->mp_flags
&= ~STOPPED
;
205 child
->mp_trace_flags
= 0;
207 check_pending(child
);
213 case T_SYSCALL
: /* resume execution */
214 if (m_in
.data
< 0 || m_in
.data
>= _NSIG
) return(EINVAL
);
216 if (m_in
.data
> 0) { /* issue signal */
217 sig_proc(child
, (int) m_in
.data
, FALSE
/*trace*/);
220 /* If there are any other signals waiting to be delivered,
221 * feign a successful resumption.
223 for (i
= 1; i
< _NSIG
; i
++) {
224 if (sigismember(&child
->mp_sigtrace
, i
)) {
225 mp
->mp_reply
.reply_trace
= 0;
230 child
->mp_flags
&= ~STOPPED
;
232 check_pending(child
);
236 r
= sys_trace(req
, child
->mp_endpoint
, m_in
.PMTRACE_ADDR
, &m_in
.data
);
237 if (r
!= OK
) return(r
);
239 mp
->mp_reply
.reply_trace
= m_in
.data
;
243 /*===========================================================================*
245 *===========================================================================*/
246 PUBLIC
void stop_proc(rmp
, signo
)
247 register struct mproc
*rmp
;
250 /* A traced process got a signal so stop it. */
252 register struct mproc
*rpmp
= mproc
+ rmp
->mp_tracer
;
255 r
= sys_trace(T_STOP
, rmp
->mp_endpoint
, 0L, (long *) 0);
256 if (r
!= OK
) panic("pm", "sys_trace failed", r
);
258 rmp
->mp_flags
|= STOPPED
;
259 if (wait_test(rpmp
, rmp
)) {
260 sigdelset(&rmp
->mp_sigtrace
, signo
);
262 rpmp
->mp_flags
&= ~WAITING
; /* parent is no longer waiting */
263 rpmp
->mp_reply
.reply_res2
= 0177 | (signo
<< 8);
264 setreply(rmp
->mp_tracer
, rmp
->mp_pid
);