49e236d4c622a49a7e3337847477482200ff47e9
1 /* Remote debugging interface for Array Tech RAID controller..
2 Copyright 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
5 This module talks to a debug monitor called 'MONITOR', which
6 We communicate with MONITOR via either a direct serial line, or a TCP
7 (or possibly TELNET) stream to a terminal multiplexor,
8 which in turn talks to the target board.
10 This file is part of GDB.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #ifdef ANSI_PROTOTYPES
37 #include <sys/types.h>
38 #include "gdb_string.h"
42 #include "remote-utils.h"
46 static const char hexchars
[]="0123456789abcdef";
47 static char *hex2mem();
50 #define ARRAY_PROMPT ">> "
52 #define SWAP_TARGET_AND_HOST(buffer,len) \
55 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
58 char *p = (char *)(buffer); \
59 char *q = ((char *)(buffer)) + len - 1; \
60 for (; p < q; p++, q--) \
70 static void debuglogs
PARAMS((int, char *, ...));
71 static void array_open();
72 static void array_close();
73 static void array_detach();
74 static void array_attach();
75 static void array_resume();
76 static void array_fetch_register();
77 static void array_store_register();
78 static void array_fetch_registers();
79 static void array_store_registers();
80 static void array_prepare_to_store();
81 static void array_files_info();
82 static void array_kill();
83 static void array_create_inferior();
84 static void array_mourn_inferior();
85 static void make_gdb_packet();
86 static int array_xfer_memory();
87 static int array_wait();
88 static int array_insert_breakpoint();
89 static int array_remove_breakpoint();
92 static int from_hex();
93 static int array_send_packet();
94 static int array_get_packet();
95 static unsigned long ascii2hexword();
96 static char *hexword2ascii();
100 #define LOG_FILE "monitor.log"
101 #if defined (LOG_FILE)
105 static int timeout
= 30;
106 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
107 and i386-stub.c. Normally, no one would notice because it only matters
108 for writing large chunks of memory (e.g. in downloads). Also, this needs
109 to be more than 400 if required to hold the registers (see below, where
110 we round it up based on REGISTER_BYTES). */
114 * Descriptor for I/O to remote machine. Initialize it to NULL so that
115 * array_open knows that we don't have a file open when the program starts.
117 serial_t array_desc
= NULL
;
120 * this array of registers need to match the indexes used by GDB. The
121 * whole reason this exists is cause the various ROM monitors use
122 * different strings than GDB does, and doesn't support all the
123 * registers either. So, typing "info reg sp" becomes a "r30".
125 extern char *tmp_mips_processor_type
;
126 extern int mips_set_processor_type();
128 static struct target_ops array_ops
= {
129 "array", /* to_shortname */
131 "Debug using the standard GDB remote protocol for the Array Tech target.",
133 "Debug using the standard GDB remote protocol for the Array Tech target.\n\
134 Specify the serial device it is connected to (e.g. /dev/ttya).",
135 array_open
, /* to_open */
136 array_close
, /* to_close */
137 NULL
, /* to_attach */
138 array_detach
, /* to_detach */
139 array_resume
, /* to_resume */
140 array_wait
, /* to_wait */
141 array_fetch_registers
, /* to_fetch_registers */
142 array_store_registers
, /* to_store_registers */
143 array_prepare_to_store
, /* to_prepare_to_store */
144 array_xfer_memory
, /* to_xfer_memory */
145 array_files_info
, /* to_files_info */
146 array_insert_breakpoint
, /* to_insert_breakpoint */
147 array_remove_breakpoint
, /* to_remove_breakpoint */
148 0, /* to_terminal_init */
149 0, /* to_terminal_inferior */
150 0, /* to_terminal_ours_for_output */
151 0, /* to_terminal_ours */
152 0, /* to_terminal_info */
153 array_kill
, /* to_kill */
155 0, /* to_lookup_symbol */
156 array_create_inferior
, /* to_create_inferior */
157 array_mourn_inferior
, /* to_mourn_inferior */
159 0, /* to_notice_signals */
160 0, /* to_thread_alive */
162 process_stratum
, /* to_stratum */
164 1, /* to_has_all_memory */
165 1, /* to_has_memory */
166 1, /* to_has_stack */
167 1, /* to_has_registers */
168 1, /* to_has_execution */
170 0, /* sections_end */
171 OPS_MAGIC
/* to_magic */
175 * printf_monitor -- send data to monitor. Works just like printf.
178 #ifdef ANSI_PROTOTYPES
179 printf_monitor(char *pattern
, ...)
181 printf_monitor(va_alist
)
189 #ifdef ANSI_PROTOTYPES
190 va_start(args
, pattern
);
194 pattern
= va_arg(args
, char *);
197 vsprintf(buf
, pattern
, args
);
199 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf
);
201 if (strlen(buf
) > PBUFSIZ
)
202 error ("printf_monitor(): string too long");
203 if (SERIAL_WRITE(array_desc
, buf
, strlen(buf
)))
204 fprintf(stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror(errno
));
207 * write_monitor -- send raw data to monitor.
210 write_monitor(data
, len
)
214 if (SERIAL_WRITE(array_desc
, data
, len
))
215 fprintf(stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror(errno
));
217 *(data
+ len
+1) = '\0';
218 debuglogs (1, "write_monitor(), Sending: \"%s\".", data
);
223 * debuglogs -- deal with debugging info to multiple sources. This takes
224 * two real args, the first one is the level to be compared against
225 * the sr_get_debug() value, the second arg is a printf buffer and args
226 * to be formatted and printed. A CR is added after each string is printed.
229 #ifdef ANSI_PROTOTYPES
230 debuglogs(int level
, char *pattern
, ...)
238 unsigned char buf
[PBUFSIZ
];
239 char newbuf
[PBUFSIZ
];
242 #ifdef ANSI_PROTOTYPES
243 va_start(args
, pattern
);
248 level
= va_arg(args
, int); /* get the debug level */
249 pattern
= va_arg(args
, char *); /* get the printf style pattern */
252 if ((level
<0) || (level
> 100)) {
253 error ("Bad argument passed to debuglogs(), needs debug level");
257 vsprintf(buf
, pattern
, args
); /* format the string */
259 /* convert some characters so it'll look right in the log */
261 for (i
= 0 ; buf
[i
] != '\0'; i
++) {
263 error ("Debug message too long");
265 case '\n': /* newlines */
269 case '\r': /* carriage returns */
273 case '\033': /* escape */
281 case '\b': /* backspace */
285 default: /* no change */
289 if (buf
[i
] < 26) { /* modify control characters */
294 if (buf
[i
] >= 128) { /* modify control characters */
300 *p
= '\0'; /* terminate the string */
302 if (sr_get_debug() > level
)
303 printf_unfiltered ("%s\n", newbuf
);
305 #ifdef LOG_FILE /* write to the monitor log */
306 if (log_file
!= 0x0) {
307 fputs (newbuf
, log_file
);
308 fputc ('\n', log_file
);
314 /* readchar -- read a character from the remote system, doing all the fancy
323 c
= SERIAL_READCHAR(array_desc
, abs(timeout
));
325 if (sr_get_debug() > 5) {
327 debuglogs (5, "readchar: timeout = %d\n", timeout
);
332 putc(c
& 0x7f, log_file
);
338 if (c
== SERIAL_TIMEOUT
) {
340 return c
; /* Polls shouldn't generate timeout errors */
341 error("Timeout reading from remote system.");
343 fputs ("ERROR: Timeout reading from remote system", log_file
);
346 perror_with_name("readchar");
350 * expect -- scan input from the remote system, until STRING is found.
351 * If DISCARD is non-zero, then discard non-matching input, else print
352 * it out. Let the user break out immediately.
355 expect (string
, discard
)
363 debuglogs (1, "Expecting \"%s\".", string
);
367 c
= readchar(timeout
);
373 debuglogs (4, "Matched");
378 fputc_unfiltered (c
, gdb_stdout
);
385 /* Keep discarding input until we see the MONITOR array_cmds->prompt.
387 The convention for dealing with the expect_prompt is that you
389 o *then* wait for the expect_prompt.
391 Thus the last thing that a procedure does with the serial line
392 will be an expect_prompt(). Exception: array_resume does not
393 wait for the expect_prompt, because the terminal is being handed over
394 to the inferior. However, the next thing which happens after that
395 is a array_wait which does wait for the expect_prompt.
396 Note that this includes abnormal exit, e.g. error(). This is
397 necessary to prevent getting into states from which we can't
400 expect_prompt(discard
)
403 expect (ARRAY_PROMPT
, discard
);
407 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
420 if (sr_get_debug() > 5)
421 debuglogs (5, "Ignoring \'%c\'.", ch
);
424 if (sr_get_debug() > 5)
425 debuglogs (5, "Accepting \'%c\'.", ch
);
431 * get_hex_digit -- Get a hex digit from the remote system & return its value.
432 * If ignore is nonzero, ignore spaces, newline & tabs.
435 get_hex_digit(ignore
)
440 ch
= readchar(timeout
);
443 if (sr_get_debug() > 4) {
444 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch
, ch
);
446 #ifdef LOG_FILE /* write to the monitor log */
447 if (log_file
!= 0x0) {
448 fputs ("get_hex_digit() got a 0x", log_file
);
449 fputc (ch
, log_file
);
450 fputc ('\n', log_file
);
456 if (ch
>= '0' && ch
<= '9')
458 else if (ch
>= 'A' && ch
<= 'F')
459 return ch
- 'A' + 10;
460 else if (ch
>= 'a' && ch
<= 'f')
461 return ch
- 'a' + 10;
462 else if (ch
== ' ' && ignore
)
466 debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch
);
467 error("Invalid hex digit from remote system. (0x%x)", ch
);
472 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
473 * Accept any number leading spaces.
481 val
= get_hex_digit (1) << 4;
482 debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val
);
484 val
|= get_hex_digit (0);
485 debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val
);
488 debuglogs (4, "get_hex_byte() -- Read a 0x%x", val
);
492 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
493 * and put them in registers starting at REGNO.
504 if (HOST_BYTE_ORDER
== BIG_ENDIAN
) {
506 for (i
= 0; i
< 8; i
++)
507 val
= (val
<< 4) + get_hex_digit (i
== 0);
510 for (i
= 7; i
>= 0; i
--)
511 val
= (val
<< 4) + get_hex_digit (i
== 0);
515 debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val
, (HOST_BYTE_ORDER
== BIG_ENDIAN
) ? "big endian" : "little endian");
520 /* This is called not only when we first attach, but also when the
521 user types "run" after having attached. */
523 array_create_inferior (execfile
, args
, env
)
531 error("Can't pass arguments to remote MONITOR process");
533 if (execfile
== 0 || exec_bfd
== 0)
534 error("No exec file specified");
536 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
538 /* The "process" (board) is already stopped awaiting our commands, and
539 the program is already downloaded. We just set its PC and go. */
541 clear_proceed_status ();
543 /* Tell wait_for_inferior that we've started a new process. */
544 init_wait_for_inferior ();
546 /* Set up the "saved terminal modes" of the inferior
547 based on what modes we are starting it with. */
548 target_terminal_init ();
550 /* Install inferior's terminal modes. */
551 target_terminal_inferior ();
553 /* insert_step_breakpoint (); FIXME, do we need this? */
556 proceed ((CORE_ADDR
)entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
560 * array_open -- open a connection to a remote debugger.
561 * NAME is the filename used for communication.
563 static int baudrate
= 9600;
564 static char dev_name
[100];
567 array_open(args
, name
, from_tty
)
572 char packet
[PBUFSIZ
];
575 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
576 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
581 target_preopen (from_tty
);
582 unpush_target (&array_ops
);
584 tmp_mips_processor_type
= "lsi33k"; /* change the default from r3051 */
585 mips_set_processor_type_command ("lsi33k", 0);
587 strcpy(dev_name
, args
);
588 array_desc
= SERIAL_OPEN(dev_name
);
590 if (array_desc
== NULL
)
591 perror_with_name(dev_name
);
593 if (baud_rate
!= -1) {
594 if (SERIAL_SETBAUDRATE (array_desc
, baud_rate
)) {
595 SERIAL_CLOSE (array_desc
);
596 perror_with_name (name
);
600 SERIAL_RAW(array_desc
);
602 #if defined (LOG_FILE)
603 log_file
= fopen (LOG_FILE
, "w");
604 if (log_file
== NULL
)
605 perror_with_name (LOG_FILE
);
606 fprintf_filtered (log_file
, "GDB %s (%s", version
);
607 fprintf_filtered (log_file
, " --target %s)\n", array_ops
.to_shortname
);
608 fprintf_filtered (log_file
, "Remote target %s connected to %s\n\n", array_ops
.to_shortname
, dev_name
);
611 /* see if the target is alive. For a ROM monitor, we can just try to force the
612 expect_prompt to print a few times. For the GDB remote protocol, the application
613 being debugged is sitting at a breakpoint and waiting for GDB to initialize
614 the connection. We force it to give us an empty packet to see if it's alive.
616 debuglogs (3, "Trying to ACK the target's debug stub");
617 /* unless your are on the new hardware, the old board won't initialize
618 because the '@' doesn't flush output like it does on the new ROMS.
620 printf_monitor ("@"); /* ask for the last signal */
621 expect_prompt(1); /* See if we get a expect_prompt */
622 #ifdef TEST_ARRAY /* skip packet for testing */
623 make_gdb_packet (packet
, "?"); /* ask for a bogus packet */
624 if (array_send_packet (packet
) == 0)
625 error ("Couldn't transmit packet\n");
626 printf_monitor ("@\n"); /* force it to flush stdout */
627 expect_prompt(1); /* See if we get a expect_prompt */
629 push_target (&array_ops
);
631 printf("Remote target %s connected to %s\n", array_ops
.to_shortname
, dev_name
);
635 * array_close -- Close out all files and local state before this
636 * target loses control.
640 array_close (quitting
)
643 SERIAL_CLOSE(array_desc
);
646 debuglogs (1, "array_close (quitting=%d)", quitting
);
648 #if defined (LOG_FILE)
650 if (ferror(log_file
))
651 printf_filtered ("Error writing log file.\n");
652 if (fclose(log_file
) != 0)
653 printf_filtered ("Error closing log file.\n");
659 * array_detach -- terminate the open connection to the remote
660 * debugger. Use this when you want to detach and do something
661 * else with your gdb.
664 array_detach (from_tty
)
668 debuglogs (1, "array_detach ()");
670 pop_target(); /* calls array_close to do the real work */
672 printf ("Ending remote %s debugging\n", target_shortname
);
676 * array_attach -- attach GDB to the target.
679 array_attach (args
, from_tty
)
684 printf ("Starting remote %s debugging\n", target_shortname
);
686 debuglogs (1, "array_attach (args=%s)", args
);
688 printf_monitor ("go %x\n");
689 /* swallow the echo. */
690 expect ("go %x\n", 1);
694 * array_resume -- Tell the remote machine to resume.
697 array_resume (pid
, step
, sig
)
699 enum target_signal sig
;
701 debuglogs (1, "array_resume (step=%d, sig=%d)", step
, sig
);
704 printf_monitor ("s\n");
706 printf_monitor ("go");
713 * array_wait -- Wait until the remote machine stops, then return,
714 * storing status in status just as `wait' would.
717 array_wait (pid
, status
)
719 struct target_waitstatus
*status
;
721 int old_timeout
= timeout
;
725 serial_ttystate ttystate
;
727 debuglogs(1, "array_wait (), printing extraneous text.");
729 status
->kind
= TARGET_WAITKIND_EXITED
;
730 status
->value
.integer
= 0;
732 timeout
= 0; /* Don't time out -- user program is running. */
734 #if !defined(__GO32__) && !defined(__MSDOS__) && !defined(__WIN32__)
735 tty_desc
= SERIAL_FDOPEN (0);
736 ttystate
= SERIAL_GET_TTY_STATE (tty_desc
);
737 SERIAL_RAW (tty_desc
);
740 /* poll on the serial port and the keyboard. */
742 c
= readchar(timeout
);
744 if (c
== *(ARRAY_PROMPT
+ i
)) {
745 if (++i
>= strlen (ARRAY_PROMPT
)) { /* matched the prompt */
746 debuglogs (4, "array_wait(), got the expect_prompt.");
749 } else { /* not the prompt */
752 fputc_unfiltered (c
, gdb_stdout
);
755 c
= SERIAL_READCHAR(tty_desc
, timeout
);
757 SERIAL_WRITE(array_desc
, &c
, 1);
758 /* do this so it looks like there's keyboard echo */
759 if (c
== 3) /* exit on Control-C */
761 fputc_unfiltered (c
, gdb_stdout
);
765 SERIAL_SET_TTY_STATE (tty_desc
, ttystate
);
768 debuglogs (4, "array_wait(), got the expect_prompt.");
771 status
->kind
= TARGET_WAITKIND_STOPPED
;
772 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
774 timeout
= old_timeout
;
780 * array_fetch_registers -- read the remote registers into the
784 array_fetch_registers (ignored
)
789 unsigned char packet
[PBUFSIZ
];
790 char regs
[REGISTER_BYTES
];
792 debuglogs (1, "array_fetch_registers (ignored=%d)\n", ignored
);
794 memset (packet
, 0, PBUFSIZ
);
795 /* Unimplemented registers read as all bits zero. */
796 memset (regs
, 0, REGISTER_BYTES
);
797 make_gdb_packet (packet
, "g");
798 if (array_send_packet (packet
) == 0)
799 error ("Couldn't transmit packet\n");
800 if (array_get_packet (packet
) == 0)
801 error ("Couldn't receive packet\n");
802 /* FIXME: read bytes from packet */
803 debuglogs (4, "array_fetch_registers: Got a \"%s\" back\n", packet
);
804 for (regno
= 0; regno
<= PC_REGNUM
+4; regno
++) {
805 /* supply register stores in target byte order, so swap here */
806 /* FIXME: convert from ASCII hex to raw bytes */
807 i
= ascii2hexword (packet
+ (regno
* 8));
808 debuglogs (5, "Adding register %d = %x\n", regno
, i
);
809 SWAP_TARGET_AND_HOST (&i
, 4);
810 supply_register (regno
, (char *)&i
);
815 * This is unused by targets like this one that use a
816 * protocol based on GDB's remote protocol.
819 array_fetch_register (ignored
)
822 array_fetch_registers ();
826 * Get all the registers from the targets. They come back in a large array.
829 array_store_registers (ignored
)
834 char packet
[PBUFSIZ
];
838 debuglogs (1, "array_store_registers()");
840 memset (packet
, 0, PBUFSIZ
);
841 memset (buf
, 0, PBUFSIZ
);
844 /* Unimplemented registers read as all bits zero. */
845 /* FIXME: read bytes from packet */
846 for (regno
= 0; regno
< 41; regno
++) { /* FIXME */
847 /* supply register stores in target byte order, so swap here */
848 /* FIXME: convert from ASCII hex to raw bytes */
849 i
= (unsigned long)read_register (regno
);
850 hexword2ascii (num
, i
);
851 strcpy (buf
+(regno
* 8)+1, num
);
853 *(buf
+ (regno
* 8) + 2) = 0;
854 make_gdb_packet (packet
, buf
);
855 if (array_send_packet (packet
) == 0)
856 error ("Couldn't transmit packet\n");
857 if (array_get_packet (packet
) == 0)
858 error ("Couldn't receive packet\n");
860 registers_changed ();
864 * This is unused by targets like this one that use a
865 * protocol based on GDB's remote protocol.
868 array_store_register (ignored
)
871 array_store_registers ();
874 /* Get ready to modify the registers array. On machines which store
875 individual registers, this doesn't need to do anything. On machines
876 which store all the registers in one fell swoop, this makes sure
877 that registers contains all the registers from the program being
881 array_prepare_to_store ()
883 /* Do nothing, since we can store individual regs */
889 printf ("\tAttached to %s at %d baud.\n",
894 * array_write_inferior_memory -- Copy LEN bytes of data from debugger
895 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
898 array_write_inferior_memory (memaddr
, myaddr
, len
)
900 unsigned char *myaddr
;
905 char packet
[PBUFSIZ
];
910 debuglogs (1, "array_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
911 memset (buf
, '\0', PBUFSIZ
); /* this also sets the string terminator */
914 *p
++ = 'M'; /* The command to write memory */
915 hexword2ascii (num
, memaddr
); /* convert the address */
916 strcpy (p
, num
); /* copy the address */
918 *p
++ = ','; /* add comma delimeter */
919 hexword2ascii (num
, len
); /* Get the length as a 4 digit number */
924 *p
++ = ':'; /* add the colon delimeter */
925 for (j
= 0; j
< len
; j
++) { /* copy the data in after converting it */
926 *p
++ = tohex ((myaddr
[j
] >> 4) & 0xf);
927 *p
++ = tohex (myaddr
[j
] & 0xf);
930 make_gdb_packet (packet
, buf
);
931 if (array_send_packet (packet
) == 0)
932 error ("Couldn't transmit packet\n");
933 if (array_get_packet (packet
) == 0)
934 error ("Couldn't receive packet\n");
940 * array_read_inferior_memory -- read LEN bytes from inferior memory
941 * at MEMADDR. Put the result at debugger address MYADDR. Returns
945 array_read_inferior_memory(memaddr
, myaddr
, len
)
952 char packet
[PBUFSIZ
];
954 /* Number of bytes read so far. */
957 /* Starting address of this pass. */
958 unsigned long startaddr
;
960 /* Starting address of this pass. */
961 unsigned long endaddr
;
963 /* Number of bytes to read in this pass. */
966 debuglogs (1, "array_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
968 /* Note that this code works correctly if startaddr is just less
969 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
970 thing). That is, something like
971 array_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
972 works--it never adds len To memaddr and gets 0. */
973 /* However, something like
974 array_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
975 doesn't need to work. Detect it and give up if there's an attempt
977 if (((memaddr
- 1) + len
) < memaddr
) {
984 while (count
< len
) {
986 if ((startaddr
% 16) != 0)
987 len_this_pass
-= startaddr
% 16;
988 if (len_this_pass
> (len
- count
))
989 len_this_pass
= (len
- count
);
991 debuglogs (3, "Display %d bytes at %x for Big Endian host", len_this_pass
, startaddr
);
993 for (i
= 0; i
< len_this_pass
; i
++) {
994 sprintf (buf
, "m%08x,%04x", startaddr
, len_this_pass
);
995 make_gdb_packet (packet
, buf
);
996 if (array_send_packet (packet
) == 0)
997 error ("Couldn't transmit packet\n");
998 if (array_get_packet (packet
) == 0)
999 error ("Couldn't receive packet\n");
1001 error ("Got no data in the GDB packet\n");
1002 debuglogs (4, "array_read_inferior: Got a \"%s\" back\n", packet
);
1003 for (j
= 0; j
< len_this_pass
; j
++) { /* extract the byte values */
1004 myaddr
[count
++] = from_hex (*(packet
+(j
*2))) * 16 + from_hex (*(packet
+(j
*2)+1));
1005 debuglogs (5, "myaddr set to %x\n", myaddr
[count
-1]);
1014 /* FIXME-someday! merge these two. */
1016 array_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
1021 struct target_ops
*target
; /* ignored */
1024 return array_write_inferior_memory (memaddr
, myaddr
, len
);
1026 return array_read_inferior_memory (memaddr
, myaddr
, len
);
1030 array_kill (args
, from_tty
)
1034 return; /* ignore attempts to kill target system */
1037 /* Clean up when a program exits.
1038 The program actually lives on in the remote processor's RAM, and may be
1039 run again without a download. Don't leave it full of breakpoint
1043 array_mourn_inferior ()
1045 remove_breakpoints ();
1046 generic_mourn_inferior (); /* Do all the proper things now */
1049 #define MAX_ARRAY_BREAKPOINTS 16
1051 extern int memory_breakpoint_size
;
1052 static CORE_ADDR breakaddr
[MAX_ARRAY_BREAKPOINTS
] = {0};
1055 * array_insert_breakpoint -- add a breakpoint
1058 array_insert_breakpoint (addr
, shadow
)
1064 debuglogs (1, "array_insert_breakpoint() addr = 0x%x", addr
);
1066 for (i
= 0; i
<= MAX_ARRAY_BREAKPOINTS
; i
++) {
1067 if (breakaddr
[i
] == 0) {
1068 breakaddr
[i
] = addr
;
1069 if (sr_get_debug() > 4)
1070 printf ("Breakpoint at %x\n", addr
);
1071 array_read_inferior_memory(addr
, shadow
, memory_breakpoint_size
);
1072 printf_monitor("b 0x%x\n", addr
);
1078 fprintf(stderr
, "Too many breakpoints (> 16) for monitor\n");
1083 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1086 array_remove_breakpoint (addr
, shadow
)
1092 debuglogs (1, "array_remove_breakpoint() addr = 0x%x", addr
);
1094 for (i
= 0; i
< MAX_ARRAY_BREAKPOINTS
; i
++) {
1095 if (breakaddr
[i
] == addr
) {
1097 /* some monitors remove breakpoints based on the address */
1098 printf_monitor("bd %x\n", i
);
1103 fprintf(stderr
, "Can't find breakpoint associated with 0x%x\n", addr
);
1110 debuglogs (1, "array_stop()");
1111 printf_monitor("\003");
1116 * array_command -- put a command string, in args, out to MONITOR.
1117 * Output from MONITOR is placed on the users terminal until the
1118 * expect_prompt is seen. FIXME
1121 monitor_command (args
, fromtty
)
1125 debuglogs (1, "monitor_command (args=%s)", args
);
1127 if (array_desc
== NULL
)
1128 error("monitor target not open.");
1131 error("Missing command.");
1133 printf_monitor ("%s\n", args
);
1138 * make_gdb_packet -- make a GDB packet. The data is always ASCII.
1139 * A debug packet whose contents are <data>
1140 * is encapsulated for transmission in the form:
1142 * $ <data> # CSUM1 CSUM2
1144 * <data> must be ASCII alphanumeric and cannot include characters
1145 * '$' or '#'. If <data> starts with two characters followed by
1146 * ':', then the existing stubs interpret this as a sequence number.
1148 * CSUM1 and CSUM2 are ascii hex representation of an 8-bit
1149 * checksum of <data>, the most significant nibble is sent first.
1150 * the hex digits 0-9,a-f are used.
1154 make_gdb_packet (buf
, data
)
1158 unsigned char csum
= 0;
1162 debuglogs (3, "make_gdb_packet(%s)\n", data
);
1163 cnt
= strlen (data
);
1165 error ("make_gdb_packet(): to much data\n");
1167 /* start with the packet header */
1171 /* calculate the checksum */
1172 for (i
= 0; i
< cnt
; i
++) {
1177 /* terminate the data with a '#' */
1180 /* add the checksum as two ascii digits */
1181 *p
++ = tohex ((csum
>> 4) & 0xf);
1182 *p
++ = tohex (csum
& 0xf);
1183 *p
= 0x0; /* Null terminator on string */
1187 * array_send_packet -- send a GDB packet to the target with error handling. We
1188 * get a '+' (ACK) back if the packet is received and the checksum
1189 * matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a
1190 * successful transmition, or a 0 for a failure.
1193 array_send_packet (packet
)
1202 /* scan the packet to make sure it only contains valid characters.
1203 this may sound silly, but sometimes a garbled packet will hang
1204 the target board. We scan the whole thing, then print the error
1207 for (i
= 0; i
< strlen(packet
); i
++) {
1208 debuglogs (5, "array_send_packet(): Scanning \'%c\'\n", packet
[i
]);
1209 /* legit hex numbers or command */
1210 if ((isxdigit(packet
[i
])) || (isalpha(packet
[i
])))
1212 switch (packet
[i
]) {
1215 case '#': /* end of packet */
1216 case '$': /* start of packet */
1218 default: /* bogus character */
1220 debuglogs (4, "array_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet
[i
]);
1226 error ("Can't send packet, found %d non-ascii characters", retries
);
1228 /* ok, try to send the packet */
1230 while (retries
++ <= 10) {
1231 printf_monitor ("%s", packet
);
1233 /* read until either a timeout occurs (-2) or '+' is read */
1234 while (retries
<= 10) {
1235 c
= readchar (-timeout
);
1236 debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c
);
1239 debuglogs (3, "Got Ack\n");
1241 case SERIAL_TIMEOUT
:
1242 debuglogs (3, "Timed out reading serial port\n");
1243 printf_monitor("@"); /* resync with the monitor */
1244 expect_prompt(1); /* See if we get a expect_prompt */
1245 break; /* Retransmit buffer */
1247 debuglogs (3, "Got NAK\n");
1248 printf_monitor("@"); /* resync with the monitor */
1249 expect_prompt(1); /* See if we get a expect_prompt */
1252 /* it's probably an old response, or the echo of our command.
1253 * just gobble up the packet and ignore it.
1255 debuglogs (3, "Got a junk packet\n");
1258 c
= readchar (timeout
);
1261 c
= readchar (timeout
);
1263 c
= readchar (timeout
);
1266 debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk
);
1267 continue; /* Now, go look for next packet */
1272 debuglogs (3, "Retransmitting packet \"%s\"\n", packet
);
1273 break; /* Here to retransmit */
1280 * array_get_packet -- get a GDB packet from the target. Basically we read till we
1281 * see a '#', then check the checksum. It returns a 1 if it's gotten a
1282 * packet, or a 0 it the packet wasn't transmitted correctly.
1285 array_get_packet (packet
)
1291 unsigned char pktcsum
;
1297 memset (packet
, 1, PBUFSIZ
);
1299 while (retries
<= 10) {
1301 c
= readchar (timeout
);
1302 if (c
== SERIAL_TIMEOUT
) {
1303 debuglogs (3, "array_get_packet: got time out from serial port.\n");
1305 debuglogs (3, "Waiting for a '$', got a %c\n", c
);
1309 while (retries
<= 10) {
1310 c
= readchar (timeout
);
1311 debuglogs (3, "array_get_packet: got a '%c'\n", c
);
1313 case SERIAL_TIMEOUT
:
1314 debuglogs (3, "Timeout in mid-packet, retrying\n");
1317 debuglogs (3, "Saw new packet start in middle of old one\n");
1318 return 0; /* Start a new packet, count retries */
1321 pktcsum
= from_hex (readchar (timeout
)) << 4;
1322 pktcsum
|= from_hex (readchar (timeout
));
1324 debuglogs (3, "\nGDB packet checksum zero, must be a bogus packet\n");
1325 if (csum
== pktcsum
) {
1326 debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet
);
1327 printf_monitor ("@");
1331 debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum
, csum
);
1333 case '*': /* Run length encoding */
1334 debuglogs (5, "Run length encoding in packet\n");
1336 c
= readchar (timeout
);
1338 c
= c
- ' ' + 3; /* Compute repeat count */
1340 if (c
> 0 && c
< 255 && bp
+ c
- 1 < packet
+ PBUFSIZ
- 1) {
1341 memset (bp
, *(bp
- 1), c
);
1346 printf_filtered ("Repeat count %d too large for buffer.\n", c
);
1350 if ((!isxdigit(c
)) && (!ispunct(c
)))
1351 debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c
);
1352 if (bp
< packet
+ PBUFSIZ
- 1) {
1359 puts_filtered ("Remote packet too long.\n");
1367 * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
1369 static unsigned long
1378 for (i
= 0; i
< 8; i
++) {
1380 if (mem
[i
] >= 'A' && mem
[i
] <= 'F')
1381 val
= val
+ mem
[i
] - 'A' + 10;
1382 if (mem
[i
] >= 'a' && mem
[i
] <= 'f')
1383 val
= val
+ mem
[i
] - 'a' + 10;
1384 if (mem
[i
] >= '0' && mem
[i
] <= '9')
1385 val
= val
+ mem
[i
] - '0';
1389 debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val
, buf
, mem
);
1394 * ascii2hexword -- convert a hex value to an ascii number represented by 8
1398 hexword2ascii (mem
, num
)
1405 debuglogs (4, "hexword2ascii() converting %x ", num
);
1406 for (i
= 7; i
>= 0; i
--) {
1407 mem
[i
] = tohex ((num
>> 4) & 0xf);
1408 mem
[i
] = tohex (num
& 0xf);
1412 debuglogs (4, "\tto a %s", mem
);
1415 /* Convert hex digit A to a number. */
1423 debuglogs (4, "from_hex got a 0x%x(%c)\n",a
,a
);
1424 if (a
>= '0' && a
<= '9')
1426 if (a
>= 'a' && a
<= 'f')
1427 return a
- 'a' + 10;
1428 if (a
>= 'A' && a
<= 'F')
1429 return a
- 'A' + 10;
1431 error ("Reply contains invalid hex digit 0x%x", a
);
1435 /* Convert number NIB to a hex digit. */
1447 * _initialize_remote_monitors -- setup a few addtitional commands that
1448 * are usually only used by monitors.
1451 _initialize_remote_monitors ()
1453 /* generic monitor command */
1454 add_com ("monitor", class_obscure
, monitor_command
,
1455 "Send a command to the debug monitor.");
1460 * _initialize_array -- do any special init stuff for the target.
1463 _initialize_array ()
1465 add_target (&array_ops
);
This page took 0.061383 seconds and 4 git commands to generate.