Replace calls to abort() with calls to internal_error().
[deliverable/binutils-gdb.git] / gdb / monitor.c
CommitLineData
c906108c 1/* Remote debugging interface for boot monitors, for GDB.
29e57380 2 Copyright 1990-1993, 1995-1997, 1999-2000, 2001 Free Software Foundation, Inc.
c906108c
SS
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
4 Resurrected from the ashes by Stu Grossman.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
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
8e1a459b 26 into existence, and makes for quick porting.
c906108c
SS
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,
31 which in turn talks to the target board. */
32
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
40#include "defs.h"
41#include "gdbcore.h"
42#include "target.h"
c906108c
SS
43#include <signal.h>
44#include <ctype.h>
45#include "gdb_string.h"
46#include <sys/types.h>
47#include "command.h"
48#include "serial.h"
49#include "monitor.h"
50#include "gdbcmd.h"
51#include "inferior.h"
88987551 52#include "gdb_regex.h"
c906108c
SS
53#include "srec.h"
54
55static char *dev_name;
56static struct target_ops *targ_ops;
57
a14ed312 58static void monitor_vsprintf (char *sndbuf, char *pattern, va_list args);
c906108c 59
a14ed312 60static int readchar (int timeout);
c906108c 61
a14ed312
KB
62static void monitor_fetch_register (int regno);
63static void monitor_store_register (int regno);
c906108c 64
2df3850c
JM
65static void monitor_printable_string (char *newstr, char *oldstr, int len);
66static void monitor_error (char *function, char *message, CORE_ADDR memaddr, int len, char *string, int final_char);
a14ed312
KB
67static void monitor_detach (char *args, int from_tty);
68static void monitor_resume (int pid, int step, enum target_signal sig);
69static void monitor_interrupt (int signo);
70static void monitor_interrupt_twice (int signo);
71static void monitor_interrupt_query (void);
72static void monitor_wait_cleanup (void *old_timeout);
73
74static int monitor_wait (int pid, struct target_waitstatus *status);
75static void monitor_fetch_registers (int regno);
76static void monitor_store_registers (int regno);
77static void monitor_prepare_to_store (void);
78static int monitor_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
29e57380
C
79 int write,
80 struct mem_attrib *attrib,
81 struct target_ops *target);
a14ed312
KB
82static void monitor_files_info (struct target_ops *ops);
83static int monitor_insert_breakpoint (CORE_ADDR addr, char *shadow);
84static int monitor_remove_breakpoint (CORE_ADDR addr, char *shadow);
85static void monitor_kill (void);
86static void monitor_load (char *file, int from_tty);
87static void monitor_mourn_inferior (void);
88static void monitor_stop (void);
89
90static int monitor_read_memory (CORE_ADDR addr, char *myaddr, int len);
91static int monitor_write_memory (CORE_ADDR addr, char *myaddr, int len);
92static int monitor_write_memory_bytes (CORE_ADDR addr, char *myaddr, int len);
93static int monitor_write_memory_block (CORE_ADDR memaddr,
94 char *myaddr, int len);
95static int monitor_expect_regexp (struct re_pattern_buffer *pat,
96 char *buf, int buflen);
97static void monitor_dump_regs (void);
c906108c 98#if 0
a14ed312
KB
99static int from_hex (int a);
100static unsigned long get_hex_word (void);
c906108c 101#endif
a14ed312 102static void parse_register_dump (char *, int);
c906108c
SS
103
104static struct monitor_ops *current_monitor;
105
106static int hashmark; /* flag set by "set hash" */
107
108static int timeout = 30;
109
110static int in_monitor_wait = 0; /* Non-zero means we are in monitor_wait() */
111
c5aa993b 112static void (*ofunc) (); /* Old SIGINT signal handler */
c906108c 113
9e086581
JM
114static CORE_ADDR *breakaddr;
115
c906108c
SS
116/* Descriptor for I/O to remote machine. Initialize it to NULL so
117 that monitor_open knows that we don't have a file open when the
118 program starts. */
119
120static serial_t monitor_desc = NULL;
121
122/* Pointer to regexp pattern matching data */
123
124static struct re_pattern_buffer register_pattern;
125static char register_fastmap[256];
126
127static struct re_pattern_buffer getmem_resp_delim_pattern;
128static char getmem_resp_delim_fastmap[256];
129
130static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when
131 monitor_wait wakes up. */
132
c5aa993b
JM
133static int first_time = 0; /* is this the first time we're executing after
134 gaving created the child proccess? */
c906108c 135
d4f3574e
SS
136#define TARGET_BUF_SIZE 2048
137
2df3850c
JM
138/* Monitor specific debugging information. Typically only useful to
139 the developer of a new monitor interface. */
c906108c 140
2df3850c
JM
141static void monitor_debug (const char *fmt, ...) ATTR_FORMAT(printf, 1, 2);
142
143static int monitor_debug_p = 0;
144
145/* NOTE: This file alternates between monitor_debug_p and remote_debug
146 when determining if debug information is printed. Perhaphs this
147 could be simplified. */
148
149static void
150monitor_debug (const char *fmt, ...)
151{
152 if (monitor_debug_p)
153 {
154 va_list args;
155 va_start (args, fmt);
156 vfprintf_filtered (gdb_stdlog, fmt, args);
157 va_end (args);
158 }
159}
160
161
162/* Convert a string into a printable representation, Return # byte in
163 the new string. When LEN is >0 it specifies the size of the
164 string. Otherwize strlen(oldstr) is used. */
165
166static void
167monitor_printable_string (char *newstr, char *oldstr, int len)
c906108c 168{
c906108c 169 int ch;
2df3850c
JM
170 int i;
171
172 if (len <= 0)
173 len = strlen (oldstr);
c906108c 174
2df3850c 175 for (i = 0; i < len; i++)
c906108c 176 {
2df3850c 177 ch = oldstr[i];
c906108c 178 switch (ch)
c5aa993b 179 {
c906108c
SS
180 default:
181 if (isprint (ch))
182 *newstr++ = ch;
183
184 else
185 {
186 sprintf (newstr, "\\x%02x", ch & 0xff);
187 newstr += 4;
188 }
189 break;
190
c5aa993b
JM
191 case '\\':
192 *newstr++ = '\\';
193 *newstr++ = '\\';
194 break;
195 case '\b':
196 *newstr++ = '\\';
197 *newstr++ = 'b';
198 break;
199 case '\f':
200 *newstr++ = '\\';
201 *newstr++ = 't';
202 break;
203 case '\n':
204 *newstr++ = '\\';
205 *newstr++ = 'n';
206 break;
207 case '\r':
208 *newstr++ = '\\';
209 *newstr++ = 'r';
210 break;
211 case '\t':
212 *newstr++ = '\\';
213 *newstr++ = 't';
214 break;
215 case '\v':
216 *newstr++ = '\\';
217 *newstr++ = 'v';
218 break;
219 }
c906108c
SS
220 }
221
222 *newstr++ = '\0';
c906108c
SS
223}
224
225/* Print monitor errors with a string, converting the string to printable
226 representation. */
227
228static void
2df3850c
JM
229monitor_error (char *function, char *message,
230 CORE_ADDR memaddr, int len, char *string, int final_char)
c906108c 231{
c5aa993b 232 int real_len = (len == 0 && string != (char *) 0) ? strlen (string) : len;
c906108c 233 char *safe_string = alloca ((real_len * 4) + 1);
2df3850c 234 monitor_printable_string (safe_string, string, real_len);
c906108c
SS
235
236 if (final_char)
2df3850c 237 error ("%s (0x%s): %s: %s%c", function, paddr_nz (memaddr), message, safe_string, final_char);
c906108c 238 else
2df3850c 239 error ("%s (0x%s): %s: %s", function, paddr_nz (memaddr), message, safe_string);
c906108c
SS
240}
241
242/* Convert hex digit A to a number. */
243
244static int
fba45db2 245fromhex (int a)
c906108c
SS
246{
247 if (a >= '0' && a <= '9')
248 return a - '0';
249 else if (a >= 'a' && a <= 'f')
250 return a - 'a' + 10;
c5aa993b
JM
251 else if (a >= 'A' && a <= 'F')
252 return a - 'A' + 10;
c906108c 253 else
c5aa993b 254 error ("Invalid hex digit %d", a);
c906108c
SS
255}
256
257/* monitor_vsprintf - similar to vsprintf but handles 64-bit addresses
258
259 This function exists to get around the problem that many host platforms
260 don't have a printf that can print 64-bit addresses. The %A format
261 specification is recognized as a special case, and causes the argument
262 to be printed as a 64-bit hexadecimal address.
263
264 Only format specifiers of the form "[0-9]*[a-z]" are recognized.
265 If it is a '%s' format, the argument is a string; otherwise the
266 argument is assumed to be a long integer.
267
268 %% is also turned into a single %.
c5aa993b
JM
269 */
270
c906108c 271static void
fba45db2 272monitor_vsprintf (char *sndbuf, char *pattern, va_list args)
c906108c
SS
273{
274 char format[10];
275 char fmt;
276 char *p;
277 int i;
278 long arg_int;
279 CORE_ADDR arg_addr;
280 char *arg_string;
281
282 for (p = pattern; *p; p++)
283 {
284 if (*p == '%')
285 {
286 /* Copy the format specifier to a separate buffer. */
287 format[0] = *p++;
288 for (i = 1; *p >= '0' && *p <= '9' && i < (int) sizeof (format) - 2;
289 i++, p++)
290 format[i] = *p;
291 format[i] = fmt = *p;
c5aa993b 292 format[i + 1] = '\0';
c906108c
SS
293
294 /* Fetch the next argument and print it. */
295 switch (fmt)
296 {
297 case '%':
298 strcpy (sndbuf, "%");
299 break;
300 case 'A':
301 arg_addr = va_arg (args, CORE_ADDR);
302 strcpy (sndbuf, paddr_nz (arg_addr));
303 break;
304 case 's':
305 arg_string = va_arg (args, char *);
306 sprintf (sndbuf, format, arg_string);
307 break;
308 default:
309 arg_int = va_arg (args, long);
310 sprintf (sndbuf, format, arg_int);
311 break;
312 }
313 sndbuf += strlen (sndbuf);
314 }
315 else
316 *sndbuf++ = *p;
317 }
318 *sndbuf = '\0';
319}
320
321
322/* monitor_printf_noecho -- Send data to monitor, but don't expect an echo.
323 Works just like printf. */
324
325void
c5aa993b 326monitor_printf_noecho (char *pattern,...)
c906108c
SS
327{
328 va_list args;
329 char sndbuf[2000];
330 int len;
331
c906108c 332 va_start (args, pattern);
c906108c
SS
333
334 monitor_vsprintf (sndbuf, pattern, args);
335
336 len = strlen (sndbuf);
337 if (len + 1 > sizeof sndbuf)
e1e9e218 338 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c 339
2df3850c 340 if (monitor_debug_p)
c906108c
SS
341 {
342 char *safe_string = (char *) alloca ((strlen (sndbuf) * 4) + 1);
2df3850c
JM
343 monitor_printable_string (safe_string, sndbuf, 0);
344 fprintf_unfiltered (gdb_stdlog, "sent[%s]\n", safe_string);
c906108c 345 }
c5aa993b 346
c906108c
SS
347 monitor_write (sndbuf, len);
348}
349
350/* monitor_printf -- Send data to monitor and check the echo. Works just like
351 printf. */
352
353void
c5aa993b 354monitor_printf (char *pattern,...)
c906108c
SS
355{
356 va_list args;
357 char sndbuf[2000];
358 int len;
359
c906108c 360 va_start (args, pattern);
c906108c
SS
361
362 monitor_vsprintf (sndbuf, pattern, args);
363
364 len = strlen (sndbuf);
365 if (len + 1 > sizeof sndbuf)
e1e9e218 366 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c 367
2df3850c 368 if (monitor_debug_p)
c906108c
SS
369 {
370 char *safe_string = (char *) alloca ((len * 4) + 1);
2df3850c
JM
371 monitor_printable_string (safe_string, sndbuf, 0);
372 fprintf_unfiltered (gdb_stdlog, "sent[%s]\n", safe_string);
c906108c
SS
373 }
374
375 monitor_write (sndbuf, len);
376
377 /* We used to expect that the next immediate output was the characters we
378 just output, but sometimes some extra junk appeared before the characters
379 we expected, like an extra prompt, or a portmaster sending telnet negotiations.
380 So, just start searching for what we sent, and skip anything unknown. */
2df3850c
JM
381 monitor_debug ("ExpectEcho\n");
382 monitor_expect (sndbuf, (char *) 0, 0);
c906108c
SS
383}
384
385
386/* Write characters to the remote system. */
387
388void
fba45db2 389monitor_write (char *buf, int buflen)
c906108c 390{
c5aa993b 391 if (SERIAL_WRITE (monitor_desc, buf, buflen))
c906108c
SS
392 fprintf_unfiltered (gdb_stderr, "SERIAL_WRITE failed: %s\n",
393 safe_strerror (errno));
394}
395
396
397/* Read a binary character from the remote system, doing all the fancy
398 timeout stuff, but without interpreting the character in any way,
399 and without printing remote debug information. */
400
401int
fba45db2 402monitor_readchar (void)
c906108c
SS
403{
404 int c;
405 int looping;
406
407 do
408 {
409 looping = 0;
410 c = SERIAL_READCHAR (monitor_desc, timeout);
411
412 if (c >= 0)
c5aa993b 413 c &= 0xff; /* don't lose bit 7 */
c906108c
SS
414 }
415 while (looping);
416
417 if (c >= 0)
418 return c;
419
420 if (c == SERIAL_TIMEOUT)
c5aa993b 421 error ("Timeout reading from remote system.");
c906108c
SS
422
423 perror_with_name ("remote-monitor");
424}
425
426
427/* Read a character from the remote system, doing all the fancy
428 timeout stuff. */
429
430static int
fba45db2 431readchar (int timeout)
c906108c
SS
432{
433 int c;
c5aa993b
JM
434 static enum
435 {
436 last_random, last_nl, last_cr, last_crnl
437 }
438 state = last_random;
c906108c
SS
439 int looping;
440
441 do
442 {
443 looping = 0;
444 c = SERIAL_READCHAR (monitor_desc, timeout);
445
446 if (c >= 0)
447 {
448 c &= 0x7f;
c906108c
SS
449 /* This seems to interfere with proper function of the
450 input stream */
2df3850c 451 if (monitor_debug_p || remote_debug)
c906108c
SS
452 {
453 char buf[2];
454 buf[0] = c;
455 buf[1] = '\0';
456 puts_debug ("read -->", buf, "<--");
457 }
c5aa993b 458
c906108c
SS
459 }
460
461 /* Canonicialize \n\r combinations into one \r */
462 if ((current_monitor->flags & MO_HANDLE_NL) != 0)
463 {
464 if ((c == '\r' && state == last_nl)
465 || (c == '\n' && state == last_cr))
466 {
467 state = last_crnl;
468 looping = 1;
469 }
470 else if (c == '\r')
471 state = last_cr;
472 else if (c != '\n')
473 state = last_random;
474 else
475 {
476 state = last_nl;
477 c = '\r';
478 }
479 }
480 }
481 while (looping);
482
483 if (c >= 0)
484 return c;
485
486 if (c == SERIAL_TIMEOUT)
7a292a7a 487#if 0
c906108c
SS
488 /* I fail to see how detaching here can be useful */
489 if (in_monitor_wait) /* Watchdog went off */
490 {
491 target_mourn_inferior ();
492 error ("GDB serial timeout has expired. Target detached.\n");
493 }
494 else
495#endif
496 error ("Timeout reading from remote system.");
497
498 perror_with_name ("remote-monitor");
499}
500
501/* Scan input from the remote system, until STRING is found. If BUF is non-
502 zero, then collect input until we have collected either STRING or BUFLEN-1
503 chars. In either case we terminate BUF with a 0. If input overflows BUF
504 because STRING can't be found, return -1, else return number of chars in BUF
505 (minus the terminating NUL). Note that in the non-overflow case, STRING
506 will be at the end of BUF. */
507
508int
fba45db2 509monitor_expect (char *string, char *buf, int buflen)
c906108c
SS
510{
511 char *p = string;
512 int obuflen = buflen;
513 int c;
514 extern struct target_ops *targ_ops;
515
2df3850c 516 if (monitor_debug_p)
c906108c
SS
517 {
518 char *safe_string = (char *) alloca ((strlen (string) * 4) + 1);
2df3850c
JM
519 monitor_printable_string (safe_string, string, 0);
520 fprintf_unfiltered (gdb_stdlog, "MON Expecting '%s'\n", safe_string);
c906108c
SS
521 }
522
8edbea78 523 immediate_quit++;
c906108c
SS
524 while (1)
525 {
526 if (buf)
527 {
528 if (buflen < 2)
529 {
530 *buf = '\000';
8edbea78 531 immediate_quit--;
c906108c
SS
532 return -1;
533 }
534
535 c = readchar (timeout);
536 if (c == '\000')
537 continue;
538 *buf++ = c;
539 buflen--;
540 }
541 else
542 c = readchar (timeout);
543
544 /* Don't expect any ^C sent to be echoed */
c5aa993b 545
c906108c
SS
546 if (*p == '\003' || c == *p)
547 {
548 p++;
549 if (*p == '\0')
550 {
8edbea78 551 immediate_quit--;
c906108c
SS
552
553 if (buf)
554 {
555 *buf++ = '\000';
556 return obuflen - buflen;
557 }
558 else
559 return 0;
560 }
561 }
562 else if ((c == '\021' || c == '\023') &&
563 (STREQ (targ_ops->to_shortname, "m32r")
564 || STREQ (targ_ops->to_shortname, "mon2000")))
c5aa993b 565 { /* m32r monitor emits random DC1/DC3 chars */
c906108c
SS
566 continue;
567 }
568 else
569 {
a0b3c4fd
JM
570 /* We got a character that doesn't match the string. We need to
571 back up p, but how far? If we're looking for "..howdy" and the
572 monitor sends "...howdy"? There's certainly a match in there,
573 but when we receive the third ".", we won't find it if we just
574 restart the matching at the beginning of the string.
575
576 This is a Boyer-Moore kind of situation. We want to reset P to
577 the end of the longest prefix of STRING that is a suffix of
578 what we've read so far. In the example above, that would be
579 ".." --- the longest prefix of "..howdy" that is a suffix of
580 "...". This longest prefix could be the empty string, if C
581 is nowhere to be found in STRING.
582
583 If this longest prefix is not the empty string, it must contain
584 C, so let's search from the end of STRING for instances of C,
585 and see if the portion of STRING before that is a suffix of
586 what we read before C. Actually, we can search backwards from
587 p, since we know no prefix can be longer than that.
588
589 Note that we can use STRING itself, along with C, as a record
590 of what we've received so far. :) */
591 int i;
592
593 for (i = (p - string) - 1; i >= 0; i--)
594 if (string[i] == c)
595 {
596 /* Is this prefix a suffix of what we've read so far?
597 In other words, does
598 string[0 .. i-1] == string[p - i, p - 1]? */
599 if (! memcmp (string, p - i, i))
600 {
601 p = string + i + 1;
602 break;
603 }
604 }
605 if (i < 0)
606 p = string;
c906108c
SS
607 }
608 }
609}
610
611/* Search for a regexp. */
612
613static int
fba45db2 614monitor_expect_regexp (struct re_pattern_buffer *pat, char *buf, int buflen)
c906108c
SS
615{
616 char *mybuf;
617 char *p;
2df3850c 618 monitor_debug ("MON Expecting regexp\n");
c906108c
SS
619 if (buf)
620 mybuf = buf;
621 else
622 {
d4f3574e
SS
623 mybuf = alloca (TARGET_BUF_SIZE);
624 buflen = TARGET_BUF_SIZE;
c906108c
SS
625 }
626
627 p = mybuf;
628 while (1)
629 {
630 int retval;
631
632 if (p - mybuf >= buflen)
633 { /* Buffer about to overflow */
634
635/* On overflow, we copy the upper half of the buffer to the lower half. Not
636 great, but it usually works... */
637
638 memcpy (mybuf, mybuf + buflen / 2, buflen / 2);
639 p = mybuf + buflen / 2;
640 }
641
642 *p++ = readchar (timeout);
643
644 retval = re_search (pat, mybuf, p - mybuf, 0, p - mybuf, NULL);
645 if (retval >= 0)
646 return 1;
647 }
648}
649
650/* Keep discarding input until we see the MONITOR prompt.
651
652 The convention for dealing with the prompt is that you
653 o give your command
654 o *then* wait for the prompt.
655
656 Thus the last thing that a procedure does with the serial line will
657 be an monitor_expect_prompt(). Exception: monitor_resume does not
658 wait for the prompt, because the terminal is being handed over to
659 the inferior. However, the next thing which happens after that is
660 a monitor_wait which does wait for the prompt. Note that this
661 includes abnormal exit, e.g. error(). This is necessary to prevent
662 getting into states from which we can't recover. */
663
664int
fba45db2 665monitor_expect_prompt (char *buf, int buflen)
c906108c 666{
2df3850c
JM
667 monitor_debug ("MON Expecting prompt\n");
668 return monitor_expect (current_monitor->prompt, buf, buflen);
c906108c
SS
669}
670
671/* Get N 32-bit words from remote, each preceded by a space, and put
672 them in registers starting at REGNO. */
673
674#if 0
675static unsigned long
fba45db2 676get_hex_word (void)
c906108c
SS
677{
678 unsigned long val;
679 int i;
680 int ch;
681
682 do
683 ch = readchar (timeout);
c5aa993b 684 while (isspace (ch));
c906108c
SS
685
686 val = from_hex (ch);
687
688 for (i = 7; i >= 1; i--)
689 {
690 ch = readchar (timeout);
691 if (!isxdigit (ch))
692 break;
693 val = (val << 4) | from_hex (ch);
694 }
695
696 return val;
697}
698#endif
699
700static void
fba45db2
KB
701compile_pattern (char *pattern, struct re_pattern_buffer *compiled_pattern,
702 char *fastmap)
c906108c
SS
703{
704 int tmp;
705 const char *val;
706
707 compiled_pattern->fastmap = fastmap;
708
709 tmp = re_set_syntax (RE_SYNTAX_EMACS);
710 val = re_compile_pattern (pattern,
711 strlen (pattern),
712 compiled_pattern);
713 re_set_syntax (tmp);
714
715 if (val)
716 error ("compile_pattern: Can't compile pattern string `%s': %s!", pattern, val);
717
718 if (fastmap)
719 re_compile_fastmap (compiled_pattern);
720}
721
722/* Open a connection to a remote debugger. NAME is the filename used
723 for communication. */
724
725void
fba45db2 726monitor_open (char *args, struct monitor_ops *mon_ops, int from_tty)
c906108c
SS
727{
728 char *name;
729 char **p;
730
731 if (mon_ops->magic != MONITOR_OPS_MAGIC)
732 error ("Magic number of monitor_ops struct wrong.");
733
734 targ_ops = mon_ops->target;
735 name = targ_ops->to_shortname;
736
737 if (!args)
738 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
739`target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
740
741 target_preopen (from_tty);
742
743 /* Setup pattern for register dump */
744
745 if (mon_ops->register_pattern)
746 compile_pattern (mon_ops->register_pattern, &register_pattern,
747 register_fastmap);
748
749 if (mon_ops->getmem.resp_delim)
750 compile_pattern (mon_ops->getmem.resp_delim, &getmem_resp_delim_pattern,
751 getmem_resp_delim_fastmap);
752
753 unpush_target (targ_ops);
754
755 if (dev_name)
b8c9b27d 756 xfree (dev_name);
4fcf66da 757 dev_name = xstrdup (args);
c906108c
SS
758
759 monitor_desc = SERIAL_OPEN (dev_name);
760
761 if (!monitor_desc)
762 perror_with_name (dev_name);
763
764 if (baud_rate != -1)
765 {
766 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
767 {
768 SERIAL_CLOSE (monitor_desc);
769 perror_with_name (dev_name);
770 }
771 }
c5aa993b 772
c906108c
SS
773 SERIAL_RAW (monitor_desc);
774
775 SERIAL_FLUSH_INPUT (monitor_desc);
776
777 /* some systems only work with 2 stop bits */
778
779 SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits);
780
781 current_monitor = mon_ops;
782
783 /* See if we can wake up the monitor. First, try sending a stop sequence,
784 then send the init strings. Last, remove all breakpoints. */
785
786 if (current_monitor->stop)
787 {
788 monitor_stop ();
789 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
c5aa993b 790 {
2df3850c 791 monitor_debug ("EXP Open echo\n");
c5aa993b
JM
792 monitor_expect_prompt (NULL, 0);
793 }
c906108c
SS
794 }
795
796 /* wake up the monitor and see if it's alive */
797 for (p = mon_ops->init; *p != NULL; p++)
798 {
799 /* Some of the characters we send may not be echoed,
c5aa993b
JM
800 but we hope to get a prompt at the end of it all. */
801
c906108c 802 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
c5aa993b 803 monitor_printf (*p);
c906108c 804 else
c5aa993b 805 monitor_printf_noecho (*p);
c906108c
SS
806 monitor_expect_prompt (NULL, 0);
807 }
808
809 SERIAL_FLUSH_INPUT (monitor_desc);
810
9e086581
JM
811 /* Alloc breakpoints */
812 if (mon_ops->set_break != NULL)
813 {
814 if (mon_ops->num_breakpoints == 0)
815 mon_ops->num_breakpoints = 8;
816
817 breakaddr = (CORE_ADDR *) xmalloc (mon_ops->num_breakpoints * sizeof (CORE_ADDR));
818 memset (breakaddr, 0, mon_ops->num_breakpoints * sizeof (CORE_ADDR));
819 }
820
c906108c
SS
821 /* Remove all breakpoints */
822
823 if (mon_ops->clr_all_break)
824 {
825 monitor_printf (mon_ops->clr_all_break);
826 monitor_expect_prompt (NULL, 0);
827 }
828
829 if (from_tty)
830 printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name);
831
832 push_target (targ_ops);
833
834 inferior_pid = 42000; /* Make run command think we are busy... */
835
836 /* Give monitor_wait something to read */
837
838 monitor_printf (current_monitor->line_term);
839
c906108c
SS
840 start_remote ();
841}
842
843/* Close out all files and local state before this target loses
844 control. */
845
846void
fba45db2 847monitor_close (int quitting)
c906108c
SS
848{
849 if (monitor_desc)
850 SERIAL_CLOSE (monitor_desc);
9e086581
JM
851
852 /* Free breakpoint memory */
853 if (breakaddr != NULL)
854 {
b8c9b27d 855 xfree (breakaddr);
9e086581
JM
856 breakaddr = NULL;
857 }
858
c906108c
SS
859 monitor_desc = NULL;
860}
861
862/* Terminate the open connection to the remote debugger. Use this
863 when you want to detach and do something else with your gdb. */
864
865static void
fba45db2 866monitor_detach (char *args, int from_tty)
c906108c
SS
867{
868 pop_target (); /* calls monitor_close to do the real work */
869 if (from_tty)
870 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
871}
872
873/* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
874
875char *
fba45db2 876monitor_supply_register (int regno, char *valstr)
c906108c 877{
d4f3574e 878 ULONGEST val;
c906108c
SS
879 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
880 char *p;
881
4ce44c66 882 val = 0;
d4f3574e
SS
883 p = valstr;
884 while (p && *p != '\0')
885 {
886 if (*p == '\r' || *p == '\n')
887 {
888 while (*p != '\0')
889 p++;
890 break;
891 }
892 if (isspace (*p))
893 {
894 p++;
895 continue;
896 }
897 if (!isxdigit (*p) && *p != 'x')
898 {
899 break;
900 }
901
902 val <<= 4;
903 val += fromhex (*p++);
904 }
2df3850c 905 monitor_debug ("Supplying Register %d %s\n", regno, valstr);
c906108c 906
d4f3574e 907 if (*p != '\0')
c906108c
SS
908 error ("monitor_supply_register (%d): bad value from monitor: %s.",
909 regno, valstr);
910
911 /* supply register stores in target byte order, so swap here */
912
913 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
914
915 supply_register (regno, regbuf);
916
917 return p;
918}
919
920/* Tell the remote machine to resume. */
921
c906108c 922static void
fba45db2 923monitor_resume (int pid, int step, enum target_signal sig)
c906108c
SS
924{
925 /* Some monitors require a different command when starting a program */
2df3850c 926 monitor_debug ("MON resume\n");
c906108c
SS
927 if (current_monitor->flags & MO_RUN_FIRST_TIME && first_time == 1)
928 {
929 first_time = 0;
930 monitor_printf ("run\r");
931 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
c5aa993b 932 dump_reg_flag = 1;
c906108c
SS
933 return;
934 }
c906108c
SS
935 if (step)
936 monitor_printf (current_monitor->step);
937 else
938 {
939 if (current_monitor->continue_hook)
c5aa993b
JM
940 (*current_monitor->continue_hook) ();
941 else
942 monitor_printf (current_monitor->cont);
c906108c
SS
943 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
944 dump_reg_flag = 1;
945 }
946}
947
948/* Parse the output of a register dump command. A monitor specific
949 regexp is used to extract individual register descriptions of the
950 form REG=VAL. Each description is split up into a name and a value
951 string which are passed down to monitor specific code. */
952
953static void
fba45db2 954parse_register_dump (char *buf, int len)
c906108c 955{
2df3850c
JM
956 monitor_debug ("MON Parsing register dump\n");
957 while (1)
c906108c
SS
958 {
959 int regnamelen, vallen;
960 char *regname, *val;
961 /* Element 0 points to start of register name, and element 1
c5aa993b 962 points to the start of the register value. */
c906108c
SS
963 struct re_registers register_strings;
964
965 memset (&register_strings, 0, sizeof (struct re_registers));
966
967 if (re_search (&register_pattern, buf, len, 0, len,
968 &register_strings) == -1)
969 break;
970
971 regnamelen = register_strings.end[1] - register_strings.start[1];
972 regname = buf + register_strings.start[1];
973 vallen = register_strings.end[2] - register_strings.start[2];
974 val = buf + register_strings.start[2];
975
976 current_monitor->supply_register (regname, regnamelen, val, vallen);
977
978 buf += register_strings.end[0];
979 len -= register_strings.end[0];
980 }
981}
982
983/* Send ^C to target to halt it. Target will respond, and send us a
984 packet. */
985
986static void
fba45db2 987monitor_interrupt (int signo)
c906108c
SS
988{
989 /* If this doesn't work, try more severe steps. */
990 signal (signo, monitor_interrupt_twice);
c5aa993b 991
2df3850c
JM
992 if (monitor_debug_p || remote_debug)
993 fprintf_unfiltered (gdb_stdlog, "monitor_interrupt called\n");
c906108c
SS
994
995 target_stop ();
996}
997
998/* The user typed ^C twice. */
999
1000static void
fba45db2 1001monitor_interrupt_twice (int signo)
c906108c
SS
1002{
1003 signal (signo, ofunc);
c5aa993b 1004
c906108c
SS
1005 monitor_interrupt_query ();
1006
1007 signal (signo, monitor_interrupt);
1008}
1009
1010/* Ask the user what to do when an interrupt is received. */
1011
1012static void
fba45db2 1013monitor_interrupt_query (void)
c906108c
SS
1014{
1015 target_terminal_ours ();
1016
1017 if (query ("Interrupted while waiting for the program.\n\
1018Give up (and stop debugging it)? "))
1019 {
1020 target_mourn_inferior ();
1021 return_to_top_level (RETURN_QUIT);
1022 }
1023
1024 target_terminal_inferior ();
1025}
1026
1027static void
fba45db2 1028monitor_wait_cleanup (void *old_timeout)
c906108c 1029{
c5aa993b 1030 timeout = *(int *) old_timeout;
c906108c
SS
1031 signal (SIGINT, ofunc);
1032 in_monitor_wait = 0;
1033}
1034
1035
1036
c5aa993b
JM
1037void
1038monitor_wait_filter (char *buf,
1039 int bufmax,
1040 int *ext_resp_len,
1041 struct target_waitstatus *status
1042)
c906108c 1043{
c5aa993b 1044 int resp_len;
c906108c
SS
1045 do
1046 {
1047 resp_len = monitor_expect_prompt (buf, bufmax);
c5aa993b 1048 *ext_resp_len = resp_len;
c906108c
SS
1049
1050 if (resp_len <= 0)
1051 fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf);
1052 }
1053 while (resp_len < 0);
1054
1055 /* Print any output characters that were preceded by ^O. */
1056 /* FIXME - This would be great as a user settabgle flag */
2df3850c
JM
1057 if (monitor_debug_p || remote_debug
1058 || current_monitor->flags & MO_PRINT_PROGRAM_OUTPUT)
c906108c
SS
1059 {
1060 int i;
1061
1062 for (i = 0; i < resp_len - 1; i++)
1063 if (buf[i] == 0x0f)
1064 putchar_unfiltered (buf[++i]);
1065 }
1066}
1067
1068
1069
1070/* Wait until the remote machine stops, then return, storing status in
1071 status just as `wait' would. */
1072
1073static int
fba45db2 1074monitor_wait (int pid, struct target_waitstatus *status)
c906108c
SS
1075{
1076 int old_timeout = timeout;
d4f3574e 1077 char buf[TARGET_BUF_SIZE];
c906108c
SS
1078 int resp_len;
1079 struct cleanup *old_chain;
1080
1081 status->kind = TARGET_WAITKIND_EXITED;
1082 status->value.integer = 0;
1083
1084 old_chain = make_cleanup (monitor_wait_cleanup, &old_timeout);
2df3850c 1085 monitor_debug ("MON wait\n");
c906108c 1086
7a292a7a 1087#if 0
c5aa993b
JM
1088 /* This is somthing other than a maintenance command */
1089 in_monitor_wait = 1;
c906108c
SS
1090 timeout = watchdog > 0 ? watchdog : -1;
1091#else
2df3850c 1092 timeout = -1; /* Don't time out -- user program is running. */
c906108c
SS
1093#endif
1094
1095 ofunc = (void (*)()) signal (SIGINT, monitor_interrupt);
1096
1097 if (current_monitor->wait_filter)
c5aa993b
JM
1098 (*current_monitor->wait_filter) (buf, sizeof (buf), &resp_len, status);
1099 else
1100 monitor_wait_filter (buf, sizeof (buf), &resp_len, status);
1101
1102#if 0 /* Transferred to monitor wait filter */
c906108c
SS
1103 do
1104 {
1105 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1106
1107 if (resp_len <= 0)
1108 fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf);
1109 }
1110 while (resp_len < 0);
1111
1112 /* Print any output characters that were preceded by ^O. */
1113 /* FIXME - This would be great as a user settabgle flag */
2df3850c
JM
1114 if (monitor_debug_p || remote_debug
1115 || current_monitor->flags & MO_PRINT_PROGRAM_OUTPUT)
c906108c
SS
1116 {
1117 int i;
1118
1119 for (i = 0; i < resp_len - 1; i++)
1120 if (buf[i] == 0x0f)
1121 putchar_unfiltered (buf[++i]);
1122 }
c5aa993b 1123#endif
c906108c
SS
1124
1125 signal (SIGINT, ofunc);
1126
1127 timeout = old_timeout;
1128#if 0
1129 if (dump_reg_flag && current_monitor->dump_registers)
1130 {
1131 dump_reg_flag = 0;
1132 monitor_printf (current_monitor->dump_registers);
1133 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1134 }
1135
1136 if (current_monitor->register_pattern)
1137 parse_register_dump (buf, resp_len);
1138#else
2df3850c 1139 monitor_debug ("Wait fetching registers after stop\n");
c5aa993b
JM
1140 monitor_dump_regs ();
1141#endif
c906108c
SS
1142
1143 status->kind = TARGET_WAITKIND_STOPPED;
1144 status->value.sig = TARGET_SIGNAL_TRAP;
1145
1146 discard_cleanups (old_chain);
1147
1148 in_monitor_wait = 0;
1149
1150 return inferior_pid;
1151}
1152
1153/* Fetch register REGNO, or all registers if REGNO is -1. Returns
1154 errno value. */
1155
1156static void
fba45db2 1157monitor_fetch_register (int regno)
c906108c
SS
1158{
1159 char *name;
86110418
MS
1160 char *zerobuf;
1161 char *regbuf;
c906108c
SS
1162 int i;
1163
86110418
MS
1164 regbuf = alloca (MAX_REGISTER_RAW_SIZE * 2 + 1);
1165 zerobuf = alloca (MAX_REGISTER_RAW_SIZE);
1166 memset (zerobuf, 0, MAX_REGISTER_RAW_SIZE);
1167
c906108c 1168 name = current_monitor->regnames[regno];
2df3850c 1169 monitor_debug ("MON fetchreg %d '%s'\n", regno, name ? name : "(null name)");
c906108c 1170
2df3850c 1171 if (!name || (*name == '\0'))
7a292a7a 1172 {
2df3850c
JM
1173 monitor_debug ("No register known for %d\n", regno);
1174 supply_register (regno, zerobuf);
c906108c
SS
1175 return;
1176 }
1177
1178 /* send the register examine command */
1179
1180 monitor_printf (current_monitor->getreg.cmd, name);
1181
1182 /* If RESP_DELIM is specified, we search for that as a leading
1183 delimiter for the register value. Otherwise, we just start
1184 searching from the start of the buf. */
1185
1186 if (current_monitor->getreg.resp_delim)
1187 {
2df3850c
JM
1188 monitor_debug ("EXP getreg.resp_delim\n");
1189 monitor_expect (current_monitor->getreg.resp_delim, NULL, 0);
c906108c
SS
1190 /* Handle case of first 32 registers listed in pairs. */
1191 if (current_monitor->flags & MO_32_REGS_PAIRED
7a292a7a 1192 && (regno & 1) != 0 && regno < 32)
c5aa993b 1193 {
2df3850c 1194 monitor_debug ("EXP getreg.resp_delim\n");
c906108c
SS
1195 monitor_expect (current_monitor->getreg.resp_delim, NULL, 0);
1196 }
1197 }
1198
1199 /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set */
c5aa993b 1200 if (current_monitor->flags & MO_HEX_PREFIX)
c906108c
SS
1201 {
1202 int c;
1203 c = readchar (timeout);
1204 while (c == ' ')
1205 c = readchar (timeout);
1206 if ((c == '0') && ((c = readchar (timeout)) == 'x'))
1207 ;
1208 else
c5aa993b
JM
1209 error ("Bad value returned from monitor while fetching register %x.",
1210 regno);
c906108c
SS
1211 }
1212
1213 /* Read upto the maximum number of hex digits for this register, skipping
1214 spaces, but stop reading if something else is seen. Some monitors
1215 like to drop leading zeros. */
1216
1217 for (i = 0; i < REGISTER_RAW_SIZE (regno) * 2; i++)
1218 {
1219 int c;
1220 c = readchar (timeout);
1221 while (c == ' ')
1222 c = readchar (timeout);
1223
1224 if (!isxdigit (c))
1225 break;
1226
1227 regbuf[i] = c;
1228 }
1229
1230 regbuf[i] = '\000'; /* terminate the number */
2df3850c 1231 monitor_debug ("REGVAL '%s'\n", regbuf);
c906108c
SS
1232
1233 /* If TERM is present, we wait for that to show up. Also, (if TERM
1234 is present), we will send TERM_CMD if that is present. In any
1235 case, we collect all of the output into buf, and then wait for
1236 the normal prompt. */
1237
1238 if (current_monitor->getreg.term)
1239 {
2df3850c
JM
1240 monitor_debug ("EXP getreg.term\n");
1241 monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */
c906108c
SS
1242 }
1243
1244 if (current_monitor->getreg.term_cmd)
c5aa993b 1245 {
2df3850c
JM
1246 monitor_debug ("EMIT getreg.term.cmd\n");
1247 monitor_printf (current_monitor->getreg.term_cmd);
c906108c 1248 }
c5aa993b
JM
1249 if (!current_monitor->getreg.term || /* Already expected or */
1250 current_monitor->getreg.term_cmd) /* ack expected */
1251 monitor_expect_prompt (NULL, 0); /* get response */
c906108c
SS
1252
1253 monitor_supply_register (regno, regbuf);
1254}
1255
1256/* Sometimes, it takes several commands to dump the registers */
1257/* This is a primitive for use by variations of monitor interfaces in
1258 case they need to compose the operation.
c5aa993b
JM
1259 */
1260int
1261monitor_dump_reg_block (char *block_cmd)
c906108c 1262{
d4f3574e 1263 char buf[TARGET_BUF_SIZE];
c906108c
SS
1264 int resp_len;
1265 monitor_printf (block_cmd);
1266 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1267 parse_register_dump (buf, resp_len);
c5aa993b 1268 return 1;
c906108c
SS
1269}
1270
1271
1272/* Read the remote registers into the block regs. */
1273/* Call the specific function if it has been provided */
1274
1275static void
fba45db2 1276monitor_dump_regs (void)
c906108c 1277{
d4f3574e 1278 char buf[TARGET_BUF_SIZE];
c906108c
SS
1279 int resp_len;
1280 if (current_monitor->dumpregs)
c5aa993b
JM
1281 (*(current_monitor->dumpregs)) (); /* call supplied function */
1282 else if (current_monitor->dump_registers) /* default version */
1283 {
1284 monitor_printf (current_monitor->dump_registers);
c906108c
SS
1285 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1286 parse_register_dump (buf, resp_len);
1287 }
1288 else
e1e9e218 1289 internal_error (__FILE__, __LINE__, "failed internal consistency check"); /* Need some way to read registers */
c906108c
SS
1290}
1291
1292static void
fba45db2 1293monitor_fetch_registers (int regno)
c906108c 1294{
2df3850c 1295 monitor_debug ("MON fetchregs\n");
c5aa993b 1296 if (current_monitor->getreg.cmd)
c906108c
SS
1297 {
1298 if (regno >= 0)
1299 {
1300 monitor_fetch_register (regno);
1301 return;
1302 }
1303
1304 for (regno = 0; regno < NUM_REGS; regno++)
1305 monitor_fetch_register (regno);
1306 }
c5aa993b
JM
1307 else
1308 {
1309 monitor_dump_regs ();
1310 }
c906108c
SS
1311}
1312
1313/* Store register REGNO, or all if REGNO == 0. Return errno value. */
1314
1315static void
fba45db2 1316monitor_store_register (int regno)
c906108c
SS
1317{
1318 char *name;
d4f3574e 1319 ULONGEST val;
c906108c
SS
1320
1321 name = current_monitor->regnames[regno];
1322 if (!name || (*name == '\0'))
c5aa993b 1323 {
2df3850c
JM
1324 monitor_debug ("MON Cannot store unknown register\n");
1325 return;
c906108c
SS
1326 }
1327
1328 val = read_register (regno);
5683e87a
AC
1329 monitor_debug ("MON storeg %d %s\n", regno,
1330 phex (val, REGISTER_RAW_SIZE (regno)));
c906108c
SS
1331
1332 /* send the register deposit command */
1333
2df3850c 1334 if (current_monitor->flags & MO_REGISTER_VALUE_FIRST)
c906108c
SS
1335 monitor_printf (current_monitor->setreg.cmd, val, name);
1336 else if (current_monitor->flags & MO_SETREG_INTERACTIVE)
1337 monitor_printf (current_monitor->setreg.cmd, name);
1338 else
1339 monitor_printf (current_monitor->setreg.cmd, name, val);
1340
1341 if (current_monitor->setreg.term)
c5aa993b 1342 {
2df3850c
JM
1343 monitor_debug ("EXP setreg.term\n");
1344 monitor_expect (current_monitor->setreg.term, NULL, 0);
c906108c 1345 if (current_monitor->flags & MO_SETREG_INTERACTIVE)
2df3850c 1346 monitor_printf ("%s\r", paddr_nz (val));
c906108c
SS
1347 monitor_expect_prompt (NULL, 0);
1348 }
1349 else
1350 monitor_expect_prompt (NULL, 0);
c5aa993b
JM
1351 if (current_monitor->setreg.term_cmd) /* Mode exit required */
1352 {
2df3850c 1353 monitor_debug ("EXP setreg_termcmd\n");
c5aa993b
JM
1354 monitor_printf ("%s", current_monitor->setreg.term_cmd);
1355 monitor_expect_prompt (NULL, 0);
c906108c 1356 }
c5aa993b 1357} /* monitor_store_register */
c906108c
SS
1358
1359/* Store the remote registers. */
1360
1361static void
fba45db2 1362monitor_store_registers (int regno)
c906108c
SS
1363{
1364 if (regno >= 0)
1365 {
1366 monitor_store_register (regno);
1367 return;
1368 }
1369
1370 for (regno = 0; regno < NUM_REGS; regno++)
1371 monitor_store_register (regno);
1372}
1373
1374/* Get ready to modify the registers array. On machines which store
1375 individual registers, this doesn't need to do anything. On machines
1376 which store all the registers in one fell swoop, this makes sure
1377 that registers contains all the registers from the program being
1378 debugged. */
1379
1380static void
fba45db2 1381monitor_prepare_to_store (void)
c906108c
SS
1382{
1383 /* Do nothing, since we can store individual regs */
1384}
1385
1386static void
fba45db2 1387monitor_files_info (struct target_ops *ops)
c906108c
SS
1388{
1389 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate);
1390}
1391
1392static int
fba45db2 1393monitor_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 1394{
c5aa993b 1395 unsigned int val, hostval;
c906108c
SS
1396 char *cmd;
1397 int i;
1398
2df3850c 1399 monitor_debug ("MON write %d %s\n", len, paddr (memaddr));
c906108c 1400
2df3850c 1401 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
c906108c
SS
1402 memaddr = ADDR_BITS_REMOVE (memaddr);
1403
1404 /* Use memory fill command for leading 0 bytes. */
1405
1406 if (current_monitor->fill)
1407 {
1408 for (i = 0; i < len; i++)
1409 if (myaddr[i] != 0)
1410 break;
1411
1412 if (i > 4) /* More than 4 zeros is worth doing */
1413 {
2df3850c
JM
1414 monitor_debug ("MON FILL %d\n", i);
1415 if (current_monitor->flags & MO_FILL_USES_ADDR)
c5aa993b
JM
1416 monitor_printf (current_monitor->fill, memaddr, (memaddr + i) - 1, 0);
1417 else
1418 monitor_printf (current_monitor->fill, memaddr, i, 0);
c906108c
SS
1419
1420 monitor_expect_prompt (NULL, 0);
1421
1422 return i;
1423 }
1424 }
1425
1426#if 0
1427 /* Can't actually use long longs if VAL is an int (nice idea, though). */
1428 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll)
1429 {
1430 len = 8;
1431 cmd = current_monitor->setmem.cmdll;
1432 }
1433 else
1434#endif
1435 if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl)
1436 {
1437 len = 4;
1438 cmd = current_monitor->setmem.cmdl;
1439 }
1440 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw)
1441 {
1442 len = 2;
1443 cmd = current_monitor->setmem.cmdw;
1444 }
1445 else
1446 {
1447 len = 1;
1448 cmd = current_monitor->setmem.cmdb;
1449 }
1450
1451 val = extract_unsigned_integer (myaddr, len);
c5aa993b 1452
c906108c 1453 if (len == 4)
c5aa993b
JM
1454 {
1455 hostval = *(unsigned int *) myaddr;
2df3850c 1456 monitor_debug ("Hostval(%08x) val(%08x)\n", hostval, val);
c906108c
SS
1457 }
1458
1459
1460 if (current_monitor->flags & MO_NO_ECHO_ON_SETMEM)
1461 monitor_printf_noecho (cmd, memaddr, val);
1462 else if (current_monitor->flags & MO_SETMEM_INTERACTIVE)
1463 {
1464
1465 monitor_printf_noecho (cmd, memaddr);
1466
1467 if (current_monitor->setmem.term)
c5aa993b 1468 {
2df3850c 1469 monitor_debug ("EXP setmem.term");
c906108c
SS
1470 monitor_expect (current_monitor->setmem.term, NULL, 0);
1471 monitor_printf ("%x\r", val);
1472 }
1473 if (current_monitor->setmem.term_cmd)
c5aa993b
JM
1474 { /* Emit this to get out of the memory editing state */
1475 monitor_printf ("%s", current_monitor->setmem.term_cmd);
c906108c
SS
1476 /* Drop through to expecting a prompt */
1477 }
1478 }
1479 else
1480 monitor_printf (cmd, memaddr, val);
1481
1482 monitor_expect_prompt (NULL, 0);
1483
1484 return len;
1485}
1486
1487
1488static int
fba45db2 1489monitor_write_even_block (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 1490{
c5aa993b
JM
1491 unsigned int val;
1492 int written = 0;;
c906108c 1493 /* Enter the sub mode */
c5aa993b
JM
1494 monitor_printf (current_monitor->setmem.cmdl, memaddr);
1495 monitor_expect_prompt (NULL, 0);
1496
c906108c
SS
1497 while (len)
1498 {
c5aa993b
JM
1499 val = extract_unsigned_integer (myaddr, 4); /* REALLY */
1500 monitor_printf ("%x\r", val);
1501 myaddr += 4;
1502 memaddr += 4;
1503 written += 4;
2df3850c 1504 monitor_debug (" @ %s\n", paddr (memaddr));
c906108c 1505 /* If we wanted to, here we could validate the address */
2df3850c 1506 monitor_expect_prompt (NULL, 0);
c906108c
SS
1507 }
1508 /* Now exit the sub mode */
1509 monitor_printf (current_monitor->getreg.term_cmd);
c5aa993b
JM
1510 monitor_expect_prompt (NULL, 0);
1511 return written;
c906108c
SS
1512}
1513
1514
c5aa993b 1515static int
fba45db2 1516monitor_write_memory_bytes (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 1517{
c5aa993b
JM
1518 unsigned char val;
1519 int written = 0;
1520 if (len == 0)
1521 return 0;
c906108c 1522 /* Enter the sub mode */
c5aa993b
JM
1523 monitor_printf (current_monitor->setmem.cmdb, memaddr);
1524 monitor_expect_prompt (NULL, 0);
c906108c
SS
1525 while (len)
1526 {
c5aa993b
JM
1527 val = *myaddr;
1528 monitor_printf ("%x\r", val);
1529 myaddr++;
1530 memaddr++;
1531 written++;
c906108c 1532 /* If we wanted to, here we could validate the address */
c5aa993b
JM
1533 monitor_expect_prompt (NULL, 0);
1534 len--;
c906108c
SS
1535 }
1536 /* Now exit the sub mode */
1537 monitor_printf (current_monitor->getreg.term_cmd);
c5aa993b
JM
1538 monitor_expect_prompt (NULL, 0);
1539 return written;
c906108c
SS
1540}
1541
1542
1543static void
c5aa993b 1544longlongendswap (unsigned char *a)
c906108c 1545{
c5aa993b
JM
1546 int i, j;
1547 unsigned char x;
1548 i = 0;
1549 j = 7;
c906108c 1550 while (i < 4)
c5aa993b
JM
1551 {
1552 x = *(a + i);
1553 *(a + i) = *(a + j);
1554 *(a + j) = x;
1555 i++, j--;
c906108c
SS
1556 }
1557}
1558/* Format 32 chars of long long value, advance the pointer */
c5aa993b
JM
1559static char *hexlate = "0123456789abcdef";
1560static char *
1561longlong_hexchars (unsigned long long value,
1562 char *outbuff)
c906108c 1563{
c5aa993b
JM
1564 if (value == 0)
1565 {
1566 *outbuff++ = '0';
1567 return outbuff;
1568 }
c906108c 1569 else
c5aa993b
JM
1570 {
1571 static unsigned char disbuf[8]; /* disassembly buffer */
1572 unsigned char *scan, *limit; /* loop controls */
1573 unsigned char c, nib;
1574 int leadzero = 1;
1575 scan = disbuf;
1576 limit = scan + 8;
1577 {
1578 unsigned long long *dp;
1579 dp = (unsigned long long *) scan;
1580 *dp = value;
c906108c 1581 }
c5aa993b 1582 longlongendswap (disbuf); /* FIXME: ONly on big endian hosts */
c906108c 1583 while (scan < limit)
7a292a7a 1584 {
c5aa993b 1585 c = *scan++; /* a byte of our long long value */
c906108c 1586 if (leadzero)
7a292a7a
SS
1587 {
1588 if (c == 0)
1589 continue;
1590 else
c5aa993b 1591 leadzero = 0; /* henceforth we print even zeroes */
7a292a7a 1592 }
c5aa993b 1593 nib = c >> 4; /* high nibble bits */
7a292a7a 1594 *outbuff++ = hexlate[nib];
c5aa993b 1595 nib = c & 0x0f; /* low nibble bits */
7a292a7a 1596 *outbuff++ = hexlate[nib];
c906108c 1597 }
c5aa993b 1598 return outbuff;
c906108c 1599 }
c5aa993b 1600} /* longlong_hexchars */
c906108c
SS
1601
1602
1603
1604/* I am only going to call this when writing virtual byte streams.
1605 Which possably entails endian conversions
c5aa993b
JM
1606 */
1607static int
fba45db2 1608monitor_write_memory_longlongs (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 1609{
c5aa993b
JM
1610 static char hexstage[20]; /* At least 16 digits required, plus null */
1611 char *endstring;
1612 long long *llptr;
1613 long long value;
1614 int written = 0;
1615 llptr = (unsigned long long *) myaddr;
1616 if (len == 0)
1617 return 0;
1618 monitor_printf (current_monitor->setmem.cmdll, memaddr);
1619 monitor_expect_prompt (NULL, 0);
1620 while (len >= 8)
1621 {
1622 value = *llptr;
1623 endstring = longlong_hexchars (*llptr, hexstage);
1624 *endstring = '\0'; /* NUll terminate for printf */
1625 monitor_printf ("%s\r", hexstage);
1626 llptr++;
1627 memaddr += 8;
1628 written += 8;
c906108c 1629 /* If we wanted to, here we could validate the address */
c5aa993b
JM
1630 monitor_expect_prompt (NULL, 0);
1631 len -= 8;
c906108c
SS
1632 }
1633 /* Now exit the sub mode */
1634 monitor_printf (current_monitor->getreg.term_cmd);
c5aa993b
JM
1635 monitor_expect_prompt (NULL, 0);
1636 return written;
1637} /* */
c906108c
SS
1638
1639
1640
1641/* ----- MONITOR_WRITE_MEMORY_BLOCK ---------------------------- */
1642/* This is for the large blocks of memory which may occur in downloading.
1643 And for monitors which use interactive entry,
1644 And for monitors which do not have other downloading methods.
1645 Without this, we will end up calling monitor_write_memory many times
1646 and do the entry and exit of the sub mode many times
1647 This currently assumes...
c5aa993b
JM
1648 MO_SETMEM_INTERACTIVE
1649 ! MO_NO_ECHO_ON_SETMEM
1650 To use this, the you have to patch the monitor_cmds block with
1651 this function. Otherwise, its not tuned up for use by all
1652 monitor variations.
1653 */
c906108c 1654
c5aa993b 1655static int
fba45db2 1656monitor_write_memory_block (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 1657{
c5aa993b
JM
1658 int written;
1659 written = 0;
c906108c 1660 /* FIXME: This would be a good place to put the zero test */
c5aa993b 1661#if 1
c906108c 1662 if ((len > 8) && (((len & 0x07)) == 0) && current_monitor->setmem.cmdll)
c5aa993b
JM
1663 {
1664 return monitor_write_memory_longlongs (memaddr, myaddr, len);
1665 }
1666#endif
1667#if 0
c906108c
SS
1668 if (len > 4)
1669 {
c5aa993b
JM
1670 int sublen;
1671 written = monitor_write_even_block (memaddr, myaddr, len);
c906108c 1672 /* Adjust calling parameters by written amount */
c5aa993b
JM
1673 memaddr += written;
1674 myaddr += written;
1675 len -= written;
c906108c
SS
1676 }
1677#endif
c5aa993b
JM
1678 written = monitor_write_memory_bytes (memaddr, myaddr, len);
1679 return written;
c906108c
SS
1680}
1681
1682/* This is an alternate form of monitor_read_memory which is used for monitors
1683 which can only read a single byte/word/etc. at a time. */
1684
1685static int
fba45db2 1686monitor_read_memory_single (CORE_ADDR memaddr, char *myaddr, int len)
c906108c
SS
1687{
1688 unsigned int val;
c5aa993b 1689 char membuf[sizeof (int) * 2 + 1];
c906108c
SS
1690 char *p;
1691 char *cmd;
1692 int i;
1693
2df3850c 1694 monitor_debug ("MON read single\n");
c906108c
SS
1695#if 0
1696 /* Can't actually use long longs (nice idea, though). In fact, the
1697 call to strtoul below will fail if it tries to convert a value
1698 that's too big to fit in a long. */
1699 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll)
1700 {
1701 len = 8;
1702 cmd = current_monitor->getmem.cmdll;
1703 }
1704 else
1705#endif
1706 if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl)
1707 {
1708 len = 4;
1709 cmd = current_monitor->getmem.cmdl;
1710 }
1711 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw)
1712 {
1713 len = 2;
1714 cmd = current_monitor->getmem.cmdw;
1715 }
1716 else
1717 {
1718 len = 1;
1719 cmd = current_monitor->getmem.cmdb;
1720 }
1721
1722 /* Send the examine command. */
1723
1724 monitor_printf (cmd, memaddr);
1725
1726 /* If RESP_DELIM is specified, we search for that as a leading
1727 delimiter for the memory value. Otherwise, we just start
1728 searching from the start of the buf. */
1729
1730 if (current_monitor->getmem.resp_delim)
c5aa993b 1731 {
2df3850c 1732 monitor_debug ("EXP getmem.resp_delim\n");
c906108c
SS
1733 monitor_expect_regexp (&getmem_resp_delim_pattern, NULL, 0);
1734 }
1735
1736 /* Now, read the appropriate number of hex digits for this loc,
1737 skipping spaces. */
1738
1739 /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set. */
c5aa993b 1740 if (current_monitor->flags & MO_HEX_PREFIX)
c906108c
SS
1741 {
1742 int c;
1743
1744 c = readchar (timeout);
1745 while (c == ' ')
1746 c = readchar (timeout);
1747 if ((c == '0') && ((c = readchar (timeout)) == 'x'))
1748 ;
1749 else
2df3850c
JM
1750 monitor_error ("monitor_read_memory_single",
1751 "bad response from monitor",
c906108c
SS
1752 memaddr, i, membuf, c);
1753 }
1754 for (i = 0; i < len * 2; i++)
1755 {
1756 int c;
1757
1758 while (1)
1759 {
1760 c = readchar (timeout);
1761 if (isxdigit (c))
1762 break;
1763 if (c == ' ')
1764 continue;
1765
2df3850c
JM
1766 monitor_error ("monitor_read_memory_single",
1767 "bad response from monitor",
c906108c
SS
1768 memaddr, i, membuf, c);
1769 }
1770
1771 membuf[i] = c;
1772 }
1773
1774 membuf[i] = '\000'; /* terminate the number */
1775
1776/* If TERM is present, we wait for that to show up. Also, (if TERM is
1777 present), we will send TERM_CMD if that is present. In any case, we collect
1778 all of the output into buf, and then wait for the normal prompt. */
1779
1780 if (current_monitor->getmem.term)
1781 {
c5aa993b 1782 monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */
c906108c
SS
1783
1784 if (current_monitor->getmem.term_cmd)
1785 {
1786 monitor_printf (current_monitor->getmem.term_cmd);
1787 monitor_expect_prompt (NULL, 0);
1788 }
1789 }
1790 else
c5aa993b 1791 monitor_expect_prompt (NULL, 0); /* get response */
c906108c
SS
1792
1793 p = membuf;
1794 val = strtoul (membuf, &p, 16);
1795
1796 if (val == 0 && membuf == p)
2df3850c
JM
1797 monitor_error ("monitor_read_memory_single",
1798 "bad value from monitor",
c906108c
SS
1799 memaddr, 0, membuf, 0);
1800
1801 /* supply register stores in target byte order, so swap here */
1802
1803 store_unsigned_integer (myaddr, len, val);
1804
1805 return len;
1806}
1807
1808/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
1809 memory at MEMADDR. Returns length moved. Currently, we do no more
1810 than 16 bytes at a time. */
1811
1812static int
fba45db2 1813monitor_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c
SS
1814{
1815 unsigned int val;
1816 char buf[512];
1817 char *p, *p1;
1818 int resp_len;
1819 int i;
1820 CORE_ADDR dumpaddr;
1821
1822 if (len <= 0)
1823 {
2df3850c 1824 monitor_debug ("Zero length call to monitor_read_memory\n");
c906108c
SS
1825 return 0;
1826 }
1827
2df3850c
JM
1828 monitor_debug ("MON read block ta(%s) ha(%lx) %d\n",
1829 paddr_nz (memaddr), (long) myaddr, len);
c906108c
SS
1830
1831 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
1832 memaddr = ADDR_BITS_REMOVE (memaddr);
1833
1834 if (current_monitor->flags & MO_GETMEM_READ_SINGLE)
1835 return monitor_read_memory_single (memaddr, myaddr, len);
1836
1837 len = min (len, 16);
1838
1839 /* Some dumpers align the first data with the preceeding 16
1840 byte boundary. Some print blanks and start at the
1841 requested boundary. EXACT_DUMPADDR
c5aa993b 1842 */
c906108c
SS
1843
1844 dumpaddr = (current_monitor->flags & MO_EXACT_DUMPADDR)
c5aa993b 1845 ? memaddr : memaddr & ~0x0f;
c906108c
SS
1846
1847 /* See if xfer would cross a 16 byte boundary. If so, clip it. */
1848 if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0)
1849 len = ((memaddr + len) & ~0xf) - memaddr;
1850
1851 /* send the memory examine command */
1852
1853 if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE)
7a292a7a 1854 monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len);
c906108c
SS
1855 else if (current_monitor->flags & MO_GETMEM_16_BOUNDARY)
1856 monitor_printf (current_monitor->getmem.cmdb, dumpaddr);
1857 else
1858 monitor_printf (current_monitor->getmem.cmdb, memaddr, len);
1859
1860 /* If TERM is present, we wait for that to show up. Also, (if TERM
1861 is present), we will send TERM_CMD if that is present. In any
1862 case, we collect all of the output into buf, and then wait for
1863 the normal prompt. */
1864
1865 if (current_monitor->getmem.term)
1866 {
c5aa993b 1867 resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
c906108c
SS
1868
1869 if (resp_len <= 0)
2df3850c
JM
1870 monitor_error ("monitor_read_memory",
1871 "excessive response from monitor",
c906108c
SS
1872 memaddr, resp_len, buf, 0);
1873
1874 if (current_monitor->getmem.term_cmd)
1875 {
1876 SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd,
1877 strlen (current_monitor->getmem.term_cmd));
1878 monitor_expect_prompt (NULL, 0);
1879 }
1880 }
1881 else
c5aa993b 1882 resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */
c906108c
SS
1883
1884 p = buf;
1885
1886 /* If RESP_DELIM is specified, we search for that as a leading
1887 delimiter for the values. Otherwise, we just start searching
1888 from the start of the buf. */
1889
1890 if (current_monitor->getmem.resp_delim)
1891 {
1892 int retval, tmp;
1893 struct re_registers resp_strings;
2df3850c 1894 monitor_debug ("MON getmem.resp_delim %s\n", current_monitor->getmem.resp_delim);
c906108c
SS
1895
1896 memset (&resp_strings, 0, sizeof (struct re_registers));
1897 tmp = strlen (p);
1898 retval = re_search (&getmem_resp_delim_pattern, p, tmp, 0, tmp,
1899 &resp_strings);
1900
1901 if (retval < 0)
2df3850c
JM
1902 monitor_error ("monitor_read_memory",
1903 "bad response from monitor",
c906108c
SS
1904 memaddr, resp_len, buf, 0);
1905
1906 p += resp_strings.end[0];
1907#if 0
1908 p = strstr (p, current_monitor->getmem.resp_delim);
1909 if (!p)
2df3850c
JM
1910 monitor_error ("monitor_read_memory",
1911 "bad response from monitor",
c906108c
SS
1912 memaddr, resp_len, buf, 0);
1913 p += strlen (current_monitor->getmem.resp_delim);
1914#endif
1915 }
2df3850c 1916 monitor_debug ("MON scanning %d ,%lx '%s'\n", len, (long) p, p);
c906108c
SS
1917 if (current_monitor->flags & MO_GETMEM_16_BOUNDARY)
1918 {
c5aa993b
JM
1919 char c;
1920 int fetched = 0;
c906108c 1921 i = len;
c5aa993b 1922 c = *p;
c906108c 1923
c5aa993b
JM
1924
1925 while (!(c == '\000' || c == '\n' || c == '\r') && i > 0)
1926 {
1927 if (isxdigit (c))
1928 {
1929 if ((dumpaddr >= memaddr) && (i > 0))
1930 {
1931 val = fromhex (c) * 16 + fromhex (*(p + 1));
c906108c 1932 *myaddr++ = val;
2df3850c
JM
1933 if (monitor_debug_p || remote_debug)
1934 fprintf_unfiltered (gdb_stdlog, "[%02x]", val);
c906108c 1935 --i;
c5aa993b 1936 fetched++;
c906108c
SS
1937 }
1938 ++dumpaddr;
1939 ++p;
1940 }
c5aa993b
JM
1941 ++p; /* skip a blank or other non hex char */
1942 c = *p;
c906108c 1943 }
c5aa993b
JM
1944 if (fetched == 0)
1945 error ("Failed to read via monitor");
2df3850c
JM
1946 if (monitor_debug_p || remote_debug)
1947 fprintf_unfiltered (gdb_stdlog, "\n");
c5aa993b 1948 return fetched; /* Return the number of bytes actually read */
c906108c 1949 }
2df3850c 1950 monitor_debug ("MON scanning bytes\n");
c906108c
SS
1951
1952 for (i = len; i > 0; i--)
1953 {
1954 /* Skip non-hex chars, but bomb on end of string and newlines */
1955
1956 while (1)
1957 {
1958 if (isxdigit (*p))
1959 break;
1960
1961 if (*p == '\000' || *p == '\n' || *p == '\r')
2df3850c
JM
1962 monitor_error ("monitor_read_memory",
1963 "badly terminated response from monitor",
c906108c
SS
1964 memaddr, resp_len, buf, 0);
1965 p++;
1966 }
1967
1968 val = strtoul (p, &p1, 16);
1969
1970 if (val == 0 && p == p1)
2df3850c
JM
1971 monitor_error ("monitor_read_memory",
1972 "bad value from monitor",
c906108c
SS
1973 memaddr, resp_len, buf, 0);
1974
1975 *myaddr++ = val;
1976
1977 if (i == 1)
1978 break;
1979
1980 p = p1;
1981 }
1982
1983 return len;
1984}
1985
0e7e8d51
KB
1986/* Transfer LEN bytes between target address MEMADDR and GDB address
1987 MYADDR. Returns 0 for success, errno code for failure. TARGET is
1988 unused. */
1989
c906108c 1990static int
0e7e8d51 1991monitor_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
29e57380
C
1992 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
1993 struct target_ops *target ATTRIBUTE_UNUSED)
c906108c 1994{
4930751a
C
1995 int res;
1996
1997 if (write)
1998 {
1999 if (current_monitor->flags & MO_HAS_BLOCKWRITES)
2000 res = monitor_write_memory_block(memaddr, myaddr, len);
2001 else
2002 res = monitor_write_memory(memaddr, myaddr, len);
2003 }
2004 else
2005 {
2006 res = monitor_read_memory(memaddr, myaddr, len);
2007 }
2008
2009 return res;
c906108c
SS
2010}
2011
2012static void
fba45db2 2013monitor_kill (void)
c906108c 2014{
c5aa993b 2015 return; /* ignore attempts to kill target system */
c906108c
SS
2016}
2017
2018/* All we actually do is set the PC to the start address of exec_bfd, and start
2019 the program at that point. */
2020
2021static void
fba45db2 2022monitor_create_inferior (char *exec_file, char *args, char **env)
c906108c
SS
2023{
2024 if (args && (*args != '\000'))
2025 error ("Args are not supported by the monitor.");
2026
2027 first_time = 1;
2028 clear_proceed_status ();
2029 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
2030}
2031
2032/* Clean up when a program exits.
2033 The program actually lives on in the remote processor's RAM, and may be
2034 run again without a download. Don't leave it full of breakpoint
2035 instructions. */
2036
2037static void
fba45db2 2038monitor_mourn_inferior (void)
c906108c
SS
2039{
2040 unpush_target (targ_ops);
2041 generic_mourn_inferior (); /* Do all the proper things now */
2042}
2043
c906108c
SS
2044/* Tell the monitor to add a breakpoint. */
2045
2046static int
fba45db2 2047monitor_insert_breakpoint (CORE_ADDR addr, char *shadow)
c906108c
SS
2048{
2049 int i;
2050 unsigned char *bp;
2051 int bplen;
2052
2df3850c
JM
2053 monitor_debug ("MON inst bkpt %s\n", paddr (addr));
2054 if (current_monitor->set_break == NULL)
c906108c
SS
2055 error ("No set_break defined for this monitor");
2056
2057 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
2058 addr = ADDR_BITS_REMOVE (addr);
2059
2060 /* Determine appropriate breakpoint size for this address. */
2061 bp = memory_breakpoint_from_pc (&addr, &bplen);
2062
9e086581 2063 for (i = 0; i < current_monitor->num_breakpoints; i++)
c906108c
SS
2064 {
2065 if (breakaddr[i] == 0)
2066 {
2067 breakaddr[i] = addr;
2068 monitor_read_memory (addr, shadow, bplen);
2069 monitor_printf (current_monitor->set_break, addr);
2070 monitor_expect_prompt (NULL, 0);
2071 return 0;
2072 }
2073 }
2074
9e086581 2075 error ("Too many breakpoints (> %d) for monitor.", current_monitor->num_breakpoints);
c906108c
SS
2076}
2077
2078/* Tell the monitor to remove a breakpoint. */
2079
2080static int
fba45db2 2081monitor_remove_breakpoint (CORE_ADDR addr, char *shadow)
c906108c
SS
2082{
2083 int i;
2084
2df3850c
JM
2085 monitor_debug ("MON rmbkpt %s\n", paddr (addr));
2086 if (current_monitor->clr_break == NULL)
c906108c
SS
2087 error ("No clr_break defined for this monitor");
2088
2089 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
2090 addr = ADDR_BITS_REMOVE (addr);
2091
9e086581 2092 for (i = 0; i < current_monitor->num_breakpoints; i++)
c906108c
SS
2093 {
2094 if (breakaddr[i] == addr)
2095 {
2096 breakaddr[i] = 0;
2097 /* some monitors remove breakpoints based on the address */
2098 if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR)
2099 monitor_printf (current_monitor->clr_break, addr);
2100 else if (current_monitor->flags & MO_CLR_BREAK_1_BASED)
2101 monitor_printf (current_monitor->clr_break, i + 1);
2102 else
2103 monitor_printf (current_monitor->clr_break, i);
2104 monitor_expect_prompt (NULL, 0);
2105 return 0;
2106 }
2107 }
2108 fprintf_unfiltered (gdb_stderr,
2df3850c
JM
2109 "Can't find breakpoint associated with 0x%s\n",
2110 paddr_nz (addr));
c906108c
SS
2111 return 1;
2112}
2113
2114/* monitor_wait_srec_ack -- wait for the target to send an acknowledgement for
2115 an S-record. Return non-zero if the ACK is received properly. */
2116
2117static int
fba45db2 2118monitor_wait_srec_ack (void)
c906108c 2119{
d4f3574e 2120 int ch;
c906108c
SS
2121
2122 if (current_monitor->flags & MO_SREC_ACK_PLUS)
2123 {
2124 return (readchar (timeout) == '+');
2125 }
2126 else if (current_monitor->flags & MO_SREC_ACK_ROTATE)
2127 {
2128 /* Eat two backspaces, a "rotating" char (|/-\), and a space. */
2129 if ((ch = readchar (1)) < 0)
2130 return 0;
2131 if ((ch = readchar (1)) < 0)
2132 return 0;
2133 if ((ch = readchar (1)) < 0)
2134 return 0;
2135 if ((ch = readchar (1)) < 0)
2136 return 0;
2137 }
2138 return 1;
2139}
2140
2141/* monitor_load -- download a file. */
2142
2143static void
fba45db2 2144monitor_load (char *file, int from_tty)
c906108c 2145{
2df3850c 2146 monitor_debug ("MON load\n");
c906108c 2147
2df3850c 2148 if (current_monitor->load_routine)
c906108c
SS
2149 current_monitor->load_routine (monitor_desc, file, hashmark);
2150 else
2151 { /* The default is ascii S-records */
2152 int n;
2153 unsigned long load_offset;
2154 char buf[128];
2155
2156 /* enable user to specify address for downloading as 2nd arg to load */
2157 n = sscanf (file, "%s 0x%lx", buf, &load_offset);
2158 if (n > 1)
2159 file = buf;
2160 else
2161 load_offset = 0;
2162
2163 monitor_printf (current_monitor->load);
2164 if (current_monitor->loadresp)
2165 monitor_expect (current_monitor->loadresp, NULL, 0);
2166
2167 load_srec (monitor_desc, file, (bfd_vma) load_offset,
2168 32, SREC_ALL, hashmark,
2169 current_monitor->flags & MO_SREC_ACK ?
c5aa993b 2170 monitor_wait_srec_ack : NULL);
c906108c
SS
2171
2172 monitor_expect_prompt (NULL, 0);
2173 }
2174
2175/* Finally, make the PC point at the start address */
2176
2177 if (exec_bfd)
2178 write_pc (bfd_get_start_address (exec_bfd));
2179
2180 inferior_pid = 0; /* No process now */
2181
2182/* This is necessary because many things were based on the PC at the time that
2183 we attached to the monitor, which is no longer valid now that we have loaded
2184 new code (and just changed the PC). Another way to do this might be to call
2185 normal_stop, except that the stack may not be valid, and things would get
2186 horribly confused... */
2187
2188 clear_symtab_users ();
2189}
2190
2191static void
fba45db2 2192monitor_stop (void)
c906108c 2193{
2df3850c 2194 monitor_debug ("MON stop\n");
c906108c
SS
2195 if ((current_monitor->flags & MO_SEND_BREAK_ON_STOP) != 0)
2196 SERIAL_SEND_BREAK (monitor_desc);
2197 if (current_monitor->stop)
2198 monitor_printf_noecho (current_monitor->stop);
2199}
2200
96baa820
JM
2201/* Put a COMMAND string out to MONITOR. Output from MONITOR is placed
2202 in OUTPUT until the prompt is seen. FIXME: We read the characters
2203 ourseleves here cause of a nasty echo. */
c906108c
SS
2204
2205static void
96baa820 2206monitor_rcmd (char *command,
d9fcf2fb 2207 struct ui_file *outbuf)
c906108c
SS
2208{
2209 char *p;
2210 int resp_len;
2211 char buf[1000];
2212
2213 if (monitor_desc == NULL)
2214 error ("monitor target not open.");
2215
2216 p = current_monitor->prompt;
2217
2218 /* Send the command. Note that if no args were supplied, then we're
2219 just sending the monitor a newline, which is sometimes useful. */
2220
96baa820 2221 monitor_printf ("%s\r", (command ? command : ""));
c906108c
SS
2222
2223 resp_len = monitor_expect_prompt (buf, sizeof buf);
2224
96baa820 2225 fputs_unfiltered (buf, outbuf); /* Output the response */
c906108c
SS
2226}
2227
2228/* Convert hex digit A to a number. */
2229
2230#if 0
2231static int
fba45db2 2232from_hex (int a)
c5aa993b 2233{
c906108c
SS
2234 if (a >= '0' && a <= '9')
2235 return a - '0';
2236 if (a >= 'a' && a <= 'f')
2237 return a - 'a' + 10;
2238 if (a >= 'A' && a <= 'F')
2239 return a - 'A' + 10;
2240
2241 error ("Reply contains invalid hex digit 0x%x", a);
2242}
2243#endif
2244
2245char *
fba45db2 2246monitor_get_dev_name (void)
c906108c
SS
2247{
2248 return dev_name;
2249}
2250
2251static struct target_ops monitor_ops;
2252
2253static void
2254init_base_monitor_ops (void)
2255{
2256 monitor_ops.to_shortname = NULL;
2257 monitor_ops.to_longname = NULL;
2258 monitor_ops.to_doc = NULL;
2259 monitor_ops.to_open = NULL;
2260 monitor_ops.to_close = monitor_close;
2261 monitor_ops.to_attach = NULL;
2262 monitor_ops.to_post_attach = NULL;
2263 monitor_ops.to_require_attach = NULL;
2264 monitor_ops.to_detach = monitor_detach;
2265 monitor_ops.to_require_detach = NULL;
2266 monitor_ops.to_resume = monitor_resume;
2267 monitor_ops.to_wait = monitor_wait;
2268 monitor_ops.to_post_wait = NULL;
2269 monitor_ops.to_fetch_registers = monitor_fetch_registers;
2270 monitor_ops.to_store_registers = monitor_store_registers;
2271 monitor_ops.to_prepare_to_store = monitor_prepare_to_store;
2272 monitor_ops.to_xfer_memory = monitor_xfer_memory;
2273 monitor_ops.to_files_info = monitor_files_info;
2274 monitor_ops.to_insert_breakpoint = monitor_insert_breakpoint;
2275 monitor_ops.to_remove_breakpoint = monitor_remove_breakpoint;
2276 monitor_ops.to_terminal_init = 0;
2277 monitor_ops.to_terminal_inferior = 0;
2278 monitor_ops.to_terminal_ours_for_output = 0;
2279 monitor_ops.to_terminal_ours = 0;
2280 monitor_ops.to_terminal_info = 0;
2281 monitor_ops.to_kill = monitor_kill;
2282 monitor_ops.to_load = monitor_load;
2283 monitor_ops.to_lookup_symbol = 0;
2284 monitor_ops.to_create_inferior = monitor_create_inferior;
2285 monitor_ops.to_post_startup_inferior = NULL;
2286 monitor_ops.to_acknowledge_created_inferior = NULL;
2287 monitor_ops.to_clone_and_follow_inferior = NULL;
2288 monitor_ops.to_post_follow_inferior_by_clone = NULL;
2289 monitor_ops.to_insert_fork_catchpoint = NULL;
2290 monitor_ops.to_remove_fork_catchpoint = NULL;
2291 monitor_ops.to_insert_vfork_catchpoint = NULL;
2292 monitor_ops.to_remove_vfork_catchpoint = NULL;
2293 monitor_ops.to_has_forked = NULL;
2294 monitor_ops.to_has_vforked = NULL;
2295 monitor_ops.to_can_follow_vfork_prior_to_exec = NULL;
2296 monitor_ops.to_post_follow_vfork = NULL;
2297 monitor_ops.to_insert_exec_catchpoint = NULL;
2298 monitor_ops.to_remove_exec_catchpoint = NULL;
2299 monitor_ops.to_has_execd = NULL;
2300 monitor_ops.to_reported_exec_events_per_exec_call = NULL;
2301 monitor_ops.to_has_exited = NULL;
2302 monitor_ops.to_mourn_inferior = monitor_mourn_inferior;
2303 monitor_ops.to_can_run = 0;
2304 monitor_ops.to_notice_signals = 0;
2305 monitor_ops.to_thread_alive = 0;
2306 monitor_ops.to_stop = monitor_stop;
96baa820 2307 monitor_ops.to_rcmd = monitor_rcmd;
c906108c
SS
2308 monitor_ops.to_pid_to_exec_file = NULL;
2309 monitor_ops.to_core_file_to_sym_file = NULL;
2310 monitor_ops.to_stratum = process_stratum;
2311 monitor_ops.DONT_USE = 0;
2312 monitor_ops.to_has_all_memory = 1;
2313 monitor_ops.to_has_memory = 1;
2314 monitor_ops.to_has_stack = 1;
2315 monitor_ops.to_has_registers = 1;
2316 monitor_ops.to_has_execution = 1;
2317 monitor_ops.to_sections = 0;
2318 monitor_ops.to_sections_end = 0;
2319 monitor_ops.to_magic = OPS_MAGIC;
c5aa993b 2320} /* init_base_monitor_ops */
c906108c
SS
2321
2322/* Init the target_ops structure pointed at by OPS */
2323
2324void
fba45db2 2325init_monitor_ops (struct target_ops *ops)
c906108c
SS
2326{
2327 if (monitor_ops.to_magic != OPS_MAGIC)
2328 init_base_monitor_ops ();
2329
2330 memcpy (ops, &monitor_ops, sizeof monitor_ops);
2331}
2332
2333/* Define additional commands that are usually only used by monitors. */
2334
2335void
fba45db2 2336_initialize_remote_monitors (void)
c906108c
SS
2337{
2338 init_base_monitor_ops ();
2339 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
c5aa993b 2340 (char *) &hashmark,
c906108c
SS
2341 "Set display of activity while downloading a file.\n\
2342When enabled, a hashmark \'#\' is displayed.",
c5aa993b 2343 &setlist),
c906108c 2344 &showlist);
2df3850c 2345
2df3850c 2346 add_show_from_set
5d161b24 2347 (add_set_cmd ("monitor", no_class, var_zinteger,
2df3850c
JM
2348 (char *) &monitor_debug_p,
2349 "Set debugging of remote monitor communication.\n\
2350When enabled, communication between GDB and the remote monitor\n\
5d161b24
DB
2351is displayed.", &setdebuglist),
2352 &showdebuglist);
c906108c 2353}
This page took 0.199596 seconds and 4 git commands to generate.