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