Cleanup debug logging, fix single stepping. WinBond works good!
[binutils-gdb.git] / gdb / monitor.c
1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
20
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.
25
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.
30 */
31
32 #include "defs.h"
33 #include "gdbcore.h"
34 #include "target.h"
35 #include "wait.h"
36 #include <varargs.h>
37 #include <signal.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include "command.h"
41 #include "serial.h"
42 #include "monitor.h"
43 #include "remote-utils.h"
44
45 #ifdef HAVE_TERMIO
46 # define TERMINAL struct termios
47 #else
48 # define TERMINAL struct sgttyb
49 #endif
50
51 struct monitor_ops *current_monitor;
52 extern struct cmd_list_element *setlist;
53 extern struct cmd_list_element *unsetlist;
54 struct cmd_list_element *showlist;
55
56 static int hashmark; /* flag set by "set hash" */
57
58 /* FIXME: Replace with sr_get_debug (). */
59 #define LOG_FILE "monitor.log"
60 #if defined (LOG_FILE)
61 FILE *log_file;
62 #endif
63
64 static int timeout = 24;
65
66 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
67 monitor_open knows that we don't have a file open when the program starts.
68 */
69 static serial_t monitor_desc = NULL;
70
71 /* sets the download protocol, choices are srec, generic, boot */
72 char *loadtype;
73 static char *loadtype_str;
74 static void set_loadtype_command();
75
76 /*
77 * set_loadtype_command -- set the type for downloading. Check to make
78 * sure you have a support protocol for this target.
79 */
80 static void
81 set_loadtype_command (ignore, from_tty, c)
82 char *ignore;
83 int from_tty;
84 struct cmd_list_element *c;
85 {
86 #if 0
87 char *type;
88 if (strcmp (LOADTYPES, "")) {
89 error ("No loadtype set");
90 return;
91 }
92
93 type = strtok(LOADTYPES, ",");
94 if (STREQ (type, (*(char **) c->var))) {
95 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
96 return;
97 }
98
99 while (type = strtok (NULL, ",") != (char *)NULL)
100 if (STREQ (type, (*(char **) c->var)))
101 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
102 #endif
103 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
104 }
105
106 /*
107 * printf_monitor -- send data to monitor. Works just like printf.
108 */
109 static void
110 printf_monitor(va_alist)
111 va_dcl
112 {
113 va_list args;
114 char *pattern;
115 char buf[200];
116 int i;
117
118 va_start(args);
119
120 pattern = va_arg(args, char *);
121
122 vsprintf(buf, pattern, args);
123
124 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);
125
126 if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
127 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
128 }
129
130 /*
131 * debuglogs -- deal with debugging info to multiple sources. This takes
132 * two real args, the first one is the level to be compared against
133 * the sr_get_debug() value, the second arg is a printf buffer and args
134 * to be formatted and printed. A CR is added after each string is printed.
135 */
136 static void
137 debuglogs(va_alist)
138 va_dcl
139 {
140 va_list args;
141 char *pattern, *p;
142 char buf[200];
143 char newbuf[300];
144 int level, i;
145
146 va_start(args);
147
148 level = va_arg(args, int); /* get the debug level */
149 if ((level <0) || (level > 100)) {
150 error ("Bad argument passed to debuglogs()");
151 return;
152 }
153
154 pattern = va_arg(args, char *); /* get the printf style pattern */
155
156 vsprintf(buf, pattern, args); /* format the string */
157
158 /* convert some characters so it'll look right in the log */
159 p = newbuf;
160 for (i=0 ; buf[i] != '\0'; i++) {
161 switch (buf[i]) {
162 case '\n': /* newlines */
163 *p++ = '\\';
164 *p++ = 'n';
165 continue;
166 case '\r': /* carriage returns */
167 *p++ = '\\';
168 *p++ = 'r';
169 continue;
170 case '\033': /* escape */
171 *p++ = '\\';
172 *p++ = 'e';
173 continue;
174 case '\t': /* tab */
175 *p++ = '\\';
176 *p++ = 't';
177 continue;
178 case '\b': /* backspace */
179 *p++ = '\\';
180 *p++ = 'b';
181 continue;
182 default: /* no change */
183 *p++ = buf[i];
184 }
185
186 if (buf[i] < 26) { /* modify control characters */
187 *p++ = '^';
188 *p++ = buf[i] + 'A';
189 continue;
190 }
191 }
192 *p = '\0'; /* terminate the string */
193
194 if (sr_get_debug() > level)
195 puts (newbuf);
196
197 #ifdef LOG_FILE /* write to the monitor log */
198 if (log_file != 0x0) {
199 fputs (newbuf, log_file);
200 fputc ('\n', log_file);
201 fflush (log_file);
202 }
203 #endif
204 }
205
206 /* readchar -- read a character from the remote system, doing all the fancy
207 * timeout stuff.
208 */
209 static int
210 readchar(timeout)
211 int timeout;
212 {
213 int c;
214
215 c = SERIAL_READCHAR(monitor_desc, timeout);
216
217 if (sr_get_debug() > 4)
218 putchar(c & 0x7f);
219
220 #ifdef LOG_FILE
221 if (isascii (c))
222 putc(c & 0x7f, log_file);
223 #endif
224
225 if (c >= 0)
226 return c & 0x7f;
227
228 if (c == SERIAL_TIMEOUT) {
229 if (timeout == 0)
230 return c; /* Polls shouldn't generate timeout errors */
231 error("Timeout reading from remote system.");
232 }
233 perror_with_name("remote-monitor");
234 }
235
236 /*
237 * expect -- scan input from the remote system, until STRING is found.
238 * If DISCARD is non-zero, then discard non-matching input, else print
239 * it out. Let the user break out immediately.
240 */
241 static void
242 expect (string, discard)
243 char *string;
244 int discard;
245 {
246 char *p = string;
247 int c;
248
249
250 debuglogs (1, "Expecting \"%s\".", string);
251
252 immediate_quit = 1;
253 while (1) {
254 c = readchar(timeout);
255 if (!isascii (c))
256 continue;
257 if (c == *p++) {
258 if (*p == '\0') {
259 immediate_quit = 0;
260 if (sr_get_debug())
261 printf ("\nMatched\n");
262 return;
263 }
264 } else {
265 if (!discard) {
266 fwrite(string, 1, (p - 1) - string, stdout);
267 putchar((char)c);
268 fflush(stdout);
269 }
270 p = string;
271 }
272 }
273 }
274
275 /* Keep discarding input until we see the MONITOR prompt.
276
277 The convention for dealing with the prompt is that you
278 o give your command
279 o *then* wait for the prompt.
280
281 Thus the last thing that a procedure does with the serial line
282 will be an expect_prompt(). Exception: monitor_resume does not
283 wait for the prompt, because the terminal is being handed over
284 to the inferior. However, the next thing which happens after that
285 is a monitor_wait which does wait for the prompt.
286 Note that this includes abnormal exit, e.g. error(). This is
287 necessary to prevent getting into states from which we can't
288 recover. */
289 static void
290 expect_prompt(discard)
291 int discard;
292 {
293 #if defined (LOG_FILE)
294 /* This is a convenient place to do this. The idea is to do it often
295 enough that we never lose much data if we terminate abnormally. */
296 fflush(log_file);
297 #endif
298 expect (PROMPT, discard);
299 }
300
301 /*
302 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
303 */
304 static int
305 junk(ch)
306 char ch;
307 {
308 switch (ch) {
309 case ' ':
310 case '-':
311 case '\t':
312 case '\r':
313 case '\n':
314 if (sr_get_debug() > 5)
315 debuglogs (5, "Ignoring \'%c\'.", ch);
316 return 1;
317 default:
318 if (sr_get_debug() > 5)
319 debuglogs (5, "Accepting \'%c\'.", ch);
320 return 0;
321 }
322 }
323
324 /*
325 * get_hex_digit -- Get a hex digit from the remote system & return its value.
326 * If ignore is nonzero, ignore spaces, newline & tabs.
327 */
328 static int
329 get_hex_digit(ignore)
330 int ignore;
331 {
332 static int ch;
333 while (1) {
334 ch = readchar(timeout);
335 if (junk(ch))
336 continue;
337 if (sr_get_debug() > 4)
338 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
339
340 if (ch >= '0' && ch <= '9')
341 return ch - '0';
342 else if (ch >= 'A' && ch <= 'F')
343 return ch - 'A' + 10;
344 else if (ch >= 'a' && ch <= 'f')
345 return ch - 'a' + 10;
346 else if (ch == ' ' && ignore)
347 ;
348 else {
349 expect_prompt(1);
350 error("Invalid hex digit from remote system.");
351 }
352 }
353 }
354
355 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
356 * Accept any number leading spaces.
357 */
358 static void
359 get_hex_byte (byt)
360 char *byt;
361 {
362 int val;
363
364 val = get_hex_digit (1) << 4;
365 debuglogs (4, "get_hex_digit() -- Read first nibble 0x%x", val);
366
367 val |= get_hex_digit (0);
368 debuglogs (4, "get_hex_digit() -- Read second nibble 0x%x", val);
369 *byt = val;
370
371 debuglogs (4, "get_hex_digit() -- Read a 0x%x", val);
372 }
373
374 /*
375 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
376 * and put them in registers starting at REGNO.
377 */
378 static int
379 get_hex_word ()
380 {
381 long val;
382 int i;
383
384 val = 0;
385 for (i = 0; i < 8; i++)
386 val = (val << 4) + get_hex_digit (i == 0);
387
388 debuglogs (3, "get_hex_word() got a 0x%x.", val);
389
390 return val;
391 }
392
393 /* This is called not only when we first attach, but also when the
394 user types "run" after having attached. */
395 void
396 monitor_create_inferior (execfile, args, env)
397 char *execfile;
398 char *args;
399 char **env;
400 {
401 int entry_pt;
402
403 if (args && *args)
404 error("Can't pass arguments to remote MONITOR process");
405
406 if (execfile == 0 || exec_bfd == 0)
407 error("No exec file specified");
408
409 entry_pt = (int) bfd_get_start_address (exec_bfd);
410
411 debuglogs (2, "create_inferior(exexfile=%s, args=%s, env=%s)", execfile, args, env);
412
413 /* The "process" (board) is already stopped awaiting our commands, and
414 the program is already downloaded. We just set its PC and go. */
415
416 clear_proceed_status ();
417
418 /* Tell wait_for_inferior that we've started a new process. */
419 init_wait_for_inferior ();
420
421 /* Set up the "saved terminal modes" of the inferior
422 based on what modes we are starting it with. */
423 target_terminal_init ();
424
425 /* Install inferior's terminal modes. */
426 target_terminal_inferior ();
427
428 /* insert_step_breakpoint (); FIXME, do we need this? */
429
430 /* Let 'er rip... */
431 proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
432 }
433
434 /*
435 * monitor_open -- open a connection to a remote debugger.
436 * NAME is the filename used for communication.
437 */
438 static int baudrate = 9600;
439 static char dev_name[100];
440
441 void
442 monitor_open(args, name, from_tty)
443 char *args;
444 char *name;
445 int from_tty;
446 {
447
448 if (args == NULL)
449 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
450 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
451
452 /* if (is_open) */
453 monitor_close(0);
454
455 strcpy(dev_name, args);
456 monitor_desc = SERIAL_OPEN(dev_name);
457
458 if (monitor_desc == NULL)
459 perror_with_name(dev_name);
460
461 if (baud_rate != -1)
462 {
463 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
464 {
465 SERIAL_CLOSE (monitor_desc);
466 perror_with_name (name);
467 }
468 }
469
470 SERIAL_RAW(monitor_desc);
471
472 #if defined (LOG_FILE)
473 log_file = fopen (LOG_FILE, "w");
474 if (log_file == NULL)
475 perror_with_name (LOG_FILE);
476 #endif
477
478 /* wake up the monitor and see if it's alive */
479 printf_monitor(INIT_CMD);
480 expect_prompt(1); /* See if we get a prompt */
481
482 /* try again to be sure */
483 printf_monitor(INIT_CMD);
484 expect_prompt(1); /* See if we get a prompt */
485
486 if (from_tty)
487 printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name);
488
489
490 }
491
492 /*
493 * monitor_close -- Close out all files and local state before this
494 * target loses control.
495 */
496
497 void
498 monitor_close (quitting)
499 int quitting;
500 {
501 SERIAL_CLOSE(monitor_desc);
502 monitor_desc = NULL;
503
504 debuglogs (1, "monitor_close (quitting=%d)", quitting);
505
506 #if defined (LOG_FILE)
507 if (log_file) {
508 if (ferror(log_file))
509 fprintf(stderr, "Error writing log file.\n");
510 if (fclose(log_file) != 0)
511 fprintf(stderr, "Error closing log file.\n");
512 }
513 #endif
514 }
515
516 /*
517 * monitor_detach -- terminate the open connection to the remote
518 * debugger. Use this when you want to detach and do something
519 * else with your gdb.
520 */
521 void
522 monitor_detach (from_tty)
523 int from_tty;
524 {
525
526 debuglogs (4, "monitor_detach ()");
527
528 pop_target(); /* calls monitor_close to do the real work */
529 if (from_tty)
530 printf ("Ending remote %s debugging\n", target_shortname);
531 }
532
533 /*
534 * monitor_attach -- attach GDB to the target.
535 */
536 void
537 monitor_attach (args, from_tty)
538 char *args;
539 int from_tty;
540 {
541 if (from_tty)
542 printf ("Starting remote %s debugging\n", target_shortname);
543
544 #ifdef LOG_FILE
545 fprintf (log_file, "\nmonitor_attach (args=%s)\n", args);
546 #endif
547
548 if (sr_get_debug() > 4)
549 printf ("\nmonitor_attach (args=%s)\n", args);
550
551 printf_monitor (GO_CMD);
552 /* swallow the echo. */
553 expect (GO_CMD, 1);
554 }
555
556 /*
557 * monitor_resume -- Tell the remote machine to resume.
558 */
559 void
560 monitor_resume (pid, step, sig)
561 int pid, step;
562 enum target_signal sig;
563 {
564 debuglogs (4, "monitor_resume (step=%d, sig=%d)", step, sig);
565
566 if (step) {
567 printf_monitor (STEP_CMD);
568 /* wait for the echo. */
569 expect (STEP_CMD, 1);
570 } else {
571 printf_monitor (CONT_CMD);
572 /* swallow the echo. */
573 expect (CONT_CMD, 1);
574 }
575 }
576
577 /*
578 * _wait -- Wait until the remote machine stops, then return,
579 * storing status in status just as `wait' would.
580 */
581
582 int
583 monitor_wait (pid, status)
584 int pid;
585 struct target_waitstatus *status;
586 {
587 int old_timeout = timeout;
588
589 debuglogs(4, "monitor_wait (), printing extraneous text.", log_file);
590
591 status->kind = TARGET_WAITKIND_EXITED;
592 status->value.integer = 0;
593
594 timeout = 0; /* Don't time out -- user program is running. */
595
596 expect_prompt(0); /* Wait for prompt, outputting extraneous text */
597 if (sr_get_debug() > 4)
598 puts ("monitor_wait(), got the prompt.");
599
600 debuglogs (4, "monitor_wait(%x), got the prompt.", 12345678);
601
602 status->kind = TARGET_WAITKIND_STOPPED;
603 status->value.sig = TARGET_SIGNAL_TRAP;
604
605 timeout = old_timeout;
606
607 return 0;
608 }
609
610 /* Return the name of register number regno in the form input and output by
611 monitor. Currently, register_names just happens to contain exactly what
612 monitor wants. Lets take advantage of that just as long as possible! */
613
614 static char *
615 get_reg_name (regno)
616 int regno;
617 {
618 static char buf[50];
619 const char *p;
620 char *b;
621
622 b = buf;
623
624 if (regno < 0)
625 return ("");
626
627 for (p = REGNAMES(regno); *p; p++)
628 *b++ = tolower(*p);
629
630 *b = '\000';
631
632 debuglogs (5, "Got name \"%s\" from regno #%d.", buf, regno);
633
634 return buf;
635 }
636
637 /*
638 * monitor_fetch_registers -- read the remote registers into the
639 * block regs.
640 */
641 void
642 monitor_fetch_registers ()
643 {
644 int regno;
645
646 /* yeah yeah, i know this is horribly inefficient. but it isn't done
647 very often... i'll clean it up later. */
648
649 for (regno = 0; regno <= PC_REGNUM; regno++)
650 monitor_fetch_register(regno);
651 }
652
653 /*
654 * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO
655 * is -1. Returns errno value.
656 */
657 void
658 monitor_fetch_register (regno)
659 int regno;
660 {
661 int val, j;
662
663 debuglogs (1, "monitor_fetch_register (regno=%d)", get_reg_name (regno));
664
665 if (regno < 0) {
666 monitor_fetch_registers ();
667 } else {
668 char *name = get_reg_name (regno);
669 if (STREQ(name, ""))
670 return;
671 printf_monitor (ROMCMD(GET_REG), name); /* send the command */
672 expect (name, 1); /* then strip the leading garbage */
673 if (*ROMDELIM(GET_REG) != 0) { /* if there's a delimiter */
674 expect (ROMDELIM(GET_REG), 1);
675 }
676
677 val = get_hex_word(); /* get the value, ignore junk */
678 supply_register (regno, (char *) &val);
679
680 if (*ROMDELIM(GET_REG) != 0) {
681 /*** expect (ROMRES(GET_REG)); ***/
682 printf_monitor (CMD_END);
683 }
684 expect_prompt (1);
685 }
686 return;
687 }
688
689 /* Store the remote registers from the contents of the block REGS. */
690
691 void
692 monitor_store_registers ()
693 {
694 int regno;
695
696 debuglogs (1, "monitor_store_registers()");
697
698 for (regno = 0; regno <= PC_REGNUM; regno++)
699 monitor_store_register(regno);
700
701 registers_changed ();
702 }
703
704 /*
705 * monitor_store_register -- store register REGNO, or all if REGNO == 0.
706 * return errno value.
707 */
708 void
709 monitor_store_register (regno)
710 int regno;
711 {
712 char *name;
713 int i;
714
715 i = read_register(regno);
716
717 debuglogs (1, "monitor_store_register (regno=%d)", regno);
718
719 if (regno < 0)
720 monitor_store_registers ();
721 else {
722 debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno), read_register (regno));
723
724 name = get_reg_name (regno);
725 if (STREQ(name, ""))
726 return;
727 printf_monitor (ROMCMD(SET_REG), name, read_register(regno));
728 expect (name, 1); /* strip the leading garbage */
729 if (*ROMDELIM(SET_REG) != 0) { /* if there's a delimiter */
730 expect (ROMDELIM(SET_REG), 1);
731 get_hex_word(1);
732 printf_monitor ("%d%s\n", i, CMD_END);
733 }
734 expect_prompt (1);
735 }
736 return;
737
738 #if 0
739 printf_monitor (SET_REG, get_reg_name (regno),
740 read_register (regno));
741 expect_prompt (1);
742 }
743 #endif
744 }
745
746 /* Get ready to modify the registers array. On machines which store
747 individual registers, this doesn't need to do anything. On machines
748 which store all the registers in one fell swoop, this makes sure
749 that registers contains all the registers from the program being
750 debugged. */
751
752 void
753 monitor_prepare_to_store ()
754 {
755 /* Do nothing, since we can store individual regs */
756 }
757
758 void
759 monitor_files_info ()
760 {
761 printf ("\tAttached to %s at %d baud.\n",
762 dev_name, baudrate);
763 }
764
765 /*
766 * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger
767 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
768 */
769 int
770 monitor_write_inferior_memory (memaddr, myaddr, len)
771 CORE_ADDR memaddr;
772 unsigned char *myaddr;
773 int len;
774 {
775 int i;
776 char buf[10];
777
778 debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
779
780 for (i = 0; i < len; i++) {
781 printf_monitor (ROMCMD(SET_MEM), memaddr + i, myaddr[i] );
782 if (*ROMDELIM(SET_MEM) != 0) { /* if there's a delimiter */
783 expect (ROMDELIM(SET_MEM), 1);
784 expect (CMD_DELIM);
785 printf_monitor ("%x", myaddr[i]);
786 }
787 /*** printf_monitor ("%x", myaddr[i]); ***/
788 if (sr_get_debug() > 1)
789 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
790 if (*ROMDELIM(SET_MEM) != 0) {
791 expect (CMD_DELIM);
792 printf_monitor (CMD_END);
793 }
794 expect_prompt (1);
795 }
796 return len;
797 }
798
799 /*
800 * monitor_read_inferior_memory -- read LEN bytes from inferior memory
801 * at MEMADDR. Put the result at debugger address MYADDR. Returns
802 * length moved.
803 */
804 int
805 monitor_read_inferior_memory(memaddr, myaddr, len)
806 CORE_ADDR memaddr;
807 char *myaddr;
808 int len;
809 {
810 int i, j;
811 char buf[20];
812
813 /* Number of bytes read so far. */
814 int count;
815
816 /* Starting address of this pass. */
817 unsigned long startaddr;
818
819 /* Number of bytes to read in this pass. */
820 int len_this_pass;
821
822 debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
823
824 /* Note that this code works correctly if startaddr is just less
825 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
826 thing). That is, something like
827 monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
828 works--it never adds len To memaddr and gets 0. */
829 /* However, something like
830 monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
831 doesn't need to work. Detect it and give up if there's an attempt
832 to do that. */
833 if (((memaddr - 1) + len) < memaddr) {
834 errno = EIO;
835 return 0;
836 }
837
838 startaddr = memaddr;
839 count = 0;
840 while (count < len) {
841 len_this_pass = 16;
842 if ((startaddr % 16) != 0)
843 len_this_pass -= startaddr % 16;
844 if (len_this_pass > (len - count))
845 len_this_pass = (len - count);
846
847 debuglogs (3, "Display %d bytes at %x", len_this_pass, startaddr);
848
849 for (i = 0; i < len_this_pass; i++) {
850 printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr);
851 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
852 if (*ROMDELIM(GET_MEM) != 0) { /* if there's a delimiter */
853 expect (ROMDELIM(GET_MEM), 1);
854 } else {
855 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
856 expect (buf,1); /* get the command echo */
857 get_hex_word(1); /* strip away the address */
858 }
859 get_hex_byte (&myaddr[count++]); /* get the value at this address */
860
861 if (*ROMDELIM(GET_MEM) != 0) {
862 printf_monitor (CMD_END);
863 }
864 expect_prompt (1);
865 startaddr += 1;
866 }
867 }
868 return len;
869 }
870
871 /* FIXME-someday! merge these two. */
872 int
873 monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target)
874 CORE_ADDR memaddr;
875 char *myaddr;
876 int len;
877 int write;
878 struct target_ops *target; /* ignored */
879 {
880 if (write)
881 return monitor_write_inferior_memory (memaddr, myaddr, len);
882 else
883 return monitor_read_inferior_memory (memaddr, myaddr, len);
884 }
885
886 void
887 monitor_kill (args, from_tty)
888 char *args;
889 int from_tty;
890 {
891 return; /* ignore attempts to kill target system */
892 }
893
894 /* Clean up when a program exits.
895 The program actually lives on in the remote processor's RAM, and may be
896 run again without a download. Don't leave it full of breakpoint
897 instructions. */
898
899 void
900 monitor_mourn_inferior ()
901 {
902 remove_breakpoints ();
903 generic_mourn_inferior (); /* Do all the proper things now */
904 }
905
906 #define MAX_MONITOR_BREAKPOINTS 16
907
908 extern int memory_breakpoint_size;
909 static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
910
911 /*
912 * monitor_insert_breakpoint -- add a breakpoint
913 */
914 int
915 monitor_insert_breakpoint (addr, shadow)
916 CORE_ADDR addr;
917 char *shadow;
918 {
919 int i;
920
921 debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr);
922
923 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++) {
924 if (breakaddr[i] == 0) {
925 breakaddr[i] = addr;
926 if (sr_get_debug() > 4)
927 printf ("Breakpoint at %x\n", addr);
928 monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size);
929 printf_monitor(SET_BREAK_CMD, addr);
930 expect_prompt(1);
931 return 0;
932 }
933 }
934
935 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
936 return 1;
937 }
938
939 /*
940 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
941 */
942 int
943 monitor_remove_breakpoint (addr, shadow)
944 CORE_ADDR addr;
945 char *shadow;
946 {
947 int i;
948
949 debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr);
950
951 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++) {
952 if (breakaddr[i] == addr) {
953 breakaddr[i] = 0;
954 /* some monitors remove breakpoints based on the address */
955 if (CLR_BREAK_ADDR)
956 printf_monitor(CLR_BREAK_CMD, addr);
957 else
958 printf_monitor(CLR_BREAK_CMD, i);
959 expect_prompt(1);
960 return 0;
961 }
962 }
963 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
964 return 1;
965 }
966
967 /* Load a file. This is usually an srecord, which is ascii. No
968 protocol, just sent line by line. */
969
970 #define DOWNLOAD_LINE_SIZE 100
971 void
972 monitor_load (arg)
973 char *arg;
974 {
975 FILE *download;
976 char buf[DOWNLOAD_LINE_SIZE];
977 int i, bytes_read;
978
979 if (sr_get_debug())
980 printf ("Loading %s to monitor\n", arg);
981
982 download = fopen (arg, "r");
983 if (download == NULL)
984 {
985 error (sprintf (buf, "%s Does not exist", arg));
986 return;
987 }
988
989 printf_monitor (LOAD_CMD);
990 /* expect ("Waiting for S-records from host... ", 1); */
991
992 while (!feof (download))
993 {
994 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
995 if (hashmark)
996 {
997 putchar ('.');
998 fflush (stdout);
999 }
1000
1001 if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
1002 fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
1003 break;
1004 }
1005 i = 0;
1006 while (i++ <=200000) {} ; /* Ugly HACK, probably needs flow control */
1007 if (bytes_read < DOWNLOAD_LINE_SIZE)
1008 {
1009 if (!feof (download))
1010 error ("Only read %d bytes\n", bytes_read);
1011 break;
1012 }
1013 }
1014
1015 if (hashmark)
1016 {
1017 putchar ('\n');
1018 }
1019 if (!feof (download))
1020 error ("Never got EOF while downloading");
1021 fclose (download);
1022 }
1023
1024 /*
1025 * monitor_command -- put a command string, in args, out to MONITOR.
1026 * Output from MONITOR is placed on the users terminal until the
1027 * prompt is seen. FIXME: We read the charcters ourseleves here
1028 * cause of a nasty echo.
1029 */
1030 void
1031 monitor_command (args, fromtty)
1032 char *args;
1033 int fromtty;
1034 {
1035
1036 char *p;
1037 char c, cp;
1038 p = PROMPT;
1039
1040 debuglogs (1, "monitor_command (args=%s)", args);
1041
1042 if (monitor_desc == NULL)
1043 error("monitor target not open.");
1044
1045 if (!args)
1046 error("Missing command.");
1047
1048 printf_monitor ("%s\n", args);
1049
1050 expect_prompt(0);
1051 }
1052
1053 /*
1054 * _initialize_remote_monitors -- setup a few addtitional commands that
1055 * are usually only used by monitors.
1056 */
1057 void
1058 _initialize_remote_monitors ()
1059 {
1060 struct cmd_list_element *c;
1061
1062 /* this sets the type of download protocol */
1063 c = add_set_cmd ("loadtype", no_class, var_string, (char *)&loadtype_str,
1064 "Set the type of the remote load protocol.\n", &setlist);
1065 c->function.sfunc = set_loadtype_command;
1066 add_show_from_set (c, &showlist);
1067 loadtype_str = savestring ("generic", 8);
1068
1069 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1070 (char *)&hashmark,
1071 "Set display of activity while downloading a file.\n\
1072 When enabled, a period \'.\' is displayed.",
1073 &setlist),
1074 &showlist);
1075
1076 /* generic monitor command */
1077 add_com ("monitor", class_obscure, monitor_command,
1078 "Send a command to the debug monitor.");
1079 }