]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/monitor.c
* monitor.c (monitor_command): Don't use PROMPT until monitor
[thirdparty/binutils-gdb.git] / gdb / monitor.c
1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
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 #include "defs.h"
32 #include "gdbcore.h"
33 #include "target.h"
34 #include "wait.h"
35 #include <varargs.h>
36 #include <signal.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include "command.h"
40 #include "serial.h"
41 #include "monitor.h"
42 #include "gdbcmd.h"
43 #include "inferior.h"
44 #include "regex.h"
45
46 static void monitor_command PARAMS ((char *args, int fromtty));
47 static void monitor_load_srec PARAMS ((char *args));
48
49 static int monitor_make_srec PARAMS ((char *buffer, int type,
50 CORE_ADDR memaddr,
51 unsigned char *myaddr, int len));
52
53 static void monitor_fetch_register PARAMS ((int regno));
54 static void monitor_store_register PARAMS ((int regno));
55
56 static void monitor_close PARAMS ((int quitting));
57 static void monitor_detach PARAMS ((char *args, int from_tty));
58 static void monitor_resume PARAMS ((int pid, int step, enum target_signal sig));
59 static int monitor_wait PARAMS ((int pid, struct target_waitstatus *status));
60 static void monitor_fetch_registers PARAMS ((int regno));
61 static void monitor_store_registers PARAMS ((int regno));
62 static void monitor_prepare_to_store PARAMS ((void));
63 static int monitor_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, int write, struct target_ops *target));
64 static void monitor_files_info PARAMS ((struct target_ops *ops));
65 static int monitor_insert_breakpoint PARAMS ((CORE_ADDR addr, char *shadow));
66 static int monitor_remove_breakpoint PARAMS ((CORE_ADDR addr, char *shadow));
67 static void monitor_kill PARAMS ((void));
68 static void monitor_load PARAMS ((char *file, int from_tty));
69 static void monitor_mourn_inferior PARAMS ((void));
70 static void monitor_stop PARAMS ((void));
71
72 static int from_hex PARAMS ((int a));
73 static unsigned long get_hex_word PARAMS ((void));
74
75 static struct monitor_ops *current_monitor;
76
77 static int hashmark; /* flag set by "set hash" */
78
79 static int timeout = 30;
80
81 /* Descriptor for I/O to remote machine. Initialize it to NULL so
82 that monitor_open knows that we don't have a file open when the
83 program starts. */
84
85 static serial_t monitor_desc = NULL;
86
87 /* Pointer to regexp pattern matching data */
88
89 static struct re_pattern_buffer register_pattern;
90
91 /* Element 0 points to start of register name, and element 1 points to the
92 start of the register value. */
93
94 static struct re_registers register_strings;
95
96 static char fastmap[256];
97
98 static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when
99 monitor_wait wakes up. */
100
101 /* monitor_printf -- send data to monitor. Works just like printf. */
102
103 void
104 monitor_printf (va_alist)
105 va_dcl
106 {
107 va_list args;
108 char *pattern;
109 char buf[2000];
110 int len;
111
112 va_start (args);
113
114 pattern = va_arg (args, char *);
115
116 vsprintf (buf, pattern, args);
117
118 if (remote_debug > 0)
119 fputs_unfiltered (buf, gdb_stderr);
120
121 len = strlen (buf);
122
123 if (len + 1 > sizeof buf)
124 abort ();
125
126 if (SERIAL_WRITE(monitor_desc, buf, len))
127 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
128 }
129
130 /* Read a character from the remote system, doing all the fancy
131 timeout stuff. */
132
133 static int
134 readchar (timeout)
135 int timeout;
136 {
137 int c;
138
139 c = SERIAL_READCHAR (monitor_desc, timeout);
140
141 if (remote_debug > 0)
142 fputc_unfiltered (c, gdb_stderr);
143
144 if (c >= 0)
145 return c & 0x7f;
146
147 if (c == SERIAL_TIMEOUT)
148 error ("Timeout reading from remote system.");
149
150 perror_with_name ("remote-monitor");
151 }
152
153 /* Scan input from the remote system, until STRING is found. If BUF is non-
154 zero, then collect input until we have collected either STRING or BUFLEN-1
155 chars. In either case we terminate BUF with a 0. If input overflows BUF
156 because STRING can't be found, return -1, else return number of chars in BUF
157 (minus the terminating NUL). Note that in the non-overflow case, STRING
158 will be at the end of BUF. */
159
160 int
161 monitor_expect (string, buf, buflen)
162 char *string;
163 char *buf;
164 int buflen;
165 {
166 char *p = string;
167 int obuflen = buflen;
168 int c;
169
170 immediate_quit = 1;
171 while (1)
172 {
173 if (buf)
174 {
175 if (buflen < 2)
176 {
177 *buf = '\000';
178 immediate_quit = 0;
179 return -1;
180 }
181
182 c = readchar (timeout);
183 *buf++ = c;
184 buflen--;
185 }
186 else
187 c = readchar (timeout);
188
189 if (c == *p++)
190 {
191 if (*p == '\0')
192 {
193 immediate_quit = 0;
194
195 if (buf)
196 {
197 *buf++ = '\000';
198 return obuflen - buflen;
199 }
200 else
201 return 0;
202 }
203 }
204 else
205 {
206 p = string;
207 if (c == *p)
208 p++;
209 }
210 }
211 }
212
213 /* Keep discarding input until we see the MONITOR prompt.
214
215 The convention for dealing with the prompt is that you
216 o give your command
217 o *then* wait for the prompt.
218
219 Thus the last thing that a procedure does with the serial line
220 will be an monitor_expect_prompt(). Exception: monitor_resume does not
221 wait for the prompt, because the terminal is being handed over
222 to the inferior. However, the next thing which happens after that
223 is a monitor_wait which does wait for the prompt.
224 Note that this includes abnormal exit, e.g. error(). This is
225 necessary to prevent getting into states from which we can't
226 recover. */
227
228 int
229 monitor_expect_prompt (buf, buflen)
230 char *buf;
231 int buflen;
232 {
233 return monitor_expect (PROMPT, buf, buflen);
234 }
235
236 /* Get N 32-bit words from remote, each preceded by a space, and put
237 them in registers starting at REGNO. */
238
239 static unsigned long
240 get_hex_word ()
241 {
242 unsigned long val;
243 int i;
244 int ch;
245
246 do
247 ch = readchar (timeout);
248 while (isspace(ch));
249
250 val = from_hex (ch);
251
252 for (i = 7; i >= 1; i--)
253 {
254 ch = readchar (timeout);
255 if (!isxdigit (ch))
256 break;
257 val = (val << 4) | from_hex (ch);
258 }
259
260 return val;
261 }
262
263 /* Open a connection to a remote debugger. NAME is the filename used
264 for communication. */
265
266 static char *dev_name;
267 static struct target_ops *targ_ops;
268
269 void
270 monitor_open (args, mon_ops, from_tty)
271 char *args;
272 struct monitor_ops *mon_ops;
273 int from_tty;
274 {
275 char *name;
276 int i;
277 char **p;
278
279 if (mon_ops->magic != MONITOR_OPS_MAGIC)
280 error ("Magic number of monitor_ops struct wrong.");
281
282 targ_ops = mon_ops->target;
283 name = targ_ops->to_shortname;
284
285 if (!args)
286 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
287 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
288
289 target_preopen (from_tty);
290
291 /* Setup pattern for register dump */
292
293 if (mon_ops->register_pattern)
294 {
295 int tmp;
296 char *val;
297
298 register_pattern.fastmap = fastmap;
299 tmp = re_set_syntax (RE_SYNTAX_EMACS);
300 val = re_compile_pattern (mon_ops->register_pattern,
301 strlen (mon_ops->register_pattern),
302 &register_pattern);
303 re_set_syntax (tmp);
304 if (val)
305 error ("Can't compiler register pattern string: %s!", val);
306 re_compile_fastmap (&register_pattern);
307 }
308
309 unpush_target (targ_ops);
310
311 if (dev_name)
312 free (dev_name);
313 dev_name = strsave (args);
314
315 monitor_desc = SERIAL_OPEN (dev_name);
316
317 if (!monitor_desc)
318 perror_with_name (dev_name);
319
320 if (baud_rate != -1)
321 {
322 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
323 {
324 SERIAL_CLOSE (monitor_desc);
325 perror_with_name (dev_name);
326 }
327 }
328
329 SERIAL_RAW (monitor_desc);
330
331 SERIAL_FLUSH_INPUT (monitor_desc);
332
333 /* some systems only work with 2 stop bits */
334
335 SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits);
336
337 current_monitor = mon_ops;
338
339 /* See if we can wake up the monitor. First, try sending a stop sequence,
340 then send the init strings. Last, remove all breakpoints. */
341
342 monitor_stop ();
343
344 /* wake up the monitor and see if it's alive */
345 for (p = mon_ops->init; *p != NULL; p++)
346 {
347 monitor_printf (*p);
348 monitor_expect_prompt (NULL, 0);
349 }
350
351 /* Remove all breakpoints */
352
353 if (mon_ops->clr_all_break)
354 {
355 monitor_printf (mon_ops->clr_all_break);
356 monitor_expect_prompt (NULL, 0);
357 }
358
359 if (from_tty)
360 printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name);
361
362 push_target (targ_ops);
363
364 inferior_pid = 42000; /* Make run command think we are busy... */
365
366 monitor_printf ("\r"); /* Give monitor_wait something to read */
367
368 start_remote ();
369 }
370
371 /* Close out all files and local state before this target loses
372 control. */
373
374 static void
375 monitor_close (quitting)
376 int quitting;
377 {
378 if (monitor_desc)
379 SERIAL_CLOSE (monitor_desc);
380 monitor_desc = NULL;
381 }
382
383 /* Terminate the open connection to the remote debugger. Use this
384 when you want to detach and do something else with your gdb. */
385
386 static void
387 monitor_detach (args, from_tty)
388 char *args;
389 int from_tty;
390 {
391 pop_target (); /* calls monitor_close to do the real work */
392 if (from_tty)
393 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
394 }
395
396 /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
397
398 char *
399 monitor_supply_register (regno, valstr)
400 int regno;
401 char *valstr;
402 {
403 unsigned LONGEST val;
404 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
405 char *p;
406
407 val = strtoul (valstr, &p, 16);
408
409 if (val == 0 && valstr == p)
410 error ("monitor_supply_register (%d): bad value from monitor: %s.",
411 regno, valstr);
412
413 /* supply register stores in target byte order, so swap here */
414
415 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
416
417 supply_register (regno, regbuf);
418
419 return p;
420 }
421
422 /* Tell the remote machine to resume. */
423
424 static void
425 monitor_resume (pid, step, sig)
426 int pid, step;
427 enum target_signal sig;
428 {
429 if (step)
430 monitor_printf (STEP_CMD);
431 else
432 {
433 monitor_printf (CONT_CMD);
434 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
435 dump_reg_flag = 1;
436 }
437 }
438
439 /* Parse the output of a register dump command. A monitor specific regexp is
440 used to extract individual register descriptions of the form REG=VAL. Each
441 description is split up into a name and a value string which are passed down
442 to monitor specific code. */
443
444 static char *
445 parse_register_dump (buf, len)
446 char *buf;
447 int len;
448 {
449 while (1)
450 {
451 int regnamelen, vallen;
452 char *regname, *val;
453
454 if (re_search (&register_pattern, buf, len, 0, len,
455 &register_strings) == -1)
456 break;
457
458 regnamelen = register_strings.end[1] - register_strings.start[1];
459 regname = buf + register_strings.start[1];
460 vallen = register_strings.end[2] - register_strings.start[2];
461 val = buf + register_strings.start[2];
462
463 current_monitor->supply_register (regname, regnamelen, val, vallen);
464
465 buf += register_strings.end[0];
466 len -= register_strings.end[0];
467 }
468 }
469
470 /* Wait until the remote machine stops, then return, storing status in
471 status just as `wait' would. */
472
473 static int
474 monitor_wait (pid, status)
475 int pid;
476 struct target_waitstatus *status;
477 {
478 int old_timeout = timeout;
479 char buf[1024];
480 int resp_len;
481
482 status->kind = TARGET_WAITKIND_EXITED;
483 status->value.integer = 0;
484
485 timeout = -1; /* Don't time out -- user program is running. */
486
487 do
488 {
489 resp_len = monitor_expect_prompt (buf, sizeof (buf));
490
491 if (resp_len <= 0)
492 fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf);
493 }
494 while (resp_len < 0);
495
496 timeout = old_timeout;
497
498 if (dump_reg_flag && current_monitor->dump_registers)
499 {
500 dump_reg_flag = 0;
501
502 monitor_printf (current_monitor->dump_registers);
503 resp_len = monitor_expect_prompt (buf, sizeof (buf));
504 }
505
506 if (current_monitor->register_pattern)
507 parse_register_dump (buf, resp_len);
508
509 status->kind = TARGET_WAITKIND_STOPPED;
510 status->value.sig = TARGET_SIGNAL_TRAP;
511
512 return inferior_pid;
513 }
514
515 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
516 errno value. */
517
518 static void
519 monitor_fetch_register (regno)
520 int regno;
521 {
522 char buf[200];
523 char *p;
524 char *name;
525 int resp_len;
526 static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0};
527
528 name = REGNAMES (regno);
529
530 if (!name)
531 {
532 supply_register (regno, zerobuf);
533 return;
534 }
535
536 /* send the register examine command */
537
538 monitor_printf (current_monitor->getreg.cmd, name);
539
540 /* If TERM is present, we wait for that to show up. Also, (if TERM is
541 present), we will send TERM_CMD if that is present. In any case, we collect
542 all of the output into buf, and then wait for the normal prompt. */
543
544 if (current_monitor->getreg.term)
545 {
546 resp_len = monitor_expect (current_monitor->getreg.term, buf, sizeof buf); /* get response */
547
548 if (resp_len <= 0)
549 error ("monitor_fetch_register (%d): excessive response from monitor: %.*s.",
550 regno, resp_len, buf);
551
552 if (current_monitor->getreg.term_cmd)
553 {
554 SERIAL_WRITE (monitor_desc, current_monitor->getreg.term_cmd,
555 strlen (current_monitor->getreg.term_cmd));
556 monitor_expect_prompt (NULL, 0);
557 }
558 }
559 else
560 resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */
561
562
563 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
564 the register value. Otherwise, we just start searching from the start of
565 the buf. */
566
567 if (current_monitor->getreg.resp_delim)
568 {
569 p = strstr (buf, current_monitor->getreg.resp_delim);
570 if (!p)
571 error ("monitor_fetch_register (%d): bad response from monitor: %.*s.",
572 regno, resp_len, buf);
573 p += strlen (current_monitor->getreg.resp_delim);
574 }
575 else
576 p = buf;
577
578 monitor_supply_register (regno, p);
579 }
580
581 /* Read the remote registers into the block regs. */
582
583 static void
584 monitor_fetch_registers (regno)
585 int regno;
586 {
587 if (regno >= 0)
588 {
589 monitor_fetch_register (regno);
590 return;
591 }
592
593 for (regno = 0; regno < NUM_REGS; regno++)
594 monitor_fetch_register (regno);
595 }
596
597 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
598
599 static void
600 monitor_store_register (regno)
601 int regno;
602 {
603 char *name;
604 unsigned LONGEST val;
605
606 name = REGNAMES (regno);
607 if (!name)
608 return;
609
610 val = read_register (regno);
611
612 /* send the register deposit command */
613
614 monitor_printf (current_monitor->setreg.cmd, name, val);
615
616 /* It's possible that there are actually some monitors out there that will
617 prompt you when you set a register. In that case, you may need to add some
618 code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get
619 an idea of what's needed...) */
620
621 monitor_expect_prompt (NULL, 0);
622 }
623
624 /* Store the remote registers. */
625
626 static void
627 monitor_store_registers (regno)
628 int regno;
629 {
630 if (regno >= 0)
631 {
632 monitor_store_register (regno);
633 return;
634 }
635
636 for (regno = 0; regno < NUM_REGS; regno++)
637 monitor_store_register (regno);
638 }
639
640 /* Get ready to modify the registers array. On machines which store
641 individual registers, this doesn't need to do anything. On machines
642 which store all the registers in one fell swoop, this makes sure
643 that registers contains all the registers from the program being
644 debugged. */
645
646 static void
647 monitor_prepare_to_store ()
648 {
649 /* Do nothing, since we can store individual regs */
650 }
651
652 static void
653 monitor_files_info (ops)
654 struct target_ops *ops;
655 {
656 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate);
657 }
658
659 static int
660 monitor_write_memory (memaddr, myaddr, len)
661 CORE_ADDR memaddr;
662 unsigned char *myaddr;
663 int len;
664 {
665 unsigned LONGEST val;
666 char *cmd;
667 int i;
668
669 /* Use memory fill command for leading 0 bytes. */
670
671 if (current_monitor->fill)
672 {
673 for (i = 0; i < len; i++)
674 if (myaddr[i] != 0)
675 break;
676
677 if (i > 4) /* More than 4 zeros is worth doing */
678 {
679 if (current_monitor->flags & MO_FILL_USES_ADDR)
680 monitor_printf (current_monitor->fill, memaddr, memaddr + i, 0);
681 else
682 monitor_printf (current_monitor->fill, memaddr, i, 0);
683
684 monitor_expect_prompt (NULL, 0);
685
686 return i;
687 }
688 }
689
690 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll)
691 {
692 len = 8;
693 cmd = current_monitor->setmem.cmdll;
694 }
695 else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl)
696 {
697 len = 4;
698 cmd = current_monitor->setmem.cmdl;
699 }
700 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw)
701 {
702 len = 2;
703 cmd = current_monitor->setmem.cmdw;
704 }
705 else
706 {
707 len = 1;
708 cmd = current_monitor->setmem.cmdb;
709 }
710
711 val = extract_unsigned_integer (myaddr, len);
712
713 monitor_printf (cmd, memaddr, val);
714
715 monitor_expect_prompt (NULL, 0);
716
717 return len;
718 }
719
720 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
721 at MEMADDR. Returns length moved. Currently, we only do one byte at a
722 time. */
723
724 static int
725 monitor_read_memory (memaddr, myaddr, len)
726 CORE_ADDR memaddr;
727 char *myaddr;
728 int len;
729 {
730 unsigned LONGEST val;
731 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
732 char buf[512];
733 char *p, *p1;
734 char *name;
735 int resp_len;
736 int i;
737
738 len = min (len, 16);
739
740 /* See if xfer would cross a 16 byte boundary. If so, clip it. */
741 if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0)
742 len = ((memaddr + len) & ~0xf) - memaddr;
743
744 /* send the memory examine command */
745
746 if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE)
747 monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len - 1);
748 else
749 monitor_printf (current_monitor->getmem.cmdb, memaddr, len);
750
751 /* If TERM is present, we wait for that to show up. Also, (if TERM is
752 present), we will send TERM_CMD if that is present. In any case, we collect
753 all of the output into buf, and then wait for the normal prompt. */
754
755 if (current_monitor->getmem.term)
756 {
757 resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
758
759 if (resp_len <= 0)
760 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
761 memaddr, resp_len, buf);
762
763 if (current_monitor->getmem.term_cmd)
764 {
765 SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd,
766 strlen (current_monitor->getmem.term_cmd));
767 monitor_expect_prompt (NULL, 0);
768 }
769 }
770 else
771 resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */
772
773 p = buf;
774
775 while (*p != '\r') /* Skip command echo and line delim */
776 p++;
777
778 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
779 the values. Otherwise, we just start searching from the start of the buf.
780 */
781
782 if (current_monitor->getmem.resp_delim)
783 {
784 p = strstr (p, current_monitor->getmem.resp_delim);
785 if (!p)
786 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
787 memaddr, resp_len, buf);
788 p += strlen (current_monitor->getmem.resp_delim);
789 }
790
791 for (i = len; i > 0; i--)
792 {
793 /* Skip non-hex chars, but bomb on end of string and newlines */
794
795 while (1)
796 {
797 if (isxdigit (*p))
798 break;
799 if (*p == '\000' || *p == '\n' || *p == '\r')
800 error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr, resp_len, buf);
801 p++;
802 }
803
804 val = strtoul (p, &p1, 16);
805
806 if (val == 0 && p == p1)
807 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr,
808 resp_len, buf);
809
810 *myaddr++ = val;
811
812 if (i == 1)
813 break;
814
815 p = p1;
816 }
817
818 return len;
819 }
820
821 /* FIXME-someday! merge these two. */
822
823 static int
824 monitor_xfer_memory (memaddr, myaddr, len, write, target)
825 CORE_ADDR memaddr;
826 char *myaddr;
827 int len;
828 int write;
829 struct target_ops *target; /* ignored */
830 {
831 if (write)
832 return monitor_write_memory (memaddr, myaddr, len);
833 else
834 return monitor_read_memory (memaddr, myaddr, len);
835 }
836
837 static void
838 monitor_kill ()
839 {
840 return; /* ignore attempts to kill target system */
841 }
842
843 /* All we actually do is set the PC to the start address of exec_bfd, and start
844 the program at that point. */
845
846 static void
847 monitor_create_inferior (exec_file, args, env)
848 char *exec_file;
849 char *args;
850 char **env;
851 {
852 if (args && (*args != '\000'))
853 error ("Args are not supported by the monitor.");
854
855 clear_proceed_status ();
856 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
857 }
858
859 /* Clean up when a program exits.
860 The program actually lives on in the remote processor's RAM, and may be
861 run again without a download. Don't leave it full of breakpoint
862 instructions. */
863
864 static void
865 monitor_mourn_inferior ()
866 {
867 unpush_target (targ_ops);
868 generic_mourn_inferior (); /* Do all the proper things now */
869 }
870
871 #define NUM_MONITOR_BREAKPOINTS 8
872
873 static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0};
874
875 /* Tell the monitor to add a breakpoint. */
876
877 static int
878 monitor_insert_breakpoint (addr, shadow)
879 CORE_ADDR addr;
880 char *shadow;
881 {
882 int i;
883 static unsigned char break_insn[] = BREAKPOINT;
884
885 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
886 {
887 if (breakaddr[i] == 0)
888 {
889 breakaddr[i] = addr;
890 monitor_read_memory (addr, shadow, sizeof (break_insn));
891 monitor_printf (SET_BREAK_CMD, addr);
892 monitor_expect_prompt (NULL, 0);
893 return 0;
894 }
895 }
896
897 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS);
898 }
899
900 /* Tell the monitor to remove a breakpoint. */
901
902 static int
903 monitor_remove_breakpoint (addr, shadow)
904 CORE_ADDR addr;
905 char *shadow;
906 {
907 int i;
908
909 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
910 {
911 if (breakaddr[i] == addr)
912 {
913 breakaddr[i] = 0;
914 /* some monitors remove breakpoints based on the address */
915 if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR)
916 monitor_printf (CLR_BREAK_CMD, addr);
917 else
918 monitor_printf (CLR_BREAK_CMD, i);
919 monitor_expect_prompt (NULL, 0);
920 return 0;
921 }
922 }
923 fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
924 return 1;
925 }
926
927 /* monitor_load -- download a file. */
928
929 static void
930 monitor_load (file, from_tty)
931 char *file;
932 int from_tty;
933 {
934 if (current_monitor->load_routine)
935 current_monitor->load_routine (monitor_desc, file, hashmark);
936 else
937 monitor_load_srec (file);
938
939 /* Finally, make the PC point at the start address */
940
941 write_pc (bfd_get_start_address (exec_bfd));
942
943 inferior_pid = 0; /* No process now */
944
945 /* This is necessary because many things were based on the PC at the time that
946 we attached to the monitor, which is no longer valid now that we have loaded
947 new code (and just changed the PC). Another way to do this might be to call
948 normal_stop, except that the stack may not be valid, and things would get
949 horribly confused... */
950
951 clear_symtab_users ();
952 }
953
954 static void
955 monitor_stop ()
956 {
957 if (!current_monitor->stop)
958 return;
959
960 monitor_printf(current_monitor->stop);
961 monitor_expect_prompt (NULL, 0);
962 }
963
964 /* Put a command string, in args, out to MONITOR. Output from MONITOR
965 is placed on the users terminal until the prompt is seen. FIXME: We
966 read the characters ourseleves here cause of a nasty echo. */
967
968 static void
969 monitor_command (args, from_tty)
970 char *args;
971 int from_tty;
972 {
973 char *p;
974 int resp_len;
975 char buf[1000];
976
977 if (monitor_desc == NULL)
978 error ("monitor target not open.");
979
980 p = PROMPT;
981
982 /* Send the command. Note that if no args were supplied, then we're
983 just sending the monitor a newline, which is sometimes useful. */
984
985 monitor_printf ("%s\r", (args ? args : ""));
986
987 resp_len = monitor_expect_prompt (buf, sizeof buf);
988
989 fputs_unfiltered (buf, gdb_stdout); /* Output the response */
990 }
991
992 /* Download a binary file by converting it to S records. */
993
994 static void
995 monitor_load_srec (args)
996 char *args;
997 {
998 bfd *abfd;
999 asection *s;
1000 char *buffer, srec[1024];
1001 int i;
1002 int srec_frame = 128;
1003 int reclen;
1004
1005 buffer = alloca (srec_frame * 2 + 256);
1006
1007 abfd = bfd_openr (args, 0);
1008 if (!abfd)
1009 {
1010 printf_filtered ("Unable to open file %s\n", args);
1011 return;
1012 }
1013
1014 if (bfd_check_format (abfd, bfd_object) == 0)
1015 {
1016 printf_filtered ("File is not an object file\n");
1017 return;
1018 }
1019
1020 monitor_printf (LOAD_CMD); /* tell the monitor to load */
1021 if (current_monitor->loadresp)
1022 monitor_expect (current_monitor->loadresp, NULL, 0);
1023
1024 for (s = abfd->sections; s; s = s->next)
1025 if (s->flags & SEC_LOAD)
1026 {
1027 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
1028 s->vma + s->_raw_size);
1029 gdb_flush (gdb_stdout);
1030
1031 for (i = 0; i < s->_raw_size; i += srec_frame)
1032 {
1033 int numbytes;
1034
1035 numbytes = min (srec_frame, s->_raw_size - i);
1036
1037 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
1038
1039 reclen = monitor_make_srec (srec, 3, s->vma + i, buffer, numbytes);
1040
1041 monitor_printf ("%.*s\r", reclen, srec);
1042
1043 if (hashmark)
1044 {
1045 putchar_unfiltered ('#');
1046 gdb_flush (gdb_stdout);
1047 }
1048 } /* Per-packet (or S-record) loop */
1049
1050 putchar_unfiltered ('\n');
1051 } /* Loadable sections */
1052
1053 if (hashmark)
1054 putchar_unfiltered ('\n');
1055
1056 /* Write a type 7 terminator record. no data for a type 7, and there
1057 is no data, so len is 0. */
1058
1059 reclen = monitor_make_srec (srec, 7, abfd->start_address, NULL, 0);
1060
1061 monitor_printf ("%.*s\r", reclen, srec);
1062
1063 monitor_printf ("\r\r"); /* Some monitors need these to wake up */
1064
1065 monitor_expect_prompt (NULL, 0);
1066 }
1067
1068 /*
1069 * monitor_make_srec -- make an srecord. This writes each line, one at a
1070 * time, each with it's own header and trailer line.
1071 * An srecord looks like this:
1072 *
1073 * byte count-+ address
1074 * start ---+ | | data +- checksum
1075 * | | | |
1076 * S01000006F6B692D746573742E73726563E4
1077 * S315000448600000000000000000FC00005900000000E9
1078 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1079 * S30B0004485A0000000000004E
1080 * S70500040000F6
1081 *
1082 * S<type><length><address><data><checksum>
1083 *
1084 * Where
1085 * - length
1086 * is the number of bytes following upto the checksum. Note that
1087 * this is not the number of chars following, since it takes two
1088 * chars to represent a byte.
1089 * - type
1090 * is one of:
1091 * 0) header record
1092 * 1) two byte address data record
1093 * 2) three byte address data record
1094 * 3) four byte address data record
1095 * 7) four byte address termination record
1096 * 8) three byte address termination record
1097 * 9) two byte address termination record
1098 *
1099 * - address
1100 * is the start address of the data following, or in the case of
1101 * a termination record, the start address of the image
1102 * - data
1103 * is the data.
1104 * - checksum
1105 * is the sum of all the raw byte data in the record, from the length
1106 * upwards, modulo 256 and subtracted from 255.
1107 *
1108 * This routine returns the length of the S-record.
1109 *
1110 */
1111
1112 static int
1113 monitor_make_srec (buffer, type, memaddr, myaddr, len)
1114 char *buffer;
1115 int type;
1116 CORE_ADDR memaddr;
1117 unsigned char *myaddr;
1118 int len;
1119 {
1120 unsigned char checksum;
1121 int i;
1122 char *buf;
1123 static char hextab[] = "0123456789ABCDEF";
1124
1125 buf = buffer;
1126
1127 checksum = 0;
1128
1129 /* Create the header for the srec. 4 is the number of bytes in the address,
1130 and 1 is the number of bytes in the count. */
1131
1132 sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr);
1133 buf += 12;
1134
1135 /* Note that the checksum is calculated on the raw data, not the hexified
1136 data. It includes the length, address and the data portions of the
1137 packet. */
1138
1139 checksum += (len + 4 + 1 /* Packet length */
1140 + (memaddr & 0xff) /* Address... */
1141 + ((memaddr >> 8) & 0xff)
1142 + ((memaddr >> 16) & 0xff)
1143 + ((memaddr >> 24) & 0xff));
1144
1145 /* build the srecord */
1146 for (i = 0; i < len; i++)
1147 {
1148 *buf++ = hextab [myaddr[i] >> 4];
1149 *buf++ = hextab [myaddr[i] & 0xf];
1150 checksum += myaddr[i];
1151 }
1152
1153 checksum = ~checksum;
1154
1155 *buf++ = hextab[checksum >> 4];
1156 *buf++ = hextab[checksum & 0xf];
1157
1158 return buf - buffer;
1159 }
1160
1161 /* Convert hex digit A to a number. */
1162
1163 static int
1164 from_hex (a)
1165 int a;
1166 {
1167 if (a >= '0' && a <= '9')
1168 return a - '0';
1169 if (a >= 'a' && a <= 'f')
1170 return a - 'a' + 10;
1171 if (a >= 'A' && a <= 'F')
1172 return a - 'A' + 10;
1173
1174 error ("Reply contains invalid hex digit 0x%x", a);
1175 }
1176
1177 static struct target_ops monitor_ops =
1178 {
1179 NULL, /* to_shortname */
1180 NULL, /* to_longname */
1181 NULL, /* to_doc */
1182 NULL, /* to_open */
1183 monitor_close, /* to_close */
1184 NULL, /* to_attach */
1185 monitor_detach, /* to_detach */
1186 monitor_resume, /* to_resume */
1187 monitor_wait, /* to_wait */
1188 monitor_fetch_registers, /* to_fetch_registers */
1189 monitor_store_registers, /* to_store_registers */
1190 monitor_prepare_to_store, /* to_prepare_to_store */
1191 monitor_xfer_memory, /* to_xfer_memory */
1192 monitor_files_info, /* to_files_info */
1193 monitor_insert_breakpoint, /* to_insert_breakpoint */
1194 monitor_remove_breakpoint, /* to_remove_breakpoint */
1195 0, /* to_terminal_init */
1196 0, /* to_terminal_inferior */
1197 0, /* to_terminal_ours_for_output */
1198 0, /* to_terminal_ours */
1199 0, /* to_terminal_info */
1200 monitor_kill, /* to_kill */
1201 monitor_load, /* to_load */
1202 0, /* to_lookup_symbol */
1203 monitor_create_inferior, /* to_create_inferior */
1204 monitor_mourn_inferior, /* to_mourn_inferior */
1205 0, /* to_can_run */
1206 0, /* to_notice_signals */
1207 monitor_stop, /* to_stop */
1208 process_stratum, /* to_stratum */
1209 0, /* to_next */
1210 1, /* to_has_all_memory */
1211 1, /* to_has_memory */
1212 1, /* to_has_stack */
1213 1, /* to_has_registers */
1214 1, /* to_has_execution */
1215 0, /* sections */
1216 0, /* sections_end */
1217 OPS_MAGIC /* to_magic */
1218 };
1219
1220 /* Init the target_ops structure pointed at by OPS */
1221
1222 void
1223 init_monitor_ops (ops)
1224 struct target_ops *ops;
1225 {
1226 memcpy (ops, &monitor_ops, sizeof monitor_ops);
1227 }
1228
1229 /* Define additional commands that are usually only used by monitors. */
1230
1231 void
1232 _initialize_remote_monitors ()
1233 {
1234 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1235 (char *)&hashmark,
1236 "Set display of activity while downloading a file.\n\
1237 When enabled, a hashmark \'#\' is displayed.",
1238 &setlist),
1239 &showlist);
1240
1241 add_com ("monitor", class_obscure, monitor_command,
1242 "Send a command to the debug monitor.");
1243 }