gdbbrk.c
18 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
/*---------------------------------------------------------------------*
* Copyright (C) 2004 BroadOn Communications Corp.
*
* $RCSfile: gdbbrk.c,v $ (derived from rmonbrk.c)
* $Revision: 1.2 $
* $Date: 2004/02/17 23:20:02 $
*---------------------------------------------------------------------*/
/*
* This file provides the functions for handling breakpoints for the GDB stub
* for debugging the BBPlayer
*/
/************************************************************************
Program: GDB Stub for the BBPlayer
File: gdbbrk.c (derived from rmonbrk.c with RSP functionality removed)
Author: Angel Chang
Purpose: The breakpoint access functions
************************************************************************/
#ifndef _FINALROM
#include "ultragdb.h"
#define __GDB_NBREAKS 16 /* number of breakpoints */
#define GDB_MSG_EXCEPTION 0x8004
/*
The following macro defines a breakpoint instruction with a
built in breakpoint number. Thus, we always know which
breakpoint was hit as soon as the break occurs, saving the
time and trouble of actually looking it up. We add 16 to the
break number to stay away from compiler-defined breaks and
user set breaks.
*/
#define __GDB_BREAKINST(n) (0x0d | (((n+16) & 0xfffff) << 6))
/*
The following structure holds information about active breakpoints.
The 0th entry is the temproary breakpoint, inserted when the user
specifies "run to location" and removed when ANY breakpoint is
encountered. There are also (__GDB_NBREAKS - 1) persistent breakpoints.
These must be manually set and cleared. On encountering one, it
remains set, although the original contents of memory may be
requested from time to time.
An alternate breakpoint is used for single stepping the CPU.
Since we may step over a conditional branch, we don't know which
way the branch will go. Thus, we set breakpoints at both locations
and run until either is hit. This means that sometimes we need two
breaks to single step, in which case we use break 0 and break alt.
The RSP breakpoints use the same structure. Since single step
is implemented in hardware, we don't need the temp breaks.
*/
/* TODO: What happens if the temp/permanent breakpoints overlap? */
typedef struct {
u32 * breakAddress;
u32 oldInstruction;
} BREAKINFO;
static BREAKINFO __gdbBreakpoints[__GDB_NBREAKS]; /* the CPU breaks */
static BREAKINFO __gdbTmpBreaks[2]; /* temporary breaks for stepping */
/************************************************************************
Function: (static) __gdbSetBreakpoint
Args: BREAKINFO* brk - Pointer to breakpoint to set
u32* - Address of breakpoint
Purpose: This function sets a breakpoint at the specified address (addr)
and stores the old instruction and the address in brk.
************************************************************************/
static void __gdbSetBreakpoint(BREAKINFO* brk, u32* addr)
{
if (addr == NULL) return;
if (brk == NULL) return;
brk->oldInstruction = *addr;
*addr = __GDB_BREAKINST( 0 );
gdbWritebackDCache( addr, 4 );
gdbInvalICache( addr, 4 );
brk->breakAddress = addr;
}
/************************************************************************
Function: (static) __gdbClearBreakpoint
Args: BREAKINFO* brk - Pointer to breakpoint to clear
Purpose: This function clears the breakpoint by restoring the old
instruction and clearoing the remembered breakpoint address.
************************************************************************/
static void __gdbClearBreakpoint(BREAKINFO* brk)
{
u32 inst;
if (brk && brk->breakAddress)
{
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF( "%s: Clear Break @ 0x%x\n",
gdb_module_name, brk->breakAddress );
/* Check to make sure one was really there */
inst = *brk->breakAddress;
if ( (inst & GDB_BREAKMASK) == 0xd )
{
*(brk->breakAddress) = brk->oldInstruction;
gdbWritebackDCache( brk->breakAddress, 4 );
gdbInvalICache( brk->breakAddress, 4 );
}
brk->breakAddress = 0;
}
}
/************************************************************************
Function: (static) __gdbFindBreak
Args: addr - Address of breakpoint
Returns: int - Returns breakpoint index, otherwise error type (<0)
0 to __GDB_NBREAK (breakpoint index) on success
GDB_ERROR_INVALID_ADDRESS if the address does not
correspond to a breakpoint
Purpose: This function finds the breakpoint at the given address
permanent breakpoint was set.
************************************************************************/
static BREAKINFO* __gdbFindBreak(u32* addr)
{
register BREAKINFO * whichBreak;
u32 i;
for (i = 0; i < __GDB_NBREAKS; i++)
{
whichBreak = &__gdbBreakpoints[i];
if (whichBreak->breakAddress == addr)
return whichBreak;
}
return NULL; /* break not set at this address */
}
/************************************************************************
Function: (static) __gdbSetTempBreakpoint
Args: u32 * addr1 - the address where the breakpoint is to be set
u32 * addr2 - the address of the optional second location
Type: void
Purpose: This function sets a temporary breakpoint. The temporary
breakpoint will be removed when ANY breakpoint is encountered.
Setting the temporary breakpoint is not a response to a user
set break command, but is a side effect of other commands like
step over line. The debugger sets a breakpoint at the location
corresponding to the next source line, runs the application,
and removes the breakpoint as soon as it is hit. This command
is also used to implement single-step, since the MIPS CPU has
no trace bit. If single stepping over a conditional jump, then
we need two breakpoints (one at each target).
TODO: See if there is a permanent break at this address.
************************************************************************/
static void __gdbSetTempBreakpoint( u32 * addr1, u32 * addr2 )
{
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
{
GDB_PRINTF("%s: Set temp BP at 0x%x and 0x%x\n",
gdb_module_name, addr1, addr2);
}
__gdbSetBreakpoint(&__gdbTmpBreaks[0], addr1);
__gdbSetBreakpoint(&__gdbTmpBreaks[1], addr2);
}
/************************************************************************
Function: (static) __gdbClearTempBreakpoint
Args: none
Type: void
Purpose: This function restores the contents of memory where the
temporary breakpoint was set. Note that the breakinfo does not
really need to be cleared. This function should be called by
the break ISR when any breakpoint is encountered. Since the
alternate break may be set from a single step of a branch,
we clear it too if it was set.
TODO: See if there is a permanent break at this address.
************************************************************************/
static void __gdbClearTempBreakpoint( void )
{
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF( "%s: Clearing temp breakpoints\n", gdb_module_name);
__gdbClearBreakpoint(&__gdbTmpBreaks[0]);
__gdbClearBreakpoint(&__gdbTmpBreaks[1]);
}
/************************************************************************
Function: gdbRemoveBreakpoint
Args: type - The type of the breakpoint to insert (NOT USED)
addr - The address at which to insert a breakpoint
length - How many bytes the breakpoint should cover (NOT USED)
Type: int - returns 0 if successful, otherwise error type (<0)
Purpose: This function removes a permanent breakpoint.
************************************************************************/
int gdbRemoveBreakpoint(int type, u32* addr, int length)
{
BREAKINFO* br;
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF( "%s: Remove breakpoint at 0x%x\n",
gdb_module_name, addr);
br = __gdbFindBreak(addr);
if (br)
{
__gdbClearBreakpoint(br);
return GDB_ERROR_NO_ERROR;
}
return GDB_ERROR_INVALID_ADDRESS;
}
/************************************************************************
Function: gdbRemoveAllBreakpoints
Type: int - returns number of breakpoints cleared
Purpose: This function removes all permanent breakpoints.
************************************************************************/
int gdbRemoveAllBreakpoints()
{
BREAKINFO* br;
u32 i, cnt;
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF( "%s: Removing all breakpoints\n", gdb_module_name);
cnt = 0;
for (i = 0; i < __GDB_NBREAKS; i++)
{
br = &__gdbBreakpoints[i];
if (br->breakAddress)
{
__gdbClearBreakpoint(br);
cnt++;
}
}
return cnt;
}
/************************************************************************
Function: gdbInsertBreakpoint
Args: type - The type of the breakpoint to insert (NOT USED)
addr - The address at which to insert a breakpoint
length - How many bytes the breakpoint should cover (NOT USED)
Type: int - returns 0 if successful, otherwise error type (<0)
Purpose: This function sets a permanent breakpoint. The permanent
breakpoint will remain set until explicitly cleared.
TODO: See if there is already a temporary break at this address.
************************************************************************/
int gdbInsertBreakpoint(int type, u32* addr, int length)
{
BREAKINFO* brk;
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF( "%s: Insert breakpoint at 0x%x\n",
gdb_module_name, addr);
/* Check to see if breakpoint already set */
brk = __gdbFindBreak(addr);
if (brk)
{
/* Break already exists - no need to set it */
return GDB_ERROR_NO_ERROR;
}
/* Find empty slot */
brk = __gdbFindBreak(NULL);
if (brk)
{
/* Empty spot found */
__gdbSetBreakpoint(brk, addr);
return GDB_ERROR_NO_ERROR;
}
else return GDB_ERROR_NO_MORE_IDS;
}
/************************************************************************
Function: (static) __gdbGetBranchTarget
Args: OSThread* thread - which thread is of interest
u32 * addr - address of an instruction to analyze
Type: u32 - The target address of the branch, or -1 if none
Purpose: This function examines an instruction and (if it is some kind of
jump or branch) returns the target address from register, calculated
relative address, or whatever.
???: Why is the addr a char*?
************************************************************************/
static u32 __gdbGetBranchTarget(OSThread* thread, char* addr )
{
int inst;
inst = *(int *) addr;
switch ((inst >> 26) & 0x3f)
{
case 0x0: /* jump register */
if ((((inst >> 5) & 0x7fff) == 0) &&
((inst & 0x3f) == 8))
return __gdbGetRegisterContents(thread, (inst>>21) & 0x1f );
/* jump and link register */
if ((((inst >> 16) & 0x1f) == 0) && ((inst & 0x7ff) == 9))
return __gdbGetRegisterContents( thread, (inst>>21) & 0x1f );
break;
case 0x1:
switch ((inst >> 16) & 0x1f)
{
case 0: /* branch on less than zero */
case 1: /* branch on greater than or equal to zero */
case 2: /* branch on less than zero likely */
case 3: /* branch on greater than or equal to zero likely */
case 0x10: /* branch on less than zero and link */
case 0x11: /* branch on greater than or equal to zero and link */
case 0x12: /* branch on less than zero and link likely */
case 0x13: /* branch on greater than or equal to zero and link likely */
return (u32) (((inst << 16) >> 14) + addr + 4);
}
break;
case 0x2: /* jump */
case 0x3: /* jump and link */
return ((u32)(((unsigned int)(inst << 6) >> 4) +
((((int) addr + 4) >> 28) << 28)));
case 0x4: /* branch on equal */
case 0x5: /* branch on not equal */
case 0x14: /* branch on equal likely */
case 0x15: /* branch on not equal likely */
return (u32)(((inst << 16) >> 14) + addr + 4);
case 0x6: /* branch on less than or equal to zero */
case 0x7: /* branch on greater than zero */
case 0x16: /* branch on less than or equal to zero likely */
case 0x17: /* branch on greater than zero likely */
if (((inst>>16) & 0x1f) == 0)
return (u32)(((inst << 16) >> 14) + addr + 4);
break;
case 0x10: /* branch on coprocessor instructions */
case 0x11:
case 0x12:
case 0x13:
if (((inst>>21) & 0x1f) == 8)
{ /* bc */
switch ((inst>>16) & 0x1f)
{
case 0: /* bcf */
case 2: /* bcfl */
case 1: /* bct */
case 3: /* bctl */
return (u32)(((inst << 16) >> 14) + addr + 4);
}
}
break;
}
/*
* XXX - didn't figure out the target, either is wasn't jump/branch
* or it could be branch on coprocessor (not implemented yet)
* since these had better be valid values for the program counter
* returning anything with the lower two bits means it didn't work.
*/
return (u32) -1;
}
/************************************************************************
Function: (static) __gdbIsJump
Args: u32 inst - an instruction to analyze
Type: int - returns boolean value
Purpose: This function examines an instruction and returns true if it is
a jump, false otherwise
************************************************************************/
static int __gdbIsJump( u32 inst )
{
switch ((inst >> 26) & 0x3f)
{
case 0x0: /* jump register */
if ((((inst >> 5) & 0x7fff) == 0) && ((inst & 0x3f) == 8))
return 1;
/* jump and link register */
if ((((inst >> 16) & 0x1f) == 0) && ((inst & 0x7ff) == 9))
return 1;
break;
case 0x2: /* jump */
case 0x3: /* jump and link */
return 1;
}
return 0;
}
/************************************************************************
Function: __gdbSetSingleStep
Args:
int thread - which thread is of interest
u32 * instptr - current PC for thread
Type: int - returns 1 if OK, 0 otherwise
Purpose: This function sets a temporary breakpoint for single stepping,
since the MIPS CPU has no trace bit. It must also check to see if
the instruction is a jump (in which case it must set the breakpoint
at the jump target) or a conditional branch (in which case it must
set TWO breakpoints). Otherwise, it just sets the breakpoint at
the next instruction.
TODO: Check to see if it is a break instruction; if so, put the real
instruction back, step over it, then put the breakpoint back.
************************************************************************/
int __gdbSetSingleStep( OSThread* thread, u32 * instptr )
{
u32 branchTarget;
if (gdbDebug & GDB_DEBUG_SHOW_INFO)
GDB_PRINTF( "%s: Single step for thead %d at 0x%x\n",
gdb_module_name, thread, instptr);
/*
Fetch the instruction and see what it is. Set breaks
accordingly. WARNING! If you were not paying sufficient
attention, you did not notice that the arg instptr
is a pointer to INSTRUCTIONS here, not chars or void.
Thus, incrementing it increases its numeric value by
four, not by one. Just like real life, only better.
We fetch the branch target, which will be invalid if
the instruction is not a branch.
*/
branchTarget = __gdbGetBranchTarget( thread, (char *) instptr );
if ( branchTarget & 3 ) /* then it wasn't a branch at all */
{
__gdbSetTempBreakpoint( instptr + 1, 0 );
}
/*
OK, it was some kind of branch, either a jump or a
conditional branch. If it is a jump, then just set
the breakpoint at the target. If it was a conditional
branch, set it at both possible targets. Of course,
you can't really set it in both places if they are
the same, so be careful about that possibility. Also,
you can't set a breakpoint on a jump to yourself,
because you wouldn't have an instruction to execute
after you substituted the breakpoint instruction for
the instruction you wanted to step through. Confusing,
but true. For now, we just do nothing in that case.
*/
else if ( branchTarget == (u32) instptr ) /* uh-oh, can't do this */
return 0;
else if ( __gdbIsJump( *instptr ) || (branchTarget == (u32) (instptr+2)) )
__gdbSetTempBreakpoint( (u32 *) branchTarget, 0 );
else
__gdbSetTempBreakpoint( (u32 *) branchTarget, instptr + 2 );
return 1;
}
/************************************************************************
Function: __gdbHitBreak
Args: int breakNumber - an indicator of which breakpoint was encountered.
Type: void
Purpose: This function is called whenever a breakpoint is hit. It clears
all temporary breakpoints, creates an exception packet for the
debugger, and sends it to the host.
************************************************************************/
void __gdbHitBreak( void )
{
__gdbClearTempBreakpoint();
gdbStopUserThread(GDB_ALL_THREADS);
gdbFindFaultedThreads();
}
/************************************************************************
Function: __gdbHitCpuFault
Args: none
Type: void
Purpose: This function is called whenever the CPU faults. It kills all
user threads, disables all interrupts except its own in the idle
thread, creates an exception packet for the
debugger, and sends it to the host.
************************************************************************/
void __gdbHitCpuFault( void )
{
__gdbMaskIdleThreadInts();
gdbStopUserThread(GDB_ALL_THREADS);
gdbFindFaultedThreads(); /* will send fault message */
}
#endif /* #ifndef _FINALROM */