* rom68k-rom.c: New file. Replaces the old remote-mon.c and uses
[deliverable/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 extern void make_xmodem_packet();
52 extern void print_xmodem_packet();
53
54 struct monitor_ops *current_monitor;
55 extern struct cmd_list_element *setlist;
56 extern struct cmd_list_element *unsetlist;
57 struct cmd_list_element *showlist;
58 extern char *version;
59 extern char *host_name;
60 extern char *target_name;
61
62 static int hashmark; /* flag set by "set hash" */
63
64 #define LOG_FILE "monitor.log"
65 #if defined (LOG_FILE)
66 FILE *log_file;
67 #endif
68
69 static int timeout = 24;
70
71 /*
72 * Descriptor for I/O to remote machine. Initialize it to NULL so that
73 * monitor_open knows that we don't have a file open when the program starts.
74 */
75 static serial_t monitor_desc = NULL;
76
77 /* sets the download protocol, choices are srec, generic, boot */
78 char *loadtype;
79 static char *loadtype_str;
80 static void set_loadtype_command();
81 static void monitor_load_srec();
82 static int monitor_write_srec();
83
84 /*
85 * these definitions are for xmodem protocol
86 */
87 #define SOH 0x01
88 #define ACK 0x06
89 #define NAK 0x15
90 #define EOT 0x04
91 #define CANCEL 0x18
92 #define GETACK getacknak(ACK)
93 #define GETNAK getacknak(NAK)
94 #define XMODEM_DATASIZE 128 /* the data size is ALWAYS 128 */
95 #define XMODEM_PACKETSIZE 131 /* the packet size is ALWAYS 132 (zero based) */
96 #define XMODEM 1
97
98 /*
99 * set_loadtype_command -- set the type for downloading. Check to make
100 * sure you have a support protocol for this target.
101 */
102 static void
103 set_loadtype_command (ignore, from_tty, c)
104 char *ignore;
105 int from_tty;
106 struct cmd_list_element *c;
107 {
108 char *tmp;
109 char *type;
110 if (STREQ (LOADTYPES, "")) {
111 error ("No loadtype set");
112 return;
113 }
114
115 tmp = savestring (LOADTYPES, strlen(LOADTYPES));
116 type = strtok(tmp, ",");
117 if (STREQ (type, (*(char **) c->var))) {
118 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
119 return;
120 }
121
122 while ((type = strtok (NULL, ",")) != (char *)NULL) {
123 if (STREQ (type, (*(char **) c->var)))
124 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
125 return;
126 }
127 free (tmp);
128 error ("Loadtype \"%s\" does not exist.", (*(char **) c->var));
129 }
130
131 /*
132 * printf_monitor -- send data to monitor. Works just like printf.
133 */
134 static void
135 printf_monitor(va_alist)
136 va_dcl
137 {
138 va_list args;
139 char *pattern;
140 char buf[200];
141 int i;
142
143 va_start(args);
144
145 pattern = va_arg(args, char *);
146
147 vsprintf(buf, pattern, args);
148
149 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);
150
151 if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
152 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
153 }
154 /*
155 * write_monitor -- send raw data to monitor.
156 */
157 static void
158 write_monitor(data, len)
159 char data[];
160 int len;
161 {
162 if (SERIAL_WRITE(monitor_desc, data, len))
163 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
164
165 *(data + len+1) = '\0';
166 debuglogs (1, "write_monitor(), Sending: \"%s\".", data);
167
168 }
169
170 /*
171 * debuglogs -- deal with debugging info to multiple sources. This takes
172 * two real args, the first one is the level to be compared against
173 * the sr_get_debug() value, the second arg is a printf buffer and args
174 * to be formatted and printed. A CR is added after each string is printed.
175 */
176 static void
177 debuglogs(va_alist)
178 va_dcl
179 {
180 va_list args;
181 char *pattern, *p;
182 char buf[200];
183 char newbuf[300];
184 int level, i;
185
186 va_start(args);
187
188 level = va_arg(args, int); /* get the debug level */
189 if ((level <0) || (level > 100)) {
190 error ("Bad argument passed to debuglogs(), needs debug level");
191 return;
192 }
193
194 pattern = va_arg(args, char *); /* get the printf style pattern */
195
196 vsprintf(buf, pattern, args); /* format the string */
197
198 /* convert some characters so it'll look right in the log */
199 p = newbuf;
200 for (i=0 ; buf[i] != '\0'; i++) {
201 switch (buf[i]) {
202 case '\n': /* newlines */
203 *p++ = '\\';
204 *p++ = 'n';
205 continue;
206 case '\r': /* carriage returns */
207 *p++ = '\\';
208 *p++ = 'r';
209 continue;
210 case '\033': /* escape */
211 *p++ = '\\';
212 *p++ = 'e';
213 continue;
214 case '\t': /* tab */
215 *p++ = '\\';
216 *p++ = 't';
217 continue;
218 case '\b': /* backspace */
219 *p++ = '\\';
220 *p++ = 'b';
221 continue;
222 default: /* no change */
223 *p++ = buf[i];
224 }
225
226 if (buf[i] < 26) { /* modify control characters */
227 *p++ = '^';
228 *p++ = buf[i] + 'A';
229 continue;
230 }
231 }
232 *p = '\0'; /* terminate the string */
233
234 if (sr_get_debug() > level)
235 puts (newbuf);
236
237 #ifdef LOG_FILE /* write to the monitor log */
238 if (log_file != 0x0) {
239 fputs (newbuf, log_file);
240 fputc ('\n', log_file);
241 fflush (log_file);
242 }
243 #endif
244 }
245
246 /* readchar -- read a character from the remote system, doing all the fancy
247 * timeout stuff.
248 */
249 static int
250 readchar(timeout)
251 int timeout;
252 {
253 int c;
254
255 c = SERIAL_READCHAR(monitor_desc, timeout);
256
257 if (sr_get_debug() > 5)
258 putchar(c & 0x7f);
259
260 #ifdef LOG_FILE
261 if (isascii (c))
262 putc(c & 0x7f, log_file);
263 #endif
264
265 if (c >= 0)
266 return c & 0x7f;
267
268 if (c == SERIAL_TIMEOUT) {
269 if (timeout == 0)
270 return c; /* Polls shouldn't generate timeout errors */
271 error("Timeout reading from remote system.");
272 #ifdef LOG_FILE
273 fputs ("ERROR: Timeout reading from remote system", log_file);
274 #endif
275 }
276 perror_with_name("remote-monitor");
277 }
278
279 /*
280 * expect -- scan input from the remote system, until STRING is found.
281 * If DISCARD is non-zero, then discard non-matching input, else print
282 * it out. Let the user break out immediately.
283 */
284 static void
285 expect (string, discard)
286 char *string;
287 int discard;
288 {
289 char *p = string;
290 int c;
291
292
293 debuglogs (1, "Expecting \"%s\".", string);
294
295 immediate_quit = 1;
296 while (1) {
297 c = readchar(timeout);
298 if (!isascii (c))
299 continue;
300 if (c == *p++) {
301 if (*p == '\0') {
302 immediate_quit = 0;
303 debuglogs (4, "Matched");
304 return;
305 }
306 } else {
307 if (!discard) {
308 fwrite(string, 1, (p - 1) - string, stdout);
309 putchar((char)c);
310 fflush(stdout);
311 }
312 p = string;
313 }
314 }
315 }
316
317 /* Keep discarding input until we see the MONITOR prompt.
318
319 The convention for dealing with the prompt is that you
320 o give your command
321 o *then* wait for the prompt.
322
323 Thus the last thing that a procedure does with the serial line
324 will be an expect_prompt(). Exception: monitor_resume does not
325 wait for the prompt, because the terminal is being handed over
326 to the inferior. However, the next thing which happens after that
327 is a monitor_wait which does wait for the prompt.
328 Note that this includes abnormal exit, e.g. error(). This is
329 necessary to prevent getting into states from which we can't
330 recover. */
331 static void
332 expect_prompt(discard)
333 int discard;
334 {
335 expect (PROMPT, discard);
336 }
337
338 /*
339 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
340 */
341 static int
342 junk(ch)
343 char ch;
344 {
345 switch (ch) {
346 case '\0':
347 case ' ':
348 case '-':
349 case '\t':
350 case '\r':
351 case '\n':
352 if (sr_get_debug() > 5)
353 debuglogs (5, "Ignoring \'%c\'.", ch);
354 return 1;
355 default:
356 if (sr_get_debug() > 5)
357 debuglogs (5, "Accepting \'%c\'.", ch);
358 return 0;
359 }
360 }
361
362 /*
363 * get_hex_digit -- Get a hex digit from the remote system & return its value.
364 * If ignore is nonzero, ignore spaces, newline & tabs.
365 */
366 static int
367 get_hex_digit(ignore)
368 int ignore;
369 {
370 static int ch;
371 while (1) {
372 ch = readchar(timeout);
373 if (junk(ch))
374 continue;
375 if (sr_get_debug() > 4)
376 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
377
378 if (ch >= '0' && ch <= '9')
379 return ch - '0';
380 else if (ch >= 'A' && ch <= 'F')
381 return ch - 'A' + 10;
382 else if (ch >= 'a' && ch <= 'f')
383 return ch - 'a' + 10;
384 else if (ch == ' ' && ignore)
385 ;
386 else {
387 expect_prompt(1);
388 error("Invalid hex digit from remote system. (0x%x)", ch);
389 }
390 }
391 }
392
393 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
394 * Accept any number leading spaces.
395 */
396 static void
397 get_hex_byte (byt)
398 char *byt;
399 {
400 int val;
401
402 val = get_hex_digit (1) << 4;
403 debuglogs (4, "get_hex_digit() -- Read first nibble 0x%x", val);
404
405 val |= get_hex_digit (0);
406 debuglogs (4, "get_hex_digit() -- Read second nibble 0x%x", val);
407 *byt = val;
408
409 debuglogs (4, "get_hex_digit() -- Read a 0x%x", val);
410 }
411
412 /*
413 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
414 * and put them in registers starting at REGNO.
415 */
416 static int
417 get_hex_word ()
418 {
419 long val;
420 int i;
421
422 val = 0;
423 for (i = 0; i < 8; i++)
424 val = (val << 4) + get_hex_digit (i == 0);
425
426 debuglogs (4, "get_hex_word() got a 0x%x.", val);
427
428 return val;
429 }
430
431 /* This is called not only when we first attach, but also when the
432 user types "run" after having attached. */
433 void
434 monitor_create_inferior (execfile, args, env)
435 char *execfile;
436 char *args;
437 char **env;
438 {
439 int entry_pt;
440
441 if (args && *args)
442 error("Can't pass arguments to remote MONITOR process");
443
444 if (execfile == 0 || exec_bfd == 0)
445 error("No exec file specified");
446
447 entry_pt = (int) bfd_get_start_address (exec_bfd);
448
449 debuglogs (1, "create_inferior(exexfile=%s, args=%s, env=%s)", execfile, args, env);
450
451 /* The "process" (board) is already stopped awaiting our commands, and
452 the program is already downloaded. We just set its PC and go. */
453
454 clear_proceed_status ();
455
456 /* Tell wait_for_inferior that we've started a new process. */
457 init_wait_for_inferior ();
458
459 /* Set up the "saved terminal modes" of the inferior
460 based on what modes we are starting it with. */
461 target_terminal_init ();
462
463 /* Install inferior's terminal modes. */
464 target_terminal_inferior ();
465
466 /* insert_step_breakpoint (); FIXME, do we need this? */
467
468 /* Let 'er rip... */
469 proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
470 }
471
472 /*
473 * monitor_open -- open a connection to a remote debugger.
474 * NAME is the filename used for communication.
475 */
476 static int baudrate = 9600;
477 static char dev_name[100];
478
479 void
480 monitor_open(args, name, from_tty)
481 char *args;
482 char *name;
483 int from_tty;
484 {
485
486 if (args == NULL)
487 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
488 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
489
490 /* if (is_open) */
491 monitor_close(0);
492
493 strcpy(dev_name, args);
494 monitor_desc = SERIAL_OPEN(dev_name);
495
496 if (monitor_desc == NULL)
497 perror_with_name(dev_name);
498
499 if (baud_rate != -1) {
500 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) {
501 SERIAL_CLOSE (monitor_desc);
502 perror_with_name (name);
503 }
504 }
505
506 SERIAL_RAW(monitor_desc);
507
508 #if defined (LOG_FILE)
509 log_file = fopen (LOG_FILE, "w");
510 if (log_file == NULL)
511 perror_with_name (LOG_FILE);
512 fprintf_filtered (log_file, "GDB %s (%s", version, host_name);
513 fprintf_filtered (log_file, " --target %s)\n", target_name);
514 fprintf_filtered (log_file, "Remote target %s connected to %s\n\n", TARGET_NAME, dev_name);
515 #endif
516
517 /* wake up the monitor and see if it's alive */
518 printf_monitor(INIT_CMD);
519 expect_prompt(1); /* See if we get a prompt */
520
521 /* try again to be sure */
522 printf_monitor(INIT_CMD);
523 expect_prompt(1); /* See if we get a prompt */
524
525 if (from_tty)
526 printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name);
527 }
528
529 /*
530 * monitor_close -- Close out all files and local state before this
531 * target loses control.
532 */
533
534 void
535 monitor_close (quitting)
536 int quitting;
537 {
538 SERIAL_CLOSE(monitor_desc);
539 monitor_desc = NULL;
540
541 debuglogs (1, "monitor_close (quitting=%d)", quitting);
542
543 #if defined (LOG_FILE)
544 if (log_file) {
545 if (ferror(log_file))
546 fprintf(stderr, "Error writing log file.\n");
547 if (fclose(log_file) != 0)
548 fprintf(stderr, "Error closing log file.\n");
549 }
550 #endif
551 }
552
553 /*
554 * monitor_detach -- terminate the open connection to the remote
555 * debugger. Use this when you want to detach and do something
556 * else with your gdb.
557 */
558 void
559 monitor_detach (from_tty)
560 int from_tty;
561 {
562
563 debuglogs (1, "monitor_detach ()");
564
565 pop_target(); /* calls monitor_close to do the real work */
566 if (from_tty)
567 printf ("Ending remote %s debugging\n", target_shortname);
568 }
569
570 /*
571 * monitor_attach -- attach GDB to the target.
572 */
573 void
574 monitor_attach (args, from_tty)
575 char *args;
576 int from_tty;
577 {
578 if (from_tty)
579 printf ("Starting remote %s debugging\n", target_shortname);
580
581 debuglogs (1, "monitor_attach (args=%s)", args);
582
583 printf_monitor (GO_CMD);
584 /* swallow the echo. */
585 expect (GO_CMD, 1);
586 }
587
588 /*
589 * monitor_resume -- Tell the remote machine to resume.
590 */
591 void
592 monitor_resume (pid, step, sig)
593 int pid, step;
594 enum target_signal sig;
595 {
596 debuglogs (1, "monitor_resume (step=%d, sig=%d)", step, sig);
597
598 if (step) {
599 printf_monitor (STEP_CMD);
600 } else {
601 printf_monitor (CONT_CMD);
602 }
603 }
604
605 /*
606 * monitor_wait -- Wait until the remote machine stops, then return,
607 * storing status in status just as `wait' would.
608 */
609 int
610 monitor_wait (pid, status)
611 int pid;
612 struct target_waitstatus *status;
613 {
614 int old_timeout = timeout;
615
616 debuglogs(1, "monitor_wait (), printing extraneous text.");
617
618 status->kind = TARGET_WAITKIND_EXITED;
619 status->value.integer = 0;
620
621 timeout = 0; /* Don't time out -- user program is running. */
622
623 expect_prompt(0); /* Wait for prompt, outputting extraneous text */
624 debuglogs (4, "monitor_wait(), got the prompt.");
625
626 status->kind = TARGET_WAITKIND_STOPPED;
627 status->value.sig = TARGET_SIGNAL_TRAP;
628
629 timeout = old_timeout;
630
631 return 0;
632 }
633
634 /* Return the name of register number regno in the form input and output by
635 monitor. Currently, register_names just happens to contain exactly what
636 monitor wants. Lets take advantage of that just as long as possible! */
637
638 static char *
639 get_reg_name (regno)
640 int regno;
641 {
642 static char buf[50];
643 const char *p;
644 char *b;
645
646 b = buf;
647
648 if (regno < 0)
649 return ("");
650
651 for (p = REGNAMES(regno); *p; p++)
652 *b++ = tolower(*p);
653
654 *b = '\000';
655
656 debuglogs (5, "Got name \"%s\" from regno #%d.", buf, regno);
657
658 return buf;
659 }
660
661 /*
662 * monitor_fetch_registers -- read the remote registers into the
663 * block regs.
664 */
665 void
666 monitor_fetch_registers ()
667 {
668 int regno;
669
670 /* yeah yeah, i know this is horribly inefficient. but it isn't done
671 very often... i'll clean it up later. */
672
673 for (regno = 0; regno <= PC_REGNUM; regno++)
674 monitor_fetch_register(regno);
675 }
676
677 /*
678 * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO
679 * is -1. Returns errno value.
680 */
681 void
682 monitor_fetch_register (regno)
683 int regno;
684 {
685 int val, j;
686
687 debuglogs (1, "monitor_fetch_register (reg=%s)", get_reg_name (regno));
688
689 if (regno < 0) {
690 monitor_fetch_registers ();
691 } else {
692 char *name = get_reg_name (regno);
693 if (STREQ(name, ""))
694 return;
695 printf_monitor (ROMCMD(GET_REG), name); /* send the command */
696 expect (name, 1); /* then strip the leading garbage */
697 if (*ROMDELIM(GET_REG) != 0) { /* if there's a delimiter */
698 expect (ROMDELIM(GET_REG), 1);
699 }
700
701 val = get_hex_word(); /* get the value, ignore junk */
702 supply_register (regno, (char *) &val);
703
704 if (*ROMDELIM(GET_REG) != 0) {
705 /*** expect (ROMRES(GET_REG)); ***/
706 printf_monitor (CMD_END);
707 }
708 expect_prompt (1);
709 }
710 return;
711 }
712
713 /* Store the remote registers from the contents of the block REGS. */
714
715 void
716 monitor_store_registers ()
717 {
718 int regno;
719
720 debuglogs (1, "monitor_store_registers()");
721
722 for (regno = 0; regno <= PC_REGNUM; regno++)
723 monitor_store_register(regno);
724
725 registers_changed ();
726 }
727
728 /*
729 * monitor_store_register -- store register REGNO, or all if REGNO == 0.
730 * return errno value.
731 */
732 void
733 monitor_store_register (regno)
734 int regno;
735 {
736 char *name;
737 int i;
738
739 i = read_register(regno);
740
741 debuglogs (1, "monitor_store_register (regno=%d)", regno);
742
743 if (regno < 0)
744 monitor_store_registers ();
745 else {
746 debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno), read_register (regno));
747
748 name = get_reg_name (regno);
749 if (STREQ(name, ""))
750 return;
751 printf_monitor (ROMCMD(SET_REG), name, read_register(regno));
752 expect (name, 1); /* strip the leading garbage */
753 if (*ROMDELIM(SET_REG) != 0) { /* if there's a delimiter */
754 expect (ROMDELIM(SET_REG), 1);
755 get_hex_word(1);
756 printf_monitor ("%d%s\n", i, CMD_END);
757 }
758 expect_prompt (1);
759 }
760 return;
761
762 #if 0
763 printf_monitor (SET_REG, get_reg_name (regno),
764 read_register (regno));
765 expect_prompt (1);
766 }
767 #endif
768 }
769
770 /* Get ready to modify the registers array. On machines which store
771 individual registers, this doesn't need to do anything. On machines
772 which store all the registers in one fell swoop, this makes sure
773 that registers contains all the registers from the program being
774 debugged. */
775
776 void
777 monitor_prepare_to_store ()
778 {
779 /* Do nothing, since we can store individual regs */
780 }
781
782 void
783 monitor_files_info ()
784 {
785 printf ("\tAttached to %s at %d baud.\n",
786 dev_name, baudrate);
787 }
788
789 /*
790 * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger
791 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
792 */
793 int
794 monitor_write_inferior_memory (memaddr, myaddr, len)
795 CORE_ADDR memaddr;
796 unsigned char *myaddr;
797 int len;
798 {
799 int i;
800 char buf[10];
801
802 debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
803
804 for (i = 0; i < len; i++) {
805 printf_monitor (ROMCMD(SET_MEM), memaddr + i, myaddr[i] );
806 if (*ROMDELIM(SET_MEM) != 0) { /* if there's a delimiter */
807 expect (ROMDELIM(SET_MEM), 1);
808 expect (CMD_DELIM);
809 printf_monitor ("%x", myaddr[i]);
810 }
811 /*** printf_monitor ("%x", myaddr[i]); ***/
812 if (sr_get_debug() > 1)
813 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
814 if (*ROMDELIM(SET_MEM) != 0) {
815 expect (CMD_DELIM);
816 printf_monitor (CMD_END);
817 }
818 expect_prompt (1);
819 }
820 return len;
821 }
822
823 /*
824 * monitor_read_inferior_memory -- read LEN bytes from inferior memory
825 * at MEMADDR. Put the result at debugger address MYADDR. Returns
826 * length moved.
827 */
828 int
829 monitor_read_inferior_memory(memaddr, myaddr, len)
830 CORE_ADDR memaddr;
831 char *myaddr;
832 int len;
833 {
834 int i, j;
835 char buf[20];
836
837 /* Number of bytes read so far. */
838 int count;
839
840 /* Starting address of this pass. */
841 unsigned long startaddr;
842
843 /* Number of bytes to read in this pass. */
844 int len_this_pass;
845
846 debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
847
848 /* Note that this code works correctly if startaddr is just less
849 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
850 thing). That is, something like
851 monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
852 works--it never adds len To memaddr and gets 0. */
853 /* However, something like
854 monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
855 doesn't need to work. Detect it and give up if there's an attempt
856 to do that. */
857 if (((memaddr - 1) + len) < memaddr) {
858 errno = EIO;
859 return 0;
860 }
861
862 startaddr = memaddr;
863 count = 0;
864 while (count < len) {
865 len_this_pass = 16;
866 if ((startaddr % 16) != 0)
867 len_this_pass -= startaddr % 16;
868 if (len_this_pass > (len - count))
869 len_this_pass = (len - count);
870
871 debuglogs (3, "Display %d bytes at %x", len_this_pass, startaddr);
872
873 for (i = 0; i < len_this_pass; i++) {
874 printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr);
875 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
876 if (*ROMDELIM(GET_MEM) != 0) { /* if there's a delimiter */
877 expect (ROMDELIM(GET_MEM), 1);
878 } else {
879 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
880 expect (buf,1); /* get the command echo */
881 get_hex_word(1); /* strip away the address */
882 }
883 get_hex_byte (&myaddr[count++]); /* get the value at this address */
884
885 if (*ROMDELIM(GET_MEM) != 0) {
886 printf_monitor (CMD_END);
887 }
888 expect_prompt (1);
889 startaddr += 1;
890 }
891 }
892 return len;
893 }
894
895 /* FIXME-someday! merge these two. */
896 int
897 monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target)
898 CORE_ADDR memaddr;
899 char *myaddr;
900 int len;
901 int write;
902 struct target_ops *target; /* ignored */
903 {
904 if (write)
905 return monitor_write_inferior_memory (memaddr, myaddr, len);
906 else
907 return monitor_read_inferior_memory (memaddr, myaddr, len);
908 }
909
910 void
911 monitor_kill (args, from_tty)
912 char *args;
913 int from_tty;
914 {
915 return; /* ignore attempts to kill target system */
916 }
917
918 /* Clean up when a program exits.
919 The program actually lives on in the remote processor's RAM, and may be
920 run again without a download. Don't leave it full of breakpoint
921 instructions. */
922
923 void
924 monitor_mourn_inferior ()
925 {
926 remove_breakpoints ();
927 generic_mourn_inferior (); /* Do all the proper things now */
928 }
929
930 #define MAX_MONITOR_BREAKPOINTS 16
931
932 extern int memory_breakpoint_size;
933 static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
934
935 /*
936 * monitor_insert_breakpoint -- add a breakpoint
937 */
938 int
939 monitor_insert_breakpoint (addr, shadow)
940 CORE_ADDR addr;
941 char *shadow;
942 {
943 int i;
944
945 debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr);
946
947 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++) {
948 if (breakaddr[i] == 0) {
949 breakaddr[i] = addr;
950 if (sr_get_debug() > 4)
951 printf ("Breakpoint at %x\n", addr);
952 monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size);
953 printf_monitor(SET_BREAK_CMD, addr);
954 expect_prompt(1);
955 return 0;
956 }
957 }
958
959 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
960 return 1;
961 }
962
963 /*
964 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
965 */
966 int
967 monitor_remove_breakpoint (addr, shadow)
968 CORE_ADDR addr;
969 char *shadow;
970 {
971 int i;
972
973 debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr);
974
975 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++) {
976 if (breakaddr[i] == addr) {
977 breakaddr[i] = 0;
978 /* some monitors remove breakpoints based on the address */
979 if (CLR_BREAK_ADDR)
980 printf_monitor(CLR_BREAK_CMD, addr);
981 else
982 printf_monitor(CLR_BREAK_CMD, i);
983 expect_prompt(1);
984 return 0;
985 }
986 }
987 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
988 return 1;
989 }
990
991 /* monitor_load -- load a file. This file determines which of the
992 * supported formats to use. The current types are:
993 * FIXME: not all types supported yet.
994 * default - reads any file using bfd and writes it to memory. This
995 * is really slow.
996 * srec - reads binary file using bfd and writes it as an
997 * ascii srecord.
998 * xmodem-bin - reads a binary file using bfd, and downloads it
999 * using xmodem protocol.
1000 * xmodem-srec - reads a binary file using bfd, and after converting
1001 * it downloads it as an srecord using xmodem protocol.
1002 * ascii-srec - reads a ascii srecord file and downloads it
1003 * without a change.
1004 * ascii-xmodem - reads a ascii file and downloads using xmodem
1005 * protocol.
1006 */
1007 void
1008 monitor_load (file, fromtty)
1009 char *file;
1010 int fromtty;
1011 {
1012 FILE *download;
1013 int i, bytes_read;
1014
1015 debuglogs (1, "Loading %s to monitor", file);
1016
1017 if (STREQ (loadtype_str, "default")) { /* default, load a binary */
1018 gr_load_image (file, fromtty); /* by writing it into memory */
1019 }
1020
1021 if (STREQ (loadtype_str, "srec")) { /* load an srecord by converting */
1022 monitor_load_srec(file, 0); /* if from a binary */
1023 }
1024
1025 if (STREQ (loadtype_str, "ascii-srec")) { /* load an srecord file */
1026 monitor_load_ascii_srec(file, fromtty); /* if from a binary */
1027 }
1028
1029 if (STREQ (loadtype_str, "xmodem-srec")) { /* load an srecord using the */
1030 monitor_load_srec(file, XMODEM);
1031 }
1032 }
1033
1034 /*
1035 * monitor_load_ascii_srec -- download an ASCII srecord file.
1036 */
1037 #define DOWNLOAD_LINE_SIZE 100
1038 int
1039 monitor_load_ascii_srec (file, fromtty)
1040 char *file;
1041 int fromtty;
1042 {
1043 FILE *download;
1044 char buf[DOWNLOAD_LINE_SIZE];
1045 int i, bytes_read;
1046
1047 debuglogs (1, "Loading an ASCII srecord file, %s.", file);
1048
1049 download = fopen (file, "r");
1050 if (download == NULL) {
1051 error ("%s Does not exist", file);
1052 return;
1053 }
1054
1055 printf_monitor (LOAD_CMD);
1056 sleep(1);
1057 while (!feof (download)) {
1058 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
1059 if (hashmark) {
1060 putchar ('.');
1061 fflush (stdout);
1062 }
1063 if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
1064 fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
1065 break;
1066 }
1067 i = 0;
1068 while (i++ <=200) {} ; /* Ugly HACK, probably needs flow control */
1069 if (bytes_read < DOWNLOAD_LINE_SIZE) {
1070 if (!feof (download))
1071 error ("Only read %d bytes\n", bytes_read);
1072 break;
1073 }
1074 }
1075
1076 if (hashmark) {
1077 putchar ('\n');
1078 }
1079 if (!feof (download))
1080 error ("Never got EOF while downloading");
1081 expect_prompt(1);
1082 fclose (download);
1083 }
1084
1085 /*
1086 * monitor_command -- put a command string, in args, out to MONITOR.
1087 * Output from MONITOR is placed on the users terminal until the
1088 * prompt is seen. FIXME: We read the charcters ourseleves here
1089 * cause of a nasty echo.
1090 */
1091 void
1092 monitor_command (args, fromtty)
1093 char *args;
1094 int fromtty;
1095 {
1096
1097 char *p;
1098 char c, cp;
1099 p = PROMPT;
1100
1101 debuglogs (1, "monitor_command (args=%s)", args);
1102
1103 if (monitor_desc == NULL)
1104 error("monitor target not open.");
1105
1106 if (!args)
1107 error("Missing command.");
1108
1109 printf_monitor ("%s\n", args);
1110
1111 expect_prompt(0);
1112 }
1113
1114 /*
1115 * monitor_load_srec -- download a binary file by converting it to srecords. This
1116 * will also use xmodem to download the resulting file.
1117 *
1118 * A download goes like this when using xmodem:
1119 * Receiver: Sender
1120 * NAK ---------->
1121 * <-------- (packet) [SOH|1|1|data|SUM]
1122 * ACK ---------->
1123 * <-------- (packet) [SOH|2|2|data|SUM]
1124 * ACK ---------->
1125 * <-------- EOT
1126 * ACK ---------->
1127 *
1128 * ACK = 0x06
1129 * NAK = 0x15
1130 * EOT = 0x04
1131 *
1132 */
1133 static void
1134 monitor_load_srec (args, protocol)
1135 char *args;
1136 int protocol;
1137 {
1138 bfd *abfd;
1139 asection *s;
1140 char buffer[1024];
1141 char srec[1024];
1142 char packet[XMODEM_PACKETSIZE];
1143 int i;
1144 int retries;
1145 int type = 0; /* default to a type 0, header record */
1146 int srec_frame = 57; /* FIXME: this must be 57 There is 12 bytes
1147 of header, and 2 bytes of checksum at the end.
1148 The problem is an xmodem packet holds exactly
1149 128 bytes. */
1150
1151 abfd = bfd_openr (args, 0);
1152 if (!abfd) {
1153 printf_filtered ("Unable to open file %s\n", args);
1154 return;
1155 }
1156
1157 if (bfd_check_format (abfd, bfd_object) == 0) {
1158 printf_filtered ("File is not an object file\n");
1159 return;
1160 }
1161
1162 printf_monitor (LOAD_CMD); /* tell the monitor to load */
1163 if (protocol == XMODEM) { /* get the NAK from the target */
1164 if (GETNAK) {
1165 debuglogs (3, "Got the NAK to start loading");
1166 } else {
1167 printf_monitor ("%c", EOT);
1168 debuglogs (3, "Never got the NAK to start loading");
1169 error ("Never got the NAK to start loading");
1170 }
1171 }
1172
1173 s = abfd->sections;
1174 while (s != (asection *) NULL) {
1175 if (s->flags & SEC_LOAD) {
1176 char *buffer = xmalloc (srec_frame);
1177 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size);
1178 fflush (stdout);
1179 for (i = 0; i < s->_raw_size; i += srec_frame) {
1180 if (srec_frame > s->_raw_size - i)
1181 srec_frame = s->_raw_size - i;
1182
1183 bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
1184 monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame);
1185 if (protocol == XMODEM) { /* send a packet using xmodem */
1186 make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
1187 write_monitor (packet, XMODEM_PACKETSIZE+1);
1188 retries = 0;
1189 while (retries++ <= 3) {
1190 if (GETNAK) { /* Resend packet */
1191 debuglogs (3, "Got a NAK, resending packet");
1192 sleep(1);
1193 write_monitor (packet, XMODEM_PACKETSIZE+1); /* send it again */
1194 if (GETACK) /* ACKnowledged, get next data chunk */
1195 break;
1196 }
1197 }
1198 if (retries >= 4) { /* too many tries, must be hosed */
1199 printf_monitor ("%c", EOT);
1200 error ("Never got a ACK after sending an xmodem packet");
1201 }
1202 } else { /* no protocols at all */
1203 printf_monitor ("%s\n", srec);
1204 }
1205 if (hashmark)
1206 printf_filtered ("#");
1207 type = 3; /* switch to a 4 byte address record */
1208 fflush (stdout);
1209 }
1210 printf_filtered ("\n");
1211 free (buffer);
1212 } else {
1213 debuglogs (3, "%s doesn't need to be loaded", s->name);
1214 }
1215 s = s->next;
1216 }
1217
1218 /*
1219 write a type 7 terminator record. no data for a type 7,
1220 and there is no data, so len is 0.
1221 */
1222 monitor_make_srec (srec, 7, abfd->start_address, "", 0);
1223 printf_monitor ("%s\n", srec);
1224 if (protocol == XMODEM) {
1225 printf_monitor ("%c", EOT);
1226 if (!GETACK)
1227 error ("Never got ACK after sending EOT");
1228 }
1229
1230 if (hashmark)
1231 putchar ('\n');
1232
1233 expect_prompt ();
1234 }
1235
1236 /*
1237 * getacknak -- get an ACK or a NAK from the target.
1238 * returns 1 (true) or 0 (false) This is
1239 * for xmodem. ANy string starting with "***"
1240 * is an error message from the target.
1241 * Here's a few from the WinBond w89k "Cougar" PA board.
1242 * *** Too many errors found.
1243 * *** Bad command
1244 * *** Command syntax error
1245 */
1246 int
1247 getacknak (byte)
1248 int byte;
1249 {
1250 char character;
1251 int i;
1252
1253 i = 0;
1254 while (i++ < 60) {
1255 character = (char)readchar (0);
1256 if (character == 0xfffffffe) { /* empty uart */
1257 if (sr_get_debug() > 3)
1258 putchar ('.');
1259 fflush (stdout);
1260 sleep (1);
1261 continue;
1262 }
1263 if (character == CANCEL) { /* target aborted load */
1264 expect_prompt (0);
1265 error ("Got a CANCEL from the target.");
1266 }
1267 if (character == '*') { /* look for missed error message */
1268 expect_prompt (0);
1269 error ("Got an error message from the target");
1270 }
1271 debuglogs (3, "Got a %s (0x%x or \'%c\'), expecting a %s.\n",
1272 (character == ACK) ? "ACK" : (character == NAK) ? "NAK" : "BOGUS",
1273 character, character, (byte == ACK) ? "ACK" : "NAK");
1274 if (character == byte) /* got what we wanted */
1275 return 1;
1276 if (character == ((byte == ACK) ? NAK : ACK)) { /* got the opposite */
1277 debuglogs (3, "Got the opposite, wanted 0x%x, got a 0x%x", byte, character);
1278 return 0;
1279 }
1280 sleep (1);
1281 }
1282 return 0;
1283 }
1284
1285 /*
1286 * monitor_make_srec -- make an srecord. This writes each line, one at a
1287 * time, each with it's own header and trailer line.
1288 * An srecord looks like this:
1289 *
1290 * byte count-+ address
1291 * start ---+ | | data +- checksum
1292 * | | | |
1293 * S01000006F6B692D746573742E73726563E4
1294 * S315000448600000000000000000FC00005900000000E9
1295 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1296 * S30B0004485A0000000000004E
1297 * S70500040000F6
1298 *
1299 * S<type><length><address><data><checksum>
1300 *
1301 * Where
1302 * - length
1303 * is the number of bytes following upto the checksum. Note that
1304 * this is not the number of chars following, since it takes two
1305 * chars to represent a byte.
1306 * - type
1307 * is one of:
1308 * 0) header record
1309 * 1) two byte address data record
1310 * 2) three byte address data record
1311 * 3) four byte address data record
1312 * 7) four byte address termination record
1313 * 8) three byte address termination record
1314 * 9) two byte address termination record
1315 *
1316 * - address
1317 * is the start address of the data following, or in the case of
1318 * a termination record, the start address of the image
1319 * - data
1320 * is the data.
1321 * - checksum
1322 * is the sum of all the raw byte data in the record, from the length
1323 * upwards, modulo 256 and subtracted from 255.
1324 */
1325 int
1326 monitor_make_srec (buffer, type, memaddr, myaddr, len)
1327 char *buffer;
1328 int type;
1329 CORE_ADDR memaddr;
1330 unsigned char *myaddr;
1331 int len;
1332 {
1333 int checksum;
1334 int i;
1335 char *buf;
1336
1337 buf = buffer;
1338 debuglogs (4, "monitor_make_srec (buffer=0x%x, type=%d, memaddr=0x%x, len=%d",
1339 buffer, type, memaddr, len);
1340 checksum = 0;
1341
1342 /*
1343 create the header for the srec. 4 is the number of bytes in the address,
1344 and 1 is the number of bytes in the count.
1345 */
1346 if (type == 0) /* FIXME: type 0 is optional */
1347 type = 3; /* so use data as it works */
1348 sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr);
1349 buf += 12;
1350
1351 checksum += (len + 4 + 1 /* calculate the checksum */
1352 + (memaddr & 0xff)
1353 + ((memaddr >> 8) & 0xff)
1354 + ((memaddr >> 16) & 0xff)
1355 + ((memaddr >> 24) & 0xff));
1356
1357 for (i = 0; i < len; i++) { /* build the srecord */
1358 sprintf (buf, "%02X", myaddr[i]);
1359 checksum += myaddr[i];
1360 buf += 2;
1361 }
1362
1363 sprintf(buf, "%02X", ~checksum & 0xff); /* add the checksum */
1364 debuglogs (3, "srec is \"%s\"", buffer);
1365
1366 return(0);
1367 }
1368
1369 /*
1370 * make_xmodem_packet -- this takes a 128 bytes of data and makes a packet
1371 * out of it.
1372 *
1373 * Each packet looks like this:
1374 * +-----+-------+-------+------+-----+
1375 * | SOH | Seq1. | Seq2. | data | SUM |
1376 * +-----+-------+-------+------+-----+
1377 * SOH = 0x01
1378 * Seq1 = The sequence number.
1379 * Seq2 = The complement of the sequence number.
1380 * Data = A 128 bytes of data.
1381 * SUM = Add the contents of the 128 bytes and use the low-order
1382 * 8 bits of the result.
1383 */
1384 void
1385 make_xmodem_packet (packet, data, len)
1386 unsigned char packet[];
1387 unsigned char *data;
1388 int len;
1389 {
1390 static int sequence = 1;
1391 int i, sum;
1392 unsigned char *buf;
1393
1394 buf = data;
1395 /* build the packet header */
1396 packet[0] = SOH;
1397 packet[1] = sequence;
1398 packet[2] = 255 - sequence;
1399 sequence++;
1400 #if 0
1401 packet[2] = ~sequence++; /* the complement is the sequence checksum */
1402 #endif
1403
1404 sum = 0; /* calculate the data checksum */
1405 for (i = 3; i <= len + 2; i++) {
1406 packet[i] = *buf;
1407 sum += *buf;
1408 buf++;
1409 }
1410
1411 for (i = len+1 ; i <= XMODEM_DATASIZE ; i++) { /* add padding for the rest of the packet */
1412 packet[i] = '0';
1413 }
1414
1415 packet[XMODEM_PACKETSIZE] = sum & 0xff; /* add the checksum */
1416
1417 if (sr_get_debug() > 4)
1418 debuglogs (4, "The xmodem checksum is %d (0x%x)\n", sum & 0xff, sum & 0xff);
1419 print_xmodem_packet (packet);
1420 }
1421
1422 /*
1423 * print_xmodem_packet -- print the packet as a debug check
1424 */
1425 void
1426 print_xmodem_packet(packet)
1427 char packet[];
1428 {
1429 int i;
1430 static int lastseq;
1431 int sum;
1432
1433 /* take apart the packet header the packet header */
1434 if (packet[0] == SOH) {
1435 ("SOH");
1436 } else {
1437 error ("xmodem: SOH is wrong");
1438 }
1439
1440 /* check the sequence */
1441 if (packet[1] != 0) {
1442 lastseq = packet[1];
1443 if (packet[2] != ~lastseq)
1444 error ("xmodem: Sequence checksum is wrong");
1445 else
1446 printf_filtered (" %d %d", lastseq, ~lastseq);
1447 }
1448
1449 /* check the data checksum */
1450 sum = 0;
1451 for (i = 3; i <= XMODEM_DATASIZE; i++) {
1452 sum += packet[i];
1453 }
1454
1455 /* ignore the data */
1456 #if 0
1457 printf (" [128 bytes of data] %d\n", sum & 0xff);
1458 #endif
1459 printf_filtered (" [%s] %d\n", packet, sum & 0xff);
1460
1461 if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff)) {
1462 debuglogs (4, "xmodem: data checksum wrong, got a %d", packet[XMODEM_PACKETSIZE] & 0xff);
1463 }
1464 putchar ('\n');
1465 }
1466
1467 /*
1468 * _initialize_remote_monitors -- setup a few addtitional commands that
1469 * are usually only used by monitors.
1470 */
1471 void
1472 _initialize_remote_monitors ()
1473 {
1474 struct cmd_list_element *c;
1475
1476 /* this sets the type of download protocol */
1477 c = add_set_cmd ("loadtype", no_class, var_string, (char *)&loadtype_str,
1478 "Set the type of the remote load protocol.\n", &setlist);
1479 c->function.sfunc = set_loadtype_command;
1480 add_show_from_set (c, &showlist);
1481 loadtype_str = savestring ("default", 8);
1482
1483 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1484 (char *)&hashmark,
1485 "Set display of activity while downloading a file.\n\
1486 When enabled, a period \'.\' is displayed.",
1487 &setlist),
1488 &showlist);
1489
1490 /* generic monitor command */
1491 add_com ("monitor", class_obscure, monitor_command,
1492 "Send a command to the debug monitor.");
1493 }
This page took 0.060697 seconds and 5 git commands to generate.