1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1995
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. */
37 #ifdef ANSI_PROTOTYPES
44 #include "gdb_string.h"
45 #include <sys/types.h>
51 #include "gnu-regex.h"
55 static int readchar
PARAMS ((int timeout
));
57 static void monitor_command
PARAMS ((char *args
, int fromtty
));
59 static void monitor_fetch_register
PARAMS ((int regno
));
60 static void monitor_store_register
PARAMS ((int regno
));
62 static void monitor_close
PARAMS ((int quitting
));
63 static void monitor_detach
PARAMS ((char *args
, int from_tty
));
64 static void monitor_resume
PARAMS ((int pid
, int step
, enum target_signal sig
));
65 static void monitor_interrupt
PARAMS ((int signo
));
66 static void monitor_interrupt_twice
PARAMS ((int signo
));
67 static void monitor_interrupt_query
PARAMS ((void));
68 static void monitor_wait_cleanup
PARAMS ((int old_timeout
));
70 static int monitor_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
71 static void monitor_fetch_registers
PARAMS ((int regno
));
72 static void monitor_store_registers
PARAMS ((int regno
));
73 static void monitor_prepare_to_store
PARAMS ((void));
74 static int monitor_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
, int write
, struct target_ops
*target
));
75 static void monitor_files_info
PARAMS ((struct target_ops
*ops
));
76 static int monitor_insert_breakpoint
PARAMS ((CORE_ADDR addr
, char *shadow
));
77 static int monitor_remove_breakpoint
PARAMS ((CORE_ADDR addr
, char *shadow
));
78 static void monitor_kill
PARAMS ((void));
79 static void monitor_load
PARAMS ((char *file
, int from_tty
));
80 static void monitor_mourn_inferior
PARAMS ((void));
81 static void monitor_stop
PARAMS ((void));
82 static void monitor_debug
PARAMS ((char *string
));
84 static int monitor_read_memory
PARAMS ((CORE_ADDR addr
, char *myaddr
,int len
));
85 static int monitor_write_memory
PARAMS ((CORE_ADDR addr
, char *myaddr
,int len
));
87 static int from_hex
PARAMS ((int a
));
88 static unsigned long get_hex_word
PARAMS ((void));
90 static struct monitor_ops
*current_monitor
;
92 static int hashmark
; /* flag set by "set hash" */
94 static int timeout
= 30;
96 static int in_monitor_wait
= 0; /* Non-zero means we are in monitor_wait() */
98 static void (*ofunc
)(); /* Old SIGINT signal handler */
100 /* Descriptor for I/O to remote machine. Initialize it to NULL so
101 that monitor_open knows that we don't have a file open when the
104 static serial_t monitor_desc
= NULL
;
106 /* Pointer to regexp pattern matching data */
108 static struct re_pattern_buffer register_pattern
;
109 static char register_fastmap
[256];
111 static struct re_pattern_buffer getmem_resp_delim_pattern
;
112 static char getmem_resp_delim_fastmap
[256];
114 static int dump_reg_flag
; /* Non-zero means do a dump_registers cmd when
115 monitor_wait wakes up. */
117 static DCACHE
*remote_dcache
;
119 /* monitor_debug is like fputs_unfiltered, except it prints special
120 characters in printable fashion. */
123 monitor_debug (string
)
128 while ((ch
= *string
++) != '\0')
133 fputc_unfiltered (ch
, gdb_stderr
);
136 fprintf_unfiltered (gdb_stderr
, "\\%03o", ch
);
140 case '\\': fputs_unfiltered ("\\\\", gdb_stderr
); break;
141 case '\b': fputs_unfiltered ("\\b", gdb_stderr
); break;
142 case '\f': fputs_unfiltered ("\\f", gdb_stderr
); break;
143 case '\n': fputs_unfiltered ("\\n\n", gdb_stderr
); break;
144 case '\r': fputs_unfiltered ("\\r\n", gdb_stderr
); break;
145 case '\t': fputs_unfiltered ("\\t", gdb_stderr
); break;
146 case '\v': fputs_unfiltered ("\\v", gdb_stderr
); break;
152 /* monitor_printf_noecho -- Send data to monitor, but don't expect an echo.
153 Works just like printf. */
156 #ifdef ANSI_PROTOTYPES
157 monitor_printf_noecho (char *pattern
, ...)
159 monitor_printf_noecho (va_alist
)
168 va_start (args
, pattern
);
172 pattern
= va_arg (args
, char *);
175 vsprintf (sndbuf
, pattern
, args
);
177 if (remote_debug
> 0)
178 monitor_debug (sndbuf
);
180 len
= strlen (sndbuf
);
182 if (len
+ 1 > sizeof sndbuf
)
185 if (SERIAL_WRITE(monitor_desc
, sndbuf
, len
))
186 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
189 /* monitor_printf -- Send data to monitor and check the echo. Works just like
193 #ifdef ANSI_PROTOTYPES
194 monitor_printf (char *pattern
, ...)
196 monitor_printf (va_alist
)
205 #ifdef ANSI_PROTOTYPES
206 va_start (args
, pattern
);
210 pattern
= va_arg (args
, char *);
213 vsprintf (sndbuf
, pattern
, args
);
215 if (remote_debug
> 0)
216 monitor_debug (sndbuf
);
218 len
= strlen (sndbuf
);
220 if (len
+ 1 > sizeof sndbuf
)
223 if (SERIAL_WRITE(monitor_desc
, sndbuf
, len
))
224 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
226 for (i
= 0; i
< len
; i
++)
229 c
= readchar (timeout
);
233 /* Don't fail if we sent a ^C, they're never echoed */
234 if (sndbuf
[i
] == '\003')
237 if (sndbuf
[i
] == '\r'
241 warning ("monitor_printf: Bad echo. Sent: \"%s\", Got: \"%.*s%c\".",
242 sndbuf
, i
, sndbuf
, c
);
247 /* Read a character from the remote system, doing all the fancy
256 c
= SERIAL_READCHAR (monitor_desc
, timeout
);
258 if (remote_debug
> 0)
259 fputc_unfiltered (c
, gdb_stderr
);
264 if (c
== SERIAL_TIMEOUT
)
265 #ifdef MAINTENANCE_CMDS
266 if (in_monitor_wait
) /* Watchdog went off */
268 target_mourn_inferior ();
269 error ("Watchdog has expired. Target detached.\n");
273 error ("Timeout reading from remote system.");
275 perror_with_name ("remote-monitor");
278 /* Scan input from the remote system, until STRING is found. If BUF is non-
279 zero, then collect input until we have collected either STRING or BUFLEN-1
280 chars. In either case we terminate BUF with a 0. If input overflows BUF
281 because STRING can't be found, return -1, else return number of chars in BUF
282 (minus the terminating NUL). Note that in the non-overflow case, STRING
283 will be at the end of BUF. */
286 monitor_expect (string
, buf
, buflen
)
292 int obuflen
= buflen
;
307 c
= readchar (timeout
);
314 c
= readchar (timeout
);
325 return obuflen
- buflen
;
340 /* Search for a regexp. */
343 monitor_expect_regexp (pat
, buf
, buflen
)
344 struct re_pattern_buffer
*pat
;
355 mybuf
= alloca (1024);
364 if (p
- mybuf
>= buflen
)
365 { /* Buffer about to overflow */
367 /* On overflow, we copy the upper half of the buffer to the lower half. Not
368 great, but it usually works... */
370 memcpy (mybuf
, mybuf
+ buflen
/ 2, buflen
/ 2);
371 p
= mybuf
+ buflen
/ 2;
374 *p
++ = readchar (timeout
);
376 retval
= re_search (pat
, mybuf
, p
- mybuf
, 0, p
- mybuf
, NULL
);
382 /* Keep discarding input until we see the MONITOR prompt.
384 The convention for dealing with the prompt is that you
386 o *then* wait for the prompt.
388 Thus the last thing that a procedure does with the serial line
389 will be an monitor_expect_prompt(). Exception: monitor_resume does not
390 wait for the prompt, because the terminal is being handed over
391 to the inferior. However, the next thing which happens after that
392 is a monitor_wait which does wait for the prompt.
393 Note that this includes abnormal exit, e.g. error(). This is
394 necessary to prevent getting into states from which we can't
398 monitor_expect_prompt (buf
, buflen
)
402 return monitor_expect (PROMPT
, buf
, buflen
);
405 /* Get N 32-bit words from remote, each preceded by a space, and put
406 them in registers starting at REGNO. */
416 ch
= readchar (timeout
);
421 for (i
= 7; i
>= 1; i
--)
423 ch
= readchar (timeout
);
426 val
= (val
<< 4) | from_hex (ch
);
433 compile_pattern (pattern
, compiled_pattern
, fastmap
)
435 struct re_pattern_buffer
*compiled_pattern
;
441 compiled_pattern
->fastmap
= fastmap
;
443 tmp
= re_set_syntax (RE_SYNTAX_EMACS
);
444 val
= re_compile_pattern (pattern
,
450 error ("compile_pattern: Can't compile pattern string `%s': %s!", pattern
, val
);
453 re_compile_fastmap (compiled_pattern
);
456 /* Open a connection to a remote debugger. NAME is the filename used
457 for communication. */
459 static char *dev_name
;
460 static struct target_ops
*targ_ops
;
463 monitor_open (args
, mon_ops
, from_tty
)
465 struct monitor_ops
*mon_ops
;
472 if (mon_ops
->magic
!= MONITOR_OPS_MAGIC
)
473 error ("Magic number of monitor_ops struct wrong.");
475 targ_ops
= mon_ops
->target
;
476 name
= targ_ops
->to_shortname
;
479 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
480 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
482 target_preopen (from_tty
);
484 /* Setup pattern for register dump */
486 if (mon_ops
->register_pattern
)
487 compile_pattern (mon_ops
->register_pattern
, ®ister_pattern
,
490 if (mon_ops
->getmem
.resp_delim
)
491 compile_pattern (mon_ops
->getmem
.resp_delim
, &getmem_resp_delim_pattern
,
492 getmem_resp_delim_fastmap
);
494 unpush_target (targ_ops
);
498 dev_name
= strsave (args
);
500 monitor_desc
= SERIAL_OPEN (dev_name
);
503 perror_with_name (dev_name
);
507 if (SERIAL_SETBAUDRATE (monitor_desc
, baud_rate
))
509 SERIAL_CLOSE (monitor_desc
);
510 perror_with_name (dev_name
);
514 SERIAL_RAW (monitor_desc
);
516 SERIAL_FLUSH_INPUT (monitor_desc
);
518 /* some systems only work with 2 stop bits */
520 SERIAL_SETSTOPBITS (monitor_desc
, mon_ops
->stopbits
);
522 current_monitor
= mon_ops
;
524 /* See if we can wake up the monitor. First, try sending a stop sequence,
525 then send the init strings. Last, remove all breakpoints. */
527 if (current_monitor
->stop
)
530 monitor_expect_prompt (NULL
, 0);
533 /* wake up the monitor and see if it's alive */
534 for (p
= mon_ops
->init
; *p
!= NULL
; p
++)
537 monitor_expect_prompt (NULL
, 0);
540 SERIAL_FLUSH_INPUT (monitor_desc
);
542 /* Remove all breakpoints */
544 if (mon_ops
->clr_all_break
)
546 monitor_printf (mon_ops
->clr_all_break
);
547 monitor_expect_prompt (NULL
, 0);
551 printf_unfiltered ("Remote target %s connected to %s\n", name
, dev_name
);
553 push_target (targ_ops
);
555 inferior_pid
= 42000; /* Make run command think we are busy... */
557 /* Give monitor_wait something to read */
559 monitor_printf (current_monitor
->line_term
);
561 remote_dcache
= dcache_init (monitor_read_memory
, monitor_write_memory
);
566 /* Close out all files and local state before this target loses
570 monitor_close (quitting
)
574 SERIAL_CLOSE (monitor_desc
);
578 /* Terminate the open connection to the remote debugger. Use this
579 when you want to detach and do something else with your gdb. */
582 monitor_detach (args
, from_tty
)
586 pop_target (); /* calls monitor_close to do the real work */
588 printf_unfiltered ("Ending remote %s debugging\n", target_shortname
);
591 /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
594 monitor_supply_register (regno
, valstr
)
598 unsigned LONGEST val
;
599 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
602 val
= strtoul (valstr
, &p
, 16);
604 if (val
== 0 && valstr
== p
)
605 error ("monitor_supply_register (%d): bad value from monitor: %s.",
608 /* supply register stores in target byte order, so swap here */
610 store_unsigned_integer (regbuf
, REGISTER_RAW_SIZE (regno
), val
);
612 supply_register (regno
, regbuf
);
617 /* Tell the remote machine to resume. */
620 monitor_resume (pid
, step
, sig
)
622 enum target_signal sig
;
624 dcache_flush (remote_dcache
);
626 monitor_printf (STEP_CMD
);
629 monitor_printf (CONT_CMD
);
630 if (current_monitor
->flags
& MO_NEED_REGDUMP_AFTER_CONT
)
635 /* Parse the output of a register dump command. A monitor specific regexp is
636 used to extract individual register descriptions of the form REG=VAL. Each
637 description is split up into a name and a value string which are passed down
638 to monitor specific code. */
641 parse_register_dump (buf
, len
)
647 int regnamelen
, vallen
;
649 /* Element 0 points to start of register name, and element 1 points to the
650 start of the register value. */
651 struct re_registers register_strings
;
653 if (re_search (®ister_pattern
, buf
, len
, 0, len
,
654 ®ister_strings
) == -1)
657 regnamelen
= register_strings
.end
[1] - register_strings
.start
[1];
658 regname
= buf
+ register_strings
.start
[1];
659 vallen
= register_strings
.end
[2] - register_strings
.start
[2];
660 val
= buf
+ register_strings
.start
[2];
662 current_monitor
->supply_register (regname
, regnamelen
, val
, vallen
);
664 buf
+= register_strings
.end
[0];
665 len
-= register_strings
.end
[0];
669 /* Send ^C to target to halt it. Target will respond, and send us a
673 monitor_interrupt (signo
)
676 /* If this doesn't work, try more severe steps. */
677 signal (signo
, monitor_interrupt_twice
);
680 printf_unfiltered ("monitor_interrupt called\n");
685 /* The user typed ^C twice. */
688 monitor_interrupt_twice (signo
)
691 signal (signo
, ofunc
);
693 monitor_interrupt_query ();
695 signal (signo
, monitor_interrupt
);
698 /* Ask the user what to do when an interrupt is received. */
701 monitor_interrupt_query ()
703 target_terminal_ours ();
705 if (query ("Interrupted while waiting for the program.\n\
706 Give up (and stop debugging it)? "))
708 target_mourn_inferior ();
709 return_to_top_level (RETURN_QUIT
);
712 target_terminal_inferior ();
716 monitor_wait_cleanup (old_timeout
)
719 timeout
= old_timeout
;
720 signal (SIGINT
, ofunc
);
724 /* Wait until the remote machine stops, then return, storing status in
725 status just as `wait' would. */
728 monitor_wait (pid
, status
)
730 struct target_waitstatus
*status
;
732 int old_timeout
= timeout
;
735 struct cleanup
*old_chain
;
737 status
->kind
= TARGET_WAITKIND_EXITED
;
738 status
->value
.integer
= 0;
740 old_chain
= make_cleanup (monitor_wait_cleanup
, old_timeout
);
742 #ifdef MAINTENANCE_CMDS
744 timeout
= watchdog
> 0 ? watchdog
: -1;
746 timeout
= -1; /* Don't time out -- user program is running. */
749 ofunc
= (void (*)()) signal (SIGINT
, monitor_interrupt
);
753 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
756 fprintf_unfiltered (gdb_stderr
, "monitor_wait: excessive response from monitor: %s.", buf
);
758 while (resp_len
< 0);
760 signal (SIGINT
, ofunc
);
762 timeout
= old_timeout
;
764 if (dump_reg_flag
&& current_monitor
->dump_registers
)
768 monitor_printf (current_monitor
->dump_registers
);
769 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
772 if (current_monitor
->register_pattern
)
773 parse_register_dump (buf
, resp_len
);
775 status
->kind
= TARGET_WAITKIND_STOPPED
;
776 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
778 discard_cleanups (old_chain
);
785 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
789 monitor_fetch_register (regno
)
793 static char zerobuf
[MAX_REGISTER_RAW_SIZE
] = {0};
794 char regbuf
[MAX_REGISTER_RAW_SIZE
* 2 + 1];
797 name
= REGNAMES (regno
);
801 supply_register (regno
, zerobuf
);
805 /* send the register examine command */
807 monitor_printf (current_monitor
->getreg
.cmd
, name
);
809 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
810 the register value. Otherwise, we just start searching from the start of
813 if (current_monitor
->getreg
.resp_delim
)
814 monitor_expect (current_monitor
->getreg
.resp_delim
, NULL
, 0);
816 /* Read upto the maximum number of hex digits for this register, skipping
817 spaces, but stop reading if something else is seen. Some monitors
818 like to drop leading zeros. */
820 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
) * 2; i
++)
823 c
= readchar (timeout
);
825 c
= readchar (timeout
);
833 regbuf
[i
] = '\000'; /* terminate the number */
835 /* If TERM is present, we wait for that to show up. Also, (if TERM is
836 present), we will send TERM_CMD if that is present. In any case, we collect
837 all of the output into buf, and then wait for the normal prompt. */
839 if (current_monitor
->getreg
.term
)
841 monitor_expect (current_monitor
->getreg
.term
, NULL
, 0); /* get response */
843 if (current_monitor
->getreg
.term_cmd
)
845 monitor_printf (current_monitor
->getreg
.term_cmd
);
846 monitor_expect_prompt (NULL
, 0);
850 monitor_expect_prompt (NULL
, 0); /* get response */
852 monitor_supply_register (regno
, regbuf
);
855 /* Read the remote registers into the block regs. */
857 static void monitor_dump_regs ()
859 if (current_monitor
->dump_registers
)
863 monitor_printf (current_monitor
->dump_registers
);
864 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
865 parse_register_dump (buf
, resp_len
);
868 abort(); /* Need some way to read registers */
872 monitor_fetch_registers (regno
)
875 if (current_monitor
->getreg
.cmd
)
879 monitor_fetch_register (regno
);
883 for (regno
= 0; regno
< NUM_REGS
; regno
++)
884 monitor_fetch_register (regno
);
887 monitor_dump_regs ();
891 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
894 monitor_store_register (regno
)
898 unsigned LONGEST val
;
900 name
= REGNAMES (regno
);
904 val
= read_register (regno
);
906 /* send the register deposit command */
908 monitor_printf (current_monitor
->setreg
.cmd
, name
, val
);
910 /* It's possible that there are actually some monitors out there that will
911 prompt you when you set a register. In that case, you may need to add some
912 code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get
913 an idea of what's needed...) */
915 monitor_expect_prompt (NULL
, 0);
918 /* Store the remote registers. */
921 monitor_store_registers (regno
)
926 monitor_store_register (regno
);
930 for (regno
= 0; regno
< NUM_REGS
; regno
++)
931 monitor_store_register (regno
);
934 /* Get ready to modify the registers array. On machines which store
935 individual registers, this doesn't need to do anything. On machines
936 which store all the registers in one fell swoop, this makes sure
937 that registers contains all the registers from the program being
941 monitor_prepare_to_store ()
943 /* Do nothing, since we can store individual regs */
947 monitor_files_info (ops
)
948 struct target_ops
*ops
;
950 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name
, baud_rate
);
954 monitor_write_memory (memaddr
, myaddr
, len
)
959 unsigned LONGEST val
;
963 /* Use memory fill command for leading 0 bytes. */
965 if (current_monitor
->fill
)
967 for (i
= 0; i
< len
; i
++)
971 if (i
> 4) /* More than 4 zeros is worth doing */
973 if (current_monitor
->flags
& MO_FILL_USES_ADDR
)
974 monitor_printf (current_monitor
->fill
, memaddr
, memaddr
+ i
, 0);
976 monitor_printf (current_monitor
->fill
, memaddr
, i
, 0);
978 monitor_expect_prompt (NULL
, 0);
984 if ((memaddr
& 0x7) == 0 && len
>= 8 && current_monitor
->setmem
.cmdll
)
987 cmd
= current_monitor
->setmem
.cmdll
;
989 else if ((memaddr
& 0x3) == 0 && len
>= 4 && current_monitor
->setmem
.cmdl
)
992 cmd
= current_monitor
->setmem
.cmdl
;
994 else if ((memaddr
& 0x1) == 0 && len
>= 2 && current_monitor
->setmem
.cmdw
)
997 cmd
= current_monitor
->setmem
.cmdw
;
1002 cmd
= current_monitor
->setmem
.cmdb
;
1005 val
= extract_unsigned_integer (myaddr
, len
);
1007 monitor_printf (cmd
, memaddr
, val
);
1009 monitor_expect_prompt (NULL
, 0);
1014 /* This is an alternate form of monitor_read_memory which is used for monitors
1015 which can only read a single byte/word/etc. at a time. */
1018 monitor_read_memory_single (memaddr
, myaddr
, len
)
1023 unsigned LONGEST val
;
1024 char membuf
[sizeof(LONGEST
) * 2 + 1];
1029 if ((memaddr
& 0x7) == 0 && len
>= 8 && current_monitor
->getmem
.cmdll
)
1032 cmd
= current_monitor
->getmem
.cmdll
;
1034 else if ((memaddr
& 0x3) == 0 && len
>= 4 && current_monitor
->getmem
.cmdl
)
1037 cmd
= current_monitor
->getmem
.cmdl
;
1039 else if ((memaddr
& 0x1) == 0 && len
>= 2 && current_monitor
->getmem
.cmdw
)
1042 cmd
= current_monitor
->getmem
.cmdw
;
1047 cmd
= current_monitor
->getmem
.cmdb
;
1050 /* Send the examine command. */
1052 monitor_printf (cmd
, memaddr
);
1054 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
1055 the register value. Otherwise, we just start searching from the start of
1058 if (current_monitor
->getmem
.resp_delim
)
1059 monitor_expect_regexp (getmem_resp_delim_pattern
, NULL
, 0);
1061 /* Now, read the appropriate number of hex digits for this loc, skipping
1064 for (i
= 0; i
< len
* 2; i
++)
1070 c
= readchar (timeout
);
1076 error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.",
1077 memaddr
, i
, membuf
, c
);
1083 membuf
[i
] = '\000'; /* terminate the number */
1085 /* If TERM is present, we wait for that to show up. Also, (if TERM is
1086 present), we will send TERM_CMD if that is present. In any case, we collect
1087 all of the output into buf, and then wait for the normal prompt. */
1089 if (current_monitor
->getmem
.term
)
1091 monitor_expect (current_monitor
->getmem
.term
, NULL
, 0); /* get response */
1093 if (current_monitor
->getmem
.term_cmd
)
1095 monitor_printf (current_monitor
->getmem
.term_cmd
);
1096 monitor_expect_prompt (NULL
, 0);
1100 monitor_expect_prompt (NULL
, 0); /* get response */
1103 val
= strtoul (membuf
, &p
, 16);
1105 if (val
== 0 && membuf
== p
)
1106 error ("monitor_read_memory_single (0x%x): bad value from monitor: %s.",
1109 /* supply register stores in target byte order, so swap here */
1111 store_unsigned_integer (myaddr
, len
, val
);
1116 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
1117 at MEMADDR. Returns length moved. Currently, we only do one byte at a
1121 monitor_read_memory (memaddr
, myaddr
, len
)
1126 unsigned LONGEST val
;
1127 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
1134 if (current_monitor
->flags
& MO_GETMEM_READ_SINGLE
)
1135 return monitor_read_memory_single (memaddr
, myaddr
, len
);
1137 len
= min (len
, 16);
1139 /* See if xfer would cross a 16 byte boundary. If so, clip it. */
1140 if (((memaddr
^ (memaddr
+ len
- 1)) & ~0xf) != 0)
1141 len
= ((memaddr
+ len
) & ~0xf) - memaddr
;
1143 /* send the memory examine command */
1145 if (current_monitor
->flags
& MO_GETMEM_NEEDS_RANGE
)
1146 monitor_printf (current_monitor
->getmem
.cmdb
, memaddr
, memaddr
+ len
- 1);
1148 monitor_printf (current_monitor
->getmem
.cmdb
, memaddr
, len
);
1150 /* If TERM is present, we wait for that to show up. Also, (if TERM is
1151 present), we will send TERM_CMD if that is present. In any case, we collect
1152 all of the output into buf, and then wait for the normal prompt. */
1154 if (current_monitor
->getmem
.term
)
1156 resp_len
= monitor_expect (current_monitor
->getmem
.term
, buf
, sizeof buf
); /* get response */
1159 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
1160 memaddr
, resp_len
, buf
);
1162 if (current_monitor
->getmem
.term_cmd
)
1164 SERIAL_WRITE (monitor_desc
, current_monitor
->getmem
.term_cmd
,
1165 strlen (current_monitor
->getmem
.term_cmd
));
1166 monitor_expect_prompt (NULL
, 0);
1170 resp_len
= monitor_expect_prompt (buf
, sizeof buf
); /* get response */
1174 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
1175 the values. Otherwise, we just start searching from the start of the buf.
1178 if (current_monitor
->getmem
.resp_delim
)
1181 struct re_registers resp_strings
;
1184 retval
= re_search (&getmem_resp_delim_pattern
, p
, tmp
, 0, tmp
,
1188 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1189 memaddr
, resp_len
, buf
);
1191 p
+= resp_strings
.end
[0];
1193 p
= strstr (p
, current_monitor
->getmem
.resp_delim
);
1195 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1196 memaddr
, resp_len
, buf
);
1197 p
+= strlen (current_monitor
->getmem
.resp_delim
);
1201 for (i
= len
; i
> 0; i
--)
1203 /* Skip non-hex chars, but bomb on end of string and newlines */
1209 if (*p
== '\000' || *p
== '\n' || *p
== '\r')
1210 error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr
, resp_len
, buf
);
1214 val
= strtoul (p
, &p1
, 16);
1216 if (val
== 0 && p
== p1
)
1217 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr
,
1232 monitor_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
1237 struct target_ops
*target
; /* ignored */
1239 return dcache_xfer_memory (remote_dcache
, memaddr
, myaddr
, len
, write
);
1245 return; /* ignore attempts to kill target system */
1248 /* All we actually do is set the PC to the start address of exec_bfd, and start
1249 the program at that point. */
1252 monitor_create_inferior (exec_file
, args
, env
)
1257 if (args
&& (*args
!= '\000'))
1258 error ("Args are not supported by the monitor.");
1260 clear_proceed_status ();
1261 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
1264 /* Clean up when a program exits.
1265 The program actually lives on in the remote processor's RAM, and may be
1266 run again without a download. Don't leave it full of breakpoint
1270 monitor_mourn_inferior ()
1272 unpush_target (targ_ops
);
1273 generic_mourn_inferior (); /* Do all the proper things now */
1276 #define NUM_MONITOR_BREAKPOINTS 8
1278 static CORE_ADDR breakaddr
[NUM_MONITOR_BREAKPOINTS
] = {0};
1280 /* Tell the monitor to add a breakpoint. */
1283 monitor_insert_breakpoint (addr
, shadow
)
1288 static unsigned char break_insn
[] = BREAKPOINT
;
1290 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
1292 if (breakaddr
[i
] == 0)
1294 breakaddr
[i
] = addr
;
1295 monitor_read_memory (addr
, shadow
, sizeof (break_insn
));
1296 monitor_printf (SET_BREAK_CMD
, addr
);
1297 monitor_expect_prompt (NULL
, 0);
1302 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS
);
1305 /* Tell the monitor to remove a breakpoint. */
1308 monitor_remove_breakpoint (addr
, shadow
)
1314 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
1316 if (breakaddr
[i
] == addr
)
1319 /* some monitors remove breakpoints based on the address */
1320 if (current_monitor
->flags
& MO_CLR_BREAK_USES_ADDR
)
1321 monitor_printf (CLR_BREAK_CMD
, addr
);
1323 monitor_printf (CLR_BREAK_CMD
, i
);
1324 monitor_expect_prompt (NULL
, 0);
1328 fprintf_unfiltered (stderr
, "Can't find breakpoint associated with 0x%x\n", addr
);
1332 /* monitor_load -- download a file. */
1335 monitor_load (file
, from_tty
)
1339 dcache_flush (remote_dcache
);
1341 if (current_monitor
->load_routine
)
1342 current_monitor
->load_routine (monitor_desc
, file
, hashmark
);
1344 { /* The default is ascii S-records */
1345 monitor_printf (LOAD_CMD
); /* tell the monitor to load */
1346 if (current_monitor
->loadresp
)
1347 monitor_expect (current_monitor
->loadresp
, NULL
, 0);
1349 load_srec (monitor_desc
, file
, 32, SREC_ALL
, hashmark
);
1351 monitor_expect_prompt (NULL
, 0);
1354 /* Finally, make the PC point at the start address */
1357 write_pc (bfd_get_start_address (exec_bfd
));
1359 inferior_pid
= 0; /* No process now */
1361 /* This is necessary because many things were based on the PC at the time that
1362 we attached to the monitor, which is no longer valid now that we have loaded
1363 new code (and just changed the PC). Another way to do this might be to call
1364 normal_stop, except that the stack may not be valid, and things would get
1365 horribly confused... */
1367 clear_symtab_users ();
1373 if (current_monitor
->stop
)
1374 monitor_printf_noecho (current_monitor
->stop
);
1377 /* Put a command string, in args, out to MONITOR. Output from MONITOR
1378 is placed on the users terminal until the prompt is seen. FIXME: We
1379 read the characters ourseleves here cause of a nasty echo. */
1382 monitor_command (args
, from_tty
)
1390 if (monitor_desc
== NULL
)
1391 error ("monitor target not open.");
1395 /* Send the command. Note that if no args were supplied, then we're
1396 just sending the monitor a newline, which is sometimes useful. */
1398 monitor_printf ("%s\r", (args
? args
: ""));
1400 resp_len
= monitor_expect_prompt (buf
, sizeof buf
);
1402 fputs_unfiltered (buf
, gdb_stdout
); /* Output the response */
1405 /* Convert hex digit A to a number. */
1411 if (a
>= '0' && a
<= '9')
1413 if (a
>= 'a' && a
<= 'f')
1414 return a
- 'a' + 10;
1415 if (a
>= 'A' && a
<= 'F')
1416 return a
- 'A' + 10;
1418 error ("Reply contains invalid hex digit 0x%x", a
);
1421 static struct target_ops monitor_ops
=
1423 NULL
, /* to_shortname */
1424 NULL
, /* to_longname */
1427 monitor_close
, /* to_close */
1428 NULL
, /* to_attach */
1429 monitor_detach
, /* to_detach */
1430 monitor_resume
, /* to_resume */
1431 monitor_wait
, /* to_wait */
1432 monitor_fetch_registers
, /* to_fetch_registers */
1433 monitor_store_registers
, /* to_store_registers */
1434 monitor_prepare_to_store
, /* to_prepare_to_store */
1435 monitor_xfer_memory
, /* to_xfer_memory */
1436 monitor_files_info
, /* to_files_info */
1437 monitor_insert_breakpoint
, /* to_insert_breakpoint */
1438 monitor_remove_breakpoint
, /* to_remove_breakpoint */
1439 0, /* to_terminal_init */
1440 0, /* to_terminal_inferior */
1441 0, /* to_terminal_ours_for_output */
1442 0, /* to_terminal_ours */
1443 0, /* to_terminal_info */
1444 monitor_kill
, /* to_kill */
1445 monitor_load
, /* to_load */
1446 0, /* to_lookup_symbol */
1447 monitor_create_inferior
, /* to_create_inferior */
1448 monitor_mourn_inferior
, /* to_mourn_inferior */
1450 0, /* to_notice_signals */
1451 0, /* to_thread_alive */
1452 monitor_stop
, /* to_stop */
1453 process_stratum
, /* to_stratum */
1455 1, /* to_has_all_memory */
1456 1, /* to_has_memory */
1457 1, /* to_has_stack */
1458 1, /* to_has_registers */
1459 1, /* to_has_execution */
1461 0, /* sections_end */
1462 OPS_MAGIC
/* to_magic */
1465 /* Init the target_ops structure pointed at by OPS */
1468 init_monitor_ops (ops
)
1469 struct target_ops
*ops
;
1471 memcpy (ops
, &monitor_ops
, sizeof monitor_ops
);
1474 /* Define additional commands that are usually only used by monitors. */
1477 _initialize_remote_monitors ()
1479 add_show_from_set (add_set_cmd ("hash", no_class
, var_boolean
,
1481 "Set display of activity while downloading a file.\n\
1482 When enabled, a hashmark \'#\' is displayed.",
1486 add_com ("monitor", class_obscure
, monitor_command
,
1487 "Send a command to the debug monitor.");