1 /* Remote debugging interface for Array Tech RAID controller..
3 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
6 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
8 This module talks to a debug monitor called 'MONITOR', which
9 We communicate with MONITOR via either a direct serial line, or a TCP
10 (or possibly TELNET) stream to a terminal multiplexor,
11 which in turn talks to the target board.
13 This file is part of GDB.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
34 #include <sys/types.h>
35 #include "gdb_string.h"
39 #include "remote-utils.h"
46 #define ARRAY_PROMPT ">> "
48 static void debuglogs (int, char *, ...);
49 static void array_open ();
50 static void array_close ();
51 static void array_detach ();
52 static void array_attach ();
53 static void array_resume (ptid_t ptid
, int step
, enum target_signal sig
);
54 static void array_fetch_register ();
55 static void array_store_register ();
56 static void array_fetch_registers ();
57 static void array_store_registers ();
58 static void array_prepare_to_store ();
59 static void array_files_info ();
60 static void array_kill ();
61 static void array_create_inferior ();
62 static void array_mourn_inferior ();
63 static void make_gdb_packet ();
64 static int array_xfer_memory ();
65 static ptid_t
array_wait (ptid_t ptid
,
66 struct target_waitstatus
*status
);
67 static int array_insert_breakpoint ();
68 static int array_remove_breakpoint ();
71 static int from_hex ();
72 static int array_send_packet ();
73 static int array_get_packet ();
74 static unsigned long ascii2hexword ();
75 static void hexword2ascii ();
77 #define LOG_FILE "monitor.log"
78 #if defined (LOG_FILE)
82 static int timeout
= 30;
83 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
84 and i386-stub.c. Normally, no one would notice because it only matters
85 for writing large chunks of memory (e.g. in downloads). Also, this needs
86 to be more than 400 if required to hold the registers (see below, where
87 we round it up based on REGISTER_BYTES). */
91 * Descriptor for I/O to remote machine. Initialize it to NULL so that
92 * array_open knows that we don't have a file open when the program starts.
94 struct serial
*array_desc
= NULL
;
97 * this array of registers need to match the indexes used by GDB. The
98 * whole reason this exists is cause the various ROM monitors use
99 * different strings than GDB does, and doesn't support all the
100 * registers either. So, typing "info reg sp" becomes a "r30".
102 extern char *tmp_mips_processor_type
;
103 extern int mips_set_processor_type ();
105 static struct target_ops array_ops
;
108 init_array_ops (void)
110 array_ops
.to_shortname
= "array";
111 array_ops
.to_longname
=
112 "Debug using the standard GDB remote protocol for the Array Tech target.",
114 "Debug using the standard GDB remote protocol for the Array Tech target.\n\
115 Specify the serial device it is connected to (e.g. /dev/ttya).";
116 array_ops
.to_open
= array_open
;
117 array_ops
.to_close
= array_close
;
118 array_ops
.to_detach
= array_detach
;
119 array_ops
.to_resume
= array_resume
;
120 array_ops
.to_wait
= array_wait
;
121 array_ops
.to_fetch_registers
= array_fetch_registers
;
122 array_ops
.to_store_registers
= array_store_registers
;
123 array_ops
.to_prepare_to_store
= array_prepare_to_store
;
124 array_ops
.to_xfer_memory
= array_xfer_memory
;
125 array_ops
.to_files_info
= array_files_info
;
126 array_ops
.to_insert_breakpoint
= array_insert_breakpoint
;
127 array_ops
.to_remove_breakpoint
= array_remove_breakpoint
;
128 array_ops
.to_kill
= array_kill
;
129 array_ops
.to_create_inferior
= array_create_inferior
;
130 array_ops
.to_mourn_inferior
= array_mourn_inferior
;
131 array_ops
.to_stratum
= process_stratum
;
132 array_ops
.to_has_all_memory
= 1;
133 array_ops
.to_has_memory
= 1;
134 array_ops
.to_has_stack
= 1;
135 array_ops
.to_has_registers
= 1;
136 array_ops
.to_has_execution
= 1;
137 array_ops
.to_magic
= OPS_MAGIC
;
141 * printf_monitor -- send data to monitor. Works just like printf.
144 printf_monitor (char *pattern
,...)
150 va_start (args
, pattern
);
152 vsprintf (buf
, pattern
, args
);
154 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf
);
156 if (strlen (buf
) > PBUFSIZ
)
157 error ("printf_monitor(): string too long");
158 if (serial_write (array_desc
, buf
, strlen (buf
)))
159 fprintf_unfiltered (gdb_stderr
, "serial_write failed: %s\n",
160 safe_strerror (errno
));
163 * write_monitor -- send raw data to monitor.
166 write_monitor (char data
[], int len
)
168 if (serial_write (array_desc
, data
, len
))
169 fprintf_unfiltered (gdb_stderr
, "serial_write failed: %s\n",
170 safe_strerror (errno
));
172 *(data
+ len
+ 1) = '\0';
173 debuglogs (1, "write_monitor(), Sending: \"%s\".", data
);
178 * debuglogs -- deal with debugging info to multiple sources. This takes
179 * two real args, the first one is the level to be compared against
180 * the sr_get_debug() value, the second arg is a printf buffer and args
181 * to be formatted and printed. A CR is added after each string is printed.
184 debuglogs (int level
, char *pattern
,...)
188 unsigned char buf
[PBUFSIZ
];
189 char newbuf
[PBUFSIZ
];
192 va_start (args
, pattern
);
194 if ((level
< 0) || (level
> 100))
196 error ("Bad argument passed to debuglogs(), needs debug level");
200 vsprintf (buf
, pattern
, args
); /* format the string */
202 /* convert some characters so it'll look right in the log */
204 for (i
= 0; buf
[i
] != '\0'; i
++)
207 error ("Debug message too long");
210 case '\n': /* newlines */
214 case '\r': /* carriage returns */
218 case '\033': /* escape */
226 case '\b': /* backspace */
230 default: /* no change */
235 { /* modify control characters */
241 { /* modify control characters */
247 *p
= '\0'; /* terminate the string */
249 if (sr_get_debug () > level
)
250 printf_unfiltered ("%s\n", newbuf
);
252 #ifdef LOG_FILE /* write to the monitor log */
255 fputs (newbuf
, log_file
);
256 fputc ('\n', log_file
);
262 /* readchar -- read a character from the remote system, doing all the fancy
266 readchar (int timeout
)
270 c
= serial_readchar (array_desc
, abs (timeout
));
272 if (sr_get_debug () > 5)
275 debuglogs (5, "readchar: timeout = %d\n", timeout
);
280 putc (c
& 0x7f, log_file
);
286 if (c
== SERIAL_TIMEOUT
)
289 return c
; /* Polls shouldn't generate timeout errors */
290 error ("Timeout reading from remote system.");
292 fputs ("ERROR: Timeout reading from remote system", log_file
);
295 perror_with_name ("readchar");
299 * expect -- scan input from the remote system, until STRING is found.
300 * If DISCARD is non-zero, then discard non-matching input, else print
301 * it out. Let the user break out immediately.
304 expect (char *string
, int discard
)
310 debuglogs (1, "Expecting \"%s\".", string
);
315 c
= readchar (timeout
);
323 debuglogs (4, "Matched");
331 fputc_unfiltered (c
, gdb_stdout
);
338 /* Keep discarding input until we see the MONITOR array_cmds->prompt.
340 The convention for dealing with the expect_prompt is that you
342 o *then* wait for the expect_prompt.
344 Thus the last thing that a procedure does with the serial line
345 will be an expect_prompt(). Exception: array_resume does not
346 wait for the expect_prompt, because the terminal is being handed over
347 to the inferior. However, the next thing which happens after that
348 is a array_wait which does wait for the expect_prompt.
349 Note that this includes abnormal exit, e.g. error(). This is
350 necessary to prevent getting into states from which we can't
353 expect_prompt (int discard
)
355 expect (ARRAY_PROMPT
, discard
);
359 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
372 if (sr_get_debug () > 5)
373 debuglogs (5, "Ignoring \'%c\'.", ch
);
376 if (sr_get_debug () > 5)
377 debuglogs (5, "Accepting \'%c\'.", ch
);
383 * get_hex_digit -- Get a hex digit from the remote system & return its value.
384 * If ignore is nonzero, ignore spaces, newline & tabs.
387 get_hex_digit (int ignore
)
392 ch
= readchar (timeout
);
395 if (sr_get_debug () > 4)
397 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch
, ch
);
401 #ifdef LOG_FILE /* write to the monitor log */
404 fputs ("get_hex_digit() got a 0x", log_file
);
405 fputc (ch
, log_file
);
406 fputc ('\n', log_file
);
412 if (ch
>= '0' && ch
<= '9')
414 else if (ch
>= 'A' && ch
<= 'F')
415 return ch
- 'A' + 10;
416 else if (ch
>= 'a' && ch
<= 'f')
417 return ch
- 'a' + 10;
418 else if (ch
== ' ' && ignore
)
423 debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch
);
424 error ("Invalid hex digit from remote system. (0x%x)", ch
);
429 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
430 * Accept any number leading spaces.
433 get_hex_byte (char *byt
)
437 val
= get_hex_digit (1) << 4;
438 debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val
);
440 val
|= get_hex_digit (0);
441 debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val
);
444 debuglogs (4, "get_hex_byte() -- Read a 0x%x", val
);
448 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
449 * and put them in registers starting at REGNO.
459 for (i
= 0; i
< 8; i
++)
460 val
= (val
<< 4) + get_hex_digit (i
== 0);
462 debuglogs (4, "get_hex_word() got a 0x%x.", val
);
467 /* This is called not only when we first attach, but also when the
468 user types "run" after having attached. */
470 array_create_inferior (char *execfile
, char *args
, char **env
)
475 error ("Can't pass arguments to remote MONITOR process");
477 if (execfile
== 0 || exec_bfd
== 0)
478 error ("No executable file specified");
480 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
482 /* The "process" (board) is already stopped awaiting our commands, and
483 the program is already downloaded. We just set its PC and go. */
485 clear_proceed_status ();
487 /* Tell wait_for_inferior that we've started a new process. */
488 init_wait_for_inferior ();
490 /* Set up the "saved terminal modes" of the inferior
491 based on what modes we are starting it with. */
492 target_terminal_init ();
494 /* Install inferior's terminal modes. */
495 target_terminal_inferior ();
497 /* insert_step_breakpoint (); FIXME, do we need this? */
500 proceed ((CORE_ADDR
) entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
504 * array_open -- open a connection to a remote debugger.
505 * NAME is the filename used for communication.
507 static int baudrate
= 9600;
508 static char dev_name
[100];
511 array_open (char *args
, char *name
, int from_tty
)
513 char packet
[PBUFSIZ
];
516 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
517 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
522 target_preopen (from_tty
);
523 unpush_target (&array_ops
);
525 tmp_mips_processor_type
= "lsi33k"; /* change the default from r3051 */
526 mips_set_processor_type_command ("lsi33k", 0);
528 strcpy (dev_name
, args
);
529 array_desc
= serial_open (dev_name
);
531 if (array_desc
== NULL
)
532 perror_with_name (dev_name
);
536 if (serial_setbaudrate (array_desc
, baud_rate
))
538 serial_close (array_desc
);
539 perror_with_name (name
);
543 serial_raw (array_desc
);
545 #if defined (LOG_FILE)
546 log_file
= fopen (LOG_FILE
, "w");
547 if (log_file
== NULL
)
548 perror_with_name (LOG_FILE
);
549 fprintf (log_file
, "GDB %s (%s", version
, host_name
);
550 fprintf (log_file
, " --target %s)\n", array_ops
.to_shortname
);
551 fprintf (log_file
, "Remote target %s connected to %s\n\n", array_ops
.to_shortname
, dev_name
);
554 /* see if the target is alive. For a ROM monitor, we can just try to force the
555 expect_prompt to print a few times. For the GDB remote protocol, the application
556 being debugged is sitting at a breakpoint and waiting for GDB to initialize
557 the connection. We force it to give us an empty packet to see if it's alive.
559 debuglogs (3, "Trying to ACK the target's debug stub");
560 /* unless your are on the new hardware, the old board won't initialize
561 because the '@' doesn't flush output like it does on the new ROMS.
563 printf_monitor ("@"); /* ask for the last signal */
564 expect_prompt (1); /* See if we get a expect_prompt */
565 #ifdef TEST_ARRAY /* skip packet for testing */
566 make_gdb_packet (packet
, "?"); /* ask for a bogus packet */
567 if (array_send_packet (packet
) == 0)
568 error ("Couldn't transmit packet\n");
569 printf_monitor ("@\n"); /* force it to flush stdout */
570 expect_prompt (1); /* See if we get a expect_prompt */
572 push_target (&array_ops
);
574 printf ("Remote target %s connected to %s\n", array_ops
.to_shortname
, dev_name
);
578 * array_close -- Close out all files and local state before this
579 * target loses control.
583 array_close (int quitting
)
585 serial_close (array_desc
);
588 debuglogs (1, "array_close (quitting=%d)", quitting
);
590 #if defined (LOG_FILE)
593 if (ferror (log_file
))
594 printf_filtered ("Error writing log file.\n");
595 if (fclose (log_file
) != 0)
596 printf_filtered ("Error closing log file.\n");
602 * array_detach -- terminate the open connection to the remote
603 * debugger. Use this when you want to detach and do something
604 * else with your gdb.
607 array_detach (int from_tty
)
610 debuglogs (1, "array_detach ()");
612 pop_target (); /* calls array_close to do the real work */
614 printf ("Ending remote %s debugging\n", target_shortname
);
618 * array_attach -- attach GDB to the target.
621 array_attach (char *args
, int from_tty
)
624 printf ("Starting remote %s debugging\n", target_shortname
);
626 debuglogs (1, "array_attach (args=%s)", args
);
628 printf_monitor ("go %x\n");
629 /* swallow the echo. */
630 expect ("go %x\n", 1);
634 * array_resume -- Tell the remote machine to resume.
637 array_resume (ptid_t ptid
, int step
, enum target_signal sig
)
639 debuglogs (1, "array_resume (step=%d, sig=%d)", step
, sig
);
643 printf_monitor ("s\n");
647 printf_monitor ("go\n");
654 * array_wait -- Wait until the remote machine stops, then return,
655 * storing status in status just as `wait' would.
658 array_wait (ptid_t ptid
, struct target_waitstatus
*status
)
660 int old_timeout
= timeout
;
663 struct serial
*tty_desc
;
664 serial_ttystate ttystate
;
666 debuglogs (1, "array_wait (), printing extraneous text.");
668 status
->kind
= TARGET_WAITKIND_EXITED
;
669 status
->value
.integer
= 0;
671 timeout
= 0; /* Don't time out -- user program is running. */
673 #if !defined(__GO32__) && !defined(__MSDOS__) && !defined(_WIN32)
674 tty_desc
= serial_fdopen (0);
675 ttystate
= serial_get_tty_state (tty_desc
);
676 serial_raw (tty_desc
);
679 /* poll on the serial port and the keyboard. */
682 c
= readchar (timeout
);
685 if (c
== *(ARRAY_PROMPT
+ i
))
687 if (++i
>= strlen (ARRAY_PROMPT
))
688 { /* matched the prompt */
689 debuglogs (4, "array_wait(), got the expect_prompt.");
694 { /* not the prompt */
697 fputc_unfiltered (c
, gdb_stdout
);
698 gdb_flush (gdb_stdout
);
700 c
= serial_readchar (tty_desc
, timeout
);
703 serial_write (array_desc
, &c
, 1);
704 /* do this so it looks like there's keyboard echo */
705 if (c
== 3) /* exit on Control-C */
708 fputc_unfiltered (c
, gdb_stdout
);
709 gdb_flush (gdb_stdout
);
713 serial_set_tty_state (tty_desc
, ttystate
);
716 debuglogs (4, "array_wait(), got the expect_prompt.");
719 status
->kind
= TARGET_WAITKIND_STOPPED
;
720 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
722 timeout
= old_timeout
;
724 return inferior_ptid
;
728 * array_fetch_registers -- read the remote registers into the
732 array_fetch_registers (int ignored
)
734 char reg
[MAX_REGISTER_SIZE
];
737 char *packet
= alloca (PBUFSIZ
);
739 debuglogs (1, "array_fetch_registers (ignored=%d)\n", ignored
);
741 memset (packet
, 0, PBUFSIZ
);
742 make_gdb_packet (packet
, "g");
743 if (array_send_packet (packet
) == 0)
744 error ("Couldn't transmit packet\n");
745 if (array_get_packet (packet
) == 0)
746 error ("Couldn't receive packet\n");
747 /* FIXME: read bytes from packet */
748 debuglogs (4, "array_fetch_registers: Got a \"%s\" back\n", packet
);
749 for (regno
= 0; regno
<= PC_REGNUM
+ 4; regno
++)
751 /* supply register stores in target byte order, so swap here */
752 /* FIXME: convert from ASCII hex to raw bytes */
753 LONGEST i
= ascii2hexword (packet
+ (regno
* 8));
754 debuglogs (5, "Adding register %d = %x\n", regno
, i
);
755 store_unsigned_integer (®
, REGISTER_RAW_SIZE (regno
), i
);
756 supply_register (regno
, (char *) ®
);
761 * This is unused by targets like this one that use a
762 * protocol based on GDB's remote protocol.
765 array_fetch_register (int ignored
)
767 array_fetch_registers (0 /* ignored */);
771 * Get all the registers from the targets. They come back in a large array.
774 array_store_registers (int ignored
)
778 char packet
[PBUFSIZ
];
782 debuglogs (1, "array_store_registers()");
784 memset (packet
, 0, PBUFSIZ
);
785 memset (buf
, 0, PBUFSIZ
);
788 /* Unimplemented registers read as all bits zero. */
789 /* FIXME: read bytes from packet */
790 for (regno
= 0; regno
< 41; regno
++)
792 /* supply register stores in target byte order, so swap here */
793 /* FIXME: convert from ASCII hex to raw bytes */
794 i
= (unsigned long) read_register (regno
);
795 hexword2ascii (num
, i
);
796 strcpy (buf
+ (regno
* 8) + 1, num
);
798 *(buf
+ (regno
* 8) + 2) = 0;
799 make_gdb_packet (packet
, buf
);
800 if (array_send_packet (packet
) == 0)
801 error ("Couldn't transmit packet\n");
802 if (array_get_packet (packet
) == 0)
803 error ("Couldn't receive packet\n");
805 registers_changed ();
809 * This is unused by targets like this one that use a
810 * protocol based on GDB's remote protocol.
813 array_store_register (int ignored
)
815 array_store_registers (0 /* ignored */);
818 /* Get ready to modify the registers array. On machines which store
819 individual registers, this doesn't need to do anything. On machines
820 which store all the registers in one fell swoop, this makes sure
821 that registers contains all the registers from the program being
825 array_prepare_to_store (void)
827 /* Do nothing, since we can store individual regs */
831 array_files_info (void)
833 printf ("\tAttached to %s at %d baud.\n",
838 * array_write_inferior_memory -- Copy LEN bytes of data from debugger
839 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
842 array_write_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
846 char packet
[PBUFSIZ
];
851 debuglogs (1, "array_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
852 memset (buf
, '\0', PBUFSIZ
); /* this also sets the string terminator */
855 *p
++ = 'M'; /* The command to write memory */
856 hexword2ascii (num
, memaddr
); /* convert the address */
857 strcpy (p
, num
); /* copy the address */
859 *p
++ = ','; /* add comma delimeter */
860 hexword2ascii (num
, len
); /* Get the length as a 4 digit number */
865 *p
++ = ':'; /* add the colon delimeter */
866 for (j
= 0; j
< len
; j
++)
867 { /* copy the data in after converting it */
868 *p
++ = tohex ((myaddr
[j
] >> 4) & 0xf);
869 *p
++ = tohex (myaddr
[j
] & 0xf);
872 make_gdb_packet (packet
, buf
);
873 if (array_send_packet (packet
) == 0)
874 error ("Couldn't transmit packet\n");
875 if (array_get_packet (packet
) == 0)
876 error ("Couldn't receive packet\n");
882 * array_read_inferior_memory -- read LEN bytes from inferior memory
883 * at MEMADDR. Put the result at debugger address MYADDR. Returns
887 array_read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
891 char packet
[PBUFSIZ
];
892 int count
; /* Number of bytes read so far. */
893 unsigned long startaddr
; /* Starting address of this pass. */
894 int len_this_pass
; /* Number of bytes to read in this pass. */
896 debuglogs (1, "array_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
898 /* Note that this code works correctly if startaddr is just less
899 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
900 thing). That is, something like
901 array_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
902 works--it never adds len To memaddr and gets 0. */
903 /* However, something like
904 array_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
905 doesn't need to work. Detect it and give up if there's an attempt
907 if (((memaddr
- 1) + len
) < memaddr
)
913 for (count
= 0, startaddr
= memaddr
; count
< len
; startaddr
+= len_this_pass
)
915 /* Try to align to 16 byte boundry (why?) */
917 if ((startaddr
% 16) != 0)
919 len_this_pass
-= startaddr
% 16;
921 /* Only transfer bytes we need */
922 if (len_this_pass
> (len
- count
))
924 len_this_pass
= (len
- count
);
926 /* Fetch the bytes */
927 debuglogs (3, "read %d bytes from inferior address %x", len_this_pass
,
929 sprintf (buf
, "m%08lx,%04x", startaddr
, len_this_pass
);
930 make_gdb_packet (packet
, buf
);
931 if (array_send_packet (packet
) == 0)
933 error ("Couldn't transmit packet\n");
935 if (array_get_packet (packet
) == 0)
937 error ("Couldn't receive packet\n");
941 error ("Got no data in the GDB packet\n");
943 /* Pick packet apart and xfer bytes to myaddr */
944 debuglogs (4, "array_read_inferior_memory: Got a \"%s\" back\n", packet
);
945 for (j
= 0; j
< len_this_pass
; j
++)
947 /* extract the byte values */
948 myaddr
[count
++] = from_hex (*(packet
+ (j
* 2))) * 16 + from_hex (*(packet
+ (j
* 2) + 1));
949 debuglogs (5, "myaddr[%d] set to %x\n", count
- 1, myaddr
[count
- 1]);
955 /* Transfer LEN bytes between GDB address MYADDR and target address
956 MEMADDR. If WRITE is non-zero, transfer them to the target,
957 otherwise transfer them from the target. TARGET is unused.
959 Returns the number of bytes transferred. */
962 array_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
963 struct mem_attrib
*attrib
, struct target_ops
*target
)
966 return array_write_inferior_memory (memaddr
, myaddr
, len
);
968 return array_read_inferior_memory (memaddr
, myaddr
, len
);
972 array_kill (char *args
, int from_tty
)
974 return; /* ignore attempts to kill target system */
977 /* Clean up when a program exits.
978 The program actually lives on in the remote processor's RAM, and may be
979 run again without a download. Don't leave it full of breakpoint
983 array_mourn_inferior (void)
985 remove_breakpoints ();
986 generic_mourn_inferior (); /* Do all the proper things now */
989 #define MAX_ARRAY_BREAKPOINTS 16
991 static CORE_ADDR breakaddr
[MAX_ARRAY_BREAKPOINTS
] =
995 * array_insert_breakpoint -- add a breakpoint
998 array_insert_breakpoint (CORE_ADDR addr
, char *shadow
)
1002 CORE_ADDR bp_addr
= addr
;
1004 debuglogs (1, "array_insert_breakpoint() addr = 0x%x", addr
);
1005 BREAKPOINT_FROM_PC (&bp_addr
, &bp_size
);
1007 for (i
= 0; i
<= MAX_ARRAY_BREAKPOINTS
; i
++)
1009 if (breakaddr
[i
] == 0)
1011 breakaddr
[i
] = addr
;
1012 if (sr_get_debug () > 4)
1013 printf ("Breakpoint at %s\n", paddr_nz (addr
));
1014 array_read_inferior_memory (bp_addr
, shadow
, bp_size
);
1015 printf_monitor ("b 0x%x\n", addr
);
1021 fprintf_unfiltered (gdb_stderr
, "Too many breakpoints (> 16) for monitor\n");
1026 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1029 array_remove_breakpoint (CORE_ADDR addr
, char *shadow
)
1033 debuglogs (1, "array_remove_breakpoint() addr = 0x%x", addr
);
1035 for (i
= 0; i
< MAX_ARRAY_BREAKPOINTS
; i
++)
1037 if (breakaddr
[i
] == addr
)
1040 /* some monitors remove breakpoints based on the address */
1041 printf_monitor ("bd %x\n", i
);
1046 fprintf_unfiltered (gdb_stderr
,
1047 "Can't find breakpoint associated with 0x%s\n",
1055 debuglogs (1, "array_stop()");
1056 printf_monitor ("\003");
1061 * array_command -- put a command string, in args, out to MONITOR.
1062 * Output from MONITOR is placed on the users terminal until the
1063 * expect_prompt is seen. FIXME
1066 monitor_command (char *args
, int fromtty
)
1068 debuglogs (1, "monitor_command (args=%s)", args
);
1070 if (array_desc
== NULL
)
1071 error ("monitor target not open.");
1074 error ("Missing command.");
1076 printf_monitor ("%s\n", args
);
1081 * make_gdb_packet -- make a GDB packet. The data is always ASCII.
1082 * A debug packet whose contents are <data>
1083 * is encapsulated for transmission in the form:
1085 * $ <data> # CSUM1 CSUM2
1087 * <data> must be ASCII alphanumeric and cannot include characters
1088 * '$' or '#'. If <data> starts with two characters followed by
1089 * ':', then the existing stubs interpret this as a sequence number.
1091 * CSUM1 and CSUM2 are ascii hex representation of an 8-bit
1092 * checksum of <data>, the most significant nibble is sent first.
1093 * the hex digits 0-9,a-f are used.
1097 make_gdb_packet (char *buf
, char *data
)
1100 unsigned char csum
= 0;
1104 debuglogs (3, "make_gdb_packet(%s)\n", data
);
1105 cnt
= strlen (data
);
1107 error ("make_gdb_packet(): to much data\n");
1109 /* start with the packet header */
1113 /* calculate the checksum */
1114 for (i
= 0; i
< cnt
; i
++)
1120 /* terminate the data with a '#' */
1123 /* add the checksum as two ascii digits */
1124 *p
++ = tohex ((csum
>> 4) & 0xf);
1125 *p
++ = tohex (csum
& 0xf);
1126 *p
= 0x0; /* Null terminator on string */
1130 * array_send_packet -- send a GDB packet to the target with error handling. We
1131 * get a '+' (ACK) back if the packet is received and the checksum
1132 * matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a
1133 * successful transmition, or a 0 for a failure.
1136 array_send_packet (char *packet
)
1144 /* scan the packet to make sure it only contains valid characters.
1145 this may sound silly, but sometimes a garbled packet will hang
1146 the target board. We scan the whole thing, then print the error
1149 for (i
= 0; i
< strlen (packet
); i
++)
1151 debuglogs (5, "array_send_packet(): Scanning \'%c\'\n", packet
[i
]);
1152 /* legit hex numbers or command */
1153 if ((isxdigit (packet
[i
])) || (isalpha (packet
[i
])))
1159 case '#': /* end of packet */
1160 case '$': /* start of packet */
1162 default: /* bogus character */
1164 debuglogs (4, "array_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet
[i
]);
1170 error ("Can't send packet, found %d non-ascii characters", retries
);
1172 /* ok, try to send the packet */
1174 while (retries
++ <= 10)
1176 printf_monitor ("%s", packet
);
1178 /* read until either a timeout occurs (-2) or '+' is read */
1179 while (retries
<= 10)
1181 c
= readchar (-timeout
);
1182 debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c
);
1186 debuglogs (3, "Got Ack\n");
1188 case SERIAL_TIMEOUT
:
1189 debuglogs (3, "Timed out reading serial port\n");
1190 printf_monitor ("@"); /* resync with the monitor */
1191 expect_prompt (1); /* See if we get a expect_prompt */
1192 break; /* Retransmit buffer */
1194 debuglogs (3, "Got NAK\n");
1195 printf_monitor ("@"); /* resync with the monitor */
1196 expect_prompt (1); /* See if we get a expect_prompt */
1199 /* it's probably an old response, or the echo of our command.
1200 * just gobble up the packet and ignore it.
1202 debuglogs (3, "Got a junk packet\n");
1206 c
= readchar (timeout
);
1210 c
= readchar (timeout
);
1212 c
= readchar (timeout
);
1215 debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk
);
1216 continue; /* Now, go look for next packet */
1221 debuglogs (3, "Retransmitting packet \"%s\"\n", packet
);
1222 break; /* Here to retransmit */
1229 * array_get_packet -- get a GDB packet from the target. Basically we read till we
1230 * see a '#', then check the checksum. It returns a 1 if it's gotten a
1231 * packet, or a 0 it the packet wasn't transmitted correctly.
1234 array_get_packet (char *packet
)
1239 unsigned char pktcsum
;
1245 memset (packet
, 1, PBUFSIZ
);
1247 while (retries
<= 10)
1251 c
= readchar (timeout
);
1252 if (c
== SERIAL_TIMEOUT
)
1254 debuglogs (3, "array_get_packet: got time out from serial port.\n");
1256 debuglogs (3, "Waiting for a '$', got a %c\n", c
);
1261 while (retries
<= 10)
1263 c
= readchar (timeout
);
1264 debuglogs (3, "array_get_packet: got a '%c'\n", c
);
1267 case SERIAL_TIMEOUT
:
1268 debuglogs (3, "Timeout in mid-packet, retrying\n");
1271 debuglogs (3, "Saw new packet start in middle of old one\n");
1272 return 0; /* Start a new packet, count retries */
1275 pktcsum
= from_hex (readchar (timeout
)) << 4;
1276 pktcsum
|= from_hex (readchar (timeout
));
1278 debuglogs (3, "\nGDB packet checksum zero, must be a bogus packet\n");
1279 if (csum
== pktcsum
)
1281 debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet
);
1282 printf_monitor ("@");
1286 debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum
, csum
);
1288 case '*': /* Run length encoding */
1289 debuglogs (5, "Run length encoding in packet\n");
1291 c
= readchar (timeout
);
1293 c
= c
- ' ' + 3; /* Compute repeat count */
1295 if (c
> 0 && c
< 255 && bp
+ c
- 1 < packet
+ PBUFSIZ
- 1)
1297 memset (bp
, *(bp
- 1), c
);
1302 printf_filtered ("Repeat count %d too large for buffer.\n", c
);
1306 if ((!isxdigit (c
)) && (!ispunct (c
)))
1307 debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c
);
1308 if (bp
< packet
+ PBUFSIZ
- 1)
1316 puts_filtered ("Remote packet too long.\n");
1321 return 0; /* exceeded retries */
1325 * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
1327 static unsigned long
1328 ascii2hexword (unsigned char *mem
)
1335 for (i
= 0; i
< 8; i
++)
1338 if (mem
[i
] >= 'A' && mem
[i
] <= 'F')
1339 val
= val
+ mem
[i
] - 'A' + 10;
1340 if (mem
[i
] >= 'a' && mem
[i
] <= 'f')
1341 val
= val
+ mem
[i
] - 'a' + 10;
1342 if (mem
[i
] >= '0' && mem
[i
] <= '9')
1343 val
= val
+ mem
[i
] - '0';
1347 debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val
, buf
, mem
);
1352 * ascii2hexword -- convert a hex value to an ascii number represented by 8
1356 hexword2ascii (unsigned char *mem
, unsigned long num
)
1361 debuglogs (4, "hexword2ascii() converting %x ", num
);
1362 for (i
= 7; i
>= 0; i
--)
1364 mem
[i
] = tohex ((num
>> 4) & 0xf);
1365 mem
[i
] = tohex (num
& 0xf);
1369 debuglogs (4, "\tto a %s", mem
);
1372 /* Convert hex digit A to a number. */
1379 debuglogs (4, "from_hex got a 0x%x(%c)\n", a
, a
);
1380 if (a
>= '0' && a
<= '9')
1382 if (a
>= 'a' && a
<= 'f')
1383 return a
- 'a' + 10;
1384 if (a
>= 'A' && a
<= 'F')
1385 return a
- 'A' + 10;
1388 error ("Reply contains invalid hex digit 0x%x", a
);
1392 /* Convert number NIB to a hex digit. */
1399 return 'a' + nib
- 10;
1403 * _initialize_remote_monitors -- setup a few addtitional commands that
1404 * are usually only used by monitors.
1407 _initialize_remote_monitors (void)
1409 /* generic monitor command */
1410 add_com ("monitor", class_obscure
, monitor_command
,
1411 "Send a command to the debug monitor.");
1416 * _initialize_array -- do any special init stuff for the target.
1419 _initialize_array (void)
1422 add_target (&array_ops
);