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