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