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