1 | /* |
---|
2 | ************************************************************************** |
---|
3 | * |
---|
4 | * Component = |
---|
5 | * |
---|
6 | * Synopsis = rkdb/rkdb.c |
---|
7 | * |
---|
8 | * $Id$ |
---|
9 | * |
---|
10 | ************************************************************************** |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <rdbg/rdbg.h> |
---|
16 | #include <rdbg/rdbg_f.h> |
---|
17 | #include <rdbg/servrpc.h> |
---|
18 | |
---|
19 | extern rtems_id serializeSemId; |
---|
20 | extern rtems_id wakeupEventSemId; |
---|
21 | extern rtems_id eventTaskId; |
---|
22 | extern Exception_context *FirstCtx; |
---|
23 | extern Exception_context *LastCtx; |
---|
24 | extern CPU_Exception_frame SavedContext; |
---|
25 | extern unsigned int NbExceptCtx; |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | /* -------------------------------------------------------------------- |
---|
30 | return a pointer to the Thread Control structure of the specified |
---|
31 | Id |
---|
32 | -------------------------------------------------------------------- */ |
---|
33 | |
---|
34 | Thread_Control *Thread_Get_RDBG ( |
---|
35 | Objects_Id Id |
---|
36 | ) |
---|
37 | { |
---|
38 | unsigned index; |
---|
39 | |
---|
40 | if ( Id <_Objects_Information_table[OBJECTS_RTEMS_TASKS]->maximum_id && |
---|
41 | Id >_Objects_Information_table[OBJECTS_RTEMS_TASKS]->minimum_id) { |
---|
42 | |
---|
43 | index = Id - _Objects_Information_table[OBJECTS_RTEMS_TASKS]->minimum_id; |
---|
44 | if ( _Objects_Information_table[OBJECTS_RTEMS_TASKS]->local_table[1+index] != NULL) { |
---|
45 | return (Thread_Control *)(_Objects_Information_table[OBJECTS_RTEMS_TASKS]->local_table[1+index]); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | if ( Id <_Objects_Information_table[OBJECTS_POSIX_THREADS]->maximum_id && |
---|
50 | Id >_Objects_Information_table[OBJECTS_POSIX_THREADS]->minimum_id) { |
---|
51 | |
---|
52 | index = Id - _Objects_Information_table[OBJECTS_POSIX_THREADS]->minimum_id; |
---|
53 | if ( _Objects_Information_table[OBJECTS_POSIX_THREADS]->local_table[1+index] != NULL) |
---|
54 | return (Thread_Control *)(_Objects_Information_table[OBJECTS_POSIX_THREADS]->local_table[1+index]); |
---|
55 | } |
---|
56 | |
---|
57 | return 0; |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | /* -------------------------------------------------------------------- |
---|
63 | Memory read |
---|
64 | -------------------------------------------------------------------- */ |
---|
65 | |
---|
66 | int |
---|
67 | safeMemRead(void *src, void *dest, int nbBytes){ |
---|
68 | |
---|
69 | /* |
---|
70 | * safe because if it generates an exception, |
---|
71 | * it must return normally |
---|
72 | * TBD |
---|
73 | */ |
---|
74 | |
---|
75 | memcpy(dest, src, nbBytes); |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | /* -------------------------------------------------------------------- |
---|
80 | Memory write |
---|
81 | -------------------------------------------------------------------- */ |
---|
82 | int |
---|
83 | safeMemWrite(void *src, void * dest, int nbBytes){ |
---|
84 | |
---|
85 | /* |
---|
86 | * safe because if it generates an exception, |
---|
87 | * it must return normally |
---|
88 | * TBD |
---|
89 | */ |
---|
90 | |
---|
91 | memcpy(dest, src, nbBytes); |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | /* -------------------------------------------------------------------- |
---|
96 | Ptrace |
---|
97 | -------------------------------------------------------------------- */ |
---|
98 | |
---|
99 | int |
---|
100 | ptrace (int request, int pid, char* addr, int data, char* addr2) |
---|
101 | { |
---|
102 | int diag; |
---|
103 | errno = 0 ; |
---|
104 | if (pid != 1) { |
---|
105 | errno = ESRCH; |
---|
106 | return -1; |
---|
107 | } |
---|
108 | switch (request) { |
---|
109 | |
---|
110 | case RPT_SINGLESTEP:{ |
---|
111 | Exception_context *ctx; |
---|
112 | |
---|
113 | if (CannotRestart == 1){ |
---|
114 | setErrno(EIO); |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | if ((ctx = GetExceptCtx (currentTargetThread)) != NULL) { |
---|
119 | Single_Step(ctx->ctx); |
---|
120 | rtems_semaphore_release( ctx->semaphoreId ); |
---|
121 | return 0; |
---|
122 | } |
---|
123 | break; |
---|
124 | } |
---|
125 | |
---|
126 | case RPT_PEEKTEXT: |
---|
127 | case RPT_PEEKDATA: { |
---|
128 | diag = safeMemRead(addr, &data, sizeof data); |
---|
129 | if (diag == 0) return data; |
---|
130 | mem_error: |
---|
131 | return -1; |
---|
132 | } |
---|
133 | |
---|
134 | case RPT_POKETEXT: { |
---|
135 | diag = safeMemWrite(&data, addr, sizeof data); |
---|
136 | |
---|
137 | /* |
---|
138 | * We must flush the INSTR and DATA cache to be sure the |
---|
139 | * opcode modification is taken into account, because |
---|
140 | * the breakpoint opcode is written via the data cache |
---|
141 | * while execution code is fetched via the instruction |
---|
142 | * cache |
---|
143 | */ |
---|
144 | |
---|
145 | if (diag == 0) { |
---|
146 | copyback_data_cache_and_invalidate_instr_cache(addr, sizeof data); |
---|
147 | return 0; |
---|
148 | } |
---|
149 | goto mem_error; |
---|
150 | } |
---|
151 | case RPT_POKEDATA: { |
---|
152 | diag = safeMemWrite(&data, addr, sizeof data); |
---|
153 | if (diag == 0) return 0; |
---|
154 | goto mem_error; |
---|
155 | } |
---|
156 | case RPT_CONT: { |
---|
157 | Exception_context *ctx; |
---|
158 | |
---|
159 | if (CannotRestart == 1){ |
---|
160 | setErrno (EIO); |
---|
161 | return -1; |
---|
162 | } |
---|
163 | |
---|
164 | ctx = GetExceptCtx (currentTargetThread); |
---|
165 | |
---|
166 | if (!isRdbgException(ctx)) { |
---|
167 | CannotRestart = 1; |
---|
168 | setErrno (EIO); |
---|
169 | return -1; |
---|
170 | } |
---|
171 | |
---|
172 | assert (data == 0); |
---|
173 | assert (ExitForSingleStep == 0); |
---|
174 | |
---|
175 | rtems_semaphore_release( serializeSemId ); |
---|
176 | |
---|
177 | if ((ctx = GetExceptCtx (currentTargetThread)) != NULL) { |
---|
178 | rtems_semaphore_release( ctx->semaphoreId ); |
---|
179 | } |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | case RPT_ATTACH: |
---|
184 | return 0; |
---|
185 | |
---|
186 | case RPT_DETACH:{ |
---|
187 | Exception_context *ctx; |
---|
188 | |
---|
189 | if (NbExceptCtx || NbSerializedCtx) { |
---|
190 | ctx = FirstCtx; |
---|
191 | rtems_task_delete(eventTaskId); |
---|
192 | rtems_semaphore_delete(serializeSemId); |
---|
193 | rtems_semaphore_delete(wakeupEventSemId); |
---|
194 | } |
---|
195 | return 0; |
---|
196 | } |
---|
197 | |
---|
198 | case RPT_GETREGS:{ |
---|
199 | Exception_context *ctx; |
---|
200 | |
---|
201 | if ((ctx = GetExceptCtx (currentTargetThread)) != NULL) { |
---|
202 | CtxToRegs (ctx->ctx, (xdr_regs*) addr); |
---|
203 | return 0; |
---|
204 | } |
---|
205 | break; |
---|
206 | } |
---|
207 | |
---|
208 | case RPT_SETREGS:{ |
---|
209 | Exception_context *ctx; |
---|
210 | |
---|
211 | if ((ctx = GetExceptCtx (currentTargetThread)) != NULL) { |
---|
212 | RegsToCtx ((xdr_regs*) addr, ctx->ctx); |
---|
213 | return 0; |
---|
214 | } |
---|
215 | break; |
---|
216 | } |
---|
217 | |
---|
218 | case RPT_READTEXT: |
---|
219 | case RPT_READDATA: { |
---|
220 | diag = safeMemRead(addr, addr2, data); |
---|
221 | if (diag == 0) return 0; |
---|
222 | goto mem_error; |
---|
223 | } |
---|
224 | case RPT_WRITETEXT: |
---|
225 | case RPT_WRITEDATA: { |
---|
226 | diag = safeMemWrite(addr2, addr, data); |
---|
227 | if (diag == 0) return 0; |
---|
228 | goto mem_error; |
---|
229 | } |
---|
230 | |
---|
231 | case RPT_GETTARGETTHREAD: |
---|
232 | if (!NbExceptCtx) { |
---|
233 | errno = EBUSY; |
---|
234 | return -1; |
---|
235 | } |
---|
236 | return currentTargetThread; |
---|
237 | |
---|
238 | case RPT_SETTARGETTHREAD: |
---|
239 | if (!NbExceptCtx) { |
---|
240 | errno = EBUSY; |
---|
241 | return -1; |
---|
242 | } |
---|
243 | currentTargetThread = data; |
---|
244 | return 0; |
---|
245 | |
---|
246 | case RPT_GETTHREADNAME: { |
---|
247 | return TgtGetThreadName (NULL, (unsigned)(data), (char *) addr); |
---|
248 | } |
---|
249 | |
---|
250 | case RPT_THREADLIST: { |
---|
251 | int count = TgtThreadList (NULL, (unsigned*) addr, UTHREAD_MAX |
---|
252 | * sizeof (unsigned)); |
---|
253 | if (count < 0) { |
---|
254 | errno = EINVAL; |
---|
255 | return -1; |
---|
256 | } |
---|
257 | return count; |
---|
258 | } |
---|
259 | |
---|
260 | case RPT_SETTHREADREGS: { |
---|
261 | Exception_context *ctx; |
---|
262 | CPU_Exception_frame Ectx; |
---|
263 | Thread_Control *thread; |
---|
264 | rtems_id id; |
---|
265 | |
---|
266 | rtems_task_ident(RTEMS_SELF, RTEMS_SEARCH_ALL_NODES, &id); |
---|
267 | if (data == (unsigned)id) |
---|
268 | break; |
---|
269 | |
---|
270 | if ((ctx = GetExceptCtx (data)) != NULL) { |
---|
271 | RegsToCtx ((xdr_regs*) addr, ctx->ctx); |
---|
272 | return 0; |
---|
273 | } |
---|
274 | thread = Thread_Get_RDBG ((Objects_Id)(data)); |
---|
275 | if (thread != NULL) { |
---|
276 | RegsToCtx ((xdr_regs*) addr, &Ectx); |
---|
277 | set_ctx_thread (thread, &Ectx); |
---|
278 | return 0; |
---|
279 | } |
---|
280 | break; |
---|
281 | } |
---|
282 | |
---|
283 | case RPT_GETTHREADREGS: { |
---|
284 | Exception_context *ctx; |
---|
285 | CPU_Exception_frame Ectx; |
---|
286 | Thread_Control *thread; |
---|
287 | rtems_id id; |
---|
288 | |
---|
289 | rtems_task_ident(RTEMS_SELF, RTEMS_SEARCH_ALL_NODES, &id); |
---|
290 | if (data == (unsigned)id){ |
---|
291 | justSaveContext = 1; |
---|
292 | enterRdbg(); |
---|
293 | CtxToRegs (&(SavedContext), (xdr_regs*) addr); |
---|
294 | return 0; |
---|
295 | } |
---|
296 | |
---|
297 | if ((ctx = GetExceptCtx (data)) != NULL) { |
---|
298 | CtxToRegs (ctx->ctx, (xdr_regs*) addr); |
---|
299 | return 0; |
---|
300 | } |
---|
301 | thread = Thread_Get_RDBG ((Objects_Id)(data)); |
---|
302 | if (thread != NULL) { |
---|
303 | get_ctx_thread (thread, &Ectx); |
---|
304 | CtxToRegs (&Ectx, (xdr_regs*) addr); |
---|
305 | return 0; |
---|
306 | } |
---|
307 | break; |
---|
308 | } |
---|
309 | |
---|
310 | case RPT_KILL: |
---|
311 | TotalReboot = 1; |
---|
312 | return 0; |
---|
313 | |
---|
314 | case RPT_TRACEME: |
---|
315 | case RPT_PEEKUSER: |
---|
316 | case RPT_POKEUSER: |
---|
317 | case RPT_GETFPREGS: |
---|
318 | case RPT_SETFPREGS: |
---|
319 | case RPT_GETFPAREGS: |
---|
320 | case RPT_SETFPAREGS: |
---|
321 | case RPT_SYSCALL: |
---|
322 | case RPT_DUMPCORE: |
---|
323 | case RPT_GETUCODE: |
---|
324 | case RPT_THREADSUSPEND: |
---|
325 | case RPT_THREADRESUME: |
---|
326 | case RPT_SETTHREADNAME: |
---|
327 | default: |
---|
328 | break; |
---|
329 | } |
---|
330 | errno = EINVAL; |
---|
331 | return -1; |
---|
332 | } |
---|