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