1 /* Remote debugging interface for Motorola's MVME187BUG monitor, an embedded
4 Copyright 1992, 1993 Free Software Foundation, Inc.
5 Contributed by Cygnus Support. Written by K. Richard Pixley.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
38 /* External data declarations */
39 extern int stop_soon_quietly; /* for wait_for_inferior */
41 /* Forward data declarations */
42 extern struct target_ops bug_ops; /* Forward declaration */
44 /* Forward function declarations */
45 static void bug_fetch_registers ();
46 static int bug_store_registers ();
47 static void bug_close ();
48 static int bug_clear_breakpoints ();
55 /***********************************************************************/
56 /* Caching stuff stolen from remote-nindy.c */
58 /* The data cache records all the data read from the remote machine
59 since the last time it stopped.
61 Each cache block holds LINE_SIZE bytes of data
62 starting at a multiple-of-LINE_SIZE address. */
64 #define LINE_SIZE_POWER 4
65 #define LINE_SIZE (1<<LINE_SIZE_POWER) /* eg 1<<3 == 8 */
66 #define LINE_SIZE_MASK ((LINE_SIZE-1)) /* eg 7*2+1= 111*/
67 #define DCACHE_SIZE 64 /* Number of cache blocks */
68 #define XFORM(x) ((x&LINE_SIZE_MASK)>>2)
71 struct dcache_block *next, *last;
72 unsigned int addr; /* Address for which data is recorded. */
73 int data[LINE_SIZE / sizeof (int)];
76 struct dcache_block dcache_free, dcache_valid;
78 /* Free all the data cache blocks, thus discarding all cached data. */
83 register struct dcache_block *db;
85 while ((db = dcache_valid.next) != &dcache_valid)
88 insque (db, &dcache_free);
93 * If addr is present in the dcache, return the address of the block
101 register struct dcache_block *db;
106 /* Search all cache blocks for one that is at this address. */
107 db = dcache_valid.next;
108 while (db != &dcache_valid)
110 if ((addr & ~LINE_SIZE_MASK) == db->addr)
117 /* Return the int data at address ADDR in dcache block DC. */
120 dcache_value (db, addr)
121 struct dcache_block *db;
126 return (db->data[XFORM (addr)]);
129 /* Get a free cache block, put or keep it on the valid list,
130 and return its address. The caller should store into the block
131 the address and data that it describes, then remque it from the
132 free list and insert it into the valid list. This procedure
133 prevents errors from creeping in if a ninMemGet is interrupted
134 (which used to put garbage blocks in the valid list...). */
136 struct dcache_block *
139 register struct dcache_block *db;
141 if ((db = dcache_free.next) == &dcache_free)
143 /* If we can't get one from the free list, take last valid and put
144 it on the free list. */
145 db = dcache_valid.last;
147 insque (db, &dcache_free);
151 insque (db, &dcache_valid);
155 /* Return the contents of the word at address ADDR in the remote machine,
156 using the data cache. */
162 register struct dcache_block *db;
164 db = dcache_hit (addr);
167 db = dcache_alloc ();
169 bug_read_inferior_memory (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
171 db->addr = addr & ~LINE_SIZE_MASK;
172 remque (db); /* Off the free list */
173 insque (db, &dcache_valid); /* On the valid list */
175 return (dcache_value (db, addr));
178 /* Write the word at ADDR both in the data cache and in the remote machine. */
180 dcache_poke (addr, data)
184 register struct dcache_block *db;
186 /* First make sure the word is IN the cache. DB is its cache block. */
187 db = dcache_hit (addr);
190 db = dcache_alloc ();
192 bug_write_inferior_memory (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
194 db->addr = addr & ~LINE_SIZE_MASK;
195 remque (db); /* Off the free list */
196 insque (db, &dcache_valid); /* On the valid list */
199 /* Modify the word in the cache. */
200 db->data[XFORM (addr)] = data;
202 /* Send the changed word. */
204 bug_write_inferior_memory (addr, (unsigned char *) &data, 4);
208 /* The cache itself. */
209 struct dcache_block the_cache[DCACHE_SIZE];
211 /* Initialize the data cache. */
216 register struct dcache_block *db;
219 dcache_free.next = dcache_free.last = &dcache_free;
220 dcache_valid.next = dcache_valid.last = &dcache_valid;
221 for (i = 0; i < DCACHE_SIZE; i++, db++)
222 insque (db, &dcache_free);
225 /***********************************************************************
226 * I/O stuff stolen from remote-eb.c
227 ***********************************************************************/
229 static int timeout = 2;
231 static const char *dev_name;
233 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
234 bug_open knows that we don't have a file open when the program
243 error ("remote device not open");
250 /* Read a character from the remote system, doing all the fancy
257 buf = SERIAL_READCHAR (desc, timeout);
259 if (buf == SERIAL_TIMEOUT)
260 error ("Timeout reading from remote system.");
273 buf = SERIAL_READCHAR (desc, timeout);
274 if (buf == SERIAL_TIMEOUT)
283 /* Keep discarding input from the remote system, until STRING is found.
284 Let the user break out immediately. */
294 if (readchar () == *p)
308 /* Keep discarding input until we see the bug prompt.
310 The convention for dealing with the prompt is that you
312 o *then* wait for the prompt.
314 Thus the last thing that a procedure does with the serial line
315 will be an expect_prompt(). Exception: bug_resume does not
316 wait for the prompt, because the terminal is being handed over
317 to the inferior. However, the next thing which happens after that
318 is a bug_wait which does wait for the prompt.
319 Note that this includes abnormal exit, e.g. error(). This is
320 necessary to prevent getting into states from which we can't
328 /* Get a hex digit from the remote system & return its value.
329 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
331 get_hex_digit (ignore_space)
339 if (ch >= '0' && ch <= '9')
341 else if (ch >= 'A' && ch <= 'F')
342 return ch - 'A' + 10;
343 else if (ch >= 'a' && ch <= 'f')
344 return ch - 'a' + 10;
345 else if (ch == ' ' && ignore_space)
350 error ("Invalid hex digit from remote system.");
355 /* Get a byte from bug_desc and put it in *BYT. Accept any number
363 val = get_hex_digit (1) << 4;
364 val |= get_hex_digit (0);
368 /* Read a 32-bit hex word from the bug, preceded by a space */
376 for (j = 0; j < 8; j++)
377 val = (val << 4) + get_hex_digit (j == 0);
381 /* Called when SIGALRM signal sent due to alarm() timeout. */
383 /* Number of SIGTRAPs we need to simulate. That is, the next
384 NEED_ARTIFICIAL_TRAP calls to bug_wait should just return
385 SIGTRAP without actually waiting for anything. */
387 static int need_artificial_trap = 0;
390 bug_kill (arg, from_tty)
398 * Download a file specified in 'args', to the bug.
401 bug_load (args, fromtty)
414 abfd = bfd_openr (args, 0);
417 printf_filtered ("Unable to open file %s\n", args);
421 if (bfd_check_format (abfd, bfd_object) == 0)
423 printf_filtered ("File is not an object file\n");
428 while (s != (asection *) NULL)
430 if (s->flags & SEC_LOAD)
435 char *buffer = xmalloc (DELTA);
437 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size);
438 for (i = 0; i < s->_raw_size; i += DELTA)
442 if (delta > s->_raw_size - i)
443 delta = s->_raw_size - i;
445 bfd_get_section_contents (abfd, s, buffer, i, delta);
446 bug_write_inferior_memory (s->vma + i, buffer, delta);
447 printf_filtered ("*");
450 printf_filtered ("\n");
455 sprintf (buffer, "rs ip %x", abfd->start_address);
456 bug_write_cr (buffer);
460 /* This is called not only when we first attach, but also when the
461 user types "run" after having attached. */
463 bug_create_inferior (execfile, args, env)
472 error ("Can't pass arguments to remote bug process.");
474 if (execfile == 0 || exec_bfd == 0)
475 error ("No exec file specified");
477 entry_pt = (int) bfd_get_start_address (exec_bfd);
480 bug_kill (NULL, NULL);
481 bug_clear_breakpoints ();
482 init_wait_for_inferior ();
486 insert_breakpoints (); /* Needed to get correct instruction in cache */
487 proceed (entry_pt, -1, 0);
490 /* Open a connection to a remote debugger.
491 NAME is the filename used for communication, then a space,
499 while (*s && !isspace (*s))
520 while (*s && !isspace (*s))
526 copy = xmalloc (len + 1);
527 memcpy (copy, word, len);
533 static int baudrate = 9600;
541 /* Put this port into NORMAL mode, send the 'normal' character */
543 bug_write ("\001", 1); /* Control A */
544 bug_write ("\r", 1); /* Cr */
548 ok = SERIAL_READCHAR (desc, timeout);
555 if (readchar_nofail () == 'r')
558 /* Not the right baudrate, or the board's not on */
566 if (!SERIAL_SETBAUDRATE (desc, baudrate))
567 error ("Can't set baudrate");
572 bug_open (name, from_tty)
579 push_target (&bug_ops);
587 dev_name = strdup (name);
589 if (!(desc = SERIAL_OPEN (dev_name)))
590 perror_with_name ((char *) dev_name);
597 /* Hello? Are you there? */
598 SERIAL_WRITE (desc, "\r", 1);
601 /* Clear any break points */
602 bug_clear_breakpoints ();
604 printf_filtered ("Connected to remote 187bug system.\n");
607 /* Close out all files and local state before this target loses control. */
613 /* Clear any break points */
614 bug_clear_breakpoints ();
622 /* Terminate the open connection to the remote debugger.
623 Use this when you want to detach and do something else
626 bug_detach (args, from_tty)
631 bug_clear_breakpoints ();
633 pop_target (); /* calls bug_close to do the real work */
636 printf_filtered ("Ending remote %s debugging\n", target_shortname);
639 /* Tell the remote machine to resume. */
642 bug_resume (step, sig)
651 /* Force the next bug_wait to return a trap. Not doing anything
652 about I/O from the target means that the user has to type
653 "continue" to see any. FIXME, this should be fixed. */
654 need_artificial_trap = 1;
662 /* Wait until the remote machine stops, then return,
663 storing status in STATUS just as `wait' would. */
666 not_bug_wait (status)
673 expect("Effective address: ");
677 WSETEXIT ((*status), 0);
679 old_timeout = timeout;
680 timeout = 99999; /* while user program runs. */
681 old_quit = immediate_quit;
682 immediate_quit = 1; /* helps ability to quit. */
684 while (strchr("\n\r", i = readchar()) != NULL) ;;
686 immediate_quit = old_quit;
687 timeout = old_timeout;
692 expect("t Breakpoint");
693 WSETSTOP ((*status), SIGTRAP);
698 /* finished cleanly */
709 /* Strings to look for. '?' means match any single character.
710 Note that with the algorithm we use, the initial character
711 of the string cannot recur in the string, or we will not
712 find some cases of the string in the input. */
714 static char bpt[] = "At Breakpoint";
715 static char exitmsg[] = "????-Bug>";
719 /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars. */
722 /* Current position in swallowed. */
723 char *swallowed_p = swallowed;
727 int old_timeout = timeout;
728 int old_immediate_quit = immediate_quit;
729 int swallowed_cr = 0;
731 WSETEXIT ((*status), 0);
733 if (need_artificial_trap != 0)
735 WSETSTOP ((*status), SIGTRAP);
736 need_artificial_trap--;
737 /* user output from the target can be discarded here. (?) */
742 /* read off leftovers from resume */
743 expect("Effective address: ");
744 (void) get_hex_word();
745 while (strchr("\r\n", ch = readchar()) != NULL) ;;
747 timeout = 99999; /* Don't time out -- user program is running. */
748 immediate_quit = 1; /* Helps ability to QUIT */
751 QUIT; /* Let user quit and leave process running */
766 if (ch == *ep || *ep == '?')
785 /* Print out any characters which have been swallowed. */
786 for (p = swallowed; p < swallowed_p; ++p)
788 swallowed_p = swallowed;
790 if ((ch != '\r' && ch != '\n') || swallowed_cr > 10)
803 WSETSTOP ((*status), SIGTRAP);
808 WSETEXIT ((*status), 0);
811 timeout = old_timeout;
812 immediate_quit = old_immediate_quit;
816 /* Return the name of register number REGNO
817 in the form input and output by bug.
819 Returns a pointer to a static buffer containing the answer. */
824 static char *rn[] = {
825 "r00", "r01", "r02", "r03", "r04", "r05", "r06", "r07",
826 "r08", "r09", "r10", "r11", "r12", "r13", "r14", "r15",
827 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
828 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
830 /* these get confusing because we omit a few and switch some ordering around. */
832 "cr01", /* 32 = psr */
833 "fcr62", /* 33 = fpsr*/
834 "fcr63", /* 34 = fpcr */
835 "cr04", /* 35 = sxip */
836 "cr05", /* 36 = snip */
837 "cr06", /* 37 = sfip */
844 gethex (length, start, ok)
854 if (*start >= 'a' && *start <= 'f')
856 result += *start - 'a' + 10;
858 else if (*start >= 'A' && *start <= 'F')
860 result += *start - 'A' + 10;
862 else if (*start >= '0' && *start <= '9')
864 result += *start - '0';
874 timed_read (buf, n, timeout)
900 SERIAL_WRITE (desc, a, l);
903 for (i = 0; i < l; i++)
912 bug_write (s, strlen (s));
916 /* Store register REGNO, or all if REGNO == -1. */
919 bug_fetch_register(regno)
922 REGISTER_TYPE regval;
929 for (i = 0; i < NUM_REGS; ++i)
930 bug_fetch_register(i);
935 bug_write_cr(get_reg_name(regno));
937 regval = get_hex_word();
940 /* the following registers contain flag bits in the lower to bit slots.
942 if (regno == PC_REGNUM /* aka sxip */
943 || regno == NPC_REGNUM /* aka snip */
944 || regno == SFIP_REGNUM) /* aka sfip */
947 supply_register(regno, (char *) ®val);
953 /* Store register REGNO, or all if REGNO == -1. */
956 bug_store_register (regno)
959 REGISTER_TYPE regval;
967 for (i = 0; i < NUM_REGS; ++i)
968 bug_store_register(i);
974 /* get_reg_name thinks that the pc is in sxip. This is only partially
975 true. When *assigning* it, we must assign to ip on m88k-bug. */
977 regname = ((regno == PC_REGNUM)
979 : get_reg_name(regno));
981 sprintf(buffer, "rs %s %08x",
983 read_register(regno));
985 bug_write_cr(buffer);
992 /* Get ready to modify the registers array. On machines which store
993 individual registers, this doesn't need to do anything. On machines
994 which store all the registers in one fell swoop, this makes sure
995 that registers contains all the registers from the program being
999 bug_prepare_to_store ()
1001 /* Do nothing, since we can store individual regs */
1005 translate_addr (addr)
1013 /* Read a word from remote address ADDR and return it.
1014 * This goes through the data cache.
1017 bug_fetch_word (addr)
1020 return dcache_fetch (addr);
1023 /* Write a word WORD into remote address ADDR.
1024 This goes through the data cache. */
1027 bug_store_word (addr, word)
1031 dcache_poke (addr, word);
1035 bug_xfer_inferior_memory (memaddr, myaddr, len, write, target)
1040 struct target_ops *target; /* ignored */
1044 /* Round starting address down to longword boundary. */
1045 register CORE_ADDR addr;
1047 /* Round ending address up; get number of longwords that makes. */
1050 /* Allocate buffer of that many longwords. */
1051 register int *buffer;
1053 addr = memaddr & -sizeof (int);
1054 count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
1056 buffer = (int *) alloca (count * sizeof (int));
1060 /* Fill start and end extra bytes of buffer with existing memory data. */
1062 if (addr != memaddr || len < (int) sizeof (int))
1064 /* Need part of initial word -- fetch it. */
1065 buffer[0] = bug_fetch_word (addr);
1068 if (count > 1) /* FIXME, avoid if even boundary */
1071 = bug_fetch_word (addr + (count - 1) * sizeof (int));
1074 /* Copy data to be written over corresponding part of buffer */
1076 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
1078 /* Write the entire buffer. */
1080 for (i = 0; i < count; i++, addr += sizeof (int))
1083 bug_store_word (addr, buffer[i]);
1094 /* Read all the longwords */
1095 for (i = 0; i < count; i++, addr += sizeof (int))
1098 buffer[i] = bug_fetch_word (addr);
1106 /* Copy appropriate bytes out of the buffer. */
1107 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
1113 /* fixme: make this user settable */
1114 #define CHUNK_SIZE (30)
1117 bug_write_inferior_memory (memaddr, myaddr, len)
1119 unsigned char *myaddr;
1125 char buffer[(CHUNK_SIZE + 8) << 1];
1129 #define LOAD_COMMAND "lo 0"
1130 bug_write_cr (LOAD_COMMAND);
1140 thisgo = len - done;
1141 if (thisgo > CHUNK_SIZE)
1142 thisgo = CHUNK_SIZE;
1144 address = memaddr + done;
1145 sprintf (buf, "S3%02X%08X", thisgo + 4 + 1, address);
1148 checksum += (thisgo + 4 + 1
1150 + ((address >> 8) & 0xff)
1151 + ((address >> 16) & 0xff)
1152 + ((address >> 24) & 0xff));
1154 for (idx = 0; idx < thisgo; idx++)
1156 sprintf (buf, "%02X", myaddr[idx + done]);
1157 checksum += myaddr[idx + done];
1160 sprintf(buf, "%02X", ~checksum & 0xff);
1161 bug_write_cr (buffer);
1165 bug_write_cr("S7060000000000F9");
1172 char *file = "nothing";
1175 file = bfd_get_filename (exec_bfd);
1179 printf_filtered ("\tAttached to DOS asynctsr and running program %s\n", file);
1181 printf_filtered ("\tAttached to %s at %d baud and running program %s\n", dev_name, baudrate, file);
1183 printf_filtered ("\ton an m88k processor.\n");
1186 /* Copy LEN bytes of data from debugger memory at MYADDR
1187 to inferior's memory at MEMADDR. Returns errno value.
1188 * sb/sh instructions don't work on unaligned addresses, when TU=1.
1191 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
1192 at debugger address MYADDR. Returns errno value. */
1194 bug_read_inferior_memory (memaddr, myaddr, len)
1205 unsigned int inaddr;
1206 unsigned int checksum;
1208 sprintf(request, "du 0 %x:&%d", memaddr, len);
1209 bug_write_cr(request);
1211 p = buffer = alloca(len);
1213 /* scan up through the header */
1214 expect("S0030000FC");
1216 while (p < buffer + len)
1218 /* scan off any white space. */
1219 while (readchar() != 'S') ;;
1221 /* what kind of s-rec? */
1224 /* scan record size */
1225 get_hex_byte(&size);
1239 inaddr = (inaddr << 8) + c;
1242 /* intentional fall through */
1245 inaddr = (inaddr << 8) + c;
1248 /* intentional fall through */
1251 inaddr = (inaddr << 8) + c;
1255 inaddr = (inaddr << 8) + c;
1262 error("reading s-records.");
1265 if (inaddr < memaddr
1266 || (memaddr + len) < (inaddr + size))
1267 error("srec out of memory range.");
1269 if (p != buffer + inaddr - memaddr)
1270 error("srec out of sequence.");
1272 for (; size; --size, ++p)
1279 if (c != (~checksum & 0xff))
1280 error("bad s-rec checksum");
1285 if (p != buffer + len)
1288 memcpy(myaddr, buffer, len);
1292 /* This routine is run as a hook, just before the main command loop is
1293 entered. If gdb is configured for the H8, but has not had its
1294 target specified yet, this will loop prompting the user to do so.
1297 bug_before_main_loop ()
1301 extern FILE *instream;
1303 push_target (&bug_ops);
1306 #define MAX_BREAKS 16
1307 static int num_brkpts = 0;
1309 bug_insert_breakpoint (addr, save)
1311 char *save; /* Throw away, let bug save instructions */
1315 if (num_brkpts < MAX_BREAKS)
1320 sprintf (buffer, "br %x", addr);
1321 bug_write_cr (buffer);
1327 fprintf_filtered (stderr,
1328 "Too many break points, break point not installed\n");
1334 bug_remove_breakpoint (addr, save)
1336 char *save; /* Throw away, let bug save instructions */
1343 sprintf (buffer, "nobr %x", addr);
1344 bug_write_cr (buffer);
1351 /* Clear the bugs notion of what the break points are */
1353 bug_clear_breakpoints ()
1358 bug_write_cr ("nobr");
1366 bug_clear_breakpoints ();
1367 generic_mourn_inferior ();
1370 /* Put a command string, in args, out to the bug. The bug is assumed to
1371 be in raw mode, all writing/reading done through desc.
1372 Ouput from the bug is placed on the users terminal until the
1373 prompt from the bug is seen.
1374 FIXME: Can't handle commands that take input. */
1377 bug_com (args, fromtty)
1386 /* Clear all input so only command relative output is displayed */
1388 bug_write_cr (args);
1389 bug_write ("\030", 1);
1397 printf_filtered ("Snoop disabled\n");
1399 printf_filtered ("Snoop enabled\n");
1408 dev_name = get_word (&s);
1422 int newrate = atoi (s);
1425 if (SERIAL_SETBAUDRATE (desc, newrate))
1426 error ("Can't use %d baud\n", newrate);
1428 printf_filtered ("Checking target is in sync\n");
1430 printf_filtered ("Sending commands to set target to %d\n",
1433 sprintf (buffer, "tm %d. N 8 1", baudrate);
1434 bug_write_cr (buffer);
1439 struct target_ops bug_ops =
1441 "bug", "Remote BUG monitor",
1442 "Use the mvme187 board running the BUG monitor connected\n\
1445 bug_open, bug_close,
1446 0, bug_detach, bug_resume, bug_wait, /* attach */
1447 bug_fetch_register, bug_store_register,
1448 bug_prepare_to_store,
1449 bug_xfer_inferior_memory,
1451 bug_insert_breakpoint, bug_remove_breakpoint, /* Breakpoints */
1452 0, 0, 0, 0, 0, /* Terminal handling */
1453 bug_kill, /* FIXME, kill */
1455 0, /* lookup_symbol */
1456 bug_create_inferior, /* create_inferior */
1457 bug_mourn, /* mourn_inferior FIXME */
1459 0, /* notice_signals */
1460 process_stratum, 0, /* next */
1461 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1462 0, 0, /* Section pointers */
1463 OPS_MAGIC, /* Always the last thing */
1467 _initialize_remote_bug ()
1469 add_target (&bug_ops);
1471 add_com ("bug <command>", class_obscure, bug_com,
1472 "Send a command to the BUG monitor.");
1473 add_com ("snoop", class_obscure, bug_quiet,
1474 "Show what commands are going to the monitor");
1476 add_com ("device", class_obscure, bug_device,
1477 "Set the terminal line for BUG communications");
1480 add_com ("speed", class_obscure, bug_speed,
1481 "Set the terminal line speed for BUG communications");