1 /****************************************************************************
3 THIS SOFTWARE IS NOT COPYRIGHTED
5 HP offers the following for use in the public domain. HP makes no
6 warranty with regard to the software or it's performance and the
7 user accepts the software "AS IS" with all faults.
9 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
10 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 /****************************************************************************
16 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
18 * Module name: remcom.c $
20 * Date: 91/03/09 12:29:49 $
21 * Contributor: Lake Stevens Instrument Division$
23 * Description: low level support for gdb debugger. $
25 * Considerations: only works on target hardware $
27 * Written by: Glenn Engel $
28 * ModuleState: Experimental $
32 * To enable debugger support, two things need to happen. One, a
33 * call to set_debug_traps() is necessary in order to allow any breakpoints
34 * or error conditions to be properly intercepted and reported to gdb.
35 * Two, a breakpoint needs to be generated to begin communication. This
36 * is most easily accomplished by a call to breakpoint(). Breakpoint()
37 * simulates a breakpoint by executing a trap #1. The breakpoint instruction
38 * is hardwired to trap #1 because not to do so is a compatibility problem--
39 * there either should be a standard breakpoint instruction, or the protocol
40 * should be extended to provide some means to communicate which breakpoint
41 * instruction is in use (or have the stub insert the breakpoint).
43 * Some explanation is probably necessary to explain how exceptions are
44 * handled. When an exception is encountered the 68000 pushes the current
45 * program counter and status register onto the supervisor stack and then
46 * transfers execution to a location specified in it's vector table.
47 * The handlers for the exception vectors are hardwired to jmp to an address
48 * given by the relation: (exception - 256) * 6. These are decending
49 * addresses starting from -6, -12, -18, ... By allowing 6 bytes for
50 * each entry, a jsr, jmp, bsr, ... can be used to enter the exception
51 * handler. Using a jsr to handle an exception has an added benefit of
52 * allowing a single handler to service several exceptions and use the
53 * return address as the key differentiation. The vector number can be
54 * computed from the return address by [ exception = (addr + 1530) / 6 ].
55 * The sole purpose of the routine _catchException is to compute the
56 * exception number and push it on the stack in place of the return address.
57 * The external function exceptionHandler() is
58 * used to attach a specific handler to a specific m68k exception.
59 * For 68020 machines, the ability to have a return address around just
60 * so the vector can be determined is not necessary because the '020 pushes an
61 * extra word onto the stack containing the vector offset
63 * Because gdb will sometimes write to the stack area to execute function
64 * calls, this program cannot rely on using the supervisor stack so it
65 * uses it's own stack area reserved in the int array remcomStack.
69 * The following gdb commands are supported:
71 * command function Return value
73 * g return the value of the CPU registers hex data or ENN
74 * G set the value of the CPU registers OK or ENN
76 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
77 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
79 * c Resume at current address SNN ( signal NN)
80 * cAA..AA Continue at address AA..AA SNN
82 * s Step one instruction SNN
83 * sAA..AA Step one instruction from AA..AA SNN
87 * ? What was the last sigval ? SNN (signal NN)
89 * All commands and responses are sent with a packet which includes a
90 * checksum. A packet consists of
92 * $<packet info>#<checksum>.
95 * <packet info> :: <characters representing the command or response>
96 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
98 * When a packet is received, it is first acknowledged with either '+' or '-'.
99 * '+' indicates a successful transfer. '-' indicates a failed transfer.
104 * $m0,10#2a +$00010203040506070809101112131415#42
106 ****************************************************************************/
112 /************************************************************************
114 * external low-level support routines
116 typedef void (*ExceptionHook
)(int); /* pointer to function with int parm */
117 typedef void (*Function
)(); /* pointer to a function */
119 extern putDebugChar(); /* write a single character */
120 extern getDebugChar(); /* read and return a single char */
122 extern Function
exceptionHandler(); /* assign an exception handler */
123 extern ExceptionHook exceptionHook
; /* hook variable for errors/exceptions */
125 /************************/
126 /* FORWARD DECLARATIONS */
127 /************************/
129 initializeRemcomErrorFrame ();
131 /************************************************************************/
132 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
133 /* at least NUMREGBYTES*2 are needed for register packets */
136 static char initialized
; /* boolean flag. != 0 means we've been initialized */
139 /* debug > 0 prints ill-formed commands in valid packets & checksum errors */
141 static const char hexchars
[]="0123456789abcdef";
143 /* there are 180 bytes of registers on a 68020 w/68881 */
144 /* many of the fpa registers are 12 byte (96 bit) registers */
145 #define NUMREGBYTES 180
146 enum regnames
{D0
,D1
,D2
,D3
,D4
,D5
,D6
,D7
,
147 A0
,A1
,A2
,A3
,A4
,A5
,A6
,A7
,
149 FP0
,FP1
,FP2
,FP3
,FP4
,FP5
,FP6
,FP7
,
150 FPCONTROL
,FPSTATUS
,FPIADDR
154 /* We keep a whole frame cache here. "Why?", I hear you cry, "doesn't
155 GDB handle that sort of thing?" Well, yes, I believe the only
156 reason for this cache is to save and restore floating point state
157 (fsave/frestore). A cleaner way to do this would be to make the
158 fsave data part of the registers which GDB deals with like any
159 other registers. This should not be a performance problem if the
160 ability to read individual registers is added to the protocol. */
162 typedef struct FrameStruct
164 struct FrameStruct
*previous
;
165 int exceptionPC
; /* pc value when this frame created */
166 int exceptionVector
; /* cpu vector causing exception */
167 short frameSize
; /* size of cpu frame in words */
168 short sr
; /* for 68000, this not always sr */
172 int morejunk
[0]; /* exception frame, fp save... */
175 #define FRAMESIZE 500
176 int gdbFrameStack
[FRAMESIZE
];
177 static Frame
*lastFrame
;
180 * these should not be static cuz they can be used outside this module
182 int registers
[NUMREGBYTES
/4];
185 #define STACKSIZE 10000
186 int remcomStack
[STACKSIZE
/sizeof(int)];
187 static int* stackPtr
= &remcomStack
[STACKSIZE
/sizeof(int) - 1];
190 * In many cases, the system will want to continue exception processing
191 * when a continue command is given.
192 * oldExceptionHook is a function to invoke in this case.
195 static ExceptionHook oldExceptionHook
;
197 /* the size of the exception stack on the 68020 varies with the type of
198 * exception. The following table is the number of WORDS used
199 * for each exception format.
201 const short exceptionSize
[] = { 4,4,6,4,4,4,4,4,29,10,16,46,12,4,4,4 };
203 /************* jump buffer used for setjmp/longjmp **************************/
206 /*************************** ASSEMBLY CODE MACROS *************************/
209 #ifdef __HAVE_68881__
210 /* do an fsave, then remember the address to begin a restore from */
211 #define SAVE_FP_REGS() asm(" fsave a0@-"); \
212 asm(" fmovemx fp0-fp7,_registers+72"); \
213 asm(" fmoveml fpcr/fpsr/fpi,_registers+168");
214 #define RESTORE_FP_REGS() \
216 fmoveml _registers+168,fpcr/fpsr/fpi \n\
217 fmovemx _registers+72,fp0-fp7 \n\
218 cmpl #-1,a0@ | skip frestore flag set ? \n\
219 beq skip_frestore \n\
225 #define SAVE_FP_REGS()
226 #define RESTORE_FP_REGS()
227 #endif /* __HAVE_68881__ */
229 void return_to_super();
230 void return_to_user();
234 .globl _return_to_super
236 movel _registers+60,sp /* get new stack pointer */
237 movel _lastFrame,a0 /* get last frame info */
240 .globl _return_to_user
242 movel _registers+60,a0 /* get usp */
243 movel a0,usp /* set usp */
244 movel _superStack,sp /* get original stack pointer */
247 movel _lastFrame,a0 /* get last frame info */
248 movel a0@+,_lastFrame /* link in previous frame */
249 addql #8,a0 /* skip over pc, vector#*/
250 movew a0@+,d0 /* get # of words in cpu frame */
251 addw d0,a0 /* point to end of data */
252 addw d0,a0 /* point to end of data */
255 # copy the stack frame
262 asm(" moveml _registers,d0-d7/a0-a6");
263 asm(" rte"); /* pop and go! */
265 #define DISABLE_INTERRUPTS() asm(" oriw #0x0700,sr");
266 #define BREAKPOINT() asm(" trap #1");
268 /* this function is called immediately when a level 7 interrupt occurs */
269 /* if the previous interrupt level was 7 then we're already servicing */
270 /* this interrupt and an rte is in order to return to the debugger. */
271 /* For the 68000, the offset for sr is 6 due to the jsr return address */
274 .globl __debug_level7
278 asm(" movew sp@(2),d0");
280 asm(" movew sp@(6),d0");
282 asm(" andiw #0x700,d0
290 asm(" lea sp@(4),sp"); /* pull off 68000 return address */
294 extern void _catchException ();
297 /* This function is called when a 68020 exception occurs. It saves
298 * all the cpu and fpcp regs in the _registers array, creates a frame on a
299 * linked list of frames which has the cpu and fpcp stack frames needed
300 * to properly restore the context of these processors, and invokes
301 * an exception handler (remcom_handler).
303 * stack on entry: stack on exit:
304 * N bytes of junk exception # MSWord
305 * Exception Format Word exception # MSWord
306 * Program counter LSWord
307 * Program counter MSWord
314 .globl __catchException
316 DISABLE_INTERRUPTS();
318 moveml d0-d7/a0-a6,_registers /* save registers */
319 movel _lastFrame,a0 /* last frame pointer */
323 lea _registers,a5 /* get address of registers */
324 movew sp@,d1 /* get status register */
325 movew d1,a5@(66) /* save sr */
326 movel sp@(2),a4 /* save pc in a4 for later use */
327 movel a4,a5@(68) /* save pc in _regisers[] */
330 # figure out how many bytes in the stack frame
331 movew sp@(6),d0 /* get '020 exception format */
332 movew d0,d2 /* make a copy of format word */
333 andiw #0xf000,d0 /* mask off format type */
334 rolw #5,d0 /* rotate into the low byte *2 */
335 lea _exceptionSize,a1
336 addw d0,a1 /* index into the table */
337 movew a1@,d0 /* get number of words in frame */
338 movew d0,d3 /* save it */
339 subw d0,a0 /* adjust save pointer */
340 subw d0,a0 /* adjust save pointer(bytes) */
341 movel a0,a1 /* copy save pointer */
342 subql #1,d0 /* predecrement loop counter */
349 # now that the stack has been clenaed,
350 # save the a7 in use at time of exception
351 movel sp,_superStack /* save supervisor sp */
352 andiw #0x2000,d1 /* were we in supervisor mode ? */
354 movel a7,a5@(60) /* save a7 */
358 movel a1,a5@(60) /* save user stack pointer */
366 # compute exception number
367 andl #0xfff,d2 /* mask off vector offset */
368 lsrw #2,d2 /* divide by 4 to get vect num */
369 movel d2,a0@- /* save it */
371 # save pc causing exception
374 # save old frame link and set the new value
375 movel _lastFrame,a1 /* last frame pointer */
376 movel a1,a0@- /* save pointer to prev frame */
379 movel d2,sp@- /* push exception num */
380 movel _exceptionHook,a0 /* get address of handler */
381 jbsr a0@ /* and call it */
382 clrl sp@ /* replace exception num parm with frame ptr */
383 jbsr __returnFromException /* jbsr, but never returns */
386 /* This function is called when an exception occurs. It translates the
387 * return address found on the stack into an exception vector # which
388 * is then handled by either handle_exception or a system handler.
389 * _catchException provides a front end for both.
391 * stack on entry: stack on exit:
392 * Program counter MSWord exception # MSWord
393 * Program counter LSWord exception # MSWord
395 * Return Address MSWord
396 * Return Address LSWord
400 .globl __catchException
402 DISABLE_INTERRUPTS();
404 moveml d0-d7/a0-a6,_registers /* save registers */
405 movel _lastFrame,a0 /* last frame pointer */
409 lea _registers,a5 /* get address of registers */
410 movel sp@+,d2 /* pop return address */
411 addl #1530,d2 /* convert return addr to */
412 divs #6,d2 /* exception number */
415 moveql #3,d3 /* assume a three word frame */
417 cmpiw #3,d2 /* bus error or address error ? */
418 bgt normal /* if >3 then normal error */
419 movel sp@+,a0@- /* copy error info to frame buff*/
420 movel sp@+,a0@- /* these are never used */
421 moveql #7,d3 /* this is a 7 word frame */
424 movew sp@+,d1 /* pop status register */
425 movel sp@+,a4 /* pop program counter */
426 movew d1,a5@(66) /* save sr */
427 movel a4,a5@(68) /* save pc in _regisers[] */
428 movel a4,a0@- /* copy pc to frame buffer */
429 movew d1,a0@- /* copy sr to frame buffer */
431 movel sp,_superStack /* save supervisor sp */
433 andiw #0x2000,d1 /* were we in supervisor mode ? */
435 movel a7,a5@(60) /* save a7 */
438 movel usp,a1 /* save user stack pointer */
439 movel a1,a5@(60) /* save user stack pointer */
442 movew d3,a0@- /* push frame size in words */
443 movel d2,a0@- /* push vector number */
444 movel a4,a0@- /* push exception pc */
447 # save old frame link and set the new value
448 movel _lastFrame,a1 /* last frame pointer */
449 movel a1,a0@- /* save pointer to prev frame */
452 movel d2,sp@- /* push exception num */
453 movel _exceptionHook,a0 /* get address of handler */
454 jbsr a0@ /* and call it */
455 clrl sp@ /* replace exception num parm with frame ptr */
456 jbsr __returnFromException /* jbsr, but never returns */
462 * remcomHandler is a front end for handle_exception. It moves the
463 * stack pointer into an area reserved for debugger use in case the
464 * breakpoint happened in supervisor mode.
466 asm("_remcomHandler:");
467 asm(" addl #4,sp"); /* pop off return address */
468 asm(" movel sp@+,d0"); /* get the exception number */
469 asm(" movel _stackPtr,sp"); /* move to remcom stack area */
470 asm(" movel d0,sp@-"); /* push exception onto stack */
471 asm(" jbsr _handle_exception"); /* this never returns */
472 asm(" rts"); /* return */
474 void _returnFromException( Frame
*frame
)
476 /* if no passed in frame, use the last one */
480 frame
->frameSize
= 4;
482 frame
->fsaveHeader
= -1; /* restore regs, but we dont have fsave info*/
486 /* a 68000 cannot use the internal info pushed onto a bus error
487 * or address error frame when doing an RTE so don't put this info
488 * onto the stack or the stack will creep every time this happens.
493 /* throw away any frames in the list after this frame */
496 frame
->sr
= registers
[(int) PS
];
497 frame
->pc
= registers
[(int) PC
];
499 if (registers
[(int) PS
] & 0x2000)
501 /* return to supervisor mode... */
505 { /* return to user mode */
513 if ((ch
>= 'a') && (ch
<= 'f')) return (ch
-'a'+10);
514 if ((ch
>= '0') && (ch
<= '9')) return (ch
-'0');
515 if ((ch
>= 'A') && (ch
<= 'F')) return (ch
-'A'+10);
520 /* scan for the sequence $<data>#<checksum> */
521 void getpacket(buffer
)
524 unsigned char checksum
;
525 unsigned char xmitcsum
;
531 /* wait around for the start character, ignore all other characters */
532 while ((ch
= getDebugChar()) != '$');
538 /* now, read until a # or end of buffer is found */
539 while (count
< BUFMAX
) {
541 if (ch
== '#') break;
542 checksum
= checksum
+ ch
;
549 xmitcsum
= hex(getDebugChar()) << 4;
550 xmitcsum
+= hex(getDebugChar());
551 if ((remote_debug
) && (checksum
!= xmitcsum
)) {
552 fprintf(stderr
,"bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n",
553 checksum
,xmitcsum
,buffer
);
556 if (checksum
!= xmitcsum
) putDebugChar('-'); /* failed checksum */
558 putDebugChar('+'); /* successful transfer */
559 /* if a sequence char is present, reply the sequence ID */
560 if (buffer
[2] == ':') {
561 putDebugChar( buffer
[0] );
562 putDebugChar( buffer
[1] );
563 /* remove sequence chars from buffer */
564 count
= strlen(buffer
);
565 for (i
=3; i
<= count
; i
++) buffer
[i
-3] = buffer
[i
];
569 } while (checksum
!= xmitcsum
);
573 /* send the packet in buffer. The host get's one chance to read it.
574 This routine does not wait for a positive acknowledge. */
577 void putpacket(buffer
)
580 unsigned char checksum
;
584 /* $<packet info>#<checksum>. */
590 while (ch
=buffer
[count
]) {
591 if (! putDebugChar(ch
)) return;
597 putDebugChar(hexchars
[checksum
>> 4]);
598 putDebugChar(hexchars
[checksum
% 16]);
600 } while (1 == 0); /* (getDebugChar() != '+'); */
604 char remcomInBuffer
[BUFMAX
];
605 char remcomOutBuffer
[BUFMAX
];
609 void debug_error(format
, parm
)
613 if (remote_debug
) fprintf(stderr
,format
,parm
);
616 /* convert the memory pointed to by mem into hex, placing result in buf */
617 /* return a pointer to the last char put in buf (null) */
618 char* mem2hex(mem
, buf
, count
)
625 for (i
=0;i
<count
;i
++) {
627 *buf
++ = hexchars
[ch
>> 4];
628 *buf
++ = hexchars
[ch
% 16];
634 /* convert the hex array pointed to by buf into binary to be placed in mem */
635 /* return a pointer to the character AFTER the last byte written */
636 char* hex2mem(buf
, mem
, count
)
643 for (i
=0;i
<count
;i
++) {
644 ch
= hex(*buf
++) << 4;
645 ch
= ch
+ hex(*buf
++);
651 /* a bus error has occurred, perform a longjmp
652 to return execution and allow handling of the error */
654 void handle_buserror()
656 longjmp(remcomEnv
,1);
659 /* this function takes the 68000 exception number and attempts to
660 translate this number into a unix compatible signal value */
661 int computeSignal( exceptionVector
)
665 switch (exceptionVector
) {
666 case 2 : sigval
= 10; break; /* bus error */
667 case 3 : sigval
= 10; break; /* address error */
668 case 4 : sigval
= 4; break; /* illegal instruction */
669 case 5 : sigval
= 8; break; /* zero divide */
670 case 6 : sigval
= 16; break; /* chk instruction */
671 case 7 : sigval
= 16; break; /* trapv instruction */
672 case 8 : sigval
= 11; break; /* privilege violation */
673 case 9 : sigval
= 5; break; /* trace trap */
674 case 10: sigval
= 4; break; /* line 1010 emulator */
675 case 11: sigval
= 4; break; /* line 1111 emulator */
676 case 13: sigval
= 8; break; /* floating point err */
677 case 31: sigval
= 2; break; /* interrupt */
678 case 33: sigval
= 5; break; /* breakpoint */
680 /* This is a trap #8 instruction. Apparently it is someone's software
681 convention for some sort of SIGFPE condition. Whose? How many
682 people are being screwed by having this code the way it is?
683 Is there a clean solution? */
684 case 40: sigval
= 8; break; /* floating point err */
686 case 48: sigval
= 8; break; /* floating point err */
687 case 49: sigval
= 8; break; /* floating point err */
688 case 50: sigval
= 8; break; /* zero divide */
689 case 51: sigval
= 8; break; /* underflow */
690 case 52: sigval
= 8; break; /* operand error */
691 case 53: sigval
= 8; break; /* overflow */
692 case 54: sigval
= 8; break; /* NAN */
694 sigval
= 7; /* "software generated"*/
699 /**********************************************/
700 /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
701 /* RETURN NUMBER OF CHARS PROCESSED */
702 /**********************************************/
703 int hexToInt(char **ptr
, int *intValue
)
712 hexValue
= hex(**ptr
);
715 *intValue
= (*intValue
<<4) | hexValue
;
728 * This function does all command procesing for interfacing to gdb.
730 void handle_exception(int exceptionVector
)
738 if (remote_debug
) printf("vector=%d, sr=0x%x, pc=0x%x\n",
743 /* reply to host that an exception has occurred */
744 sigval
= computeSignal( exceptionVector
);
745 remcomOutBuffer
[0] = 'S';
746 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
747 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
748 remcomOutBuffer
[3] = 0;
750 putpacket(remcomOutBuffer
);
754 remcomOutBuffer
[0] = 0;
755 getpacket(remcomInBuffer
);
756 switch (remcomInBuffer
[0]) {
757 case '?' : remcomOutBuffer
[0] = 'S';
758 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
759 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
760 remcomOutBuffer
[3] = 0;
762 case 'd' : remote_debug
= !(remote_debug
); /* toggle debug flag */
764 case 'g' : /* return the value of the CPU registers */
765 mem2hex((char*) registers
, remcomOutBuffer
, NUMREGBYTES
);
767 case 'G' : /* set the value of the CPU registers - return OK */
768 hex2mem(&remcomInBuffer
[1], (char*) registers
, NUMREGBYTES
);
769 strcpy(remcomOutBuffer
,"OK");
772 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
774 if (setjmp(remcomEnv
) == 0)
776 exceptionHandler(2,handle_buserror
);
778 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
779 ptr
= &remcomInBuffer
[1];
780 if (hexToInt(&ptr
,&addr
))
782 if (hexToInt(&ptr
,&length
))
785 mem2hex((char*) addr
, remcomOutBuffer
, length
);
790 strcpy(remcomOutBuffer
,"E01");
791 debug_error("malformed read memory command: %s",remcomInBuffer
);
795 exceptionHandler(2,_catchException
);
796 strcpy(remcomOutBuffer
,"E03");
797 debug_error("bus error");
800 /* restore handler for bus error */
801 exceptionHandler(2,_catchException
);
804 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
806 if (setjmp(remcomEnv
) == 0) {
807 exceptionHandler(2,handle_buserror
);
809 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
810 ptr
= &remcomInBuffer
[1];
811 if (hexToInt(&ptr
,&addr
))
813 if (hexToInt(&ptr
,&length
))
816 hex2mem(ptr
, (char*) addr
, length
);
818 strcpy(remcomOutBuffer
,"OK");
822 strcpy(remcomOutBuffer
,"E02");
823 debug_error("malformed write memory command: %s",remcomInBuffer
);
827 exceptionHandler(2,_catchException
);
828 strcpy(remcomOutBuffer
,"E03");
829 debug_error("bus error");
832 /* restore handler for bus error */
833 exceptionHandler(2,_catchException
);
836 /* cAA..AA Continue at address AA..AA(optional) */
837 /* sAA..AA Step one instruction from AA..AA(optional) */
840 /* try to read optional parameter, pc unchanged if no parm */
841 ptr
= &remcomInBuffer
[1];
842 if (hexToInt(&ptr
,&addr
))
843 registers
[ PC
] = addr
;
845 newPC
= registers
[ PC
];
847 /* clear the trace bit */
848 registers
[ PS
] &= 0x7fff;
850 /* set the trace bit if we're stepping */
851 if (remcomInBuffer
[0] == 's') registers
[ PS
] |= 0x8000;
854 * look for newPC in the linked list of exception frames.
855 * if it is found, use the old frame it. otherwise,
856 * fake up a dummy frame in returnFromException().
858 if (remote_debug
) printf("new pc = 0x%x\n",newPC
);
863 printf("frame at 0x%x has pc=0x%x, except#=%d\n",
864 frame
,frame
->exceptionPC
,
865 frame
->exceptionVector
);
866 if (frame
->exceptionPC
== newPC
) break; /* bingo! a match */
868 * for a breakpoint instruction, the saved pc may
869 * be off by two due to re-executing the instruction
870 * replaced by the trap instruction. Check for this.
872 if ((frame
->exceptionVector
== 33) &&
873 (frame
->exceptionPC
== (newPC
+2))) break;
874 if (frame
== frame
->previous
)
876 frame
= 0; /* no match found */
879 frame
= frame
->previous
;
883 * If we found a match for the PC AND we are not returning
884 * as a result of a breakpoint (33),
885 * trace exception (9), nmi (31), jmp to
886 * the old exception handler as if this code never ran.
890 if ((frame
->exceptionVector
!= 9) &&
891 (frame
->exceptionVector
!= 31) &&
892 (frame
->exceptionVector
!= 33))
895 * invoke the previous handler.
897 if (oldExceptionHook
)
898 (*oldExceptionHook
) (frame
->exceptionVector
);
899 newPC
= registers
[ PC
]; /* pc may have changed */
900 if (newPC
!= frame
->exceptionPC
)
903 printf("frame at 0x%x has pc=0x%x, except#=%d\n",
904 frame
,frame
->exceptionPC
,
905 frame
->exceptionVector
);
906 /* re-use the last frame, we're skipping it (longjump?)*/
908 _returnFromException( frame
); /* this is a jump */
913 /* if we couldn't find a frame, create one */
916 frame
= lastFrame
-1 ;
918 /* by using a bunch of print commands with breakpoints,
919 it's possible for the frame stack to creep down. If it creeps
920 too far, give up and reset it to the top. Normal use should
923 if ((unsigned int) (frame
-2) < (unsigned int) &gdbFrameStack
)
925 initializeRemcomErrorFrame();
928 frame
->previous
= lastFrame
;
930 frame
= 0; /* null so _return... will properly initialize it */
933 _returnFromException( frame
); /* this is a jump */
937 /* kill the program */
938 case 'k' : /* do nothing */
942 /* reply to the request */
943 putpacket(remcomOutBuffer
);
949 initializeRemcomErrorFrame()
951 lastFrame
= ((Frame
*) &gdbFrameStack
[FRAMESIZE
-1]) - 1;
952 lastFrame
->previous
= lastFrame
;
955 /* this function is used to set up exception handlers for tracing and
957 void set_debug_traps()
959 extern void _debug_level7();
960 extern void remcomHandler();
963 initializeRemcomErrorFrame();
964 stackPtr
= &remcomStack
[STACKSIZE
/sizeof(int) - 1];
966 for (exception
= 2; exception
<= 23; exception
++)
967 exceptionHandler(exception
,_catchException
);
969 /* level 7 interrupt */
970 exceptionHandler(31,_debug_level7
);
972 /* breakpoint exception (trap #1) */
973 exceptionHandler(33,_catchException
);
975 /* This is a trap #8 instruction. Apparently it is someone's software
976 convention for some sort of SIGFPE condition. Whose? How many
977 people are being screwed by having this code the way it is?
978 Is there a clean solution? */
979 exceptionHandler(40,_catchException
);
981 /* 48 to 54 are floating point coprocessor errors */
982 for (exception
= 48; exception
<= 54; exception
++)
983 exceptionHandler(exception
,_catchException
);
985 if (oldExceptionHook
!= remcomHandler
)
987 oldExceptionHook
= exceptionHook
;
988 exceptionHook
= remcomHandler
;
995 /* This function will generate a breakpoint exception. It is used at the
996 beginning of a program to sync up with a debugger and can be used
997 otherwise as a quick means to stop program execution and "break" into
1002 if (initialized
) BREAKPOINT();