]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote-array.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / remote-array.c
CommitLineData
c906108c
SS
1/* Remote debugging interface for Array Tech RAID controller..
2 Copyright 90, 91, 92, 93, 94, 1995, 1998 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
4
5 This module talks to a debug monitor called 'MONITOR', which
6 We communicate with MONITOR via either a direct serial line, or a TCP
7 (or possibly TELNET) stream to a terminal multiplexor,
8 which in turn talks to the target board.
9
c5aa993b
JM
10 This file is part of GDB.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.
c906108c
SS
26 */
27
28#include "defs.h"
29#include "gdbcore.h"
30#include "target.h"
31#include "wait.h"
32#ifdef ANSI_PROTOTYPES
33#include <stdarg.h>
34#else
35#include <varargs.h>
36#endif
37#include <ctype.h>
38#include <signal.h>
39#include <sys/types.h>
40#include "gdb_string.h"
41#include "command.h"
42#include "serial.h"
43#include "monitor.h"
44#include "remote-utils.h"
45
46extern int baud_rate;
47
48#define ARRAY_PROMPT ">> "
49
50#define SWAP_TARGET_AND_HOST(buffer,len) \
51 do \
52 { \
53 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
54 { \
55 char tmp; \
56 char *p = (char *)(buffer); \
57 char *q = ((char *)(buffer)) + len - 1; \
58 for (; p < q; p++, q--) \
59 { \
60 tmp = *q; \
61 *q = *p; \
62 *p = tmp; \
63 } \
64 } \
65 } \
66 while (0)
67
c5aa993b
JM
68static void debuglogs PARAMS ((int, char *,...));
69static void array_open ();
70static void array_close ();
71static void array_detach ();
72static void array_attach ();
73static void array_resume ();
74static void array_fetch_register ();
75static void array_store_register ();
76static void array_fetch_registers ();
77static void array_store_registers ();
78static void array_prepare_to_store ();
79static void array_files_info ();
80static void array_kill ();
81static void array_create_inferior ();
82static void array_mourn_inferior ();
83static void make_gdb_packet ();
84static int array_xfer_memory ();
85static int array_wait ();
86static int array_insert_breakpoint ();
87static int array_remove_breakpoint ();
88static int tohex ();
89static int to_hex ();
90static int from_hex ();
91static int array_send_packet ();
92static int array_get_packet ();
93static unsigned long ascii2hexword ();
94static void hexword2ascii ();
c906108c
SS
95
96extern char *version;
97
98#define LOG_FILE "monitor.log"
99#if defined (LOG_FILE)
100FILE *log_file;
101#endif
102
103static int timeout = 30;
104/* Having this larger than 400 causes us to be incompatible with m68k-stub.c
105 and i386-stub.c. Normally, no one would notice because it only matters
106 for writing large chunks of memory (e.g. in downloads). Also, this needs
107 to be more than 400 if required to hold the registers (see below, where
108 we round it up based on REGISTER_BYTES). */
109#define PBUFSIZ 400
110
111/*
112 * Descriptor for I/O to remote machine. Initialize it to NULL so that
113 * array_open knows that we don't have a file open when the program starts.
114 */
115serial_t array_desc = NULL;
116
117/*
118 * this array of registers need to match the indexes used by GDB. The
119 * whole reason this exists is cause the various ROM monitors use
120 * different strings than GDB does, and doesn't support all the
121 * registers either. So, typing "info reg sp" becomes a "r30".
122 */
123extern char *tmp_mips_processor_type;
c5aa993b 124extern int mips_set_processor_type ();
c906108c 125
c5aa993b 126static struct target_ops array_ops;
c906108c 127
c5aa993b
JM
128static void
129init_array_ops (void)
c906108c 130{
c5aa993b
JM
131 array_ops.to_shortname = "array";
132 array_ops.to_longname =
c906108c 133 "Debug using the standard GDB remote protocol for the Array Tech target.",
c5aa993b 134 array_ops.to_doc =
c906108c 135 "Debug using the standard GDB remote protocol for the Array Tech target.\n\
c5aa993b
JM
136Specify the serial device it is connected to (e.g. /dev/ttya).";
137 array_ops.to_open = array_open;
138 array_ops.to_close = array_close;
139 array_ops.to_attach = NULL;
140 array_ops.to_post_attach = NULL;
c906108c 141 array_ops.to_require_attach = NULL;
c5aa993b 142 array_ops.to_detach = array_detach;
c906108c 143 array_ops.to_require_detach = NULL;
c5aa993b
JM
144 array_ops.to_resume = array_resume;
145 array_ops.to_wait = array_wait;
c906108c 146 array_ops.to_post_wait = NULL;
c5aa993b
JM
147 array_ops.to_fetch_registers = array_fetch_registers;
148 array_ops.to_store_registers = array_store_registers;
149 array_ops.to_prepare_to_store = array_prepare_to_store;
150 array_ops.to_xfer_memory = array_xfer_memory;
151 array_ops.to_files_info = array_files_info;
152 array_ops.to_insert_breakpoint = array_insert_breakpoint;
153 array_ops.to_remove_breakpoint = array_remove_breakpoint;
154 array_ops.to_terminal_init = 0;
155 array_ops.to_terminal_inferior = 0;
156 array_ops.to_terminal_ours_for_output = 0;
157 array_ops.to_terminal_ours = 0;
158 array_ops.to_terminal_info = 0;
159 array_ops.to_kill = array_kill;
160 array_ops.to_load = 0;
161 array_ops.to_lookup_symbol = 0;
162 array_ops.to_create_inferior = array_create_inferior;
c906108c
SS
163 array_ops.to_post_startup_inferior = NULL;
164 array_ops.to_acknowledge_created_inferior = NULL;
c5aa993b
JM
165 array_ops.to_clone_and_follow_inferior = NULL;
166 array_ops.to_post_follow_inferior_by_clone = NULL;
c906108c
SS
167 array_ops.to_insert_fork_catchpoint = NULL;
168 array_ops.to_remove_fork_catchpoint = NULL;
169 array_ops.to_insert_vfork_catchpoint = NULL;
c5aa993b 170 array_ops.to_remove_vfork_catchpoint = NULL;
c906108c 171 array_ops.to_has_forked = NULL;
c5aa993b
JM
172 array_ops.to_has_vforked = NULL;
173 array_ops.to_can_follow_vfork_prior_to_exec = NULL;
c906108c
SS
174 array_ops.to_post_follow_vfork = NULL;
175 array_ops.to_insert_exec_catchpoint = NULL;
176 array_ops.to_remove_exec_catchpoint = NULL;
177 array_ops.to_has_execd = NULL;
178 array_ops.to_reported_exec_events_per_exec_call = NULL;
179 array_ops.to_has_exited = NULL;
c5aa993b
JM
180 array_ops.to_mourn_inferior = array_mourn_inferior;
181 array_ops.to_can_run = 0;
182 array_ops.to_notice_signals = 0;
183 array_ops.to_thread_alive = 0;
184 array_ops.to_stop = 0;
c906108c 185 array_ops.to_pid_to_exec_file = NULL;
c5aa993b
JM
186 array_ops.to_core_file_to_sym_file = NULL;
187 array_ops.to_stratum = process_stratum;
188 array_ops.DONT_USE = 0;
189 array_ops.to_has_all_memory = 1;
190 array_ops.to_has_memory = 1;
191 array_ops.to_has_stack = 1;
192 array_ops.to_has_registers = 1;
193 array_ops.to_has_execution = 1;
194 array_ops.to_sections = 0;
195 array_ops.to_sections_end = 0;
196 array_ops.to_magic = OPS_MAGIC;
c906108c
SS
197};
198
199/*
200 * printf_monitor -- send data to monitor. Works just like printf.
201 */
202static void
203#ifdef ANSI_PROTOTYPES
c5aa993b 204printf_monitor (char *pattern,...)
c906108c 205#else
c5aa993b 206printf_monitor (va_alist)
c906108c
SS
207 va_dcl
208#endif
209{
210 va_list args;
211 char buf[PBUFSIZ];
212 int i;
213
214#ifdef ANSI_PROTOTYPES
c5aa993b 215 va_start (args, pattern);
c906108c
SS
216#else
217 char *pattern;
c5aa993b
JM
218 va_start (args);
219 pattern = va_arg (args, char *);
c906108c
SS
220#endif
221
c5aa993b 222 vsprintf (buf, pattern, args);
c906108c
SS
223
224 debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);
225
c5aa993b 226 if (strlen (buf) > PBUFSIZ)
c906108c 227 error ("printf_monitor(): string too long");
c5aa993b
JM
228 if (SERIAL_WRITE (array_desc, buf, strlen (buf)))
229 fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
c906108c
SS
230}
231/*
232 * write_monitor -- send raw data to monitor.
233 */
234static void
c5aa993b 235write_monitor (data, len)
c906108c
SS
236 char data[];
237 int len;
238{
c5aa993b
JM
239 if (SERIAL_WRITE (array_desc, data, len))
240 fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
241
242 *(data + len + 1) = '\0';
c906108c
SS
243 debuglogs (1, "write_monitor(), Sending: \"%s\".", data);
244
245}
246
247/*
248 * debuglogs -- deal with debugging info to multiple sources. This takes
c5aa993b
JM
249 * two real args, the first one is the level to be compared against
250 * the sr_get_debug() value, the second arg is a printf buffer and args
251 * to be formatted and printed. A CR is added after each string is printed.
c906108c
SS
252 */
253static void
254#ifdef ANSI_PROTOTYPES
c5aa993b 255debuglogs (int level, char *pattern,...)
c906108c 256#else
c5aa993b 257debuglogs (va_alist)
c906108c
SS
258 va_dcl
259#endif
260{
261 va_list args;
262 char *p;
263 unsigned char buf[PBUFSIZ];
264 char newbuf[PBUFSIZ];
265 int i;
266
267#ifdef ANSI_PROTOTYPES
c5aa993b 268 va_start (args, pattern);
c906108c
SS
269#else
270 char *pattern;
271 int level;
c5aa993b
JM
272 va_start (args);
273 level = va_arg (args, int); /* get the debug level */
274 pattern = va_arg (args, char *); /* get the printf style pattern */
c906108c
SS
275#endif
276
c5aa993b
JM
277 if ((level < 0) || (level > 100))
278 {
279 error ("Bad argument passed to debuglogs(), needs debug level");
280 return;
281 }
282
283 vsprintf (buf, pattern, args); /* format the string */
284
c906108c
SS
285 /* convert some characters so it'll look right in the log */
286 p = newbuf;
c5aa993b
JM
287 for (i = 0; buf[i] != '\0'; i++)
288 {
289 if (i > PBUFSIZ)
290 error ("Debug message too long");
291 switch (buf[i])
292 {
293 case '\n': /* newlines */
294 *p++ = '\\';
295 *p++ = 'n';
296 continue;
297 case '\r': /* carriage returns */
298 *p++ = '\\';
299 *p++ = 'r';
300 continue;
301 case '\033': /* escape */
302 *p++ = '\\';
303 *p++ = 'e';
304 continue;
305 case '\t': /* tab */
306 *p++ = '\\';
307 *p++ = 't';
308 continue;
309 case '\b': /* backspace */
310 *p++ = '\\';
311 *p++ = 'b';
312 continue;
313 default: /* no change */
314 *p++ = buf[i];
315 }
c906108c 316
c5aa993b
JM
317 if (buf[i] < 26)
318 { /* modify control characters */
319 *p++ = '^';
320 *p++ = buf[i] + 'A';
321 continue;
322 }
323 if (buf[i] >= 128)
324 { /* modify control characters */
325 *p++ = '!';
326 *p++ = buf[i] + 'A';
327 continue;
328 }
c906108c 329 }
c5aa993b 330 *p = '\0'; /* terminate the string */
c906108c 331
c5aa993b 332 if (sr_get_debug () > level)
c906108c
SS
333 printf_unfiltered ("%s\n", newbuf);
334
c5aa993b
JM
335#ifdef LOG_FILE /* write to the monitor log */
336 if (log_file != 0x0)
337 {
338 fputs (newbuf, log_file);
339 fputc ('\n', log_file);
340 fflush (log_file);
341 }
c906108c
SS
342#endif
343}
344
345/* readchar -- read a character from the remote system, doing all the fancy
c5aa993b 346 * timeout stuff.
c906108c
SS
347 */
348static int
c5aa993b 349readchar (timeout)
c906108c
SS
350 int timeout;
351{
352 int c;
353
c5aa993b 354 c = SERIAL_READCHAR (array_desc, abs (timeout));
c906108c 355
c5aa993b
JM
356 if (sr_get_debug () > 5)
357 {
358 putchar (c & 0x7f);
359 debuglogs (5, "readchar: timeout = %d\n", timeout);
360 }
c906108c
SS
361
362#ifdef LOG_FILE
363 if (isascii (c))
c5aa993b 364 putc (c & 0x7f, log_file);
c906108c
SS
365#endif
366
367 if (c >= 0)
368 return c & 0x7f;
369
c5aa993b
JM
370 if (c == SERIAL_TIMEOUT)
371 {
372 if (timeout <= 0)
373 return c; /* Polls shouldn't generate timeout errors */
374 error ("Timeout reading from remote system.");
c906108c
SS
375#ifdef LOG_FILE
376 fputs ("ERROR: Timeout reading from remote system", log_file);
377#endif
c5aa993b
JM
378 }
379 perror_with_name ("readchar");
c906108c
SS
380}
381
382/*
383 * expect -- scan input from the remote system, until STRING is found.
c5aa993b
JM
384 * If DISCARD is non-zero, then discard non-matching input, else print
385 * it out. Let the user break out immediately.
c906108c
SS
386 */
387static void
388expect (string, discard)
389 char *string;
390 int discard;
391{
392 char *p = string;
393 int c;
394
395
396 debuglogs (1, "Expecting \"%s\".", string);
397
398 immediate_quit = 1;
c5aa993b
JM
399 while (1)
400 {
401 c = readchar (timeout);
402 if (!isascii (c))
403 continue;
404 if (c == *p++)
405 {
406 if (*p == '\0')
407 {
408 immediate_quit = 0;
409 debuglogs (4, "Matched");
410 return;
411 }
412 }
413 else
414 {
415 if (!discard)
416 {
417 fputc_unfiltered (c, gdb_stdout);
418 }
419 p = string;
420 }
c906108c 421 }
c906108c
SS
422}
423
424/* Keep discarding input until we see the MONITOR array_cmds->prompt.
425
426 The convention for dealing with the expect_prompt is that you
427 o give your command
428 o *then* wait for the expect_prompt.
429
430 Thus the last thing that a procedure does with the serial line
431 will be an expect_prompt(). Exception: array_resume does not
432 wait for the expect_prompt, because the terminal is being handed over
433 to the inferior. However, the next thing which happens after that
434 is a array_wait which does wait for the expect_prompt.
435 Note that this includes abnormal exit, e.g. error(). This is
436 necessary to prevent getting into states from which we can't
437 recover. */
438static void
c5aa993b 439expect_prompt (discard)
c906108c
SS
440 int discard;
441{
442 expect (ARRAY_PROMPT, discard);
443}
444
445/*
446 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
447 */
448static int
c5aa993b 449junk (ch)
c906108c
SS
450 char ch;
451{
c5aa993b
JM
452 switch (ch)
453 {
454 case '\0':
455 case ' ':
456 case '-':
457 case '\t':
458 case '\r':
459 case '\n':
460 if (sr_get_debug () > 5)
461 debuglogs (5, "Ignoring \'%c\'.", ch);
462 return 1;
463 default:
464 if (sr_get_debug () > 5)
465 debuglogs (5, "Accepting \'%c\'.", ch);
466 return 0;
467 }
c906108c
SS
468}
469
470/*
471 * get_hex_digit -- Get a hex digit from the remote system & return its value.
c5aa993b 472 * If ignore is nonzero, ignore spaces, newline & tabs.
c906108c
SS
473 */
474static int
c5aa993b 475get_hex_digit (ignore)
c906108c
SS
476 int ignore;
477{
478 static int ch;
c5aa993b
JM
479 while (1)
480 {
481 ch = readchar (timeout);
482 if (junk (ch))
483 continue;
484 if (sr_get_debug () > 4)
485 {
486 debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
487 }
488 else
489 {
490#ifdef LOG_FILE /* write to the monitor log */
491 if (log_file != 0x0)
492 {
493 fputs ("get_hex_digit() got a 0x", log_file);
494 fputc (ch, log_file);
495 fputc ('\n', log_file);
496 fflush (log_file);
497 }
c906108c 498#endif
c5aa993b 499 }
c906108c 500
c5aa993b
JM
501 if (ch >= '0' && ch <= '9')
502 return ch - '0';
503 else if (ch >= 'A' && ch <= 'F')
504 return ch - 'A' + 10;
505 else if (ch >= 'a' && ch <= 'f')
506 return ch - 'a' + 10;
507 else if (ch == ' ' && ignore)
508 ;
509 else
510 {
511 expect_prompt (1);
512 debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch);
513 error ("Invalid hex digit from remote system. (0x%x)", ch);
514 }
c906108c 515 }
c906108c
SS
516}
517
518/* get_hex_byte -- Get a byte from monitor and put it in *BYT.
c5aa993b 519 * Accept any number leading spaces.
c906108c
SS
520 */
521static void
522get_hex_byte (byt)
523 char *byt;
524{
525 int val;
526
527 val = get_hex_digit (1) << 4;
528 debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val);
c5aa993b 529
c906108c
SS
530 val |= get_hex_digit (0);
531 debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val);
532 *byt = val;
c5aa993b 533
c906108c
SS
534 debuglogs (4, "get_hex_byte() -- Read a 0x%x", val);
535}
536
537/*
538 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
c5aa993b 539 * and put them in registers starting at REGNO.
c906108c
SS
540 */
541static int
542get_hex_word ()
543{
544 long val, newval;
545 int i;
546
547 val = 0;
548
549#if 0
c5aa993b
JM
550 if (HOST_BYTE_ORDER == BIG_ENDIAN)
551 {
c906108c 552#endif
c5aa993b
JM
553 for (i = 0; i < 8; i++)
554 val = (val << 4) + get_hex_digit (i == 0);
c906108c 555#if 0
c5aa993b
JM
556 }
557 else
558 {
559 for (i = 7; i >= 0; i--)
560 val = (val << 4) + get_hex_digit (i == 0);
561 }
c906108c
SS
562#endif
563
564 debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val, (HOST_BYTE_ORDER == BIG_ENDIAN) ? "big endian" : "little endian");
565
566 return val;
567}
568
569/* This is called not only when we first attach, but also when the
570 user types "run" after having attached. */
571static void
572array_create_inferior (execfile, args, env)
573 char *execfile;
574 char *args;
575 char **env;
576{
577 int entry_pt;
578
579 if (args && *args)
c5aa993b 580 error ("Can't pass arguments to remote MONITOR process");
c906108c
SS
581
582 if (execfile == 0 || exec_bfd == 0)
c5aa993b 583 error ("No executable file specified");
c906108c
SS
584
585 entry_pt = (int) bfd_get_start_address (exec_bfd);
586
587/* The "process" (board) is already stopped awaiting our commands, and
588 the program is already downloaded. We just set its PC and go. */
589
590 clear_proceed_status ();
591
592 /* Tell wait_for_inferior that we've started a new process. */
593 init_wait_for_inferior ();
594
595 /* Set up the "saved terminal modes" of the inferior
596 based on what modes we are starting it with. */
597 target_terminal_init ();
598
599 /* Install inferior's terminal modes. */
600 target_terminal_inferior ();
601
602 /* insert_step_breakpoint (); FIXME, do we need this? */
603
604 /* Let 'er rip... */
c5aa993b 605 proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);
c906108c
SS
606}
607
608/*
609 * array_open -- open a connection to a remote debugger.
c5aa993b 610 * NAME is the filename used for communication.
c906108c
SS
611 */
612static int baudrate = 9600;
613static char dev_name[100];
614
615static void
c5aa993b 616array_open (args, name, from_tty)
c906108c
SS
617 char *args;
618 char *name;
619 int from_tty;
620{
621 char packet[PBUFSIZ];
622
623 if (args == NULL)
624 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
625`target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
626
627/* if (is_open) */
c5aa993b 628 array_close (0);
c906108c
SS
629
630 target_preopen (from_tty);
631 unpush_target (&array_ops);
632
633 tmp_mips_processor_type = "lsi33k"; /* change the default from r3051 */
634 mips_set_processor_type_command ("lsi33k", 0);
635
c5aa993b
JM
636 strcpy (dev_name, args);
637 array_desc = SERIAL_OPEN (dev_name);
c906108c
SS
638
639 if (array_desc == NULL)
c5aa993b 640 perror_with_name (dev_name);
c906108c 641
c5aa993b
JM
642 if (baud_rate != -1)
643 {
644 if (SERIAL_SETBAUDRATE (array_desc, baud_rate))
645 {
646 SERIAL_CLOSE (array_desc);
647 perror_with_name (name);
648 }
c906108c 649 }
c5aa993b
JM
650
651 SERIAL_RAW (array_desc);
c906108c
SS
652
653#if defined (LOG_FILE)
654 log_file = fopen (LOG_FILE, "w");
655 if (log_file == NULL)
656 perror_with_name (LOG_FILE);
657 fprintf (log_file, "GDB %s (%s", version);
658 fprintf (log_file, " --target %s)\n", array_ops.to_shortname);
659 fprintf (log_file, "Remote target %s connected to %s\n\n", array_ops.to_shortname, dev_name);
660#endif
661
662 /* see if the target is alive. For a ROM monitor, we can just try to force the
663 expect_prompt to print a few times. For the GDB remote protocol, the application
664 being debugged is sitting at a breakpoint and waiting for GDB to initialize
665 the connection. We force it to give us an empty packet to see if it's alive.
c5aa993b
JM
666 */
667 debuglogs (3, "Trying to ACK the target's debug stub");
668 /* unless your are on the new hardware, the old board won't initialize
669 because the '@' doesn't flush output like it does on the new ROMS.
670 */
671 printf_monitor ("@"); /* ask for the last signal */
672 expect_prompt (1); /* See if we get a expect_prompt */
c906108c 673#ifdef TEST_ARRAY /* skip packet for testing */
c5aa993b
JM
674 make_gdb_packet (packet, "?"); /* ask for a bogus packet */
675 if (array_send_packet (packet) == 0)
676 error ("Couldn't transmit packet\n");
677 printf_monitor ("@\n"); /* force it to flush stdout */
678 expect_prompt (1); /* See if we get a expect_prompt */
c906108c
SS
679#endif
680 push_target (&array_ops);
681 if (from_tty)
c5aa993b 682 printf ("Remote target %s connected to %s\n", array_ops.to_shortname, dev_name);
c906108c
SS
683}
684
685/*
686 * array_close -- Close out all files and local state before this
c5aa993b 687 * target loses control.
c906108c
SS
688 */
689
690static void
691array_close (quitting)
692 int quitting;
693{
c5aa993b 694 SERIAL_CLOSE (array_desc);
c906108c
SS
695 array_desc = NULL;
696
697 debuglogs (1, "array_close (quitting=%d)", quitting);
698
699#if defined (LOG_FILE)
c5aa993b
JM
700 if (log_file)
701 {
702 if (ferror (log_file))
703 printf_filtered ("Error writing log file.\n");
704 if (fclose (log_file) != 0)
705 printf_filtered ("Error closing log file.\n");
706 }
c906108c
SS
707#endif
708}
709
710/*
711 * array_detach -- terminate the open connection to the remote
c5aa993b
JM
712 * debugger. Use this when you want to detach and do something
713 * else with your gdb.
c906108c
SS
714 */
715static void
716array_detach (from_tty)
717 int from_tty;
718{
719
720 debuglogs (1, "array_detach ()");
721
c5aa993b 722 pop_target (); /* calls array_close to do the real work */
c906108c
SS
723 if (from_tty)
724 printf ("Ending remote %s debugging\n", target_shortname);
725}
726
727/*
728 * array_attach -- attach GDB to the target.
729 */
730static void
731array_attach (args, from_tty)
732 char *args;
733 int from_tty;
734{
735 if (from_tty)
736 printf ("Starting remote %s debugging\n", target_shortname);
c5aa993b 737
c906108c 738 debuglogs (1, "array_attach (args=%s)", args);
c5aa993b 739
c906108c
SS
740 printf_monitor ("go %x\n");
741 /* swallow the echo. */
742 expect ("go %x\n", 1);
743}
c5aa993b 744
c906108c
SS
745/*
746 * array_resume -- Tell the remote machine to resume.
747 */
748static void
749array_resume (pid, step, sig)
750 int pid, step;
751 enum target_signal sig;
752{
753 debuglogs (1, "array_resume (step=%d, sig=%d)", step, sig);
754
c5aa993b
JM
755 if (step)
756 {
757 printf_monitor ("s\n");
758 }
759 else
760 {
761 printf_monitor ("go\n");
762 }
c906108c
SS
763}
764
765#define TMPBUFSIZ 5
766
767/*
768 * array_wait -- Wait until the remote machine stops, then return,
769 * storing status in status just as `wait' would.
770 */
771static int
772array_wait (pid, status)
773 int pid;
774 struct target_waitstatus *status;
775{
776 int old_timeout = timeout;
777 int result, i;
778 char c;
779 serial_t tty_desc;
780 serial_ttystate ttystate;
781
c5aa993b
JM
782 debuglogs (1, "array_wait (), printing extraneous text.");
783
c906108c
SS
784 status->kind = TARGET_WAITKIND_EXITED;
785 status->value.integer = 0;
786
c5aa993b
JM
787 timeout = 0; /* Don't time out -- user program is running. */
788
c906108c
SS
789#if !defined(__GO32__) && !defined(__MSDOS__) && !defined(_WIN32)
790 tty_desc = SERIAL_FDOPEN (0);
791 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
792 SERIAL_RAW (tty_desc);
793
794 i = 0;
795 /* poll on the serial port and the keyboard. */
c5aa993b
JM
796 while (1)
797 {
798 c = readchar (timeout);
799 if (c > 0)
800 {
801 if (c == *(ARRAY_PROMPT + i))
802 {
803 if (++i >= strlen (ARRAY_PROMPT))
804 { /* matched the prompt */
805 debuglogs (4, "array_wait(), got the expect_prompt.");
806 break;
807 }
808 }
809 else
810 { /* not the prompt */
811 i = 0;
812 }
813 fputc_unfiltered (c, gdb_stdout);
814 gdb_flush (gdb_stdout);
c906108c 815 }
c5aa993b
JM
816 c = SERIAL_READCHAR (tty_desc, timeout);
817 if (c > 0)
818 {
819 SERIAL_WRITE (array_desc, &c, 1);
820 /* do this so it looks like there's keyboard echo */
821 if (c == 3) /* exit on Control-C */
822 break;
c906108c 823#if 0
c5aa993b
JM
824 fputc_unfiltered (c, gdb_stdout);
825 gdb_flush (gdb_stdout);
c906108c 826#endif
c5aa993b 827 }
c906108c 828 }
c906108c
SS
829 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
830#else
c5aa993b 831 expect_prompt (1);
c906108c
SS
832 debuglogs (4, "array_wait(), got the expect_prompt.");
833#endif
834
835 status->kind = TARGET_WAITKIND_STOPPED;
836 status->value.sig = TARGET_SIGNAL_TRAP;
837
838 timeout = old_timeout;
839
840 return 0;
841}
842
843/*
844 * array_fetch_registers -- read the remote registers into the
c5aa993b 845 * block regs.
c906108c
SS
846 */
847static void
848array_fetch_registers (ignored)
849 int ignored;
850{
851 int regno, i;
852 char *p;
853 unsigned char packet[PBUFSIZ];
854 char regs[REGISTER_BYTES];
855
856 debuglogs (1, "array_fetch_registers (ignored=%d)\n", ignored);
857
858 memset (packet, 0, PBUFSIZ);
859 /* Unimplemented registers read as all bits zero. */
860 memset (regs, 0, REGISTER_BYTES);
861 make_gdb_packet (packet, "g");
862 if (array_send_packet (packet) == 0)
863 error ("Couldn't transmit packet\n");
864 if (array_get_packet (packet) == 0)
c5aa993b 865 error ("Couldn't receive packet\n");
c906108c
SS
866 /* FIXME: read bytes from packet */
867 debuglogs (4, "array_fetch_registers: Got a \"%s\" back\n", packet);
c5aa993b
JM
868 for (regno = 0; regno <= PC_REGNUM + 4; regno++)
869 {
870 /* supply register stores in target byte order, so swap here */
871 /* FIXME: convert from ASCII hex to raw bytes */
872 i = ascii2hexword (packet + (regno * 8));
873 debuglogs (5, "Adding register %d = %x\n", regno, i);
874 SWAP_TARGET_AND_HOST (&i, 4);
875 supply_register (regno, (char *) &i);
876 }
c906108c
SS
877}
878
879/*
880 * This is unused by targets like this one that use a
881 * protocol based on GDB's remote protocol.
882 */
883static void
884array_fetch_register (ignored)
885 int ignored;
886{
887 array_fetch_registers ();
888}
889
890/*
891 * Get all the registers from the targets. They come back in a large array.
892 */
893static void
894array_store_registers (ignored)
895 int ignored;
896{
897 int regno;
898 unsigned long i;
899 char packet[PBUFSIZ];
900 char buf[PBUFSIZ];
901 char num[9];
c5aa993b 902
c906108c
SS
903 debuglogs (1, "array_store_registers()");
904
905 memset (packet, 0, PBUFSIZ);
906 memset (buf, 0, PBUFSIZ);
907 buf[0] = 'G';
908
909 /* Unimplemented registers read as all bits zero. */
910 /* FIXME: read bytes from packet */
c5aa993b
JM
911 for (regno = 0; regno < 41; regno++)
912 { /* FIXME */
913 /* supply register stores in target byte order, so swap here */
914 /* FIXME: convert from ASCII hex to raw bytes */
915 i = (unsigned long) read_register (regno);
916 hexword2ascii (num, i);
917 strcpy (buf + (regno * 8) + 1, num);
918 }
c906108c
SS
919 *(buf + (regno * 8) + 2) = 0;
920 make_gdb_packet (packet, buf);
921 if (array_send_packet (packet) == 0)
922 error ("Couldn't transmit packet\n");
923 if (array_get_packet (packet) == 0)
c5aa993b
JM
924 error ("Couldn't receive packet\n");
925
c906108c
SS
926 registers_changed ();
927}
928
929/*
930 * This is unused by targets like this one that use a
931 * protocol based on GDB's remote protocol.
932 */
933static void
934array_store_register (ignored)
935 int ignored;
936{
937 array_store_registers ();
938}
939
940/* Get ready to modify the registers array. On machines which store
941 individual registers, this doesn't need to do anything. On machines
942 which store all the registers in one fell swoop, this makes sure
943 that registers contains all the registers from the program being
944 debugged. */
945
946static void
947array_prepare_to_store ()
948{
949 /* Do nothing, since we can store individual regs */
950}
951
952static void
953array_files_info ()
954{
955 printf ("\tAttached to %s at %d baud.\n",
956 dev_name, baudrate);
957}
958
959/*
960 * array_write_inferior_memory -- Copy LEN bytes of data from debugger
c5aa993b 961 * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved.
c906108c
SS
962 */
963static int
964array_write_inferior_memory (memaddr, myaddr, len)
965 CORE_ADDR memaddr;
966 unsigned char *myaddr;
967 int len;
968{
969 unsigned long i;
970 int j;
971 char packet[PBUFSIZ];
972 char buf[PBUFSIZ];
973 char num[9];
974 char *p;
c5aa993b 975
c906108c 976 debuglogs (1, "array_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
c5aa993b 977 memset (buf, '\0', PBUFSIZ); /* this also sets the string terminator */
c906108c
SS
978 p = buf;
979
c5aa993b 980 *p++ = 'M'; /* The command to write memory */
c906108c 981 hexword2ascii (num, memaddr); /* convert the address */
c5aa993b 982 strcpy (p, num); /* copy the address */
c906108c 983 p += 8;
c5aa993b
JM
984 *p++ = ','; /* add comma delimeter */
985 hexword2ascii (num, len); /* Get the length as a 4 digit number */
c906108c
SS
986 *p++ = num[4];
987 *p++ = num[5];
988 *p++ = num[6];
989 *p++ = num[7];
c5aa993b
JM
990 *p++ = ':'; /* add the colon delimeter */
991 for (j = 0; j < len; j++)
992 { /* copy the data in after converting it */
993 *p++ = tohex ((myaddr[j] >> 4) & 0xf);
994 *p++ = tohex (myaddr[j] & 0xf);
995 }
996
c906108c
SS
997 make_gdb_packet (packet, buf);
998 if (array_send_packet (packet) == 0)
999 error ("Couldn't transmit packet\n");
1000 if (array_get_packet (packet) == 0)
c5aa993b 1001 error ("Couldn't receive packet\n");
c906108c
SS
1002
1003 return len;
1004}
1005
1006/*
1007 * array_read_inferior_memory -- read LEN bytes from inferior memory
c5aa993b
JM
1008 * at MEMADDR. Put the result at debugger address MYADDR. Returns
1009 * length moved.
c906108c
SS
1010 */
1011static int
c5aa993b 1012array_read_inferior_memory (memaddr, myaddr, len)
c906108c
SS
1013 CORE_ADDR memaddr;
1014 char *myaddr;
1015 int len;
1016{
1017 int j;
1018 char buf[20];
1019 char packet[PBUFSIZ];
1020 int count; /* Number of bytes read so far. */
1021 unsigned long startaddr; /* Starting address of this pass. */
1022 int len_this_pass; /* Number of bytes to read in this pass. */
1023
1024 debuglogs (1, "array_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
1025
1026 /* Note that this code works correctly if startaddr is just less
1027 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
1028 thing). That is, something like
1029 array_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
1030 works--it never adds len To memaddr and gets 0. */
1031 /* However, something like
1032 array_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
1033 doesn't need to work. Detect it and give up if there's an attempt
1034 to do that. */
c5aa993b
JM
1035 if (((memaddr - 1) + len) < memaddr)
1036 {
1037 errno = EIO;
1038 return 0;
1039 }
1040
c906108c
SS
1041 for (count = 0, startaddr = memaddr; count < len; startaddr += len_this_pass)
1042 {
1043 /* Try to align to 16 byte boundry (why?) */
1044 len_this_pass = 16;
1045 if ((startaddr % 16) != 0)
1046 {
1047 len_this_pass -= startaddr % 16;
1048 }
1049 /* Only transfer bytes we need */
1050 if (len_this_pass > (len - count))
1051 {
1052 len_this_pass = (len - count);
1053 }
1054 /* Fetch the bytes */
1055 debuglogs (3, "read %d bytes from inferior address %x", len_this_pass,
1056 startaddr);
1057 sprintf (buf, "m%08x,%04x", startaddr, len_this_pass);
1058 make_gdb_packet (packet, buf);
1059 if (array_send_packet (packet) == 0)
1060 {
1061 error ("Couldn't transmit packet\n");
1062 }
1063 if (array_get_packet (packet) == 0)
1064 {
c5aa993b 1065 error ("Couldn't receive packet\n");
c906108c
SS
1066 }
1067 if (*packet == 0)
1068 {
1069 error ("Got no data in the GDB packet\n");
1070 }
1071 /* Pick packet apart and xfer bytes to myaddr */
1072 debuglogs (4, "array_read_inferior_memory: Got a \"%s\" back\n", packet);
c5aa993b 1073 for (j = 0; j < len_this_pass; j++)
c906108c
SS
1074 {
1075 /* extract the byte values */
c5aa993b
JM
1076 myaddr[count++] = from_hex (*(packet + (j * 2))) * 16 + from_hex (*(packet + (j * 2) + 1));
1077 debuglogs (5, "myaddr[%d] set to %x\n", count - 1, myaddr[count - 1]);
c906108c
SS
1078 }
1079 }
1080 return (count);
1081}
1082
1083/* FIXME-someday! merge these two. */
1084static int
1085array_xfer_memory (memaddr, myaddr, len, write, target)
1086 CORE_ADDR memaddr;
1087 char *myaddr;
1088 int len;
1089 int write;
c5aa993b 1090 struct target_ops *target; /* ignored */
c906108c
SS
1091{
1092 if (write)
1093 return array_write_inferior_memory (memaddr, myaddr, len);
1094 else
1095 return array_read_inferior_memory (memaddr, myaddr, len);
1096}
1097
1098static void
1099array_kill (args, from_tty)
1100 char *args;
1101 int from_tty;
1102{
c5aa993b 1103 return; /* ignore attempts to kill target system */
c906108c
SS
1104}
1105
1106/* Clean up when a program exits.
1107 The program actually lives on in the remote processor's RAM, and may be
1108 run again without a download. Don't leave it full of breakpoint
1109 instructions. */
1110
1111static void
1112array_mourn_inferior ()
1113{
1114 remove_breakpoints ();
1115 generic_mourn_inferior (); /* Do all the proper things now */
1116}
1117
1118#define MAX_ARRAY_BREAKPOINTS 16
1119
c5aa993b
JM
1120static CORE_ADDR breakaddr[MAX_ARRAY_BREAKPOINTS] =
1121{0};
c906108c
SS
1122
1123/*
1124 * array_insert_breakpoint -- add a breakpoint
1125 */
1126static int
1127array_insert_breakpoint (addr, shadow)
1128 CORE_ADDR addr;
1129 char *shadow;
1130{
1131 int i;
1132 int bp_size = 0;
1133 CORE_ADDR bp_addr = addr;
1134
1135 debuglogs (1, "array_insert_breakpoint() addr = 0x%x", addr);
1136 BREAKPOINT_FROM_PC (&bp_addr, &bp_size);
1137
c5aa993b
JM
1138 for (i = 0; i <= MAX_ARRAY_BREAKPOINTS; i++)
1139 {
1140 if (breakaddr[i] == 0)
1141 {
1142 breakaddr[i] = addr;
1143 if (sr_get_debug () > 4)
1144 printf ("Breakpoint at %x\n", addr);
1145 array_read_inferior_memory (bp_addr, shadow, bp_size);
1146 printf_monitor ("b 0x%x\n", addr);
1147 expect_prompt (1);
1148 return 0;
1149 }
c906108c 1150 }
c906108c 1151
c5aa993b 1152 fprintf (stderr, "Too many breakpoints (> 16) for monitor\n");
c906108c
SS
1153 return 1;
1154}
1155
1156/*
1157 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
1158 */
1159static int
1160array_remove_breakpoint (addr, shadow)
1161 CORE_ADDR addr;
1162 char *shadow;
1163{
1164 int i;
1165
1166 debuglogs (1, "array_remove_breakpoint() addr = 0x%x", addr);
1167
c5aa993b
JM
1168 for (i = 0; i < MAX_ARRAY_BREAKPOINTS; i++)
1169 {
1170 if (breakaddr[i] == addr)
1171 {
1172 breakaddr[i] = 0;
1173 /* some monitors remove breakpoints based on the address */
1174 printf_monitor ("bd %x\n", i);
1175 expect_prompt (1);
1176 return 0;
1177 }
c906108c 1178 }
c5aa993b 1179 fprintf (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
c906108c
SS
1180 return 1;
1181}
1182
1183static void
1184array_stop ()
1185{
1186 debuglogs (1, "array_stop()");
c5aa993b
JM
1187 printf_monitor ("\003");
1188 expect_prompt (1);
c906108c
SS
1189}
1190
1191/*
1192 * array_command -- put a command string, in args, out to MONITOR.
c5aa993b
JM
1193 * Output from MONITOR is placed on the users terminal until the
1194 * expect_prompt is seen. FIXME
c906108c
SS
1195 */
1196static void
1197monitor_command (args, fromtty)
c5aa993b
JM
1198 char *args;
1199 int fromtty;
c906108c
SS
1200{
1201 debuglogs (1, "monitor_command (args=%s)", args);
1202
1203 if (array_desc == NULL)
c5aa993b 1204 error ("monitor target not open.");
c906108c
SS
1205
1206 if (!args)
c5aa993b
JM
1207 error ("Missing command.");
1208
c906108c 1209 printf_monitor ("%s\n", args);
c5aa993b 1210 expect_prompt (0);
c906108c
SS
1211}
1212
1213/*
1214 * make_gdb_packet -- make a GDB packet. The data is always ASCII.
c5aa993b
JM
1215 * A debug packet whose contents are <data>
1216 * is encapsulated for transmission in the form:
c906108c 1217 *
c5aa993b 1218 * $ <data> # CSUM1 CSUM2
c906108c
SS
1219 *
1220 * <data> must be ASCII alphanumeric and cannot include characters
1221 * '$' or '#'. If <data> starts with two characters followed by
1222 * ':', then the existing stubs interpret this as a sequence number.
1223 *
1224 * CSUM1 and CSUM2 are ascii hex representation of an 8-bit
1225 * checksum of <data>, the most significant nibble is sent first.
1226 * the hex digits 0-9,a-f are used.
1227 *
1228 */
1229static void
1230make_gdb_packet (buf, data)
1231 char *buf, *data;
1232{
1233 int i;
1234 unsigned char csum = 0;
1235 int cnt;
1236 char *p;
1237
1238 debuglogs (3, "make_gdb_packet(%s)\n", data);
c5aa993b 1239 cnt = strlen (data);
c906108c
SS
1240 if (cnt > PBUFSIZ)
1241 error ("make_gdb_packet(): to much data\n");
1242
1243 /* start with the packet header */
1244 p = buf;
1245 *p++ = '$';
1246
1247 /* calculate the checksum */
c5aa993b
JM
1248 for (i = 0; i < cnt; i++)
1249 {
1250 csum += data[i];
1251 *p++ = data[i];
1252 }
c906108c
SS
1253
1254 /* terminate the data with a '#' */
1255 *p++ = '#';
c5aa993b 1256
c906108c
SS
1257 /* add the checksum as two ascii digits */
1258 *p++ = tohex ((csum >> 4) & 0xf);
1259 *p++ = tohex (csum & 0xf);
c5aa993b 1260 *p = 0x0; /* Null terminator on string */
c906108c
SS
1261}
1262
1263/*
1264 * array_send_packet -- send a GDB packet to the target with error handling. We
c5aa993b
JM
1265 * get a '+' (ACK) back if the packet is received and the checksum
1266 * matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a
1267 * successful transmition, or a 0 for a failure.
c906108c
SS
1268 */
1269static int
1270array_send_packet (packet)
1271 char *packet;
1272{
1273 int c, retries, i;
1274 char junk[PBUFSIZ];
1275
1276 retries = 0;
1277
1278#if 0
1279 /* scan the packet to make sure it only contains valid characters.
1280 this may sound silly, but sometimes a garbled packet will hang
1281 the target board. We scan the whole thing, then print the error
1282 message.
c5aa993b
JM
1283 */
1284 for (i = 0; i < strlen (packet); i++)
1285 {
1286 debuglogs (5, "array_send_packet(): Scanning \'%c\'\n", packet[i]);
1287 /* legit hex numbers or command */
1288 if ((isxdigit (packet[i])) || (isalpha (packet[i])))
1289 continue;
1290 switch (packet[i])
1291 {
1292 case '+': /* ACK */
1293 case '-': /* NAK */
1294 case '#': /* end of packet */
1295 case '$': /* start of packet */
1296 continue;
1297 default: /* bogus character */
1298 retries++;
1299 debuglogs (4, "array_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet[i]);
1300 }
c906108c 1301 }
c5aa993b 1302#endif
c906108c
SS
1303
1304 if (retries > 0)
1305 error ("Can't send packet, found %d non-ascii characters", retries);
1306
1307 /* ok, try to send the packet */
1308 retries = 0;
c5aa993b
JM
1309 while (retries++ <= 10)
1310 {
1311 printf_monitor ("%s", packet);
1312
1313 /* read until either a timeout occurs (-2) or '+' is read */
1314 while (retries <= 10)
1315 {
1316 c = readchar (-timeout);
1317 debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c);
1318 switch (c)
1319 {
1320 case '+':
1321 debuglogs (3, "Got Ack\n");
1322 return 1;
1323 case SERIAL_TIMEOUT:
1324 debuglogs (3, "Timed out reading serial port\n");
1325 printf_monitor ("@"); /* resync with the monitor */
1326 expect_prompt (1); /* See if we get a expect_prompt */
1327 break; /* Retransmit buffer */
1328 case '-':
1329 debuglogs (3, "Got NAK\n");
1330 printf_monitor ("@"); /* resync with the monitor */
1331 expect_prompt (1); /* See if we get a expect_prompt */
1332 break;
1333 case '$':
1334 /* it's probably an old response, or the echo of our command.
1335 * just gobble up the packet and ignore it.
1336 */
1337 debuglogs (3, "Got a junk packet\n");
1338 i = 0;
1339 do
1340 {
1341 c = readchar (timeout);
1342 junk[i++] = c;
1343 }
1344 while (c != '#');
1345 c = readchar (timeout);
1346 junk[i++] = c;
1347 c = readchar (timeout);
1348 junk[i++] = c;
1349 junk[i++] = '\0';
1350 debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk);
1351 continue; /* Now, go look for next packet */
1352 default:
1353 continue;
1354 }
1355 retries++;
1356 debuglogs (3, "Retransmitting packet \"%s\"\n", packet);
1357 break; /* Here to retransmit */
1358 }
1359 } /* outer while */
c906108c
SS
1360 return 0;
1361}
1362
1363/*
1364 * array_get_packet -- get a GDB packet from the target. Basically we read till we
c5aa993b
JM
1365 * see a '#', then check the checksum. It returns a 1 if it's gotten a
1366 * packet, or a 0 it the packet wasn't transmitted correctly.
c906108c
SS
1367 */
1368static int
1369array_get_packet (packet)
1370 char *packet;
1371{
1372 int c;
1373 int retries;
1374 unsigned char csum;
1375 unsigned char pktcsum;
1376 char *bp;
1377
1378 csum = 0;
1379 bp = packet;
1380
1381 memset (packet, 1, PBUFSIZ);
1382 retries = 0;
c5aa993b
JM
1383 while (retries <= 10)
1384 {
1385 do
1386 {
1387 c = readchar (timeout);
1388 if (c == SERIAL_TIMEOUT)
1389 {
1390 debuglogs (3, "array_get_packet: got time out from serial port.\n");
1391 }
1392 debuglogs (3, "Waiting for a '$', got a %c\n", c);
c906108c 1393 }
c5aa993b
JM
1394 while (c != '$');
1395
1396 retries = 0;
1397 while (retries <= 10)
1398 {
1399 c = readchar (timeout);
1400 debuglogs (3, "array_get_packet: got a '%c'\n", c);
1401 switch (c)
1402 {
1403 case SERIAL_TIMEOUT:
1404 debuglogs (3, "Timeout in mid-packet, retrying\n");
1405 return 0;
1406 case '$':
1407 debuglogs (3, "Saw new packet start in middle of old one\n");
1408 return 0; /* Start a new packet, count retries */
1409 case '#':
1410 *bp = '\0';
1411 pktcsum = from_hex (readchar (timeout)) << 4;
1412 pktcsum |= from_hex (readchar (timeout));
1413 if (csum == 0)
1414 debuglogs (3, "\nGDB packet checksum zero, must be a bogus packet\n");
1415 if (csum == pktcsum)
1416 {
1417 debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet);
1418 printf_monitor ("@");
1419 expect_prompt (1);
1420 return 1;
1421 }
1422 debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum, csum);
1423 return 0;
1424 case '*': /* Run length encoding */
1425 debuglogs (5, "Run length encoding in packet\n");
1426 csum += c;
1427 c = readchar (timeout);
1428 csum += c;
1429 c = c - ' ' + 3; /* Compute repeat count */
1430
1431 if (c > 0 && c < 255 && bp + c - 1 < packet + PBUFSIZ - 1)
1432 {
1433 memset (bp, *(bp - 1), c);
1434 bp += c;
1435 continue;
1436 }
1437 *bp = '\0';
1438 printf_filtered ("Repeat count %d too large for buffer.\n", c);
1439 return 0;
1440
1441 default:
1442 if ((!isxdigit (c)) && (!ispunct (c)))
1443 debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c);
1444 if (bp < packet + PBUFSIZ - 1)
1445 {
1446 *bp++ = c;
1447 csum += c;
1448 continue;
1449 }
1450
1451 *bp = '\0';
1452 puts_filtered ("Remote packet too long.\n");
1453 return 0;
1454 }
c906108c 1455 }
c906108c 1456 }
c5aa993b 1457 return 0; /* exceeded retries */
c906108c
SS
1458}
1459
1460/*
1461 * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
1462 */
1463static unsigned long
1464ascii2hexword (mem)
1465 unsigned char *mem;
1466{
1467 unsigned long val;
1468 int i;
1469 char buf[9];
1470
1471 val = 0;
c5aa993b
JM
1472 for (i = 0; i < 8; i++)
1473 {
1474 val <<= 4;
1475 if (mem[i] >= 'A' && mem[i] <= 'F')
1476 val = val + mem[i] - 'A' + 10;
1477 if (mem[i] >= 'a' && mem[i] <= 'f')
1478 val = val + mem[i] - 'a' + 10;
1479 if (mem[i] >= '0' && mem[i] <= '9')
1480 val = val + mem[i] - '0';
1481 buf[i] = mem[i];
1482 }
c906108c
SS
1483 buf[8] = '\0';
1484 debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val, buf, mem);
1485 return val;
1486}
1487
1488/*
1489 * ascii2hexword -- convert a hex value to an ascii number represented by 8
c5aa993b 1490 * digits.
c906108c
SS
1491 */
1492static void
1493hexword2ascii (mem, num)
1494 unsigned char *mem;
1495 unsigned long num;
1496{
1497 int i;
1498 unsigned char ch;
c5aa993b 1499
c906108c 1500 debuglogs (4, "hexword2ascii() converting %x ", num);
c5aa993b
JM
1501 for (i = 7; i >= 0; i--)
1502 {
1503 mem[i] = tohex ((num >> 4) & 0xf);
1504 mem[i] = tohex (num & 0xf);
1505 num = num >> 4;
1506 }
c906108c
SS
1507 mem[8] = '\0';
1508 debuglogs (4, "\tto a %s", mem);
1509}
1510
1511/* Convert hex digit A to a number. */
1512static int
1513from_hex (a)
1514 int a;
c5aa993b 1515{
c906108c
SS
1516 if (a == 0)
1517 return 0;
1518
c5aa993b 1519 debuglogs (4, "from_hex got a 0x%x(%c)\n", a, a);
c906108c
SS
1520 if (a >= '0' && a <= '9')
1521 return a - '0';
1522 if (a >= 'a' && a <= 'f')
1523 return a - 'a' + 10;
1524 if (a >= 'A' && a <= 'F')
1525 return a - 'A' + 10;
c5aa993b
JM
1526 else
1527 {
1528 error ("Reply contains invalid hex digit 0x%x", a);
1529 }
c906108c
SS
1530}
1531
1532/* Convert number NIB to a hex digit. */
1533static int
1534tohex (nib)
1535 int nib;
1536{
1537 if (nib < 10)
c5aa993b 1538 return '0' + nib;
c906108c 1539 else
c5aa993b 1540 return 'a' + nib - 10;
c906108c
SS
1541}
1542
1543/*
1544 * _initialize_remote_monitors -- setup a few addtitional commands that
c5aa993b 1545 * are usually only used by monitors.
c906108c
SS
1546 */
1547void
1548_initialize_remote_monitors ()
1549{
1550 /* generic monitor command */
1551 add_com ("monitor", class_obscure, monitor_command,
c5aa993b 1552 "Send a command to the debug monitor.");
c906108c
SS
1553
1554}
1555
1556/*
1557 * _initialize_array -- do any special init stuff for the target.
1558 */
1559void
1560_initialize_array ()
1561{
c5aa993b 1562 init_array_ops ();
c906108c
SS
1563 add_target (&array_ops);
1564}