2 * Copyright (c) 1995, 1996 Cygnus Support
4 * The authors hereby grant permission to use, copy, modify, distribute,
5 * and license this software and its documentation for any purpose, provided
6 * that existing copyright notices are retained in all copies and that this
7 * notice is included verbatim in any distributions. No written agreement,
8 * license, or royalty fee is required for any of the authorized uses.
9 * Modifications to this software may be copyrighted by their authors
10 * and need not follow the licensing terms described here, provided that
11 * the new terms are clearly indicated on the first page of each file where
16 * A debug packet whose contents are <data> looks like:
18 * $ <data> # CSUM1 CSUM2
20 * <data> must be ASCII alphanumeric and cannot include characters
21 * '$' or '#'. If <data> starts with two characters followed by
22 * ':', then the existing stubs interpret this as a sequence number.
24 * CSUM1 and CSUM2 are ascii hex representation of an 8-bit
25 * checksum of <data>, the most significant nibble is sent first.
26 * the hex digits 0-9,a-f are used.
30 * + - if CSUM is correct and ready for next packet
31 * - - if CSUM is incorrect
33 * <data> is as follows:
34 * Most values are encoded in ascii hex digits.
41 * buffers that hold the packets while they're being constructed.
43 char packet_in_buf
[BUFMAX
];
44 char packet_out_buf
[BUFMAX
];
48 * indicate to caller of mem2hex or hex2mem that there has been an error.
49 * 0 means ok, 1 means error
51 volatile int mem_err
= 0;
54 * 1 means print debugging messages from the target, 0 means be quiet. This is
55 * changed by gdb_debug().
60 * indicate whether the debug vectors ahave been initialized
61 * 0 means not yet, 1 means yep, it's ready.
66 * These variables are instantialted in the GDB stub code.
69 /* this is a list of signal to exception mappings. */
70 extern struct trap_info hard_trap_info
[];
72 /* this is a memory fault exception handler, used by mem2hex & hex2mem */
73 extern void set_mem_fault_trap();
76 * print debugging messages. This uses print, rather than one of the
77 * stdio routines, cause if there are stack or memory problems, the
78 * stdio routines don't work.
79 * params are the debug level, and the string to print
80 * it doesn't return anything.
83 debuglog(int level
, char *msg
)
86 unsigned char buf
[BUFMAX
];
90 if (level
> remote_debug
)
93 if ((level
<0) || (level
> 100)) {
94 print ("ERROR: debug print level out of range");
98 /* convert some characters so it'll look right in the log */
100 for (i
= 0 ; msg
[i
] != '\0'; i
++) {
102 print ("\r\nERROR: Debug message too long\r\n");
104 case '\n': /* newlines */
108 case '\r': /* carriage returns */
112 case '\033': /* escape */
120 case '\b': /* backspace */
124 default: /* no change */
128 if (msg
[i
] < 26) { /* modify control characters */
133 if (msg
[i
] >= 127) { /* modify control characters */
139 *p
= '\0'; /* terminate the string */
145 * convert an ascii hex digit to a number.
146 * param is hex digit.
147 * returns a decimal digit.
150 hex2digit (int digit
)
155 if (digit
>= '0' && digit
<= '9')
157 if (digit
>= 'a' && digit
<= 'f')
158 return digit
- 'a' + 10;
159 if (digit
>= 'A' && digit
<= 'F')
160 return digit
- 'A' + 10;
162 /* shouldn't ever get this far */
167 * convert number NIB to a hex digit.
168 * param is a decimal digit.
169 * returns a hex digit.
177 return 'a' + digit
- 10;
181 * Convert the memory pointed to by mem into hex, placing result in buf.
182 * Return a pointer to the last char put in buf (null), in case of mem fault,
184 * If MAY_FAULT is non-zero, then we will handle memory faults by returning
185 * a 0, else treat a fault like any other fault in the stub.
188 mem2hex(unsigned char *mem
, unsigned char *buf
, int count
, int may_fault
)
192 DEBUG (1, "In mem2hex");
194 set_mem_fault_trap(MAY_FAULT
);
196 while (count
-- > 0) {
199 DEBUG (1, "memory fault in mem2hex");
202 *buf
++ = digit2hex(ch
>> 4);
203 *buf
++ = digit2hex(ch
& 0xf);
208 set_mem_fault_trap(OK
);
214 * Convert the hex array pointed to by buf into binary to be placed in mem
215 * return a pointer to the character AFTER the last byte written
218 hex2mem(unsigned char *buf
, unsigned char *mem
, int count
, int may_fault
)
223 DEBUG (1, "In hex2mem");
225 set_mem_fault_trap(may_fault
);
227 for (i
=0; i
<count
; i
++) {
228 ch
= hex2digit(*buf
++) << 4;
229 ch
|= hex2digit(*buf
++);
235 set_mem_fault_trap(0);
241 * while we find nice hex chars, build an int.
242 * param is a pointer to the string.
243 * returns the int in the param field, and the number of chars processed.
246 hex2int (char **ptr
, int *intValue
)
255 hexValue
= hex2digit(**ptr
);
259 *intValue
= (*intValue
<< 4) | hexValue
;
267 * Scan for the sequence $<data>#<checksum>
270 getpacket(unsigned char *buffer
)
272 unsigned char checksum
;
273 unsigned char xmitcsum
;
279 /* wait around for the start character, ignore all other characters */
280 while ((ch
= (inbyte() & 0x7f)) != '$') ;
287 /* now, read until a # or end of buffer is found */
288 while (count
< BUFMAX
) {
289 ch
= inbyte() & 0x7f;
292 checksum
= checksum
+ ch
;
303 xmitcsum
= hex2digit(inbyte() & 0x7f) << 4;
304 xmitcsum
|= hex2digit(inbyte() & 0x7f);
306 /* Humans shouldn't have to figure out checksums to type to it. */
310 if (checksum
!= xmitcsum
)
311 outbyte('-'); /* failed checksum */
313 outbyte('+'); /* successful transfer */
314 /* if a sequence char is present, reply the sequence ID */
315 if (buffer
[2] == ':') {
318 /* remove sequence chars from buffer */
319 count
= strlen(buffer
);
320 for (i
=3; i
<= count
; i
++)
321 buffer
[i
-3] = buffer
[i
];
326 while (checksum
!= xmitcsum
);
330 * Send the packet in buffer.
333 putpacket(unsigned char *buffer
)
335 unsigned char checksum
;
339 /* $<packet info>#<checksum>. */
345 while (ch
= buffer
[count
]) {
353 outbyte(digit2hex(checksum
>> 4));
354 outbyte(digit2hex(checksum
& 0xf));
357 while ((inbyte() & 0x7f) != '+');
364 gdb_event_loop(int sigval
, unsigned long *registers
)
369 ptr
= packet_out_buf
;
371 DEBUG (1, "In gdb_event_loop");
374 packet_out_buf
[0] = 0;
376 getpacket(packet_in_buf
);
377 ptr
= &packet_in_buf
[1];
379 switch (packet_in_buf
[0]) {
380 case '?': /* get the last known signal */
381 gdb_last_signal(sigval
);
384 case 'd': /* toggle debug messages from the stub */
388 case 'g': /* return the value of the CPU registers */
389 target_read_registers(registers
);
392 case 'G': /* set the value of the CPU registers - return OK */
393 target_write_registers(registers
);
396 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
397 /* Try to read %x,%x. */
398 if (hex2int((char **)&ptr
, &addr
)
400 && hex2int((char **)&ptr
, &length
)) {
401 gdb_read_memory(addr
, length
);
403 make_return_packet(1);
407 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
408 /* Try to read '%x,%x:'. */
409 if (hex2int((char **)&ptr
, &addr
)
411 && hex2int((char **)&ptr
, &length
)
413 gdb_write_memory (addr
, length
, ptr
);
415 make_return_packet(2);
419 case 'c': /* cAA..AA Continue at address AA..AA(optional) */
420 /* try to read optional parameter, pc unchanged if no parm */
421 if (hex2int((char **)&ptr
, &addr
)) {
422 write_pc(registers
, addr
);
426 * we need to flush the instruction cache here, as we may have
427 * deposited a breakpoint, and the icache probably has no way of
428 * knowing that a data ref to some location may have changed
429 * something that is in the instruction cache.
433 /* by returning, we pick up execution where we left off */
436 /* kill the program */
440 case 'r': /* Reset */
445 /* reply to the request */
446 putpacket(packet_out_buf
);
448 DEBUG (1, "Leaving handle_exception()");
451 /* Convert the hardware trap type code to a unix signal number. */
454 computeSignal(int tt
)
456 struct trap_info
*ht
;
458 for (ht
= hard_trap_info
; ht
->tt
&& ht
->signo
; ht
++)
462 return SIGHUP
; /* default for things we don't know about */
466 * Set up exception handlers for tracing and breakpoints
471 struct trap_info
*ht
;
473 DEBUG (1, "Entering set_debug_traps()");
475 if (hard_trap_info
->tt
== 0) {
476 print ("ERROR: ARG#$@%^&*!! no hard trap info!!\r\n");
479 for (ht
= hard_trap_info
; ht
->tt
&& ht
->signo
; ht
++) {
480 exception_handler(ht
->tt
, (unsigned long)default_trap_hook
);
483 /* In case GDB is started before us, ack any packets (presumably
484 "$?#xx") sitting there. */
489 DEBUG (1, "Leaving set_debug_traps()");
493 * make a return packet.
494 * param is the value to return.
495 * 0 = OK, any other value is converted to a two digit hex number.
496 * returns a string or "OK" or "ENN", where NN is the error number. Each N
497 * is an ASCII encoded hex digit.
500 make_return_packet(int val
)
503 packet_out_buf
[0] = 'O';
504 packet_out_buf
[1] = 'K';
505 packet_out_buf
[2] = 0;
507 packet_out_buf
[0] = 'E';
508 packet_out_buf
[1] = digit2hex((val
>> 4) & 0xf);
509 packet_out_buf
[2] = digit2hex(val
& 0xf);
510 packet_out_buf
[3] = 0;
512 return(packet_out_buf
);
516 * g - read registers.
518 * returns a vector of words, size is NUM_REGS.
526 * G - write registers.
527 * param is a vector of words, size is NUM_REGS.
528 * returns an OK or an error number.
531 gdb_write_registers(char *regs
)
537 * params are the address to start the read at and the number of
539 * returns a vector of nbytes or an error number.
540 * Can be fewer bytes than requested if able to read only part of the
544 gdb_read_memory(long addr
, int nbytes
)
546 if (mem2hex((char *)addr
, packet_out_buf
, nbytes
, MAY_FAULT
))
547 return(packet_out_buf
);
549 return(make_return_packet(3));
555 * params are the address to start writing to, the number of
556 * bytes to write, and the new values of the bytes.
557 * returns an OK or an error number.
560 gdb_write_memory(long addr
, int nbytes
, char *mem
)
562 if (hex2mem(mem
, (char *)addr
, nbytes
, MAY_FAULT
))
563 return(make_return_packet(OK
));
565 return(make_return_packet(3));
570 * c - continue at address.
571 * param is the address to start at, and an optional signal. If
572 * sig is zero, then ignore it.
573 * returns an OK or an error number.
576 gdb_continue(int sig
, long addr
)
581 * s - step instruction(s)
582 * param is the address to start at, and an optional signal. If
583 * sig is zero, then ignore it.
584 * returns an OK or an error number.
587 gdb_step(int sig
, long addr
)
594 * returns an OK or an error number.
599 /* generically, we can't do anything for this command */
600 return(make_return_packet(OK
));
606 * returns the last signal number.
609 gdb_last_signal(int val
)
611 DEBUG (1, "Entering gdb_last_signal()");
613 packet_out_buf
[0] = 'S';
614 packet_out_buf
[1] = digit2hex(val
>> 4);
615 packet_out_buf
[2] = digit2hex(val
& 0xf);
616 packet_out_buf
[3] = 0;
618 DEBUG (1, "Leaving gdb_last_signal()");
619 return (packet_out_buf
);
623 * b - change baud rate.
624 * param is the new baudrate
625 * returns the baud rate.
628 gdb_baudrate(int baud
)
630 /* generically, we can't do anything for this command */
631 return(make_return_packet(OK
));
637 * returns the signal number, the registers, the thread ID, and
638 * possible extensions in a vector that looks like:
639 * TAAn...:r...;n...:r...;n...:r...; where:
641 * n... = register number (hex)
642 * r... = register contents
644 * r... = thread process ID. This is a hex integer.
645 * n... = other string not starting with valid hex digit.
646 * gdb should ignore this n,r pair and go on to
647 * the next. This way we can extend the protocol.
655 * D - host requests a detach
657 * returns either a S, T, W, or X command.
658 * returns an OK or an error number.
667 * params are the command to execute and the thread ID.
668 * cmd = 'c' for thread used in step and continue;
669 * cmd = 'g' for thread used in other operations.
670 * tid = -1 for all threads.
671 * tid = zero, pick a thread,any thread.
672 * returns an OK or an error number.
675 gdb_set_thread(int cmd
, int tid
)
677 /* generically, we can't do anything for this command */
678 return(make_return_packet(OK
));
682 * p - read one register.
683 * param is the register number.
684 * returns the register value or ENN.
687 gdb_read_reg(int reg
)
689 /* generically, we can't do anything for this command */
690 return(make_return_packet(OK
));
694 * P - write one register.
695 * params are the register number, and it's new value.
696 * returns the register value or ENN.
699 gdb_write_reg(int reg
, long val
)
701 /* generically, we can't do anything for this command */
703 return(make_return_packet(OK
));
707 * W - process exited.
709 * returns the exit status.
714 /* generically, we can't do anything for this command */
715 return(make_return_packet(OK
));
719 * X - process terminated.
721 * returns the last signal.
730 * params are a vector of bytes, and the number of bytes to encode.
731 * returns a vector of ASCII encoded hex numbers.
734 gdb_hex(char *str
, int nbytes
)
739 * A - tread alive request.
740 * param is the thread ID.
741 * returns an OK or an error number.
744 gdb_thread_alive(int tid
)
746 /* generically, we can't do anything for this command */
747 return(make_return_packet(OK
));
751 * ! - extended protocol.
753 * returns an OK or an error number.
758 /* generically, we can't do anything for this command */
759 return(make_return_packet(OK
));
763 * d - toggle gdb stub diagnostics.
765 * returns an OK or an error number.
770 if (remote_debug
> 0)
775 return(make_return_packet(OK
));
779 * d - toggle gdb stub.
781 * returns an OK or an error number.
786 static int level
= 0;
789 level
= remote_debug
;
792 remote_debug
= level
;
795 return(make_return_packet(OK
));
801 * returns an OK or an error number.
806 /* generically, we can't do anything for this command */
807 return(make_return_packet(OK
));
811 * t - search backwards.
812 * params are the address to start searching from, a pattern to match, and
814 * FIXME: not entirely sure what this is supposed to return.
817 gdb_search(long addr
, long pat
, long mask
)
819 /* generically, we can't do anything for this command */
820 return(make_return_packet(OK
));
824 * q - general get query.
825 * param is a string, that's the query to be executed.
826 * FIXME: not entirely sure what this is supposed to return.
829 gdb_get_query(char *query
)
831 /* generically, we can't do anything for this command */
832 return(make_return_packet(OK
));
836 * Q - general set query
837 * param is a string, that's the query to be executed.
838 * FIXME: not entirely sure what this means.
839 * returns an OK or an error number.
844 /* generically, we can't do anything for this command */
845 return(make_return_packet(OK
));