* command.c (shell_escape, make_command, _initialze_command):
[deliverable/binutils-gdb.git] / gdb / i386-stub.c
CommitLineData
740b7efa
SG
1/****************************************************************************
2
3 THIS SOFTWARE IS NOT COPYRIGHTED
4
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.
8
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.
12
13****************************************************************************/
14
15/****************************************************************************
16 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
17 *
18 * Module name: remcom.c $
19 * Revision: 1.34 $
20 * Date: 91/03/09 12:29:49 $
21 * Contributor: Lake Stevens Instrument Division$
22 *
23 * Description: low level support for gdb debugger. $
24 *
25 * Considerations: only works on target hardware $
26 *
27 * Written by: Glenn Engel $
28 * ModuleState: Experimental $
29 *
30 * NOTES: See Below $
31 *
32 * Modified for 386 by Jim Kingdon, Cygnus Support.
33 *
34 * To enable debugger support, two things need to happen. One, a
35 * call to set_debug_traps() is necessary in order to allow any breakpoints
36 * or error conditions to be properly intercepted and reported to gdb.
37 * Two, a breakpoint needs to be generated to begin communication. This
38 * is most easily accomplished by a call to breakpoint(). Breakpoint()
39 * simulates a breakpoint by executing a trap #1.
40 *
41 * The external function exceptionHandler() is
42 * used to attach a specific handler to a specific 386 vector number.
43 * It should use the same privilege level it runs at. It should
44 * install it as an interrupt gate so that interrupts are masked
45 * while the handler runs.
46 * Also, need to assign exceptionHook and oldExceptionHook.
47 *
48 * Because gdb will sometimes write to the stack area to execute function
49 * calls, this program cannot rely on using the supervisor stack so it
50 * uses it's own stack area reserved in the int array remcomStack.
51 *
52 *************
53 *
54 * The following gdb commands are supported:
55 *
56 * command function Return value
57 *
58 * g return the value of the CPU registers hex data or ENN
59 * G set the value of the CPU registers OK or ENN
60 *
61 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
62 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
63 *
64 * c Resume at current address SNN ( signal NN)
65 * cAA..AA Continue at address AA..AA SNN
66 *
67 * s Step one instruction SNN
68 * sAA..AA Step one instruction from AA..AA SNN
69 *
70 * k kill
71 *
72 * ? What was the last sigval ? SNN (signal NN)
73 *
74 * All commands and responses are sent with a packet which includes a
75 * checksum. A packet consists of
76 *
77 * $<packet info>#<checksum>.
78 *
79 * where
80 * <packet info> :: <characters representing the command or response>
81 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
82 *
83 * When a packet is received, it is first acknowledged with either '+' or '-'.
84 * '+' indicates a successful transfer. '-' indicates a failed transfer.
85 *
86 * Example:
87 *
88 * Host: Reply:
89 * $m0,10#2a +$00010203040506070809101112131415#42
90 *
91 ****************************************************************************/
92
93#include <stdio.h>
94#include <string.h>
95#include "ansidecl.h"
96
97/************************************************************************
98 *
99 * external low-level support routines
100 */
101typedef void (*ExceptionHook)(int); /* pointer to function with int parm */
102typedef void (*Function)(); /* pointer to a function */
103
104extern putDebugChar(); /* write a single character */
105extern getDebugChar(); /* read and return a single char */
106
107extern Function exceptionHandler(); /* assign an exception handler */
108extern ExceptionHook exceptionHook; /* hook variable for errors/exceptions */
109
110/************************************************************************/
111/* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
112/* at least NUMREGBYTES*2 are needed for register packets */
113#define BUFMAX 400
114
115static char initialized; /* boolean flag. != 0 means we've been initialized */
116
117int remote_debug;
118/* debug > 0 prints ill-formed commands in valid packets & checksum errors */
119
120void waitabit();
121
122static const char hexchars[]="0123456789abcdef";
123
124/* Number of bytes of registers. */
125#define NUMREGBYTES 64
126enum regnames {EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
127 PC /* also known as eip */,
128 PS /* also known as eflags */,
129 CS, SS, DS, ES, FS, GS};
130
131/*
132 * these should not be static cuz they can be used outside this module
133 */
134int registers[NUMREGBYTES/4];
135
136#define STACKSIZE 10000
137int remcomStack[STACKSIZE/sizeof(int)];
138static int* stackPtr = &remcomStack[STACKSIZE/sizeof(int) - 1];
139
140/*
141 * In many cases, the system will want to continue exception processing
142 * when a continue command is given.
143 * oldExceptionHook is a function to invoke in this case.
144 */
145
146static ExceptionHook oldExceptionHook;
147
148/*************************** ASSEMBLY CODE MACROS *************************/
149/* */
150
151extern void
152return_to_prog PARAMS ((void));
153
154/* Restore the program's registers (including the stack pointer, which
155 means we get the right stack and don't have to worry about popping our
156 return address and any stack frames and so on) and return. */
157asm(".text");
158asm(".globl _return_to_prog");
159asm("_return_to_prog:");
160asm(" movw _registers+44, %ss");
161asm(" movl _registers+16, %esp");
162asm(" movl _registers+4, %ecx");
163asm(" movl _registers+8, %edx");
164asm(" movl _registers+12, %ebx");
165asm(" movl _registers+20, %ebp");
166asm(" movl _registers+24, %esi");
167asm(" movl _registers+28, %edi");
168asm(" movw _registers+48, %ds");
169asm(" movw _registers+52, %es");
170asm(" movw _registers+56, %fs");
171asm(" movw _registers+60, %gs");
172asm(" movl _registers+36, %eax");
173asm(" pushl %eax"); /* saved eflags */
174asm(" movl _registers+40, %eax");
175asm(" pushl %eax"); /* saved cs */
176asm(" movl _registers+32, %eax");
177asm(" pushl %eax"); /* saved eip */
178asm(" movl _registers, %eax");
179/* use iret to restore pc and flags together so
180 that trace flag works right. */
181asm(" iret");
182
183#define BREAKPOINT() asm(" int $3");
184
185/* Put the error code here just in case the user cares. */
186int gdb_i386errcode;
187/* Likewise, the vector number here (since GDB only gets the signal
188 number through the usual means, and that's not very specific). */
189int gdb_i386vector = -1;
190
191/* GDB stores segment registers in 32-bit words (that's just the way
192 m-i386v.h is written). So zero the appropriate areas in registers. */
193#define SAVE_REGISTERS1() \
194 asm ("movl %eax, _registers"); \
195 asm ("movl %ecx, _registers+4"); \
196 asm ("movl %edx, _registers+8"); \
197 asm ("movl %ebx, _registers+12"); \
198 asm ("movl %ebp, _registers+20"); \
199 asm ("movl %esi, _registers+24"); \
200 asm ("movl %edi, _registers+28"); \
201 asm ("movw $0, %ax"); \
202 asm ("movw %ds, _registers+48"); \
203 asm ("movw %ax, _registers+50"); \
204 asm ("movw %es, _registers+52"); \
205 asm ("movw %ax, _registers+54"); \
206 asm ("movw %fs, _registers+56"); \
207 asm ("movw %ax, _registers+58"); \
208 asm ("movw %gs, _registers+60"); \
209 asm ("movw %ax, _registers+62");
210#define SAVE_ERRCODE() \
211 asm ("popl %ebx"); \
212 asm ("movl %ebx, _gdb_i386errcode");
213#define SAVE_REGISTERS2() \
214 asm ("popl %ebx"); /* old eip */ \
215 asm ("movl %ebx, _registers+32"); \
216 asm ("popl %ebx"); /* old cs */ \
217 asm ("movl %ebx, _registers+40"); \
218 asm ("movw %ax, _registers+42"); \
219 asm ("popl %ebx"); /* old eflags */ \
220 asm ("movl %ebx, _registers+36"); \
221 /* Now that we've done the pops, we can save the stack pointer."); */ \
222 asm ("movw %ss, _registers+44"); \
223 asm ("movw %ax, _registers+46"); \
224 asm ("movl %esp, _registers+16");
225
226/* See if mem_fault_routine is set, if so just IRET to that address. */
227#define CHECK_FAULT() \
228 asm ("cmpl $0, _mem_fault_routine"); \
229 asm ("jne mem_fault");
230
231asm ("mem_fault:");
232/* OK to clobber temp registers; we're just going to end up in set_mem_err. */
233/* Pop error code from the stack and save it. */
234asm (" popl %eax");
235asm (" movl %eax, _gdb_i386errcode");
236
237asm (" popl %eax"); /* eip */
238/* We don't want to return there, we want to return to the function
239 pointed to by mem_fault_routine instead. */
240asm (" movl _mem_fault_routine, %eax");
241asm (" popl %ecx"); /* cs (low 16 bits; junk in hi 16 bits). */
242asm (" popl %edx"); /* eflags */
243
244/* Remove this stack frame; when we do the iret, we will be going to
245 the start of a function, so we want the stack to look just like it
246 would after a "call" instruction. */
247asm (" leave");
248
249/* Push the stuff that iret wants. */
250asm (" pushl %edx"); /* eflags */
251asm (" pushl %ecx"); /* cs */
252asm (" pushl %eax"); /* eip */
253
254/* Zero mem_fault_routine. */
255asm (" movl $0, %eax");
256asm (" movl %eax, _mem_fault_routine");
257
258asm ("iret");
259
260#define CALL_HOOK() asm("call _remcomHandler");
261
262/* This function is called when a i386 exception occurs. It saves
263 * all the cpu regs in the _registers array, munges the stack a bit,
264 * and invokes an exception handler (remcom_handler).
265 *
266 * stack on entry: stack on exit:
267 * old eflags vector number
268 * old cs (zero-filled to 32 bits)
269 * old eip
270 *
271 */
272extern void _catchException3();
273asm(".text");
274asm(".globl __catchException3");
275asm("__catchException3:");
276SAVE_REGISTERS1();
277SAVE_REGISTERS2();
278asm ("pushl $3");
279CALL_HOOK();
280
281/* Same thing for exception 1. */
282extern void _catchException1();
283asm(".text");
284asm(".globl __catchException1");
285asm("__catchException1:");
286SAVE_REGISTERS1();
287SAVE_REGISTERS2();
288asm ("pushl $1");
289CALL_HOOK();
290
291/* Same thing for exception 0. */
292extern void _catchException0();
293asm(".text");
294asm(".globl __catchException0");
295asm("__catchException0:");
296SAVE_REGISTERS1();
297SAVE_REGISTERS2();
298asm ("pushl $0");
299CALL_HOOK();
300
301/* Same thing for exception 4. */
302extern void _catchException4();
303asm(".text");
304asm(".globl __catchException4");
305asm("__catchException4:");
306SAVE_REGISTERS1();
307SAVE_REGISTERS2();
308asm ("pushl $4");
309CALL_HOOK();
310
311/* Same thing for exception 5. */
312extern void _catchException5();
313asm(".text");
314asm(".globl __catchException5");
315asm("__catchException5:");
316SAVE_REGISTERS1();
317SAVE_REGISTERS2();
318asm ("pushl $5");
319CALL_HOOK();
320
321/* Same thing for exception 6. */
322extern void _catchException6();
323asm(".text");
324asm(".globl __catchException6");
325asm("__catchException6:");
326SAVE_REGISTERS1();
327SAVE_REGISTERS2();
328asm ("pushl $6");
329CALL_HOOK();
330
331/* Same thing for exception 7. */
332extern void _catchException7();
333asm(".text");
334asm(".globl __catchException7");
335asm("__catchException7:");
336SAVE_REGISTERS1();
337SAVE_REGISTERS2();
338asm ("pushl $7");
339CALL_HOOK();
340
341/* Same thing for exception 8. */
342extern void _catchException8();
343asm(".text");
344asm(".globl __catchException8");
345asm("__catchException8:");
346SAVE_REGISTERS1();
347SAVE_ERRCODE();
348SAVE_REGISTERS2();
349asm ("pushl $8");
350CALL_HOOK();
351
352/* Same thing for exception 9. */
353extern void _catchException9();
354asm(".text");
355asm(".globl __catchException9");
356asm("__catchException9:");
357SAVE_REGISTERS1();
358SAVE_REGISTERS2();
359asm ("pushl $9");
360CALL_HOOK();
361
362/* Same thing for exception 10. */
363extern void _catchException10();
364asm(".text");
365asm(".globl __catchException10");
366asm("__catchException10:");
367SAVE_REGISTERS1();
368SAVE_ERRCODE();
369SAVE_REGISTERS2();
370asm ("pushl $10");
371CALL_HOOK();
372
373/* Same thing for exception 12. */
374extern void _catchException12();
375asm(".text");
376asm(".globl __catchException12");
377asm("__catchException12:");
378SAVE_REGISTERS1();
379SAVE_ERRCODE();
380SAVE_REGISTERS2();
381asm ("pushl $12");
382CALL_HOOK();
383
384/* Same thing for exception 16. */
385extern void _catchException16();
386asm(".text");
387asm(".globl __catchException16");
388asm("__catchException16:");
389SAVE_REGISTERS1();
390SAVE_REGISTERS2();
391asm ("pushl $16");
392CALL_HOOK();
393
394/* For 13, 11, and 14 we have to deal with the CHECK_FAULT stuff. */
395
396/* Same thing for exception 13. */
397extern void _catchException13 ();
398asm (".text");
399asm (".globl __catchException13");
400asm ("__catchException13:");
401CHECK_FAULT();
402SAVE_REGISTERS1();
403SAVE_ERRCODE();
404SAVE_REGISTERS2();
405asm ("pushl $13");
406CALL_HOOK();
407
408/* Same thing for exception 11. */
409extern void _catchException11 ();
410asm (".text");
411asm (".globl __catchException11");
412asm ("__catchException11:");
413CHECK_FAULT();
414SAVE_REGISTERS1();
415SAVE_ERRCODE();
416SAVE_REGISTERS2();
417asm ("pushl $11");
418CALL_HOOK();
419
420/* Same thing for exception 14. */
421extern void _catchException14 ();
422asm (".text");
423asm (".globl __catchException14");
424asm ("__catchException14:");
425CHECK_FAULT();
426SAVE_REGISTERS1();
427SAVE_ERRCODE();
428SAVE_REGISTERS2();
429asm ("pushl $14");
430CALL_HOOK();
431
432/*
433 * remcomHandler is a front end for handle_exception. It moves the
434 * stack pointer into an area reserved for debugger use.
435 */
436asm("_remcomHandler:");
437asm(" popl %eax"); /* pop off return address */
438asm(" popl %eax"); /* get the exception number */
439asm(" movl _stackPtr, %esp"); /* move to remcom stack area */
440asm(" pushl %eax"); /* push exception onto stack */
441asm(" call _handle_exception"); /* this never returns */
442
443void _returnFromException()
444{
445 return_to_prog ();
446}
447
448int hex(ch)
449char ch;
450{
451 if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10);
452 if ((ch >= '0') && (ch <= '9')) return (ch-'0');
453 if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10);
454 return (-1);
455}
456
457
458/* scan for the sequence $<data>#<checksum> */
459void getpacket(buffer)
460char * buffer;
461{
462 unsigned char checksum;
463 unsigned char xmitcsum;
464 int i;
465 int count;
466 char ch;
467
468 do {
469 /* wait around for the start character, ignore all other characters */
470 while ((ch = getDebugChar()) != '$');
471 checksum = 0;
472 xmitcsum = -1;
473
474 count = 0;
475
476 /* now, read until a # or end of buffer is found */
477 while (count < BUFMAX) {
478 ch = getDebugChar();
479 if (ch == '#') break;
480 checksum = checksum + ch;
481 buffer[count] = ch;
482 count = count + 1;
483 }
484 buffer[count] = 0;
485
486 if (ch == '#') {
487 xmitcsum = hex(getDebugChar()) << 4;
488 xmitcsum += hex(getDebugChar());
489 if ((remote_debug ) && (checksum != xmitcsum)) {
490 fprintf(stderr,"bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n",
491 checksum,xmitcsum,buffer);
492 }
493
494 if (checksum != xmitcsum) putDebugChar('-'); /* failed checksum */
495 else {
496 putDebugChar('+'); /* successful transfer */
497 /* if a sequence char is present, reply the sequence ID */
498 if (buffer[2] == ':') {
499 putDebugChar( buffer[0] );
500 putDebugChar( buffer[1] );
501 /* remove sequence chars from buffer */
502 count = strlen(buffer);
503 for (i=3; i <= count; i++) buffer[i-3] = buffer[i];
504 }
505 }
506 }
507 } while (checksum != xmitcsum);
508
509}
510
511/* send the packet in buffer. */
512
513
514void putpacket(buffer)
515char * buffer;
516{
517 unsigned char checksum;
518 int count;
519 char ch;
520
521 /* $<packet info>#<checksum>. */
522 do {
523 putDebugChar('$');
524 checksum = 0;
525 count = 0;
526
527 while (ch=buffer[count]) {
528 if (! putDebugChar(ch)) return;
529 checksum += ch;
530 count += 1;
531 }
532
533 putDebugChar('#');
534 putDebugChar(hexchars[checksum >> 4]);
535 putDebugChar(hexchars[checksum % 16]);
536
537 } while (getDebugChar() != '+');
538
539}
540
541char remcomInBuffer[BUFMAX];
542char remcomOutBuffer[BUFMAX];
543static short error;
544
545
546void debug_error(format, parm)
547char * format;
548char * parm;
549{
550 if (remote_debug) fprintf(stderr,format,parm);
551}
552
553/* Address of a routine to RTE to if we get a memory fault. */
554static NORETURN void (*mem_fault_routine)() = NULL;
555
556/* Indicate to caller of mem2hex or hex2mem that there has been an
557 error. */
558static volatile int mem_err = 0;
559
560void
561set_mem_err ()
562{
563 mem_err = 1;
564}
565
566/* These are separate functions so that they are so short and sweet
567 that the compiler won't save any registers (if there is a fault
568 to mem_fault, they won't get restored, so there better not be any
569 saved). */
570int
571get_char (addr)
572 char *addr;
573{
574 return *addr;
575}
576
577void
578set_char (addr, val)
579 char *addr;
580 int val;
581{
582 *addr = val;
583}
584
585/* convert the memory pointed to by mem into hex, placing result in buf */
586/* return a pointer to the last char put in buf (null) */
587/* If MAY_FAULT is non-zero, then we should set mem_err in response to
588 a fault; if zero treat a fault like any other fault in the stub. */
589char* mem2hex(mem, buf, count, may_fault)
590char* mem;
591char* buf;
592int count;
593int may_fault;
594{
595 int i;
596 unsigned char ch;
597
598 if (may_fault)
599 mem_fault_routine = set_mem_err;
600 for (i=0;i<count;i++) {
601 ch = get_char (mem++);
602 if (may_fault && mem_err)
603 return (buf);
604 *buf++ = hexchars[ch >> 4];
605 *buf++ = hexchars[ch % 16];
606 }
607 *buf = 0;
608 if (may_fault)
609 mem_fault_routine = NULL;
610 return(buf);
611}
612
613/* convert the hex array pointed to by buf into binary to be placed in mem */
614/* return a pointer to the character AFTER the last byte written */
615char* hex2mem(buf, mem, count, may_fault)
616char* buf;
617char* mem;
618int count;
619int may_fault;
620{
621 int i;
622 unsigned char ch;
623
624 if (may_fault)
625 mem_fault_routine = set_mem_err;
626 for (i=0;i<count;i++) {
627 ch = hex(*buf++) << 4;
628 ch = ch + hex(*buf++);
629 set_char (mem++, ch);
630 if (may_fault && mem_err)
631 return (mem);
632 }
633 if (may_fault)
634 mem_fault_routine = NULL;
635 return(mem);
636}
637
638/* this function takes the 386 exception vector and attempts to
639 translate this number into a unix compatible signal value */
640int computeSignal( exceptionVector )
641int exceptionVector;
642{
643 int sigval;
644 switch (exceptionVector) {
645 case 0 : sigval = 8; break; /* divide by zero */
646 case 1 : sigval = 5; break; /* debug exception */
647 case 3 : sigval = 5; break; /* breakpoint */
648 case 4 : sigval = 16; break; /* into instruction (overflow) */
649 case 5 : sigval = 16; break; /* bound instruction */
650 case 6 : sigval = 4; break; /* Invalid opcode */
651 case 7 : sigval = 8; break; /* coprocessor not available */
652 case 8 : sigval = 7; break; /* double fault */
653 case 9 : sigval = 11; break; /* coprocessor segment overrun */
654 case 10 : sigval = 11; break; /* Invalid TSS */
655 case 11 : sigval = 11; break; /* Segment not present */
656 case 12 : sigval = 11; break; /* stack exception */
657 case 13 : sigval = 11; break; /* general protection */
658 case 14 : sigval = 11; break; /* page fault */
659 case 16 : sigval = 7; break; /* coprocessor error */
660 default:
661 sigval = 7; /* "software generated"*/
662 }
663 return (sigval);
664}
665
666/**********************************************/
667/* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
668/* RETURN NUMBER OF CHARS PROCESSED */
669/**********************************************/
670int hexToInt(char **ptr, int *intValue)
671{
672 int numChars = 0;
673 int hexValue;
674
675 *intValue = 0;
676
677 while (**ptr)
678 {
679 hexValue = hex(**ptr);
680 if (hexValue >=0)
681 {
682 *intValue = (*intValue <<4) | hexValue;
683 numChars ++;
684 }
685 else
686 break;
687
688 (*ptr)++;
689 }
690
691 return (numChars);
692}
693
694/*
695 * This function does all command procesing for interfacing to gdb.
696 */
697void handle_exception(int exceptionVector)
698{
699 int sigval;
700 int addr, length;
701 char * ptr;
702 int newPC;
703
704 gdb_i386vector = exceptionVector;
705
706 if (remote_debug) printf("vector=%d, sr=0x%x, pc=0x%x\n",
707 exceptionVector,
708 registers[ PS ],
709 registers[ PC ]);
710
711 /* reply to host that an exception has occurred */
712 sigval = computeSignal( exceptionVector );
713 remcomOutBuffer[0] = 'S';
714 remcomOutBuffer[1] = hexchars[sigval >> 4];
715 remcomOutBuffer[2] = hexchars[sigval % 16];
716 remcomOutBuffer[3] = 0;
717
718 putpacket(remcomOutBuffer);
719
720 while (1==1) {
721 error = 0;
722 remcomOutBuffer[0] = 0;
723 getpacket(remcomInBuffer);
724 switch (remcomInBuffer[0]) {
725 case '?' : remcomOutBuffer[0] = 'S';
726 remcomOutBuffer[1] = hexchars[sigval >> 4];
727 remcomOutBuffer[2] = hexchars[sigval % 16];
728 remcomOutBuffer[3] = 0;
729 break;
730 case 'd' : remote_debug = !(remote_debug); /* toggle debug flag */
731 break;
732 case 'g' : /* return the value of the CPU registers */
733 mem2hex((char*) registers, remcomOutBuffer, NUMREGBYTES, 0);
734 break;
735 case 'G' : /* set the value of the CPU registers - return OK */
736 hex2mem(&remcomInBuffer[1], (char*) registers, NUMREGBYTES, 0);
737 strcpy(remcomOutBuffer,"OK");
738 break;
739
740 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
741 case 'm' :
742 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
743 ptr = &remcomInBuffer[1];
744 if (hexToInt(&ptr,&addr))
745 if (*(ptr++) == ',')
746 if (hexToInt(&ptr,&length))
747 {
748 ptr = 0;
749 mem_err = 0;
750 mem2hex((char*) addr, remcomOutBuffer, length, 1);
751 if (mem_err) {
752 strcpy (remcomOutBuffer, "E03");
753 debug_error ("memory fault");
754 }
755 }
756
757 if (ptr)
758 {
759 strcpy(remcomOutBuffer,"E01");
760 debug_error("malformed read memory command: %s",remcomInBuffer);
761 }
762 break;
763
764 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
765 case 'M' :
766 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
767 ptr = &remcomInBuffer[1];
768 if (hexToInt(&ptr,&addr))
769 if (*(ptr++) == ',')
770 if (hexToInt(&ptr,&length))
771 if (*(ptr++) == ':')
772 {
773 mem_err = 0;
774 hex2mem(ptr, (char*) addr, length, 1);
775
776 if (mem_err) {
777 strcpy (remcomOutBuffer, "E03");
778 debug_error ("memory fault");
779 } else {
780 strcpy(remcomOutBuffer,"OK");
781 }
782
783 ptr = 0;
784 }
785 if (ptr)
786 {
787 strcpy(remcomOutBuffer,"E02");
788 debug_error("malformed write memory command: %s",remcomInBuffer);
789 }
790 break;
791
792 /* cAA..AA Continue at address AA..AA(optional) */
793 /* sAA..AA Step one instruction from AA..AA(optional) */
794 case 'c' :
795 case 's' :
796 /* try to read optional parameter, pc unchanged if no parm */
797 ptr = &remcomInBuffer[1];
798 if (hexToInt(&ptr,&addr))
799 registers[ PC ] = addr;
800
801 newPC = registers[ PC];
802
803 /* clear the trace bit */
804 registers[ PS ] &= 0xfffffeff;
805
806 /* set the trace bit if we're stepping */
807 if (remcomInBuffer[0] == 's') registers[ PS ] |= 0x100;
808
809 /*
810 * If we found a match for the PC AND we are not returning
811 * as a result of a breakpoint (33),
812 * trace exception (9), nmi (31), jmp to
813 * the old exception handler as if this code never ran.
814 */
815#if 0
816 /* Don't really think we need this, except maybe for protection
817 exceptions. */
818 /*
819 * invoke the previous handler.
820 */
821 if (oldExceptionHook)
822 (*oldExceptionHook) (frame->exceptionVector);
823 newPC = registers[ PC ]; /* pc may have changed */
824#endif /* 0 */
825
826 _returnFromException(); /* this is a jump */
827
828 break;
829
830 /* kill the program */
831 case 'k' : /* do nothing */
832 BREAKPOINT();
833 break;
834 } /* switch */
835
836 /* reply to the request */
837 putpacket(remcomOutBuffer);
838 }
839}
840
841/* this function is used to set up exception handlers for tracing and
842 breakpoints */
843void set_debug_traps()
844{
845extern void remcomHandler();
846int exception;
847
848 stackPtr = &remcomStack[STACKSIZE/sizeof(int) - 1];
849
850 exceptionHandler (0, _catchException0);
851 exceptionHandler (1, _catchException1);
852 exceptionHandler (3, _catchException3);
853 exceptionHandler (4, _catchException4);
854 exceptionHandler (5, _catchException5);
855 exceptionHandler (6, _catchException6);
856 exceptionHandler (7, _catchException7);
857 exceptionHandler (8, _catchException8);
858 exceptionHandler (9, _catchException9);
859 exceptionHandler (10, _catchException10);
860 exceptionHandler (11, _catchException11);
861 exceptionHandler (12, _catchException12);
862 exceptionHandler (13, _catchException13);
863 exceptionHandler (14, _catchException14);
864 exceptionHandler (16, _catchException16);
865
866 if (exceptionHook != remcomHandler)
867 {
868 oldExceptionHook = exceptionHook;
869 exceptionHook = remcomHandler;
870 }
871
872 /* In case GDB is started before us, ack any packets (presumably
873 "$?#xx") sitting there. */
874 putDebugChar ('+');
875
876 initialized = 1;
877
878}
879
880/* This function will generate a breakpoint exception. It is used at the
881 beginning of a program to sync up with a debugger and can be used
882 otherwise as a quick means to stop program execution and "break" into
883 the debugger. */
884
885void breakpoint()
886{
887 if (initialized)
888#if 0
889 handle_exception(3);
890#else
891 BREAKPOINT();
892#endif
893 waitabit();
894}
895
896int waitlimit = 1000000;
897
898#if 0
899void
900bogon()
901{
902 waitabit();
903}
904#endif
905
906void
907waitabit()
908{
909 int i;
910 for (i = 0; i < waitlimit; i++) ;
911}
This page took 0.061186 seconds and 4 git commands to generate.