gdb/stubs/i386-stub.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /****************************************************************************

  2.                 THIS SOFTWARE IS NOT COPYRIGHTED

  3.    HP offers the following for use in the public domain.  HP makes no
  4.    warranty with regard to the software or it's performance and the
  5.    user accepts the software "AS IS" with all faults.

  6.    HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
  7.    TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  8.    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

  9. ****************************************************************************/

  10. /****************************************************************************
  11. *  Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
  12. *
  13. *  Module name: remcom.c $
  14. *  Revision: 1.34 $
  15. *  Date: 91/03/09 12:29:49 $
  16. *  Contributor:     Lake Stevens Instrument Division$
  17. *
  18. *  Description:     low level support for gdb debugger. $
  19. *
  20. *  Considerations:  only works on target hardware $
  21. *
  22. *  Written by:      Glenn Engel $
  23. *  ModuleState:     Experimental $
  24. *
  25. *  NOTES:           See Below $
  26. *
  27. *  Modified for 386 by Jim Kingdon, Cygnus Support.
  28. *
  29. *  To enable debugger support, two things need to happen.  One, a
  30. *  call to set_debug_traps() is necessary in order to allow any breakpoints
  31. *  or error conditions to be properly intercepted and reported to gdb.
  32. Two, a breakpoint needs to be generated to begin communication.  This
  33. *  is most easily accomplished by a call to breakpoint().  Breakpoint()
  34. *  simulates a breakpoint by executing a trap #1.
  35. *
  36. *  The external function exceptionHandler() is
  37. *  used to attach a specific handler to a specific 386 vector number.
  38. *  It should use the same privilege level it runs at.  It should
  39. *  install it as an interrupt gate so that interrupts are masked
  40. *  while the handler runs.
  41. *
  42. *  Because gdb will sometimes write to the stack area to execute function
  43. *  calls, this program cannot rely on using the supervisor stack so it
  44. *  uses it's own stack area reserved in the int array remcomStack.
  45. *
  46. *************
  47. *
  48. *    The following gdb commands are supported:
  49. *
  50. * command          function                               Return value
  51. *
  52. *    g             return the value of the CPU registers  hex data or ENN
  53. *    G             set the value of the CPU registers     OK or ENN
  54. *
  55. *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
  56. *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
  57. *
  58. *    c             Resume at current address              SNN   ( signal NN)
  59. *    cAA..AA       Continue at address AA..AA             SNN
  60. *
  61. *    s             Step one instruction                   SNN
  62. *    sAA..AA       Step one instruction from AA..AA       SNN
  63. *
  64. *    k             kill
  65. *
  66. *    ?             What was the last sigval ?             SNN   (signal NN)
  67. *
  68. * All commands and responses are sent with a packet which includes a
  69. * checksum.  A packet consists of
  70. *
  71. * $<packet info>#<checksum>.
  72. *
  73. * where
  74. * <packet info> :: <characters representing the command or response>
  75. * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
  76. *
  77. * When a packet is received, it is first acknowledged with either '+' or '-'.
  78. * '+' indicates a successful transfer.  '-' indicates a failed transfer.
  79. *
  80. * Example:
  81. *
  82. * Host:                  Reply:
  83. * $m0,10#2a               +$00010203040506070809101112131415#42
  84. *
  85. ****************************************************************************/

  86. #include <stdio.h>
  87. #include <string.h>

  88. /************************************************************************
  89. *
  90. * external low-level support routines
  91. */

  92. extern void putDebugChar();        /* write a single character      */
  93. extern int getDebugChar();        /* read and return a single char */
  94. extern void exceptionHandler();        /* assign an exception handler   */

  95. /************************************************************************/
  96. /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
  97. /* at least NUMREGBYTES*2 are needed for register packets */
  98. #define BUFMAX 400

  99. static char initialized/* boolean flag. != 0 means we've been initialized */

  100. int     remote_debug;
  101. /*  debug >  0 prints ill-formed commands in valid packets & checksum errors */

  102. static const char hexchars[]="0123456789abcdef";

  103. /* Number of registers.  */
  104. #define NUMREGS        16

  105. /* Number of bytes of registers.  */
  106. #define NUMREGBYTES (NUMREGS * 4)

  107. enum regnames {EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
  108.                PC /* also known as eip */,
  109.                PS /* also known as eflags */,
  110.                CS, SS, DS, ES, FS, GS};

  111. /*
  112. * these should not be static cuz they can be used outside this module
  113. */
  114. int registers[NUMREGS];

  115. #define STACKSIZE 10000
  116. int remcomStack[STACKSIZE/sizeof(int)];
  117. static int* stackPtr = &remcomStack[STACKSIZE/sizeof(int) - 1];

  118. /***************************  ASSEMBLY CODE MACROS *************************/
  119. /*                                                                            */

  120. extern void
  121. return_to_prog ();

  122. /* Restore the program's registers (including the stack pointer, which
  123.    means we get the right stack and don't have to worry about popping our
  124.    return address and any stack frames and so on) and return.  */
  125. asm(".text");
  126. asm(".globl _return_to_prog");
  127. asm("_return_to_prog:");
  128. asm("        movw _registers+44, %ss");
  129. asm("        movl _registers+16, %esp");
  130. asm("        movl _registers+4, %ecx");
  131. asm("        movl _registers+8, %edx");
  132. asm("        movl _registers+12, %ebx");
  133. asm("        movl _registers+20, %ebp");
  134. asm("        movl _registers+24, %esi");
  135. asm("        movl _registers+28, %edi");
  136. asm("        movw _registers+48, %ds");
  137. asm("        movw _registers+52, %es");
  138. asm("        movw _registers+56, %fs");
  139. asm("        movw _registers+60, %gs");
  140. asm("        movl _registers+36, %eax");
  141. asm("        pushl %eax");  /* saved eflags */
  142. asm("        movl _registers+40, %eax");
  143. asm("        pushl %eax");  /* saved cs */
  144. asm("        movl _registers+32, %eax");
  145. asm("        pushl %eax");  /* saved eip */
  146. asm("        movl _registers, %eax");
  147. /* use iret to restore pc and flags together so
  148.    that trace flag works right.  */
  149. asm("        iret");

  150. #define BREAKPOINT() asm("   int $3");

  151. /* Put the error code here just in case the user cares.  */
  152. int gdb_i386errcode;
  153. /* Likewise, the vector number here (since GDB only gets the signal
  154.    number through the usual means, and that's not very specific).  */
  155. int gdb_i386vector = -1;

  156. /* GDB stores segment registers in 32-bit words (that's just the way
  157.    m-i386v.h is written).  So zero the appropriate areas in registers.  */
  158. #define SAVE_REGISTERS1() \
  159.   asm ("movl %eax, _registers");                                             \
  160.   asm ("movl %ecx, _registers+4");                                               \
  161.   asm ("movl %edx, _registers+8");                                               \
  162.   asm ("movl %ebx, _registers+12");                                               \
  163.   asm ("movl %ebp, _registers+20");                                               \
  164.   asm ("movl %esi, _registers+24");                                               \
  165.   asm ("movl %edi, _registers+28");                                               \
  166.   asm ("movw $0, %ax");                                                             \
  167.   asm ("movw %ds, _registers+48");                                               \
  168.   asm ("movw %ax, _registers+50");                                             \
  169.   asm ("movw %es, _registers+52");                                               \
  170.   asm ("movw %ax, _registers+54");                                             \
  171.   asm ("movw %fs, _registers+56");                                               \
  172.   asm ("movw %ax, _registers+58");                                             \
  173.   asm ("movw %gs, _registers+60");                                               \
  174.   asm ("movw %ax, _registers+62");
  175. #define SAVE_ERRCODE() \
  176.   asm ("popl %ebx");                                  \
  177.   asm ("movl %ebx, _gdb_i386errcode");
  178. #define SAVE_REGISTERS2() \
  179.   asm ("popl %ebx"); /* old eip */                                               \
  180.   asm ("movl %ebx, _registers+32");                                               \
  181.   asm ("popl %ebx");         /* old cs */                                               \
  182.   asm ("movl %ebx, _registers+40");                                               \
  183.   asm ("movw %ax, _registers+42");                                           \
  184.   asm ("popl %ebx");         /* old eflags */                                       \
  185.   asm ("movl %ebx, _registers+36");                                              \
  186.   /* Now that we've done the pops, we can save the stack pointer.");  */   \
  187.   asm ("movw %ss, _registers+44");                                             \
  188.   asm ("movw %ax, _registers+46");                                                                              \
  189.   asm ("movl %esp, _registers+16");

  190. /* See if mem_fault_routine is set, if so just IRET to that address.  */
  191. #define CHECK_FAULT() \
  192.   asm ("cmpl $0, _mem_fault_routine");                                           \
  193.   asm ("jne mem_fault");

  194. asm (".text");
  195. asm ("mem_fault:");
  196. /* OK to clobber temp registers; we're just going to end up in set_mem_err.  */
  197. /* Pop error code from the stack and save it.  */
  198. asm ("     popl %eax");
  199. asm ("     movl %eax, _gdb_i386errcode");

  200. asm ("     popl %eax"); /* eip */
  201. /* We don't want to return there, we want to return to the function
  202.    pointed to by mem_fault_routine instead.  */
  203. asm ("     movl _mem_fault_routine, %eax");
  204. asm ("     popl %ecx"); /* cs (low 16 bits; junk in hi 16 bits).  */
  205. asm ("     popl %edx"); /* eflags */

  206. /* Remove this stack frame; when we do the iret, we will be going to
  207.    the start of a function, so we want the stack to look just like it
  208.    would after a "call" instruction.  */
  209. asm ("     leave");

  210. /* Push the stuff that iret wants.  */
  211. asm ("     pushl %edx"); /* eflags */
  212. asm ("     pushl %ecx"); /* cs */
  213. asm ("     pushl %eax"); /* eip */

  214. /* Zero mem_fault_routine.  */
  215. asm ("     movl $0, %eax");
  216. asm ("     movl %eax, _mem_fault_routine");

  217. asm ("iret");

  218. #define CALL_HOOK() asm("call _remcomHandler");

  219. /* This function is called when a i386 exception occurs.  It saves
  220. * all the cpu regs in the _registers array, munges the stack a bit,
  221. * and invokes an exception handler (remcom_handler).
  222. *
  223. * stack on entry:                       stack on exit:
  224. *   old eflags                          vector number
  225. *   old cs (zero-filled to 32 bits)
  226. *   old eip
  227. *
  228. */
  229. extern void _catchException3();
  230. asm(".text");
  231. asm(".globl __catchException3");
  232. asm("__catchException3:");
  233. SAVE_REGISTERS1();
  234. SAVE_REGISTERS2();
  235. asm ("pushl $3");
  236. CALL_HOOK();

  237. /* Same thing for exception 1.  */
  238. extern void _catchException1();
  239. asm(".text");
  240. asm(".globl __catchException1");
  241. asm("__catchException1:");
  242. SAVE_REGISTERS1();
  243. SAVE_REGISTERS2();
  244. asm ("pushl $1");
  245. CALL_HOOK();

  246. /* Same thing for exception 0.  */
  247. extern void _catchException0();
  248. asm(".text");
  249. asm(".globl __catchException0");
  250. asm("__catchException0:");
  251. SAVE_REGISTERS1();
  252. SAVE_REGISTERS2();
  253. asm ("pushl $0");
  254. CALL_HOOK();

  255. /* Same thing for exception 4.  */
  256. extern void _catchException4();
  257. asm(".text");
  258. asm(".globl __catchException4");
  259. asm("__catchException4:");
  260. SAVE_REGISTERS1();
  261. SAVE_REGISTERS2();
  262. asm ("pushl $4");
  263. CALL_HOOK();

  264. /* Same thing for exception 5.  */
  265. extern void _catchException5();
  266. asm(".text");
  267. asm(".globl __catchException5");
  268. asm("__catchException5:");
  269. SAVE_REGISTERS1();
  270. SAVE_REGISTERS2();
  271. asm ("pushl $5");
  272. CALL_HOOK();

  273. /* Same thing for exception 6.  */
  274. extern void _catchException6();
  275. asm(".text");
  276. asm(".globl __catchException6");
  277. asm("__catchException6:");
  278. SAVE_REGISTERS1();
  279. SAVE_REGISTERS2();
  280. asm ("pushl $6");
  281. CALL_HOOK();

  282. /* Same thing for exception 7.  */
  283. extern void _catchException7();
  284. asm(".text");
  285. asm(".globl __catchException7");
  286. asm("__catchException7:");
  287. SAVE_REGISTERS1();
  288. SAVE_REGISTERS2();
  289. asm ("pushl $7");
  290. CALL_HOOK();

  291. /* Same thing for exception 8.  */
  292. extern void _catchException8();
  293. asm(".text");
  294. asm(".globl __catchException8");
  295. asm("__catchException8:");
  296. SAVE_REGISTERS1();
  297. SAVE_ERRCODE();
  298. SAVE_REGISTERS2();
  299. asm ("pushl $8");
  300. CALL_HOOK();

  301. /* Same thing for exception 9.  */
  302. extern void _catchException9();
  303. asm(".text");
  304. asm(".globl __catchException9");
  305. asm("__catchException9:");
  306. SAVE_REGISTERS1();
  307. SAVE_REGISTERS2();
  308. asm ("pushl $9");
  309. CALL_HOOK();

  310. /* Same thing for exception 10.  */
  311. extern void _catchException10();
  312. asm(".text");
  313. asm(".globl __catchException10");
  314. asm("__catchException10:");
  315. SAVE_REGISTERS1();
  316. SAVE_ERRCODE();
  317. SAVE_REGISTERS2();
  318. asm ("pushl $10");
  319. CALL_HOOK();

  320. /* Same thing for exception 12.  */
  321. extern void _catchException12();
  322. asm(".text");
  323. asm(".globl __catchException12");
  324. asm("__catchException12:");
  325. SAVE_REGISTERS1();
  326. SAVE_ERRCODE();
  327. SAVE_REGISTERS2();
  328. asm ("pushl $12");
  329. CALL_HOOK();

  330. /* Same thing for exception 16.  */
  331. extern void _catchException16();
  332. asm(".text");
  333. asm(".globl __catchException16");
  334. asm("__catchException16:");
  335. SAVE_REGISTERS1();
  336. SAVE_REGISTERS2();
  337. asm ("pushl $16");
  338. CALL_HOOK();

  339. /* For 13, 11, and 14 we have to deal with the CHECK_FAULT stuff.  */

  340. /* Same thing for exception 13.  */
  341. extern void _catchException13 ();
  342. asm (".text");
  343. asm (".globl __catchException13");
  344. asm ("__catchException13:");
  345. CHECK_FAULT();
  346. SAVE_REGISTERS1();
  347. SAVE_ERRCODE();
  348. SAVE_REGISTERS2();
  349. asm ("pushl $13");
  350. CALL_HOOK();

  351. /* Same thing for exception 11.  */
  352. extern void _catchException11 ();
  353. asm (".text");
  354. asm (".globl __catchException11");
  355. asm ("__catchException11:");
  356. CHECK_FAULT();
  357. SAVE_REGISTERS1();
  358. SAVE_ERRCODE();
  359. SAVE_REGISTERS2();
  360. asm ("pushl $11");
  361. CALL_HOOK();

  362. /* Same thing for exception 14.  */
  363. extern void _catchException14 ();
  364. asm (".text");
  365. asm (".globl __catchException14");
  366. asm ("__catchException14:");
  367. CHECK_FAULT();
  368. SAVE_REGISTERS1();
  369. SAVE_ERRCODE();
  370. SAVE_REGISTERS2();
  371. asm ("pushl $14");
  372. CALL_HOOK();

  373. /*
  374. * remcomHandler is a front end for handle_exception.  It moves the
  375. * stack pointer into an area reserved for debugger use.
  376. */
  377. asm("_remcomHandler:");
  378. asm("           popl %eax");        /* pop off return address     */
  379. asm("           popl %eax");      /* get the exception number   */
  380. asm("                movl _stackPtr, %esp"); /* move to remcom stack area  */
  381. asm("                pushl %eax");        /* push exception onto stack  */
  382. asm("                call  _handle_exception");    /* this never returns */

  383. void
  384. _returnFromException ()
  385. {
  386.   return_to_prog ();
  387. }

  388. int
  389. hex (ch)
  390.      char ch;
  391. {
  392.   if ((ch >= 'a') && (ch <= 'f'))
  393.     return (ch - 'a' + 10);
  394.   if ((ch >= '0') && (ch <= '9'))
  395.     return (ch - '0');
  396.   if ((ch >= 'A') && (ch <= 'F'))
  397.     return (ch - 'A' + 10);
  398.   return (-1);
  399. }

  400. static char remcomInBuffer[BUFMAX];
  401. static char remcomOutBuffer[BUFMAX];

  402. /* scan for the sequence $<data>#<checksum>     */

  403. unsigned char *
  404. getpacket (void)
  405. {
  406.   unsigned char *buffer = &remcomInBuffer[0];
  407.   unsigned char checksum;
  408.   unsigned char xmitcsum;
  409.   int count;
  410.   char ch;

  411.   while (1)
  412.     {
  413.       /* wait around for the start character, ignore all other characters */
  414.       while ((ch = getDebugChar ()) != '$')
  415.         ;

  416.     retry:
  417.       checksum = 0;
  418.       xmitcsum = -1;
  419.       count = 0;

  420.       /* now, read until a # or end of buffer is found */
  421.       while (count < BUFMAX - 1)
  422.         {
  423.           ch = getDebugChar ();
  424.           if (ch == '$')
  425.             goto retry;
  426.           if (ch == '#')
  427.             break;
  428.           checksum = checksum + ch;
  429.           buffer[count] = ch;
  430.           count = count + 1;
  431.         }
  432.       buffer[count] = 0;

  433.       if (ch == '#')
  434.         {
  435.           ch = getDebugChar ();
  436.           xmitcsum = hex (ch) << 4;
  437.           ch = getDebugChar ();
  438.           xmitcsum += hex (ch);

  439.           if (checksum != xmitcsum)
  440.             {
  441.               if (remote_debug)
  442.                 {
  443.                   fprintf (stderr,
  444.                            "bad checksum.  My count = 0x%x, sent=0x%x. buf=%s\n",
  445.                            checksum, xmitcsum, buffer);
  446.                 }
  447.               putDebugChar ('-');        /* failed checksum */
  448.             }
  449.           else
  450.             {
  451.               putDebugChar ('+');        /* successful transfer */

  452.               /* if a sequence char is present, reply the sequence ID */
  453.               if (buffer[2] == ':')
  454.                 {
  455.                   putDebugChar (buffer[0]);
  456.                   putDebugChar (buffer[1]);

  457.                   return &buffer[3];
  458.                 }

  459.               return &buffer[0];
  460.             }
  461.         }
  462.     }
  463. }

  464. /* send the packet in buffer.  */

  465. void
  466. putpacket (unsigned char *buffer)
  467. {
  468.   unsigned char checksum;
  469.   int count;
  470.   char ch;

  471.   /*  $<packet info>#<checksum>.  */
  472.   do
  473.     {
  474.       putDebugChar ('$');
  475.       checksum = 0;
  476.       count = 0;

  477.       while (ch = buffer[count])
  478.         {
  479.           putDebugChar (ch);
  480.           checksum += ch;
  481.           count += 1;
  482.         }

  483.       putDebugChar ('#');
  484.       putDebugChar (hexchars[checksum >> 4]);
  485.       putDebugChar (hexchars[checksum % 16]);

  486.     }
  487.   while (getDebugChar () != '+');
  488. }

  489. void
  490. debug_error (format, parm)
  491.      char *format;
  492.      char *parm;
  493. {
  494.   if (remote_debug)
  495.     fprintf (stderr, format, parm);
  496. }

  497. /* Address of a routine to RTE to if we get a memory fault.  */
  498. static void (*volatile mem_fault_routine) () = NULL;

  499. /* Indicate to caller of mem2hex or hex2mem that there has been an
  500.    error.  */
  501. static volatile int mem_err = 0;

  502. void
  503. set_mem_err (void)
  504. {
  505.   mem_err = 1;
  506. }

  507. /* These are separate functions so that they are so short and sweet
  508.    that the compiler won't save any registers (if there is a fault
  509.    to mem_fault, they won't get restored, so there better not be any
  510.    saved).  */
  511. int
  512. get_char (char *addr)
  513. {
  514.   return *addr;
  515. }

  516. void
  517. set_char (char *addr, int val)
  518. {
  519.   *addr = val;
  520. }

  521. /* convert the memory pointed to by mem into hex, placing result in buf */
  522. /* return a pointer to the last char put in buf (null) */
  523. /* If MAY_FAULT is non-zero, then we should set mem_err in response to
  524.    a fault; if zero treat a fault like any other fault in the stub.  */
  525. char *
  526. mem2hex (mem, buf, count, may_fault)
  527.      char *mem;
  528.      char *buf;
  529.      int count;
  530.      int may_fault;
  531. {
  532.   int i;
  533.   unsigned char ch;

  534.   if (may_fault)
  535.     mem_fault_routine = set_mem_err;
  536.   for (i = 0; i < count; i++)
  537.     {
  538.       ch = get_char (mem++);
  539.       if (may_fault && mem_err)
  540.         return (buf);
  541.       *buf++ = hexchars[ch >> 4];
  542.       *buf++ = hexchars[ch % 16];
  543.     }
  544.   *buf = 0;
  545.   if (may_fault)
  546.     mem_fault_routine = NULL;
  547.   return (buf);
  548. }

  549. /* convert the hex array pointed to by buf into binary to be placed in mem */
  550. /* return a pointer to the character AFTER the last byte written */
  551. char *
  552. hex2mem (buf, mem, count, may_fault)
  553.      char *buf;
  554.      char *mem;
  555.      int count;
  556.      int may_fault;
  557. {
  558.   int i;
  559.   unsigned char ch;

  560.   if (may_fault)
  561.     mem_fault_routine = set_mem_err;
  562.   for (i = 0; i < count; i++)
  563.     {
  564.       ch = hex (*buf++) << 4;
  565.       ch = ch + hex (*buf++);
  566.       set_char (mem++, ch);
  567.       if (may_fault && mem_err)
  568.         return (mem);
  569.     }
  570.   if (may_fault)
  571.     mem_fault_routine = NULL;
  572.   return (mem);
  573. }

  574. /* this function takes the 386 exception vector and attempts to
  575.    translate this number into a unix compatible signal value */
  576. int
  577. computeSignal (int exceptionVector)
  578. {
  579.   int sigval;
  580.   switch (exceptionVector)
  581.     {
  582.     case 0:
  583.       sigval = 8;
  584.       break;                        /* divide by zero */
  585.     case 1:
  586.       sigval = 5;
  587.       break;                        /* debug exception */
  588.     case 3:
  589.       sigval = 5;
  590.       break;                        /* breakpoint */
  591.     case 4:
  592.       sigval = 16;
  593.       break;                        /* into instruction (overflow) */
  594.     case 5:
  595.       sigval = 16;
  596.       break;                        /* bound instruction */
  597.     case 6:
  598.       sigval = 4;
  599.       break;                        /* Invalid opcode */
  600.     case 7:
  601.       sigval = 8;
  602.       break;                        /* coprocessor not available */
  603.     case 8:
  604.       sigval = 7;
  605.       break;                        /* double fault */
  606.     case 9:
  607.       sigval = 11;
  608.       break;                        /* coprocessor segment overrun */
  609.     case 10:
  610.       sigval = 11;
  611.       break;                        /* Invalid TSS */
  612.     case 11:
  613.       sigval = 11;
  614.       break;                        /* Segment not present */
  615.     case 12:
  616.       sigval = 11;
  617.       break;                        /* stack exception */
  618.     case 13:
  619.       sigval = 11;
  620.       break;                        /* general protection */
  621.     case 14:
  622.       sigval = 11;
  623.       break;                        /* page fault */
  624.     case 16:
  625.       sigval = 7;
  626.       break;                        /* coprocessor error */
  627.     default:
  628.       sigval = 7;                /* "software generated" */
  629.     }
  630.   return (sigval);
  631. }

  632. /**********************************************/
  633. /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
  634. /* RETURN NUMBER OF CHARS PROCESSED           */
  635. /**********************************************/
  636. int
  637. hexToInt (char **ptr, int *intValue)
  638. {
  639.   int numChars = 0;
  640.   int hexValue;

  641.   *intValue = 0;

  642.   while (**ptr)
  643.     {
  644.       hexValue = hex (**ptr);
  645.       if (hexValue >= 0)
  646.         {
  647.           *intValue = (*intValue << 4) | hexValue;
  648.           numChars++;
  649.         }
  650.       else
  651.         break;

  652.       (*ptr)++;
  653.     }

  654.   return (numChars);
  655. }

  656. /*
  657. * This function does all command procesing for interfacing to gdb.
  658. */
  659. void
  660. handle_exception (int exceptionVector)
  661. {
  662.   int sigval, stepping;
  663.   int addr, length;
  664.   char *ptr;
  665.   int newPC;

  666.   gdb_i386vector = exceptionVector;

  667.   if (remote_debug)
  668.     {
  669.       printf ("vector=%d, sr=0x%x, pc=0x%x\n",
  670.               exceptionVector, registers[PS], registers[PC]);
  671.     }

  672.   /* reply to host that an exception has occurred */
  673.   sigval = computeSignal (exceptionVector);

  674.   ptr = remcomOutBuffer;

  675.   *ptr++ = 'T';                        /* notify gdb with signo, PC, FP and SP */
  676.   *ptr++ = hexchars[sigval >> 4];
  677.   *ptr++ = hexchars[sigval & 0xf];

  678.   *ptr++ = hexchars[ESP];
  679.   *ptr++ = ':';
  680.   ptr = mem2hex((char *)&registers[ESP], ptr, 4, 0);        /* SP */
  681.   *ptr++ = ';';

  682.   *ptr++ = hexchars[EBP];
  683.   *ptr++ = ':';
  684.   ptr = mem2hex((char *)&registers[EBP], ptr, 4, 0);         /* FP */
  685.   *ptr++ = ';';

  686.   *ptr++ = hexchars[PC];
  687.   *ptr++ = ':';
  688.   ptr = mem2hex((char *)&registers[PC], ptr, 4, 0);         /* PC */
  689.   *ptr++ = ';';

  690.   *ptr = '\0'

  691.   putpacket (remcomOutBuffer);

  692.   stepping = 0;

  693.   while (1 == 1)
  694.     {
  695.       remcomOutBuffer[0] = 0;
  696.       ptr = getpacket ();

  697.       switch (*ptr++)
  698.         {
  699.         case '?':
  700.           remcomOutBuffer[0] = 'S';
  701.           remcomOutBuffer[1] = hexchars[sigval >> 4];
  702.           remcomOutBuffer[2] = hexchars[sigval % 16];
  703.           remcomOutBuffer[3] = 0;
  704.           break;
  705.         case 'd':
  706.           remote_debug = !(remote_debug);        /* toggle debug flag */
  707.           break;
  708.         case 'g':                /* return the value of the CPU registers */
  709.           mem2hex ((char *) registers, remcomOutBuffer, NUMREGBYTES, 0);
  710.           break;
  711.         case 'G':                /* set the value of the CPU registers - return OK */
  712.           hex2mem (ptr, (char *) registers, NUMREGBYTES, 0);
  713.           strcpy (remcomOutBuffer, "OK");
  714.           break;
  715.         case 'P':                /* set the value of a single CPU register - return OK */
  716.           {
  717.             int regno;

  718.             if (hexToInt (&ptr, &regno) && *ptr++ == '=')
  719.               if (regno >= 0 && regno < NUMREGS)
  720.                 {
  721.                   hex2mem (ptr, (char *) &registers[regno], 4, 0);
  722.                   strcpy (remcomOutBuffer, "OK");
  723.                   break;
  724.                 }

  725.             strcpy (remcomOutBuffer, "E01");
  726.             break;
  727.           }

  728.           /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
  729.         case 'm':
  730.           /* TRY TO READ %x,%x.  IF SUCCEED, SET PTR = 0 */
  731.           if (hexToInt (&ptr, &addr))
  732.             if (*(ptr++) == ',')
  733.               if (hexToInt (&ptr, &length))
  734.                 {
  735.                   ptr = 0;
  736.                   mem_err = 0;
  737.                   mem2hex ((char *) addr, remcomOutBuffer, length, 1);
  738.                   if (mem_err)
  739.                     {
  740.                       strcpy (remcomOutBuffer, "E03");
  741.                       debug_error ("memory fault");
  742.                     }
  743.                 }

  744.           if (ptr)
  745.             {
  746.               strcpy (remcomOutBuffer, "E01");
  747.             }
  748.           break;

  749.           /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
  750.         case 'M':
  751.           /* TRY TO READ '%x,%x:'.  IF SUCCEED, SET PTR = 0 */
  752.           if (hexToInt (&ptr, &addr))
  753.             if (*(ptr++) == ',')
  754.               if (hexToInt (&ptr, &length))
  755.                 if (*(ptr++) == ':')
  756.                   {
  757.                     mem_err = 0;
  758.                     hex2mem (ptr, (char *) addr, length, 1);

  759.                     if (mem_err)
  760.                       {
  761.                         strcpy (remcomOutBuffer, "E03");
  762.                         debug_error ("memory fault");
  763.                       }
  764.                     else
  765.                       {
  766.                         strcpy (remcomOutBuffer, "OK");
  767.                       }

  768.                     ptr = 0;
  769.                   }
  770.           if (ptr)
  771.             {
  772.               strcpy (remcomOutBuffer, "E02");
  773.             }
  774.           break;

  775.           /* cAA..AA    Continue at address AA..AA(optional) */
  776.           /* sAA..AA   Step one instruction from AA..AA(optional) */
  777.         case 's':
  778.           stepping = 1;
  779.         case 'c':
  780.           /* try to read optional parameter, pc unchanged if no parm */
  781.           if (hexToInt (&ptr, &addr))
  782.             registers[PC] = addr;

  783.           newPC = registers[PC];

  784.           /* clear the trace bit */
  785.           registers[PS] &= 0xfffffeff;

  786.           /* set the trace bit if we're stepping */
  787.           if (stepping)
  788.             registers[PS] |= 0x100;

  789.           _returnFromException ();        /* this is a jump */
  790.           break;

  791.           /* kill the program */
  792.         case 'k':                /* do nothing */
  793. #if 0
  794.           /* Huh? This doesn't look like "nothing".
  795.              m68k-stub.c and sparc-stub.c don't have it.  */
  796.           BREAKPOINT ();
  797. #endif
  798.           break;
  799.         }                        /* switch */

  800.       /* reply to the request */
  801.       putpacket (remcomOutBuffer);
  802.     }
  803. }

  804. /* this function is used to set up exception handlers for tracing and
  805.    breakpoints */
  806. void
  807. set_debug_traps (void)
  808. {
  809.   stackPtr = &remcomStack[STACKSIZE / sizeof (int) - 1];

  810.   exceptionHandler (0, _catchException0);
  811.   exceptionHandler (1, _catchException1);
  812.   exceptionHandler (3, _catchException3);
  813.   exceptionHandler (4, _catchException4);
  814.   exceptionHandler (5, _catchException5);
  815.   exceptionHandler (6, _catchException6);
  816.   exceptionHandler (7, _catchException7);
  817.   exceptionHandler (8, _catchException8);
  818.   exceptionHandler (9, _catchException9);
  819.   exceptionHandler (10, _catchException10);
  820.   exceptionHandler (11, _catchException11);
  821.   exceptionHandler (12, _catchException12);
  822.   exceptionHandler (13, _catchException13);
  823.   exceptionHandler (14, _catchException14);
  824.   exceptionHandler (16, _catchException16);

  825.   initialized = 1;
  826. }

  827. /* This function will generate a breakpoint exception.  It is used at the
  828.    beginning of a program to sync up with a debugger and can be used
  829.    otherwise as a quick means to stop program execution and "break" into
  830.    the debugger.  */

  831. void
  832. breakpoint (void)
  833. {
  834.   if (initialized)
  835.     BREAKPOINT ();
  836. }