1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1995, 1996
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
5 Resurrected from the ashes by Stu Grossman.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* This file was derived from various remote-* modules. It is a collection
24 of generic support functions so GDB can talk directly to a ROM based
25 monitor. This saves use from having to hack an exception based handler
26 into existance, and makes for quick porting.
28 This module talks to a debug monitor called 'MONITOR', which
29 We communicate with MONITOR via either a direct serial line, or a TCP
30 (or possibly TELNET) stream to a terminal multiplexor,
31 which in turn talks to the target board. */
33 /* FIXME 32x64: This code assumes that registers and addresses are at
34 most 32 bits long. If they can be larger, you will need to declare
35 values as LONGEST and use %llx or some such to print values when
36 building commands to send to the monitor. Since we don't know of
37 any actual 64-bit targets with ROM monitors that use this code,
38 it's not an issue right now. -sts 4/18/96 */
44 #ifdef ANSI_PROTOTYPES
51 #include "gdb_string.h"
52 #include <sys/types.h>
58 #include "gnu-regex.h"
62 static int readchar
PARAMS ((int timeout
));
64 static void monitor_command
PARAMS ((char *args
, int fromtty
));
66 static void monitor_fetch_register
PARAMS ((int regno
));
67 static void monitor_store_register
PARAMS ((int regno
));
69 static void monitor_detach
PARAMS ((char *args
, int from_tty
));
70 static void monitor_resume
PARAMS ((int pid
, int step
, enum target_signal sig
));
71 static void monitor_interrupt
PARAMS ((int signo
));
72 static void monitor_interrupt_twice
PARAMS ((int signo
));
73 static void monitor_interrupt_query
PARAMS ((void));
74 static void monitor_wait_cleanup
PARAMS ((int old_timeout
));
76 static int monitor_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
77 static void monitor_fetch_registers
PARAMS ((int regno
));
78 static void monitor_store_registers
PARAMS ((int regno
));
79 static void monitor_prepare_to_store
PARAMS ((void));
80 static int monitor_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
, int write
, struct target_ops
*target
));
81 static void monitor_files_info
PARAMS ((struct target_ops
*ops
));
82 static int monitor_insert_breakpoint
PARAMS ((CORE_ADDR addr
, char *shadow
));
83 static int monitor_remove_breakpoint
PARAMS ((CORE_ADDR addr
, char *shadow
));
84 static void monitor_kill
PARAMS ((void));
85 static void monitor_load
PARAMS ((char *file
, int from_tty
));
86 static void monitor_mourn_inferior
PARAMS ((void));
87 static void monitor_stop
PARAMS ((void));
88 static void monitor_debug
PARAMS ((char *prefix
, char *string
, char *suffix
));
90 static int monitor_read_memory
PARAMS ((CORE_ADDR addr
, char *myaddr
,int len
));
91 static int monitor_write_memory
PARAMS ((CORE_ADDR addr
, char *myaddr
,int len
));
93 static int monitor_expect_regexp
PARAMS ((struct re_pattern_buffer
*pat
,
94 char *buf
, int buflen
));
95 static int from_hex
PARAMS ((int a
));
96 static unsigned long get_hex_word
PARAMS ((void));
98 static struct monitor_ops
*current_monitor
;
100 static int hashmark
; /* flag set by "set hash" */
102 static int timeout
= 30;
104 static int in_monitor_wait
= 0; /* Non-zero means we are in monitor_wait() */
106 static void (*ofunc
)(); /* Old SIGINT signal handler */
108 /* Descriptor for I/O to remote machine. Initialize it to NULL so
109 that monitor_open knows that we don't have a file open when the
112 static serial_t monitor_desc
= NULL
;
114 /* Pointer to regexp pattern matching data */
116 static struct re_pattern_buffer register_pattern
;
117 static char register_fastmap
[256];
119 static struct re_pattern_buffer getmem_resp_delim_pattern
;
120 static char getmem_resp_delim_fastmap
[256];
122 static int dump_reg_flag
; /* Non-zero means do a dump_registers cmd when
123 monitor_wait wakes up. */
125 static DCACHE
*remote_dcache
;
127 /* monitor_debug is like fputs_unfiltered, except it prints special
128 characters in printable fashion. */
131 monitor_debug (prefix
, string
, suffix
)
138 /* print prefix and suffix after each line */
139 static int new_line
=1;
140 static char *prev_prefix
= "";
141 static char *prev_suffix
= "";
143 /* if the prefix is changing, print the previous suffix, a new line,
144 and the new prefix */
145 if (strcmp(prev_prefix
, prefix
) != 0 && !new_line
)
147 fputs_unfiltered (prev_suffix
, gdb_stderr
);
148 fputs_unfiltered ("\n", gdb_stderr
);
149 fputs_unfiltered (prefix
, gdb_stderr
);
151 prev_prefix
= prefix
;
152 prev_suffix
= suffix
;
154 /* print prefix if last char was a newline*/
157 fputs_unfiltered (prefix
, gdb_stderr
);
160 if (strchr(string
,'\n')) /* save state for next call */
163 while ((ch
= *string
++) != '\0')
168 fputc_unfiltered (ch
, gdb_stderr
);
171 fprintf_unfiltered (gdb_stderr
, "\\%03o", ch
);
175 case '\\': fputs_unfiltered ("\\\\", gdb_stderr
); break;
176 case '\b': fputs_unfiltered ("\\b", gdb_stderr
); break;
177 case '\f': fputs_unfiltered ("\\f", gdb_stderr
); break;
178 case '\n': fputs_unfiltered ("\\n", gdb_stderr
); break;
179 case '\r': fputs_unfiltered ("\\r", gdb_stderr
); break;
180 case '\t': fputs_unfiltered ("\\t", gdb_stderr
); break;
181 case '\v': fputs_unfiltered ("\\v", gdb_stderr
); break;
185 if (new_line
==1) { /* print suffix if last char was a newline */
186 fputs_unfiltered (suffix
, gdb_stderr
);
187 fputs_unfiltered ("\n", gdb_stderr
);
191 /* monitor_printf_noecho -- Send data to monitor, but don't expect an echo.
192 Works just like printf. */
195 #ifdef ANSI_PROTOTYPES
196 monitor_printf_noecho (char *pattern
, ...)
198 monitor_printf_noecho (va_alist
)
207 va_start (args
, pattern
);
211 pattern
= va_arg (args
, char *);
214 vsprintf (sndbuf
, pattern
, args
);
216 if (remote_debug
> 0)
217 monitor_debug ("sent -->", sndbuf
, "<--");
219 len
= strlen (sndbuf
);
221 if (len
+ 1 > sizeof sndbuf
)
224 if (SERIAL_WRITE(monitor_desc
, sndbuf
, len
))
225 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
228 /* monitor_printf -- Send data to monitor and check the echo. Works just like
232 #ifdef ANSI_PROTOTYPES
233 monitor_printf (char *pattern
, ...)
235 monitor_printf (va_alist
)
243 #ifdef ANSI_PROTOTYPES
244 va_start (args
, pattern
);
248 pattern
= va_arg (args
, char *);
251 vsprintf (sndbuf
, pattern
, args
);
253 if (remote_debug
> 0)
254 monitor_debug ("sent -->", sndbuf
, "<--");
256 len
= strlen (sndbuf
);
258 if (len
+ 1 > sizeof sndbuf
)
261 if (SERIAL_WRITE(monitor_desc
, sndbuf
, len
))
262 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
264 /* We used to expect that the next immediate output was the characters we
265 just output, but sometimes some extra junk appeared before the characters
266 we expected, like an extra prompt, or a portmaster sending telnet negotiations.
267 So, just start searching for what we sent, and skip anything unknown. */
268 monitor_expect (sndbuf
, (char *)0, 0);
271 /* Read a character from the remote system, doing all the fancy
279 static enum { last_random
, last_nl
, last_cr
, last_crnl
} state
= last_random
;
285 c
= SERIAL_READCHAR (monitor_desc
, timeout
);
290 if (remote_debug
> 0)
295 monitor_debug ("read -->", buf
, "<--");
299 /* Canonicialize \n\r combinations into one \r */
300 if ((current_monitor
->flags
& MO_HANDLE_NL
) != 0)
302 if ((c
== '\r' && state
== last_nl
)
303 || (c
== '\n' && state
== last_cr
))
324 if (c
== SERIAL_TIMEOUT
)
325 #ifdef MAINTENANCE_CMDS
326 if (in_monitor_wait
) /* Watchdog went off */
328 target_mourn_inferior ();
329 error ("Watchdog has expired. Target detached.\n");
333 error ("Timeout reading from remote system.");
335 perror_with_name ("remote-monitor");
338 /* Scan input from the remote system, until STRING is found. If BUF is non-
339 zero, then collect input until we have collected either STRING or BUFLEN-1
340 chars. In either case we terminate BUF with a 0. If input overflows BUF
341 because STRING can't be found, return -1, else return number of chars in BUF
342 (minus the terminating NUL). Note that in the non-overflow case, STRING
343 will be at the end of BUF. */
346 monitor_expect (string
, buf
, buflen
)
352 int obuflen
= buflen
;
367 c
= readchar (timeout
);
374 c
= readchar (timeout
);
376 /* Don't expect any ^C sent to be echoed */
378 if (*p
== '\003' || c
== *p
)
388 return obuflen
- buflen
;
403 /* Search for a regexp. */
406 monitor_expect_regexp (pat
, buf
, buflen
)
407 struct re_pattern_buffer
*pat
;
418 mybuf
= alloca (1024);
427 if (p
- mybuf
>= buflen
)
428 { /* Buffer about to overflow */
430 /* On overflow, we copy the upper half of the buffer to the lower half. Not
431 great, but it usually works... */
433 memcpy (mybuf
, mybuf
+ buflen
/ 2, buflen
/ 2);
434 p
= mybuf
+ buflen
/ 2;
437 *p
++ = readchar (timeout
);
439 retval
= re_search (pat
, mybuf
, p
- mybuf
, 0, p
- mybuf
, NULL
);
445 /* Keep discarding input until we see the MONITOR prompt.
447 The convention for dealing with the prompt is that you
449 o *then* wait for the prompt.
451 Thus the last thing that a procedure does with the serial line
452 will be an monitor_expect_prompt(). Exception: monitor_resume does not
453 wait for the prompt, because the terminal is being handed over
454 to the inferior. However, the next thing which happens after that
455 is a monitor_wait which does wait for the prompt.
456 Note that this includes abnormal exit, e.g. error(). This is
457 necessary to prevent getting into states from which we can't
461 monitor_expect_prompt (buf
, buflen
)
465 return monitor_expect (current_monitor
->prompt
, buf
, buflen
);
468 /* Get N 32-bit words from remote, each preceded by a space, and put
469 them in registers starting at REGNO. */
479 ch
= readchar (timeout
);
484 for (i
= 7; i
>= 1; i
--)
486 ch
= readchar (timeout
);
489 val
= (val
<< 4) | from_hex (ch
);
496 compile_pattern (pattern
, compiled_pattern
, fastmap
)
498 struct re_pattern_buffer
*compiled_pattern
;
504 compiled_pattern
->fastmap
= fastmap
;
506 tmp
= re_set_syntax (RE_SYNTAX_EMACS
);
507 val
= re_compile_pattern (pattern
,
513 error ("compile_pattern: Can't compile pattern string `%s': %s!", pattern
, val
);
516 re_compile_fastmap (compiled_pattern
);
519 /* Open a connection to a remote debugger. NAME is the filename used
520 for communication. */
522 static char *dev_name
;
523 static struct target_ops
*targ_ops
;
526 monitor_open (args
, mon_ops
, from_tty
)
528 struct monitor_ops
*mon_ops
;
535 if (mon_ops
->magic
!= MONITOR_OPS_MAGIC
)
536 error ("Magic number of monitor_ops struct wrong.");
538 targ_ops
= mon_ops
->target
;
539 name
= targ_ops
->to_shortname
;
542 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
543 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
545 target_preopen (from_tty
);
547 /* Setup pattern for register dump */
549 if (mon_ops
->register_pattern
)
550 compile_pattern (mon_ops
->register_pattern
, ®ister_pattern
,
553 if (mon_ops
->getmem
.resp_delim
)
554 compile_pattern (mon_ops
->getmem
.resp_delim
, &getmem_resp_delim_pattern
,
555 getmem_resp_delim_fastmap
);
557 unpush_target (targ_ops
);
561 dev_name
= strsave (args
);
563 monitor_desc
= SERIAL_OPEN (dev_name
);
566 perror_with_name (dev_name
);
570 if (SERIAL_SETBAUDRATE (monitor_desc
, baud_rate
))
572 SERIAL_CLOSE (monitor_desc
);
573 perror_with_name (dev_name
);
577 SERIAL_RAW (monitor_desc
);
579 SERIAL_FLUSH_INPUT (monitor_desc
);
581 /* some systems only work with 2 stop bits */
583 SERIAL_SETSTOPBITS (monitor_desc
, mon_ops
->stopbits
);
585 current_monitor
= mon_ops
;
587 /* See if we can wake up the monitor. First, try sending a stop sequence,
588 then send the init strings. Last, remove all breakpoints. */
590 if (current_monitor
->stop
)
593 if ((current_monitor
->flags
& MO_NO_ECHO_ON_OPEN
) == 0)
595 monitor_expect_prompt (NULL
, 0);
599 /* wake up the monitor and see if it's alive */
600 for (p
= mon_ops
->init
; *p
!= NULL
; p
++)
602 /* Some of the characters we send may not be echoed,
603 but we hope to get a prompt at the end of it all. */
605 if ((current_monitor
->flags
& MO_NO_ECHO_ON_OPEN
) == 0)
608 monitor_printf_noecho (*p
);
609 monitor_expect_prompt (NULL
, 0);
612 SERIAL_FLUSH_INPUT (monitor_desc
);
614 /* Remove all breakpoints */
616 if (mon_ops
->clr_all_break
)
618 monitor_printf (mon_ops
->clr_all_break
);
619 monitor_expect_prompt (NULL
, 0);
623 printf_unfiltered ("Remote target %s connected to %s\n", name
, dev_name
);
625 push_target (targ_ops
);
627 inferior_pid
= 42000; /* Make run command think we are busy... */
629 /* Give monitor_wait something to read */
631 monitor_printf (current_monitor
->line_term
);
633 remote_dcache
= dcache_init (monitor_read_memory
, monitor_write_memory
);
638 /* Close out all files and local state before this target loses
642 monitor_close (quitting
)
646 SERIAL_CLOSE (monitor_desc
);
650 /* Terminate the open connection to the remote debugger. Use this
651 when you want to detach and do something else with your gdb. */
654 monitor_detach (args
, from_tty
)
658 pop_target (); /* calls monitor_close to do the real work */
660 printf_unfiltered ("Ending remote %s debugging\n", target_shortname
);
663 /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
666 monitor_supply_register (regno
, valstr
)
671 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
674 val
= strtoul (valstr
, &p
, 16);
676 if (val
== 0 && valstr
== p
)
677 error ("monitor_supply_register (%d): bad value from monitor: %s.",
680 /* supply register stores in target byte order, so swap here */
682 store_unsigned_integer (regbuf
, REGISTER_RAW_SIZE (regno
), val
);
684 supply_register (regno
, regbuf
);
689 /* Tell the remote machine to resume. */
692 monitor_resume (pid
, step
, sig
)
694 enum target_signal sig
;
696 dcache_flush (remote_dcache
);
698 monitor_printf (current_monitor
->step
);
701 monitor_printf (current_monitor
->cont
);
702 if (current_monitor
->flags
& MO_NEED_REGDUMP_AFTER_CONT
)
707 /* Parse the output of a register dump command. A monitor specific
708 regexp is used to extract individual register descriptions of the
709 form REG=VAL. Each description is split up into a name and a value
710 string which are passed down to monitor specific code. */
713 parse_register_dump (buf
, len
)
719 int regnamelen
, vallen
;
721 /* Element 0 points to start of register name, and element 1
722 points to the start of the register value. */
723 struct re_registers register_strings
;
725 if (re_search (®ister_pattern
, buf
, len
, 0, len
,
726 ®ister_strings
) == -1)
729 regnamelen
= register_strings
.end
[1] - register_strings
.start
[1];
730 regname
= buf
+ register_strings
.start
[1];
731 vallen
= register_strings
.end
[2] - register_strings
.start
[2];
732 val
= buf
+ register_strings
.start
[2];
734 current_monitor
->supply_register (regname
, regnamelen
, val
, vallen
);
736 buf
+= register_strings
.end
[0];
737 len
-= register_strings
.end
[0];
741 /* Send ^C to target to halt it. Target will respond, and send us a
745 monitor_interrupt (signo
)
748 /* If this doesn't work, try more severe steps. */
749 signal (signo
, monitor_interrupt_twice
);
752 printf_unfiltered ("monitor_interrupt called\n");
757 /* The user typed ^C twice. */
760 monitor_interrupt_twice (signo
)
763 signal (signo
, ofunc
);
765 monitor_interrupt_query ();
767 signal (signo
, monitor_interrupt
);
770 /* Ask the user what to do when an interrupt is received. */
773 monitor_interrupt_query ()
775 target_terminal_ours ();
777 if (query ("Interrupted while waiting for the program.\n\
778 Give up (and stop debugging it)? "))
780 target_mourn_inferior ();
781 return_to_top_level (RETURN_QUIT
);
784 target_terminal_inferior ();
788 monitor_wait_cleanup (old_timeout
)
791 timeout
= old_timeout
;
792 signal (SIGINT
, ofunc
);
796 /* Wait until the remote machine stops, then return, storing status in
797 status just as `wait' would. */
800 monitor_wait (pid
, status
)
802 struct target_waitstatus
*status
;
804 int old_timeout
= timeout
;
807 struct cleanup
*old_chain
;
809 status
->kind
= TARGET_WAITKIND_EXITED
;
810 status
->value
.integer
= 0;
812 old_chain
= make_cleanup (monitor_wait_cleanup
, old_timeout
);
814 #ifdef MAINTENANCE_CMDS
816 timeout
= watchdog
> 0 ? watchdog
: -1;
818 timeout
= -1; /* Don't time out -- user program is running. */
821 ofunc
= (void (*)()) signal (SIGINT
, monitor_interrupt
);
825 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
828 fprintf_unfiltered (gdb_stderr
, "monitor_wait: excessive response from monitor: %s.", buf
);
830 while (resp_len
< 0);
832 signal (SIGINT
, ofunc
);
834 timeout
= old_timeout
;
836 if (dump_reg_flag
&& current_monitor
->dump_registers
)
840 monitor_printf (current_monitor
->dump_registers
);
841 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
844 if (current_monitor
->register_pattern
)
845 parse_register_dump (buf
, resp_len
);
847 status
->kind
= TARGET_WAITKIND_STOPPED
;
848 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
850 discard_cleanups (old_chain
);
857 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
861 monitor_fetch_register (regno
)
865 static char zerobuf
[MAX_REGISTER_RAW_SIZE
] = {0};
866 char regbuf
[MAX_REGISTER_RAW_SIZE
* 2 + 1];
869 name
= current_monitor
->regnames
[regno
];
873 supply_register (regno
, zerobuf
);
877 /* send the register examine command */
879 monitor_printf (current_monitor
->getreg
.cmd
, name
);
881 /* If RESP_DELIM is specified, we search for that as a leading
882 delimiter for the register value. Otherwise, we just start
883 searching from the start of the buf. */
885 if (current_monitor
->getreg
.resp_delim
)
886 monitor_expect (current_monitor
->getreg
.resp_delim
, NULL
, 0);
888 /* Read upto the maximum number of hex digits for this register, skipping
889 spaces, but stop reading if something else is seen. Some monitors
890 like to drop leading zeros. */
892 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
) * 2; i
++)
895 c
= readchar (timeout
);
897 c
= readchar (timeout
);
905 regbuf
[i
] = '\000'; /* terminate the number */
907 /* If TERM is present, we wait for that to show up. Also, (if TERM
908 is present), we will send TERM_CMD if that is present. In any
909 case, we collect all of the output into buf, and then wait for
910 the normal prompt. */
912 if (current_monitor
->getreg
.term
)
914 monitor_expect (current_monitor
->getreg
.term
, NULL
, 0); /* get response */
916 if (current_monitor
->getreg
.term_cmd
)
918 monitor_printf (current_monitor
->getreg
.term_cmd
);
919 monitor_expect_prompt (NULL
, 0);
923 monitor_expect_prompt (NULL
, 0); /* get response */
925 monitor_supply_register (regno
, regbuf
);
928 /* Read the remote registers into the block regs. */
930 static void monitor_dump_regs ()
932 if (current_monitor
->dump_registers
)
937 monitor_printf (current_monitor
->dump_registers
);
938 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
939 parse_register_dump (buf
, resp_len
);
942 abort(); /* Need some way to read registers */
946 monitor_fetch_registers (regno
)
949 if (current_monitor
->getreg
.cmd
)
953 monitor_fetch_register (regno
);
957 for (regno
= 0; regno
< NUM_REGS
; regno
++)
958 monitor_fetch_register (regno
);
961 monitor_dump_regs ();
965 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
968 monitor_store_register (regno
)
974 name
= current_monitor
->regnames
[regno
];
978 val
= read_register (regno
);
980 /* send the register deposit command */
982 monitor_printf (current_monitor
->setreg
.cmd
, name
, val
);
984 /* It's possible that there are actually some monitors out there that
985 will prompt you when you set a register. In that case, you may
986 need to add some code here to deal with TERM and TERM_CMD (see
987 monitor_fetch_register to get an idea of what's needed...) */
989 monitor_expect_prompt (NULL
, 0);
992 /* Store the remote registers. */
995 monitor_store_registers (regno
)
1000 monitor_store_register (regno
);
1004 for (regno
= 0; regno
< NUM_REGS
; regno
++)
1005 monitor_store_register (regno
);
1008 /* Get ready to modify the registers array. On machines which store
1009 individual registers, this doesn't need to do anything. On machines
1010 which store all the registers in one fell swoop, this makes sure
1011 that registers contains all the registers from the program being
1015 monitor_prepare_to_store ()
1017 /* Do nothing, since we can store individual regs */
1021 monitor_files_info (ops
)
1022 struct target_ops
*ops
;
1024 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name
, baud_rate
);
1028 monitor_write_memory (memaddr
, myaddr
, len
)
1037 /* Use memory fill command for leading 0 bytes. */
1039 if (current_monitor
->fill
)
1041 for (i
= 0; i
< len
; i
++)
1045 if (i
> 4) /* More than 4 zeros is worth doing */
1047 if (current_monitor
->flags
& MO_FILL_USES_ADDR
)
1048 monitor_printf (current_monitor
->fill
, memaddr
, memaddr
+ i
, 0);
1050 monitor_printf (current_monitor
->fill
, memaddr
, i
, 0);
1052 monitor_expect_prompt (NULL
, 0);
1058 if ((memaddr
& 0x7) == 0 && len
>= 8 && current_monitor
->setmem
.cmdll
)
1061 cmd
= current_monitor
->setmem
.cmdll
;
1063 else if ((memaddr
& 0x3) == 0 && len
>= 4 && current_monitor
->setmem
.cmdl
)
1066 cmd
= current_monitor
->setmem
.cmdl
;
1068 else if ((memaddr
& 0x1) == 0 && len
>= 2 && current_monitor
->setmem
.cmdw
)
1071 cmd
= current_monitor
->setmem
.cmdw
;
1076 cmd
= current_monitor
->setmem
.cmdb
;
1079 val
= extract_unsigned_integer (myaddr
, len
);
1081 monitor_printf (cmd
, memaddr
, val
);
1083 monitor_expect_prompt (NULL
, 0);
1088 /* This is an alternate form of monitor_read_memory which is used for monitors
1089 which can only read a single byte/word/etc. at a time. */
1092 monitor_read_memory_single (memaddr
, myaddr
, len
)
1098 char membuf
[sizeof(int) * 2 + 1];
1103 if ((memaddr
& 0x7) == 0 && len
>= 8 && current_monitor
->getmem
.cmdll
)
1106 cmd
= current_monitor
->getmem
.cmdll
;
1108 else if ((memaddr
& 0x3) == 0 && len
>= 4 && current_monitor
->getmem
.cmdl
)
1111 cmd
= current_monitor
->getmem
.cmdl
;
1113 else if ((memaddr
& 0x1) == 0 && len
>= 2 && current_monitor
->getmem
.cmdw
)
1116 cmd
= current_monitor
->getmem
.cmdw
;
1121 cmd
= current_monitor
->getmem
.cmdb
;
1124 /* Send the examine command. */
1126 monitor_printf (cmd
, memaddr
);
1128 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
1129 the register value. Otherwise, we just start searching from the start of
1132 if (current_monitor
->getmem
.resp_delim
)
1133 monitor_expect_regexp (&getmem_resp_delim_pattern
, NULL
, 0);
1135 /* Now, read the appropriate number of hex digits for this loc, skipping
1138 for (i
= 0; i
< len
* 2; i
++)
1144 c
= readchar (timeout
);
1150 error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.",
1151 memaddr
, i
, membuf
, c
);
1157 membuf
[i
] = '\000'; /* terminate the number */
1159 /* If TERM is present, we wait for that to show up. Also, (if TERM is
1160 present), we will send TERM_CMD if that is present. In any case, we collect
1161 all of the output into buf, and then wait for the normal prompt. */
1163 if (current_monitor
->getmem
.term
)
1165 monitor_expect (current_monitor
->getmem
.term
, NULL
, 0); /* get response */
1167 if (current_monitor
->getmem
.term_cmd
)
1169 monitor_printf (current_monitor
->getmem
.term_cmd
);
1170 monitor_expect_prompt (NULL
, 0);
1174 monitor_expect_prompt (NULL
, 0); /* get response */
1177 val
= strtoul (membuf
, &p
, 16);
1179 if (val
== 0 && membuf
== p
)
1180 error ("monitor_read_memory_single (0x%x): bad value from monitor: %s.",
1183 /* supply register stores in target byte order, so swap here */
1185 store_unsigned_integer (myaddr
, len
, val
);
1190 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
1191 at MEMADDR. Returns length moved. Currently, we only do one byte at a
1195 monitor_read_memory (memaddr
, myaddr
, len
)
1201 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
1208 if (current_monitor
->flags
& MO_GETMEM_READ_SINGLE
)
1209 return monitor_read_memory_single (memaddr
, myaddr
, len
);
1211 len
= min (len
, 16);
1213 /* See if xfer would cross a 16 byte boundary. If so, clip it. */
1214 if (((memaddr
^ (memaddr
+ len
- 1)) & ~0xf) != 0)
1215 len
= ((memaddr
+ len
) & ~0xf) - memaddr
;
1217 /* send the memory examine command */
1219 if (current_monitor
->flags
& MO_GETMEM_NEEDS_RANGE
)
1220 monitor_printf (current_monitor
->getmem
.cmdb
, memaddr
, memaddr
+ len
- 1);
1222 monitor_printf (current_monitor
->getmem
.cmdb
, memaddr
, len
);
1224 /* If TERM is present, we wait for that to show up. Also, (if TERM is
1225 present), we will send TERM_CMD if that is present. In any case, we collect
1226 all of the output into buf, and then wait for the normal prompt. */
1228 if (current_monitor
->getmem
.term
)
1230 resp_len
= monitor_expect (current_monitor
->getmem
.term
, buf
, sizeof buf
); /* get response */
1233 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
1234 memaddr
, resp_len
, buf
);
1236 if (current_monitor
->getmem
.term_cmd
)
1238 SERIAL_WRITE (monitor_desc
, current_monitor
->getmem
.term_cmd
,
1239 strlen (current_monitor
->getmem
.term_cmd
));
1240 monitor_expect_prompt (NULL
, 0);
1244 resp_len
= monitor_expect_prompt (buf
, sizeof buf
); /* get response */
1248 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
1249 the values. Otherwise, we just start searching from the start of the buf.
1252 if (current_monitor
->getmem
.resp_delim
)
1255 struct re_registers resp_strings
;
1258 retval
= re_search (&getmem_resp_delim_pattern
, p
, tmp
, 0, tmp
,
1262 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1263 memaddr
, resp_len
, buf
);
1265 p
+= resp_strings
.end
[0];
1267 p
= strstr (p
, current_monitor
->getmem
.resp_delim
);
1269 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1270 memaddr
, resp_len
, buf
);
1271 p
+= strlen (current_monitor
->getmem
.resp_delim
);
1275 for (i
= len
; i
> 0; i
--)
1277 /* Skip non-hex chars, but bomb on end of string and newlines */
1283 if (*p
== '\000' || *p
== '\n' || *p
== '\r')
1284 error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr
, resp_len
, buf
);
1288 val
= strtoul (p
, &p1
, 16);
1290 if (val
== 0 && p
== p1
)
1291 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr
,
1306 monitor_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
1311 struct target_ops
*target
; /* ignored */
1313 return dcache_xfer_memory (remote_dcache
, memaddr
, myaddr
, len
, write
);
1319 return; /* ignore attempts to kill target system */
1322 /* All we actually do is set the PC to the start address of exec_bfd, and start
1323 the program at that point. */
1326 monitor_create_inferior (exec_file
, args
, env
)
1331 if (args
&& (*args
!= '\000'))
1332 error ("Args are not supported by the monitor.");
1334 clear_proceed_status ();
1335 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
1338 /* Clean up when a program exits.
1339 The program actually lives on in the remote processor's RAM, and may be
1340 run again without a download. Don't leave it full of breakpoint
1344 monitor_mourn_inferior ()
1346 unpush_target (targ_ops
);
1347 generic_mourn_inferior (); /* Do all the proper things now */
1350 #define NUM_MONITOR_BREAKPOINTS 8
1352 static CORE_ADDR breakaddr
[NUM_MONITOR_BREAKPOINTS
] = {0};
1354 /* Tell the monitor to add a breakpoint. */
1357 monitor_insert_breakpoint (addr
, shadow
)
1362 static unsigned char break_insn
[] = BREAKPOINT
;
1364 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
1366 if (breakaddr
[i
] == 0)
1368 breakaddr
[i
] = addr
;
1369 monitor_read_memory (addr
, shadow
, sizeof (break_insn
));
1370 monitor_printf (current_monitor
->set_break
, addr
);
1371 monitor_expect_prompt (NULL
, 0);
1376 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS
);
1379 /* Tell the monitor to remove a breakpoint. */
1382 monitor_remove_breakpoint (addr
, shadow
)
1388 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
1390 if (breakaddr
[i
] == addr
)
1393 /* some monitors remove breakpoints based on the address */
1394 if (current_monitor
->flags
& MO_CLR_BREAK_USES_ADDR
)
1395 monitor_printf (current_monitor
->clr_break
, addr
);
1397 monitor_printf (current_monitor
->clr_break
, i
);
1398 monitor_expect_prompt (NULL
, 0);
1402 fprintf_unfiltered (stderr
, "Can't find breakpoint associated with 0x%x\n", addr
);
1406 /* monitor_wait_srec_ack -- wait for the target to send an acknowledgement for
1407 an S-record. Return non-zero if the ACK is received properly. */
1410 monitor_wait_srec_ack ()
1412 /* FIXME: eventually we'll want to be able to handle acknowledgements
1413 of something other than a '+' character. Right now this is only
1414 going to work for EST visionICE. */
1415 return readchar (timeout
) == '+';
1418 /* monitor_load -- download a file. */
1421 monitor_load (file
, from_tty
)
1425 dcache_flush (remote_dcache
);
1427 if (current_monitor
->load_routine
)
1428 current_monitor
->load_routine (monitor_desc
, file
, hashmark
);
1430 { /* The default is ascii S-records */
1431 monitor_printf (current_monitor
->load
);
1432 if (current_monitor
->loadresp
)
1433 monitor_expect (current_monitor
->loadresp
, NULL
, 0);
1435 load_srec (monitor_desc
, file
, 32, SREC_ALL
, hashmark
,
1436 current_monitor
->flags
& MO_SREC_ACK
?
1437 monitor_wait_srec_ack
: NULL
);
1439 monitor_expect_prompt (NULL
, 0);
1442 /* Finally, make the PC point at the start address */
1445 write_pc (bfd_get_start_address (exec_bfd
));
1447 inferior_pid
= 0; /* No process now */
1449 /* This is necessary because many things were based on the PC at the time that
1450 we attached to the monitor, which is no longer valid now that we have loaded
1451 new code (and just changed the PC). Another way to do this might be to call
1452 normal_stop, except that the stack may not be valid, and things would get
1453 horribly confused... */
1455 clear_symtab_users ();
1461 if ((current_monitor
->flags
& MO_SEND_BREAK_ON_STOP
) != 0)
1462 SERIAL_SEND_BREAK (monitor_desc
);
1463 if (current_monitor
->stop
)
1464 monitor_printf_noecho (current_monitor
->stop
);
1467 /* Put a command string, in args, out to MONITOR. Output from MONITOR
1468 is placed on the users terminal until the prompt is seen. FIXME: We
1469 read the characters ourseleves here cause of a nasty echo. */
1472 monitor_command (args
, from_tty
)
1480 if (monitor_desc
== NULL
)
1481 error ("monitor target not open.");
1483 p
= current_monitor
->prompt
;
1485 /* Send the command. Note that if no args were supplied, then we're
1486 just sending the monitor a newline, which is sometimes useful. */
1488 monitor_printf ("%s\r", (args
? args
: ""));
1490 resp_len
= monitor_expect_prompt (buf
, sizeof buf
);
1492 fputs_unfiltered (buf
, gdb_stdout
); /* Output the response */
1495 /* Convert hex digit A to a number. */
1501 if (a
>= '0' && a
<= '9')
1503 if (a
>= 'a' && a
<= 'f')
1504 return a
- 'a' + 10;
1505 if (a
>= 'A' && a
<= 'F')
1506 return a
- 'A' + 10;
1508 error ("Reply contains invalid hex digit 0x%x", a
);
1511 static struct target_ops monitor_ops
=
1513 NULL
, /* to_shortname */
1514 NULL
, /* to_longname */
1517 monitor_close
, /* to_close */
1518 NULL
, /* to_attach */
1519 monitor_detach
, /* to_detach */
1520 monitor_resume
, /* to_resume */
1521 monitor_wait
, /* to_wait */
1522 monitor_fetch_registers
, /* to_fetch_registers */
1523 monitor_store_registers
, /* to_store_registers */
1524 monitor_prepare_to_store
, /* to_prepare_to_store */
1525 monitor_xfer_memory
, /* to_xfer_memory */
1526 monitor_files_info
, /* to_files_info */
1527 monitor_insert_breakpoint
, /* to_insert_breakpoint */
1528 monitor_remove_breakpoint
, /* to_remove_breakpoint */
1529 0, /* to_terminal_init */
1530 0, /* to_terminal_inferior */
1531 0, /* to_terminal_ours_for_output */
1532 0, /* to_terminal_ours */
1533 0, /* to_terminal_info */
1534 monitor_kill
, /* to_kill */
1535 monitor_load
, /* to_load */
1536 0, /* to_lookup_symbol */
1537 monitor_create_inferior
, /* to_create_inferior */
1538 monitor_mourn_inferior
, /* to_mourn_inferior */
1540 0, /* to_notice_signals */
1541 0, /* to_thread_alive */
1542 monitor_stop
, /* to_stop */
1543 process_stratum
, /* to_stratum */
1545 1, /* to_has_all_memory */
1546 1, /* to_has_memory */
1547 1, /* to_has_stack */
1548 1, /* to_has_registers */
1549 1, /* to_has_execution */
1551 0, /* sections_end */
1552 OPS_MAGIC
/* to_magic */
1555 /* Init the target_ops structure pointed at by OPS */
1558 init_monitor_ops (ops
)
1559 struct target_ops
*ops
;
1561 memcpy (ops
, &monitor_ops
, sizeof monitor_ops
);
1564 /* Define additional commands that are usually only used by monitors. */
1567 _initialize_remote_monitors ()
1569 add_show_from_set (add_set_cmd ("hash", no_class
, var_boolean
,
1571 "Set display of activity while downloading a file.\n\
1572 When enabled, a hashmark \'#\' is displayed.",
1576 add_com ("monitor", class_obscure
, monitor_command
,
1577 "Send a command to the debug monitor.");