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