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