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. */
62 #define LINE_SIZE_POWER 4
63 #define LINE_SIZE (1<<LINE_SIZE_POWER) /* eg 1<<3 == 8 */
64 #define LINE_SIZE_MASK ((LINE_SIZE-1)) /* eg 7*2+1= 111*/
65 #define DCACHE_SIZE 64 /* Number of cache blocks */
66 #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);
223 /***********************************************************************
224 * I/O stuff stolen from remote-eb.c
225 ***********************************************************************/
227 static int timeout = 2;
229 static const char *dev_name;
232 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
233 hms_open knows that we don't have a file open when the program
242 error("remote device not open");
249 /* Read a character from the remote system, doing all the fancy
256 buf = serial_timedreadchar(timeout, &ok);
259 error ("Timeout reading from remote system.");
272 buf = serial_timedreadchar(timeout, &ok);
281 /* Keep discarding input from the remote system, until STRING is found.
282 Let the user break out immediately. */
293 if (readchar() == *p)
307 /* Keep discarding input until we see the hms prompt.
309 The convention for dealing with the prompt is that you
311 o *then* wait for the prompt.
313 Thus the last thing that a procedure does with the serial line
314 will be an expect_prompt(). Exception: hms_resume does not
315 wait for the prompt, because the terminal is being handed over
316 to the inferior. However, the next thing which happens after that
317 is a hms_wait which does wait for the prompt.
318 Note that this includes abnormal exit, e.g. error(). This is
319 necessary to prevent getting into states from which we can't
327 /* Get a hex digit from the remote system & return its value.
328 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
330 get_hex_digit (ignore_space)
337 if (ch >= '0' && ch <= '9')
339 else if (ch >= 'A' && ch <= 'F')
340 return ch - 'A' + 10;
341 else if (ch >= 'a' && ch <= 'f')
342 return ch - 'a' + 10;
343 else if (ch == ' ' && ignore_space)
348 error ("Invalid hex digit from remote system.");
353 /* Get a byte from hms_desc and put it in *BYT. Accept any number
361 val = get_hex_digit (1) << 4;
362 val |= get_hex_digit (0);
366 /* Read a 32-bit hex word from the hms, preceded by a space */
374 for (j = 0; j < 8; j++)
375 val = (val << 4) + get_hex_digit (j == 0);
378 /* Called when SIGALRM signal sent due to alarm() timeout. */
382 /* Number of SIGTRAPs we need to simulate. That is, the next
383 NEED_ARTIFICIAL_TRAP calls to hms_wait should just return
384 SIGTRAP without actually waiting for anything. */
386 static int need_artificial_trap = 0;
389 hms_kill(arg,from_tty)
399 * Download a file specified in 'args', to the hms.
402 hms_load(args,fromtty)
415 abfd = bfd_openr(args,"coff-h8300");
418 printf_filtered("Unable to open file %s\n", args);
422 if (bfd_check_format(abfd, bfd_object) ==0)
424 printf_filtered("File is not an object file\n");
429 while (s != (asection *)NULL)
431 if (s->flags & SEC_LOAD)
437 char *buffer = xmalloc(DELTA);
438 printf_filtered("%s\t: 0x%4x .. 0x%4x ",s->name, s->vma, s->vma + s->_raw_size);
439 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 hms_write_inferior_memory(s->vma + i, buffer, delta);
447 printf_filtered("*");
450 printf_filtered( "\n");
455 sprintf(buffer, "r PC=%x", abfd->start_address);
456 hms_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 hms_create_inferior (execfile, args, env)
472 error ("Can't pass arguments to remote hms process.");
474 if (execfile == 0 || exec_bfd == 0)
475 error ("No exec file specified");
477 entry_pt = (int) bfd_get_start_address (exec_bfd);
482 hms_clear_breakpoints();
483 init_wait_for_inferior ();
487 insert_breakpoints (); /* Needed to get correct instruction in cache */
488 proceed(entry_pt, -1, 0);
492 /* Open a connection to a remote debugger.
493 NAME is the filename used for communication, then a space,
501 while (*s && !isspace(*s))
506 static char *get_word(p)
521 while (*s && !isspace(*s))
527 copy = xmalloc(len+1);
528 memcpy(copy, word, len);
534 static int baudrate = 9600;
540 /* Put this port into NORMAL mode, send the 'normal' character */
542 hms_write("\001", 1); /* Control A */
543 hms_write("\r", 1); /* Cr */
547 serial_timedreadchar(timeout, &ok);
553 if (readchar_nofail() == 'r')
556 /* Not the right baudrate, or the board's not on */
562 if (!serial_setbaudrate(baudrate))
563 error("Can't set baudrate");
569 while (!is_baudrate_right())
571 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);
583 hms_open (name, from_tty)
597 if (name && strlen(name))
598 dev_name = strdup(name);
599 if (!serial_open(dev_name))
600 perror_with_name ((char *)dev_name);
606 get_baudrate_right();
608 /* Hello? Are you there? */
609 serial_write("\r",1);
612 /* Clear any break points */
613 hms_clear_breakpoints();
615 printf_filtered("Connected to remote H8/300 HMS system.\n");
618 /* Close out all files and local state before this target loses control. */
626 /* Clear any break points */
627 hms_clear_breakpoints();
629 /* Put this port back into REMOTE mode */
630 sleep(1); /* Let any output make it all the way back */
631 serial_write("R\r", 2);
636 /* Terminate the open connection to the remote debugger.
637 Use this when you want to detach and do something else
640 hms_detach (args,from_tty)
646 hms_clear_breakpoints();
649 pop_target(); /* calls hms_close to do the real work */
651 printf_filtered ("Ending remote %s debugging\n", target_shortname);
654 /* Tell the remote machine to resume. */
657 hms_resume (step, sig)
667 /* Force the next hms_wait to return a trap. Not doing anything
668 about I/O from the target means that the user has to type
669 "continue" to see any. FIXME, this should be fixed. */
670 need_artificial_trap = 1;
679 /* Wait until the remote machine stops, then return,
680 storing status in STATUS just as `wait' would. */
686 /* Strings to look for. '?' means match any single character.
687 Note that with the algorithm we use, the initial character
688 of the string cannot recur in the string, or we will not
689 find some cases of the string in the input. */
691 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. */
701 /* Current position in swallowed. */
702 char *swallowed_p = swallowed;
706 int old_timeout = timeout;
707 int old_immediate_quit = immediate_quit;
708 int swallowed_cr = 0;
710 WSETEXIT ((*status), 0);
712 if (need_artificial_trap != 0)
714 WSETSTOP ((*status), SIGTRAP);
715 need_artificial_trap--;
719 timeout = 99999; /* Don't time out -- user program is running. */
720 immediate_quit = 1; /* Helps ability to QUIT */
723 QUIT; /* Let user quit and leave process running */
739 if (ch == *ep || *ep == '?')
756 /* Print out any characters which have been swallowed. */
757 for (p = swallowed; p < swallowed_p; ++p)
759 swallowed_p = swallowed;
762 if ((ch != '\r' && ch != '\n') || swallowed_cr>10)
773 WSETSTOP ((*status), SIGTRAP);
778 WSETEXIT ((*status), 0);
781 timeout = old_timeout;
782 immediate_quit = old_immediate_quit;
786 /* Return the name of register number REGNO
787 in the form input and output by hms.
789 Returns a pointer to a static buffer containing the answer. */
794 static char *rn[NUM_REGS]= REGISTER_NAMES;
798 /* Read the remote registers. */
799 static int gethex(length, start, ok)
808 if (*start >='a' && *start <= 'f')
810 result += *start - 'a' + 10;
812 else if (*start >='A' && *start <= 'F')
814 result += *start - 'A' + 10;
816 else if (*start >='0' && *start <= '9')
818 result += *start - '0' ;
827 timed_read(buf, n, timeout)
838 if (c == 0) return i;
853 for (i = 0; i < l ; i++)
862 hms_write( s, strlen(s));
867 hms_fetch_register (dummy)
870 #define REGREPLY_SIZE 79
871 char linebuf[REGREPLY_SIZE+1];
876 REGISTER_TYPE reg[NUM_REGS];
884 s = timed_read(linebuf, REGREPLY_SIZE, 1);
887 linebuf[REGREPLY_SIZE] = 0;
889 if (linebuf[0] == 'r' &&
893 linebuf[75] == 'H' &&
894 linebuf[76] == 'M' &&
898 PC=XXXX CCR=XX:XXXXXXXX R0-R7= XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX
899 5436789012345678901234567890123456789012345678901234567890123456789012
904 reg[PC_REGNUM] = gethex(4,linebuf+6, &gottok);
905 reg[CCR_REGNUM] = gethex(2,linebuf+15, &gottok);
906 for (i = 0; i < 8; i++)
908 reg[i] = gethex(4, linebuf+34+5*i, &gottok);
913 for (i = 0; i < NUM_REGS; i++)
917 swapped[0] = (reg[i])>> 8;
919 supply_register (i, swapped);
924 /* Store register REGNO, or all if REGNO == -1.
925 Return errno value. */
927 hms_store_register (regno)
931 /* printf("hms_store_register() called.\n"); fflush(stdout); /* */
934 for (regno = 0; regno < NUM_REGS; regno ++)
936 hms_store_register(regno);
941 char *name = get_reg_name (regno);
943 sprintf(buffer,"r %s=%x", name, read_register(regno));
944 hms_write_cr(buffer);
951 /* Get ready to modify the registers array. On machines which store
952 individual registers, this doesn't need to do anything. On machines
953 which store all the registers in one fell swoop, this makes sure
954 that registers contains all the registers from the program being
958 hms_prepare_to_store ()
960 /* Do nothing, since we can store individual regs */
972 /* Read a word from remote address ADDR and return it.
973 * This goes through the data cache.
976 hms_fetch_word (addr)
979 return dcache_fetch (addr);
982 /* Write a word WORD into remote address ADDR.
983 This goes through the data cache. */
986 hms_store_word (addr, word)
990 dcache_poke (addr, word);
994 hms_xfer_inferior_memory(memaddr, myaddr, len, write, target)
999 struct target_ops *target; /* ignored */
1002 /* Round starting address down to longword boundary. */
1003 register CORE_ADDR addr;
1004 /* Round ending address up; get number of longwords that makes. */
1006 /* Allocate buffer of that many longwords. */
1007 register int *buffer ;
1010 addr = memaddr & - sizeof (int);
1011 count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
1014 buffer = (int *)alloca (count * sizeof (int));
1017 /* Fill start and end extra bytes of buffer with existing memory data. */
1019 if (addr != memaddr || len < (int)sizeof (int)) {
1020 /* Need part of initial word -- fetch it. */
1021 buffer[0] = hms_fetch_word (addr);
1024 if (count > 1) /* FIXME, avoid if even boundary */
1027 = hms_fetch_word (addr + (count - 1) * sizeof (int));
1030 /* Copy data to be written over corresponding part of buffer */
1032 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
1034 /* Write the entire buffer. */
1036 for (i = 0; i < count; i++, addr += sizeof (int))
1039 hms_store_word (addr, buffer[i]);
1050 /* Read all the longwords */
1051 for (i = 0; i < count; i++, addr += sizeof (int))
1054 buffer[i] = hms_fetch_word (addr);
1062 /* Copy appropriate bytes out of the buffer. */
1063 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
1071 hms_write_inferior_memory (memaddr, myaddr, len)
1073 unsigned char *myaddr;
1085 thisgo = len - done;
1086 if (thisgo > 20) thisgo = 20;
1088 sprintf(buffer,"M.B %4x =", memaddr+done);
1089 hms_write(buffer,10);
1090 for (idx = 0; idx < thisgo; idx++)
1093 sprintf(buf, "%2x ", myaddr[idx+done]);
1107 char *file = "nothing";
1109 file = bfd_get_filename(exec_bfd);
1113 printf_filtered("\tAttached to DOS asynctsr and running program %s\n",file);
1115 printf_filtered("\tAttached to %s at %d baud and running program %s\n",file);
1117 printf_filtered("\ton an H8/300 processor.\n");
1120 /* Copy LEN bytes of data from debugger memory at MYADDR
1121 to inferior's memory at MEMADDR. Returns errno value.
1122 * sb/sh instructions don't work on unaligned addresses, when TU=1.
1126 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
1127 at debugger address MYADDR. Returns errno value. */
1129 hms_read_inferior_memory(memaddr, myaddr, len)
1134 /* Align to nearest low 16 bits */
1138 CORE_ADDR start = memaddr & ~0xf;
1139 CORE_ADDR end = ((memaddr + len +16) & ~0xf) -1;
1141 CORE_ADDR start = memaddr;
1142 CORE_ADDR end = memaddr + len -1;
1147 AAAA: XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX '................'
1148 012345678901234567890123456789012345678901234567890123456789012345
1152 if (memaddr & 0xf) abort();
1153 if (len != 16) abort();
1155 sprintf(buffer, "m %4x %4x", start & 0xffff, end & 0xffff);
1156 hms_write_cr(buffer);
1157 /* drop the echo and newline*/
1158 for (i = 0; i < 13; i++)
1163 /* Grab the lines as they come out and fill the area */
1173 buffer[0] = readchar();
1174 if (buffer[0] == 'M')
1176 for (i = 1; i < 66; i++)
1177 buffer[i] = readchar();
1179 /* Now parse the line */
1181 addr = gethex(4, buffer, &ok);
1183 for (p = 0; p < 16; p+=2)
1185 byte[p] = gethex(2, buffer + idx, &ok);
1186 byte[p+1] = gethex(2, buffer+ idx + 2, &ok);
1192 for (p = 0; p<16;p++)
1194 if (addr + p >= memaddr &&
1195 addr + p < memaddr + len)
1197 myaddr[ (addr + p)-memaddr] = byte[p];
1208 /* This routine is run as a hook, just before the main command loop is
1209 entered. If gdb is configured for the H8, but has not had its
1210 target specified yet, this will loop prompting the user to do so.
1213 hms_before_main_loop ()
1217 extern FILE *instream;
1218 extern jmp_buf to_top_level;
1220 push_target (&hms_ops);
1224 #define MAX_BREAKS 16
1225 static int num_brkpts=0;
1227 hms_insert_breakpoint(addr, save)
1229 char *save; /* Throw away, let hms save instructions */
1233 if (num_brkpts < MAX_BREAKS)
1237 sprintf(buffer,"b %x", addr & 0xffff);
1238 hms_write_cr(buffer);
1244 fprintf_filtered(stderr,
1245 "Too many break points, break point not installed\n");
1252 hms_remove_breakpoint(addr, save)
1254 char *save; /* Throw away, let hms save instructions */
1261 sprintf(buffer,"b - %x", addr & 0xffff);
1262 hms_write_cr(buffer);
1269 /* Clear the hmss notion of what the break points are */
1271 hms_clear_breakpoints()
1275 hms_write_cr("b -");
1283 hms_clear_breakpoints();
1284 generic_mourn_inferior ();
1289 /* Put a command string, in args, out to the hms. The hms is assumed to
1290 be in raw mode, all writing/reading done through desc.
1291 Ouput from the hms is placed on the users terminal until the
1292 prompt from the hms is seen.
1293 FIXME: Can't handle commands that take input. */
1296 hms_com (args, fromtty)
1304 /* Clear all input so only command relative output is displayed */
1307 hms_write("\030",1);
1311 /* Define the target subroutine names */
1313 struct target_ops hms_ops = {
1314 "hms", "Remote HMS monitor",
1315 "Use the H8 evaluation board running the HMS monitor connected\n\
1318 hms_open, hms_close,
1319 0, hms_detach, hms_resume, hms_wait, /* attach */
1320 hms_fetch_register, hms_store_register,
1321 hms_prepare_to_store,
1322 hms_xfer_inferior_memory,
1324 hms_insert_breakpoint, hms_remove_breakpoint, /* Breakpoints */
1325 0, 0, 0, 0, 0, /* Terminal handling */
1326 hms_kill, /* FIXME, kill */
1328 0, /* lookup_symbol */
1329 hms_create_inferior, /* create_inferior */
1330 hms_mourn, /* mourn_inferior FIXME */
1332 process_stratum, 0, /* next */
1333 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1334 0,0, /* Section pointers */
1335 OPS_MAGIC, /* Always the last thing */
1343 printf_filtered("Snoop disabled\n");
1345 printf_filtered("Snoop enabled\n");
1354 dev_name = get_word(&s);
1368 int newrate = atoi(s);
1370 if (!serial_setbaudrate(newrate))
1371 error("Can't use %d baud\n", newrate);
1373 printf_filtered("Checking target is in sync\n");
1375 get_baudrate_right();
1377 printf_filtered("Sending commands to set target to %d\n",
1380 sprintf(buffer, "tm %d. N 8 1", baudrate);
1381 hms_write_cr(buffer);
1385 /***********************************************************************/
1388 _initialize_remote_hms ()
1390 add_target (&hms_ops);
1391 add_com ("hms <command>", class_obscure, hms_com,
1392 "Send a command to the HMS monitor.");
1393 add_com ("snoop", class_obscure, hms_quiet,
1394 "Show what commands are going to the monitor");
1396 add_com ("device", class_obscure, hms_device,
1397 "Set the terminal line for HMS communications");
1399 add_com ("speed", class_obscure, hms_speed,
1400 "Set the terminal line speed for HMS communications");
1402 dev_name = serial_default_name();