1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file was derived from various remote-* modules. It is a collection
22 of generic support functions so GDB can talk directly to a ROM based
23 monitor. This saves use from having to hack an exception based handler
24 into existance, and makes for quick porting.
26 This module talks to a debug monitor called 'MONITOR', which
27 We communicate with MONITOR via either a direct serial line, or a TCP
28 (or possibly TELNET) stream to a terminal multiplexor,
29 which in turn talks to the target board. */
38 #include <sys/types.h>
46 static void monitor_command
PARAMS ((char *args
, int fromtty
));
47 static void monitor_load_srec
PARAMS ((char *args
));
49 static int monitor_make_srec
PARAMS ((char *buffer
, int type
,
51 unsigned char *myaddr
, int len
));
53 static void monitor_fetch_register
PARAMS ((int regno
));
54 static void monitor_store_register
PARAMS ((int regno
));
56 static void monitor_close
PARAMS ((int quitting
));
57 static void monitor_detach
PARAMS ((char *args
, int from_tty
));
58 static void monitor_resume
PARAMS ((int pid
, int step
, enum target_signal sig
));
59 static int monitor_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
60 static void monitor_fetch_registers
PARAMS ((int regno
));
61 static void monitor_store_registers
PARAMS ((int regno
));
62 static void monitor_prepare_to_store
PARAMS ((void));
63 static int monitor_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
, int write
, struct target_ops
*target
));
64 static void monitor_files_info
PARAMS ((struct target_ops
*ops
));
65 static int monitor_insert_breakpoint
PARAMS ((CORE_ADDR addr
, char *shadow
));
66 static int monitor_remove_breakpoint
PARAMS ((CORE_ADDR addr
, char *shadow
));
67 static void monitor_kill
PARAMS ((void));
68 static void monitor_load
PARAMS ((char *file
, int from_tty
));
69 static void monitor_mourn_inferior
PARAMS ((void));
70 static void monitor_stop
PARAMS ((void));
72 static int from_hex
PARAMS ((int a
));
73 static unsigned long get_hex_word
PARAMS ((void));
75 static struct monitor_ops
*current_monitor
;
77 static int hashmark
; /* flag set by "set hash" */
79 static int timeout
= 30;
81 /* Descriptor for I/O to remote machine. Initialize it to NULL so
82 that monitor_open knows that we don't have a file open when the
85 static serial_t monitor_desc
= NULL
;
87 /* Pointer to regexp pattern matching data */
89 static struct re_pattern_buffer register_pattern
;
91 /* Element 0 points to start of register name, and element 1 points to the
92 start of the register value. */
94 static struct re_registers register_strings
;
96 static char fastmap
[256];
98 static int dump_reg_flag
; /* Non-zero means do a dump_registers cmd when
99 monitor_wait wakes up. */
101 /* monitor_printf -- send data to monitor. Works just like printf. */
104 monitor_printf (va_alist
)
114 pattern
= va_arg (args
, char *);
116 vsprintf (buf
, pattern
, args
);
118 if (remote_debug
> 0)
119 fputs_unfiltered (buf
, gdb_stderr
);
123 if (len
+ 1 > sizeof buf
)
126 if (SERIAL_WRITE(monitor_desc
, buf
, len
))
127 fprintf_unfiltered (stderr
, "SERIAL_WRITE failed: %s\n", safe_strerror (errno
));
130 /* Read a character from the remote system, doing all the fancy
139 c
= SERIAL_READCHAR (monitor_desc
, timeout
);
141 if (remote_debug
> 0)
142 fputc_unfiltered (c
, gdb_stderr
);
147 if (c
== SERIAL_TIMEOUT
)
148 error ("Timeout reading from remote system.");
150 perror_with_name ("remote-monitor");
153 /* Scan input from the remote system, until STRING is found. If BUF is non-
154 zero, then collect input until we have collected either STRING or BUFLEN-1
155 chars. In either case we terminate BUF with a 0. If input overflows BUF
156 because STRING can't be found, return -1, else return number of chars in BUF
157 (minus the terminating NUL). Note that in the non-overflow case, STRING
158 will be at the end of BUF. */
161 monitor_expect (string
, buf
, buflen
)
167 int obuflen
= buflen
;
182 c
= readchar (timeout
);
187 c
= readchar (timeout
);
198 return obuflen
- buflen
;
213 /* Keep discarding input until we see the MONITOR prompt.
215 The convention for dealing with the prompt is that you
217 o *then* wait for the prompt.
219 Thus the last thing that a procedure does with the serial line
220 will be an monitor_expect_prompt(). Exception: monitor_resume does not
221 wait for the prompt, because the terminal is being handed over
222 to the inferior. However, the next thing which happens after that
223 is a monitor_wait which does wait for the prompt.
224 Note that this includes abnormal exit, e.g. error(). This is
225 necessary to prevent getting into states from which we can't
229 monitor_expect_prompt (buf
, buflen
)
233 return monitor_expect (PROMPT
, buf
, buflen
);
236 /* Get N 32-bit words from remote, each preceded by a space, and put
237 them in registers starting at REGNO. */
247 ch
= readchar (timeout
);
252 for (i
= 7; i
>= 1; i
--)
254 ch
= readchar (timeout
);
257 val
= (val
<< 4) | from_hex (ch
);
263 /* Open a connection to a remote debugger. NAME is the filename used
264 for communication. */
266 static char *dev_name
;
267 static struct target_ops
*targ_ops
;
270 monitor_open (args
, mon_ops
, from_tty
)
272 struct monitor_ops
*mon_ops
;
279 if (mon_ops
->magic
!= MONITOR_OPS_MAGIC
)
280 error ("Magic number of monitor_ops struct wrong.");
282 targ_ops
= mon_ops
->target
;
283 name
= targ_ops
->to_shortname
;
286 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
287 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name
, name
);
289 target_preopen (from_tty
);
291 /* Setup pattern for register dump */
293 if (mon_ops
->register_pattern
)
298 register_pattern
.fastmap
= fastmap
;
299 tmp
= re_set_syntax (RE_SYNTAX_EMACS
);
300 val
= re_compile_pattern (mon_ops
->register_pattern
,
301 strlen (mon_ops
->register_pattern
),
305 error ("Can't compiler register pattern string: %s!", val
);
306 re_compile_fastmap (®ister_pattern
);
309 unpush_target (targ_ops
);
313 dev_name
= strsave (args
);
315 monitor_desc
= SERIAL_OPEN (dev_name
);
318 perror_with_name (dev_name
);
322 if (SERIAL_SETBAUDRATE (monitor_desc
, baud_rate
))
324 SERIAL_CLOSE (monitor_desc
);
325 perror_with_name (dev_name
);
329 SERIAL_RAW (monitor_desc
);
331 SERIAL_FLUSH_INPUT (monitor_desc
);
333 /* some systems only work with 2 stop bits */
335 SERIAL_SETSTOPBITS (monitor_desc
, mon_ops
->stopbits
);
337 current_monitor
= mon_ops
;
339 /* See if we can wake up the monitor. First, try sending a stop sequence,
340 then send the init strings. Last, remove all breakpoints. */
344 /* wake up the monitor and see if it's alive */
345 for (p
= mon_ops
->init
; *p
!= NULL
; p
++)
348 monitor_expect_prompt (NULL
, 0);
351 /* Remove all breakpoints */
353 if (mon_ops
->clr_all_break
)
355 monitor_printf (mon_ops
->clr_all_break
);
356 monitor_expect_prompt (NULL
, 0);
360 printf_unfiltered ("Remote target %s connected to %s\n", name
, dev_name
);
362 push_target (targ_ops
);
364 inferior_pid
= 42000; /* Make run command think we are busy... */
366 monitor_printf ("\r"); /* Give monitor_wait something to read */
371 /* Close out all files and local state before this target loses
375 monitor_close (quitting
)
379 SERIAL_CLOSE (monitor_desc
);
383 /* Terminate the open connection to the remote debugger. Use this
384 when you want to detach and do something else with your gdb. */
387 monitor_detach (args
, from_tty
)
391 pop_target (); /* calls monitor_close to do the real work */
393 printf_unfiltered ("Ending remote %s debugging\n", target_shortname
);
396 /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
399 monitor_supply_register (regno
, valstr
)
403 unsigned LONGEST val
;
404 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
407 val
= strtoul (valstr
, &p
, 16);
409 if (val
== 0 && valstr
== p
)
410 error ("monitor_supply_register (%d): bad value from monitor: %s.",
413 /* supply register stores in target byte order, so swap here */
415 store_unsigned_integer (regbuf
, REGISTER_RAW_SIZE (regno
), val
);
417 supply_register (regno
, regbuf
);
422 /* Tell the remote machine to resume. */
425 monitor_resume (pid
, step
, sig
)
427 enum target_signal sig
;
430 monitor_printf (STEP_CMD
);
433 monitor_printf (CONT_CMD
);
434 if (current_monitor
->flags
& MO_NEED_REGDUMP_AFTER_CONT
)
439 /* Parse the output of a register dump command. A monitor specific regexp is
440 used to extract individual register descriptions of the form REG=VAL. Each
441 description is split up into a name and a value string which are passed down
442 to monitor specific code. */
445 parse_register_dump (buf
, len
)
451 int regnamelen
, vallen
;
454 if (re_search (®ister_pattern
, buf
, len
, 0, len
,
455 ®ister_strings
) == -1)
458 regnamelen
= register_strings
.end
[1] - register_strings
.start
[1];
459 regname
= buf
+ register_strings
.start
[1];
460 vallen
= register_strings
.end
[2] - register_strings
.start
[2];
461 val
= buf
+ register_strings
.start
[2];
463 current_monitor
->supply_register (regname
, regnamelen
, val
, vallen
);
465 buf
+= register_strings
.end
[0];
466 len
-= register_strings
.end
[0];
470 /* Wait until the remote machine stops, then return, storing status in
471 status just as `wait' would. */
474 monitor_wait (pid
, status
)
476 struct target_waitstatus
*status
;
478 int old_timeout
= timeout
;
482 status
->kind
= TARGET_WAITKIND_EXITED
;
483 status
->value
.integer
= 0;
485 timeout
= -1; /* Don't time out -- user program is running. */
489 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
492 fprintf_unfiltered (gdb_stderr
, "monitor_wait: excessive response from monitor: %s.", buf
);
494 while (resp_len
< 0);
496 timeout
= old_timeout
;
498 if (dump_reg_flag
&& current_monitor
->dump_registers
)
502 monitor_printf (current_monitor
->dump_registers
);
503 resp_len
= monitor_expect_prompt (buf
, sizeof (buf
));
506 if (current_monitor
->register_pattern
)
507 parse_register_dump (buf
, resp_len
);
509 status
->kind
= TARGET_WAITKIND_STOPPED
;
510 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
515 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
519 monitor_fetch_register (regno
)
526 static char zerobuf
[MAX_REGISTER_RAW_SIZE
] = {0};
528 name
= REGNAMES (regno
);
532 supply_register (regno
, zerobuf
);
536 /* send the register examine command */
538 monitor_printf (current_monitor
->getreg
.cmd
, name
);
540 /* If TERM is present, we wait for that to show up. Also, (if TERM is
541 present), we will send TERM_CMD if that is present. In any case, we collect
542 all of the output into buf, and then wait for the normal prompt. */
544 if (current_monitor
->getreg
.term
)
546 resp_len
= monitor_expect (current_monitor
->getreg
.term
, buf
, sizeof buf
); /* get response */
549 error ("monitor_fetch_register (%d): excessive response from monitor: %.*s.",
550 regno
, resp_len
, buf
);
552 if (current_monitor
->getreg
.term_cmd
)
554 SERIAL_WRITE (monitor_desc
, current_monitor
->getreg
.term_cmd
,
555 strlen (current_monitor
->getreg
.term_cmd
));
556 monitor_expect_prompt (NULL
, 0);
560 resp_len
= monitor_expect_prompt (buf
, sizeof buf
); /* get response */
563 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
564 the register value. Otherwise, we just start searching from the start of
567 if (current_monitor
->getreg
.resp_delim
)
569 p
= strstr (buf
, current_monitor
->getreg
.resp_delim
);
571 error ("monitor_fetch_register (%d): bad response from monitor: %.*s.",
572 regno
, resp_len
, buf
);
573 p
+= strlen (current_monitor
->getreg
.resp_delim
);
578 monitor_supply_register (regno
, p
);
581 /* Read the remote registers into the block regs. */
584 monitor_fetch_registers (regno
)
589 monitor_fetch_register (regno
);
593 for (regno
= 0; regno
< NUM_REGS
; regno
++)
594 monitor_fetch_register (regno
);
597 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
600 monitor_store_register (regno
)
604 unsigned LONGEST val
;
606 name
= REGNAMES (regno
);
610 val
= read_register (regno
);
612 /* send the register deposit command */
614 monitor_printf (current_monitor
->setreg
.cmd
, name
, val
);
616 /* It's possible that there are actually some monitors out there that will
617 prompt you when you set a register. In that case, you may need to add some
618 code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get
619 an idea of what's needed...) */
621 monitor_expect_prompt (NULL
, 0);
624 /* Store the remote registers. */
627 monitor_store_registers (regno
)
632 monitor_store_register (regno
);
636 for (regno
= 0; regno
< NUM_REGS
; regno
++)
637 monitor_store_register (regno
);
640 /* Get ready to modify the registers array. On machines which store
641 individual registers, this doesn't need to do anything. On machines
642 which store all the registers in one fell swoop, this makes sure
643 that registers contains all the registers from the program being
647 monitor_prepare_to_store ()
649 /* Do nothing, since we can store individual regs */
653 monitor_files_info (ops
)
654 struct target_ops
*ops
;
656 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name
, baud_rate
);
660 monitor_write_memory (memaddr
, myaddr
, len
)
662 unsigned char *myaddr
;
665 unsigned LONGEST val
;
669 /* Use memory fill command for leading 0 bytes. */
671 if (current_monitor
->fill
)
673 for (i
= 0; i
< len
; i
++)
677 if (i
> 4) /* More than 4 zeros is worth doing */
679 if (current_monitor
->flags
& MO_FILL_USES_ADDR
)
680 monitor_printf (current_monitor
->fill
, memaddr
, memaddr
+ i
, 0);
682 monitor_printf (current_monitor
->fill
, memaddr
, i
, 0);
684 monitor_expect_prompt (NULL
, 0);
690 if ((memaddr
& 0x7) == 0 && len
>= 8 && current_monitor
->setmem
.cmdll
)
693 cmd
= current_monitor
->setmem
.cmdll
;
695 else if ((memaddr
& 0x3) == 0 && len
>= 4 && current_monitor
->setmem
.cmdl
)
698 cmd
= current_monitor
->setmem
.cmdl
;
700 else if ((memaddr
& 0x1) == 0 && len
>= 2 && current_monitor
->setmem
.cmdw
)
703 cmd
= current_monitor
->setmem
.cmdw
;
708 cmd
= current_monitor
->setmem
.cmdb
;
711 val
= extract_unsigned_integer (myaddr
, len
);
713 monitor_printf (cmd
, memaddr
, val
);
715 monitor_expect_prompt (NULL
, 0);
720 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
721 at MEMADDR. Returns length moved. Currently, we only do one byte at a
725 monitor_read_memory (memaddr
, myaddr
, len
)
730 unsigned LONGEST val
;
731 unsigned char regbuf
[MAX_REGISTER_RAW_SIZE
];
740 /* See if xfer would cross a 16 byte boundary. If so, clip it. */
741 if (((memaddr
^ (memaddr
+ len
- 1)) & ~0xf) != 0)
742 len
= ((memaddr
+ len
) & ~0xf) - memaddr
;
744 /* send the memory examine command */
746 if (current_monitor
->flags
& MO_GETMEM_NEEDS_RANGE
)
747 monitor_printf (current_monitor
->getmem
.cmdb
, memaddr
, memaddr
+ len
- 1);
749 monitor_printf (current_monitor
->getmem
.cmdb
, memaddr
, len
);
751 /* If TERM is present, we wait for that to show up. Also, (if TERM is
752 present), we will send TERM_CMD if that is present. In any case, we collect
753 all of the output into buf, and then wait for the normal prompt. */
755 if (current_monitor
->getmem
.term
)
757 resp_len
= monitor_expect (current_monitor
->getmem
.term
, buf
, sizeof buf
); /* get response */
760 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
761 memaddr
, resp_len
, buf
);
763 if (current_monitor
->getmem
.term_cmd
)
765 SERIAL_WRITE (monitor_desc
, current_monitor
->getmem
.term_cmd
,
766 strlen (current_monitor
->getmem
.term_cmd
));
767 monitor_expect_prompt (NULL
, 0);
771 resp_len
= monitor_expect_prompt (buf
, sizeof buf
); /* get response */
775 while (*p
!= '\r') /* Skip command echo and line delim */
778 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
779 the values. Otherwise, we just start searching from the start of the buf.
782 if (current_monitor
->getmem
.resp_delim
)
784 p
= strstr (p
, current_monitor
->getmem
.resp_delim
);
786 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
787 memaddr
, resp_len
, buf
);
788 p
+= strlen (current_monitor
->getmem
.resp_delim
);
791 for (i
= len
; i
> 0; i
--)
793 /* Skip non-hex chars, but bomb on end of string and newlines */
799 if (*p
== '\000' || *p
== '\n' || *p
== '\r')
800 error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr
, resp_len
, buf
);
804 val
= strtoul (p
, &p1
, 16);
806 if (val
== 0 && p
== p1
)
807 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr
,
821 /* FIXME-someday! merge these two. */
824 monitor_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
829 struct target_ops
*target
; /* ignored */
832 return monitor_write_memory (memaddr
, myaddr
, len
);
834 return monitor_read_memory (memaddr
, myaddr
, len
);
840 return; /* ignore attempts to kill target system */
843 /* All we actually do is set the PC to the start address of exec_bfd, and start
844 the program at that point. */
847 monitor_create_inferior (exec_file
, args
, env
)
852 if (args
&& (*args
!= '\000'))
853 error ("Args are not supported by the monitor.");
855 clear_proceed_status ();
856 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
859 /* Clean up when a program exits.
860 The program actually lives on in the remote processor's RAM, and may be
861 run again without a download. Don't leave it full of breakpoint
865 monitor_mourn_inferior ()
867 unpush_target (targ_ops
);
868 generic_mourn_inferior (); /* Do all the proper things now */
871 #define NUM_MONITOR_BREAKPOINTS 8
873 static CORE_ADDR breakaddr
[NUM_MONITOR_BREAKPOINTS
] = {0};
875 /* Tell the monitor to add a breakpoint. */
878 monitor_insert_breakpoint (addr
, shadow
)
883 static unsigned char break_insn
[] = BREAKPOINT
;
885 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
887 if (breakaddr
[i
] == 0)
890 monitor_read_memory (addr
, shadow
, sizeof (break_insn
));
891 monitor_printf (SET_BREAK_CMD
, addr
);
892 monitor_expect_prompt (NULL
, 0);
897 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS
);
900 /* Tell the monitor to remove a breakpoint. */
903 monitor_remove_breakpoint (addr
, shadow
)
909 for (i
= 0; i
< NUM_MONITOR_BREAKPOINTS
; i
++)
911 if (breakaddr
[i
] == addr
)
914 /* some monitors remove breakpoints based on the address */
915 if (current_monitor
->flags
& MO_CLR_BREAK_USES_ADDR
)
916 monitor_printf (CLR_BREAK_CMD
, addr
);
918 monitor_printf (CLR_BREAK_CMD
, i
);
919 monitor_expect_prompt (NULL
, 0);
923 fprintf_unfiltered (stderr
, "Can't find breakpoint associated with 0x%x\n", addr
);
927 /* monitor_load -- download a file. */
930 monitor_load (file
, from_tty
)
934 if (current_monitor
->load_routine
)
935 current_monitor
->load_routine (monitor_desc
, file
, hashmark
);
937 monitor_load_srec (file
);
939 /* Finally, make the PC point at the start address */
941 write_pc (bfd_get_start_address (exec_bfd
));
943 inferior_pid
= 0; /* No process now */
945 /* This is necessary because many things were based on the PC at the time that
946 we attached to the monitor, which is no longer valid now that we have loaded
947 new code (and just changed the PC). Another way to do this might be to call
948 normal_stop, except that the stack may not be valid, and things would get
949 horribly confused... */
951 clear_symtab_users ();
957 if (!current_monitor
->stop
)
960 monitor_printf(current_monitor
->stop
);
961 monitor_expect_prompt (NULL
, 0);
964 /* Put a command string, in args, out to MONITOR. Output from MONITOR
965 is placed on the users terminal until the prompt is seen. FIXME: We
966 read the characters ourseleves here cause of a nasty echo. */
969 monitor_command (args
, from_tty
)
977 if (monitor_desc
== NULL
)
978 error ("monitor target not open.");
982 /* Send the command. Note that if no args were supplied, then we're
983 just sending the monitor a newline, which is sometimes useful. */
985 monitor_printf ("%s\r", (args
? args
: ""));
987 resp_len
= monitor_expect_prompt (buf
, sizeof buf
);
989 fputs_unfiltered (buf
, gdb_stdout
); /* Output the response */
992 /* Download a binary file by converting it to S records. */
995 monitor_load_srec (args
)
1000 char *buffer
, srec
[1024];
1002 int srec_frame
= 128;
1005 buffer
= alloca (srec_frame
* 2 + 256);
1007 abfd
= bfd_openr (args
, 0);
1010 printf_filtered ("Unable to open file %s\n", args
);
1014 if (bfd_check_format (abfd
, bfd_object
) == 0)
1016 printf_filtered ("File is not an object file\n");
1020 monitor_printf (LOAD_CMD
); /* tell the monitor to load */
1021 if (current_monitor
->loadresp
)
1022 monitor_expect (current_monitor
->loadresp
, NULL
, 0);
1024 for (s
= abfd
->sections
; s
; s
= s
->next
)
1025 if (s
->flags
& SEC_LOAD
)
1027 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s
->name
, s
->vma
,
1028 s
->vma
+ s
->_raw_size
);
1029 gdb_flush (gdb_stdout
);
1031 for (i
= 0; i
< s
->_raw_size
; i
+= srec_frame
)
1035 numbytes
= min (srec_frame
, s
->_raw_size
- i
);
1037 bfd_get_section_contents (abfd
, s
, buffer
, i
, numbytes
);
1039 reclen
= monitor_make_srec (srec
, 3, s
->vma
+ i
, buffer
, numbytes
);
1041 monitor_printf ("%.*s\r", reclen
, srec
);
1045 putchar_unfiltered ('#');
1046 gdb_flush (gdb_stdout
);
1048 } /* Per-packet (or S-record) loop */
1050 putchar_unfiltered ('\n');
1051 } /* Loadable sections */
1054 putchar_unfiltered ('\n');
1056 /* Write a type 7 terminator record. no data for a type 7, and there
1057 is no data, so len is 0. */
1059 reclen
= monitor_make_srec (srec
, 7, abfd
->start_address
, NULL
, 0);
1061 monitor_printf ("%.*s\r", reclen
, srec
);
1063 monitor_printf ("\r\r"); /* Some monitors need these to wake up */
1065 monitor_expect_prompt (NULL
, 0);
1069 * monitor_make_srec -- make an srecord. This writes each line, one at a
1070 * time, each with it's own header and trailer line.
1071 * An srecord looks like this:
1073 * byte count-+ address
1074 * start ---+ | | data +- checksum
1076 * S01000006F6B692D746573742E73726563E4
1077 * S315000448600000000000000000FC00005900000000E9
1078 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1079 * S30B0004485A0000000000004E
1082 * S<type><length><address><data><checksum>
1086 * is the number of bytes following upto the checksum. Note that
1087 * this is not the number of chars following, since it takes two
1088 * chars to represent a byte.
1092 * 1) two byte address data record
1093 * 2) three byte address data record
1094 * 3) four byte address data record
1095 * 7) four byte address termination record
1096 * 8) three byte address termination record
1097 * 9) two byte address termination record
1100 * is the start address of the data following, or in the case of
1101 * a termination record, the start address of the image
1105 * is the sum of all the raw byte data in the record, from the length
1106 * upwards, modulo 256 and subtracted from 255.
1108 * This routine returns the length of the S-record.
1113 monitor_make_srec (buffer
, type
, memaddr
, myaddr
, len
)
1117 unsigned char *myaddr
;
1120 unsigned char checksum
;
1123 static char hextab
[] = "0123456789ABCDEF";
1129 /* Create the header for the srec. 4 is the number of bytes in the address,
1130 and 1 is the number of bytes in the count. */
1132 sprintf (buf
, "S%d%02X%08X", type
, len
+ 4 + 1, memaddr
);
1135 /* Note that the checksum is calculated on the raw data, not the hexified
1136 data. It includes the length, address and the data portions of the
1139 checksum
+= (len
+ 4 + 1 /* Packet length */
1140 + (memaddr
& 0xff) /* Address... */
1141 + ((memaddr
>> 8) & 0xff)
1142 + ((memaddr
>> 16) & 0xff)
1143 + ((memaddr
>> 24) & 0xff));
1145 /* build the srecord */
1146 for (i
= 0; i
< len
; i
++)
1148 *buf
++ = hextab
[myaddr
[i
] >> 4];
1149 *buf
++ = hextab
[myaddr
[i
] & 0xf];
1150 checksum
+= myaddr
[i
];
1153 checksum
= ~checksum
;
1155 *buf
++ = hextab
[checksum
>> 4];
1156 *buf
++ = hextab
[checksum
& 0xf];
1158 return buf
- buffer
;
1161 /* Convert hex digit A to a number. */
1167 if (a
>= '0' && a
<= '9')
1169 if (a
>= 'a' && a
<= 'f')
1170 return a
- 'a' + 10;
1171 if (a
>= 'A' && a
<= 'F')
1172 return a
- 'A' + 10;
1174 error ("Reply contains invalid hex digit 0x%x", a
);
1177 static struct target_ops monitor_ops
=
1179 NULL
, /* to_shortname */
1180 NULL
, /* to_longname */
1183 monitor_close
, /* to_close */
1184 NULL
, /* to_attach */
1185 monitor_detach
, /* to_detach */
1186 monitor_resume
, /* to_resume */
1187 monitor_wait
, /* to_wait */
1188 monitor_fetch_registers
, /* to_fetch_registers */
1189 monitor_store_registers
, /* to_store_registers */
1190 monitor_prepare_to_store
, /* to_prepare_to_store */
1191 monitor_xfer_memory
, /* to_xfer_memory */
1192 monitor_files_info
, /* to_files_info */
1193 monitor_insert_breakpoint
, /* to_insert_breakpoint */
1194 monitor_remove_breakpoint
, /* to_remove_breakpoint */
1195 0, /* to_terminal_init */
1196 0, /* to_terminal_inferior */
1197 0, /* to_terminal_ours_for_output */
1198 0, /* to_terminal_ours */
1199 0, /* to_terminal_info */
1200 monitor_kill
, /* to_kill */
1201 monitor_load
, /* to_load */
1202 0, /* to_lookup_symbol */
1203 monitor_create_inferior
, /* to_create_inferior */
1204 monitor_mourn_inferior
, /* to_mourn_inferior */
1206 0, /* to_notice_signals */
1207 monitor_stop
, /* to_stop */
1208 process_stratum
, /* to_stratum */
1210 1, /* to_has_all_memory */
1211 1, /* to_has_memory */
1212 1, /* to_has_stack */
1213 1, /* to_has_registers */
1214 1, /* to_has_execution */
1216 0, /* sections_end */
1217 OPS_MAGIC
/* to_magic */
1220 /* Init the target_ops structure pointed at by OPS */
1223 init_monitor_ops (ops
)
1224 struct target_ops
*ops
;
1226 memcpy (ops
, &monitor_ops
, sizeof monitor_ops
);
1229 /* Define additional commands that are usually only used by monitors. */
1232 _initialize_remote_monitors ()
1234 add_show_from_set (add_set_cmd ("hash", no_class
, var_boolean
,
1236 "Set display of activity while downloading a file.\n\
1237 When enabled, a hashmark \'#\' is displayed.",
1241 add_com ("monitor", class_obscure
, monitor_command
,
1242 "Send a command to the debug monitor.");