source: rtems/c/src/lib/libbsp/i386/shared/comm/i386-stub.c @ 5b6438a

4.115
Last change on this file since 5b6438a was 5b6438a, checked in by Joel Sherrill <joel.sherrill@…>, on 10/12/14 at 20:48:12

libbsp/i386/shared/comm: Fix warnings

  • Property mode set to 100644
File size: 26.2 KB
Line 
1/*
2 *  This is the gdb i386 remote debug stub from gdb 4.XX.
3 */
4
5/****************************************************************************
6
7  THIS SOFTWARE IS NOT COPYRIGHTED
8
9  HP offers the following for use in the public domain.  HP makes no
10  warranty with regard to the software or it's performance and the
11  user accepts the software "AS IS" with all faults.
12
13  HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
14  TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16
17  ****************************************************************************/
18
19/****************************************************************************
20 *  Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
21 *
22 *  Module name: remcom.c $
23 *  Revision: 1.34 $
24 *  Date: 91/03/09 12:29:49 $
25 *  Contributor:     Lake Stevens Instrument Division$
26 *
27 *  Description:     low level support for gdb debugger. $
28 *
29 *  Considerations:  only works on target hardware $
30 *
31 *  Written by:      Glenn Engel $
32 *  ModuleState:     Experimental $
33 *
34 *  NOTES:           See Below $
35 *
36 *  Modified for 386 by Jim Kingdon, Cygnus Support.
37 *  Modified for RTEMS by Aleksey Romanov, Quality Quorum, Inc.
38 *
39 *  To enable debugger support, two things need to happen.  One, a
40 *  call to set_debug_traps() is necessary in order to allow any breakpoints
41 *  or error conditions to be properly intercepted and reported to gdb.
42 *  Two, a breakpoint needs to be generated to begin communication.  This
43 *  is most easily accomplished by a call to breakpoint().  Breakpoint()
44 *  simulates a breakpoint by executing a trap #1.
45 *
46 *  The external function exceptionHandler() is
47 *  used to attach a specific handler to a specific 386 vector number.
48 *  It should use the same privilege level it runs at.  It should
49 *  install it as an interrupt gate so that interrupts are masked
50 *  while the handler runs.
51 *  Also, need to assign exceptionHook and oldExceptionHook.
52 *
53 *  Because gdb will sometimes write to the stack area to execute function
54 *  calls, this program cannot rely on using the supervisor stack so it
55 *  uses it's own stack area reserved in the int array remcomStack.
56 *
57 *************
58 *
59 *    The following gdb commands are supported:
60 *
61 * command          function                               Return value
62 *
63 *    g             return the value of the CPU registers  hex data or ENN
64 *    G             set the value of the CPU registers     OK or ENN
65 *
66 *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
67 *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
68 *
69 *    c             Resume at current address              SNN   ( signal NN)
70 *    cAA..AA       Continue at address AA..AA             SNN
71 *
72 *    s             Step one instruction                   SNN
73 *    sAA..AA       Step one instruction from AA..AA       SNN
74 *
75 *    k             kill
76 *
77 *    ?             What was the last sigval ?             SNN   (signal NN)
78 *
79 * All commands and responses are sent with a packet which includes a
80 * checksum.  A packet consists of
81 *
82 * $<packet info>#<checksum>.
83 *
84 * where
85 * <packet info> :: <characters representing the command or response>
86 * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
87 *
88 * When a packet is received, it is first acknowledged with either '+' or '-'.
89 * '+' indicates a successful transfer.  '-' indicates a failed transfer.
90 *
91 * Example:
92 *
93 * Host:                  Reply:
94 * $m0,10#2a               +$00010203040506070809101112131415#42
95 *
96 ****************************************************************************/
97
98#include <stdio.h>
99#include <string.h>
100#include <stdbool.h>
101
102/************************************************************************
103 *
104 * external low-level support routines
105 */
106extern int putDebugChar (int ch);          /* write a single character      */
107extern int getDebugChar (void);            /* read and return a single char */
108
109/* assign an exception handler */
110extern void exceptionHandler (int, void (*handler) (void));
111
112/************************************************************************/
113/* BUFMAX defines the maximum number of characters in inbound/outbound buffers */
114/* at least NUMREGBYTES*2 are needed for register packets */
115#define BUFMAX 400
116
117static bool initialized = false;        /* boolean flag. != 0 means we've been initialized */
118
119extern int remote_debug;
120/*  debug >  0 prints ill-formed commands in valid packets & checksum errors */
121
122extern void waitabit (void);
123
124static const char hexchars[] = "0123456789abcdef";
125
126/* Number of registers.  */
127#define NUMREGS 16
128
129/* Number of bytes per register.  */
130#define REGBYTES 4
131
132/* Number of bytes of registers.  */
133#define NUMREGBYTES (NUMREGS * REGBYTES)
134
135enum regnames
136  {
137    EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
138    PC /* also known as eip */ ,
139    PS /* also known as eflags */ ,
140    CS, SS, DS, ES, FS, GS
141  };
142
143/*
144 * these should not be static cuz they can be used outside this module
145 */
146int registers[NUMREGS];
147
148#define STACKSIZE 10000
149int remcomStack[STACKSIZE / sizeof (int)];
150static int *stackPtr = &remcomStack[STACKSIZE / sizeof (int) - 1];
151
152/***************************  ASSEMBLY CODE MACROS *************************/
153/*                                                                         */
154
155extern void
156  return_to_prog (void);
157
158/* Restore the program's registers (including the stack pointer, which
159   means we get the right stack and don't have to worry about popping our
160   return address and any stack frames and so on) and return.  */
161__asm__ (".text");
162__asm__ (".globl return_to_prog");
163__asm__ ("return_to_prog:");
164__asm__ ("        movw registers+44, %ss");
165__asm__ ("        movl registers+16, %esp");
166__asm__ ("        movl registers+4, %ecx");
167__asm__ ("        movl registers+8, %edx");
168__asm__ ("        movl registers+12, %ebx");
169__asm__ ("        movl registers+20, %ebp");
170__asm__ ("        movl registers+24, %esi");
171__asm__ ("        movl registers+28, %edi");
172__asm__ ("        movw registers+48, %ds");
173__asm__ ("        movw registers+52, %es");
174__asm__ ("        movw registers+56, %fs");
175__asm__ ("        movw registers+60, %gs");
176__asm__ ("        movl registers+36, %eax");
177__asm__ ("        pushl %eax"); /* saved eflags */
178__asm__ ("        movl registers+40, %eax");
179__asm__ ("        pushl %eax"); /* saved cs */
180__asm__ ("        movl registers+32, %eax");
181__asm__ ("        pushl %eax"); /* saved eip */
182__asm__ ("        movl registers, %eax");
183/* use iret to restore pc and flags together so
184   that trace flag works right.  */
185__asm__ ("        iret");
186
187#define BREAKPOINT() __asm__ ("   int $3");
188
189/* Put the error code here just in case the user cares.  */
190int gdb_i386errcode;
191/* Likewise, the vector number here (since GDB only gets the signal
192   number through the usual means, and that's not very specific).  */
193int gdb_i386vector = -1;
194
195/* GDB stores segment registers in 32-bit words (that's just the way
196   m-i386v.h is written).  So zero the appropriate areas in registers.  */
197#define SAVE_REGISTERS1() \
198  __asm__ ("movl %eax, registers");                                       \
199  __asm__ ("movl %ecx, registers+4");                                        \
200  __asm__ ("movl %edx, registers+8");                                        \
201  __asm__ ("movl %ebx, registers+12");                                       \
202  __asm__ ("movl %ebp, registers+20");                                       \
203  __asm__ ("movl %esi, registers+24");                                       \
204  __asm__ ("movl %edi, registers+28");                                       \
205  __asm__ ("movw $0, %ax");                                                          \
206  __asm__ ("movw %ds, registers+48");                                        \
207  __asm__ ("movw %ax, registers+50");                                        \
208  __asm__ ("movw %es, registers+52");                                        \
209  __asm__ ("movw %ax, registers+54");                                        \
210  __asm__ ("movw %fs, registers+56");                                        \
211  __asm__ ("movw %ax, registers+58");                                        \
212  __asm__ ("movw %gs, registers+60");                                        \
213  __asm__ ("movw %ax, registers+62");
214#define SAVE_ERRCODE() \
215  __asm__ ("popl %ebx");                                  \
216  __asm__ ("movl %ebx, gdb_i386errcode");
217#define SAVE_REGISTERS2() \
218  __asm__ ("popl %ebx"); /* old eip */                                       \
219  __asm__ ("movl %ebx, registers+32");                                       \
220  __asm__ ("popl %ebx");         /* old cs */                                        \
221  __asm__ ("movl %ebx, registers+40");                                       \
222  __asm__ ("movw %ax, registers+42");                                           \
223  __asm__ ("popl %ebx");         /* old eflags */                                    \
224  __asm__ ("movl %ebx, registers+36");                                       \
225  /* Now that we've done the pops, we can save the stack pointer.");  */   \
226  __asm__ ("movw %ss, registers+44");                                        \
227  __asm__ ("movw %ax, registers+46");                                                \
228  __asm__ ("movl %esp, registers+16");
229
230/* See if mem_fault_routine is set, if so just IRET to that address.  */
231#define CHECK_FAULT() \
232  __asm__ ("cmpl $0, mem_fault_routine");                                          \
233  __asm__ ("jne mem_fault");
234
235__asm__ (".text");
236__asm__ ("mem_fault:");
237/* OK to clobber temp registers; we're just going to end up in set_mem_err.  */
238/* Pop error code from the stack and save it.  */
239__asm__ ("     popl %eax");
240__asm__ ("     movl %eax, gdb_i386errcode");
241
242__asm__ ("     popl %eax");             /* eip */
243/* We don't want to return there, we want to return to the function
244   pointed to by mem_fault_routine instead.  */
245__asm__ ("     movl mem_fault_routine, %eax");
246__asm__ ("     popl %ecx");             /* cs (low 16 bits; junk in hi 16 bits).  */
247__asm__ ("     popl %edx");             /* eflags */
248
249/* Remove this stack frame; when we do the iret, we will be going to
250   the start of a function, so we want the stack to look just like it
251   would after a "call" instruction.  */
252__asm__ ("     leave");
253
254/* Push the stuff that iret wants.  */
255__asm__ ("     pushl %edx");    /* eflags */
256__asm__ ("     pushl %ecx");    /* cs */
257__asm__ ("     pushl %eax");    /* eip */
258
259/* Zero mem_fault_routine.  */
260__asm__ ("     movl $0, %eax");
261__asm__ ("     movl %eax, mem_fault_routine");
262
263__asm__ ("iret");
264
265#define CALL_HOOK() __asm__ ("call _remcomHandler");
266
267/* This function is called when a i386 exception occurs.  It saves
268 * all the cpu regs in the registers array, munges the stack a bit,
269 * and invokes an exception handler (remcom_handler).
270 *
271 * stack on entry:                       stack on exit:
272 *   old eflags                          vector number
273 *   old cs (zero-filled to 32 bits)
274 *   old eip
275 *
276 */
277extern void _catchException3 (void);
278__asm__ (".text");
279__asm__ (".globl _catchException3");
280__asm__ ("_catchException3:");
281SAVE_REGISTERS1 ();
282SAVE_REGISTERS2 ();
283__asm__ ("pushl $3");
284CALL_HOOK ();
285
286/* Same thing for exception 1.  */
287extern void _catchException1 (void);
288__asm__ (".text");
289__asm__ (".globl _catchException1");
290__asm__ ("_catchException1:");
291SAVE_REGISTERS1 ();
292SAVE_REGISTERS2 ();
293__asm__ ("pushl $1");
294CALL_HOOK ();
295
296/* Same thing for exception 0.  */
297extern void _catchException0 (void);
298__asm__ (".text");
299__asm__ (".globl _catchException0");
300__asm__ ("_catchException0:");
301SAVE_REGISTERS1 ();
302SAVE_REGISTERS2 ();
303__asm__ ("pushl $0");
304CALL_HOOK ();
305
306/* Same thing for exception 4.  */
307extern void _catchException4 (void);
308__asm__ (".text");
309__asm__ (".globl _catchException4");
310__asm__ ("_catchException4:");
311SAVE_REGISTERS1 ();
312SAVE_REGISTERS2 ();
313__asm__ ("pushl $4");
314CALL_HOOK ();
315
316/* Same thing for exception 5.  */
317extern void _catchException5 (void);
318__asm__ (".text");
319__asm__ (".globl _catchException5");
320__asm__ ("_catchException5:");
321SAVE_REGISTERS1 ();
322SAVE_REGISTERS2 ();
323__asm__ ("pushl $5");
324CALL_HOOK ();
325
326/* Same thing for exception 6.  */
327extern void _catchException6 (void);
328__asm__ (".text");
329__asm__ (".globl _catchException6");
330__asm__ ("_catchException6:");
331SAVE_REGISTERS1 ();
332SAVE_REGISTERS2 ();
333__asm__ ("pushl $6");
334CALL_HOOK ();
335
336/* Same thing for exception 7.  */
337extern void _catchException7 (void);
338__asm__ (".text");
339__asm__ (".globl _catchException7");
340__asm__ ("_catchException7:");
341SAVE_REGISTERS1 ();
342SAVE_REGISTERS2 ();
343__asm__ ("pushl $7");
344CALL_HOOK ();
345
346/* Same thing for exception 8.  */
347extern void _catchException8 (void);
348__asm__ (".text");
349__asm__ (".globl _catchException8");
350__asm__ ("_catchException8:");
351SAVE_REGISTERS1 ();
352SAVE_ERRCODE ();
353SAVE_REGISTERS2 ();
354__asm__ ("pushl $8");
355CALL_HOOK ();
356
357/* Same thing for exception 9.  */
358extern void _catchException9 (void);
359__asm__ (".text");
360__asm__ (".globl _catchException9");
361__asm__ ("_catchException9:");
362SAVE_REGISTERS1 ();
363SAVE_REGISTERS2 ();
364__asm__ ("pushl $9");
365CALL_HOOK ();
366
367/* Same thing for exception 10.  */
368extern void _catchException10 (void);
369__asm__ (".text");
370__asm__ (".globl _catchException10");
371__asm__ ("_catchException10:");
372SAVE_REGISTERS1 ();
373SAVE_ERRCODE ();
374SAVE_REGISTERS2 ();
375__asm__ ("pushl $10");
376CALL_HOOK ();
377
378/* Same thing for exception 12.  */
379extern void _catchException12 (void);
380__asm__ (".text");
381__asm__ (".globl _catchException12");
382__asm__ ("_catchException12:");
383SAVE_REGISTERS1 ();
384SAVE_ERRCODE ();
385SAVE_REGISTERS2 ();
386__asm__ ("pushl $12");
387CALL_HOOK ();
388
389/* Same thing for exception 16.  */
390extern void _catchException16 (void);
391__asm__ (".text");
392__asm__ (".globl _catchException16");
393__asm__ ("_catchException16:");
394SAVE_REGISTERS1 ();
395SAVE_REGISTERS2 ();
396__asm__ ("pushl $16");
397CALL_HOOK ();
398
399/* For 13, 11, and 14 we have to deal with the CHECK_FAULT stuff.  */
400
401/* Same thing for exception 13.  */
402extern void _catchException13 (void);
403__asm__ (".text");
404__asm__ (".globl _catchException13");
405__asm__ ("_catchException13:");
406CHECK_FAULT ();
407SAVE_REGISTERS1 ();
408SAVE_ERRCODE ();
409SAVE_REGISTERS2 ();
410__asm__ ("pushl $13");
411CALL_HOOK ();
412
413/* Same thing for exception 11.  */
414extern void _catchException11 (void);
415__asm__ (".text");
416__asm__ (".globl _catchException11");
417__asm__ ("_catchException11:");
418CHECK_FAULT ();
419SAVE_REGISTERS1 ();
420SAVE_ERRCODE ();
421SAVE_REGISTERS2 ();
422__asm__ ("pushl $11");
423CALL_HOOK ();
424
425/* Same thing for exception 14.  */
426extern void _catchException14 (void);
427__asm__ (".text");
428__asm__ (".globl _catchException14");
429__asm__ ("_catchException14:");
430CHECK_FAULT ();
431SAVE_REGISTERS1 ();
432SAVE_ERRCODE ();
433SAVE_REGISTERS2 ();
434__asm__ ("pushl $14");
435CALL_HOOK ();
436
437/*
438 * remcomHandler is a front end for handle_exception.  It moves the
439 * stack pointer into an area reserved for debugger use.
440 */
441extern void remcomHandler (void);
442__asm__ ("_remcomHandler:");
443__asm__ ("           popl %eax");       /* pop off return address     */
444__asm__ ("           popl %eax");       /* get the exception number   */
445__asm__ ("              movl stackPtr, %esp");  /* move to remcom stack area  */
446__asm__ ("              pushl %eax");   /* push exception onto stack  */
447__asm__ ("              call  handle_exception");       /* this never returns */
448
449void
450_returnFromException (void)
451{
452  return_to_prog ();
453}
454
455static int
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>     */
468static void
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
532static void
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
600static void
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.  */
610static char *
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 */
634static char *
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 */
657static int
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/**********************************************/
718static int
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 *
746 * NOTE: This method is called from assembly code so must be marked
747 *       as used.
748 */
749static void handle_exception (int exceptionVector) __attribute__((used));
750static void
751handle_exception (int exceptionVector)
752{
753  int sigval;
754  int addr, length, reg;
755  char *ptr;
756  int newPC;
757
758  gdb_i386vector = exceptionVector;
759
760  if (remote_debug)
761    printf ("vector=%d, sr=0x%x, pc=0x%x\n",
762            exceptionVector,
763            registers[PS],
764            registers[PC]);
765
766  /* Reply to host that an exception has occurred.  Always return the
767     PC, SP, and FP, since gdb always wants them.  */
768  ptr = remcomOutBuffer;
769  *ptr++ = 'T';
770  sigval = computeSignal (exceptionVector);
771  *ptr++ = hexchars[sigval >> 4];
772  *ptr++ = hexchars[sigval % 16];
773
774  *ptr++ = hexchars[ESP];
775  *ptr++ = ':';
776  mem2hex ((char *) &registers[ESP], ptr, REGBYTES, 0);
777  ptr += REGBYTES * 2;
778  *ptr++ = ';';
779
780  *ptr++ = hexchars[EBP];
781  *ptr++ = ':';
782  mem2hex ((char *) &registers[EBP], ptr, REGBYTES, 0);
783  ptr += REGBYTES * 2;
784  *ptr++ = ';';
785
786  *ptr++ = hexchars[PC];
787  *ptr++ = ':';
788  mem2hex ((char *) &registers[PC], ptr, REGBYTES, 0);
789  ptr += REGBYTES * 2;
790  *ptr++ = ';';
791
792  *ptr = '\0';
793
794  putpacket (remcomOutBuffer);
795
796  while (1 == 1)
797    {
798      error = 0;
799      remcomOutBuffer[0] = 0;
800      getpacket (remcomInBuffer);
801      switch (remcomInBuffer[0])
802        {
803        case '?':
804          remcomOutBuffer[0] = 'S';
805          remcomOutBuffer[1] = hexchars[sigval >> 4];
806          remcomOutBuffer[2] = hexchars[sigval % 16];
807          remcomOutBuffer[3] = 0;
808          break;
809        case 'd':
810          remote_debug = !(remote_debug);       /* toggle debug flag */
811          break;
812        case 'g':               /* return the value of the CPU registers */
813          mem2hex ((char *) registers, remcomOutBuffer, NUMREGBYTES, 0);
814          break;
815        case 'G':               /* set the value of the CPU registers - return OK */
816          hex2mem (&remcomInBuffer[1], (char *) registers, NUMREGBYTES, 0);
817          strcpy (remcomOutBuffer, "OK");
818          break;
819
820        case 'P':               /* Set specific register */
821          ptr = &remcomInBuffer[1];
822          if (hexToInt (&ptr, &reg)
823              && *ptr++ == '=')
824            {
825              hex2mem (ptr, (char *) &registers[reg], REGBYTES, 0);
826              strcpy (remcomOutBuffer, "OK");
827            }
828          else
829            {
830              strcpy (remcomOutBuffer, "E01");
831              debug_error ("malformed register set command; %s",
832                           remcomInBuffer);
833            }
834          break;
835
836          /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
837        case 'm':
838          /* TRY TO READ %x,%x.  IF SUCCEED, SET PTR = 0 */
839          ptr = &remcomInBuffer[1];
840          if (hexToInt (&ptr, &addr))
841            if (*(ptr++) == ',')
842              if (hexToInt (&ptr, &length))
843                {
844                  ptr = 0;
845                  mem_err = 0;
846                  mem2hex ((char *) addr, remcomOutBuffer, length, 1);
847                  if (mem_err)
848                    {
849                      strcpy (remcomOutBuffer, "E03");
850                      debug_error ("memory fault", 0);
851                    }
852                }
853
854          if (ptr)
855            {
856              strcpy (remcomOutBuffer, "E01");
857              debug_error ("malformed read memory command: %s", remcomInBuffer);
858            }
859          break;
860
861          /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
862        case 'M':
863          /* TRY TO READ '%x,%x:'.  IF SUCCEED, SET PTR = 0 */
864          ptr = &remcomInBuffer[1];
865          if (hexToInt (&ptr, &addr))
866            if (*(ptr++) == ',')
867              if (hexToInt (&ptr, &length))
868                if (*(ptr++) == ':')
869                  {
870                    mem_err = 0;
871                    hex2mem (ptr, (char *) addr, length, 1);
872
873                    if (mem_err)
874                      {
875                        strcpy (remcomOutBuffer, "E03");
876                        debug_error ("memory fault", 0);
877                      }
878                    else
879                      {
880                        strcpy (remcomOutBuffer, "OK");
881                      }
882
883                    ptr = 0;
884                  }
885          if (ptr)
886            {
887              strcpy (remcomOutBuffer, "E02");
888              debug_error ("malformed write memory command: %s", remcomInBuffer);
889            }
890          break;
891
892          /* cAA..AA    Continue at address AA..AA(optional) */
893          /* sAA..AA   Step one instruction from AA..AA(optional) */
894        case 'c':
895        case 's':
896          /* try to read optional parameter, pc unchanged if no parm */
897          ptr = &remcomInBuffer[1];
898          if (hexToInt (&ptr, &addr))
899            registers[PC] = addr;
900
901          newPC = registers[PC];
902
903          /* clear the trace bit */
904          registers[PS] &= 0xfffffeff;
905
906          /* set the trace bit if we're stepping */
907          if (remcomInBuffer[0] == 's')
908            registers[PS] |= 0x100;
909
910          _returnFromException ();      /* this is a jump */
911
912          break;
913
914          /* Detach.  */
915        case 'D':
916          putpacket (remcomOutBuffer);
917          registers[PS] &= 0xfffffeff;
918          _returnFromException ();      /* this is a jump */
919
920          break;
921
922          /* kill the program */
923        case 'k':               /* do nothing */
924          break;
925        }                       /* switch */
926
927      /* reply to the request */
928      putpacket (remcomOutBuffer);
929    }
930}
931
932/* this function is used to set up exception handlers for tracing and
933   breakpoints */
934void
935set_debug_traps (void)
936{
937  stackPtr = &remcomStack[STACKSIZE / sizeof (int) - 1];
938
939  exceptionHandler (0, _catchException0);
940  exceptionHandler (1, _catchException1);
941  exceptionHandler (3, _catchException3);
942  exceptionHandler (4, _catchException4);
943  exceptionHandler (5, _catchException5);
944  exceptionHandler (6, _catchException6);
945  exceptionHandler (7, _catchException7);
946  exceptionHandler (8, _catchException8);
947  exceptionHandler (9, _catchException9);
948  exceptionHandler (10, _catchException10);
949  exceptionHandler (11, _catchException11);
950  exceptionHandler (12, _catchException12);
951  exceptionHandler (13, _catchException13);
952  exceptionHandler (14, _catchException14);
953  exceptionHandler (16, _catchException16);
954
955  /* In case GDB is started before us, ack any packets (presumably
956     "$?#xx") sitting there.  */
957  putDebugChar ('+');
958
959  initialized = true;
960
961}
962
963/* This function will generate a breakpoint exception.  It is used at the
964   beginning of a program to sync up with a debugger and can be used
965   otherwise as a quick means to stop program execution and "break" into
966   the debugger. */
967
968void
969breakpoint (void)
970{
971  if (initialized)
972    {
973      BREAKPOINT ();
974    }
975  waitabit ();
976}
977
978int waitlimit = 1000000;
979
980void
981waitabit (void)
982{
983  int i;
984  for (i = 0; i < waitlimit; i++);
985}
Note: See TracBrowser for help on using the repository browser.