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