* gdb.base/maint.exp: Treat $EXEEXT as optional in output.
[deliverable/binutils-gdb.git] / gdb / remote-e7000.c
CommitLineData
c906108c 1/* Remote debugging interface for Hitachi E7000 ICE, for GDB
b6ba6518
KB
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
c906108c
SS
4 Contributed by Cygnus Support.
5
6 Written by Steve Chamberlain for Cygnus Support.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
c5aa993b
JM
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c
SS
24
25/* The E7000 is an in-circuit emulator for the Hitachi H8/300-H and
26 Hitachi-SH processor. It has serial port and a lan port.
27
28 The monitor command set makes it difficult to load large ammounts of
29 data over the lan without using ftp - so try not to issue load
30 commands when communicating over ethernet; use the ftpload command.
31
32 The monitor pauses for a second when dumping srecords to the serial
33 line too, so we use a slower per byte mechanism but without the
34 startup overhead. Even so, it's pretty slow... */
35
36#include "defs.h"
37#include "gdbcore.h"
38#include "gdbarch.h"
39#include "inferior.h"
40#include "target.h"
c906108c
SS
41#include "value.h"
42#include "command.h"
c906108c
SS
43#include "gdb_string.h"
44#include "gdbcmd.h"
45#include <sys/types.h>
46#include "serial.h"
47#include "remote-utils.h"
48#include "symfile.h"
4e052eda 49#include "regcache.h"
c906108c
SS
50#include <time.h>
51#include <ctype.h>
52
53
54#if 1
55#define HARD_BREAKPOINTS /* Now handled by set option. */
56#define BC_BREAKPOINTS use_hard_breakpoints
57#endif
58
59#define CTRLC 0x03
60#define ENQ 0x05
61#define ACK 0x06
62#define CTRLZ 0x1a
63
cc17453a
EZ
64/* This file is used by 2 different targets, sh-elf and h8300. The
65 h8300 is not multiarched and doesn't use the registers defined in
66 tm-sh.h. To avoid using a macro GDB_TARGET_IS_SH, we do runtime check
67 of the target, which requires that these namse below are always
68 defined also in the h8300 case. */
69
70#if !defined (PR_REGNUM)
71#define PR_REGNUM -1
72#endif
73#if !defined (GBR_REGNUM)
74#define GBR_REGNUM -1
75#endif
76#if !defined (VBR_REGNUM)
77#define VBR_REGNUM -1
78#endif
79#if !defined (MACH_REGNUM)
80#define MACH_REGNUM -1
81#endif
82#if !defined (MACL_REGNUM)
83#define MACL_REGNUM -1
84#endif
85#if !defined (SR_REGNUM)
86#define SR_REGNUM -1
87#endif
88
a14ed312 89extern void report_transfer_performance (unsigned long, time_t, time_t);
c906108c
SS
90
91extern char *sh_processor_type;
92
93/* Local function declarations. */
94
a14ed312 95static void e7000_close (int);
c906108c 96
a14ed312 97static void e7000_fetch_register (int);
c906108c 98
a14ed312 99static void e7000_store_register (int);
c906108c 100
a14ed312 101static void e7000_command (char *, int);
c906108c 102
a14ed312 103static void e7000_login_command (char *, int);
c906108c 104
a14ed312 105static void e7000_ftp_command (char *, int);
c906108c 106
a14ed312 107static void e7000_drain_command (char *, int);
c906108c 108
a14ed312 109static void expect (char *);
c906108c 110
a14ed312 111static void expect_full_prompt (void);
c906108c 112
a14ed312 113static void expect_prompt (void);
c906108c 114
a14ed312 115static int e7000_parse_device (char *args, char *dev_name, int baudrate);
c906108c
SS
116/* Variables. */
117
819cc324 118static struct serial *e7000_desc;
c906108c
SS
119
120/* Allow user to chose between using hardware breakpoints or memory. */
c5aa993b 121static int use_hard_breakpoints = 0; /* use sw breakpoints by default */
c906108c
SS
122
123/* Nonzero if using the tcp serial driver. */
124
c5aa993b 125static int using_tcp; /* direct tcp connection to target */
c906108c
SS
126static int using_tcp_remote; /* indirect connection to target
127 via tcp to controller */
128
129/* Nonzero if using the pc isa card. */
130
131static int using_pc;
132
133extern struct target_ops e7000_ops; /* Forward declaration */
134
135char *ENQSTRING = "\005";
136
137/* Nonzero if some routine (as opposed to the user) wants echoing.
138 FIXME: Do this reentrantly with an extra parameter. */
139
140static int echo;
141
142static int ctrl_c;
143
144static int timeout = 20;
145
146/* Send data to e7000debug. */
147
148static void
fba45db2 149puts_e7000debug (char *buf)
c906108c
SS
150{
151 if (!e7000_desc)
152 error ("Use \"target e7000 ...\" first.");
153
154 if (remote_debug)
155 printf_unfiltered ("Sending %s\n", buf);
156
2cd58942
AC
157 if (serial_write (e7000_desc, buf, strlen (buf)))
158 fprintf_unfiltered (gdb_stderr, "serial_write failed: %s\n", safe_strerror (errno));
c906108c
SS
159
160 /* And expect to see it echoed, unless using the pc interface */
161#if 0
162 if (!using_pc)
163#endif
164 expect (buf);
165}
166
167static void
fba45db2 168putchar_e7000 (int x)
c906108c
SS
169{
170 char b[1];
171
172 b[0] = x;
2cd58942 173 serial_write (e7000_desc, b, 1);
c906108c
SS
174}
175
176static void
fba45db2 177write_e7000 (char *s)
c906108c 178{
2cd58942 179 serial_write (e7000_desc, s, strlen (s));
c906108c
SS
180}
181
182static int
fba45db2 183normal (int x)
c906108c
SS
184{
185 if (x == '\n')
186 return '\r';
187 return x;
188}
189
190/* Read a character from the remote system, doing all the fancy timeout
191 stuff. Handles serial errors and EOF. If TIMEOUT == 0, and no chars,
192 returns -1, else returns next char. Discards chars > 127. */
193
194static int
fba45db2 195readchar (int timeout)
c906108c
SS
196{
197 int c;
198
199 do
200 {
2cd58942 201 c = serial_readchar (e7000_desc, timeout);
c906108c
SS
202 }
203 while (c > 127);
204
205 if (c == SERIAL_TIMEOUT)
206 {
207 if (timeout == 0)
208 return -1;
209 echo = 0;
210 error ("Timeout reading from remote system.");
211 }
212 else if (c < 0)
213 error ("Serial communication error");
214
c5aa993b 215 if (remote_debug)
c906108c
SS
216 {
217 putchar_unfiltered (c);
218 gdb_flush (gdb_stdout);
219 }
220
221 return normal (c);
222}
223
224#if 0
225char *
fba45db2 226tl (int x)
c906108c
SS
227{
228 static char b[8][10];
229 static int p;
230
231 p++;
232 p &= 7;
c5aa993b
JM
233 if (x >= ' ')
234 {
c906108c
SS
235 b[p][0] = x;
236 b[p][1] = 0;
237 }
238 else
239 {
c5aa993b 240 sprintf (b[p], "<%d>", x);
c906108c
SS
241 }
242
243 return b[p];
244}
245#endif
246
247/* Scan input from the remote system, until STRING is found. If
248 DISCARD is non-zero, then discard non-matching input, else print it
249 out. Let the user break out immediately. */
250
251static void
fba45db2 252expect (char *string)
c906108c
SS
253{
254 char *p = string;
255 int c;
256 int nl = 0;
257
258 while (1)
259 {
260 c = readchar (timeout);
c5aa993b 261
c906108c
SS
262 if (echo)
263 {
264 if (c == '\r' || c == '\n')
265 {
266 if (!nl)
267 putchar_unfiltered ('\n');
268 nl = 1;
269 }
270 else
271 {
272 nl = 0;
273 putchar_unfiltered (c);
274 }
275 gdb_flush (gdb_stdout);
276 }
277 if (normal (c) == normal (*p++))
278 {
279 if (*p == '\0')
280 return;
281 }
282 else
283 {
284 p = string;
285
286 if (normal (c) == normal (string[0]))
287 p++;
288 }
289 }
290}
291
292/* Keep discarding input until we see the e7000 prompt.
293
294 The convention for dealing with the prompt is that you
295 o give your command
296 o *then* wait for the prompt.
297
298 Thus the last thing that a procedure does with the serial line will
299 be an expect_prompt(). Exception: e7000_resume does not wait for
300 the prompt, because the terminal is being handed over to the
301 inferior. However, the next thing which happens after that is a
302 e7000_wait which does wait for the prompt. Note that this includes
303 abnormal exit, e.g. error(). This is necessary to prevent getting
304 into states from which we can't recover. */
305
306static void
fba45db2 307expect_prompt (void)
c906108c
SS
308{
309 expect (":");
310}
311
312static void
fba45db2 313expect_full_prompt (void)
c906108c
SS
314{
315 expect ("\r:");
316}
317
318static int
fba45db2 319convert_hex_digit (int ch)
c906108c
SS
320{
321 if (ch >= '0' && ch <= '9')
322 return ch - '0';
323 else if (ch >= 'A' && ch <= 'F')
324 return ch - 'A' + 10;
325 else if (ch >= 'a' && ch <= 'f')
326 return ch - 'a' + 10;
327 return -1;
328}
329
330static int
fba45db2 331get_hex (int *start)
c906108c
SS
332{
333 int value = convert_hex_digit (*start);
334 int try;
335
336 *start = readchar (timeout);
337 while ((try = convert_hex_digit (*start)) >= 0)
338 {
339 value <<= 4;
340 value += try;
341 *start = readchar (timeout);
342 }
343 return value;
344}
345
346#if 0
347/* Get N 32-bit words from remote, each preceded by a space, and put
348 them in registers starting at REGNO. */
349
350static void
fba45db2 351get_hex_regs (int n, int regno)
c906108c
SS
352{
353 long val;
354 int i;
355
356 for (i = 0; i < n; i++)
357 {
358 int j;
359
360 val = 0;
361 for (j = 0; j < 8; j++)
362 val = (val << 4) + get_hex_digit (j == 0);
363 supply_register (regno++, (char *) &val);
364 }
365}
366#endif
367
368/* This is called not only when we first attach, but also when the
369 user types "run" after having attached. */
370
371static void
fba45db2 372e7000_create_inferior (char *execfile, char *args, char **env)
c906108c
SS
373{
374 int entry_pt;
375
376 if (args && *args)
377 error ("Can't pass arguments to remote E7000DEBUG process");
378
379 if (execfile == 0 || exec_bfd == 0)
380 error ("No executable file specified");
381
382 entry_pt = (int) bfd_get_start_address (exec_bfd);
383
384#ifdef CREATE_INFERIOR_HOOK
385 CREATE_INFERIOR_HOOK (0); /* No process-ID */
386#endif
387
388 /* The "process" (board) is already stopped awaiting our commands, and
389 the program is already downloaded. We just set its PC and go. */
390
391 clear_proceed_status ();
392
393 /* Tell wait_for_inferior that we've started a new process. */
394 init_wait_for_inferior ();
395
396 /* Set up the "saved terminal modes" of the inferior
397 based on what modes we are starting it with. */
398 target_terminal_init ();
399
400 /* Install inferior's terminal modes. */
401 target_terminal_inferior ();
402
403 /* insert_step_breakpoint (); FIXME, do we need this? */
404 proceed ((CORE_ADDR) entry_pt, -1, 0); /* Let 'er rip... */
405}
406
407/* Open a connection to a remote debugger. NAME is the filename used
408 for communication. */
409
410static int baudrate = 9600;
411static char dev_name[100];
412
413static char *machine = "";
414static char *user = "";
415static char *passwd = "";
416static char *dir = "";
417
418/* Grab the next token and buy some space for it */
419
420static char *
fba45db2 421next (char **ptr)
c906108c
SS
422{
423 char *p = *ptr;
424 char *s;
425 char *r;
426 int l = 0;
427
428 while (*p && *p == ' ')
429 p++;
430 s = p;
431 while (*p && (*p != ' ' && *p != '\t'))
432 {
433 l++;
434 p++;
435 }
436 r = xmalloc (l + 1);
437 memcpy (r, s, l);
438 r[l] = 0;
439 *ptr = p;
440 return r;
441}
442
443static void
fba45db2 444e7000_login_command (char *args, int from_tty)
c906108c
SS
445{
446 if (args)
447 {
448 machine = next (&args);
449 user = next (&args);
450 passwd = next (&args);
451 dir = next (&args);
452 if (from_tty)
453 {
454 printf_unfiltered ("Set info to %s %s %s %s\n", machine, user, passwd, dir);
455 }
456 }
457 else
458 {
459 error ("Syntax is ftplogin <machine> <user> <passwd> <directory>");
460 }
461}
462
463/* Start an ftp transfer from the E7000 to a host */
464
465static void
fba45db2 466e7000_ftp_command (char *args, int from_tty)
c906108c
SS
467{
468 /* FIXME: arbitrary limit on machine names and such. */
469 char buf[200];
470
471 int oldtimeout = timeout;
472 timeout = remote_timeout;
473
474 sprintf (buf, "ftp %s\r", machine);
475 puts_e7000debug (buf);
476 expect (" Username : ");
477 sprintf (buf, "%s\r", user);
478 puts_e7000debug (buf);
479 expect (" Password : ");
480 write_e7000 (passwd);
481 write_e7000 ("\r");
482 expect ("success\r");
483 expect ("FTP>");
484 sprintf (buf, "cd %s\r", dir);
485 puts_e7000debug (buf);
486 expect ("FTP>");
487 sprintf (buf, "ll 0;s:%s\r", args);
488 puts_e7000debug (buf);
489 expect ("FTP>");
490 puts_e7000debug ("bye\r");
491 expect (":");
492 timeout = oldtimeout;
493}
494
c5aa993b 495static int
fba45db2 496e7000_parse_device (char *args, char *dev_name, int baudrate)
c906108c
SS
497{
498 char junk[128];
499 int n = 0;
500 if (args && strcasecmp (args, "pc") == 0)
501 {
502 strcpy (dev_name, args);
503 using_pc = 1;
504 }
c5aa993b 505 else
c906108c
SS
506 {
507 /* FIXME! temp hack to allow use with port master -
c5aa993b
JM
508 target tcp_remote <device> */
509 if (args && strncmp (args, "tcp", 10) == 0)
510 {
c906108c
SS
511 char com_type[128];
512 n = sscanf (args, " %s %s %d %s", com_type, dev_name, &baudrate, junk);
c5aa993b 513 using_tcp_remote = 1;
c906108c 514 n--;
c5aa993b
JM
515 }
516 else if (args)
c906108c
SS
517 {
518 n = sscanf (args, " %s %d %s", dev_name, &baudrate, junk);
519 }
520
521 if (n != 1 && n != 2)
522 {
523 error ("Bad arguments. Usage:\ttarget e7000 <device> <speed>\n\
524or \t\ttarget e7000 <host>[:<port>]\n\
525or \t\ttarget e7000 tcp_remote <host>[:<port>]\n\
526or \t\ttarget e7000 pc\n");
527 }
528
608506ed 529#if !defined(__GO32__) && !defined(_WIN32) && !defined(__CYGWIN__)
c906108c
SS
530 /* FIXME! test for ':' is ambiguous */
531 if (n == 1 && strchr (dev_name, ':') == 0)
532 {
533 /* Default to normal telnet port */
534 /* serial_open will use this to determine tcp communication */
535 strcat (dev_name, ":23");
536 }
537#endif
538 if (!using_tcp_remote && strchr (dev_name, ':'))
c5aa993b 539 using_tcp = 1;
c906108c
SS
540 }
541
542 return n;
543}
544
545/* Stub for catch_errors. */
546
547static int
34e9d9bb 548e7000_start_remote (void *dummy)
c906108c
SS
549{
550 int loop;
551 int sync;
552 int try;
553 int quit_trying;
554
8edbea78 555 immediate_quit++; /* Allow user to interrupt it */
c906108c
SS
556
557 /* Hello? Are you there? */
558 sync = 0;
c5aa993b 559 loop = 0;
c906108c
SS
560 try = 0;
561 quit_trying = 20;
562 putchar_e7000 (CTRLC);
563 while (!sync && ++try <= quit_trying)
564 {
565 int c;
566
567 printf_unfiltered ("[waiting for e7000...]\n");
568
569 write_e7000 ("\r");
570 c = readchar (1);
571
572 /* FIXME! this didn't seem right-> while (c != SERIAL_TIMEOUT)
573 * we get stuck in this loop ...
574 * We may never timeout, and never sync up :-(
575 */
576 while (!sync && c != -1)
577 {
578 /* Dont echo cr's */
579 if (c != '\r')
580 {
581 putchar_unfiltered (c);
582 gdb_flush (gdb_stdout);
583 }
584 /* Shouldn't we either break here, or check for sync in inner loop? */
585 if (c == ':')
586 sync = 1;
587
c5aa993b 588 if (loop++ == 20)
c906108c
SS
589 {
590 putchar_e7000 (CTRLC);
591 loop = 0;
592 }
593
c5aa993b 594 QUIT;
c906108c
SS
595
596 if (quit_flag)
597 {
598 putchar_e7000 (CTRLC);
599 /* Was-> quit_flag = 0; */
600 c = -1;
c5aa993b 601 quit_trying = try + 1; /* we don't want to try anymore */
c906108c
SS
602 }
603 else
604 {
605 c = readchar (1);
606 }
607 }
608 }
609
610 if (!sync)
611 {
c5aa993b 612 fprintf_unfiltered (gdb_stderr, "Giving up after %d tries...\n", try);
8e1a459b 613 error ("Unable to synchronize with target.\n");
c906108c
SS
614 }
615
616 puts_e7000debug ("\r");
617 expect_prompt ();
618 puts_e7000debug ("b -\r"); /* Clear breakpoints */
619 expect_prompt ();
620
8edbea78 621 immediate_quit--;
c906108c
SS
622
623/* This is really the job of start_remote however, that makes an assumption
624 that the target is about to print out a status message of some sort. That
625 doesn't happen here. */
626
627 flush_cached_frames ();
628 registers_changed ();
629 stop_pc = read_pc ();
630 set_current_frame (create_new_frame (read_fp (), stop_pc));
631 select_frame (get_current_frame (), 0);
632 print_stack_frame (selected_frame, -1, 1);
633
634 return 1;
635}
636
637static void
fba45db2 638e7000_open (char *args, int from_tty)
c906108c
SS
639{
640 int n;
641
642 target_preopen (from_tty);
643
644 n = e7000_parse_device (args, dev_name, baudrate);
645
646 push_target (&e7000_ops);
647
2cd58942 648 e7000_desc = serial_open (dev_name);
c906108c
SS
649
650 if (!e7000_desc)
651 perror_with_name (dev_name);
652
2cd58942 653 if (serial_setbaudrate (e7000_desc, baudrate))
67dd5ca6 654 {
2cd58942 655 serial_close (e7000_desc);
67dd5ca6
FN
656 perror_with_name (dev_name);
657 }
2cd58942 658 serial_raw (e7000_desc);
c906108c
SS
659
660#ifdef GDB_TARGET_IS_H8300
661 h8300hmode = 1;
662#endif
663
664 /* Start the remote connection; if error (0), discard this target.
665 In particular, if the user quits, be sure to discard it
666 (we'd be in an inconsistent state otherwise). */
c5aa993b
JM
667 if (!catch_errors (e7000_start_remote, (char *) 0,
668 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
669 if (from_tty)
670 printf_filtered ("Remote target %s connected to %s\n", target_shortname,
671 dev_name);
c906108c
SS
672}
673
674/* Close out all files and local state before this target loses control. */
675
676static void
fba45db2 677e7000_close (int quitting)
c906108c
SS
678{
679 if (e7000_desc)
680 {
2cd58942 681 serial_close (e7000_desc);
c906108c
SS
682 e7000_desc = 0;
683 }
684}
685
686/* Terminate the open connection to the remote debugger. Use this
687 when you want to detach and do something else with your gdb. */
688
689static void
55d80160 690e7000_detach (char *arg, int from_tty)
c906108c
SS
691{
692 pop_target (); /* calls e7000_close to do the real work */
693 if (from_tty)
694 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
695}
696
697/* Tell the remote machine to resume. */
698
699static void
39f77062 700e7000_resume (ptid_t ptid, int step, enum target_signal sigal)
c906108c
SS
701{
702 if (step)
703 puts_e7000debug ("S\r");
704 else
705 puts_e7000debug ("G\r");
706}
707
708/* Read the remote registers into the block REGS.
709
710 For the H8/300 a register dump looks like:
711
712 PC=00021A CCR=80:I*******
713 ER0 - ER3 0000000A 0000002E 0000002E 00000000
714 ER4 - ER7 00000000 00000000 00000000 00FFEFF6
715 000218 MOV.B R1L,R2L
716 STEP NORMAL END or
717 BREAK POINT
c5aa993b 718 */
c906108c 719
c906108c
SS
720char *want_h8300h = "PC=%p CCR=%c\n\
721 ER0 - ER3 %0 %1 %2 %3\n\
722 ER4 - ER7 %4 %5 %6 %7\n";
723
724char *want_nopc_h8300h = "%p CCR=%c\n\
725 ER0 - ER3 %0 %1 %2 %3\n\
726 ER4 - ER7 %4 %5 %6 %7";
727
728char *want_h8300s = "PC=%p CCR=%c\n\
729 MACH=\n\
730 ER0 - ER3 %0 %1 %2 %3\n\
731 ER4 - ER7 %4 %5 %6 %7\n";
732
733char *want_nopc_h8300s = "%p CCR=%c EXR=%9\n\
734 ER0 - ER3 %0 %1 %2 %3\n\
735 ER4 - ER7 %4 %5 %6 %7";
736
cc17453a 737char *want_sh = "PC=%16 SR=%22\n\
c906108c
SS
738PR=%17 GBR=%18 VBR=%19\n\
739MACH=%20 MACL=%21\n\
740R0-7 %0 %1 %2 %3 %4 %5 %6 %7\n\
741R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n";
742
cc17453a 743char *want_nopc_sh = "%16 SR=%22\n\
c906108c
SS
744 PR=%17 GBR=%18 VBR=%19\n\
745 MACH=%20 MACL=%21\n\
746 R0-7 %0 %1 %2 %3 %4 %5 %6 %7\n\
747 R8-15 %8 %9 %10 %11 %12 %13 %14 %15";
748
749char *want_sh3 = "PC=%16 SR=%22\n\
750PR=%17 GBR=%18 VBR=%19\n\
751MACH=%20 MACL=%21 SSR=%23 SPC=%24\n\
752R0-7 %0 %1 %2 %3 %4 %5 %6 %7\n\
753R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n\
754R0_BANK0-R3_BANK0 %25 %26 %27 %28\n\
755R4_BANK0-R7_BANK0 %29 %30 %31 %32\n\
756R0_BANK1-R3_BANK1 %33 %34 %35 %36\n\
757R4_BANK1-R7_BANK1 %37 %38 %39 %40";
758
cc17453a 759char *want_nopc_sh3 = "%16 SR=%22\n\
c906108c
SS
760 PR=%17 GBR=%18 VBR=%19\n\
761 MACH=%20 MACL=%21 SSR=%22 SPC=%23\n\
762 R0-7 %0 %1 %2 %3 %4 %5 %6 %7\n\
763 R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n\
764 R0_BANK0-R3_BANK0 %25 %26 %27 %28\n\
765 R4_BANK0-R7_BANK0 %29 %30 %31 %32\n\
766 R0_BANK1-R3_BANK1 %33 %34 %35 %36\n\
767 R4_BANK1-R7_BANK1 %37 %38 %39 %40";
768
c906108c 769static int
fba45db2 770gch (void)
c906108c
SS
771{
772 return readchar (timeout);
773}
774
775static unsigned int
fba45db2 776gbyte (void)
c906108c
SS
777{
778 int high = convert_hex_digit (gch ());
779 int low = convert_hex_digit (gch ());
780
781 return (high << 4) + low;
782}
783
784void
69dc947a 785fetch_regs_from_dump (int (*nextchar) (), char *want)
c906108c
SS
786{
787 int regno;
788 char buf[MAX_REGISTER_RAW_SIZE];
789
790 int thischar = nextchar ();
791
4902674b
EZ
792 if (want == NULL)
793 internal_error (__FILE__, __LINE__, "Register set not selected.");
794
c906108c
SS
795 while (*want)
796 {
797 switch (*want)
798 {
799 case '\n':
800 /* Skip to end of line and then eat all new line type stuff */
c5aa993b 801 while (thischar != '\n' && thischar != '\r')
c906108c 802 thischar = nextchar ();
c5aa993b 803 while (thischar == '\n' || thischar == '\r')
c906108c
SS
804 thischar = nextchar ();
805 want++;
806 break;
807
808 case ' ':
809 while (thischar == ' '
810 || thischar == '\t'
811 || thischar == '\r'
812 || thischar == '\n')
813 thischar = nextchar ();
814 want++;
815 break;
c5aa993b 816
c906108c
SS
817 default:
818 if (*want == thischar)
819 {
820 want++;
821 if (*want)
822 thischar = nextchar ();
c5aa993b 823
c906108c
SS
824 }
825 else if (thischar == ' ' || thischar == '\n' || thischar == '\r')
826 {
827 thischar = nextchar ();
828 }
c5aa993b
JM
829 else
830 {
831 error ("out of sync in fetch registers wanted <%s>, got <%c 0x%x>",
832 want, thischar, thischar);
833 }
834
c906108c
SS
835 break;
836 case '%':
837 /* Got a register command */
838 want++;
839 switch (*want)
840 {
841#ifdef PC_REGNUM
842 case 'p':
843 regno = PC_REGNUM;
844 want++;
845 break;
846#endif
847#ifdef CCR_REGNUM
848 case 'c':
849 regno = CCR_REGNUM;
850 want++;
851 break;
852#endif
853#ifdef SP_REGNUM
854 case 's':
855 regno = SP_REGNUM;
856 want++;
857 break;
858#endif
859#ifdef FP_REGNUM
860 case 'f':
861 regno = FP_REGNUM;
862 want++;
863 break;
864#endif
865
866 default:
c5aa993b 867 if (isdigit (want[0]))
c906108c
SS
868 {
869 if (isdigit (want[1]))
870 {
871 regno = (want[0] - '0') * 10 + want[1] - '0';
872 want += 2;
873 }
c5aa993b 874 else
c906108c
SS
875 {
876 regno = want[0] - '0';
877 want++;
878 }
879 }
c5aa993b 880
c906108c 881 else
e1e9e218 882 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
883 }
884 store_signed_integer (buf,
c5aa993b 885 REGISTER_RAW_SIZE (regno),
34e9d9bb 886 (LONGEST) get_hex (&thischar));
c906108c
SS
887 supply_register (regno, buf);
888 break;
889 }
890 }
891}
892
893static void
fba45db2 894e7000_fetch_registers (void)
c906108c
SS
895{
896 int regno;
4902674b 897 char *wanted = NULL;
c906108c
SS
898
899 puts_e7000debug ("R\r");
900
c906108c 901 if (TARGET_ARCHITECTURE->arch == bfd_arch_sh)
cc17453a
EZ
902 {
903 wanted = want_sh;
904 switch (TARGET_ARCHITECTURE->mach)
905 {
906 case bfd_mach_sh3:
907 case bfd_mach_sh3e:
908 case bfd_mach_sh4:
909 wanted = want_sh3;
910 }
911 }
912#ifdef GDB_TARGET_IS_H8300
913 if (TARGET_ARCHITECTURE->arch == bfd_arch_h8300)
914 {
915 if (h8300smode)
916 wanted = want_h8300s;
917 else
918 wanted = want_h8300h;
919 }
c906108c 920#endif
cc17453a 921
c906108c
SS
922 fetch_regs_from_dump (gch, wanted);
923
924 /* And supply the extra ones the simulator uses */
925 for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
926 {
927 int buf = 0;
928
929 supply_register (regno, (char *) (&buf));
930 }
931}
932
933/* Fetch register REGNO, or all registers if REGNO is -1. Returns
934 errno value. */
935
936static void
fba45db2 937e7000_fetch_register (int regno)
c906108c
SS
938{
939 e7000_fetch_registers ();
940}
941
942/* Store the remote registers from the contents of the block REGS. */
943
944static void
fba45db2 945e7000_store_registers (void)
c906108c
SS
946{
947 int regno;
948
949 for (regno = 0; regno < NUM_REALREGS; regno++)
950 e7000_store_register (regno);
951
952 registers_changed ();
953}
954
955/* Store register REGNO, or all if REGNO == 0. Return errno value. */
956
957static void
fba45db2 958e7000_store_register (int regno)
c906108c
SS
959{
960 char buf[200];
961
962 if (regno == -1)
963 {
964 e7000_store_registers ();
965 return;
966 }
967
cc17453a 968 if (TARGET_ARCHITECTURE->arch == bfd_arch_h8300)
c906108c 969 {
cc17453a
EZ
970 if (regno <= 7)
971 {
4902674b 972 sprintf (buf, ".ER%d %s\r", regno, phex_nz (read_register (regno), 0));
cc17453a
EZ
973 puts_e7000debug (buf);
974 }
975 else if (regno == PC_REGNUM)
976 {
4902674b 977 sprintf (buf, ".PC %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
978 puts_e7000debug (buf);
979 }
980#ifdef CCR_REGNUM
981 else if (regno == CCR_REGNUM)
982 {
4902674b 983 sprintf (buf, ".CCR %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
984 puts_e7000debug (buf);
985 }
986#endif
c906108c 987 }
c906108c 988
cc17453a 989 else if (TARGET_ARCHITECTURE->arch == bfd_arch_sh)
c906108c 990 {
cc17453a
EZ
991 if (regno == PC_REGNUM)
992 {
4902674b 993 sprintf (buf, ".PC %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
994 puts_e7000debug (buf);
995 }
c906108c 996
cc17453a
EZ
997 else if (regno == SR_REGNUM)
998 {
4902674b 999 sprintf (buf, ".SR %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
1000 puts_e7000debug (buf);
1001 }
c906108c 1002
cc17453a
EZ
1003 else if (regno == PR_REGNUM)
1004 {
4902674b 1005 sprintf (buf, ".PR %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
1006 puts_e7000debug (buf);
1007 }
c906108c 1008
cc17453a
EZ
1009 else if (regno == GBR_REGNUM)
1010 {
4902674b 1011 sprintf (buf, ".GBR %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
1012 puts_e7000debug (buf);
1013 }
c906108c 1014
cc17453a
EZ
1015 else if (regno == VBR_REGNUM)
1016 {
4902674b 1017 sprintf (buf, ".VBR %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
1018 puts_e7000debug (buf);
1019 }
c906108c 1020
cc17453a
EZ
1021 else if (regno == MACH_REGNUM)
1022 {
4902674b 1023 sprintf (buf, ".MACH %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
1024 puts_e7000debug (buf);
1025 }
c906108c 1026
cc17453a
EZ
1027 else if (regno == MACL_REGNUM)
1028 {
4902674b 1029 sprintf (buf, ".MACL %s\r", phex_nz (read_register (regno), 0));
cc17453a
EZ
1030 puts_e7000debug (buf);
1031 }
1032 else
1033 {
4902674b 1034 sprintf (buf, ".R%d %s\r", regno, phex_nz (read_register (regno), 0));
cc17453a
EZ
1035 puts_e7000debug (buf);
1036 }
c906108c
SS
1037 }
1038
c906108c
SS
1039 expect_prompt ();
1040}
1041
1042/* Get ready to modify the registers array. On machines which store
1043 individual registers, this doesn't need to do anything. On machines
1044 which store all the registers in one fell swoop, this makes sure
1045 that registers contains all the registers from the program being
1046 debugged. */
1047
1048static void
fba45db2 1049e7000_prepare_to_store (void)
c906108c
SS
1050{
1051 /* Do nothing, since we can store individual regs */
1052}
1053
1054static void
55d80160 1055e7000_files_info (struct target_ops *ops)
c906108c
SS
1056{
1057 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baudrate);
1058}
1059
1060static int
fba45db2 1061stickbyte (char *where, unsigned int what)
c906108c
SS
1062{
1063 static CONST char digs[] = "0123456789ABCDEF";
1064
1065 where[0] = digs[(what >> 4) & 0xf];
1066 where[1] = digs[(what & 0xf) & 0xf];
1067
1068 return what;
1069}
1070
1071/* Write a small ammount of memory. */
1072
1073static int
fba45db2 1074write_small (CORE_ADDR memaddr, unsigned char *myaddr, int len)
c906108c
SS
1075{
1076 int i;
1077 char buf[200];
1078
1079 for (i = 0; i < len; i++)
1080 {
1081 if (((memaddr + i) & 3) == 0 && (i + 3 < len))
1082 {
1083 /* Can be done with a long word */
cc17453a 1084 sprintf (buf, "m %lx %x%02x%02x%02x;l\r",
c906108c
SS
1085 memaddr + i,
1086 myaddr[i], myaddr[i + 1], myaddr[i + 2], myaddr[i + 3]);
1087 puts_e7000debug (buf);
1088 i += 3;
1089 }
1090 else
1091 {
cc17453a 1092 sprintf (buf, "m %lx %x\r", memaddr + i, myaddr[i]);
c906108c
SS
1093 puts_e7000debug (buf);
1094 }
1095 }
1096
1097 expect_prompt ();
1098
1099 return len;
1100}
1101
1102/* Write a large ammount of memory, this only works with the serial
1103 mode enabled. Command is sent as
1104
c5aa993b
JM
1105 il ;s:s\r ->
1106 <- il ;s:s\r
1107 <- ENQ
1108 ACK ->
1109 <- LO s\r
1110 Srecords...
1111 ^Z ->
1112 <- ENQ
1113 ACK ->
1114 <- :
1115 */
c906108c
SS
1116
1117static int
fba45db2 1118write_large (CORE_ADDR memaddr, unsigned char *myaddr, int len)
c906108c
SS
1119{
1120 int i;
1121#define maxstride 128
1122 int stride;
1123
1124 puts_e7000debug ("IL ;S:FK\r");
1125 expect (ENQSTRING);
1126 putchar_e7000 (ACK);
1127 expect ("LO FK\r");
1128
1129 for (i = 0; i < len; i += stride)
1130 {
1131 char compose[maxstride * 2 + 50];
1132 int address = i + memaddr;
1133 int j;
1134 int check_sum;
1135 int where = 0;
1136 int alen;
1137
1138 stride = len - i;
1139 if (stride > maxstride)
1140 stride = maxstride;
1141
1142 compose[where++] = 'S';
1143 check_sum = 0;
1144 if (address >= 0xffffff)
1145 alen = 4;
1146 else if (address >= 0xffff)
1147 alen = 3;
1148 else
1149 alen = 2;
1150 /* Insert type. */
1151 compose[where++] = alen - 1 + '0';
1152 /* Insert length. */
1153 check_sum += stickbyte (compose + where, alen + stride + 1);
1154 where += 2;
1155 while (alen > 0)
1156 {
1157 alen--;
1158 check_sum += stickbyte (compose + where, address >> (8 * (alen)));
1159 where += 2;
1160 }
1161
1162 for (j = 0; j < stride; j++)
1163 {
1164 check_sum += stickbyte (compose + where, myaddr[i + j]);
1165 where += 2;
1166 }
1167 stickbyte (compose + where, ~check_sum);
1168 where += 2;
1169 compose[where++] = '\r';
1170 compose[where++] = '\n';
1171 compose[where++] = 0;
1172
2cd58942 1173 serial_write (e7000_desc, compose, where);
c906108c
SS
1174 j = readchar (0);
1175 if (j == -1)
1176 {
1177 /* This is ok - nothing there */
1178 }
1179 else if (j == ENQ)
1180 {
1181 /* Hmm, it's trying to tell us something */
1182 expect (":");
1183 error ("Error writing memory");
1184 }
1185 else
1186 {
1187 printf_unfiltered ("@%d}@", j);
c5aa993b 1188 while ((j = readchar (0)) > 0)
c906108c 1189 {
c5aa993b 1190 printf_unfiltered ("@{%d}@", j);
c906108c
SS
1191 }
1192 }
1193 }
1194
1195 /* Send the trailer record */
1196 write_e7000 ("S70500000000FA\r");
1197 putchar_e7000 (CTRLZ);
1198 expect (ENQSTRING);
1199 putchar_e7000 (ACK);
1200 expect (":");
1201
1202 return len;
1203}
1204
1205/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
1206 memory at MEMADDR. Returns length moved.
1207
1208 Can't use the Srecord load over ethernet, so don't use fast method
1209 then. */
1210
1211static int
fba45db2 1212e7000_write_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
c906108c
SS
1213{
1214 if (len < 16 || using_tcp || using_pc)
1215 return write_small (memaddr, myaddr, len);
1216 else
1217 return write_large (memaddr, myaddr, len);
1218}
1219
1220/* Read LEN bytes from inferior memory at MEMADDR. Put the result
1221 at debugger address MYADDR. Returns length moved.
1222
c5aa993b
JM
1223 Small transactions we send
1224 m <addr>;l
1225 and receive
1226 00000000 12345678 ?
c906108c
SS
1227 */
1228
1229static int
fba45db2 1230e7000_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
c906108c
SS
1231{
1232 int count;
1233 int c;
1234 int i;
1235 char buf[200];
1236 /* Starting address of this pass. */
1237
c5aa993b 1238/* printf("READ INF %x %x %d\n", memaddr, myaddr, len); */
c906108c
SS
1239 if (((memaddr - 1) + len) < memaddr)
1240 {
1241 errno = EIO;
1242 return 0;
1243 }
1244
cc17453a 1245 sprintf (buf, "m %lx;l\r", memaddr);
c906108c
SS
1246 puts_e7000debug (buf);
1247
c5aa993b 1248 for (count = 0; count < len; count += 4)
c906108c
SS
1249 {
1250 /* Suck away the address */
c5aa993b 1251 c = gch ();
c906108c 1252 while (c != ' ')
c5aa993b 1253 c = gch ();
c906108c 1254 c = gch ();
c5aa993b 1255 if (c == '*')
c906108c 1256 { /* Some kind of error */
c5aa993b
JM
1257 puts_e7000debug (".\r"); /* Some errors leave us in memory input mode */
1258 expect_full_prompt ();
c906108c
SS
1259 return -1;
1260 }
1261 while (c != ' ')
c5aa993b 1262 c = gch ();
c906108c
SS
1263
1264 /* Now read in the data */
c5aa993b 1265 for (i = 0; i < 4; i++)
c906108c 1266 {
c5aa993b
JM
1267 int b = gbyte ();
1268 if (count + i < len)
1269 {
1270 myaddr[count + i] = b;
1271 }
c906108c
SS
1272 }
1273
1274 /* Skip the trailing ? and send a . to end and a cr for more */
c5aa993b 1275 gch ();
c906108c
SS
1276 gch ();
1277 if (count + 4 >= len)
c5aa993b 1278 puts_e7000debug (".\r");
c906108c 1279 else
c5aa993b 1280 puts_e7000debug ("\r");
c906108c
SS
1281
1282 }
c5aa993b 1283 expect_prompt ();
c906108c
SS
1284 return len;
1285}
1286
1287
1288
1289/*
c5aa993b 1290 For large transfers we used to send
c906108c
SS
1291
1292
c5aa993b 1293 d <addr> <endaddr>\r
c906108c 1294
c5aa993b 1295 and receive
c906108c
SS
1296 <ADDRESS> < D A T A > < ASCII CODE >
1297 00000000 5F FD FD FF DF 7F DF FF 01 00 01 00 02 00 08 04 "_..............."
1298 00000010 FF D7 FF 7F D7 F1 7F FF 00 05 00 00 08 00 40 00 "..............@."
1299 00000020 7F FD FF F7 7F FF FF F7 00 00 00 00 00 00 00 00 "................"
1300
c5aa993b 1301 A cost in chars for each transaction of 80 + 5*n-bytes.
c906108c 1302
c5aa993b
JM
1303 Large transactions could be done with the srecord load code, but
1304 there is a pause for a second before dumping starts, which slows the
1305 average rate down!
1306 */
c906108c
SS
1307
1308static int
fba45db2
KB
1309e7000_read_inferior_memory_large (CORE_ADDR memaddr, unsigned char *myaddr,
1310 int len)
c906108c
SS
1311{
1312 int count;
1313 int c;
1314 char buf[200];
1315
1316 /* Starting address of this pass. */
1317
1318 if (((memaddr - 1) + len) < memaddr)
1319 {
1320 errno = EIO;
1321 return 0;
1322 }
1323
cc17453a 1324 sprintf (buf, "d %lx %lx\r", memaddr, memaddr + len - 1);
c906108c
SS
1325 puts_e7000debug (buf);
1326
1327 count = 0;
1328 c = gch ();
c5aa993b 1329
c906108c 1330 /* skip down to the first ">" */
c5aa993b 1331 while (c != '>')
c906108c
SS
1332 c = gch ();
1333 /* now skip to the end of that line */
c5aa993b 1334 while (c != '\r')
c906108c
SS
1335 c = gch ();
1336 c = gch ();
1337
1338 while (count < len)
1339 {
1340 /* get rid of any white space before the address */
1341 while (c <= ' ')
1342 c = gch ();
1343
1344 /* Skip the address */
1345 get_hex (&c);
1346
1347 /* read in the bytes on the line */
1348 while (c != '"' && count < len)
1349 {
1350 if (c == ' ')
1351 c = gch ();
1352 else
1353 {
1354 myaddr[count++] = get_hex (&c);
1355 }
1356 }
1357 /* throw out the rest of the line */
c5aa993b 1358 while (c != '\r')
c906108c
SS
1359 c = gch ();
1360 }
1361
1362 /* wait for the ":" prompt */
1363 while (c != ':')
1364 c = gch ();
1365
1366 return len;
1367}
1368
1369#if 0
1370
1371static int
fba45db2
KB
1372fast_but_for_the_pause_e7000_read_inferior_memory (CORE_ADDR memaddr,
1373 char *myaddr, int len)
c906108c
SS
1374{
1375 int loop;
1376 int c;
1377 char buf[200];
1378
1379 if (((memaddr - 1) + len) < memaddr)
1380 {
1381 errno = EIO;
1382 return 0;
1383 }
1384
1385 sprintf (buf, "is %x@%x:s\r", memaddr, len);
1386 puts_e7000debug (buf);
1387 gch ();
1388 c = gch ();
1389 if (c != ENQ)
1390 {
1391 /* Got an error */
1392 error ("Memory read error");
1393 }
1394 putchar_e7000 (ACK);
1395 expect ("SV s");
1396 loop = 1;
1397 while (loop)
1398 {
1399 int type;
1400 int length;
1401 int addr;
1402 int i;
1403
1404 c = gch ();
1405 switch (c)
1406 {
1407 case ENQ: /* ENQ, at the end */
1408 loop = 0;
1409 break;
1410 case 'S':
1411 /* Start of an Srecord */
1412 type = gch ();
1413 length = gbyte ();
1414 switch (type)
1415 {
1416 case '7': /* Termination record, ignore */
1417 case '0':
1418 case '8':
1419 case '9':
1420 /* Header record - ignore it */
1421 while (length--)
1422 {
1423 gbyte ();
1424 }
1425 break;
1426 case '1':
1427 case '2':
1428 case '3':
1429 {
1430 int alen;
1431
1432 alen = type - '0' + 1;
1433 addr = 0;
1434 while (alen--)
1435 {
1436 addr = (addr << 8) + gbyte ();
1437 length--;
1438 }
1439
1440 for (i = 0; i < length - 1; i++)
1441 myaddr[i + addr - memaddr] = gbyte ();
1442
1443 gbyte (); /* Ignore checksum */
1444 }
1445 }
1446 }
1447 }
1448
1449 putchar_e7000 (ACK);
1450 expect ("TOP ADDRESS =");
1451 expect ("END ADDRESS =");
1452 expect (":");
1453
1454 return len;
1455}
1456
1457#endif
1458
69dc947a
KB
1459/* Transfer LEN bytes between GDB address MYADDR and target address
1460 MEMADDR. If WRITE is non-zero, transfer them to the target,
1461 otherwise transfer them from the target. TARGET is unused.
1462
1463 Returns the number of bytes transferred. */
1464
c906108c 1465static int
55d80160 1466e7000_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr,
29e57380
C
1467 int len, int write,
1468 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
1469 struct target_ops *target ATTRIBUTE_UNUSED)
c906108c
SS
1470{
1471 if (write)
c5aa993b
JM
1472 return e7000_write_inferior_memory (memaddr, myaddr, len);
1473 else if (len < 16)
1474 return e7000_read_inferior_memory (memaddr, myaddr, len);
1475 else
1476 return e7000_read_inferior_memory_large (memaddr, myaddr, len);
c906108c
SS
1477}
1478
1479static void
55d80160 1480e7000_kill (void)
c906108c
SS
1481{
1482}
1483
1484static void
fba45db2 1485e7000_load (char *args, int from_tty)
c906108c
SS
1486{
1487 struct cleanup *old_chain;
1488 asection *section;
1489 bfd *pbfd;
1490 bfd_vma entry;
1491#define WRITESIZE 0x1000
c5aa993b 1492 char buf[2 + 4 + 4 + WRITESIZE]; /* `DT' + <addr> + <len> + <data> */
c906108c
SS
1493 char *filename;
1494 int quiet;
1495 int nostart;
1496 time_t start_time, end_time; /* Start and end times of download */
1497 unsigned long data_count; /* Number of bytes transferred to memory */
c5aa993b 1498 int oldtimeout = timeout;
c906108c
SS
1499
1500 timeout = remote_timeout;
1501
1502
1503 /* FIXME! change test to test for type of download */
1504 if (!using_tcp)
1505 {
1506 generic_load (args, from_tty);
1507 return;
1508 }
1509
1510 /* for direct tcp connections, we can do a fast binary download */
1511 buf[0] = 'D';
1512 buf[1] = 'T';
1513 quiet = 0;
1514 nostart = 0;
1515 filename = NULL;
1516
1517 while (*args != '\000')
1518 {
1519 char *arg;
1520
c5aa993b
JM
1521 while (isspace (*args))
1522 args++;
c906108c
SS
1523
1524 arg = args;
1525
c5aa993b
JM
1526 while ((*args != '\000') && !isspace (*args))
1527 args++;
c906108c
SS
1528
1529 if (*args != '\000')
1530 *args++ = '\000';
1531
1532 if (*arg != '-')
1533 filename = arg;
1534 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1535 quiet = 1;
1536 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1537 nostart = 1;
1538 else
1539 error ("unknown option `%s'", arg);
1540 }
1541
1542 if (!filename)
1543 filename = get_exec_file (1);
1544
1545 pbfd = bfd_openr (filename, gnutarget);
1546 if (pbfd == NULL)
1547 {
1548 perror_with_name (filename);
1549 return;
1550 }
5c65bbb6 1551 old_chain = make_cleanup_bfd_close (pbfd);
c906108c 1552
c5aa993b 1553 if (!bfd_check_format (pbfd, bfd_object))
c906108c
SS
1554 error ("\"%s\" is not an object file: %s", filename,
1555 bfd_errmsg (bfd_get_error ()));
1556
1557 start_time = time (NULL);
1558 data_count = 0;
1559
1560 puts_e7000debug ("mw\r");
1561
1562 expect ("\nOK");
1563
c5aa993b 1564 for (section = pbfd->sections; section; section = section->next)
c906108c
SS
1565 {
1566 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1567 {
1568 bfd_vma section_address;
1569 bfd_size_type section_size;
1570 file_ptr fptr;
1571
1572 section_address = bfd_get_section_vma (pbfd, section);
1573 section_size = bfd_get_section_size_before_reloc (section);
1574
1575 if (!quiet)
4902674b 1576 printf_filtered ("[Loading section %s at 0x%s (%s bytes)]\n",
c906108c 1577 bfd_get_section_name (pbfd, section),
4902674b
EZ
1578 paddr_nz (section_address),
1579 paddr_u (section_size));
c906108c
SS
1580
1581 fptr = 0;
c5aa993b 1582
c906108c
SS
1583 data_count += section_size;
1584
1585 while (section_size > 0)
1586 {
1587 int count;
1588 static char inds[] = "|/-\\";
1589 static int k = 0;
1590
1591 QUIT;
1592
1593 count = min (section_size, WRITESIZE);
1594
1595 buf[2] = section_address >> 24;
1596 buf[3] = section_address >> 16;
1597 buf[4] = section_address >> 8;
1598 buf[5] = section_address;
1599
1600 buf[6] = count >> 24;
1601 buf[7] = count >> 16;
1602 buf[8] = count >> 8;
1603 buf[9] = count;
1604
1605 bfd_get_section_contents (pbfd, section, buf + 10, fptr, count);
1606
2cd58942 1607 if (serial_write (e7000_desc, buf, count + 10))
c906108c 1608 fprintf_unfiltered (gdb_stderr,
2cd58942 1609 "e7000_load: serial_write failed: %s\n",
c5aa993b 1610 safe_strerror (errno));
c906108c
SS
1611
1612 expect ("OK");
1613
1614 if (!quiet)
1615 {
1616 printf_unfiltered ("\r%c", inds[k++ % 4]);
1617 gdb_flush (gdb_stdout);
1618 }
1619
1620 section_address += count;
1621 fptr += count;
1622 section_size -= count;
1623 }
1624 }
1625 }
1626
1627 write_e7000 ("ED");
1628
1629 expect_prompt ();
1630
1631 end_time = time (NULL);
1632
1633/* Finally, make the PC point at the start address */
1634
1635 if (exec_bfd)
1636 write_pc (bfd_get_start_address (exec_bfd));
1637
39f77062 1638 inferior_ptid = null_ptid; /* No process now */
c906108c
SS
1639
1640/* This is necessary because many things were based on the PC at the time that
1641 we attached to the monitor, which is no longer valid now that we have loaded
1642 new code (and just changed the PC). Another way to do this might be to call
1643 normal_stop, except that the stack may not be valid, and things would get
1644 horribly confused... */
1645
1646 clear_symtab_users ();
1647
1648 if (!nostart)
1649 {
1650 entry = bfd_get_start_address (pbfd);
1651
1652 if (!quiet)
4902674b 1653 printf_unfiltered ("[Starting %s at 0x%s]\n", filename, paddr_nz (entry));
c906108c 1654
c5aa993b 1655/* start_routine (entry); */
c906108c
SS
1656 }
1657
1658 report_transfer_performance (data_count, start_time, end_time);
1659
1660 do_cleanups (old_chain);
1661 timeout = oldtimeout;
1662}
1663
1664/* Clean up when a program exits.
1665
1666 The program actually lives on in the remote processor's RAM, and may be
1667 run again without a download. Don't leave it full of breakpoint
1668 instructions. */
1669
1670static void
fba45db2 1671e7000_mourn_inferior (void)
c906108c
SS
1672{
1673 remove_breakpoints ();
1674 unpush_target (&e7000_ops);
1675 generic_mourn_inferior (); /* Do all the proper things now */
1676}
1677
1678#define MAX_BREAKPOINTS 200
1679#ifdef HARD_BREAKPOINTS
1680#define MAX_E7000DEBUG_BREAKPOINTS (BC_BREAKPOINTS ? 5 : MAX_BREAKPOINTS)
1681#else
1682#define MAX_E7000DEBUG_BREAKPOINTS MAX_BREAKPOINTS
1683#endif
1684
1685/* Since we can change to soft breakpoints dynamically, we must define
1686 more than enough. Was breakaddr[MAX_E7000DEBUG_BREAKPOINTS]. */
c5aa993b
JM
1687static CORE_ADDR breakaddr[MAX_BREAKPOINTS] =
1688{0};
c906108c
SS
1689
1690static int
55d80160 1691e7000_insert_breakpoint (CORE_ADDR addr, char *shadow)
c906108c
SS
1692{
1693 int i;
1694 char buf[200];
1695#if 0
1696 static char nop[2] = NOP;
1697#endif
1698
1699 for (i = 0; i <= MAX_E7000DEBUG_BREAKPOINTS; i++)
1700 if (breakaddr[i] == 0)
1701 {
1702 breakaddr[i] = addr;
1703 /* Save old contents, and insert a nop in the space */
1704#ifdef HARD_BREAKPOINTS
c5aa993b 1705 if (BC_BREAKPOINTS)
c906108c 1706 {
cc17453a 1707 sprintf (buf, "BC%d A=%lx\r", i + 1, addr);
c906108c
SS
1708 puts_e7000debug (buf);
1709 }
c5aa993b 1710 else
c906108c 1711 {
cc17453a 1712 sprintf (buf, "B %lx\r", addr);
c906108c
SS
1713 puts_e7000debug (buf);
1714 }
1715#else
1716#if 0
1717 e7000_read_inferior_memory (addr, shadow, 2);
1718 e7000_write_inferior_memory (addr, nop, 2);
1719#endif
1720
1721 sprintf (buf, "B %x\r", addr);
1722 puts_e7000debug (buf);
1723#endif
1724 expect_prompt ();
1725 return 0;
1726 }
1727
1728 error ("Too many breakpoints ( > %d) for the E7000\n",
1729 MAX_E7000DEBUG_BREAKPOINTS);
1730 return 1;
1731}
1732
1733static int
55d80160 1734e7000_remove_breakpoint (CORE_ADDR addr, char *shadow)
c906108c
SS
1735{
1736 int i;
1737 char buf[200];
1738
1739 for (i = 0; i < MAX_E7000DEBUG_BREAKPOINTS; i++)
1740 if (breakaddr[i] == addr)
1741 {
1742 breakaddr[i] = 0;
1743#ifdef HARD_BREAKPOINTS
c5aa993b 1744 if (BC_BREAKPOINTS)
c906108c 1745 {
c5aa993b 1746 sprintf (buf, "BC%d - \r", i + 1);
c906108c
SS
1747 puts_e7000debug (buf);
1748 }
c5aa993b 1749 else
c906108c 1750 {
cc17453a 1751 sprintf (buf, "B - %lx\r", addr);
c906108c
SS
1752 puts_e7000debug (buf);
1753 }
1754 expect_prompt ();
1755#else
cc17453a 1756 sprintf (buf, "B - %lx\r", addr);
c906108c
SS
1757 puts_e7000debug (buf);
1758 expect_prompt ();
1759
1760#if 0
1761 /* Replace the insn under the break */
1762 e7000_write_inferior_memory (addr, shadow, 2);
1763#endif
1764#endif
1765
1766 return 0;
1767 }
1768
cc17453a 1769 warning ("Can't find breakpoint associated with 0x%lx\n", addr);
c906108c
SS
1770 return 1;
1771}
1772
1773/* Put a command string, in args, out to STDBUG. Output from STDBUG
1774 is placed on the users terminal until the prompt is seen. */
1775
1776static void
fba45db2 1777e7000_command (char *args, int fromtty)
c906108c
SS
1778{
1779 /* FIXME: arbitrary limit on length of args. */
1780 char buf[200];
1781
1782 echo = 0;
1783
1784 if (!e7000_desc)
1785 error ("e7000 target not open.");
1786 if (!args)
1787 {
1788 puts_e7000debug ("\r");
1789 }
1790 else
1791 {
1792 sprintf (buf, "%s\r", args);
1793 puts_e7000debug (buf);
1794 }
1795
1796 echo++;
1797 ctrl_c = 2;
1798 expect_full_prompt ();
1799 echo--;
1800 ctrl_c = 0;
1801 printf_unfiltered ("\n");
1802
1803 /* Who knows what the command did... */
1804 registers_changed ();
1805}
1806
1807
1808static void
fba45db2 1809e7000_drain_command (char *args, int fromtty)
c906108c
SS
1810{
1811 int c;
1812
c5aa993b 1813 puts_e7000debug ("end\r");
c906108c
SS
1814 putchar_e7000 (CTRLC);
1815
1816 while ((c = readchar (1) != -1))
1817 {
1818 if (quit_flag)
1819 {
c5aa993b 1820 putchar_e7000 (CTRLC);
c906108c
SS
1821 quit_flag = 0;
1822 }
1823 if (c > ' ' && c < 127)
1824 printf_unfiltered ("%c", c & 0xff);
1825 else
1826 printf_unfiltered ("<%x>", c & 0xff);
1827 }
1828}
1829
1830#define NITEMS 7
1831
1832static int
fba45db2 1833why_stop (void)
c906108c 1834{
c5aa993b
JM
1835 static char *strings[NITEMS] =
1836 {
c906108c
SS
1837 "STEP NORMAL",
1838 "BREAK POINT",
1839 "BREAK KEY",
1840 "BREAK CONDI",
1841 "CYCLE ACCESS",
1842 "ILLEGAL INSTRUCTION",
1843 "WRITE PROTECT",
1844 };
1845 char *p[NITEMS];
1846 int c;
1847 int i;
1848
1849 for (i = 0; i < NITEMS; ++i)
1850 p[i] = strings[i];
c5aa993b 1851
c906108c
SS
1852 c = gch ();
1853 while (1)
1854 {
1855 for (i = 0; i < NITEMS; i++)
1856 {
c5aa993b 1857 if (c == *(p[i]))
c906108c
SS
1858 {
1859 p[i]++;
c5aa993b
JM
1860 if (*(p[i]) == 0)
1861 {
c906108c
SS
1862 /* found one of the choices */
1863 return i;
1864 }
1865 }
1866 else
1867 p[i] = strings[i];
1868 }
1869
1870 c = gch ();
1871 }
1872}
1873
1874/* Suck characters, if a string match, then return the strings index
1875 otherwise echo them. */
1876
1877int
fba45db2 1878expect_n (char **strings)
c906108c
SS
1879{
1880 char *(ptr[10]);
c5aa993b 1881 int n;
c906108c
SS
1882 int c;
1883 char saveaway[100];
1884 char *buffer = saveaway;
1885 /* Count number of expect strings */
1886
c5aa993b 1887 for (n = 0; strings[n]; n++)
c906108c
SS
1888 {
1889 ptr[n] = strings[n];
1890 }
1891
1892 while (1)
1893 {
1894 int i;
1895 int gotone = 0;
1896
1897 c = readchar (1);
1898 if (c == -1)
1899 {
1900 printf_unfiltered ("[waiting for e7000...]\n");
1901 }
1902#ifdef __GO32__
1903 if (kbhit ())
1904 {
c5aa993b 1905 int k = getkey ();
c906108c
SS
1906
1907 if (k == 1)
1908 quit_flag = 1;
1909 }
1910#endif
1911 if (quit_flag)
1912 {
1913 putchar_e7000 (CTRLC); /* interrupt the running program */
1914 quit_flag = 0;
1915 }
1916
1917 for (i = 0; i < n; i++)
1918 {
c5aa993b 1919 if (c == ptr[i][0])
c906108c
SS
1920 {
1921 ptr[i]++;
1922 if (ptr[i][0] == 0)
1923 {
1924 /* Gone all the way */
1925 return i;
1926 }
1927 gotone = 1;
1928 }
1929 else
1930 {
1931 ptr[i] = strings[i];
1932 }
1933 }
c5aa993b 1934
c906108c
SS
1935 if (gotone)
1936 {
1937 /* Save it up incase we find that there was no match */
c5aa993b 1938 *buffer++ = c;
c906108c
SS
1939 }
1940 else
1941 {
c5aa993b 1942 if (buffer != saveaway)
c906108c
SS
1943 {
1944 *buffer++ = 0;
1945 printf_unfiltered ("%s", buffer);
1946 buffer = saveaway;
1947 }
1948 if (c != -1)
1949 {
1950 putchar_unfiltered (c);
1951 gdb_flush (gdb_stdout);
1952 }
1953 }
1954 }
1955}
1956
1957/* We subtract two from the pc here rather than use
1958 DECR_PC_AFTER_BREAK since the e7000 doesn't always add two to the
1959 pc, and the simulators never do. */
1960
1961static void
fba45db2 1962sub2_from_pc (void)
c906108c
SS
1963{
1964 char buf[4];
1965 char buf2[200];
1966
1967 store_signed_integer (buf,
c5aa993b
JM
1968 REGISTER_RAW_SIZE (PC_REGNUM),
1969 read_register (PC_REGNUM) - 2);
c906108c 1970 supply_register (PC_REGNUM, buf);
4902674b 1971 sprintf (buf2, ".PC %s\r", phex_nz (read_register (PC_REGNUM), 0));
c906108c
SS
1972 puts_e7000debug (buf2);
1973}
1974
1975#define WAS_SLEEP 0
1976#define WAS_INT 1
1977#define WAS_RUNNING 2
1978#define WAS_OTHER 3
1979
c5aa993b
JM
1980static char *estrings[] =
1981{
c906108c
SS
1982 "** SLEEP",
1983 "BREAK !",
1984 "** PC",
1985 "PC",
1986 NULL
1987};
1988
1989/* Wait until the remote machine stops, then return, storing status in
1990 STATUS just as `wait' would. */
1991
39f77062
KB
1992static ptid_t
1993e7000_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c
SS
1994{
1995 int stop_reason;
1996 int regno;
1997 int running_count = 0;
1998 int had_sleep = 0;
1999 int loop = 1;
4902674b 2000 char *wanted_nopc = NULL;
c906108c
SS
2001
2002 /* Then echo chars until PC= string seen */
2003 gch (); /* Drop cr */
2004 gch (); /* and space */
2005
2006 while (loop)
2007 {
2008 switch (expect_n (estrings))
c5aa993b 2009 {
c906108c
SS
2010 case WAS_OTHER:
2011 /* how did this happen ? */
2012 loop = 0;
2013 break;
2014 case WAS_SLEEP:
2015 had_sleep = 1;
2016 putchar_e7000 (CTRLC);
2017 loop = 0;
2018 break;
2019 case WAS_INT:
2020 loop = 0;
2021 break;
2022 case WAS_RUNNING:
2023 running_count++;
2024 if (running_count == 20)
2025 {
2026 printf_unfiltered ("[running...]\n");
2027 running_count = 0;
2028 }
2029 break;
2030 default:
2031 /* error? */
2032 break;
2033 }
2034 }
2035
2036 /* Skip till the PC= */
2037 expect ("=");
2038
c906108c 2039 if (TARGET_ARCHITECTURE->arch == bfd_arch_sh)
cc17453a
EZ
2040 {
2041 wanted_nopc = want_nopc_sh;
2042 switch (TARGET_ARCHITECTURE->mach)
2043 {
2044 case bfd_mach_sh3:
2045 case bfd_mach_sh3e:
2046 case bfd_mach_sh4:
2047 wanted_nopc = want_nopc_sh3;
2048 }
2049 }
2050#ifdef GDB_TARGET_IS_H8300
2051 if (TARGET_ARCHITECTURE->arch == bfd_arch_h8300)
2052 {
2053 if (h8300smode)
2054 wanted_nopc = want_nopc_h8300s;
2055 else
2056 wanted_nopc = want_nopc_h8300h;
2057 }
c906108c
SS
2058#endif
2059 fetch_regs_from_dump (gch, wanted_nopc);
2060
2061 /* And supply the extra ones the simulator uses */
2062 for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
2063 {
2064 int buf = 0;
2065 supply_register (regno, (char *) &buf);
2066 }
2067
2068 stop_reason = why_stop ();
2069 expect_full_prompt ();
2070
2071 status->kind = TARGET_WAITKIND_STOPPED;
2072 status->value.sig = TARGET_SIGNAL_TRAP;
2073
2074 switch (stop_reason)
2075 {
2076 case 1: /* Breakpoint */
c5aa993b
JM
2077 write_pc (read_pc ()); /* PC is always off by 2 for breakpoints */
2078 status->value.sig = TARGET_SIGNAL_TRAP;
c906108c
SS
2079 break;
2080 case 0: /* Single step */
c5aa993b 2081 status->value.sig = TARGET_SIGNAL_TRAP;
c906108c
SS
2082 break;
2083 case 2: /* Interrupt */
2084 if (had_sleep)
2085 {
c5aa993b 2086 status->value.sig = TARGET_SIGNAL_TRAP;
c906108c
SS
2087 sub2_from_pc ();
2088 }
2089 else
2090 {
c5aa993b 2091 status->value.sig = TARGET_SIGNAL_INT;
c906108c
SS
2092 }
2093 break;
2094 case 3:
2095 break;
2096 case 4:
2097 printf_unfiltered ("a cycle address error?\n");
c5aa993b 2098 status->value.sig = TARGET_SIGNAL_UNKNOWN;
c906108c
SS
2099 break;
2100 case 5:
c5aa993b 2101 status->value.sig = TARGET_SIGNAL_ILL;
c906108c
SS
2102 break;
2103 case 6:
c5aa993b 2104 status->value.sig = TARGET_SIGNAL_SEGV;
c906108c
SS
2105 break;
2106 case 7: /* Anything else (NITEMS + 1) */
2107 printf_unfiltered ("a write protect error?\n");
c5aa993b 2108 status->value.sig = TARGET_SIGNAL_UNKNOWN;
c906108c
SS
2109 break;
2110 default:
2111 /* Get the user's attention - this should never happen. */
e1e9e218 2112 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
2113 }
2114
39f77062 2115 return inferior_ptid;
c906108c
SS
2116}
2117
2118/* Stop the running program. */
2119
2120static void
fba45db2 2121e7000_stop (void)
c906108c
SS
2122{
2123 /* Sending a ^C is supposed to stop the running program. */
2124 putchar_e7000 (CTRLC);
2125}
2126
2127/* Define the target subroutine names. */
2128
c5aa993b 2129struct target_ops e7000_ops;
c906108c 2130
c5aa993b
JM
2131static void
2132init_e7000_ops (void)
c906108c 2133{
c5aa993b
JM
2134 e7000_ops.to_shortname = "e7000";
2135 e7000_ops.to_longname = "Remote Hitachi e7000 target";
2136 e7000_ops.to_doc = "Use a remote Hitachi e7000 ICE connected by a serial line;\n\
c906108c
SS
2137or a network connection.\n\
2138Arguments are the name of the device for the serial line,\n\
2139the speed to connect at in bits per second.\n\
2140eg\n\
2141target e7000 /dev/ttya 9600\n\
c5aa993b
JM
2142target e7000 foobar";
2143 e7000_ops.to_open = e7000_open;
2144 e7000_ops.to_close = e7000_close;
2145 e7000_ops.to_attach = 0;
c906108c 2146 e7000_ops.to_post_attach = NULL;
c5aa993b
JM
2147 e7000_ops.to_require_attach = NULL;
2148 e7000_ops.to_detach = e7000_detach;
2149 e7000_ops.to_require_detach = NULL;
2150 e7000_ops.to_resume = e7000_resume;
2151 e7000_ops.to_wait = e7000_wait;
2152 e7000_ops.to_post_wait = NULL;
2153 e7000_ops.to_fetch_registers = e7000_fetch_register;
2154 e7000_ops.to_store_registers = e7000_store_register;
2155 e7000_ops.to_prepare_to_store = e7000_prepare_to_store;
2156 e7000_ops.to_xfer_memory = e7000_xfer_inferior_memory;
2157 e7000_ops.to_files_info = e7000_files_info;
2158 e7000_ops.to_insert_breakpoint = e7000_insert_breakpoint;
2159 e7000_ops.to_remove_breakpoint = e7000_remove_breakpoint;
2160 e7000_ops.to_terminal_init = 0;
2161 e7000_ops.to_terminal_inferior = 0;
2162 e7000_ops.to_terminal_ours_for_output = 0;
2163 e7000_ops.to_terminal_ours = 0;
2164 e7000_ops.to_terminal_info = 0;
2165 e7000_ops.to_kill = e7000_kill;
2166 e7000_ops.to_load = e7000_load;
2167 e7000_ops.to_lookup_symbol = 0;
2168 e7000_ops.to_create_inferior = e7000_create_inferior;
c906108c
SS
2169 e7000_ops.to_post_startup_inferior = NULL;
2170 e7000_ops.to_acknowledge_created_inferior = NULL;
c5aa993b
JM
2171 e7000_ops.to_clone_and_follow_inferior = NULL;
2172 e7000_ops.to_post_follow_inferior_by_clone = NULL;
c906108c
SS
2173 e7000_ops.to_insert_fork_catchpoint = NULL;
2174 e7000_ops.to_remove_fork_catchpoint = NULL;
2175 e7000_ops.to_insert_vfork_catchpoint = NULL;
c5aa993b 2176 e7000_ops.to_remove_vfork_catchpoint = NULL;
c906108c
SS
2177 e7000_ops.to_has_forked = NULL;
2178 e7000_ops.to_has_vforked = NULL;
c5aa993b 2179 e7000_ops.to_can_follow_vfork_prior_to_exec = NULL;
c906108c
SS
2180 e7000_ops.to_post_follow_vfork = NULL;
2181 e7000_ops.to_insert_exec_catchpoint = NULL;
2182 e7000_ops.to_remove_exec_catchpoint = NULL;
2183 e7000_ops.to_has_execd = NULL;
2184 e7000_ops.to_reported_exec_events_per_exec_call = NULL;
2185 e7000_ops.to_has_exited = NULL;
c5aa993b
JM
2186 e7000_ops.to_mourn_inferior = e7000_mourn_inferior;
2187 e7000_ops.to_can_run = 0;
2188 e7000_ops.to_notice_signals = 0;
2189 e7000_ops.to_thread_alive = 0;
2190 e7000_ops.to_stop = e7000_stop;
2191 e7000_ops.to_pid_to_exec_file = NULL;
c5aa993b
JM
2192 e7000_ops.to_stratum = process_stratum;
2193 e7000_ops.DONT_USE = 0;
2194 e7000_ops.to_has_all_memory = 1;
2195 e7000_ops.to_has_memory = 1;
2196 e7000_ops.to_has_stack = 1;
2197 e7000_ops.to_has_registers = 1;
2198 e7000_ops.to_has_execution = 1;
2199 e7000_ops.to_sections = 0;
2200 e7000_ops.to_sections_end = 0;
2201 e7000_ops.to_magic = OPS_MAGIC;
c906108c
SS
2202};
2203
2204void
fba45db2 2205_initialize_remote_e7000 (void)
c906108c 2206{
c5aa993b 2207 init_e7000_ops ();
c906108c
SS
2208 add_target (&e7000_ops);
2209
2210 add_com ("e7000", class_obscure, e7000_command,
2211 "Send a command to the e7000 monitor.");
2212
2213 add_com ("ftplogin", class_obscure, e7000_login_command,
2214 "Login to machine and change to directory.");
2215
2216 add_com ("ftpload", class_obscure, e7000_ftp_command,
2217 "Fetch and load a file from previously described place.");
2218
2219 add_com ("drain", class_obscure, e7000_drain_command,
2220 "Drain pending e7000 text buffers.");
2221
2222 add_show_from_set (add_set_cmd ("usehardbreakpoints", no_class,
c5aa993b
JM
2223 var_integer, (char *) &use_hard_breakpoints,
2224 "Set use of hardware breakpoints for all breakpoints.\n", &setlist),
c906108c
SS
2225 &showlist);
2226}
This page took 0.232319 seconds and 4 git commands to generate.