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