ramdisk Makefile: CLEANFILES fix
[minix.git] / kernel / system / do_trace.c
blob24bfcc5883bf433d2e3cd1274ceb4a84326dac07
1 /* The kernel call implemented in this file:
2 * m_type: SYS_TRACE
4 * The parameters for this kernel call are:
5 * m2_i1: CTL_ENDPT process that is traced
6 * m2_i2: CTL_REQUEST trace request
7 * m2_l1: CTL_ADDRESS address at traced process' space
8 * m2_l2: CTL_DATA data to be written or returned here
9 */
11 #include "kernel/system.h"
12 #include <sys/ptrace.h>
14 #if USE_TRACE
16 /*==========================================================================*
17 * do_trace *
18 *==========================================================================*/
19 int do_trace(struct proc * caller, message * m_ptr)
21 /* Handle the debugging commands supported by the ptrace system call
22 * The commands are:
23 * T_STOP stop the process
24 * T_OK enable tracing by parent for this process
25 * T_GETINS return value from instruction space
26 * T_GETDATA return value from data space
27 * T_GETUSER return value from user process table
28 * T_SETINS set value in instruction space
29 * T_SETDATA set value in data space
30 * T_SETUSER set value in user process table
31 * T_RESUME resume execution
32 * T_EXIT exit
33 * T_STEP set trace bit
34 * T_SYSCALL trace system call
35 * T_ATTACH attach to an existing process
36 * T_DETACH detach from a traced process
37 * T_SETOPT set trace options
38 * T_GETRANGE get range of values
39 * T_SETRANGE set range of values
41 * The T_OK, T_ATTACH, T_EXIT, and T_SETOPT commands are handled completely by
42 * the process manager. T_GETRANGE and T_SETRANGE use sys_vircopy(). All others
43 * come here.
46 register struct proc *rp;
47 vir_bytes tr_addr = (vir_bytes) m_ptr->CTL_ADDRESS;
48 long tr_data = m_ptr->CTL_DATA;
49 int tr_request = m_ptr->CTL_REQUEST;
50 int tr_proc_nr_e = m_ptr->CTL_ENDPT, tr_proc_nr;
51 unsigned char ub;
52 int i;
54 #define COPYTOPROC(addr, myaddr, length) { \
55 struct vir_addr fromaddr, toaddr; \
56 int r; \
57 fromaddr.proc_nr_e = KERNEL; \
58 toaddr.proc_nr_e = tr_proc_nr_e; \
59 fromaddr.offset = (myaddr); \
60 toaddr.offset = (addr); \
61 if((r=virtual_copy_vmcheck(caller, &fromaddr, \
62 &toaddr, length)) != OK) { \
63 printf("Can't copy in sys_trace: %d\n", r);\
64 return r;\
65 } \
68 #define COPYFROMPROC(addr, myaddr, length) { \
69 struct vir_addr fromaddr, toaddr; \
70 int r; \
71 fromaddr.proc_nr_e = tr_proc_nr_e; \
72 toaddr.proc_nr_e = KERNEL; \
73 fromaddr.offset = (addr); \
74 toaddr.offset = (myaddr); \
75 if((r=virtual_copy_vmcheck(caller, &fromaddr, \
76 &toaddr, length)) != OK) { \
77 printf("Can't copy in sys_trace: %d\n", r);\
78 return r;\
79 } \
82 if(!isokendpt(tr_proc_nr_e, &tr_proc_nr)) return(EINVAL);
83 if (iskerneln(tr_proc_nr)) return(EPERM);
85 rp = proc_addr(tr_proc_nr);
86 if (isemptyp(rp)) return(EINVAL);
87 switch (tr_request) {
88 case T_STOP: /* stop process */
89 RTS_SET(rp, RTS_P_STOP);
90 /* clear syscall trace and single step flags */
91 rp->p_misc_flags &= ~(MF_SC_TRACE | MF_STEP);
92 return(OK);
94 case T_GETINS: /* return value from instruction space */
95 COPYFROMPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
96 m_ptr->CTL_DATA = tr_data;
97 break;
99 case T_GETDATA: /* return value from data space */
100 COPYFROMPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
101 m_ptr->CTL_DATA= tr_data;
102 break;
104 case T_GETUSER: /* return value from process table */
105 if ((tr_addr & (sizeof(long) - 1)) != 0) return(EFAULT);
107 if (tr_addr <= sizeof(struct proc) - sizeof(long)) {
108 m_ptr->CTL_DATA = *(long *) ((char *) rp + (int) tr_addr);
109 break;
112 /* The process's proc struct is followed by its priv struct.
113 * The alignment here should be unnecessary, but better safe..
115 i = sizeof(long) - 1;
116 tr_addr -= (sizeof(struct proc) + i) & ~i;
118 if (tr_addr > sizeof(struct priv) - sizeof(long)) return(EFAULT);
120 m_ptr->CTL_DATA = *(long *) ((char *) rp->p_priv + (int) tr_addr);
121 break;
123 case T_SETINS: /* set value in instruction space */
124 COPYTOPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
125 m_ptr->CTL_DATA = 0;
126 break;
128 case T_SETDATA: /* set value in data space */
129 COPYTOPROC(tr_addr, (vir_bytes) &tr_data, sizeof(long));
130 m_ptr->CTL_DATA = 0;
131 break;
133 case T_SETUSER: /* set value in process table */
134 if ((tr_addr & (sizeof(reg_t) - 1)) != 0 ||
135 tr_addr > sizeof(struct stackframe_s) - sizeof(reg_t))
136 return(EFAULT);
137 i = (int) tr_addr;
138 #if defined(__i386__)
139 /* Altering segment registers might crash the kernel when it
140 * tries to load them prior to restarting a process, so do
141 * not allow it.
143 if (i == (int) &((struct proc *) 0)->p_reg.cs ||
144 i == (int) &((struct proc *) 0)->p_reg.ds ||
145 i == (int) &((struct proc *) 0)->p_reg.es ||
146 i == (int) &((struct proc *) 0)->p_reg.gs ||
147 i == (int) &((struct proc *) 0)->p_reg.fs ||
148 i == (int) &((struct proc *) 0)->p_reg.ss)
149 return(EFAULT);
150 #endif
151 #if defined(__i386__)
152 if (i == (int) &((struct proc *) 0)->p_reg.psw)
153 /* only selected bits are changeable */
154 SETPSW(rp, tr_data);
155 else
156 *(reg_t *) ((char *) &rp->p_reg + i) = (reg_t) tr_data;
157 #endif
158 m_ptr->CTL_DATA = 0;
159 break;
161 case T_DETACH: /* detach tracer */
162 rp->p_misc_flags &= ~MF_SC_ACTIVE;
164 /* fall through */
165 case T_RESUME: /* resume execution */
166 RTS_UNSET(rp, RTS_P_STOP);
167 m_ptr->CTL_DATA = 0;
168 break;
170 case T_STEP: /* set trace bit */
171 rp->p_misc_flags |= MF_STEP;
172 RTS_UNSET(rp, RTS_P_STOP);
173 m_ptr->CTL_DATA = 0;
174 break;
176 case T_SYSCALL: /* trace system call */
177 rp->p_misc_flags |= MF_SC_TRACE;
178 RTS_UNSET(rp, RTS_P_STOP);
179 m_ptr->CTL_DATA = 0;
180 break;
182 case T_READB_INS: /* get value from instruction space */
183 COPYFROMPROC(tr_addr, (vir_bytes) &ub, 1);
184 m_ptr->CTL_DATA = ub;
185 break;
187 case T_WRITEB_INS: /* set value in instruction space */
188 ub = (unsigned char) (tr_data & 0xff);
189 COPYTOPROC(tr_addr, (vir_bytes) &ub, 1);
190 m_ptr->CTL_DATA = 0;
191 break;
193 default:
194 return(EINVAL);
196 return(OK);
199 #endif /* USE_TRACE */