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