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