1 /* Remote debugging interface for Hitachi HMS Monitor Version 1.0
2 Copyright 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Steve Chamberlain
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
36 /* External data declarations */
37 extern int stop_soon_quietly; /* for wait_for_inferior */
39 /* Forward data declarations */
40 extern struct target_ops hms_ops; /* Forward declaration */
42 /* Forward function declarations */
43 static void hms_fetch_registers ();
44 static int hms_store_registers ();
45 static void hms_close ();
46 static int hms_clear_breakpoints ();
48 extern struct target_ops hms_ops;
52 /***********************************************************************/
53 /* Caching stuff stolen from remote-nindy.c */
55 /* The data cache records all the data read from the remote machine
56 since the last time it stopped.
58 Each cache block holds LINE_SIZE bytes of data
59 starting at a multiple-of-LINE_SIZE address. */
61 #define LINE_SIZE_POWER 4
62 #define LINE_SIZE (1<<LINE_SIZE_POWER) /* eg 1<<3 == 8 */
63 #define LINE_SIZE_MASK ((LINE_SIZE-1)) /* eg 7*2+1= 111*/
64 #define DCACHE_SIZE 64 /* Number of cache blocks */
65 #define XFORM(x) ((x&LINE_SIZE_MASK)>>2)
68 struct dcache_block *next, *last;
69 unsigned int addr; /* Address for which data is recorded. */
70 int data[LINE_SIZE / sizeof (int)];
73 struct dcache_block dcache_free, dcache_valid;
75 /* Free all the data cache blocks, thus discarding all cached data. */
80 register struct dcache_block *db;
82 while ((db = dcache_valid.next) != &dcache_valid)
85 insque (db, &dcache_free);
90 * If addr is present in the dcache, return the address of the block
98 register struct dcache_block *db;
103 /* Search all cache blocks for one that is at this address. */
104 db = dcache_valid.next;
105 while (db != &dcache_valid)
107 if ((addr & ~LINE_SIZE_MASK) == db->addr)
114 /* Return the int data at address ADDR in dcache block DC. */
117 dcache_value (db, addr)
118 struct dcache_block *db;
123 return (db->data[XFORM (addr)]);
126 /* Get a free cache block, put or keep it on the valid list,
127 and return its address. The caller should store into the block
128 the address and data that it describes, then remque it from the
129 free list and insert it into the valid list. This procedure
130 prevents errors from creeping in if a ninMemGet is interrupted
131 (which used to put garbage blocks in the valid list...). */
133 struct dcache_block *
136 register struct dcache_block *db;
138 if ((db = dcache_free.next) == &dcache_free)
140 /* If we can't get one from the free list, take last valid and put
141 it on the free list. */
142 db = dcache_valid.last;
144 insque (db, &dcache_free);
148 insque (db, &dcache_valid);
152 /* Return the contents of the word at address ADDR in the remote machine,
153 using the data cache. */
159 register struct dcache_block *db;
161 db = dcache_hit (addr);
164 db = dcache_alloc ();
166 hms_read_inferior_memory (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
168 db->addr = addr & ~LINE_SIZE_MASK;
169 remque (db); /* Off the free list */
170 insque (db, &dcache_valid); /* On the valid list */
172 return (dcache_value (db, addr));
175 /* Write the word at ADDR both in the data cache and in the remote machine. */
177 dcache_poke (addr, data)
181 register struct dcache_block *db;
183 /* First make sure the word is IN the cache. DB is its cache block. */
184 db = dcache_hit (addr);
187 db = dcache_alloc ();
189 hms_write_inferior_memory (addr & ~LINE_SIZE_MASK, (unsigned char *) db->data, LINE_SIZE);
191 db->addr = addr & ~LINE_SIZE_MASK;
192 remque (db); /* Off the free list */
193 insque (db, &dcache_valid); /* On the valid list */
196 /* Modify the word in the cache. */
197 db->data[XFORM (addr)] = data;
199 /* Send the changed word. */
201 hms_write_inferior_memory (addr, (unsigned char *) &data, 4);
205 /* The cache itself. */
206 struct dcache_block the_cache[DCACHE_SIZE];
208 /* Initialize the data cache. */
213 register struct dcache_block *db;
216 dcache_free.next = dcache_free.last = &dcache_free;
217 dcache_valid.next = dcache_valid.last = &dcache_valid;
218 for (i = 0; i < DCACHE_SIZE; i++, db++)
219 insque (db, &dcache_free);
222 /***********************************************************************
223 * I/O stuff stolen from remote-eb.c
224 ***********************************************************************/
226 static int timeout = 2;
228 static const char *dev_name;
230 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
231 hms_open knows that we don't have a file open when the program
240 error ("remote device not open");
247 /* Read a character from the remote system, doing all the fancy
254 buf = serial_readchar (timeout);
257 error ("Timeout reading from remote system.");
270 buf = serial_readchar (timeout);
280 /* Keep discarding input from the remote system, until STRING is found.
281 Let the user break out immediately. */
291 if (readchar () == *p)
305 /* Keep discarding input until we see the hms prompt.
307 The convention for dealing with the prompt is that you
309 o *then* wait for the prompt.
311 Thus the last thing that a procedure does with the serial line
312 will be an expect_prompt(). Exception: hms_resume does not
313 wait for the prompt, because the terminal is being handed over
314 to the inferior. However, the next thing which happens after that
315 is a hms_wait which does wait for the prompt.
316 Note that this includes abnormal exit, e.g. error(). This is
317 necessary to prevent getting into states from which we can't
325 /* Get a hex digit from the remote system & return its value.
326 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
328 get_hex_digit (ignore_space)
336 if (ch >= '0' && ch <= '9')
338 else if (ch >= 'A' && ch <= 'F')
339 return ch - 'A' + 10;
340 else if (ch >= 'a' && ch <= 'f')
341 return ch - 'a' + 10;
342 else if (ch == ' ' && ignore_space)
347 error ("Invalid hex digit from remote system.");
352 /* Get a byte from hms_desc and put it in *BYT. Accept any number
360 val = get_hex_digit (1) << 4;
361 val |= get_hex_digit (0);
365 /* Read a 32-bit hex word from the hms, preceded by a space */
373 for (j = 0; j < 8; j++)
374 val = (val << 4) + get_hex_digit (j == 0);
378 /* Called when SIGALRM signal sent due to alarm() timeout. */
380 /* Number of SIGTRAPs we need to simulate. That is, the next
381 NEED_ARTIFICIAL_TRAP calls to hms_wait should just return
382 SIGTRAP without actually waiting for anything. */
384 static int need_artificial_trap = 0;
387 hms_kill (arg, from_tty)
395 * Download a file specified in 'args', to the hms.
398 hms_load (args, fromtty)
411 abfd = bfd_openr (args, 0);
414 printf_filtered ("Unable to open file %s\n", args);
418 if (bfd_check_format (abfd, bfd_object) == 0)
420 printf_filtered ("File is not an object file\n");
425 while (s != (asection *) NULL)
427 if (s->flags & SEC_LOAD)
432 char *buffer = xmalloc (DELTA);
434 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size);
435 for (i = 0; i < s->_raw_size; i += DELTA)
439 if (delta > s->_raw_size - i)
440 delta = s->_raw_size - i;
442 bfd_get_section_contents (abfd, s, buffer, i, delta);
443 hms_write_inferior_memory (s->vma + i, buffer, delta);
444 printf_filtered ("*");
447 printf_filtered ("\n");
452 sprintf (buffer, "r PC=%x", abfd->start_address);
453 hms_write_cr (buffer);
457 /* This is called not only when we first attach, but also when the
458 user types "run" after having attached. */
460 hms_create_inferior (execfile, args, env)
469 error ("Can't pass arguments to remote hms process.");
471 if (execfile == 0 || exec_bfd == 0)
472 error ("No exec file specified");
474 entry_pt = (int) bfd_get_start_address (exec_bfd);
477 hms_kill (NULL, NULL);
478 hms_clear_breakpoints ();
479 init_wait_for_inferior ();
483 insert_breakpoints (); /* Needed to get correct instruction in cache */
484 proceed (entry_pt, -1, 0);
487 /* Open a connection to a remote debugger.
488 NAME is the filename used for communication, then a space,
496 while (*s && !isspace (*s))
517 while (*s && !isspace (*s))
523 copy = xmalloc (len + 1);
524 memcpy (copy, word, len);
530 static int baudrate = 9600;
537 /* Put this port into NORMAL mode, send the 'normal' character */
539 hms_write ("\001", 1); /* Control A */
540 hms_write ("\r", 1); /* Cr */
544 ok = serial_readchar (timeout);
551 if (readchar_nofail () == 'r')
554 /* Not the right baudrate, or the board's not on */
560 if (!serial_setbaudrate (baudrate))
561 error ("Can't set baudrate");
565 get_baudrate_right ()
568 while (!is_baudrate_right ())
570 baudrate = serial_nextbaudrate (baudrate);
573 printf_filtered ("Board not yet in sync\n");
576 printf_filtered ("Board not responding, trying %d baud\n", baudrate);
578 serial_setbaudrate (baudrate);
584 hms_open (name, from_tty)
598 if (name && strlen (name))
599 dev_name = strdup (name);
600 if (!serial_open (dev_name))
601 perror_with_name ((char *) dev_name);
607 get_baudrate_right ();
609 /* Hello? Are you there? */
610 serial_write ("\r", 1);
613 /* Clear any break points */
614 hms_clear_breakpoints ();
616 printf_filtered ("Connected to remote H8/300 HMS system.\n");
619 /* Close out all files and local state before this target loses control. */
625 /* Clear any break points */
626 hms_clear_breakpoints ();
627 sleep (1); /* Let any output make it all the way back */
629 serial_write ("R\r", 2);
635 /* Terminate the open connection to the remote debugger.
636 Use this when you want to detach and do something else
639 hms_detach (args, from_tty)
645 hms_clear_breakpoints ();
648 pop_target (); /* calls hms_close to do the real work */
650 printf_filtered ("Ending remote %s debugging\n", target_shortname);
653 /* Tell the remote machine to resume. */
656 hms_resume (step, sig)
666 /* Force the next hms_wait to return a trap. Not doing anything
667 about I/O from the target means that the user has to type
668 "continue" to see any. FIXME, this should be fixed. */
669 need_artificial_trap = 1;
678 /* Wait until the remote machine stops, then return,
679 storing status in STATUS just as `wait' would. */
685 /* Strings to look for. '?' means match any single character.
686 Note that with the algorithm we use, the initial character
687 of the string cannot recur in the string, or we will not
688 find some cases of the string in the input. */
690 static char bpt[] = "At breakpoint:";
692 /* It would be tempting to look for "\n[__exit + 0x8]\n"
693 but that requires loading symbols with "yc i" and even if
694 we did do that we don't know that the file has symbols. */
695 static char exitmsg[] = "HMS>";
699 /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars. */
702 /* Current position in swallowed. */
703 char *swallowed_p = swallowed;
707 int old_timeout = timeout;
708 int old_immediate_quit = immediate_quit;
709 int swallowed_cr = 0;
711 WSETEXIT ((*status), 0);
713 if (need_artificial_trap != 0)
715 WSETSTOP ((*status), SIGTRAP);
716 need_artificial_trap--;
720 timeout = 99999; /* Don't time out -- user program is running. */
721 immediate_quit = 1; /* Helps ability to QUIT */
724 QUIT; /* Let user quit and leave process running */
740 if (ch == *ep || *ep == '?')
759 /* Print out any characters which have been swallowed. */
760 for (p = swallowed; p < swallowed_p; ++p)
762 swallowed_p = swallowed;
764 if ((ch != '\r' && ch != '\n') || swallowed_cr > 10)
775 WSETSTOP ((*status), SIGTRAP);
780 WSETEXIT ((*status), 0);
783 timeout = old_timeout;
784 immediate_quit = old_immediate_quit;
788 /* Return the name of register number REGNO
789 in the form input and output by hms.
791 Returns a pointer to a static buffer containing the answer. */
796 static char *rn[] = REGISTER_NAMES;
801 /* Read the remote registers. */
803 gethex (length, start, ok)
813 if (*start >= 'a' && *start <= 'f')
815 result += *start - 'a' + 10;
817 else if (*start >= 'A' && *start <= 'F')
819 result += *start - 'A' + 10;
821 else if (*start >= '0' && *start <= '9')
823 result += *start - '0';
833 timed_read (buf, n, timeout)
863 for (i = 0; i < l; i++)
872 hms_write (s, strlen (s));
877 hms_fetch_register (dummy)
880 #define REGREPLY_SIZE 79
881 char linebuf[REGREPLY_SIZE + 1];
886 REGISTER_TYPE reg[NUM_REGS];
895 s = timed_read (linebuf, REGREPLY_SIZE, 1);
897 linebuf[REGREPLY_SIZE] = 0;
899 if (linebuf[0] == 'r' &&
903 linebuf[75] == 'H' &&
904 linebuf[76] == 'M' &&
908 PC=XXXX CCR=XX:XXXXXXXX R0-R7= XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX
909 5436789012345678901234567890123456789012345678901234567890123456789012
914 reg[PC_REGNUM] = gethex (4, linebuf + 6, &gottok);
915 reg[CCR_REGNUM] = gethex (2, linebuf + 15, &gottok);
916 for (i = 0; i < 8; i++)
918 reg[i] = gethex (4, linebuf + 34 + 5 * i, &gottok);
923 for (i = 0; i < NUM_REGS; i++)
928 swapped[0] = (reg[i]) >> 8;
930 supply_register (i, swapped);
934 /* Store register REGNO, or all if REGNO == -1.
935 Return errno value. */
937 hms_store_register (regno)
941 /* printf("hms_store_register() called.\n"); fflush(stdout); /* */
944 for (regno = 0; regno < NUM_REGS; regno++)
946 hms_store_register (regno);
951 char *name = get_reg_name (regno);
954 sprintf (buffer, "r %s=%x", name, read_register (regno));
955 hms_write_cr (buffer);
960 /* Get ready to modify the registers array. On machines which store
961 individual registers, this doesn't need to do anything. On machines
962 which store all the registers in one fell swoop, this makes sure
963 that registers contains all the registers from the program being
967 hms_prepare_to_store ()
969 /* Do nothing, since we can store individual regs */
973 translate_addr (addr)
981 /* Read a word from remote address ADDR and return it.
982 * This goes through the data cache.
985 hms_fetch_word (addr)
988 return dcache_fetch (addr);
991 /* Write a word WORD into remote address ADDR.
992 This goes through the data cache. */
995 hms_store_word (addr, word)
999 dcache_poke (addr, word);
1003 hms_xfer_inferior_memory (memaddr, myaddr, len, write, target)
1008 struct target_ops *target; /* ignored */
1012 /* Round starting address down to longword boundary. */
1013 register CORE_ADDR addr;
1015 /* Round ending address up; get number of longwords that makes. */
1018 /* Allocate buffer of that many longwords. */
1019 register int *buffer;
1022 addr = memaddr & -sizeof (int);
1023 count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
1025 buffer = (int *) alloca (count * sizeof (int));
1029 /* Fill start and end extra bytes of buffer with existing memory data. */
1031 if (addr != memaddr || len < (int) sizeof (int))
1033 /* Need part of initial word -- fetch it. */
1034 buffer[0] = hms_fetch_word (addr);
1037 if (count > 1) /* FIXME, avoid if even boundary */
1040 = hms_fetch_word (addr + (count - 1) * sizeof (int));
1043 /* Copy data to be written over corresponding part of buffer */
1045 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
1047 /* Write the entire buffer. */
1049 for (i = 0; i < count; i++, addr += sizeof (int))
1052 hms_store_word (addr, buffer[i]);
1063 /* Read all the longwords */
1064 for (i = 0; i < count; i++, addr += sizeof (int))
1067 buffer[i] = hms_fetch_word (addr);
1075 /* Copy appropriate bytes out of the buffer. */
1076 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
1083 hms_write_inferior_memory (memaddr, myaddr, len)
1085 unsigned char *myaddr;
1099 thisgo = len - done;
1103 sprintf (buffer, "M.B %4x =", memaddr + done);
1104 hms_write (buffer, 10);
1105 for (idx = 0; idx < thisgo; idx++)
1109 sprintf (buf, "%2x ", myaddr[idx + done]);
1122 char *file = "nothing";
1125 file = bfd_get_filename (exec_bfd);
1129 printf_filtered ("\tAttached to DOS asynctsr and running program %s\n", file);
1131 printf_filtered ("\tAttached to %s at %d baud and running program %s\n", file);
1133 printf_filtered ("\ton an H8/300 processor.\n");
1136 /* Copy LEN bytes of data from debugger memory at MYADDR
1137 to inferior's memory at MEMADDR. Returns errno value.
1138 * sb/sh instructions don't work on unaligned addresses, when TU=1.
1141 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
1142 at debugger address MYADDR. Returns errno value. */
1144 hms_read_inferior_memory (memaddr, myaddr, len)
1149 /* Align to nearest low 16 bits */
1153 CORE_ADDR start = memaddr & ~0xf;
1154 CORE_ADDR end = ((memaddr + len + 16) & ~0xf) - 1;
1157 CORE_ADDR start = memaddr;
1158 CORE_ADDR end = memaddr + len - 1;
1163 AAAA: XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX '................'
1164 012345678901234567890123456789012345678901234567890123456789012345
1174 sprintf (buffer, "m %4x %4x", start & 0xffff, end & 0xffff);
1175 hms_write_cr (buffer);
1176 /* drop the echo and newline*/
1177 for (i = 0; i < 13; i++)
1180 /* Grab the lines as they come out and fill the area */
1191 buffer[0] = readchar ();
1192 if (buffer[0] == 'M')
1194 for (i = 1; i < 66; i++)
1195 buffer[i] = readchar ();
1197 /* Now parse the line */
1199 addr = gethex (4, buffer, &ok);
1201 for (p = 0; p < 16; p += 2)
1203 byte[p] = gethex (2, buffer + idx, &ok);
1204 byte[p + 1] = gethex (2, buffer + idx + 2, &ok);
1209 for (p = 0; p < 16; p++)
1211 if (addr + p >= memaddr &&
1212 addr + p < memaddr + len)
1214 myaddr[(addr + p) - memaddr] = byte[p];
1226 /* This routine is run as a hook, just before the main command loop is
1227 entered. If gdb is configured for the H8, but has not had its
1228 target specified yet, this will loop prompting the user to do so.
1231 hms_before_main_loop ()
1235 extern FILE *instream;
1236 extern jmp_buf to_top_level;
1238 push_target (&hms_ops);
1241 #define MAX_BREAKS 16
1242 static int num_brkpts = 0;
1244 hms_insert_breakpoint (addr, save)
1246 char *save; /* Throw away, let hms save instructions */
1250 if (num_brkpts < MAX_BREAKS)
1255 sprintf (buffer, "b %x", addr & 0xffff);
1256 hms_write_cr (buffer);
1262 fprintf_filtered (stderr,
1263 "Too many break points, break point not installed\n");
1269 hms_remove_breakpoint (addr, save)
1271 char *save; /* Throw away, let hms save instructions */
1278 sprintf (buffer, "b - %x", addr & 0xffff);
1279 hms_write_cr (buffer);
1286 /* Clear the hmss notion of what the break points are */
1288 hms_clear_breakpoints ()
1293 hms_write_cr ("b -");
1301 hms_clear_breakpoints ();
1302 generic_mourn_inferior ();
1305 /* Put a command string, in args, out to the hms. The hms is assumed to
1306 be in raw mode, all writing/reading done through desc.
1307 Ouput from the hms is placed on the users terminal until the
1308 prompt from the hms is seen.
1309 FIXME: Can't handle commands that take input. */
1312 hms_com (args, fromtty)
1321 /* Clear all input so only command relative output is displayed */
1323 hms_write_cr (args);
1324 hms_write ("\030", 1);
1328 /* Define the target subroutine names */
1330 struct target_ops hms_ops =
1332 "hms", "Remote HMS monitor",
1333 "Use the H8 evaluation board running the HMS monitor connected\n\
1336 hms_open, hms_close,
1337 0, hms_detach, hms_resume, hms_wait, /* attach */
1338 hms_fetch_register, hms_store_register,
1339 hms_prepare_to_store,
1340 hms_xfer_inferior_memory,
1342 hms_insert_breakpoint, hms_remove_breakpoint, /* Breakpoints */
1343 0, 0, 0, 0, 0, /* Terminal handling */
1344 hms_kill, /* FIXME, kill */
1346 0, /* lookup_symbol */
1347 hms_create_inferior, /* create_inferior */
1348 hms_mourn, /* mourn_inferior FIXME */
1350 0, /* notice_signals */
1351 process_stratum, 0, /* next */
1352 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1353 0, 0, /* Section pointers */
1354 OPS_MAGIC, /* Always the last thing */
1361 printf_filtered ("Snoop disabled\n");
1363 printf_filtered ("Snoop enabled\n");
1372 dev_name = get_word (&s);
1385 int newrate = atoi (s);
1388 if (!serial_setbaudrate (newrate))
1389 error ("Can't use %d baud\n", newrate);
1391 printf_filtered ("Checking target is in sync\n");
1393 get_baudrate_right ();
1395 printf_filtered ("Sending commands to set target to %d\n",
1398 sprintf (buffer, "tm %d. N 8 1", baudrate);
1399 hms_write_cr (buffer);
1403 /***********************************************************************/
1406 _initialize_remote_hms ()
1408 add_target (&hms_ops);
1410 add_com ("hms <command>", class_obscure, hms_com,
1411 "Send a command to the HMS monitor.");
1412 add_com ("snoop", class_obscure, hms_quiet,
1413 "Show what commands are going to the monitor");
1415 add_com ("device", class_obscure, hms_device,
1416 "Set the terminal line for HMS communications");
1418 add_com ("speed", class_obscure, hms_speed,
1419 "Set the terminal line speed for HMS communications");