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