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