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