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