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