|
0
|
1 /*========================================================================== |
|
|
2 // |
|
2
|
3 // dbg_gdb.c |
|
0
|
4 // |
|
2
|
5 // GDB Debugging Interface |
|
0
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): nickg |
|
|
33 // Contributors: nickg |
|
|
34 // Date: 1998-08-22 |
|
0
|
35 // Purpose: GDB Debugging Interface |
|
2
|
36 // Description: Interface for calls from GDB stubs into the OS. These |
|
0
|
37 // currently mostly support thread awareness. |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //========================================================================*/ |
|
|
42 |
|
|
43 #include <pkgconf/kernel.h> |
|
|
44 #include <pkgconf/hal.h> // CYG_HAL_USE_ROM_MONITOR_CYGMON |
|
|
45 |
|
|
46 #ifdef CYGDBG_KERNEL_DEBUG_GDB_THREAD_SUPPORT |
|
|
47 |
|
|
48 #include <cyg/kernel/ktypes.h> |
|
|
49 |
|
|
50 #include <cyg/kernel/thread.hxx> |
|
|
51 #include <cyg/kernel/sched.hxx> |
|
|
52 |
|
|
53 #include <cyg/kernel/thread.inl> |
|
|
54 #include <cyg/kernel/sched.inl> |
|
|
55 |
|
|
56 #include <cyg/hal/hal_arch.h> |
|
|
57 #include <cyg/hal/hal_stub.h> |
|
|
58 |
|
|
59 extern "C" |
|
|
60 { |
|
|
61 #include <cyg/hal/dbg-threads-api.h> |
|
|
62 }; |
|
|
63 |
|
|
64 #define USE_ID 1 |
|
|
65 |
|
|
66 #if (CYG_BYTEORDER == CYG_LSBFIRST) |
|
|
67 |
|
|
68 unsigned long swap32(unsigned long x) |
|
|
69 { |
|
|
70 unsigned long r = 0; |
|
|
71 |
|
|
72 r |= (x>>24)&0xFF; |
|
|
73 r |= ((x>>16)&0xFF)<<8; |
|
|
74 r |= ((x>>8)&0xFF)<<16; |
|
|
75 r |= ((x)&0xFF)<<24; |
|
|
76 |
|
|
77 return r; |
|
|
78 } |
|
|
79 |
|
|
80 #else |
|
|
81 |
|
|
82 #define swap32(x) ((unsigned long)(x)) |
|
|
83 |
|
|
84 #endif |
|
|
85 |
|
|
86 //-------------------------------------------------------------------------- |
|
|
87 |
|
|
88 externC int dbg_thread_capabilities(struct dbg_capabilities * cpb) |
|
|
89 { |
|
|
90 cpb->mask1 = has_thread_current | |
|
|
91 has_thread_registers | |
|
|
92 has_thread_reg_change | |
|
|
93 has_thread_list | |
|
|
94 has_thread_info ; |
|
|
95 return 1 ; |
|
|
96 } |
|
|
97 |
|
|
98 //-------------------------------------------------------------------------- |
|
|
99 |
|
|
100 static void dbg_make_threadref(Cyg_Thread *thread, threadref *ref ) |
|
|
101 { |
|
|
102 cyg_uint16 id = thread->get_unique_id(); |
|
|
103 |
|
|
104 #if USE_ID |
|
|
105 ((unsigned long *)ref)[0] = (unsigned long)thread; |
|
|
106 ((unsigned long *)ref)[1] = (unsigned long)swap32(id); |
|
|
107 #else |
|
|
108 ((unsigned long *)ref)[1] = (unsigned long)thread; |
|
|
109 ((unsigned long *)ref)[0] = (unsigned long)id; |
|
|
110 #endif |
|
|
111 |
|
|
112 } |
|
|
113 |
|
|
114 static Cyg_Thread *dbg_get_thread( threadref *ref) |
|
|
115 { |
|
|
116 #if USE_ID |
|
|
117 |
|
|
118 cyg_uint16 id = 0; |
|
|
119 |
|
|
120 id = (cyg_uint16)swap32(((unsigned long *)ref)[1]); |
|
|
121 |
|
|
122 Cyg_Thread *th = Cyg_Thread::get_list_head(); |
|
|
123 while( th != 0 ) |
|
|
124 { |
|
|
125 if( th->get_unique_id() == id ) break; |
|
|
126 th = th->get_list_next(); |
|
|
127 } |
|
|
128 |
|
|
129 // if( thread->get_unique_id() != id ) th = 0; |
|
|
130 |
|
|
131 #else |
|
|
132 |
|
|
133 cyg_uint16 id = 0; |
|
|
134 |
|
|
135 Cyg_Thread *thread = (Cyg_Thread *)(((unsigned long *)ref)[1]); |
|
|
136 id = (cyg_uint16)(((unsigned long *)ref)[0]); |
|
|
137 |
|
|
138 // Validate the thread. |
|
|
139 Cyg_Thread *th = Cyg_Thread::get_list_head(); |
|
|
140 while( th != 0 ) |
|
|
141 { |
|
|
142 if( th == thread ) break; |
|
|
143 th = th->get_list_next(); |
|
|
144 } |
|
|
145 |
|
|
146 // if( thread->get_unique_id() != id ) th = 0; |
|
|
147 |
|
|
148 #endif |
|
|
149 |
|
|
150 return th; |
|
|
151 } |
|
|
152 |
|
|
153 //-------------------------------------------------------------------------- |
|
|
154 |
|
|
155 externC int dbg_currthread(threadref * varparm) |
|
|
156 { |
|
|
157 Cyg_Thread *thread = Cyg_Scheduler::get_current_thread(); |
|
|
158 |
|
|
159 dbg_make_threadref(thread, varparm ); |
|
|
160 |
|
|
161 return 1 ; |
|
|
162 } |
|
|
163 |
|
|
164 //-------------------------------------------------------------------------- |
|
|
165 |
|
|
166 externC int dbg_currthread_id(void) |
|
|
167 { |
|
|
168 Cyg_Thread *thread = Cyg_Scheduler::get_current_thread(); |
|
|
169 return thread->get_unique_id (); |
|
|
170 } |
|
|
171 |
|
|
172 //-------------------------------------------------------------------------- |
|
|
173 |
|
|
174 externC int dbg_threadlist(int startflag, |
|
2
|
175 threadref * lastthreadid, |
|
|
176 threadref * next_thread) |
|
0
|
177 { |
|
|
178 Cyg_Thread *thread; |
|
|
179 if( startflag ) |
|
|
180 { |
|
|
181 thread = Cyg_Thread::get_list_head(); |
|
|
182 dbg_make_threadref(thread, next_thread); |
|
|
183 } |
|
|
184 else |
|
|
185 { |
|
|
186 thread = dbg_get_thread(lastthreadid); |
|
|
187 |
|
|
188 if( thread == 0 ) return 0; |
|
|
189 thread = thread->get_list_next(); |
|
|
190 |
|
|
191 if( thread == 0 ) return 0; |
|
|
192 dbg_make_threadref(thread, next_thread); |
|
|
193 } |
|
|
194 return 1 ; |
|
|
195 } |
|
|
196 |
|
|
197 //-------------------------------------------------------------------------- |
|
|
198 // Some support routines for manufacturing thread info strings |
|
|
199 |
|
|
200 static char *dbg_addstr(char *s, char *t) |
|
|
201 { |
|
|
202 while( (*s++ = *t++) != 0 ); |
|
|
203 |
|
|
204 return s-1; |
|
|
205 } |
|
|
206 |
|
|
207 static char *dbg_addint(char *s, int n, int base) |
|
|
208 { |
|
|
209 char buf[16]; |
|
|
210 char sign = '+'; |
|
|
211 cyg_count8 bpos; |
|
|
212 char *digits = "0123456789ABCDEF"; |
|
|
213 |
|
|
214 if( n < 0 ) n = -n, sign = '-'; |
|
|
215 |
|
|
216 /* Set pos to start */ |
|
|
217 bpos = 0; |
|
|
218 |
|
|
219 /* construct digits into buffer in reverse order */ |
|
|
220 if( n == 0 ) buf[bpos++] = '0'; |
|
|
221 else while( n != 0 ) |
|
|
222 { |
|
|
223 cyg_ucount8 d = n % base; |
|
|
224 buf[bpos++] = digits[d]; |
|
|
225 n /= base; |
|
|
226 } |
|
|
227 |
|
|
228 /* set sign if negative. */ |
|
|
229 if( sign == '-' ) |
|
|
230 { |
|
|
231 buf[bpos] = sign; |
|
|
232 } |
|
|
233 else bpos--; |
|
|
234 |
|
|
235 /* Now write it out in correct order. */ |
|
|
236 while( bpos >= 0 ) |
|
|
237 *s++ = buf[bpos--]; |
|
|
238 |
|
|
239 *s = 0; |
|
|
240 |
|
|
241 return s; |
|
|
242 } |
|
|
243 |
|
|
244 static char *dbg_adddec(char *s, int x) |
|
|
245 { |
|
|
246 return dbg_addint(s, x, 10); |
|
|
247 } |
|
|
248 |
|
|
249 //-------------------------------------------------------------------------- |
|
|
250 |
|
|
251 externC int dbg_threadinfo( |
|
2
|
252 threadref * threadid, |
|
|
253 struct cygmon_thread_debug_info * info) |
|
0
|
254 { |
|
|
255 static char statebuf[60]; |
|
|
256 |
|
|
257 Cyg_Thread *thread = dbg_get_thread(threadid); |
|
|
258 if( thread == 0 ) return 0; |
|
|
259 |
|
|
260 info->context_exists = 1; |
|
|
261 |
|
|
262 char *sbp = statebuf; |
|
|
263 char *s; |
|
|
264 |
|
|
265 if( thread->get_state() & Cyg_Thread::SUSPENDED ) |
|
|
266 { |
|
|
267 sbp = dbg_addstr( sbp, "suspended+"); |
|
|
268 } |
|
|
269 |
|
|
270 switch( thread->get_state() & ~Cyg_Thread::SUSPENDED ) |
|
|
271 { |
|
|
272 case Cyg_Thread::RUNNING: |
|
|
273 s = "running"; break; |
|
|
274 case Cyg_Thread::SLEEPING: |
|
|
275 s = "sleeping"; break; |
|
|
276 case Cyg_Thread::COUNTSLEEP: |
|
|
277 s = "counted sleep"; break; |
|
|
278 case Cyg_Thread::CREATING: |
|
|
279 s = "creating"; break; |
|
|
280 case Cyg_Thread::EXITED: |
|
|
281 s = "exited"; break; |
|
|
282 default: |
|
|
283 s = "unknown state"; break; |
|
|
284 } |
|
|
285 |
|
|
286 sbp = dbg_addstr( sbp, s ); |
|
|
287 sbp = dbg_addstr( sbp, ", Priority: " ); |
|
|
288 sbp = dbg_adddec( sbp, thread->get_priority() ); |
|
|
289 |
|
|
290 info->thread_display = statebuf; |
|
|
291 |
|
|
292 #ifdef CYGVAR_KERNEL_THREADS_NAME |
|
|
293 info->unique_thread_name = thread->get_name(); |
|
|
294 #else |
|
|
295 info->unique_thread_name = 0; |
|
|
296 #endif |
|
|
297 |
|
|
298 info->more_display = 0; |
|
|
299 |
|
|
300 return 1 ; |
|
|
301 } |
|
|
302 |
|
|
303 //-------------------------------------------------------------------------- |
|
|
304 |
|
|
305 externC int dbg_getthreadreg( |
|
2
|
306 threadref * osthreadid, |
|
|
307 int regcount, /* count of registers in the array */ |
|
|
308 void * regval) /* fillin this array */ |
|
0
|
309 { |
|
|
310 Cyg_Thread *thread = dbg_get_thread(osthreadid); |
|
|
311 |
|
|
312 if( thread == 0 ) return 0; |
|
|
313 |
|
|
314 if( thread == Cyg_Scheduler::get_current_thread() ) |
|
|
315 { |
|
|
316 #if defined(CYG_HAL_USE_ROM_MONITOR_CYGMON) |
|
|
317 // We have no state for the current thread, Cygmon has |
|
|
318 // got that and we cannot get at it. |
|
|
319 return 0; |
|
|
320 #elif defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS) |
|
2
|
321 // registers hold the state of the current thread. |
|
|
322 __stub_copy_registers ((target_register_t *)regval, registers); |
|
0
|
323 #else |
|
|
324 return 0; |
|
|
325 #endif |
|
|
326 } |
|
|
327 else |
|
|
328 { |
|
|
329 HAL_SavedRegisters *regs = thread->get_saved_context(); |
|
|
330 if( regs == 0 ) return 0; |
|
|
331 |
|
|
332 HAL_GET_GDB_REGISTERS (regval, regs); |
|
|
333 } |
|
|
334 |
|
|
335 return 1 ; |
|
|
336 } |
|
|
337 |
|
|
338 //-------------------------------------------------------------------------- |
|
2
|
339 |
|
0
|
340 externC int dbg_setthreadreg( |
|
2
|
341 threadref * osthreadid, |
|
|
342 int regcount , /* number of registers */ |
|
|
343 void * regval) |
|
0
|
344 { |
|
|
345 Cyg_Thread *thread = dbg_get_thread(osthreadid); |
|
|
346 |
|
|
347 if( thread == 0 ) return 0; |
|
|
348 |
|
|
349 if( thread == Cyg_Scheduler::get_current_thread() ) |
|
|
350 { |
|
|
351 #if defined(CYG_HAL_USE_ROM_MONITOR_CYGMON) |
|
|
352 // We have no state for the current thread, Cygmon has |
|
|
353 // got that and we cannot get at it. |
|
|
354 return 0; |
|
|
355 #elif defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS) |
|
2
|
356 // registers hold the state of the current thread. |
|
|
357 __stub_copy_registers (registers, (target_register_t *)regval); |
|
0
|
358 #else |
|
|
359 return 0; |
|
|
360 #endif |
|
|
361 } |
|
|
362 else |
|
|
363 { |
|
|
364 HAL_SavedRegisters *regs = thread->get_saved_context(); |
|
|
365 if( regs == 0 ) return 0; |
|
|
366 |
|
|
367 HAL_SET_GDB_REGISTERS (regs, regval); |
|
|
368 } |
|
|
369 |
|
|
370 return 1; |
|
|
371 } |
|
|
372 |
|
2
|
373 //-------------------------------------------------------------------------- |
|
|
374 // Thread scheduler control for debugger. |
|
|
375 // Arguments: |
|
|
376 // osthreadid : must match currently executing thread. |
|
|
377 // Future use: change the currently executing thread. |
|
|
378 // lock : 0 == unlock scheduler, 1 == lock scheduler |
|
|
379 // mode : 0 == single-instruction step, 1 == free running |
|
|
380 // |
|
|
381 // Return values: |
|
|
382 // 1 == success |
|
|
383 // 0 == failure |
|
|
384 // -1 == request that the caller handle this itself |
|
|
385 // (eg.by disabling interrupts) |
|
|
386 // |
|
|
387 |
|
|
388 externC int dbg_scheduler( |
|
|
389 threadref * osthreadid, |
|
|
390 int lock, /* 0 == unlock, 1 == lock */ |
|
|
391 int mode) /* 0 == step, 1 == continue */ |
|
|
392 { |
|
|
393 #if 0 |
|
|
394 /* Minimal implementation: let stub do the work. */ |
|
|
395 return -1; // Stub will disable interrupts |
|
|
396 #else |
|
|
397 Cyg_Thread *thread = dbg_get_thread(osthreadid); |
|
|
398 |
|
|
399 if( thread == 0 ) return 0; // fail |
|
|
400 |
|
|
401 if( thread == Cyg_Scheduler::get_current_thread() ) |
|
|
402 { |
|
|
403 // OK to proceed |
|
|
404 |
|
|
405 if (lock) |
|
|
406 { |
|
|
407 Cyg_Scheduler::lock(); |
|
|
408 } |
|
|
409 else |
|
|
410 { |
|
|
411 if (Cyg_Scheduler::get_sched_lock() >= 1) |
|
|
412 Cyg_Scheduler::unlock_simple(); |
|
|
413 } |
|
|
414 return 1; // success |
|
|
415 } |
|
|
416 else |
|
|
417 { |
|
|
418 // Cannot accept any thread other than current one |
|
|
419 return 0; // fail |
|
|
420 } |
|
|
421 #endif |
|
|
422 } |
|
|
423 |
|
|
424 |
|
0
|
425 #endif // CYGDBG_KERNEL_DEBUG_GDB_THREAD_SUPPORT |
|
|
426 |
|
|
427 //-------------------------------------------------------------------------- |
|
|
428 // End of dbg_gdb.cxx |