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