xref: /minix3/minix/kernel/system/do_trace.c (revision 502b6bda83b3fd4191ca1856b0e005caceec282f)
1433d6423SLionel Sambuc /* The kernel call implemented in this file:
2433d6423SLionel Sambuc  *   m_type:	SYS_TRACE
3433d6423SLionel Sambuc  *
4433d6423SLionel Sambuc  * The parameters for this kernel call are:
5433d6423SLionel Sambuc  *   m_lsys_krn_sys_trace.endpt		process that is traced
6433d6423SLionel Sambuc  *   m_lsys_krn_sys_trace.request	trace request
7433d6423SLionel Sambuc  *   m_lsys_krn_sys_trace.address	address at traced process' space
8433d6423SLionel Sambuc  *   m_lsys_krn_sys_trace.data		data to be written
9433d6423SLionel Sambuc  *   m_krn_lsys_sys_trace.data		data to be returned
10433d6423SLionel Sambuc  */
11433d6423SLionel Sambuc 
12433d6423SLionel Sambuc #include "kernel/system.h"
13433d6423SLionel Sambuc #include <sys/ptrace.h>
14433d6423SLionel Sambuc 
15433d6423SLionel Sambuc #if USE_TRACE
16433d6423SLionel Sambuc 
17433d6423SLionel Sambuc /*==========================================================================*
18433d6423SLionel Sambuc  *				do_trace				    *
19433d6423SLionel Sambuc  *==========================================================================*/
do_trace(struct proc * caller,message * m_ptr)20433d6423SLionel Sambuc int do_trace(struct proc * caller, message * m_ptr)
21433d6423SLionel Sambuc {
22433d6423SLionel Sambuc /* Handle the debugging commands supported by the ptrace system call
23433d6423SLionel Sambuc  * The commands are:
24433d6423SLionel Sambuc  * T_STOP	stop the process
25433d6423SLionel Sambuc  * T_OK		enable tracing by parent for this process
26433d6423SLionel Sambuc  * T_GETINS	return value from instruction space
27433d6423SLionel Sambuc  * T_GETDATA	return value from data space
28433d6423SLionel Sambuc  * T_GETUSER	return value from user process table
29433d6423SLionel Sambuc  * T_SETINS	set value in instruction space
30433d6423SLionel Sambuc  * T_SETDATA	set value in data space
31433d6423SLionel Sambuc  * T_SETUSER	set value in user process table
32433d6423SLionel Sambuc  * T_RESUME	resume execution
33433d6423SLionel Sambuc  * T_EXIT	exit
34433d6423SLionel Sambuc  * T_STEP	set trace bit
35433d6423SLionel Sambuc  * T_SYSCALL	trace system call
36433d6423SLionel Sambuc  * T_ATTACH	attach to an existing process
37433d6423SLionel Sambuc  * T_DETACH	detach from a traced process
38433d6423SLionel Sambuc  * T_SETOPT	set trace options
39433d6423SLionel Sambuc  * T_GETRANGE	get range of values
40433d6423SLionel Sambuc  * T_SETRANGE	set range of values
41433d6423SLionel Sambuc  *
42433d6423SLionel Sambuc  * The T_OK, T_ATTACH, T_EXIT, and T_SETOPT commands are handled completely by
43433d6423SLionel Sambuc  * the process manager. T_GETRANGE and T_SETRANGE use sys_vircopy(). All others
44433d6423SLionel Sambuc  * come here.
45433d6423SLionel Sambuc  */
46433d6423SLionel Sambuc 
47433d6423SLionel Sambuc   register struct proc *rp;
48433d6423SLionel Sambuc   vir_bytes tr_addr = m_ptr->m_lsys_krn_sys_trace.address;
49433d6423SLionel Sambuc   long tr_data = m_ptr->m_lsys_krn_sys_trace.data;
50433d6423SLionel Sambuc   int tr_request = m_ptr->m_lsys_krn_sys_trace.request;
51433d6423SLionel Sambuc   int tr_proc_nr_e = m_ptr->m_lsys_krn_sys_trace.endpt, tr_proc_nr;
52433d6423SLionel Sambuc   unsigned char ub;
53433d6423SLionel Sambuc   int i;
54433d6423SLionel Sambuc 
55433d6423SLionel Sambuc #define COPYTOPROC(addr, myaddr, length) {		\
56433d6423SLionel Sambuc 	struct vir_addr fromaddr, toaddr;		\
57433d6423SLionel Sambuc 	int r;	\
58433d6423SLionel Sambuc 	fromaddr.proc_nr_e = KERNEL;			\
59433d6423SLionel Sambuc 	toaddr.proc_nr_e = tr_proc_nr_e;		\
60433d6423SLionel Sambuc 	fromaddr.offset = (myaddr);			\
61433d6423SLionel Sambuc 	toaddr.offset = (addr);				\
62433d6423SLionel Sambuc 	if((r=virtual_copy_vmcheck(caller, &fromaddr,	\
63433d6423SLionel Sambuc 			&toaddr, length)) != OK) {	\
64433d6423SLionel Sambuc 		printf("Can't copy in sys_trace: %d\n", r);\
65433d6423SLionel Sambuc 		return r;\
66433d6423SLionel Sambuc 	}  \
67433d6423SLionel Sambuc }
68433d6423SLionel Sambuc 
69433d6423SLionel Sambuc #define COPYFROMPROC(addr, myaddr, length) {	\
70433d6423SLionel Sambuc 	struct vir_addr fromaddr, toaddr;		\
71433d6423SLionel Sambuc 	int r;	\
72433d6423SLionel Sambuc 	fromaddr.proc_nr_e = tr_proc_nr_e;		\
73433d6423SLionel Sambuc 	toaddr.proc_nr_e = KERNEL;			\
74433d6423SLionel Sambuc 	fromaddr.offset = (addr);			\
75433d6423SLionel Sambuc 	toaddr.offset = (myaddr);			\
76433d6423SLionel Sambuc 	if((r=virtual_copy_vmcheck(caller, &fromaddr,	\
77433d6423SLionel Sambuc 			&toaddr, length)) != OK) {	\
78433d6423SLionel Sambuc 		printf("Can't copy in sys_trace: %d\n", r);\
79433d6423SLionel Sambuc 		return r;\
80433d6423SLionel Sambuc 	}  \
81433d6423SLionel Sambuc }
82433d6423SLionel Sambuc 
83433d6423SLionel Sambuc   if(!isokendpt(tr_proc_nr_e, &tr_proc_nr)) return(EINVAL);
84433d6423SLionel Sambuc   if (iskerneln(tr_proc_nr)) return(EPERM);
85433d6423SLionel Sambuc 
86433d6423SLionel Sambuc   rp = proc_addr(tr_proc_nr);
87433d6423SLionel Sambuc   if (isemptyp(rp)) return(EINVAL);
88433d6423SLionel Sambuc   switch (tr_request) {
89433d6423SLionel Sambuc   case T_STOP:			/* stop process */
90433d6423SLionel Sambuc 	RTS_SET(rp, RTS_P_STOP);
91433d6423SLionel Sambuc 	/* clear syscall trace and single step flags */
92433d6423SLionel Sambuc 	rp->p_misc_flags &= ~(MF_SC_TRACE | MF_STEP);
93433d6423SLionel Sambuc 	return(OK);
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc   case T_GETINS:		/* return value from instruction space */
96433d6423SLionel Sambuc 	COPYFROMPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
97*502b6bdaSDavid van Moolenbroek 	m_ptr->m_krn_lsys_sys_trace.data = tr_data;
98433d6423SLionel Sambuc 	break;
99433d6423SLionel Sambuc 
100433d6423SLionel Sambuc   case T_GETDATA:		/* return value from data space */
101433d6423SLionel Sambuc 	COPYFROMPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
102*502b6bdaSDavid van Moolenbroek 	m_ptr->m_krn_lsys_sys_trace.data= tr_data;
103433d6423SLionel Sambuc 	break;
104433d6423SLionel Sambuc 
105433d6423SLionel Sambuc   case T_GETUSER:		/* return value from process table */
106433d6423SLionel Sambuc 	if ((tr_addr & (sizeof(long) - 1)) != 0) return(EFAULT);
107433d6423SLionel Sambuc 
108433d6423SLionel Sambuc 	if (tr_addr <= sizeof(struct proc) - sizeof(long)) {
109*502b6bdaSDavid van Moolenbroek 		m_ptr->m_krn_lsys_sys_trace.data =
110*502b6bdaSDavid van Moolenbroek 		    *(long *) ((char *) rp + (int) tr_addr);
111433d6423SLionel Sambuc 		break;
112433d6423SLionel Sambuc 	}
113433d6423SLionel Sambuc 
114433d6423SLionel Sambuc 	/* The process's proc struct is followed by its priv struct.
115433d6423SLionel Sambuc 	 * The alignment here should be unnecessary, but better safe..
116433d6423SLionel Sambuc 	 */
117433d6423SLionel Sambuc 	i = sizeof(long) - 1;
118433d6423SLionel Sambuc 	tr_addr -= (sizeof(struct proc) + i) & ~i;
119433d6423SLionel Sambuc 
120433d6423SLionel Sambuc 	if (tr_addr > sizeof(struct priv) - sizeof(long)) return(EFAULT);
121433d6423SLionel Sambuc 
122*502b6bdaSDavid van Moolenbroek 	m_ptr->m_krn_lsys_sys_trace.data =
123*502b6bdaSDavid van Moolenbroek 	    *(long *) ((char *) rp->p_priv + (int) tr_addr);
124433d6423SLionel Sambuc 	break;
125433d6423SLionel Sambuc 
126433d6423SLionel Sambuc   case T_SETINS:		/* set value in instruction space */
127433d6423SLionel Sambuc 	COPYTOPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
128433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = 0;
129433d6423SLionel Sambuc 	break;
130433d6423SLionel Sambuc 
131433d6423SLionel Sambuc   case T_SETDATA:			/* set value in data space */
132433d6423SLionel Sambuc 	COPYTOPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
133433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = 0;
134433d6423SLionel Sambuc 	break;
135433d6423SLionel Sambuc 
136433d6423SLionel Sambuc   case T_SETUSER:			/* set value in process table */
137433d6423SLionel Sambuc 	if ((tr_addr & (sizeof(reg_t) - 1)) != 0 ||
138433d6423SLionel Sambuc 	     tr_addr > sizeof(struct stackframe_s) - sizeof(reg_t))
139433d6423SLionel Sambuc 		return(EFAULT);
140433d6423SLionel Sambuc 	i = (int) tr_addr;
141433d6423SLionel Sambuc #if defined(__i386__)
142433d6423SLionel Sambuc 	/* Altering segment registers might crash the kernel when it
143433d6423SLionel Sambuc 	 * tries to load them prior to restarting a process, so do
144433d6423SLionel Sambuc 	 * not allow it.
145433d6423SLionel Sambuc 	 */
146433d6423SLionel Sambuc 	if (i == (int) &((struct proc *) 0)->p_reg.cs ||
147433d6423SLionel Sambuc 	    i == (int) &((struct proc *) 0)->p_reg.ds ||
148433d6423SLionel Sambuc 	    i == (int) &((struct proc *) 0)->p_reg.es ||
149433d6423SLionel Sambuc 	    i == (int) &((struct proc *) 0)->p_reg.gs ||
150433d6423SLionel Sambuc 	    i == (int) &((struct proc *) 0)->p_reg.fs ||
151433d6423SLionel Sambuc 	    i == (int) &((struct proc *) 0)->p_reg.ss)
152433d6423SLionel Sambuc 		return(EFAULT);
153433d6423SLionel Sambuc 
154433d6423SLionel Sambuc 	if (i == (int) &((struct proc *) 0)->p_reg.psw)
155433d6423SLionel Sambuc 		/* only selected bits are changeable */
156433d6423SLionel Sambuc 		SETPSW(rp, tr_data);
157433d6423SLionel Sambuc 	else
158433d6423SLionel Sambuc 		*(reg_t *) ((char *) &rp->p_reg + i) = (reg_t) tr_data;
159433d6423SLionel Sambuc #elif defined(__arm__)
160433d6423SLionel Sambuc 	if (i == (int) &((struct proc *) 0)->p_reg.psr) {
161433d6423SLionel Sambuc 		/* only selected bits are changeable */
162433d6423SLionel Sambuc 		SET_USR_PSR(rp, tr_data);
163433d6423SLionel Sambuc 	} else {
164433d6423SLionel Sambuc 		*(reg_t *) ((char *) &rp->p_reg + i) = (reg_t) tr_data;
165433d6423SLionel Sambuc 	}
166433d6423SLionel Sambuc #endif
167433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = 0;
168433d6423SLionel Sambuc 	break;
169433d6423SLionel Sambuc 
170433d6423SLionel Sambuc   case T_DETACH:		/* detach tracer */
171433d6423SLionel Sambuc 	rp->p_misc_flags &= ~MF_SC_ACTIVE;
172433d6423SLionel Sambuc 
173433d6423SLionel Sambuc 	/* fall through */
174433d6423SLionel Sambuc   case T_RESUME:		/* resume execution */
175433d6423SLionel Sambuc 	RTS_UNSET(rp, RTS_P_STOP);
176433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = 0;
177433d6423SLionel Sambuc 	break;
178433d6423SLionel Sambuc 
179433d6423SLionel Sambuc   case T_STEP:			/* set trace bit */
180433d6423SLionel Sambuc 	rp->p_misc_flags |= MF_STEP;
181433d6423SLionel Sambuc 	RTS_UNSET(rp, RTS_P_STOP);
182433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = 0;
183433d6423SLionel Sambuc 	break;
184433d6423SLionel Sambuc 
185433d6423SLionel Sambuc   case T_SYSCALL:		/* trace system call */
186433d6423SLionel Sambuc 	rp->p_misc_flags |= MF_SC_TRACE;
187433d6423SLionel Sambuc 	RTS_UNSET(rp, RTS_P_STOP);
188433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = 0;
189433d6423SLionel Sambuc 	break;
190433d6423SLionel Sambuc 
191433d6423SLionel Sambuc   case T_READB_INS:		/* get value from instruction space */
192433d6423SLionel Sambuc 	COPYFROMPROC(tr_addr, (vir_bytes) &ub, 1);
193433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = ub;
194433d6423SLionel Sambuc 	break;
195433d6423SLionel Sambuc 
196433d6423SLionel Sambuc   case T_WRITEB_INS:		/* set value in instruction space */
197433d6423SLionel Sambuc 	ub = (unsigned char) (tr_data & 0xff);
198433d6423SLionel Sambuc 	COPYTOPROC(tr_addr, (vir_bytes) &ub, 1);
199433d6423SLionel Sambuc 	m_ptr->m_krn_lsys_sys_trace.data = 0;
200433d6423SLionel Sambuc 	break;
201433d6423SLionel Sambuc 
202433d6423SLionel Sambuc   default:
203433d6423SLionel Sambuc 	return(EINVAL);
204433d6423SLionel Sambuc   }
205433d6423SLionel Sambuc   return(OK);
206433d6423SLionel Sambuc }
207433d6423SLionel Sambuc 
208433d6423SLionel Sambuc #endif /* USE_TRACE */
209