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