1 /* Remote debugging interface for Array Tech RAID controller..
2 Copyright 90, 91, 92, 93, 94, 1995, 1998 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,
25 Boston, MA 02111-1307, USA.
34 #include <sys/types.h>
35 #include "gdb_string.h"
39 #include "remote-utils.h"
45 #define ARRAY_PROMPT ">> "
47 #define SWAP_TARGET_AND_HOST(buffer,len) \
50 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
53 char *p = (char *)(buffer); \
54 char *q = ((char *)(buffer)) + len - 1; \
55 for (; p < q; p++, q--) \
65 static void debuglogs (int, char *, ...);
66 static void array_open ();
67 static void array_close ();
68 static void array_detach ();
69 static void array_attach ();
70 static void array_resume ();
71 static void array_fetch_register ();
72 static void array_store_register ();
73 static void array_fetch_registers ();
74 static void array_store_registers ();
75 static void array_prepare_to_store ();
76 static void array_files_info ();
77 static void array_kill ();
78 static void array_create_inferior ();
79 static void array_mourn_inferior ();
80 static void make_gdb_packet ();
81 static int array_xfer_memory ();
82 static int array_wait ();
83 static int array_insert_breakpoint ();
84 static int array_remove_breakpoint ();
87 static int from_hex ();
88 static int array_send_packet ();
89 static int array_get_packet ();
90 static unsigned long ascii2hexword ();
91 static void hexword2ascii ();
93 #define LOG_FILE "monitor.log"
94 #if defined (LOG_FILE)
98 static int timeout = 30;
99 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
100 and i386-stub.c. Normally, no one would notice because it only matters
101 for writing large chunks of memory (e.g. in downloads). Also, this needs
102 to be more than 400 if required to hold the registers (see below, where
103 we round it up based on REGISTER_BYTES). */
107 * Descriptor for I/O to remote machine. Initialize it to NULL so that
108 * array_open knows that we don't have a file open when the program starts.
110 serial_t array_desc = NULL;
113 * this array of registers need to match the indexes used by GDB. The
114 * whole reason this exists is cause the various ROM monitors use
115 * different strings than GDB does, and doesn't support all the
116 * registers either. So, typing "info reg sp" becomes a "r30".
118 extern char *tmp_mips_processor_type;
119 extern int mips_set_processor_type ();
121 static struct target_ops array_ops;
124 init_array_ops (void)
126 array_ops.to_shortname = "array";
127 array_ops.to_longname =
128 "Debug using the standard GDB remote protocol for the Array Tech target.",
130 "Debug using the standard GDB remote protocol for the Array Tech target.\n\
131 Specify the serial device it is connected to (e.g. /dev/ttya).";
132 array_ops.to_open = array_open;
133 array_ops.to_close = array_close;
134 array_ops.to_attach = NULL;
135 array_ops.to_post_attach = NULL;
136 array_ops.to_require_attach = NULL;
137 array_ops.to_detach = array_detach;
138 array_ops.to_require_detach = NULL;
139 array_ops.to_resume = array_resume;
140 array_ops.to_wait = array_wait;
141 array_ops.to_post_wait = NULL;
142 array_ops.to_fetch_registers = array_fetch_registers;
143 array_ops.to_store_registers = array_store_registers;
144 array_ops.to_prepare_to_store = array_prepare_to_store;
145 array_ops.to_xfer_memory = array_xfer_memory;
146 array_ops.to_files_info = array_files_info;
147 array_ops.to_insert_breakpoint = array_insert_breakpoint;
148 array_ops.to_remove_breakpoint = array_remove_breakpoint;
149 array_ops.to_terminal_init = 0;
150 array_ops.to_terminal_inferior = 0;
151 array_ops.to_terminal_ours_for_output = 0;
152 array_ops.to_terminal_ours = 0;
153 array_ops.to_terminal_info = 0;
154 array_ops.to_kill = array_kill;
155 array_ops.to_load = 0;
156 array_ops.to_lookup_symbol = 0;
157 array_ops.to_create_inferior = array_create_inferior;
158 array_ops.to_post_startup_inferior = NULL;
159 array_ops.to_acknowledge_created_inferior = NULL;
160 array_ops.to_clone_and_follow_inferior = NULL;
161 array_ops.to_post_follow_inferior_by_clone = NULL;
162 array_ops.to_insert_fork_catchpoint = NULL;
163 array_ops.to_remove_fork_catchpoint = NULL;
164 array_ops.to_insert_vfork_catchpoint = NULL;
165 array_ops.to_remove_vfork_catchpoint = NULL;
166 array_ops.to_has_forked = NULL;
167 array_ops.to_has_vforked = NULL;
168 array_ops.to_can_follow_vfork_prior_to_exec = NULL;
169 array_ops.to_post_follow_vfork = NULL;
170 array_ops.to_insert_exec_catchpoint = NULL;
171 array_ops.to_remove_exec_catchpoint = NULL;
172 array_ops.to_has_execd = NULL;
173 array_ops.to_reported_exec_events_per_exec_call = NULL;
174 array_ops.to_has_exited = NULL;
175 array_ops.to_mourn_inferior = array_mourn_inferior;
176 array_ops.to_can_run = 0;
177 array_ops.to_notice_signals = 0;
178 array_ops.to_thread_alive = 0;
179 array_ops.to_stop = 0;
180 array_ops.to_pid_to_exec_file = NULL;
181 array_ops.to_core_file_to_sym_file = NULL;
182 array_ops.to_stratum = process_stratum;
183 array_ops.DONT_USE = 0;
184 array_ops.to_has_all_memory = 1;
185 array_ops.to_has_memory = 1;
186 array_ops.to_has_stack = 1;
187 array_ops.to_has_registers = 1;
188 array_ops.to_has_execution = 1;
189 array_ops.to_sections = 0;
190 array_ops.to_sections_end = 0;
191 array_ops.to_magic = OPS_MAGIC;
195 * printf_monitor -- send data to monitor. Works just like printf.
198 printf_monitor (char *pattern,...)
204 va_start (args, pattern);
206 vsprintf (buf, pattern, args);
208 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);
210 if (strlen (buf) > PBUFSIZ)
211 error ("printf_monitor(): string too long");
212 if (SERIAL_WRITE (array_desc, buf, strlen (buf)))
213 fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
216 * write_monitor -- send raw data to monitor.
219 write_monitor (data, len)
223 if (SERIAL_WRITE (array_desc, data, len))
224 fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
226 *(data + len + 1) = '\0';
227 debuglogs (1, "write_monitor(), Sending: \"%s\".", data);
232 * debuglogs -- deal with debugging info to multiple sources. This takes
233 * two real args, the first one is the level to be compared against
234 * the sr_get_debug() value, the second arg is a printf buffer and args
235 * to be formatted and printed. A CR is added after each string is printed.
238 debuglogs (int level, char *pattern,...)
242 unsigned char buf[PBUFSIZ];
243 char newbuf[PBUFSIZ];
246 va_start (args, pattern);
248 if ((level < 0) || (level > 100))
250 error ("Bad argument passed to debuglogs(), needs debug level");
254 vsprintf (buf, pattern, args); /* format the string */
256 /* convert some characters so it'll look right in the log */
258 for (i = 0; buf[i] != '\0'; i++)
261 error ("Debug message too long");
264 case '\n': /* newlines */
268 case '\r': /* carriage returns */
272 case '\033': /* escape */
280 case '\b': /* backspace */
284 default: /* no change */
289 { /* modify control characters */
295 { /* modify control characters */
301 *p = '\0'; /* terminate the string */
303 if (sr_get_debug () > level)
304 printf_unfiltered ("%s\n", newbuf);
306 #ifdef LOG_FILE /* write to the monitor log */
309 fputs (newbuf, log_file);
310 fputc ('\n', log_file);
316 /* readchar -- read a character from the remote system, doing all the fancy
325 c = SERIAL_READCHAR (array_desc, abs (timeout));
327 if (sr_get_debug () > 5)
330 debuglogs (5, "readchar: timeout = %d\n", timeout);
335 putc (c & 0x7f, log_file);
341 if (c == SERIAL_TIMEOUT)
344 return c; /* Polls shouldn't generate timeout errors */
345 error ("Timeout reading from remote system.");
347 fputs ("ERROR: Timeout reading from remote system", log_file);
350 perror_with_name ("readchar");
354 * expect -- scan input from the remote system, until STRING is found.
355 * If DISCARD is non-zero, then discard non-matching input, else print
356 * it out. Let the user break out immediately.
359 expect (string, discard)
367 debuglogs (1, "Expecting \"%s\".", string);
372 c = readchar (timeout);
380 debuglogs (4, "Matched");
388 fputc_unfiltered (c, gdb_stdout);
395 /* Keep discarding input until we see the MONITOR array_cmds->prompt.
397 The convention for dealing with the expect_prompt is that you
399 o *then* wait for the expect_prompt.
401 Thus the last thing that a procedure does with the serial line
402 will be an expect_prompt(). Exception: array_resume does not
403 wait for the expect_prompt, because the terminal is being handed over
404 to the inferior. However, the next thing which happens after that
405 is a array_wait which does wait for the expect_prompt.
406 Note that this includes abnormal exit, e.g. error(). This is
407 necessary to prevent getting into states from which we can't
410 expect_prompt (discard)
413 expect (ARRAY_PROMPT, discard);
417 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
431 if (sr_get_debug () > 5)
432 debuglogs (5, "Ignoring \'%c\'.", ch);
435 if (sr_get_debug () > 5)
436 debuglogs (5, "Accepting \'%c\'.", ch);
442 * get_hex_digit -- Get a hex digit from the remote system & return its value.
443 * If ignore is nonzero, ignore spaces, newline & tabs.
446 get_hex_digit (ignore)
452 ch = readchar (timeout);
455 if (sr_get_debug () > 4)
457 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
461 #ifdef LOG_FILE /* write to the monitor log */
464 fputs ("get_hex_digit() got a 0x", log_file);
465 fputc (ch, log_file);
466 fputc ('\n', log_file);
472 if (ch >= '0' && ch <= '9')
474 else if (ch >= 'A' && ch <= 'F')
475 return ch - 'A' + 10;
476 else if (ch >= 'a' && ch <= 'f')
477 return ch - 'a' + 10;
478 else if (ch == ' ' && ignore)
483 debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch);
484 error ("Invalid hex digit from remote system. (0x%x)", ch);
489 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
490 * Accept any number leading spaces.
498 val = get_hex_digit (1) << 4;
499 debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val);
501 val |= get_hex_digit (0);
502 debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val);
505 debuglogs (4, "get_hex_byte() -- Read a 0x%x", val);
509 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
510 * and put them in registers starting at REGNO.
521 if (HOST_BYTE_ORDER == BIG_ENDIAN)
524 for (i = 0; i < 8; i++)
525 val = (val << 4) + get_hex_digit (i == 0);
530 for (i = 7; i >= 0; i--)
531 val = (val << 4) + get_hex_digit (i == 0);
535 debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val, (HOST_BYTE_ORDER == BIG_ENDIAN) ? "big endian" : "little endian");
540 /* This is called not only when we first attach, but also when the
541 user types "run" after having attached. */
543 array_create_inferior (execfile, args, env)
551 error ("Can't pass arguments to remote MONITOR process");
553 if (execfile == 0 || exec_bfd == 0)
554 error ("No executable file specified");
556 entry_pt = (int) bfd_get_start_address (exec_bfd);
558 /* The "process" (board) is already stopped awaiting our commands, and
559 the program is already downloaded. We just set its PC and go. */
561 clear_proceed_status ();
563 /* Tell wait_for_inferior that we've started a new process. */
564 init_wait_for_inferior ();
566 /* Set up the "saved terminal modes" of the inferior
567 based on what modes we are starting it with. */
568 target_terminal_init ();
570 /* Install inferior's terminal modes. */
571 target_terminal_inferior ();
573 /* insert_step_breakpoint (); FIXME, do we need this? */
576 proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);
580 * array_open -- open a connection to a remote debugger.
581 * NAME is the filename used for communication.
583 static int baudrate = 9600;
584 static char dev_name[100];
587 array_open (args, name, from_tty)
592 char packet[PBUFSIZ];
595 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
596 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
601 target_preopen (from_tty);
602 unpush_target (&array_ops);
604 tmp_mips_processor_type = "lsi33k"; /* change the default from r3051 */
605 mips_set_processor_type_command ("lsi33k", 0);
607 strcpy (dev_name, args);
608 array_desc = SERIAL_OPEN (dev_name);
610 if (array_desc == NULL)
611 perror_with_name (dev_name);
615 if (SERIAL_SETBAUDRATE (array_desc, baud_rate))
617 SERIAL_CLOSE (array_desc);
618 perror_with_name (name);
622 SERIAL_RAW (array_desc);
624 #if defined (LOG_FILE)
625 log_file = fopen (LOG_FILE, "w");
626 if (log_file == NULL)
627 perror_with_name (LOG_FILE);
628 fprintf (log_file, "GDB %s (%s", version, host_name);
629 fprintf (log_file, " --target %s)\n", array_ops.to_shortname);
630 fprintf (log_file, "Remote target %s connected to %s\n\n", array_ops.to_shortname, dev_name);
633 /* see if the target is alive. For a ROM monitor, we can just try to force the
634 expect_prompt to print a few times. For the GDB remote protocol, the application
635 being debugged is sitting at a breakpoint and waiting for GDB to initialize
636 the connection. We force it to give us an empty packet to see if it's alive.
638 debuglogs (3, "Trying to ACK the target's debug stub");
639 /* unless your are on the new hardware, the old board won't initialize
640 because the '@' doesn't flush output like it does on the new ROMS.
642 printf_monitor ("@"); /* ask for the last signal */
643 expect_prompt (1); /* See if we get a expect_prompt */
644 #ifdef TEST_ARRAY /* skip packet for testing */
645 make_gdb_packet (packet, "?"); /* ask for a bogus packet */
646 if (array_send_packet (packet) == 0)
647 error ("Couldn't transmit packet\n");
648 printf_monitor ("@\n"); /* force it to flush stdout */
649 expect_prompt (1); /* See if we get a expect_prompt */
651 push_target (&array_ops);
653 printf ("Remote target %s connected to %s\n", array_ops.to_shortname, dev_name);
657 * array_close -- Close out all files and local state before this
658 * target loses control.
662 array_close (quitting)
665 SERIAL_CLOSE (array_desc);
668 debuglogs (1, "array_close (quitting=%d)", quitting);
670 #if defined (LOG_FILE)
673 if (ferror (log_file))
674 printf_filtered ("Error writing log file.\n");
675 if (fclose (log_file) != 0)
676 printf_filtered ("Error closing log file.\n");
682 * array_detach -- terminate the open connection to the remote
683 * debugger. Use this when you want to detach and do something
684 * else with your gdb.
687 array_detach (from_tty)
691 debuglogs (1, "array_detach ()");
693 pop_target (); /* calls array_close to do the real work */
695 printf ("Ending remote %s debugging\n", target_shortname);
699 * array_attach -- attach GDB to the target.
702 array_attach (args, from_tty)
707 printf ("Starting remote %s debugging\n", target_shortname);
709 debuglogs (1, "array_attach (args=%s)", args);
711 printf_monitor ("go %x\n");
712 /* swallow the echo. */
713 expect ("go %x\n", 1);
717 * array_resume -- Tell the remote machine to resume.
720 array_resume (pid, step, sig)
722 enum target_signal sig;
724 debuglogs (1, "array_resume (step=%d, sig=%d)", step, sig);
728 printf_monitor ("s\n");
732 printf_monitor ("go\n");
739 * array_wait -- Wait until the remote machine stops, then return,
740 * storing status in status just as `wait' would.
743 array_wait (pid, status)
745 struct target_waitstatus *status;
747 int old_timeout = timeout;
751 serial_ttystate ttystate;
753 debuglogs (1, "array_wait (), printing extraneous text.");
755 status->kind = TARGET_WAITKIND_EXITED;
756 status->value.integer = 0;
758 timeout = 0; /* Don't time out -- user program is running. */
760 #if !defined(__GO32__) && !defined(__MSDOS__) && !defined(_WIN32)
761 tty_desc = SERIAL_FDOPEN (0);
762 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
763 SERIAL_RAW (tty_desc);
766 /* poll on the serial port and the keyboard. */
769 c = readchar (timeout);
772 if (c == *(ARRAY_PROMPT + i))
774 if (++i >= strlen (ARRAY_PROMPT))
775 { /* matched the prompt */
776 debuglogs (4, "array_wait(), got the expect_prompt.");
781 { /* not the prompt */
784 fputc_unfiltered (c, gdb_stdout);
785 gdb_flush (gdb_stdout);
787 c = SERIAL_READCHAR (tty_desc, timeout);
790 SERIAL_WRITE (array_desc, &c, 1);
791 /* do this so it looks like there's keyboard echo */
792 if (c == 3) /* exit on Control-C */
795 fputc_unfiltered (c, gdb_stdout);
796 gdb_flush (gdb_stdout);
800 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
803 debuglogs (4, "array_wait(), got the expect_prompt.");
806 status->kind = TARGET_WAITKIND_STOPPED;
807 status->value.sig = TARGET_SIGNAL_TRAP;
809 timeout = old_timeout;
815 * array_fetch_registers -- read the remote registers into the
819 array_fetch_registers (ignored)
824 unsigned char packet[PBUFSIZ];
825 char regs[REGISTER_BYTES];
827 debuglogs (1, "array_fetch_registers (ignored=%d)\n", ignored);
829 memset (packet, 0, PBUFSIZ);
830 /* Unimplemented registers read as all bits zero. */
831 memset (regs, 0, REGISTER_BYTES);
832 make_gdb_packet (packet, "g");
833 if (array_send_packet (packet) == 0)
834 error ("Couldn't transmit packet\n");
835 if (array_get_packet (packet) == 0)
836 error ("Couldn't receive packet\n");
837 /* FIXME: read bytes from packet */
838 debuglogs (4, "array_fetch_registers: Got a \"%s\" back\n", packet);
839 for (regno = 0; regno <= PC_REGNUM + 4; regno++)
841 /* supply register stores in target byte order, so swap here */
842 /* FIXME: convert from ASCII hex to raw bytes */
843 i = ascii2hexword (packet + (regno * 8));
844 debuglogs (5, "Adding register %d = %x\n", regno, i);
845 SWAP_TARGET_AND_HOST (&i, 4);
846 supply_register (regno, (char *) &i);
851 * This is unused by targets like this one that use a
852 * protocol based on GDB's remote protocol.
855 array_fetch_register (ignored)
858 array_fetch_registers ();
862 * Get all the registers from the targets. They come back in a large array.
865 array_store_registers (ignored)
870 char packet[PBUFSIZ];
874 debuglogs (1, "array_store_registers()");
876 memset (packet, 0, PBUFSIZ);
877 memset (buf, 0, PBUFSIZ);
880 /* Unimplemented registers read as all bits zero. */
881 /* FIXME: read bytes from packet */
882 for (regno = 0; regno < 41; regno++)
884 /* supply register stores in target byte order, so swap here */
885 /* FIXME: convert from ASCII hex to raw bytes */
886 i = (unsigned long) read_register (regno);
887 hexword2ascii (num, i);
888 strcpy (buf + (regno * 8) + 1, num);
890 *(buf + (regno * 8) + 2) = 0;
891 make_gdb_packet (packet, buf);
892 if (array_send_packet (packet) == 0)
893 error ("Couldn't transmit packet\n");
894 if (array_get_packet (packet) == 0)
895 error ("Couldn't receive packet\n");
897 registers_changed ();
901 * This is unused by targets like this one that use a
902 * protocol based on GDB's remote protocol.
905 array_store_register (ignored)
908 array_store_registers ();
911 /* Get ready to modify the registers array. On machines which store
912 individual registers, this doesn't need to do anything. On machines
913 which store all the registers in one fell swoop, this makes sure
914 that registers contains all the registers from the program being
918 array_prepare_to_store ()
920 /* Do nothing, since we can store individual regs */
926 printf ("\tAttached to %s at %d baud.\n",
931 * array_write_inferior_memory -- Copy LEN bytes of data from debugger
932 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
935 array_write_inferior_memory (memaddr, myaddr, len)
937 unsigned char *myaddr;
942 char packet[PBUFSIZ];
947 debuglogs (1, "array_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
948 memset (buf, '\0', PBUFSIZ); /* this also sets the string terminator */
951 *p++ = 'M'; /* The command to write memory */
952 hexword2ascii (num, memaddr); /* convert the address */
953 strcpy (p, num); /* copy the address */
955 *p++ = ','; /* add comma delimeter */
956 hexword2ascii (num, len); /* Get the length as a 4 digit number */
961 *p++ = ':'; /* add the colon delimeter */
962 for (j = 0; j < len; j++)
963 { /* copy the data in after converting it */
964 *p++ = tohex ((myaddr[j] >> 4) & 0xf);
965 *p++ = tohex (myaddr[j] & 0xf);
968 make_gdb_packet (packet, buf);
969 if (array_send_packet (packet) == 0)
970 error ("Couldn't transmit packet\n");
971 if (array_get_packet (packet) == 0)
972 error ("Couldn't receive packet\n");
978 * array_read_inferior_memory -- read LEN bytes from inferior memory
979 * at MEMADDR. Put the result at debugger address MYADDR. Returns
983 array_read_inferior_memory (memaddr, myaddr, len)
990 char packet[PBUFSIZ];
991 int count; /* Number of bytes read so far. */
992 unsigned long startaddr; /* Starting address of this pass. */
993 int len_this_pass; /* Number of bytes to read in this pass. */
995 debuglogs (1, "array_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
997 /* Note that this code works correctly if startaddr is just less
998 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
999 thing). That is, something like
1000 array_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
1001 works--it never adds len To memaddr and gets 0. */
1002 /* However, something like
1003 array_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
1004 doesn't need to work. Detect it and give up if there's an attempt
1006 if (((memaddr - 1) + len) < memaddr)
1012 for (count = 0, startaddr = memaddr; count < len; startaddr += len_this_pass)
1014 /* Try to align to 16 byte boundry (why?) */
1016 if ((startaddr % 16) != 0)
1018 len_this_pass -= startaddr % 16;
1020 /* Only transfer bytes we need */
1021 if (len_this_pass > (len - count))
1023 len_this_pass = (len - count);
1025 /* Fetch the bytes */
1026 debuglogs (3, "read %d bytes from inferior address %x", len_this_pass,
1028 sprintf (buf, "m%08lx,%04x", startaddr, len_this_pass);
1029 make_gdb_packet (packet, buf);
1030 if (array_send_packet (packet) == 0)
1032 error ("Couldn't transmit packet\n");
1034 if (array_get_packet (packet) == 0)
1036 error ("Couldn't receive packet\n");
1040 error ("Got no data in the GDB packet\n");
1042 /* Pick packet apart and xfer bytes to myaddr */
1043 debuglogs (4, "array_read_inferior_memory: Got a \"%s\" back\n", packet);
1044 for (j = 0; j < len_this_pass; j++)
1046 /* extract the byte values */
1047 myaddr[count++] = from_hex (*(packet + (j * 2))) * 16 + from_hex (*(packet + (j * 2) + 1));
1048 debuglogs (5, "myaddr[%d] set to %x\n", count - 1, myaddr[count - 1]);
1054 /* FIXME-someday! merge these two. */
1056 array_xfer_memory (memaddr, myaddr, len, write, target)
1061 struct target_ops *target; /* ignored */
1064 return array_write_inferior_memory (memaddr, myaddr, len);
1066 return array_read_inferior_memory (memaddr, myaddr, len);
1070 array_kill (args, from_tty)
1074 return; /* ignore attempts to kill target system */
1077 /* Clean up when a program exits.
1078 The program actually lives on in the remote processor's RAM, and may be
1079 run again without a download. Don't leave it full of breakpoint
1083 array_mourn_inferior ()
1085 remove_breakpoints ();
1086 generic_mourn_inferior (); /* Do all the proper things now */
1089 #define MAX_ARRAY_BREAKPOINTS 16
1091 static CORE_ADDR breakaddr[MAX_ARRAY_BREAKPOINTS] =
1095 * array_insert_breakpoint -- add a breakpoint
1098 array_insert_breakpoint (addr, shadow)
1104 CORE_ADDR bp_addr = addr;
1106 debuglogs (1, "array_insert_breakpoint() addr = 0x%x", addr);
1107 BREAKPOINT_FROM_PC (&bp_addr, &bp_size);
1109 for (i = 0; i <= MAX_ARRAY_BREAKPOINTS; i++)
1111 if (breakaddr[i] == 0)
1113 breakaddr[i] = addr;
1114 if (sr_get_debug () > 4)
1115 printf ("Breakpoint at %s\n", paddr_nz (addr));
1116 array_read_inferior_memory (bp_addr, shadow, bp_size);
1117 printf_monitor ("b 0x%x\n", addr);
1123 fprintf (stderr, "Too many breakpoints (> 16) for monitor\n");
1128 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1131 array_remove_breakpoint (addr, shadow)
1137 debuglogs (1, "array_remove_breakpoint() addr = 0x%x", addr);
1139 for (i = 0; i < MAX_ARRAY_BREAKPOINTS; i++)
1141 if (breakaddr[i] == addr)
1144 /* some monitors remove breakpoints based on the address */
1145 printf_monitor ("bd %x\n", i);
1150 fprintf (stderr, "Can't find breakpoint associated with 0x%s\n",
1158 debuglogs (1, "array_stop()");
1159 printf_monitor ("\003");
1164 * array_command -- put a command string, in args, out to MONITOR.
1165 * Output from MONITOR is placed on the users terminal until the
1166 * expect_prompt is seen. FIXME
1169 monitor_command (args, fromtty)
1173 debuglogs (1, "monitor_command (args=%s)", args);
1175 if (array_desc == NULL)
1176 error ("monitor target not open.");
1179 error ("Missing command.");
1181 printf_monitor ("%s\n", args);
1186 * make_gdb_packet -- make a GDB packet. The data is always ASCII.
1187 * A debug packet whose contents are <data>
1188 * is encapsulated for transmission in the form:
1190 * $ <data> # CSUM1 CSUM2
1192 * <data> must be ASCII alphanumeric and cannot include characters
1193 * '$' or '#'. If <data> starts with two characters followed by
1194 * ':', then the existing stubs interpret this as a sequence number.
1196 * CSUM1 and CSUM2 are ascii hex representation of an 8-bit
1197 * checksum of <data>, the most significant nibble is sent first.
1198 * the hex digits 0-9,a-f are used.
1202 make_gdb_packet (buf, data)
1206 unsigned char csum = 0;
1210 debuglogs (3, "make_gdb_packet(%s)\n", data);
1211 cnt = strlen (data);
1213 error ("make_gdb_packet(): to much data\n");
1215 /* start with the packet header */
1219 /* calculate the checksum */
1220 for (i = 0; i < cnt; i++)
1226 /* terminate the data with a '#' */
1229 /* add the checksum as two ascii digits */
1230 *p++ = tohex ((csum >> 4) & 0xf);
1231 *p++ = tohex (csum & 0xf);
1232 *p = 0x0; /* Null terminator on string */
1236 * array_send_packet -- send a GDB packet to the target with error handling. We
1237 * get a '+' (ACK) back if the packet is received and the checksum
1238 * matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a
1239 * successful transmition, or a 0 for a failure.
1242 array_send_packet (packet)
1251 /* scan the packet to make sure it only contains valid characters.
1252 this may sound silly, but sometimes a garbled packet will hang
1253 the target board. We scan the whole thing, then print the error
1256 for (i = 0; i < strlen (packet); i++)
1258 debuglogs (5, "array_send_packet(): Scanning \'%c\'\n", packet[i]);
1259 /* legit hex numbers or command */
1260 if ((isxdigit (packet[i])) || (isalpha (packet[i])))
1266 case '#': /* end of packet */
1267 case '$': /* start of packet */
1269 default: /* bogus character */
1271 debuglogs (4, "array_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet[i]);
1277 error ("Can't send packet, found %d non-ascii characters", retries);
1279 /* ok, try to send the packet */
1281 while (retries++ <= 10)
1283 printf_monitor ("%s", packet);
1285 /* read until either a timeout occurs (-2) or '+' is read */
1286 while (retries <= 10)
1288 c = readchar (-timeout);
1289 debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c);
1293 debuglogs (3, "Got Ack\n");
1295 case SERIAL_TIMEOUT:
1296 debuglogs (3, "Timed out reading serial port\n");
1297 printf_monitor ("@"); /* resync with the monitor */
1298 expect_prompt (1); /* See if we get a expect_prompt */
1299 break; /* Retransmit buffer */
1301 debuglogs (3, "Got NAK\n");
1302 printf_monitor ("@"); /* resync with the monitor */
1303 expect_prompt (1); /* See if we get a expect_prompt */
1306 /* it's probably an old response, or the echo of our command.
1307 * just gobble up the packet and ignore it.
1309 debuglogs (3, "Got a junk packet\n");
1313 c = readchar (timeout);
1317 c = readchar (timeout);
1319 c = readchar (timeout);
1322 debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk);
1323 continue; /* Now, go look for next packet */
1328 debuglogs (3, "Retransmitting packet \"%s\"\n", packet);
1329 break; /* Here to retransmit */
1336 * array_get_packet -- get a GDB packet from the target. Basically we read till we
1337 * see a '#', then check the checksum. It returns a 1 if it's gotten a
1338 * packet, or a 0 it the packet wasn't transmitted correctly.
1341 array_get_packet (packet)
1347 unsigned char pktcsum;
1353 memset (packet, 1, PBUFSIZ);
1355 while (retries <= 10)
1359 c = readchar (timeout);
1360 if (c == SERIAL_TIMEOUT)
1362 debuglogs (3, "array_get_packet: got time out from serial port.\n");
1364 debuglogs (3, "Waiting for a '$', got a %c\n", c);
1369 while (retries <= 10)
1371 c = readchar (timeout);
1372 debuglogs (3, "array_get_packet: got a '%c'\n", c);
1375 case SERIAL_TIMEOUT:
1376 debuglogs (3, "Timeout in mid-packet, retrying\n");
1379 debuglogs (3, "Saw new packet start in middle of old one\n");
1380 return 0; /* Start a new packet, count retries */
1383 pktcsum = from_hex (readchar (timeout)) << 4;
1384 pktcsum |= from_hex (readchar (timeout));
1386 debuglogs (3, "\nGDB packet checksum zero, must be a bogus packet\n");
1387 if (csum == pktcsum)
1389 debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet);
1390 printf_monitor ("@");
1394 debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum, csum);
1396 case '*': /* Run length encoding */
1397 debuglogs (5, "Run length encoding in packet\n");
1399 c = readchar (timeout);
1401 c = c - ' ' + 3; /* Compute repeat count */
1403 if (c > 0 && c < 255 && bp + c - 1 < packet + PBUFSIZ - 1)
1405 memset (bp, *(bp - 1), c);
1410 printf_filtered ("Repeat count %d too large for buffer.\n", c);
1414 if ((!isxdigit (c)) && (!ispunct (c)))
1415 debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c);
1416 if (bp < packet + PBUFSIZ - 1)
1424 puts_filtered ("Remote packet too long.\n");
1429 return 0; /* exceeded retries */
1433 * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
1435 static unsigned long
1444 for (i = 0; i < 8; i++)
1447 if (mem[i] >= 'A' && mem[i] <= 'F')
1448 val = val + mem[i] - 'A' + 10;
1449 if (mem[i] >= 'a' && mem[i] <= 'f')
1450 val = val + mem[i] - 'a' + 10;
1451 if (mem[i] >= '0' && mem[i] <= '9')
1452 val = val + mem[i] - '0';
1456 debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val, buf, mem);
1461 * ascii2hexword -- convert a hex value to an ascii number represented by 8
1465 hexword2ascii (mem, num)
1472 debuglogs (4, "hexword2ascii() converting %x ", num);
1473 for (i = 7; i >= 0; i--)
1475 mem[i] = tohex ((num >> 4) & 0xf);
1476 mem[i] = tohex (num & 0xf);
1480 debuglogs (4, "\tto a %s", mem);
1483 /* Convert hex digit A to a number. */
1491 debuglogs (4, "from_hex got a 0x%x(%c)\n", a, a);
1492 if (a >= '0' && a <= '9')
1494 if (a >= 'a' && a <= 'f')
1495 return a - 'a' + 10;
1496 if (a >= 'A' && a <= 'F')
1497 return a - 'A' + 10;
1500 error ("Reply contains invalid hex digit 0x%x", a);
1504 /* Convert number NIB to a hex digit. */
1512 return 'a' + nib - 10;
1516 * _initialize_remote_monitors -- setup a few addtitional commands that
1517 * are usually only used by monitors.
1520 _initialize_remote_monitors ()
1522 /* generic monitor command */
1523 add_com ("monitor", class_obscure, monitor_command,
1524 "Send a command to the debug monitor.");
1529 * _initialize_array -- do any special init stuff for the target.
1532 _initialize_array ()
1535 add_target (&array_ops);