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 ****************************************************************************/
95 * $FreeBSD: src/sys/i386/i386/i386-gdbstub.c,v 1.13.2.1 2000/08/03 00:54:41 peter 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);
111 /************************************************************************/
113 extern jmp_buf db_jmpbuf;
115 /************************************************************************/
116 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
117 /* at least NUMREGBYTES*2 are needed for register packets */
120 /* Create private copies of common functions used by the stub. This prevents
121 nasty interactions between app code and the stub (for instance if user steps
122 into strlen, etc..) */
124 #define strlen gdb_strlen
125 #define strcpy gdb_strcpy
128 strlen (const char *s)
132 while (*s1++ != '\000');
138 strcpy (char *dst, const char *src)
142 while ((*dst++ = *src++) != '\000');
148 putDebugChar (int c) /* write a single character */
152 gdb_tab->cn_putc(gdb_tab->cn_gdbprivate, c);
157 getDebugChar (void) /* read and return a single char */
161 return gdb_tab->cn_getc(gdb_tab->cn_gdbprivate);
164 static const char hexchars[]="0123456789abcdef";
169 if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10);
170 if ((ch >= '0') && (ch <= '9')) return (ch-'0');
171 if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10);
175 /* scan for the sequence $<data>#<checksum> */
177 getpacket (char *buffer)
179 unsigned char checksum;
180 unsigned char xmitcsum;
187 /* wait around for the start character, ignore all other characters */
189 while ((ch = (getDebugChar () & 0x7f)) != '$');
196 /* now, read until a # or end of buffer is found */
198 while (count < BUFMAX)
200 ch = getDebugChar () & 0x7f;
203 checksum = checksum + ch;
211 xmitcsum = hex (getDebugChar () & 0x7f) << 4;
212 xmitcsum += hex (getDebugChar () & 0x7f);
214 if (checksum != xmitcsum)
215 putDebugChar ('-'); /* failed checksum */
218 putDebugChar ('+'); /* successful transfer */
219 /* if a sequence char is present, reply the sequence ID */
220 if (buffer[2] == ':')
222 putDebugChar (buffer[0]);
223 putDebugChar (buffer[1]);
225 /* remove sequence chars from buffer */
227 count = strlen (buffer);
228 for (i=3; i <= count; i++)
229 buffer[i-3] = buffer[i];
234 while (checksum != xmitcsum);
237 /* send the packet in buffer. */
240 putpacket (char *buffer)
242 unsigned char checksum;
246 /* $<packet info>#<checksum>. */
250 * This is a non-standard hack to allow use of the serial console for
251 * operation as well as debugging. Simply turn on 'remotechat' in gdb.
253 * This extension is not part of the Cygnus protocol, is kinda gross,
254 * but gets the job done.
256 #ifdef GDB_REMOTE_CHAT
266 while ((ch=buffer[count]) != 0)
274 putDebugChar (hexchars[checksum >> 4]);
275 putDebugChar (hexchars[checksum & 0xf]);
277 while ((getDebugChar () & 0x7f) != '+');
280 static char remcomInBuffer[BUFMAX];
281 static char remcomOutBuffer[BUFMAX];
284 get_char (vm_offset_t addr)
288 if (setjmp (db_jmpbuf))
291 db_read_bytes (addr, 1, &data);
297 set_char (vm_offset_t addr, int val)
301 if (setjmp (db_jmpbuf))
306 db_write_bytes (addr, 1, &data);
310 /* convert the memory pointed to by mem into hex, placing result in buf */
311 /* return a pointer to the last char put in buf (null) */
314 mem2hex (vm_offset_t mem, char *buf, int count)
319 for (i=0;i<count;i++) {
320 ch = get_char (mem++);
323 *buf++ = hexchars[ch >> 4];
324 *buf++ = hexchars[ch % 16];
330 /* convert the hex array pointed to by buf into binary to be placed in mem */
331 /* return a pointer to the character AFTER the last byte written */
333 hex2mem (char *buf, vm_offset_t mem, int count)
339 for (i=0;i<count;i++) {
340 ch = hex(*buf++) << 4;
341 ch = ch + hex(*buf++);
342 rv = set_char (mem++, ch);
349 /* this function takes the 386 exception vector and attempts to
350 translate this number into a unix compatible signal value */
352 computeSignal (int exceptionVector)
355 switch (exceptionVector & ~T_USER)
357 case 0: sigval = 8; break; /* divide by zero */
358 case 1: sigval = 5; break; /* debug exception */
359 case 3: sigval = 5; break; /* breakpoint */
360 case 4: sigval = 16; break; /* into instruction (overflow) */
361 case 5: sigval = 16; break; /* bound instruction */
362 case 6: sigval = 4; break; /* Invalid opcode */
363 case 7: sigval = 8; break; /* coprocessor not available */
364 case 8: sigval = 7; break; /* double fault */
365 case 9: sigval = 11; break; /* coprocessor segment overrun */
366 case 10: sigval = 5; break; /* Invalid TSS (also single-step) */
367 case 11: sigval = 11; break; /* Segment not present */
368 case 12: sigval = 11; break; /* stack exception */
369 case 13: sigval = 11; break; /* general protection */
370 case 14: sigval = 11; break; /* page fault */
371 case 16: sigval = 7; break; /* coprocessor error */
373 sigval = 7; /* "software generated"*/
379 * While we find nice hex chars, build an int.
380 * Return number of chars processed.
384 hexToInt(char **ptr, int *intValue)
393 hexValue = hex(**ptr);
396 *intValue = (*intValue <<4) | hexValue;
409 * While we find nice hex chars, build a long.
410 * Return number of chars processed.
414 hexToLong(char **ptr, long *longValue)
423 hexValue = hex(**ptr);
426 *longValue = (*longValue <<4) | hexValue;
438 #define NUMREGBYTES (sizeof registers)
445 * This function does all command procesing for interfacing to gdb.
448 gdb_handle_exception (db_regs_t *raw_regs, int type, int code)
472 unsigned long rflags;
476 struct x86_64regs registers;
478 registers.rax = raw_regs->tf_rax;
479 registers.rbx = raw_regs->tf_rbx;
480 registers.rcx = raw_regs->tf_rcx;
481 registers.rdx = raw_regs->tf_rdx;
483 registers.rsp = raw_regs->tf_rsp;
484 registers.rbp = raw_regs->tf_rbp;
485 registers.rsi = raw_regs->tf_rsi;
486 registers.rdi = raw_regs->tf_rdi;
488 registers.r8 = raw_regs->tf_r8;
489 registers.r9 = raw_regs->tf_r9;
490 registers.r10 = raw_regs->tf_r10;
491 registers.r11 = raw_regs->tf_r11;
492 registers.r12 = raw_regs->tf_r12;
493 registers.r13 = raw_regs->tf_r13;
494 registers.r14 = raw_regs->tf_r14;
495 registers.r15 = raw_regs->tf_r15;
497 registers.rip = raw_regs->tf_rip;
498 registers.rflags = raw_regs->tf_rflags;
500 registers.cs = raw_regs->tf_cs;
501 registers.ss = raw_regs->tf_ss;
503 /* reply to host that an exception has occurred */
504 sigval = computeSignal (type);
505 ptr = remcomOutBuffer;
508 *ptr++ = hexchars[sigval >> 4];
509 *ptr++ = hexchars[sigval & 0xf];
511 *ptr++ = hexchars[PC >> 4];
512 *ptr++ = hexchars[PC & 0xf];
514 ptr = mem2hex ((vm_offset_t)®isters.rip, ptr, 8);
517 *ptr++ = hexchars[FP >> 4];
518 *ptr++ = hexchars[FP & 0xf];
520 ptr = mem2hex ((vm_offset_t)®isters.rbp, ptr, 8);
523 *ptr++ = hexchars[SP >> 4];
524 *ptr++ = hexchars[SP & 0xf];
526 ptr = mem2hex ((vm_offset_t)®isters.rsp, ptr, 8);
531 putpacket (remcomOutBuffer);
535 remcomOutBuffer[0] = 0;
537 getpacket (remcomInBuffer);
538 switch (remcomInBuffer[0])
541 remcomOutBuffer[0] = 'S';
542 remcomOutBuffer[1] = hexchars[sigval >> 4];
543 remcomOutBuffer[2] = hexchars[sigval % 16];
544 remcomOutBuffer[3] = 0;
547 case 'D': /* detach; say OK and turn off gdb */
548 putpacket(remcomOutBuffer);
549 boothowto &= ~RB_GDB;
552 case 'g': /* return the value of the CPU registers */
553 mem2hex ((vm_offset_t)®isters, remcomOutBuffer, NUMREGBYTES);
556 case 'G': /* set the value of the CPU registers - return OK */
557 hex2mem (&remcomInBuffer[1], (vm_offset_t)®isters, NUMREGBYTES);
558 strcpy (remcomOutBuffer, "OK");
561 case 'P': /* Set the value of one register */
565 ptr = &remcomInBuffer[1];
567 if (hexToInt (&ptr, ®no)
572 hex2mem (ptr, (vm_offset_t)®isters + regno * 8, 8);
573 strcpy(remcomOutBuffer,"OK");
576 strcpy (remcomOutBuffer, "P01");
579 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
580 /* Try to read %x,%x. */
582 ptr = &remcomInBuffer[1];
584 if (hexToLong (&ptr, &addr)
586 && hexToInt (&ptr, &length))
588 if (mem2hex((vm_offset_t) addr, remcomOutBuffer, length) == NULL)
589 strcpy (remcomOutBuffer, "E03");
593 strcpy (remcomOutBuffer, "E01");
596 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
598 /* Try to read '%x,%x:'. */
600 ptr = &remcomInBuffer[1];
602 if (hexToLong(&ptr,&addr)
604 && hexToInt(&ptr, &length)
607 if (hex2mem(ptr, (vm_offset_t) addr, length) == NULL)
608 strcpy (remcomOutBuffer, "E03");
610 strcpy (remcomOutBuffer, "OK");
613 strcpy (remcomOutBuffer, "E02");
616 /* cAA..AA Continue at address AA..AA(optional) */
617 /* sAA..AA Step one instruction from AA..AA(optional) */
620 /* try to read optional parameter, pc unchanged if no parm */
622 ptr = &remcomInBuffer[1];
623 if (hexToLong(&ptr,&addr))
624 registers.rip = addr;
627 /* set the trace bit if we're stepping */
628 if (remcomInBuffer[0] == 's')
629 registers.rflags |= PSL_T;
631 registers.rflags &= ~PSL_T;
633 raw_regs->tf_rax = registers.rax;
634 raw_regs->tf_rbx = registers.rbx;
635 raw_regs->tf_rcx = registers.rcx;
636 raw_regs->tf_rdx = registers.rdx;
638 raw_regs->tf_rsp = registers.rsp;
639 raw_regs->tf_rbp = registers.rbp;
640 raw_regs->tf_rsi = registers.rsi;
641 raw_regs->tf_rdi = registers.rdi;
643 raw_regs->tf_r8 = registers.r8;
644 raw_regs->tf_r9 = registers.r9;
645 raw_regs->tf_r10 = registers.r10;
646 raw_regs->tf_r11 = registers.r11;
647 raw_regs->tf_r12 = registers.r12;
648 raw_regs->tf_r13 = registers.r13;
649 raw_regs->tf_r14 = registers.r14;
650 raw_regs->tf_r15 = registers.r15;
652 raw_regs->tf_rip = registers.rip;
653 raw_regs->tf_rflags = registers.rflags;
655 raw_regs->tf_cs = registers.cs;
656 raw_regs->tf_ss = registers.ss;
661 /* reply to the request */
662 putpacket (remcomOutBuffer);