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