Add SH ELF support.
[deliverable/binutils-gdb.git] / gdb / monitor.c
CommitLineData
51d6a954 1/* Remote debugging interface for boot monitors, for GDB.
69159fad 2 Copyright 1990, 1991, 1992, 1993, 1995, 1996
b9ab37f0
MM
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
5 Resurrected from the ashes by Stu Grossman.
51d6a954
RS
6
7This file is part of GDB.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
5be86c56 21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
51d6a954
RS
22
23/* This file was derived from various remote-* modules. It is a collection
24 of generic support functions so GDB can talk directly to a ROM based
25 monitor. This saves use from having to hack an exception based handler
26 into existance, and makes for quick porting.
27
28 This module talks to a debug monitor called 'MONITOR', which
29 We communicate with MONITOR via either a direct serial line, or a TCP
30 (or possibly TELNET) stream to a terminal multiplexor,
431b7d5f 31 which in turn talks to the target board. */
51d6a954 32
69159fad
SS
33/* FIXME 32x64: This code assumes that registers and addresses are at
34 most 32 bits long. If they can be larger, you will need to declare
35 values as LONGEST and use %llx or some such to print values when
36 building commands to send to the monitor. Since we don't know of
37 any actual 64-bit targets with ROM monitors that use this code,
38 it's not an issue right now. -sts 4/18/96 */
39
51d6a954
RS
40#include "defs.h"
41#include "gdbcore.h"
42#include "target.h"
43#include "wait.h"
4a430794 44#ifdef ANSI_PROTOTYPES
85c613aa
C
45#include <stdarg.h>
46#else
51d6a954 47#include <varargs.h>
85c613aa 48#endif
51d6a954 49#include <signal.h>
b9ab37f0 50#include <ctype.h>
2b576293 51#include "gdb_string.h"
51d6a954
RS
52#include <sys/types.h>
53#include "command.h"
54#include "serial.h"
55#include "monitor.h"
1265e2d8
SG
56#include "gdbcmd.h"
57#include "inferior.h"
b9ab37f0 58#include "gnu-regex.h"
45993f61 59#include "dcache.h"
b9ab37f0 60#include "srec.h"
51d6a954 61
eba08643
C
62static int readchar PARAMS ((int timeout));
63
8f078234 64static void monitor_command PARAMS ((char *args, int fromtty));
431b7d5f 65
1265e2d8
SG
66static void monitor_fetch_register PARAMS ((int regno));
67static void monitor_store_register PARAMS ((int regno));
68
a706069f
SG
69static void monitor_detach PARAMS ((char *args, int from_tty));
70static void monitor_resume PARAMS ((int pid, int step, enum target_signal sig));
eba08643
C
71static void monitor_interrupt PARAMS ((int signo));
72static void monitor_interrupt_twice PARAMS ((int signo));
73static void monitor_interrupt_query PARAMS ((void));
74static void monitor_wait_cleanup PARAMS ((int old_timeout));
75
a706069f
SG
76static int monitor_wait PARAMS ((int pid, struct target_waitstatus *status));
77static void monitor_fetch_registers PARAMS ((int regno));
78static void monitor_store_registers PARAMS ((int regno));
79static void monitor_prepare_to_store PARAMS ((void));
80static int monitor_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, int write, struct target_ops *target));
81static void monitor_files_info PARAMS ((struct target_ops *ops));
82static int monitor_insert_breakpoint PARAMS ((CORE_ADDR addr, char *shadow));
83static int monitor_remove_breakpoint PARAMS ((CORE_ADDR addr, char *shadow));
84static void monitor_kill PARAMS ((void));
85static void monitor_load PARAMS ((char *file, int from_tty));
86static void monitor_mourn_inferior PARAMS ((void));
87static void monitor_stop PARAMS ((void));
69159fad 88static void monitor_debug PARAMS ((char *prefix, char *string, char *suffix));
a706069f 89
45993f61
SC
90static int monitor_read_memory PARAMS ((CORE_ADDR addr, char *myaddr,int len));
91static int monitor_write_memory PARAMS ((CORE_ADDR addr, char *myaddr,int len));
92
69159fad
SS
93static int monitor_expect_regexp PARAMS ((struct re_pattern_buffer *pat,
94 char *buf, int buflen));
8f078234 95static int from_hex PARAMS ((int a));
1265e2d8 96static unsigned long get_hex_word PARAMS ((void));
06b8f5e4 97
8f078234 98static struct monitor_ops *current_monitor;
431b7d5f 99
774e5d7f 100static int hashmark; /* flag set by "set hash" */
51d6a954 101
1b552670 102static int timeout = 30;
431b7d5f 103
4a430794
SS
104static int in_monitor_wait = 0; /* Non-zero means we are in monitor_wait() */
105
eba08643
C
106static void (*ofunc)(); /* Old SIGINT signal handler */
107
431b7d5f
SS
108/* Descriptor for I/O to remote machine. Initialize it to NULL so
109 that monitor_open knows that we don't have a file open when the
110 program starts. */
111
1265e2d8 112static serial_t monitor_desc = NULL;
431b7d5f 113
a706069f
SG
114/* Pointer to regexp pattern matching data */
115
116static struct re_pattern_buffer register_pattern;
283dc598 117static char register_fastmap[256];
a706069f 118
283dc598
SG
119static struct re_pattern_buffer getmem_resp_delim_pattern;
120static char getmem_resp_delim_fastmap[256];
a706069f
SG
121
122static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when
123 monitor_wait wakes up. */
124
45993f61 125static DCACHE *remote_dcache;
012be3ce
DP
126static int first_time=0; /* is this the first time we're executing after
127 gaving created the child proccess? */
45993f61 128
b9ab37f0
MM
129/* monitor_debug is like fputs_unfiltered, except it prints special
130 characters in printable fashion. */
131
132static void
69159fad
SS
133monitor_debug (prefix, string, suffix)
134 char *prefix;
b9ab37f0 135 char *string;
69159fad 136 char *suffix;
b9ab37f0
MM
137{
138 int ch;
139
69159fad
SS
140 /* print prefix and suffix after each line */
141 static int new_line=1;
f7ce02f4
DP
142 static char *prev_prefix = "";
143 static char *prev_suffix = "";
012be3ce 144
f7ce02f4
DP
145 /* if the prefix is changing, print the previous suffix, a new line,
146 and the new prefix */
147 if (strcmp(prev_prefix, prefix) != 0 && !new_line)
148 {
149 fputs_unfiltered (prev_suffix, gdb_stderr);
150 fputs_unfiltered ("\n", gdb_stderr);
151 fputs_unfiltered (prefix, gdb_stderr);
152 }
153 prev_prefix = prefix;
154 prev_suffix = suffix;
155
156 /* print prefix if last char was a newline*/
157
158 if (new_line == 1) {
69159fad
SS
159 fputs_unfiltered (prefix, gdb_stderr);
160 new_line=0;
161 }
162 if (strchr(string,'\n')) /* save state for next call */
163 new_line=1;
164
b9ab37f0
MM
165 while ((ch = *string++) != '\0')
166 {
167 switch (ch) {
168 default:
169 if (isprint (ch))
170 fputc_unfiltered (ch, gdb_stderr);
171
172 else
173 fprintf_unfiltered (gdb_stderr, "\\%03o", ch);
174
175 break;
176
177 case '\\': fputs_unfiltered ("\\\\", gdb_stderr); break;
178 case '\b': fputs_unfiltered ("\\b", gdb_stderr); break;
179 case '\f': fputs_unfiltered ("\\f", gdb_stderr); break;
69159fad
SS
180 case '\n': fputs_unfiltered ("\\n", gdb_stderr); break;
181 case '\r': fputs_unfiltered ("\\r", gdb_stderr); break;
b9ab37f0
MM
182 case '\t': fputs_unfiltered ("\\t", gdb_stderr); break;
183 case '\v': fputs_unfiltered ("\\v", gdb_stderr); break;
184 }
185 }
b9ab37f0 186
69159fad
SS
187 if (new_line==1) { /* print suffix if last char was a newline */
188 fputs_unfiltered (suffix, gdb_stderr);
189 fputs_unfiltered ("\n", gdb_stderr);
190 }
191}
b9ab37f0 192
eba08643
C
193/* monitor_printf_noecho -- Send data to monitor, but don't expect an echo.
194 Works just like printf. */
195
196void
4a430794 197#ifdef ANSI_PROTOTYPES
85c613aa
C
198monitor_printf_noecho (char *pattern, ...)
199#else
eba08643
C
200monitor_printf_noecho (va_alist)
201 va_dcl
85c613aa 202#endif
eba08643
C
203{
204 va_list args;
eba08643
C
205 char sndbuf[2000];
206 int len;
207
4a430794 208#if ANSI_PROTOTYPES
85c613aa
C
209 va_start (args, pattern);
210#else
211 char *pattern;
eba08643 212 va_start (args);
eba08643 213 pattern = va_arg (args, char *);
85c613aa 214#endif
eba08643
C
215
216 vsprintf (sndbuf, pattern, args);
217
218 if (remote_debug > 0)
69159fad 219 monitor_debug ("sent -->", sndbuf, "<--");
eba08643
C
220
221 len = strlen (sndbuf);
222
223 if (len + 1 > sizeof sndbuf)
224 abort ();
225
226 if (SERIAL_WRITE(monitor_desc, sndbuf, len))
227 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
228}
229
230/* monitor_printf -- Send data to monitor and check the echo. Works just like
231 printf. */
431b7d5f 232
774e5d7f 233void
4a430794 234#ifdef ANSI_PROTOTYPES
85c613aa
C
235monitor_printf (char *pattern, ...)
236#else
774e5d7f 237monitor_printf (va_alist)
51d6a954 238 va_dcl
85c613aa 239#endif
51d6a954
RS
240{
241 va_list args;
eba08643 242 char sndbuf[2000];
774e5d7f 243 int len;
51d6a954 244
4a430794 245#ifdef ANSI_PROTOTYPES
85c613aa
C
246 va_start (args, pattern);
247#else
248 char *pattern;
431b7d5f 249 va_start (args);
431b7d5f 250 pattern = va_arg (args, char *);
85c613aa 251#endif
51d6a954 252
eba08643 253 vsprintf (sndbuf, pattern, args);
51d6a954 254
1265e2d8 255 if (remote_debug > 0)
69159fad 256 monitor_debug ("sent -->", sndbuf, "<--");
51d6a954 257
eba08643 258 len = strlen (sndbuf);
431b7d5f 259
eba08643 260 if (len + 1 > sizeof sndbuf)
774e5d7f 261 abort ();
431b7d5f 262
eba08643 263 if (SERIAL_WRITE(monitor_desc, sndbuf, len))
774e5d7f 264 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
eba08643 265
69159fad
SS
266 /* We used to expect that the next immediate output was the characters we
267 just output, but sometimes some extra junk appeared before the characters
268 we expected, like an extra prompt, or a portmaster sending telnet negotiations.
269 So, just start searching for what we sent, and skip anything unknown. */
270 monitor_expect (sndbuf, (char *)0, 0);
e6fa5bd6
RS
271}
272
431b7d5f
SS
273/* Read a character from the remote system, doing all the fancy
274 timeout stuff. */
275
51d6a954 276static int
431b7d5f 277readchar (timeout)
51d6a954
RS
278 int timeout;
279{
280 int c;
69159fad
SS
281 static enum { last_random, last_nl, last_cr, last_crnl } state = last_random;
282 int looping;
51d6a954 283
69159fad
SS
284 do
285 {
286 looping = 0;
287 c = SERIAL_READCHAR (monitor_desc, timeout);
51d6a954 288
69159fad
SS
289 if (c >= 0)
290 {
291 c &= 0x7f;
292 if (remote_debug > 0)
293 {
294 char buf[2];
295 buf[0] = c;
296 buf[1] = '\0';
297 monitor_debug ("read -->", buf, "<--");
298 }
299 }
300
301 /* Canonicialize \n\r combinations into one \r */
302 if ((current_monitor->flags & MO_HANDLE_NL) != 0)
303 {
304 if ((c == '\r' && state == last_nl)
305 || (c == '\n' && state == last_cr))
306 {
307 state = last_crnl;
308 looping = 1;
309 }
310 else if (c == '\r')
311 state = last_cr;
312 else if (c != '\n')
313 state = last_random;
314 else
315 {
316 state = last_nl;
317 c = '\r';
318 }
319 }
320 }
321 while (looping);
51d6a954
RS
322
323 if (c >= 0)
69159fad 324 return c;
51d6a954 325
431b7d5f 326 if (c == SERIAL_TIMEOUT)
4a430794
SS
327#ifdef MAINTENANCE_CMDS
328 if (in_monitor_wait) /* Watchdog went off */
329 {
330 target_mourn_inferior ();
331 error ("Watchdog has expired. Target detached.\n");
332 }
333 else
334#endif
335 error ("Timeout reading from remote system.");
a706069f 336
431b7d5f 337 perror_with_name ("remote-monitor");
51d6a954
RS
338}
339
1265e2d8 340/* Scan input from the remote system, until STRING is found. If BUF is non-
a706069f
SG
341 zero, then collect input until we have collected either STRING or BUFLEN-1
342 chars. In either case we terminate BUF with a 0. If input overflows BUF
343 because STRING can't be found, return -1, else return number of chars in BUF
344 (minus the terminating NUL). Note that in the non-overflow case, STRING
345 will be at the end of BUF. */
431b7d5f 346
774e5d7f
SS
347int
348monitor_expect (string, buf, buflen)
51d6a954 349 char *string;
1265e2d8
SG
350 char *buf;
351 int buflen;
51d6a954
RS
352{
353 char *p = string;
1265e2d8 354 int obuflen = buflen;
51d6a954
RS
355 int c;
356
51d6a954 357 immediate_quit = 1;
431b7d5f
SS
358 while (1)
359 {
1265e2d8 360 if (buf)
431b7d5f 361 {
a706069f 362 if (buflen < 2)
431b7d5f 363 {
a706069f 364 *buf = '\000';
431b7d5f 365 immediate_quit = 0;
1265e2d8 366 return -1;
431b7d5f 367 }
1265e2d8
SG
368
369 c = readchar (timeout);
283dc598
SG
370 if (c == '\000')
371 continue;
1265e2d8
SG
372 *buf++ = c;
373 buflen--;
431b7d5f
SS
374 }
375 else
1265e2d8
SG
376 c = readchar (timeout);
377
69159fad
SS
378 /* Don't expect any ^C sent to be echoed */
379
380 if (*p == '\003' || c == *p)
431b7d5f 381 {
69159fad 382 p++;
1265e2d8 383 if (*p == '\0')
431b7d5f 384 {
1265e2d8
SG
385 immediate_quit = 0;
386
a706069f
SG
387 if (buf)
388 {
389 *buf++ = '\000';
390 return obuflen - buflen;
391 }
392 else
393 return 0;
431b7d5f 394 }
1265e2d8
SG
395 }
396 else
397 {
431b7d5f 398 p = string;
1265e2d8
SG
399 if (c == *p)
400 p++;
431b7d5f 401 }
51d6a954 402 }
51d6a954
RS
403}
404
283dc598
SG
405/* Search for a regexp. */
406
69159fad 407static int
283dc598
SG
408monitor_expect_regexp (pat, buf, buflen)
409 struct re_pattern_buffer *pat;
410 char *buf;
411 int buflen;
412{
413 char *mybuf;
414 char *p;
415
416 if (buf)
417 mybuf = buf;
418 else
419 {
420 mybuf = alloca (1024);
421 buflen = 1024;
422 }
423
424 p = mybuf;
425 while (1)
426 {
427 int retval;
428
429 if (p - mybuf >= buflen)
430 { /* Buffer about to overflow */
431
432/* On overflow, we copy the upper half of the buffer to the lower half. Not
433 great, but it usually works... */
434
435 memcpy (mybuf, mybuf + buflen / 2, buflen / 2);
436 p = mybuf + buflen / 2;
437 }
438
439 *p++ = readchar (timeout);
440
441 retval = re_search (pat, mybuf, p - mybuf, 0, p - mybuf, NULL);
442 if (retval >= 0)
443 return 1;
444 }
445}
446
51d6a954
RS
447/* Keep discarding input until we see the MONITOR prompt.
448
449 The convention for dealing with the prompt is that you
450 o give your command
451 o *then* wait for the prompt.
452
453 Thus the last thing that a procedure does with the serial line
774e5d7f 454 will be an monitor_expect_prompt(). Exception: monitor_resume does not
51d6a954
RS
455 wait for the prompt, because the terminal is being handed over
456 to the inferior. However, the next thing which happens after that
457 is a monitor_wait which does wait for the prompt.
458 Note that this includes abnormal exit, e.g. error(). This is
459 necessary to prevent getting into states from which we can't
460 recover. */
431b7d5f 461
774e5d7f
SS
462int
463monitor_expect_prompt (buf, buflen)
1265e2d8
SG
464 char *buf;
465 int buflen;
51d6a954 466{
f7ce02f4 467 return monitor_expect (current_monitor->prompt, buf, buflen);
51d6a954
RS
468}
469
431b7d5f
SS
470/* Get N 32-bit words from remote, each preceded by a space, and put
471 them in registers starting at REGNO. */
472
1265e2d8 473static unsigned long
51d6a954
RS
474get_hex_word ()
475{
1265e2d8 476 unsigned long val;
51d6a954 477 int i;
1265e2d8 478 int ch;
51d6a954 479
1265e2d8
SG
480 do
481 ch = readchar (timeout);
482 while (isspace(ch));
cf51c601 483
1265e2d8
SG
484 val = from_hex (ch);
485
486 for (i = 7; i >= 1; i--)
431b7d5f 487 {
1265e2d8
SG
488 ch = readchar (timeout);
489 if (!isxdigit (ch))
490 break;
491 val = (val << 4) | from_hex (ch);
431b7d5f 492 }
51d6a954
RS
493
494 return val;
495}
496
283dc598
SG
497static void
498compile_pattern (pattern, compiled_pattern, fastmap)
499 char *pattern;
500 struct re_pattern_buffer *compiled_pattern;
501 char *fastmap;
502{
503 int tmp;
504 char *val;
505
506 compiled_pattern->fastmap = fastmap;
507
508 tmp = re_set_syntax (RE_SYNTAX_EMACS);
509 val = re_compile_pattern (pattern,
510 strlen (pattern),
511 compiled_pattern);
512 re_set_syntax (tmp);
513
514 if (val)
515 error ("compile_pattern: Can't compile pattern string `%s': %s!", pattern, val);
516
517 if (fastmap)
518 re_compile_fastmap (compiled_pattern);
519}
520
431b7d5f
SS
521/* Open a connection to a remote debugger. NAME is the filename used
522 for communication. */
523
1265e2d8 524static char *dev_name;
8f078234 525static struct target_ops *targ_ops;
51d6a954
RS
526
527void
1265e2d8 528monitor_open (args, mon_ops, from_tty)
51d6a954 529 char *args;
1265e2d8 530 struct monitor_ops *mon_ops;
51d6a954
RS
531 int from_tty;
532{
1265e2d8
SG
533 char *name;
534 int i;
a706069f
SG
535 char **p;
536
537 if (mon_ops->magic != MONITOR_OPS_MAGIC)
538 error ("Magic number of monitor_ops struct wrong.");
1265e2d8
SG
539
540 targ_ops = mon_ops->target;
541 name = targ_ops->to_shortname;
51d6a954 542
1265e2d8 543 if (!args)
51d6a954
RS
544 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
545`target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
546
1265e2d8
SG
547 target_preopen (from_tty);
548
a706069f
SG
549 /* Setup pattern for register dump */
550
551 if (mon_ops->register_pattern)
283dc598
SG
552 compile_pattern (mon_ops->register_pattern, &register_pattern,
553 register_fastmap);
554
555 if (mon_ops->getmem.resp_delim)
556 compile_pattern (mon_ops->getmem.resp_delim, &getmem_resp_delim_pattern,
557 getmem_resp_delim_fastmap);
a706069f 558
1265e2d8
SG
559 unpush_target (targ_ops);
560
561 if (dev_name)
562 free (dev_name);
563 dev_name = strsave (args);
51d6a954 564
431b7d5f 565 monitor_desc = SERIAL_OPEN (dev_name);
51d6a954 566
1265e2d8 567 if (!monitor_desc)
431b7d5f
SS
568 perror_with_name (dev_name);
569
570 if (baud_rate != -1)
571 {
572 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
573 {
574 SERIAL_CLOSE (monitor_desc);
1265e2d8 575 perror_with_name (dev_name);
431b7d5f 576 }
51d6a954 577 }
7a1330f7 578
431b7d5f 579 SERIAL_RAW (monitor_desc);
51d6a954 580
1265e2d8
SG
581 SERIAL_FLUSH_INPUT (monitor_desc);
582
cf51c601 583 /* some systems only work with 2 stop bits */
1265e2d8
SG
584
585 SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits);
586
587 current_monitor = mon_ops;
51d6a954 588
a706069f
SG
589 /* See if we can wake up the monitor. First, try sending a stop sequence,
590 then send the init strings. Last, remove all breakpoints. */
591
eba08643
C
592 if (current_monitor->stop)
593 {
594 monitor_stop ();
69159fad
SS
595 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
596 {
597 monitor_expect_prompt (NULL, 0);
598 }
eba08643 599 }
431b7d5f 600
1265e2d8 601 /* wake up the monitor and see if it's alive */
a706069f
SG
602 for (p = mon_ops->init; *p != NULL; p++)
603 {
69159fad
SS
604 /* Some of the characters we send may not be echoed,
605 but we hope to get a prompt at the end of it all. */
606
607 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
608 monitor_printf(*p);
609 else
610 monitor_printf_noecho (*p);
774e5d7f 611 monitor_expect_prompt (NULL, 0);
a706069f
SG
612 }
613
eba08643
C
614 SERIAL_FLUSH_INPUT (monitor_desc);
615
a706069f 616 /* Remove all breakpoints */
431b7d5f 617
a706069f
SG
618 if (mon_ops->clr_all_break)
619 {
774e5d7f
SS
620 monitor_printf (mon_ops->clr_all_break);
621 monitor_expect_prompt (NULL, 0);
a706069f 622 }
1265e2d8 623
51d6a954 624 if (from_tty)
1265e2d8
SG
625 printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name);
626
627 push_target (targ_ops);
628
629 inferior_pid = 42000; /* Make run command think we are busy... */
630
eba08643
C
631 /* Give monitor_wait something to read */
632
633 monitor_printf (current_monitor->line_term);
1265e2d8 634
45993f61
SC
635 remote_dcache = dcache_init (monitor_read_memory, monitor_write_memory);
636
1265e2d8 637 start_remote ();
51d6a954
RS
638}
639
431b7d5f
SS
640/* Close out all files and local state before this target loses
641 control. */
51d6a954 642
69159fad 643void
51d6a954
RS
644monitor_close (quitting)
645 int quitting;
646{
1265e2d8
SG
647 if (monitor_desc)
648 SERIAL_CLOSE (monitor_desc);
51d6a954 649 monitor_desc = NULL;
51d6a954
RS
650}
651
431b7d5f
SS
652/* Terminate the open connection to the remote debugger. Use this
653 when you want to detach and do something else with your gdb. */
654
a706069f 655static void
1265e2d8
SG
656monitor_detach (args, from_tty)
657 char *args;
51d6a954
RS
658 int from_tty;
659{
431b7d5f 660 pop_target (); /* calls monitor_close to do the real work */
51d6a954 661 if (from_tty)
1265e2d8 662 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
51d6a954 663}
7804e5bc 664
a706069f
SG
665/* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
666
667char *
668monitor_supply_register (regno, valstr)
669 int regno;
670 char *valstr;
671{
69159fad 672 unsigned int val;
a706069f
SG
673 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
674 char *p;
675
676 val = strtoul (valstr, &p, 16);
677
678 if (val == 0 && valstr == p)
679 error ("monitor_supply_register (%d): bad value from monitor: %s.",
680 regno, valstr);
681
682 /* supply register stores in target byte order, so swap here */
683
684 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
685
686 supply_register (regno, regbuf);
687
688 return p;
689}
690
431b7d5f
SS
691/* Tell the remote machine to resume. */
692
a706069f 693static void
51d6a954
RS
694monitor_resume (pid, step, sig)
695 int pid, step;
696 enum target_signal sig;
697{
012be3ce
DP
698 /* Some monitors require a different command when starting a program */
699 if (current_monitor->flags & MO_RUN_FIRST_TIME && first_time == 1)
700 {
701 first_time = 0;
702 monitor_printf ("run\r");
703 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
704 dump_reg_flag = 1;
705 return;
706 }
45993f61 707 dcache_flush (remote_dcache);
431b7d5f 708 if (step)
f7ce02f4 709 monitor_printf (current_monitor->step);
431b7d5f 710 else
a706069f 711 {
f7ce02f4 712 monitor_printf (current_monitor->cont);
a706069f
SG
713 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
714 dump_reg_flag = 1;
715 }
716}
717
69159fad
SS
718/* Parse the output of a register dump command. A monitor specific
719 regexp is used to extract individual register descriptions of the
720 form REG=VAL. Each description is split up into a name and a value
721 string which are passed down to monitor specific code. */
a706069f
SG
722
723static char *
724parse_register_dump (buf, len)
725 char *buf;
726 int len;
727{
728 while (1)
729 {
730 int regnamelen, vallen;
731 char *regname, *val;
69159fad
SS
732 /* Element 0 points to start of register name, and element 1
733 points to the start of the register value. */
283dc598 734 struct re_registers register_strings;
a706069f
SG
735
736 if (re_search (&register_pattern, buf, len, 0, len,
737 &register_strings) == -1)
738 break;
739
740 regnamelen = register_strings.end[1] - register_strings.start[1];
741 regname = buf + register_strings.start[1];
742 vallen = register_strings.end[2] - register_strings.start[2];
743 val = buf + register_strings.start[2];
744
745 current_monitor->supply_register (regname, regnamelen, val, vallen);
746
747 buf += register_strings.end[0];
748 len -= register_strings.end[0];
749 }
51d6a954
RS
750}
751
eba08643
C
752/* Send ^C to target to halt it. Target will respond, and send us a
753 packet. */
754
755static void
756monitor_interrupt (signo)
757 int signo;
758{
759 /* If this doesn't work, try more severe steps. */
760 signal (signo, monitor_interrupt_twice);
761
762 if (remote_debug)
763 printf_unfiltered ("monitor_interrupt called\n");
764
765 target_stop ();
766}
767
768/* The user typed ^C twice. */
769
770static void
771monitor_interrupt_twice (signo)
772 int signo;
773{
774 signal (signo, ofunc);
775
776 monitor_interrupt_query ();
777
778 signal (signo, monitor_interrupt);
779}
780
781/* Ask the user what to do when an interrupt is received. */
782
783static void
784monitor_interrupt_query ()
785{
786 target_terminal_ours ();
787
788 if (query ("Interrupted while waiting for the program.\n\
789Give up (and stop debugging it)? "))
790 {
791 target_mourn_inferior ();
792 return_to_top_level (RETURN_QUIT);
793 }
794
795 target_terminal_inferior ();
796}
797
798static void
799monitor_wait_cleanup (old_timeout)
800 int old_timeout;
801{
802 timeout = old_timeout;
803 signal (SIGINT, ofunc);
4a430794 804 in_monitor_wait = 0;
eba08643
C
805}
806
431b7d5f
SS
807/* Wait until the remote machine stops, then return, storing status in
808 status just as `wait' would. */
809
a706069f 810static int
51d6a954
RS
811monitor_wait (pid, status)
812 int pid;
813 struct target_waitstatus *status;
814{
815 int old_timeout = timeout;
774e5d7f 816 char buf[1024];
a706069f 817 int resp_len;
eba08643 818 struct cleanup *old_chain;
51d6a954
RS
819
820 status->kind = TARGET_WAITKIND_EXITED;
821 status->value.integer = 0;
822
eba08643
C
823 old_chain = make_cleanup (monitor_wait_cleanup, old_timeout);
824
4a430794
SS
825#ifdef MAINTENANCE_CMDS
826 in_monitor_wait = 1;
827 timeout = watchdog > 0 ? watchdog : -1;
828#else
1265e2d8 829 timeout = -1; /* Don't time out -- user program is running. */
4a430794 830#endif
51d6a954 831
eba08643
C
832 ofunc = (void (*)()) signal (SIGINT, monitor_interrupt);
833
a706069f
SG
834 do
835 {
774e5d7f 836 resp_len = monitor_expect_prompt (buf, sizeof (buf));
51d6a954 837
a706069f
SG
838 if (resp_len <= 0)
839 fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf);
840 }
841 while (resp_len < 0);
51d6a954 842
eba08643
C
843 signal (SIGINT, ofunc);
844
51d6a954
RS
845 timeout = old_timeout;
846
a706069f
SG
847 if (dump_reg_flag && current_monitor->dump_registers)
848 {
849 dump_reg_flag = 0;
850
774e5d7f
SS
851 monitor_printf (current_monitor->dump_registers);
852 resp_len = monitor_expect_prompt (buf, sizeof (buf));
a706069f
SG
853 }
854
855 if (current_monitor->register_pattern)
856 parse_register_dump (buf, resp_len);
857
858 status->kind = TARGET_WAITKIND_STOPPED;
859 status->value.sig = TARGET_SIGNAL_TRAP;
860
eba08643
C
861 discard_cleanups (old_chain);
862
4a430794
SS
863 in_monitor_wait = 0;
864
1265e2d8 865 return inferior_pid;
51d6a954
RS
866}
867
1265e2d8
SG
868/* Fetch register REGNO, or all registers if REGNO is -1. Returns
869 errno value. */
51d6a954 870
1265e2d8
SG
871static void
872monitor_fetch_register (regno)
873 int regno;
874{
1265e2d8 875 char *name;
774e5d7f 876 static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0};
eba08643
C
877 char regbuf[MAX_REGISTER_RAW_SIZE * 2 + 1];
878 int i;
51d6a954 879
f7ce02f4 880 name = current_monitor->regnames[regno];
f1ca4cbc 881
1265e2d8 882 if (!name)
774e5d7f
SS
883 {
884 supply_register (regno, zerobuf);
885 return;
886 }
51d6a954 887
5be86c56 888 /* send the register examine command */
431b7d5f 889
774e5d7f 890 monitor_printf (current_monitor->getreg.cmd, name);
1b552670 891
69159fad
SS
892 /* If RESP_DELIM is specified, we search for that as a leading
893 delimiter for the register value. Otherwise, we just start
894 searching from the start of the buf. */
eba08643
C
895
896 if (current_monitor->getreg.resp_delim)
897 monitor_expect (current_monitor->getreg.resp_delim, NULL, 0);
898
012be3ce
DP
899 /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set */
900 if (current_monitor->flags & MO_HEX_PREFIX)
901 {
902 int c;
903 c = readchar (timeout);
904 while (c == ' ')
905 c = readchar (timeout);
906 if ((c == '0') && ((c = readchar (timeout)) == 'x'))
907 ;
908 else
909 error ("Bad value returned from monitor while fetching register %x.",
910 regno);
911 }
912
5be86c56
JL
913 /* Read upto the maximum number of hex digits for this register, skipping
914 spaces, but stop reading if something else is seen. Some monitors
915 like to drop leading zeros. */
eba08643
C
916
917 for (i = 0; i < REGISTER_RAW_SIZE (regno) * 2; i++)
918 {
919 int c;
5be86c56
JL
920 c = readchar (timeout);
921 while (c == ' ')
922 c = readchar (timeout);
eba08643 923
5be86c56
JL
924 if (!isxdigit (c))
925 break;
eba08643
C
926
927 regbuf[i] = c;
928 }
929
930 regbuf[i] = '\000'; /* terminate the number */
931
69159fad
SS
932 /* If TERM is present, we wait for that to show up. Also, (if TERM
933 is present), we will send TERM_CMD if that is present. In any
934 case, we collect all of the output into buf, and then wait for
935 the normal prompt. */
1b552670 936
1265e2d8 937 if (current_monitor->getreg.term)
431b7d5f 938 {
eba08643 939 monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */
1265e2d8
SG
940
941 if (current_monitor->getreg.term_cmd)
431b7d5f 942 {
eba08643 943 monitor_printf (current_monitor->getreg.term_cmd);
774e5d7f 944 monitor_expect_prompt (NULL, 0);
431b7d5f
SS
945 }
946 }
947 else
eba08643 948 monitor_expect_prompt (NULL, 0); /* get response */
51d6a954 949
eba08643 950 monitor_supply_register (regno, regbuf);
51d6a954
RS
951}
952
1265e2d8 953/* Read the remote registers into the block regs. */
431b7d5f 954
45993f61
SC
955static void monitor_dump_regs ()
956{
957 if (current_monitor->dump_registers)
958 {
959 char buf[200];
960 int resp_len;
69159fad 961
45993f61
SC
962 monitor_printf (current_monitor->dump_registers);
963 resp_len = monitor_expect_prompt (buf, sizeof (buf));
964 parse_register_dump (buf, resp_len);
965 }
966 else
967 abort(); /* Need some way to read registers */
968}
969
a706069f 970static void
1265e2d8
SG
971monitor_fetch_registers (regno)
972 int regno;
51d6a954 973{
45993f61 974 if (current_monitor->getreg.cmd)
431b7d5f 975 {
45993f61
SC
976 if (regno >= 0)
977 {
978 monitor_fetch_register (regno);
979 return;
980 }
51d6a954 981
45993f61
SC
982 for (regno = 0; regno < NUM_REGS; regno++)
983 monitor_fetch_register (regno);
984 }
985 else {
986 monitor_dump_regs ();
987 }
51d6a954
RS
988}
989
431b7d5f
SS
990/* Store register REGNO, or all if REGNO == 0. Return errno value. */
991
1265e2d8 992static void
51d6a954
RS
993monitor_store_register (regno)
994 int regno;
995{
996 char *name;
69159fad 997 unsigned int val;
f1ca4cbc 998
f7ce02f4 999 name = current_monitor->regnames[regno];
1265e2d8
SG
1000 if (!name)
1001 return;
51d6a954 1002
1265e2d8 1003 val = read_register (regno);
51d6a954 1004
1265e2d8
SG
1005 /* send the register deposit command */
1006
774e5d7f 1007 monitor_printf (current_monitor->setreg.cmd, name, val);
1265e2d8 1008
69159fad
SS
1009/* It's possible that there are actually some monitors out there that
1010 will prompt you when you set a register. In that case, you may
1011 need to add some code here to deal with TERM and TERM_CMD (see
1012 monitor_fetch_register to get an idea of what's needed...) */
1265e2d8 1013
774e5d7f 1014 monitor_expect_prompt (NULL, 0);
1265e2d8
SG
1015}
1016
1017/* Store the remote registers. */
1018
a706069f 1019static void
1265e2d8
SG
1020monitor_store_registers (regno)
1021 int regno;
1022{
1023 if (regno >= 0)
431b7d5f 1024 {
1265e2d8
SG
1025 monitor_store_register (regno);
1026 return;
51d6a954 1027 }
431b7d5f 1028
1265e2d8
SG
1029 for (regno = 0; regno < NUM_REGS; regno++)
1030 monitor_store_register (regno);
51d6a954
RS
1031}
1032
1033/* Get ready to modify the registers array. On machines which store
1034 individual registers, this doesn't need to do anything. On machines
1035 which store all the registers in one fell swoop, this makes sure
1036 that registers contains all the registers from the program being
1037 debugged. */
1038
a706069f 1039static void
51d6a954
RS
1040monitor_prepare_to_store ()
1041{
1042 /* Do nothing, since we can store individual regs */
1043}
1044
a706069f
SG
1045static void
1046monitor_files_info (ops)
1047 struct target_ops *ops;
51d6a954 1048{
1265e2d8 1049 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate);
51d6a954
RS
1050}
1051
1265e2d8
SG
1052static int
1053monitor_write_memory (memaddr, myaddr, len)
51d6a954 1054 CORE_ADDR memaddr;
2b576293 1055 char *myaddr;
51d6a954
RS
1056 int len;
1057{
69159fad 1058 unsigned int val;
a706069f
SG
1059 char *cmd;
1060 int i;
1b552670 1061
a706069f 1062 /* Use memory fill command for leading 0 bytes. */
1b552670 1063
a706069f
SG
1064 if (current_monitor->fill)
1065 {
1066 for (i = 0; i < len; i++)
1067 if (myaddr[i] != 0)
1068 break;
1069
1070 if (i > 4) /* More than 4 zeros is worth doing */
1071 {
1072 if (current_monitor->flags & MO_FILL_USES_ADDR)
774e5d7f 1073 monitor_printf (current_monitor->fill, memaddr, memaddr + i, 0);
a706069f 1074 else
774e5d7f 1075 monitor_printf (current_monitor->fill, memaddr, i, 0);
a706069f 1076
774e5d7f 1077 monitor_expect_prompt (NULL, 0);
a706069f
SG
1078
1079 return i;
1080 }
1081 }
1082
1083 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll)
1084 {
1085 len = 8;
1086 cmd = current_monitor->setmem.cmdll;
1087 }
1088 else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl)
1089 {
1090 len = 4;
1091 cmd = current_monitor->setmem.cmdl;
1092 }
1093 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw)
1094 {
1095 len = 2;
1096 cmd = current_monitor->setmem.cmdw;
1097 }
1098 else
1099 {
1100 len = 1;
1101 cmd = current_monitor->setmem.cmdb;
1102 }
1103
1104 val = extract_unsigned_integer (myaddr, len);
1105
012be3ce
DP
1106 if (current_monitor->flags & MO_NO_ECHO_ON_SETMEM)
1107 monitor_printf_noecho (cmd, memaddr, val);
1108 else
1109 monitor_printf (cmd, memaddr, val);
1265e2d8 1110
774e5d7f 1111 monitor_expect_prompt (NULL, 0);
1265e2d8 1112
a706069f 1113 return len;
51d6a954
RS
1114}
1115
eba08643
C
1116/* This is an alternate form of monitor_read_memory which is used for monitors
1117 which can only read a single byte/word/etc. at a time. */
1118
1119static int
1120monitor_read_memory_single (memaddr, myaddr, len)
1121 CORE_ADDR memaddr;
2b576293 1122 char *myaddr;
eba08643
C
1123 int len;
1124{
69159fad
SS
1125 unsigned int val;
1126 char membuf[sizeof(int) * 2 + 1];
eba08643
C
1127 char *p;
1128 char *cmd;
1129 int i;
1130
1131 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll)
1132 {
1133 len = 8;
1134 cmd = current_monitor->getmem.cmdll;
1135 }
1136 else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl)
1137 {
1138 len = 4;
1139 cmd = current_monitor->getmem.cmdl;
1140 }
1141 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw)
1142 {
1143 len = 2;
1144 cmd = current_monitor->getmem.cmdw;
1145 }
1146 else
1147 {
1148 len = 1;
1149 cmd = current_monitor->getmem.cmdb;
1150 }
1151
1152/* Send the examine command. */
1153
1154 monitor_printf (cmd, memaddr);
1155
1156/* If RESP_DELIM is specified, we search for that as a leading delimiter for
1157 the register value. Otherwise, we just start searching from the start of
1158 the buf. */
1159
1160 if (current_monitor->getmem.resp_delim)
69159fad 1161 monitor_expect_regexp (&getmem_resp_delim_pattern, NULL, 0);
eba08643
C
1162
1163/* Now, read the appropriate number of hex digits for this loc, skipping
1164 spaces. */
1165
012be3ce
DP
1166 /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set */
1167 if (current_monitor->flags & MO_HEX_PREFIX)
1168 {
1169 int c;
1170 c = readchar (timeout);
1171 while (c == ' ')
1172 c = readchar (timeout);
1173 if ((c == '0') && ((c = readchar (timeout)) == 'x'))
1174 ;
1175 else
1176 error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.",
1177 memaddr, i, membuf, c);
1178 }
eba08643
C
1179 for (i = 0; i < len * 2; i++)
1180 {
1181 int c;
1182
1183 while (1)
1184 {
1185 c = readchar (timeout);
1186 if (isxdigit (c))
1187 break;
1188 if (c == ' ')
1189 continue;
1190
1191 error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.",
1192 memaddr, i, membuf, c);
1193 }
1194
1195 membuf[i] = c;
1196 }
1197
1198 membuf[i] = '\000'; /* terminate the number */
1199
1200/* If TERM is present, we wait for that to show up. Also, (if TERM is
1201 present), we will send TERM_CMD if that is present. In any case, we collect
1202 all of the output into buf, and then wait for the normal prompt. */
1203
1204 if (current_monitor->getmem.term)
1205 {
1206 monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */
1207
1208 if (current_monitor->getmem.term_cmd)
1209 {
1210 monitor_printf (current_monitor->getmem.term_cmd);
1211 monitor_expect_prompt (NULL, 0);
1212 }
1213 }
1214 else
1215 monitor_expect_prompt (NULL, 0); /* get response */
1216
1217 p = membuf;
1218 val = strtoul (membuf, &p, 16);
1219
1220 if (val == 0 && membuf == p)
1221 error ("monitor_read_memory_single (0x%x): bad value from monitor: %s.",
1222 memaddr, membuf);
1223
1224 /* supply register stores in target byte order, so swap here */
1225
1226 store_unsigned_integer (myaddr, len, val);
1227
1228 return len;
1229}
1230
1265e2d8
SG
1231/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
1232 at MEMADDR. Returns length moved. Currently, we only do one byte at a
1233 time. */
431b7d5f 1234
1265e2d8
SG
1235static int
1236monitor_read_memory (memaddr, myaddr, len)
51d6a954
RS
1237 CORE_ADDR memaddr;
1238 char *myaddr;
1239 int len;
1240{
69159fad 1241 unsigned int val;
1265e2d8 1242 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
774e5d7f 1243 char buf[512];
1265e2d8
SG
1244 char *p, *p1;
1245 char *name;
1246 int resp_len;
a706069f
SG
1247 int i;
1248
eba08643
C
1249 if (current_monitor->flags & MO_GETMEM_READ_SINGLE)
1250 return monitor_read_memory_single (memaddr, myaddr, len);
1251
a706069f 1252 len = min (len, 16);
1265e2d8 1253
774e5d7f
SS
1254/* See if xfer would cross a 16 byte boundary. If so, clip it. */
1255 if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0)
1256 len = ((memaddr + len) & ~0xf) - memaddr;
1257
1265e2d8
SG
1258 /* send the memory examine command */
1259
774e5d7f
SS
1260 if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE)
1261 monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len - 1);
1262 else
1263 monitor_printf (current_monitor->getmem.cmdb, memaddr, len);
1265e2d8
SG
1264
1265/* If TERM is present, we wait for that to show up. Also, (if TERM is
1266 present), we will send TERM_CMD if that is present. In any case, we collect
1267 all of the output into buf, and then wait for the normal prompt. */
1268
1269 if (current_monitor->getmem.term)
431b7d5f 1270 {
774e5d7f 1271 resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
1265e2d8
SG
1272
1273 if (resp_len <= 0)
8f078234
SG
1274 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
1275 memaddr, resp_len, buf);
1265e2d8
SG
1276
1277 if (current_monitor->getmem.term_cmd)
431b7d5f 1278 {
1265e2d8
SG
1279 SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd,
1280 strlen (current_monitor->getmem.term_cmd));
774e5d7f 1281 monitor_expect_prompt (NULL, 0);
1b552670 1282 }
51d6a954 1283 }
1265e2d8 1284 else
774e5d7f
SS
1285 resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */
1286
1287 p = buf;
1288
1265e2d8 1289 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
a706069f
SG
1290 the values. Otherwise, we just start searching from the start of the buf.
1291 */
1265e2d8
SG
1292
1293 if (current_monitor->getmem.resp_delim)
1294 {
283dc598
SG
1295 int retval, tmp;
1296 struct re_registers resp_strings;
1297
1298 tmp = strlen (p);
1299 retval = re_search (&getmem_resp_delim_pattern, p, tmp, 0, tmp,
1300 &resp_strings);
1301
1302 if (retval < 0)
1303 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1304 memaddr, resp_len, buf);
1305
1306 p += resp_strings.end[0];
1307#if 0
774e5d7f 1308 p = strstr (p, current_monitor->getmem.resp_delim);
1265e2d8 1309 if (!p)
8f078234
SG
1310 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1311 memaddr, resp_len, buf);
1265e2d8 1312 p += strlen (current_monitor->getmem.resp_delim);
283dc598 1313#endif
1265e2d8 1314 }
1265e2d8 1315
a706069f
SG
1316 for (i = len; i > 0; i--)
1317 {
774e5d7f
SS
1318 /* Skip non-hex chars, but bomb on end of string and newlines */
1319
1320 while (1)
1321 {
1322 if (isxdigit (*p))
1323 break;
1324 if (*p == '\000' || *p == '\n' || *p == '\r')
1325 error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr, resp_len, buf);
1326 p++;
1327 }
1328
a706069f 1329 val = strtoul (p, &p1, 16);
1265e2d8 1330
a706069f
SG
1331 if (val == 0 && p == p1)
1332 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr,
1333 resp_len, buf);
1265e2d8 1334
a706069f 1335 *myaddr++ = val;
774e5d7f
SS
1336
1337 if (i == 1)
1338 break;
1339
1340 p = p1;
a706069f 1341 }
1265e2d8 1342
a706069f 1343 return len;
51d6a954
RS
1344}
1345
a706069f 1346static int
1265e2d8 1347monitor_xfer_memory (memaddr, myaddr, len, write, target)
51d6a954
RS
1348 CORE_ADDR memaddr;
1349 char *myaddr;
1350 int len;
1351 int write;
1352 struct target_ops *target; /* ignored */
1353{
45993f61 1354 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, write);
51d6a954
RS
1355}
1356
a706069f
SG
1357static void
1358monitor_kill ()
51d6a954
RS
1359{
1360 return; /* ignore attempts to kill target system */
1361}
1362
a706069f
SG
1363/* All we actually do is set the PC to the start address of exec_bfd, and start
1364 the program at that point. */
1365
1366static void
1367monitor_create_inferior (exec_file, args, env)
1368 char *exec_file;
1369 char *args;
1370 char **env;
1371{
1372 if (args && (*args != '\000'))
1373 error ("Args are not supported by the monitor.");
1374
012be3ce 1375 first_time = 1;
a706069f
SG
1376 clear_proceed_status ();
1377 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1378}
1379
51d6a954
RS
1380/* Clean up when a program exits.
1381 The program actually lives on in the remote processor's RAM, and may be
1382 run again without a download. Don't leave it full of breakpoint
1383 instructions. */
1384
a706069f 1385static void
51d6a954
RS
1386monitor_mourn_inferior ()
1387{
8f078234 1388 unpush_target (targ_ops);
51d6a954
RS
1389 generic_mourn_inferior (); /* Do all the proper things now */
1390}
1391
1265e2d8 1392#define NUM_MONITOR_BREAKPOINTS 8
51d6a954 1393
1265e2d8 1394static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0};
51d6a954 1395
431b7d5f
SS
1396/* Tell the monitor to add a breakpoint. */
1397
a706069f 1398static int
51d6a954
RS
1399monitor_insert_breakpoint (addr, shadow)
1400 CORE_ADDR addr;
1401 char *shadow;
1402{
1403 int i;
1265e2d8 1404 static unsigned char break_insn[] = BREAKPOINT;
51d6a954 1405
1265e2d8 1406 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
431b7d5f
SS
1407 {
1408 if (breakaddr[i] == 0)
1409 {
1410 breakaddr[i] = addr;
1265e2d8 1411 monitor_read_memory (addr, shadow, sizeof (break_insn));
f7ce02f4 1412 monitor_printf (current_monitor->set_break, addr);
774e5d7f 1413 monitor_expect_prompt (NULL, 0);
431b7d5f
SS
1414 return 0;
1415 }
f1ca4cbc 1416 }
f1ca4cbc 1417
1265e2d8 1418 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS);
51d6a954
RS
1419}
1420
431b7d5f
SS
1421/* Tell the monitor to remove a breakpoint. */
1422
a706069f 1423static int
51d6a954
RS
1424monitor_remove_breakpoint (addr, shadow)
1425 CORE_ADDR addr;
1426 char *shadow;
1427{
1428 int i;
1429
1265e2d8 1430 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
431b7d5f
SS
1431 {
1432 if (breakaddr[i] == addr)
1433 {
1434 breakaddr[i] = 0;
1435 /* some monitors remove breakpoints based on the address */
a706069f 1436 if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR)
f7ce02f4 1437 monitor_printf (current_monitor->clr_break, addr);
431b7d5f 1438 else
f7ce02f4 1439 monitor_printf (current_monitor->clr_break, i);
774e5d7f 1440 monitor_expect_prompt (NULL, 0);
431b7d5f
SS
1441 return 0;
1442 }
7804e5bc 1443 }
1265e2d8 1444 fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
51d6a954
RS
1445 return 1;
1446}
1447
f7ce02f4
DP
1448/* monitor_wait_srec_ack -- wait for the target to send an acknowledgement for
1449 an S-record. Return non-zero if the ACK is received properly. */
1450
1451static int
1452monitor_wait_srec_ack ()
1453{
1454 /* FIXME: eventually we'll want to be able to handle acknowledgements
1455 of something other than a '+' character. Right now this is only
1456 going to work for EST visionICE. */
1457 return readchar (timeout) == '+';
1458}
1459
774e5d7f 1460/* monitor_load -- download a file. */
431b7d5f 1461
a706069f
SG
1462static void
1463monitor_load (file, from_tty)
21ed3dcd 1464 char *file;
a706069f 1465 int from_tty;
21ed3dcd 1466{
45993f61
SC
1467 dcache_flush (remote_dcache);
1468
774e5d7f
SS
1469 if (current_monitor->load_routine)
1470 current_monitor->load_routine (monitor_desc, file, hashmark);
1471 else
b9ab37f0 1472 { /* The default is ascii S-records */
f7ce02f4 1473 monitor_printf (current_monitor->load);
b9ab37f0
MM
1474 if (current_monitor->loadresp)
1475 monitor_expect (current_monitor->loadresp, NULL, 0);
1476
012be3ce 1477/* FIXME Should add arg here for load_offset (already done for generic_load) */
f7ce02f4
DP
1478 load_srec (monitor_desc, file, 32, SREC_ALL, hashmark,
1479 current_monitor->flags & MO_SREC_ACK ?
1480 monitor_wait_srec_ack : NULL);
b9ab37f0
MM
1481
1482 monitor_expect_prompt (NULL, 0);
1483 }
21ed3dcd 1484
a706069f 1485/* Finally, make the PC point at the start address */
431b7d5f 1486
45993f61
SC
1487 if (exec_bfd)
1488 write_pc (bfd_get_start_address (exec_bfd));
431b7d5f 1489
a706069f 1490 inferior_pid = 0; /* No process now */
51d6a954 1491
a706069f
SG
1492/* This is necessary because many things were based on the PC at the time that
1493 we attached to the monitor, which is no longer valid now that we have loaded
1494 new code (and just changed the PC). Another way to do this might be to call
1495 normal_stop, except that the stack may not be valid, and things would get
1496 horribly confused... */
51d6a954 1497
a706069f
SG
1498 clear_symtab_users ();
1499}
1500
1501static void
1502monitor_stop ()
1503{
69159fad
SS
1504 if ((current_monitor->flags & MO_SEND_BREAK_ON_STOP) != 0)
1505 SERIAL_SEND_BREAK (monitor_desc);
eba08643
C
1506 if (current_monitor->stop)
1507 monitor_printf_noecho (current_monitor->stop);
51d6a954
RS
1508}
1509
431b7d5f
SS
1510/* Put a command string, in args, out to MONITOR. Output from MONITOR
1511 is placed on the users terminal until the prompt is seen. FIXME: We
1512 read the characters ourseleves here cause of a nasty echo. */
1513
8f078234 1514static void
a706069f 1515monitor_command (args, from_tty)
431b7d5f 1516 char *args;
a706069f 1517 int from_tty;
51d6a954 1518{
7804e5bc 1519 char *p;
a706069f
SG
1520 int resp_len;
1521 char buf[1000];
431b7d5f 1522
51d6a954 1523 if (monitor_desc == NULL)
431b7d5f 1524 error ("monitor target not open.");
7804e5bc 1525
f7ce02f4 1526 p = current_monitor->prompt;
774e5d7f 1527
431b7d5f
SS
1528 /* Send the command. Note that if no args were supplied, then we're
1529 just sending the monitor a newline, which is sometimes useful. */
1530
774e5d7f 1531 monitor_printf ("%s\r", (args ? args : ""));
7804e5bc 1532
774e5d7f 1533 resp_len = monitor_expect_prompt (buf, sizeof buf);
a706069f
SG
1534
1535 fputs_unfiltered (buf, gdb_stdout); /* Output the response */
51d6a954
RS
1536}
1537
1b552670 1538/* Convert hex digit A to a number. */
431b7d5f 1539
1b552670
RS
1540static int
1541from_hex (a)
1542 int a;
1543{
1b552670
RS
1544 if (a >= '0' && a <= '9')
1545 return a - '0';
1546 if (a >= 'a' && a <= 'f')
1547 return a - 'a' + 10;
1548 if (a >= 'A' && a <= 'F')
1549 return a - 'A' + 10;
1b552670 1550
1265e2d8 1551 error ("Reply contains invalid hex digit 0x%x", a);
1b552670
RS
1552}
1553
774e5d7f
SS
1554static struct target_ops monitor_ops =
1555{
a706069f
SG
1556 NULL, /* to_shortname */
1557 NULL, /* to_longname */
1558 NULL, /* to_doc */
1559 NULL, /* to_open */
1560 monitor_close, /* to_close */
1561 NULL, /* to_attach */
1562 monitor_detach, /* to_detach */
1563 monitor_resume, /* to_resume */
1564 monitor_wait, /* to_wait */
1565 monitor_fetch_registers, /* to_fetch_registers */
1566 monitor_store_registers, /* to_store_registers */
1567 monitor_prepare_to_store, /* to_prepare_to_store */
1568 monitor_xfer_memory, /* to_xfer_memory */
1569 monitor_files_info, /* to_files_info */
1570 monitor_insert_breakpoint, /* to_insert_breakpoint */
1571 monitor_remove_breakpoint, /* to_remove_breakpoint */
1572 0, /* to_terminal_init */
1573 0, /* to_terminal_inferior */
1574 0, /* to_terminal_ours_for_output */
1575 0, /* to_terminal_ours */
1576 0, /* to_terminal_info */
1577 monitor_kill, /* to_kill */
1578 monitor_load, /* to_load */
1579 0, /* to_lookup_symbol */
1580 monitor_create_inferior, /* to_create_inferior */
1581 monitor_mourn_inferior, /* to_mourn_inferior */
1582 0, /* to_can_run */
1583 0, /* to_notice_signals */
2b576293 1584 0, /* to_thread_alive */
a706069f
SG
1585 monitor_stop, /* to_stop */
1586 process_stratum, /* to_stratum */
1587 0, /* to_next */
1588 1, /* to_has_all_memory */
1589 1, /* to_has_memory */
1590 1, /* to_has_stack */
1591 1, /* to_has_registers */
1592 1, /* to_has_execution */
1593 0, /* sections */
1594 0, /* sections_end */
1595 OPS_MAGIC /* to_magic */
1596};
1597
1598/* Init the target_ops structure pointed at by OPS */
1599
1600void
1601init_monitor_ops (ops)
1602 struct target_ops *ops;
1603{
1604 memcpy (ops, &monitor_ops, sizeof monitor_ops);
1605}
1606
431b7d5f
SS
1607/* Define additional commands that are usually only used by monitors. */
1608
51d6a954
RS
1609void
1610_initialize_remote_monitors ()
1611{
51d6a954
RS
1612 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1613 (char *)&hashmark,
1614 "Set display of activity while downloading a file.\n\
1265e2d8 1615When enabled, a hashmark \'#\' is displayed.",
51d6a954
RS
1616 &setlist),
1617 &showlist);
1618
51d6a954
RS
1619 add_com ("monitor", class_obscure, monitor_command,
1620 "Send a command to the debug monitor.");
1621}
This page took 0.19631 seconds and 4 git commands to generate.