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.
32 #ifdef ANSI_PROTOTYPES
39 #include <sys/types.h>
40 #include "gdb_string.h"
44 #include "remote-utils.h"
48 #define ARRAY_PROMPT ">> "
50 #define SWAP_TARGET_AND_HOST(buffer,len) \
53 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
56 char *p = (char *)(buffer); \
57 char *q = ((char *)(buffer)) + len - 1; \
58 for (; p < q; p++, q--) \
68 static void debuglogs
PARAMS ((int, char *,...));
69 static void array_open ();
70 static void array_close ();
71 static void array_detach ();
72 static void array_attach ();
73 static void array_resume ();
74 static void array_fetch_register ();
75 static void array_store_register ();
76 static void array_fetch_registers ();
77 static void array_store_registers ();
78 static void array_prepare_to_store ();
79 static void array_files_info ();
80 static void array_kill ();
81 static void array_create_inferior ();
82 static void array_mourn_inferior ();
83 static void make_gdb_packet ();
84 static int array_xfer_memory ();
85 static int array_wait ();
86 static int array_insert_breakpoint ();
87 static int array_remove_breakpoint ();
90 static int from_hex ();
91 static int array_send_packet ();
92 static int array_get_packet ();
93 static unsigned long ascii2hexword ();
94 static void hexword2ascii ();
98 #define LOG_FILE "monitor.log"
99 #if defined (LOG_FILE)
103 static int timeout
= 30;
104 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
105 and i386-stub.c. Normally, no one would notice because it only matters
106 for writing large chunks of memory (e.g. in downloads). Also, this needs
107 to be more than 400 if required to hold the registers (see below, where
108 we round it up based on REGISTER_BYTES). */
112 * Descriptor for I/O to remote machine. Initialize it to NULL so that
113 * array_open knows that we don't have a file open when the program starts.
115 serial_t array_desc
= NULL
;
118 * this array of registers need to match the indexes used by GDB. The
119 * whole reason this exists is cause the various ROM monitors use
120 * different strings than GDB does, and doesn't support all the
121 * registers either. So, typing "info reg sp" becomes a "r30".
123 extern char *tmp_mips_processor_type
;
124 extern int mips_set_processor_type ();
126 static struct target_ops array_ops
;
129 init_array_ops (void)
131 array_ops
.to_shortname
= "array";
132 array_ops
.to_longname
=
133 "Debug using the standard GDB remote protocol for the Array Tech target.",
135 "Debug using the standard GDB remote protocol for the Array Tech target.\n\
136 Specify the serial device it is connected to (e.g. /dev/ttya).";
137 array_ops
.to_open
= array_open
;
138 array_ops
.to_close
= array_close
;
139 array_ops
.to_attach
= NULL
;
140 array_ops
.to_post_attach
= NULL
;
141 array_ops
.to_require_attach
= NULL
;
142 array_ops
.to_detach
= array_detach
;
143 array_ops
.to_require_detach
= NULL
;
144 array_ops
.to_resume
= array_resume
;
145 array_ops
.to_wait
= array_wait
;
146 array_ops
.to_post_wait
= NULL
;
147 array_ops
.to_fetch_registers
= array_fetch_registers
;
148 array_ops
.to_store_registers
= array_store_registers
;
149 array_ops
.to_prepare_to_store
= array_prepare_to_store
;
150 array_ops
.to_xfer_memory
= array_xfer_memory
;
151 array_ops
.to_files_info
= array_files_info
;
152 array_ops
.to_insert_breakpoint
= array_insert_breakpoint
;
153 array_ops
.to_remove_breakpoint
= array_remove_breakpoint
;
154 array_ops
.to_terminal_init
= 0;
155 array_ops
.to_terminal_inferior
= 0;
156 array_ops
.to_terminal_ours_for_output
= 0;
157 array_ops
.to_terminal_ours
= 0;
158 array_ops
.to_terminal_info
= 0;
159 array_ops
.to_kill
= array_kill
;
160 array_ops
.to_load
= 0;
161 array_ops
.to_lookup_symbol
= 0;
162 array_ops
.to_create_inferior
= array_create_inferior
;
163 array_ops
.to_post_startup_inferior
= NULL
;
164 array_ops
.to_acknowledge_created_inferior
= NULL
;
165 array_ops
.to_clone_and_follow_inferior
= NULL
;
166 array_ops
.to_post_follow_inferior_by_clone
= NULL
;
167 array_ops
.to_insert_fork_catchpoint
= NULL
;
168 array_ops
.to_remove_fork_catchpoint
= NULL
;
169 array_ops
.to_insert_vfork_catchpoint
= NULL
;
170 array_ops
.to_remove_vfork_catchpoint
= NULL
;
171 array_ops
.to_has_forked
= NULL
;
172 array_ops
.to_has_vforked
= NULL
;
173 array_ops
.to_can_follow_vfork_prior_to_exec
= NULL
;
174 array_ops
.to_post_follow_vfork
= NULL
;
175 array_ops
.to_insert_exec_catchpoint
= NULL
;
176 array_ops
.to_remove_exec_catchpoint
= NULL
;
177 array_ops
.to_has_execd
= NULL
;
178 array_ops
.to_reported_exec_events_per_exec_call
= NULL
;
179 array_ops
.to_has_exited
= NULL
;
180 array_ops
.to_mourn_inferior
= array_mourn_inferior
;
181 array_ops
.to_can_run
= 0;
182 array_ops
.to_notice_signals
= 0;
183 array_ops
.to_thread_alive
= 0;
184 array_ops
.to_stop
= 0;
185 array_ops
.to_pid_to_exec_file
= NULL
;
186 array_ops
.to_core_file_to_sym_file
= NULL
;
187 array_ops
.to_stratum
= process_stratum
;
188 array_ops
.DONT_USE
= 0;
189 array_ops
.to_has_all_memory
= 1;
190 array_ops
.to_has_memory
= 1;
191 array_ops
.to_has_stack
= 1;
192 array_ops
.to_has_registers
= 1;
193 array_ops
.to_has_execution
= 1;
194 array_ops
.to_sections
= 0;
195 array_ops
.to_sections_end
= 0;
196 array_ops
.to_magic
= OPS_MAGIC
;
200 * printf_monitor -- send data to monitor. Works just like printf.
203 #ifdef ANSI_PROTOTYPES
204 printf_monitor (char *pattern
,...)
206 printf_monitor (va_alist
)
214 #ifdef ANSI_PROTOTYPES
215 va_start (args
, pattern
);
219 pattern
= va_arg (args
, char *);
222 vsprintf (buf
, pattern
, args
);
224 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf
);
226 if (strlen (buf
) > PBUFSIZ
)
227 error ("printf_monitor(): string too long");
228 if (SERIAL_WRITE (array_desc
, buf
, strlen (buf
)))
229 fprintf (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
232 * write_monitor -- send raw data to monitor.
235 write_monitor (data
, len
)
239 if (SERIAL_WRITE (array_desc
, data
, len
))
240 fprintf (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
242 *(data
+ len
+ 1) = '\0';
243 debuglogs (1, "write_monitor(), Sending: \"%s\".", data
);
248 * debuglogs -- deal with debugging info to multiple sources. This takes
249 * two real args, the first one is the level to be compared against
250 * the sr_get_debug() value, the second arg is a printf buffer and args
251 * to be formatted and printed. A CR is added after each string is printed.
254 #ifdef ANSI_PROTOTYPES
255 debuglogs (int level
, char *pattern
,...)
263 unsigned char buf
[PBUFSIZ
];
264 char newbuf
[PBUFSIZ
];
267 #ifdef ANSI_PROTOTYPES
268 va_start (args
, pattern
);
273 level
= va_arg (args
, int); /* get the debug level */
274 pattern
= va_arg (args
, char *); /* get the printf style pattern */
277 if ((level
< 0) || (level
> 100))
279 error ("Bad argument passed to debuglogs(), needs debug level");
283 vsprintf (buf
, pattern
, args
); /* format the string */
285 /* convert some characters so it'll look right in the log */
287 for (i
= 0; buf
[i
] != '\0'; i
++)
290 error ("Debug message too long");
293 case '\n': /* newlines */
297 case '\r': /* carriage returns */
301 case '\033': /* escape */
309 case '\b': /* backspace */
313 default: /* no change */
318 { /* modify control characters */
324 { /* modify control characters */
330 *p
= '\0'; /* terminate the string */
332 if (sr_get_debug () > level
)
333 printf_unfiltered ("%s\n", newbuf
);
335 #ifdef LOG_FILE /* write to the monitor log */
338 fputs (newbuf
, log_file
);
339 fputc ('\n', log_file
);
345 /* readchar -- read a character from the remote system, doing all the fancy
354 c
= SERIAL_READCHAR (array_desc
, abs (timeout
));
356 if (sr_get_debug () > 5)
359 debuglogs (5, "readchar: timeout = %d\n", timeout
);
364 putc (c
& 0x7f, log_file
);
370 if (c
== SERIAL_TIMEOUT
)
373 return c
; /* Polls shouldn't generate timeout errors */
374 error ("Timeout reading from remote system.");
376 fputs ("ERROR: Timeout reading from remote system", log_file
);
379 perror_with_name ("readchar");
383 * expect -- scan input from the remote system, until STRING is found.
384 * If DISCARD is non-zero, then discard non-matching input, else print
385 * it out. Let the user break out immediately.
388 expect (string
, discard
)
396 debuglogs (1, "Expecting \"%s\".", string
);
401 c
= readchar (timeout
);
409 debuglogs (4, "Matched");
417 fputc_unfiltered (c
, gdb_stdout
);
424 /* Keep discarding input until we see the MONITOR array_cmds->prompt.
426 The convention for dealing with the expect_prompt is that you
428 o *then* wait for the expect_prompt.
430 Thus the last thing that a procedure does with the serial line
431 will be an expect_prompt(). Exception: array_resume does not
432 wait for the expect_prompt, because the terminal is being handed over
433 to the inferior. However, the next thing which happens after that
434 is a array_wait which does wait for the expect_prompt.
435 Note that this includes abnormal exit, e.g. error(). This is
436 necessary to prevent getting into states from which we can't
439 expect_prompt (discard
)
442 expect (ARRAY_PROMPT
, discard
);
446 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
460 if (sr_get_debug () > 5)
461 debuglogs (5, "Ignoring \'%c\'.", ch
);
464 if (sr_get_debug () > 5)
465 debuglogs (5, "Accepting \'%c\'.", ch
);
471 * get_hex_digit -- Get a hex digit from the remote system & return its value.
472 * If ignore is nonzero, ignore spaces, newline & tabs.
475 get_hex_digit (ignore
)
481 ch
= readchar (timeout
);
484 if (sr_get_debug () > 4)
486 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch
, ch
);
490 #ifdef LOG_FILE /* write to the monitor log */
493 fputs ("get_hex_digit() got a 0x", log_file
);
494 fputc (ch
, log_file
);
495 fputc ('\n', log_file
);
501 if (ch
>= '0' && ch
<= '9')
503 else if (ch
>= 'A' && ch
<= 'F')
504 return ch
- 'A' + 10;
505 else if (ch
>= 'a' && ch
<= 'f')
506 return ch
- 'a' + 10;
507 else if (ch
== ' ' && ignore
)
512 debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch
);
513 error ("Invalid hex digit from remote system. (0x%x)", ch
);
518 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
519 * Accept any number leading spaces.
527 val
= get_hex_digit (1) << 4;
528 debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val
);
530 val
|= get_hex_digit (0);
531 debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val
);
534 debuglogs (4, "get_hex_byte() -- Read a 0x%x", val
);
538 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
539 * and put them in registers starting at REGNO.
550 if (HOST_BYTE_ORDER
== BIG_ENDIAN
)
553 for (i
= 0; i
< 8; i
++)
554 val
= (val
<< 4) + get_hex_digit (i
== 0);
559 for (i
= 7; i
>= 0; i
--)
560 val
= (val
<< 4) + get_hex_digit (i
== 0);
564 debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val
, (HOST_BYTE_ORDER
== BIG_ENDIAN
) ? "big endian" : "little endian");
569 /* This is called not only when we first attach, but also when the
570 user types "run" after having attached. */
572 array_create_inferior (execfile
, args
, env
)
580 error ("Can't pass arguments to remote MONITOR process");
582 if (execfile
== 0 || exec_bfd
== 0)
583 error ("No executable file specified");
585 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
587 /* The "process" (board) is already stopped awaiting our commands, and
588 the program is already downloaded. We just set its PC and go. */
590 clear_proceed_status ();
592 /* Tell wait_for_inferior that we've started a new process. */
593 init_wait_for_inferior ();
595 /* Set up the "saved terminal modes" of the inferior
596 based on what modes we are starting it with. */
597 target_terminal_init ();
599 /* Install inferior's terminal modes. */
600 target_terminal_inferior ();
602 /* insert_step_breakpoint (); FIXME, do we need this? */
605 proceed ((CORE_ADDR
) entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
609 * array_open -- open a connection to a remote debugger.
610 * NAME is the filename used for communication.
612 static int baudrate
= 9600;
613 static char dev_name
[100];
616 array_open (args
, name
, from_tty
)
621 char packet
[PBUFSIZ
];
624 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
625 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
630 target_preopen (from_tty
);
631 unpush_target (&array_ops
);
633 tmp_mips_processor_type
= "lsi33k"; /* change the default from r3051 */
634 mips_set_processor_type_command ("lsi33k", 0);
636 strcpy (dev_name
, args
);
637 array_desc
= SERIAL_OPEN (dev_name
);
639 if (array_desc
== NULL
)
640 perror_with_name (dev_name
);
644 if (SERIAL_SETBAUDRATE (array_desc
, baud_rate
))
646 SERIAL_CLOSE (array_desc
);
647 perror_with_name (name
);
651 SERIAL_RAW (array_desc
);
653 #if defined (LOG_FILE)
654 log_file
= fopen (LOG_FILE
, "w");
655 if (log_file
== NULL
)
656 perror_with_name (LOG_FILE
);
657 fprintf (log_file
, "GDB %s (%s", version
);
658 fprintf (log_file
, " --target %s)\n", array_ops
.to_shortname
);
659 fprintf (log_file
, "Remote target %s connected to %s\n\n", array_ops
.to_shortname
, dev_name
);
662 /* see if the target is alive. For a ROM monitor, we can just try to force the
663 expect_prompt to print a few times. For the GDB remote protocol, the application
664 being debugged is sitting at a breakpoint and waiting for GDB to initialize
665 the connection. We force it to give us an empty packet to see if it's alive.
667 debuglogs (3, "Trying to ACK the target's debug stub");
668 /* unless your are on the new hardware, the old board won't initialize
669 because the '@' doesn't flush output like it does on the new ROMS.
671 printf_monitor ("@"); /* ask for the last signal */
672 expect_prompt (1); /* See if we get a expect_prompt */
673 #ifdef TEST_ARRAY /* skip packet for testing */
674 make_gdb_packet (packet
, "?"); /* ask for a bogus packet */
675 if (array_send_packet (packet
) == 0)
676 error ("Couldn't transmit packet\n");
677 printf_monitor ("@\n"); /* force it to flush stdout */
678 expect_prompt (1); /* See if we get a expect_prompt */
680 push_target (&array_ops
);
682 printf ("Remote target %s connected to %s\n", array_ops
.to_shortname
, dev_name
);
686 * array_close -- Close out all files and local state before this
687 * target loses control.
691 array_close (quitting
)
694 SERIAL_CLOSE (array_desc
);
697 debuglogs (1, "array_close (quitting=%d)", quitting
);
699 #if defined (LOG_FILE)
702 if (ferror (log_file
))
703 printf_filtered ("Error writing log file.\n");
704 if (fclose (log_file
) != 0)
705 printf_filtered ("Error closing log file.\n");
711 * array_detach -- terminate the open connection to the remote
712 * debugger. Use this when you want to detach and do something
713 * else with your gdb.
716 array_detach (from_tty
)
720 debuglogs (1, "array_detach ()");
722 pop_target (); /* calls array_close to do the real work */
724 printf ("Ending remote %s debugging\n", target_shortname
);
728 * array_attach -- attach GDB to the target.
731 array_attach (args
, from_tty
)
736 printf ("Starting remote %s debugging\n", target_shortname
);
738 debuglogs (1, "array_attach (args=%s)", args
);
740 printf_monitor ("go %x\n");
741 /* swallow the echo. */
742 expect ("go %x\n", 1);
746 * array_resume -- Tell the remote machine to resume.
749 array_resume (pid
, step
, sig
)
751 enum target_signal sig
;
753 debuglogs (1, "array_resume (step=%d, sig=%d)", step
, sig
);
757 printf_monitor ("s\n");
761 printf_monitor ("go\n");
768 * array_wait -- Wait until the remote machine stops, then return,
769 * storing status in status just as `wait' would.
772 array_wait (pid
, status
)
774 struct target_waitstatus
*status
;
776 int old_timeout
= timeout
;
780 serial_ttystate ttystate
;
782 debuglogs (1, "array_wait (), printing extraneous text.");
784 status
->kind
= TARGET_WAITKIND_EXITED
;
785 status
->value
.integer
= 0;
787 timeout
= 0; /* Don't time out -- user program is running. */
789 #if !defined(__GO32__) && !defined(__MSDOS__) && !defined(_WIN32)
790 tty_desc
= SERIAL_FDOPEN (0);
791 ttystate
= SERIAL_GET_TTY_STATE (tty_desc
);
792 SERIAL_RAW (tty_desc
);
795 /* poll on the serial port and the keyboard. */
798 c
= readchar (timeout
);
801 if (c
== *(ARRAY_PROMPT
+ i
))
803 if (++i
>= strlen (ARRAY_PROMPT
))
804 { /* matched the prompt */
805 debuglogs (4, "array_wait(), got the expect_prompt.");
810 { /* not the prompt */
813 fputc_unfiltered (c
, gdb_stdout
);
814 gdb_flush (gdb_stdout
);
816 c
= SERIAL_READCHAR (tty_desc
, timeout
);
819 SERIAL_WRITE (array_desc
, &c
, 1);
820 /* do this so it looks like there's keyboard echo */
821 if (c
== 3) /* exit on Control-C */
824 fputc_unfiltered (c
, gdb_stdout
);
825 gdb_flush (gdb_stdout
);
829 SERIAL_SET_TTY_STATE (tty_desc
, ttystate
);
832 debuglogs (4, "array_wait(), got the expect_prompt.");
835 status
->kind
= TARGET_WAITKIND_STOPPED
;
836 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
838 timeout
= old_timeout
;
844 * array_fetch_registers -- read the remote registers into the
848 array_fetch_registers (ignored
)
853 unsigned char packet
[PBUFSIZ
];
854 char regs
[REGISTER_BYTES
];
856 debuglogs (1, "array_fetch_registers (ignored=%d)\n", ignored
);
858 memset (packet
, 0, PBUFSIZ
);
859 /* Unimplemented registers read as all bits zero. */
860 memset (regs
, 0, REGISTER_BYTES
);
861 make_gdb_packet (packet
, "g");
862 if (array_send_packet (packet
) == 0)
863 error ("Couldn't transmit packet\n");
864 if (array_get_packet (packet
) == 0)
865 error ("Couldn't receive packet\n");
866 /* FIXME: read bytes from packet */
867 debuglogs (4, "array_fetch_registers: Got a \"%s\" back\n", packet
);
868 for (regno
= 0; regno
<= PC_REGNUM
+ 4; regno
++)
870 /* supply register stores in target byte order, so swap here */
871 /* FIXME: convert from ASCII hex to raw bytes */
872 i
= ascii2hexword (packet
+ (regno
* 8));
873 debuglogs (5, "Adding register %d = %x\n", regno
, i
);
874 SWAP_TARGET_AND_HOST (&i
, 4);
875 supply_register (regno
, (char *) &i
);
880 * This is unused by targets like this one that use a
881 * protocol based on GDB's remote protocol.
884 array_fetch_register (ignored
)
887 array_fetch_registers ();
891 * Get all the registers from the targets. They come back in a large array.
894 array_store_registers (ignored
)
899 char packet
[PBUFSIZ
];
903 debuglogs (1, "array_store_registers()");
905 memset (packet
, 0, PBUFSIZ
);
906 memset (buf
, 0, PBUFSIZ
);
909 /* Unimplemented registers read as all bits zero. */
910 /* FIXME: read bytes from packet */
911 for (regno
= 0; regno
< 41; regno
++)
913 /* supply register stores in target byte order, so swap here */
914 /* FIXME: convert from ASCII hex to raw bytes */
915 i
= (unsigned long) read_register (regno
);
916 hexword2ascii (num
, i
);
917 strcpy (buf
+ (regno
* 8) + 1, num
);
919 *(buf
+ (regno
* 8) + 2) = 0;
920 make_gdb_packet (packet
, buf
);
921 if (array_send_packet (packet
) == 0)
922 error ("Couldn't transmit packet\n");
923 if (array_get_packet (packet
) == 0)
924 error ("Couldn't receive packet\n");
926 registers_changed ();
930 * This is unused by targets like this one that use a
931 * protocol based on GDB's remote protocol.
934 array_store_register (ignored
)
937 array_store_registers ();
940 /* Get ready to modify the registers array. On machines which store
941 individual registers, this doesn't need to do anything. On machines
942 which store all the registers in one fell swoop, this makes sure
943 that registers contains all the registers from the program being
947 array_prepare_to_store ()
949 /* Do nothing, since we can store individual regs */
955 printf ("\tAttached to %s at %d baud.\n",
960 * array_write_inferior_memory -- Copy LEN bytes of data from debugger
961 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
964 array_write_inferior_memory (memaddr
, myaddr
, len
)
966 unsigned char *myaddr
;
971 char packet
[PBUFSIZ
];
976 debuglogs (1, "array_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
977 memset (buf
, '\0', PBUFSIZ
); /* this also sets the string terminator */
980 *p
++ = 'M'; /* The command to write memory */
981 hexword2ascii (num
, memaddr
); /* convert the address */
982 strcpy (p
, num
); /* copy the address */
984 *p
++ = ','; /* add comma delimeter */
985 hexword2ascii (num
, len
); /* Get the length as a 4 digit number */
990 *p
++ = ':'; /* add the colon delimeter */
991 for (j
= 0; j
< len
; j
++)
992 { /* copy the data in after converting it */
993 *p
++ = tohex ((myaddr
[j
] >> 4) & 0xf);
994 *p
++ = tohex (myaddr
[j
] & 0xf);
997 make_gdb_packet (packet
, buf
);
998 if (array_send_packet (packet
) == 0)
999 error ("Couldn't transmit packet\n");
1000 if (array_get_packet (packet
) == 0)
1001 error ("Couldn't receive packet\n");
1007 * array_read_inferior_memory -- read LEN bytes from inferior memory
1008 * at MEMADDR. Put the result at debugger address MYADDR. Returns
1012 array_read_inferior_memory (memaddr
, myaddr
, len
)
1019 char packet
[PBUFSIZ
];
1020 int count
; /* Number of bytes read so far. */
1021 unsigned long startaddr
; /* Starting address of this pass. */
1022 int len_this_pass
; /* Number of bytes to read in this pass. */
1024 debuglogs (1, "array_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr
, myaddr
, len
);
1026 /* Note that this code works correctly if startaddr is just less
1027 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
1028 thing). That is, something like
1029 array_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
1030 works--it never adds len To memaddr and gets 0. */
1031 /* However, something like
1032 array_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
1033 doesn't need to work. Detect it and give up if there's an attempt
1035 if (((memaddr
- 1) + len
) < memaddr
)
1041 for (count
= 0, startaddr
= memaddr
; count
< len
; startaddr
+= len_this_pass
)
1043 /* Try to align to 16 byte boundry (why?) */
1045 if ((startaddr
% 16) != 0)
1047 len_this_pass
-= startaddr
% 16;
1049 /* Only transfer bytes we need */
1050 if (len_this_pass
> (len
- count
))
1052 len_this_pass
= (len
- count
);
1054 /* Fetch the bytes */
1055 debuglogs (3, "read %d bytes from inferior address %x", len_this_pass
,
1057 sprintf (buf
, "m%08x,%04x", startaddr
, len_this_pass
);
1058 make_gdb_packet (packet
, buf
);
1059 if (array_send_packet (packet
) == 0)
1061 error ("Couldn't transmit packet\n");
1063 if (array_get_packet (packet
) == 0)
1065 error ("Couldn't receive packet\n");
1069 error ("Got no data in the GDB packet\n");
1071 /* Pick packet apart and xfer bytes to myaddr */
1072 debuglogs (4, "array_read_inferior_memory: Got a \"%s\" back\n", packet
);
1073 for (j
= 0; j
< len_this_pass
; j
++)
1075 /* extract the byte values */
1076 myaddr
[count
++] = from_hex (*(packet
+ (j
* 2))) * 16 + from_hex (*(packet
+ (j
* 2) + 1));
1077 debuglogs (5, "myaddr[%d] set to %x\n", count
- 1, myaddr
[count
- 1]);
1083 /* FIXME-someday! merge these two. */
1085 array_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
1090 struct target_ops
*target
; /* ignored */
1093 return array_write_inferior_memory (memaddr
, myaddr
, len
);
1095 return array_read_inferior_memory (memaddr
, myaddr
, len
);
1099 array_kill (args
, from_tty
)
1103 return; /* ignore attempts to kill target system */
1106 /* Clean up when a program exits.
1107 The program actually lives on in the remote processor's RAM, and may be
1108 run again without a download. Don't leave it full of breakpoint
1112 array_mourn_inferior ()
1114 remove_breakpoints ();
1115 generic_mourn_inferior (); /* Do all the proper things now */
1118 #define MAX_ARRAY_BREAKPOINTS 16
1120 static CORE_ADDR breakaddr
[MAX_ARRAY_BREAKPOINTS
] =
1124 * array_insert_breakpoint -- add a breakpoint
1127 array_insert_breakpoint (addr
, shadow
)
1133 CORE_ADDR bp_addr
= addr
;
1135 debuglogs (1, "array_insert_breakpoint() addr = 0x%x", addr
);
1136 BREAKPOINT_FROM_PC (&bp_addr
, &bp_size
);
1138 for (i
= 0; i
<= MAX_ARRAY_BREAKPOINTS
; i
++)
1140 if (breakaddr
[i
] == 0)
1142 breakaddr
[i
] = addr
;
1143 if (sr_get_debug () > 4)
1144 printf ("Breakpoint at %x\n", addr
);
1145 array_read_inferior_memory (bp_addr
, shadow
, bp_size
);
1146 printf_monitor ("b 0x%x\n", addr
);
1152 fprintf (stderr
, "Too many breakpoints (> 16) for monitor\n");
1157 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1160 array_remove_breakpoint (addr
, shadow
)
1166 debuglogs (1, "array_remove_breakpoint() addr = 0x%x", addr
);
1168 for (i
= 0; i
< MAX_ARRAY_BREAKPOINTS
; i
++)
1170 if (breakaddr
[i
] == addr
)
1173 /* some monitors remove breakpoints based on the address */
1174 printf_monitor ("bd %x\n", i
);
1179 fprintf (stderr
, "Can't find breakpoint associated with 0x%x\n", addr
);
1186 debuglogs (1, "array_stop()");
1187 printf_monitor ("\003");
1192 * array_command -- put a command string, in args, out to MONITOR.
1193 * Output from MONITOR is placed on the users terminal until the
1194 * expect_prompt is seen. FIXME
1197 monitor_command (args
, fromtty
)
1201 debuglogs (1, "monitor_command (args=%s)", args
);
1203 if (array_desc
== NULL
)
1204 error ("monitor target not open.");
1207 error ("Missing command.");
1209 printf_monitor ("%s\n", args
);
1214 * make_gdb_packet -- make a GDB packet. The data is always ASCII.
1215 * A debug packet whose contents are <data>
1216 * is encapsulated for transmission in the form:
1218 * $ <data> # CSUM1 CSUM2
1220 * <data> must be ASCII alphanumeric and cannot include characters
1221 * '$' or '#'. If <data> starts with two characters followed by
1222 * ':', then the existing stubs interpret this as a sequence number.
1224 * CSUM1 and CSUM2 are ascii hex representation of an 8-bit
1225 * checksum of <data>, the most significant nibble is sent first.
1226 * the hex digits 0-9,a-f are used.
1230 make_gdb_packet (buf
, data
)
1234 unsigned char csum
= 0;
1238 debuglogs (3, "make_gdb_packet(%s)\n", data
);
1239 cnt
= strlen (data
);
1241 error ("make_gdb_packet(): to much data\n");
1243 /* start with the packet header */
1247 /* calculate the checksum */
1248 for (i
= 0; i
< cnt
; i
++)
1254 /* terminate the data with a '#' */
1257 /* add the checksum as two ascii digits */
1258 *p
++ = tohex ((csum
>> 4) & 0xf);
1259 *p
++ = tohex (csum
& 0xf);
1260 *p
= 0x0; /* Null terminator on string */
1264 * array_send_packet -- send a GDB packet to the target with error handling. We
1265 * get a '+' (ACK) back if the packet is received and the checksum
1266 * matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a
1267 * successful transmition, or a 0 for a failure.
1270 array_send_packet (packet
)
1279 /* scan the packet to make sure it only contains valid characters.
1280 this may sound silly, but sometimes a garbled packet will hang
1281 the target board. We scan the whole thing, then print the error
1284 for (i
= 0; i
< strlen (packet
); i
++)
1286 debuglogs (5, "array_send_packet(): Scanning \'%c\'\n", packet
[i
]);
1287 /* legit hex numbers or command */
1288 if ((isxdigit (packet
[i
])) || (isalpha (packet
[i
])))
1294 case '#': /* end of packet */
1295 case '$': /* start of packet */
1297 default: /* bogus character */
1299 debuglogs (4, "array_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet
[i
]);
1305 error ("Can't send packet, found %d non-ascii characters", retries
);
1307 /* ok, try to send the packet */
1309 while (retries
++ <= 10)
1311 printf_monitor ("%s", packet
);
1313 /* read until either a timeout occurs (-2) or '+' is read */
1314 while (retries
<= 10)
1316 c
= readchar (-timeout
);
1317 debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c
);
1321 debuglogs (3, "Got Ack\n");
1323 case SERIAL_TIMEOUT
:
1324 debuglogs (3, "Timed out reading serial port\n");
1325 printf_monitor ("@"); /* resync with the monitor */
1326 expect_prompt (1); /* See if we get a expect_prompt */
1327 break; /* Retransmit buffer */
1329 debuglogs (3, "Got NAK\n");
1330 printf_monitor ("@"); /* resync with the monitor */
1331 expect_prompt (1); /* See if we get a expect_prompt */
1334 /* it's probably an old response, or the echo of our command.
1335 * just gobble up the packet and ignore it.
1337 debuglogs (3, "Got a junk packet\n");
1341 c
= readchar (timeout
);
1345 c
= readchar (timeout
);
1347 c
= readchar (timeout
);
1350 debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk
);
1351 continue; /* Now, go look for next packet */
1356 debuglogs (3, "Retransmitting packet \"%s\"\n", packet
);
1357 break; /* Here to retransmit */
1364 * array_get_packet -- get a GDB packet from the target. Basically we read till we
1365 * see a '#', then check the checksum. It returns a 1 if it's gotten a
1366 * packet, or a 0 it the packet wasn't transmitted correctly.
1369 array_get_packet (packet
)
1375 unsigned char pktcsum
;
1381 memset (packet
, 1, PBUFSIZ
);
1383 while (retries
<= 10)
1387 c
= readchar (timeout
);
1388 if (c
== SERIAL_TIMEOUT
)
1390 debuglogs (3, "array_get_packet: got time out from serial port.\n");
1392 debuglogs (3, "Waiting for a '$', got a %c\n", c
);
1397 while (retries
<= 10)
1399 c
= readchar (timeout
);
1400 debuglogs (3, "array_get_packet: got a '%c'\n", c
);
1403 case SERIAL_TIMEOUT
:
1404 debuglogs (3, "Timeout in mid-packet, retrying\n");
1407 debuglogs (3, "Saw new packet start in middle of old one\n");
1408 return 0; /* Start a new packet, count retries */
1411 pktcsum
= from_hex (readchar (timeout
)) << 4;
1412 pktcsum
|= from_hex (readchar (timeout
));
1414 debuglogs (3, "\nGDB packet checksum zero, must be a bogus packet\n");
1415 if (csum
== pktcsum
)
1417 debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet
);
1418 printf_monitor ("@");
1422 debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum
, csum
);
1424 case '*': /* Run length encoding */
1425 debuglogs (5, "Run length encoding in packet\n");
1427 c
= readchar (timeout
);
1429 c
= c
- ' ' + 3; /* Compute repeat count */
1431 if (c
> 0 && c
< 255 && bp
+ c
- 1 < packet
+ PBUFSIZ
- 1)
1433 memset (bp
, *(bp
- 1), c
);
1438 printf_filtered ("Repeat count %d too large for buffer.\n", c
);
1442 if ((!isxdigit (c
)) && (!ispunct (c
)))
1443 debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c
);
1444 if (bp
< packet
+ PBUFSIZ
- 1)
1452 puts_filtered ("Remote packet too long.\n");
1457 return 0; /* exceeded retries */
1461 * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
1463 static unsigned long
1472 for (i
= 0; i
< 8; i
++)
1475 if (mem
[i
] >= 'A' && mem
[i
] <= 'F')
1476 val
= val
+ mem
[i
] - 'A' + 10;
1477 if (mem
[i
] >= 'a' && mem
[i
] <= 'f')
1478 val
= val
+ mem
[i
] - 'a' + 10;
1479 if (mem
[i
] >= '0' && mem
[i
] <= '9')
1480 val
= val
+ mem
[i
] - '0';
1484 debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val
, buf
, mem
);
1489 * ascii2hexword -- convert a hex value to an ascii number represented by 8
1493 hexword2ascii (mem
, num
)
1500 debuglogs (4, "hexword2ascii() converting %x ", num
);
1501 for (i
= 7; i
>= 0; i
--)
1503 mem
[i
] = tohex ((num
>> 4) & 0xf);
1504 mem
[i
] = tohex (num
& 0xf);
1508 debuglogs (4, "\tto a %s", mem
);
1511 /* Convert hex digit A to a number. */
1519 debuglogs (4, "from_hex got a 0x%x(%c)\n", a
, a
);
1520 if (a
>= '0' && a
<= '9')
1522 if (a
>= 'a' && a
<= 'f')
1523 return a
- 'a' + 10;
1524 if (a
>= 'A' && a
<= 'F')
1525 return a
- 'A' + 10;
1528 error ("Reply contains invalid hex digit 0x%x", a
);
1532 /* Convert number NIB to a hex digit. */
1540 return 'a' + nib
- 10;
1544 * _initialize_remote_monitors -- setup a few addtitional commands that
1545 * are usually only used by monitors.
1548 _initialize_remote_monitors ()
1550 /* generic monitor command */
1551 add_com ("monitor", class_obscure
, monitor_command
,
1552 "Send a command to the debug monitor.");
1557 * _initialize_array -- do any special init stuff for the target.
1560 _initialize_array ()
1563 add_target (&array_ops
);