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