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