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