panic() cleanup.
[minix.git] / kernel / system / do_trace.c
blobfe10eb6589e5cc406c92130fa2ffa800ce26cde9
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 "../system.h"
12 #include <sys/ptrace.h>
14 #if USE_TRACE
16 /*==========================================================================*
17 * do_trace *
18 *==========================================================================*/
19 PUBLIC 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(seg, 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 fromaddr.segment = D; \
62 toaddr.segment = (seg); \
63 if((r=virtual_copy_vmcheck(caller, &fromaddr, \
64 &toaddr, length)) != OK) { \
65 printf("Can't copy in sys_trace: %d\n", r);\
66 return r;\
67 } \
70 #define COPYFROMPROC(seg, addr, myaddr, length) { \
71 struct vir_addr fromaddr, toaddr; \
72 int r; \
73 fromaddr.proc_nr_e = tr_proc_nr_e; \
74 toaddr.proc_nr_e = KERNEL; \
75 fromaddr.offset = (addr); \
76 toaddr.offset = (myaddr); \
77 fromaddr.segment = (seg); \
78 toaddr.segment = D; \
79 if((r=virtual_copy_vmcheck(caller, &fromaddr, \
80 &toaddr, length)) != OK) { \
81 printf("Can't copy in sys_trace: %d\n", r);\
82 return r;\
83 } \
86 if(!isokendpt(tr_proc_nr_e, &tr_proc_nr)) return(EINVAL);
87 if (iskerneln(tr_proc_nr)) return(EPERM);
89 rp = proc_addr(tr_proc_nr);
90 if (isemptyp(rp)) return(EINVAL);
91 switch (tr_request) {
92 case T_STOP: /* stop process */
93 RTS_SET(rp, RTS_P_STOP);
94 rp->p_reg.psw &= ~TRACEBIT; /* clear trace bit */
95 rp->p_misc_flags &= ~MF_SC_TRACE; /* clear syscall trace flag */
96 return(OK);
98 case T_GETINS: /* return value from instruction space */
99 COPYFROMPROC(T, tr_addr, (vir_bytes) &tr_data, sizeof(long));
100 m_ptr->CTL_DATA = tr_data;
101 break;
103 case T_GETDATA: /* return value from data space */
104 COPYFROMPROC(D, tr_addr, (vir_bytes) &tr_data, sizeof(long));
105 m_ptr->CTL_DATA= tr_data;
106 break;
108 case T_GETUSER: /* return value from process table */
109 if ((tr_addr & (sizeof(long) - 1)) != 0) return(EFAULT);
111 if (tr_addr <= sizeof(struct proc) - sizeof(long)) {
112 m_ptr->CTL_DATA = *(long *) ((char *) rp + (int) tr_addr);
113 break;
116 /* The process's proc struct is followed by its priv struct.
117 * The alignment here should be unnecessary, but better safe..
119 i = sizeof(long) - 1;
120 tr_addr -= (sizeof(struct proc) + i) & ~i;
122 if (tr_addr > sizeof(struct priv) - sizeof(long)) return(EFAULT);
124 m_ptr->CTL_DATA = *(long *) ((char *) rp->p_priv + (int) tr_addr);
125 break;
127 case T_SETINS: /* set value in instruction space */
128 COPYTOPROC(T, tr_addr, (vir_bytes) &tr_data, sizeof(long));
129 m_ptr->CTL_DATA = 0;
130 break;
132 case T_SETDATA: /* set value in data space */
133 COPYTOPROC(D, tr_addr, (vir_bytes) &tr_data, sizeof(long));
134 m_ptr->CTL_DATA = 0;
135 break;
137 case T_SETUSER: /* set value in process table */
138 if ((tr_addr & (sizeof(reg_t) - 1)) != 0 ||
139 tr_addr > sizeof(struct stackframe_s) - sizeof(reg_t))
140 return(EFAULT);
141 i = (int) tr_addr;
142 #if (_MINIX_CHIP == _CHIP_INTEL)
143 /* Altering segment registers might crash the kernel when it
144 * tries to load them prior to restarting a process, so do
145 * not allow it.
147 if (i == (int) &((struct proc *) 0)->p_reg.cs ||
148 i == (int) &((struct proc *) 0)->p_reg.ds ||
149 i == (int) &((struct proc *) 0)->p_reg.es ||
150 #if _WORD_SIZE == 4
151 i == (int) &((struct proc *) 0)->p_reg.gs ||
152 i == (int) &((struct proc *) 0)->p_reg.fs ||
153 #endif
154 i == (int) &((struct proc *) 0)->p_reg.ss)
155 return(EFAULT);
156 #endif
157 if (i == (int) &((struct proc *) 0)->p_reg.psw)
158 /* only selected bits are changeable */
159 SETPSW(rp, tr_data);
160 else
161 *(reg_t *) ((char *) &rp->p_reg + i) = (reg_t) tr_data;
162 m_ptr->CTL_DATA = 0;
163 break;
165 case T_DETACH: /* detach tracer */
166 rp->p_misc_flags &= ~MF_SC_ACTIVE;
168 /* fall through */
169 case T_RESUME: /* resume execution */
170 RTS_UNSET(rp, RTS_P_STOP);
171 m_ptr->CTL_DATA = 0;
172 break;
174 case T_STEP: /* set trace bit */
175 rp->p_reg.psw |= TRACEBIT;
176 RTS_UNSET(rp, RTS_P_STOP);
177 m_ptr->CTL_DATA = 0;
178 break;
180 case T_SYSCALL: /* trace system call */
181 rp->p_misc_flags |= MF_SC_TRACE;
182 RTS_UNSET(rp, RTS_P_STOP);
183 m_ptr->CTL_DATA = 0;
184 break;
186 case T_READB_INS: /* get value from instruction space */
187 COPYFROMPROC(T, tr_addr, (vir_bytes) &ub, 1);
188 m_ptr->CTL_DATA = ub;
189 break;
191 case T_WRITEB_INS: /* set value in instruction space */
192 ub = (unsigned char) (tr_data & 0xff);
193 COPYTOPROC(T, tr_addr, (vir_bytes) &ub, 1);
194 m_ptr->CTL_DATA = 0;
195 break;
197 default:
198 return(EINVAL);
200 return(OK);
203 #endif /* USE_TRACE */