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