1 | /* |
---|
2 | ============================================================================ |
---|
3 | _SERVTGT |
---|
4 | $Id$ |
---|
5 | ============================================================================ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <string.h> |
---|
9 | #include <rtems.h> |
---|
10 | #include <rtems/error.h> |
---|
11 | |
---|
12 | #include <rdbg/rdbg.h> |
---|
13 | #include <rdbg/rdbg_f.h> |
---|
14 | #include <rdbg/servrpc.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <sys/socket.h> |
---|
17 | #include <assert.h> |
---|
18 | #include <rtems/score/cpu.h> |
---|
19 | |
---|
20 | #ifdef DDEBUG |
---|
21 | #define Ptrace TgtDbgPtrace |
---|
22 | #else |
---|
23 | #define Ptrace TgtRealPtrace |
---|
24 | #endif |
---|
25 | |
---|
26 | extern int errno; |
---|
27 | |
---|
28 | rtems_id eventTaskId; |
---|
29 | rtems_id serializeSemId; |
---|
30 | rtems_id wakeupEventSemId; |
---|
31 | rtems_id debugId; |
---|
32 | |
---|
33 | CPU_Exception_frame Idle_frame; |
---|
34 | |
---|
35 | /* ----------------------------------------------------------------- |
---|
36 | TgtRealPtrace - lowest level ptrace() wrapper |
---|
37 | ----------------------------------------------------------------- */ |
---|
38 | |
---|
39 | int |
---|
40 | TgtRealPtrace (int req, PID aid, char *addr, int d, void *addr2) |
---|
41 | { |
---|
42 | return ptrace (req, aid, addr, d, addr2); |
---|
43 | } |
---|
44 | |
---|
45 | /* ----------------------------------------------------------------------- |
---|
46 | TgtChange() is called when the system stops. |
---|
47 | It informs the generic layers must be informed of |
---|
48 | that fact. |
---|
49 | ----------------------------------------------------------------------- */ |
---|
50 | |
---|
51 | static int |
---|
52 | TgtChange (PID pid, CPU_Exception_frame * ctx, int status) |
---|
53 | { |
---|
54 | |
---|
55 | if (TgtHandleChildChange (pid, &status, NULL, ctx)) { |
---|
56 | TgtNotifyWaitChange (pid, status, -1); |
---|
57 | } |
---|
58 | |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | /* ----------------------------------------------------------------------- |
---|
63 | eventTask |
---|
64 | ----------------------------------------------------------------------- */ |
---|
65 | |
---|
66 | rtems_task |
---|
67 | eventTask (rtems_task_argument pid) |
---|
68 | { |
---|
69 | Exception_context *ctx; |
---|
70 | |
---|
71 | DPRINTF (("event task: pid %d\n", pid)); |
---|
72 | |
---|
73 | /* |
---|
74 | * we spend all our time waiting for a semaphore. |
---|
75 | * If wait change, we send info |
---|
76 | */ |
---|
77 | |
---|
78 | for (;;) { |
---|
79 | DPRINTF (("Event Task: wait event\n")); |
---|
80 | rtems_semaphore_obtain (wakeupEventSemId, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
81 | DPRINTF (("Event Task: wake up !!!!!!!!!!!!!\n")); |
---|
82 | |
---|
83 | errno = 0; |
---|
84 | ctx = GetExceptCtx (currentTargetThread); |
---|
85 | |
---|
86 | CheckForSingleStep (ctx->ctx); |
---|
87 | |
---|
88 | TgtChange (pid, ctx->ctx, STS_MAKESIG (ExcepToSig (ctx))); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | /* ------------------------------------------------------------------- |
---|
93 | MyThreadIdle - |
---|
94 | |
---|
95 | This task is used to initiate the exception mechanism: |
---|
96 | It calls the enterDebug function with justSaveContext=1 |
---|
97 | only to push a first valid context in the list |
---|
98 | ---------------------------------------------------------------------*/ |
---|
99 | |
---|
100 | rtems_task |
---|
101 | MyThreadIdle (rtems_task_argument argument) |
---|
102 | { |
---|
103 | enterRdbg (); |
---|
104 | rtems_task_delete (RTEMS_SELF); |
---|
105 | } |
---|
106 | |
---|
107 | /* ----------------------------------------------------------------------- |
---|
108 | TgtAttach - attach to a process that is running without control. |
---|
109 | |
---|
110 | Notes: |
---|
111 | - this function performs a ptrace ATTACH equivalent (attaching to a |
---|
112 | process that we do not control now). |
---|
113 | ----------------------------------------------------------------------- */ |
---|
114 | |
---|
115 | Boolean |
---|
116 | TgtAttach (int conn_idx, /* client that is requesting */ |
---|
117 | PID pid) |
---|
118 | { /* process to attach to */ |
---|
119 | rtems_name task_name; |
---|
120 | rtems_status_code status; |
---|
121 | |
---|
122 | errno = 0; |
---|
123 | |
---|
124 | DPRINTF (("TgtAttach pid=%d\n", pid)); |
---|
125 | |
---|
126 | Ptrace (RPT_ATTACH, pid, NULL, 0, NULL); |
---|
127 | if (errno) |
---|
128 | return (False); /* failed */ |
---|
129 | |
---|
130 | TgtCreateNew (pid, conn_idx, 0, NULL, False); |
---|
131 | |
---|
132 | connect_rdbg_exception (); |
---|
133 | |
---|
134 | /* |
---|
135 | * Search for and remove debugging tasks / semaphores left over from |
---|
136 | * a previous attach without a detach. |
---|
137 | */ |
---|
138 | |
---|
139 | if (rtems_task_ident (rtems_build_name ('E', 'v', 'n', 't'), |
---|
140 | RTEMS_SEARCH_ALL_NODES, |
---|
141 | &eventTaskId) == RTEMS_SUCCESSFUL) |
---|
142 | rtems_task_delete (eventTaskId); |
---|
143 | |
---|
144 | if (rtems_semaphore_ident (rtems_build_name ('D', 'B', 'G', 's'), |
---|
145 | RTEMS_SEARCH_ALL_NODES, |
---|
146 | &serializeSemId) == RTEMS_SUCCESSFUL) |
---|
147 | rtems_semaphore_delete (serializeSemId); |
---|
148 | |
---|
149 | if (rtems_semaphore_ident (rtems_build_name ('D', 'B', 'G', 'w'), |
---|
150 | RTEMS_SEARCH_ALL_NODES, |
---|
151 | &wakeupEventSemId) == RTEMS_SUCCESSFUL) |
---|
152 | rtems_semaphore_delete (wakeupEventSemId); |
---|
153 | |
---|
154 | /* |
---|
155 | * Create the attach debugger task |
---|
156 | */ |
---|
157 | task_name = rtems_build_name ('E', 'v', 'n', 't'); |
---|
158 | if ((status = rtems_task_create (task_name, 10, 24576, |
---|
159 | RTEMS_INTERRUPT_LEVEL (0), |
---|
160 | RTEMS_DEFAULT_ATTRIBUTES | |
---|
161 | RTEMS_SYSTEM_TASK, &eventTaskId)) |
---|
162 | != RTEMS_SUCCESSFUL) { |
---|
163 | printf ("status = %d\n", status); |
---|
164 | rtems_panic ("Can't create task.\n"); |
---|
165 | } |
---|
166 | |
---|
167 | status = rtems_task_start (eventTaskId, eventTask, pid); |
---|
168 | |
---|
169 | status = rtems_semaphore_create (rtems_build_name ('D', 'B', 'G', 's'), |
---|
170 | 1, |
---|
171 | RTEMS_FIFO | |
---|
172 | RTEMS_COUNTING_SEMAPHORE | |
---|
173 | RTEMS_NO_INHERIT_PRIORITY | |
---|
174 | RTEMS_NO_PRIORITY_CEILING | |
---|
175 | RTEMS_LOCAL, 0, &serializeSemId); |
---|
176 | if (status != RTEMS_SUCCESSFUL) |
---|
177 | rtems_panic ("Can't create serialize semaphore: `%s'\n", |
---|
178 | rtems_status_text (status)); |
---|
179 | |
---|
180 | status = rtems_semaphore_create (rtems_build_name ('D', 'B', 'G', 'w'), |
---|
181 | 0, |
---|
182 | RTEMS_FIFO | |
---|
183 | RTEMS_COUNTING_SEMAPHORE | |
---|
184 | RTEMS_NO_INHERIT_PRIORITY | |
---|
185 | RTEMS_NO_PRIORITY_CEILING | |
---|
186 | RTEMS_LOCAL, 0, &wakeupEventSemId); |
---|
187 | if (status != RTEMS_SUCCESSFUL) |
---|
188 | rtems_panic ("Can't create wakeup semaphore: `%s'\n", |
---|
189 | rtems_status_text (status)); |
---|
190 | |
---|
191 | /* |
---|
192 | * Create the MyThreadIdle task to init Exception mechanism |
---|
193 | */ |
---|
194 | task_name = rtems_build_name ('R', 'i', 'n', 'i'); |
---|
195 | if ((status = rtems_task_create (task_name, 10, 24576, |
---|
196 | RTEMS_INTERRUPT_LEVEL (0), |
---|
197 | RTEMS_DEFAULT_ATTRIBUTES, &debugId)) |
---|
198 | != RTEMS_SUCCESSFUL) { |
---|
199 | printf ("status = %d (%s)\n", status, rtems_status_text (status)); |
---|
200 | rtems_panic ("Can't create task.\n"); |
---|
201 | } |
---|
202 | |
---|
203 | status = rtems_task_start (debugId, MyThreadIdle, pid); |
---|
204 | |
---|
205 | return (True); |
---|
206 | } |
---|
207 | |
---|
208 | /* ----------------------------------------------------------------------- |
---|
209 | TgtPtrace - handle ptrace requests for server. |
---|
210 | ----------------------------------------------------------------------- */ |
---|
211 | |
---|
212 | int |
---|
213 | TgtPtrace (int req, PID pid, char *addr, int data, void *addr2) |
---|
214 | { |
---|
215 | if ((req == RPT_SINGLESTEP || req == RPT_CONT) |
---|
216 | && addr2) { /* clear then step */ /* addr2 is the old value */ |
---|
217 | |
---|
218 | int ret; |
---|
219 | |
---|
220 | errno = 0; |
---|
221 | TgtBreakRestoreOrig (pid, addr, addr2); |
---|
222 | ret = Ptrace (RPT_SINGLESTEP, pid, addr, data, NULL); /* step over */ |
---|
223 | if (ret) { /* error, cannot single-step */ |
---|
224 | int pid_idx = FindPidEntry (pid); |
---|
225 | TgtBreakCancelStep (&pid_list[pid_idx]); |
---|
226 | } |
---|
227 | return (ret); /* failed or done */ |
---|
228 | } else |
---|
229 | return (Ptrace (req, pid, addr, data, addr2)); /* normal call */ |
---|
230 | } |
---|
231 | |
---|
232 | /* ----------------------------------------------------------------- |
---|
233 | TgtGetThreadName - get thread name |
---|
234 | --------------------------------------------------------------- */ |
---|
235 | |
---|
236 | int |
---|
237 | TgtGetThreadName (PID_LIST * plst, /* Process entry */ |
---|
238 | unsigned Id, /* Thread ID */ |
---|
239 | char *ThrName) |
---|
240 | { /* Thread name */ |
---|
241 | int index; |
---|
242 | unsigned name; |
---|
243 | |
---|
244 | if (Id < _Objects_Information_table[OBJECTS_CLASSIC_API][1]->maximum_id && |
---|
245 | Id > _Objects_Information_table[OBJECTS_CLASSIC_API][1]->minimum_id) { |
---|
246 | |
---|
247 | index = Id - _Objects_Information_table[OBJECTS_CLASSIC_API][1]->minimum_id; |
---|
248 | name = |
---|
249 | *(unsigned *) (_Objects_Information_table[OBJECTS_CLASSIC_API][1]-> |
---|
250 | local_table[1 + index]->name); |
---|
251 | ThrName[0] = (char) ((name >> 24) & 0xFF); |
---|
252 | ThrName[1] = (char) ((name >> 16) & 0xFF); |
---|
253 | ThrName[2] = (char) ((name >> 8) & 0xFF); |
---|
254 | ThrName[3] = (char) (name & 0xFF); |
---|
255 | ThrName[4] = 0x0; |
---|
256 | return 0; |
---|
257 | } |
---|
258 | |
---|
259 | if (Id < _Objects_Information_table[OBJECTS_POSIX_API][1]->maximum_id && |
---|
260 | Id > _Objects_Information_table[OBJECTS_POSIX_API][1]->minimum_id) { |
---|
261 | |
---|
262 | index = |
---|
263 | Id - _Objects_Information_table[OBJECTS_POSIX_API][1]->minimum_id; |
---|
264 | name = |
---|
265 | *(unsigned *) (_Objects_Information_table[OBJECTS_POSIX_API][1]-> |
---|
266 | local_table[1 + index]->name); |
---|
267 | ThrName[0] = (char) ((name >> 24) & 0xFF); |
---|
268 | ThrName[1] = (char) ((name >> 16) & 0xFF); |
---|
269 | ThrName[2] = (char) ((name >> 8) & 0xFF); |
---|
270 | ThrName[3] = (char) (name & 0xFF); |
---|
271 | ThrName[4] = 0x0; |
---|
272 | return 0; |
---|
273 | } |
---|
274 | |
---|
275 | return -1; |
---|
276 | } |
---|
277 | |
---|
278 | /* ----------------------------------------------------------------- |
---|
279 | TgtThreadList - return all the threads in the system |
---|
280 | ----------------------------------------------------------------- */ |
---|
281 | |
---|
282 | int |
---|
283 | TgtThreadList (PID_LIST * plst, /* Process entry */ |
---|
284 | unsigned *threads, /* Output buffer */ |
---|
285 | unsigned size) |
---|
286 | { /* Output buffer size */ |
---|
287 | int curr = 0; |
---|
288 | int api; |
---|
289 | Objects_Id id, min_id, max_id; |
---|
290 | unsigned index; |
---|
291 | |
---|
292 | for ( api=OBJECTS_CLASSIC_API ; api <= OBJECTS_ITRON_API ; api++ ) { |
---|
293 | min_id = _Objects_Information_table[api][1]->minimum_id; |
---|
294 | max_id = _Objects_Information_table[api][1]->maximum_id; |
---|
295 | id = min_id; |
---|
296 | |
---|
297 | while (id < max_id) { |
---|
298 | index = id - min_id; |
---|
299 | if (_Objects_Information_table[api][1]->local_table[1 + index] != NULL) { |
---|
300 | threads[curr] = (unsigned) id; |
---|
301 | curr++; |
---|
302 | } |
---|
303 | id++; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | return curr; |
---|
308 | } |
---|