gdbthreads.c
18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*---------------------------------------------------------------------*
* Copyright (C) 2004 BroadOn Communications Corp.
*
* $RCSfile: gdbthreads.c,v $ (derived from rmontask.c)
* $Revision: 1.4 $
* $Date: 2004/02/24 20:50:31 $
*---------------------------------------------------------------------*/
/*
* This file provides the thread handling functions for the GDB stub
* for debugging the BBPlayer
*/
#include "ultragdb.h"
#include <R4300.h>
#include <sys/signal.h>
extern OSThread *__osActiveQueue;
extern OSThread __osThreadSave;
extern OSThread *__osRunningThread;
/* Separate buffer for BB to GDB messages for when a breakpoint
is encountered. This is used by the gdbMain thread. */
static char __gdbBreakBuffer[__GDB_BUFMAX];
static int __gdbUserThreadsStopped = 0;
OSThread *__gdb_curRunThread = GDB_ALL_THREADS; /* Thread for continue/step commands */
OSThread *__gdb_curThread = GDB_ALL_THREADS; /* Thread for other commands */
/**************** GDB Thread Support Functions **************/
OSThread* __gdbGetActiveQueue(void) /* Same as __osGetActiveQueue */
{
return (__osActiveQueue);
}
int __gdbGetThreadID(OSThread* t)
{
#if GDB_USE_THREADID
/* Use the thread ID from the structure */
if (t == NULL) return 0;
if (t == GDB_ALL_THREADS) return -1;
return t->id;
#else
/* Use the physical address as the thread id */
/* Don't use the virtual address because
GDB does something funny with negative numbers */
return osVirtualToPhysical(t);
#endif
}
/************************************************************************
Function: __gdbFindIdleThread
Purpose: This function finds the idle thread (i.e. priority is 0)
Returns: OSThread* - Pointer to the idle thread, NULL if none found.
************************************************************************/
OSThread* __gdbFindIdleThread()
{
OSThread *t;
/* Finds and returns the idle thread */
/* loop through the list of current threads */
for (t = __gdbGetActiveQueue(); (t != NULL) && (t->priority != -1); t = t->tlnext)
{
if (__OS_IS_IDLE_THREAD(t)) {
return t;
}
}
if (gdbDebug & GDB_DEBUG_SHOW_ERROR)
GDB_PRINTF("%s: Idle thread not found\n", gdb_module_name);
return NULL;
}
/************************************************************************
Function: __gdbFindUserThread
Purpose: This function finds a user thread (i.e. priority from 1 to 127)
Returns: OSThread* - Pointer to a user thread, NULL if none found.
************************************************************************/
OSThread* __gdbFindUserThread()
{
OSThread *t;
/* Finds and returns the user thread */
/* loop through the list of current threads */
for (t = __gdbGetActiveQueue(); (t != NULL) && (t->priority != -1); t = t->tlnext)
{
if (__OS_IS_USER_THREAD(t)) {
return t;
}
}
if (gdbDebug & GDB_DEBUG_SHOW_ERROR)
GDB_PRINTF("%s: User thread not found\n", gdb_module_name);
return NULL;
}
/************************************************************************
Function: gdbGetSignal
Args: u32 cause - The R4300 cause register
Purpose: This function takes the cause of a break/fault and determines
the appropriate signal to send to GDB.
Returns: int - GDB signal corresponding to the cause
************************************************************************/
int gdbGetSignal( u32 cause )
{
int sig;
switch (cause & CAUSE_EXCMASK) {
case (EXC_INT): sig = SIGINT; break; /* interrupt */
case (EXC_BREAK): sig = SIGTRAP; break; /* BREAKpoint */
case (EXC_WATCH): sig = SIGTRAP; break; /* Watchpoint reference */
case (EXC_TRAP): sig = SIGTRAP; break; /* Trap exception */
case (EXC_RADE): sig = SIGSEGV; break; /* Read Address Error */
case (EXC_WADE): sig = SIGSEGV; break; /* Write Address Error */
case (EXC_RMISS): sig = SIGSEGV; break; /* Read TLB Miss */
case (EXC_WMISS): sig = SIGSEGV; break; /* Write TLB Miss */
case (EXC_IBE): sig = SIGBUS; break; /* Instruction Bus Error */
case (EXC_DBE): sig = SIGBUS; break; /* Data Bus Error */
case (EXC_II): sig = SIGILL; break; /* Illegal Instruction */
case (EXC_FPE): sig = SIGFPE; break; /* Floating Point Exception */
case (EXC_SYSCALL): sig = SIGSYS; break; /* SYSCALL */
default: sig = 7; /* software generated */
}
return sig;
}
/************************************************************************
Function: __gdbIDToThread
Args: int threadID - Thread ID
Purpose: This function returns a pointer to the OSThread structure
given a threadID.
Returns: OSThread* - Pointer to thread with given thread ID
NULL if there are no threads with the given thread ID
************************************************************************/
OSThread * __gdbIDToThread(int threadID)
{
OSThread *t;
/* loop through the list of current threads */
for (t = __gdbGetActiveQueue(); (t != NULL) && (t->priority != -1); t = t->tlnext)
{
if (__gdbGetThreadID(t) == threadID) {
return t;
}
}
if (gdbDebug & GDB_DEBUG_SHOW_ERROR)
GDB_PRINTF("%s: Thread 0x%x not found\n", gdb_module_name, threadID);
return NULL;
}
/************************************************************************
Function: (static) __gdbRunThread
Args: OSThread* tptr - Pointer to thread
int flags - The flags field specifies actions:
KK_STEP: single step
KK_VADDR: set PC before running
KK_ABORT: commit suicide (NOT IMPLEMENTED)
int addr - Address at which to continue running
Type: int - returns 0 if successful, otherwise error type (<0)
Purpose: This function is a GDB internal function that runs the
specified thread. DO NOT call this function directly, use
gdbRunThread instead.
************************************************************************/
static int __gdbRunThread(OSThread* tptr, int flags, int addr)
{
register int runNeeded = 0;
/* Is this a legitimate thread? */
if ((tptr == NULL) || (tptr->priority == -1))
return GDB_ERROR_INVALID_ID;
if ( tptr->state != OS_STATE_STOPPED )
return GDB_ERROR_OP_PROTECTED;
tptr->flags &= ~3; /* neither break nor fault */
if (flags & KK_VADDR ) /* set new pc */
tptr->context.pc = addr;
if (flags & KK_STEP )
if ( !__gdbSetSingleStep( tptr, (u32 *) tptr->context.pc ) )
return GDB_ERROR_OP_PROTECTED;
runNeeded = 1;
if ( runNeeded )
osStartThread( tptr );
return GDB_ERROR_NO_ERROR; /* Dispatch the app */
}
/************************************************************************
Function: gdbRunThread
Args: OSThread* thread - Pointer to thread
int flags - The flags field specifies actions:
KK_STEP: single step
KK_VADDR: set PC before running
KK_ABORT: commit suicide (NOT IMPLEMENTED)
int addr - Address at which to continue running
Type: int - returns 0 if successful, otherwise error type (<0)
Purpose: This function runs the specified thread. It runs all threads
if thread is GDB_ALL_THREADS (-1).
************************************************************************/
int gdbRunThread(OSThread* thread, int flags, int addr)
{
if (thread == GDB_ALL_THREADS)
{
/* Run all threads */
OSThread* t;
int err;
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF("%s: Starting all threads\n", gdb_module_name);
for (t = __gdbGetActiveQueue(); (t != NULL) && (t->priority != -1); t = t->tlnext)
{
err = __gdbRunThread(t, flags, addr);
}
__gdbUserThreadsStopped = 0;
return GDB_ERROR_NO_ERROR;
}
else {
if (thread != NULL)
{
__gdbUserThreadsStopped = 0;
return __gdbRunThread(thread, flags, addr);
}
return GDB_ERROR_INVALID_ID;
}
}
/************************************************************************
Function: gdbFindFaultedThreads
Purpose: This function finds all threads that have faulted due to
a breakpoint or otherwise, and sends a message to the host.
This function is called from the gdbMain thread when breakpoint
is hit. It will need to synchronize with the gdbIOHandler thread
to make sure that the message is sent only when GDB is expecting
it.
TO FIX: What if different threads have different fault states/signals??
The GDB serial protocol doesn't seem able to handle that.
************************************************************************/
extern OSMesgQueue BKmq;
void gdbFindFaultedThreads( void )
{
int nfaulted = 0;
OSThread* t;
OSThread* faultedThread = NULL;
char* buf = &__gdbBreakBuffer[0];
buf[0] = 0;
/* loop through the list of current threads */
for (t = __gdbGetActiveQueue(); (t != NULL) && (t->priority != -1); t = t->tlnext)
{
int isUserThread = __OS_IS_USER_THREAD(t);
int tid = __gdbGetThreadID(t);
if (gdbShowAllThreadsFlag || isUserThread)
{
int inst = 0;
int cause = t->context.cause;
int sigval = gdbGetSignal(cause);
if (t->flags & OS_FLAG_CPU_BREAK || t->flags & OS_FLAG_FAULT)
{
if (nfaulted == 0)
{
/* First fault - start message */
buf = __gdbStartThreadSignalReply(buf, sigval);
faultedThread = t;
}
nfaulted++;
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
{
if (t->flags & OS_FLAG_FAULT)
{
GDB_PRINTF("%s: Fault in thread %d at 0x%08x"
"due to cause=0x%x, signal=%d\n",
gdb_module_name, tid, t->context.pc,
cause, sigval);
__gdbSendFault( t );
}
else {
inst = * (int*) t->context.pc;
GDB_PRINTF("%s: Break in thread %d at 0x%08x, inst 0x%08x "
"due to cause=0x%x, signal=%d\n",
gdb_module_name, tid, t->context.pc,
inst, cause, sigval);
}
}
buf = __gdbWriteSRThreadID(buf, tid);
}
}
}
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF("%s: %d faulted threads found.\n",
gdb_module_name, nfaulted);
if (nfaulted == 0) return; /* No faulted threads */
/* Check flag to see if we are going to sync with another thread */
if (gdbWaitToSendBreakFlag)
{
/* Wait for GDB to need a breakpoint message */
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF("%s: waiting for GDB to be ready to get target status\n",
gdb_module_name);
osRecvMesg(&BKmq, NULL, OS_MESG_BLOCK);
}
/* At this point, GDB is waiting for the target to halt and will
not be sending data to the GDB IO processing thread */
/* Let's switch to the thread that faulted */
if (faultedThread) __gdb_curThread = faultedThread;
__gdbPutPacket(__gdbBreakBuffer);
}
/************************************************************************
Function: __gdbMaskIdleThreadInts
Args: none
Type: void
Purpose: This function is called when a fault occurs to change the idle
thread interrupt mask to allow only debugger interrupts to get
through.
************************************************************************/
void __gdbMaskIdleThreadInts( void )
{
register OSThread * tptr = __gdbGetActiveQueue();
while ( tptr->priority != -1 )
{
if ( tptr->priority == 0 ) /* found it */
{
tptr->context.sr &= ~OS_IM_CPU;
tptr->context.sr |= (OS_IM_CART |
OS_IM_RDBWRITE | OS_IM_RDBREAD);
break;
}
tptr = tptr->tlnext;
}
}
/************************************************************************
Function: (static) __gdbStopThread
Args: OSThread* tptr - Pointer to thread to stop.
Returns: int - Thread ID if the thread was successfully stop.
Error code (<0) otherwise.
GDB_ERROR_INVALID_ID - Invalid thread.
GDB_ERROR_OP_FAILED - Cannot stop thread.
Purpose: This function stops the specified thread.
************************************************************************/
static int __gdbStopThread(OSThread* tptr)
{
if (tptr == NULL) return GDB_ERROR_INVALID_ID;
osStopThread( tptr );
if ( tptr->state != OS_STATE_STOPPED )
{
if (gdbDebug & GDB_DEBUG_SHOW_ERROR)
GDB_PRINTF("%s: Couldn't stop thread %d\n",
gdb_module_name, __gdbGetThreadID(tptr) );
return GDB_ERROR_OP_FAILED;
}
return __gdbGetThreadID(tptr);
}
/************************************************************************
Function: gdbStopUserThread
Args: OSThread* tptr - Pointer to thread to stop.
GDB_ALL_THREADS (-1) for all user threads.
Type: int - the ID of the thread that stopped, or 0 if unknown
If the thread was not stopped, then an error code (<0)
is returned:
GDB_ERROR_INVALID_ID - Invalid thread.
GDB_ERROR_OP_FAILED - Cannot stop thread.
GDB_ERROR_OP_PROTECTED - Was not a user thread or no idle thread.
Purpose: This function stops one or more running threads. If a thread number
is specified, then the function was called from the explicit stop
thread command. If zero, then it was called in response to a break
or fault being hit, which (for now) stops all threads from running.
************************************************************************/
int gdbStopUserThread(OSThread* tptr)
{
if (__gdbUserThreadsStopped) /* Threads already stopped */
return GDB_ERROR_NO_ERROR;
if (tptr == GDB_ALL_THREADS)
{
OSThread* t;
/* Let's always set this even if we can't stop the threads so we
don't annoy the user with the messages */
__gdbUserThreadsStopped = 1;
/* Make sure we have a idle thread - otherwise if we stop all the
user threads, we may lock up the system */
t = __gdbFindIdleThread();
if (t == NULL)
{
/* Always print this message - very nasty!!! */
GDB_PRINTF("%s: WARNING - No idle (priority = %d) thread found!!!."
" Refusing to stop user threads for debugging!!!.\n",
gdb_module_name, OS_PRIORITY_IDLE);
return GDB_ERROR_OP_PROTECTED;
}
for (t = __gdbGetActiveQueue(); (t != NULL) && (t->priority != -1); t = t->tlnext)
{
int isUserThread = __OS_IS_USER_THREAD(t);
if (isUserThread)
{
__gdbStopThread(t);
}
}
return GDB_ERROR_NO_ERROR;
}
else if (tptr != NULL)
{
int isUserThread = __OS_IS_USER_THREAD(tptr);
if (isUserThread)
{
return __gdbStopThread( tptr );
}
return GDB_ERROR_OP_PROTECTED;
}
else return GDB_ERROR_INVALID_ID;
}
OSThread* getOneCurrentThread()
{
OSThread* tmp_thread;
if (__gdb_curThread == NULL)
{
/* No Thread selected - lets get the idle thread */
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF("%s: No thread selected, using user or idle thread\n",
gdb_module_name);
tmp_thread = __gdbFindUserThread();
if (tmp_thread == NULL)
tmp_thread = __gdbFindIdleThread();
__gdb_curThread = tmp_thread;
}
else if (__gdb_curThread == GDB_ALL_THREADS)
{
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF("%s: All thread selected, using user or idle thread\n",
gdb_module_name);
/* All threads selected - just return user/idle thread */
tmp_thread = __gdbFindUserThread();
if (tmp_thread == NULL)
tmp_thread = __gdbFindIdleThread();
return tmp_thread;
}
return __gdb_curThread;
}
int __gdbGetCurrentThreadID()
{
OSThread* t = getOneCurrentThread();
if (__gdb_curThread == NULL)
{
if (gdbDebug & GDB_DEBUG_SHOW_ERROR)
GDB_PRINTF("%s: No thread selected and no user or idle threads,
using debugging thread ID\n", gdb_module_name);
return __gdbGetThreadID(__osRunningThread);
}
else return __gdbGetThreadID(t);
}
int __gdbSetRequestedThread(OSThread** thread, int tid)
{
OSThread* tmp_thread;
if (thread == NULL) return -1;
if (tid == -1) {
/* All Threads */
*thread = GDB_ALL_THREADS;
}
else if (tid == 0) {
/* Pick any thread - this is so vague, let's pick all threads just
* to be sure. This is useful for handling the following scenario:
* If someone does "set scheduler-lock on", then GDB will set the
* run thread to the current thread.
* But if someone does "set scheduler-lock off" after that, GDB
* will set the run thread to 0. In this case, we really want
* GDB to continue all threads again, not just any old thread.
**/
*thread = GDB_ALL_THREADS;
}
else {
tmp_thread = __gdbIDToThread(tid);
if (tmp_thread) {
*thread = tmp_thread;
} else {
if (gdbDebug & GDB_DEBUG_SHOW_ERROR)
GDB_PRINTF("%s: unknown thread %d\n", gdb_module_name, tid);
/* Didn't find a thread - set error */
return GDB_ERROR_INVALID_ID;
}
}
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF("%s: Selected thread 0x%x, thread ID %d\n",
gdb_module_name, *thread, __gdbGetThreadID(*thread));
return GDB_ERROR_NO_ERROR;
}