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