1 /****************************************************************************
3 THIS SOFTWARE IS NOT COPYRIGHTED
5 HP offers the following for use in the public domain. HP makes no
6 warranty with regard to the software or its performance and the
7 user accepts the software "AS IS" with all faults.
9 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
10 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 /****************************************************************************
16 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
18 * Module name: remcom.c $
20 * Date: 91/03/09 12:29:49 $
21 * Contributor: Lake Stevens Instrument Division$
23 * Description: low level support for gdb debugger. $
25 * Considerations: only works on target hardware $
27 * Written by: Glenn Engel $
28 * ModuleState: Experimental $
32 * Modified for FreeBSD by Stu Grossman.
34 * To enable debugger support, two things need to happen. One, a
35 * call to set_debug_traps() is necessary in order to allow any breakpoints
36 * or error conditions to be properly intercepted and reported to gdb.
37 * Two, a breakpoint needs to be generated to begin communication. This
38 * is most easily accomplished by a call to breakpoint(). Breakpoint()
39 * simulates a breakpoint by executing a trap #1.
41 * The external function exceptionHandler() is
42 * used to attach a specific handler to a specific 386 vector number.
43 * It should use the same privilege level it runs at. It should
44 * install it as an interrupt gate so that interrupts are masked
45 * while the handler runs.
46 * Also, need to assign exceptionHook and oldExceptionHook.
48 * Because gdb will sometimes write to the stack area to execute function
49 * calls, this program cannot rely on using the supervisor stack so it
50 * uses its own stack area reserved in the int array remcomStack.
54 * The following gdb commands are supported:
56 * command function Return value
58 * g return the value of the CPU registers hex data or ENN
59 * G set the value of the CPU registers OK or ENN
61 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
62 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
64 * c Resume at current address SNN ( signal NN)
65 * cAA..AA Continue at address AA..AA SNN
67 * s Step one instruction SNN
68 * sAA..AA Step one instruction from AA..AA SNN
72 * ? What was the last sigval ? SNN (signal NN)
76 * All commands and responses are sent with a packet which includes a
77 * checksum. A packet consists of
79 * $<packet info>#<checksum>.
82 * <packet info> :: <characters representing the command or response>
83 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
85 * When a packet is received, it is first acknowledged with either '+' or '-'.
86 * '+' indicates a successful transfer. '-' indicates a failed transfer.
91 * $m0,10#2a +$00010203040506070809101112131415#42
93 ****************************************************************************/
94 /* $FreeBSD: src/sys/i386/i386/i386-gdbstub.c,v 1.13.2.1 2000/08/03 00:54:41 peter Exp $ */
95 /* $DragonFly: src/sys/platform/pc32/i386/Attic/i386-gdbstub.c,v 1.3 2003/08/07 21:17:22 dillon Exp $ */
100 #include <sys/param.h>
101 #include <sys/reboot.h>
102 #include <sys/systm.h>
103 #include <sys/cons.h>
109 void gdb_handle_exception (db_regs_t *, int, int);
113 gdb_handle_exception (db_regs_t *raw_regs, int type, int code)
117 /************************************************************************/
119 extern jmp_buf db_jmpbuf;
121 /************************************************************************/
122 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
123 /* at least NUMREGBYTES*2 are needed for register packets */
126 /* Create private copies of common functions used by the stub. This prevents
127 nasty interactions between app code and the stub (for instance if user steps
128 into strlen, etc..) */
130 #define strlen gdb_strlen
131 #define strcpy gdb_strcpy
134 strlen (const char *s)
138 while (*s1++ != '\000');
144 strcpy (char *dst, const char *src)
148 while ((*dst++ = *src++) != '\000');
154 putDebugChar (int c) /* write a single character */
158 (*gdb_putc)(gdbdev, c);
163 getDebugChar (void) /* read and return a single char */
167 return (*gdb_getc)(gdbdev);
170 static const char hexchars[]="0123456789abcdef";
175 if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10);
176 if ((ch >= '0') && (ch <= '9')) return (ch-'0');
177 if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10);
181 /* scan for the sequence $<data>#<checksum> */
183 getpacket (char *buffer)
185 unsigned char checksum;
186 unsigned char xmitcsum;
193 /* wait around for the start character, ignore all other characters */
195 while ((ch = (getDebugChar () & 0x7f)) != '$');
202 /* now, read until a # or end of buffer is found */
204 while (count < BUFMAX)
206 ch = getDebugChar () & 0x7f;
209 checksum = checksum + ch;
217 xmitcsum = hex (getDebugChar () & 0x7f) << 4;
218 xmitcsum += hex (getDebugChar () & 0x7f);
220 if (checksum != xmitcsum)
221 putDebugChar ('-'); /* failed checksum */
224 putDebugChar ('+'); /* successful transfer */
225 /* if a sequence char is present, reply the sequence ID */
226 if (buffer[2] == ':')
228 putDebugChar (buffer[0]);
229 putDebugChar (buffer[1]);
231 /* remove sequence chars from buffer */
233 count = strlen (buffer);
234 for (i=3; i <= count; i++)
235 buffer[i-3] = buffer[i];
240 while (checksum != xmitcsum);
243 /* send the packet in buffer. */
246 putpacket (char *buffer)
248 unsigned char checksum;
252 /* $<packet info>#<checksum>. */
256 * This is a non-standard hack to allow use of the serial console for
257 * operation as well as debugging. Simply turn on 'remotechat' in gdb.
259 * This extension is not part of the Cygnus protocol, is kinda gross,
260 * but gets the job done.
262 #ifdef GDB_REMOTE_CHAT
272 while ((ch=buffer[count]) != 0)
280 putDebugChar (hexchars[checksum >> 4]);
281 putDebugChar (hexchars[checksum & 0xf]);
283 while ((getDebugChar () & 0x7f) != '+');
286 static char remcomInBuffer[BUFMAX];
287 static char remcomOutBuffer[BUFMAX];
290 get_char (vm_offset_t addr)
294 if (setjmp (db_jmpbuf))
297 db_read_bytes (addr, 1, &data);
303 set_char (vm_offset_t addr, int val)
307 if (setjmp (db_jmpbuf))
312 db_write_bytes (addr, 1, &data);
316 /* convert the memory pointed to by mem into hex, placing result in buf */
317 /* return a pointer to the last char put in buf (null) */
320 mem2hex (vm_offset_t mem, char *buf, int count)
325 for (i=0;i<count;i++) {
326 ch = get_char (mem++);
329 *buf++ = hexchars[ch >> 4];
330 *buf++ = hexchars[ch % 16];
336 /* convert the hex array pointed to by buf into binary to be placed in mem */
337 /* return a pointer to the character AFTER the last byte written */
339 hex2mem (char *buf, vm_offset_t mem, int count)
345 for (i=0;i<count;i++) {
346 ch = hex(*buf++) << 4;
347 ch = ch + hex(*buf++);
348 rv = set_char (mem++, ch);
355 /* this function takes the 386 exception vector and attempts to
356 translate this number into a unix compatible signal value */
358 computeSignal (int exceptionVector)
361 switch (exceptionVector & ~T_USER)
363 case 0: sigval = 8; break; /* divide by zero */
364 case 1: sigval = 5; break; /* debug exception */
365 case 3: sigval = 5; break; /* breakpoint */
366 case 4: sigval = 16; break; /* into instruction (overflow) */
367 case 5: sigval = 16; break; /* bound instruction */
368 case 6: sigval = 4; break; /* Invalid opcode */
369 case 7: sigval = 8; break; /* coprocessor not available */
370 case 8: sigval = 7; break; /* double fault */
371 case 9: sigval = 11; break; /* coprocessor segment overrun */
372 case 10: sigval = 5; break; /* Invalid TSS (also single-step) */
373 case 11: sigval = 11; break; /* Segment not present */
374 case 12: sigval = 11; break; /* stack exception */
375 case 13: sigval = 11; break; /* general protection */
376 case 14: sigval = 11; break; /* page fault */
377 case 16: sigval = 7; break; /* coprocessor error */
379 sigval = 7; /* "software generated"*/
385 * While we find nice hex chars, build an int.
386 * Return number of chars processed.
390 hexToInt(char **ptr, int *intValue)
399 hexValue = hex(**ptr);
402 *intValue = (*intValue <<4) | hexValue;
414 #define NUMREGBYTES (sizeof registers)
421 * This function does all command procesing for interfacing to gdb.
424 gdb_handle_exception (db_regs_t *raw_regs, int type, int code)
445 struct i386regs registers;
447 registers.eax = raw_regs->tf_eax;
448 registers.ebx = raw_regs->tf_ebx;
449 registers.ecx = raw_regs->tf_ecx;
450 registers.edx = raw_regs->tf_edx;
452 registers.esp = raw_regs->tf_esp;
453 registers.ebp = raw_regs->tf_ebp;
454 registers.esi = raw_regs->tf_esi;
455 registers.edi = raw_regs->tf_edi;
457 registers.eip = raw_regs->tf_eip;
458 registers.eflags = raw_regs->tf_eflags;
460 registers.cs = raw_regs->tf_cs;
461 registers.ss = raw_regs->tf_ss;
462 registers.ds = raw_regs->tf_ds;
463 registers.es = raw_regs->tf_es;
465 /* reply to host that an exception has occurred */
466 sigval = computeSignal (type);
467 ptr = remcomOutBuffer;
470 *ptr++ = hexchars[sigval >> 4];
471 *ptr++ = hexchars[sigval & 0xf];
473 *ptr++ = hexchars[PC >> 4];
474 *ptr++ = hexchars[PC & 0xf];
476 ptr = mem2hex ((vm_offset_t)®isters.eip, ptr, 4);
479 *ptr++ = hexchars[FP >> 4];
480 *ptr++ = hexchars[FP & 0xf];
482 ptr = mem2hex ((vm_offset_t)®isters.ebp, ptr, 4);
485 *ptr++ = hexchars[SP >> 4];
486 *ptr++ = hexchars[SP & 0xf];
488 ptr = mem2hex ((vm_offset_t)®isters.esp, ptr, 4);
493 putpacket (remcomOutBuffer);
497 remcomOutBuffer[0] = 0;
499 getpacket (remcomInBuffer);
500 switch (remcomInBuffer[0])
503 remcomOutBuffer[0] = 'S';
504 remcomOutBuffer[1] = hexchars[sigval >> 4];
505 remcomOutBuffer[2] = hexchars[sigval % 16];
506 remcomOutBuffer[3] = 0;
509 case 'D': /* detach; say OK and turn off gdb */
510 putpacket(remcomOutBuffer);
511 boothowto &= ~RB_GDB;
514 case 'g': /* return the value of the CPU registers */
515 mem2hex ((vm_offset_t)®isters, remcomOutBuffer, NUMREGBYTES);
518 case 'G': /* set the value of the CPU registers - return OK */
519 hex2mem (&remcomInBuffer[1], (vm_offset_t)®isters, NUMREGBYTES);
520 strcpy (remcomOutBuffer, "OK");
523 case 'P': /* Set the value of one register */
527 ptr = &remcomInBuffer[1];
529 if (hexToInt (&ptr, ®no)
533 hex2mem (ptr, (vm_offset_t)®isters + regno * 4, 4);
534 strcpy(remcomOutBuffer,"OK");
537 strcpy (remcomOutBuffer, "P01");
540 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
541 /* Try to read %x,%x. */
543 ptr = &remcomInBuffer[1];
545 if (hexToInt (&ptr, &addr)
547 && hexToInt (&ptr, &length))
549 if (mem2hex((vm_offset_t) addr, remcomOutBuffer, length) == NULL)
550 strcpy (remcomOutBuffer, "E03");
554 strcpy (remcomOutBuffer, "E01");
557 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
559 /* Try to read '%x,%x:'. */
561 ptr = &remcomInBuffer[1];
563 if (hexToInt(&ptr,&addr)
565 && hexToInt(&ptr, &length)
568 if (hex2mem(ptr, (vm_offset_t) addr, length) == NULL)
569 strcpy (remcomOutBuffer, "E03");
571 strcpy (remcomOutBuffer, "OK");
574 strcpy (remcomOutBuffer, "E02");
577 /* cAA..AA Continue at address AA..AA(optional) */
578 /* sAA..AA Step one instruction from AA..AA(optional) */
581 /* try to read optional parameter, pc unchanged if no parm */
583 ptr = &remcomInBuffer[1];
584 if (hexToInt(&ptr,&addr))
585 registers.eip = addr;
588 /* set the trace bit if we're stepping */
589 if (remcomInBuffer[0] == 's')
590 registers.eflags |= PSL_T;
592 registers.eflags &= ~PSL_T;
594 raw_regs->tf_eax = registers.eax;
595 raw_regs->tf_ebx = registers.ebx;
596 raw_regs->tf_ecx = registers.ecx;
597 raw_regs->tf_edx = registers.edx;
599 raw_regs->tf_esp = registers.esp;
600 raw_regs->tf_ebp = registers.ebp;
601 raw_regs->tf_esi = registers.esi;
602 raw_regs->tf_edi = registers.edi;
604 raw_regs->tf_eip = registers.eip;
605 raw_regs->tf_eflags = registers.eflags;
607 raw_regs->tf_cs = registers.cs;
608 raw_regs->tf_ss = registers.ss;
609 raw_regs->tf_ds = registers.ds;
610 raw_regs->tf_es = registers.es;
615 /* reply to the request */
616 putpacket (remcomOutBuffer);
619 #endif /* NSIO > 0 */