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