source: rtems/c/src/lib/libbsp/i386/shared/comm/i386-stub.c @ 290268f

4.104.114.84.95
Last change on this file since 290268f was 290268f, checked in by Joel Sherrill <joel.sherrill@…>, on 05/23/05 at 21:56:38

2005-05-23 Joel Sherrill <joel@…>

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