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 void putDebugChar(); /* write a single character */
120 extern int 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
;
198 /* the size of the exception stack on the 68020 varies with the type of
199 * exception. The following table is the number of WORDS used
200 * for each exception format.
202 const short exceptionSize
[] = { 4,4,6,4,4,4,4,4,29,10,16,46,12,4,4,4 };
206 static const short exceptionSize
[] = { 4,4,6,4,4,4,4,4,4,4,4,4,16,4,4,4 };
209 /************* jump buffer used for setjmp/longjmp **************************/
212 /*************************** ASSEMBLY CODE MACROS *************************/
215 #ifdef __HAVE_68881__
216 /* do an fsave, then remember the address to begin a restore from */
217 #define SAVE_FP_REGS() asm(" fsave a0@-"); \
218 asm(" fmovemx fp0-fp7,_registers+72"); \
219 asm(" fmoveml fpcr/fpsr/fpi,_registers+168");
220 #define RESTORE_FP_REGS() \
222 fmoveml _registers+168,fpcr/fpsr/fpi \n\
223 fmovemx _registers+72,fp0-fp7 \n\
224 cmpl #-1,a0@ | skip frestore flag set ? \n\
225 beq skip_frestore \n\
231 #define SAVE_FP_REGS()
232 #define RESTORE_FP_REGS()
233 #endif /* __HAVE_68881__ */
235 void return_to_super();
236 void return_to_user();
240 .globl _return_to_super
242 movel _registers+60,sp /* get new stack pointer */
243 movel _lastFrame,a0 /* get last frame info */
246 .globl _return_to_user
248 movel _registers+60,a0 /* get usp */
249 movel a0,usp /* set usp */
250 movel _superStack,sp /* get original stack pointer */
253 movel _lastFrame,a0 /* get last frame info */
254 movel a0@+,_lastFrame /* link in previous frame */
255 addql #8,a0 /* skip over pc, vector#*/
256 movew a0@+,d0 /* get # of words in cpu frame */
257 addw d0,a0 /* point to end of data */
258 addw d0,a0 /* point to end of data */
261 # copy the stack frame
268 asm(" moveml _registers,d0-d7/a0-a6");
269 asm(" rte"); /* pop and go! */
271 #define DISABLE_INTERRUPTS() asm(" oriw #0x0700,sr");
272 #define BREAKPOINT() asm(" trap #1");
274 /* this function is called immediately when a level 7 interrupt occurs */
275 /* if the previous interrupt level was 7 then we're already servicing */
276 /* this interrupt and an rte is in order to return to the debugger. */
277 /* For the 68000, the offset for sr is 6 due to the jsr return address */
280 .globl __debug_level7
283 #if defined (mc68020) || defined (mc68332)
284 asm(" movew sp@(2),d0");
286 asm(" movew sp@(6),d0");
288 asm(" andiw #0x700,d0
295 #if !defined (mc68020) && !defined (mc68332)
296 asm(" lea sp@(4),sp"); /* pull off 68000 return address */
300 extern void _catchException ();
302 #if defined (mc68020) || defined (mc68332)
303 /* This function is called when a 68020 exception occurs. It saves
304 * all the cpu and fpcp regs in the _registers array, creates a frame on a
305 * linked list of frames which has the cpu and fpcp stack frames needed
306 * to properly restore the context of these processors, and invokes
307 * an exception handler (remcom_handler).
309 * stack on entry: stack on exit:
310 * N bytes of junk exception # MSWord
311 * Exception Format Word exception # MSWord
312 * Program counter LSWord
313 * Program counter MSWord
320 .globl __catchException
322 DISABLE_INTERRUPTS();
324 moveml d0-d7/a0-a6,_registers /* save registers */
325 movel _lastFrame,a0 /* last frame pointer */
329 lea _registers,a5 /* get address of registers */
330 movew sp@,d1 /* get status register */
331 movew d1,a5@(66) /* save sr */
332 movel sp@(2),a4 /* save pc in a4 for later use */
333 movel a4,a5@(68) /* save pc in _regisers[] */
336 # figure out how many bytes in the stack frame
337 movew sp@(6),d0 /* get '020 exception format */
338 movew d0,d2 /* make a copy of format word */
339 andiw #0xf000,d0 /* mask off format type */
340 rolw #5,d0 /* rotate into the low byte *2 */
341 lea _exceptionSize,a1
342 addw d0,a1 /* index into the table */
343 movew a1@,d0 /* get number of words in frame */
344 movew d0,d3 /* save it */
345 subw d0,a0 /* adjust save pointer */
346 subw d0,a0 /* adjust save pointer(bytes) */
347 movel a0,a1 /* copy save pointer */
348 subql #1,d0 /* predecrement loop counter */
355 # now that the stack has been clenaed,
356 # save the a7 in use at time of exception
357 movel sp,_superStack /* save supervisor sp */
358 andiw #0x2000,d1 /* were we in supervisor mode ? */
360 movel a7,a5@(60) /* save a7 */
364 movel a1,a5@(60) /* save user stack pointer */
372 # compute exception number
373 andl #0xfff,d2 /* mask off vector offset */
374 lsrw #2,d2 /* divide by 4 to get vect num */
375 movel d2,a0@- /* save it */
377 # save pc causing exception
380 # save old frame link and set the new value
381 movel _lastFrame,a1 /* last frame pointer */
382 movel a1,a0@- /* save pointer to prev frame */
385 movel d2,sp@- /* push exception num */
386 movel _exceptionHook,a0 /* get address of handler */
387 jbsr a0@ /* and call it */
388 clrl sp@ /* replace exception num parm with frame ptr */
389 jbsr __returnFromException /* jbsr, but never returns */
392 /* This function is called when an exception occurs. It translates the
393 * return address found on the stack into an exception vector # which
394 * is then handled by either handle_exception or a system handler.
395 * _catchException provides a front end for both.
397 * stack on entry: stack on exit:
398 * Program counter MSWord exception # MSWord
399 * Program counter LSWord exception # MSWord
401 * Return Address MSWord
402 * Return Address LSWord
406 .globl __catchException
408 DISABLE_INTERRUPTS();
410 moveml d0-d7/a0-a6,_registers /* save registers */
411 movel _lastFrame,a0 /* last frame pointer */
415 lea _registers,a5 /* get address of registers */
416 movel sp@+,d2 /* pop return address */
417 addl #1530,d2 /* convert return addr to */
418 divs #6,d2 /* exception number */
421 moveql #3,d3 /* assume a three word frame */
423 cmpiw #3,d2 /* bus error or address error ? */
424 bgt normal /* if >3 then normal error */
425 movel sp@+,a0@- /* copy error info to frame buff*/
426 movel sp@+,a0@- /* these are never used */
427 moveql #7,d3 /* this is a 7 word frame */
430 movew sp@+,d1 /* pop status register */
431 movel sp@+,a4 /* pop program counter */
432 movew d1,a5@(66) /* save sr */
433 movel a4,a5@(68) /* save pc in _regisers[] */
434 movel a4,a0@- /* copy pc to frame buffer */
435 movew d1,a0@- /* copy sr to frame buffer */
437 movel sp,_superStack /* save supervisor sp */
439 andiw #0x2000,d1 /* were we in supervisor mode ? */
441 movel a7,a5@(60) /* save a7 */
444 movel usp,a1 /* save user stack pointer */
445 movel a1,a5@(60) /* save user stack pointer */
448 movew d3,a0@- /* push frame size in words */
449 movel d2,a0@- /* push vector number */
450 movel a4,a0@- /* push exception pc */
453 # save old frame link and set the new value
454 movel _lastFrame,a1 /* last frame pointer */
455 movel a1,a0@- /* save pointer to prev frame */
458 movel d2,sp@- /* push exception num */
459 movel _exceptionHook,a0 /* get address of handler */
460 jbsr a0@ /* and call it */
461 clrl sp@ /* replace exception num parm with frame ptr */
462 jbsr __returnFromException /* jbsr, but never returns */
468 * remcomHandler is a front end for handle_exception. It moves the
469 * stack pointer into an area reserved for debugger use in case the
470 * breakpoint happened in supervisor mode.
472 asm("_remcomHandler:");
473 asm(" addl #4,sp"); /* pop off return address */
474 asm(" movel sp@+,d0"); /* get the exception number */
475 asm(" movel _stackPtr,sp"); /* move to remcom stack area */
476 asm(" movel d0,sp@-"); /* push exception onto stack */
477 asm(" jbsr _handle_exception"); /* this never returns */
478 asm(" rts"); /* return */
480 void _returnFromException( Frame
*frame
)
482 /* if no passed in frame, use the last one */
486 frame
->frameSize
= 4;
488 frame
->fsaveHeader
= -1; /* restore regs, but we dont have fsave info*/
491 #if !defined (mc68020) && !defined (mc68332)
492 /* a 68000 cannot use the internal info pushed onto a bus error
493 * or address error frame when doing an RTE so don't put this info
494 * onto the stack or the stack will creep every time this happens.
499 /* throw away any frames in the list after this frame */
502 frame
->sr
= registers
[(int) PS
];
503 frame
->pc
= registers
[(int) PC
];
505 if (registers
[(int) PS
] & 0x2000)
507 /* return to supervisor mode... */
511 { /* return to user mode */
519 if ((ch
>= 'a') && (ch
<= 'f')) return (ch
-'a'+10);
520 if ((ch
>= '0') && (ch
<= '9')) return (ch
-'0');
521 if ((ch
>= 'A') && (ch
<= 'F')) return (ch
-'A'+10);
526 /* scan for the sequence $<data>#<checksum> */
527 void getpacket(buffer
)
530 unsigned char checksum
;
531 unsigned char xmitcsum
;
537 /* wait around for the start character, ignore all other characters */
538 while ((ch
= (getDebugChar() & 0x7f)) != '$');
544 /* now, read until a # or end of buffer is found */
545 while (count
< BUFMAX
) {
546 ch
= getDebugChar() & 0x7f;
547 if (ch
== '#') break;
548 checksum
= checksum
+ ch
;
555 xmitcsum
= hex(getDebugChar() & 0x7f) << 4;
556 xmitcsum
+= hex(getDebugChar() & 0x7f);
557 if ((remote_debug
) && (checksum
!= xmitcsum
)) {
558 fprintf (stderr
,"bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n",
559 checksum
,xmitcsum
,buffer
);
562 if (checksum
!= xmitcsum
) putDebugChar('-'); /* failed checksum */
564 putDebugChar('+'); /* successful transfer */
565 /* if a sequence char is present, reply the sequence ID */
566 if (buffer
[2] == ':') {
567 putDebugChar( buffer
[0] );
568 putDebugChar( buffer
[1] );
569 /* remove sequence chars from buffer */
570 count
= strlen(buffer
);
571 for (i
=3; i
<= count
; i
++) buffer
[i
-3] = buffer
[i
];
575 } while (checksum
!= xmitcsum
);
579 /* send the packet in buffer. */
582 void putpacket(buffer
)
585 unsigned char checksum
;
589 /* $<packet info>#<checksum>. */
595 while (ch
=buffer
[count
]) {
602 putDebugChar(hexchars
[checksum
>> 4]);
603 putDebugChar(hexchars
[checksum
% 16]);
605 } while (getDebugChar() != '+');
609 char remcomInBuffer
[BUFMAX
];
610 char remcomOutBuffer
[BUFMAX
];
614 void debug_error(format
, parm
)
618 if (remote_debug
) fprintf (stderr
,format
,parm
);
621 /* convert the memory pointed to by mem into hex, placing result in buf */
622 /* return a pointer to the last char put in buf (null) */
623 char* mem2hex(mem
, buf
, count
)
630 for (i
=0;i
<count
;i
++) {
632 *buf
++ = hexchars
[ch
>> 4];
633 *buf
++ = hexchars
[ch
% 16];
639 /* convert the hex array pointed to by buf into binary to be placed in mem */
640 /* return a pointer to the character AFTER the last byte written */
641 char* hex2mem(buf
, mem
, count
)
648 for (i
=0;i
<count
;i
++) {
649 ch
= hex(*buf
++) << 4;
650 ch
= ch
+ hex(*buf
++);
656 /* a bus error has occurred, perform a longjmp
657 to return execution and allow handling of the error */
659 void handle_buserror()
661 longjmp(remcomEnv
,1);
664 /* this function takes the 68000 exception number and attempts to
665 translate this number into a unix compatible signal value */
666 int computeSignal( exceptionVector
)
670 switch (exceptionVector
) {
671 case 2 : sigval
= 10; break; /* bus error */
672 case 3 : sigval
= 10; break; /* address error */
673 case 4 : sigval
= 4; break; /* illegal instruction */
674 case 5 : sigval
= 8; break; /* zero divide */
675 case 6 : sigval
= 8; break; /* chk instruction */
676 case 7 : sigval
= 8; break; /* trapv instruction */
677 case 8 : sigval
= 11; break; /* privilege violation */
678 case 9 : sigval
= 5; break; /* trace trap */
679 case 10: sigval
= 4; break; /* line 1010 emulator */
680 case 11: sigval
= 4; break; /* line 1111 emulator */
682 /* Coprocessor protocol violation. Using a standard MMU or FPU
683 this cannot be triggered by software. Call it a SIGBUS. */
684 case 13: sigval
= 10; break;
686 case 31: sigval
= 2; break; /* interrupt */
687 case 33: sigval
= 5; break; /* breakpoint */
689 /* This is a trap #8 instruction. Apparently it is someone's software
690 convention for some sort of SIGFPE condition. Whose? How many
691 people are being screwed by having this code the way it is?
692 Is there a clean solution? */
693 case 40: sigval
= 8; break; /* floating point err */
695 case 48: sigval
= 8; break; /* floating point err */
696 case 49: sigval
= 8; break; /* floating point err */
697 case 50: sigval
= 8; break; /* zero divide */
698 case 51: sigval
= 8; break; /* underflow */
699 case 52: sigval
= 8; break; /* operand error */
700 case 53: sigval
= 8; break; /* overflow */
701 case 54: sigval
= 8; break; /* NAN */
703 sigval
= 7; /* "software generated"*/
708 /**********************************************/
709 /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
710 /* RETURN NUMBER OF CHARS PROCESSED */
711 /**********************************************/
712 int hexToInt(char **ptr
, int *intValue
)
721 hexValue
= hex(**ptr
);
724 *intValue
= (*intValue
<<4) | hexValue
;
737 * This function does all command procesing for interfacing to gdb.
739 void handle_exception(int exceptionVector
)
747 if (remote_debug
) printf("vector=%d, sr=0x%x, pc=0x%x\n",
752 /* reply to host that an exception has occurred */
753 sigval
= computeSignal( exceptionVector
);
754 remcomOutBuffer
[0] = 'S';
755 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
756 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
757 remcomOutBuffer
[3] = 0;
759 putpacket(remcomOutBuffer
);
763 remcomOutBuffer
[0] = 0;
764 getpacket(remcomInBuffer
);
765 switch (remcomInBuffer
[0]) {
766 case '?' : remcomOutBuffer
[0] = 'S';
767 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
768 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
769 remcomOutBuffer
[3] = 0;
771 case 'd' : remote_debug
= !(remote_debug
); /* toggle debug flag */
773 case 'g' : /* return the value of the CPU registers */
774 mem2hex((char*) registers
, remcomOutBuffer
, NUMREGBYTES
);
776 case 'G' : /* set the value of the CPU registers - return OK */
777 hex2mem(&remcomInBuffer
[1], (char*) registers
, NUMREGBYTES
);
778 strcpy(remcomOutBuffer
,"OK");
781 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
783 if (setjmp(remcomEnv
) == 0)
785 exceptionHandler(2,handle_buserror
);
787 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
788 ptr
= &remcomInBuffer
[1];
789 if (hexToInt(&ptr
,&addr
))
791 if (hexToInt(&ptr
,&length
))
794 mem2hex((char*) addr
, remcomOutBuffer
, length
);
799 strcpy(remcomOutBuffer
,"E01");
800 debug_error("malformed read memory command: %s",remcomInBuffer
);
804 exceptionHandler(2,_catchException
);
805 strcpy(remcomOutBuffer
,"E03");
806 debug_error("bus error");
809 /* restore handler for bus error */
810 exceptionHandler(2,_catchException
);
813 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
815 if (setjmp(remcomEnv
) == 0) {
816 exceptionHandler(2,handle_buserror
);
818 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
819 ptr
= &remcomInBuffer
[1];
820 if (hexToInt(&ptr
,&addr
))
822 if (hexToInt(&ptr
,&length
))
825 hex2mem(ptr
, (char*) addr
, length
);
827 strcpy(remcomOutBuffer
,"OK");
831 strcpy(remcomOutBuffer
,"E02");
832 debug_error("malformed write memory command: %s",remcomInBuffer
);
836 exceptionHandler(2,_catchException
);
837 strcpy(remcomOutBuffer
,"E03");
838 debug_error("bus error");
841 /* restore handler for bus error */
842 exceptionHandler(2,_catchException
);
845 /* cAA..AA Continue at address AA..AA(optional) */
846 /* sAA..AA Step one instruction from AA..AA(optional) */
849 /* try to read optional parameter, pc unchanged if no parm */
850 ptr
= &remcomInBuffer
[1];
851 if (hexToInt(&ptr
,&addr
))
852 registers
[ PC
] = addr
;
854 newPC
= registers
[ PC
];
856 /* clear the trace bit */
857 registers
[ PS
] &= 0x7fff;
859 /* set the trace bit if we're stepping */
860 if (remcomInBuffer
[0] == 's') registers
[ PS
] |= 0x8000;
863 * look for newPC in the linked list of exception frames.
864 * if it is found, use the old frame it. otherwise,
865 * fake up a dummy frame in returnFromException().
867 if (remote_debug
) printf("new pc = 0x%x\n",newPC
);
872 printf("frame at 0x%x has pc=0x%x, except#=%d\n",
873 frame
,frame
->exceptionPC
,
874 frame
->exceptionVector
);
875 if (frame
->exceptionPC
== newPC
) break; /* bingo! a match */
877 * for a breakpoint instruction, the saved pc may
878 * be off by two due to re-executing the instruction
879 * replaced by the trap instruction. Check for this.
881 if ((frame
->exceptionVector
== 33) &&
882 (frame
->exceptionPC
== (newPC
+2))) break;
883 if (frame
== frame
->previous
)
885 frame
= 0; /* no match found */
888 frame
= frame
->previous
;
892 * If we found a match for the PC AND we are not returning
893 * as a result of a breakpoint (33),
894 * trace exception (9), nmi (31), jmp to
895 * the old exception handler as if this code never ran.
899 if ((frame
->exceptionVector
!= 9) &&
900 (frame
->exceptionVector
!= 31) &&
901 (frame
->exceptionVector
!= 33))
904 * invoke the previous handler.
906 if (oldExceptionHook
)
907 (*oldExceptionHook
) (frame
->exceptionVector
);
908 newPC
= registers
[ PC
]; /* pc may have changed */
909 if (newPC
!= frame
->exceptionPC
)
912 printf("frame at 0x%x has pc=0x%x, except#=%d\n",
913 frame
,frame
->exceptionPC
,
914 frame
->exceptionVector
);
915 /* re-use the last frame, we're skipping it (longjump?)*/
917 _returnFromException( frame
); /* this is a jump */
922 /* if we couldn't find a frame, create one */
925 frame
= lastFrame
-1 ;
927 /* by using a bunch of print commands with breakpoints,
928 it's possible for the frame stack to creep down. If it creeps
929 too far, give up and reset it to the top. Normal use should
932 if ((unsigned int) (frame
-2) < (unsigned int) &gdbFrameStack
)
934 initializeRemcomErrorFrame();
937 frame
->previous
= lastFrame
;
939 frame
= 0; /* null so _return... will properly initialize it */
942 _returnFromException( frame
); /* this is a jump */
946 /* kill the program */
947 case 'k' : /* do nothing */
951 /* reply to the request */
952 putpacket(remcomOutBuffer
);
958 initializeRemcomErrorFrame()
960 lastFrame
= ((Frame
*) &gdbFrameStack
[FRAMESIZE
-1]) - 1;
961 lastFrame
->previous
= lastFrame
;
964 /* this function is used to set up exception handlers for tracing and
966 void set_debug_traps()
968 extern void _debug_level7();
969 extern void remcomHandler();
972 initializeRemcomErrorFrame();
973 stackPtr
= &remcomStack
[STACKSIZE
/sizeof(int) - 1];
975 for (exception
= 2; exception
<= 23; exception
++)
976 exceptionHandler(exception
,_catchException
);
978 /* level 7 interrupt */
979 exceptionHandler(31,_debug_level7
);
981 /* breakpoint exception (trap #1) */
982 exceptionHandler(33,_catchException
);
984 /* This is a trap #8 instruction. Apparently it is someone's software
985 convention for some sort of SIGFPE condition. Whose? How many
986 people are being screwed by having this code the way it is?
987 Is there a clean solution? */
988 exceptionHandler(40,_catchException
);
990 /* 48 to 54 are floating point coprocessor errors */
991 for (exception
= 48; exception
<= 54; exception
++)
992 exceptionHandler(exception
,_catchException
);
994 if (oldExceptionHook
!= remcomHandler
)
996 oldExceptionHook
= exceptionHook
;
997 exceptionHook
= remcomHandler
;
1004 /* This function will generate a breakpoint exception. It is used at the
1005 beginning of a program to sync up with a debugger and can be used
1006 otherwise as a quick means to stop program execution and "break" into
1011 if (initialized
) BREAKPOINT();