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